]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDresolution.cxx
clear debug cluster array after each tracklet fill
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDresolution.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-commercialf 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 /* $Id: AliTRDresolution.cxx 27496 2008-07-22 08:35:45Z cblume $ */
17
18 ////////////////////////////////////////////////////////////////////////////
19 //                                                                        //
20 //  TRD tracking resolution                                               //
21 //
22 // The class performs resolution and residual studies 
23 // of the TRD tracks for the following quantities :
24 //   - spatial position (y, [z])
25 //   - angular (phi) tracklet
26 //   - momentum at the track level
27 // 
28 // The class has to be used for regular detector performance checks using the official macros:
29 //   - $ALICE_ROOT/TRD/qaRec/run.C
30 //   - $ALICE_ROOT/TRD/qaRec/makeResults.C
31 // 
32 // For stand alone usage please refer to the following example: 
33 // {  
34 //   gSystem->Load("libANALYSIS.so");
35 //   gSystem->Load("libTRDqaRec.so");
36 //   AliTRDresolution *res = new AliTRDresolution();
37 //   //res->SetMCdata();
38 //   //res->SetVerbose();
39 //   //res->SetVisual();
40 //   res->Load();
41 //   if(!res->PostProcess()) return;
42 //   res->GetRefFigure(0);
43 // }  
44 //
45 //  Authors:                                                              //
46 //    Alexandru Bercuci <A.Bercuci@gsi.de>                                //
47 //    Markus Fasel <M.Fasel@gsi.de>                                       //
48 //                                                                        //
49 ////////////////////////////////////////////////////////////////////////////
50
51 #include <TSystem.h>
52
53 #include <TROOT.h>
54 #include <TObjArray.h>
55 #include <TH3.h>
56 #include <TH2.h>
57 #include <TH1.h>
58 #include <TF1.h>
59 #include <TCanvas.h>
60 #include <TGaxis.h>
61 #include <TBox.h>
62 #include <TLegend.h>
63 #include <TGraphErrors.h>
64 #include <TGraphAsymmErrors.h>
65 #include <TMath.h>
66 #include <TMatrixT.h>
67 #include <TVectorT.h>
68 #include <TTreeStream.h>
69 #include <TGeoManager.h>
70 #include <TDatabasePDG.h>
71
72 #include "AliPID.h"
73 #include "AliLog.h"
74 #include "AliESDtrack.h"
75
76 #include "AliTRDresolution.h"
77 #include "AliTRDgeometry.h"
78 #include "AliTRDpadPlane.h"
79 #include "AliTRDcluster.h"
80 #include "AliTRDseedV1.h"
81 #include "AliTRDtrackV1.h"
82 #include "AliTRDReconstructor.h"
83 #include "AliTRDrecoParam.h"
84 #include "AliTRDpidUtil.h"
85
86 #include "info/AliTRDclusterInfo.h"
87
88 ClassImp(AliTRDresolution)
89
90 UChar_t const AliTRDresolution::fgNproj[kNviews] = {
91   2, 2, 5, 5, 5,
92   2, 5, 11, 11, 11
93 };
94 Char_t const * AliTRDresolution::fgPerformanceName[kNviews] = {
95      "Charge"
96     ,"Cluster2Track"
97     ,"Tracklet2Track"
98     ,"Tracklet2TRDin" 
99     ,"Tracklet2TRDout" 
100     ,"Cluster2MC"
101     ,"Tracklet2MC"
102     ,"TRDin2MC"
103     ,"TRDout2MC"
104     ,"TRD2MC"
105 };
106 // Configure segmentation for y resolution/residuals
107 Int_t const AliTRDresolution::fgkNresYsegm[3] = {
108   AliTRDgeometry::kNsector
109  ,AliTRDgeometry::kNsector*AliTRDgeometry::kNstack
110  ,AliTRDgeometry::kNdet
111 };
112 Char_t const *AliTRDresolution::fgkResYsegmName[3] = {
113   "Sector", "Stack", "Detector"};
114
115
116 //________________________________________________________
117 AliTRDresolution::AliTRDresolution()
118   :AliTRDrecoTask()
119   ,fStatus(0)
120   ,fSegmentLevel(0)
121   ,fIdxPlot(0)
122   ,fIdxFrame(0)
123   ,fPtThreshold(1.)
124   ,fReconstructor(NULL)
125   ,fGeo(NULL)
126   ,fDBPDG(NULL)
127   ,fGraphS(NULL)
128   ,fGraphM(NULL)
129   ,fCl(NULL)
130   ,fMCcl(NULL)
131 /*  ,fTrklt(NULL)
132   ,fMCtrklt(NULL)*/
133 {
134   //
135   // Default constructor
136   //
137   SetNameTitle("TRDresolution", "TRD spatial and momentum resolution");
138   SetSegmentationLevel();
139 }
140
141 //________________________________________________________
142 AliTRDresolution::AliTRDresolution(char* name)
143   :AliTRDrecoTask(name, "TRD spatial and momentum resolution")
144   ,fStatus(0)
145   ,fSegmentLevel(0)
146   ,fIdxPlot(0)
147   ,fIdxFrame(0)
148   ,fPtThreshold(1.)
149   ,fReconstructor(NULL)
150   ,fGeo(NULL)
151   ,fDBPDG(NULL)
152   ,fGraphS(NULL)
153   ,fGraphM(NULL)
154   ,fCl(NULL)
155   ,fMCcl(NULL)
156 /*  ,fTrklt(NULL)
157   ,fMCtrklt(NULL)*/
158 {
159   //
160   // Default constructor
161   //
162
163   fReconstructor = new AliTRDReconstructor();
164   fReconstructor->SetRecoParam(AliTRDrecoParam::GetLowFluxParam());
165   fGeo = new AliTRDgeometry();
166
167   InitFunctorList();
168   SetSegmentationLevel();
169
170   DefineOutput(kClToTrk, TObjArray::Class()); // cluster2track
171   DefineOutput(kClToMC, TObjArray::Class()); // cluster2mc
172 /*  DefineOutput(kTrkltToTrk, TObjArray::Class()); // tracklet2track
173   DefineOutput(kTrkltToMC, TObjArray::Class()); // tracklet2mc*/
174 }
175
176 //________________________________________________________
177 AliTRDresolution::~AliTRDresolution()
178 {
179   //
180   // Destructor
181   //
182
183   if(fGraphS){fGraphS->Delete(); delete fGraphS;}
184   if(fGraphM){fGraphM->Delete(); delete fGraphM;}
185   delete fGeo;
186   delete fReconstructor;
187   if(gGeoManager) delete gGeoManager;
188   if(fCl){fCl->Delete(); delete fCl;}
189   if(fMCcl){fMCcl->Delete(); delete fMCcl;}
190 /*  if(fTrklt){fTrklt->Delete(); delete fTrklt;}
191   if(fMCtrklt){fMCtrklt->Delete(); delete fMCtrklt;}*/
192 }
193
194
195 //________________________________________________________
196 void AliTRDresolution::UserCreateOutputObjects()
197 {
198   // spatial resolution
199
200   if(!fReconstructor){
201     fReconstructor = new AliTRDReconstructor();
202     fReconstructor->SetRecoParam(AliTRDrecoParam::GetLowFluxParam());
203   }
204   if(!fGeo) fGeo = new AliTRDgeometry();
205
206   if(!HasFunctorList()) InitFunctorList();
207   fContainer = Histos();
208   InitExchangeContainers();
209 }
210
211 //________________________________________________________
212 void AliTRDresolution::InitExchangeContainers()
213 {
214   fCl = new TObjArray();
215   fCl->SetOwner(kTRUE);
216   fMCcl = new TObjArray();
217   fMCcl->SetOwner(kTRUE);
218 /*  fTrklt = new TObjArray();
219   fTrklt->SetOwner(kTRUE);
220   fMCtrklt = new TObjArray();
221   fMCtrklt->SetOwner(kTRUE);*/
222 }
223
224 //________________________________________________________
225 void AliTRDresolution::UserExec(Option_t *opt)
226 {
227   //
228   // Execution part
229   //
230
231   fCl->Delete();
232   fMCcl->Delete();
233 /*  fTrklt->Delete();
234   fMCtrklt->Delete();*/
235   AliTRDrecoTask::UserExec(opt);
236   PostData(kClToTrk, fCl);
237   PostData(kClToMC, fMCcl);
238 /*  PostData(kTrkltToTrk, fTrklt);
239   PostData(kTrkltToMC, fMCtrklt);*/
240 }
241
242 //________________________________________________________
243 Bool_t AliTRDresolution::Pulls(Double_t dyz[2], Double_t cov[3], Double_t tilt)
244 {
245 // Helper function to calculate pulls in the yz plane 
246 // using proper tilt rotation
247 // Uses functionality defined by AliTRDseedV1.
248
249   Double_t t2(tilt*tilt);
250
251   // rotate along pad
252   Double_t cc[3];
253   cc[0] = cov[0] - 2.*tilt*cov[1] + t2*cov[2]; 
254   cc[1] = cov[1]*(1.-t2) + tilt*(cov[0] - cov[2]);
255   cc[2] = t2*cov[0] + 2.*tilt*cov[1] + cov[2];
256   // do sqrt
257   Double_t sqr[3]={0., 0., 0.}; 
258   if(AliTRDseedV1::GetCovSqrt(cc, sqr)) return kFALSE;
259   Double_t invsqr[3]={0., 0., 0.}; 
260   if(AliTRDseedV1::GetCovInv(sqr, invsqr)<1.e-40) return kFALSE;
261   Double_t tmp(dyz[0]);
262   dyz[0] = invsqr[0]*tmp + invsqr[1]*dyz[1];
263   dyz[1] = invsqr[1]*tmp + invsqr[2]*dyz[1];
264   return kTRUE;
265 }
266
267 //________________________________________________________
268 TH1* AliTRDresolution::PlotCharge(const AliTRDtrackV1 *track)
269 {
270   //
271   // Plots the charge distribution
272   //
273
274   if(track) fkTrack = track;
275   if(!fkTrack){
276     AliDebug(4, "No Track defined.");
277     return NULL;
278   }
279   TObjArray *arr = NULL;
280   if(!fContainer || !(arr = ((TObjArray*)fContainer->At(kCharge)))){
281     AliWarning("No output container defined.");
282     return NULL;
283   }
284   TH3S* h = NULL;
285
286   AliTRDseedV1 *fTracklet = NULL;  
287   AliTRDcluster *c = NULL;
288   for(Int_t ily=0; ily<AliTRDgeometry::kNlayer; ily++){
289     if(!(fTracklet = fkTrack->GetTracklet(ily))) continue;
290     if(!fTracklet->IsOK()) continue;
291     Float_t x0 = fTracklet->GetX0();
292     Float_t dq, dl;
293     for(Int_t itb=AliTRDseedV1::kNtb; itb--;){
294       if(!(c = fTracklet->GetClusters(itb))){ 
295         if(!(c = fTracklet->GetClusters(AliTRDseedV1::kNtb+itb))) continue;
296       }
297       dq = fTracklet->GetdQdl(itb, &dl);
298       dl /= 0.15; // dl/dl0, dl0 = 1.5 mm for nominal vd
299       (h = (TH3S*)arr->At(0))->Fill(dl, x0-c->GetX(), dq);
300     }
301
302 //     if(!HasMCdata()) continue;
303 //     UChar_t s;
304 //     Float_t pt0, y0, z0, dydx0, dzdx0;
305 //     if(!fMC->GetDirections(x0, y0, z0, dydx0, dzdx0, pt0, s)) continue;
306
307   }
308   return h;
309 }
310
311
312 //________________________________________________________
313 TH1* AliTRDresolution::PlotCluster(const AliTRDtrackV1 *track)
314 {
315   //
316   // Plot the cluster distributions
317   //
318
319   if(track) fkTrack = track;
320   if(!fkTrack){
321     AliDebug(4, "No Track defined.");
322     return NULL;
323   }
324   TObjArray *arr = NULL;
325   if(!fContainer || !(arr = ((TObjArray*)fContainer->At(kCluster)))){
326     AliWarning("No output container defined.");
327     return NULL;
328   }
329   ULong_t status = fkESD ? fkESD->GetStatus():0;
330
331   Int_t sgm[3];
332   Double_t covR[7], cov[3], dy[2], dz[2];
333   Float_t pt, x0, y0, z0, dydx, dzdx;
334   AliTRDseedV1 *fTracklet(NULL);  TObjArray *clInfoArr(NULL);
335   for(Int_t ily=0; ily<AliTRDgeometry::kNlayer; ily++){
336     if(!(fTracklet = fkTrack->GetTracklet(ily))) continue;
337     if(!fTracklet->IsOK()) continue;
338     x0 = fTracklet->GetX0();
339     pt = fTracklet->GetPt();
340     sgm[2] = fTracklet->GetDetector();
341     sgm[0] = AliTRDgeometry::GetSector(sgm[2]);
342     sgm[1] = sgm[0] * AliTRDgeometry::kNstack + AliTRDgeometry::GetStack(sgm[2]);
343     
344     // retrive the track angle with the chamber
345     y0   = fTracklet->GetYref(0);
346     z0   = fTracklet->GetZref(0);
347     dydx = fTracklet->GetYref(1);
348     dzdx = fTracklet->GetZref(1);
349     fTracklet->GetCovRef(covR);
350     Double_t tilt(fTracklet->GetTilt())
351             ,t2(tilt*tilt)
352             ,corr(1./(1. + t2))
353             ,cost(TMath::Sqrt(corr));
354     AliTRDcluster *c = NULL;
355     fTracklet->ResetClusterIter(kFALSE);
356     while((c = fTracklet->PrevCluster())){
357       Float_t xc = c->GetX();
358       Float_t yc = c->GetY();
359       Float_t zc = c->GetZ();
360       Float_t dx = x0 - xc; 
361       Float_t yt = y0 - dx*dydx;
362       Float_t zt = z0 - dx*dzdx; 
363       dy[0] = yc-yt; dz[0]= zc-zt;
364
365       // rotate along pad
366       dy[1] = cost*(dy[0] - dz[0]*tilt);
367       dz[1] = cost*(dz[0] + dy[0]*tilt);
368       if(pt>fPtThreshold && c->IsInChamber()) ((TH3S*)arr->At(0))->Fill(dydx, dy[1], sgm[fSegmentLevel]);
369
370       // tilt rotation of covariance for clusters
371       Double_t sy2(c->GetSigmaY2()), sz2(c->GetSigmaZ2());
372       cov[0] = (sy2+t2*sz2)*corr;
373       cov[1] = tilt*(sz2 - sy2)*corr;
374       cov[2] = (t2*sy2+sz2)*corr;
375       // sum with track covariance
376       cov[0]+=covR[0]; cov[1]+=covR[1]; cov[2]+=covR[2];
377       Double_t dyz[2]= {dy[1], dz[1]};
378       Pulls(dyz, cov, tilt);
379       ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], dyz[0], dyz[1]);
380   
381       // Get z-position with respect to anode wire
382       Int_t istk = fGeo->GetStack(c->GetDetector());
383       AliTRDpadPlane *pp = fGeo->GetPadPlane(ily, istk);
384       Float_t row0 = pp->GetRow0();
385       Float_t d  =  row0 - zt + pp->GetAnodeWireOffset();
386       d -= ((Int_t)(2 * d)) / 2.0;
387       if (d > 0.25) d  = 0.5 - d;
388
389       AliTRDclusterInfo *clInfo = new AliTRDclusterInfo;
390       clInfo->SetCluster(c);
391       Float_t covF[] = {cov[0], cov[1], cov[2]};
392       clInfo->SetGlobalPosition(yt, zt, dydx, dzdx, covF);
393       clInfo->SetResolution(dy[1]);
394       clInfo->SetAnisochronity(d);
395       clInfo->SetDriftLength(dx);
396       clInfo->SetTilt(tilt);
397       if(fCl) fCl->Add(clInfo);
398       else AliDebug(1, "Cl exchange container missing. Activate by calling \"InitExchangeContainers()\"");
399
400       if(DebugLevel()>=1){
401         if(!clInfoArr){ 
402           clInfoArr=new TObjArray(AliTRDseedV1::kNclusters);
403           clInfoArr->SetOwner(kFALSE);
404         }
405         clInfoArr->Add(clInfo);
406       }
407     }
408     if(DebugLevel()>=1 && clInfoArr){
409       (*DebugStream()) << "cluster"
410         <<"status="  << status
411         <<"clInfo.=" << clInfoArr
412         << "\n";
413       clInfoArr->Clear();
414     }
415   }
416   if(clInfoArr) delete clInfoArr;
417   return (TH3S*)arr->At(0);
418 }
419
420
421 //________________________________________________________
422 TH1* AliTRDresolution::PlotTracklet(const AliTRDtrackV1 *track)
423 {
424 // Plot normalized residuals for tracklets to track. 
425 // 
426 // We start from the result that if X=N(|m|, |Cov|)
427 // BEGIN_LATEX
428 // (Cov^{-1})^{1/2}X = N((Cov^{-1})^{1/2}*|m|, |1|)
429 // END_LATEX
430 // in our case X=(y_trklt - y_trk z_trklt - z_trk) and |Cov| = |Cov_trklt| + |Cov_trk| at the radial 
431 // reference position. 
432   if(track) fkTrack = track;
433   if(!fkTrack){
434     AliDebug(4, "No Track defined.");
435     return NULL;
436   }
437   TObjArray *arr = NULL;
438   if(!fContainer || !(arr = (TObjArray*)fContainer->At(kTrack ))){
439     AliWarning("No output container defined.");
440     return NULL;
441   }
442
443   Int_t sgm[3];
444   Double_t cov[3], covR[7]/*, sqr[3], inv[3]*/;
445   Double_t pt, phi, tht, x, dx, dy[2], dz[2];
446   AliTRDseedV1 *fTracklet(NULL);  
447   for(Int_t il(0); il<AliTRDgeometry::kNlayer; il++){
448     if(!(fTracklet = fkTrack->GetTracklet(il))) continue;
449     if(!fTracklet->IsOK()) continue;
450     sgm[2] = fTracklet->GetDetector();
451     sgm[0] = AliTRDgeometry::GetSector(sgm[2]);
452     sgm[1] = sgm[0] * AliTRDgeometry::kNstack + AliTRDgeometry::GetStack(sgm[2]);
453     x   = fTracklet->GetX();
454     dx  = fTracklet->GetX0() - x;
455     pt  = fTracklet->GetPt();
456     phi = fTracklet->GetYref(1);
457     tht = fTracklet->GetZref(1);
458     // compute dy and dz
459     dy[0]= fTracklet->GetYref(0)-dx*fTracklet->GetYref(1) - fTracklet->GetY();
460     dz[0]= fTracklet->GetZref(0)-dx*fTracklet->GetZref(1) - fTracklet->GetZ();
461     Double_t tilt(fTracklet->GetTilt())
462             ,t2(tilt*tilt)
463             ,corr(1./(1. + t2))
464             ,cost(TMath::Sqrt(corr));
465     Bool_t rc(fTracklet->IsRowCross());
466
467     // calculate residuals using tilt rotation
468     dy[1]= cost*(dy[0] - dz[0]*tilt);
469     dz[1]= cost*(dz[0] + dy[0]*tilt);
470     ((TH3S*)arr->At(0))->Fill(phi, dy[1], sgm[fSegmentLevel]+rc*fgkNresYsegm[fSegmentLevel]);
471     ((TH3S*)arr->At(2))->Fill(tht, dz[1], rc);
472
473     // compute covariance matrix
474     fTracklet->GetCovAt(x, cov);
475     fTracklet->GetCovRef(covR);
476     cov[0] += covR[0]; cov[1] += covR[1]; cov[2] += covR[2]; 
477     Double_t dyz[2]= {dy[1], dz[1]};
478     Pulls(dyz, cov, tilt);
479     ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], dyz[0], dyz[1]);
480     ((TH3S*)arr->At(3))->Fill(tht, dyz[1], rc);
481
482     Double_t dphi((phi-fTracklet->GetYfit(1))/(1-phi*fTracklet->GetYfit(1)));
483     Double_t dtht((tht-fTracklet->GetZfit(1))/(1-tht*fTracklet->GetZfit(1)));
484     ((TH2I*)arr->At(4))->Fill(phi, TMath::ATan(dphi));
485
486     if(DebugLevel()>=1){
487       UChar_t err(fTracklet->GetErrorMsg());
488       (*DebugStream()) << "tracklet"
489         <<"pt="  << pt
490         <<"phi=" << phi
491         <<"tht=" << tht
492         <<"det=" << sgm[2]
493         <<"dy0="  << dy[0]
494         <<"dz0="  << dz[0]
495         <<"dy="  << dy[1]
496         <<"dz="  << dz[1]
497         <<"dphi="<< dphi
498         <<"dtht="<< dtht
499         <<"dyp=" << dyz[0]
500         <<"dzp=" << dyz[1]
501         <<"rc="  << rc
502         <<"err=" << err
503         << "\n";
504     }
505   }
506
507
508   return (TH2I*)arr->At(0);
509 }
510
511
512 //________________________________________________________
513 TH1* AliTRDresolution::PlotTrackIn(const AliTRDtrackV1 *track)
514 {
515 // Store resolution/pulls of Kalman before updating with the TRD information 
516 // at the radial position of the first tracklet. The following points are used 
517 // for comparison  
518 //  - the (y,z,snp) of the first TRD tracklet
519 //  - the (y, z, snp, tgl, pt) of the MC track reference
520 // 
521 // Additionally the momentum resolution/pulls are calculated for usage in the 
522 // PID calculation. 
523
524   if(track) fkTrack = track;
525   if(!fkTrack){
526     AliDebug(4, "No Track defined.");
527     return NULL;
528   }
529   TObjArray *arr = NULL;
530   if(!fContainer || !(arr = (TObjArray*)fContainer->At(kTrackIn))){
531     AliWarning("No output container defined.");
532     return NULL;
533   }
534   AliExternalTrackParam *tin = NULL;
535   if(!(tin = fkTrack->GetTrackIn())){
536     AliWarning("Track did not entered TRD fiducial volume.");
537     return NULL;
538   }
539   TH1 *h = NULL;
540   
541   Double_t x = tin->GetX();
542   AliTRDseedV1 *fTracklet = NULL;  
543   for(Int_t ily=0; ily<AliTRDgeometry::kNlayer; ily++){
544     if(!(fTracklet = fkTrack->GetTracklet(ily))) continue;
545     break;
546   }
547   if(!fTracklet || TMath::Abs(x-fTracklet->GetX())>1.e-3){
548     AliWarning("Tracklet did not match Track.");
549     return NULL;
550   }
551   Int_t sgm[3];
552   sgm[2] = fTracklet->GetDetector();
553   sgm[0] = AliTRDgeometry::GetSector(sgm[2]);
554   sgm[1] = sgm[0] * AliTRDgeometry::kNstack + AliTRDgeometry::GetStack(sgm[2]);
555   Double_t tilt(fTracklet->GetTilt())
556           ,t2(tilt*tilt)
557           ,corr(1./(1. + t2))
558           ,cost(TMath::Sqrt(corr));
559   Bool_t rc(fTracklet->IsRowCross());
560
561   const Int_t kNPAR(5);
562   Double_t parR[kNPAR]; memcpy(parR, tin->GetParameter(), kNPAR*sizeof(Double_t));
563   Double_t covR[3*kNPAR]; memcpy(covR, tin->GetCovariance(), 3*kNPAR*sizeof(Double_t));
564   Double_t cov[3]; fTracklet->GetCovAt(x, cov);
565
566   // define sum covariances
567   TMatrixDSym COV(kNPAR); TVectorD PAR(kNPAR);
568   Double_t *pc = &covR[0], *pp = &parR[0];
569   for(Int_t ir=0; ir<kNPAR; ir++, pp++){
570     PAR(ir) = (*pp);
571     for(Int_t ic = 0; ic<=ir; ic++,pc++){ 
572       COV(ir,ic) = (*pc); COV(ic,ir) = (*pc);
573     }
574   }
575   PAR[4] = TMath::Abs(PAR[4]); // remove sign of pt !!
576   //COV.Print(); PAR.Print();
577
578   //TODO Double_t dydx =  TMath::Sqrt(1.-parR[2]*parR[2])/parR[2]; 
579   Double_t dy[2]={parR[0] - fTracklet->GetY(), 0.}
580           ,dz[2]={parR[1] - fTracklet->GetZ(), 0.}
581           ,dphi(TMath::ASin(PAR[2])-TMath::ATan(fTracklet->GetYfit(1)));
582   // calculate residuals using tilt rotation
583   dy[1] = cost*(dy[0] - dz[0]*tilt);
584   dz[1] = cost*(dz[0] + dy[0]*tilt);
585
586   if(1./PAR[4]>fPtThreshold) ((TH3S*)arr->At(0))->Fill(fTracklet->GetYref(1), dy[1], sgm[fSegmentLevel]+rc*fgkNresYsegm[fSegmentLevel]);
587   ((TH3S*)arr->At(2))->Fill(fTracklet->GetZref(1), dz[1], rc);
588   ((TH2I*)arr->At(4))->Fill(fTracklet->GetYref(1), dphi);
589
590   Double_t dyz[2] = {dy[1], dz[1]};
591   Double_t cc[3] = {COV(0,0)+cov[0], COV(0,1)+cov[1], COV(1,1)+cov[2]};
592   Pulls(dyz, cc, tilt);
593   ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], dyz[0], dyz[1]);
594   ((TH3S*)arr->At(3))->Fill(fTracklet->GetZref(1), dyz[1], rc);
595
596
597
598   // register reference histo for mini-task
599   h = (TH2I*)arr->At(0);
600
601   if(DebugLevel()>=2){
602     (*DebugStream()) << "trackIn"
603       << "x="       << x
604       << "P="       << &PAR
605       << "C="       << &COV
606       << "\n";
607
608     Double_t y = fTracklet->GetY(); 
609     Double_t z = fTracklet->GetZ(); 
610     (*DebugStream()) << "trackletIn"
611       << "y="       << y
612       << "z="       << z
613       << "Vy="      << cov[0]
614       << "Cyz="     << cov[1]
615       << "Vz="      << cov[2]
616       << "\n";
617   }
618
619
620   if(!HasMCdata()) return h;
621   UChar_t s;
622   Float_t dx, pt0, x0=fTracklet->GetX0(), y0, z0, dydx0, dzdx0;
623   if(!fkMC->GetDirections(x0, y0, z0, dydx0, dzdx0, pt0, s)) return h;
624   Int_t pdg = fkMC->GetPDG(),
625         sIdx(AliTRDpidUtil::Pdg2Pid(TMath::Abs(pdg))+1), // species index
626         sign(0);
627   if(!fDBPDG) fDBPDG=TDatabasePDG::Instance();
628   TParticlePDG *ppdg(fDBPDG->GetParticle(pdg));
629   if(ppdg) sign = ppdg->Charge() > 0. ? 1 : -1;
630
631   // translate to reference radial position
632   dx = x0 - x; y0 -= dx*dydx0; z0 -= dx*dzdx0;
633   Float_t norm = 1./TMath::Sqrt(1.+dydx0*dydx0); // 1/sqrt(1+tg^2(phi))
634   //Fill MC info
635   TVectorD PARMC(kNPAR);
636   PARMC[0]=y0; PARMC[1]=z0;
637   PARMC[2]=dydx0*norm; PARMC[3]=dzdx0*norm;
638   PARMC[4]=1./pt0;
639
640 //   TMatrixDSymEigen eigen(COV);
641 //   TVectorD evals = eigen.GetEigenValues();
642 //   TMatrixDSym evalsm(kNPAR);
643 //   for(Int_t ir=0; ir<kNPAR; ir++) for(Int_t ic=0; ic<kNPAR; ic++) evalsm(ir,ic) = (ir==ic ? evals(ir): 0.);
644 //   TMatrixD evecs = eigen.GetEigenVectors();
645 //   TMatrixD sqrcov(evecs, TMatrixD::kMult, TMatrixD(evalsm, TMatrixD::kMult, evecs.T()));
646   
647   // fill histos
648   if(!(arr = (TObjArray*)fContainer->At(kMCtrackIn))) {
649     AliWarning("No MC container defined.");
650     return h;
651   }
652
653   // y resolution/pulls
654   if(pt0>fPtThreshold) ((TH3S*)arr->At(0))->Fill(dydx0, PARMC[0]-PAR[0], sgm[fSegmentLevel]);
655   ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], (PARMC[0]-PAR[0])/TMath::Sqrt(COV(0,0)), (PARMC[1]-PAR[1])/TMath::Sqrt(COV(1,1)));
656   // z resolution/pulls
657   ((TH3S*)arr->At(2))->Fill(dzdx0, PARMC[1]-PAR[1], 0);
658   ((TH3S*)arr->At(3))->Fill(dzdx0, (PARMC[1]-PAR[1])/TMath::Sqrt(COV(1,1)), 0);
659   // phi resolution/snp pulls
660   ((TH2I*)arr->At(4))->Fill(dydx0, TMath::ASin(PARMC[2])-TMath::ASin(PAR[2]));
661   ((TH2I*)arr->At(5))->Fill(dydx0, (PARMC[2]-PAR[2])/TMath::Sqrt(COV(2,2)));
662   // theta resolution/tgl pulls
663   ((TH2I*)arr->At(6))->Fill(dzdx0, TMath::ATan((PARMC[3]-PAR[3])/(1-PARMC[3]*PAR[3])));
664   ((TH2I*)arr->At(7))->Fill(dzdx0, (PARMC[3]-PAR[3])/TMath::Sqrt(COV(3,3)));
665   // pt resolution\\1/pt pulls\\p resolution/pull
666   ((TH3S*)arr->At(8))->Fill(pt0, PARMC[4]/PAR[4]-1., sign*sIdx);
667   ((TH3S*)arr->At(9))->Fill(PARMC[4], (PARMC[4]-PAR[4])/TMath::Sqrt(COV(4,4)), sign*sIdx);
668
669   Double_t p0 = TMath::Sqrt(1.+ PARMC[3]*PARMC[3])*pt0, p;
670   p = TMath::Sqrt(1.+ PAR[3]*PAR[3])/PAR[4];
671   ((TH3S*)arr->At(10))->Fill(p0, p/p0-1., sign*sIdx);
672 //   Float_t sp = 
673 //     p*p*PAR[4]*PAR[4]*COV(4,4)
674 //    +2.*PAR[3]*COV(3,4)/PAR[4]
675 //    +PAR[3]*PAR[3]*COV(3,3)/p/p/PAR[4]/PAR[4]/PAR[4]/PAR[4];
676 //   if(sp>0.) ((TH3S*)arr->At(11))->Fill(p0, (p0-p)/TMath::Sqrt(sp), sign*sIdx);
677
678   // fill debug for MC 
679   if(DebugLevel()>=3){
680     (*DebugStream()) << "trackInMC"
681       << "P="   << &PARMC
682       << "\n";
683   }
684   return h;
685 }
686
687 //________________________________________________________
688 TH1* AliTRDresolution::PlotTrackOut(const AliTRDtrackV1 *track)
689 {
690 // Store resolution/pulls of Kalman after last update with the TRD information 
691 // at the radial position of the first tracklet. The following points are used 
692 // for comparison  
693 //  - the (y,z,snp) of the first TRD tracklet
694 //  - the (y, z, snp, tgl, pt) of the MC track reference
695 // 
696 // Additionally the momentum resolution/pulls are calculated for usage in the 
697 // PID calculation. 
698
699   if(track) fkTrack = track;
700   if(!fkTrack){
701     AliDebug(4, "No Track defined.");
702     return NULL;
703   }
704   TObjArray *arr = NULL;
705   if(!fContainer || !(arr = (TObjArray*)fContainer->At(kTrackOut))){
706     AliWarning("No output container defined.");
707     return NULL;
708   }
709   AliExternalTrackParam *tout = NULL;
710   if(!(tout = fkTrack->GetTrackOut())){
711     AliWarning("Track did not exit TRD.");
712     return NULL;
713   }
714   TH1 *h(NULL);
715   
716   Double_t x = tout->GetX();
717   AliTRDseedV1 *fTracklet(NULL);  
718   for(Int_t ily=0; ily<AliTRDgeometry::kNlayer; ily++){
719     if(!(fTracklet = fkTrack->GetTracklet(ily))) continue;
720     break;
721   }
722   if(!fTracklet || TMath::Abs(x-fTracklet->GetX())>1.e-3){
723     AliWarning("Tracklet did not match Track position.");
724     return NULL;
725   }
726   Int_t sgm[3];
727   sgm[2] = fTracklet->GetDetector();
728   sgm[0] = AliTRDgeometry::GetSector(sgm[2]);
729   sgm[1] = sgm[0] * AliTRDgeometry::kNstack + AliTRDgeometry::GetStack(sgm[2]);
730   Double_t tilt(fTracklet->GetTilt())
731           ,t2(tilt*tilt)
732           ,corr(1./(1. + t2))
733           ,cost(TMath::Sqrt(corr));
734   Bool_t rc(fTracklet->IsRowCross());
735
736   const Int_t kNPAR(5);
737   Double_t parR[kNPAR]; memcpy(parR, tout->GetParameter(), kNPAR*sizeof(Double_t));
738   Double_t covR[3*kNPAR]; memcpy(covR, tout->GetCovariance(), 3*kNPAR*sizeof(Double_t));
739   Double_t cov[3]; fTracklet->GetCovAt(x, cov);
740
741   // define sum covariances
742   TMatrixDSym COV(kNPAR); TVectorD PAR(kNPAR);
743   Double_t *pc = &covR[0], *pp = &parR[0];
744   for(Int_t ir=0; ir<kNPAR; ir++, pp++){
745     PAR(ir) = (*pp);
746     for(Int_t ic = 0; ic<=ir; ic++,pc++){ 
747       COV(ir,ic) = (*pc); COV(ic,ir) = (*pc);
748     }
749   }
750   PAR[4] = TMath::Abs(PAR[4]); // remove sign of pt !!
751   //COV.Print(); PAR.Print();
752
753   //TODO Double_t dydx =  TMath::Sqrt(1.-parR[2]*parR[2])/parR[2]; 
754   Double_t dy[3]={parR[0] - fTracklet->GetY(), 0., 0.}
755           ,dz[3]={parR[1] - fTracklet->GetZ(), 0., 0.}
756           ,dphi(TMath::ASin(PAR[2])-TMath::ATan(fTracklet->GetYfit(1)));
757   // calculate residuals using tilt rotation
758   dy[1] = cost*(dy[0] - dz[0]*tilt);
759   dz[1] = cost*(dz[0] + dy[0]*tilt);
760
761   if(1./PAR[4]>fPtThreshold) ((TH3S*)arr->At(0))->Fill(fTracklet->GetYref(1), 1.e2*dy[1], sgm[fSegmentLevel]+rc*fgkNresYsegm[fSegmentLevel]); // scale to fit general residual range !!!
762   ((TH3S*)arr->At(2))->Fill(fTracklet->GetZref(1), dz[1], rc);
763   ((TH2I*)arr->At(4))->Fill(fTracklet->GetYref(1), dphi);
764
765   Double_t dyz[2] = {dy[1], dz[1]};
766   Double_t cc[3] = {COV(0,0)+cov[0], COV(0,1)+cov[1], COV(1,1)+cov[2]};
767   Pulls(dyz, cc, tilt);
768   ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], dyz[0], dyz[1]);
769   ((TH3S*)arr->At(3))->Fill(fTracklet->GetZref(1), dyz[1], rc);
770
771   // register reference histo for mini-task
772   h = (TH2I*)arr->At(0);
773
774   if(DebugLevel()>=2){
775     (*DebugStream()) << "trackOut"
776       << "x="       << x
777       << "P="       << &PAR
778       << "C="       << &COV
779       << "\n";
780
781     Double_t y = fTracklet->GetY(); 
782     Double_t z = fTracklet->GetZ(); 
783     (*DebugStream()) << "trackletOut"
784       << "y="       << y
785       << "z="       << z
786       << "Vy="      << cov[0]
787       << "Cyz="     << cov[1]
788       << "Vz="      << cov[2]
789       << "\n";
790   }
791
792
793   if(!HasMCdata()) return h;
794   UChar_t s;
795   Float_t dx, pt0, x0=fTracklet->GetX0(), y0, z0, dydx0, dzdx0;
796   if(!fkMC->GetDirections(x0, y0, z0, dydx0, dzdx0, pt0, s)) return h;
797   Int_t pdg = fkMC->GetPDG(),
798         sIdx(AliTRDpidUtil::Pdg2Pid(TMath::Abs(pdg))+1), // species index
799         sign(0);
800   if(!fDBPDG) fDBPDG=TDatabasePDG::Instance();
801   TParticlePDG *ppdg(fDBPDG->GetParticle(pdg));
802   if(ppdg) sign = ppdg->Charge() > 0. ? 1 : -1;
803
804   // translate to reference radial position
805   dx = x0 - x; y0 -= dx*dydx0; z0 -= dx*dzdx0;
806   Float_t norm = 1./TMath::Sqrt(1.+dydx0*dydx0); // 1/sqrt(1+tg^2(phi))
807   //Fill MC info
808   TVectorD PARMC(kNPAR);
809   PARMC[0]=y0; PARMC[1]=z0;
810   PARMC[2]=dydx0*norm; PARMC[3]=dzdx0*norm;
811   PARMC[4]=1./pt0;
812
813 //   TMatrixDSymEigen eigen(COV);
814 //   TVectorD evals = eigen.GetEigenValues();
815 //   TMatrixDSym evalsm(kNPAR);
816 //   for(Int_t ir=0; ir<kNPAR; ir++) for(Int_t ic=0; ic<kNPAR; ic++) evalsm(ir,ic) = (ir==ic ? evals(ir): 0.);
817 //   TMatrixD evecs = eigen.GetEigenVectors();
818 //   TMatrixD sqrcov(evecs, TMatrixD::kMult, TMatrixD(evalsm, TMatrixD::kMult, evecs.T()));
819   
820   // fill histos
821   if(!(arr = (TObjArray*)fContainer->At(kMCtrackOut))){
822     AliWarning("No MC container defined.");
823     return h;
824   }
825   // y resolution/pulls
826   if(pt0>fPtThreshold) ((TH3S*)arr->At(0))->Fill(dydx0, PARMC[0]-PAR[0], sgm[fSegmentLevel]);
827   ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], (PARMC[0]-PAR[0])/TMath::Sqrt(COV(0,0)), (PARMC[1]-PAR[1])/TMath::Sqrt(COV(1,1)));
828   // z resolution/pulls
829   ((TH3S*)arr->At(2))->Fill(dzdx0, PARMC[1]-PAR[1], 0);
830   ((TH3S*)arr->At(3))->Fill(dzdx0, (PARMC[1]-PAR[1])/TMath::Sqrt(COV(1,1)), 0);
831   // phi resolution/snp pulls
832   ((TH2I*)arr->At(4))->Fill(dydx0, TMath::ASin(PARMC[2])-TMath::ASin(PAR[2]));
833   ((TH2I*)arr->At(5))->Fill(dydx0, (PARMC[2]-PAR[2])/TMath::Sqrt(COV(2,2)));
834   // theta resolution/tgl pulls
835   ((TH2I*)arr->At(6))->Fill(dzdx0, TMath::ATan((PARMC[3]-PAR[3])/(1-PARMC[3]*PAR[3])));
836   ((TH2I*)arr->At(7))->Fill(dzdx0, (PARMC[3]-PAR[3])/TMath::Sqrt(COV(3,3)));
837   // pt resolution\\1/pt pulls\\p resolution/pull
838   ((TH3S*)arr->At(8))->Fill(pt0, PARMC[4]/PAR[4]-1., sign*sIdx);
839   ((TH3S*)arr->At(9))->Fill(PARMC[4], (PARMC[4]-PAR[4])/TMath::Sqrt(COV(4,4)), sign*sIdx);
840
841   Double_t p0 = TMath::Sqrt(1.+ PARMC[3]*PARMC[3])*pt0, p;
842   p = TMath::Sqrt(1.+ PAR[3]*PAR[3])/PAR[4];
843   ((TH3S*)arr->At(10))->Fill(p0, p/p0-1., sign*sIdx);
844 //   Float_t sp = 
845 //     p*p*PAR[4]*PAR[4]*COV(4,4)
846 //    +2.*PAR[3]*COV(3,4)/PAR[4]
847 //    +PAR[3]*PAR[3]*COV(3,3)/p/p/PAR[4]/PAR[4]/PAR[4]/PAR[4];
848 //   if(sp>0.) ((TH3S*)arr->At(11))->Fill(p0, (p0-p)/TMath::Sqrt(sp), sign*sIdx);
849
850   // fill debug for MC 
851   if(DebugLevel()>=3){
852     (*DebugStream()) << "trackOutMC"
853       << "P="   << &PARMC
854       << "\n";
855   }
856   return h;
857 }
858
859 //________________________________________________________
860 TH1* AliTRDresolution::PlotMC(const AliTRDtrackV1 *track)
861 {
862   //
863   // Plot MC distributions
864   //
865
866   if(!HasMCdata()){ 
867     AliWarning("No MC defined. Results will not be available.");
868     return NULL;
869   }
870   if(track) fkTrack = track;
871   if(!fkTrack){
872     AliDebug(4, "No Track defined.");
873     return NULL;
874   }
875   if(!fContainer){
876     AliWarning("No output container defined.");
877     return NULL;
878   }
879   // retriev track characteristics
880   Int_t pdg = fkMC->GetPDG(),
881         sIdx(AliTRDpidUtil::Pdg2Pid(TMath::Abs(pdg))+1), // species index
882         sign(0),
883         sgm[3],
884         label(fkMC->GetLabel());
885   if(!fDBPDG) fDBPDG=TDatabasePDG::Instance();
886   TParticlePDG *ppdg(fDBPDG->GetParticle(pdg));
887   if(ppdg) sign = ppdg->Charge() > 0. ? 1 : -1;
888
889   TObjArray *arr(NULL);TH1 *h(NULL);
890   UChar_t s;
891   Double_t xAnode, x, y, z, pt, dydx, dzdx, dzdl;
892   Float_t pt0, x0, y0, z0, dx, dy, dz, dydx0, dzdx0;
893   Double_t covR[7]/*, cov[3]*/;
894
895   if(DebugLevel()>=3){
896     TVectorD dX(12), dY(12), dZ(12), Pt(12), dPt(12), cCOV(12*15);
897     fkMC->PropagateKalman(&dX, &dY, &dZ, &Pt, &dPt, &cCOV);
898     (*DebugStream()) << "MCkalman"
899       << "pdg=" << pdg
900       << "dx="  << &dX
901       << "dy="  << &dY
902       << "dz="  << &dZ
903       << "pt="  << &Pt
904       << "dpt=" << &dPt
905       << "cov=" << &cCOV
906       << "\n";
907   }
908
909   AliTRDReconstructor rec;
910   AliTRDseedV1 *fTracklet(NULL); TObjArray *clInfoArr(NULL);
911   for(Int_t ily=0; ily<AliTRDgeometry::kNlayer; ily++){
912     if(!(fTracklet = fkTrack->GetTracklet(ily)))/* ||
913        !fTracklet->IsOK())*/ continue;
914
915     sgm[2] = fTracklet->GetDetector();
916     sgm[0] = AliTRDgeometry::GetSector(sgm[2]);
917     sgm[1] = sgm[0] * AliTRDgeometry::kNstack + AliTRDgeometry::GetStack(sgm[2]);
918     Double_t tilt(fTracklet->GetTilt())
919             ,t2(tilt*tilt)
920             ,corr(1./(1. + t2))
921             ,cost(TMath::Sqrt(corr));
922     x0  = fTracklet->GetX0();
923     //radial shift with respect to the MC reference (radial position of the pad plane)
924     x= fTracklet->GetX();
925     Bool_t rc(fTracklet->IsRowCross());
926     if(!fkMC->GetDirections(x0, y0, z0, dydx0, dzdx0, pt0, s)) continue;
927     xAnode  = fTracklet->GetX0();
928
929     // MC track position at reference radial position
930     dx  = x0 - x;
931     if(DebugLevel()>=4){
932       (*DebugStream()) << "MC"
933         << "det="     << sgm[2]
934         << "pdg="     << pdg
935         << "sgn="     << sign
936         << "pt="      << pt0
937         << "x="       << x0
938         << "y="       << y0
939         << "z="       << z0
940         << "dydx="    << dydx0
941         << "dzdx="    << dzdx0
942         << "\n";
943     }
944     Float_t ymc = y0 - dx*dydx0;
945     Float_t zmc = z0 - dx*dzdx0;
946     //p = pt0*TMath::Sqrt(1.+dzdx0*dzdx0); // pt -> p
947
948     // Kalman position at reference radial position
949     dx = xAnode - x;
950     dydx = fTracklet->GetYref(1);
951     dzdx = fTracklet->GetZref(1);
952     dzdl = fTracklet->GetTgl();
953     y  = fTracklet->GetYref(0) - dx*dydx;
954     dy = y - ymc;
955     z  = fTracklet->GetZref(0) - dx*dzdx;
956     dz = z - zmc;
957     pt = TMath::Abs(fTracklet->GetPt());
958     fTracklet->GetCovRef(covR);
959
960     arr = (TObjArray*)((TObjArray*)fContainer->At(kMCtrack))->At(ily);
961     // y resolution/pulls
962     if(pt0>fPtThreshold) ((TH3S*)arr->At(0))->Fill(dydx0, dy, sgm[fSegmentLevel]);
963     ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], dy/TMath::Sqrt(covR[0]), dz/TMath::Sqrt(covR[2]));
964     // z resolution/pulls
965     ((TH3S*)arr->At(2))->Fill(dzdx0, dz, 0);
966     ((TH3S*)arr->At(3))->Fill(dzdx0, dz/TMath::Sqrt(covR[2]), 0);
967     // phi resolution/ snp pulls
968     Double_t dtgp = (dydx - dydx0)/(1.- dydx*dydx0);
969     ((TH2I*)arr->At(4))->Fill(dydx0, TMath::ATan(dtgp));
970     Double_t dsnp = dydx/TMath::Sqrt(1.+dydx*dydx) - dydx0/TMath::Sqrt(1.+dydx0*dydx0);
971     ((TH2I*)arr->At(5))->Fill(dydx0, dsnp/TMath::Sqrt(covR[3]));
972     // theta resolution/ tgl pulls
973     Double_t dzdl0 = dzdx0/TMath::Sqrt(1.+dydx0*dydx0),
974               dtgl = (dzdl - dzdl0)/(1.- dzdl*dzdl0);
975     ((TH2I*)arr->At(6))->Fill(dzdl0, 
976     TMath::ATan(dtgl));
977     ((TH2I*)arr->At(7))->Fill(dzdl0, (dzdl - dzdl0)/TMath::Sqrt(covR[4]));
978     // pt resolution  \\ 1/pt pulls \\ p resolution for PID
979     Double_t p0 = TMath::Sqrt(1.+ dzdl0*dzdl0)*pt0,
980              p  = TMath::Sqrt(1.+ dzdl*dzdl)*pt;
981     ((TH3S*)((TObjArray*)arr->At(8)))->Fill(pt0, pt/pt0-1., sign*sIdx);
982     ((TH3S*)((TObjArray*)arr->At(9)))->Fill(1./pt0, (1./pt-1./pt0)/TMath::Sqrt(covR[6]), sign*sIdx);
983     ((TH3S*)((TObjArray*)arr->At(10)))->Fill(p0, p/p0-1., sign*sIdx);
984
985     // Fill Debug stream for Kalman track
986     if(DebugLevel()>=4){
987       (*DebugStream()) << "MCtrack"
988         << "pt="      << pt
989         << "x="       << x
990         << "y="       << y
991         << "z="       << z
992         << "dydx="    << dydx
993         << "dzdx="    << dzdx
994         << "s2y="     << covR[0]
995         << "s2z="     << covR[2]
996         << "\n";
997     }
998
999     // recalculate tracklet based on the MC info
1000     AliTRDseedV1 tt(*fTracklet);
1001     tt.SetZref(0, z0 - (x0-xAnode)*dzdx0);
1002     tt.SetZref(1, dzdx0); 
1003     tt.SetReconstructor(&rec);
1004     tt.Fit(1);
1005     x= tt.GetX();y= tt.GetY();z= tt.GetZ();
1006     dydx = tt.GetYfit(1);
1007     dx = x0 - x;
1008     ymc = y0 - dx*dydx0;
1009     zmc = z0 - dx*dzdx0;
1010     dy = y-ymc;
1011     dz = z-zmc;
1012     Float_t dphi  = (dydx - dydx0);
1013     dphi /= (1.- dydx*dydx0);
1014
1015     // add tracklet residuals for y and dydx
1016     arr = (TObjArray*)fContainer->At(kMCtracklet);
1017
1018     if(pt0>fPtThreshold) ((TH3S*)arr->At(0))->Fill(dydx0, dy, sgm[fSegmentLevel]);
1019     if(tt.GetS2Y()>0. && tt.GetS2Z()>0.) ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], dy/TMath::Sqrt(tt.GetS2Y()), dz/TMath::Sqrt(tt.GetS2Z()));
1020     ((TH3S*)arr->At(2))->Fill(dzdl0, dz, rc);
1021     if(tt.GetS2Z()>0.) ((TH3S*)arr->At(3))->Fill(dzdl0, dz/TMath::Sqrt(tt.GetS2Z()), rc);
1022     ((TH2I*)arr->At(4))->Fill(dydx0, TMath::ATan(dphi));
1023   
1024     // Fill Debug stream for tracklet
1025     if(DebugLevel()>=4){
1026       Float_t s2y = tt.GetS2Y();
1027       Float_t s2z = tt.GetS2Z();
1028       (*DebugStream()) << "MCtracklet"
1029         << "rc="    << rc
1030         << "x="     << x
1031         << "y="     << y
1032         << "z="     << z
1033         << "dydx="  << dydx
1034         << "s2y="   << s2y
1035         << "s2z="   << s2z
1036         << "\n";
1037     }
1038
1039     AliTRDpadPlane *pp = fGeo->GetPadPlane(ily, AliTRDgeometry::GetStack(sgm[2]));
1040     Float_t zr0 = pp->GetRow0() + pp->GetAnodeWireOffset();
1041     //Double_t exb = AliTRDCommonParam::Instance()->GetOmegaTau(1.5);
1042
1043     arr = (TObjArray*)fContainer->At(kMCcluster);
1044     AliTRDcluster *c = NULL;
1045     tt.ResetClusterIter(kFALSE);
1046     while((c = tt.PrevCluster())){
1047       Float_t  q = TMath::Abs(c->GetQ());
1048       x = c->GetX(); y = c->GetY();z = c->GetZ();
1049       dx = x0 - x; 
1050       ymc= y0 - dx*dydx0;
1051       zmc= z0 - dx*dzdx0;
1052       dy = cost*(y - ymc - tilt*(z-zmc));
1053       dz = cost*(z - zmc + tilt*(y-ymc));
1054       
1055       // Fill Histograms
1056       if(q>20. && q<250. && pt0>fPtThreshold && c->IsInChamber()){ 
1057         ((TH3S*)arr->At(0))->Fill(dydx0, dy, sgm[fSegmentLevel]);
1058         ((TH3S*)arr->At(1))->Fill(sgm[fSegmentLevel], dy/TMath::Sqrt(c->GetSigmaY2()), dz/TMath::Sqrt(c->GetSigmaZ2()));
1059       }
1060
1061       // Fill calibration container
1062       Float_t d = zr0 - zmc;
1063       d -= ((Int_t)(2 * d)) / 2.0;
1064       if (d > 0.25) d  = 0.5 - d;
1065       AliTRDclusterInfo *clInfo = new AliTRDclusterInfo;
1066       clInfo->SetCluster(c);
1067       clInfo->SetMC(pdg, label);
1068       clInfo->SetGlobalPosition(ymc, zmc, dydx0, dzdx0);
1069       clInfo->SetResolution(dy);
1070       clInfo->SetAnisochronity(d);
1071       clInfo->SetDriftLength(dx);
1072       clInfo->SetTilt(tilt);
1073       if(fMCcl) fMCcl->Add(clInfo);
1074       else AliDebug(1, "MCcl exchange container missing. Activate by calling \"InitExchangeContainers()\"");
1075       if(DebugLevel()>=5){ 
1076         if(!clInfoArr){ 
1077           clInfoArr=new TObjArray(AliTRDseedV1::kNclusters);
1078           clInfoArr->SetOwner(kFALSE);
1079         }
1080         clInfoArr->Add(clInfo);
1081       }
1082     }
1083     // Fill Debug Tree
1084     if(DebugLevel()>=5 && clInfoArr){
1085       (*DebugStream()) << "MCcluster"
1086         <<"clInfo.=" << clInfoArr
1087         << "\n";
1088       clInfoArr->Clear();
1089     }
1090   }
1091   if(clInfoArr) delete clInfoArr;
1092   return h;
1093 }
1094
1095
1096 //________________________________________________________
1097 Bool_t AliTRDresolution::GetRefFigure(Int_t ifig)
1098 {
1099   //
1100   // Get the reference figures
1101   //
1102
1103   Float_t xy[4] = {0., 0., 0., 0.};
1104   if(!gPad){
1105     AliWarning("Please provide a canvas to draw results.");
1106     return kFALSE;
1107   }
1108   Int_t selection[100], n(0), selStart(0); // 
1109   Int_t ly0(0), dly(5);
1110   //Int_t ly0(1), dly(2); // used for SA
1111   TList *l(NULL); TVirtualPad *pad(NULL); 
1112   TGraphErrors *g(NULL);TGraphAsymmErrors *ga(NULL);
1113   switch(ifig){
1114   case 0: // charge resolution
1115     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1116     ((TVirtualPad*)l->At(0))->cd();
1117     ga=((TGraphAsymmErrors*)((TObjArray*)fGraphM->At(kCharge))->At(0));
1118     if(ga->GetN()) ga->Draw("apl");
1119     ((TVirtualPad*)l->At(1))->cd();
1120     g = ((TGraphErrors*)((TObjArray*)fGraphS->At(kCharge))->At(0));
1121     if(g->GetN()) g->Draw("apl");
1122     break;
1123   case 1: // cluster2track residuals
1124     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1125     xy[0] = -.3; xy[1] = -100.; xy[2] = .3; xy[3] = 1000.;
1126     pad = (TVirtualPad*)l->At(0); pad->cd();
1127     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1128     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1129     if(!GetGraphArray(xy, kCluster, 0, 1, n, selection)) break;
1130     pad=(TVirtualPad*)l->At(1); pad->cd();
1131     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1132     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1133     if(!GetGraphArray(xy, kCluster, 0, 1, n, selection)) break;
1134     return kTRUE;
1135   case 2: // cluster2track residuals
1136     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1137     xy[0] = -.3; xy[1] = -100.; xy[2] = .3; xy[3] = 1000.;
1138     pad = (TVirtualPad*)l->At(0); pad->cd();
1139     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1140     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1141     if(!GetGraphArray(xy, kCluster, 0, 1, n, selection)) break;
1142     xy[0] = -.5; xy[1] = -0.5; xy[2] = fgkNresYsegm[fSegmentLevel]-0.5; xy[3] = 2.5;
1143     pad=(TVirtualPad*)l->At(1); pad->cd();
1144     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1145     if(!GetGraphArray(xy, kCluster, 1, 1)) break;
1146     return kTRUE;
1147   case 3: // kTrack y
1148     gPad->Divide(3, 2, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1149     xy[0] = -.3; xy[1] = -20.; xy[2] = .3; xy[3] = 100.;
1150     ((TVirtualPad*)l->At(0))->cd();
1151     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1152     if(!GetGraphArray(xy, kTrack, 0, 1, n, selection)) break;
1153
1154     ((TVirtualPad*)l->At(1))->cd();
1155     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1156     if(!GetGraphArray(xy, kTrack, 0, 1, n, selection)) break;
1157
1158     ((TVirtualPad*)l->At(2))->cd();
1159     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1160     if(!GetGraphArray(xy, kTrack, 0, 1, n, selection)) break;
1161
1162     ((TVirtualPad*)l->At(3))->cd();
1163     selStart=fgkNresYsegm[fSegmentLevel]; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1164     if(!GetGraphArray(xy, kTrack, 0, 1, n, selection, "[RC]")) break;
1165
1166     ((TVirtualPad*)l->At(4))->cd();
1167     selStart=fgkNresYsegm[fSegmentLevel]/3+fgkNresYsegm[fSegmentLevel]; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1168     if(!GetGraphArray(xy, kTrack, 0, 1, n, selection, "[RC]")) break;
1169
1170     ((TVirtualPad*)l->At(5))->cd();
1171     selStart=2*fgkNresYsegm[fSegmentLevel]/3+fgkNresYsegm[fSegmentLevel]; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1172     if(!GetGraphArray(xy, kTrack, 0, 1, n, selection, "[RC]")) break;
1173     return kTRUE;
1174   case 4: // kTrack z
1175     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1176
1177     xy[0] = -1.; xy[1] = -150.; xy[2] = 1.; xy[3] = 1000.;
1178     ((TVirtualPad*)l->At(0))->cd();
1179     selection[0]=1;
1180     if(!GetGraphArray(xy, kTrack, 2, 1, 1, selection)) break;
1181
1182     xy[0] = -1.; xy[1] = -1500.; xy[2] = 1.; xy[3] = 10000.;
1183     ((TVirtualPad*)l->At(1))->cd();
1184     selection[0]=0;
1185     if(!GetGraphArray(xy, kTrack, 2, 1, 1, selection)) break;
1186
1187     return kTRUE;
1188   case 5: // kTrack  pulls
1189     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1190
1191     xy[0] = -.5; xy[1] = -0.5; xy[2] = fgkNresYsegm[fSegmentLevel]-.5; xy[3] = 2.5;
1192     ((TVirtualPad*)l->At(0))->cd();
1193     if(!GetGraphArray(xy, kTrack, 1, 1)) break;
1194
1195     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 2.5;
1196     ((TVirtualPad*)l->At(1))->cd();
1197     if(!GetGraphArray(xy, kTrack, 3, 1)) break;
1198     return kTRUE;
1199   case 6: // kTrack  phi
1200     xy[0] = -.3; xy[1] = -5.; xy[2] = .3; xy[3] = 50.;
1201     if(GetGraph(&xy[0], kTrack , 4)) return kTRUE;
1202     break;
1203   case 7: // kTrackIn y
1204     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1205     xy[0] = -.3; xy[1] = -1500.; xy[2] = .3; xy[3] = 5000.;
1206     pad = ((TVirtualPad*)l->At(0)); pad->cd();
1207     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1208     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1209     if(!GetGraphArray(xy, kTrackIn, 0, 1, n, selection)) break;
1210     pad=((TVirtualPad*)l->At(1)); pad->cd();
1211     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1212     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1213     if(!GetGraphArray(xy, kTrackIn, 0, 1, n, selection)) break;
1214     return kTRUE;
1215   case 8: // kTrackIn y
1216     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1217     xy[0] = -.3; xy[1] = -1500.; xy[2] = .3; xy[3] = 5000.;
1218     pad = ((TVirtualPad*)l->At(0)); pad->cd();
1219     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1220     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1221     if(!GetGraphArray(xy, kTrackIn, 0, 1, n, selection)) break;
1222     xy[0] = -.5; xy[1] = -0.5; xy[2] = fgkNresYsegm[fSegmentLevel]-.5; xy[3] = 2.5;
1223     pad=((TVirtualPad*)l->At(1)); pad->cd();
1224     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1225     if(!GetGraphArray(xy, kTrackIn, 1, 1)) break;
1226     return kTRUE;
1227   case 9: // kTrackIn z
1228     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1229     xy[0] = -1.; xy[1] = -1000.; xy[2] = 1.; xy[3] = 4000.;
1230     pad = ((TVirtualPad*)l->At(0)); pad->cd();
1231     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1232     selection[0]=1;
1233     if(!GetGraphArray(xy, kTrackIn, 2, 1, 1, selection)) break;
1234     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 2.5;
1235     pad = ((TVirtualPad*)l->At(1)); pad->cd();
1236     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1237     if(!GetGraphArray(xy, kTrackIn, 3, 1)) break;
1238     return kTRUE;
1239   case 10: // kTrackIn phi
1240     xy[0] = -.3; xy[1] = -5.; xy[2] = .3; xy[3] = 50.;
1241     if(GetGraph(&xy[0], kTrackIn, 4)) return kTRUE;
1242     break;
1243   case 11: // kTrackOut y
1244     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1245     xy[0] = -.3; xy[1] = -50.; xy[2] = .3; xy[3] = 150.;
1246     pad = ((TVirtualPad*)l->At(0)); pad->cd();
1247     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1248     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1249     if(!GetGraphArray(xy, kTrackOut, 0, 1, n, selection)) break;
1250     pad=((TVirtualPad*)l->At(1)); pad->cd();
1251     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1252     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1253     if(!GetGraphArray(xy, kTrackOut, 0, 1, n, selection)) break;
1254     return kTRUE;
1255   case 12: // kTrackOut y
1256     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1257     xy[0] = -.3; xy[1] = -50.; xy[2] = .3; xy[3] = 150.;
1258     pad = ((TVirtualPad*)l->At(0)); pad->cd();
1259     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1260     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1261     if(!GetGraphArray(xy, kTrackOut, 0, 1, n, selection)) break;
1262     xy[0] = -.5; xy[1] = -0.5; xy[2] = fgkNresYsegm[fSegmentLevel]-.5; xy[3] = 2.5;
1263     pad=((TVirtualPad*)l->At(1)); pad->cd();
1264     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1265     if(!GetGraphArray(xy, kTrackOut, 1, 1)) break;
1266     return kTRUE;
1267   case 13: // kTrackOut z
1268     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1269     xy[0] = -1.; xy[1] = -1000.; xy[2] = 1.; xy[3] = 4000.;
1270     pad = ((TVirtualPad*)l->At(0)); pad->cd();
1271     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1272     if(!GetGraphArray(xy, kTrackOut, 2, 1)) break;
1273     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 2.5;
1274     pad = ((TVirtualPad*)l->At(1)); pad->cd();
1275     pad->SetMargin(0.1, 0.1, 0.1, 0.01);
1276     if(!GetGraphArray(xy, kTrackOut, 3, 1)) break;
1277     return kTRUE;
1278   case 14: // kTrackOut phi
1279     xy[0] = -.3; xy[1] = -5.; xy[2] = .3; xy[3] = 50.;
1280     if(GetGraph(&xy[0], kTrackOut, 4)) return kTRUE;
1281     break;
1282   case 15: // kMCcluster
1283     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1284     xy[0]=-.3; xy[1]=-50.; xy[2]=.3; xy[3]=650.;
1285     ((TVirtualPad*)l->At(0))->cd();
1286     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1287     if(!GetGraphArray(xy, kMCcluster, 0, 1, n, selection)) break;
1288     ((TVirtualPad*)l->At(1))->cd();
1289     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1290     if(!GetGraphArray(xy, kMCcluster, 0, 1, n, selection)) break;
1291     return kTRUE;
1292   case 16: // kMCcluster
1293     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1294     xy[0]=-.3; xy[1]=-50.; xy[2]=.3; xy[3]=650.;
1295     ((TVirtualPad*)l->At(0))->cd();
1296     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1297     if(!GetGraphArray(xy, kMCcluster, 0, 1, n, selection)) break;
1298     ((TVirtualPad*)l->At(1))->cd();
1299     xy[0]=-.5; xy[1]=-0.5; xy[2]=fgkNresYsegm[fSegmentLevel]-.5; xy[3]=2.5;
1300     if(!GetGraphArray(xy, kMCcluster, 1, 1)) break;
1301     return kTRUE;
1302   case 17: //kMCtracklet [y]
1303     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1304     xy[0]=-.3; xy[1]=-50.; xy[2]=.3; xy[3] =500.;
1305     ((TVirtualPad*)l->At(0))->cd();
1306     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1307     if(!GetGraphArray(xy, kMCtracklet, 0, 1, n, selection)) break;
1308     ((TVirtualPad*)l->At(1))->cd();
1309     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1310     if(!GetGraphArray(xy, kMCtracklet, 0, 1, n, selection)) break;
1311     return kTRUE;
1312   case 18: //kMCtracklet [y]
1313     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1314     xy[0]=-.3; xy[1]=-50.; xy[2]=.3; xy[3] =500.;
1315     ((TVirtualPad*)l->At(0))->cd();
1316     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1317     if(!GetGraphArray(xy, kMCtracklet, 0, 1, n, selection)) break;
1318     ((TVirtualPad*)l->At(1))->cd();
1319     xy[0]=-.5; xy[1]=-0.5; xy[2]=fgkNresYsegm[fSegmentLevel]-.5; xy[3]=2.5;
1320     if(!GetGraphArray(xy, kMCtracklet, 1, 1)) break;
1321     return kTRUE;
1322   case 19: //kMCtracklet [z]
1323     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1324     xy[0]=-1.; xy[1]=-100.; xy[2]=1.; xy[3] =2500.;
1325     ((TVirtualPad*)l->At(0))->cd();
1326     if(!GetGraphArray(xy, kMCtracklet, 2)) break;
1327     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 2.5;
1328     ((TVirtualPad*)l->At(1))->cd();
1329     if(!GetGraphArray(xy, kMCtracklet, 3)) break;
1330     return kTRUE;
1331   case 20: //kMCtracklet [phi]
1332     xy[0]=-.3; xy[1]=-3.; xy[2]=.3; xy[3] =25.;
1333     if(!GetGraph(&xy[0], kMCtracklet, 4)) break;
1334     return kTRUE;
1335   case 21: //kMCtrack [y] ly [0]
1336     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1337     xy[0]=-.2; xy[1]=-50.; xy[2]=.2; xy[3] =400.;
1338     ((TVirtualPad*)l->At(0))->cd();
1339     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*0.); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1340     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer1")) break;
1341     ((TVirtualPad*)l->At(1))->cd();
1342     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*0.5); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1343     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer1")) break;
1344     return kTRUE;
1345   case 22: //kMCtrack [y] ly [1]
1346     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1347     xy[0]=-.2; xy[1]=-50.; xy[2]=.2; xy[3] =400.;
1348     ((TVirtualPad*)l->At(0))->cd();
1349     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*1.); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1350     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer2")) break;
1351     ((TVirtualPad*)l->At(1))->cd();
1352     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*1.5); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1353     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer2")) break;
1354     return kTRUE;
1355   case 23: //kMCtrack [y] ly [2]
1356     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1357     xy[0]=-.2; xy[1]=-50.; xy[2]=.2; xy[3] =400.;
1358     ((TVirtualPad*)l->At(0))->cd();
1359     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*2.); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1360     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer3")) break;
1361     ((TVirtualPad*)l->At(1))->cd();
1362     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*2.5); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1363     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer3")) break;
1364     return kTRUE;
1365   case 24: //kMCtrack [y] ly [3]
1366     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1367     xy[0]=-.2; xy[1]=-50.; xy[2]=.2; xy[3] =400.;
1368     ((TVirtualPad*)l->At(0))->cd();
1369     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*3.); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1370     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer4")) break;
1371     ((TVirtualPad*)l->At(1))->cd();
1372     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*3.5); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1373     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer4")) break;
1374     return kTRUE;
1375   case 25: //kMCtrack [y] ly [4]
1376     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1377     xy[0]=-.2; xy[1]=-50.; xy[2]=.2; xy[3] =400.;
1378     ((TVirtualPad*)l->At(0))->cd();
1379     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*4.); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1380     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer5")) break;
1381     ((TVirtualPad*)l->At(1))->cd();
1382     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*4.5); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1383     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer5")) break;
1384     return kTRUE;
1385   case 26: //kMCtrack [y] ly [5]
1386     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1387     xy[0]=-.2; xy[1]=-50.; xy[2]=.2; xy[3] =400.;
1388     ((TVirtualPad*)l->At(0))->cd();
1389     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*5.); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1390     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer6")) break;
1391     ((TVirtualPad*)l->At(1))->cd();
1392     selStart=Int_t(fgkNresYsegm[fSegmentLevel]*5.5); for(n=0; n<fgkNresYsegm[fSegmentLevel]/2; n++) selection[n]=selStart+n;
1393     if(!GetGraphArray(xy, kMCtrack, 0, 1, n, selection, "Layer6")) break;
1394     return kTRUE;
1395   case 27: //kMCtrack [y pulls] 
1396     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1397     xy[0] = -.5; xy[1] = -0.5; xy[2] = fgkNresYsegm[fSegmentLevel]-.5; xy[3] = 5.5;
1398     ((TVirtualPad*)l->At(0))->cd();
1399     selStart=0; for(n=0; n<6; n++) selection[n]=selStart+n;
1400     if(!GetGraphArray(xy, kMCtrack, 1, 1, n, selection)) break;
1401     ((TVirtualPad*)l->At(1))->cd();
1402     selStart=6; for(n=0; n<6; n++) selection[n]=selStart+n;
1403     if(!GetGraphArray(xy, kMCtrack, 1, 1, n, selection)) break;
1404     return kTRUE;
1405   case 28: //kMCtrack [z]
1406     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1407     xy[0]=-1.; xy[1]=-1500.; xy[2]=1.; xy[3] =6000.;
1408     ((TVirtualPad*)l->At(0))->cd();
1409     if(!GetGraphArray(xy, kMCtrack, 2)) break;
1410     xy[0] = -1.; xy[1] = -1.5; xy[2] = 1.; xy[3] = 5.;
1411     ((TVirtualPad*)l->At(1))->cd();
1412     if(!GetGraphArray(xy, kMCtrack, 3)) break;
1413     return kTRUE;
1414   case 29: //kMCtrack [phi/snp]
1415     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1416     xy[0]=-.2; xy[1]=-0.5; xy[2]=.2; xy[3] =10.;
1417     ((TVirtualPad*)l->At(0))->cd();
1418     if(!GetGraphArray(xy, kMCtrack, 4)) break;
1419     xy[0] = -.2; xy[1] = -1.5; xy[2] = .2; xy[3] = 5.;
1420     ((TVirtualPad*)l->At(1))->cd();
1421     if(!GetGraphArray(xy, kMCtrack, 5)) break;
1422     return kTRUE;
1423   case 30: //kMCtrack [theta/tgl]
1424     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1425     xy[0]=-1.; xy[1]=-0.5; xy[2]=1.; xy[3] =5.;
1426     ((TVirtualPad*)l->At(0))->cd();
1427     if(!GetGraphArray(xy, kMCtrack, 6)) break;
1428     xy[0] = -.2; xy[1] = -0.5; xy[2] = .2; xy[3] = 2.5;
1429     ((TVirtualPad*)l->At(1))->cd();
1430     if(!GetGraphArray(xy, kMCtrack, 7)) break;
1431     return kTRUE;
1432   case 31: //kMCtrack [pt]
1433     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1434     pad = (TVirtualPad*)l->At(0); pad->cd();
1435     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1436     // pi selection
1437     n=0;
1438     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1439       selection[n++] = il*11 + 2; // pi-
1440       selection[n++] = il*11 + 8; // pi+
1441     }
1442     xy[0] = 0.2; xy[1] = -.7; xy[2] = 7.; xy[3] = 4.;
1443     //xy[0] = 0.2; xy[1] = -1.; xy[2] = 7.; xy[3] = 10.; // SA
1444     if(!GetGraphArray(xy, kMCtrack, 8, kTRUE, n, selection, "#pi#pm")) break;
1445     pad->Modified(); pad->Update(); pad->SetLogx();
1446     pad = (TVirtualPad*)l->At(1); pad->cd();
1447     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1448     // mu selection
1449     n=0;    
1450     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1451       selection[n++] = il*11 + 3; // mu-
1452       selection[n++] = il*11 + 7; // mu+
1453     }
1454     if(!GetGraphArray(xy, kMCtrack, 8, kTRUE, n, selection, "#mu#pm")) break;
1455     pad->Modified(); pad->Update(); pad->SetLogx();
1456     return kTRUE;
1457   case 32: //kMCtrack [pt]
1458     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1459     pad = (TVirtualPad*)l->At(0); pad->cd();
1460     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1461     // p selection
1462     n=0;
1463     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1464       selection[n++] = il*11 + 0; // p bar
1465       selection[n++] = il*11 + 10; // p
1466     }
1467     xy[0] = 0.2; xy[1] = -.7; xy[2] = 7.; xy[3] = 8.;
1468     //xy[0] = 0.2; xy[1] = -1.; xy[2] = 7.; xy[3] = 10.; // SA
1469     if(!GetGraphArray(xy, kMCtrack, 8, kTRUE, n, selection, "p&p bar")) break;
1470     pad->Modified(); pad->Update(); pad->SetLogx();
1471     pad = (TVirtualPad*)l->At(1); pad->cd();
1472     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1473     // e selection
1474     n=0;    
1475     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1476       selection[n++] = il*11 + 4; // e-
1477       selection[n++] = il*11 + 6; // e+
1478     }
1479     xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 12.;
1480     //xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 14.; // SA
1481     if(!GetGraphArray(xy, kMCtrack, 8, kTRUE, n, selection, "e#pm")) break;
1482     pad->Modified(); pad->Update(); pad->SetLogx();
1483     return kTRUE;
1484   case 33: //kMCtrack [1/pt] pulls
1485     xy[0] = 0.; xy[1] = -1.; xy[2] = 2.; xy[3] = 3.5;
1486     //xy[0] = 0.; xy[1] = -1.; xy[2] = 2.; xy[3] = 4.5; // SA
1487     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1488     pad = (TVirtualPad*)l->At(0); pad->cd();
1489     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1490     // pi selection
1491     n=0;
1492     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1493       selection[n++] = il*11 + 2; // pi-
1494       selection[n++] = il*11 + 8; // pi+
1495     }
1496     if(!GetGraphArray(xy, kMCtrack, 9, kTRUE, n, selection, "#pi#pm")) break;
1497     pad = (TVirtualPad*)l->At(1); pad->cd();
1498     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1499     // mu selection
1500     n=0;    
1501     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1502       selection[n++] = il*11 + 3; // mu-
1503       selection[n++] = il*11 + 7; // mu+
1504     }
1505     if(!GetGraphArray(xy, kMCtrack, 9, kTRUE, n, selection, "#mu#pm")) break;
1506     return kTRUE;
1507   case 34: //kMCtrack [1/pt] pulls
1508     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1509     pad = (TVirtualPad*)l->At(0); pad->cd();
1510     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1511     // p selection
1512     n=0;
1513     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1514       selection[n++] = il*11 + 0; // p bar
1515       selection[n++] = il*11 + 10; // p
1516     }
1517     xy[0] = 0.; xy[1] = -1.; xy[2] = 2.; xy[3] = 3.5;
1518     //xy[0] = 0.; xy[1] = -1.; xy[2] = 2.; xy[3] = 6.; // SA
1519     if(!GetGraphArray(xy, kMCtrack, 9, kTRUE, n, selection, "p & p bar")) break;
1520     pad = (TVirtualPad*)l->At(1); pad->cd();
1521     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1522     // e selection
1523     n=0;    
1524     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1525       selection[n++] = il*11 + 4; // e-
1526       selection[n++] = il*11 + 6; // e+
1527     }
1528     xy[0] = 0.; xy[1] = -2.; xy[2] = 2.; xy[3] = 4.5;
1529     if(!GetGraphArray(xy, kMCtrack, 9, kTRUE, n, selection, "e#pm")) break;
1530     return kTRUE;
1531   case 35: //kMCtrack [p]
1532     xy[0] = 0.2; xy[1] = -.7; xy[2] = 7.; xy[3] = 4.;
1533     //xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 10.;
1534     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1535     pad = (TVirtualPad*)l->At(0); pad->cd();
1536     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1537     // pi selection
1538     n=0;
1539     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1540       selection[n++] = il*11 + 2; // pi-
1541       selection[n++] = il*11 + 8; // pi+
1542     }
1543     if(!GetGraphArray(xy, kMCtrack, 10, kTRUE, n, selection, "#pi#pm")) break;
1544     pad->Modified(); pad->Update(); pad->SetLogx();
1545     pad = (TVirtualPad*)l->At(1); pad->cd();
1546     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1547     // mu selection
1548     n=0;    
1549     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1550       selection[n++] = il*11 + 3; // mu-
1551       selection[n++] = il*11 + 7; // mu+
1552     }
1553     if(!GetGraphArray(xy, kMCtrack, 10, kTRUE, n, selection, "#mu#pm")) break;
1554     pad->Modified(); pad->Update(); pad->SetLogx();
1555     return kTRUE;
1556   case 36: //kMCtrack [p]
1557     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1558     pad = (TVirtualPad*)l->At(0); pad->cd();
1559     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1560     // p selection
1561     n=0;
1562     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1563       selection[n++] = il*11 + 0; // p bar
1564       selection[n++] = il*11 + 10; // p
1565     }
1566     xy[0] = 0.2; xy[1] = -.7; xy[2] = 7.; xy[3] = 8.;
1567     //xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 12.; // SA
1568     if(!GetGraphArray(xy, kMCtrack, 10, kTRUE, n, selection, "p & p bar")) break;
1569     pad->Modified(); pad->Update(); pad->SetLogx();
1570     pad = (TVirtualPad*)l->At(1); pad->cd();
1571     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1572     // e selection
1573     n=0;    
1574     for(Int_t il(ly0); il<AliTRDgeometry::kNlayer; il+=dly){
1575       selection[n++] = il*11 + 4; // e-
1576       selection[n++] = il*11 + 6; // e+
1577     }
1578     xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 12.;
1579     //xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 14.; // SA
1580     if(!GetGraphArray(xy, kMCtrack, 10, kTRUE, n, selection, "e#pm")) break;
1581     pad->Modified(); pad->Update(); pad->SetLogx();
1582     return kTRUE;
1583   case 37: // kMCtrackIn [y]
1584     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1585     xy[0]=-.25; xy[1]=-1000.; xy[2]=.25; xy[3] =3000.;
1586     ((TVirtualPad*)l->At(0))->cd();
1587     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1588     if(!GetGraphArray(xy, kMCtrackIn, 0, 1, n, selection)) break;
1589     ((TVirtualPad*)l->At(1))->cd();
1590     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1591     if(!GetGraphArray(&xy[0], kMCtrackIn, 0, 1, n, selection)) break;
1592     return kTRUE;
1593   case 38: // kMCtrackIn [y]
1594     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1595     xy[0]=-.25; xy[1]=-1000.; xy[2]=.25; xy[3] =3000.;
1596     ((TVirtualPad*)l->At(0))->cd();
1597     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1598     if(!GetGraphArray(xy, kMCtrackIn, 0, 1, n, selection)) break;
1599     xy[0] = -.5; xy[1] = -0.5; xy[2] = fgkNresYsegm[fSegmentLevel]-.5; xy[3] = 2.5;
1600     ((TVirtualPad*)l->At(1))->cd();
1601     if(!GetGraphArray(xy, kMCtrackIn, 1, 1)) break;
1602     return kTRUE;
1603   case 39: // kMCtrackIn [z]
1604     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1605     xy[0]=-1.; xy[1]=-500.; xy[2]=1.; xy[3] =800.;
1606     ((TVirtualPad*)l->At(0))->cd();
1607     if(!GetGraphArray(xy, kMCtrackIn, 2, 1)) break;
1608     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 2.5;
1609     ((TVirtualPad*)l->At(1))->cd();
1610     if(!GetGraphArray(xy, kMCtrackIn, 3, 1)) break;
1611     return kTRUE;
1612   case 40: // kMCtrackIn [phi|snp]
1613     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1614     xy[0]=-.25; xy[1]=-0.5; xy[2]=.25; xy[3] =2.5;
1615     ((TVirtualPad*)l->At(0))->cd();
1616     if(!GetGraph(&xy[0], kMCtrackIn, 4)) break;
1617     xy[0] = -.25; xy[1] = -0.5; xy[2] = .25; xy[3] = 1.5;
1618     ((TVirtualPad*)l->At(1))->cd();
1619     if(!GetGraph(&xy[0], kMCtrackIn, 5)) break;
1620     return kTRUE;
1621   case 41: // kMCtrackIn [theta|tgl]
1622     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1623     xy[0]=-1.; xy[1]=-1.; xy[2]=1.; xy[3] =4.;
1624     ((TVirtualPad*)l->At(0))->cd();
1625     if(!GetGraph(&xy[0], kMCtrackIn, 6)) break;
1626     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 1.5;
1627     ((TVirtualPad*)l->At(1))->cd();
1628     if(!GetGraph(&xy[0], kMCtrackIn, 7)) break;
1629     return kTRUE;
1630   case 42: // kMCtrackIn [pt]
1631     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1632     xy[0] = 0.2; xy[1] = -.8; xy[2] = 7.; xy[3] = 6.;
1633     //xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 10.; // SA
1634     pad=(TVirtualPad*)l->At(0); pad->cd(); pad->SetLogx();
1635     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1636     n=0; selection[n++]=2; selection[n++]=3; selection[n++]=7; selection[n++]=8;
1637     if(!GetGraphArray(xy, kMCtrackIn, 8, 1, n, selection)) break;
1638     pad = (TVirtualPad*)l->At(1); pad->cd(); pad->SetLogx();
1639     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1640     n=0; selection[n++]=0; selection[n++]=4; selection[n++]=6; selection[n++]=10;
1641     if(!GetGraphArray(xy, kMCtrackIn, 8, 1, n, selection)) break;
1642     return kTRUE;
1643   case 43: //kMCtrackIn [1/pt] pulls
1644     xy[0] = 0.; xy[1] = -1.; xy[2] = 2.; xy[3] = 3.5;
1645     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1646     pad = (TVirtualPad*)l->At(0); pad->cd();
1647     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1648     n=0; selection[n++]=2; selection[n++]=3; selection[n++]=7; selection[n++]=8;
1649     if(!GetGraphArray(xy, kMCtrackIn, 9, 1, n, selection)) break;
1650     pad = (TVirtualPad*)l->At(1); pad->cd();
1651     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1652     n=0; selection[n++]=0; selection[n++]=4; selection[n++]=6; selection[n++]=10;
1653     if(!GetGraphArray(xy, kMCtrackIn, 9, 1, n, selection)) break;
1654     return kTRUE;
1655   case 44: // kMCtrackIn [p]
1656     xy[0] = 0.2; xy[1] = -.8; xy[2] = 7.; xy[3] = 6.;
1657     //xy[0] = 0.2; xy[1] = -1.5; xy[2] = 7.; xy[3] = 10.;
1658     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1659     pad = ((TVirtualPad*)l->At(0));pad->cd();pad->SetLogx();
1660     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1661     n=0; selection[n++]=2; selection[n++]=3; selection[n++]=7; selection[n++]=8;
1662     if(!GetGraphArray(xy, kMCtrackIn, 10, 1, n, selection)) break;
1663     pad = ((TVirtualPad*)l->At(1)); pad->cd();pad->SetLogx();
1664     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1665     n=0; selection[n++]=0; selection[n++]=4; selection[n++]=6; selection[n++]=10;
1666     if(!GetGraphArray(xy, kMCtrackIn, 10, 1,  n, selection)) break;
1667     return kTRUE;
1668   case 45: // kMCtrackOut [y]
1669     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1670     xy[0]=-.3; xy[1]=-50.; xy[2]=.3; xy[3] =400.;
1671     ((TVirtualPad*)l->At(0))->cd();
1672     selStart=0; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1673     if(!GetGraphArray(xy, kMCtrackOut, 0, 1, n, selection)) break;
1674     ((TVirtualPad*)l->At(1))->cd();
1675     selStart=fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1676     if(!GetGraphArray(&xy[0], kMCtrackOut, 0, 1, n, selection)) break;
1677     return kTRUE;
1678   case 46: // kMCtrackOut [y]
1679     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1680     xy[0]=-.3; xy[1]=-50.; xy[2]=.3; xy[3] =400.;
1681     ((TVirtualPad*)l->At(0))->cd();
1682     selStart=2*fgkNresYsegm[fSegmentLevel]/3; for(n=0; n<fgkNresYsegm[fSegmentLevel]/3; n++) selection[n]=selStart+n;
1683     if(!GetGraphArray(xy, kMCtrackOut, 0, 1, n, selection)) break;
1684     xy[0] = -.5; xy[1] = -0.5; xy[2] = fgkNresYsegm[fSegmentLevel]-.5; xy[3] = 2.5;
1685     ((TVirtualPad*)l->At(1))->cd();
1686     if(!GetGraphArray(xy, kMCtrackOut, 1, 1)) break;
1687     return kTRUE;
1688   case 47: // kMCtrackOut [z]
1689     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1690     xy[0]=-1.; xy[1]=-500.; xy[2]=1.; xy[3] =1500.;
1691     ((TVirtualPad*)l->At(0))->cd();
1692     if(!GetGraphArray(xy, kMCtrackOut, 2, 1)) break;
1693     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 2.5;
1694     ((TVirtualPad*)l->At(1))->cd();
1695     if(!GetGraphArray(xy, kMCtrackOut, 3, 1)) break;
1696     return kTRUE;
1697   case 48: // kMCtrackOut [phi|snp]
1698     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1699     xy[0]=-.25; xy[1]=-0.5; xy[2]=.25; xy[3] =2.5;
1700     ((TVirtualPad*)l->At(0))->cd();
1701     if(!GetGraph(&xy[0], kMCtrackOut, 4)) break;
1702     xy[0] = -.25; xy[1] = -0.5; xy[2] = .25; xy[3] = 1.5;
1703     ((TVirtualPad*)l->At(1))->cd();
1704     if(!GetGraph(&xy[0], kMCtrackOut, 5)) break;
1705     return kTRUE;
1706   case 49: // kMCtrackOut [theta|tgl]
1707     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1708     xy[0]=-1.; xy[1]=-1.; xy[2]=1.; xy[3] =4.;
1709     ((TVirtualPad*)l->At(0))->cd();
1710     if(!GetGraph(&xy[0], kMCtrackOut, 6)) break;
1711     xy[0] = -1.; xy[1] = -0.5; xy[2] = 1.; xy[3] = 15.;
1712     ((TVirtualPad*)l->At(1))->cd();
1713     if(!GetGraph(&xy[0], kMCtrackOut, 7)) break;
1714     return kTRUE;
1715   case 50: // kMCtrackOut [pt]
1716     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1717     xy[0] = 0.2; xy[1] = -.8; xy[2] = 7.; xy[3] = 6.;
1718     pad=(TVirtualPad*)l->At(0); pad->cd(); pad->SetLogx();
1719     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1720     n=0; selection[n++]=2; selection[n++]=3; selection[n++]=7; selection[n++]=8;
1721     if(!GetGraphArray(xy, kMCtrackOut, 8, 1, n, selection)) break;
1722     pad = (TVirtualPad*)l->At(1); pad->cd();pad->SetLogx();
1723     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1724     n=0; selection[n++]=0; selection[n++]=4; selection[n++]=6; selection[n++]=10;
1725     if(!GetGraphArray(xy, kMCtrackOut, 8, 1, n, selection)) break;
1726     return kTRUE;
1727   case 51: //kMCtrackOut [1/pt] pulls
1728     xy[0] = 0.; xy[1] = -1.; xy[2] = 2.; xy[3] = 3.5;
1729     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1730     pad = (TVirtualPad*)l->At(0); pad->cd();
1731     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1732     n=0; selection[n++]=2; selection[n++]=3; selection[n++]=7; selection[n++]=8;
1733     if(!GetGraphArray(xy, kMCtrackOut, 9, 1, n, selection)) break;
1734     pad = (TVirtualPad*)l->At(1); pad->cd();
1735     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1736     n=0; selection[n++]=0; selection[n++]=4; selection[n++]=6; selection[n++]=10;
1737     if(!GetGraphArray(xy, kMCtrackOut, 9, 1, n, selection)) break;
1738     return kTRUE;
1739   case 52: // kMCtrackOut [p]
1740     gPad->Divide(2, 1, 1.e-5, 1.e-5); l=gPad->GetListOfPrimitives(); 
1741     xy[0] = 0.2; xy[1] = -.8; xy[2] = 7.; xy[3] = 6.;
1742     pad = ((TVirtualPad*)l->At(0));pad->cd();pad->SetLogx();
1743     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1744     n=0; selection[n++]=2; selection[n++]=3; selection[n++]=7; selection[n++]=8;
1745     if(!GetGraphArray(xy, kMCtrackOut, 10, 1, n, selection)) break;
1746     pad = ((TVirtualPad*)l->At(1)); pad->cd();pad->SetLogx();
1747     pad->SetMargin(0.125, 0.015, 0.1, 0.015);
1748     n=0; selection[n++]=0; selection[n++]=4; selection[n++]=6; selection[n++]=10;
1749     if(!GetGraphArray(xy, kMCtrackOut, 10, 1, n, selection)) break;
1750     return kTRUE;
1751   }
1752   AliWarning(Form("Reference plot [%d] missing result", ifig));
1753   return kFALSE;
1754 }
1755
1756 Char_t const *fgParticle[11]={
1757   " p bar", " K -", " #pi -", " #mu -", " e -",
1758   " No PID",
1759   " e +", " #mu +", " #pi +", " K +", " p",
1760 };
1761 const Color_t fgColorS[11]={
1762 kOrange, kOrange-3, kMagenta+1, kViolet, kRed,
1763 kGray,
1764 kRed, kViolet, kMagenta+1, kOrange-3, kOrange
1765 };
1766 const Color_t fgColorM[11]={
1767 kCyan-5, kAzure-4, kBlue-7, kBlue+2, kViolet+10,
1768 kBlack,
1769 kViolet+10, kBlue+2, kBlue-7, kAzure-4, kCyan-5
1770 };
1771 const Marker_t fgMarker[11]={
1772 30, 30, 26, 25, 24,
1773 28,
1774 20, 21, 22, 29, 29
1775 };
1776 //________________________________________________________
1777 Bool_t AliTRDresolution::PostProcess()
1778 {
1779   //fContainer = dynamic_cast<TObjArray*>(GetOutputData(0));
1780   if (!fContainer) {
1781     AliError("ERROR: list not available");
1782     return kFALSE;
1783   }
1784   TGraph *gm= NULL, *gs= NULL;
1785   if(!fGraphS && !fGraphM){ 
1786     TObjArray *aM(NULL), *aS(NULL);
1787     Int_t n = fContainer->GetEntriesFast();
1788     fGraphS = new TObjArray(n); fGraphS->SetOwner();
1789     fGraphM = new TObjArray(n); fGraphM->SetOwner();
1790     for(Int_t ig(0), nc(0); ig<n; ig++){
1791       fGraphM->AddAt(aM = new TObjArray(fgNproj[ig]), ig);
1792       fGraphS->AddAt(aS = new TObjArray(fgNproj[ig]), ig);
1793       
1794       for(Int_t ic=0; ic<fgNproj[ig]; ic++, nc++){
1795         AliDebug(2, Form("building G[%d] P[%d] N[%d]", ig, ic, fNcomp[nc]));
1796         if(fNcomp[nc]>1){
1797           TObjArray *agS(NULL), *agM(NULL);
1798           aS->AddAt(agS = new TObjArray(fNcomp[nc]), ic); 
1799           aM->AddAt(agM = new TObjArray(fNcomp[nc]), ic); 
1800           for(Int_t is=fNcomp[nc]; is--;){
1801             agS->AddAt(gs = new TGraphErrors(), is);
1802             Int_t is0(is%11), il0(is/11);
1803             gs->SetMarkerStyle(fgMarker[is0]);
1804             gs->SetMarkerColor(fgColorS[is0]);
1805             gs->SetLineColor(fgColorS[is0]);
1806             gs->SetLineStyle(il0);gs->SetLineWidth(2);
1807             gs->SetName(Form("s_%d_%02d_%02d", ig, ic, is));
1808
1809             agM->AddAt(gm = new TGraphErrors(), is);
1810             gm->SetMarkerStyle(fgMarker[is0]);
1811             gm->SetMarkerColor(fgColorM[is0]);
1812             gm->SetLineColor(fgColorM[is0]);
1813             gm->SetLineStyle(il0);gm->SetLineWidth(2);
1814             gm->SetName(Form("m_%d_%02d_%02d", ig, ic, is));
1815             // this is important for labels in the legend 
1816             if(ic==0) {
1817               gs->SetTitle(Form("%s %02d", fgkResYsegmName[fSegmentLevel], is%fgkNresYsegm[fSegmentLevel]));
1818               gm->SetTitle(Form("%s %02d", fgkResYsegmName[fSegmentLevel], is%fgkNresYsegm[fSegmentLevel]));
1819             } else if(ic==1) {
1820               gs->SetTitle(Form("%s Ly[%d]", is%2 ?"z":"y", is/2));
1821               gm->SetTitle(Form("%s Ly[%d]", is%2?"z":"y", is/2));
1822             } else if(ic==2||ic==3) {
1823               gs->SetTitle(Form("%s Ly[%d]", is%2 ?"RC":"no RC", is/2));
1824               gm->SetTitle(Form("%s Ly[%d]", is%2?"RC":"no RC", is/2));
1825             } else if(ic<=7) {
1826               gs->SetTitle(Form("Layer[%d]", is%AliTRDgeometry::kNlayer));
1827               gm->SetTitle(Form("Layer[%d]", is%AliTRDgeometry::kNlayer));
1828             } else {
1829               gs->SetTitle(Form("%s @ ly[%d]", fgParticle[is0], il0));
1830               gm->SetTitle(Form("%s @ ly[%d]", fgParticle[is0], il0));
1831             }
1832           }
1833         } else {
1834           aS->AddAt(gs = new TGraphErrors(), ic);
1835           gs->SetMarkerStyle(23);
1836           gs->SetMarkerColor(kRed);
1837           gs->SetLineColor(kRed);
1838           gs->SetNameTitle(Form("s_%d_%02d", ig, ic), "sigma");
1839   
1840           aM->AddAt(gm = ig ? (TGraph*)new TGraphErrors() : (TGraph*)new TGraphAsymmErrors(), ic);
1841           gm->SetLineColor(kBlack);
1842           gm->SetMarkerStyle(7);
1843           gm->SetMarkerColor(kBlack);
1844           gm->SetNameTitle(Form("m_%d_%02d", ig, ic), "mean");
1845         }
1846       }
1847     }
1848   }
1849
1850 /*  printf("\n\n\n"); fGraphS->ls();
1851   printf("\n\n\n"); fGraphM->ls();*/
1852   
1853
1854   // DEFINE MODELS
1855   // simple gauss
1856   TF1 fg("fGauss", "gaus", -.5, .5);  
1857   // Landau for charge resolution
1858   TF1 fch("fClCh", "landau", 0., 1000.);  
1859   // Landau for e+- pt resolution
1860   TF1 fpt("fPt", "landau", -0.1, 0.2);  
1861
1862   //PROCESS EXPERIMENTAL DISTRIBUTIONS
1863   // Charge resolution
1864   //Process3DL(kCharge, 0, &fl); 
1865   // Clusters residuals
1866   Process3D(kCluster, 0, &fg, 1.e4); 
1867   Process3Dlinked(kCluster, 1, &fg); 
1868   fNRefFigures = 3;
1869   // Tracklet residual/pulls
1870   Process3D(kTrack , 0, &fg, 1.e4);
1871   Process3Dlinked(kTrack , 1, &fg); 
1872   Process3D(kTrack , 2, &fg, 1.e4); 
1873   Process3D(kTrack , 3, &fg); 
1874   Process2D(kTrack , 4, &fg, 1.e3);
1875   fNRefFigures = 7;
1876   // TRDin residual/pulls
1877   Process3D(kTrackIn, 0, &fg, 1.e4); 
1878   Process3Dlinked(kTrackIn, 1, &fg); 
1879   Process3D(kTrackIn, 2, &fg, 1.e4); 
1880   Process3D(kTrackIn, 3, &fg); 
1881   Process2D(kTrackIn, 4, &fg, 1.e3); 
1882   fNRefFigures = 11;
1883   // TRDout residual/pulls
1884   Process3D(kTrackOut, 0, &fg, 1.e3); // scale to fit - see PlotTrackOut
1885   Process3Dlinked(kTrackOut, 1, &fg); 
1886   Process3D(kTrackOut, 2, &fg, 1.e4); 
1887   Process3D(kTrackOut, 3, &fg); 
1888   Process2D(kTrackOut, 4, &fg, 1.e3); 
1889   fNRefFigures = 15;
1890
1891   if(!HasMCdata()) return kTRUE;
1892
1893
1894   //PROCESS MC RESIDUAL DISTRIBUTIONS
1895
1896   // CLUSTER Y RESOLUTION/PULLS
1897   Process3D(kMCcluster, 0, &fg, 1.e4);
1898   Process3Dlinked(kMCcluster, 1, &fg, 1.);
1899   fNRefFigures = 17;
1900
1901   // TRACKLET RESOLUTION/PULLS
1902   Process3D(kMCtracklet, 0, &fg, 1.e4); // y
1903   Process3Dlinked(kMCtracklet, 1, &fg, 1.);   // y pulls
1904   Process3D(kMCtracklet, 2, &fg, 1.e4); // z
1905   Process3D(kMCtracklet, 3, &fg, 1.);   // z pulls
1906   Process2D(kMCtracklet, 4, &fg, 1.e3); // phi
1907   fNRefFigures = 21;
1908
1909   // TRACK RESOLUTION/PULLS
1910   Process3Darray(kMCtrack, 0, &fg, 1.e4);   // y
1911   Process3DlinkedArray(kMCtrack, 1, &fg);   // y PULL
1912   Process3Darray(kMCtrack, 2, &fg, 1.e4);   // z
1913   Process3Darray(kMCtrack, 3, &fg);         // z PULL
1914   Process2Darray(kMCtrack, 4, &fg, 1.e3);   // phi
1915   Process2Darray(kMCtrack, 5, &fg);         // snp PULL
1916   Process2Darray(kMCtrack, 6, &fg, 1.e3);   // theta
1917   Process2Darray(kMCtrack, 7, &fg);         // tgl PULL
1918   Process3Darray(kMCtrack, 8, &fg, 1.e2);   // pt resolution
1919   Process3Darray(kMCtrack, 9, &fg);         // 1/pt pulls
1920   Process3Darray(kMCtrack, 10, &fg, 1.e2);  // p resolution
1921   fNRefFigures+=16;
1922
1923   // TRACK TRDin RESOLUTION/PULLS
1924   Process3D(kMCtrackIn, 0, &fg, 1.e4);// y resolution
1925   Process3Dlinked(kMCtrackIn, 1, &fg);      // y pulls
1926   Process3D(kMCtrackIn, 2, &fg, 1.e4);// z resolution
1927   Process3D(kMCtrackIn, 3, &fg);      // z pulls
1928   Process2D(kMCtrackIn, 4, &fg, 1.e3);// phi resolution
1929   Process2D(kMCtrackIn, 5, &fg);      // snp pulls
1930   Process2D(kMCtrackIn, 6, &fg, 1.e3);// theta resolution
1931   Process2D(kMCtrackIn, 7, &fg);      // tgl pulls
1932   Process3D(kMCtrackIn, 8, &fg, 1.e2);// pt resolution
1933   Process3D(kMCtrackIn, 9, &fg);      // 1/pt pulls
1934   Process3D(kMCtrackIn, 10, &fg, 1.e2);// p resolution
1935   fNRefFigures+=8;
1936
1937   // TRACK TRDout RESOLUTION/PULLS
1938   Process3D(kMCtrackOut, 0, &fg, 1.e4);// y resolution
1939   Process3Dlinked(kMCtrackOut, 1, &fg);      // y pulls
1940   Process3D(kMCtrackOut, 2, &fg, 1.e4);// z resolution
1941   Process3D(kMCtrackOut, 3, &fg);      // z pulls
1942   Process2D(kMCtrackOut, 4, &fg, 1.e3);// phi resolution
1943   Process2D(kMCtrackOut, 5, &fg);      // snp pulls
1944   Process2D(kMCtrackOut, 6, &fg, 1.e3);// theta resolution
1945   Process2D(kMCtrackOut, 7, &fg);      // tgl pulls
1946   Process3D(kMCtrackOut, 8, &fg, 1.e2);// pt resolution
1947   Process3D(kMCtrackOut, 9, &fg);      // 1/pt pulls
1948   Process3D(kMCtrackOut, 10, &fg, 1.e2);// p resolution
1949   fNRefFigures+=8;
1950
1951   return kTRUE;
1952 }
1953
1954
1955 //________________________________________________________
1956 void AliTRDresolution::Terminate(Option_t *opt)
1957 {
1958   AliTRDrecoTask::Terminate(opt);
1959   if(HasPostProcess()) PostProcess();
1960 }
1961
1962 //________________________________________________________
1963 void AliTRDresolution::AdjustF1(TH1 *h, TF1 *f)
1964 {
1965 // Helper function to avoid duplication of code
1966 // Make first guesses on the fit parameters
1967
1968   // find the intial parameters of the fit !! (thanks George)
1969   Int_t nbinsy = Int_t(.5*h->GetNbinsX());
1970   Double_t sum = 0.;
1971   for(Int_t jbin=nbinsy-4; jbin<=nbinsy+4; jbin++) sum+=h->GetBinContent(jbin); sum/=9.;
1972   f->SetParLimits(0, 0., 3.*sum);
1973   f->SetParameter(0, .9*sum);
1974   Double_t rms = h->GetRMS();
1975   f->SetParLimits(1, -rms, rms);
1976   f->SetParameter(1, h->GetMean());
1977
1978   f->SetParLimits(2, 0., 2.*rms);
1979   f->SetParameter(2, rms);
1980   if(f->GetNpar() <= 4) return;
1981
1982   f->SetParLimits(3, 0., sum);
1983   f->SetParameter(3, .1*sum);
1984
1985   f->SetParLimits(4, -.3, .3);
1986   f->SetParameter(4, 0.);
1987
1988   f->SetParLimits(5, 0., 1.e2);
1989   f->SetParameter(5, 2.e-1);
1990 }
1991
1992 //________________________________________________________
1993 TObjArray* AliTRDresolution::BuildMonitorContainerCluster(const char* name, Bool_t expand)
1994 {
1995 // Build performance histograms for AliTRDcluster.vs TRD track or MC
1996 //  - y reziduals/pulls
1997
1998   TObjArray *arr = new TObjArray(2);
1999   arr->SetName(name); arr->SetOwner();
2000   TH1 *h(NULL); char hname[100], htitle[300];
2001
2002   // tracklet resolution/pull in y direction
2003   sprintf(hname, "%s_%s_Y", GetNameId(), name);
2004   sprintf(htitle, "Y res for \"%s\" @ %s;tg(#phi);#Delta y [cm];%s", GetNameId(), name, fgkResYsegmName[fSegmentLevel]);
2005   if(!(h = (TH3S*)gROOT->FindObject(hname))){
2006     Int_t nybins=fgkNresYsegm[fSegmentLevel];
2007     if(expand) nybins*=2;
2008     h = new TH3S(hname, htitle, 
2009                  48, -.48, .48, 60, -.15, .15, nybins, -0.5, nybins-0.5);
2010   } else h->Reset();
2011   arr->AddAt(h, 0);
2012   sprintf(hname, "%s_%s_YZpull", GetNameId(), name);
2013   sprintf(htitle, "YZ pull for \"%s\" @ %s;%s;#Delta y  / #sigma_{y};#Delta z  / #sigma_{z}", GetNameId(), name, fgkResYsegmName[fSegmentLevel]);
2014   if(!(h = (TH3S*)gROOT->FindObject(hname))){
2015     h = new TH3S(hname, htitle, fgkNresYsegm[fSegmentLevel], -0.5, fgkNresYsegm[fSegmentLevel]-0.5, 100, -4.5, 4.5, 100, -4.5, 4.5);
2016   } else h->Reset();
2017   arr->AddAt(h, 1);
2018
2019   return arr;
2020 }
2021
2022 //________________________________________________________
2023 TObjArray* AliTRDresolution::BuildMonitorContainerTracklet(const char* name, Bool_t expand)
2024 {
2025 // Build performance histograms for AliExternalTrackParam.vs TRD tracklet
2026 //  - y reziduals/pulls
2027 //  - z reziduals/pulls
2028 //  - phi reziduals
2029   TObjArray *arr = BuildMonitorContainerCluster(name, expand); 
2030   arr->Expand(5);
2031   TH1 *h(NULL); char hname[100], htitle[300];
2032
2033   // tracklet resolution/pull in z direction
2034   sprintf(hname, "%s_%s_Z", GetNameId(), name);
2035   sprintf(htitle, "Z res for \"%s\" @ %s;tg(#theta);#Delta z [cm];row cross", GetNameId(), name);
2036   if(!(h = (TH3S*)gROOT->FindObject(hname))){
2037     h = new TH3S(hname, htitle, 50, -1., 1., 100, -1.5, 1.5, 2, -0.5, 1.5);
2038   } else h->Reset();
2039   arr->AddAt(h, 2);
2040   sprintf(hname, "%s_%s_Zpull", GetNameId(), name);
2041   sprintf(htitle, "Z pull for \"%s\" @ %s;tg(#theta);#Delta z  / #sigma_{z};row cross", GetNameId(), name);
2042   if(!(h = (TH3S*)gROOT->FindObject(hname))){
2043     h = new TH3S(hname, htitle, 50, -1., 1., 100, -5.5, 5.5, 2, -0.5, 1.5);
2044     h->GetZaxis()->SetBinLabel(1, "no RC");
2045     h->GetZaxis()->SetBinLabel(2, "RC");
2046   } else h->Reset();
2047   arr->AddAt(h, 3);
2048
2049   // tracklet to track phi resolution
2050   sprintf(hname, "%s_%s_PHI", GetNameId(), name);
2051   sprintf(htitle, "#Phi res for \"%s\" @ %s;tg(#phi);#Delta #phi [rad];entries", GetNameId(), name);
2052   if(!(h = (TH2I*)gROOT->FindObject(hname))){
2053     h = new TH2I(hname, htitle, 21, -.33, .33, 100, -.5, .5);
2054   } else h->Reset();
2055   arr->AddAt(h, 4);
2056
2057   return arr;
2058 }
2059
2060 //________________________________________________________
2061 TObjArray* AliTRDresolution::BuildMonitorContainerTrack(const char* name)
2062 {
2063 // Build performance histograms for AliExternalTrackParam.vs MC
2064 //  - y resolution/pulls
2065 //  - z resolution/pulls
2066 //  - phi resolution, snp pulls
2067 //  - theta resolution, tgl pulls
2068 //  - pt resolution, 1/pt pulls, p resolution
2069
2070   TObjArray *arr = BuildMonitorContainerTracklet(name); 
2071   arr->Expand(11);
2072   TH1 *h(NULL); char hname[100], htitle[300];
2073   TAxis *ax(NULL);
2074
2075   // snp pulls
2076   sprintf(hname, "%s_%s_SNPpull", GetNameId(), name);
2077   sprintf(htitle, "SNP pull for \"%s\" @ %s;tg(#phi);#Delta snp  / #sigma_{snp};entries", GetNameId(), name);
2078   if(!(h = (TH2I*)gROOT->FindObject(hname))){
2079     h = new TH2I(hname, htitle, 60, -.3, .3, 100, -4.5, 4.5);
2080   } else h->Reset();
2081   arr->AddAt(h, 5);
2082
2083   // theta resolution
2084   sprintf(hname, "%s_%s_THT", GetNameId(), name);
2085   sprintf(htitle, "#Theta res for \"%s\" @ %s;tg(#theta);#Delta #theta [rad];entries", GetNameId(), name);
2086   if(!(h = (TH2I*)gROOT->FindObject(hname))){
2087     h = new TH2I(hname, htitle, 100, -1., 1., 100, -5e-3, 5e-3);
2088   } else h->Reset();
2089   arr->AddAt(h, 6);
2090   // tgl pulls
2091   sprintf(hname, "%s_%s_TGLpull", GetNameId(), name);
2092   sprintf(htitle, "TGL pull for \"%s\" @ %s;tg(#theta);#Delta tgl  / #sigma_{tgl};entries", GetNameId(), name);
2093   if(!(h = (TH2I*)gROOT->FindObject(hname))){
2094     h = new TH2I(hname, htitle, 100, -1., 1., 100, -4.5, 4.5);
2095   } else h->Reset();
2096   arr->AddAt(h, 7);
2097   
2098   const Int_t kNpt(14);
2099   const Int_t kNdpt(150); 
2100   const Int_t kNspc = 2*AliPID::kSPECIES+1;
2101   Float_t Pt=0.1, DPt=-.1, Spc=-5.5;
2102   Float_t binsPt[kNpt+1], binsSpc[kNspc+1], binsDPt[kNdpt+1];
2103   for(Int_t i=0;i<kNpt+1; i++,Pt=TMath::Exp(i*.15)-1.) binsPt[i]=Pt;
2104   for(Int_t i=0; i<kNspc+1; i++,Spc+=1.) binsSpc[i]=Spc;
2105   for(Int_t i=0; i<kNdpt+1; i++,DPt+=2.e-3) binsDPt[i]=DPt;
2106
2107   // Pt resolution
2108   sprintf(hname, "%s_%s_Pt", GetNameId(), name);
2109   sprintf(htitle, "P_{t} res for \"%s\" @ %s;p_{t} [GeV/c];#Delta p_{t}/p_{t}^{MC};SPECIES", GetNameId(), name);
2110   if(!(h = (TH3S*)gROOT->FindObject(hname))){
2111     h = new TH3S(hname, htitle, 
2112                  kNpt, binsPt, kNdpt, binsDPt, kNspc, binsSpc);
2113     ax = h->GetZaxis();
2114     for(Int_t ib(1); ib<=ax->GetNbins(); ib++) ax->SetBinLabel(ib, fgParticle[ib-1]);
2115   } else h->Reset();
2116   arr->AddAt(h, 8);
2117   // 1/Pt pulls
2118   sprintf(hname, "%s_%s_1Pt", GetNameId(), name);
2119   sprintf(htitle, "1/P_{t} pull for \"%s\" @ %s;1/p_{t}^{MC} [c/GeV];#Delta(1/p_{t})/#sigma(1/p_{t});SPECIES", GetNameId(), name);
2120   if(!(h = (TH3S*)gROOT->FindObject(hname))){
2121     h = new TH3S(hname, htitle, 
2122                  kNpt, 0., 2., 100, -4., 4., kNspc, -5.5, 5.5);
2123     ax = h->GetZaxis();
2124     for(Int_t ib(1); ib<=ax->GetNbins(); ib++) ax->SetBinLabel(ib, fgParticle[ib-1]);
2125   } else h->Reset();
2126   arr->AddAt(h, 9);
2127   // P resolution
2128   sprintf(hname, "%s_%s_P", GetNameId(), name);
2129   sprintf(htitle, "P res for \"%s\" @ %s;p [GeV/c];#Delta p/p^{MC};SPECIES", GetNameId(), name);
2130   if(!(h = (TH3S*)gROOT->FindObject(hname))){
2131     h = new TH3S(hname, htitle, 
2132                  kNpt, binsPt, kNdpt, binsDPt, kNspc, binsSpc);
2133     ax = h->GetZaxis();
2134     for(Int_t ib(1); ib<=ax->GetNbins(); ib++) ax->SetBinLabel(ib, fgParticle[ib-1]);
2135   } else h->Reset();
2136   arr->AddAt(h, 10);
2137
2138   return arr;
2139 }
2140
2141
2142 //________________________________________________________
2143 TObjArray* AliTRDresolution::Histos()
2144 {
2145   //
2146   // Define histograms
2147   //
2148
2149   if(fContainer) return fContainer;
2150
2151   fContainer  = new TObjArray(kNviews);
2152   //fContainer->SetOwner(kTRUE);
2153   TH1 *h(NULL);
2154   TObjArray *arr(NULL);
2155
2156   // binnings for plots containing momentum or pt
2157   const Int_t kNpt(14), kNphi(48), kNdy(60);
2158   Float_t Phi=-.48, Dy=-.3, Pt=0.1;
2159   Float_t binsPhi[kNphi+1], binsDy[kNdy+1], binsPt[kNpt+1];
2160   for(Int_t i=0; i<kNphi+1; i++,Phi+=.02) binsPhi[i]=Phi;
2161   for(Int_t i=0; i<kNdy+1; i++,Dy+=.01) binsDy[i]=Dy;
2162   for(Int_t i=0;i<kNpt+1; i++,Pt=TMath::Exp(i*.15)-1.) binsPt[i]=Pt;
2163
2164   // cluster to track residuals/pulls
2165   fContainer->AddAt(arr = new TObjArray(2), kCharge);
2166   arr->SetName("Charge");
2167   if(!(h = (TH3S*)gROOT->FindObject("hCharge"))){
2168     h = new TH3S("hCharge", "Charge Resolution", 20, 1., 2., 24, 0., 3.6, 100, 0., 500.);
2169     h->GetXaxis()->SetTitle("dx/dx_{0}");
2170     h->GetYaxis()->SetTitle("x_{d} [cm]");
2171     h->GetZaxis()->SetTitle("dq/dx [ADC/cm]");
2172   } else h->Reset();
2173   arr->AddAt(h, 0);
2174
2175   // cluster to track residuals/pulls
2176   fContainer->AddAt(BuildMonitorContainerCluster("Cl"), kCluster);
2177   // tracklet to TRD track
2178   fContainer->AddAt(BuildMonitorContainerTracklet("Trk", kTRUE), kTrack);
2179   // tracklet to TRDin
2180   fContainer->AddAt(BuildMonitorContainerTracklet("TrkIN", kTRUE), kTrackIn);
2181   // tracklet to TRDout
2182   fContainer->AddAt(BuildMonitorContainerTracklet("TrkOUT"), kTrackOut);
2183
2184
2185   // Resolution histos
2186   if(!HasMCdata()) return fContainer;
2187
2188   // cluster resolution 
2189   fContainer->AddAt(BuildMonitorContainerCluster("MCcl"),  kMCcluster);
2190
2191   // tracklet resolution
2192   fContainer->AddAt(BuildMonitorContainerTracklet("MCtracklet"), kMCtracklet);
2193
2194   // track resolution
2195   fContainer->AddAt(arr = new TObjArray(AliTRDgeometry::kNlayer), kMCtrack);
2196   arr->SetName("MCtrk");
2197   for(Int_t il(0); il<AliTRDgeometry::kNlayer; il++) arr->AddAt(BuildMonitorContainerTrack(Form("MCtrk_Ly%d", il)), il);
2198
2199   // TRDin TRACK RESOLUTION
2200   fContainer->AddAt(BuildMonitorContainerTrack("MCtrkIN"), kMCtrackIn);
2201
2202   // TRDout TRACK RESOLUTION
2203   fContainer->AddAt(BuildMonitorContainerTrack("MCtrkOUT"), kMCtrackOut);
2204
2205   return fContainer;
2206 }
2207
2208 //________________________________________________________
2209 Bool_t AliTRDresolution::Load(const Char_t *file, const Char_t *dir)
2210 {
2211 // Custom load function. Used to guess the segmentation level of the data.
2212
2213   if(!AliTRDrecoTask::Load(file, dir)) return kFALSE;
2214
2215   // look for cluster residual plot - always available
2216   TH3S* h3((TH3S*)((TObjArray*)fContainer->At(kClToTrk))->At(0));
2217   Int_t segmentation(h3->GetNbinsZ()/2);
2218   if(segmentation==fgkNresYsegm[0]){ // default segmentation. Nothing to do
2219     return kTRUE;
2220   } else if(segmentation==fgkNresYsegm[1]){ // stack segmentation.
2221     SetSegmentationLevel(1);
2222   } else if(segmentation==fgkNresYsegm[2]){ // detector segmentation.
2223     SetSegmentationLevel(2);
2224   } else {
2225     AliError(Form("Unknown segmentation [%d].", h3->GetNbinsZ()));
2226     return kFALSE;
2227   }
2228
2229   AliDebug(2, Form("Segmentation set to level \"%s\"", fgkResYsegmName[fSegmentLevel]));
2230   return kTRUE;
2231 }
2232
2233
2234 //________________________________________________________
2235 Bool_t AliTRDresolution::Process(TH2 * const h2, TF1 *f, Float_t k, TGraphErrors **g)
2236 {
2237   //
2238   // Do the processing
2239   //
2240
2241   Char_t pn[10]; sprintf(pn, "p%03d", fIdxPlot);
2242   Int_t n = 0;
2243   if((n=g[0]->GetN())) for(;n--;) g[0]->RemovePoint(n);
2244   if((n=g[1]->GetN())) for(;n--;) g[1]->RemovePoint(n);
2245   if(Int_t(h2->GetEntries())){ 
2246     AliDebug(4, Form("%s: g[%s %s]", pn, g[0]->GetName(), g[0]->GetTitle()));
2247   } else {
2248     AliDebug(2, Form("%s: g[%s %s]: Missing entries.", pn, g[0]->GetName(), g[0]->GetTitle()));
2249     fIdxPlot++;
2250     return kTRUE;
2251   }
2252
2253   const Int_t kINTEGRAL=1;
2254   for(Int_t ibin = 0; ibin < Int_t(h2->GetNbinsX()/kINTEGRAL); ibin++){
2255     Int_t abin(ibin*kINTEGRAL+1),bbin(abin+kINTEGRAL-1),mbin(abin+Int_t(kINTEGRAL/2));
2256     Double_t x = h2->GetXaxis()->GetBinCenter(mbin);
2257     TH1D *h = h2->ProjectionY(pn, abin, bbin);
2258     if((n=(Int_t)h->GetEntries())<100){ 
2259       AliDebug(4, Form("  x[%f] range[%d %d] stat[%d] low statistics !", x, abin, bbin, n));
2260       continue;
2261     }
2262     h->Fit(f, "QN");
2263     Int_t ip = g[0]->GetN();
2264     AliDebug(4, Form("  x_%d[%f] range[%d %d] stat[%d] M[%f] Sgm[%f]", ip, x, abin, bbin, n, f->GetParameter(1), f->GetParameter(2)));
2265     g[0]->SetPoint(ip, x, k*f->GetParameter(1));
2266     g[0]->SetPointError(ip, 0., k*f->GetParError(1));
2267     g[1]->SetPoint(ip, x, k*f->GetParameter(2));
2268     g[1]->SetPointError(ip, 0., k*f->GetParError(2));
2269 /*  
2270     g[0]->SetPoint(ip, x, k*h->GetMean());
2271     g[0]->SetPointError(ip, 0., k*h->GetMeanError());
2272     g[1]->SetPoint(ip, x, k*h->GetRMS());
2273     g[1]->SetPointError(ip, 0., k*h->GetRMSError());*/
2274   }
2275   fIdxPlot++;
2276   return kTRUE;
2277 }
2278
2279 //________________________________________________________
2280 Bool_t AliTRDresolution::Process2D(ETRDresolutionPlot plot, Int_t idx, TF1 *f, Float_t k, Int_t gidx)
2281 {
2282   //
2283   // Do the processing
2284   //
2285
2286   if(!fContainer || !fGraphS || !fGraphM) return kFALSE;
2287
2288   // retrive containers
2289   TH2I *h2(NULL);
2290   if(idx<0){
2291     if(!(h2= (TH2I*)(fContainer->At(plot)))) return kFALSE; 
2292   } else{
2293     TObjArray *a0(NULL);
2294     if(!(a0=(TObjArray*)(fContainer->At(plot)))) return kFALSE;
2295     if(!(h2=(TH2I*)a0->At(idx))) return kFALSE;
2296   }
2297   if(Int_t(h2->GetEntries())){ 
2298     AliDebug(2, Form("p[%d] idx[%d] : h[%s] %s", plot, idx, h2->GetName(), h2->GetTitle()));
2299   } else {
2300     AliDebug(2, Form("p[%d] idx[%d] : Missing entries.", plot, idx));
2301     return kFALSE;
2302   }
2303
2304   TGraphErrors *g[2];
2305   if(gidx<0) gidx=idx;
2306   if(!(g[0] = gidx<0 ? (TGraphErrors*)fGraphM->At(plot) : (TGraphErrors*)((TObjArray*)(fGraphM->At(plot)))->At(gidx))) return kFALSE;
2307
2308   if(!(g[1] = gidx<0 ? (TGraphErrors*)fGraphS->At(plot) : (TGraphErrors*)((TObjArray*)(fGraphS->At(plot)))->At(gidx))) return kFALSE;
2309
2310   return Process(h2, f, k, g);
2311 }
2312
2313 //________________________________________________________
2314 Bool_t AliTRDresolution::Process3D(ETRDresolutionPlot plot, Int_t idx, TF1 *f, Float_t k)
2315 {
2316   //
2317   // Do the processing
2318   //
2319
2320   if(!fContainer || !fGraphS || !fGraphM) return kFALSE;
2321
2322   // retrive containers
2323   TH3S *h3(NULL);
2324   if(idx<0){
2325     if(!(h3= (TH3S*)(fContainer->At(plot)))) return kFALSE; 
2326   } else{
2327     TObjArray *a0(NULL);
2328     if(!(a0=(TObjArray*)(fContainer->At(plot)))) return kFALSE;
2329     if(!(h3=(TH3S*)a0->At(idx))) return kFALSE;
2330   }
2331   if(Int_t(h3->GetEntries())){ 
2332     AliDebug(2, Form("p[%d] idx[%d] h[%s] %s", plot, idx, h3->GetName(), h3->GetTitle()));
2333   } else {
2334     AliDebug(2, Form("p[%d] idx[%d] : Missing entries.", plot, idx));
2335     return kFALSE;
2336   }
2337
2338   TObjArray *gm, *gs;
2339   if(!(gm = (TObjArray*)((TObjArray*)(fGraphM->At(plot)))->At(idx))) return kFALSE;
2340   if(!(gs = (TObjArray*)((TObjArray*)(fGraphS->At(plot)))->At(idx))) return kFALSE;
2341   TGraphErrors *g[2];
2342
2343   TAxis *az = h3->GetZaxis();
2344   for(Int_t iz(0); iz<gm->GetEntriesFast(); iz++){
2345     if(!(g[0] = (TGraphErrors*)gm->At(iz))) return kFALSE;
2346     if(!(g[1] = (TGraphErrors*)gs->At(iz))) return kFALSE;
2347     az->SetRange(iz+1, iz+1);
2348     if(!Process((TH2*)h3->Project3D("yx"), f, k, g)) return kFALSE;
2349   }
2350
2351   return kTRUE;
2352 }
2353
2354
2355 //________________________________________________________
2356 Bool_t AliTRDresolution::Process3Dlinked(ETRDresolutionPlot plot, Int_t idx, TF1 *f, Float_t k)
2357 {
2358   //
2359   // Do the processing
2360   //
2361
2362   if(!fContainer || !fGraphS || !fGraphM) return kFALSE;
2363
2364   // retrive containers
2365   TH3S *h3(NULL);
2366   if(idx<0){
2367     if(!(h3= (TH3S*)(fContainer->At(plot)))) return kFALSE; 
2368   } else{
2369     TObjArray *a0(NULL);
2370     if(!(a0=(TObjArray*)(fContainer->At(plot)))) return kFALSE;
2371     if(!(h3=(TH3S*)a0->At(idx))) return kFALSE;
2372   }
2373   if(Int_t(h3->GetEntries())){ 
2374     AliDebug(2, Form("p[%d] idx[%d] h[%s] %s", plot, idx, h3->GetName(), h3->GetTitle()));
2375   } else {
2376     AliDebug(2, Form("p[%d] idx[%d] : Missing entries.", plot, idx));
2377     return kFALSE;
2378   }
2379
2380   TObjArray *gm, *gs;
2381   if(!(gm = (TObjArray*)((TObjArray*)(fGraphM->At(plot)))->At(idx))) return kFALSE;
2382   if(!(gs = (TObjArray*)((TObjArray*)(fGraphS->At(plot)))->At(idx))) return kFALSE;
2383   TGraphErrors *g[2];
2384
2385   if(!(g[0] = (TGraphErrors*)gm->At(0))) return kFALSE;
2386   if(!(g[1] = (TGraphErrors*)gs->At(0))) return kFALSE;
2387   if(!Process((TH2*)h3->Project3D("yx"), f, k, g)) return kFALSE;
2388
2389   if(!(g[0] = (TGraphErrors*)gm->At(1))) return kFALSE;
2390   if(!(g[1] = (TGraphErrors*)gs->At(1))) return kFALSE;
2391   if(!Process((TH2*)h3->Project3D("zx"), f, k, g)) return kFALSE;
2392
2393   return kTRUE;
2394 }
2395
2396
2397 //________________________________________________________
2398 Bool_t AliTRDresolution::Process3DL(ETRDresolutionPlot plot, Int_t idx, TF1 *f, Float_t k)
2399 {
2400   //
2401   // Do the processing
2402   //
2403
2404   if(!fContainer || !fGraphS || !fGraphM) return kFALSE;
2405
2406   // retrive containers
2407   TH3S *h3 = (TH3S*)((TObjArray*)fContainer->At(plot))->At(idx);
2408   if(!h3) return kFALSE;
2409   AliDebug(2, Form("p[%d] idx[%d] h[%s] %s", plot, idx, h3->GetName(), h3->GetTitle()));
2410
2411   TGraphAsymmErrors *gm; 
2412   TGraphErrors *gs;
2413   if(!(gm = (TGraphAsymmErrors*)((TObjArray*)fGraphM->At(plot))->At(0))) return kFALSE;
2414   if(!(gs = (TGraphErrors*)((TObjArray*)fGraphS->At(plot)))) return kFALSE;
2415
2416   Float_t x, r, mpv, xM, xm;
2417   TAxis *ay = h3->GetYaxis();
2418   for(Int_t iy=1; iy<=h3->GetNbinsY(); iy++){
2419     ay->SetRange(iy, iy);
2420     x = ay->GetBinCenter(iy);
2421     TH2F *h2=(TH2F*)h3->Project3D("zx");
2422     TAxis *ax = h2->GetXaxis();
2423     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
2424       r = ax->GetBinCenter(ix);
2425       TH1D *h1 = h2->ProjectionY("py", ix, ix);
2426       if(h1->Integral()<50) continue;
2427       h1->Fit(f, "QN");
2428
2429       GetLandauMpvFwhm(f, mpv, xm, xM);
2430       Int_t ip = gm->GetN();
2431       gm->SetPoint(ip, x, k*mpv);
2432       gm->SetPointError(ip, 0., 0., k*xm, k*xM);
2433       gs->SetPoint(ip, r, k*(xM-xm)/mpv);
2434       gs->SetPointError(ip, 0., 0.);
2435     }
2436   }
2437
2438   return kTRUE;
2439 }
2440
2441 //________________________________________________________
2442 Bool_t AliTRDresolution::Process2Darray(ETRDresolutionPlot plot, Int_t idx, TF1 *f, Float_t k)
2443 {
2444   //
2445   // Do the processing
2446   //
2447
2448   if(!fContainer || !fGraphS || !fGraphM) return kFALSE;
2449
2450   // retrive containers
2451   TObjArray *arr = (TObjArray*)(fContainer->At(plot));
2452   if(!arr) return kFALSE;
2453   AliDebug(2, Form("p[%d] idx[%d] arr[%s]", plot, idx, arr->GetName()));
2454
2455   TObjArray *gm, *gs;
2456   if(!(gm = (TObjArray*)((TObjArray*)(fGraphM->At(plot)))->At(idx))) return kFALSE;
2457   if(!(gs = (TObjArray*)((TObjArray*)(fGraphS->At(plot)))->At(idx))) return kFALSE;
2458
2459   TGraphErrors *g[2]; TH2I *h2(NULL); TObjArray *a0(NULL);
2460   for(Int_t ia(0); ia<arr->GetEntriesFast(); ia++){
2461     if(!(a0 = (TObjArray*)arr->At(ia))) continue;
2462
2463     if(!(h2 = (TH2I*)a0->At(idx))) return kFALSE;
2464     if(Int_t(h2->GetEntries())){ 
2465       AliDebug(4, Form("   idx[%d] h[%s] %s", ia, h2->GetName(), h2->GetTitle()));
2466     } else {
2467       AliDebug(2, Form("   idx[%d] : Missing entries.", ia));
2468       continue;
2469     }
2470
2471     if(!(g[0] = (TGraphErrors*)gm->At(ia))) return kFALSE;
2472     if(!(g[1] = (TGraphErrors*)gs->At(ia))) return kFALSE;
2473     if(!Process(h2, f, k, g)) return kFALSE;
2474   }
2475
2476   return kTRUE;
2477 }
2478
2479 //________________________________________________________
2480 Bool_t AliTRDresolution::Process3Darray(ETRDresolutionPlot plot, Int_t idx, TF1 *f, Float_t k)
2481 {
2482   //
2483   // Do the processing
2484   //
2485
2486   if(!fContainer || !fGraphS || !fGraphM) return kFALSE;
2487   //printf("Process4D : processing plot[%d] idx[%d]\n", plot, idx);
2488
2489   // retrive containers
2490   TObjArray *arr = (TObjArray*)(fContainer->At(plot));
2491   if(!arr) return kFALSE;
2492   AliDebug(2, Form("p[%d] idx[%d] arr[%s]", plot, idx, arr->GetName()));
2493
2494   TObjArray *gm, *gs;
2495   if(!(gm = (TObjArray*)((TObjArray*)(fGraphM->At(plot)))->At(idx))) return kFALSE;
2496   if(!(gs = (TObjArray*)((TObjArray*)(fGraphS->At(plot)))->At(idx))) return kFALSE;
2497
2498   TGraphErrors *g[2]; TH3S *h3(NULL); TObjArray *a0(NULL);
2499   Int_t in(0);
2500   for(Int_t ia(0); ia<arr->GetEntriesFast(); ia++){
2501     if(!(a0 = (TObjArray*)arr->At(ia))) continue;
2502
2503     if(!(h3 = (TH3S*)a0->At(idx))) return kFALSE;
2504      if(Int_t(h3->GetEntries())){ 
2505        AliDebug(4, Form("   idx[%d] h[%s] %s", ia, h3->GetName(), h3->GetTitle()));
2506     } else {
2507       AliDebug(2, Form("   idx[%d] : Missing entries.", ia));
2508       continue;
2509     }
2510     TAxis *az = h3->GetZaxis();
2511     for(Int_t iz=1; iz<=az->GetNbins(); iz++, in++){
2512       if(in >= gm->GetEntriesFast()) break;
2513       if(!(g[0] = (TGraphErrors*)gm->At(in))) return kFALSE;
2514       if(!(g[1] = (TGraphErrors*)gs->At(in))) return kFALSE;
2515       az->SetRange(iz, iz);
2516       if(!Process((TH2*)h3->Project3D("yx"), f, k, g)) return kFALSE;
2517     }
2518   }
2519   AliDebug(2, Form("Projections [%d] from [%d]", in, gs->GetEntriesFast()));
2520
2521   return kTRUE;
2522 }
2523
2524 //________________________________________________________
2525 Bool_t AliTRDresolution::Process3DlinkedArray(ETRDresolutionPlot plot, Int_t idx, TF1 *f, Float_t k)
2526 {
2527   //
2528   // Do the processing
2529   //
2530
2531   if(!fContainer || !fGraphS || !fGraphM) return kFALSE;
2532   //printf("Process4D : processing plot[%d] idx[%d]\n", plot, idx);
2533
2534   // retrive containers
2535   TObjArray *arr = (TObjArray*)(fContainer->At(plot));
2536   if(!arr) return kFALSE;
2537   AliDebug(2, Form("p[%d] idx[%d] arr[%s]", plot, idx, arr->GetName()));
2538
2539   TObjArray *gm, *gs;
2540   if(!(gm = (TObjArray*)((TObjArray*)(fGraphM->At(plot)))->At(idx))) return kFALSE;
2541   if(!(gs = (TObjArray*)((TObjArray*)(fGraphS->At(plot)))->At(idx))) return kFALSE;
2542
2543   TGraphErrors *g[2]; TH3S *h3(NULL); TObjArray *a0(NULL);
2544   Int_t in(0);
2545   for(Int_t ia(0); ia<arr->GetEntriesFast(); ia++){
2546     if(!(a0 = (TObjArray*)arr->At(ia))) continue;
2547     if(!(h3 = (TH3S*)a0->At(idx))) return kFALSE;
2548     if(Int_t(h3->GetEntries())){     
2549       AliDebug(4, Form("   idx[%d] h[%s] %s", ia, h3->GetName(), h3->GetTitle()));
2550     } else {
2551       AliDebug(2, Form("   idx[%d] : Missing entries.", ia));
2552       continue;
2553     }
2554     if(!(g[0] = (TGraphErrors*)gm->At(in))) return kFALSE;
2555     if(!(g[1] = (TGraphErrors*)gs->At(in))) return kFALSE;
2556     if(!Process((TH2*)h3->Project3D("yx"), f, k, g)) return kFALSE;
2557     in++;
2558
2559     if(!(g[0] = (TGraphErrors*)gm->At(in))) return kFALSE;
2560     if(!(g[1] = (TGraphErrors*)gs->At(in))) return kFALSE;
2561     if(!Process((TH2*)h3->Project3D("zx"), f, k, g)) return kFALSE;
2562     in++;
2563   }
2564   AliDebug(2, Form("Projections [%d] from [%d]", in, gs->GetEntriesFast()));
2565
2566   return kTRUE;
2567 }
2568
2569 //________________________________________________________
2570 Bool_t AliTRDresolution::GetGraph(Float_t *bb, ETRDresolutionPlot ip, Int_t idx, Bool_t kLEG, const Char_t *explain)
2571 {
2572   //
2573   // Get the graphs
2574   //
2575
2576   if(!fGraphS || !fGraphM) return kFALSE;
2577   // axis titles look up
2578   Int_t nref = 0;
2579   for(Int_t jp=0; jp<(Int_t)ip; jp++) nref+=fgNproj[jp];
2580   UChar_t jdx = idx<0?0:idx;
2581   for(Int_t jc=0; jc<TMath::Min(jdx,fgNproj[ip]-1); jc++) nref++;
2582   Char_t **at = fAxTitle[nref];
2583
2584   // build legends if requiered
2585   TLegend *leg(NULL);
2586   if(kLEG){
2587     leg=new TLegend(.65, .6, .95, .9);
2588     leg->SetBorderSize(0);
2589     leg->SetFillStyle(0);
2590   }
2591   // build frame
2592   TH1S *h1(NULL);
2593   h1 = new TH1S(Form("h1TF_%02d", fIdxFrame++), Form("%s %s;%s;%s", at[0], explain?explain:"", at[1], at[2]), 2, bb[0], bb[2]);
2594   h1->SetMinimum(bb[1]);h1->SetMaximum(bb[3]);
2595   h1->SetLineColor(kBlack); h1->SetLineWidth(1);h1->SetLineStyle(2); 
2596   // axis range
2597   TAxis *ax = h1->GetXaxis();
2598   ax->CenterTitle();ax->SetMoreLogLabels();ax->SetTitleOffset(1.2);
2599   ax = h1->GetYaxis();
2600   ax->SetRangeUser(bb[1], bb[3]);
2601   ax->CenterTitle(); ax->SetTitleOffset(1.4);
2602   h1->Draw();
2603   // bounding box
2604   TBox *b = new TBox(-.15, bb[1], .15, bb[3]);
2605   b->SetFillStyle(3002);b->SetLineColor(0);
2606   b->SetFillColor(ip<=Int_t(kMCcluster)?kGreen:kBlue);
2607   b->Draw();
2608
2609   TGraphErrors *gm = idx<0 ? (TGraphErrors*)fGraphM->At(ip) : (TGraphErrors*)((TObjArray*)(fGraphM->At(ip)))->At(idx);
2610   if(!gm) return kFALSE;
2611   TGraphErrors *gs = idx<0 ? (TGraphErrors*)fGraphS->At(ip) : (TGraphErrors*)((TObjArray*)(fGraphS->At(ip)))->At(idx);
2612   if(!gs) return kFALSE;
2613
2614   Int_t n(0), nPlots(0);
2615   if((n=gm->GetN())) {
2616     nPlots++;
2617     gm->Draw("pl"); if(leg) leg->AddEntry(gm, gm->GetTitle(), "pl");
2618     PutTrendValue(Form("%s_%s", fgPerformanceName[ip], at[0]), gm->GetMean(2));
2619     PutTrendValue(Form("%s_%sRMS", fgPerformanceName[ip], at[0]), gm->GetRMS(2));
2620   }
2621
2622   if((n=gs->GetN())){
2623     nPlots++;
2624     gs->Draw("pl"); if(leg) leg->AddEntry(gs, gs->GetTitle(), "pl");
2625     gs->Sort(&TGraph::CompareY);
2626     PutTrendValue(Form("%s_%sSigMin", fgPerformanceName[ip], at[0]), gs->GetY()[0]);
2627     PutTrendValue(Form("%s_%sSigMax", fgPerformanceName[ip], at[0]), gs->GetY()[n-1]);
2628     gs->Sort(&TGraph::CompareX); 
2629   }
2630   if(!nPlots) return kFALSE;
2631   if(leg) leg->Draw();
2632   return kTRUE;
2633 }
2634
2635 //________________________________________________________
2636 Bool_t AliTRDresolution::GetGraphArray(Float_t *bb, ETRDresolutionPlot ip, Int_t idx, Bool_t kLEG, Int_t n, Int_t *sel, const Char_t *explain)
2637 {
2638   //
2639   // Get the graphs
2640   //
2641
2642   if(!fGraphS || !fGraphM) return kFALSE;
2643
2644   // axis titles look up
2645   Int_t nref(0);
2646   for(Int_t jp(0); jp<ip; jp++) nref+=fgNproj[jp];
2647   nref+=idx;
2648   Char_t **at = fAxTitle[nref];
2649
2650   // build legends if requiered
2651   TLegend *legM(NULL), *legS(NULL);
2652   if(kLEG){
2653     legM=new TLegend(.35, .6, .65, .9);
2654     legM->SetHeader("Mean");
2655     legM->SetBorderSize(0);
2656     legM->SetFillStyle(0);
2657     legS=new TLegend(.65, .6, .95, .9);
2658     legS->SetHeader("Sigma");
2659     legS->SetBorderSize(0);
2660     legS->SetFillStyle(0);
2661   }
2662   // build frame
2663   TH1S *h1(NULL);
2664   h1 = new TH1S(Form("h1TF_%02d", fIdxFrame++), Form("%s %s;%s;%s", at[0], explain?explain:"", at[1], at[2]), 2, bb[0], bb[2]);
2665   h1->SetMinimum(bb[1]);h1->SetMaximum(bb[3]);
2666   h1->SetLineColor(kBlack); h1->SetLineWidth(1);h1->SetLineStyle(2); 
2667   // axis range
2668   TAxis *ax = h1->GetXaxis();
2669   ax->CenterTitle();ax->SetMoreLogLabels();ax->SetTitleOffset(1.2);
2670   ax = h1->GetYaxis();
2671   ax->SetRangeUser(bb[1], bb[3]);
2672   ax->CenterTitle(); ax->SetTitleOffset(1.4);
2673   h1->Draw();
2674
2675   TGraphErrors *gm(NULL), *gs(NULL);
2676   TObjArray *a0(NULL), *a1(NULL);
2677   a0 = (TObjArray*)((TObjArray*)fGraphM->At(ip))->At(idx);
2678   a1 = (TObjArray*)((TObjArray*)fGraphS->At(ip))->At(idx);
2679   if(!n) n=a0->GetEntriesFast();
2680   AliDebug(4, Form("Graph : Ref[%d] Title[%s] Limits{x[%f %f] y[%f %f]} Comp[%d] Selection[%c]", nref, at[0], bb[0], bb[2], bb[1], bb[3], n, sel ? 'y' : 'n'));
2681   Int_t nn(0), nPlots(0);
2682   for(Int_t is(0), is0(0); is<n; is++){
2683     is0 = sel ? sel[is] : is;
2684     if(!(gs =  (TGraphErrors*)a1->At(is0))) return kFALSE;
2685     if(!(gm =  (TGraphErrors*)a0->At(is0))) return kFALSE;
2686
2687     if((nn=gs->GetN())){
2688       nPlots++;
2689       gs->Draw("pc"); 
2690       if(legS){ 
2691         //printf("LegEntry %s [%s]%s\n", at[0], gs->GetName(), gs->GetTitle());
2692         legS->AddEntry(gs, gs->GetTitle(), "pl");
2693       }
2694       gs->Sort(&TGraph::CompareY);
2695       PutTrendValue(Form("%s_%sSigMin", fgPerformanceName[kMCtrack], at[0]), gs->GetY()[0]);
2696       PutTrendValue(Form("%s_%sSigMax", fgPerformanceName[kMCtrack], at[0]), gs->GetY()[nn-1]);
2697       gs->Sort(&TGraph::CompareX); 
2698     }
2699     if(gm->GetN()){
2700       nPlots++;
2701       gm->Draw("pc");
2702       if(legM){ 
2703         //printf("LegEntry %s [%s]%s\n", at[0], gm->GetName(), gm->GetTitle());
2704         legM->AddEntry(gm, gm->GetTitle(), "pl");
2705       }
2706       PutTrendValue(Form("%s_%s", fgPerformanceName[kMCtrack], at[0]), gm->GetMean(2));
2707       PutTrendValue(Form("%s_%sRMS", fgPerformanceName[kMCtrack], at[0]), gm->GetRMS(2));
2708     }
2709   }
2710   if(!nPlots) return kFALSE;
2711   if(kLEG){
2712     legM->Draw();
2713     legS->Draw();
2714   }
2715   return kTRUE;
2716 }
2717
2718 //________________________________________________________
2719 void AliTRDresolution::GetLandauMpvFwhm(TF1 * const f, Float_t &mpv, Float_t &xm, Float_t &xM)
2720 {
2721   //
2722   // Get the most probable value and the full width half mean 
2723   // of a Landau distribution
2724   //
2725
2726   const Float_t dx = 1.;
2727   mpv = f->GetParameter(1);
2728   Float_t fx, max = f->Eval(mpv);
2729
2730   xm = mpv - dx;
2731   while((fx = f->Eval(xm))>.5*max){
2732     if(fx>max){ 
2733       max = fx;
2734       mpv = xm;
2735     }
2736     xm -= dx;
2737   }
2738
2739   xM += 2*(mpv - xm);
2740   while((fx = f->Eval(xM))>.5*max) xM += dx;
2741 }
2742
2743
2744 //________________________________________________________
2745 void AliTRDresolution::SetRecoParam(AliTRDrecoParam *r)
2746 {
2747
2748   fReconstructor->SetRecoParam(r);
2749 }
2750
2751
2752 //________________________________________________________
2753 void AliTRDresolution::SetSegmentationLevel(Int_t l) 
2754 {
2755 // Setting the segmentation level to "l"
2756   fSegmentLevel = l;
2757
2758   UShort_t const lNcomp[kNprojs] = {
2759     1,  1, //2, 
2760     fgkNresYsegm[fSegmentLevel], 2, //2, 
2761     2*fgkNresYsegm[fSegmentLevel], 2, 2, 2, 1, //5, 
2762     2*fgkNresYsegm[fSegmentLevel], 2, 2, 2, 1, //5,
2763     2*fgkNresYsegm[fSegmentLevel], 2, 2, 2, 1, //5,
2764   // MC
2765     fgkNresYsegm[fSegmentLevel], 2,          //2, 
2766     fgkNresYsegm[fSegmentLevel], 2, 2, 2, 1, //5, 
2767     fgkNresYsegm[fSegmentLevel], 2, 2, 2, 1, 1, 1, 1, 11, 11, 11, //11
2768     fgkNresYsegm[fSegmentLevel], 2, 2, 2, 1, 1, 1, 1, 11, 11, 11, //11
2769     6*fgkNresYsegm[fSegmentLevel], 6*2, 6*2, 6*2, 6, 6, 6, 6, 6*11, 6*11, 6*11  //11
2770   };
2771   memcpy(fNcomp, lNcomp, kNprojs*sizeof(UShort_t));
2772
2773   Char_t const *lAxTitle[kNprojs][4] = {
2774     // Charge
2775     {"Impv", "x [cm]", "I_{mpv}", "x/x_{0}"}
2776   ,{"dI/Impv", "x/x_{0}", "#delta I/I_{mpv}", "x[cm]"}
2777     // Clusters to Kalman
2778   ,{"Cluster2Track residuals", "tg(#phi)", "y [#mum]", "#sigma_{y} [#mum]"}
2779   ,{"Cluster2Track  YZ pulls", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2780     // TRD tracklet to Kalman fit
2781   ,{"Tracklet2Track Y residuals", "tg(#phi)", "y [#mum]", "#sigma_{y} [#mum]"}
2782   ,{"Tracklet2Track YZ pulls", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2783   ,{"Tracklet2Track Z residuals", "tg(#theta)", "z [#mum]", "#sigma_{z} [#mum]"}
2784   ,{"Tracklet2Track Z pulls", "tg(#theta)", "z", "#sigma_{z}"}
2785   ,{"Tracklet2Track Phi residuals", "tg(#phi)", "#phi [mrad]", "#sigma_{#phi} [mrad]"}
2786     // TRDin 2 first TRD tracklet
2787   ,{"Tracklet2Track Y residuals @ TRDin", "tg(#phi)", "y [#mum]", "#sigma_{y} [#mum]"}
2788   ,{"Tracklet2Track YZ pulls @ TRDin", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2789   ,{"Tracklet2Track Z residuals @ TRDin", "tg(#theta)", "z [#mum]", "#sigma_{z} [#mum]"}
2790   ,{"Tracklet2Track Z pulls @ TRDin", "tg(#theta)", "z", "#sigma_{z}"}
2791   ,{"Tracklet2Track Phi residuals @ TRDin", "tg(#phi)", "#phi [mrad]", "#sigma_{#phi} [mrad]"}
2792     // TRDout 2 first TRD tracklet
2793   ,{"Tracklet2Track Y residuals @ TRDout", "tg(#phi)", "y [#mum]", "#sigma_{y} [#mum]"}
2794   ,{"Tracklet2Track YZ pulls @ TRDout", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2795   ,{"Tracklet2Track Z residuals @ TRDout", "tg(#theta)", "z [#mum]", "#sigma_{z} [#mum]"}
2796   ,{"Tracklet2Track Z pulls @ TRDout", "tg(#theta)", "z", "#sigma_{z}"}
2797   ,{"Tracklet2Track Phi residuals @ TRDout", "tg(#phi)", "#phi [mrad]", "#sigma_{#phi} [mrad]"}
2798     // MC cluster
2799   ,{"MC Cluster Y resolution", "tg(#phi)", "y [#mum]", "#sigma_{y} [#mum]"}
2800   ,{"MC Cluster YZ pulls", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2801     // MC tracklet
2802   ,{"MC Tracklet Y resolution", "tg(#phi)", "y [#mum]",  "#sigma_{y}[#mum]"}
2803   ,{"MC Tracklet YZ pulls", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2804   ,{"MC Tracklet Z resolution", "tg(#theta)", "z [#mum]", "#sigma_{z} [#mum]"}
2805   ,{"MC Tracklet Z pulls", "tg(#theta)", "z", "#sigma_{z}"}
2806   ,{"MC Tracklet Phi resolution", "tg(#phi)", "#phi [mrad]", "#sigma_{#phi} [mrad]"}
2807     // MC track TRDin
2808   ,{"MC Y resolution @ TRDin", "tg(#phi)", "y [#mum]", "#sigma_{y}[#mum]"}
2809   ,{"MC YZ pulls @ TRDin", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2810   ,{"MC Z resolution @ TRDin", "tg(#theta)", "z [#mum]", "#sigma_{z} [#mum]"}
2811   ,{"MC Z pulls @ TRDin", "tg(#theta)", "z", "#sigma_{z}"}
2812   ,{"MC #Phi resolution @ TRDin", "tg(#phi)", "#phi [mrad]", "#sigma_{#phi} [mrad]"}
2813   ,{"MC SNP pulls @ TRDin", "tg(#phi)", "SNP", "#sigma_{snp}"}
2814   ,{"MC #Theta resolution @ TRDin", "tg(#theta)", "#theta [mrad]", "#sigma_{#theta} [mrad]"}
2815   ,{"MC TGL pulls @ TRDin", "tg(#theta)", "TGL", "#sigma_{tgl}"}
2816   ,{"MC P_{t} resolution @ TRDin", "p_{t}^{MC} [GeV/c]", "(p_{t}^{REC}-p_{t}^{MC})/p_{t}^{MC} [%]", "MC: #sigma^{TPC}(#Deltap_{t}/p_{t}^{MC}) [%]"}
2817   ,{"MC 1/P_{t} pulls @ TRDin", "1/p_{t}^{MC} [c/GeV]", "1/p_{t}^{REC}-1/p_{t}^{MC}", "MC PULL: #sigma_{1/p_{t}}^{TPC}"}
2818   ,{"MC P resolution @ TRDin", "p^{MC} [GeV/c]", "(p^{REC}-p^{MC})/p^{MC} [%]", "MC: #sigma^{TPC}(#Deltap/p^{MC}) [%]"}
2819     // MC track TRDout
2820   ,{"MC Y resolution @ TRDout", "tg(#phi)", "y [#mum]", "#sigma_{y}[#mum]"}
2821   ,{"MC YZ pulls @ TRDout", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2822   ,{"MC Z resolution @ TRDout", "tg(#theta)", "z [#mum]", "#sigma_{z} [#mum]"}
2823   ,{"MC Z pulls @ TRDout", "tg(#theta)", "z", "#sigma_{z}"}
2824   ,{"MC #Phi resolution @ TRDout", "tg(#phi)", "#phi [mrad]", "#sigma_{#phi} [mrad]"}
2825   ,{"MC SNP pulls @ TRDout", "tg(#phi)", "SNP", "#sigma_{snp}"}
2826   ,{"MC #Theta resolution @ TRDout", "tg(#theta)", "#theta [mrad]", "#sigma_{#theta} [mrad]"}
2827   ,{"MC TGL pulls @ TRDout", "tg(#theta)", "TGL", "#sigma_{tgl}"}
2828   ,{"MC P_{t} resolution @ TRDout", "p_{t}^{MC} [GeV/c]", "(p_{t}^{REC}-p_{t}^{MC})/p_{t}^{MC} [%]", "MC: #sigma^{TPC}(#Deltap_{t}/p_{t}^{MC}) [%]"}
2829   ,{"MC 1/P_{t} pulls @ TRDout", "1/p_{t}^{MC} [c/GeV]", "1/p_{t}^{REC}-1/p_{t}^{MC}", "MC PULL: #sigma_{1/p_{t}}^{TPC}"}
2830   ,{"MC P resolution @ TRDout", "p^{MC} [GeV/c]", "(p^{REC}-p^{MC})/p^{MC} [%]", "MC: #sigma^{TPC}(#Deltap/p^{MC}) [%]"}
2831     // MC track in TRD
2832   ,{"MC Track Y resolution", "tg(#phi)", "y [#mum]", "#sigma_{y} [#mum]"}
2833   ,{"MC Track YZ pulls", fgkResYsegmName[fSegmentLevel], "y / z", "#sigma_{y}"}
2834   ,{"MC Track Z resolution", "tg(#theta)", "z [#mum]", "#sigma_{z} [#mum]"}
2835   ,{"MC Track Z pulls", "tg(#theta)", "z", "#sigma_{z}"}
2836   ,{"MC Track #Phi resolution", "tg(#phi)", "#phi [mrad]", "#sigma_{#phi} [mrad]"}
2837   ,{"MC Track SNP pulls", "tg(#phi)", "SNP", "#sigma_{snp}"}
2838   ,{"MC Track #Theta resolution", "tg(#theta)", "#theta [mrad]", "#sigma_{#theta} [mrad]"}
2839   ,{"MC Track TGL pulls", "tg(#theta)", "TGL", "#sigma_{tgl}"}
2840   ,{"MC P_{t} resolution", "p_{t} [GeV/c]", "(p_{t}^{REC}-p_{t}^{MC})/p_{t}^{MC} [%]", "#sigma(#Deltap_{t}/p_{t}^{MC}) [%]"}
2841   ,{"MC 1/P_{t} pulls", "1/p_{t}^{MC} [c/GeV]", "1/p_{t}^{REC} - 1/p_{t}^{MC}", "#sigma_{1/p_{t}}"}
2842   ,{"MC P resolution", "p [GeV/c]", "(p^{REC}-p^{MC})/p^{MC} [%]", "#sigma(#Deltap/p^{MC}) [%]"}
2843   };
2844   memcpy(fAxTitle, lAxTitle, 4*kNprojs*sizeof(Char_t*));
2845 }