]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCComparison.C
Add MEVSIM and TMEVSIM
[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   Int_t i;
15    gBenchmark->Start("AliTPCComparison");
16
17    TFile *cf=TFile::Open("AliTPCclusters.root");
18    if (!cf->IsOpen()) {cerr<<"Can't open AliTPCclusters.root !\n"; return 1;}
19    AliTPCParam *digp= (AliTPCParam*)cf->Get("75x40_100x60");
20    if (!digp) { cerr<<"TPC parameters have not been found !\n"; return 2; }
21
22 // Load clusters
23    AliTPCClustersArray *ca=new AliTPCClustersArray;
24    ca->Setup(digp);
25    ca->SetClusterType("AliTPCcluster");
26    ca->ConnectTree("Segment Tree");
27    Int_t nentr=Int_t(ca->GetTree()->GetEntries());
28    for (i=0; i<nentr; i++) ca->LoadEntry(i);
29
30 // Load tracks
31    TFile *tf=TFile::Open("AliTPCtracks.root");
32    if (!tf->IsOpen()) {cerr<<"Can't open AliTPCtracks.root !\n"; return 3;}
33    TObjArray tarray(2000);
34    TTree *tracktree=(TTree*)tf->Get("TreeT");
35    TBranch *tbranch=tracktree->GetBranch("tracks");
36    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        iotrack->CookLabel(ca);
44        tarray.AddLast(iotrack);
45    }   
46    tf->Close();
47
48    delete ca;
49    cf->Close();
50
51 /////////////////////////////////////////////////////////////////////////
52    GoodTrack gt[15000];
53    Int_t ngood=0;
54    ifstream in("good_tracks_tpc");
55    if (in) {
56       cerr<<"Reading good tracks...\n";
57       while (in>>gt[ngood].lab>>gt[ngood].code>>
58                  gt[ngood].px>>gt[ngood].py>>gt[ngood].pz>>
59                  gt[ngood].x >>gt[ngood].y >>gt[ngood].z) {
60          ngood++;
61          cerr<<ngood<<'\r';
62          if (ngood==15000) {
63             cerr<<"Too many good tracks !\n";
64             break;
65          }
66       }
67       if (!in.eof()) cerr<<"Read error (good_tracks_tpc) !\n";
68    } else {
69       cerr<<"Marking good tracks (this will take a while)...\n";
70       ngood=good_tracks(gt,15000);
71       ofstream out("good_tracks_tpc");
72       if (out) {
73          for (Int_t ngd=0; ngd<ngood; ngd++)            
74             out<<gt[ngd].lab<<' '<<gt[ngd].code<<' '<<
75                  gt[ngd].px<<' '<<gt[ngd].py<<' '<<gt[ngd].pz<<' '<<
76                  gt[ngd].x <<' '<<gt[ngd].y <<' '<<gt[ngd].z <<endl;
77       } else cerr<<"Can not open file (good_tracks_tpc) !\n";
78       out.close();
79
80       cerr<<"Preparing tracks for matching with the ITS...\n";
81       tarray.Sort();
82       tf=TFile::Open("AliTPCtracks.root","recreate");
83       tracktree=new TTree("TreeT","Tree with TPC tracks");
84       tracktree->Branch("tracks","AliTPCtrack",&iotrack,32000,0);
85       for (i=0; i<nentr; i++) {
86           iotrack=(AliTPCtrack*)tarray.UncheckedAt(i);
87           tracktree->Fill();
88       }
89       tracktree->Write();
90       tf->Close();
91
92    }
93    cerr<<"Number of good tracks : "<<ngood<<endl;
94
95    cerr<<"Doing comparison...\n";
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.,200.);
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.0,7,1.0); line1->SetLineStyle(4);
261    line1->Draw("same");
262    TLine *line2 = new TLine(0,0.9,7,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    if (!file->IsOpen()) {cerr<<"Can't open galice.root !\n"; exit(4);}
305
306    if (!(gAlice=(AliRun*)file->Get("gAlice"))) {
307      cerr<<"gAlice have not been found on galice.root !\n";
308      exit(5);
309    }
310
311    Int_t np=gAlice->GetEvent(0);   
312
313    AliTPC *TPC=(AliTPC*)gAlice->GetDetector("TPC");
314    Int_t ver = TPC->IsVersion(); 
315    cerr<<"TPC version "<<ver<<" has been found !\n";
316
317    AliTPCParam *digp=(AliTPCParam*)file->Get("75x40_100x60");
318    if (!digp) { cerr<<"TPC parameters have not been found !\n"; exit(6); }
319    TPC->SetParam(digp);
320
321    Int_t nrow_up=digp->GetNRowUp();
322    Int_t nrows=digp->GetNRowLow()+nrow_up;
323    Int_t zero=digp->GetZeroSup();
324    Int_t gap=Int_t(0.125*nrows);
325    Int_t good_number=Int_t(0.4*nrows);
326
327    Int_t *good=new Int_t[np];
328    for (Int_t ii=0; ii<np; ii++) good[ii]=0;
329
330
331    //MI change to be possible compile macro
332    //definition out of the swith statemnet
333     Int_t sectors_by_rows=0;
334     TTree *TD=0;
335     AliSimDigits da, *digits=&da;
336     Int_t *count=0;
337    switch (ver) {
338    case 1:
339      {
340       TFile *cf=TFile::Open("AliTPCclusters.root");
341       if (!cf->IsOpen()){cerr<<"Can't open AliTPCclusters.root !\n";exit(5);}
342       AliTPCClustersArray *ca=new AliTPCClustersArray;
343       ca->Setup(digp);
344       ca->SetClusterType("AliTPCcluster");
345       ca->ConnectTree("Segment Tree");
346       Int_t nrows=Int_t(ca->GetTree()->GetEntries());
347       for (Int_t n=0; n<nrows; n++) {
348           AliSegmentID *s=ca->LoadEntry(n);
349           Int_t sec,row;
350           digp->AdjustSectorRow(s->GetID(),sec,row);
351           AliTPCClustersRow &clrow = *ca->GetRow(sec,row);
352           Int_t ncl=clrow.GetArray()->GetEntriesFast();
353           while (ncl--) {
354               AliTPCcluster *c=(AliTPCcluster*)clrow[ncl];
355               Int_t lab=c->GetLabel(0);
356               if (lab<0) continue; //noise cluster
357               lab=TMath::Abs(lab);
358               if (sec>=digp->GetNInnerSector())
359               if (row==nrow_up-1    ) good[lab]|=0x1000;
360               if (sec>=digp->GetNInnerSector())
361               if (row==nrow_up-1-gap) good[lab]|=0x800;
362               good[lab]++;
363           }
364           ca->ClearRow(sec,row);
365       }
366       cf->Close();
367      }
368       break;
369    case 2:
370       TD=(TTree*)gDirectory->Get("TreeD_75x40_100x60");
371       TD->GetBranch("Segment")->SetAddress(&digits);
372       count = new Int_t[np];
373       Int_t i;
374       for (i=0; i<np; i++) count[i]=0;
375       sectors_by_rows=(Int_t)TD->GetEntries();
376       for (i=0; i<sectors_by_rows; i++) {
377           if (!TD->GetEvent(i)) continue;
378           Int_t sec,row;
379           digp->AdjustSectorRow(digits->GetID(),sec,row);
380           cerr<<sec<<' '<<row<<"                                     \r";
381           digits->First();
382           while (digits->Next()) {
383               Int_t it=digits->CurrentRow(), ip=digits->CurrentColumn();
384               Short_t dig = digits->GetDigit(it,ip);
385               Int_t idx0=digits->GetTrackID(it,ip,0); 
386               Int_t idx1=digits->GetTrackID(it,ip,1);
387               Int_t idx2=digits->GetTrackID(it,ip,2);
388               if (idx0>=0 && dig>=zero) count[idx0]+=1;
389               if (idx1>=0 && dig>=zero) count[idx1]+=1;
390               if (idx2>=0 && dig>=zero) count[idx2]+=1;
391           }
392           for (Int_t j=0; j<np; j++) {
393               if (count[j]>1) {
394                  if (sec>=digp->GetNInnerSector())
395                    if (row==nrow_up-1    ) good[j]|=0x1000;
396                  if (sec>=digp->GetNInnerSector())
397                    if (row==nrow_up-1-gap) good[j]|=0x800;
398                  good[j]++;
399               }
400               count[j]=0;
401           }
402       }
403       delete[] count;
404       break;
405    default:
406       cerr<<"Invalid TPC version !\n";
407       file->Close();
408       exit(7);
409    }
410
411    TTree *TH=gAlice->TreeH();
412    Int_t npart=(Int_t)TH->GetEntries();
413
414    while (npart--) {
415       AliTPChit *hit0=0;
416
417       TPC->ResetHits();
418       TH->GetEvent(npart);
419       AliTPChit * hit = (AliTPChit*) TPC->FirstHit(-1);
420       while (hit){
421         if (hit->fQ==0.) break;
422         hit =  (AliTPChit*) TPC->NextHit();
423       }
424       if (hit) {
425         hit0 = new AliTPChit(*hit); //Make copy of hit
426         hit = hit0;
427       }
428       else continue;
429       AliTPChit *hit1=(AliTPChit*)TPC->NextHit();       
430       if (hit1==0) continue;
431       if (hit1->fQ != 0.) continue;
432       Int_t i=hit->Track();
433       TParticle *p = (TParticle*)gAlice->Particle(i);
434
435       if (p->GetFirstMother()>=0) continue;  //secondary particle
436       if (good[i] < 0x1000+0x800+2+good_number) continue;
437       if (p->Pt()<0.100) continue;
438       if (TMath::Abs(p->Pz()/p->Pt())>0.999) continue;
439
440       gt[nt].lab=i;
441       gt[nt].code=p->GetPdgCode();
442 //**** px py pz - in global coordinate system, x y z - in local !
443       gt[nt].px=hit->X(); gt[nt].py=hit->Y(); gt[nt].pz=hit->Z();
444       Float_t cs,sn; digp->AdjustCosSin(hit1->fSector,cs,sn);
445       gt[nt].x = hit1->X()*cs + hit1->Y()*sn;
446       gt[nt].y =-hit1->X()*sn + hit1->Y()*cs;
447       gt[nt].z = hit1->Z();
448       nt++;     
449         if (hit0) delete hit0;
450       cerr<<i<<"                \r";
451       if (nt==max) {cerr<<"Too many good tracks !\n"; break;}
452    }
453    delete[] good;
454
455    delete gAlice; gAlice=0;
456
457    file->Close();
458    gBenchmark->Stop("AliTPCComparison");
459    gBenchmark->Show("AliTPCComparison");   
460    return nt;
461 }
462
463
464