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