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