ALIROOT-5486 Propagate MUON trigger deviation sign to AliAODTrack
[u/mrichter/AliRoot.git] / STEER / AliV0Comparison.C
1 /****************************************************************************
2  *           Very important, delicate and rather obscure macro.             *
3  *                                                                          *
4  *                  Creates list of "findable" V0s,                         *
5  *             calculates efficiency, resolutions etc.                      *
6  *                                                                          *
7  *   To get the list of the "findable" V0s, you should first get the list   *
8  *   of "findable" tracks, which can be done by running                     *
9  *   TPC/AliTPCComparison.C and ITS/AliITSComparisonV2.C macros.            *
10  *                                                                          *
11  *   Origin: I.Belikov, IReS, Strasbourg, Jouri.Belikov@cern.ch             *
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 <TTree.h>
20   #include <TParticle.h>
21   #include <TPDGCode.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 "AliESDv0.h"
37 #endif
38
39 Int_t GoodV0s(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 allfound=0;
47
48 Int_t AliV0Comparison(Int_t code=310, const Char_t *dir=".") { 
49    //Lambda=3122, LambdaBar=-3122
50    gBenchmark->Start("AliV0Comparison");
51
52    ::Info("AliV0Comparison.C","Doing comparison...");
53
54
55    const Double_t V0window=0.05;
56    Double_t ptncut=0.13, ptpcut=0.13, kinecut=0.03; 
57    Double_t V0mass=0.497672, V0width=0.020;
58    switch (code) {
59    case kK0Short: 
60         break;
61    case kLambda0:    
62         V0mass=1.115683; V0width=0.015; ptpcut=0.50; kinecut=0.002; 
63         break;
64    case kLambda0Bar: 
65         V0mass=1.115683; V0width=0.015; ptncut=0.50; kinecut=0.002;
66         break;
67    default: cerr<<"Invalid PDG code !\n"; return 1;
68    }
69
70    TH1F *hp=(TH1F*)gROOT->FindObject("hp");
71    if (!hp) hp=new TH1F("hp","PHI Resolution",30,-30.,30.);
72    hp->SetXTitle("(mrad)"); hp->SetFillColor(2);
73
74    TH1F *hl=(TH1F*)gROOT->FindObject("hl");
75    if (!hl) hl=new TH1F("hl","LAMBDA Resolution",30,-30,30);
76    hl->SetXTitle("(mrad)"); hl->SetFillColor(1); hl->SetFillStyle(3013);
77  
78    TH1F *hpt=(TH1F*)gROOT->FindObject("hpt");
79    if (!hpt) hpt=new TH1F("hpt","Relative Pt Resolution",30,-10.,10.); 
80    hpt->SetXTitle("(%)"); hpt->SetFillColor(2); 
81
82    TH1F *hx=(TH1F*)gROOT->FindObject("hx");
83    if (!hx) hx=new TH1F("hx","Position Resolution (X)",30,-3.,3.);
84    hx->SetXTitle("(mm)"); hx->SetFillColor(6);
85
86    TH1F *hy=(TH1F*)gROOT->FindObject("hy");
87    if (!hy) hy=new TH1F("hy","Position Resolution (Y)",30,-3.,3.); 
88    hy->SetXTitle("(mm)"); hy->SetFillColor(1); hy->SetFillStyle(3013);
89
90    TH1F *hz=(TH1F*)gROOT->FindObject("hz");
91    if (!hz) hz=new TH1F("hz","Position Resolution (Z)",30,-3.,3.);
92    hz->SetXTitle("(mm)"); hz->SetFillColor(6);
93
94
95    Double_t pmin=0.2, pmax=4.2; Int_t nchan=20;
96    TH1F *hgood=(TH1F*)gROOT->FindObject("hgood");    
97    if (!hgood) hgood=new TH1F("hgood","Good Vertices",nchan,pmin,pmax);
98     
99    TH1F *hfound=(TH1F*)gROOT->FindObject("hfound");
100    if (!hfound) hfound=new TH1F("hfound","Found Vertices",nchan,pmin,pmax);
101
102    TH1F *hfake=(TH1F*)gROOT->FindObject("hfake");
103    if (!hfake) hfake=new TH1F("hfake","Fake Vertices",nchan,pmin,pmax);
104
105    TH1F *hg=(TH1F*)gROOT->FindObject("hg");
106    if (!hg) hg=new TH1F("hg","Efficiency for Good Vertices",nchan,pmin,pmax);
107    hg->SetLineColor(4); hg->SetLineWidth(2);
108
109    TH1F *hf=(TH1F*)gROOT->FindObject("hf");
110    if (!hf) hf=new TH1F("hf","Probability of Fake Vertices",nchan,pmin,pmax);
111    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
112
113
114    Double_t mmin=V0mass-V0window, mmax=V0mass+V0window;
115    TH1F *v0s=(TH1F*)gROOT->FindObject("v0s");
116    if (!v0s) v0s=new TH1F("v0s","V0s Effective Mass",40, mmin, mmax);
117    v0s->SetXTitle("(GeV)"); v0s->SetLineColor(4); v0s->SetLineWidth(4);
118
119    TH1F *v0sf=(TH1F*)gROOT->FindObject("v0sf");
120    if (!v0sf) v0sf=new TH1F("v0sf","Fake V0s Effective Mass",40, mmin, mmax);
121    v0sf->SetXTitle("(GeV)"); v0sf->SetFillColor(6);
122
123
124    Char_t fname[100];
125    sprintf(fname,"%s/GoodV0s.root",dir);
126
127    TFile *refFile=TFile::Open(fname,"old");
128    if (!refFile || !refFile->IsOpen()) {
129    ::Info("AliV0Comparison.C","Marking good V0s (will take a while)...");
130      if (GoodV0s(dir)) {
131         ::Error("AliV0Comparison.C","Can't generate the reference file !");
132         return 1;
133      }
134    }
135    refFile=TFile::Open(fname,"old");
136    if (!refFile || !refFile->IsOpen()) {
137      ::Error("AliV0Comparison.C","Can't open the reference file !");
138      return 1;
139    }   
140   
141    TTree *v0Tree=(TTree*)refFile->Get("v0Tree");
142    if (!v0Tree) {
143      ::Error("AliV0Comparison.C","Can't get the reference tree !");
144      return 2;
145    }
146    TBranch *pbranch=v0Tree->GetBranch("positive");
147    if (!pbranch) {
148      ::Error("AliV0Comparison.C","Can't get the positive daughter branch !");
149      return 3;
150    }
151    TClonesArray dummy("AliTrackReference",1000), *prefs=&dummy;
152    pbranch->SetAddress(&prefs);
153
154    TBranch *nbranch=v0Tree->GetBranch("negative");
155    if (!nbranch) {
156      ::Error("AliV0Comparison.C","Can't get the negative daughter branch !");
157      return 4;
158    }
159    TClonesArray dumm("AliTrackReference",1000), *nrefs=&dumm;
160    nbranch->SetAddress(&nrefs);
161
162    
163    sprintf(fname,"%s/AliESDs.root",dir);
164    TFile *ef=TFile::Open(fname);
165    if ((!ef)||(!ef->IsOpen())) {
166       sprintf(fname,"%s/AliESDv0.root",dir);
167       ef=TFile::Open(fname);
168       if ((!ef)||(!ef->IsOpen())) {
169          ::Error("AliV0Comparison.C","Can't open AliESDv0.root !");
170          return 5;
171       }
172    }
173    AliESDEvent* event = new AliESDEvent();
174    TTree* esdTree = (TTree*) ef->Get("esdTree");
175    if (!esdTree) {
176       ::Error("AliV0Comparison.C", "no ESD tree found");
177       return 6;
178    }
179    event->ReadFromTree(esdTree);
180
181
182    //******* Loop over events *********
183    Int_t e=0;
184    while (esdTree->GetEvent(e)) {
185       cout<<endl<<endl<<"********* Processing event number: "<<e<<"*******\n";
186  
187       Int_t nentr=event->GetNumberOfV0s();
188       allfound+=nentr;
189
190       if (v0Tree->GetEvent(e++)==0) {
191          cerr<<"No reconstructable V0s !\n";
192          continue;
193       }
194
195       Int_t ngood=prefs->GetEntriesFast(),ng=0; 
196
197       Double_t pxg=0.,pyg=0.,pzg=0.,ptg=0.;
198       Int_t nlab=-1, plab=-1;
199       Int_t i;
200       for (i=0; i<nentr; i++) {
201           AliESDv0 *vertex=event->GetV0(i);
202
203           if (vertex->GetOnFlyStatus()) continue;
204
205           Int_t nidx=TMath::Abs(vertex->GetNindex());
206           Int_t pidx=TMath::Abs(vertex->GetPindex());
207
208           AliESDtrack *ntrack=event->GetTrack(nidx);
209           AliESDtrack *ptrack=event->GetTrack(pidx);
210
211           nlab=TMath::Abs(ntrack->GetLabel());
212           plab=TMath::Abs(ptrack->GetLabel());
213
214           /** Kinematical cuts **/
215           Double_t pxn,pyn,pzn; vertex->GetNPxPyPz(pxn,pyn,pzn); 
216           Double_t ptn=TMath::Sqrt(pxn*pxn + pyn*pyn);
217           if (ptn < ptncut) continue;
218           Double_t pxp,pyp,pzp; vertex->GetPPxPyPz(pxp,pyp,pzp); 
219           Double_t ptp=TMath::Sqrt(pxp*pxp + pyp*pyp);
220           if (ptp < ptpcut) continue;
221           Double_t kine=vertex->ChangeMassHypothesis(code);
222           //if (TMath::Abs(kine)>kinecut) continue;
223
224           Double_t mass=vertex->GetEffMass();
225           v0s->Fill(mass);
226           v0sf->Fill(mass);
227
228           AliTrackReference *nref=0, *pref=0;
229           Int_t j;
230           for (j=0; j<ngood; j++) {
231               nref=(AliTrackReference*)nrefs->UncheckedAt(j);
232               pref=(AliTrackReference*)prefs->UncheckedAt(j);
233               if (nref->Label() == nlab)
234               if (pref->Label() == plab) break;
235           }
236
237           if (TMath::Abs(mass-V0mass)>V0width) continue;
238
239           Double_t px,py,pz; vertex->GetPxPyPz(px,py,pz);
240           Double_t pt=TMath::Sqrt(px*px+py*py);
241
242           if (j==ngood) {
243              hfake->Fill(pt);
244              cout<<"Fake vertex: ("<<nlab<<","<<plab<<")\n";
245              continue;
246           }
247           v0sf->Fill(mass,-1);
248
249           pxg=nref->Px()+pref->Px(); pyg=nref->Py()+pref->Py();
250           pzg=nref->Pz()+pref->Pz(); 
251           ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
252           Double_t phig=TMath::ATan2(pyg,pxg), phi=TMath::ATan2(py,px);
253           Double_t lamg=TMath::ATan2(pzg,ptg), lam=TMath::ATan2(pz,pt);
254           hp->Fill((phi - phig)*1000.);
255           hl->Fill((lam - lamg)*1000.);
256           hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
257
258           Double_t x,y,z; vertex->GetXYZ(x,y,z);
259           hx->Fill((x - nref->X())*10);
260           hy->Fill((y - nref->Y())*10);
261           hz->Fill((z - nref->Z())*10);
262
263           hfound->Fill(ptg);
264           nref->SetLabel(-1);
265
266       }
267       for (i=0; i<ngood; i++) {
268          AliTrackReference *nref=(AliTrackReference*)nrefs->UncheckedAt(i);
269          AliTrackReference *pref=(AliTrackReference*)prefs->UncheckedAt(i);
270          Int_t pdg=(Int_t)nref->GetLength();//this is the mother's PDG !
271          if (code!=pdg) continue;
272          ng++;
273          pxg=nref->Px()+pref->Px(); pyg=nref->Py()+pref->Py(); 
274          ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
275          hgood->Fill(ptg);
276          nlab=nref->Label(); plab=pref->Label();
277          if (nlab < 0) continue;
278          cout<<"Vertex ("<<nlab<<','<<plab<<") has not been found !\n";
279       }
280       allgood+=ng;
281
282       cout<<"Number of found V0s : "<<nentr<<endl;
283       cout<<"Number of \"good\" V0s : "<<ng<<endl;
284
285       prefs->Clear();
286       nrefs->Clear();
287
288    } //**** End of the loop over events
289
290    delete event;
291    delete esdTree;
292    ef->Close();
293    
294    delete v0Tree;
295    refFile->Close();
296
297    Stat_t ng=hgood->GetEntries(), nf=hfound->GetEntries();
298    if (ng!=0) cout<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
299    cout<<"Total number of found V0s: "<<allfound<<" ("<<nf<<" in the peak)\n";
300    cout<<"Total number of \"good\" V0s: "<<allgood<<endl;
301
302    gStyle->SetOptStat(111110);
303    gStyle->SetOptFit(1);
304
305    TCanvas *c1=(TCanvas*)gROOT->FindObject("c1");
306    if (!c1) {
307       c1=new TCanvas("c1","",0,0,580,610);
308       c1->Divide(2,2);
309    }
310
311    Int_t minc=33; 
312
313    c1->cd(1);
314    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
315    if (hp->GetEntries()<minc) hp->Draw(); else hp->Fit("gaus");
316    hl->Draw("same"); c1->cd();
317
318    c1->cd(2);
319    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
320    if (hpt->GetEntries()<minc) hpt->Draw(); else hpt->Fit("gaus");
321    c1->cd();
322
323    c1->cd(3);
324    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
325    if (hx->GetEntries()<minc) hx->Draw(); else hx->Fit("gaus"); 
326    hy->Draw("same"); c1->cd();
327
328    c1->cd(4);
329    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
330    if (hz->GetEntries()<minc) hz->Draw(); else hz->Fit("gaus");
331
332    c1->Update();   
333
334    TCanvas *c2=(TCanvas*)gROOT->FindObject("c2");
335    if (!c2) {
336       c2=new TCanvas("c2","",600,0,580,610);
337       c2->Divide(1,2);
338    }
339
340    c2->cd(1);
341    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
342    hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
343    hg->Divide(hfound,hgood,1,1.,"b");
344    hf->Divide(hfake,hgood,1,1.,"b");
345    hg->SetMaximum(1.4);
346    hg->SetYTitle("V0 reconstruction efficiency");
347    hg->SetXTitle("Pt (GeV/c)");
348    hg->Draw();
349
350    TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
351    line1->Draw("same");
352    TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
353    line2->Draw("same");
354
355    hf->SetFillColor(1);
356    hf->SetFillStyle(3013);
357    hf->SetLineColor(2);
358    hf->SetLineWidth(2);
359    hf->Draw("histsame");
360    TText *text = new TText(0.461176,0.248448,"Fake vertices");
361    text->SetTextSize(0.05);
362    text->Draw();
363    text = new TText(0.453919,1.11408,"Good vertices");
364    text->SetTextSize(0.05);
365    text->Draw();
366
367
368    c2->cd(2);
369    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
370    if (v0s->GetEntries()<minc) v0s->Draw();
371    else v0s->Fit("gaus","","",V0mass-V0width,V0mass+V0width);
372    v0sf->Draw("same");
373    Double_t max=v0s->GetMaximum();
374    TLine *line3 = new TLine(V0mass-V0width,0.,V0mass-V0width,max);
375    line3->Draw("same");
376    TLine *line4 = new TLine(V0mass+V0width,0.,V0mass+V0width,max);
377    line4->Draw("same");
378
379    c2->Update();
380
381    TFile fc("AliV0Comparison.root","RECREATE");
382    c1->Write();
383    c2->Write();
384    fc.Close();
385
386    gBenchmark->Stop("AliV0Comparison");
387    gBenchmark->Show("AliV0Comparison");
388
389    return 0;
390 }
391
392
393 Int_t GoodV0s(const Char_t *dir) {
394    if (gAlice) { 
395        delete AliRunLoader::Instance();
396        delete gAlice;//if everything was OK here it is already NULL
397        gAlice = 0x0;
398    }
399
400    Char_t fname[100];
401    sprintf(fname,"%s/galice.root",dir);
402
403    AliRunLoader *rl = AliRunLoader::Open(fname,"COMPARISON");
404    if (!rl) {
405       ::Error("GoodTracksITS","Can't start session !");
406       return 1;
407    }
408
409    rl->LoadgAlice();
410    rl->LoadHeader();
411    rl->LoadKinematics();
412
413
414    Int_t nev=rl->GetNumberOfEvents();
415    ::Info("GoodV0s","Number of events : %d\n",nev);  
416
417    sprintf(fname,"%s/GoodTracksITS.root",dir);
418    TFile *itsFile=TFile::Open(fname);
419    if ((!itsFile)||(!itsFile->IsOpen())) {
420        ::Error("GoodV0s","Can't open the GoodTracksITS.root !");
421        delete rl;
422        return 5; 
423    }
424    TClonesArray dum("AliTrackReference",1000), *itsRefs=&dum;
425    TTree *itsTree=(TTree*)itsFile->Get("itsTree");
426    if (!itsTree) {
427        ::Error("GoodV0s","Can't get the ITS reference tree !");
428        delete rl;
429        return 6;
430    }
431    TBranch *itsBranch=itsTree->GetBranch("ITS");
432    if (!itsBranch) {
433       ::Error("GoodV0s","Can't get the ITS reference branch !");
434       delete rl;
435       return 7;
436    }
437    itsBranch->SetAddress(&itsRefs);
438
439
440    sprintf(fname,"%s/GoodV0s.root",dir);
441    TFile *v0File=TFile::Open(fname,"recreate");
442    TClonesArray dummy("AliTrackReference",1000), *nrefs=&dummy;
443    TClonesArray dumm("AliTrackReference",1000), *prefs=&dumm;
444    TTree v0Tree("v0Tree","Tree with info about the reconstructable V0s");
445    v0Tree.Branch("negative",&nrefs);
446    v0Tree.Branch("positive",&prefs);
447
448
449    /*** Some information about the cuts ***/
450    Double_t r2min=0.9*0.9;
451    Double_t r2max=2.9*2.9;
452
453
454
455    //********  Loop over generated events
456    for (Int_t e=0; e<nev; e++) {
457       rl->GetEvent(e);  v0File->cd();
458
459       Int_t np = rl->GetHeader()->GetNtrack();
460       cout<<"Event "<<e<<" Number of particles: "<<np<<endl;
461
462       itsTree->GetEvent(e);
463       Int_t nk=itsRefs->GetEntriesFast();
464
465       AliStack *stack=rl->Stack();
466
467       AliTrackReference *nref=0, *pref=0;
468
469       Int_t nv=0;
470       while (np--) {
471         //cerr<<np<<'\r';
472          TParticle *p0=stack->Particle(np);
473
474          /*** only these V0s are "good" ***/
475          Int_t code=p0->GetPdgCode();
476          if (code!=kK0Short)
477          if (code!=kLambda0)
478          if (code!=kLambda0Bar) continue; 
479
480          /*** daughter tracks should be "good" ***/
481          Int_t plab=p0->GetFirstDaughter(), nlab=p0->GetLastDaughter();
482          if (nlab==plab) continue;
483          if (nlab<0) continue;
484          if (plab<0) continue;
485          Int_t i;
486          TParticle * part = stack->Particle(plab);
487          if (part) {
488            TParticlePDG * partPDG = part->GetPDG();
489              if (partPDG && partPDG->Charge() < 0.) {
490                i=plab; plab=nlab; nlab=i;
491              }
492          }
493
494          for (i=0; i<nk; i++) {
495              nref=(AliTrackReference*)itsRefs->UncheckedAt(i);
496              if (nref->Label()==nlab) break;
497          }
498          if (i==nk) continue;
499          for (i=0; i<nk; i++) {
500              pref=(AliTrackReference*)itsRefs->UncheckedAt(i);
501              if (pref->Label()==plab) break;
502          }
503          if (i==nk) continue;
504
505          /*** fiducial volume ***/
506          TParticle *p=stack->Particle(nlab);
507          Double_t x=p->Vx(), y=p->Vy(), z=p->Vz(), r2=x*x+y*y;
508          if (r2<r2min) continue;
509          if (r2>r2max) continue;
510        
511          Int_t pdg=p0->GetPdgCode();
512          nref->SetLength(pdg);  //This will the V0's PDG !
513
514          new((*nrefs)[nv]) AliTrackReference(*nref);
515          new((*prefs)[nv]) AliTrackReference(*pref);
516
517          nv++;
518       }
519       itsRefs->Clear();
520
521       v0Tree.Fill();
522       nrefs->Clear(); prefs->Clear();
523
524    }  //**** end of the loop over generated events
525
526    v0Tree.Write();
527    v0File->Close();
528
529    delete itsTree;
530    itsFile->Close();
531
532    delete rl;
533    return 0;
534 }