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