StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
oflPedStat.cxx
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6 
7 #include <TFile.h>
8 #include <TString.h>
9 #include <TF1.h>
10 #include <TH1F.h>
11 #include <TCanvas.h>
12 
13 #include "oflPedStat.h"
14 
15 ClassImp(oflPedStat)
16 //=========================================
17 //=========================================
19  //not using constructor at this time
20 
21 }
22 
23 
24 //=========================================
25 //=========================================
26 int
27 oflPedStat::initRun(int index,TFile *f,TFile *fraw,FILE *fpout,FILE *fplog, int rNum)
28 {
29  mindex = index;
30  mfd=f;
31  mfdraw=fraw;
32  mfpout=fpout;
33  mfplog=fplog;
34  mrNum=rNum;
35 
36  return 0;
37 }
38 
39 
40 //=========================================
41 //=========================================
42 int
43 oflPedStat::procDetector(char *name,float maxPedPos, float minPedPos, float maxSig, float minSig, float deadentries, int msk)
44 {
45  int nfail=0;
46  int nstat=0;
47  int nStuckBit=0;
48 
49  //fail edge strips of mapmt
50  if(!strcmp(name+4,"288"))
51  { fprintf(mfplog,"%s is an edge strip\n",name);
52  nfail=1;
53  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
54  return -1;
55  }
56  if((strncmp(name,"a03U",4)==0 || strncmp(name,"a09U",4)==0) && atoi(name+4)>283 )
57  { fprintf(mfplog,"%s is an edge strip\n",name);
58  nfail=1;
59  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
60  return -1;
61  }
62  if((strncmp(name,"a04V",4)==0 || strncmp(name,"a10V",4)==0) && atoi(name+4)> 283 )
63  { fprintf(mfplog,"%s is an edge strip\n",name);
64  nfail=1;
65  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
66  return -1;
67  }
68 
69  //call histograms and fits
70  TH1F *h = (TH1F*)mfd ->Get(name);
71  TH1F *hraw = (TH1F*)mfdraw ->Get(name);
72  if(h==0 || hraw==0){
73  fprintf(mfplog,"individual channel %s not found\n",name);
74  nfail=1;
75  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
76  return -1;
77  }
78  TF1 *fn = h->GetFunction("fPeak");
79  if(fn==0){
80  nfail=1;
81  fprintf(mfplog,"%s has no fit parameters\n",name);
82  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
83  return -1;
84  }
85 
86  //sort by crate/chan
87  char title[100],crate[4],txt[3]; //int chan=-1;
88  sprintf(title,"%s",h->GetTitle()); //sprintf(title2,"%s",h->GetTitle());
89  if(strncmp(title+35,"tube=",5)==0 && name[3]!='T') {
90  sprintf(txt,"%c%c%c",title[29],title[30],title[31]); //chan=atoi(txt);
91  sprintf(crate,"%c%c%c%c",title[40],title[41],title[42],title[43]);
92  }
93  //printf("name=%s crate=%s chan=%d\n",name,crate,chan);
94 
95 
96  // place hardcoded known problem channels here
97  // for example, a hot tower known to be bad for a whole year can be set here
98 
99  if(!strcmp(name,"a12TC05")) //tube has high ped always set status flag
100  {
101  fprintf(mfplog,"%s masked out of trigger for whole run\n",name);
102  nstat+=12;
103  }
104 
105 
106  int entries=(int)h->GetEntries();
107  int integral=(int)h->Integral();
108  //printf("entries %d integral %d\n",entries,integral);
109 
110  //daq file # entries is wrong since it's done with weighting
111  if(entries != integral)
112  entries = integral;
113 
114  int nb=h->GetNbinsX();
115 
116  float *x = (float*)malloc(nb*sizeof(float));
117  x =(float*) h->GetArray();
118 
119  float ymax=0;
120  int i,jfind=-1;
121  float sum = 0.0;
122  for(i=0;i<nb;i++)
123  { if (ymax < x[i])
124  { ymax=x[i];
125  jfind=i;
126  }
127  sum+=x[i];
128  }
129  if (ymax == sum)
130  { fprintf(mfplog,"%s fails- all entries %.0f in one channel\n",name,ymax);
131  nfail=1;
132  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
133  return -1;
134  }
135  if(sum < 0.90*entries)
136  {
137  fprintf(mfplog,"%s fails needed events - %.0f of %d\n",name,sum,entries);
138  nfail=1;
139  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
140  return -1;
141  }
142 
143  float x0=h->GetBinCenter(jfind);
144  float ped = fn->GetParameter(1);
145  float sig = fn->GetParameter(2);
146 
147  //overflow test (for dead tower with counts in overflow bin)
148  double overflow=0;
149  double zerobinContent=0;
150  int zerobin=h->GetXaxis()->FindBin(0.0);
151  zerobinContent = h->GetBinContent(zerobin);
152 
153  if(name[3]=='T'){ //test for towers
154  if(zerobinContent > 0.001*entries) {
155  nfail=1;
156  printf("%s fails due to ADC=0 counts = %f\n",name,zerobinContent);
157  fprintf(mfplog,"%s has to many ADC=0 counts\n",name);
158  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
159  return -1;
160  }
161  overflow = hraw->Integral(ped+100,5000);
162  if(overflow>0.001*entries) {
163  nfail=1;
164  printf("%s fails due to overflow counts = %f\n",name,overflow);
165  fprintf(mfplog,"%s has to many overflow counts\n",name);
166  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
167  return -1;
168  }
169  }
170  else { //test for mapmt's
171  overflow = hraw->Integral(ped+200,5000);
172  if(overflow>0.01*entries) {
173  nfail=1;
174  printf("%s fails due to overflow counts = %f\n",name,overflow);
175  fprintf(mfplog,"%s has to many overflow counts\n",name);
176  fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);
177  return -1;
178  }
179  }
180 
181  //stuck bit test
182  int adc;
183  int nonzerohits=0;
184  int bitcompare=1+2+4+8;
185  int biton=bitcompare;
186  int bitoff=0;
187 
188  adc = (int)ped + 1;
189  for (int k=0; k < (hraw->GetNbinsX()); k++)
190  {
191  if (hraw ->GetBinContent(k)>0)
192  {
193  bitoff = bitoff | adc;
194  biton = biton & adc;
195  nonzerohits += 1;
196  }
197  adc += 1;
198  }
199 
200  if (name[3]=='T') { //only check for stuck bits on towers
201  if ( (bitoff & bitcompare) != bitcompare && nonzerohits >= 8)
202  {
203  printf("%s bit stuck off \n",name);
204  fprintf(mfplog,"%s has stuck bits\n",name);
205  nstat+=2;
206  nStuckBit=1;
207  }
208  else if(biton && nonzerohits >= 5 && bitoff>70) //not dead towers
209  {
210  printf("%s bit stuck on \n",name);
211  fprintf(mfplog,"%s has stuck bits\n",name);
212  nstat+=2;
213  nStuckBit=1;
214  }
215  }
216  //make sure we catch known stuck bits
217  if((!strcmp(name,"a04TB07")||!strcmp(name,"a06TA07")||!strcmp(name,"a08TB07")) && nStuckBit==0){
218  printf("%s missed stuck bit, but hardcoded fix\n",name);
219  fprintf(mfplog,"%s missed stuck bit, but hardcoded\n",name);
220  nstat+=2;
221  nStuckBit=1;
222  }
223 
224  //other ped QA
225  if(fabs(x0-ped)> 1.5 && !nStuckBit){
226  fprintf(mfplog,"%s fails, diff of x0=%.0f and ped =%.1f > 1.5\n",name,x0,ped);
227  nfail=1;
228  }
229  if(ped <= 0.0){
230  fprintf(mfplog,"%s fails, ped <= 0\n",name);
231  nfail=1;
232  }
233  if(ped >0 && ped < minPedPos){
234  fprintf(mfplog,"%s fails,%.4f, ped too low\n",name,ped);
235  nfail=1;
236  }
237  if(ped > maxPedPos) {
238  fprintf(mfplog,"%s,%.4f, ped too high\n",name,ped);
239  nstat+=16;
240  }
241 
242  if (sig < minSig && !nStuckBit){
243  fprintf(mfplog,"%s, %.4f, sig too low\n",name,sig);
244  nstat+=2;
245  }
246  // wide ped set status bit 2^7
247  if (sig > maxSig){
248  fprintf(mfplog,"%s, %.4f, sig too high\n",name,sig);
249  nstat+=128;
250  }
251 
252  //dead channel test
253  int start5sig=jfind -(int)(5.0*sig);
254  int end5sig = jfind +(int)(5.0*sig);
255  float sum2=0.0;
256  for(i=start5sig; i<=end5sig; i++)
257  { sum2+=x[i];
258  }
259  if(sum2 >= deadentries*entries && !nStuckBit){
260  fprintf(mfplog,"%s ped=%.3f x0=%.0f sig=%.3f sum2=%.0f start5sig=%d end5sig=%d jfind=%d\n",name,ped,x0,sig,sum2,start5sig,end5sig,jfind);
261  fprintf(mfplog,"%s is a dead channel\n",name);
262  nfail=1;
263  nstat+=1;
264  }
265 
266  //write to errs file
267  if(nfail > 0 || nstat > 0)
268  { fprintf(mfpout,"%s 0x%04x 0x%04x\n",name,nfail,nstat);}
269 
270 
271  return 0;
272 }
273 
274 
275