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