1 |
// |
2 |
// Plot the energy [MIP], nstrip and qtot distributions - Emiliano Mocchiutti |
3 |
// |
4 |
// CaloMIP.c version 2.12 (2005-11-10) |
5 |
// |
6 |
// The only input needed is the path to the directory created by YODA for the data file you want to analyze. |
7 |
// |
8 |
// Changelog: |
9 |
// |
10 |
// 2.11 - 2.12 (2005-11-10): compiled! |
11 |
// |
12 |
// 2.10 - 2.11 (2005-08-16): 64 bit arch bugs fixed. |
13 |
// |
14 |
// 2.09 - 2.10 (2005-07-25): don't load anymore yodaUtility.c use instead clone of routines |
15 |
// |
16 |
// 2.08 - 2.09 (2005-07-12): small changes for compilation. |
17 |
// |
18 |
// 2.07 - 2.08 (2005-06-28): changed to be inserted in package CaloQLOOK. |
19 |
// |
20 |
// 2.06 - 2.07 (2005-06-07): check if file is older than 050515_007. |
21 |
// |
22 |
// 2.05 - 2.06 (2005-03-09): saveas input parameter introduced (required by Maurizio). |
23 |
// |
24 |
// 2.04 - 2.05 (2005-03-02): Print out the position of the peak of the fitted function. |
25 |
// |
26 |
// 2.03 - 2.04 (2005-02-24): Changed colour background (required by Marco Casolino). |
27 |
// |
28 |
// 2.02 - 2.03 (2005-02-24): Changed variable definition from C/C++ style to ROOT style (int->Int_t). |
29 |
// |
30 |
// 2.01 - 2.02 (2005-02-07): Added fit of the MIP. Maxevent bug fixed. Changed name of default printed figures. |
31 |
// |
32 |
// 2.00 - 2.01 (2005-01-25): Closing the header file at the end it will clear figures on canvas, fixed. |
33 |
// |
34 |
// 1.02 - 2.00 (2005-01-20): Now it will access directly LEVEL1 ntuple instead of calibrating everything everytime. |
35 |
// |
36 |
// 1.01 - 1.02 (2005-01-17): Cleanup of the code. |
37 |
// |
38 |
// 1.00 - 1.01 (2004-12-15): Include also yodaUtility.c . |
39 |
// |
40 |
#include <fstream> |
41 |
#include <sstream> |
42 |
#include <TTree.h> |
43 |
#include <TClassEdit.h> |
44 |
#include <TObject.h> |
45 |
#include <TList.h> |
46 |
#include <TSystem.h> |
47 |
#include <TSystemDirectory.h> |
48 |
#include <TString.h> |
49 |
#include <TFile.h> |
50 |
#include <TClass.h> |
51 |
#include <TCanvas.h> |
52 |
#include <TH1.h> |
53 |
#include <TH1F.h> |
54 |
#include <TH2D.h> |
55 |
#include <TLatex.h> |
56 |
#include <TPad.h> |
57 |
#include <TPaveLabel.h> |
58 |
#include <TChain.h> |
59 |
#if !defined (__CINT__) |
60 |
#include <event/PamelaRun.h> |
61 |
#include <event/physics/calorimeter/CalorimeterEvent.h> |
62 |
#include <event/physics/trigger/TriggerEvent.h> |
63 |
#include <event/CalibCalPedEvent.h> |
64 |
extern short int CaloLEVEL1(TString, TString, Int_t); |
65 |
#include <caloclasses.h> |
66 |
#endif |
67 |
#include <CaloFunctions.h> |
68 |
|
69 |
|
70 |
void CaloMIP(TString filename, Int_t view = 0, Int_t plane = 0, Int_t strip = 0, Int_t fromevent = 0, Int_t toevent = 0, TString outDir = "", TString saveas = "gif"){ |
71 |
gROOT->GetListOfCanvases()->Delete("a"); |
72 |
gDirectory->GetList()->Delete(); |
73 |
const char* startingdir = gSystem->WorkingDirectory(); |
74 |
#if defined (__CINT__) |
75 |
emicheckLib(); |
76 |
stringstream llib; |
77 |
const char *pamlib = gSystem->ExpandPathName("$PAM_LIB"); |
78 |
llib << pamlib << "/caloclasses_h.so"; |
79 |
gSystem->Load(llib.str().c_str()); |
80 |
#endif |
81 |
gStyle->SetOptStat(1111); |
82 |
if ( outDir == "" ) outDir = filename; |
83 |
// |
84 |
// Open LEVEL1 ntuple, if it doesn't exist call CaloLEVEL1. |
85 |
// |
86 |
TFile *headerFile = 0; |
87 |
TFile *caloFile = 0; |
88 |
headerFile = emigetFile(filename, "Physics", "Header"); |
89 |
if ( !headerFile ){ |
90 |
printf("No header file, exiting...\n"); |
91 |
return; |
92 |
}; |
93 |
caloFile = emigetFile(filename, "Physics.Level1", "Calorimeter"); |
94 |
if ( !caloFile ){ |
95 |
printf("\n WARNING: No calorimeter LEVEL1 file! \n\n I am going to call CaloLEVEL1, it could take some time to generate level1 data. \n\n"); |
96 |
headerFile->Close(); |
97 |
gSystem->Exec("sleep 3"); |
98 |
short int ERR = CaloLEVEL1(filename,"",0); |
99 |
if ( ERR ) { |
100 |
printf(" I am not able to calibrate data, please call CaloLEVEL1 by hand \n\n Exiting... \n\n"); |
101 |
return; |
102 |
} else { |
103 |
printf(" OK, back to CaloMIP! \n\n"); |
104 |
headerFile = emigetFile(filename, "Physics", "Header"); |
105 |
caloFile = emigetFile(filename, "Physics.Level1", "Calorimeter"); |
106 |
}; |
107 |
}; |
108 |
//Takes the tree of the header file |
109 |
TTree *otr = (TTree*)headerFile->Get("Pscu"); |
110 |
otr->AddFriend("CaloLevel1",caloFile); |
111 |
// |
112 |
CalorimeterLevel1 *calo = new CalorimeterLevel1(); |
113 |
otr->SetBranchAddress("Event", &calo); |
114 |
// |
115 |
Long64_t nevents = otr->GetEntries(); |
116 |
// |
117 |
// input limits |
118 |
// |
119 |
Int_t minview; |
120 |
Int_t maxview; |
121 |
if ( view > 2 || view < 0 ) { |
122 |
printf("You can choose view = 1 (X-views), view = 2 (Y-views) or view = 0 (both)\n"); |
123 |
return; |
124 |
}; |
125 |
if ( view == 0 ) { |
126 |
minview = 0; |
127 |
maxview = 2; |
128 |
} else { |
129 |
minview = view - 1; |
130 |
maxview = view; |
131 |
}; |
132 |
|
133 |
if ( plane > 22 || plane < 0 ) { |
134 |
printf("You can choose plane between 1 and 22 or plane = 0 (all)\n"); |
135 |
return; |
136 |
}; |
137 |
Int_t minplane; |
138 |
Int_t maxplane; |
139 |
if ( plane == 0 ) { |
140 |
minplane = 0; |
141 |
maxplane = 22; |
142 |
} else { |
143 |
minplane = plane - 1; |
144 |
maxplane = plane; |
145 |
}; |
146 |
Int_t minstrip; |
147 |
Int_t maxstrip; |
148 |
if ( strip > 96 || strip < 0 ) { |
149 |
printf("You can choose strip between 0 and 96 or strip = 0 (all)\n"); |
150 |
return; |
151 |
}; |
152 |
if ( strip == 0 ) { |
153 |
minstrip = 0; |
154 |
maxstrip = 96; |
155 |
} else { |
156 |
minstrip = strip - 1; |
157 |
maxstrip = strip; |
158 |
}; |
159 |
|
160 |
if ( fromevent > toevent && toevent ){ |
161 |
printf("It must be fromevent < toevent \n"); |
162 |
return; |
163 |
}; |
164 |
|
165 |
|
166 |
if ( fromevent > nevents+1 || fromevent < 0 ) { |
167 |
printf("You can choose fromevent between 0 (all) and %i \n",(int)nevents+1); |
168 |
return; |
169 |
}; |
170 |
|
171 |
if ( toevent > nevents+1 || toevent < 0 ) { |
172 |
printf("You can choose toevent between 0 (all) and %i \n",(int)nevents+1); |
173 |
return; |
174 |
}; |
175 |
Int_t minevent; |
176 |
Int_t maxevent; |
177 |
if ( fromevent == 0 ) { |
178 |
minevent = 0; |
179 |
maxevent = nevents; |
180 |
} else { |
181 |
minevent = fromevent - 1; |
182 |
if ( toevent > 0 ){ |
183 |
maxevent = toevent; |
184 |
} else { |
185 |
maxevent = fromevent - 1; |
186 |
}; |
187 |
}; |
188 |
|
189 |
// |
190 |
// Book the histograms: |
191 |
// |
192 |
// |
193 |
TH1F *gmip; |
194 |
gmip = new TH1F("the MIP","",55,-0.19,1.9); |
195 |
//gmip->SetBit(TH1F::kCanRebin); |
196 |
// |
197 |
TH1F *diffbase; |
198 |
diffbase = new TH1F("diffbase","",55,-0.19,0.19); |
199 |
diffbase->SetBit(TH1F::kCanRebin); |
200 |
// |
201 |
TH1F *Qtot; |
202 |
Qtot = new TH1F("qtot","",70,0.,60.); |
203 |
Qtot->SetBit(TH1F::kCanRebin); |
204 |
// |
205 |
TH1F *Nstrip; |
206 |
Nstrip = new TH1F("nstrip","",70,0.,60.); |
207 |
Nstrip->SetBit(TH1F::kCanRebin); |
208 |
// |
209 |
// run over all the events |
210 |
// |
211 |
// |
212 |
Int_t tot2 = 0; |
213 |
Int_t baseDIFFfull = 0; |
214 |
printf("\n Processed events: \n\n"); |
215 |
// |
216 |
for (Int_t i = minevent; i < maxevent; i++){ |
217 |
if ( i%1000 == 0 && i > 0 ) printf(" %iK \n",i/1000); |
218 |
|
219 |
// |
220 |
// read from the header of the event the absolute time at which it was recorded |
221 |
// |
222 |
otr->GetEntry(i); |
223 |
|
224 |
// |
225 |
// run over views and planes |
226 |
// |
227 |
for (Int_t l = minview; l < maxview; l++){ |
228 |
for (Int_t m = minplane; m < maxplane; m++){ |
229 |
Int_t pre = -1; |
230 |
for (Int_t n = minstrip; n < maxstrip; n++){ |
231 |
// |
232 |
if ( n%16 == 0 ) pre++; |
233 |
if ( n == minstrip && pre == -1 ){ |
234 |
for (Int_t nm = minstrip; pre == -1 ; nm--){ |
235 |
if ( nm%16 == 0 ) pre++; |
236 |
}; |
237 |
}; |
238 |
if ( pre == -1 ) { |
239 |
printf("Unable to determine the preamplifier number. Quit.\n"); |
240 |
return; |
241 |
}; |
242 |
// |
243 |
if ( calo->nobase ) tot2++; |
244 |
if ( calo->diffbas[l][m][pre] != 0. ) { |
245 |
// printf("differenza %f %i %i %i %i %i \n",calo->diffbas[l][m][pre],i,l,m,pre,n); |
246 |
baseDIFFfull = 1; |
247 |
diffbase->Fill(calo->diffbas[l][m][pre]); |
248 |
// return; |
249 |
}; |
250 |
if ( calo->estrip[l][m][n] != 0. ) gmip->Fill(calo->estrip[l][m][n]); |
251 |
}; |
252 |
}; |
253 |
}; |
254 |
if ( calo->nstrip > 0. ) Nstrip->Fill(calo->nstrip); |
255 |
if ( calo->qtot > 0. ) Qtot->Fill(calo->qtot); |
256 |
}; |
257 |
if ( tot2 ){ |
258 |
printf("WARNING: events with no baselines (event lost): %i\n",tot2); |
259 |
}; |
260 |
// |
261 |
// figures: |
262 |
// |
263 |
// THE MIP |
264 |
TCanvas *figura = new TCanvas("The_calorimeter_MIP", "The_calorimeter_MIP", 1100, 900); |
265 |
figura->SetFillColor(10); |
266 |
figura->Range(0,0,100,100); |
267 |
figura->cd(); |
268 |
gPad->SetLogy(); |
269 |
gmip->SetXTitle("MIP"); |
270 |
gmip->SetYTitle("Number of events"); |
271 |
gPad->SetTicks(); |
272 |
printf("\n Fitting...\n\n"); |
273 |
// Setting fit range and start values |
274 |
Double_t fr[2]; |
275 |
Double_t sv[4], pllo[4], plhi[4], fp[4], fpe[4]; |
276 |
fr[0] = 0.7; |
277 |
fr[1] = 2.0; |
278 |
pllo[0]=0.01; pllo[1]=0.5; pllo[2]=1.0; pllo[3]=0.05; |
279 |
plhi[0]=5.0; plhi[1]=5.0; plhi[2]=1000000.0; plhi[3]=1.0; |
280 |
sv[0]=1.; sv[1]=1.0; sv[2]=500.0; sv[3]=0.2; |
281 |
Double_t chisqr; |
282 |
Int_t ndf; |
283 |
TF1 *fitsnr = langaufit(gmip,fr,sv,pllo,plhi,fp,fpe,&chisqr,&ndf); |
284 |
// |
285 |
Double_t SNRPeak = langaupro(fp); |
286 |
printf("\n Conversion factor: %f \n",SNRPeak); |
287 |
// |
288 |
// gStyle->SetOptStat(1111); |
289 |
gStyle->SetOptFit(111); |
290 |
printf("\n ...done!\n"); |
291 |
gmip->Draw(); |
292 |
fitsnr->Draw("lsame"); |
293 |
// Differences between DSP and calculated baselines in full mode |
294 |
TCanvas *base = 0; |
295 |
if ( baseDIFFfull ){ |
296 |
base = new TCanvas("WARNING", "WARNING_full mode,_differences_between_calculated_and_DSP_baselines!!", 1100, 900); |
297 |
base->SetFillColor(10); |
298 |
base->Range(0,0,100,100); |
299 |
base->cd(); |
300 |
gStyle->SetOptStat(0); |
301 |
gPad->SetLogy(); |
302 |
diffbase->Draw(); |
303 |
gStyle->SetOptStat(1111); |
304 |
}; |
305 |
// nstrip and qtot WARNING: only GOOD strips considered! |
306 |
TCanvas *figura2 = new TCanvas("nstrip_and_qtot", "nstrip_and_qtot", 1100, 900); |
307 |
figura2->SetFillColor(10); |
308 |
figura2->Range(0,0,100,100); |
309 |
TPad *pd1; |
310 |
TPad *pd2; |
311 |
pd1 = new TPad("pd1","This is pad1",0.02,0.02,0.49,0.98,10); |
312 |
pd2 = new TPad("pd2","This is pad2",0.51,0.02,0.98,0.98,10); |
313 |
figura2->cd(); |
314 |
gStyle->SetOptStat(1111); |
315 |
pd1->Range(0,0,100,100); |
316 |
pd2->Range(0,0,100,100); |
317 |
pd1->SetGrid(); |
318 |
pd2->SetGrid(); |
319 |
pd1->SetTicks(); |
320 |
pd2->SetTicks(); |
321 |
pd1->Draw(); |
322 |
pd2->Draw(); |
323 |
pd1->cd(); |
324 |
gPad->SetLogy(); |
325 |
Nstrip->SetXTitle("Number of hit (E>0.7 MIP)"); |
326 |
Nstrip->SetYTitle("Number of events"); |
327 |
Nstrip->Draw(); |
328 |
pd2->cd(); |
329 |
gPad->SetLogy(); |
330 |
Qtot->SetXTitle("MIP"); |
331 |
Qtot->SetYTitle("Number of events"); |
332 |
Qtot->Draw(); |
333 |
printf("\n"); |
334 |
// |
335 |
|
336 |
|
337 |
const string fil = (const char*)filename; |
338 |
Int_t posiz = fil.find("dw_"); |
339 |
if ( posiz == -1 ) posiz = fil.find("DW_"); |
340 |
Int_t posiz2 = posiz+13; |
341 |
TString file2; |
342 |
stringcopy(file2,filename,posiz,posiz2); |
343 |
// |
344 |
const char *figrec = file2; |
345 |
const char *outdir = outDir; |
346 |
stringstream figsave; |
347 |
const char *format = saveas; |
348 |
figsave.str(""); |
349 |
figsave << outdir << "/" ; |
350 |
figsave << figrec << "_mip1."; |
351 |
figsave << format; |
352 |
// |
353 |
figura->SaveAs(figsave.str().c_str()); |
354 |
figsave.str(""); |
355 |
figsave << outdir << "/" ; |
356 |
figsave << figrec << "_mip2."; |
357 |
figsave << format; |
358 |
// |
359 |
figura2->SaveAs(figsave.str().c_str()); |
360 |
if ( baseDIFFfull ){ |
361 |
figsave.str(""); |
362 |
figsave << outdir << "/" ; |
363 |
figsave << figrec << "_mip3."; |
364 |
figsave << format; |
365 |
// |
366 |
base->SaveAs(figsave.str().c_str()); |
367 |
}; |
368 |
// |
369 |
// caloFile->Close(); |
370 |
//headerFile->Close(); |
371 |
gSystem->ChangeDirectory(startingdir); |
372 |
} |