StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EEpixPed.cxx
1 // \class EEpixPed
2 // \author Jan Balewski
3 // \edited Justin Stevens
4 
5 #include <stdio.h>
6 #include <assert.h>
7 #include <string.h>
8 
9 
10 #include <TH1.h>
11 #include <TF1.h>
12 #include <TFile.h>
13 
14 #include <TCanvas.h>
15 
16 #include "StEEmcUtil/EEfeeRaw/EEdims.h"
17 #include "EEpixPed.h"
18 
19 ClassImp(EEpixPed)
20 
21 //-------------------------------------------
22 //-------------------------------------------
23 EEpixPed::EEpixPed(TString fname) {
24  tFd=new TFile(fname);
25  if(tFd==0 || !tFd->IsOpen()) {
26  printf("file not opened, Ignore\n");
27  return;
28  }
29  //t-d>ls();
30  printf("file '%s' opened\n",fname.Data());
31  HList=new TObjArray(0);
32 
33 }
34 
35 //-----------------------------------------
36 //-------------------------------------------
37 void EEpixPed::findTowerHisto() {
38  assert(tFd->IsOpen());
39  //printf("AA c_x1=%d c_x2=%d cMinInt=%d c_xFit=%d\n",c_x1,c_x2,c_minInt,c_xFit);
40  // assert(1==2);
41  int ntot_hist=0;
42  int i,j,k;
43  for(i=0;i<MaxSectors ;i++) { // sectors
44  int n_hist=0;
45  for(j=0;j<MaxSubSec;j++) { // subsectors
46  for(k=0;k<MaxEtaBins;k++) { // pseudorapidity
47  char tt1[100];
48  sprintf(tt1,"a%2.2dT%c%2.2d",i+1,'A'+j,k+1);
49  //printf("tryJ2 %s ...\n",tt1);
50  TH1F* h= (TH1F*) tFd->Get(tt1);
51  if(h==0) {
52  //printf("histo zero !!! \n");
53  continue;
54  }
55  //printf("S %s nEnt=%f cMinInt=%d\n",tt1, h->GetEntries(),c_minInt);
56  // h->Draw();
57  if (h->GetEntries() < c_minInt) {
58  //printf("Not reaching bottom line !!! \n");
59  continue;
60  }
61  // 3 channels with known (unfixable) stuck bits
62  bool A=strstr(tt1,"a04TB07");
63  bool B=strstr(tt1,"a06TA07");
64  bool C=strstr(tt1,"a08TB07");
65  if (A||B||C){
66  h->Rebin(4);
67  }
68  HList->Add(h);
69  n_hist++;
70  // break;
71  }
72  // break;
73  }
74  printf("sector %d found histo %d\n ",i+1,n_hist);
75  ntot_hist+=n_hist;
76  n_hist=0;
77  }
78  printf("\n");
79 
80  printf("%s() Tower histo initialized, %d found\n",GetName(),ntot_hist);
81 
82 }
83 
84 //-----------------------------------------
85 //-------------------------------------------
86 void EEpixPed::findMapmtHisto() {
87  assert(tFd->IsOpen());
88  int ntot_hist=0;
89  int i=0,ist;
90  int uv;
91 
92  for(i=0;i<MaxSectors ;i++) { // sectors
93  int secID=i+1;
94  printf("searching MAMT histsos for sect %d\n",secID);
95  int n_hist=0;
96  for(uv=0;uv<MaxSmdPlains;uv++){
97  for(ist=0;ist<MaxSmdStrips;ist++) {
98  char tt1[100];
99  sprintf(tt1,"a%2.2d%c%3.3d",secID,'U'+uv,ist+1);
100  //printf("try %s ... \n",tt1);
101  TH1F* h= (TH1F*) tFd->Get(tt1);
102  if(h==0){
103  // printf("histo zero!!!\n");
104  continue;
105  }
106  //printf("S %s %f\n",tt1, h->GetEntries());
107 
108  //if (h->GetEntries() < c_minInt) continue;
109  HList->Add(h);
110  n_hist++;
111  //break;
112  }
113 
114  }
115 
116  // pre/post
117  int j,k;
118  for(j=0;j<MaxSubSec;j++) { // subsectors
119  for(k=0;k<MaxEtaBins;k++) { // pseudorapidity
120  char tt1[100];
121  int l;
122  char preL[4]="PQR";
123  for(l=0;l<3;l++) {
124  sprintf(tt1,"a%2.2d%c%c%2.2d",i+1,preL[l],'A'+j,k+1);
125  TH1F* h= (TH1F*) tFd->Get(tt1);
126  if(h==0) continue;
127  //printf("S %s %f\n",tt1, h->GetEntries());
128  //if (h->GetEntries() < c_minInt) continue;
129  HList->Add(h);
130  n_hist++;
131  }
132  //break;
133  }
134  }
135 
136  printf("sector %d found histo %d \n ",i+1,n_hist);
137  ntot_hist+=n_hist;
138  n_hist=0;
139  }
140  printf("\n");
141 
142  printf("%s() MAPMT histo initialized, %d found\n",GetName(),ntot_hist);
143 
144 }
145 
146 //-------------------------------------------
147 //-------------------------------------------
148 void EEpixPed::fitHisto(TString fPath){
149  printf("Find pedestal within ADC limits [%d,%d], nMin>%d max+/-%d\n",c_x1,c_x2,c_minInt,c_xFit);
150  assert(c_x1>0);
151  assert(c_x2>c_x1);
152  assert(c_x2<4096);
153  assert(c_xFit>0);
154  static int xfile= 0;
155  FILE *fout;
156  TString ouT=fPath+"T.log";
157  TString ouM=fPath+"Mpt.log";
158  printf("Achtung!!! - static variable xfile = %d\n",xfile);
159  printf(" outT='%s' outM='%s'\n",ouT.Data(), ouM.Data());
160 
161  if(xfile % 2 == 0)
162  { fout=fopen(ouT.Data(),"w");}
163  else
164  { fout=fopen(ouM.Data(),"w");}
165  assert(fout);
166 
167  // printf("name Nentries chi2/ndf gaus_cons gaus_mean gaus_sig sig_con sig_mean sig_sig \n");
168  fprintf( fout,"name QA[ILHNWX] Nintegr chi2/ndf amplitude mean sigma erAmpl erMean erSigma\n");
169 
170  TCanvas *c=new TCanvas(); c=c;// just to get nice printout
171 
172  TF1* fit = new TF1("fPeak","gaus");
173  fit->SetLineColor(kGreen);
174 
175  //............................... LOOP over histo .........
176  int ja;
177  for(ja=0;ja<HList->GetEntries();ja++) {
178  TH1F* h=(TH1F*)HList->UncheckedAt(ja);
179  assert(h);
180  int nb=h->GetNbinsX();
181  h->SetAxisRange(c_x1,c_x2);
182  // printf("F: %s -->Int=%.1f Ent=%1.f\n",h->GetName(), h->Integral(),h->GetEntries() );
183 
184  if (h->Integral() < c_minInt) {
185  fprintf(fout,"%s %8.0f failed\n",h->GetName(), h->Integral());
186  continue;
187  }
188  //this histogram has some "reasonable number of entries"
189 
190  // localize pedestal peak
191  float *x=h->GetArray();
192 
193  float ym=0,ymax=0;
194  int i;
195  int j=-1,jfind=-1;
196 
197  for(i=c_x1;i<=c_x2;i++){
198  if(ym>x[i]) continue;
199  ym=x[i];
200  j=i;
201  }
202 
203  float x0=h->GetBinCenter(j);
204 
205  float x1=x0-c_xFit;
206  if(x1<c_x1) x1=c_x1;
207  float x2=x0+c_xFit;
208  if(x2>c_x2) x2=c_x2;
209 
210  for(i=0;i<=nb;i++)
211  { if(ymax < x[i])
212  { ymax = x[i];
213  jfind= i; }
214  }
215  float sum = 0;
216  for(i=jfind-5;i<=jfind+5;i++){
217  sum +=x[i];
218  }
219 
220  //h->Fit(fit,"OQN+","",x1,x2); // fit hist with gaussian
221  //h->Fit(fit,"OQ+W","",x1,x2); // fit hist with gaussian
222  // fit->Print();
223  bool A=strstr(h->GetName(),"a04TB07");
224  bool B=strstr(h->GetName(),"a06TA07");
225  bool C=strstr(h->GetName(),"a08TB07");
226  if (A||B||C){
227  //printf("\n\n Target chan=%s\n\n", h->GetName());
228  //printf("x1=%f, x2=%f\n",x1, x2);
229  //printf("x0=%f, j=%d\n",x0, j);
230  x1=0;
231  x2=60;
232  //h->Fit(fit,"O+W","",x1,x2);
233  }
234  h->Fit(fit,"OQ+W","",x1,x2);
235 
236  char qaString[7]="......";
237  if(h->Integral()<qa_minInt) qaString[0]='I';
238  if(fit->GetParameter(1)<qa_pedLow) qaString[1]='L';
239  if(fit->GetParameter(1)>qa_pedHigh) qaString[2]='H';
240  if(fit->GetParameter(2)<qa_pedSigMin) qaString[3]='N';
241  if(fit->GetParameter(2)>qa_pedSigMax) qaString[4]='W';
242 
243  fprintf(fout,"%s %6s %8.0f %8.3f %9.3f %9.3f %8.3f %7.3f %7.3f %7.3f \n",
244  h->GetName(),
245  qaString,
246  h->Integral(),
247  fit->GetChisquare()/fit->GetNDF(),
248  fit->GetParameter(0), // amplitude
249  fit->GetParameter(1), // pedestal centroid
250  fit->GetParameter(2), // sigma of gaussian
251  fit->GetParError(0), // error in amplitude
252  fit->GetParError(1), // error in centroid
253  fit->GetParError(2) // error in sigma
254  //sum
255  );
256 
257  // h->Print();
258  // fit->Print();
259  // break;
260  }// end of loop over histos
261  fclose(fout);
262  xfile++;
263 }
264 
265 //-------------------------------------------
266 //-------------------------------------------
267 void EEpixPed:: savePedTable( char *mode, TString fname){
268  //....write pedestal values to ped.sector files
269  printf(" write file of pedestal values to '%s'\n",fname.Data());
270  int secID;
271  for(secID=1;secID<=MaxSectors ;secID++) { // sectors
272  char txt[100];
273  sprintf(txt,"%s.sector%2.2d",fname.Data(),secID);
274  printf("%s ....",txt);
275  FILE *fd = fopen(txt,mode);
276  assert(fd);
277  if(mode[0]=='w') fprintf(fd,"#sector%2.2d/eemcPMTped\n",secID);
278  sprintf(txt,"a%2.2d",secID);
279 
280  //............................... LOOP over histo .........
281  int j;
282  int nOK=0;
283  for(j=0;j<HList->GetEntries();j++) {
284  TH1F* h=(TH1F*)HList->UncheckedAt(j);
285  assert(h);
286  if(strncmp(txt,h->GetName(),3) ) continue;
287  TF1* ff=h->GetFunction("fPeak");
288  //printf("cc %s \n",h->GetName());
289  if(ff==0) continue;
290  // Write values to a text file
291  fprintf(fd,"%s %6.2f %6.2f\n", h->GetName()+1,ff->GetParameter(1), ff->GetParameter(2));
292  nOK++;
293  }// end of loop within a sector
294  fclose(fd);
295  printf(" nOK=%d\n",nOK);
296  } //end of loop over sectors
297 
298 
299 #if 0
300  //.......write pedestal values to ped.crate files
301  printf(" write file of pedestal values by crate to '%s'\n",fname.Data());
302  int crateID;
303  int MaxCrates=111;
304  int crate;
305  int chan;
306  int nOK=0;
307  for(crateID=1;crateID<=MaxCrates ;crateID++) { // crates
308  char txt[100];
309  sprintf(txt,"%s.crate",fname.Data());
310  if(crateID==1) printf("%s ....",txt);
311  else mode="a";
312  FILE *fd = fopen(txt,mode);
313  assert(fd);
314  sprintf(txt,"%03d",crateID);
315 
316  //............................... LOOP over histo .........
317  int j;
318  for(j=0;j<HList->GetEntries();j++) {
319  TH1F* h=(TH1F*)HList->UncheckedAt(j);
320  assert(h);
321 
322  char title[200];
323  strcpy(title,h->GetTitle());
324  //grab crate # and channel # from title
325  char *p = strchr(title,'=');
326  crate=atoi(p+1);
327  chan=atoi(p+5);
328 
329  if(crateID != crate) continue;
330  //if(chan==1) printf("t=%s= cr=%d ch=%d\n",title,crate,chan);
331  TF1* ff=h->GetFunction("fPeak");
332  //printf("cc %s \n",h->GetName());
333  if(ff==0) continue;
334  // Write values to a text file
335  fprintf(fd,"%03d %03d %6.2f %6.2f\n", crate, chan, ff->GetParameter(1), ff->GetParameter(2));
336  nOK++;
337 
338  }// end of loop within a crate
339  fclose(fd);
340  } //end of loop over crate
341  printf(" nOK=%d\n",nOK);
342 #endif
343 
344 }
345 
346 //-------------------------------------------
347 //-------------------------------------------
348 void EEpixPed::saveHisto(TString fname) {
349  fname+=".hist.root";
350 
351  TFile f(fname.Data(),"update");
352  assert(f.IsOpen());
353  printf("%d histos are written to '%s' ...\n",HList->GetEntries(),fname.Data());
354  HList->Write();
355  TH1F* h= (TH1F*) tFd->Get("info");
356  if(h) h->Write();
357  f.Close();
358  assert(!f.IsOpen());
359 
360  printf(" , save Ok \n");
361 }