0d1c04b85c0a4e1d552353b41718e40d06aea658
[u/mrichter/AliRoot.git] / ITS / AliCascadeComparison.C
1 /****************************************************************************
2  *           Very important, delicate and rather obscure macro.             *
3  *                                                                          *
4  *               Creates list of "findable" cascades,                       *
5  *             calculates efficiency, resolutions etc.                      *
6  *                                                                          *
7  *  Origin: Christian Kuhn, IReS, Strasbourg, christian.kuhn@ires.in2p3.fr  *
8  ****************************************************************************/
9
10 #if !defined(__CINT__) || defined(__MAKECINT__)
11   #include <Riostream.h>
12   #include <fstream.h>
13
14   #include "AliRun.h"
15   #include "AliHeader.h"
16   #include "AliRunLoader.h"
17   #include "AliITSLoader.h"
18
19   #include "TH1.h"
20   #include "TFile.h"
21   #include "TTree.h"
22   #include "TObjArray.h"
23   #include "TStyle.h"
24   #include "TCanvas.h"
25   #include "TLine.h"
26   #include "TText.h"
27   #include "TParticle.h"
28   #include "TStopwatch.h"
29   #include "TPDGCode.h"
30
31   #include "AliRun.h"
32   #include "AliPDG.h"
33   #include "AliCascadeVertex.h"
34 #endif
35
36 struct GoodCascade {
37   Int_t nlab,plab;   // V0's daughter labels
38   Int_t blab;        // Bachelor label
39   Int_t code;
40   Float_t px,py,pz;
41   Float_t x,y,z;
42 };
43 Int_t good_cascades(GoodCascade *gt, Int_t max);
44
45 extern AliRun *gAlice;
46
47 Int_t AliCascadeComparison(Int_t code=3312) {
48   //code= 3312; //kXiMinus 
49   //code=-3312; //kXiPlusBar
50   //code= 3334; //kOmegaMinus
51   //code=-3334; //kOmegaPlusBar
52
53    cerr<<"Doing comparison...\n";
54
55    TStopwatch timer;
56
57    const Double_t cascadeWindow=0.05, cascadeWidth=0.015; 
58    Double_t ptncut=0.12, ptpcut=0.33, kine0cut=0.003;
59    Double_t ptbcut=0.11, kinecut=0.002;
60    Double_t cascadeMass=1.32131;
61    switch (code) {
62    case kXiMinus:
63         break;
64    case kXiPlusBar:
65         ptncut=0.33; ptpcut=0.12;
66         break;
67    case kOmegaMinus: 
68         cascadeMass=1.67245;
69         kine0cut=0.001;
70         ptbcut=0.22; kinecut=0.006;
71         break; 
72    case kOmegaPlusBar:
73         cascadeMass=1.67245; 
74         kine0cut=0.001;
75         ptncut=0.33; ptpcut=0.12;
76         ptbcut=0.22; kinecut=0.006;
77         break;
78    default: cerr<<"Invalid PDG code !\n"; return 1;
79    }
80
81    /*** create some histograms ***/
82    TH1F *hp=new TH1F("hp","Angular Resolution",30,-30.,30.); //phi resolution 
83    hp->SetXTitle("(mrad)"); hp->SetFillColor(2);
84    TH1F *hl=new TH1F("hl","Lambda Resolution",30,-30,30);
85    hl->SetXTitle("(mrad)"); hl->SetFillColor(1); hl->SetFillStyle(3013); 
86    TH1F *hpt=new TH1F("hpt","Relative Pt Resolution",30,-10.,10.); 
87    hpt->SetXTitle("(%)"); hpt->SetFillColor(2); 
88
89    TH1F *hx=new TH1F("hx","Position Resolution (X,Y)",30,-3.,3.); //x res. 
90    hx->SetXTitle("(mm)"); hx->SetFillColor(6);
91    TH1F *hy=new TH1F("hy","Position Resolution (Y)",30,-3.,3.);   //y res
92    hy->SetXTitle("(mm)"); hy->SetFillColor(1); hy->SetFillStyle(3013);
93    TH1F *hz=new TH1F("hz","Position Resolution (Z)",30,-3.,3.);   //z res. 
94    hz->SetXTitle("(mm)"); hz->SetFillColor(6);
95
96    Double_t pmin=0.2, pmax=4.2; Int_t nchan=20;
97    TH1F *hgood=new TH1F("hgood","Good Cascades",nchan,pmin,pmax);    
98    TH1F *hfound=new TH1F("hfound","Found Cascades",nchan,pmin,pmax);
99    TH1F *hfake=new TH1F("hfake","Fake Cascades",nchan,pmin,pmax);
100    TH1F *hg=new TH1F("hg","Efficiency for Good Cascades",nchan,pmin,pmax);
101    hg->SetLineColor(4); hg->SetLineWidth(2);
102    TH1F *hf=new TH1F("hf","Probability of Fake Cascades",nchan,pmin,pmax);
103    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
104
105    Double_t mmin=cascadeMass-cascadeWindow, mmax=cascadeMass+cascadeWindow;
106    TH1F *cs =new TH1F("cs","Cascade Effective Mass",40, mmin, mmax);
107    cs->SetXTitle("(GeV)");
108    cs->SetLineColor(4); cs->SetLineWidth(4);
109    TH1F *csf =new TH1F("csf","Fake Cascade Effective Mass",40, mmin, mmax);
110    csf->SetXTitle("(GeV)"); csf->SetFillColor(6);
111
112    if (gAlice) {
113       delete gAlice->GetRunLoader();
114       delete gAlice; 
115       gAlice=0;
116    }   
117    AliRunLoader *rl = AliRunLoader::Open("galice.root");
118    if (!rl) {
119        cerr<<"AliV0Comparison.C :Can't start sesion !\n";
120        return 1;
121    }
122    AliITSLoader* itsl = (AliITSLoader*)rl->GetLoader("ITSLoader");
123    if (itsl == 0x0) {
124        cerr<<"AliV0Comparison.C : Can not find the ITSLoader\n";
125        delete rl;
126        return 2;
127    }
128
129    /*** Load reconstructed cascades ***/
130    TObjArray carray(1000);
131    itsl->LoadCascades();
132    TTree *xTree=itsl->TreeX();
133    TBranch *branch=xTree->GetBranch("cascades");
134    Int_t nentr=(Int_t)xTree->GetEntries();
135    for (Int_t i=0; i<nentr; i++) {
136        AliCascadeVertex *iovertex=new AliCascadeVertex; 
137        branch->SetAddress(&iovertex);
138        xTree->GetEvent(i);
139        carray.AddLast(iovertex);
140    }
141
142    /*** Check if the file with the "good" cascades exists ***/
143    GoodCascade gc[100];
144    Int_t ngood=0;
145    ifstream in("good_cascades");
146    if (in) {
147       cerr<<"Reading good cascades...\n";
148       while (in>>gc[ngood].nlab>>gc[ngood].plab>>
149                  gc[ngood].blab>>gc[ngood].code>>
150                  gc[ngood].px>>gc[ngood].py>>gc[ngood].pz>>
151                  gc[ngood].x >>gc[ngood].y >>gc[ngood].z) {
152          ngood++;
153          cerr<<ngood<<'\r';
154          if (ngood==100) {
155             cerr<<"Too many good cascades !\n";
156             break;
157          }
158       }
159       if (!in.eof()) cerr<<"Read error (good_cascades) !\n";
160    } else {
161      /*** generate a file with the "good" cascades ***/
162       cerr<<"Marking good cascades (this will take a while)...\n";
163       ngood=good_cascades(gc,100);
164       ofstream out("good_cascades");
165       if (out) {
166          for (Int_t ngd=0; ngd<ngood; ngd++)            
167             out<<gc[ngd].nlab<<' '<<gc[ngd].plab<<' '<<
168                  gc[ngd].blab<<' '<<gc[ngd].code<<' '<<
169                  gc[ngd].px<<' '<<gc[ngd].py<<' '<<gc[ngd].pz<<' '<<
170                  gc[ngd].x <<' '<<gc[ngd].y <<' '<<gc[ngd].z <<endl;
171       } else cerr<<"Can not open file (good_cascades) !\n";
172       out.close();
173    }
174
175    Double_t pxg=0.,pyg=0.,ptg=0.;
176    Int_t nlab=-1, plab=-1, blab=-1;
177    Int_t i;
178    for (i=0; i<nentr; i++) {
179        AliCascadeVertex *cascade=(AliCascadeVertex*)carray.UncheckedAt(i);
180        nlab=TMath::Abs(cascade->GetNindex()); 
181        plab=TMath::Abs(cascade->GetPindex());
182        blab=TMath::Abs(cascade->GetBindex());
183
184        /** Kinematical cuts **/
185        Double_t pxn,pyn,pzn; cascade->GetNPxPyPz(pxn,pyn,pzn); 
186        Double_t ptn=TMath::Sqrt(pxn*pxn + pyn*pyn);
187        if (ptn < ptncut) continue;
188        Double_t pxp,pyp,pzp; cascade->GetPPxPyPz(pxp,pyp,pzp); 
189        Double_t ptp=TMath::Sqrt(pxp*pxp + pyp*pyp);
190        if (ptp < ptpcut) continue;
191        Double_t pxb,pyb,pzb; cascade->GetBPxPyPz(pxb,pyb,pzb); 
192        Double_t ptb=TMath::Sqrt(pxb*pxb + pyb*pyb);
193        if (ptb < ptbcut) continue;
194        Double_t kine0;
195        Double_t kine=cascade->ChangeMassHypothesis(kine0,code);
196        if (TMath::Abs(kine0)>kine0cut) continue;
197        //if (TMath::Abs(kine)>kinecut) continue;
198
199        Double_t mass=cascade->GetEffMass();
200        cs->Fill(mass);
201        csf->Fill(mass);
202
203        if (TMath::Abs(mass-cascadeMass)>cascadeWidth) continue;
204
205        Int_t j;
206        for (j=0; j<ngood; j++) {
207           if (gc[j].code != cascade->GetPdgCode()) continue;
208           if (gc[j].nlab == nlab)
209           if (gc[j].plab == plab)
210           if (gc[j].blab == blab) break;
211        }
212
213        Double_t px,py,pz; cascade->GetPxPyPz(px,py,pz);
214        Double_t pt=TMath::Sqrt(px*px+py*py);
215
216        if (j==ngood) {
217           hfake->Fill(pt);
218           cerr<<"Fake cascade: ("<<nlab<<","<<plab<<","<<blab<<")\n";
219           continue;
220        }
221        csf->Fill(mass,-1);
222
223        pxg=gc[j].px; pyg=gc[j].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
224        Double_t phig=TMath::ATan2(pyg,pxg), phi=TMath::ATan2(py,px);
225        Double_t lamg=TMath::ATan2(gc[j].pz,ptg), lam=TMath::ATan2(pz,pt);
226        hp->Fill((phi - phig)*1000.);
227        hl->Fill((lam - lamg)*1000.);
228        hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
229
230        Double_t x,y,z; cascade->GetXYZ(x,y,z);
231        hx->Fill((x-gc[j].x)*10);
232        hy->Fill((y-gc[j].y)*10);
233        hz->Fill((z-gc[j].z)*10);
234
235        hfound->Fill(ptg);
236        gc[j].nlab=-1;
237
238    }
239    for (i=0; i<ngood; i++) {
240       if (gc[i].code != code) continue;
241       pxg=gc[i].px; pyg=gc[i].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
242       hgood->Fill(ptg);
243       nlab=gc[i].nlab; plab=gc[i].plab; blab=gc[i].blab;
244       if (nlab < 0) continue;
245      cerr<<"Cascade ("<<nlab<<','<<plab<<","<<blab<<") has not been found !\n";
246    }
247
248    carray.Delete();
249
250    Stat_t ng=hgood->GetEntries();
251    Stat_t nf=hfound->GetEntries();
252
253    cerr<<"Number of found cascades: "<<nentr<<" ("<<nf<<" in the peak)\n";
254    cerr<<"Number of good cascades: "<<ng<<endl;
255
256    if (ng!=0) 
257       cerr<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
258
259    gStyle->SetOptStat(111110);
260    gStyle->SetOptFit(1);
261
262    TCanvas *c1=new TCanvas("c1","",0,0,580,610);
263    c1->Divide(2,2);
264
265    c1->cd(1);
266    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
267    //hp->Fit("gaus");
268    hp->Draw();
269    hl->Draw("same"); c1->cd();
270
271    c1->cd(2);
272    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
273    //hpt->Fit("gaus"); c1->cd();
274    hpt->Draw(); c1->cd();
275
276    c1->cd(3);
277    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
278    //hx->Fit("gaus"); 
279    hx->Draw(); 
280    hy->Draw("same"); c1->cd();
281
282    c1->cd(4);
283    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
284    //hz->Fit("gaus");
285    hz->Draw();
286
287
288    TCanvas *c2=new TCanvas("c2","",600,0,580,610);
289    c2->Divide(1,2);
290
291    c2->cd(1);
292    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
293    hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
294    hg->Divide(hfound,hgood,1,1.,"b");
295    hf->Divide(hfake,hgood,1,1.,"b");
296    hg->SetMaximum(1.4);
297    hg->SetYTitle("Cascade reconstruction efficiency");
298    hg->SetXTitle("Pt (GeV/c)");
299    hg->Draw();
300
301    TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
302    line1->Draw("same");
303    TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
304    line2->Draw("same");
305
306    hf->SetFillColor(1);
307    hf->SetFillStyle(3013);
308    hf->SetLineColor(2);
309    hf->SetLineWidth(2);
310    hf->Draw("histsame");
311    TText *text = new TText(0.461176,0.248448,"Fake cascades");
312    text->SetTextSize(0.05);
313    text->Draw();
314    text = new TText(0.453919,1.11408,"Good cascades");
315    text->SetTextSize(0.05);
316    text->Draw();
317
318
319    c2->cd(2);
320    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
321    //cs->Fit("gaus","","",cascadeMass-cascadeWidth,cascadeMass+cascadeWidth);
322    cs->Draw();
323    csf->Draw("same");
324    Double_t max=cs->GetMaximum();
325    TLine *line3 = 
326       new TLine(cascadeMass-cascadeWidth,0.,cascadeMass-cascadeWidth,max);
327    line3->Draw("same");
328    TLine *line4 = 
329       new TLine(cascadeMass+cascadeWidth,0.,cascadeMass+cascadeWidth,max);
330    line4->Draw("same");
331
332    timer.Stop(); timer.Print();
333
334    delete rl;
335
336    return 0;
337 }
338
339
340 Int_t good_cascades(GoodCascade *gc, Int_t max) {
341    Int_t nc=0;
342    /*** Get information about the cuts ***/
343    Double_t r2min=0.9*0.9;
344    Double_t r2max=2.9*2.9;
345
346    /*** Get labels of the "good" tracks ***/
347    Double_t dd; Int_t id, label[15000], ngt=0;
348    ifstream in("good_tracks_its");
349    if (!in) {
350      cerr<<"Can't open the file good_tracks_its \n";
351      return nc;
352    }
353    while (in>>label[ngt]>>id>>dd>>dd>>dd>>dd>>dd>>dd) {
354      ngt++;
355      if (ngt>=15000) {
356        cerr<<"Too many good ITS tracks !\n";
357        return nc;
358      }
359    }   
360    if (!in.eof()) {
361       cerr<<"Read error (good_tracks_its) !\n";
362       return nc;
363    }
364
365    /*** Get an access to the kinematics ***/
366    AliRunLoader *rl =
367         AliRunLoader::GetRunLoader(AliConfig::fgkDefaultEventFolderName);
368    if (rl == 0x0) {
369   ::Fatal("AliCascadeComparison.C::good_cascades","Can not find Run Loader !");
370    }
371
372    AliITSLoader* itsl = (AliITSLoader*)rl->GetLoader("ITSLoader");
373    if (itsl == 0x0) {
374        cerr<<"AliITSComparisonV2.C : Can not find TPCLoader\n";
375        delete rl;
376        return 1;
377    }
378    rl->LoadgAlice();
379    rl->LoadHeader();
380    rl->LoadKinematics();
381    Int_t np = rl->GetHeader()->GetNtrack();
382
383    while (np--) {
384       cerr<<np<<'\r';
385       TParticle *cp=gAlice->GetMCApp()->Particle(np);
386
387       /*** only these cascades are "good" ***/
388       Int_t code=cp->GetPdgCode();
389       if (code!=kXiMinus)    if (code!=kXiPlusBar)
390       if (code!=kOmegaMinus) if (code!=kOmegaPlusBar) continue; 
391
392       /*** daughter tracks must be "good" ***/
393       Int_t v0lab=cp->GetFirstDaughter(), blab=cp->GetLastDaughter();
394       if (v0lab==blab) continue;
395       if (v0lab<0) continue;
396       if (blab<0) continue;
397
398       TParticle *p0=gAlice->GetMCApp()->Particle(v0lab);
399       TParticle *bp=gAlice->GetMCApp()->Particle(blab);
400       if ((p0->GetPdgCode()!=kLambda0) && (p0->GetPdgCode()!=kLambda0Bar)) {
401          TParticle *p=p0; p0=bp; bp=p;
402          Int_t i=v0lab; v0lab=blab; blab=i;         
403          if ((p0->GetPdgCode()!=kLambda0) && (p0->GetPdgCode()!=kLambda0Bar))
404                                                                    continue;
405       }
406
407       /** is the bachelor "good" ? **/
408       Int_t i;
409       for (i=0; i<ngt; i++) if (label[i]==blab) break;
410       if (i==ngt) continue; 
411
412       /** is the V0 "good" ? **/
413       Int_t plab=p0->GetFirstDaughter(), nlab=p0->GetLastDaughter();
414       if (nlab==plab) continue;
415       if (nlab<0) continue;
416       if (plab<0) continue;
417
418       for (i=0; i<ngt; i++) if (label[i]==nlab) break;
419       if (i==ngt) continue;
420       for (i=0; i<ngt; i++) if (label[i]==plab) break;
421       if (i==ngt) continue;
422
423       /*** fiducial volume ***/
424       Double_t x=bp->Vx(), y=bp->Vy(), r2=x*x+y*y; //bachelor
425       if (r2<r2min) continue;
426       if (r2>r2max) continue;
427       TParticle *pp=gAlice->GetMCApp()->Particle(plab);
428       x=pp->Vx(); y=pp->Vy(); r2=x*x+y*y;                      //V0
429       if (r2<r2min) continue;
430       if (r2>r2max) continue;
431
432       if (gAlice->GetMCApp()->Particle(plab)->GetPDG()->Charge() < 0.) {
433          i=plab; plab=nlab; nlab=i;
434       }
435
436       gc[nc].code=code;
437       gc[nc].plab=plab;   gc[nc].nlab=nlab; gc[nc].blab=blab;
438       gc[nc].px=cp->Px(); gc[nc].py=cp->Py(); gc[nc].pz=cp->Pz();
439       gc[nc].x=bp->Vx(); gc[nc].y=bp->Vy(); gc[nc].z=bp->Vz();
440       nc++;
441
442    }
443
444    delete rl;
445
446    return nc;
447 }