Put back the two bellows in front of the absorber.
[u/mrichter/AliRoot.git] / TPC / AliTPCComparison.C
1 struct GoodTrack {
2   Int_t lab;
3   Int_t code;
4   Float_t px,py,pz;
5   Float_t x,y,z;
6 };
7 Int_t good_tracks(GoodTrack *gt, Int_t max);
8
9 Int_t AliTPCComparison() {
10    cerr<<"Doing comparison...\n";
11
12    TFile *cf=TFile::Open("AliTPCclusters.root");
13    if (!cf->IsOpen()) {cerr<<"Can't open AliTPCclusters.root !\n"; return 1;}
14    AliTPCParam *digp= (AliTPCParam*)cf->Get("75x40_100x60");
15    if (!digp) { cerr<<"TPC parameters have not been found !\n"; return 2; }
16
17 // Load clusters
18    AliTPCClustersArray *ca=new AliTPCClustersArray;
19    ca->Setup(digp);
20    ca->SetClusterType("AliTPCcluster");
21    ca->ConnectTree("Segment Tree");
22    Int_t nentr=Int_t(ca->GetTree()->GetEntries());
23    for (Int_t i=0; i<nentr; i++) {
24        AliSegmentID *s=ca->LoadEntry(i);
25    }
26
27 // Load tracks
28    TFile *tf=TFile::Open("AliTPCtracks.root");
29    if (!tf->IsOpen()) {cerr<<"Can't open AliTPCtracks.root !\n"; return 3;}
30    TObjArray tarray(2000);
31    TTree *tracktree=(TTree*)tf->Get("TreeT");
32    TBranch *tbranch=tracktree->GetBranch("tracks");
33    Int_t nentr=tracktree->GetEntries();
34    for (i=0; i<nentr; i++) {
35        AliTPCtrack *iotrack=new AliTPCtrack;
36        tbranch->SetAddress(&iotrack);
37        tracktree->GetEvent(i);
38        iotrack->CookLabel(ca);
39        tarray.AddLast(iotrack);
40    }   
41    tf->Close();
42
43    delete ca;
44    cf->Close();
45
46 /////////////////////////////////////////////////////////////////////////
47    GoodTrack gt[7000];
48    Int_t ngood=0;
49    ifstream in("good_tracks");
50    if (in) {
51       cerr<<"Reading good tracks...\n";
52       while (in>>gt[ngood].lab>>gt[ngood].code
53                >>gt[ngood].px >>gt[ngood].py>>gt[ngood].pz
54                >>gt[ngood].x  >>gt[ngood].y >>gt[ngood].z) {
55          ngood++;
56          cerr<<ngood<<'\r';
57          if (ngood==7000) {
58             cerr<<"Too many good tracks !\n";
59             break;
60          }
61       }
62       if (!in.eof()) cerr<<"Read error (good_tracks) !\n";
63    } else {
64       cerr<<"Marking good tracks (this will take a while)...\n";
65       ngood=good_tracks(gt,7000);
66       ofstream out("good_tracks");
67       if (out) {
68          for (Int_t ngd=0; ngd<ngood; ngd++)            
69             out<<gt[ngd].lab<<' '<<gt[ngd].code<<' '
70                <<gt[ngd].px <<' '<<gt[ngd].py<<' '<<gt[ngd].pz<<' '
71                <<gt[ngd].x  <<' '<<gt[ngd].y <<' '<<gt[ngd].z <<endl;
72       } else cerr<<"Can not open file (good_tracks) !\n";
73       out.close();
74    }
75    cerr<<"Number of good tracks : "<<ngood<<endl;
76
77    cerr<<"Doing comparison...\n";
78    TH1F *hp=new TH1F("hp","PHI resolution",50,-20.,20.); hp->SetFillColor(4);
79    TH1F *hl=new TH1F("hl","LAMBDA resolution",50,-20,20);hl->SetFillColor(4);
80    TH1F *hpt=new TH1F("hpt","Relative Pt resolution",30,-10.,10.); 
81    hpt->SetFillColor(2); 
82    TH1F *hmpt=new TH1F("hmpt","Relative Pt resolution (pt>4GeV/c)",30,-60,60); 
83    hmpt->SetFillColor(6);
84
85    TH1F *hgood=new TH1F("hgood","Good tracks",20,0,7);    
86    TH1F *hfound=new TH1F("hfound","Found tracks",20,0,7);
87    TH1F *hfake=new TH1F("hfake","Fake tracks",20,0,7);
88    TH1F *hg=new TH1F("hg","",20,0,7); //efficiency for good tracks
89    hg->SetLineColor(4); hg->SetLineWidth(2);
90    TH1F *hf=new TH1F("hf","Efficiency for fake tracks",20,0,7);
91    hf->SetFillColor(1); hf->SetFillStyle(3013); hf->SetLineWidth(2);
92
93    TH1F *he =new TH1F("he","dE/dX for pions with 0.4<p<0.5 GeV/c",50,0.,100.);
94    TH2F *hep=new TH2F("hep","dE/dX vs momentum",50,0.,2.,50,0.,200.);
95
96    while (ngood--) {
97       Int_t lab=gt[ngood].lab,tlab;
98       Float_t ptg=
99       TMath::Sqrt(gt[ngood].px*gt[ngood].px + gt[ngood].py*gt[ngood].py);
100
101       hgood->Fill(ptg);
102
103       AliTPCtrack *track;
104       Int_t i;
105       for (i=0; i<nentr; i++) {
106           track=(AliTPCtrack*)tarray->UncheckedAt(i);
107           tlab=track->GetLabel();
108           if (lab==TMath::Abs(tlab)) break;
109       }
110       if (i==nentr) {
111         cerr<<"Track "<<lab<<" was not found !\n";
112         continue;
113       }
114       Double_t xk=gt[ngood].x;//digp->GetPadRowRadii(0,0);
115       track->PropagateTo(xk);
116
117       if (lab==tlab) hfound->Fill(ptg);
118       else { hfake->Fill(ptg); cerr<<lab<<" fake\n";}
119
120       Double_t px,py,pz,pt=fabs(track->GetPt());track->GetPxPyPz(px,py,pz);
121
122       if (TMath::Abs(gt[ngood].code)==11 && ptg>4.) {
123          hmpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
124       } else {
125          Float_t phig=TMath::ATan2(gt[ngood].py,gt[ngood].px);
126          Float_t phi =TMath::ATan2(py,px );
127          hp->Fill((phi - phig)*1000.);
128
129          Float_t lamg=TMath::ATan2(gt[ngood].pz,ptg);
130          Float_t lam =TMath::ATan2(pz ,pt );
131          hl->Fill((lam - lamg)*1000.);
132
133          hpt->Fill((1/pt - 1/ptg)/(1/ptg)*100.);
134       }
135
136       Float_t mom=track->GetP();
137       Float_t dedx=track->GetdEdx();
138       hep->Fill(mom,dedx,1.);
139       if (TMath::Abs(gt[ngood].code)==211)
140          if (mom>0.4 && mom<0.5) {
141             he->Fill(dedx,1.);
142          }
143
144    }
145
146    Stat_t ng=hgood->GetEntries(); cerr<<"Good tracks "<<ng<<endl;
147    Stat_t nf=hfound->GetEntries();
148    if (ng!=0) 
149       cerr<<"Integral efficiency is about "<<nf/ng*100.<<" %\n";
150
151    gStyle->SetOptStat(111110);
152    gStyle->SetOptFit(1);
153
154    TCanvas *c1=new TCanvas("c1","",0,0,700,850);
155
156    TPad *p1=new TPad("p1","",0,0.3,.5,.6); p1->Draw();
157    p1->cd(); p1->SetFillColor(42); p1->SetFrameFillColor(10); 
158    hp->SetFillColor(4);  hp->SetXTitle("(mrad)"); hp->Fit("gaus"); c1->cd();
159
160    TPad *p2=new TPad("p2","",0.5,.3,1,.6); p2->Draw(); 
161    p2->cd(); p2->SetFillColor(42); p2->SetFrameFillColor(10);
162    hl->SetXTitle("(mrad)"); hl->Fit("gaus"); c1->cd();
163
164    TPad *p3=new TPad("p3","",0,0,0.5,0.3); p3->Draw();
165    p3->cd(); p3->SetFillColor(42); p3->SetFrameFillColor(10); 
166    hpt->SetXTitle("(%)"); hpt->Fit("gaus"); c1->cd();
167
168    TPad *p4=new TPad("p4","",0.5,0,1,0.3); p4->Draw();
169    p4->cd(); p4->SetFillColor(42); p4->SetFrameFillColor(10);
170    hmpt->SetXTitle("(%)"); hmpt->Fit("gaus"); c1->cd();
171
172    TPad *p5=new TPad("p5","",0,0.6,1,1); p5->Draw(); p5->cd(); 
173    p5->SetFillColor(41); p5->SetFrameFillColor(10);
174    hfound->Sumw2(); hgood->Sumw2(); hfake->Sumw2();
175    hg->Divide(hfound,hgood,1,1.,"b");
176    hf->Divide(hfake,hgood,1,1.,"b");
177    hg->SetMaximum(1.4);
178    hg->SetYTitle("Tracking efficiency");
179    hg->SetXTitle("Pt (GeV/c)");
180    hg->Draw();
181
182    TLine *line1 = new TLine(0,1.0,7,1.0); line1->SetLineStyle(4);
183    line1->Draw("same");
184    TLine *line2 = new TLine(0,0.9,7,0.9); line2->SetLineStyle(4);
185    line2->Draw("same");
186
187    hf->SetFillColor(1);
188    hf->SetFillStyle(3013);
189    hf->SetLineColor(2);
190    hf->SetLineWidth(2);
191    hf->Draw("histsame");
192    TText *text = new TText(0.461176,0.248448,"Fake tracks");
193    text->SetTextSize(0.05);
194    text->Draw();
195    text = new TText(0.453919,1.11408,"Good tracks");
196    text->SetTextSize(0.05);
197    text->Draw();
198
199
200
201    TCanvas *c2=new TCanvas("c2","",320,32,530,590);
202
203    TPad *p6=new TPad("p6","",0.,0.,1.,.5); p6->Draw();
204    p6->cd(); p6->SetFillColor(42); p6->SetFrameFillColor(10); 
205    he->SetFillColor(2); he->SetFillStyle(3005);  
206    he->SetXTitle("Arbitrary Units"); 
207    he->Fit("gaus"); c2->cd();
208
209    TPad *p7=new TPad("p7","",0.,0.5,1.,1.); p7->Draw(); 
210    p7->cd(); p7->SetFillColor(42); p7->SetFrameFillColor(10);
211    hep->SetXTitle("p (Gev/c)"); hep->SetYTitle("dE/dX (Arb. Units)"); 
212    hep->Draw(); c1->cd();
213
214    return 0;
215 }
216
217
218 Int_t good_tracks(GoodTrack *gt, Int_t max) {
219    Int_t nt=0;
220
221    TFile *file=TFile::Open("rfio:galice.root");
222    if (!file->IsOpen()) {cerr<<"Can't open galice.root !\n"; exit(4);}
223
224    if (!(gAlice=(AliRun*)file->Get("gAlice"))) {
225      cerr<<"gAlice have not been found on galice.root !\n";
226      exit(5);
227    }
228
229    gAlice->GetEvent(0);   
230
231    AliTPC *TPC=(AliTPC*)gAlice->GetDetector("TPC");
232    Int_t ver = TPC->IsVersion(); 
233    cerr<<"TPC version "<<ver<<" has been found !\n";
234
235    AliTPCParam *digp=(AliTPCParam*)file->Get("75x40_100x60");
236    if (!digp) { cerr<<"TPC parameters have not been found !\n"; exit(6); }
237    TPC->SetParam(digp);
238
239    Int_t nrow_up=digp->GetNRowUp();
240    Int_t nrows=digp->GetNRowLow()+nrow_up;
241    Int_t zero=digp->GetZeroSup();
242    Int_t gap=Int_t(0.125*nrows);
243    Int_t good_number=Int_t(0.4*nrows);
244
245    TClonesArray *particles=gAlice->Particles(); 
246    Int_t np=particles->GetEntriesFast();
247    Int_t *good=new Int_t[np];
248    for (Int_t ii=0; ii<np; ii++) good[ii]=0;
249
250    switch (ver) {
251    case 1:
252      {
253       TFile *cf=TFile::Open("AliTPCclusters.root");
254       if (!cf->IsOpen()){cerr<<"Can't open AliTPCclusters.root !\n";exit(5);}
255       AliTPCClustersArray *ca=new AliTPCClustersArray;
256       ca->Setup(digp);
257       ca->SetClusterType("AliTPCcluster");
258       ca->ConnectTree("Segment Tree");
259       Int_t nrows=Int_t(ca->GetTree()->GetEntries());
260       for (Int_t n=0; n<nrows; n++) {
261           AliSegmentID *s=ca->LoadEntry(n);
262           Int_t sec,row;
263           digp->AdjustSectorRow(s->GetID(),sec,row);
264           AliTPCClustersRow &clrow = *ca->GetRow(sec,row);
265           Int_t ncl=clrow.GetArray()->GetEntriesFast();
266           while (ncl--) {
267               AliTPCcluster *c=(AliTPCcluster*)clrow[ncl];
268               Int_t lab=c->GetLabel(0);
269               if (lab<0) continue; //noise cluster
270               lab=TMath::Abs(lab);
271               if (sec>=digp->GetNInnerSector())
272               if (row==nrow_up-1    ) good[lab]|=0x1000;
273               if (sec>=digp->GetNInnerSector())
274               if (row==nrow_up-1-gap) good[lab]|=0x800;
275               good[lab]++;
276           }
277           ca->ClearRow(sec,row);
278       }
279       cf->Close();
280      }
281       break;
282    case 2:
283       TTree *TD=(TTree*)gDirectory->Get("TreeD_75x40_100x60");
284       AliSimDigits da, *digits=&da;
285       TD->GetBranch("Segment")->SetAddress(&digits);
286       Int_t *count = new Int_t[np];
287       Int_t i;
288       for (i=0; i<np; i++) count[i]=0;
289       Int_t sectors_by_rows=(Int_t)TD->GetEntries();
290       for (i=0; i<sectors_by_rows; i++) {
291           if (!TD->GetEvent(i)) continue;
292           Int_t sec,row;
293           digp->AdjustSectorRow(digits->GetID(),sec,row);
294           cerr<<sec<<' '<<row<<"                                     \r";
295           digits->First();
296           while (digits->Next()) {
297               Int_t it=digits->CurrentRow(), ip=digits->CurrentColumn();
298               Short_t dig = digits->GetDigit(it,ip);
299               Int_t idx0=digits->GetTrackID(it,ip,0); 
300               Int_t idx1=digits->GetTrackID(it,ip,1);
301               Int_t idx2=digits->GetTrackID(it,ip,2);
302               if (idx0>=0 && dig>=zero) count[idx0]+=1;
303               if (idx1>=0 && dig>=zero) count[idx1]+=1;
304               if (idx2>=0 && dig>=zero) count[idx2]+=1;
305           }
306           for (Int_t j=0; j<np; j++) {
307               if (count[j]>1) {
308                  if (sec>=digp->GetNInnerSector())
309                  if (row==nrow_up-1    ) good[j]|=0x1000;
310                  if (sec>=digp->GetNInnerSector())
311                  if (row==nrow_up-1-gap) good[j]|=0x800;
312                  good[j]++;
313               }
314               count[j]=0;
315           }
316       }
317       delete[] count;
318       break;
319    default:
320       cerr<<"Invalid TPC version !\n";
321       file->Close();
322       exit(7);
323    }
324
325    TTree *TH=gAlice->TreeH();
326    TClonesArray *hits=TPC->Hits();
327    Int_t npart=TH->GetEntries();
328
329    while (npart--) {
330       TPC->ResetHits();
331       TH->GetEvent(npart);
332       Int_t nhits=hits->GetEntriesFast();
333       if (nhits==0) continue;
334       AliTPChit *hit;
335       Int_t j;
336       for (j=0; j<nhits-1; j++) {
337          hit=(AliTPChit*)hits->UncheckedAt(j);
338          if (hit->fQ==0.) break;
339       }
340       if (j==nhits-1) continue;
341       AliTPChit *hit1=(AliTPChit*)hits->UncheckedAt(j+1);
342       if (hit1->fQ != 0.) continue;
343       Int_t i=hit->fTrack;
344       TParticle *p = (TParticle*)particles->UncheckedAt(i);
345       if (p->GetFirstMother()>=0) continue;  //secondary particle
346       if (good[i] < 0x1000+0x800+2+good_number) continue;
347       if (p->Pt()<0.100) continue;
348       if (TMath::Abs(p->Pz()/p->Pt())>0.999) continue;
349
350       gt[nt].lab=i;
351       gt[nt].code=p->GetPdgCode();
352 //**** px py pz - in global coordinate system, x y z - in local !
353       gt[nt].px=hit->fX; gt[nt].py=hit->fY; gt[nt].pz=hit->fZ;
354       Float_t cs,sn; digp->AdjustCosSin(hit1->fSector,cs,sn);
355       gt[nt].x = hit1->fX*cs + hit1->fY*sn;
356       gt[nt].y =-hit1->fX*sn + hit1->fY*cs;
357       gt[nt].z = hit1->fZ;
358       nt++;
359
360       cerr<<i<<"                \r";
361       if (nt==max) {cerr<<"Too many good tracks !\n"; break;}
362    }
363    delete[] good;
364
365    delete gAlice; gAlice=0;
366
367    file->Close();
368    return nt;
369 }
370
371
372