]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDclusterResolution.cxx
add wrapper to TESTBIT macro to select TRD tasks
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDclusterResolution.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: AliTRDclusterResolution.cxx */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  TRD cluster error parameterization                                        //
21 //                                                                           //
22 // This class is designed to produce the reference plots for a detailed study//
23 // and parameterization of TRD cluster errors. The following effects are taken//
24 // into account :                                                            //
25 //   - dependence with the total charge of the cluster                       //
26 //   - dependence with the distance from the center pad. This is monitored 
27 // for each layer individually since the pad size varies with layer
28 //   - dependence with the drift length - here the influence of anisochronity 
29 // and diffusion are searched
30 //   - dependence with the distance to the anode wire - anisochronity effects
31 //   - dependence with track angle (for y resolution)
32 // The correlation between effects is taken into account. 
33 // 
34 // Since magnetic field plays a very important role in the TRD measurement 
35 // the ExB correction is forced by the setter function SetExB(Int_t). The 
36 // argument is the detector index, if none is specified all will be 
37 // considered.
38 // 
39 // Two cases are of big importance.
40 //   - comparison with MC
41 //   - comparison with Kalman fit. In this case the covariance matrix of the
42 // Kalman fit are needed.
43 // 
44 // The functionalities implemented in this class are based on the storage 
45 // class AliTRDclusterInfo.
46 // 
47 // The Method
48 // ----------
49 // 
50 // The method to disentangle s_y and s_x is based on the relation (see also fig.)
51 // BEGIN_LATEX
52 // #sigma^{2} = #sigma^{2}_{y} + tg^{2}(#alpha_{L})*#sigma^{2}_{x_{d}} + tg^{2}(#phi-#alpha_{L})*(#sigma^{2}_{x_{d}}+#sigma^{2}_{x_{c}})
53 // END_LATEX
54 // with
55 // BEGIN_LATEX
56 // #sigma^{2}_{x_{c}} #approx 0 
57 // END_LATEX
58 // we suppose the chamber is well calibrated for t_{0} and aligned in
59 // radial direction. 
60 //
61 // Clusters can be radially shifted due to three causes:
62 //   - globally shifted - due to residual misalignment/miscalibration(t0)
63 //   - locally shifted - due to different local drift velocity from the mean
64 //   - randomly shifted - due to neighboring (radial direction) clusters 
65 // charge induced by asymmetry of the TRF.
66 //
67 // We estimate this effects by the relations:
68 // BEGIN_LATEX
69 // #mu_{y} = tg(#alpha_{L})*#Delta x_{d}(...) + tg(#phi-#alpha_{L})*(#Delta x_{c}(...) + #Delta x_{d}(...))
70 // END_LATEX
71 // where
72 // BEGIN_LATEX
73 // #Delta x_{d}(...) = (<v_{d}> + #delta v_{d}(x_{d}, d)) * (t + t^{*}(Q))
74 // END_LATEX
75 // and we specified explicitely the variation of drift velocity parallel 
76 // with the track (x_{d}) and perpendicular to it due to anisochronity (d).
77 // 
78 // For estimating the contribution from asymmetry of TRF the following
79 // parameterization is being used
80 // BEGIN_LATEX
81 // t^{*}(Q) = #delta_{0} * #frac{Q_{t+1} - Q_{t-1}}{Q_{t-1} + Q_{t} + Q_{t+1}}
82 // END_LATEX
83 //
84 //
85 // Clusters can also be r-phi shifted due to:
86 //   - wrong PRF or wrong cuts at digits level
87 //The following correction is applied :
88 // BEGIN_LATEX
89 // <#Delta y> = a + b * sin(c*y_{pw})
90 // END_LATEX
91
92 // The Models
93 //
94 //   Parameterization against total charge
95 //
96 // Obtained for B=0T at phi=0. All other effects integrated out.
97 // BEGIN_LATEX
98 // #sigma^{2}_{y}(Q) = #sigma^{2}_{y}(...) + b(#frac{1}{Q} - #frac{1}{Q_{0}}) 
99 // END_LATEX
100 // For B diff 0T the error of the average ExB correction error has to be subtracted !! 
101 //
102 //   Parameterization Sx
103 //
104 // The parameterization of the error in the x direction can be written as
105 // BEGIN_LATEX
106 // #sigma_{x} = #sigma_{x}^{||} + #sigma_{x}^{#perp}
107 // END_LATEX
108 //
109 // where the parallel component is given mainly by the TRF width while 
110 // the perpendicular component by the anisochronity. The model employed for 
111 // the parallel is gaus(0)+expo(3) with the following parameters
112 // 1  C   5.49018e-01   1.23854e+00   3.84540e-04  -8.21084e-06
113 // 2  M   7.82999e-01   6.22531e-01   2.71272e-04  -6.88485e-05
114 // 3  S   2.74451e-01   1.13815e+00   2.90667e-04   1.13493e-05
115 // 4  E1  2.53596e-01   1.08646e+00   9.95591e-05  -2.11625e-05
116 // 5  E2 -2.40078e-02   4.26520e-01   4.67153e-05  -2.35392e-04
117 //
118 // and perpendicular to the track is pol2 with the parameters
119 //
120 // Par_0 = 0.190676 +/- 0.41785
121 // Par_1 = -3.9269  +/- 7.49862
122 // Par_2 = 14.7851  +/- 27.8012
123 //
124 //   Parameterization Sy
125 //
126 // The parameterization of the error in the y direction along track uses
127 // BEGIN_LATEX
128 // #sigma_{y}^{||} = #sigma_{y}^{0} -a*exp(1/(x-b))
129 // END_LATEX
130 //
131 // with following values for the parameters:
132 // 1  sy0 2.60967e-01   2.99652e-03   7.82902e-06  -1.89636e-04
133 // 2  a  -7.68941e+00   1.87883e+00   3.84539e-04   9.38268e-07
134 // 3  b  -3.41160e-01   7.72850e-02   1.63231e-05   2.51602e-05
135 //
136 //==========================================================================
137 // Example how to retrive reference plots from the task
138 // void steerClErrParam(Int_t fig=0)
139 // {
140 //   gSystem->Load("libANALYSIS.so");
141 //   gSystem->Load("libTRDqaRec.so");
142 // 
143 //   // initialize DB manager
144 //   AliCDBManager *cdb = AliCDBManager::Instance();
145 //   cdb->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
146 //   cdb->SetRun(0);
147 //   // initialize magnetic field.
148 //   AliMagFCheb *field=new AliMagFCheb("Maps","Maps", 2, 1., 10., AliMagFCheb::k5kG);
149 //   AliTracker::SetFieldMap(field, kTRUE);
150 // 
151 //   AliTRDclusterResolution *res = new AliTRDclusterResolution();
152 //   res->SetMCdata();
153 //   res->Load("TRD.TaskClErrParam.root");
154 //   res->SetExB();  
155 //   res->SetVisual(); 
156 //   //res->SetSaveAs();
157 //   res->SetProcessCharge(kFALSE);
158 //   res->SetProcessCenterPad(kFALSE);
159 //   //res->SetProcessMean(kFALSE);
160 //   res->SetProcessSigma(kFALSE);
161 //   if(!res->PostProcess()) return;
162 //   new TCanvas;
163 //   res->GetRefFigure(fig);
164 // }
165 //
166 //  Authors:                                                              //
167 //    Alexandru Bercuci <A.Bercuci@gsi.de>                                //
168 ////////////////////////////////////////////////////////////////////////////
169
170 #include "AliTRDclusterResolution.h"
171 #include "info/AliTRDclusterInfo.h"
172 #include "AliTRDgeometry.h"
173 #include "AliTRDpadPlane.h"
174 #include "AliTRDcluster.h"
175 #include "AliTRDseedV1.h"
176 #include "AliTRDcalibDB.h"
177 #include "AliTRDCommonParam.h"
178 #include "Cal/AliTRDCalROC.h"
179 #include "Cal/AliTRDCalDet.h"
180
181 #include "AliESDEvent.h"
182 #include "AliCDBManager.h"
183
184 #include "TROOT.h"
185 #include "TObjArray.h"
186 #include "TAxis.h"
187 #include "TF1.h"
188 #include "TLegend.h"
189 #include "TGraphErrors.h"
190 #include "TLine.h"
191 #include "TH2I.h"
192 #include "TH3S.h"
193 #include "TTree.h"
194 #include "TMath.h"
195 #include "TLinearFitter.h"
196 #include "TGeoGlobalMagField.h"
197
198 #include "TCanvas.h"
199 #include "TSystem.h"
200
201 ClassImp(AliTRDclusterResolution)
202
203 const Float_t AliTRDclusterResolution::fgkTimeBinLength = 1./ AliTRDCommonParam::Instance()->GetSamplingFrequency();
204 //_______________________________________________________
205 AliTRDclusterResolution::AliTRDclusterResolution()
206   : AliTRDrecoTask()
207   ,fCanvas(NULL)
208   ,fInfo(NULL)
209   ,fResults(NULL)
210   ,fStatus(0)
211   ,fDet(-1)
212   ,fCol(-1)
213   ,fRow(-1)
214   ,fExB(0.)
215   ,fVdrift(1.5)
216   ,fT0(0.)
217   ,fGain(1.)
218   ,fDyRange(1.3)
219   ,fLy(0)
220   ,fT(0.)
221   ,fX(0.)
222   ,fY(0.)
223   ,fZ(0.)
224 {
225 // Constructor
226   SetNameTitle("ClErrCalib", "Cluster Error Parameterization");
227   memset(fR, 0, 4*sizeof(Float_t));
228   memset(fP, 0, 4*sizeof(Float_t));
229 }
230
231 //_______________________________________________________
232 AliTRDclusterResolution::AliTRDclusterResolution(const char *name)
233   : AliTRDrecoTask(name, "Cluster Error Parameterization")
234   ,fCanvas(NULL)
235   ,fInfo(NULL)
236   ,fResults(NULL)
237   ,fStatus(0)
238   ,fDet(-1)
239   ,fCol(-1)
240   ,fRow(-1)
241   ,fExB(0.)
242   ,fVdrift(1.5)
243   ,fT0(0.)
244   ,fGain(1.)
245   ,fDyRange(1.3)
246   ,fLy(0)
247   ,fT(0.)
248   ,fX(0.)
249   ,fY(0.)
250   ,fZ(0.)
251 {
252 // Constructor
253
254   memset(fR, 0, 4*sizeof(Float_t));
255   memset(fP, 0, 4*sizeof(Float_t));
256
257   // By default register all analysis
258   // The user can switch them off in his steering macro
259   SetProcess(kQRes);
260   SetProcess(kCenter);
261   SetProcess(kMean);
262   SetProcess(kSigm);
263 }
264
265 //_______________________________________________________
266 AliTRDclusterResolution::~AliTRDclusterResolution()
267 {
268 // Destructor
269
270   if(fCanvas) delete fCanvas;
271   if(fResults){
272     fResults->Delete();
273     delete fResults;
274   }
275 }
276
277 //_______________________________________________________
278 void AliTRDclusterResolution::UserCreateOutputObjects()
279 {
280   fContainer = Histos();
281   PostData(1, fContainer);
282 }
283
284 //_______________________________________________________
285 Bool_t AliTRDclusterResolution::GetRefFigure(Int_t ifig)
286 {
287 // Steering function to retrieve performance plots
288
289   if(!fResults) return kFALSE;
290   TLegend *leg = NULL;
291   TList *l = NULL;
292   TObjArray *arr = NULL;
293   TTree *t = NULL;
294   TH2 *h2 = NULL;TH1 *h1 = NULL;
295   TGraphErrors *gm(NULL), *gs(NULL), *gp(NULL);
296   switch(ifig){
297   case kQRes:
298     if(!(arr = (TObjArray*)fResults->At(kQRes))) break;
299     if(!(gm = (TGraphErrors*)arr->At(0))) break;
300     if(!(gs = (TGraphErrors*)arr->At(1))) break;
301     if(!(gp = (TGraphErrors*)arr->At(2))) break;
302     leg= new TLegend(.7, .7, .9, .95);
303     leg->SetBorderSize(0); leg->SetFillColor(0); leg->SetFillStyle(0);
304     gs->Draw("apl"); leg->AddEntry(gs, "Sigma / Resolution", "pl");
305     gs->GetHistogram()->GetYaxis()->SetRangeUser(-50., 700.);
306     gs->GetHistogram()->SetXTitle("Q [a.u.]");
307     gs->GetHistogram()->SetYTitle("y - x tg(#alpha_{L}) [#mum]");
308     gm->Draw("pl");leg->AddEntry(gm, "Mean / Systematics", "pl");
309     gp->Draw("pl");leg->AddEntry(gp, "Abundance / Probability", "pl");
310     leg->Draw();
311     return kTRUE;
312   case kCenter:
313     if(!(arr = (TObjArray*)fResults->At(kCenter))) break;
314     gPad->Divide(2, 1); l = gPad->GetListOfPrimitives();
315     ((TVirtualPad*)l->At(0))->cd();
316     ((TTree*)arr->At(0))->Draw(Form("y:t>>h(%d, -0.5, %f, 51, -.51, .51)",AliTRDseedV1::kNtb, AliTRDseedV1::kNtb-0.5),
317             "m[0]*(ly==0&&abs(m[0])<1.e-1)", "colz");
318     ((TVirtualPad*)l->At(1))->cd();
319     leg= new TLegend(.7, .7, .9, .95);
320     leg->SetBorderSize(0); leg->SetFillColor(0); leg->SetFillStyle(0);
321     leg->SetHeader("TRD Plane"); 
322     for(Int_t il = 1; il<=AliTRDgeometry::kNlayer; il++){
323       if(!(gm = (TGraphErrors*)arr->At(il))) return kFALSE;
324       gm->Draw(il>1?"pc":"apc"); leg->AddEntry(gm, Form("%d", il-1), "pl");
325       if(il>1) continue;
326       gm->GetHistogram()->SetXTitle("t_{drift} [tb]");
327       gm->GetHistogram()->SetYTitle("#sigma_{y}(x|cen=0) [#mum]");
328       gm->GetHistogram()->GetYaxis()->SetRangeUser(150., 500.);
329     }
330     leg->Draw();
331     return kTRUE;
332   case kSigm:
333     if(!(t = (TTree*)fResults->At(kSigm))) break;
334     t->Draw("z:t>>h2x(23, 0.1, 2.4, 25, 0., 2.5)","sx*(1)", "lego2fb");
335     h2 = (TH2F*)gROOT->FindObject("h2x");
336     printf("  const Double_t sx[24][25]={\n");
337     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
338       printf("    {");
339       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
340         printf("%6.4f ", h2->GetBinContent(ix, iy));
341       }
342       printf("%6.4f},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
343     }
344     printf("  };\n");
345     gPad->Divide(2, 1, 1.e-5, 1.e-5); l = gPad->GetListOfPrimitives();
346     ((TVirtualPad*)l->At(0))->cd();
347     h1 = h2->ProjectionX("hsx_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
348     h1->SetYTitle("<#sigma_{x}> [#mum]");
349     h1->SetXTitle("t_{drift} [#mus]");
350     h1->GetXaxis()->SetRange(2, AliTRDseedV1::kNtb-1); h1->Draw("pc");
351
352     t->Draw("z:t>>h2y(23, 0.1, 2.4, 25, 0., 2.5)","sy*(1)", "lego2fb");
353     h2 = (TH2F*)gROOT->FindObject("h2y");
354     printf("  const Double_t sy[24][25]={\n");
355     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
356       printf("    {");
357       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
358         printf("%6.4f ", h2->GetBinContent(ix, iy));
359       }
360       printf("%6.4f},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
361     }
362     printf("  };\n");
363     ((TVirtualPad*)l->At(1))->cd();
364     h1 = h2->ProjectionX("hsy_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
365     h1->SetYTitle("<#sigma_{y}> [#mum]");
366     h1->SetXTitle("t_{drift} [#mus]");
367     h1->GetXaxis()->SetRange(2, AliTRDseedV1::kNtb-1); h1->Draw("pc");
368     return kTRUE;
369   case kMean:
370     if(!(t = (TTree*)fResults->At(kMean))) break;
371     if(!t->Draw(Form("z:t>>h2x(%d, -0.5, %3.1f, %d, 0., 2.5)", 
372       AliTRDseedV1::kNtb, AliTRDseedV1::kNtb-0.5, kND),
373       "dx*(1)", "goff")) break;
374     h2 = (TH2F*)gROOT->FindObject("h2x");
375     printf("  const Double_t dx[%d][%d]={\n", AliTRDseedV1::kNtb, kND);
376     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
377       printf("    {");
378       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
379         printf("%+6.4e, ", h2->GetBinContent(ix, iy));
380       }
381       printf("%+6.4e},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
382     }
383     printf("  };\n");
384     gPad->Divide(2, 2, 1.e-5, 1.e-5); l = gPad->GetListOfPrimitives();
385     ((TVirtualPad*)l->At(0))->cd();
386     h2->Draw("lego2fb");
387     ((TVirtualPad*)l->At(2))->cd();
388     h1 = h2->ProjectionX("hdx_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
389     h1->SetYTitle("<#deltax> [#mum]");
390     h1->SetXTitle("t_{drift} [tb]");
391     //h1->GetXaxis()->SetRange(2, AliTRDseedV1::kNtb-1); 
392     h1->Draw("pc");
393
394     if(!t->Draw(Form("z:t>>h2y(%d, -0.5, %3.1f, %d, 0., 2.5)", 
395       AliTRDseedV1::kNtb, AliTRDseedV1::kNtb-0.5, kND),
396       "dy*(1)", "goff")) break;
397     h2 = (TH2F*)gROOT->FindObject("h2y");
398     printf("  const Double_t dy[%d][%d]={\n", AliTRDseedV1::kNtb, kND);
399     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
400       printf("    {");
401       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
402         printf("%+6.4e ", h2->GetBinContent(ix, iy));
403       }
404       printf("%+6.4e},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
405     }
406     printf("  };\n");
407     ((TVirtualPad*)l->At(1))->cd();
408     h2->Draw("lego2fb");
409     ((TVirtualPad*)l->At(3))->cd();
410     h1 = h2->ProjectionX("hdy_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
411     h1->SetYTitle("<#deltay> [#mum]");
412     h1->SetXTitle("t_{drift} [tb]");
413     //h1->GetXaxis()->SetRange(2, AliTRDseedV1::kNtb-1); 
414     h1->Draw("pc");
415
416     return kTRUE;
417   default:
418     break;
419   }
420   AliWarning("No container/data found.");
421   return kFALSE;
422 }
423
424 //_______________________________________________________
425 TObjArray* AliTRDclusterResolution::Histos()
426 {
427 // Retrieve histograms array if already build or build it
428
429   if(fContainer) return fContainer;
430   fContainer = new TObjArray(kNtasks);
431   //fContainer->SetOwner(kTRUE);
432
433   TH3S *h3 = NULL;
434   TObjArray *arr = NULL;
435
436   // add resolution/pulls plots for dydx=ExB
437   fContainer->AddAt(arr = new TObjArray(2), kCenter);
438   arr->SetName("Center");
439   if(!(h3=(TH3S*)gROOT->FindObject(Form("hCenRes%03d",fDet)))) {
440     h3 = new TH3S(
441       Form("hCenRes%03d",fDet),
442       Form(" Det[%d] Col[%d] Row[%d];t [bin];y [pw];#Delta y[cm]", fDet, fCol, fRow),
443       AliTRDseedV1::kNtb, -.5, AliTRDseedV1::kNtb-0.5,   // x
444       51, -.51, .51, // y
445       60, -fDyRange, fDyRange); // dy
446   } h3->Reset();
447   arr->AddAt(h3, 0);
448   // add Pull plot for each layer
449   if(!(h3=(TH3S*)gROOT->FindObject(Form("hCenPull%03d", fDet)))){
450     h3 = new TH3S(
451       Form("hCenPull%03d", fDet),
452       Form(" Det[%d] Col[%d] Row[%d];t [bin];y [pw];#Delta y[cm]/#sigma_{y}", fDet, fCol, fRow),
453       AliTRDseedV1::kNtb, -0.5, AliTRDseedV1::kNtb-0.5,   // x
454       51, -.51, .51, // y
455       60, -4., 4.); // dy/sy
456   } h3->Reset();
457   arr->AddAt(h3, 1);
458
459   if(!(h3 = (TH3S*)gROOT->FindObject(Form("Charge%03d", fDet)))){
460     h3 = new TH3S(Form("Charge%03d", fDet),
461     "dy=f(q);log(q) [a.u.];#Delta y[cm];#Delta y/#sigma_{y}",
462     50, 2.2, 7.5, 60, -fDyRange, fDyRange, 60, -4., 4.);
463   }
464   fContainer->AddAt(h3, kQRes);
465
466   fContainer->AddAt(arr = new TObjArray(AliTRDseedV1::kNtb), kSigm);
467   arr->SetName("Resolution");
468   for(Int_t it=0; it<AliTRDseedV1::kNtb; it++){
469     if(!(h3=(TH3S*)gROOT->FindObject(Form("hr%03d_t%02d", fDet, it)))){
470       h3 = new TH3S(
471         Form("hr%03d_t%02d", fDet, it),
472         Form(" Det[%d] t_{drift}(%2d)[bin];z [mm];tg#phi;#Delta y[cm]", fDet, it), 
473         kND, 0., 2.5,   // z 
474         35, -.35, .35, // tgp 
475         60, -fDyRange, fDyRange); // dy
476     }
477     arr->AddAt(h3, it);
478   }
479
480   fContainer->AddAt(arr = new TObjArray(AliTRDseedV1::kNtb), kMean);
481   arr->SetName("Systematics");
482   for(Int_t it=0; it<AliTRDseedV1::kNtb; it++){
483     if(!(h3=(TH3S*)gROOT->FindObject(Form("hs%03d_t%02d", fDet, it)))){
484       h3 = new TH3S(
485         Form("hs%03d_t%02d", fDet, it),
486         Form(" Det[%d] t_{drift}(%2d)[bin];z [mm];tg#phi - h*tg(#theta);#Delta y[cm]", fDet, it), 
487         kND, 0., 2.5,   // z 
488         35, -.35, .35, // tgp-h tgt 
489         60, -fDyRange, fDyRange); // dy
490     }
491     arr->AddAt(h3, it);
492   }
493
494   return fContainer;
495 }
496
497 //_______________________________________________________
498 void AliTRDclusterResolution::UserExec(Option_t *)
499 {
500 // Fill container histograms
501
502   
503   fInfo = dynamic_cast<TObjArray *>(GetInputData(1));
504   AliDebug(2, Form("Clusters[%d]", fInfo->GetEntriesFast()));
505   if(!IsCalibrated()){
506     LoadCalibration();
507     if(!IsCalibrated()){
508       AliWarning("Loading the calibration settings failed. Check OCDB access.");
509       return;
510     }
511   }
512
513   Int_t det, t;
514   Float_t x, y, z, q, dy, dydx, dzdx, cov[3], covcl[3];
515   TH3S *h3 = NULL;
516
517   // define limits around ExB for which x contribution is negligible
518   const Float_t kDtgPhi = 3.5e-2; //(+- 2 deg)
519
520   TObjArray *arr0 = (TObjArray*)fContainer->At(kCenter);
521   TObjArray *arr1 = (TObjArray*)fContainer->At(kSigm);
522   TObjArray *arr2 = (TObjArray*)fContainer->At(kMean);
523
524   const AliTRDclusterInfo *cli = NULL;
525   TIterator *iter=fInfo->MakeIterator();
526   while((cli=dynamic_cast<AliTRDclusterInfo*>((*iter)()))){
527     cli->GetCluster(det, x, y, z, q, t, covcl);
528
529     // select cluster according to detector region if specified
530     if(fDet>=0 && fDet!=det) continue;
531     if(fCol>=0 && fRow>=0){
532       Int_t c,r;
533       cli->GetCenterPad(c, r);
534       if(TMath::Abs(fCol-c) > 5) continue;
535       if(TMath::Abs(fRow-r) > 2) continue;
536     }
537     dy = cli->GetResolution();
538     AliDebug(4, Form("det[%d] tb[%2d] q[%4.0f Log[%6.4f]] dy[%7.2f][um] ypull[%5.2f]", det, t, q, TMath::Log(q), 1.e4*dy, dy/TMath::Sqrt(covcl[0])));
539     
540     cli->GetGlobalPosition(y, z, dydx, dzdx, &cov[0]);
541
542     // resolution as a function of cluster charge
543     // only for phi equal exB 
544     if(TMath::Abs(dydx-fExB) < kDtgPhi){
545       h3 = (TH3S*)fContainer->At(kQRes);
546       h3->Fill(TMath::Log(q), dy, dy/TMath::Sqrt(covcl[0]));
547     }
548
549     // do not use problematic clusters in resolution analysis
550     // TODO define limits as calibration aware (gain) !!
551     if(q<20.*fGain || q>250.*fGain) continue;
552
553     //x = (t+.5)*fgkTimeBinLength; // conservative approach !!
554
555     // resolution as a function of y displacement from pad center
556     // only for phi equal exB
557     if(TMath::Abs(dydx-fExB) < kDtgPhi){
558       h3 = (TH3S*)arr0->At(0);
559       h3->Fill(t, cli->GetYDisplacement(), dy);
560       h3 = (TH3S*)arr0->At(1);
561       h3->Fill(t, cli->GetYDisplacement(), dy/TMath::Sqrt(covcl[0]));
562     }
563
564     Int_t it(((TH3S*)arr0->At(0))->GetXaxis()->FindBin(t));
565
566     // fill histo for resolution (sigma)
567     ((TH3S*)arr1->At(it-1))->Fill(10.*cli->GetAnisochronity(), dydx, dy);
568
569     // fill histo for systematic (mean)
570     ((TH3S*)arr2->At(it-1))->Fill(10.*cli->GetAnisochronity(), dydx-cli->GetTilt()*dzdx, dy);  
571   }
572 }
573
574
575 //_______________________________________________________
576 Bool_t AliTRDclusterResolution::PostProcess()
577 {
578 // Steer processing of various cluster resolution dependences :
579 //
580 //   - process resolution dependency cluster charge
581 //   if(HasProcess(kQRes)) ProcessCharge();
582 //   - process resolution dependency on y displacement
583 //   if(HasProcess(kCenter)) ProcessCenterPad();
584 //   - process resolution dependency on drift legth and drift cell width
585 //   if(HasProcess(kSigm)) ProcessSigma();
586 //   - process systematic shift on drift legth and drift cell width
587 //   if(HasProcess(kMean)) ProcessMean();
588
589   if(!fContainer) return kFALSE;
590   if(!IsCalibrated()){
591     AliWarning("Not calibrated.");
592     return kFALSE;
593   }
594   TObjArray *arr = NULL;
595   TTree *t=NULL;
596   if(!fResults){
597     TGraphErrors *g = NULL;
598     fResults = new TObjArray(kNtasks);
599     fResults->SetOwner();
600     fResults->AddAt(arr = new TObjArray(3), kQRes);
601     arr->SetOwner();
602     arr->AddAt(g = new TGraphErrors(), 0);
603     g->SetLineColor(kBlue); g->SetMarkerColor(kBlue);
604     g->SetMarkerStyle(7); 
605     arr->AddAt(g = new TGraphErrors(), 1);
606     g->SetLineColor(kRed); g->SetMarkerColor(kRed);
607     g->SetMarkerStyle(23); 
608     arr->AddAt(g = new TGraphErrors(), 2);
609     g->SetLineColor(kGreen); g->SetMarkerColor(kGreen);
610     g->SetMarkerStyle(7); 
611
612     // pad center dependence
613     fResults->AddAt(arr = new TObjArray(AliTRDgeometry::kNlayer+1), kCenter);
614     arr->SetOwner();
615     arr->AddAt(
616     t = new TTree("cent", "dy=f(y,x,ly)"), 0);
617     t->Branch("ly", &fLy, "ly/B");
618     t->Branch("t", &fT, "t/F");
619     t->Branch("y", &fY, "y/F");
620     t->Branch("m", &fR[0], "m[2]/F");
621     t->Branch("s", &fR[2], "s[2]/F");
622     t->Branch("pm", &fP[0], "pm[2]/F");
623     t->Branch("ps", &fP[2], "ps[2]/F");
624     for(Int_t il=1; il<=AliTRDgeometry::kNlayer; il++){
625       arr->AddAt(g = new TGraphErrors(), il);
626       g->SetLineColor(il); g->SetLineStyle(il);
627       g->SetMarkerColor(il);g->SetMarkerStyle(4); 
628     }
629
630
631     fResults->AddAt(t = new TTree("sigm", "dy=f(dw,x,dydx)"), kSigm);
632     t->Branch("t", &fT, "t/F");
633     t->Branch("x", &fX, "x/F");
634     t->Branch("z", &fZ, "z/F");
635     t->Branch("sx", &fR[0], "sx[2]/F");
636     t->Branch("sy", &fR[2], "sy[2]/F");
637
638
639     fResults->AddAt(t = new TTree("mean", "dy=f(dw,x,dydx - h dzdx)"), kMean);
640     t->Branch("t", &fT, "t/F");
641     t->Branch("x", &fX, "x/F");
642     t->Branch("z", &fZ, "z/F");
643     t->Branch("dx", &fR[0], "dx[2]/F");
644     t->Branch("dy", &fR[2], "dy[2]/F");
645   } else {
646     TObject *o = NULL;
647     TIterator *iter=fResults->MakeIterator();
648     while((o=((*iter)()))) o->Clear(); // maybe it is wrong but we should never reach this point
649   }
650   
651   // process resolution dependency on charge
652   if(HasProcess(kQRes)) ProcessCharge();
653   
654   // process resolution dependency on y displacement
655   if(HasProcess(kCenter)) ProcessCenterPad();
656
657   // process resolution dependency on drift legth and drift cell width
658   if(HasProcess(kSigm)) ProcessSigma();
659
660   // process systematic shift on drift legth and drift cell width
661   if(HasProcess(kMean)) ProcessMean();
662
663   return kTRUE;
664 }
665
666 //_______________________________________________________
667 Bool_t AliTRDclusterResolution::LoadCalibration()
668 {
669 // Retrieve calibration parameters from OCDB, drift velocity and t0 for the detector region specified by
670 // a previous call to AliTRDclusterResolution::SetCalibrationRegion().
671
672   AliCDBManager *cdb = AliCDBManager::Instance(); // init OCDB
673   if(cdb->GetRun() < 0){
674     AliError("OCDB manager not properly initialized");
675     return kFALSE;
676   }
677
678   // check magnetic field
679   AliESDEvent *esd = dynamic_cast<AliESDEvent*>(InputEvent());
680   if(!esd){
681     AliError("Failed retrieving ESD event");
682     return kFALSE;
683   }
684   if(!TGeoGlobalMagField::Instance()->IsLocked() && !esd->InitMagneticField()){
685     AliError("Magnetic field failed initialization.");
686     return kFALSE;
687   }
688
689   // check pad for detector
690   if(fCol>=0 && fRow>=0){
691     AliTRDgeometry geo;
692     AliTRDpadPlane *pp(geo.GetPadPlane(fDet));
693     if(fCol>=pp->GetNcols() ||
694        fRow>=pp->GetNrows()){
695       AliWarning(Form("Pad coordinates col[%d] or row[%d] incorrect for det[%d].\nLimits are max col[%d] max row[%d]. Reset to default", fCol, fRow, fDet, pp->GetNcols(), pp->GetNrows()));
696       fCol = -1; fRow=-1;
697     }
698   }
699
700   AliTRDcalibDB *fCalibration  = AliTRDcalibDB::Instance();
701   AliTRDCalROC  *fCalVdriftROC(fCalibration->GetVdriftROC(fDet>=0?fDet:0))
702                ,*fCalT0ROC(fCalibration->GetT0ROC(fDet>=0?fDet:0));
703   const AliTRDCalDet  *fCalVdriftDet = fCalibration->GetVdriftDet();
704   const AliTRDCalDet  *fCalT0Det = fCalibration->GetT0Det();
705
706   fVdrift = fCalVdriftDet->GetValue(fDet>=0?fDet:0);
707   if(fCol>=0 && fRow>=0) fVdrift*= fCalVdriftROC->GetValue(fCol, fRow);
708   fExB    = AliTRDCommonParam::Instance()->GetOmegaTau(fVdrift);
709   fT0     = fCalT0Det->GetValue(fDet>=0?fDet:0);
710   if(fCol>=0 && fRow>=0) fT0 *= fCalT0ROC->GetValue(fCol, fRow);
711   fGain = (fCol>=0 && fRow>=0)?fCalibration-> GetGainFactor(fDet, fCol, fRow):fCalibration-> GetGainFactorAverage(fDet);
712   SetBit(kCalibrated);
713
714   AliDebug(1, Form("Calibrate for Det[%3d] Col[%3d] Row[%2d] : \n   t0[%5.3f] vd[%5.3f] gain[%5.3f] ExB[%f]", fDet, fCol, fRow, fT0, fVdrift, fGain, fExB));
715
716   return kTRUE;
717 }
718
719 //_______________________________________________________
720 void AliTRDclusterResolution::SetCalibrationRegion(Int_t det, Int_t col, Int_t row)
721 {
722 // Set calibration region in terms of detector and pad. 
723 // By default detector 0 mean values are considered.
724
725   if(det>=0 && det<AliTRDgeometry::kNdet){ 
726     fDet = det;
727     if(col>=0 && row>=0){
728       fCol = col;
729       fRow = row;
730     }
731     return;
732   }
733   AliError(Form("Detector index outside range [0 %d].", AliTRDgeometry::kNdet));
734 }
735
736 //_______________________________________________________
737 void AliTRDclusterResolution::SetVisual()
738 {
739   if(fCanvas) return;
740   fCanvas = new TCanvas("clResCanvas", "Cluster Resolution Visualization", 10, 10, 600, 600);
741 }
742
743 //_______________________________________________________
744 void AliTRDclusterResolution::ProcessCharge()
745 {
746 // Resolution as a function of cluster charge.
747 //
748 // As described in the function ProcessCenterPad() the error parameterization for clusters for phi = a_L can be 
749 // written as:
750 // BEGIN_LATEX
751 // #sigma_{y}^{2} = #sigma_{y}^{2}|_{B=0} + tg^{2}(#alpha_{L})*#sigma_{x}^{2}
752 // END_LATEX
753 // with the contribution in case of B=0 given by:
754 // BEGIN_LATEX
755 // #sigma_{y}|_{B=0} = #sigma_{diff}*Gauss(0, s_{ly}) + #delta_{#sigma}(q)
756 // END_LATEX
757 // which further can be simplified to:
758 // BEGIN_LATEX
759 // <#sigma_{y}|_{B=0}>(q) = <#sigma_{y}> + #delta_{#sigma}(q)
760 // <#sigma_{y}> = #int{f(q)#sigma_{y}dq}
761 // END_LATEX
762 // The results for s_y and f(q) are displayed below:
763 //Begin_Html
764 //<img src="TRD/clusterQerror.gif">
765 //End_Html
766 // The function has to extended to accomodate gain calibration scalling and errors.
767 //
768 // Author
769 // Alexandru Bercuci <A.Bercuci@gsi.de>
770
771   TH3S *h3(NULL);
772   if(!(h3 = (TH3S*)fContainer->At(kQRes))) {
773     AliWarning("Missing dy=f(Q) histo");
774     return;
775   }
776   TF1 f("f", "gaus", -.5, .5);
777   TAxis *ax(NULL);
778   TH1 *h1(NULL);
779
780   // compute mean error on x
781   Double_t s2x = 0.; 
782   for(Int_t ix=5; ix<AliTRDseedV1::kNtb; ix++){
783     // retrieve error on the drift length
784     s2x += AliTRDcluster::GetSX(ix);
785   }
786   s2x /= (AliTRDseedV1::kNtb-5); s2x *= s2x;
787   //Double_t exb2 = fExB*fExB;
788
789   TObjArray *arr = (TObjArray*)fResults->At(kQRes);
790   TGraphErrors *gqm = (TGraphErrors*)arr->At(0);
791   TGraphErrors *gqs = (TGraphErrors*)arr->At(1);
792   TGraphErrors *gqp = (TGraphErrors*)arr->At(2);
793   Double_t q, n = 0., entries;
794   ax = h3->GetXaxis();
795   for(Int_t ix=1; ix<=ax->GetNbins(); ix++){
796     q = TMath::Exp(ax->GetBinCenter(ix));
797     ax->SetRange(ix, ix);
798     h1 = h3->Project3D("y");
799     entries = h1->GetEntries();
800     if(entries < 150) continue;
801     h1->Fit(&f, "Q");
802
803     // Fill sy^2 = f(q)
804     Int_t ip = gqm->GetN();
805     gqm->SetPoint(ip, q, 1.e4*f.GetParameter(1));
806     gqm->SetPointError(ip, 0., 1.e4*f.GetParError(1));
807
808     // correct sigma for ExB effect
809     gqs->SetPoint(ip, q, 1.e4*f.GetParameter(2)/**f.GetParameter(2)-exb2*s2x)*/);
810     gqs->SetPointError(ip, 0., 1.e4*f.GetParError(2)/**f.GetParameter(2)*/);
811
812     // save probability
813     n += entries;
814     gqp->SetPoint(ip, q, entries);
815     gqp->SetPointError(ip, 0., 0./*TMath::Sqrt(entries)*/);
816   } 
817
818   // normalize probability and get mean sy
819   Double_t sm = 0., sy;
820   for(Int_t ip=gqp->GetN(); ip--;){
821     gqp->GetPoint(ip, q, entries);
822     entries/=n;
823     gqp->SetPoint(ip, q, 1.e4*entries);
824     gqs->GetPoint(ip, q, sy);
825     sm += entries*sy;
826   }
827
828   // error parametrization s(q) = <sy> + b(1/q-1/q0)
829   TF1 fq("fq", "[0] + [1]/x", 20., 250.);
830   gqs->Fit(&fq/*, "W"*/);
831   printf("sm=%f [0]=%f [1]=%f\n", 1.e-4*sm, fq.GetParameter(0), fq.GetParameter(1));
832   printf("  const Float_t sq0inv = %f; // [1/q0]\n", (sm-fq.GetParameter(0))/fq.GetParameter(1));
833   printf("  const Float_t sqb    = %f; // [cm]\n", 1.e-4*fq.GetParameter(1));
834 }
835
836 //_______________________________________________________
837 void AliTRDclusterResolution::ProcessCenterPad()
838 {
839 // Resolution as a function of y displacement from pad center and drift length.
840 //
841 // Since the error parameterization of cluster r-phi position can be written as (see AliTRDcluster::SetSigmaY2()):
842 // BEGIN_LATEX
843 // #sigma_{y}^{2} = (#sigma_{diff}*Gauss(0, s_{ly}) + #delta_{#sigma}(q))^{2} + tg^{2}(#alpha_{L})*#sigma_{x}^{2} + tg^{2}(#phi-#alpha_{L})*#sigma_{x}^{2}+[tg(#phi-#alpha_{L})*tg(#alpha_{L})*x]^{2}/12
844 // END_LATEX
845 // one can see that for phi = a_L one gets the following expression:
846 // BEGIN_LATEX
847 // #sigma_{y}^{2} = #sigma_{y}^{2}|_{B=0} + tg^{2}(#alpha_{L})*#sigma_{x}^{2}
848 // END_LATEX
849 // where we have explicitely marked the remaining term in case of absence of magnetic field. Thus one can use the 
850 // previous equation to estimate s_y for B=0 and than by comparing in magnetic field conditions one can get the s_x.
851 // This is a simplified method to determine the error parameterization for s_x and s_y as compared to the one 
852 // implemented in ProcessSigma(). For more details on cluster error parameterization please see also 
853 // AliTRDcluster::SetSigmaY2()
854 // 
855 // The representation of dy=f(y_cen, x_drift| layer) can be also used to estimate the systematic shift in the r-phi 
856 // coordinate resulting from imperfection in the cluster shape parameterization. From the expresion of the shift derived 
857 // in ProcessMean() with phi=exb one gets: 
858 // BEGIN_LATEX
859 // <#Delta y>= <#delta x> * (tg(#alpha_{L})-h*dz/dx) + <#delta y - #delta x * tg(#alpha_{L})>
860 // <#Delta y>(y_{cen})= -h*<#delta x>(x_{drift}, q_{cl}) * dz/dx + #delta y(y_{cen}, ...)
861 // END_LATEX
862 // where all dependences are made explicit. This last expression can be used in two ways:
863 //   - by average on the dz/dx we can determine directly dy (the method implemented here) 
864 //   - by plotting as a function of dzdx one can determine both dx and dy components in an independent method.
865 //Begin_Html
866 //<img src="TRD/clusterYcorr.gif">
867 //End_Html
868 // Author
869 // Alexandru Bercuci <A.Bercuci@gsi.de>
870
871   TObjArray *arr = (TObjArray*)fContainer->At(kCenter);
872   if(!arr) {
873     AliWarning("Missing dy=f(y | x, ly) container");
874     return;
875   }
876   Double_t exb2 = fExB*fExB;
877   Float_t s[AliTRDgeometry::kNlayer];
878   TF1 f("f", "gaus", -.5, .5);
879   TF1 fp("fp", "gaus", -3.5, 3.5);
880
881   TH1D *h1 = NULL; TH2F *h2 = NULL; TH3S *h3r=NULL, *h3p=NULL;
882   TObjArray *arrRes = (TObjArray*)fResults->At(kCenter);
883   TTree *t = (TTree*)arrRes->At(0);
884   TGraphErrors *gs = NULL;
885   TAxis *ax = NULL;
886
887   AliDebug(1, Form("Calibrate for Det[%3d] t0[%5.3f] vd[%5.3f]", fDet, fT0, fVdrift));
888
889   const Int_t nl = AliTRDgeometry::kNlayer;
890   printf("  const Float_t lSy[%d][%d] = {\n      {", nl, AliTRDseedV1::kNtb);
891   for(Int_t il=0; il<nl; il++){
892     if(!(h3r = (TH3S*)arr->At(il))) continue;
893     if(!(h3p = (TH3S*)arr->At(nl+il))) continue;
894     gs = (TGraphErrors*)arrRes->At(il+1);
895     fLy = il;
896     for(Int_t ix=1; ix<=h3r->GetXaxis()->GetNbins(); ix++){
897       ax = h3r->GetXaxis(); ax->SetRange(ix, ix);
898       ax = h3p->GetXaxis(); ax->SetRange(ix, ix);
899       fT  = ax->GetBinCenter(ix);
900       for(Int_t iy=1; iy<=h3r->GetYaxis()->GetNbins(); iy++){
901         ax = h3r->GetYaxis(); ax->SetRange(iy, iy);
902         ax = h3p->GetYaxis(); ax->SetRange(iy, iy);
903         fY  = ax->GetBinCenter(iy);
904         // finish navigation in the HnSparse
905
906         h1 = (TH1D*)h3r->Project3D("z");
907         Int_t entries = (Int_t)h1->Integral();
908         if(entries < 50) continue;
909         //Adjust(&f, h1);
910         h1->Fit(&f, "QN");
911     
912         // Fill sy,my=f(y_w,x,ly)
913         fR[0] = f.GetParameter(1); fR[1] = f.GetParError(1);
914         fR[2] = f.GetParameter(2); fR[3] = f.GetParError(2);
915
916         h1 = (TH1D*)h3p->Project3D("z");
917         h1->Fit(&fp, "QN");
918         fP[0] = fp.GetParameter(1); fP[1] = fp.GetParError(1);
919         fP[2] = fp.GetParameter(2); fP[3] = fp.GetParError(2);
920
921         AliDebug(4, Form("ly[%d] tb[%2d] y[%+5.2f] m[%5.3f] s[%5.3f] pm[%5.3f] ps[%5.3f]", fLy, (Int_t)fT, fY, fR[0], fR[2], fP[0], fP[2]));
922         t->Fill();
923       }
924     }
925     t->Draw(Form("y:t>>h(%d, -0.5, %f, 51, -.51, .51)", AliTRDseedV1::kNtb, AliTRDseedV1::kNtb-0.5),
926             Form("s[0]*(ly==%d&&abs(m[0])<1.e-1)", fLy),
927             "goff");
928     h2=(TH2F*)gROOT->FindObject("h");
929     f.FixParameter(1, 0.);
930     Int_t n = h2->GetXaxis()->GetNbins(), nn(0); s[il]=0.;
931     printf("    {");
932     for(Int_t ix=1; ix<=n; ix++){
933       ax = h2->GetXaxis();
934       fT  = ax->GetBinCenter(ix);
935       h1 = h2->ProjectionY("hCenPy", ix, ix);
936       //if((Int_t)h1->Integral() < 1.e-10) continue; 
937
938       // Apply lorentz angle correction
939       // retrieve error on the drift length
940       Double_t s2x = AliTRDcluster::GetSX(ix-1); s2x *= s2x;
941       Int_t nnn = 0;
942       for(Int_t iy=1; iy<=h1->GetNbinsX(); iy++){
943         Double_t s2 = h1->GetBinContent(iy); s2*= s2;
944         // sigma square corrected for Lorentz angle
945         // s2 = s2_y(y_w,x)+exb2*s2_x
946         Double_t sy = TMath::Sqrt(TMath::Max(s2 - exb2*s2x, Double_t(0.)));
947         if(sy<1.e-20) continue;
948         h1->SetBinContent(iy, sy); nnn++;
949         AliDebug(4, Form("s[%6.2f] sx[%6.2f] sy[%6.2f]\n",
950         1.e4*TMath::Sqrt(s2), 1.e4*TMath::Abs(fExB*AliTRDcluster::GetSX(ix-1)), 
951         1.e4*h1->GetBinContent(iy)));
952       }
953       // do fit only if enough data
954       Double_t sPRF = 0.;
955       if(nnn>5){
956         h1->Fit(&f, "QN");
957         sPRF = f.GetParameter(2); nn++;
958       }
959       s[il]+=sPRF;
960       printf("%6.4f,%s", sPRF, ix%6?" ":"\n     ");
961       Int_t jx = gs->GetN();
962       gs->SetPoint(jx, fT, 1.e4*sPRF);
963       gs->SetPointError(jx, 0., 0./*f.GetParError(0)*/);
964     }
965     printf("\b},\n");
966     s[il]/=nn;
967
968     f.ReleaseParameter(1);
969
970
971     if(!fCanvas) continue;
972     h2->Draw("lego2fb");
973     fCanvas->Modified(); fCanvas->Update();
974     if(IsSaveAs()) fCanvas->SaveAs(Form("Figures/ProcessCenter_ly[%d].gif", fLy));
975     else gSystem->Sleep(100);
976   }
977   printf("  };\n");
978   printf("  const Float_t lPRF[] = {"
979     "%5.3f, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f};\n",
980     s[0], s[1], s[2], s[3], s[4], s[5]);
981 }
982
983 //_______________________________________________________
984 void AliTRDclusterResolution::ProcessSigma()
985 {
986 // As the r-phi coordinate is the only one which is measured by the TRD detector we have to rely on it to
987 // estimate both the radial (x) and r-phi (y) errors. This method is based on the following assumptions. 
988 // The measured error in the y direction is the sum of the intrinsic contribution of the r-phi measurement
989 // with the contribution of the radial measurement - because x is not a parameter of Alice track model (Kalman).
990 // BEGIN_LATEX
991 // #sigma^{2}|_{y} = #sigma^{2}_{y*} + #sigma^{2}_{x*}   
992 // END_LATEX
993 // In the general case 
994 // BEGIN_LATEX
995 // #sigma^{2}_{y*} = #sigma^{2}_{y} + tg^{2}(#alpha_{L})#sigma^{2}_{x_{drift}}   
996 // #sigma^{2}_{x*} = tg^{2}(#phi - #alpha_{L})*(#sigma^{2}_{x_{drift}} + #sigma^{2}_{x_{0}} + tg^{2}(#alpha_{L})*x^{2}/12)
997 // END_LATEX
998 // where we have explicitely show the lorentz angle correction on y and the projection of radial component on the y
999 // direction through the track angle in the bending plane (phi). Also we have shown that the radial component in the
1000 // last equation has twp terms, the drift and the misalignment (x_0). For ideal geometry or known misalignment one 
1001 // can solve the equation
1002 // BEGIN_LATEX
1003 // #sigma^{2}|_{y} = tg^{2}(#phi - #alpha_{L})*(#sigma^{2}_{x} + tg^{2}(#alpha_{L})*x^{2}/12)+ [#sigma^{2}_{y} + tg^{2}(#alpha_{L})#sigma^{2}_{x}]
1004 // END_LATEX
1005 // by fitting a straight line:
1006 // BEGIN_LATEX
1007 // #sigma^{2}|_{y} = a(x_{cl}, z_{cl}) * tg^{2}(#phi - #alpha_{L}) + b(x_{cl}, z_{cl})
1008 // END_LATEX
1009 // the error parameterization will be given by:
1010 // BEGIN_LATEX
1011 // #sigma_{x} (x_{cl}, z_{cl}) = #sqrt{a(x_{cl}, z_{cl}) - tg^{2}(#alpha_{L})*x^{2}/12}
1012 // #sigma_{y} (x_{cl}, z_{cl}) = #sqrt{b(x_{cl}, z_{cl}) - #sigma^{2}_{x} (x_{cl}, z_{cl}) * tg^{2}(#alpha_{L})}
1013 // END_LATEX
1014 // Below there is an example of such dependency. 
1015 //Begin_Html
1016 //<img src="TRD/clusterSigmaMethod.gif">
1017 //End_Html
1018 //
1019 // The error parameterization obtained by this method are implemented in the functions AliTRDcluster::GetSX() and
1020 // AliTRDcluster::GetSYdrift(). For an independent method to determine s_y as a function of drift length check the 
1021 // function ProcessCenterPad(). One has to keep in mind that while this method return the mean s_y over the distance
1022 // to pad center distribution the other method returns the *STANDARD* value at center=0 (maximum). To recover the 
1023 // standard value one has to solve the obvious equation:
1024 // BEGIN_LATEX
1025 // #sigma_{y}^{STANDARD} = #frac{<#sigma_{y}>}{#int{s exp(s^{2}/#sigma) ds}}
1026 // END_LATEX
1027 // with "<s_y>" being the value calculated here and "sigma" the width of the s_y distribution calculated in 
1028 // ProcessCenterPad().
1029 //  
1030 // Author
1031 // Alexandru Bercuci <A.Bercuci@gsi.de>
1032
1033   TObjArray *arr = (TObjArray*)fContainer->At(kSigm);
1034   if(!arr){
1035     AliWarning("Missing dy=f(x_d, d_w) container");
1036     return;
1037   }
1038
1039   // init visualization
1040   TGraphErrors *ggs = NULL;
1041   TGraph *line = NULL;
1042   if(fCanvas){
1043     ggs = new TGraphErrors();
1044     line = new TGraph();
1045     line->SetLineColor(kRed);line->SetLineWidth(2);
1046   }
1047
1048   // init logistic support
1049   TF1 f("f", "gaus", -.5, .5);
1050   TLinearFitter gs(1,"pol1");
1051   TH1 *hFrame=NULL;
1052   TH1D *h1 = NULL; TH3S *h3=NULL;
1053   TAxis *ax = NULL;
1054   Double_t exb2 = fExB*fExB;
1055   AliTRDcluster c;
1056   TTree *t = (TTree*)fResults->At(kSigm);
1057   for(Int_t ix=0; ix<AliTRDseedV1::kNtb; ix++){
1058     if(!(h3=(TH3S*)arr->At(ix))) continue;
1059     c.SetPadTime(ix);
1060     fX = c.GetXloc(fT0, fVdrift);
1061     fT = c.GetLocalTimeBin(); // ideal
1062     printf(" pad time[%d] local[%f]\n", ix, fT);
1063     for(Int_t iz=1; iz<=h3->GetXaxis()->GetNbins(); iz++){
1064       ax = h3->GetXaxis();
1065       ax->SetRange(iz, iz);
1066       fZ = ax->GetBinCenter(iz);
1067
1068       // reset visualization
1069       if(fCanvas){ 
1070         new(ggs) TGraphErrors();
1071         ggs->SetMarkerStyle(7);
1072       }
1073       gs.ClearPoints();
1074
1075       for(Int_t ip=1; ip<=h3->GetYaxis()->GetNbins(); ip++){
1076         ax = h3->GetYaxis();
1077         ax->SetRange(ip, ip); 
1078         Double_t tgl = ax->GetBinCenter(ip);
1079         // finish navigation in the HnSparse
1080
1081         //if(TMath::Abs(dydx)>0.18) continue;
1082         Double_t tgg = (tgl-fExB)/(1.+tgl*fExB);
1083         Double_t tgg2 = tgg*tgg;
1084
1085         h1 = (TH1D*)h3->Project3D("z");
1086         Int_t entries = (Int_t)h1->Integral();
1087         if(entries < 50) continue;
1088         //Adjust(&f, h1);
1089         h1->Fit(&f, "QN");
1090
1091         Double_t s2  = f.GetParameter(2)*f.GetParameter(2);
1092         Double_t s2e = 2.*f.GetParameter(2)*f.GetParError(2);
1093         // Fill sy^2 = f(tg^2(phi-a_L))
1094         gs.AddPoint(&tgg2, s2, s2e);
1095
1096         if(!ggs) continue;
1097         Int_t jp = ggs->GetN();
1098         ggs->SetPoint(jp, tgg2, s2);
1099         ggs->SetPointError(jp, 0., s2e);
1100       }
1101       // TODO here a more robust fit method has to be provided
1102       // for which lower boundaries on the parameters have to 
1103       // be imposed. Unfortunately the Minuit fit does not work 
1104       // for the TGraph in the case of B not 0.
1105       if(gs.Eval()) continue;
1106
1107       fR[0] = gs.GetParameter(1) - fX*fX*exb2/12.;
1108       AliDebug(3, Form("  s2x+x2=%f ang=%f s2x=%f", gs.GetParameter(1), fX*fX*exb2/12., fR[0]));
1109       fR[0] = TMath::Max(fR[0], Float_t(4.e-4)); 
1110
1111       // s^2_y  = s0^2_y + tg^2(a_L) * s^2_x
1112       // s0^2_y = f(D_L)*x + s_PRF^2 
1113       fR[2]= gs.GetParameter(0)-exb2*fR[0];
1114       AliDebug(3, Form("  s2y+s2x=%f s2y=%f", fR[0], fR[2]));
1115       fR[2] = TMath::Max(fR[2], Float_t(2.5e-5)); 
1116       fR[0] = TMath::Sqrt(fR[0]);
1117       fR[1] = .5*gs.GetParError(1)/fR[0];
1118       fR[2] = TMath::Sqrt(fR[2]);
1119       fR[3] = gs.GetParError(0)+exb2*exb2*gs.GetParError(1);
1120       t->Fill();
1121       AliDebug(2, Form("xd=%4.2f[cm] sx=%6.1f[um] sy=%5.1f[um]", fX, 1.e4*fR[0], 1.e4*fR[2]));
1122
1123       if(!fCanvas) continue;
1124       fCanvas->cd(); fCanvas->SetLogx(); //fCanvas->SetLogy();
1125       if(!hFrame){ 
1126         fCanvas->SetMargin(0.15, 0.01, 0.1, 0.01);
1127         hFrame=new TH1I("hFrame", "", 100, 0., .3);
1128         hFrame->SetMinimum(0.);hFrame->SetMaximum(.005);
1129         hFrame->SetXTitle("tg^{2}(#phi-#alpha_{L})");
1130         hFrame->SetYTitle("#sigma^{2}y[cm^{2}]");
1131         hFrame->GetYaxis()->SetTitleOffset(2.);
1132         hFrame->SetLineColor(1);hFrame->SetLineWidth(1);
1133         hFrame->Draw();
1134       } else hFrame->Reset();
1135       Double_t xx = 0., dxx=.2/50;
1136       for(Int_t ip=0;ip<50;ip++){ 
1137         line->SetPoint(ip, xx,  gs.GetParameter(0)+xx*gs.GetParameter(1)); 
1138         xx+=dxx;
1139       }
1140       ggs->Draw("pl"); line->Draw("l");
1141       fCanvas->Modified(); fCanvas->Update();
1142       if(IsSaveAs()) fCanvas->SaveAs(Form("Figures/ProcessSigma_z[%5.3f]_x[%5.3f].gif", fZ, fX));
1143       else gSystem->Sleep(100);
1144     }
1145   }
1146   return;
1147 }
1148
1149 //_______________________________________________________
1150 void AliTRDclusterResolution::ProcessMean()
1151 {
1152 // By this method the cluster shift in r-phi and radial directions can be estimated by comparing with the MC.
1153 // The resolution of the cluster corrected for pad tilt with respect to MC in the r-phi (measuring) plane can be 
1154 // expressed by:
1155 // BEGIN_LATEX
1156 // #Delta y=w - y_{MC}(x_{cl})
1157 // w = y_{cl}^{'} + h*(z_{MC}(x_{cl})-z_{cl})
1158 // y_{MC}(x_{cl}) = y_{0} - dy/dx*x_{cl}
1159 // z_{MC}(x_{cl}) = z_{0} - dz/dx*x_{cl}
1160 // y_{cl}^{'} = y_{cl}-x_{cl}*tg(#alpha_{L})
1161 // END_LATEX
1162 // where x_cl is the drift length attached to a cluster, y_cl is the r-phi coordinate of the cluster measured by
1163 // charge sharing on adjacent pads and y_0 and z_0 are MC reference points (as example the track references at 
1164 // entrance/exit of a chamber). If we suppose that both r-phi (y) and radial (x) coordinate of the clusters are 
1165 // affected by errors we can write
1166 // BEGIN_LATEX
1167 // x_{cl} = x_{cl}^{*} + #delta x 
1168 // y_{cl} = y_{cl}^{*} + #delta y 
1169 // END_LATEX 
1170 // where the starred components are the corrected values. Thus by definition the following quantity
1171 // BEGIN_LATEX
1172 // #Delta y^{*}= w^{*} - y_{MC}(x_{cl}^{*})
1173 // END_LATEX
1174 // has 0 average over all dependency. Using this decomposition we can write:
1175 // BEGIN_LATEX
1176 // <#Delta y>=<#Delta y^{*}> + <#delta x * (dy/dx-h*dz/dx) + #delta y - #delta x * tg(#alpha_{L})>
1177 // END_LATEX
1178 // which can be transformed to the following linear dependence:
1179 // BEGIN_LATEX
1180 // <#Delta y>= <#delta x> * (dy/dx-h*dz/dx) + <#delta y - #delta x * tg(#alpha_{L})>
1181 // END_LATEX
1182 // if expressed as function of dy/dx-h*dz/dx. Furtheremore this expression can be plotted for various clusters
1183 // i.e. we can explicitely introduce the diffusion (x_cl) and drift cell - anisochronity (z_cl) dependences. From 
1184 // plotting this dependence and linear fitting it with:
1185 // BEGIN_LATEX
1186 // <#Delta y>= a(x_{cl}, z_{cl}) * (dy/dx-h*dz/dx) + b(x_{cl}, z_{cl})
1187 // END_LATEX
1188 // the systematic shifts will be given by:
1189 // BEGIN_LATEX
1190 // #delta x (x_{cl}, z_{cl}) = a(x_{cl}, z_{cl})
1191 // #delta y (x_{cl}, z_{cl}) = b(x_{cl}, z_{cl}) + a(x_{cl}, z_{cl}) * tg(#alpha_{L})
1192 // END_LATEX
1193 // Below there is an example of such dependency. 
1194 //Begin_Html
1195 //<img src="TRD/clusterShiftMethod.gif">
1196 //End_Html
1197 //
1198 // The occurance of the radial shift is due to the following conditions 
1199 //   - the approximation of a constant drift velocity over the drift length (larger drift velocities close to 
1200 //     cathode wire plane)
1201 //   - the superposition of charge tails in the amplification region (first clusters appear to be located at the 
1202 //     anode wire)
1203 //   - the superposition of charge tails in the drift region (shift towards anode wire)
1204 //   - diffusion effects which convolute with the TRF thus enlarging it
1205 //   - approximate knowledge of the TRF (approximate measuring in test beam conditions) 
1206 // 
1207 // The occurance of the r-phi shift is due to the following conditions 
1208 //   - approximate model for cluster shape (LUT)
1209 //   - rounding-up problems
1210 //
1211 // The numerical results for ideal simulations for the radial and r-phi shifts are displayed below and used 
1212 // for the cluster reconstruction (see the functions AliTRDcluster::GetXcorr() and AliTRDcluster::GetYcorr()). 
1213 //Begin_Html
1214 //<img src="TRD/clusterShiftX.gif">
1215 //<img src="TRD/clusterShiftY.gif">
1216 //End_Html
1217 // More details can be found in the presentation given during the TRD
1218 // software meeting at the end of 2008 and beginning of year 2009, published on indico.cern.ch.
1219 // 
1220 // Author 
1221 // Alexandru Bercuci <A.Bercuci@gsi.de>
1222
1223
1224  
1225   TObjArray *arr = (TObjArray*)fContainer->At(kMean);
1226   if(!arr){
1227     AliWarning("Missing dy=f(x_d, d_w) container");
1228     return;
1229   }
1230
1231   // init logistic support
1232   TF1 f("f", "gaus", -.5, .5);
1233   TF1 line("l", "[0]+[1]*x", -.15, .15);
1234   TGraphErrors *gm = new TGraphErrors();
1235   TH1 *hFrame=NULL;
1236   TH1D *h1 = NULL; TH3S *h3 =NULL;
1237   TAxis *ax = NULL;
1238
1239   AliDebug(1, Form("Calibrate for Det[%3d] t0[%5.3f] vd[%5.3f]", fDet, fT0, fVdrift));
1240
1241   AliTRDcluster c;
1242   TTree *t = (TTree*)fResults->At(kMean);
1243   for(Int_t ix=0; ix<AliTRDseedV1::kNtb; ix++){
1244     if(!(h3=(TH3S*)arr->At(ix))) continue;
1245     c.SetPadTime(ix);
1246     fX = c.GetXloc(fT0, fVdrift);
1247     fT = c.GetLocalTimeBin();
1248     for(Int_t iz=1; iz<=h3->GetXaxis()->GetNbins(); iz++){
1249       ax = h3->GetXaxis();
1250       ax->SetRange(iz, iz);
1251       fZ = ax->GetBinCenter(iz);
1252
1253       // reset fitter
1254       new(gm) TGraphErrors();
1255       gm->SetMarkerStyle(7);
1256
1257       for(Int_t ip=1; ip<=h3->GetYaxis()->GetNbins(); ip++){
1258         ax = h3->GetYaxis();
1259         ax->SetRange(ip, ip); 
1260         Double_t tgl = ax->GetBinCenter(ip);
1261         // finish navigation in the HnSparse
1262
1263         h1 = (TH1D*)h3->Project3D("z");
1264         Int_t entries = (Int_t)h1->Integral();
1265         if(entries < 50) continue;
1266         //Adjust(&f, h1);
1267         h1->Fit(&f, "QN");
1268
1269         // Fill <Dy> = f(dydx - h*dzdx)
1270         Int_t jp = gm->GetN();
1271         gm->SetPoint(jp, tgl, f.GetParameter(1));
1272         gm->SetPointError(jp, 0., f.GetParError(1));
1273       }
1274       if(gm->GetN()<10) continue;
1275
1276       gm->Fit(&line, "QN");
1277       fR[0] = line.GetParameter(1); // dx
1278       fR[1] = line.GetParError(1);
1279       fR[2] = line.GetParameter(0) + fExB*fR[0]; // xs = dy - tg(a_L)*dx
1280       t->Fill();
1281       AliDebug(2, Form("tb[%02d] xd=%4.2f[cm] dx=%6.2f[um] dy=%6.2f[um]", ix, fX, 1.e4*fR[0], 1.e4*fR[2]));
1282       if(!fCanvas) continue;
1283
1284       fCanvas->cd();
1285       if(!hFrame){ 
1286         fCanvas->SetMargin(0.1, 0.02, 0.1, 0.01);
1287         hFrame=new TH1I("hFrame", "", 100, -.3, .3);
1288         hFrame->SetMinimum(-.1);hFrame->SetMaximum(.1);
1289         hFrame->SetXTitle("tg#phi-htg#theta");
1290         hFrame->SetYTitle("#Delta y[cm]");
1291         hFrame->GetYaxis()->SetTitleOffset(1.5);
1292         hFrame->SetLineColor(1);hFrame->SetLineWidth(1);
1293         hFrame->Draw();
1294       } else hFrame->Reset();
1295       gm->Draw("pl"); line.Draw("same");
1296       fCanvas->Modified(); fCanvas->Update();
1297       if(IsSaveAs()) fCanvas->SaveAs(Form("Figures/ProcessMean_Z[%5.3f]_TB[%02d].gif", fZ, ix));
1298       else gSystem->Sleep(100);
1299     }
1300   }
1301 }