]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDcheckESD.cxx
included all the non-friends
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDcheckESD.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 /////////////////////////////////////////////////////
17 //
18 // Check basic detector results at ESD level
19 //   - Geometrical efficiency  
20 //   - Tracking efficiency  
21 //   - PID efficiency  
22 //   - Refit efficiency  
23 //
24 // Author
25 //   Alex Bercuci <A.Bercuci@gsi.de>
26 //
27 //////////////////////////////////////////////////////
28
29 #include <TClonesArray.h>
30 #include <TObjArray.h>
31 #include <TPad.h>
32 #include <TLegend.h>
33 #include <TF1.h>
34 #include <TH2I.h>
35 #include <TH2F.h>
36 #include <TH3S.h>
37 #include <TH3F.h>
38 #include <TProfile2D.h>
39 #include <TGraphErrors.h>
40 #include <TGraphAsymmErrors.h>
41 #include <TFile.h>
42 #include <TTree.h>
43 #include <TROOT.h>
44 #include <TChain.h>
45 #include <TParticle.h>
46
47 #include "AliLog.h"
48 #include "AliAnalysisManager.h"
49 #include "AliESDEvent.h"
50 #include "AliESDkink.h"
51 #include "AliMCEvent.h"
52 #include "AliESDInputHandler.h"
53 #include "AliMCEventHandler.h"
54
55 #include "AliESDtrack.h"
56 #include "AliMCParticle.h"
57 #include "AliPID.h"
58 #include "AliStack.h"
59 #include "AliTrackReference.h"
60
61 #include "AliTRDcheckESD.h"
62
63 ClassImp(AliTRDcheckESD)
64
65 const Float_t AliTRDcheckESD::fgkxTPC = 290.;
66 const Float_t AliTRDcheckESD::fgkxTOF = 365.;
67 const UChar_t AliTRDcheckESD::fgkNgraph[AliTRDcheckESD::kNrefs] ={
68 8, 4, 2, 20};
69 FILE* AliTRDcheckESD::fgFile = NULL;
70
71 const Float_t AliTRDcheckESD::fgkEvVertexZ = 15.;
72 const Int_t   AliTRDcheckESD::fgkEvVertexN = 1;
73 const Float_t AliTRDcheckESD::fgkTrkDCAxy  = 40.;
74 const Float_t AliTRDcheckESD::fgkTrkDCAz   = 15.;
75 const Int_t   AliTRDcheckESD::fgkNclTPC    = 100;
76 const Float_t AliTRDcheckESD::fgkPt        = 0.2;
77 const Float_t AliTRDcheckESD::fgkEta       = 0.9;
78 const Float_t AliTRDcheckESD::fgkQs        = 0.002;
79
80 //____________________________________________________________________
81 AliTRDcheckESD::AliTRDcheckESD():
82   AliAnalysisTaskSE()
83   ,fStatus(0)
84   ,fNRefFigures(0)
85   ,fESD(NULL)
86   ,fMC(NULL)
87   ,fHistos(NULL)
88   ,fResults(NULL)
89 {
90   //
91   // Default constructor
92   //
93   SetNameTitle("checkESD", "Check TRD @ ESD level");
94   SetMC(kTRUE);
95 }
96
97 //____________________________________________________________________
98 AliTRDcheckESD::AliTRDcheckESD(char* name):
99   AliAnalysisTaskSE(name)
100   ,fStatus(0)
101   ,fNRefFigures(0)
102   ,fESD(NULL)
103   ,fMC(NULL)
104   ,fHistos(NULL)
105   ,fResults(NULL)
106 {
107   //
108   // Default constructor
109   //
110   SetMC(kTRUE);
111   SetTitle("Check TRD @ ESD level");
112   DefineOutput(1, TObjArray::Class());
113 }
114
115 //____________________________________________________________________
116 AliTRDcheckESD::~AliTRDcheckESD()
117 {
118 // Destructor
119   if(fHistos){
120     //fHistos->Delete();
121     delete fHistos;
122   }
123   if(fResults){
124     fResults->Delete();
125     delete fResults;
126   }
127 }
128
129 //____________________________________________________________________
130 void AliTRDcheckESD::UserCreateOutputObjects()
131 {       
132   //
133   // Create Output Containers (TObjectArray containing 1D histograms)
134   //
135   Histos();
136 }
137
138
139 //____________________________________________________________________
140 Bool_t AliTRDcheckESD::GetRefFigure(Int_t ifig)
141 {
142   if(ifig>=fNRefFigures){
143     AliWarning(Form("Ref plot %d not available. Valid only up to %d", ifig, fNRefFigures));
144     return kFALSE;
145   }
146   if(!gPad){
147     AliWarning("Please provide a canvas to draw results.");
148     return kFALSE;
149   } else {
150     gPad->SetLogx(0);gPad->SetLogy(0);
151     gPad->SetMargin(0.125, 0.015, 0.1, 0.015);
152   }
153
154   const Char_t *title[20];
155   TH1 *hF(NULL);
156   if((hF=(TH1S*)gROOT->FindObject("hFcheckESD"))) delete hF;
157   TLegend *leg(NULL);
158   TList *l(NULL); TVirtualPad *pad(NULL);
159   TGraphErrors *g(NULL);TGraphAsymmErrors *ga(NULL);
160   TObjArray *arr(NULL);
161   switch(ifig){
162   case kNCl: // number of clusters/track
163     if(!(arr = (TObjArray*)fResults->At(kNCl))) return kFALSE;
164
165     leg = new TLegend(.83, .7, .99, .96);
166     leg->SetHeader("Species");
167     leg->SetBorderSize(0); leg->SetFillStyle(0);
168     for(Int_t ig(0); ig<fgkNgraph[kNCl]; ig++){
169       if(!(g = (TGraphErrors*)arr->At(ig))) return kFALSE;
170       if(!g->GetN()) continue;
171       g->Draw(ig?"pc":"apc"); leg->AddEntry(g, g->GetTitle(), "pl");
172       if(ig) continue;
173       hF=g->GetHistogram();
174       hF->SetXTitle("no of clusters");
175       hF->SetYTitle("entries"); 
176       hF->GetYaxis()->CenterTitle(1);
177       hF->GetYaxis()->SetTitleOffset(1.2);
178       hF->SetMinimum(5);
179     }
180     leg->Draw(); gPad->SetLogy();
181     break;
182   case kTRDstat: // Efficiency
183     if(!(arr = (TObjArray*)fResults->At(kTRDstat))) return kFALSE;
184     leg = new TLegend(.62, .77, .98, .98);
185     leg->SetHeader("TRD Efficiency");
186     leg->SetBorderSize(0); leg->SetFillStyle(0);
187     title[0] = "Geometrical (TRDin/TPCout)";
188     title[1] = "Tracking (TRDout/TRDin)";
189     title[2] = "PID (TRDpid/TRDin)";
190     title[3] = "Refit (TRDrefit/TRDin)";
191     hF = new TH1S("hFcheckESD", ";p [GeV/c];Efficiency", 10, 0.1, 10.);
192     hF->SetMaximum(1.4);
193     hF->GetXaxis()->SetMoreLogLabels();
194     hF->GetYaxis()->CenterTitle(1);
195     hF->Draw("p");
196     for(Int_t ig(0); ig<fgkNgraph[kTRDstat]; ig++){
197       if(!(g = (TGraphErrors*)arr->At(ig))) return kFALSE;
198       g->Draw("pl"); leg->AddEntry(g, title[ig], "pl");
199       //PutTrendValue(name[id], g->GetMean(2));
200       //PutTrendValue(Form("%sRMS", name[id]), g->GetRMS(2));
201     }
202     leg->Draw(); gPad->SetLogx();
203     break;
204   case kTRDmom: // Energy loss
205     if(!(arr = (TObjArray*)fResults->At(kTRDmom))) return kFALSE;
206     leg = new TLegend(.65, .7, .95, .99);
207     leg->SetHeader("Energy Loss");
208     leg->SetBorderSize(1); leg->SetFillColor(0);
209     title[0] = "Max & 90% quantile";
210     title[1] = "Mean & 60% quantile";
211     hF = new TH1S("hFcheckESD", ";layer;#Delta E", 6, -0.5, 5.5);
212     hF->SetMaximum(1.3);hF->SetMinimum(-.3);
213     hF->Draw("p");
214     for(Int_t ig(0); ig<fgkNgraph[kTRDmom]; ig++){
215       if(!(ga = (TGraphAsymmErrors*)arr->At(ig))) return kFALSE;
216       ga->Draw("pl"); leg->AddEntry(ga, title[ig], "pl");
217       //PutTrendValue(name[id], g->GetMean(2));
218       //PutTrendValue(Form("%sRMS", name[id]), g->GetRMS(2));
219     }
220     leg->Draw();gPad->SetLogx(kFALSE);
221     break;
222   case kPtRes: // Pt resolution @ vertex
223     if(!(arr = (TObjArray*)fResults->At(kPtRes))) return kFALSE;
224     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
225     pad = ((TVirtualPad*)l->At(0)); pad->cd(); pad->SetLogx();
226     pad->SetMargin(0.1, 0.022, 0.1, 0.023);
227     hF = new TH1S("hFcheckESD", "ITS+TPC+TRD;p_{t} [GeV/c];#Delta p_{t} / p_{t} [%]", 10, 0.2, 10.);
228     hF->SetMaximum(10.);hF->SetMinimum(-3.);
229     hF->GetXaxis()->SetMoreLogLabels();
230     hF->GetXaxis()->SetTitleOffset(1.2);
231     hF->GetYaxis()->CenterTitle();
232     hF->Draw("p");
233     //for(Int_t ig(0); ig<fgkNgraph[kPtRes]/2; ig++){
234     for(Int_t ig(2); ig<6; ig++){
235       if(!(g = (TGraphErrors*)arr->At(ig))) continue;
236       if(!g->GetN()) continue;
237       g->Draw("pl");
238       //PutTrendValue(name[id], g->GetMean(2));
239       //PutTrendValue(Form("%sRMS", name[id]), g->GetRMS(2));
240     }
241     pad = ((TVirtualPad*)l->At(1)); pad->cd(); pad->SetLogx();
242     pad->SetMargin(0.1, 0.22, 0.1, 0.023);
243     hF = (TH1*)hF->Clone("hFcheckESD1");
244     hF->SetTitle("ITS+TPC");
245     hF->SetMaximum(10.);hF->SetMinimum(-3.);
246     hF->Draw("p");
247     leg = new TLegend(.78, .1, .99, .98);
248     leg->SetHeader("P_{t} @ DCA");
249     leg->SetBorderSize(1); leg->SetFillColor(0);
250     leg->SetTextAlign(22);
251     leg->SetTextFont(12);
252     leg->SetTextSize(0.03813559);
253     {
254       Int_t nPlots(0);
255       //for(Int_t ig(fgkNgraph[kPtRes]/2); ig<fgkNgraph[kPtRes]; ig++){
256       for(Int_t ig(12); ig<16; ig++){
257         if(!(g = (TGraphErrors*)arr->At(ig))) continue;
258         if(!g->GetN()) continue;
259         nPlots++;
260         g->Draw("pl"); leg->AddEntry(g, g->GetTitle(), "pl");
261         //PutTrendValue(name[id], g->GetMean(2));
262         //PutTrendValue(Form("%sRMS", name[id]), g->GetRMS(2));
263       }
264       if(nPlots) leg->Draw();
265     }
266
267     break;
268   }
269   return kTRUE;
270 }
271
272 //____________________________________________________________________
273 void AliTRDcheckESD::UserExec(Option_t *){
274   //
275   // Run the Analysis
276   //
277   fESD = dynamic_cast<AliESDEvent*>(InputEvent());
278   fMC = MCEvent();
279
280   if(!fESD){
281     AliError("ESD event missing.");
282     return;
283   }
284   
285   // Get MC information if available
286   AliStack * fStack = NULL;
287   if(HasMC()){
288     if(!fMC){ 
289       AliWarning("MC event missing");
290       SetMC(kFALSE);
291     } else {
292       if(!(fStack = fMC->Stack())){
293         AliWarning("MC stack missing");
294         SetMC(kFALSE);
295       }
296     }
297   }
298   TH1 *h(NULL);
299   
300   // fill event vertex histos
301   h = (TH1F*)fHistos->At(kTPCVertex);
302   h->Fill(fESD->GetPrimaryVertexTPC()->GetZv());
303   h = (TH1F*)fHistos->At(kEventVertex);
304   h->Fill(fESD->GetPrimaryVertex()->GetZv());
305   // fill the uncutted number of tracks
306   h = (TH1I*)fHistos->At(kNTracksAll);
307   h->Fill(fESD->GetNumberOfTracks());
308   
309   // counters for number of tracks in acceptance&DCA and for those with a minimum of TPC clusters
310   Int_t nTracksAcc=0;
311   Int_t nTracksTPC=0;
312   
313   AliESDtrack *esdTrack(NULL);
314   for(Int_t itrk = 0; itrk < fESD->GetNumberOfTracks(); itrk++){
315     esdTrack = fESD->GetTrack(itrk);
316
317     // track status
318     ULong_t status = esdTrack->GetStatus(); //PrintStatus(status);
319
320     // track selection
321     Bool_t selected(kTRUE);
322     if(esdTrack->Pt() < fgkPt){ 
323       AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Pt[%5.2f]", fESD->GetEventNumberInFile(), itrk, esdTrack->Pt()));
324       selected = kFALSE;
325     }
326     if(TMath::Abs(esdTrack->Eta()) > fgkEta){
327       AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Eta[%5.2f]", fESD->GetEventNumberInFile(), itrk, TMath::Abs(esdTrack->Eta())));
328       selected = kFALSE;
329     }
330     if(!Bool_t(status & AliESDtrack::kTPCout)){
331       AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] !TPCout", fESD->GetEventNumberInFile(), itrk));
332       selected = kFALSE;
333     }
334     if(esdTrack->GetKinkIndex(0) > 0){
335       AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] Kink", fESD->GetEventNumberInFile(), itrk));
336       selected = kFALSE;
337     }
338     
339     Float_t par[2], cov[3];
340     esdTrack->GetImpactParameters(par, cov);
341     if(selected && esdTrack->GetTPCNcls()>=10) {
342       // fill DCA histograms
343       h = (TH1F*)fHistos->At(kDCAxy); h->Fill(par[0]);
344       h = (TH1F*)fHistos->At(kDCAz); h->Fill(par[1]);
345       // fill pt distribution at this stage
346       h = (TH1F*)fHistos->At(kPt1); h->Fill(esdTrack->Pt());
347     }
348     if(IsCollision()){ // cuts on DCA
349       if(TMath::Abs(par[0]) > fgkTrkDCAxy){ 
350         AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] DCAxy[%f]", fESD->GetEventNumberInFile(), itrk, TMath::Abs(par[0])));
351         selected = kFALSE;
352       }
353       if(TMath::Abs(par[1]) > fgkTrkDCAz){ 
354         AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] DCAz[%f]", fESD->GetEventNumberInFile(), itrk, TMath::Abs(par[1])));
355         selected = kFALSE;
356       }
357     }
358     if(selected) {
359       nTracksAcc++;   // number of tracks in acceptance and DCA cut
360       if(esdTrack->GetTPCNcls()>=10) {
361       // fill pt distribution at this stage
362       h = (TH1F*)fHistos->At(kPt2); h->Fill(esdTrack->Pt());
363       // TPC nclusters distribution
364         h = (TH1I*)fHistos->At(kNTPCCl); h->Fill(esdTrack->GetTPCNcls());
365         if(esdTrack->GetTPCNcls()>1.0) {
366           h = (TH1I*)fHistos->At(kNTPCCl2); h->Fill(esdTrack->GetTPCNcls());
367         }
368       }
369     }
370       
371     Int_t nClustersTPC = esdTrack->GetTPCNcls();
372     if(nClustersTPC < fgkNclTPC){ 
373       AliDebug(2, Form("Reject Ev[%4d] Trk[%3d] NclTPC[%d]", fESD->GetEventNumberInFile(), itrk, nClustersTPC));
374       selected = kFALSE;
375     }
376     if(!selected) continue;
377     
378     // number of TPC reference tracks
379     nTracksTPC++;
380     
381     Int_t nTRD(esdTrack->GetNcls(2));
382     Double_t pt(esdTrack->Pt());
383     Double_t p[AliPID::kSPECIES]; esdTrack->GetTRDpid(p);
384     // pid quality
385     Bool_t kBarrel = Bool_t(status & AliESDtrack::kTRDin);
386     Float_t theta=esdTrack->Theta();
387     Float_t phi=esdTrack->Phi();
388     Float_t eta=-TMath::Log(TMath::Tan(theta/2.));
389
390     TH3F *hhh;
391     // find position and momentum of the track at entrance in TRD
392     Double_t localCoord[3] = {0., 0., 0.};
393     Bool_t localCoordGood = esdTrack->GetXYZAt(298., fESD->GetMagneticField(), localCoord);
394     if(localCoordGood) {
395       hhh = (TH3F*)fHistos->At(kPropagXYvsP); hhh->Fill(localCoord[0], localCoord[1], esdTrack->GetP());
396       hhh = (TH3F*)fHistos->At(kPropagRZvsP); hhh->Fill(localCoord[2], TMath::Sqrt(localCoord[0]*localCoord[0]+localCoord[1]*localCoord[1]), esdTrack->GetP());
397     }
398     Double_t localMom[3] = {0., 0., 0.};
399     Bool_t localMomGood = esdTrack->GetPxPyPzAt(298., fESD->GetMagneticField(), localMom);
400     Double_t localPhi = (localMomGood ? TMath::ATan2(localMom[1], localMom[0]) : 0.0);
401     Double_t localSagitaPhi = (localCoordGood ? TMath::ATan2(localCoord[1], localCoord[0]) : 0.0);
402
403     // fill pt distribution at this stage
404     if(esdTrack->Charge()>0) {
405       h = (TH1F*)fHistos->At(kPt3pos); h->Fill(pt);
406       // fill eta-phi map of TPC positive ref. tracks
407       if(localCoordGood && localMomGood) {
408         hhh = (TH3F*)fHistos->At(kTPCRefTracksPos); hhh->Fill(eta, localSagitaPhi, pt);
409       }
410     }
411     if(esdTrack->Charge()<0) {
412       h = (TH1F*)fHistos->At(kPt3neg); h->Fill(pt);
413       // fill eta-phi map of TPC negative ref. tracks
414       if(localCoordGood && localMomGood) {
415         hhh = (TH3F*)fHistos->At(kTPCRefTracksNeg); hhh->Fill(eta, localSagitaPhi, pt);
416       }
417     }
418     // TPC dE/dx vs P
419     h = (TH2F*)fHistos->At(kTPCDedx); h->Fill(esdTrack->GetP(), esdTrack->GetTPCsignal());
420     // (eta,phi) distrib of TPC ref. tracks
421     h = (TH2F*)fHistos->At(kEtaPhi); h->Fill(eta, phi);
422     // (eta,nclustersTPC) distrib of TPC ref. tracks
423     h = (TH2F*)fHistos->At(kEtaNclsTPC); h->Fill(eta, nClustersTPC);
424     // (phi,nclustersTPC) distrib of TPC ref. tracks
425     h = (TH2F*)fHistos->At(kPhiNclsTPC); h->Fill(phi, nClustersTPC);
426         
427     Int_t nTRDtrkl = esdTrack->GetTRDntracklets();
428     // TRD reference tracks
429     if(nTRDtrkl>=1) {
430       // fill pt distribution at this stage
431       if(esdTrack->Charge()>0) {
432         h = (TH1F*)fHistos->At(kPt4pos); h->Fill(pt);
433         // fill eta-phi map of TRD positive ref. tracks
434         if(localCoordGood && localMomGood) {
435           hhh = (TH3F*)fHistos->At(kTRDRefTracksPos); hhh->Fill(eta, localSagitaPhi, pt);
436         }
437       }
438       if(esdTrack->Charge()<0) {
439         h = (TH1F*)fHistos->At(kPt4neg); h->Fill(pt);
440         // fill eta-phi map of TRD negative ref. tracks
441         if(localCoordGood && localMomGood) {
442           hhh = (TH3F*)fHistos->At(kTRDRefTracksNeg); hhh->Fill(eta, localSagitaPhi, pt);
443         }
444       }
445       TProfile2D *h2d;
446       // fill eta-phi map of TRD negative ref. tracks
447       if(localCoordGood && localMomGood) {
448         h2d = (TProfile2D*)fHistos->At(kTRDEtaPhiAvNtrkl); h2d->Fill(eta, localSagitaPhi, (Float_t)nTRDtrkl);
449         h2d = (TProfile2D*)fHistos->At(kTRDEtaDeltaPhiAvNtrkl); h2d->Fill(eta, localPhi-localSagitaPhi, (Float_t)nTRDtrkl);
450       }
451       // ntracklets/track vs P
452       h = (TH2F*)fHistos->At(kNTrackletsTRD); h->Fill(esdTrack->GetP(), nTRDtrkl);
453       // ntracklets/track vs P
454       h = (TH2F*)fHistos->At(kNClsTrackTRD); h->Fill(esdTrack->GetP(), esdTrack->GetTRDncls());
455       // (slicePH,sliceNo) distribution and Qtot from slices
456       for(Int_t iPlane=0; iPlane<6; iPlane++) {
457         Float_t Qtot=0;
458         for(Int_t iSlice=0; iSlice<8; iSlice++) {
459         if(esdTrack->GetTRDslice(iPlane, iSlice)>20.) {
460           h = (TH2F*)fHistos->At(kPHSlice); h->Fill(iSlice, esdTrack->GetTRDslice(iPlane, iSlice));
461           Qtot += esdTrack->GetTRDslice(iPlane, iSlice);
462         }
463         }
464         h = (TH2F*)fHistos->At(kQtotP); h->Fill(esdTrack->GetTRDmomentum(iPlane), fgkQs*Qtot);
465       }
466       // theta distribution
467       h = (TH1F*)fHistos->At(kTheta); h->Fill(theta);
468       h = (TH1F*)fHistos->At(kPhi); h->Fill(phi);
469     }  // end if nTRDtrkl>=1
470     
471     // look at external track param
472     const AliExternalTrackParam *op = esdTrack->GetOuterParam();
473     const AliExternalTrackParam *ip = esdTrack->GetInnerParam();
474
475     Double_t pt0(0.), eta0(0.), phi0(0.), ptTRD(0.); 
476     // read MC info if available
477     Bool_t kFOUND(kFALSE), kPhysPrim(kFALSE);
478     AliMCParticle *mcParticle(NULL);
479     if(HasMC()){
480       AliTrackReference *ref(NULL); 
481       Int_t fLabel(esdTrack->GetLabel());
482       Int_t fIdx(TMath::Abs(fLabel));
483       if(fIdx > fStack->GetNtrack()) continue; 
484       
485       // read MC particle 
486       if(!(mcParticle = (AliMCParticle*) fMC->GetTrack(fIdx))) {
487         AliWarning(Form("MC particle missing. Label[ %d].", fLabel));
488         continue;
489       }
490       pt0  = mcParticle->Pt();
491       eta0 = mcParticle->Eta();
492       phi0 = mcParticle->Phi();
493       kPhysPrim = fMC->IsPhysicalPrimary(fIdx);
494
495       // read track references
496       Int_t nRefs = mcParticle->GetNumberOfTrackReferences();
497       if(!nRefs){
498         AliWarning(Form("No TR found for track @ Label[%d].", fLabel));
499         continue;
500       }
501       Int_t iref = 0;
502       while(iref<nRefs){
503         ref = mcParticle->GetTrackReference(iref);
504         if(ref->LocalX() > fgkxTPC) break;
505         ref=NULL; iref++;
506       }
507       if(ref){ 
508         if(ref->LocalX() > fgkxTOF){ // track skipping TRD fiducial volume
509           ref = mcParticle->GetTrackReference(TMath::Max(iref-1, 0));
510         }
511       } else { // track stopped in TPC 
512         ref = mcParticle->GetTrackReference(TMath::Max(iref-1, 0));
513       }
514       ptTRD = ref->Pt();kFOUND=kTRUE;
515     } else { // use reconstructed values
516       if(op){
517         Double_t x(op->GetX());
518         if(x<fgkxTOF && x>fgkxTPC){
519           ptTRD=op->Pt();
520           kFOUND=kTRUE;
521         }
522       }
523
524       if(!kFOUND && ip){
525         ptTRD=ip->Pt();
526         kFOUND=kTRUE;
527       }
528     }     // end if(HasMC())
529
530     if(kFOUND){
531       h = (TH2I*)fHistos->At(kTRDstat);
532       if(status & AliESDtrack::kTPCout) h->Fill(ptTRD, kTPCout);
533       if(status & AliESDtrack::kTRDin) h->Fill(ptTRD, kTRDin);
534       if(kBarrel && (status & AliESDtrack::kTRDout)) h->Fill(ptTRD, kTRDout);
535       if(kBarrel && (status & AliESDtrack::kTRDpid)) h->Fill(ptTRD, kTRDpid);
536       if(kBarrel && (status & AliESDtrack::kTRDrefit)) h->Fill(ptTRD, kTRDref);
537     }
538     Int_t idx(HasMC() ? Pdg2Idx(TMath::Abs(mcParticle->PdgCode())): 0)
539          ,sgn(esdTrack->Charge()<0?0:1);
540     if(kBarrel && kPhysPrim) {
541       TH3 *h3 = (TH3S*)fHistos->At(kPtRes);
542       Int_t offset = (status & AliESDtrack::kTRDrefit) ? 0 : 10; 
543       h3->Fill(pt0, 1.e2*(pt/pt0-1.), 
544         offset + 2*idx + sgn);
545     }
546     ((TH1*)fHistos->At(kNCl))->Fill(nTRD, 2*idx + sgn);
547     if(ip){
548       h = (TH2I*)fHistos->At(kTRDmom);
549       Float_t pTRD(0.);
550       for(Int_t ily=6; ily--;){
551         if((pTRD=esdTrack->GetTRDmomentum(ily))<0.) continue;
552         h->Fill(ip->GetP()-pTRD, ily);
553       }
554     }
555   }  // end loop over tracks
556   
557   // fill the number of tracks histograms
558   h = (TH1I*)fHistos->At(kNTracksAcc);
559   h->Fill(nTracksAcc);
560   h = (TH1I*)fHistos->At(kNTracksTPC);
561   h->Fill(nTracksTPC);
562   
563   PostData(1, fHistos);
564 }
565
566 //____________________________________________________________________
567 TObjArray* AliTRDcheckESD::Histos()
568 {
569 // Retrieve histograms array if already build or build it
570
571   if(fHistos) return fHistos;
572
573   fHistos = new TObjArray(kNhistos);
574   //fHistos->SetOwner(kTRUE);
575
576   TH1 *h = NULL;
577
578   // clusters per track
579   const Int_t kNpt(30);
580   Float_t Pt(0.2);
581   Float_t binsPt[kNpt+1];
582   for(Int_t i=0;i<kNpt+1; i++,Pt+=(TMath::Exp(i*i*.001)-1.)) binsPt[i]=Pt;
583   if(!(h = (TH2S*)gROOT->FindObject("hNCl"))){
584     h = new TH2S("hNCl", "Clusters per TRD track;N_{cl}^{TRD};SPECIES;entries", 60, 0., 180., 10, -0.5, 9.5);
585     TAxis *ay(h->GetYaxis());
586     ay->SetLabelOffset(0.015);
587     for(Int_t i(0); i<AliPID::kSPECIES; i++){
588       ay->SetBinLabel(2*i+1, Form("%s^{-}", AliPID::ParticleLatexName(i)));
589       ay->SetBinLabel(2*i+2, Form("%s^{+}", AliPID::ParticleLatexName(i)));
590     }
591   } else h->Reset();
592   fHistos->AddAt(h, kNCl); fNRefFigures++;
593
594   // status bits histogram
595   const Int_t kNbits(5);
596   Float_t Bits(.5);
597   Float_t binsBits[kNbits+1];
598   for(Int_t i=0; i<kNbits+1; i++,Bits+=1.) binsBits[i]=Bits;
599   if(!(h = (TH2I*)gROOT->FindObject("hTRDstat"))){
600     h = new TH2I("hTRDstat", "TRD status bits;p_{t} @ TRD [GeV/c];status;entries", kNpt, binsPt, kNbits, binsBits);
601     TAxis *ay(h->GetYaxis());
602     ay->SetBinLabel(1, "kTPCout");
603     ay->SetBinLabel(2, "kTRDin");
604     ay->SetBinLabel(3, "kTRDout");
605     ay->SetBinLabel(4, "kTRDpid");
606     ay->SetBinLabel(5, "kTRDrefit");
607   } else h->Reset();
608   fHistos->AddAt(h, kTRDstat);
609
610   // energy loss
611   if(!(h = (TH2I*)gROOT->FindObject("hTRDmom"))){
612     h = new TH2I("hTRDmom", "TRD energy loss;p_{inner} - p_{ly} [GeV/c];ly;entries", 100, -1., 2., 6, -0.5, 5.5);
613   } else h->Reset();
614   fHistos->AddAt(h, kTRDmom);
615   if(!HasMC()) return fHistos;
616
617   // pt resolution
618   const Int_t kNdpt(100), kNspec(4*AliPID::kSPECIES);
619   Float_t DPt(-3.), Spec(-0.5);
620   Float_t binsDPt[kNdpt+1], binsSpec[kNspec+1];
621   for(Int_t i=0; i<kNdpt+1; i++,DPt+=6.e-2) binsDPt[i]=DPt;
622   for(Int_t i=0; i<kNspec+1; i++,Spec+=1.) binsSpec[i]=Spec;
623   if(!(h = (TH3S*)gROOT->FindObject("hPtRes"))){
624     h = new TH3S("hPtRes", "P_{t} resolution @ DCA;p_{t}^{MC} [GeV/c];#Delta p_{t}/p_{t}^{MC} [%];SPECIES", kNpt, binsPt, kNdpt, binsDPt, kNspec, binsSpec);
625     TAxis *az(h->GetZaxis());
626     az->SetLabelOffset(0.015);
627     for(Int_t i(0); i<AliPID::kSPECIES; i++){
628       az->SetBinLabel(2*i+1, Form("%s^{-}", AliPID::ParticleLatexName(i)));
629       az->SetBinLabel(2*i+2, Form("%s^{+}", AliPID::ParticleLatexName(i)));
630       az->SetBinLabel(10+2*i+1, Form("%s^{-}", AliPID::ParticleLatexName(i)));
631       az->SetBinLabel(10+2*i+2, Form("%s^{+}", AliPID::ParticleLatexName(i)));
632     }
633   } else h->Reset();
634   fHistos->AddAt(h, kPtRes);
635
636   // TPC event vertex distribution
637   if(!(h = (TH1F*)gROOT->FindObject("hTPCVertex"))){
638     h = new TH1F("hTPCVertex", "Event vertex Z coord. from TPC tracks", 100, -25., 25.);
639   } else h->Reset();
640   fHistos->AddAt(h, kTPCVertex);
641   
642   // Event vertex
643   if(!(h = (TH1F*)gROOT->FindObject("hEventVertex"))){
644     h = new TH1F("hEventVertex", "Event vertex Z coord.", 100, -25., 25.);
645   } else h->Reset();
646   fHistos->AddAt(h, kEventVertex);
647   
648   // Number of all tracks
649   if(!(h = (TH1I*)gROOT->FindObject("hNTracksAll"))){
650     h = new TH1I("hNTracksAll", "Number of tracks per event, event vertex cuts", 5000, 0, 5000);
651   } else h->Reset();
652   fHistos->AddAt(h, kNTracksAll);
653   
654   // Number of tracks in acceptance and DCA cut
655   if(!(h = (TH1I*)gROOT->FindObject("hNTracksAcc"))){
656     h = new TH1I("hNTracksAcc", Form("Number of tracks per event, |#eta|<%.1f, |DCAxy|<%.1f, |DCAz|<%.1f",
657                                      fgkEta, fgkTrkDCAxy, fgkTrkDCAz), 5000, 0, 5000);
658   } else h->Reset();
659   fHistos->AddAt(h, kNTracksAcc);
660   
661   // Number of tracks in TPC (Ncls>10)
662   if(!(h = (TH1I*)gROOT->FindObject("hNTracksTPC"))){
663     h = new TH1I("hNTracksTPC", Form("Number of tracks per event, |#eta|<%.1f, pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
664                                      fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 5000, 0, 5000);
665   } else h->Reset();
666   fHistos->AddAt(h, kNTracksTPC);
667   
668   // Distribution of DCA-xy
669   if(!(h = (TH1F*)gROOT->FindObject("hDCAxy"))){
670     h = new TH1F("hDCAxy", "Distribution of transverse DCA", 100, -100., 100.);
671   } else h->Reset();
672   fHistos->AddAt(h, kDCAxy);
673   
674   // Distribution of DCA-z
675   if(!(h = (TH1F*)gROOT->FindObject("hDCAz"))){
676     h = new TH1F("hDCAz", "Distribution of longitudinal DCA", 100, -100., 100.);
677   } else h->Reset();
678   fHistos->AddAt(h, kDCAz);
679   
680   Float_t binPtLimits[33] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,
681                              1.0, 1.1, 1.2, 1.3, 1.4, 
682                              1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0,
683                              3.4, 3.8, 4.2, 4.6, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
684   // Pt distributions
685   if(!(h = (TH1F*)gROOT->FindObject("hPt1"))){
686     h = new TH1F("hPt1", Form("dN/dpt, |#eta|<%.1f and pt>%.1f", fgkEta, fgkPt), 32, binPtLimits);
687   } else h->Reset();
688   fHistos->AddAt(h, kPt1);
689   
690   if(!(h = (TH1F*)gROOT->FindObject("hPt2"))){
691     h = new TH1F("hPt2", Form("dN/dpt, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f",
692                               fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz), 32, binPtLimits);
693   } else h->Reset();
694   fHistos->AddAt(h, kPt2);
695   
696   if(!(h = (TH1F*)gROOT->FindObject("hPt3pos"))){
697     h = new TH1F("hPt3pos", Form("dN/dpt (positives), |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
698                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 32, binPtLimits);
699   } else h->Reset();
700   fHistos->AddAt(h, kPt3pos);
701   
702   if(!(h = (TH1F*)gROOT->FindObject("hPt3neg"))){
703     h = new TH1F("hPt3neg", Form("dN/dpt (negatives), |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
704                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 32, binPtLimits);
705   } else h->Reset();
706   fHistos->AddAt(h, kPt3neg);
707   
708   if(!(h = (TH1F*)gROOT->FindObject("hPt4pos"))){
709     h = new TH1F("hPt4pos", Form("dN/dpt (positives), |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq 1",
710                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 32, binPtLimits);
711   } else h->Reset();
712   fHistos->AddAt(h, kPt4pos);
713   
714   if(!(h = (TH1F*)gROOT->FindObject("hPt4neg"))){
715     h = new TH1F("hPt4pos", Form("dN/dpt (negatives), |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq 1",
716                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 32, binPtLimits);
717   } else h->Reset();
718   fHistos->AddAt(h, kPt4neg);
719   
720   // theta distribution of TRD tracks
721   if(!(h = (TH1F*)gROOT->FindObject("hTheta"))){
722     h = new TH1F("hTheta", Form("dN/d#theta, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq 1",
723                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 220,.5,2.7);
724   } else h->Reset();
725   fHistos->AddAt(h, kTheta);
726   
727   // phi distribution of TRD tracks
728   if(!(h = (TH1F*)gROOT->FindObject("hPhi"))){
729     h = new TH1F("hPhi", Form("dN/d#varphi, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq 1",
730                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 157,0,6.28);
731   } else h->Reset();
732   fHistos->AddAt(h, kPhi);
733   
734   // TPC cluster distribution
735   if(!(h = (TH1F*)gROOT->FindObject("hNTPCCl"))){
736     h = new TH1I("hNTPCCl", Form("Number of TPC clusters/track, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f",
737                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz), 160, 0, 160);
738   } else h->Reset();
739   fHistos->AddAt(h, kNTPCCl);
740   
741   if(!(h = (TH1I*)gROOT->FindObject("hNTPCCl2"))){
742     h = new TH1F("hNTPCCl2", Form("Number of TPC clusters/track, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, pt>1.0 GeV/c",
743                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz), 160, 0, 160);
744   } else h->Reset();
745   fHistos->AddAt(h, kNTPCCl2);
746   
747   // dE/dx vs P for TPC reference tracks
748   if(!(h = (TH2F*)gROOT->FindObject("hTPCDedx"))){
749     h = new TH2F("hTPCDedx", Form("TPC dE/dx vs P, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
750                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 100, 0.1,10.1, 120, 0,600.);
751   } else h->Reset();
752   fHistos->AddAt(h, kTPCDedx);
753   
754   // eta,phi distribution of TPC reference tracks
755   if(!(h = (TH2F*)gROOT->FindObject("hEtaPhi"))){
756     h = new TH2F("hEtaPhi", Form("TPC (#eta,#varphi), |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
757                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, -1, 1, 157, 0, 6.28);
758   } else h->Reset();
759   fHistos->AddAt(h, kEtaPhi);
760   
761   // Nclusters vs eta distribution for TPC reference tracks
762   if(!(h = (TH2F*)gROOT->FindObject("hEtaNclsTPC"))){
763     h = new TH2F("hEtaNclsTPC", Form("TPC Nclusters vs. #eta, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
764                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, -1, 1, 160, 0, 160.);
765   } else h->Reset();
766   fHistos->AddAt(h, kEtaNclsTPC);
767   
768   // Nclusters vs phi distribution for TPC reference tracks
769   if(!(h = (TH2F*)gROOT->FindObject("hPhiNclsTPC"))){
770     h = new TH2F("hPhiNclsTPC", Form("TPC Nclusters vs. #varphi, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
771                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 157, 0, 6.28, 160, 0, 160.);
772   } else h->Reset();
773   fHistos->AddAt(h, kPhiNclsTPC);
774   
775   // Ntracklets/track vs P for TRD reference tracks
776   Double_t binsP[19] = {0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.7, 2.0,
777                         2.5, 3.0, 3.5, 4.0, 5.0, 6.0, 7.0, 9.0, 12.0};
778   if(!(h = (TH2F*)gROOT->FindObject("hNTrackletsTRD"))){
779     h = new TH2F("hNTrackletsTRD", Form("TRD Ntracklets/track vs. P, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
780                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 18, binsP, 7, -0.5, 6.5);
781   } else h->Reset();
782   fHistos->AddAt(h, kNTrackletsTRD);
783   
784   // Nclusters/track vs P for TRD reference tracks
785   if(!(h = (TH2F*)gROOT->FindObject("hNClsTrackTRD"))){
786     h = new TH2F("hNClsTrackTRD", Form("TRD Nclusters/track vs. P, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
787                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 18, binsP, 180, 0., 180.);
788   } else h->Reset();
789   fHistos->AddAt(h, kNClsTrackTRD);
790   
791   // <PH> vs slice number for TRD reference tracklets
792   if(!(h = (TH2F*)gROOT->FindObject("hPHSlice"))){
793     h = new TH2F("hPHSlice", Form("<PH> vs sliceNo, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
794                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 8, -0.5, 7.5, 2000, 0., 2000.);
795   } else h->Reset();
796   fHistos->AddAt(h, kPHSlice);
797   
798   // Qtot vs P for TRD reference tracklets
799   if(!(h = (TH2F*)gROOT->FindObject("hQtotP"))){
800     h = new TH2F("hQtotP", Form("Qtot(from slices) vs P, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
801                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 18, binsP, 400, 0., 200);
802   } else h->Reset();
803   fHistos->AddAt(h, kQtotP);
804   
805   // (X,Y,momentum) distribution after AliESDtrack::PropagateTo(r=300.)
806   if(!(h = (TH3F*)gROOT->FindObject("hPropagXYvsP"))){
807     h = new TH3F("hPropagXYvsP", Form("(x,y) vs P after AliESDtrack::PropagateTo(r=300.), |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
808                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 100,-500,500, 100,-500,500, 10, 0.,10.);
809   } else h->Reset();
810   fHistos->AddAt(h, kPropagXYvsP);
811   
812   // (R,Z,momentum) distribution after AliESDtrack::PropagateTo(r=300.)
813   if(!(h = (TH3F*)gROOT->FindObject("hPropagRZvsP"))){
814     h = new TH3F("hPropagRZvsP", Form("(r,z) vs P after AliESDtrack::PropagateTo(r=300.), |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
815                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 100,-350., 350., 100,0.,500., 10, 0.,10.);
816   } else h->Reset();
817   fHistos->AddAt(h, kPropagRZvsP);
818   
819   Float_t etaBinLimits[51];     
820   for(Int_t i=0; i<51; i++) etaBinLimits[i] = -1.0 + i*2.0/50.;
821   Float_t phiBinLimits[151];
822   for(Int_t i=0; i<151; i++) phiBinLimits[i] = -1.1*TMath::Pi() + i*2.2*TMath::Pi()/150.;
823   // (eta,detector phi,P) distribution of reference TPC positive tracks
824   if(!(h = (TH3F*)gROOT->FindObject("hTPCRefTracksPos"))){
825     h = new TH3F("hTPCRefTracksPos", Form("(#eta,detector #varphi,p) for TPC positive reference tracks, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
826                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, etaBinLimits, 150, phiBinLimits, 32, binPtLimits);
827   } else h->Reset();
828   fHistos->AddAt(h, kTPCRefTracksPos);
829   
830   // (eta,detector phi,P) distribution of reference TPC negative tracks
831   if(!(h = (TH3F*)gROOT->FindObject("hTPCRefTracksNeg"))){
832     h = new TH3F("hTPCRefTracksNeg", Form("(#eta,detector #varphi,p) for TPC negative reference tracks, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d",
833                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, etaBinLimits, 150, phiBinLimits, 32, binPtLimits);
834   } else h->Reset();
835   fHistos->AddAt(h, kTPCRefTracksNeg);
836   
837   // (eta,detector phi,P) distribution of reference TRD positive tracks
838   if(!(h = (TH3F*)gROOT->FindObject("hTRDRefTracksPos"))){
839     h = new TH3F("hTRDRefTracksPos", Form("(#eta,detector #varphi,p) for TRD positive reference tracks, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq1",
840                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, etaBinLimits, 150, phiBinLimits, 32, binPtLimits);
841   } else h->Reset();
842   fHistos->AddAt(h, kTRDRefTracksPos);
843   
844   // (eta,detector phi,P) distribution of reference TRD negative tracks
845   if(!(h = (TH3F*)gROOT->FindObject("hTRDRefTracksNeg"))){
846     h = new TH3F("hTRDRefTracksNeg", Form("(#eta,detector #varphi,p) for TRD negative reference tracks, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq1",
847                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, etaBinLimits, 150, phiBinLimits, 32, binPtLimits);
848   } else h->Reset();
849   fHistos->AddAt(h, kTRDRefTracksNeg);
850   
851   // (eta,detector phi) profile of average number of TRD tracklets/track
852   if(!(h = (TProfile2D*)gROOT->FindObject("hTRDEtaPhiAvNtrkl"))){
853     h = new TProfile2D("hTRDEtaPhiAvNtrkl", Form("<Ntracklets/track> vs (#eta,detector #varphi) for TRD reference tracks, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq1",
854                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, -1.0, 1.0, 150, -1.1*TMath::Pi(), 1.1*TMath::Pi());
855   } else h->Reset();
856   fHistos->AddAt(h, kTRDEtaPhiAvNtrkl);
857
858   // (eta,delta phi) profile of average number of TRD tracklets/track
859   if(!(h = (TProfile2D*)gROOT->FindObject("hTRDEtaDeltaPhiAvNtrkl"))){
860     h = new TProfile2D("hTRDEtaDeltaPhiAvNtrkl", Form("<Ntracklets/track> vs (#eta, #Delta#varphi) for TRD reference tracks, |#eta|<%.1f and pt>%.1f, |DCAxy|<%.1f, |DCAz|<%.1f, TPC nclusters>%d, nTRDtracklets#geq1",
861                  fgkEta, fgkPt, fgkTrkDCAxy, fgkTrkDCAz, fgkNclTPC), 50, -1.0, 1.0, 50, -0.4*TMath::Pi(), 0.4*TMath::Pi());
862   } else h->Reset();
863   fHistos->AddAt(h, kTRDEtaDeltaPhiAvNtrkl);
864   
865   return fHistos;
866 }
867
868 //____________________________________________________________________
869 Bool_t AliTRDcheckESD::Load(const Char_t *file, const Char_t *dir, const Char_t *name)
870 {
871 // Load data from performance file
872
873   if(!TFile::Open(file)){
874     AliWarning(Form("Couldn't open file %s.", file));
875     return kFALSE;
876   }
877   if(dir){
878     if(!gFile->cd(dir)){
879       AliWarning(Form("Couldn't cd to %s in %s.", dir, file));
880       return kFALSE;
881     }
882   }
883   TObjArray *o(NULL);
884   const Char_t *tn=(name ? name : GetName());
885   if(!(o = (TObjArray*)gDirectory->Get(tn))){
886     AliWarning(Form("Missing histogram container %s.", tn));
887     return kFALSE;
888   }
889   fHistos = (TObjArray*)o->Clone(GetName());
890   gFile->Close();
891   return kTRUE;
892 }
893
894 //_______________________________________________________
895 Bool_t AliTRDcheckESD::PutTrendValue(const Char_t *name, Double_t val)
896 {
897 // Dump trending value to default file
898
899   if(!fgFile){
900     fgFile = fopen("TRD.Performance.txt", "at");
901   }
902   fprintf(fgFile, "%s_%s %f\n", GetName(), name, val);
903   return kTRUE;
904 }
905
906 //____________________________________________________________________
907 void AliTRDcheckESD::Terminate(Option_t *)
908 {
909 // Steer post-processing 
910   if(!fHistos){
911     fHistos = dynamic_cast<TObjArray *>(GetOutputData(1));
912     if(!fHistos){
913       AliError("Histogram container not found in output");
914       return;
915     }
916   }
917
918   const Char_t *name[kNrefs] = {
919     "Ncl", "Eff", "Eloss", "PtResDCA"
920   };
921   TObjArray *arr(NULL); TGraph *g(NULL);
922   if(!fResults){
923     fResults = new TObjArray(kNrefs);
924     fResults->SetOwner();
925     fResults->SetName("results");
926     for(Int_t iref(0); iref<kNrefs; iref++){
927       fResults->AddAt(arr = new TObjArray(fgkNgraph[iref]), iref);
928       arr->SetName(name[iref]);  arr->SetOwner();
929       switch(iref){
930       case kNCl:
931         for(Int_t ig(0); ig<fgkNgraph[iref]; ig++){
932           arr->AddAt(g = new TGraphErrors(), ig);
933           g->SetLineColor(ig+1); 
934           g->SetMarkerColor(ig+1); 
935           g->SetMarkerStyle(ig+20); 
936           g->SetName(Form("s%d", ig));
937           switch(ig){
938           case 0: g->SetTitle("ALL"); break;
939           case 1: g->SetTitle("NEG"); break;
940           case 2: g->SetTitle("POS"); break;
941           default: g->SetTitle(AliPID::ParticleLatexName(ig-3)); break;
942           };
943         }
944         break;
945       case kTRDmom:
946         for(Int_t ig(0); ig<fgkNgraph[iref]; ig++){
947           arr->AddAt(g = new TGraphAsymmErrors(), ig);
948           g->SetLineColor(ig+1); 
949           g->SetMarkerColor(ig+1); 
950           g->SetMarkerStyle(ig+20); 
951         }
952         break;
953       case kPtRes:
954         for(Int_t idx(0); idx<AliPID::kSPECIES; idx++){
955           Int_t ig(2*idx);
956           arr->AddAt(g = new TGraphErrors(), ig);
957           g->SetLineColor(kRed-idx); 
958           g->SetMarkerColor(kRed-idx); 
959           g->SetMarkerStyle(20+idx); 
960           g->SetNameTitle(Form("s%d", ig), Form("res %s", AliPID::ParticleLatexName(idx)));
961           arr->AddAt(g = new TGraphErrors(), ig+1);
962           g->SetLineColor(kBlue-idx); 
963           g->SetMarkerColor(kBlue-idx); 
964           g->SetMarkerStyle(20+idx); 
965           g->SetNameTitle(Form("m%d", ig+1), Form("sys %s", AliPID::ParticleLatexName(idx)));
966
967           ig+=10;
968           arr->AddAt(g = new TGraphErrors(), ig);
969           g->SetLineColor(kRed-idx); 
970           g->SetMarkerColor(kRed-idx); 
971           g->SetMarkerStyle(20+idx); 
972           g->SetNameTitle(Form("s%d", ig), Form("sigma %s", AliPID::ParticleLatexName(idx)));
973           arr->AddAt(g = new TGraphErrors(), ig+1);
974           g->SetLineColor(kBlue-idx); 
975           g->SetMarkerColor(kBlue-idx); 
976           g->SetMarkerStyle(20+idx); 
977           g->SetNameTitle(Form("m%d", ig+1), Form("mean %s", AliPID::ParticleLatexName(idx)));
978         }
979         break;
980       default:
981         for(Int_t ig(0); ig<fgkNgraph[iref]; ig++){
982           arr->AddAt(g = new TGraphErrors(), ig);
983           g->SetLineColor(ig+1); 
984           g->SetMarkerColor(ig+1); 
985           g->SetMarkerStyle(ig+20); 
986         }
987         break;
988       }
989     }
990   }
991   TH1 *h1[2] = {NULL, NULL};
992   TH2I *h2(NULL);
993   TAxis *ax(NULL);
994
995   // No of clusters
996   if(!(h2 = (TH2I*)fHistos->At(kNCl))) return;
997   ax = h2->GetXaxis();
998   arr = (TObjArray*)fResults->At(kNCl);
999   // All tracks
1000   h1[0] = h2->ProjectionX("Ncl_px");
1001   TGraphErrors *ge=(TGraphErrors*)arr->At(0);
1002   for(Int_t ib=2; ib<=ax->GetNbins(); ib++){
1003     ge->SetPoint(ib-2, ax->GetBinCenter(ib), h1[0]->GetBinContent(ib));
1004   }
1005   // All charged tracks
1006   TH1 *hNclCh[2] = {(TH1D*)h1[0]->Clone("NEG"), (TH1D*)h1[0]->Clone("POS")};
1007   hNclCh[0]->Reset();hNclCh[1]->Reset();
1008   for(Int_t is(1); is<=AliPID::kSPECIES; is++){
1009     hNclCh[0]->Add(h2->ProjectionX("Ncl_px", 2*is-1, 2*is-1)); // neg
1010     hNclCh[1]->Add(h2->ProjectionX("Ncl_px", 2*is, 2*is));     // pos
1011   }
1012   if(Int_t(hNclCh[0]->GetEntries())){
1013     ge=(TGraphErrors*)arr->At(1);
1014     for(Int_t ib=2; ib<=ax->GetNbins(); ib++){
1015       ge->SetPoint(ib-2, ax->GetBinCenter(ib), hNclCh[0]->GetBinContent(ib));
1016     }
1017   }
1018   if(Int_t(hNclCh[1]->GetEntries())){
1019     ge=(TGraphErrors*)arr->At(2);
1020     for(Int_t ib=2; ib<=ax->GetNbins(); ib++){
1021       ge->SetPoint(ib-2, ax->GetBinCenter(ib), hNclCh[1]->GetBinContent(ib));
1022     }
1023   }
1024   // Species wise
1025   for(Int_t is(1); is<=AliPID::kSPECIES; is++){
1026     h1[0] = h2->ProjectionX("Ncl_px", 2*is-1, 2*is);
1027     if(!Int_t(h1[0]->GetEntries())) continue;
1028     ge=(TGraphErrors*)arr->At(2+is);
1029     for(Int_t ib=2; ib<=ax->GetNbins(); ib++){
1030       ge->SetPoint(ib-2, ax->GetBinCenter(ib), h1[0]->GetBinContent(ib));
1031     }
1032   }
1033   fNRefFigures = 1;
1034
1035   // EFFICIENCY
1036   // geometrical efficiency
1037   if(!(h2 = (TH2I*)fHistos->At(kTRDstat))) return;
1038   arr = (TObjArray*)fResults->At(kTRDstat);
1039   h1[0] = h2->ProjectionX("checkESDx0", kTPCout, kTPCout);
1040   h1[1] = h2->ProjectionX("checkESDx1", kTRDin, kTRDin);
1041   Process(h1, (TGraphErrors*)arr->At(0));
1042   delete h1[0];delete h1[1];
1043   // tracking efficiency
1044   h1[0] = h2->ProjectionX("checkESDx0", kTRDin, kTRDin);
1045   h1[1] = h2->ProjectionX("checkESDx1", kTRDout, kTRDout);
1046   Process(h1, (TGraphErrors*)arr->At(1));
1047   delete h1[1];
1048   // PID efficiency
1049   h1[1] = h2->ProjectionX("checkESDx1", kTRDpid, kTRDpid);
1050   Process(h1, (TGraphErrors*)arr->At(2));
1051   delete h1[1];
1052   // Refit efficiency
1053   h1[1] = h2->ProjectionX("checkESDx1", kTRDref, kTRDref);
1054   Process(h1, (TGraphErrors*)arr->At(3));
1055   delete h1[1];
1056   fNRefFigures++;
1057
1058   // ENERGY LOSS
1059   if(!(h2 = dynamic_cast<TH2I*>(fHistos->At(kTRDmom)))) return;
1060   arr = (TObjArray*)fResults->At(kTRDmom);
1061   TGraphAsymmErrors *g06 = (TGraphAsymmErrors*)arr->At(0), *g09 = (TGraphAsymmErrors*)arr->At(1);
1062   ax=h2->GetXaxis();
1063   const Int_t nq(4);
1064   const Double_t xq[nq] = {0.05, 0.2, 0.8, 0.95};
1065   Double_t yq[nq];
1066   for(Int_t ily=6; ily--;){
1067     h1[0] = h2->ProjectionX("checkESDp0", ily+1, ily+1);
1068     h1[0]->GetQuantiles(nq,yq,xq);
1069     g06->SetPoint(ily, Float_t(ily), ax->GetBinCenter(h1[0]->GetMaximumBin()));
1070     g06->SetPointError(ily, 0., 0., TMath::Abs(yq[0]), yq[3]);
1071     g09->SetPoint(ily, Float_t(ily), h1[0]->GetMean());
1072     g09->SetPointError(ily, 0., 0., TMath::Abs(yq[1]), yq[2]);
1073
1074     //printf(" max[%f] mean[%f] q[%f %f %f %f]\n", ax->GetBinCenter(h1[0]->GetMaximumBin()), h1[0]->GetMean(), yq[0], yq[1], yq[2], yq[3]);
1075     delete h1[0];
1076   }
1077   fNRefFigures++;
1078   if(!HasMC()) return;
1079
1080   // Pt RESOLUTION @ DCA
1081   TH3S* h3(NULL); TGraphErrors *gg[2] = {NULL,NULL};
1082   if(!(h3 = dynamic_cast<TH3S*>(fHistos->At(kPtRes)))) return;
1083   arr = (TObjArray*)fResults->At(kPtRes);
1084   TAxis *az(h3->GetZaxis());
1085   for(Int_t i(0); i<AliPID::kSPECIES; i++){
1086     Int_t idx(2*i);
1087     az->SetRange(idx+1, idx+2); 
1088     gg[1] = (TGraphErrors*)arr->At(idx);
1089     gg[0] = (TGraphErrors*)arr->At(idx+1);
1090     Process2D((TH2*)h3->Project3D("yx"), gg);
1091
1092     idx+=10;
1093     az->SetRange(idx+1, idx+2); 
1094     gg[1] = (TGraphErrors*)arr->At(idx);
1095     gg[0] = (TGraphErrors*)arr->At(idx+1);
1096     Process2D((TH2*)h3->Project3D("yx"), gg);
1097   }
1098   fNRefFigures++;
1099 }
1100
1101 //____________________________________________________________________
1102 Int_t AliTRDcheckESD::Pdg2Idx(Int_t pdg)
1103 {
1104   switch(pdg){
1105   case kElectron: return AliPID::kElectron;  
1106   case kMuonMinus: return AliPID::kMuon;  
1107   case kPiPlus: return AliPID::kPion;  
1108   case kKPlus: return AliPID::kKaon;
1109   case kProton: return AliPID::kProton;
1110   } 
1111   return -1;
1112 }
1113
1114 //____________________________________________________________________
1115 void AliTRDcheckESD::Process(TH1 **h1, TGraphErrors *g)
1116 {
1117 // Generic function to process one reference plot
1118
1119   Int_t n1 = 0, n2 = 0, ip=0;
1120   Double_t eff = 0.;
1121
1122   TAxis *ax = h1[0]->GetXaxis();
1123   for(Int_t ib=1; ib<=ax->GetNbins(); ib++){
1124     if(!(n1 = (Int_t)h1[0]->GetBinContent(ib))) continue;
1125     n2 = (Int_t)h1[1]->GetBinContent(ib);
1126     eff = n2/Float_t(n1);
1127
1128     ip=g->GetN();
1129     g->SetPoint(ip, ax->GetBinCenter(ib), eff);
1130     g->SetPointError(ip, 0., n2 ? eff*TMath::Sqrt(1./n1+1./n2) : 0.);
1131   }
1132 }  
1133 //________________________________________________________
1134 void AliTRDcheckESD::Process2D(TH2 * const h2, TGraphErrors **g)
1135 {
1136   //
1137   // Do the processing
1138   //
1139
1140   Int_t n = 0;
1141   if((n=g[0]->GetN())) for(;n--;) g[0]->RemovePoint(n);
1142   if((n=g[1]->GetN())) for(;n--;) g[1]->RemovePoint(n);
1143   TF1 f("fg", "gaus", -3.,3.);
1144   for(Int_t ibin = 1; ibin <= h2->GetNbinsX(); ibin++){
1145     Double_t x = h2->GetXaxis()->GetBinCenter(ibin);
1146     TH1D *h = h2->ProjectionY("py", ibin, ibin);
1147     if(h->GetEntries()<100) continue;
1148     //AdjustF1(h, f);
1149
1150     h->Fit(&f, "QN");
1151     Int_t ip = g[0]->GetN();
1152     g[0]->SetPoint(ip, x, f.GetParameter(1));
1153     g[0]->SetPointError(ip, 0., f.GetParError(1));
1154     g[1]->SetPoint(ip, x, f.GetParameter(2));
1155     g[1]->SetPointError(ip, 0., f.GetParError(2));
1156   }
1157   return;
1158 }
1159 //____________________________________________________________________
1160 void AliTRDcheckESD::PrintStatus(ULong_t status)
1161 {
1162 // Dump track status to stdout
1163
1164   printf("ITS[i(%d) o(%d) r(%d)] TPC[i(%d) o(%d) r(%d) p(%d)] TRD[i(%d) o(%d) r(%d) p(%d) s(%d)] HMPID[o(%d) p(%d)]\n"
1165     ,Bool_t(status & AliESDtrack::kITSin)
1166     ,Bool_t(status & AliESDtrack::kITSout)
1167     ,Bool_t(status & AliESDtrack::kITSrefit)
1168     ,Bool_t(status & AliESDtrack::kTPCin)
1169     ,Bool_t(status & AliESDtrack::kTPCout)
1170     ,Bool_t(status & AliESDtrack::kTPCrefit)
1171     ,Bool_t(status & AliESDtrack::kTPCpid)
1172     ,Bool_t(status & AliESDtrack::kTRDin)
1173     ,Bool_t(status & AliESDtrack::kTRDout)
1174     ,Bool_t(status & AliESDtrack::kTRDrefit)
1175     ,Bool_t(status & AliESDtrack::kTRDpid)
1176     ,Bool_t(status & AliESDtrack::kTRDStop)
1177     ,Bool_t(status & AliESDtrack::kHMPIDout)
1178     ,Bool_t(status & AliESDtrack::kHMPIDpid)
1179   );
1180 }
1181