38 #include <TApplication.h>
46 #include <TPaveLabel.h>
47 #include <TPolyLine.h>
64 const float etaBinTable[] = {
65 2.0000,1.9008,1.8065,1.7168,1.6317,1.5507,1.4738,
66 1.4007,1.3312,1.2651,1.2023,1.1427,1.0860,-1.0 };
68 const float SamplingFraction = 0.05;
69 const float EnergyLossMip = 0.0018*9.6;
70 const short AdcRange = 4076;
71 const float EnergyTransverseMax = 60.0;
73 const int MaxSec = 12;
74 const int MaxSSec = 5;
75 const int MaxEta = 12;
77 const int FirstSec= 0;
78 const int LastSec =11;
82 float MaxCTB = 1000.0;
83 const float MinPt = 0.500;
84 const float MaxDEta = 0.035;
85 const float MaxDPhi = 0.028;
98 void mystat(TH1 *h, Double_t x1, Double_t x2,
99 Double_t& xmean, Double_t& emean, Double_t& dint,
100 Double_t& xmax , Double_t& xlo , Double_t& xhi);
101 int miptower( TH1 **hadc,
104 const char *dname=
"eta",
105 const char *func =
"landau",
106 const float xmin = 10.0,
107 const float xmax = 80.0,
108 FILE *outfd = stderr);
117 const int secNum = 0,
118 const char *fName =
"",
119 const char *histName=
"calib.root",
120 const char *fitFunc =
"landau" ,
121 const float xMin = 10.0,
122 const float xMax = 80.0,
124 const bool doSort =
false
128 gErrorIgnoreLevel=1000;
130 const int MaxHist = MaxEta*MaxSec*MaxSSec;
131 const int MaxTracks = 1024;
132 const int MaxTrigger = 32;
136 int sector[MaxTracks];
137 int subsec[MaxTracks];
138 int etabin[MaxTracks];
139 float adcval[MaxTracks];
140 int ntrack[MaxTracks];
142 float pt [MaxTracks];
143 float ptot [MaxTracks];
147 float etatrk[MaxTracks];
149 float detasmd [MaxTracks];
150 float dphismd [MaxTracks];
151 float detapres[MaxTracks];
152 float dphipres[MaxTracks];
153 float detapost[MaxTracks];
154 float dphipost[MaxTracks];
158 int trigid[MaxTrigger];
163 TH1* hadc[MaxHist];
for(
int i=0; i<MaxHist; i++) hadc[i]=NULL;
164 TH1* heta[MaxEta];
for(
int i=0; i<MaxEta ; i++) heta[i]=NULL;
168 TChain *chain = NULL;
172 chain =
new TChain(
"track");
177 TSeqCollection *seq = gROOT->GetListOfFiles();
179 while ( ( f = (TFile *)next() ) != NULL ) chain->Add(f->GetName());
180 fName =
"attached files";
182 cout <<
"sorting " << fName <<
" (trigger=" << trig <<
") to " << histName << endl;
183 cout <<
"MaxCTB: " << MaxCTB << endl;
184 nentries = (int)chain->GetEntries();
186 chain->SetBranchAddress(
"ntracks" ,&numtracks);
188 chain->SetBranchAddress(
"sec" , sector );
189 chain->SetBranchAddress(
"ssec" , subsec );
190 chain->SetBranchAddress(
"eta" , etabin );
191 chain->SetBranchAddress(
"adc" , adcval );
192 chain->SetBranchAddress(
"track" , ntrack );
194 chain->SetBranchAddress(
"pt" , pt );
195 chain->SetBranchAddress(
"ptot" , ptot );
201 chain->SetBranchAddress(
"detasmd" , detasmd );
202 chain->SetBranchAddress(
"dphismd" , dphismd );
204 chain->SetBranchAddress(
"detapres", detapres );
205 chain->SetBranchAddress(
"dphipres", dphipres );
207 chain->SetBranchAddress(
"detapost", detapost );
208 chain->SetBranchAddress(
"dphipost", dphipost );
210 chain->SetBranchAddress(
"ntrig" ,&numtrig );
211 chain->SetBranchAddress(
"trigid" , trigid );
212 chain->SetBranchAddress(
"daqbits" ,&daqbits );
213 chain->SetBranchAddress(
"ctbsum" ,&ctbsum );
215 cout << histName <<
" fitting " << fitFunc << endl;
218 int sec1=(secNum>0) ? secNum-1 : FirstSec;
219 int sec2=(secNum>0) ? secNum-1 : LastSec;
221 TFile *histfile =
new TFile(histName,doSort ?
"RECREATE" :
"READ");
222 TString outName = TString(histName).ReplaceAll(
".root",
"");
227 char name[256],titl[256];
228 for(
int sec=sec1;sec<=sec2 ;sec++) {
229 for(
int ssec=0; ssec<MaxSSec; ssec++) {
230 sprintf(dir,
"%02dT%1c",sec+1,ssec+
'A');
231 if(doSort) histfile->mkdir(dir);
233 for(
int eta=0; eta<MaxEta; eta++ ) {
234 sprintf(titl,
"ADC(%02dT%1c%02d)",sec+1,ssec+
'A',eta+1);
235 sprintf(name,
"%02dT%1c%02d" ,sec+1,ssec+
'A',eta+1);
236 int hidx=(sec*MaxSSec+ssec)*MaxEta+eta;
237 if(hidx<0 || MaxHist<=hidx)
continue;
238 if(doSort) hadc[hidx] =
new TH1F(name,titl,40,0.0,120.0);
239 else hadc[hidx] = (TH1F *)gDirectory->Get(name);
247 if(doSort) histfile->mkdir(dir);
249 for(
int eta=0; eta<MaxEta; eta++ ) {
250 sprintf(titl,
"ADC(ETA%02d)",eta+1);
251 sprintf(name,
"ETA%02d" ,eta+1);
252 if(doSort) heta[eta] =
new TH1F(name,titl,60,0.0,120.0);
253 else heta[eta] = (TH1F *)gDirectory->Get(name);
259 for(ie=0;ie<nentries;ie++) {
260 ntracks += numtracks;
262 if(ie%100==0)fprintf(stdout ,
"Entry %d/%d (%.2f%%)\r",ie,nentries,(ie*100.0)/nentries);
263 if(ctbsum>MaxCTB)
continue;
266 int *trigword=trigid;
267 while(it<numtrig && *trigword>0 && *trigword!=trig ) { it++; trigword++; };
268 if(*trigword!=trig)
continue;
271 for(
int t=0;t<numtracks; t++) {
273 if(ntrack[t] > 1 )
continue;
274 if(pt[t] < MinPt )
continue;
275 if(fabs(detapres[t]) > MaxDEta )
continue;
276 if(fabs(dphipres[t]) > MaxDPhi )
continue;
277 if(fabs(detasmd[t]) > MaxDEta )
continue;
278 if(fabs(dphismd[t]) > MaxDPhi )
continue;
279 if(fabs(detapost[t]) > MaxDEta )
continue;
280 if(fabs(dphipost[t]) > MaxDPhi )
continue;
283 int ssec = subsec[t];
285 int hidx=(sec*MaxSSec+ssec)*MaxEta+eta;
286 if(hidx<0 || MaxHist<=hidx)
continue;
287 if(hadc[hidx]!=NULL) hadc[hidx]->Fill(adcval[t]);
288 if(heta[eta ]!=NULL) heta[eta ]->Fill(adcval[t]);
291 fprintf(stdout ,
"Entry %d/%d (%.2f%%)\n",ie,nentries,(ie*100.0)/nentries);
292 fprintf(stdout ,
"Totals tracks %ld\n",ntracks);
296 TCanvas *c1 =
new TCanvas(outName,
"MIP CALIB",800,0,1024,1024);
297 c1->Print(outName+
".ps[");
299 for(
int sec=sec1;sec<=sec2 && go_on;sec++) {
301 sprintf(outfn,
"sector%02d.cal",sec+1);
302 FILE *outfd = fopen(outfn,
"w");
303 for(
int ssec=0;ssec<MaxSSec && go_on;ssec++) {
304 sprintf(dir,
"%02dT%1c",sec+1,ssec+
'A');
306 nFitOK += miptower(hadc+(sec*MaxSSec+ssec)*MaxEta,MinEta,MaxEta,dir,fitFunc,xMin,xMax,outfd);
308 c1->Print(outName+
".ps");
312 if(!gROOT->IsBatch()) {
321 if(c==0x0A) { cmd[k++]=0x00;
break; }
324 if(cmd[0]==0x0A) {
break;
325 }
else if(strncasecmp(cmd,
".cont" ,2)==0 ||
326 strncasecmp(cmd,
"cont" ,1)==0) {
break;
327 }
else if(strncasecmp(cmd,
".quit" ,2)==0 ||
328 strncasecmp(cmd,
"quit" ,1)==0) { go_on=
false;
break;
329 }
else if(strncasecmp(cmd,
".help" ,2)==0 ||
330 strncasecmp(cmd,
"help" ,1)==0) {
331 cerr <<
"commands (may be abbreviated):\n";
332 cerr <<
" .c cont - to continue with fitting\n" ;
333 cerr <<
" .q quit - to quit fitting loop \n" ;
334 cerr <<
" .h help - this help \n" << endl;
336 cerr <<
"unkown command:" << cmd <<
" type help for command list" << endl;
343 cout <<
"TOTAL FIT OK " << nFitOK << endl;
345 (void) miptower(heta,MinEta,MaxEta,
"ETA",fitFunc,xMin,xMax);
347 c1->Print(outName+
".ps");
348 c1->Print(outName+
".ps]");
358 miptower( TH1 **hadc,
367 const Double_t MinCounts = 30.0;
368 const Double_t MinPeakV = 8.0;
369 const Double_t MaxPeakV = 30.0;
370 const Double_t MaxPeakE = 6.0 ;
371 const Double_t MinChi2 = 0.0 ;
372 const Double_t MaxChi2 =10.0 ;
374 const int MaxPad = MaxEta-MinEta;
379 TString gltit(
"EEMC TOWERS ");
380 TString pz (
"Piotr A. Zolnierczuk (IU) ");
381 TPaveLabel *tlab =
new TPaveLabel(0.005,0.955,0.990,0.985,gltit+dir);
382 TDatime *now =
new TDatime;
383 TPaveLabel *date =
new TPaveLabel(0.555,0.005,0.995,0.045,pz+now->AsString());
384 TPad *gpad =
new TPad(
"Graphs",
"Graphs",0.005,0.05,0.995,0.95);
386 gStyle->SetOptStat(101111);
387 gStyle->SetOptFit(1);
391 gpad->Divide(2,MaxPad/2);
395 if(gROOT->IsBatch()) cout <<
"TOWERS " << dir <<
" " << flush;
402 for(
int eta=0,pad=0; pad<MaxPad && eta<MaxEta; eta++) {
404 Double_t xmean , xmnerr;
405 Double_t xpeak , xpkerr;
406 Double_t xgain , xgnerr;
411 sprintf(name,
"%s%02d",dir,eta+1);
413 if(hadc[eta]==NULL)
continue;
416 mystat(hadc[eta],xMin,xMax,xmean,xmnerr,xint,xpeak,xlim1,xlim2);
419 par[0] = hadc[eta]->GetMaximum();
424 TF1 *fit1 =
new TF1(
"fit1" ,func ,0.0,120.0);
425 fit1->SetParameters(par);
426 fit1->SetLineWidth(2);
427 fit1->SetLineColor(kRed);
430 if(strncmp(func,
"gaus",4)==0) {
432 hadc[eta]->Fit(
"fit1",
"Q0",
"",xlim1,xlim2);
434 hadc[eta]->Fit(
"fit1",
"Q0",
"",xMin,xMax);
440 xpeak = fit1->GetParameter(1);
441 xpkerr = fit1->GetParError(1);
442 chi2 = fit1->GetChisquare();
443 ndf = fit1->GetNDF();
444 chi2 = (ndf>=1) ? chi2/ndf : -1.0;
445 xpkerr *= (chi2>0.0) ? sqrt(chi2) : 1.0 ;
448 Double_t xeta = 0.5*(etaBinTable[eta-1]+etaBinTable[eta]);
449 Double_t xscale = SamplingFraction/EnergyLossMip*TMath::TanH(xeta);
450 xgain = xpeak * xscale;
451 xgnerr = xpkerr * xscale;
454 fprintf(outfd,
"%6s %8.3f %8.3f 0.0 # %8.3f %6.0f",
455 name,xgain,xgnerr,chi2,xint);
459 if (xint<MinCounts ) errmsg =
"not enough statistics";
460 else if(xpeak<MinPeakV || MaxPeakV<xpeak ) errmsg =
"bad fit value";
461 else if( MaxPeakE < xpkerr ) errmsg =
"fit error too large";
462 else if(chi2<MinChi2 || MaxChi2<chi2 ) errmsg =
"chi2 too large";
464 if(errmsg==NULL) fprintf(outfd,
" fit %s ok \n",func);
465 else fprintf(outfd,
" *** %s ***\n",errmsg);
466 if(eta<MinEta)
continue;
470 Double_t hmax = hadc[eta]->GetMaximum();
471 hadc[eta]->SetMaximum( (hmax>10.0) ? 1.2*hmax : 12.0 );
472 hadc[eta]->GetXaxis()->SetTitle(
"ADC");
473 hadc[eta]->Draw(
"E");
477 hmax = hadc[eta]->GetMaximum();
478 TPaveLabel* badfitlabel =
new TPaveLabel(70,0.25*hmax,118,0.40*hmax,errmsg);
479 badfitlabel->SetFillColor(kRed );
480 badfitlabel->SetTextColor(kYellow );
488 if(gROOT->IsBatch()) cout <<
" (" << nFitOK <<
") DONE " << endl;
500 Double_t x1 , Double_t x2,
501 Double_t& xmean, Double_t& emean, Double_t& dint,
502 Double_t& xmax , Double_t& xlo , Double_t& xhi)
504 TAxis* xax = h->GetXaxis();
505 Int_t i1 = xax->FindBin(x1);
506 Int_t i2 = xax->FindBin(x2);
508 Float_t sw=0.0,sw2=0.0,swx=0.0,swx2=0.0,wmax=0.0;
511 xmean=emean=xmax=0.0;
512 for(Int_t i=i1;i<=i2;i++) {
513 Double_t x = h->GetBinCenter(i);
514 Double_t w = h->GetBinContent(i);
515 if(w>wmax) { xmax=x; wmax=w; imax=i; }
523 xlo = h->GetBinCenter(i1);
524 xhi = h->GetBinCenter(i2);
529 for(Int_t i=imax;i>=i1;i--) {
530 Double_t w = h->GetBinContent(i);
533 xlo = h->GetBinCenter(i);
538 for(Int_t i=imax;i<=i2;i++) {
539 Double_t w = h->GetBinContent(i);
542 xhi = h->GetBinCenter(i);
548 xmean = (sw>0.0) ? swx/sw : 0.0 ;
549 emean = (sw>0.0) ? (swx2/sw - xmean*xmean ) : 0.0 ;
550 emean = (emean>0.0) ? sqrt(emean/n) : 0.0;
556 char *outname =
"mipcalib.hist.root";
557 char *fitfunc =
"landau";
566 cerr <<
"usage: " << name <<
" [options] rootfile(s) \n";
567 cerr <<
" -s <sector> - (0 == all) \n";
568 cerr <<
" -t <trigger_id> - (0 == any) \n";
569 cerr <<
" -o <histogram file> - .hist.root file \n";
570 cerr <<
" -L - fit Landau/Gauss distributions\n";
571 cerr <<
" -G - fit Landau/Gauss distributions\n";
572 cerr <<
" -x <xmin> - fit lower bound\n";
573 cerr <<
" -X <xmax> - fit upper bound\n";
574 cerr <<
" -b - run in batch mode without graphics \n";
575 cerr <<
" -h - this help\n";
581 main(
int argc,
char **argv)
586 cerr <<
"#===============================================\n";
587 cerr <<
"# ******* WARNING ******* \n";
588 cerr <<
"# A LOUSY PROGRAM THAT GREW OUT OF A ROOT MACRO \n";
589 cerr <<
"# needs to be rewritten \n";
590 cerr <<
"#===============================================\n" << endl;
593 while((optchar = getopt(argc, argv,
"s:t:o:LGx:X:c:bqnlh")) != EOF) {
595 case 's': sector = atoi(optarg);
break;
596 case 'o': outname = optarg ;
break;
597 case 't': trig = atoi(optarg);
break;
598 case 'L': fitfunc =
"landau";
break;
599 case 'G': fitfunc =
"gaus" ;
break;
600 case 'x': xmin = atof(optarg);
break;
601 case 'X': xmax = atof(optarg);
break;
602 case 'c': MaxCTB = atof(optarg);
break;
603 case 'b': gROOT->SetBatch(kTRUE);
607 case 'h': usage(argv[0]); exit(0);
break;
609 default: usage(argv[0]); exit(-1);
break;
615 for(
int k=optind;k<argc; k++)
new TFile(argv[k],
"");
621 myApp =
new TApplication(
"mipcalib",&argc,argv);
623 myApp =
new TRint (
"mipcalib",&argc,argv);
625 mipcalib(sector,
"",outname,fitfunc,xmin,xmax,trig,
true );
626 mipcalib(sector,
"",outname,fitfunc,xmin,xmax,trig,
false);
628 if(!gROOT->IsBatch() ) myApp->Run();