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