Opening galice.root using rfio
[u/mrichter/AliRoot.git] / TPC / AliTPCComparison.C
1 #ifndef __CINT__
2   #include "alles.h"
3   #include "AliTPCtracker.h"
4 #endif
5
6 struct GoodTrack {
7   Int_t lab;
8   Int_t code;
9   Float_t px,py,pz;
10   Float_t x,y,z;
11 };
12 Int_t good_tracks(GoodTrack *gt, Int_t max);
13
14 Int_t AliTPCComparison() {
15    cerr<<"Doing comparison...\n";
16    Int_t i;
17    gBenchmark->Start("AliTPCComparison");
18
19    TFile *cf=TFile::Open("AliTPCclusters.root");
20    if (!cf->IsOpen()) {cerr<<"Can't open AliTPCclusters.root !\n"; return 1;}
21    AliTPCParam *digp= (AliTPCParam*)cf->Get("75x40_100x60");
22    if (!digp) { cerr<<"TPC parameters have not been found !\n"; return 2; }
23    AliTPCtracker *tracker = new AliTPCtracker(digp);
24
25 // Load clusters
26    tracker->LoadInnerSectors();
27    tracker->LoadOuterSectors();
28
29 // Load tracks
30    TFile *tf=TFile::Open("AliTPCtracks.root");
31    if (!tf->IsOpen()) {cerr<<"Can't open AliTPCtracks.root !\n"; return 3;}
32    TObjArray tarray(2000);
33    TTree *tracktree=(TTree*)tf->Get("TPCf");
34    if (!tracktree) {cerr<<"Can't get a tree with TPC tracks !\n"; return 4;}
35    TBranch *tbranch=tracktree->GetBranch("tracks");
36    Int_t nentr=(Int_t)tracktree->GetEntries();
37    
38    AliTPCtrack *iotrack=0;
39    for (i=0; i<nentr; i++) {
40        iotrack=new AliTPCtrack;
41        tbranch->SetAddress(&iotrack);
42        tracktree->GetEvent(i);
43        tracker->CookLabel(iotrack,0.1);
44        tarray.AddLast(iotrack);
45    }   
46
47    delete tracker;
48
49    tf->Close();
50    cf->Close();
51
52 /////////////////////////////////////////////////////////////////////////
53    GoodTrack gt[15000];
54    Int_t ngood=0;
55    ifstream in("good_tracks_tpc");
56    if (in) {
57       cerr<<"Reading good tracks...\n";
58       while (in>>gt[ngood].lab>>gt[ngood].code>>
59                  gt[ngood].px>>gt[ngood].py>>gt[ngood].pz>>
60                  gt[ngood].x >>gt[ngood].y >>gt[ngood].z) {
61          ngood++;
62          cerr<<ngood<<'\r';
63          if (ngood==15000) {
64             cerr<<"Too many good tracks !\n";
65             break;
66          }
67       }
68       if (!in.eof()) cerr<<"Read error (good_tracks_tpc) !\n";
69    } else {
70       cerr<<"Marking good tracks (this will take a while)...\n";
71       ngood=good_tracks(gt,15000);
72       ofstream out("good_tracks_tpc");
73       if (out) {
74          for (Int_t ngd=0; ngd<ngood; ngd++)            
75             out<<gt[ngd].lab<<' '<<gt[ngd].code<<' '<<
76                  gt[ngd].px<<' '<<gt[ngd].py<<' '<<gt[ngd].pz<<' '<<
77                  gt[ngd].x <<' '<<gt[ngd].y <<' '<<gt[ngd].z <<endl;
78       } else cerr<<"Can not open file (good_tracks_tpc) !\n";
79       out.close();
80
81       cerr<<"Preparing tracks for matching with the ITS...\n";
82       tarray.Sort();
83       tf=TFile::Open("AliTPCtracks.root","recreate");
84       tracktree=new TTree("TPCf","Tree with TPC tracks");
85       tracktree->Branch("tracks","AliTPCtrack",&iotrack,32000,0);
86       for (i=0; i<nentr; i++) {
87           iotrack=(AliTPCtrack*)tarray.UncheckedAt(i);
88           tracktree->Fill();
89       }
90       tracktree->Write();
91       tf->Close();
92    }
93    cerr<<"Number of good tracks : "<<ngood<<endl;
94
95    TH1F *hp=new TH1F("hp","PHI resolution",50,-20.,20.); hp->SetFillColor(4);
96    TH1F *hl=new TH1F("hl","LAMBDA resolution",50,-20,20);hl->SetFillColor(4);
97    TH1F *hpt=new TH1F("hpt","Relative Pt resolution",30,-10.,10.); 
98    hpt->SetFillColor(2); 
99    TH1F *hmpt=new TH1F("hmpt","Relative Pt resolution (pt>4GeV/c)",30,-60,60); 
100    hmpt->SetFillColor(6);
101
102    AliTPCtrack *trk=(AliTPCtrack*)tarray.UncheckedAt(0);
103    Double_t pmin=0.1*(100/0.299792458/0.2/trk->GetConvConst());
104    Double_t pmax=6.0+pmin;
105
106    TH1F *hgood=new TH1F("hgood","Good tracks",30,pmin,pmax);    
107    TH1F *hfound=new TH1F("hfound","Found tracks",30,pmin,pmax);
108    TH1F *hfake=new TH1F("hfake","Fake tracks",30,pmin,pmax);
109    TH1F *hg=new TH1F("hg","",30,pmin,pmax); //efficiency for good tracks
110    hg->SetLineColor(4); hg->SetLineWidth(2);
111    TH1F *hf=new TH1F("hf","Efficiency for fake tracks",30,pmin,pmax);
112    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
113
114    TH1F *he =new TH1F("he","dE/dX for pions with 0.4<p<0.5 GeV/c",50,0.,100.);
115    TH2F *hep=new TH2F("hep","dE/dX vs momentum",50,0.,2.,50,0.,250.);
116    hep->SetMarkerStyle(8);
117    hep->SetMarkerSize(0.4);
118
119    Int_t mingood = ngood;  //MI change
120    Int_t * track_notfound = new Int_t[ngood];
121    Int_t   itrack_notfound =0;
122    Int_t * track_fake  = new Int_t[ngood];
123    Int_t   itrack_fake = 0;
124    Int_t * track_multifound = new Int_t[ngood];
125    Int_t * track_multifound_n = new Int_t[ngood];
126    Int_t   itrack_multifound =0;
127
128    while (ngood--) {
129       Int_t lab=gt[ngood].lab,tlab=-1;
130       Float_t ptg=
131       TMath::Sqrt(gt[ngood].px*gt[ngood].px + gt[ngood].py*gt[ngood].py);
132
133       if (ptg<pmin) continue;
134
135       hgood->Fill(ptg);
136
137       AliTPCtrack *track=0;
138       for (i=0; i<nentr; i++) {
139           track=(AliTPCtrack*)tarray.UncheckedAt(i);
140           tlab=track->GetLabel();
141           if (lab==TMath::Abs(tlab)) break;
142       }
143       if (i==nentr) {
144         //cerr<<"Track "<<lab<<" was not found !\n";
145         track_notfound[itrack_notfound++]=lab;
146         continue;
147       }
148       
149       //MI change  - addition
150       Int_t micount=0;
151       Int_t mi;
152       AliTPCtrack * mitrack;
153       for (mi=0; mi<nentr; mi++) {
154         mitrack=(AliTPCtrack*)tarray.UncheckedAt(mi);          
155         if (lab==TMath::Abs(mitrack->GetLabel())) micount++;
156       }
157       if (micount>1) {
158         //cout<<"Track no. "<<lab<<" found "<<micount<<"  times\n";
159         track_multifound[itrack_multifound]=lab;
160         track_multifound_n[itrack_multifound]=micount;
161         itrack_multifound++;
162       }
163       
164
165       //
166       Double_t xk=gt[ngood].x;//digp->GetPadRowRadii(0,0);
167       track->PropagateTo(xk);
168
169       if (lab==tlab) hfound->Fill(ptg);
170       else { 
171         track_fake[itrack_fake++]=lab;
172         hfake->Fill(ptg); 
173         //cerr<<lab<<" fake\n";
174       }
175
176       Double_t par[5]; track->GetExternalParameters(xk,par);
177       Float_t phi=TMath::ASin(par[2]) + track->GetAlpha();
178       if (phi<-TMath::Pi()) phi+=2*TMath::Pi();
179       if (phi>=TMath::Pi()) phi-=2*TMath::Pi();
180       Float_t lam=TMath::ATan(par[3]); 
181       Float_t pt_1=TMath::Abs(par[4]);
182
183       if (TMath::Abs(gt[ngood].code)==11 && ptg>4.) {
184          hmpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
185       } else {
186          Float_t phig=TMath::ATan2(gt[ngood].py,gt[ngood].px);
187          hp->Fill((phi - phig)*1000.);
188
189          Float_t lamg=TMath::ATan2(gt[ngood].pz,ptg);
190          hl->Fill((lam - lamg)*1000.);
191
192          hpt->Fill((pt_1 - 1/ptg)/(1/ptg)*100.);
193       }
194
195       Float_t mom=1./(pt_1*TMath::Cos(lam));
196       Float_t dedx=track->GetdEdx();
197       hep->Fill(mom,dedx,1.);
198       if (TMath::Abs(gt[ngood].code)==211)
199          if (mom>0.4 && mom<0.5) {
200             he->Fill(dedx,1.);
201          }
202
203    }
204
205    
206    Stat_t ng=hgood->GetEntries(); cerr<<"Good tracks "<<ng<<endl;
207    Stat_t nf=hfound->GetEntries();
208    if (ng!=0) 
209       cerr<<"\n\n\nIntegral efficiency is about "<<nf/ng*100.<<" %\n";
210    
211    //MI change  - addition
212    cout<<"Total number of found tracks ="<<nentr<<"\n";
213    cout<<"Total number of \"good\" tracks ="<<mingood<<"\n";
214
215
216    cout<<"\nList of Not found tracks :\n";
217    //   Int_t i;
218    for ( i = 0; i< itrack_notfound; i++){
219      cout<<track_notfound[i]<<"\t";
220      if ((i%5)==4) cout<<"\n";
221    }
222    cout<<"\nList of fake  tracks :\n";
223    for ( i = 0; i< itrack_fake; i++){
224      cout<<track_fake[i]<<"\t";
225      if ((i%5)==4) cout<<"\n";
226    }
227     cout<<"\nList of multiple found tracks :\n";
228    for ( i=0; i<itrack_multifound; i++) {
229        cout<<"id.   "<<track_multifound[i]<<"     found - "<<track_multifound_n[i]<<"times\n";
230    }
231    cout<<"\n\n\n";
232
233    
234    gStyle->SetOptStat(111110);
235    gStyle->SetOptFit(1);
236
237    TCanvas *c1=new TCanvas("c1","",0,0,700,850);
238
239    TPad *p1=new TPad("p1","",0,0.3,.5,.6); p1->Draw();
240    p1->cd(); p1->SetFillColor(42); p1->SetFrameFillColor(10); 
241    hp->SetFillColor(4);  hp->SetXTitle("(mrad)"); hp->Fit("gaus"); c1->cd();
242
243    TPad *p2=new TPad("p2","",0.5,.3,1,.6); p2->Draw(); 
244    p2->cd(); p2->SetFillColor(42); p2->SetFrameFillColor(10);
245    hl->SetXTitle("(mrad)"); hl->Fit("gaus"); c1->cd();
246
247    TPad *p3=new TPad("p3","",0,0,0.5,0.3); p3->Draw();
248    p3->cd(); p3->SetFillColor(42); p3->SetFrameFillColor(10); 
249    hpt->SetXTitle("(%)"); hpt->Fit("gaus"); c1->cd();
250
251    TPad *p4=new TPad("p4","",0.5,0,1,0.3); p4->Draw();
252    p4->cd(); p4->SetFillColor(42); p4->SetFrameFillColor(10);
253    hmpt->SetXTitle("(%)"); hmpt->Fit("gaus"); c1->cd();
254
255    TPad *p5=new TPad("p5","",0,0.6,1,1); p5->Draw(); p5->cd(); 
256    p5->SetFillColor(41); p5->SetFrameFillColor(10);
257    hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
258    hg->Divide(hfound,hgood,1,1.,"b");
259    hf->Divide(hfake,hgood,1,1.,"b");
260    hg->SetMaximum(1.4);
261    hg->SetYTitle("Tracking efficiency");
262    hg->SetXTitle("Pt (GeV/c)");
263    hg->Draw();
264
265    TLine *line1 = new TLine(pmin,1.0,pmax,1.0); line1->SetLineStyle(4);
266    line1->Draw("same");
267    TLine *line2 = new TLine(pmin,0.9,pmax,0.9); line2->SetLineStyle(4);
268    line2->Draw("same");
269
270    hf->SetFillColor(1);
271    hf->SetFillStyle(3013);
272    hf->SetLineColor(2);
273    hf->SetLineWidth(2);
274    hf->Draw("histsame");
275    TText *text = new TText(0.461176,0.248448,"Fake tracks");
276    text->SetTextSize(0.05);
277    text->Draw();
278    text = new TText(0.453919,1.11408,"Good tracks");
279    text->SetTextSize(0.05);
280    text->Draw();
281
282
283
284    TCanvas *c2=new TCanvas("c2","",320,32,530,590);
285
286    TPad *p6=new TPad("p6","",0.,0.,1.,.5); p6->Draw();
287    p6->cd(); p6->SetFillColor(42); p6->SetFrameFillColor(10); 
288    he->SetFillColor(2); he->SetFillStyle(3005);  
289    he->SetXTitle("Arbitrary Units"); 
290    he->Fit("gaus"); c2->cd();
291
292    TPad *p7=new TPad("p7","",0.,0.5,1.,1.); p7->Draw(); 
293    p7->cd(); p7->SetFillColor(42); p7->SetFrameFillColor(10);
294    hep->SetXTitle("p (Gev/c)"); hep->SetYTitle("dE/dX (Arb. Units)"); 
295    hep->Draw(); c1->cd();
296
297    gBenchmark->Stop("AliTPCComparison");
298    gBenchmark->Show("AliTPCComparison");
299
300
301    return 0;
302 }
303
304
305 Int_t good_tracks(GoodTrack *gt, Int_t max) {
306    Int_t nt=0;
307
308    TFile *file=TFile::Open("rfio:galice.root");
309    if (!file->IsOpen()) {cerr<<"Can't open galice.root !\n"; exit(4);}
310
311    if (!(gAlice=(AliRun*)file->Get("gAlice"))) {
312      cerr<<"gAlice have not been found on galice.root !\n";
313      exit(5);
314    }
315
316    Int_t np=gAlice->GetEvent(0);   
317
318    AliTPC *TPC=(AliTPC*)gAlice->GetDetector("TPC");
319    Int_t ver = TPC->IsVersion(); 
320    cerr<<"TPC version "<<ver<<" has been found !\n";
321
322    AliTPCParam *digp=(AliTPCParam*)file->Get("75x40_100x60");
323    if (!digp) { cerr<<"TPC parameters have not been found !\n"; exit(6); }
324    TPC->SetParam(digp);
325
326    Int_t nrow_up=digp->GetNRowUp();
327    Int_t nrows=digp->GetNRowLow()+nrow_up;
328    Int_t zero=digp->GetZeroSup();
329    Int_t gap=Int_t(0.125*nrows);
330    Int_t good_number=Int_t(0.4*nrows);
331
332    Int_t *good=new Int_t[np];
333    for (Int_t ii=0; ii<np; ii++) good[ii]=0;
334
335
336    //MI change to be possible compile macro
337    //definition out of the swith statemnet
338     Int_t sectors_by_rows=0;
339     TTree *TD=0;
340     AliSimDigits da, *digits=&da;
341     Int_t *count=0;
342    switch (ver) {
343    case 1:
344      {
345       TFile *cf=TFile::Open("AliTPCclusters.root");
346       if (!cf->IsOpen()){cerr<<"Can't open AliTPCclusters.root !\n";exit(5);}
347       AliTPCClustersArray *ca=new AliTPCClustersArray;
348       ca->Setup(digp);
349       ca->SetClusterType("AliTPCcluster");
350       ca->ConnectTree("Segment Tree");
351       Int_t nrows=Int_t(ca->GetTree()->GetEntries());
352       for (Int_t n=0; n<nrows; n++) {
353           AliSegmentID *s=ca->LoadEntry(n);
354           Int_t sec,row;
355           digp->AdjustSectorRow(s->GetID(),sec,row);
356           AliTPCClustersRow &clrow = *ca->GetRow(sec,row);
357           Int_t ncl=clrow.GetArray()->GetEntriesFast();
358           while (ncl--) {
359               AliTPCcluster *c=(AliTPCcluster*)clrow[ncl];
360               Int_t lab=c->GetLabel(0);
361               if (lab<0) continue; //noise cluster
362               lab=TMath::Abs(lab);
363               if (sec>=digp->GetNInnerSector())
364               if (row==nrow_up-1    ) good[lab]|=0x1000;
365               if (sec>=digp->GetNInnerSector())
366               if (row==nrow_up-1-gap) good[lab]|=0x800;
367               good[lab]++;
368           }
369           ca->ClearRow(sec,row);
370       }
371       cf->Close();
372      }
373       break;
374    case 2:
375       TD=(TTree*)gDirectory->Get("TreeD_75x40_100x60");
376       TD->GetBranch("Segment")->SetAddress(&digits);
377       count = new Int_t[np];
378       Int_t i;
379       for (i=0; i<np; i++) count[i]=0;
380       sectors_by_rows=(Int_t)TD->GetEntries();
381       for (i=0; i<sectors_by_rows; i++) {
382           if (!TD->GetEvent(i)) continue;
383           Int_t sec,row;
384           digp->AdjustSectorRow(digits->GetID(),sec,row);
385           cerr<<sec<<' '<<row<<"                                     \r";
386           digits->First();
387           do { //Many thanks to J.Chudoba who noticed this
388               Int_t it=digits->CurrentRow(), ip=digits->CurrentColumn();
389               Short_t dig = digits->GetDigit(it,ip);
390               Int_t idx0=digits->GetTrackID(it,ip,0); 
391               Int_t idx1=digits->GetTrackID(it,ip,1);
392               Int_t idx2=digits->GetTrackID(it,ip,2);
393               if (idx0>=0 && dig>=zero) count[idx0]+=1;
394               if (idx1>=0 && dig>=zero) count[idx1]+=1;
395               if (idx2>=0 && dig>=zero) count[idx2]+=1;
396           } while (digits->Next());
397           for (Int_t j=0; j<np; j++) {
398               if (count[j]>1) {
399                  if (sec>=digp->GetNInnerSector())
400                    if (row==nrow_up-1    ) good[j]|=0x1000;
401                  if (sec>=digp->GetNInnerSector())
402                    if (row==nrow_up-1-gap) good[j]|=0x800;
403                  good[j]++;
404               }
405               count[j]=0;
406           }
407       }
408       delete[] count;
409       break;
410    default:
411       cerr<<"Invalid TPC version !\n";
412       file->Close();
413       exit(7);
414    }
415
416    TTree *TH=gAlice->TreeH();
417    Int_t npart=(Int_t)TH->GetEntries();
418
419    while (npart--) {
420       AliTPChit *hit0=0;
421
422       TPC->ResetHits();
423       TH->GetEvent(npart);
424       AliTPChit * hit = (AliTPChit*) TPC->FirstHit(-1);
425       while (hit){
426         if (hit->fQ==0.) break;
427         hit =  (AliTPChit*) TPC->NextHit();
428       }
429       if (hit) {
430         hit0 = new AliTPChit(*hit); //Make copy of hit
431         hit = hit0;
432       }
433       else continue;
434       AliTPChit *hit1=(AliTPChit*)TPC->NextHit();       
435       if (hit1==0) continue;
436       if (hit1->fQ != 0.) continue;
437       Int_t i=hit->Track();
438       TParticle *p = (TParticle*)gAlice->Particle(i);
439
440       if (p->GetFirstMother()>=0) continue;  //secondary particle
441       if (good[i] < 0x1000+0x800+2+good_number) continue;
442       if (p->Pt()<0.100) continue;
443       if (TMath::Abs(p->Pz()/p->Pt())>0.999) continue;
444
445       gt[nt].lab=i;
446       gt[nt].code=p->GetPdgCode();
447 //**** px py pz - in global coordinate system, x y z - in local !
448       gt[nt].px=hit->X(); gt[nt].py=hit->Y(); gt[nt].pz=hit->Z();
449       Float_t cs,sn; digp->AdjustCosSin(hit1->fSector,cs,sn);
450       gt[nt].x = hit1->X()*cs + hit1->Y()*sn;
451       gt[nt].y =-hit1->X()*sn + hit1->Y()*cs;
452       gt[nt].z = hit1->Z();
453       nt++;     
454         if (hit0) delete hit0;
455       cerr<<i<<"                \r";
456       if (nt==max) {cerr<<"Too many good tracks !\n"; break;}
457    }
458    delete[] good;
459
460    delete gAlice; gAlice=0;
461
462    file->Close();
463    gBenchmark->Stop("AliTPCComparison");
464    gBenchmark->Show("AliTPCComparison");   
465    return nt;
466 }
467
468
469