]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDclusterResolution.cxx
Streamlining with the analyis framework:
[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 "AliTRDcluster.h"
174 #include "AliTRDcalibDB.h"
175 #include "AliTRDCommonParam.h"
176 #include "Cal/AliTRDCalROC.h"
177 #include "Cal/AliTRDCalDet.h"
178
179 #include "AliLog.h"
180 #include "AliTracker.h"
181 #include "AliCDBManager.h"
182
183 #include "TROOT.h"
184 #include "TObjArray.h"
185 #include "TAxis.h"
186 #include "TF1.h"
187 #include "TLegend.h"
188 #include "TGraphErrors.h"
189 #include "TLine.h"
190 #include "TH2I.h"
191 #include "TH3S.h"
192 #include "TTree.h"
193 #include "TMath.h"
194 #include "TLinearFitter.h"
195
196 #include "TCanvas.h"
197 #include "TSystem.h"
198
199 ClassImp(AliTRDclusterResolution)
200
201 const Float_t AliTRDclusterResolution::fgkTimeBinLength = 1./ AliTRDCommonParam::Instance()->GetSamplingFrequency();
202 //_______________________________________________________
203 AliTRDclusterResolution::AliTRDclusterResolution()
204   : AliTRDrecoTask()
205   ,fCanvas(0x0)
206   ,fInfo(0x0)
207   ,fResults(0x0)
208   ,fAt(0x0)
209   ,fStatus(0)
210   ,fDet(-1)
211   ,fExB(0.)
212   ,fVdrift(0.)
213   ,fLy(0)
214   ,fX(0.)
215   ,fY(0.)
216   ,fZ(0.)
217 {
218 // Constructor
219 }
220
221 AliTRDclusterResolution::AliTRDclusterResolution(const char *name, const char *title)
222   : AliTRDrecoTask(name, title)
223   ,fCanvas(NULL)
224   ,fInfo(NULL)
225   ,fResults(NULL)
226   ,fAt(NULL)
227   ,fStatus(0)
228   ,fDet(-1)
229   ,fExB(0.)
230   ,fVdrift(0.)
231   ,fLy(0)
232   ,fX(0.)
233   ,fY(0.)
234   ,fZ(0.)
235 {
236 // Constructor
237
238   memset(fR, 0, 4*sizeof(Float_t));
239   memset(fP, 0, 4*sizeof(Float_t));
240   // time drift axis
241   fAt = new TAxis(kNTB, 0., kNTB*fgkTimeBinLength);
242
243   // By default register all analysis
244   // The user can switch them off in his steering macro
245   SetProcess(kQRes);
246   SetProcess(kCenter);
247   SetProcess(kMean);
248   SetProcess(kSigm);
249 }
250
251 //_______________________________________________________
252 AliTRDclusterResolution::~AliTRDclusterResolution()
253 {
254 // Destructor
255
256   if(fCanvas) delete fCanvas;
257   if(fAt) delete fAt;
258   if(fResults){
259     fResults->Delete();
260     delete fResults;
261   }
262 }
263
264 //_______________________________________________________
265 void AliTRDclusterResolution::ConnectInputData(Option_t *)
266 {
267     AliAnalysisTaskSE::ConnectInputData();
268     fInfo = dynamic_cast<TObjArray *>(GetInputData(0));
269 }
270
271 //_______________________________________________________
272 void AliTRDclusterResolution::UserCreateOutputObjects()
273 {
274   OpenFile(1, "RECREATE");
275   fContainer = Histos();
276 }
277
278 //_______________________________________________________
279 Bool_t AliTRDclusterResolution::GetRefFigure(Int_t ifig)
280 {
281 // Steering function to retrieve performance plots
282
283   if(!fResults) return kFALSE;
284   TLegend *leg = NULL;
285   TList *l = NULL;
286   TObjArray *arr = NULL;
287   TTree *t = NULL;
288   TH2 *h2 = NULL;TH1 *h1 = NULL;
289   TGraphErrors *gm(NULL), *gs(NULL), *gp(NULL);
290   switch(ifig){
291   case kQRes:
292     if(!(arr = (TObjArray*)fResults->At(kQRes))) break;
293     if(!(gm = (TGraphErrors*)arr->At(0))) break;
294     if(!(gs = (TGraphErrors*)arr->At(1))) break;
295     if(!(gp = (TGraphErrors*)arr->At(2))) break;
296     gs->Draw("apl");
297     gs->GetHistogram()->GetYaxis()->SetRangeUser(-50., 700.);
298     gs->GetHistogram()->SetXTitle("Q [a.u.]");
299     gs->GetHistogram()->SetYTitle("#sigma_{y} / #mu_{y} [#mum] / freq");
300     gm->Draw("pl");
301     gp->Draw("pl");
302     return kTRUE;
303   case kCenter:
304     if(!(arr = (TObjArray*)fResults->At(kCenter))) break;
305     gPad->Divide(2, 1); l = gPad->GetListOfPrimitives();
306     ((TVirtualPad*)l->At(0))->cd();
307     ((TTree*)arr->At(0))->Draw("y:x>>h(23, 0.1, 2.4, 51, -.51, .51)",
308             "m[0]*(ly==0&&abs(m[0])<1.e-1)", "colz");
309     ((TVirtualPad*)l->At(1))->cd();
310     leg= new TLegend(.7, .7, .9, .95);
311     leg->SetBorderSize(0); leg->SetFillColor(0); leg->SetFillStyle(0);
312     leg->SetHeader("TRD Plane"); 
313     for(Int_t il = 1; il<=AliTRDgeometry::kNlayer; il++){
314       if(!(gm = (TGraphErrors*)arr->At(il))) return kFALSE;
315       gm->Draw(il>1?"pc":"apc"); leg->AddEntry(gm, Form("%d", il-1), "pl");
316       if(il>1) continue;
317       gm->GetHistogram()->SetXTitle("t_{drift} [#mus]");
318       gm->GetHistogram()->SetYTitle("#sigma_{y}(x|cen=0) [#mum]");
319       gm->GetHistogram()->GetYaxis()->SetRangeUser(150., 500.);
320     }
321     leg->Draw();
322     return kTRUE;
323   case kSigm:
324     if(!(t = (TTree*)fResults->At(kSigm))) break;
325     t->Draw("z:t>>h2x(23, 0.1, 2.4, 25, 0., 2.5)","sx*(1)", "lego2fb");
326     h2 = (TH2F*)gROOT->FindObject("h2x");
327     printf("  const Double_t sx[24][25]={\n");
328     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
329       printf("    {");
330       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
331         printf("%6.4f ", h2->GetBinContent(ix, iy));
332       }
333       printf("%6.4f},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
334     }
335     printf("  };\n");
336     gPad->Divide(2, 1, 1.e-5, 1.e-5); l = gPad->GetListOfPrimitives();
337     ((TVirtualPad*)l->At(0))->cd();
338     h1 = h2->ProjectionX("hsx_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
339     h1->SetYTitle("<#sigma_{x}> [#mum]");
340     h1->SetXTitle("t_{drift} [#mus]");
341     h1->GetXaxis()->SetRange(2, kNTB-1); h1->Draw("pc");
342
343     t->Draw("z:t>>h2y(23, 0.1, 2.4, 25, 0., 2.5)","sy*(1)", "lego2fb");
344     h2 = (TH2F*)gROOT->FindObject("h2y");
345     printf("  const Double_t sy[24][25]={\n");
346     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
347       printf("    {");
348       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
349         printf("%6.4f ", h2->GetBinContent(ix, iy));
350       }
351       printf("%6.4f},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
352     }
353     printf("  };\n");
354     ((TVirtualPad*)l->At(1))->cd();
355     h1 = h2->ProjectionX("hsy_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
356     h1->SetYTitle("<#sigma_{y}> [#mum]");
357     h1->SetXTitle("t_{drift} [#mus]");
358     h1->GetXaxis()->SetRange(2, kNTB-1); h1->Draw("pc");
359     return kTRUE;
360   case kMean:
361     if(!(t = (TTree*)fResults->At(kMean))) break;
362     t->Draw("z:t>>h2x(23, 0.1, 2.4, 25, 0., 2.5)","dx*(1)", "goff");
363     h2 = (TH2F*)gROOT->FindObject("h2x");
364     printf("  const Double_t dx[24][25]={\n");
365     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
366       printf("    {");
367       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
368         printf("%6.4f ", h2->GetBinContent(ix, iy));
369       }
370       printf("%6.4f},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
371     }
372     printf("  };\n");
373     gPad->Divide(2, 1, 1.e-5, 1.e-5); l = gPad->GetListOfPrimitives();
374     ((TVirtualPad*)l->At(0))->cd();
375     h1 = h2->ProjectionX("hdx_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
376     h1->SetYTitle("<dx> [#mum]");
377     h1->SetXTitle("t_{drift} [#mus]");
378     h1->GetXaxis()->SetRange(2, kNTB-1); h1->Draw("pc");
379
380     t->Draw("z:t>>h2y(23, 0.1, 2.4, 25, 0., 2.5)","dy*(1)", "goff");
381     h2 = (TH2F*)gROOT->FindObject("h2y");
382     printf("  const Double_t dy[24][25]={\n");
383     for(Int_t ix=1; ix<=h2->GetNbinsX(); ix++){
384       printf("    {");
385       for(Int_t iy=1; iy<h2->GetNbinsY(); iy++){
386         printf("%6.4f ", h2->GetBinContent(ix, iy));
387       }
388       printf("%6.4f},\n", h2->GetBinContent(ix, h2->GetNbinsY()));
389     }
390     printf("  };\n");
391     ((TVirtualPad*)l->At(1))->cd();
392     h1 = h2->ProjectionX("hdy_pxx"); h1->Scale(1.e4/kND); h1->SetMarkerStyle(24);
393     h1->SetYTitle("<dy> [#mum]");
394     h1->SetXTitle("t_{drift} [#mus]");
395     h1->GetXaxis()->SetRange(2, kNTB-1); h1->Draw("pc");
396
397     return kTRUE;
398   default:
399     break;
400   }
401   AliWarning("No container/data found.");
402   return kFALSE;
403 }
404
405 //_______________________________________________________
406 TObjArray* AliTRDclusterResolution::Histos()
407 {
408 // Retrieve histograms array if already build or build it
409
410   if(fContainer) return fContainer;
411   fContainer = new TObjArray(kNtasks);
412   //fContainer->SetOwner(kTRUE);
413
414   TH3S *h3 = NULL;
415   TObjArray *arr = NULL;
416
417   fContainer->AddAt(arr = new TObjArray(2*AliTRDgeometry::kNlayer), kCenter);
418   arr->SetName("Center");
419   for(Int_t il=0; il<AliTRDgeometry::kNlayer; il++){
420     // add resolution plot for each layer
421     if(!(h3=(TH3S*)gROOT->FindObject(Form("hCenResLy%d", il)))){ 
422       h3 = new TH3S(
423         Form("hCenResLy%d", il), 
424         Form(" ly [%d]", il), 
425         kNTB, fAt->GetBinLowEdge(1), fAt->GetBinUpEdge(kNTB),   // x
426         51, -.51, .51, // y 
427         60, -.3, .3); // dy
428       h3->SetXTitle("x [#mus]");
429       h3->SetYTitle("y [pw]");
430       h3->SetZTitle("#Delta y[cm]");
431     } h3->Reset();
432     arr->AddAt(h3, il);
433     // add Pull plot for each layer
434     if(!(h3=(TH3S*)gROOT->FindObject(Form("hCenPullLy%d", il)))){ 
435       h3 = new TH3S(
436         Form("hCenPullLy%d", il), 
437         Form(" ly [%d]", il), 
438         kNTB, fAt->GetBinLowEdge(1), fAt->GetBinUpEdge(kNTB),   // x
439         51, -.51, .51, // y 
440         60, -4., 4.); // dy
441       h3->SetXTitle("x [#mus]");
442       h3->SetYTitle("y [pw]");
443       h3->SetZTitle("#Delta y/#sigma_{y}");
444     } h3->Reset();
445     arr->AddAt(h3, AliTRDgeometry::kNlayer+il);
446   }
447
448   if(!(h3 = (TH3S*)gROOT->FindObject("Charge"))){
449     h3 = new TH3S("Charge", "dy=f(q)", 50, 2.2, 7.5, 60, -.3, .3, 60, -4., 4.);
450     h3->SetXTitle("log(q) [a.u.]");
451     h3->SetYTitle("#Delta y[cm]");
452     h3->SetZTitle("#Delta y/#sigma_{y}");
453   }
454   fContainer->AddAt(h3, kQRes);
455
456   fContainer->AddAt(arr = new TObjArray(kNTB), kSigm);
457   arr->SetName("Resolution");
458   for(Int_t ix=0; ix<kNTB; ix++){
459     if(!(h3=(TH3S*)gROOT->FindObject(Form("hr_x%02d", ix)))){
460       h3 = new TH3S(
461         Form("hr_x%02d", ix), 
462         Form(" t_{drift}(%3.1f-%3.1f)[#mus]", fAt->GetBinLowEdge(ix+1), fAt->GetBinUpEdge(ix+1)), 
463         kND, 0., 2.5,   // z 
464         35, -.35, .35, // tgp 
465         60, -.3, .3); // dy
466       h3->SetXTitle("z [mm]");
467       h3->SetYTitle("tg#phi");
468       h3->SetZTitle("#Delta y[cm]");
469     }
470     arr->AddAt(h3, ix);
471   }
472
473   fContainer->AddAt(arr = new TObjArray(kNTB), kMean);
474   arr->SetName("Systematics");
475   for(Int_t ix=0; ix<kNTB; ix++){
476     if(!(h3=(TH3S*)gROOT->FindObject(Form("hs_x%02d", ix)))){
477       h3 = new TH3S(
478         Form("hs_x%02d", ix), 
479         Form(" t_{drift}(%3.1f-%3.1f)[#mus]", fAt->GetBinLowEdge(ix+1), fAt->GetBinUpEdge(ix+1)), 
480         kND, 0., 2.5,   // z 
481         35, -.35, .35, // tgp-h tgt 
482         60, -.3, .3); // dy
483       h3->SetXTitle("z [mm]");
484       h3->SetYTitle("tg(#phi) - h*tg(#theta)");
485       h3->SetZTitle("#Delta y[cm]");
486     }
487     arr->AddAt(h3, ix);
488   }
489
490   return fContainer;
491 }
492
493 //_______________________________________________________
494 void AliTRDclusterResolution::UserExec(Option_t *)
495 {
496 // Fill container histograms
497
498   if(!HasExB()) AliWarning("ExB was not set. Call SetExB() before running the task.");
499
500   Int_t det, t;
501   Float_t x, y, z, q, dy, dydx, dzdx, cov[3], covcl[3];
502   TH3S *h3 = NULL;
503
504   // define limits around ExB for which x contribution is negligible
505   const Float_t kDtgPhi = 3.5e-2; //(+- 2 deg)
506
507   TObjArray *arr0 = (TObjArray*)fContainer->At(kCenter);
508   TObjArray *arr1 = (TObjArray*)fContainer->At(kSigm);
509   TObjArray *arr2 = (TObjArray*)fContainer->At(kMean);
510
511   const AliTRDclusterInfo *cli = NULL;
512   TIterator *iter=fInfo->MakeIterator();
513   while((cli=dynamic_cast<AliTRDclusterInfo*>((*iter)()))){
514     cli->GetCluster(det, x, y, z, q, t, covcl);
515     if(fDet>=0 && fDet!=det) continue;
516     
517     dy = cli->GetResolution();
518     cli->GetGlobalPosition(y, z, dydx, dzdx, &cov[0]);
519
520     // resolution as a function of cluster charge
521     // only for phi equal exB 
522     if(TMath::Abs(dydx-fExB) < kDtgPhi){
523       h3 = (TH3S*)fContainer->At(kQRes);
524       h3->Fill(TMath::Log(q), dy, dy/TMath::Sqrt(covcl[0]));
525
526       AliDebug(4, Form("q=%4.0f Log(q)=%6.4f dy[um]=%7.2f pull=%5.2f",q, TMath::Log(q), 1.e4*dy, dy/TMath::Sqrt(covcl[0])));
527     }
528
529     // do not use problematic clusters in resolution analysis
530     // TODO define limits as calibration aware (gain) !!
531     if(q<20. || q>250.) continue;
532
533     x = (t+.5)*fgkTimeBinLength; // conservative approach !!
534
535     // resolution as a function of y displacement from pad center
536     // only for phi equal exB
537     if(TMath::Abs(dydx-fExB) < kDtgPhi/* &&
538        TMath::Abs(x-0.675)<0.225*/){
539       Int_t ly(AliTRDgeometry::GetLayer(det));
540       h3 = (TH3S*)arr0->At(ly);
541       h3->Fill(x, cli->GetYDisplacement(), dy);
542       h3 = (TH3S*)arr0->At(AliTRDgeometry::kNlayer+ly);
543       h3->Fill(x, cli->GetYDisplacement(), dy/TMath::Sqrt(covcl[0]));
544     }
545
546     Int_t ix = fAt->FindBin(x);
547     if(ix==0 || ix == fAt->GetNbins()+1){
548       AliWarning(Form("Drift time %3.1f outside allowed range", x));
549       continue;
550     }
551
552     // fill histo for resolution (sigma)
553     ((TH3S*)arr1->At(ix-1))->Fill(10.*cli->GetAnisochronity(), dydx, dy);
554
555     // fill histo for systematic (mean)
556     ((TH3S*)arr2->At(ix-1))->Fill(10.*cli->GetAnisochronity(), dydx-cli->GetTilt()*dzdx, dy);  
557   }
558   PostData(1, fContainer);
559 }
560
561
562 //_______________________________________________________
563 Bool_t AliTRDclusterResolution::PostProcess()
564 {
565   if(!fContainer) return kFALSE;
566   if(!HasExB()) AliWarning("ExB was not set. Call SetExB() before running the post processing.");
567   
568   TObjArray *arr = NULL;
569   TTree *t=NULL;
570   if(!fResults){
571     TGraphErrors *g = NULL;
572     fResults = new TObjArray(kNtasks);
573     fResults->SetOwner();
574     fResults->AddAt(arr = new TObjArray(3), kQRes);
575     arr->SetOwner();
576     arr->AddAt(g = new TGraphErrors(), 0);
577     g->SetLineColor(kBlue); g->SetMarkerColor(kBlue);
578     g->SetMarkerStyle(7); 
579     arr->AddAt(g = new TGraphErrors(), 1);
580     g->SetLineColor(kRed); g->SetMarkerColor(kRed);
581     g->SetMarkerStyle(23); 
582     arr->AddAt(g = new TGraphErrors(), 2);
583     g->SetLineColor(kGreen); g->SetMarkerColor(kGreen);
584     g->SetMarkerStyle(7); 
585
586     // pad center dependence
587     fResults->AddAt(arr = new TObjArray(AliTRDgeometry::kNlayer+1), kCenter);
588     arr->SetOwner();
589     arr->AddAt(
590     t = new TTree("cent", "dy=f(y,x,ly)"), 0);
591     t->Branch("ly", &fLy, "ly/B");
592     t->Branch("x", &fX, "x/F");
593     t->Branch("y", &fY, "y/F");
594     t->Branch("m", &fR[0], "m[2]/F");
595     t->Branch("s", &fR[2], "s[2]/F");
596     t->Branch("pm", &fP[0], "pm[2]/F");
597     t->Branch("ps", &fP[2], "ps[2]/F");
598     for(Int_t il=1; il<=AliTRDgeometry::kNlayer; il++){
599       arr->AddAt(g = new TGraphErrors(), il);
600       g->SetLineColor(il); g->SetLineStyle(il);
601       g->SetMarkerColor(il);g->SetMarkerStyle(4); 
602     }
603
604
605     fResults->AddAt(t = new TTree("sigm", "dy=f(dw,x,dydx)"), kSigm);
606     t->Branch("t", &fX, "t/F");
607     t->Branch("z", &fZ, "z/F");
608     t->Branch("sx", &fR[0], "sx[2]/F");
609     t->Branch("sy", &fR[2], "sy[2]/F");
610
611
612     fResults->AddAt(t = new TTree("mean", "dy=f(dw,x,dydx - h dzdx)"), kMean);
613     t->Branch("t", &fX, "t/F");
614     t->Branch("z", &fZ, "z/F");
615     t->Branch("dx", &fR[0], "dx[2]/F");
616     t->Branch("dy", &fR[2], "dy[2]/F");
617   } else {
618     TObject *o = NULL;
619     TIterator *iter=fResults->MakeIterator();
620     while((o=((*iter)()))) o->Clear(); // maybe it is wrong but we should never reach this point
621   }
622   
623   // precalculated value of tg^2(alpha_L)
624   Double_t exb2 = fExB*fExB;
625   // square of the mean value of sigma drift length.
626   // has to come from previous calibration 
627   //Double_t sxd2 = 1.;// [mm^2]
628
629   printf("ExB[%e] ExB2[%e]\n", fExB, exb2);
630
631   // process resolution dependency on charge
632   if(HasProcess(kQRes)) ProcessCharge();
633   
634   // process resolution dependency on y displacement
635   if(HasProcess(kCenter)) ProcessCenterPad();
636
637   // process resolution dependency on drift legth and drift cell width
638   if(HasProcess(kSigm)) ProcessSigma();
639
640   // process systematic shift on drift legth and drift cell width
641   if(HasProcess(kMean)) ProcessMean();
642
643   return kTRUE;
644 }
645
646 //_______________________________________________________
647 Bool_t AliTRDclusterResolution::SetExB(Int_t det, Int_t col, Int_t row)
648 {
649   // check OCDB
650   AliCDBManager *cdb = AliCDBManager::Instance();
651   if(cdb->GetRun() < 0){
652     AliError("OCDB manager not properly initialized");
653     return kFALSE;
654   }
655
656   // check magnetic field
657   if(TMath::Abs(AliTracker::GetBz()) < 1.e-10){
658     AliWarning("B=0. Magnetic field may not be initialized. Continue if you know what you are doing !");
659   }
660
661   // set reference detector if any
662   if(det>=0 && det<AliTRDgeometry::kNdet) fDet = det;
663   else det = 0;
664
665   AliTRDcalibDB *fCalibration  = AliTRDcalibDB::Instance();
666   AliTRDCalROC  *fCalVdriftROC = fCalibration->GetVdriftROC(det);
667   const AliTRDCalDet  *fCalVdriftDet = fCalibration->GetVdriftDet();
668
669   fVdrift = fCalVdriftDet->GetValue(det) * fCalVdriftROC->GetValue(col, row);
670   fExB   = AliTRDCommonParam::Instance()->GetOmegaTau(fVdrift);
671   SetBit(kExB);
672   return kTRUE;
673 }
674
675 //_______________________________________________________
676 void AliTRDclusterResolution::SetVisual()
677 {
678   if(fCanvas) return;
679   fCanvas = new TCanvas("clResCanvas", "Cluster Resolution Visualization", 10, 10, 600, 600);
680 }
681
682 //_______________________________________________________
683 void AliTRDclusterResolution::ProcessCharge()
684 {
685 // Resolution as a function of cluster charge.
686 //
687 // As described in the function ProcessCenterPad() the error parameterization for clusters for phi = a_L can be 
688 // written as:
689 // BEGIN_LATEX
690 // #sigma_{y}^{2} = #sigma_{y}^{2}|_{B=0} + tg^{2}(#alpha_{L})*#sigma_{x}^{2}
691 // END_LATEX
692 // with the contribution in case of B=0 given by:
693 // BEGIN_LATEX
694 // #sigma_{y}|_{B=0} = #sigma_{diff}*Gauss(0, s_{ly}) + #delta_{#sigma}(q)
695 // END_LATEX
696 // which further can be simplified to:
697 // BEGIN_LATEX
698 // <#sigma_{y}|_{B=0}>(q) = <#sigma_{y}> + #delta_{#sigma}(q)
699 // <#sigma_{y}> = #int{f(q)#sigma_{y}dq}
700 // END_LATEX
701 // The results for s_y and f(q) are displayed below:
702 //Begin_Html
703 //<img src="TRD/clusterQerror.gif">
704 //End_Html
705 // The function has to extended to accomodate gain calibration scalling and errors.
706 //
707 // Author
708 // Alexandru Bercuci <A.Bercuci@gsi.de>
709
710   TH2I *h2 = NULL;
711   if(!(h2 = (TH2I*)fContainer->At(kQRes))) {
712     AliWarning("Missing dy=f(Q) histo");
713     return;
714   }
715   TF1 f("f", "gaus", -.5, .5);
716   TAxis *ax = NULL;
717   TH1D *h1 = NULL;
718
719   // compute mean error on x
720   Double_t s2x = 0.; 
721   for(Int_t ix=5; ix<kNTB; ix++){
722     // retrieve error on the drift length
723     s2x += AliTRDcluster::GetSX(ix);
724   }
725   s2x /= (kNTB-5); s2x *= s2x;
726   Double_t exb2 = fExB*fExB;
727
728   TObjArray *arr = (TObjArray*)fResults->At(kQRes);
729   TGraphErrors *gqm = (TGraphErrors*)arr->At(0);
730   TGraphErrors *gqs = (TGraphErrors*)arr->At(1);
731   TGraphErrors *gqp = (TGraphErrors*)arr->At(2);
732   Double_t q, n = 0., entries;
733   ax = h2->GetXaxis();
734   for(Int_t ix=1; ix<=ax->GetNbins(); ix++){
735     q = TMath::Exp(ax->GetBinCenter(ix));
736     if(q<20. || q>250.) continue; // ?!
737
738     h1 = h2->ProjectionY("py", ix, ix);
739     entries = h1->GetEntries();
740     if(entries < 50) continue;
741     Adjust(&f, h1);
742     h1->Fit(&f, "Q");
743
744     // Fill sy^2 = f(q)
745     Int_t ip = gqm->GetN();
746     gqm->SetPoint(ip, q, 1.e4*f.GetParameter(1));
747     gqm->SetPointError(ip, 0., 1.e4*f.GetParError(1));
748
749     // correct sigma for ExB effect
750     gqs->SetPoint(ip, q, 1.e4*(f.GetParameter(2)*f.GetParameter(2)-exb2*s2x));
751     gqs->SetPointError(ip, 0., 1.e4*f.GetParError(2)*f.GetParameter(2));
752
753     // save probability
754     n += entries;
755     gqp->SetPoint(ip, q, entries);
756     gqp->SetPointError(ip, 0., 0./*TMath::Sqrt(entries)*/);
757   } 
758
759   // normalize probability and get mean sy
760   Double_t sm = 0., sy;
761   for(Int_t ip=gqp->GetN(); ip--;){
762     gqp->GetPoint(ip, q, entries);
763     entries/=n;
764     gqp->SetPoint(ip, q, 1.e3*entries);
765     gqs->GetPoint(ip, q, sy);
766     sm += entries*sy;
767   }
768
769   // error parametrization s(q) = <sy> + b(1/q-1/q0)
770   TF1 fq("fq", "[0] + [1]/x", 20., 250.);
771   gqs->Fit(&fq/*, "W"*/);
772   printf("sm=%f [0]=%f [1]=%f\n", 1.e-4*sm, fq.GetParameter(0), fq.GetParameter(1));
773   printf("  const Float_t sq0inv = %f; // [1/q0]\n", (sm-fq.GetParameter(0))/fq.GetParameter(1));
774   printf("  const Float_t sqb    = %f; // [cm]\n", 1.e-4*fq.GetParameter(1));
775 }
776
777 //_______________________________________________________
778 void AliTRDclusterResolution::ProcessCenterPad()
779 {
780 // Resolution as a function of y displacement from pad center and drift length.
781 //
782 // Since the error parameterization of cluster r-phi position can be written as (see AliTRDcluster::SetSigmaY2()):
783 // BEGIN_LATEX
784 // #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
785 // END_LATEX
786 // one can see that for phi = a_L one gets the following expression:
787 // BEGIN_LATEX
788 // #sigma_{y}^{2} = #sigma_{y}^{2}|_{B=0} + tg^{2}(#alpha_{L})*#sigma_{x}^{2}
789 // END_LATEX
790 // where we have explicitely marked the remaining term in case of absence of magnetic field. Thus one can use the 
791 // previous equation to estimate s_y for B=0 and than by comparing in magnetic field conditions one can get the s_x.
792 // This is a simplified method to determine the error parameterization for s_x and s_y as compared to the one 
793 // implemented in ProcessSigma(). For more details on cluster error parameterization please see also 
794 // AliTRDcluster::SetSigmaY2()
795 // 
796 // The representation of dy=f(y_cen, x_drift| layer) can be also used to estimate the systematic shift in the r-phi 
797 // coordinate resulting from imperfection in the cluster shape parameterization. From the expresion of the shift derived 
798 // in ProcessMean() with phi=exb one gets: 
799 // BEGIN_LATEX
800 // <#Delta y>= <#delta x> * (tg(#alpha_{L})-h*dz/dx) + <#delta y - #delta x * tg(#alpha_{L})>
801 // <#Delta y>(y_{cen})= -h*<#delta x>(x_{drift}, q_{cl}) * dz/dx + #delta y(y_{cen}, ...)
802 // END_LATEX
803 // where all dependences are made explicit. This last expression can be used in two ways:
804 //   - by average on the dz/dx we can determine directly dy (the method implemented here) 
805 //   - by plotting as a function of dzdx one can determine both dx and dy components in an independent method.
806 //Begin_Html
807 //<img src="TRD/clusterYcorr.gif">
808 //End_Html
809 // Author
810 // Alexandru Bercuci <A.Bercuci@gsi.de>
811
812   TObjArray *arr = (TObjArray*)fContainer->At(kCenter);
813   if(!arr) {
814     AliWarning("Missing dy=f(y | x, ly) container");
815     return;
816   }
817   Double_t exb2 = fExB*fExB;
818   Float_t s[AliTRDgeometry::kNlayer];
819   TF1 f("f", "gaus", -.5, .5);
820   TF1 fp("fp", "gaus", -3.5, 3.5);
821
822   TH1D *h1 = NULL; TH2F *h2 = NULL; TH3S *h3r=NULL, *h3p=NULL;
823   TObjArray *arrRes = (TObjArray*)fResults->At(kCenter);
824   TTree *t = (TTree*)arrRes->At(0);
825   TGraphErrors *gs = NULL;
826   TAxis *ax = NULL;
827
828   printf("  const Float_t lSy[6][24] = {\n      {");
829   const Int_t nl = AliTRDgeometry::kNlayer;
830   for(Int_t il=0; il<nl; il++){
831     if(!(h3r = (TH3S*)arr->At(il))) continue;
832     if(!(h3p = (TH3S*)arr->At(nl+il))) continue;
833     gs = (TGraphErrors*)arrRes->At(il+1);
834     fLy = il;
835 //    printf("Ly[%d]\n", il);
836     for(Int_t ix=1; ix<=h3r->GetXaxis()->GetNbins(); ix++){
837       ax = h3r->GetXaxis(); ax->SetRange(ix, ix);
838       ax = h3p->GetXaxis(); ax->SetRange(ix, ix);
839       fX  = ax->GetBinCenter(ix);
840 //      printf("  x[%2d]=%4.2f\n", ix, fX);
841       for(Int_t iy=1; iy<=h3r->GetYaxis()->GetNbins(); iy++){
842         ax = h3r->GetYaxis(); ax->SetRange(iy, iy);
843         ax = h3p->GetYaxis(); ax->SetRange(iy, iy);
844         fY  = ax->GetBinCenter(iy);
845 //        printf("    y[%2d]=%5.2f\n", iy, fY);
846         // finish navigation in the HnSparse
847
848         h1 = (TH1D*)h3r->Project3D("z");
849         Int_t entries = (Int_t)h1->Integral();
850         if(entries < 50) continue;
851         //Adjust(&f, h1);
852         h1->Fit(&f, "QN");
853     
854         // Fill sy,my=f(y_w,x,ly)
855         fR[0] = f.GetParameter(1); fR[1] = f.GetParError(1);
856         fR[2] = f.GetParameter(2); fR[3] = f.GetParError(2);
857
858         h1 = (TH1D*)h3p->Project3D("z");
859         h1->Fit(&fp, "QN");
860         fP[0] = fp.GetParameter(1); fP[1] = fp.GetParError(1);
861         fP[2] = fp.GetParameter(2); fP[3] = fp.GetParError(2);
862
863         //printf("ly[%d] x[%3.1f] y[%+5.2f] m[%5.3f] s[%5.3f] \n", fLy, fX, fY, fR[0], fR[2]);
864         t->Fill();
865
866
867       }
868     }
869     t->Draw("y:x>>h(24, 0., 2.4, 51, -.51, .51)",
870             Form("s[0]*(ly==%d&&abs(m[0])<1.e-1)", fLy),
871             "goff");
872     h2=(TH2F*)gROOT->FindObject("h");
873     f.FixParameter(1, 0.);
874     Int_t n = h2->GetXaxis()->GetNbins(), nn(0); s[il]=0.;
875     printf("    {");
876     for(Int_t ix=1; ix<=n; ix++){
877       ax = h2->GetXaxis();
878       fX  = ax->GetBinCenter(ix);
879       h1 = h2->ProjectionY("hCenPy", ix, ix);
880       //if((Int_t)h1->Integral() < 1.e-10) continue; 
881
882       // Apply lorentz angle correction
883       // retrieve error on the drift length
884       Double_t s2x = AliTRDcluster::GetSX(ix-1); s2x *= s2x;
885       Int_t nnn = 0;
886       for(Int_t iy=1; iy<=h1->GetNbinsX(); iy++){
887         Double_t s2 = h1->GetBinContent(iy); s2*= s2;
888         // sigma square corrected for Lorentz angle
889         // s2 = s2_y(y_w,x)+exb2*s2_x
890         Double_t sy = TMath::Sqrt(TMath::Max(s2 - exb2*s2x, Double_t(0.)));
891         if(sy<1.e-20) continue;
892         h1->SetBinContent(iy, sy); nnn++;
893         printf("s[%6.2f] sx[%6.2f] sy[%6.2f]\n",
894         1.e4*TMath::Sqrt(s2), 1.e4*TMath::Abs(fExB*AliTRDcluster::GetSX(ix-1)), 
895         1.e4*h1->GetBinContent(iy));
896       }
897       // do fit only if enough data
898       Double_t sPRF = 0.;
899       if(nnn>5){
900         h1->Fit(&f, "QN");
901         sPRF = f.GetParameter(2);
902         nn++;
903       }
904       s[il]+=sPRF;
905       printf("%6.4f,%s", sPRF, ix%6?" ":"\n     ");
906       Int_t jx = gs->GetN();
907       gs->SetPoint(jx, fX, 1.e4*sPRF);
908       gs->SetPointError(jx, 0., 0./*f.GetParError(0)*/);
909     }
910     printf("\b},\n");
911     s[il]/=nn;
912
913     f.ReleaseParameter(2);
914
915
916     if(!fCanvas) continue;
917     h2->Draw("lego2fb");
918     fCanvas->Modified(); fCanvas->Update();
919     if(IsSaveAs()) fCanvas->SaveAs(Form("Figures/ProcessCenter_ly[%d].gif", fLy));
920     else gSystem->Sleep(100);
921   }
922   printf("  };\n");
923   printf("  const Float_t lPRF[] = {"
924     "%5.3f, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f};\n",
925     s[0], s[1], s[2], s[3], s[4], s[5]);
926 }
927
928 //_______________________________________________________
929 void AliTRDclusterResolution::ProcessSigma()
930 {
931 // As the r-phi coordinate is the only one which is measured by the TRD detector we have to rely on it to
932 // estimate both the radial (x) and r-phi (y) errors. This method is based on the following assumptions. 
933 // The measured error in the y direction is the sum of the intrinsic contribution of the r-phi measurement
934 // with the contribution of the radial measurement - because x is not a parameter of Alice track model (Kalman).
935 // BEGIN_LATEX
936 // #sigma^{2}|_{y} = #sigma^{2}_{y*} + #sigma^{2}_{x*}   
937 // END_LATEX
938 // In the general case 
939 // BEGIN_LATEX
940 // #sigma^{2}_{y*} = #sigma^{2}_{y} + tg^{2}(#alpha_{L})#sigma^{2}_{x_{drift}}   
941 // #sigma^{2}_{x*} = tg^{2}(#phi - #alpha_{L})*(#sigma^{2}_{x_{drift}} + #sigma^{2}_{x_{0}} + tg^{2}(#alpha_{L})*x^{2}/12)
942 // END_LATEX
943 // where we have explicitely show the lorentz angle correction on y and the projection of radial component on the y
944 // direction through the track angle in the bending plane (phi). Also we have shown that the radial component in the
945 // last equation has twp terms, the drift and the misalignment (x_0). For ideal geometry or known misalignment one 
946 // can solve the equation
947 // BEGIN_LATEX
948 // #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}]
949 // END_LATEX
950 // by fitting a straight line:
951 // BEGIN_LATEX
952 // #sigma^{2}|_{y} = a(x_{cl}, z_{cl}) * tg^{2}(#phi - #alpha_{L}) + b(x_{cl}, z_{cl})
953 // END_LATEX
954 // the error parameterization will be given by:
955 // BEGIN_LATEX
956 // #sigma_{x} (x_{cl}, z_{cl}) = #sqrt{a(x_{cl}, z_{cl}) - tg^{2}(#alpha_{L})*x^{2}/12}
957 // #sigma_{y} (x_{cl}, z_{cl}) = #sqrt{b(x_{cl}, z_{cl}) - #sigma^{2}_{x} (x_{cl}, z_{cl}) * tg^{2}(#alpha_{L})}
958 // END_LATEX
959 // Below there is an example of such dependency. 
960 //Begin_Html
961 //<img src="TRD/clusterSigmaMethod.gif">
962 //End_Html
963 //
964 // The error parameterization obtained by this method are implemented in the functions AliTRDcluster::GetSX() and
965 // AliTRDcluster::GetSYdrift(). For an independent method to determine s_y as a function of drift length check the 
966 // function ProcessCenterPad(). One has to keep in mind that while this method return the mean s_y over the distance
967 // to pad center distribution the other method returns the *STANDARD* value at center=0 (maximum). To recover the 
968 // standard value one has to solve the obvious equation:
969 // BEGIN_LATEX
970 // #sigma_{y}^{STANDARD} = #frac{<#sigma_{y}>}{#int{s exp(s^{2}/#sigma) ds}}
971 // END_LATEX
972 // with "<s_y>" being the value calculated here and "sigma" the width of the s_y distribution calculated in 
973 // ProcessCenterPad().
974 //  
975 // Author
976 // Alexandru Bercuci <A.Bercuci@gsi.de>
977
978   TObjArray *arr = (TObjArray*)fContainer->At(kSigm);
979   if(!arr){
980     AliWarning("Missing dy=f(x_d, d_w) container");
981     return;
982   }
983
984   // init visualization
985   TGraphErrors *ggs = NULL;
986   TGraph *line = NULL;
987   if(fCanvas){
988     ggs = new TGraphErrors();
989     line = new TGraph();
990     line->SetLineColor(kRed);line->SetLineWidth(2);
991   }
992
993   // init logistic support
994   TF1 f("f", "gaus", -.5, .5);
995   TLinearFitter gs(1,"pol1");
996   TH1 *hFrame=NULL;
997   TH1D *h1 = NULL; TH3S *h3=NULL;
998   TAxis *ax = NULL;
999   Double_t exb2 = fExB*fExB, x;
1000   AliTRDcluster c;
1001   TTree *t = (TTree*)fResults->At(kSigm);
1002   for(Int_t ix=0; ix<kNTB; ix++){
1003     if(!(h3=(TH3S*)arr->At(ix))) continue;
1004     c.SetPadTime(ix);
1005     x = c.GetXloc(0., 1.5);
1006     fX= fAt->GetBinCenter(ix+1);
1007     for(Int_t iz=1; iz<=h3->GetXaxis()->GetNbins(); iz++){
1008       ax = h3->GetXaxis();
1009       ax->SetRange(iz, iz);
1010       fZ = ax->GetBinCenter(iz);
1011
1012       // reset visualization
1013       if(fCanvas){ 
1014         new(ggs) TGraphErrors();
1015         ggs->SetMarkerStyle(7);
1016       }
1017       gs.ClearPoints();
1018
1019       for(Int_t ip=1; ip<=h3->GetYaxis()->GetNbins(); ip++){
1020         ax = h3->GetYaxis();
1021         ax->SetRange(ip, ip); 
1022         Double_t tgl = ax->GetBinCenter(ip);
1023         // finish navigation in the HnSparse
1024
1025         //if(TMath::Abs(dydx)>0.18) continue;
1026         Double_t tgg = (tgl-fExB)/(1.+tgl*fExB);
1027         Double_t tgg2 = tgg*tgg;
1028
1029         h1 = (TH1D*)h3->Project3D("z");
1030         Int_t entries = (Int_t)h1->Integral();
1031         if(entries < 50) continue;
1032         //Adjust(&f, h1);
1033         h1->Fit(&f, "QN");
1034
1035         Double_t s2  = f.GetParameter(2)*f.GetParameter(2);
1036         Double_t s2e = 2.*f.GetParameter(2)*f.GetParError(2);
1037         // Fill sy^2 = f(tg^2(phi-a_L))
1038         gs.AddPoint(&tgg2, s2, s2e);
1039
1040         if(!ggs) continue;
1041         Int_t jp = ggs->GetN();
1042         ggs->SetPoint(jp, tgg2, s2);
1043         ggs->SetPointError(jp, 0., s2e);
1044       }
1045       // TODO here a more robust fit method has to be provided
1046       // for which lower boundaries on the parameters have to 
1047       // be imposed. Unfortunately the Minuit fit does not work 
1048       // for the TGraph in the case of B not 0.
1049       if(gs.Eval()) continue;
1050
1051       fR[0] = gs.GetParameter(1) - x*x*exb2/12.;
1052       printf("s2x+x2=%f ang=%f s2x=%f\n", gs.GetParameter(1), x*x*exb2/12., fR[0]);
1053       fR[0] = TMath::Max(fR[0], Float_t(4.e-4)); 
1054
1055       // s^2_y  = s0^2_y + tg^2(a_L) * s^2_x
1056       // s0^2_y = f(D_L)*x + s_PRF^2 
1057       fR[2]= gs.GetParameter(0)-exb2*fR[0];
1058       printf("s2y+s2x=%f s2y=%f\n", fR[0], fR[2]);
1059       fR[2] = TMath::Max(fR[2], Float_t(2.5e-5)); 
1060       fR[0] = TMath::Sqrt(fR[0]);
1061       fR[1] = .5*gs.GetParError(1)/fR[0];
1062       fR[2] = TMath::Sqrt(fR[2]);
1063       fR[3] = gs.GetParError(0)+exb2*exb2*gs.GetParError(1);
1064       t->Fill();
1065       printf("    xd=%4.2f[cm] sx=%6.1f[um] sy=%5.1f[um]\n", x, 1.e4*fR[0], 1.e4*fR[2]);
1066
1067       if(!fCanvas) continue;
1068       fCanvas->cd(); fCanvas->SetLogx(); //fCanvas->SetLogy();
1069       if(!hFrame){ 
1070         fCanvas->SetMargin(0.15, 0.01, 0.1, 0.01);
1071         hFrame=new TH1I("hFrame", "", 100, 0., .3);
1072         hFrame->SetMinimum(0.);hFrame->SetMaximum(.005);
1073         hFrame->SetXTitle("tg^{2}(#phi-#alpha_{L})");
1074         hFrame->SetYTitle("#sigma^{2}y[cm^{2}]");
1075         hFrame->GetYaxis()->SetTitleOffset(2.);
1076         hFrame->SetLineColor(1);hFrame->SetLineWidth(1);
1077         hFrame->Draw();
1078       } else hFrame->Reset();
1079       Double_t xx = 0., dxx=.2/50;
1080       for(Int_t ip=0;ip<50;ip++){ 
1081         line->SetPoint(ip, xx,  gs.GetParameter(0)+xx*gs.GetParameter(1)); 
1082         xx+=dxx;
1083       }
1084       ggs->Draw("pl"); line->Draw("l");
1085       fCanvas->Modified(); fCanvas->Update();
1086       if(IsSaveAs()) fCanvas->SaveAs(Form("Figures/ProcessSigma_z[%5.3f]_x[%5.3f].gif", fZ, fX));
1087       else gSystem->Sleep(100);
1088     }
1089   }
1090   return;
1091 }
1092
1093 //_______________________________________________________
1094 void AliTRDclusterResolution::ProcessMean()
1095 {
1096 // By this method the cluster shift in r-phi and radial directions can be estimated by comparing with the MC.
1097 // The resolution of the cluster corrected for pad tilt with respect to MC in the r-phi (measuring) plane can be 
1098 // expressed by:
1099 // BEGIN_LATEX
1100 // #Delta y=w - y_{MC}(x_{cl})
1101 // w = y_{cl}^{'} + h*(z_{MC}(x_{cl})-z_{cl})
1102 // y_{MC}(x_{cl}) = y_{0} - dy/dx*x_{cl}
1103 // z_{MC}(x_{cl}) = z_{0} - dz/dx*x_{cl}
1104 // y_{cl}^{'} = y_{cl}-x_{cl}*tg(#alpha_{L})
1105 // END_LATEX
1106 // where x_cl is the drift length attached to a cluster, y_cl is the r-phi coordinate of the cluster measured by
1107 // charge sharing on adjacent pads and y_0 and z_0 are MC reference points (as example the track references at 
1108 // entrance/exit of a chamber). If we suppose that both r-phi (y) and radial (x) coordinate of the clusters are 
1109 // affected by errors we can write
1110 // BEGIN_LATEX
1111 // x_{cl} = x_{cl}^{*} + #delta x 
1112 // y_{cl} = y_{cl}^{*} + #delta y 
1113 // END_LATEX 
1114 // where the starred components are the corrected values. Thus by definition the following quantity
1115 // BEGIN_LATEX
1116 // #Delta y^{*}= w^{*} - y_{MC}(x_{cl}^{*})
1117 // END_LATEX
1118 // has 0 average over all dependency. Using this decomposition we can write:
1119 // BEGIN_LATEX
1120 // <#Delta y>=<#Delta y^{*}> + <#delta x * (dy/dx-h*dz/dx) + #delta y - #delta x * tg(#alpha_{L})>
1121 // END_LATEX
1122 // which can be transformed to the following linear dependence:
1123 // BEGIN_LATEX
1124 // <#Delta y>= <#delta x> * (dy/dx-h*dz/dx) + <#delta y - #delta x * tg(#alpha_{L})>
1125 // END_LATEX
1126 // if expressed as function of dy/dx-h*dz/dx. Furtheremore this expression can be plotted for various clusters
1127 // i.e. we can explicitely introduce the diffusion (x_cl) and drift cell - anisochronity (z_cl) dependences. From 
1128 // plotting this dependence and linear fitting it with:
1129 // BEGIN_LATEX
1130 // <#Delta y>= a(x_{cl}, z_{cl}) * (dy/dx-h*dz/dx) + b(x_{cl}, z_{cl})
1131 // END_LATEX
1132 // the systematic shifts will be given by:
1133 // BEGIN_LATEX
1134 // #delta x (x_{cl}, z_{cl}) = a(x_{cl}, z_{cl})
1135 // #delta y (x_{cl}, z_{cl}) = b(x_{cl}, z_{cl}) + a(x_{cl}, z_{cl}) * tg(#alpha_{L})
1136 // END_LATEX
1137 // Below there is an example of such dependency. 
1138 //Begin_Html
1139 //<img src="TRD/clusterShiftMethod.gif">
1140 //End_Html
1141 //
1142 // The occurance of the radial shift is due to the following conditions 
1143 //   - the approximation of a constant drift velocity over the drift length (larger drift velocities close to 
1144 //     cathode wire plane)
1145 //   - the superposition of charge tails in the amplification region (first clusters appear to be located at the 
1146 //     anode wire)
1147 //   - the superposition of charge tails in the drift region (shift towards anode wire)
1148 //   - diffusion effects which convolute with the TRF thus enlarging it
1149 //   - approximate knowledge of the TRF (approximate measuring in test beam conditions) 
1150 // 
1151 // The occurance of the r-phi shift is due to the following conditions 
1152 //   - approximate model for cluster shape (LUT)
1153 //   - rounding-up problems
1154 //
1155 // The numerical results for ideal simulations for the radial and r-phi shifts are displayed below and used 
1156 // for the cluster reconstruction (see the functions AliTRDcluster::GetXcorr() and AliTRDcluster::GetYcorr()). 
1157 //Begin_Html
1158 //<img src="TRD/clusterShiftX.gif">
1159 //<img src="TRD/clusterShiftY.gif">
1160 //End_Html
1161 // More details can be found in the presentation given during the TRD
1162 // software meeting at the end of 2008 and beginning of year 2009, published on indico.cern.ch.
1163 // 
1164 // Author 
1165 // Alexandru Bercuci <A.Bercuci@gsi.de>
1166
1167
1168  
1169   TObjArray *arr = (TObjArray*)fContainer->At(kMean);
1170   if(!arr){
1171     AliWarning("Missing dy=f(x_d, d_w) container");
1172     return;
1173   }
1174
1175   // init logistic support
1176   TF1 f("f", "gaus", -.5, .5);
1177   TF1 line("l", "[0]+[1]*x", -.15, .15);
1178   TGraphErrors *gm = new TGraphErrors();
1179   TH1 *hFrame=NULL;
1180   TH1D *h1 = NULL; TH3S *h3 =NULL;
1181   TAxis *ax = NULL;
1182   Double_t x;
1183
1184   AliTRDcluster c;
1185   TTree *t = (TTree*)fResults->At(kMean);
1186   for(Int_t ix=0; ix<kNTB; ix++){
1187     if(!(h3=(TH3S*)arr->At(ix))) continue;
1188     c.SetPadTime(ix);
1189     x = c.GetXloc(0., 1.5);
1190     fX= fAt->GetBinCenter(ix+1);
1191     for(Int_t iz=1; iz<=h3->GetXaxis()->GetNbins(); iz++){
1192       ax = h3->GetXaxis();
1193       ax->SetRange(iz, iz);
1194       fZ = ax->GetBinCenter(iz);
1195
1196       // reset fitter
1197       new(gm) TGraphErrors();
1198       gm->SetMarkerStyle(7);
1199
1200       for(Int_t ip=1; ip<=h3->GetYaxis()->GetNbins(); ip++){
1201         ax = h3->GetYaxis();
1202         ax->SetRange(ip, ip); 
1203         Double_t tgl = ax->GetBinCenter(ip);
1204         // finish navigation in the HnSparse
1205
1206         h1 = (TH1D*)h3->Project3D("z");
1207         Int_t entries = (Int_t)h1->Integral();
1208         if(entries < 50) continue;
1209         //Adjust(&f, h1);
1210         h1->Fit(&f, "QN");
1211
1212         // Fill <Dy> = f(dydx - h*dzdx)
1213         Int_t jp = gm->GetN();
1214         gm->SetPoint(jp, tgl, f.GetParameter(1));
1215         gm->SetPointError(jp, 0., f.GetParError(1));
1216       }
1217       if(gm->GetN()<4) continue;
1218
1219       gm->Fit(&line, "QN");
1220       fR[0] = line.GetParameter(1); // dx
1221       fR[1] = line.GetParError(1);
1222       fR[2] = line.GetParameter(0) + fExB*fR[0]; // xs = dy - tg(a_L)*dx
1223       t->Fill();
1224       printf("    xd=%4.2f[cm] dx=%6.2f[um] dy=%6.2f[um]\n", x, 1.e4*fR[0], 1.e4*fR[2]);
1225
1226       if(!fCanvas) continue;
1227       fCanvas->cd();
1228       if(!hFrame){ 
1229         fCanvas->SetMargin(0.1, 0.02, 0.1, 0.01);
1230         hFrame=new TH1I("hFrame", "", 100, -.3, .3);
1231         hFrame->SetMinimum(-.1);hFrame->SetMaximum(.1);
1232         hFrame->SetXTitle("tg#phi-htg#theta");
1233         hFrame->SetYTitle("#Delta y[cm]");
1234         hFrame->GetYaxis()->SetTitleOffset(1.5);
1235         hFrame->SetLineColor(1);hFrame->SetLineWidth(1);
1236         hFrame->Draw();
1237       } else hFrame->Reset();
1238       gm->Draw("pl"); line.Draw("same");
1239       fCanvas->Modified(); fCanvas->Update();
1240       if(IsSaveAs()) fCanvas->SaveAs(Form("Figures/ProcessMean_Z[%5.3f]_X[%5.3f].gif", fZ, fX));
1241       else gSystem->Sleep(100);
1242     }
1243   }
1244 }