/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ /* Michael Borland, 1999 $Log: sddseventhist.c,v $ Revision 1.8 2006/12/14 22:21:58 soliday Updated a bunch of programs because SDDS_SaveLayout is now called by SDDS_WriteLayout and it is no longer required to be called directly. Also the AutoCheckMode is turned off by default now so I removed calls to SDDS_SetAutoCheckMode that would attempt to turn it off. It is now up to the programmer to turn it on in new programs until debugging is completed and then remove the call to SDDS_SetAutoCheckMode. Revision 1.7 2005/11/15 00:06:06 jiaox Fixed a bug that normalize option does not work. Revision 1.6 2002/08/14 17:12:45 soliday Added Open License Revision 1.5 2001/01/10 19:35:37 soliday Standardized usage message. Revision 1.4 2000/12/07 23:26:10 borland Fixed a bug in counting events that occured when a single event sorted to the end of the list. Revision 1.3 1999/05/25 19:09:25 soliday Removed compiler warning on linux. Revision 1.2 1999/04/17 01:37:53 borland Much faster version that uses sorted event lists. Revision 1.1 1999/04/16 14:03:33 borland First version in repository. */ #include "mdb.h" #include "scan.h" #include "SDDS.h" #define SET_BINS 0 #define SET_LOWERLIMIT 1 #define SET_UPPERLIMIT 2 #define SET_DATACOLUMN 3 #define SET_BINSIZE 4 #define SET_NORMALIZE 5 #define SET_SIDES 6 #define SET_PIPE 7 #define SET_EVENTIDENTIFIER 8 #define SET_OVERLAPEVENT 9 #define N_OPTIONS 10 char *option[N_OPTIONS] = { "bins", "lowerlimit", "upperlimit", "datacolumn", "sizeofbins", "normalize", "sides", "pipe", "eventidentifier", "overlapevent", }; char *USAGE="sddseventhist [] []\n\ [-pipe=[input][,output]]\n\ -dataColumn= -eventIdentifier= [-overlapEvent=]\n\ [{-bins= | -sizeOfBins=}] [-lowerLimit=] [-upperLimit=]\n\ [-sides] [-normalize[={sum|area|peak}]] \n"; static char *additional_help = "\n\ dataColumn give the name of column to histogram.\n\ eventIdentifier give the name of the column to use in identifying events.\n\ There will be one histogram for each unique value in this\n\ column. The column must contain string or integer data;\n\ if string data, the values must be allowed SDDS column names.\n\ overlapEvent If given, the histograms for all events are multiplied bin-by-bin\n\ with the histogram for the named event.\n\ bins number of bins for histogram.\n\ sizeOfBins size of bins for histogram.\n\ lowerLimit lower limit of histogram.\n\ upperLimit upper limit of histogram.\n\ normalize normalize the histogram, as specified.\n\ sides puts sides on the histogram, down to zero level.\n\ Program by Michael Borland. (This is version 1, February 1999.)\n"; #define NORMALIZE_PEAK 0 #define NORMALIZE_AREA 1 #define NORMALIZE_SUM 2 #define NORMALIZE_NO 3 #define N_NORMALIZE_OPTIONS 4 char *normalize_option[N_NORMALIZE_OPTIONS] = { "peak", "area", "sum", "no" } ; typedef struct { char *string; long events; double *data; /* Indices in output file of histogram and overlap */ long histogramIndex, overlapIndex; } EVENT_DATA; typedef struct { char *string; double data; } EVENT_PAIR; static long setupOutputFile(SDDS_DATASET *outTable, char *outputfile, SDDS_DATASET *inTable, char *inputfile, char *dataColumn, char *eventIDColumn, char *overlapEventID, EVENT_DATA **eventDataRet, long *eventIDsRet, double **dataRet, long bins, double binSize, long normalizeMode); long makeEventHistogram(double *hist, long bins, double lowerLimit, double dx, EVENT_DATA *eventRefData); void makeEventOverlap(double *overlap, double *hist, double *overlapHist, long bins); int event_cmpasc(const void *ep1, const void *ep2); int main(int argc, char **argv) { /* next line defines flags to keep track of what is set in command line: */ long binsGiven, lowerLimitGiven, upperLimitGiven; SDDS_DATASET inTable, outTable; double *data; /* pointer to the array to histogram */ double *hist, *hist1; /* to store the histogram */ double *indep; /* to store values of bin centers */ double *overlap, *overlap1, *overlapHist; double lowerLimit, upperLimit; /* lower and upper limits in histogram */ double givenLowerLimit, givenUpperLimit; /* lower and upper limits in histogram */ double range, binSize; long bins; /* number of bins in the histogram */ char *dataColumn, *eventIDColumn, *overlapEventID; SCANNED_ARG *scanned; /* scanned argument structure */ char *inputfile, *outputfile; /* array of input files, output file */ double dx; /* spacing of bins in histogram */ long pointsBinned; /* number of points that are in histogram */ long normalizeMode, doSides, verbose=0, readCode; unsigned long pipeFlags; long eventIDIndex, eventIDType, eventRefIDs; EVENT_DATA *eventRefData; char **eventID; long i, iEvent, firstData, points; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<3) { fprintf(stderr, "usage: %s\n", USAGE); fputs(additional_help, stderr); exit(1); } hist = overlap = overlapHist = NULL; binsGiven = lowerLimitGiven = upperLimitGiven = 0; binSize = doSides = 0; inputfile = outputfile = NULL; dataColumn = eventIDColumn = overlapEventID = NULL; normalizeMode = NORMALIZE_NO; pipeFlags = 0; for (i=1; i0) { if (readCode>1) SDDS_Bomb("This program cannot process multiple page files"); pointsBinned = 0; if ((points = SDDS_CountRowsOfInterest(&inTable))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!points) SDDS_Bomb("No data in file"); if (!(data = SDDS_GetColumnInDoubles(&inTable, dataColumn)) || !(eventID = SDDS_GetColumn(&inTable, eventIDColumn))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!setupOutputFile(&outTable, outputfile, &inTable, inputfile, dataColumn, eventIDColumn, overlapEventID, &eventRefData, &eventRefIDs, &data, bins, binSize, normalizeMode)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!lowerLimitGiven) { lowerLimit = 0; if (points) lowerLimit = data[0]; for (i=0; idata[i]) lowerLimit = data[i]; } else lowerLimit = givenLowerLimit; if (!upperLimitGiven) { upperLimit = 0; if (points) upperLimit = data[0]; for (i=0; ievents; iRow++) { iBin = (eventRefData->data[iRow]-lowerLimit)/dx; if (iBin>=0 || iBinstring, ev2->string))) return comp; return ev1->data>ev2->data; }