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