146 {
147
148 long binsGiven, lowerLimitGiven, upperLimitGiven;
150 double *data;
151 double *hist;
152 double *indep;
153 double *overlap, *overlapHist;
154 double lowerLimit, upperLimit;
155 double givenLowerLimit, givenUpperLimit;
156 double range, binSize;
157 long bins;
158 char *dataColumn, *eventIDColumn, *overlapEventID;
159 SCANNED_ARG *scanned;
160 char *inputfile, *outputfile;
161 double dx;
162 long pointsBinned;
163 long normalizeMode, doSides, verbose = 0, readCode;
164 unsigned long pipeFlags, majorOrderFlag;
165 long eventIDIndex, eventIDType;
166 int64_t eventRefIDs = 0;
168
169 int64_t i, points, iEvent;
170 short columnMajorOrder = -1;
171
173
174 argc =
scanargs(&scanned, argc, argv);
175 if (argc < 3) {
176 fprintf(stderr, "Usage: %s\n", USAGE);
177 fputs(additional_help, stderr);
178 exit(EXIT_FAILURE);
179 }
180
181 hist = overlap = overlapHist = NULL;
182 binsGiven = lowerLimitGiven = upperLimitGiven = 0;
183 binSize = doSides = 0;
184 inputfile = outputfile = NULL;
185 dataColumn = eventIDColumn = overlapEventID = NULL;
186 normalizeMode = NORMALIZE_NO;
187 pipeFlags = 0;
188
189 for (i = 1; i < argc; i++) {
190 if (scanned[i].arg_type == OPTION) {
191 switch (
match_string(scanned[i].list[0], option, N_OPTIONS, 0)) {
192 case SET_MAJOR_ORDER:
193 majorOrderFlag = 0;
194 scanned[i].n_items--;
195 if (scanned[i].n_items > 0 && (!
scanItemList(&majorOrderFlag, scanned[i].list + 1, &scanned[i].n_items, 0,
"row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
"column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
196 SDDS_Bomb(
"invalid -majorOrder syntax/values");
197 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
198 columnMajorOrder = 1;
199 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
200 columnMajorOrder = 0;
201 break;
202 case SET_BINS:
203 if (binsGiven)
204 SDDS_Bomb(
"-bins specified more than once");
205 binsGiven = 1;
206 if (sscanf(scanned[i].list[1], "%ld", &bins) != 1 || bins <= 0)
208 break;
209 case SET_LOWERLIMIT:
210 if (lowerLimitGiven)
211 SDDS_Bomb(
"-lowerLimit specified more than once");
212 lowerLimitGiven = 1;
213 if (sscanf(scanned[i].list[1], "%lf", &givenLowerLimit) != 1)
214 SDDS_Bomb(
"invalid value for lowerLimit");
215 break;
216 case SET_UPPERLIMIT:
217 if (upperLimitGiven)
218 SDDS_Bomb(
"-upperLimit specified more than once");
219 upperLimitGiven = 1;
220 if (sscanf(scanned[i].list[1], "%lf", &givenUpperLimit) != 1)
221 SDDS_Bomb(
"invalid value for upperLimit");
222 break;
223 case SET_DATACOLUMN:
224 if (dataColumn)
225 SDDS_Bomb(
"-dataColumn specified more than once");
226 if (scanned[i].n_items != 2)
227 SDDS_Bomb(
"invalid -dataColumn syntax---supply name");
228 dataColumn = scanned[i].list[1];
229 break;
230 case SET_EVENTIDENTIFIER:
231 if (eventIDColumn)
232 SDDS_Bomb(
"-eventIdentifier specified more than once");
233 if (scanned[i].n_items != 2)
234 SDDS_Bomb(
"invalid -eventIdentifier syntax---supply name");
235 eventIDColumn = scanned[i].list[1];
236 break;
237 case SET_OVERLAPEVENT:
238 if (overlapEventID)
239 SDDS_Bomb(
"-overlapEvent specified more than once");
240 if (scanned[i].n_items != 2)
241 SDDS_Bomb(
"invalid -overlapEvent syntax---supply value");
242 overlapEventID = scanned[i].list[1];
243 if (!strlen(overlapEventID))
244 SDDS_Bomb(
"invalid -overlapEvent syntax---supply value");
245 break;
246 case SET_NORMALIZE:
247 if (scanned[i].n_items == 1)
248 normalizeMode = NORMALIZE_SUM;
249 else if (scanned[i].n_items != 2 || (normalizeMode =
match_string(scanned[i].list[1], normalize_option, N_NORMALIZE_OPTIONS, 0)) < 0)
251 break;
252 case SET_SIDES:
253 doSides = 1;
254 break;
255 case SET_BINSIZE:
256 if (sscanf(scanned[i].list[1], "%le", &binSize) != 1 || binSize <= 0)
258 break;
259 case SET_PIPE:
262 break;
263 default:
264 fprintf(stderr, "Error: option %s not recognized\n", scanned[i].list[0]);
265 exit(EXIT_FAILURE);
266 break;
267 }
268 } else {
269
270 if (!inputfile)
271 inputfile = scanned[i].list[0];
272 else if (!outputfile)
273 outputfile = scanned[i].list[0];
274 else
275 SDDS_Bomb(
"Too many filenames provided.");
276 }
277 }
278
279 processFilenames(
"sddseventhist", &inputfile, &outputfile, pipeFlags, 0, NULL);
280
281 if (binSize && binsGiven)
282 SDDS_Bomb(
"Specify either -binSize or -bins, not both.");
283 if (!binsGiven)
284 bins = 20;
285 if (!dataColumn)
286 SDDS_Bomb(
"-dataColumn must be specified.");
287 if (!eventIDColumn)
288 SDDS_Bomb(
"-eventIdentifier must be specified.");
289
290 if (!(indep =
SDDS_Malloc(
sizeof(*indep) * (bins + 2))) ||
291 !(hist =
SDDS_Malloc(
sizeof(*hist) * (bins + 2))) ||
292 !(overlap =
SDDS_Malloc(
sizeof(*overlap) * (bins + 2))) ||
293 !(overlapHist =
SDDS_Malloc(
sizeof(*overlapHist) * (bins + 2))))
295
303 SDDS_Bomb(
"Event ID column must be of string type.");
305 SDDS_Bomb(
"Data column must be of a numeric data type.");
306
307 data = NULL;
309 if (readCode > 1)
310 SDDS_Bomb(
"This program cannot process multi-page files.");
311 pointsBinned = 0;
314 if (!points)
316
317 if (!setupOutputFile(&outTable, outputfile, &inTable, inputfile, dataColumn, eventIDColumn, overlapEventID, &eventRefData, &eventRefIDs, &data, bins, binSize, normalizeMode, columnMajorOrder))
319
320 if (!lowerLimitGiven) {
321 lowerLimit = 0;
322 if (points)
323 lowerLimit = data[0];
324 for (i = 0; i < points; i++)
325 if (lowerLimit > data[i])
326 lowerLimit = data[i];
327 } else {
328 lowerLimit = givenLowerLimit;
329 }
330
331 if (!upperLimitGiven) {
332 upperLimit = 0;
333 if (points)
334 upperLimit = data[0];
335 for (i = 0; i < points; i++)
336 if (upperLimit < data[i])
337 upperLimit = data[i];
338 } else {
339 upperLimit = givenUpperLimit;
340 }
341
342 free(data);
343 data = NULL;
344 range = upperLimit - lowerLimit;
345 if (!lowerLimitGiven)
346 lowerLimit -= range * 1e-7;
347 if (!upperLimitGiven)
348 upperLimit += range * 1e-7;
349 if (upperLimit == lowerLimit) {
350 if (binSize) {
351 upperLimit += binSize / 2;
352 lowerLimit -= binSize / 2;
353 } else if (fabs(upperLimit) < sqrt(DBL_MIN)) {
354 upperLimit = sqrt(DBL_MIN);
355 lowerLimit = -sqrt(DBL_MIN);
356 } else {
357 upperLimit += upperLimit * (1 + 2 * DBL_EPSILON);
358 lowerLimit -= upperLimit * (1 - 2 * DBL_EPSILON);
359 }
360 }
361 dx = (upperLimit - lowerLimit) / bins;
362
363 if (binSize) {
364 double middle;
365 range = ((range / binSize) + 1) * binSize;
366 middle = (lowerLimit + upperLimit) / 2;
367 lowerLimit = middle - range / 2;
368 upperLimit = middle + range / 2;
369 dx = binSize;
370 bins = range / binSize + 0.5;
371 if (bins < 1 && !doSides)
372 bins = 2;
373 if (!(indep =
SDDS_Realloc(indep,
sizeof(*indep) * (bins + 2))) ||
374 !(hist =
SDDS_Realloc(hist,
sizeof(*hist) * (bins + 2))) ||
375 !(overlap =
SDDS_Realloc(overlap,
sizeof(*overlap) * (bins + 2))) ||
376 !(overlapHist =
SDDS_Realloc(overlapHist,
sizeof(*overlapHist) * (bins + 2))))
378 }
379
380 for (i = -1; i < bins + 1; i++) {
381 indep[i + 1] = (i + 0.5) * dx + lowerLimit;
382 }
383 if (!
SDDS_StartPage(&outTable, points ? (doSides ? bins + 2 : bins) : 0) ||
385 (points && (!
SDDS_SetColumnFromDoubles(&outTable, SDDS_SET_BY_NAME, indep + (doSides ? 0 : 1), doSides ? bins + 2 : bins, dataColumn))))
387
388 if (overlapEventID) {
389 for (iEvent = 0; iEvent < eventRefIDs; iEvent++) {
390 if (strcmp(eventRefData[iEvent].string, overlapEventID) == 0)
391 break;
392 }
393 if (iEvent == eventRefIDs)
394 SDDS_Bomb(
"Cannot create overlap as the specified overlap event is not present.");
395 makeEventHistogram(overlapHist, bins, lowerLimit, dx, eventRefData + iEvent);
396 }
397
398 for (iEvent = pointsBinned = 0; iEvent < eventRefIDs; iEvent++) {
399 pointsBinned += makeEventHistogram(hist, bins, lowerLimit, dx, eventRefData + iEvent);
400 if (normalizeMode != NORMALIZE_NO) {
401 double norm = 0;
402 switch (normalizeMode) {
403 case NORMALIZE_PEAK:
405 break;
406 case NORMALIZE_AREA:
407 case NORMALIZE_SUM:
408 for (i = 0; i < bins; i++)
409 norm += hist[i];
410 if (normalizeMode == NORMALIZE_AREA)
411 norm *= dx;
412 break;
413 default:
414 SDDS_Bomb(
"Invalid normalization mode.");
415 break;
416 }
417 if (norm)
418 for (i = 0; i < bins; i++)
419 hist[i] /= norm;
420 }
421
422 if (!
SDDS_SetColumnFromDoubles(&outTable, SDDS_SET_BY_INDEX, hist + (doSides ? 0 : 1), doSides ? bins + 2 : bins, eventRefData[iEvent].histogramIndex))
424 if (overlapEventID) {
425 makeEventOverlap(overlap, hist, overlapHist, bins + 2);
426 if (!
SDDS_SetColumnFromDoubles(&outTable, SDDS_SET_BY_INDEX, overlap + (doSides ? 0 : 1), doSides ? bins + 2 : bins, eventRefData[iEvent].overlapIndex))
428 }
429 free(eventRefData[iEvent].data);
430 }
431
433 "sddseventhistBins", bins,
434 "sddseventhistBinSize", dx,
435 "sddseventhistPointsBinned", pointsBinned,
436 NULL))
438
439 if (verbose)
440 fprintf(stderr, "%ld points of %" PRId64 " from page %ld histogrammed in %ld bins\n", pointsBinned, points, readCode, bins);
441
444 free(eventRefData);
445 }
446
449 return EXIT_FAILURE;
450 }
453 return EXIT_FAILURE;
454 }
455
456 free(hist);
457 free(overlapHist);
458 free(overlap);
459 free(indep);
460 return EXIT_SUCCESS;
461}
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
int32_t SDDS_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_GetNamedColumnType(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the data type of a column in the SDDS dataset by its name.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void * SDDS_Malloc(size_t size)
Allocates memory of a specified size.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_NUMERIC_TYPE(type)
Checks if the given type identifier corresponds to any numeric type.
double max_in_array(double *array, long n)
Finds the maximum value in an array of doubles.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.