19 L2Histo::set(
int idx,
const char *tit,
int nbx,
int nby) {
20 if (!(idx>0 && nbx >0 && nby >0 && tit))
26 head.dataSize=head.nBin*
sizeof(int);
27 data=
new int [head.nBin];
28 memset(
data,0,head.dataSize);
30 strncpy(head.title,
"set(): HISTOGRAM IS BROKEN IN. CONTACT EXPERT.",mxTx);
34 head.nOver=head.nUnder=0;
40 head.dataSize=head.nBin*
sizeof(int);
41 data=
new int [head.nBin];
42 memset(
data,0,head.dataSize);
44 strncpy(head.title,tit,mxTx);
50 L2Histo::setTitle(
const char *tit){
54 strncpy(head.title,
"setTitle(): HISTOGRAM IS BROKEN. CONTACT EXPERT.",mxTx);
57 strncpy(head.title,tit,mxTx);
63 head.nOver=head.nUnder=-1;
78 head.nOver=head.nUnder=0;
79 if(head.dataSize>0) memset(
data,0,head.dataSize);
85 L2Histo::print(
int flag, FILE *fd){
87 if(head.nBinY>1) dim=2;
88 fprintf(fd,
" L2Histo::print h%d, %dD, tit='%s' head.nBin=%d nBinX=%d ver=%d\n", head.hid, dim, head.title, head.nBin, head.nBinX,head.ver);
92 for(i=0;i< head.nBin;i++) {
96 fprintf(fd,
" bin=%2d value=%4d sum=%d\n",i,
data[i],sum);
100 fprintf(fd,
" bin=%d ix=%d iy=%d value=%d \n",i,ix,iy,
data[i]);
104 fprintf(fd,
" Counts: inRange=%d nUnder=%d, nOver=%d\n",sum, head.nUnder, head.nOver);
111 L2Histo::printCSV( FILE *fd){
113 printf(
"printCSV() passed bad output file!\n");
116 fprintf(fd,
"#L2H%d,%d,%s,", head.hid, head.nBin, head.title);
118 fprintf(fd,
"ny=%d,CVS format not supported for 2D histos,\n",head.nBinY);
123 for(i=0;i< head.nBin;i++) fprintf(fd,
"%d,",
data[i]);
132 L2Histo::fill(
int bin){
136 if(bin<0) { head.nUnder++;
return;}
137 if(bin>=head.nBin) { head.nOver++;
return;}
145 L2Histo::fillW(
int bin,
int w){
150 if(bin<0) { head.nUnder+=w;
return;}
151 if(bin>=head.nBin) { head.nOver+=w;
return;}
158 L2Histo::fill(
int binX,
int binY){
163 if(binX<0 || binY <0) { head.nUnder++;
return;}
164 if(binX>=head.nBinX || binY>=head.nBinY) { head.nOver++;
return;}
165 data[binY*head.nBinX+binX]++;
171 L2Histo::fillW(
int binX,
int binY,
int w){
176 if(binX<0 || binY <0) { head.nUnder+=w;
return;}
177 if(binX>=head.nBinX || binY>=head.nBinY) { head.nOver+=w;
return;}
178 data[binY*head.nBinX+binX]+=w;
184 L2Histo::write(FILE *fd,
int dbg) {
186 if( head.nBin<=0)
return;
187 if(dbg) print(dbg-1);
192 for(i=0;i<
sizeof(L2Histo::Head);i++) fputc(c[i],fd);
196 for(i=0;i< head.dataSize;i++) fputc(c[i],fd);
204 L2Histo::read(FILE *fd,
int dbg) {
207 printf(
"L2Histo::read called with no file. Aborting\n");
216 for(i=0;i<
sizeof(L2Histo::Head);i++) {
218 if(i==0 && val==EOF)
return 1;
221 printf(
"L2Histo::read val==EOF. Aborting\n");
227 if (!( head.ver==version))
229 printf(
"L2Histo::read head.ver!=version. Aborting\n");
234 printf(
"L2Histo::read head.nBin<=0. Aborting\n");
237 if (!( head.dataSize== head.nBin*
sizeof(
int)))
239 printf(
"L2Histo::read head.dataSize!= head.nBin*sizeof(int). Aborting\n");
243 data=
new int [head.dataSize];
244 memset(
data,0, head.dataSize);
248 for(i=0;i< head.dataSize;i++) {
250 if(dbg)printf(
"i=%d val=%d\n",i,val);
251 if(i==0 && val==EOF)
return 1;
254 printf(
"L2Histo::read val==EOF. Aborting\n");
260 if(dbg) printf(
"got histo hid=%d , head.nBin=%d, tit='%s'\n", head.hid, head.nBin, head.title);
270 L2Histo::findMax(
int *iMax,
int *iFWHM) {
275 int maxVal=-1, maxJ=-1;
277 for(j=0; j<head.nBin;j++) {
279 if(
data[j]<=maxVal)
continue;
284 if(maxJ<0)
return false;
296 int halfMax=maxVal/2;
298 for(j=maxJ+1; j<head.nBin;j++) {
299 if(
data[j]>halfMax)
continue;
304 for(j=maxJ-1; j>=0;j--) {
305 if(
data[j]>halfMax)
continue;
309 if(j1<0 || j2<0)
return false;
320 L2Histo::findMean(
int *iMean,
int *iRMS) {
327 int totalVal=0, weightedVal=0, meanJ=-1, rms=-1;
329 for(j=0; j<head.nBin;j++) {
331 weightedVal+=
data[j]*j;
334 if (totalVal>0) meanJ=weightedVal/totalVal;
335 if(meanJ<0)
return false;
341 for(j=0; j<head.nBin;j++) {
343 weightedVal+=
data[j]*(j-meanJ)*(j-meanJ);
345 rms=weightedVal/totalVal;
354 L2Histo::printPed( FILE *fd,
int x0,
int x1,
char term){
356 fprintf(fd,
" printped NOT implemented for 2D histos: hid=%d tit=%s\n",head.hid, head.title);
360 for(j=0; j<head.nBin;j++) {
379 fprintf(fd,
"%c",y2c(val));
384 for(j=x1; j<head.nBin;j++) sum+=
data[j];
386 fprintf(fd,
"ov=%d %c",sum,term);
392 L2Histo::y2c(
float val){
393 float valLog=log(val);
400 k=
'a'+(int)(valLog-9);