doxy: consider comments after last line
[u/mrichter/AliRoot.git] / ITS / AliITSComparisonV2.C
1 /****************************************************************************
2  *           Very important, delicate and rather obscure macro.             *
3  *                                                                          *
4  *               Creates list of "trackable" tracks,                        *
5  *             calculates efficiency, resolutions etc.                      *
6  *  The ESD tracks must be in an appropriate state (kITSin or kITSrefit)    *
7  *                                                                          *
8  *           Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch                 *
9  ****************************************************************************/
10
11 #if !defined(__CINT__) || defined(__MAKECINT__)
12   #include <TMath.h>
13   #include <TError.h>
14   #include <Riostream.h>
15   #include <TH1F.h>
16   #include <TH2F.h>
17   #include <TTree.h>
18   #include <TParticle.h>
19   #include <TCanvas.h>
20   #include <TLine.h>
21   #include <TText.h>
22   #include <TBenchmark.h>
23   #include <TStyle.h>
24   #include <TFile.h>
25   #include <TROOT.h>
26
27   #include "AliStack.h"
28   #include "AliHeader.h"
29   #include "AliTrackReference.h"
30   #include "AliRunLoader.h"
31   #include "AliRun.h"
32   #include "AliESDEvent.h"
33   #include "AliESDtrack.h"
34
35   #include "AliITSRecPoint.h"
36   #include "AliITSLoader.h"
37 #endif
38
39 Int_t GoodTracksITS(const Char_t *dir=".");
40
41 extern AliRun *gAlice;
42 extern TBenchmark *gBenchmark;
43 extern TROOT *gROOT;
44
45 static Int_t allgood=0;
46 static Int_t allselected=0;
47 static Int_t allfound=0;
48
49 Int_t AliITSComparisonV2
50 (Float_t ptcutl=0.2, Float_t ptcuth=10., const Char_t *dir=".") {
51    gBenchmark->Start("AliITSComparisonV2");
52
53    ::Info("AliITSComparisonV2.C","Doing comparison...");
54    
55
56    TH1F *hp=(TH1F*)gROOT->FindObject("hp");
57    if (!hp) hp=new TH1F("hp","PHI resolution",50,-20.,20.); 
58    hp->SetFillColor(4);
59
60    TH1F *hl=(TH1F*)gROOT->FindObject("hl");
61    if (!hl) hl=new TH1F("hl","LAMBDA resolution",50,-20,20);
62    hl->SetFillColor(4);
63
64    TH1F *hpt=(TH1F*)gROOT->FindObject("hpt");
65    if (!hpt) hpt=new TH1F("hpt","Relative Pt resolution",30,-10.,10.); 
66    hpt->SetFillColor(2);
67  
68    TH1F *hmpt=(TH1F*)gROOT->FindObject("hmpt");
69    if (!hmpt) 
70       hmpt=new TH1F("hmpt","Transverse impact parameter",30,-777,777); 
71    hmpt->SetFillColor(6);
72
73    TH1F *hz=(TH1F*)gROOT->FindObject("hz");
74    if (!hz) hz=new TH1F("hz","Longitudinal impact parameter",30,-777,777); 
75
76
77
78    TH1F *hgood=(TH1F*)gROOT->FindObject("hgood");
79    if (!hgood) hgood=new TH1F("hgood","Good tracks",30,0.2,6.1);
80     
81    TH1F *hfound=(TH1F*)gROOT->FindObject("hfound");
82    if (!hfound) hfound=new TH1F("hfound","Found tracks",30,0.2,6.1);
83
84    TH1F *hfake=(TH1F*)gROOT->FindObject("hfake");
85    if (!hfake) hfake=new TH1F("hfake","Fake tracks",30,0.2,6.1);
86
87    TH1F *hg=(TH1F*)gROOT->FindObject("hg");
88    if (!hg) hg=new TH1F("hg","Efficiency for good tracks",30,0.2,6.1);
89    hg->SetLineColor(4); hg->SetLineWidth(2);
90
91    TH1F *hf=(TH1F*)gROOT->FindObject("hf");
92    if (!hf) hf=new TH1F("hf","Efficiency for fake tracks",30,0.2,6.1);
93    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
94
95    TH1F *he=(TH1F*)gROOT->FindObject("he");
96    if (!he) 
97       he =new TH1F("he","dE/dX for pions with 0.4<p<0.5 GeV/c",50,0.,200.);
98
99    TH2F *hep=(TH2F*)gROOT->FindObject("hep");
100    if (!hep) hep=new TH2F("hep","dE/dX vs momentum",50,0.,2.,50,0.,400.);
101    hep->SetMarkerStyle(8);
102    hep->SetMarkerSize(0.4);
103
104
105    Char_t fname[100];
106    sprintf(fname,"%s/GoodTracksITS.root",dir);
107
108    TFile *refFile=TFile::Open(fname,"old");
109    if (!refFile || !refFile->IsOpen()) {
110    ::Info("AliITSComparisonV2.C","Marking good tracks (will take a while)...");
111      if (GoodTracksITS(dir)) {
112         ::Error("AliITSComparisonV2.C","Can't generate the reference file !");
113         return 1;
114      }
115    }
116    refFile=TFile::Open(fname,"old");
117    if (!refFile || !refFile->IsOpen()) {
118      ::Error("AliITSComparisonV2.C","Can't open the reference file !");
119      return 1;
120    }   
121   
122    TTree *itsTree=(TTree*)refFile->Get("itsTree");
123    if (!itsTree) {
124      ::Error("AliITSComparisonV2.C","Can't get the reference tree !");
125      return 2;
126    }
127    TBranch *branch=itsTree->GetBranch("ITS");
128    if (!branch) {
129      ::Error("AliITSComparisonV2.C","Can't get the ITS branch !");
130      return 3;
131    }
132    TClonesArray dummy("AliTrackReference",1000), *refs=&dummy;
133    branch->SetAddress(&refs);
134
135
136    sprintf(fname,"%s/AliESDs.root",dir);
137    TFile *ef=TFile::Open(fname);
138    if ((!ef)||(!ef->IsOpen())) {
139       sprintf(fname,"%s/AliESDits.root",dir);
140       ef=TFile::Open(fname);
141       if ((!ef)||(!ef->IsOpen())) {
142          ::Error("AliITSComparisonV2.C","Can't open AliESDits.root !");
143          return 4;
144       }
145    }
146    AliESDEvent* event = new AliESDEvent();
147    TTree* esdTree = (TTree*) ef->Get("esdTree");
148    if (!esdTree) {
149       ::Error("AliITSComparison.C", "no ESD tree found");
150       return 6;
151    }
152    event->ReadFromTree(esdTree);
153
154
155    //******* Loop over events *********
156    Int_t e=0;
157    while (esdTree->GetEvent(e)) {
158      cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
159  
160      Int_t nentr=event->GetNumberOfTracks();
161      allfound+=nentr;
162
163      if (itsTree->GetEvent(e++)==0) {
164         cerr<<"No reconstructable tracks !\n";
165         continue;
166      }
167
168      Int_t ngood=refs->GetEntriesFast(); 
169      allgood+=ngood;
170
171      const Int_t MAX=15000;
172      Int_t notf[MAX], nnotf=0;
173      Int_t fake[MAX], nfake=0;
174      Int_t mult[MAX], numb[MAX], nmult=0;
175      Int_t k;
176      for (k=0; k<ngood; k++) {
177         AliTrackReference *ref=(AliTrackReference*)refs->UncheckedAt(k); 
178         Int_t lab=ref->Label(), tlab=-1;
179         Float_t ptg=TMath::Sqrt(ref->Px()*ref->Px() + ref->Py()*ref->Py());
180
181         if (ptg<ptcutl) continue;
182         if (ptg>ptcuth) continue;
183
184         allselected++;
185
186         hgood->Fill(ptg);
187
188         AliESDtrack *esd=0;
189         Int_t cnt=0;
190         for (Int_t i=0; i<nentr; i++) {
191            AliESDtrack *t=event->GetTrack(i);
192            UInt_t status=t->GetStatus();
193
194            if ((status&AliESDtrack::kITSrefit)==0) continue;
195
196            Int_t lbl=t->GetLabel();
197            if (lab==TMath::Abs(lbl)) {
198               if (cnt==0) {esd=t; tlab=lbl;}
199               cnt++;
200            }
201         }
202         if (cnt==0) {
203            notf[nnotf++]=lab;
204            continue;
205         } else if (cnt>1){
206            mult[nmult]=lab;
207            numb[nmult]=cnt; nmult++;        
208         }
209
210         if (lab==tlab) hfound->Fill(ptg);
211         else {
212           fake[nfake++]=lab;
213           hfake->Fill(ptg); 
214         }
215
216         Double_t alpha=esd->GetAlpha(),xv,par[5]; 
217         esd->GetExternalParameters(xv,par);
218         Float_t phi=TMath::ASin(par[2]) + alpha;
219         if (phi<-TMath::Pi()) phi+=2*TMath::Pi();
220         if (phi>=TMath::Pi()) phi-=2*TMath::Pi();
221         Float_t lam=TMath::ATan(par[3]); 
222         Float_t pt_1=TMath::Abs(par[4]);
223
224         Float_t phig=TMath::ATan2(ref->Py(),ref->Px());
225         hp->Fill((phi - phig)*1000.);
226
227         Float_t lamg=TMath::ATan2(ref->Pz(),ptg);
228         hl->Fill((lam - lamg)*1000.);
229
230         Float_t d,z; esd->GetImpactParameters(d,z);
231         hmpt->Fill(10000*d);
232         hz->Fill(10000*z);
233
234         hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
235
236         Float_t mom=1./(pt_1*TMath::Cos(lam));
237         Float_t dedx=esd->GetITSsignal();
238         hep->Fill(mom,dedx,1.);
239
240         Int_t pdg=(Int_t)ref->GetLength();  //this is particle's PDG !
241
242         if (TMath::Abs(pdg)==211) //pions
243            if (mom>0.4 && mom<0.5) he->Fill(dedx,1.);
244
245      }
246
247      cout<<"\nList of Not found tracks :\n";
248      for (k=0; k<nnotf; k++){
249        cout<<notf[k]<<"\t";
250        if ((k%9)==8) cout<<"\n";
251      }
252      cout<<"\n\nList of fake  tracks :\n";
253      for (k=0; k<nfake; k++){
254        cout<<fake[k]<<"\t";
255        if ((k%9)==8) cout<<"\n";
256      }
257      cout<<"\n\nList of multiple found tracks :\n";
258      for (k=0; k<nmult; k++) {
259          cout<<"id.   "<<mult[k]
260              <<"     found - "<<numb[k]<<"times\n";
261      }
262      cout<<endl;
263
264      cout<<"Number of found tracks : "<<nentr<<endl;
265      cout<<"Number of \"good\" tracks : "<<ngood<<endl;
266
267      refs->Clear();
268    } //***** End of the loop over events
269
270    delete event;
271    delete esdTree;
272    ef->Close();
273    
274    delete itsTree;
275    refFile->Close();
276
277    Stat_t ng=hgood->GetEntries(), nf=hfound->GetEntries();
278    if (ng!=0) cout<<"\n\nIntegral efficiency is about "<<nf/ng*100.<<" %\n";
279    cout<<"Total number selected of \"good\" tracks ="<<allselected<<endl<<endl;
280    cout<<"Total number of found tracks ="<<allfound<<endl;
281    cout<<"Total number of \"good\" tracks ="<<allgood<<endl;
282    cout<<endl;
283
284    gStyle->SetOptStat(111110);
285    gStyle->SetOptFit(1);
286
287    TCanvas *c1=new TCanvas("c1","",0,0,700,850);
288
289    Int_t minc=33; 
290
291    TPad *p1=new TPad("p1","",0,0.3,.5,.6); p1->Draw();
292    p1->cd(); p1->SetFillColor(42); p1->SetFrameFillColor(10); 
293    hp->SetFillColor(4);  hp->SetXTitle("(mrad)"); 
294    if (hp->GetEntries()<minc) hp->Draw(); else hp->Fit("gaus"); c1->cd();
295
296    TPad *p2=new TPad("p2","",0.5,.3,1,.6); p2->Draw(); 
297    p2->cd(); p2->SetFillColor(42); p2->SetFrameFillColor(10);
298    hl->SetXTitle("(mrad)");
299    if (hl->GetEntries()<minc) hl->Draw(); else hl->Fit("gaus"); c1->cd();
300
301    TPad *p3=new TPad("p3","",0,0,0.5,0.3); p3->Draw();
302    p3->cd(); p3->SetFillColor(42); p3->SetFrameFillColor(10); 
303    hpt->SetXTitle("(%)");
304    if (hpt->GetEntries()<minc) hpt->Draw(); else hpt->Fit("gaus"); c1->cd();
305
306    TPad *p4=new TPad("p4","",0.5,0,1,0.3); p4->Draw();
307    p4->cd(); p4->SetFillColor(42); p4->SetFrameFillColor(10);
308    hmpt->SetXTitle("(micron)");
309    if (hmpt->GetEntries()<minc) hmpt->Draw(); else hmpt->Fit("gaus"); 
310    hz->Draw("same"); c1->cd();
311    
312
313    TPad *p5=new TPad("p5","",0,0.6,1,1); p5->Draw(); p5->cd(); 
314    p5->SetFillColor(41); p5->SetFrameFillColor(10);
315    hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
316    hg->Divide(hfound,hgood,1,1.,"b");
317    hf->Divide(hfake,hgood,1,1.,"b");
318    hg->SetMaximum(1.4);
319    hg->SetYTitle("Tracking efficiency");
320    hg->SetXTitle("Pt (GeV/c)");
321    hg->Draw();
322
323    TLine *line1 = new TLine(0.2,1.0,6.1,1.0); line1->SetLineStyle(4);
324    line1->Draw("same");
325    TLine *line2 = new TLine(0.2,0.9,6.1,0.9); line2->SetLineStyle(4);
326    line2->Draw("same");
327
328    hf->SetFillColor(1);
329    hf->SetFillStyle(3013);
330    hf->SetLineColor(2);
331    hf->SetLineWidth(2);
332    hf->Draw("histsame");
333    TText *text = new TText(0.461176,0.248448,"Fake tracks");
334    text->SetTextSize(0.05);
335    text->Draw();
336    text = new TText(0.453919,1.11408,"Good tracks");
337    text->SetTextSize(0.05);
338    text->Draw();
339
340    TCanvas *c2=new TCanvas("c2","",320,32,530,590);
341    TPad *p6=new TPad("p6","",0.,0.,1.,.5); p6->Draw();
342    p6->cd(); p6->SetFillColor(42); p6->SetFrameFillColor(10); 
343    he->SetFillColor(2); he->SetFillStyle(3005);  
344    he->SetXTitle("Arbitrary Units"); 
345    if (he->GetEntries()<minc) he->Draw(); else he->Fit("gaus"); c2->cd();
346
347    TPad *p7=new TPad("p7","",0.,0.5,1.,1.); p7->Draw(); 
348    p7->cd(); p7->SetFillColor(42); p7->SetFrameFillColor(10);
349    hep->SetXTitle("p (Gev/c)"); hep->SetYTitle("dE/dX (Arb. Units)"); 
350    hep->Draw(); c1->cd();
351
352    TFile fc("AliITSComparisonV2.root","RECREATE");
353    c1->Write();
354    c2->Write();
355    fc.Close();
356
357    gBenchmark->Stop("AliITSComparisonV2");
358    gBenchmark->Show("AliITSComparisonV2");
359
360    return 0;
361 }
362
363
364
365 Int_t GoodTracksITS(const Char_t *dir) {
366    if (gAlice) { 
367        delete AliRunLoader::Instance();
368        delete gAlice;//if everything was OK here it is already NULL
369        gAlice = 0x0;
370    }
371
372    Char_t fname[100];
373    sprintf(fname,"%s/galice.root",dir);
374
375    AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
376    if (!rl) {
377       ::Error("GoodTracksITS","Can't start session !");
378       return 1;
379    }
380
381    rl->LoadgAlice();
382    rl->LoadHeader();
383    rl->LoadKinematics();
384
385    AliITSLoader* itsl = (AliITSLoader*)rl->GetLoader("ITSLoader");
386    if (itsl == 0x0) {
387        ::Error("GoodTracksITS","Can not find the ITSLoader");
388        delete rl;
389        return 4;
390    }
391    itsl->LoadRecPoints();
392   
393
394    Int_t nev=rl->GetNumberOfEvents();
395    ::Info("GoodTracksITS","Number of events : %d\n",nev);  
396
397    sprintf(fname,"%s/GoodTracksTPC.root",dir);
398    TFile *tpcFile=TFile::Open(fname);
399    if ((!tpcFile)||(!tpcFile->IsOpen())) {
400        ::Error("GoodTracksITS","Can't open the GoodTracksTPC.root !");
401        delete rl;
402        return 5; 
403    }
404    TClonesArray dum("AliTrackReference",1000), *tpcRefs=&dum;
405    TTree *tpcTree=(TTree*)tpcFile->Get("tpcTree");
406    if (!tpcTree) {
407        ::Error("GoodTracksITS","Can't get the TPC reference tree !");
408        delete rl;
409        return 6;
410    }
411    TBranch *tpcBranch=tpcTree->GetBranch("TPC");
412    if (!tpcBranch) {
413       ::Error("GoodTracksITS","Can't get the TPC reference branch !");
414       delete rl;
415       return 7;
416    }
417    tpcBranch->SetAddress(&tpcRefs);
418
419    sprintf(fname,"%s/GoodTracksITS.root",dir);
420    TFile *itsFile=TFile::Open(fname,"recreate");
421    TClonesArray dummy2("AliTrackReference",1000), *itsRefs=&dummy2;
422    TTree itsTree("itsTree","Tree with info about the reconstructable ITS tracks");
423    itsTree.Branch("ITS",&itsRefs);
424
425    //********  Loop over generated events 
426    for (Int_t e=0; e<nev; e++) {
427      Int_t k;
428
429      rl->GetEvent(e);  itsFile->cd();
430
431      Int_t np = rl->GetHeader()->GetNtrack();
432      cout<<"Event "<<e<<" Number of particles: "<<np<<endl;
433
434      //******** Fill the "good" masks
435      Int_t *good=new Int_t[np]; for (k=0; k<np; k++) good[k]=0;
436
437      TTree *cTree=itsl->TreeR();
438      if (!cTree) {
439         ::Error("GoodTracksITS","Can't get the cluster tree !"); 
440         delete rl;
441         return 8;
442      }
443      TBranch *branch=cTree->GetBranch("ITSRecPoints");
444      if (!branch) {
445         ::Error("GoodTracksITS","Can't get the clusters branch !"); 
446         delete rl;
447         return 9;
448      }
449      TClonesArray dummy("AliITSRecPoint",10000), *clusters=&dummy;
450      branch->SetAddress(&clusters);
451
452      Int_t entr=(Int_t)cTree->GetEntries();
453      for (k=0; k<entr; k++) {
454          cTree->GetEvent(k);
455          Int_t ncl=clusters->GetEntriesFast(); if (ncl==0) continue;
456          while (ncl--) {
457             AliITSRecPoint *pnt=(AliITSRecPoint*)clusters->UncheckedAt(ncl);
458
459             Int_t lay=pnt->GetLayer();
460             if (lay<0 || lay>5) {
461                ::Error("GoodTracksITS","Wrong layer !");
462                delete rl;
463                return 10;
464             }
465
466             Int_t l0=pnt->GetLabel(0);
467                if (l0>=np) {
468 //               cerr<<"Wrong label: "<<l0<<endl;
469                  continue;
470                }
471             Int_t l1=pnt->GetLabel(1);
472                if (l1>=np) {
473 //               cerr<<"Wrong label: "<<l1<<endl;
474                  continue;
475                }
476             Int_t l2=pnt->GetLabel(2);
477                if (l2>=np) {
478 //               cerr<<"Wrong label: "<<l2<<endl;
479                  continue;
480                }
481             Int_t mask=1<<lay;
482             if (l0>=0) good[l0]|=mask; 
483             if (l1>=0) good[l1]|=mask; 
484             if (l2>=0) good[l2]|=mask;
485          }
486          clusters->Clear();
487      }
488    
489
490      //****** select tracks which are "good" enough
491      AliStack* stack = rl->Stack();
492
493      tpcTree->GetEvent(e);
494      Int_t nk=tpcRefs->GetEntriesFast();
495      Int_t nt=0;
496      for (k=0; k<nk; k++) {
497         AliTrackReference *tpcRef=(AliTrackReference *)tpcRefs->UncheckedAt(k);
498         Int_t lab=tpcRef->Label();
499         if (good[lab] != 0x3F) continue;
500         TParticle *p = (TParticle*)stack->Particle(lab);
501         if (p == 0x0) {
502            cerr<<"Can not get particle "<<lab<<endl;
503            continue;
504         }
505
506         AliTrackReference *ref=new((*itsRefs)[nt]) AliTrackReference(*tpcRef);
507         ref->SetMomentum(p->Px(),p->Py(),p->Pz());
508         ref->SetPosition(p->Vx(),p->Vy(),p->Vz());
509         nt++;
510      }
511      tpcRefs->Clear();
512
513      itsTree.Fill();
514      itsRefs->Clear();
515
516      delete[] good;
517
518    } //*** end of the loop over generated events
519
520    itsTree.Write();
521    itsFile->Close();
522
523    delete tpcTree;
524    tpcFile->Close();
525
526    delete rl;
527    return 0;
528 }
529
530