Reading of Raw data in rootified and DATE formats. Possibility to select the detector...
[u/mrichter/AliRoot.git] / TOF / AliTOFComparison.C
CommitLineData
3f83f224 1/****************************************************************************
2 * This macro estimates efficiency of matching with the TOF. *
3 * TOF "Good" tracks are those originating from the primary vertex, *
4 * being "good" in the ITS and having at least one digit in the TOF. *
5 * (To get the list of "good" tracks one should first run *
6 * AliTPCComparison.C and AliITSComparisonV2.C macros) *
7 * Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch *
8 ****************************************************************************/
9
10#if !defined(__CINT__) || defined(__MAKECINT__)
4fa1e629 11 #include <TMath.h>
12 #include <TError.h>
3f83f224 13 #include <Riostream.h>
4fa1e629 14 #include <TH1F.h>
15 #include <TTree.h>
16 #include <TParticle.h>
17 #include <TCanvas.h>
18 #include <TLine.h>
19 #include <TText.h>
20 #include <TBenchmark.h>
21 #include <TStyle.h>
8b462fd8 22 #include <TFile.h>
4fa1e629 23 #include <TROOT.h>
3f83f224 24
3f83f224 25 #include "AliStack.h"
4fa1e629 26 #include "AliHeader.h"
27 #include "AliTrackReference.h"
3f83f224 28 #include "AliRunLoader.h"
4fa1e629 29 #include "AliRun.h"
3f83f224 30 #include "AliESD.h"
4fa1e629 31
f4af68da 32 #include "AliTOFcluster.h"
4fa1e629 33 #include "AliLoader.h"
3f83f224 34
3f83f224 35 #include "TClonesArray.h"
3f83f224 36#endif
37
4fa1e629 38Int_t GoodTracksTOF(const Char_t *dir=".");
3f83f224 39
40extern AliRun *gAlice;
4fa1e629 41extern TBenchmark *gBenchmark;
42extern TROOT *gROOT;
43
44static Int_t allgood=0;
45static Int_t allmatched=0;
46static Int_t allmismatched=0;
47
48Int_t AliTOFComparison(const Char_t *dir=".") {
49 gBenchmark->Start("AliTOFComparison");
50
51 ::Info("AliTOFComparison.C","Doing comparison...");
52
53
54 Double_t pmin=0.2;
55 Double_t pmax=3.0;
56
57 TH1F *hgood=(TH1F*)gROOT->FindObject("hgood");
58 if (!hgood) hgood=new TH1F("hgood","Good tracks",30,pmin,pmax);
59
60 TH1F *hfound=(TH1F*)gROOT->FindObject("hfound");
61 if (!hfound) hfound=new TH1F("hfound","Matched tracks",30,pmin,pmax);
62
63 TH1F *hfake=(TH1F*)gROOT->FindObject("hfake");
64 if (!hfake) hfake=new TH1F("hfake","Mismatched tracks",30,pmin,pmax);
65
66 TH1F *hgp=(TH1F*)gROOT->FindObject("hgp");
67 if (!hgp) hgp=new TH1F("hgp","",30,pmin,pmax);
68 hgp->SetLineColor(4); hgp->SetLineWidth(2);
69
70 TH1F *hfp=(TH1F*)gROOT->FindObject("hfp");
71 if (!hfp) hfp=new TH1F("hfp","Probability of mismatching",30,pmin,pmax);
72 hfp->SetFillColor(1); hfp->SetFillStyle(3013); hfp->SetLineWidth(2);
73
74 TH1F *hgoo=(TH1F*)gROOT->FindObject("hgoo");
75 if (!hgoo) hgoo=new TH1F("hgoo","Good tracks",30,-1,1);
76
77 TH1F *hfoun=(TH1F*)gROOT->FindObject("hfoun");
78 if (!hfoun) hfoun=new TH1F("hfoun","Matched tracks",30,-1,1);
79
80 TH1F *hfak=(TH1F*)gROOT->FindObject("hfak");
81 if (!hfak) hfak=new TH1F("hfak","Mismatched tracks",30,-1,1);
82
83 TH1F *hgl=(TH1F*)gROOT->FindObject("hgl");
84 if (!hgl) hgl=new TH1F("hgl","",30,-1,1);
85 hgl->SetLineColor(4); hgl->SetLineWidth(2);
86
87 TH1F *hfl=(TH1F*)gROOT->FindObject("hfl");
88 if (!hfl) hfl=new TH1F("hfl","Probability of mismatching",30,-1,1);
89 hfl->SetFillColor(1); hfl->SetFillStyle(3013); hfl->SetLineWidth(2);
90
3f83f224 91
3f83f224 92
4fa1e629 93 Char_t fname[100];
94 sprintf(fname,"%s/GoodTracksTOF.root",dir);
95
96 TFile *refFile=TFile::Open(fname,"old");
97 if (!refFile || !refFile->IsOpen()) {
98 ::Info("AliTOFComparison.C","Marking good tracks (will take a while)...");
99 if (GoodTracksTOF(dir)) {
100 ::Error("AliTOFComparison.C","Can't generate the reference file !");
101 return 1;
102 }
103 }
104 refFile=TFile::Open(fname,"old");
105 if (!refFile || !refFile->IsOpen()) {
106 ::Error("AliTOFComparison.C","Can't open the reference file !");
107 return 1;
108 }
109
110 TTree *tofTree=(TTree*)refFile->Get("tofTree");
111 if (!tofTree) {
112 ::Error("AliTOFComparison.C","Can't get the reference tree !");
113 return 2;
114 }
115 TBranch *branch=tofTree->GetBranch("TOF");
116 if (!branch) {
117 ::Error("AliTOFComparison.C","Can't get the TOF branch !");
118 return 3;
119 }
120 TClonesArray dummy("AliTrackReference",1000), *refs=&dummy;
121 branch->SetAddress(&refs);
122
3f83f224 123
124 if (gAlice) {
125 delete gAlice->GetRunLoader();
126 delete gAlice;//if everything was OK here it is already NULL
127 gAlice = 0x0;
128 }
4fa1e629 129 sprintf(fname,"%s/galice.root",dir);
130 AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
3f83f224 131 if (rl == 0x0) {
132 cerr<<"Can not open session"<<endl;
133 return 1;
134 }
135 AliLoader* tofl = rl->GetLoader("TOFLoader");
136 if (tofl == 0x0) {
137 cerr<<"Can not get the TOF loader"<<endl;
138 return 2;
139 }
f4af68da 140 tofl->LoadRecPoints("read");
3f83f224 141
3f83f224 142
4fa1e629 143 sprintf(fname,"%s/AliESDs.root",dir);
144 TFile *ef=TFile::Open(fname);
145 if ((!ef)||(!ef->IsOpen())) {
146 ::Error("AliTOFComparison.C","Can't open AliESDs.root !");
147 delete rl;
3f83f224 148 return 4;
149 }
8b462fd8 150 AliESD* event = new AliESD;
151 TTree* esdTree = (TTree*) ef->Get("esdTree");
152 if (!esdTree) {
153 ::Error("AliTOFComparison.C", "no ESD tree found");
154 return 5;
155 }
156 esdTree->SetBranchAddress("ESD", &event);
3f83f224 157
3f83f224 158
3f83f224 159
4fa1e629 160 //******* Loop over events *********
161 Int_t e=0;
8b462fd8 162 while (esdTree->GetEvent(e)) {
4fa1e629 163 cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
164
8b462fd8 165 rl->GetEvent(e);
4fa1e629 166
f4af68da 167 TTree *clsTree=tofl->TreeR();
168 if (!clsTree) {
4fa1e629 169 cerr<<"Can't get the TOF cluster tree !\n";
170 return 3;
171 }
f4af68da 172 TBranch *branch=clsTree->GetBranch("TOF");
4fa1e629 173 if (!branch) {
174 cerr<<"Can't get the branch with the TOF digits !\n";
175 return 4;
176 }
f4af68da 177 TClonesArray dummy("AliTOFcluster",10000), *clusters=&dummy;
178 branch->SetAddress(&clusters);
3f83f224 179
f4af68da 180 clsTree->GetEvent(0);
181 Int_t nd=clusters->GetEntriesFast();
182 cerr<<"Number of the TOF clusters: "<<nd<<endl;
1c3e95d8 183
3f83f224 184
3f83f224 185
3f83f224 186 Int_t ntrk=event->GetNumberOfTracks();
187 cerr<<"Number of ESD tracks : "<<ntrk<<endl;
188
4fa1e629 189
190 if (tofTree->GetEvent(e++)==0) {
191 cerr<<"No reconstructable tracks !\n";
192 continue;
193 }
194
195 Int_t ngood=refs->GetEntriesFast();
196
3f83f224 197 Int_t matched=0;
198 Int_t mismatched=0;
199 for (Int_t i=0; i<ngood; i++) {
4fa1e629 200 AliTrackReference *ref=(AliTrackReference*)refs->UncheckedAt(i);
201 Int_t lab=ref->Label();
202 Float_t ptg=TMath::Sqrt(ref->Px()*ref->Px() + ref->Py()*ref->Py());
3f83f224 203
4fa1e629 204 Double_t tgl=ref->Pz()/ptg; //tan(lambda)
3f83f224 205
206 if (ptg>pmin) { hgood->Fill(ptg); hgoo->Fill(tgl); }
207
208 Int_t j;
209 AliESDtrack *t=0;
210 for (j=0; j<ntrk; j++) {
211 AliESDtrack *tt=event->GetTrack(j);
212 if (lab!=TMath::Abs(tt->GetLabel())) continue;
213 t=tt;
214 //if ((tt->GetStatus()&AliESDtrack::kTOFpid) == 0) continue;
215 if (tt->GetTOFsignal() < 0) continue;
216 UInt_t idx=tt->GetTOFcluster();
217 if ((Int_t)idx>=nd) {
f4af68da 218 cerr<<"Wrong cluster index ! "<<idx<<endl;
3f83f224 219 return 5;
220 }
f4af68da 221 AliTOFcluster *cls=(AliTOFcluster*)clusters->UncheckedAt(idx);
222 if (cls->GetLabel(0)!=lab)
223 if (cls->GetLabel(1)!=lab)
224 if (cls->GetLabel(2)!=lab) {
3f83f224 225 mismatched++;
226 if (ptg>pmin) { hfake->Fill(ptg); hfak->Fill(tgl); }
227 break;
228 }
229 if (ptg>pmin) { hfound->Fill(ptg); hfoun->Fill(tgl); }
230 matched++;
231 break;
232 }
233 if (j==ntrk) {
234 cerr<<"Not matched: "<<lab<<" ";
235 if (t) {
236 cerr<<(t->GetStatus()&AliESDtrack::kITSout)<<' '
237 <<(t->GetStatus()&AliESDtrack::kTPCout)<<' '
238 <<(t->GetStatus()&AliESDtrack::kTRDout)<<' '
239 <<(t->GetStatus()&AliESDtrack::kTIME);
240 } else cerr<<"No ESD track !";
241 cerr<<endl;
242 }
243 }
4fa1e629 244 cout<<"Number of good tracks: "<<ngood<<endl;
245 cout<<"Number of matched tracks: "<<matched<<endl;
246 cout<<"Number of mismatched tracks: "<<mismatched<<endl;
3f83f224 247
4fa1e629 248 allgood+=ngood; allmatched+=matched; allmismatched+=mismatched;
249
250 refs->Clear();
4fa1e629 251 } //***** End of the loop over events
3f83f224 252
8b462fd8 253 delete event;
4fa1e629 254 ef->Close();
255
256 delete tofTree;
257 refFile->Close();
258
259 if (allgood!=0) cerr<<"\n\nEfficiency: "<<Float_t(allmatched)/allgood<<endl;
260 cout<<"Total number of good tracks: "<<allgood<<endl;
261 cout<<"Total number of matched tracks: "<<allmatched<<endl;
262 cout<<"Total number of mismatched tracks: "<<allmismatched<<endl;
263
264 TCanvas *c1=(TCanvas*)gROOT->FindObject("c1");
265 if (!c1) {
266 c1=new TCanvas("c1","",0,0,600,900);
267 c1->Divide(1,2);
268 }
269 hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
270 hgp->Divide(hfound,hgood,1,1.,"b");
271 hfp->Divide(hfake,hgood,1,1.,"b");
272 hgp->SetMaximum(1.4);
273 hgp->SetYTitle("Matching efficiency");
274 hgp->SetXTitle("Pt (GeV/c)");
275
276 hfoun->Sumw2(); hgoo->Sumw2(); hfak->Sumw2();
277 hgl->Divide(hfoun,hgoo,1,1.,"b");
278 hfl->Divide(hfak,hgoo,1,1.,"b");
279 hgl->SetMaximum(1.4);
280 hgl->SetYTitle("Matching efficiency");
281 hgl->SetXTitle("Tan(lambda)");
282
283 c1->cd(1);
284
285 hgp->Draw();
286 hfp->Draw("histsame");
287 TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
288 line1->Draw("same");
289 TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
290 line2->Draw("same");
291
292 c1->cd(2);
293
294 hgl->Draw();
295 hfl->Draw("histsame");
296 TLine *line3 = new TLine(-1,1.0,1,1.0); line3->SetLineStyle(4);
297 line3->Draw("same");
298 TLine *line4 = new TLine(-1,0.9,1,0.9); line4->SetLineStyle(4);
299 line4->Draw("same");
300
301 c1->Update();
302
3f7a17bb 303 TFile fc("AliTOFComparison.root","RECREATE");
304 c1->Write();
305 fc.Close();
306
4fa1e629 307 gBenchmark->Stop("AliTOFComparison");
308 gBenchmark->Show("AliTOFComparison");
309
310 delete rl;
3f83f224 311 return 0;
312}
313
4fa1e629 314Int_t GoodTracksTOF(const Char_t *dir) {
315 if (gAlice) {
316 delete gAlice->GetRunLoader();
317 delete gAlice;//if everything was OK here it is already NULL
318 gAlice = 0x0;
3f83f224 319 }
320
4fa1e629 321 Char_t fname[100];
322 sprintf(fname,"%s/galice.root",dir);
3f83f224 323
4fa1e629 324 AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
325 if (!rl) {
326 ::Error("GoodTracksTOF","Can't start session !");
327 return 1;
328 }
3f83f224 329
330 rl->LoadgAlice();
331 rl->LoadHeader();
4fa1e629 332 rl->LoadKinematics();
3f83f224 333
3f83f224 334
335 AliLoader* tofl = rl->GetLoader("TOFLoader");
336 if (tofl == 0x0) {
4fa1e629 337 ::Error("GoodTracksTOF","Can not get the TOF loader !");
338 delete rl;
339 return 2;
3f83f224 340 }
f4af68da 341 tofl->LoadRecPoints("read");
3f83f224 342
4fa1e629 343 Int_t nev=rl->GetNumberOfEvents();
344 ::Info("GoodTracksTOF","Number of events : %d\n",nev);
3f83f224 345
4fa1e629 346 sprintf(fname,"%s/GoodTracksITS.root",dir);
347 TFile *itsFile=TFile::Open(fname);
348 if ((!itsFile)||(!itsFile->IsOpen())) {
349 ::Error("GoodTracksTOF","Can't open the GoodTracksITS.root !");
350 delete rl;
351 return 5;
3f83f224 352 }
4fa1e629 353 TClonesArray dum("AliTrackReference",1000), *itsRefs=&dum;
354 TTree *itsTree=(TTree*)itsFile->Get("itsTree");
355 if (!itsTree) {
356 ::Error("GoodTracksTOF","Can't get the ITS reference tree !");
357 delete rl;
358 return 6;
359 }
360 TBranch *itsBranch=itsTree->GetBranch("ITS");
361 if (!itsBranch) {
362 ::Error("GoodTracksTOF","Can't get the ITS reference branch !");
363 delete rl;
364 return 7;
365 }
366 itsBranch->SetAddress(&itsRefs);
367
368
369 sprintf(fname,"%s/GoodTracksTOF.root",dir);
370 TFile *tofFile=TFile::Open(fname,"recreate");
371 TClonesArray dummy("AliTrackReference",1000), *tofRefs=&dummy;
372 TTree tofTree("tofTree","Tree with info about the reconstructable TOF tracks");
373 tofTree.Branch("TOF",&tofRefs);
374
375
376 //******** Loop over generated events
377 for (Int_t e=0; e<nev; e++) {
3f83f224 378
4fa1e629 379 rl->GetEvent(e); tofFile->cd();
380
381 Int_t np = rl->GetHeader()->GetNtrack();
382 cout<<"Event "<<e<<" Number of particles: "<<np<<endl;
383
384 //******** Fill the "good" masks
385 Int_t *good=new Int_t[np]; Int_t k; for (k=0; k<np; k++) good[k]=0;
386
f4af68da 387 TTree *cTree=tofl->TreeR();
388 if (!cTree) {
4fa1e629 389 ::Error("GoodTracksTOF","Can't get the TOF cluster tree !");
390 delete rl;
391 return 8;
392 }
f4af68da 393 TBranch *branch=cTree->GetBranch("TOF");
4fa1e629 394 if (!branch) {
395 ::Error("GoodTracksTOF","Can't get the branch with the TOF digits !");
396 return 9;
397 }
f4af68da 398 TClonesArray dummy("AliTOFcluster",10000), *clusters=&dummy;
399 branch->SetAddress(&clusters);
3f83f224 400
f4af68da 401 cTree->GetEvent(0);
402 Int_t nd=clusters->GetEntriesFast();
4fa1e629 403
404 for (Int_t i=0; i<nd; i++) {
f4af68da 405 AliTOFcluster *c=(AliTOFcluster*)clusters->UncheckedAt(i);
406 Int_t l0=c->GetLabel(0);
4fa1e629 407 if (l0>=np) {cerr<<"Wrong label: "<<l0<<endl; continue;}
f4af68da 408 Int_t l1=c->GetLabel(1);
4fa1e629 409 if (l1>=np) {cerr<<"Wrong label: "<<l1<<endl; continue;}
f4af68da 410 Int_t l2=c->GetLabel(2);
4fa1e629 411 if (l2>=np) {cerr<<"Wrong label: "<<l2<<endl; continue;}
412 if (l0>=0) good[l0]++;
413 if (l1>=0) good[l1]++;
414 if (l2>=0) good[l2]++;
415 }
f4af68da 416 clusters->Clear();
4fa1e629 417
418
419 //****** select tracks which are "good" enough
420 AliStack* stack = rl->Stack();
421
422 itsTree->GetEvent(e);
423 Int_t nk=itsRefs->GetEntriesFast();
424
425 Int_t nt=0;
426 for (k=0; k<nk; k++) {
427 AliTrackReference *itsRef=(AliTrackReference *)itsRefs->UncheckedAt(k);
428 Int_t lab=itsRef->Label();
429 if (good[lab] == 0) continue;
430 TParticle *p = (TParticle*)stack->Particle(lab);
431 if (p == 0x0) {
432 cerr<<"Can not get particle "<<lab<<endl;
433 continue;
434 }
435
436 if (TMath::Abs(p->Vx())>0.1) continue;
437 if (TMath::Abs(p->Vy())>0.1) continue;
438 //if (TMath::Abs(p->Vz())>0.1) continue;
439
440 new((*tofRefs)[nt]) AliTrackReference(*itsRef);
441 nt++;
442 }
443 itsRefs->Clear();
444
445 tofTree.Fill();
446 tofRefs->Clear();
447
448 delete[] good;
449
450 } //*** end of the loop over generated events
451
452 tofTree.Write();
453 tofFile->Close();
454
455 delete itsTree;
456 itsFile->Close();
457
458 delete rl;
459 return 0;
3f83f224 460}