Macros moved from oldmacros on the standard ITS directory
[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 #ifndef __CINT__
11   #include <iostream.h>
12   #include <fstream.h>
13
14   #include "TH1.h"
15   #include "TFile.h"
16   #include "TTree.h"
17   #include "TObjArray.h"
18   #include "TStyle.h"
19   #include "TCanvas.h"
20   #include "TLine.h"
21   #include "TText.h"
22   #include "TParticle.h"
23   #include "TStopwatch.h"
24
25   #include "AliRun.h"
26   #include "AliPDG.h"
27   #include "AliCascadeVertex.h"
28 #endif
29
30 struct GoodCascade {
31   Int_t nlab,plab;   // V0's daughter labels
32   Int_t blab;        // Bachelor label
33   Int_t code;
34   Float_t px,py,pz;
35   Float_t x,y,z;
36 };
37 Int_t good_cascades(GoodCascade *gt, Int_t max);
38
39 Int_t AliCascadeComparison(Int_t code=3312) {
40   //code= 3312; //kXiMinus 
41   //code=-3312; //kXiPlusBar
42   //code= 3334; //kOmegaMinus
43   //code=-3334; //kOmegaPlusBar
44
45    cerr<<"Doing comparison...\n";
46
47    const Double_t cascadeWindow=0.05, cascadeWidth=0.015; 
48    Double_t cascadeMass=0.5;
49    switch (code) {
50    case kXiMinus:
51    case kXiPlusBar:    cascadeMass=1.32131; break;
52    case kOmegaMinus: 
53    case kOmegaPlusBar: cascadeMass=1.67245; break;
54    default: cerr<<"Invalid PDG code !\n"; return 1;
55    }
56
57    TStopwatch timer;
58
59    /*** Load reconstructed cascades ***/
60    TFile *cf=TFile::Open("AliCascadeVertices.root");
61    if (!cf->IsOpen()){cerr<<"Can't open AliCascadeVertices.root !\n";return 2;}
62    TObjArray carray(1000);
63    TTree *cTree=(TTree*)cf->Get("TreeCasc");
64    TBranch *branch=cTree->GetBranch("cascades");
65    Int_t nentr=(Int_t)cTree->GetEntries();
66    for (Int_t i=0; i<nentr; i++) {
67        AliCascadeVertex *iovertex=new AliCascadeVertex; 
68        branch->SetAddress(&iovertex);
69        cTree->GetEvent(i);
70        carray.AddLast(iovertex);
71    }
72    delete cTree;
73    cf->Close();
74
75    /*** Check if the file with the "good" cascades exists ***/
76    GoodCascade gc[100];
77    Int_t ngood=0;
78    ifstream in("good_cascades");
79    if (in) {
80       cerr<<"Reading good cascades...\n";
81       while (in>>gc[ngood].nlab>>gc[ngood].plab>>
82                  gc[ngood].blab>>gc[ngood].code>>
83                  gc[ngood].px>>gc[ngood].py>>gc[ngood].pz>>
84                  gc[ngood].x >>gc[ngood].y >>gc[ngood].z) {
85          ngood++;
86          cerr<<ngood<<'\r';
87          if (ngood==100) {
88             cerr<<"Too many good cascades !\n";
89             break;
90          }
91       }
92       if (!in.eof()) cerr<<"Read error (good_cascades) !\n";
93    } else {
94      /*** generate a file with the "good" cascades ***/
95       cerr<<"Marking good cascades (this will take a while)...\n";
96       ngood=good_cascades(gc,100);
97       ofstream out("good_cascades");
98       if (out) {
99          for (Int_t ngd=0; ngd<ngood; ngd++)            
100             out<<gc[ngd].nlab<<' '<<gc[ngd].plab<<' '<<
101                  gc[ngd].blab<<' '<<gc[ngd].code<<' '<<
102                  gc[ngd].px<<' '<<gc[ngd].py<<' '<<gc[ngd].pz<<' '<<
103                  gc[ngd].x <<' '<<gc[ngd].y <<' '<<gc[ngd].z <<endl;
104       } else cerr<<"Can not open file (good_cascades) !\n";
105       out.close();
106    }
107
108    /*** create some histograms ***/
109    TH1F *hp=new TH1F("hp","Angular Resolution",30,-30.,30.); //phi resolution 
110    hp->SetXTitle("(mrad)"); hp->SetFillColor(2);
111    TH1F *hl=new TH1F("hl","Lambda Resolution",30,-30,30);
112    hl->SetXTitle("(mrad)"); hl->SetFillColor(1); hl->SetFillStyle(3013); 
113    TH1F *hpt=new TH1F("hpt","Relative Pt Resolution",30,-10.,10.); 
114    hpt->SetXTitle("(%)"); hpt->SetFillColor(2); 
115
116    TH1F *hx=new TH1F("hx","Position Resolution (X,Y)",30,-3.,3.); //x res. 
117    hx->SetXTitle("(mm)"); hx->SetFillColor(6);
118    TH1F *hy=new TH1F("hy","Position Resolution (Y)",30,-3.,3.);   //y res
119    hy->SetXTitle("(mm)"); hy->SetFillColor(1); hy->SetFillStyle(3013);
120    TH1F *hz=new TH1F("hz","Position Resolution (Z)",30,-3.,3.);   //z res. 
121    hz->SetXTitle("(mm)"); hz->SetFillColor(6);
122
123    Double_t pmin=0.2, pmax=4.2; Int_t nchan=20;
124    TH1F *hgood=new TH1F("hgood","Good Cascades",nchan,pmin,pmax);    
125    TH1F *hfound=new TH1F("hfound","Found Cascades",nchan,pmin,pmax);
126    TH1F *hfake=new TH1F("hfake","Fake Cascades",nchan,pmin,pmax);
127    TH1F *hg=new TH1F("hg","Efficiency for Good Cascades",nchan,pmin,pmax);
128    hg->SetLineColor(4); hg->SetLineWidth(2);
129    TH1F *hf=new TH1F("hf","Probability of Fake Cascades",nchan,pmin,pmax);
130    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
131
132    Double_t mmin=cascadeMass-cascadeWindow, mmax=cascadeMass+cascadeWindow;
133    TH1F *cs =new TH1F("cs","Cascade Effective Mass",40, mmin, mmax);
134    cs->SetXTitle("(GeV)"); cs->SetFillColor(6);
135
136    Double_t pxg=0.,pyg=0.,ptg=0.;
137    Int_t nlab=-1, plab=-1, blab=-1;
138    Int_t i;
139    for (i=0; i<nentr; i++) {
140        AliCascadeVertex *cascade=(AliCascadeVertex*)carray.UncheckedAt(i);
141        cascade->GetV0labels(nlab,plab); 
142        nlab=TMath::Abs(nlab); plab=TMath::Abs(plab);
143        blab=TMath::Abs(cascade->GetBachelorLabel());
144
145        cascade->ChangeMassHypothesis(code);
146
147        Double_t mass=cascade->GetEffMass();
148        cs->Fill(mass);
149
150        if (TMath::Abs(mass-cascadeMass)>cascadeWidth) continue;
151
152        Int_t j;
153        for (j=0; j<ngood; j++) {
154           if (gc[j].code != cascade->GetPdgCode()) continue;
155           if (gc[j].nlab == nlab)
156           if (gc[j].plab == plab)
157           if (gc[j].blab == blab) break;
158        }
159
160        Double_t px,py,pz; cascade->GetPxPyPz(px,py,pz);
161        Double_t pt=TMath::Sqrt(px*px+py*py);
162
163        if (j==ngood) {
164           hfake->Fill(pt);
165           cerr<<"Fake cascade: ("<<nlab<<","<<plab<<","<<blab<<")\n";
166           continue;
167        }
168
169        pxg=gc[j].px; pyg=gc[j].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
170        Double_t phig=TMath::ATan2(pyg,pxg), phi=TMath::ATan2(py,px);
171        Double_t lamg=TMath::ATan2(gc[j].pz,ptg), lam=TMath::ATan2(pz,pt);
172        hp->Fill((phi - phig)*1000.);
173        hl->Fill((lam - lamg)*1000.);
174        hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
175
176        Double_t x,y,z; cascade->GetXYZ(x,y,z);
177        hx->Fill((x-gc[j].x)*10);
178        hy->Fill((y-gc[j].y)*10);
179        hz->Fill((z-gc[j].z)*10);
180
181        hfound->Fill(ptg);
182        gc[j].nlab=-1;
183
184    }
185    for (i=0; i<ngood; i++) {
186       if (gc[i].code != code) continue;
187       pxg=gc[i].px; pyg=gc[i].py; ptg=TMath::Sqrt(pxg*pxg+pyg*pyg);
188       hgood->Fill(ptg);
189       nlab=gc[i].nlab; plab=gc[i].plab; blab=gc[i].blab;
190       if (nlab < 0) continue;
191      cerr<<"Cascade ("<<nlab<<','<<plab<<","<<blab<<") has not been found !\n";
192    }
193
194    carray.Delete();
195
196    Stat_t ng=hgood->GetEntries();
197    Stat_t nf=hfound->GetEntries();
198
199    cerr<<"Number of found cascades: "<<nentr<<" ("<<nf<<" in the peak)\n";
200    cerr<<"Number of good cascades: "<<ng<<endl;
201
202    if (ng!=0) 
203       cerr<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
204
205    gStyle->SetOptStat(111110);
206    gStyle->SetOptFit(1);
207
208    TCanvas *c1=new TCanvas("c1","",0,0,580,610);
209    c1->Divide(2,2);
210
211    c1->cd(1);
212    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
213    //hp->Fit("gaus");
214    hp->Draw();
215    hl->Draw("same"); c1->cd();
216
217    c1->cd(2);
218    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
219    //hpt->Fit("gaus"); c1->cd();
220    hpt->Draw(); c1->cd();
221
222    c1->cd(3);
223    gPad->SetFillColor(42); gPad->SetFrameFillColor(10); 
224    //hx->Fit("gaus"); 
225    hx->Draw(); 
226    hy->Draw("same"); c1->cd();
227
228    c1->cd(4);
229    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
230    //hz->Fit("gaus");
231    hz->Draw();
232
233
234    TCanvas *c2=new TCanvas("c2","",600,0,580,610);
235    c2->Divide(1,2);
236
237    c2->cd(1);
238    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
239    hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
240    hg->Divide(hfound,hgood,1,1.,"b");
241    hf->Divide(hfake,hgood,1,1.,"b");
242    hg->SetMaximum(1.4);
243    hg->SetYTitle("Cascade reconstruction efficiency");
244    hg->SetXTitle("Pt (GeV/c)");
245    hg->Draw();
246
247    TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
248    line1->Draw("same");
249    TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
250    line2->Draw("same");
251
252    hf->SetFillColor(1);
253    hf->SetFillStyle(3013);
254    hf->SetLineColor(2);
255    hf->SetLineWidth(2);
256    hf->Draw("histsame");
257    TText *text = new TText(0.461176,0.248448,"Fake cascades");
258    text->SetTextSize(0.05);
259    text->Draw();
260    text = new TText(0.453919,1.11408,"Good cascades");
261    text->SetTextSize(0.05);
262    text->Draw();
263
264
265    c2->cd(2);
266    gPad->SetFillColor(42); gPad->SetFrameFillColor(10);
267    cs->SetXTitle("(GeV/c)"); 
268    //cs->Fit("gaus","","",cascadeMass-cascadeWidth,cascadeMass+cascadeWidth);
269    cs->Draw();
270    Double_t max=cs->GetMaximum();
271    TLine *line3 = 
272       new TLine(cascadeMass-cascadeWidth,0.,cascadeMass-cascadeWidth,max);
273    line3->Draw("same");
274    TLine *line4 = 
275       new TLine(cascadeMass+cascadeWidth,0.,cascadeMass+cascadeWidth,max);
276    line4->Draw("same");
277
278    timer.Stop(); timer.Print();
279
280    return 0;
281 }
282
283
284
285 Int_t good_cascades(GoodCascade *gc, Int_t max) {
286    Int_t nc=0;
287    /*** Get information about the cuts ***/
288    Double_t r2min=0.9*0.9;
289    Double_t r2max=2.9*2.9;
290
291    /*** Get labels of the "good" tracks ***/
292    Double_t dd; Int_t id, label[15000], ngt=0;
293    ifstream in("good_tracks_its");
294    if (!in) {
295      cerr<<"Can't open the file good_tracks_its \n";
296      return nc;
297    }
298    while (in>>label[ngt]>>id>>dd>>dd>>dd>>dd>>dd>>dd) {
299      ngt++;
300      if (ngt>=15000) {
301        cerr<<"Too many good ITS tracks !\n";
302        return nc;
303      }
304    }   
305    if (!in.eof()) {
306       cerr<<"Read error (good_tracks_its) !\n";
307       return nc;
308    }
309
310    /*** Get an access to the kinematics ***/
311    if (gAlice) {delete gAlice; gAlice=0;}
312
313    TFile *file=TFile::Open("galice.root");
314    if (!file->IsOpen()) {cerr<<"Can't open galice.root !\n"; exit(4);}
315    if (!(gAlice=(AliRun*)file->Get("gAlice"))) {
316      cerr<<"gAlice has not been found on galice.root !\n";
317      exit(5);
318    }
319
320    Int_t np=gAlice->GetEvent(0);
321    while (np--) {
322       cerr<<np<<'\r';
323       TParticle *cp=gAlice->Particle(np);
324
325       /*** only these cascades are "good" ***/
326       Int_t code=cp->GetPdgCode();
327       if (code!=kXiMinus)    if (code!=kXiPlusBar)
328       if (code!=kOmegaMinus) if (code!=kOmegaPlusBar) continue; 
329
330       /*** daughter tracks must be "good" ***/
331       Int_t v0lab=cp->GetFirstDaughter(), blab=cp->GetLastDaughter();
332       if (v0lab==blab) continue;
333       if (v0lab<0) continue;
334       if (blab<0) continue;
335
336       TParticle *p0=gAlice->Particle(v0lab);
337       TParticle *bp=gAlice->Particle(blab);
338       if ((p0->GetPdgCode()!=kLambda0) && (p0->GetPdgCode()!=kLambda0Bar)) {
339          TParticle *p=p0; p0=bp; bp=p;
340          Int_t i=v0lab; v0lab=blab; blab=i;         
341          if ((p0->GetPdgCode()!=kLambda0) && (p0->GetPdgCode()!=kLambda0Bar))
342                                                                    continue;
343       }
344
345       /** is the bachelor "good" ? **/
346       Int_t i;
347       for (i=0; i<ngt; i++) if (label[i]==blab) break;
348       if (i==ngt) continue; 
349
350       /** is the V0 "good" ? **/
351       Int_t plab=p0->GetFirstDaughter(), nlab=p0->GetLastDaughter();
352       if (nlab==plab) continue;
353       if (nlab<0) continue;
354       if (plab<0) continue;
355
356       for (i=0; i<ngt; i++) if (label[i]==nlab) break;
357       if (i==ngt) continue;
358       for (i=0; i<ngt; i++) if (label[i]==plab) break;
359       if (i==ngt) continue;
360
361       /*** fiducial volume ***/
362       Double_t x=bp->Vx(), y=bp->Vy(), r2=x*x+y*y; //bachelor
363       if (r2<r2min) continue;
364       if (r2>r2max) continue;
365       TParticle *pp=gAlice->Particle(plab);
366       x=pp->Vx(); y=pp->Vy(); r2=x*x+y*y;                      //V0
367       if (r2<r2min) continue;
368       if (r2>r2max) continue;
369
370       if (gAlice->Particle(plab)->GetPDG()->Charge() < 0.) {
371          i=plab; plab=nlab; nlab=i;
372       }
373
374       gc[nc].code=code;
375       gc[nc].plab=plab;   gc[nc].nlab=nlab; gc[nc].blab=blab;
376       gc[nc].px=cp->Px(); gc[nc].py=cp->Py(); gc[nc].pz=cp->Pz();
377       gc[nc].x=bp->Vx(); gc[nc].y=bp->Vy(); gc[nc].z=bp->Vz();
378       nc++;
379
380    }
381
382
383    return nc;
384 }