]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDCalibraFit.cxx
- fix for TRD on-line track matching with tracks having TPCrefit but not TPCout ...
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibraFit.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 /////////////////////////////////////////////////////////////////////////////////
19 //                                                                             
20 // AliTRDCalibraFit                                                               
21 //                                                                             
22 // This class is for the TRD calibration of the relative gain factor, the drift velocity,
23 // the time 0 and the pad response function. It fits the histos.       
24 // The 2D histograms or vectors (first converted in 1D histos) will be fitted  
25 // if they have enough entries, otherwise the (default) value of the choosen database 
26 // will be put. For the relative gain calibration the resulted factors will be globally 
27 // normalized to the gain factors of the choosen database. It unables to precise     
28 // previous calibration procedure.
29 // The function SetDebug enables the user to see:                                     
30 // _fDebug = 0: nothing, only the values are written in the tree if wanted
31 // _fDebug = 1: only the fit of the choosen calibration group fFitVoir (SetFitVoir)
32 // _fDebug = 2: a comparaison of the coefficients found and the default values 
33 //              in the choosen database.
34 //              fCoef , histogram of the coefs as function of the calibration group number
35 //              fDelta , histogram of the relative difference of the coef with the default
36 //                        value in the database as function of the calibration group number
37 //              fError , dirstribution of this relative difference
38 // _fDebug = 3: The coefficients in the choosen detector fDet (SetDet) as function of the
39 //              pad row and col number
40 // _fDebug = 4; The coeffcicients in the choosen detector fDet (SetDet) like in the 3 but with
41 //              also the comparaison histograms of the 1 for this detector
42 //
43 //                            
44 // Author:
45 //   R. Bailhache (R.Bailhache@gsi.de)
46 //                            
47 //////////////////////////////////////////////////////////////////////////////////////
48
49 #include <TLine.h>
50 #include <TH1I.h>
51 #include <TStyle.h>
52 #include <TProfile2D.h>
53 #include <TCanvas.h>
54 #include <TGraphErrors.h>
55 #include <TObjArray.h>
56 #include <TH1.h>
57 #include <TH1F.h>
58 #include <TF1.h>
59 #include <TAxis.h>
60 #include <TMath.h>
61 #include <TDirectory.h>
62 #include <TTreeStream.h>
63 #include <TLinearFitter.h>
64 #include <TVectorD.h>
65 #include <TROOT.h>
66 #include <TString.h>
67 #include <TLine.h>
68
69 #include "AliLog.h"
70 #include "AliMathBase.h"
71
72 #include "AliTRDCalibraFit.h"
73 #include "AliTRDCalibraMode.h"
74 #include "AliTRDCalibraVector.h"
75 #include "AliTRDCalibraVdriftLinearFit.h"
76 #include "AliTRDCalibraExbAltFit.h"
77 #include "AliTRDcalibDB.h"
78 #include "AliTRDgeometry.h"
79 #include "AliTRDpadPlane.h"
80 #include "AliTRDgeometry.h"
81 #include "AliTRDCommonParam.h"
82 #include "./Cal/AliTRDCalROC.h"
83 #include "./Cal/AliTRDCalPad.h"
84 #include "./Cal/AliTRDCalDet.h"
85
86
87 ClassImp(AliTRDCalibraFit)
88
89 AliTRDCalibraFit* AliTRDCalibraFit::fgInstance = 0;
90 Bool_t AliTRDCalibraFit::fgTerminated = kFALSE;
91
92 //_____________singleton implementation_________________________________________________
93 AliTRDCalibraFit *AliTRDCalibraFit::Instance()
94 {
95   //
96   // Singleton implementation
97   //
98
99   if (fgTerminated != kFALSE) {
100     return 0;
101   }
102
103   if (fgInstance == 0) {
104     fgInstance = new AliTRDCalibraFit();
105   }
106
107   return fgInstance;
108
109 }
110 //______________________________________________________________________________________
111 void AliTRDCalibraFit::Terminate()
112 {
113   //
114   // Singleton implementation
115   // Deletes the instance of this class
116   //
117
118   fgTerminated = kTRUE;
119
120   if (fgInstance != 0) {
121     delete fgInstance;
122     fgInstance = 0;
123   }
124
125 }
126 //______________________________________________________________________________________
127 AliTRDCalibraFit::AliTRDCalibraFit()
128   :TObject()
129   ,fGeo(0)
130   ,fNumberOfBinsExpected(0)
131   ,fMethod(0)
132   ,fBeginFitCharge(3.5)
133   ,fOutliersFitChargeLow(0.03)
134   ,fOutliersFitChargeHigh(0.80)
135   ,fFitPHPeriode(1)
136   ,fTakeTheMaxPH(kTRUE)
137   ,fT0Shift0(0.124797)
138   ,fT0Shift1(0.267451)
139   ,fRangeFitPRF(1.0)
140   ,fAccCDB(kFALSE)
141   ,fMinEntries(800)
142   ,fRebin(1)
143   ,fScaleGain(0.02431)
144   ,fNumberFit(0)
145   ,fNumberFitSuccess(0)
146   ,fNumberEnt(0)
147   ,fStatisticMean(0.0)
148   ,fDebugStreamer(0x0)
149   ,fDebugLevel(0)
150   ,fFitVoir(0)
151   ,fMagneticField(0.5)
152   ,fCalibraMode(new AliTRDCalibraMode())
153   ,fCurrentCoefE(0.0)
154   ,fCurrentCoefE2(0.0)
155   ,fDect1(0)
156   ,fDect2(0)
157   ,fScaleFitFactor(0.0)
158   ,fEntriesCurrent(0)
159   ,fCountDet(0)
160   ,fCount(0)
161   ,fNbDet(0)
162   ,fCalDet(0x0)
163   ,fCalROC(0x0)
164   ,fCalDet2(0x0)
165   ,fCalROC2(0x0)
166   ,fCalDetVdriftUsed(0x0)
167   ,fCalDetExBUsed(0x0)
168   ,fCurrentCoefDetector(0x0)
169   ,fCurrentCoefDetector2(0x0)
170   ,fVectorFit(0)
171   ,fVectorFit2(0)
172 {
173   //
174   // Default constructor
175   //
176
177   fGeo         = new AliTRDgeometry();  
178  
179   // Current variables initialised
180   for (Int_t k = 0; k < 2; k++) {
181     fCurrentCoef[k]  = 0.0;
182     fCurrentCoef2[k] = 0.0;
183   }
184   for (Int_t i = 0; i < 3; i++) {
185     fPhd[i]          = 0.0;
186     fDet[i]          = 0;
187   }
188  
189 }
190 //______________________________________________________________________________________
191 AliTRDCalibraFit::AliTRDCalibraFit(const AliTRDCalibraFit &c)
192 :TObject(c)
193 ,fGeo(0)
194 ,fNumberOfBinsExpected(c.fNumberOfBinsExpected)
195 ,fMethod(c.fMethod)
196 ,fBeginFitCharge(c.fBeginFitCharge)
197 ,fOutliersFitChargeLow(c.fOutliersFitChargeLow)
198 ,fOutliersFitChargeHigh(c.fOutliersFitChargeHigh)
199 ,fFitPHPeriode(c.fFitPHPeriode)
200 ,fTakeTheMaxPH(c.fTakeTheMaxPH)
201 ,fT0Shift0(c.fT0Shift0)
202 ,fT0Shift1(c.fT0Shift1)
203 ,fRangeFitPRF(c.fRangeFitPRF)
204 ,fAccCDB(c.fAccCDB)
205 ,fMinEntries(c.fMinEntries)
206 ,fRebin(c.fRebin)
207 ,fScaleGain(c.fScaleGain)
208 ,fNumberFit(c.fNumberFit)
209 ,fNumberFitSuccess(c.fNumberFitSuccess)
210 ,fNumberEnt(c.fNumberEnt)
211 ,fStatisticMean(c.fStatisticMean)
212 ,fDebugStreamer(0x0)
213 ,fDebugLevel(c.fDebugLevel)
214 ,fFitVoir(c.fFitVoir)
215 ,fMagneticField(c.fMagneticField)
216 ,fCalibraMode(0x0)
217 ,fCurrentCoefE(c.fCurrentCoefE)
218 ,fCurrentCoefE2(c.fCurrentCoefE2)
219 ,fDect1(c.fDect1)
220 ,fDect2(c.fDect2)
221 ,fScaleFitFactor(c.fScaleFitFactor)
222 ,fEntriesCurrent(c.fEntriesCurrent)
223 ,fCountDet(c.fCountDet)
224 ,fCount(c.fCount)
225 ,fNbDet(c.fNbDet)
226 ,fCalDet(0x0)
227 ,fCalROC(0x0)
228 ,fCalDet2(0x0)
229 ,fCalROC2(0x0)
230 ,fCalDetVdriftUsed(0x0)
231 ,fCalDetExBUsed(0x0)
232 ,fCurrentCoefDetector(0x0)
233 ,fCurrentCoefDetector2(0x0)
234 ,fVectorFit(0)
235 ,fVectorFit2(0)
236 {
237   //
238   // Copy constructor
239   //
240
241   if(c.fCalibraMode)   fCalibraMode = new AliTRDCalibraMode(*c.fCalibraMode);
242  
243   //Current variables initialised
244   for (Int_t k = 0; k < 2; k++) {
245     fCurrentCoef[k]  = 0.0;
246     fCurrentCoef2[k] = 0.0;
247   }
248   for (Int_t i = 0; i < 3; i++) {
249     fPhd[i]          = 0.0;
250     fDet[i]          = 0;
251   }
252   if(c.fCalDet) fCalDet   = new AliTRDCalDet(*c.fCalDet);
253   if(c.fCalDet2) fCalDet2 = new AliTRDCalDet(*c.fCalDet2);
254   
255   if(c.fCalROC) fCalROC   = new AliTRDCalROC(*c.fCalROC);
256   if(c.fCalROC2) fCalROC  = new AliTRDCalROC(*c.fCalROC2);
257
258   if(c.fCalDetVdriftUsed) fCalDetVdriftUsed = new AliTRDCalDet(*c.fCalDetVdriftUsed);
259   if(c.fCalDetExBUsed)    fCalDetExBUsed = new AliTRDCalDet(*c.fCalDetExBUsed);
260
261   fVectorFit.SetName(c.fVectorFit.GetName());
262   for(Int_t k = 0; k < c.fVectorFit.GetEntriesFast(); k++){
263     AliTRDFitInfo *fitInfo = new AliTRDFitInfo();
264     Int_t detector         = ((AliTRDFitInfo *)c.fVectorFit.UncheckedAt(k))->GetDetector();
265     Int_t ntotal = 1;
266     if (GetStack(detector) == 2) {
267       ntotal = 1728;
268     }
269     else {
270       ntotal = 2304;
271     }
272     Float_t *coef = new Float_t[ntotal];
273     for (Int_t i = 0; i < ntotal; i++) {
274       coef[i] = ((AliTRDFitInfo *)c.fVectorFit.UncheckedAt(k))->GetCoef()[i];
275     }
276     fitInfo->SetCoef(coef);
277     fitInfo->SetDetector(detector);
278     fVectorFit.Add((TObject *) fitInfo);
279   }
280   fVectorFit.SetName(c.fVectorFit.GetName());
281   for(Int_t k = 0; k < c.fVectorFit2.GetEntriesFast(); k++){
282     AliTRDFitInfo *fitInfo = new AliTRDFitInfo();
283     Int_t detector         = ((AliTRDFitInfo *)c.fVectorFit2.UncheckedAt(k))->GetDetector();
284     Int_t ntotal = 1;
285     if (GetStack(detector) == 2) {
286       ntotal = 1728;
287     }
288     else {
289       ntotal = 2304;
290     }
291     Float_t *coef = new Float_t[ntotal];
292     for (Int_t i = 0; i < ntotal; i++) {
293       coef[i] = ((AliTRDFitInfo *)c.fVectorFit2.UncheckedAt(k))->GetCoef()[i];
294     }
295     fitInfo->SetCoef(coef);
296     fitInfo->SetDetector(detector);
297     fVectorFit2.Add((TObject *) fitInfo);
298   }
299   if (fGeo) {
300     delete fGeo;
301   }
302   fGeo = new AliTRDgeometry();
303
304 }
305 //____________________________________________________________________________________
306 AliTRDCalibraFit::~AliTRDCalibraFit()
307 {
308   //
309   // AliTRDCalibraFit destructor
310   //
311   if ( fDebugStreamer ) delete fDebugStreamer;
312   if ( fCalDet )  delete fCalDet;
313   if ( fCalDet2 ) delete fCalDet2;
314   if ( fCalROC )  delete fCalROC;
315   if ( fCalROC2 ) delete fCalROC2;
316   if ( fCalDetVdriftUsed)  delete fCalDetVdriftUsed;
317   if ( fCalDetExBUsed) delete fCalDetExBUsed;
318   if( fCurrentCoefDetector ) delete [] fCurrentCoefDetector;
319   if( fCurrentCoefDetector2 ) delete [] fCurrentCoefDetector2; 
320   fVectorFit.Delete();
321   fVectorFit2.Delete();
322   if (fGeo) {
323     delete fGeo;
324   }
325
326 }
327 //_____________________________________________________________________________
328 void AliTRDCalibraFit::Destroy() 
329 {
330   //
331   // Delete instance 
332   //
333
334   if (fgInstance) {
335     delete fgInstance;
336     fgInstance = 0x0;
337   }
338
339 }
340 //_____________________________________________________________________________
341 void AliTRDCalibraFit::DestroyDebugStreamer() 
342 {
343   //
344   // Delete DebugStreamer
345   //
346
347   if ( fDebugStreamer ) delete fDebugStreamer;
348   fDebugStreamer = 0x0;
349  
350 }
351 //____________Functions fit Online CH2d________________________________________
352 Bool_t AliTRDCalibraFit::AnalyseCH(const TH2I *ch)
353 {
354   //
355   // Fit the 1D histos, projections of the 2D ch on the Xaxis, for each
356   // calibration group normalized the resulted coefficients (to 1 normally)
357   //
358
359   // Set the calibration mode
360   //const char *name = ch->GetTitle();
361   TString name = ch->GetTitle();
362   if(!SetModeCalibration(name,0)) return kFALSE;
363
364   // Number of Ybins (detectors or groups of pads)
365   Int_t    nbins   = ch->GetNbinsX();// charge
366   Int_t    nybins  = ch->GetNbinsY();// groups number
367   if (!InitFit(nybins,0)) {
368     return kFALSE;
369   }
370   if (!InitFitCH()) {
371     return kFALSE;
372   }
373   fStatisticMean        = 0.0;
374   fNumberFit            = 0;
375   fNumberFitSuccess     = 0;
376   fNumberEnt            = 0;
377   // Init fCountDet and fCount
378   InitfCountDetAndfCount(0);
379   // Beginning of the loop betwwen dect1 and dect2
380   for (Int_t idect = fDect1; idect < fDect2; idect++) {
381     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi...
382     UpdatefCountDetAndfCount(idect,0);
383     ReconstructFitRowMinRowMax(idect, 0);
384     // Take the histo
385     TH1I *projch = (TH1I *) ch->ProjectionX("projch",idect+1,idect+1,(Option_t *)"e");
386     projch->SetDirectory(0);
387     // Number of entries for this calibration group
388     Double_t nentries = 0.0;
389     Double_t mean = 0.0;
390     for (Int_t k = 0; k < nbins; k++) {
391       Int_t binnb = (nbins+2)*(idect+1)+(k+1);
392       nentries += ch->GetBinContent(binnb);
393       mean += projch->GetBinCenter(k+1)*projch->GetBinContent(k+1);
394       projch->SetBinError(k+1,TMath::Sqrt(projch->GetBinContent(k+1)));
395     }
396     projch->SetEntries(nentries);
397     //printf("The number of entries for the group %d is %f\n",idect,nentries);
398     if (nentries > 0) {
399       fNumberEnt++;
400       mean /= nentries;
401     }
402     // Rebin and statistic stuff
403     if (fRebin > 1) {
404       projch = ReBin((TH1I *) projch);
405     }
406     // This detector has not enough statistics or was off
407     if (nentries <= fMinEntries) {
408       NotEnoughStatisticCH(idect);
409       if (fDebugLevel != 1) {
410         delete projch;
411       }
412       continue;
413     }
414     // Statistics of the group fitted
415     fStatisticMean += nentries;
416     fNumberFit++;
417     //Method choosen
418     switch(fMethod)
419       {
420       case 0: FitMeanW((TH1 *) projch, nentries); break;
421       case 1: FitMean((TH1 *) projch, nentries, mean); break;
422       case 2: FitLandau((TH1 *) projch, mean, nentries); break;
423       case 3: FitCH((TH1 *) projch, mean, nentries); break;
424       case 4: FitBisCH((TH1 *) projch, mean, nentries); break;
425       case 5: FitBisCHEx((TH1 *) projch, mean, nentries); break;
426       default: return kFALSE;
427       }
428     // Fill Infos Fit
429     FillInfosFitCH(idect);
430     // Memory!!!
431     if (fDebugLevel != 1) {
432       delete projch;
433     }
434   } // Boucle object
435   // Normierungcharge
436   if (fDebugLevel != 1) {
437     NormierungCharge();
438   }
439   // Mean Statistic
440   if (fNumberFit > 0) {
441     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit), fNumberFitSuccess));
442     fStatisticMean = fStatisticMean / fNumberFit;
443   }
444   else {
445     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
446   }
447   delete fDebugStreamer;
448   fDebugStreamer = 0x0;
449
450   return kTRUE;
451 }
452 //____________Functions fit Online CH2d________________________________________
453 Bool_t AliTRDCalibraFit::AnalyseCH(AliTRDCalibraVector *calvect)
454 {
455   //
456   // Reconstruct a 1D histo from the vectorCH for each calibration group,
457   // fit the histo, normalized the resulted coefficients (to 1 normally)
458   //
459
460   // Set the calibraMode
461   //const char *name = calvect->GetNameCH();
462   TString name = calvect->GetNameCH();
463   if(!SetModeCalibration(name,0)) return kFALSE;  
464
465   // Number of Xbins (detectors or groups of pads)
466   if (!InitFit((432*calvect->GetDetCha0(0)+108*calvect->GetDetCha2(0)),0)) {
467     return kFALSE;
468   }
469   if (!InitFitCH()) {
470     return kFALSE;
471   }
472   fStatisticMean        = 0.0;
473   fNumberFit            = 0;
474   fNumberFitSuccess     = 0;
475   fNumberEnt            = 0;
476   // Init fCountDet and fCount
477   InitfCountDetAndfCount(0);
478   // Beginning of the loop between dect1 and dect2
479   for (Int_t idect = fDect1; idect < fDect2; idect++) {
480     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi...........
481     UpdatefCountDetAndfCount(idect,0);
482     ReconstructFitRowMinRowMax(idect,0);
483     // Take the histo
484     Double_t nentries = 0.0;
485     Double_t mean = 0.0;
486     if(!calvect->GetCHEntries(fCountDet)) {
487       NotEnoughStatisticCH(idect);
488       continue;
489     }
490     
491     TString tname("CH");
492     tname += idect;
493     TH1F *projch  = calvect->ConvertVectorCHHisto(fCountDet,(idect-(fCount-(fCalibraMode->GetNfragZ(0)*fCalibraMode->GetNfragRphi(0)))),(const char *) tname);
494     projch->SetDirectory(0);
495     for (Int_t k = 0; k < calvect->GetNumberBinCharge(); k++) {
496       nentries += projch->GetBinContent(k+1);
497       mean += projch->GetBinCenter(k+1)*projch->GetBinContent(k+1);
498     }
499     if (nentries > 0) {
500       fNumberEnt++;
501       mean /= nentries;
502     }
503     //printf("The number of entries for the group %d is %f\n",idect,nentries);
504     // Rebin
505     if (fRebin >  1) {
506       projch = ReBin((TH1F *) projch);
507     }
508     // This detector has not enough statistics or was not found in VectorCH
509     if (nentries <= fMinEntries) {
510       NotEnoughStatisticCH(idect);
511       continue;
512     }
513     // Statistic of the histos fitted
514     fStatisticMean += nentries;
515     fNumberFit++;
516     //Method choosen
517     switch(fMethod)
518       {
519       case 0: FitMeanW((TH1 *) projch, nentries); break;
520       case 1: FitMean((TH1 *) projch, nentries, mean); break;
521       case 2: FitLandau((TH1 *) projch, mean, nentries); break;
522       case 3: FitCH((TH1 *) projch, mean, nentries); break;
523       case 4: FitBisCH((TH1 *) projch, mean, nentries); break;
524       case 5: FitBisCHEx((TH1 *) projch, mean, nentries); break;
525       default: return kFALSE;
526       }
527     // Fill Infos Fit
528     FillInfosFitCH(idect); 
529   } // Boucle object
530   // Normierungcharge
531   if (fDebugLevel != 1) {
532     NormierungCharge();
533   }
534   // Mean Statistics
535   if (fNumberFit > 0) {
536     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit), fNumberFitSuccess));
537     fStatisticMean = fStatisticMean / fNumberFit;
538   }
539   else {
540     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
541   }
542   delete fDebugStreamer;
543   fDebugStreamer = 0x0;
544   return kTRUE;
545 }
546 //____________Functions fit Online CH2d________________________________________
547 Double_t AliTRDCalibraFit::AnalyseCHAllTogether(const TH2I *ch)
548 {
549   //
550   // Fit the 1D histos, projections of the 2D ch on the Xaxis, for each
551   // calibration group normalized the resulted coefficients (to 1 normally)
552   //
553   Int_t    nbins   = ch->GetNbinsX();// charge
554   Int_t    nybins  = ch->GetNbinsY();// groups number
555   // Take the histo
556   TH1I *projch = (TH1I *) ch->ProjectionX("projch",1,nybins+1,(Option_t *)"e");
557   projch->SetDirectory(0);
558   // Number of entries for this calibration group
559   Double_t nentries = 0.0;
560   Double_t mean = 0.0;
561   for (Int_t k = 0; k < nbins; k++) {
562     nentries += projch->GetBinContent(k+1);
563     mean += projch->GetBinCenter(k+1)*projch->GetBinContent(k+1);
564     projch->SetBinError(k+1,TMath::Sqrt(projch->GetBinContent(k+1)));
565   }
566   projch->SetEntries(nentries);
567   //printf("The number of entries for the group %d is %f\n",idect,nentries);
568   if (nentries > 0) {
569     mean /= nentries;
570   }
571   // This detector has not enough statistics or was off
572   if (nentries <= fMinEntries) {
573     delete projch;
574     AliInfo(Form("There are %d entries for all together, it is not enough (%d)",(Int_t) nentries, fMinEntries));
575     return -100.0;
576   }
577   //Method choosen
578   switch(fMethod)
579     {
580     case 0: FitMeanW((TH1 *) projch, nentries); break;
581     case 1: FitMean((TH1 *) projch, nentries, mean); break;
582     case 2: FitLandau((TH1 *) projch, mean, nentries); break;
583     case 3: FitCH((TH1 *) projch, mean, nentries); break;
584     case 4: FitBisCH((TH1 *) projch, mean, nentries); break;
585     case 5: FitBisCHEx((TH1 *) projch, mean, nentries); break;
586     default: return -100.0;
587     }
588   delete fDebugStreamer;
589   fDebugStreamer = 0x0;
590   
591   if(fCurrentCoef[0] > 0.0) return fCurrentCoef[0];
592   else return -100.0;
593   
594 }
595 //________________functions fit Online PH2d____________________________________
596 Double_t AliTRDCalibraFit::AnalysePHAllTogether(const TProfile2D *ph)
597 {
598   //
599   // Take the 1D profiles (average pulse height), projections of the 2D PH
600   // on the Xaxis, for each calibration group
601   // Reconstruct a drift velocity
602   // A first calibration of T0 is also made  using the same method
603   //
604
605   // Number of Xbins (detectors or groups of pads)
606   Int_t    nbins   = ph->GetNbinsX();// time
607   Int_t    nybins  = ph->GetNbinsY();// calibration group
608  
609   // Take the histo
610   TH1D *projph = (TH1D *) ph->ProjectionX("projph",1,nybins+1,(Option_t *) "e");
611   projph->SetDirectory(0); 
612   // Number of entries for this calibration group
613   Double_t nentries = 0;
614   for(Int_t idect = 0; idect < nybins; idect++){
615     for (Int_t k = 0; k < nbins; k++) {
616       Int_t binnb = (nbins+2)*(idect+1)+(k+1);
617       nentries += ph->GetBinEntries(binnb);
618     }
619   }
620   //printf("AnalysePHAllTogether:: the number of entries is %f\n",nentries);
621   // This detector has not enough statistics or was off
622   if (nentries  <= fMinEntries) {
623     AliInfo(Form("There are %d entries for all together, it is not enough (%d)",(Int_t) nentries, fMinEntries));
624     if (fDebugLevel != 1) {
625       delete projph;
626     }
627     return -100.0;
628   }
629   //Method choosen
630   //printf("Method\n");
631   switch(fMethod)
632     {
633     case 0: FitLagrangePoly((TH1 *) projph); break;
634     case 1: FitPente((TH1 *) projph); break;
635     case 2: FitPH((TH1 *) projph,0); break;
636     default: return -100.0;
637     }
638   // Memory!!!
639   if (fDebugLevel != 1) {
640     delete projph;
641   }
642   delete fDebugStreamer;
643   fDebugStreamer = 0x0;
644   
645   if(fCurrentCoef[0] > 0.0) return fCurrentCoef[0];
646   else return -100.0;
647   
648 }
649 //____________Functions fit Online PH2d________________________________________
650 Bool_t AliTRDCalibraFit::AnalysePH(AliTRDCalibraVector *calvect)
651 {
652   //
653   // Reconstruct the average pulse height from the vectorPH for each
654   // calibration group
655   // Reconstruct a drift velocity
656   // A first calibration of T0 is also made  using the same method (slope method)
657   //
658
659   // Set the calibration mode
660   //const char *name = calvect->GetNamePH();
661   TString name = calvect->GetNamePH();
662   if(!SetModeCalibration(name,1)) return kFALSE;
663
664   // Number of Xbins (detectors or groups of pads)
665   if (!InitFit((432*calvect->GetDetCha0(1)+108*calvect->GetDetCha2(1)),1)) {
666     return kFALSE;
667   }
668   if (!InitFitPH()) {
669     return kFALSE;
670   }
671   fStatisticMean        = 0.0;
672   fNumberFit            = 0;
673   fNumberFitSuccess     = 0;
674   fNumberEnt            = 0;
675   // Init fCountDet and fCount
676   InitfCountDetAndfCount(1);
677   // Beginning of the loop
678   for (Int_t idect = fDect1; idect < fDect2; idect++) {
679     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi...........
680     UpdatefCountDetAndfCount(idect,1);
681     ReconstructFitRowMinRowMax(idect,1);
682     // Take the histo
683     fEntriesCurrent = 0;
684     if(!calvect->GetPHEntries(fCountDet)) {
685       NotEnoughStatisticPH(idect,fEntriesCurrent);
686       continue;
687     }
688     TString tname("PH");
689     tname += idect;
690     TH1F *projph  = calvect->CorrectTheError((TGraphErrors *) (calvect->ConvertVectorPHTGraphErrors(fCountDet,(idect-(fCount-(fCalibraMode->GetNfragZ(1)*fCalibraMode->GetNfragRphi(1)))),(const char *) tname)),fEntriesCurrent);
691     projph->SetDirectory(0);
692     if(fEntriesCurrent > 0) fNumberEnt++;
693     //printf("The number of entries for the group %d is %d\n",idect,fEntriesCurrent);
694     // This detector has not enough statistics or was off
695     if (fEntriesCurrent <=  fMinEntries) {
696       //printf("Not enough stat!\n");
697       NotEnoughStatisticPH(idect,fEntriesCurrent);
698       continue;
699     }
700     // Statistic of the histos fitted
701     fNumberFit++;
702     fStatisticMean += fEntriesCurrent;
703     // Calcul of "real" coef
704     CalculVdriftCoefMean();
705     CalculT0CoefMean();
706     //Method choosen
707     switch(fMethod)
708       {
709       case 0: FitLagrangePoly((TH1 *) projph); break;
710       case 1: FitPente((TH1 *) projph); break;
711       case 2: FitPH((TH1 *) projph,(Int_t) (idect - fDect1)); break;
712       default: return kFALSE;
713       }
714     // Fill the tree if end of a detector or only the pointer to the branch!!!
715     FillInfosFitPH(idect,fEntriesCurrent);
716   } // Boucle object
717   
718   // Mean Statistic
719   if (fNumberFit > 0) {
720     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
721     fStatisticMean = fStatisticMean / fNumberFit;
722   }
723   else {
724     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
725   }
726   delete fDebugStreamer;
727   fDebugStreamer = 0x0;
728   return kTRUE;
729 }
730 //________________functions fit Online PH2d____________________________________
731 Bool_t AliTRDCalibraFit::AnalysePH(const TProfile2D *ph)
732 {
733   //
734   // Take the 1D profiles (average pulse height), projections of the 2D PH
735   // on the Xaxis, for each calibration group
736   // Reconstruct a drift velocity
737   // A first calibration of T0 is also made  using the same method
738   //
739
740   // Set the calibration mode
741   //const char *name = ph->GetTitle();
742   TString name = ph->GetTitle();
743   if(!SetModeCalibration(name,1)) return kFALSE;
744   
745   //printf("Mode calibration set\n");
746
747   // Number of Xbins (detectors or groups of pads)
748   Int_t    nbins   = ph->GetNbinsX();// time
749   Int_t    nybins  = ph->GetNbinsY();// calibration group
750   if (!InitFit(nybins,1)) {
751     return kFALSE;
752   }
753
754   //printf("Init fit\n");
755
756   if (!InitFitPH()) {
757     return kFALSE;
758   }
759
760   //printf("Init fit PH\n");
761
762   fStatisticMean        = 0.0;
763   fNumberFit            = 0;
764   fNumberFitSuccess     = 0;
765   fNumberEnt            = 0;
766   // Init fCountDet and fCount
767   InitfCountDetAndfCount(1);
768   //printf("Init Count Det and fCount %d, %d\n",fDect1,fDect2);
769
770   // Beginning of the loop
771   for (Int_t idect = fDect1; idect < fDect2; idect++) {
772     //printf("idect = %d\n",idect);
773     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi.......
774     UpdatefCountDetAndfCount(idect,1);
775     ReconstructFitRowMinRowMax(idect,1);
776     // Take the histo
777     TH1D *projph = (TH1D *) ph->ProjectionX("projph",idect+1,idect+1,(Option_t *) "e");
778     projph->SetDirectory(0); 
779     // Number of entries for this calibration group
780     Double_t nentries = 0;
781     for (Int_t k = 0; k < nbins; k++) {
782       Int_t binnb = (nbins+2)*(idect+1)+(k+1);
783       nentries += ph->GetBinEntries(binnb);
784     }
785     if (nentries > 0) {
786       fNumberEnt++;
787     }  
788     //printf("The number of entries for the group %d is %f\n",idect,nentries);
789     // This detector has not enough statistics or was off
790     if (nentries  <= fMinEntries) {
791       //printf("Not enough statistic!\n");
792       NotEnoughStatisticPH(idect,nentries);     
793       if (fDebugLevel != 1) {
794         delete projph;
795       }
796       continue;
797     }
798     // Statistics of the histos fitted
799     fNumberFit++;
800     fStatisticMean += nentries;
801     // Calcul of "real" coef
802     CalculVdriftCoefMean();
803     CalculT0CoefMean();
804     //Method choosen
805     //printf("Method\n");
806     switch(fMethod)
807       {
808       case 0: FitLagrangePoly((TH1 *) projph); break;
809       case 1: FitPente((TH1 *) projph); break;
810       case 2: FitPH((TH1 *) projph,(Int_t) (idect - fDect1)); break;
811       default: return kFALSE;
812       }
813     // Fill the tree if end of a detector or only the pointer to the branch!!!
814     FillInfosFitPH(idect,nentries);
815     // Memory!!!
816     if (fDebugLevel != 1) {
817       delete projph;
818     }
819   } // Boucle object
820   // Mean Statistic
821   if (fNumberFit > 0) {
822     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
823     fStatisticMean = fStatisticMean / fNumberFit;
824   }
825   else {
826     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
827   }
828   delete fDebugStreamer;
829   fDebugStreamer = 0x0;
830   return kTRUE;
831 }
832 //____________Functions fit Online PRF2d_______________________________________
833 Bool_t AliTRDCalibraFit::AnalysePRF(const TProfile2D *prf)
834 {
835   //
836   // Take the 1D profiles (pad response function), projections of the 2D PRF
837   // on the Xaxis, for each calibration group
838   // Fit with a gaussian to reconstruct the sigma of the pad response function
839   //
840
841   // Set the calibration mode
842   //const char *name = prf->GetTitle();
843   TString name = prf->GetTitle();
844   if(!SetModeCalibration(name,2)) return kFALSE;
845
846   // Number of Ybins (detectors or groups of pads)
847   Int_t    nybins  = prf->GetNbinsY();// calibration groups
848   Int_t    nbins   = prf->GetNbinsX();// bins
849   Int_t    nbg     = GetNumberOfGroupsPRF((const char *)prf->GetTitle());
850   if((nbg > 0) || (nbg == -1)) return kFALSE;
851   if (!InitFit(nybins,2)) {
852     return kFALSE;
853   }
854   if (!InitFitPRF()) {
855     return kFALSE;
856   }
857   fStatisticMean        = 0.0;
858   fNumberFit            = 0;
859   fNumberFitSuccess     = 0;
860   fNumberEnt            = 0;
861   // Init fCountDet and fCount
862   InitfCountDetAndfCount(2);
863   // Beginning of the loop
864   for (Int_t idect = fDect1; idect < fDect2; idect++) {
865     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi......
866     UpdatefCountDetAndfCount(idect,2);
867     ReconstructFitRowMinRowMax(idect,2);
868     // Take the histo
869     TH1D *projprf = (TH1D *) prf->ProjectionX("projprf",idect+1,idect+1,(Option_t *) "e");
870     projprf->SetDirectory(0);
871     // Number of entries for this calibration group
872     Double_t nentries = 0;
873     for (Int_t k = 0; k < nbins; k++) {
874       Int_t binnb = (nbins+2)*(idect+1)+(k+1);
875       nentries += prf->GetBinEntries(binnb);
876     }
877     if(nentries > 0) fNumberEnt++;
878     // This detector has not enough statistics or was off
879     if (nentries <= fMinEntries) {
880       NotEnoughStatisticPRF(idect);
881       if (fDebugLevel != 1) {
882         delete projprf;
883       }
884       continue;
885     }
886     // Statistics of the histos fitted
887     fNumberFit++;
888     fStatisticMean += nentries;
889     // Calcul of "real" coef
890     CalculPRFCoefMean();
891     //Method choosen
892     switch(fMethod)
893       {
894       case 0: FitPRF((TH1 *) projprf); break;
895       case 1: RmsPRF((TH1 *) projprf); break;
896       default: return kFALSE;
897       }
898     // Fill the tree if end of a detector or only the pointer to the branch!!!
899     FillInfosFitPRF(idect);
900     // Memory!!!
901     if (fDebugLevel != 1) {
902       delete projprf;
903     }
904   } // Boucle object
905   // Mean Statistic
906   if (fNumberFit > 0) {
907     AliInfo(Form("There are %d with at least one entries.",fNumberEnt));
908     AliInfo(Form("%d fits have been proceeded (sucessfully or not...).",fNumberFit));
909     AliInfo(Form("There is a mean statistic of: %f over these fitted histograms and %d successfulled fits"
910                  ,(Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
911     fStatisticMean = fStatisticMean / fNumberFit;
912   }
913   else {
914     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
915   }
916   delete fDebugStreamer;
917   fDebugStreamer = 0x0;
918   return kTRUE;
919 }
920 //____________Functions fit Online PRF2d_______________________________________
921 Bool_t AliTRDCalibraFit::AnalysePRFMarianFit(const TProfile2D *prf)
922 {
923   //
924   // Take the 1D profiles (pad response function), projections of the 2D PRF
925   // on the Xaxis, for each calibration group
926   // Fit with a gaussian to reconstruct the sigma of the pad response function
927   //
928
929   // Set the calibration mode
930   //const char *name = prf->GetTitle();
931   TString name = prf->GetTitle();
932   if(!SetModeCalibration(name,2)) return kFALSE;
933
934   // Number of Ybins (detectors or groups of pads)
935   TAxis   *xprf    = prf->GetXaxis();
936   TAxis   *yprf    = prf->GetYaxis();
937   Int_t    nybins  = yprf->GetNbins();// calibration groups
938   Int_t    nbins   = xprf->GetNbins();// bins
939   Float_t  lowedge = (Float_t) xprf->GetBinLowEdge(1);//lowedge in bins
940   Float_t  upedge  = (Float_t) xprf->GetBinUpEdge(nbins);//upedge in bins
941   Int_t    nbg     = GetNumberOfGroupsPRF((const char *)name);
942   if(nbg == -1) return kFALSE;
943   if(nbg > 0) fMethod = 1;
944   else fMethod = 0;
945   if (!InitFit(nybins,2)) {
946     return kFALSE;
947   }
948   if (!InitFitPRF()) {
949     return kFALSE;
950   }
951   fStatisticMean        = 0.0;
952   fNumberFit            = 0;
953   fNumberFitSuccess     = 0;
954   fNumberEnt            = 0;
955   // Init fCountDet and fCount
956   InitfCountDetAndfCount(2);
957   // Beginning of the loop
958   for (Int_t idect = fDect1; idect < fDect2; idect++) {
959     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi.......
960     UpdatefCountDetAndfCount(idect,2);
961     ReconstructFitRowMinRowMax(idect,2);
962     // Build the array of entries and sum
963     TArrayD arraye  = TArrayD(nbins);
964     TArrayD arraym  = TArrayD(nbins);
965     TArrayD arrayme = TArrayD(nbins);
966     Double_t nentries = 0;
967     //printf("nbins %d\n",nbins);
968     for (Int_t k = 0; k < nbins; k++) {
969       Int_t binnb = (nbins+2)*(idect+1)+(k+1);
970       Double_t entries = (Double_t)prf->GetBinEntries(binnb);
971       Double_t mean    = (Double_t)prf->GetBinContent(binnb);
972       Double_t error   = (Double_t)prf->GetBinError(binnb); 
973       //printf("for %d we have %f\n",k,entries);
974       nentries += entries;
975       arraye.AddAt(entries,k);
976       arraym.AddAt(mean,k);
977       arrayme.AddAt(error,k);
978     }
979     if(nentries > 0) fNumberEnt++;
980     //printf("The number of entries for the group %d is %f\n",idect,nentries);
981     // This detector has not enough statistics or was off
982     if (nentries <= fMinEntries) {
983       NotEnoughStatisticPRF(idect);
984       continue;
985     }
986     // Statistics of the histos fitted
987     fNumberFit++;
988     fStatisticMean += nentries;
989     // Calcul of "real" coef
990     CalculPRFCoefMean();
991     //Method choosen
992     switch(fMethod)
993       {
994       case 0: FitPRFGausMI( arraye.GetArray(), arraym.GetArray(), arrayme.GetArray(), nbins, lowedge, upedge); break;
995       case 1: FitTnpRange( arraye.GetArray(), arraym.GetArray(), arrayme.GetArray(), nbg, nbins); break;
996       default: return kFALSE;
997       }
998     // Fill the tree if end of a detector or only the pointer to the branch!!!
999     FillInfosFitPRF(idect);
1000   } // Boucle object
1001   // Mean Statistic
1002   if (fNumberFit > 0) {
1003     AliInfo(Form("There are %d with at least one entries.",fNumberEnt));
1004     AliInfo(Form("%d fits have been proceeded (sucessfully or not...).",fNumberFit));
1005     AliInfo(Form("There is a mean statistic of: %f over these fitted histograms and %d successfulled fits"
1006                  ,(Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
1007     fStatisticMean = fStatisticMean / fNumberFit;
1008   }
1009   else {
1010     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
1011   }
1012   delete fDebugStreamer;
1013   fDebugStreamer = 0x0;
1014   return kTRUE;
1015 }
1016 //____________Functions fit Online PRF2d_______________________________________
1017 Bool_t AliTRDCalibraFit::AnalysePRF(AliTRDCalibraVector *calvect)
1018 {
1019   //
1020   // Reconstruct the 1D histo (pad response function) from the vectorPRD for
1021   // each calibration group
1022   // Fit with a gaussian to reconstruct the sigma of the pad response function
1023   //
1024
1025   // Set the calibra mode
1026   //const char *name = calvect->GetNamePRF();
1027   TString name = calvect->GetNamePRF();
1028   if(!SetModeCalibration(name,2)) return kFALSE;
1029   //printf("test0 %s\n",name);
1030
1031   // Number of Xbins (detectors or groups of pads)
1032   if (!InitFit((432*calvect->GetDetCha0(2)+108*calvect->GetDetCha2(2)),2)) {
1033     //printf("test1\n");
1034     return kFALSE;
1035   }
1036   if (!InitFitPRF()) {
1037     ///printf("test2\n");
1038     return kFALSE;
1039   }
1040   fStatisticMean        = 0.0;
1041   fNumberFit            = 0;
1042   fNumberFitSuccess     = 0;
1043   fNumberEnt            = 0;
1044   // Init fCountDet and fCount
1045   InitfCountDetAndfCount(2);
1046   // Beginning of the loop
1047   for (Int_t idect = fDect1; idect < fDect2; idect++) {
1048     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi........
1049     UpdatefCountDetAndfCount(idect,2);
1050     ReconstructFitRowMinRowMax(idect,2);
1051     // Take the histo
1052     fEntriesCurrent = 0;
1053     if(!calvect->GetPRFEntries(fCountDet)) {
1054       NotEnoughStatisticPRF(idect);
1055       continue;
1056     }
1057     TString tname("PRF");
1058     tname += idect;
1059     TH1F *projprf  = calvect->CorrectTheError((TGraphErrors *) (calvect->ConvertVectorPRFTGraphErrors(fCountDet,(idect-(fCount-(fCalibraMode->GetNfragZ(1)*fCalibraMode->GetNfragRphi(1)))),(const char *) tname)),fEntriesCurrent);
1060     projprf->SetDirectory(0);
1061     if(fEntriesCurrent > 0) fNumberEnt++;
1062     // This detector has not enough statistics or was off
1063     if (fEntriesCurrent <= fMinEntries) {
1064       NotEnoughStatisticPRF(idect);
1065       continue;
1066     }
1067     // Statistic of the histos fitted
1068     fNumberFit++;
1069     fStatisticMean += fEntriesCurrent;
1070     // Calcul of "real" coef
1071     CalculPRFCoefMean();
1072     //Method choosen
1073     switch(fMethod)
1074       {
1075       case 1: FitPRF((TH1 *) projprf); break;
1076       case 2: RmsPRF((TH1 *) projprf); break;
1077       default: return kFALSE;
1078       }    
1079     // Fill the tree if end of a detector or only the pointer to the branch!!!
1080     FillInfosFitPRF(idect);
1081   } // Boucle object
1082   // Mean Statistics
1083   if (fNumberFit > 0) {
1084     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
1085   }
1086   else {
1087     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
1088   }
1089   delete fDebugStreamer;
1090   fDebugStreamer = 0x0;
1091   return kTRUE;
1092 }
1093 //____________Functions fit Online PRF2d_______________________________________
1094 Bool_t AliTRDCalibraFit::AnalysePRFMarianFit(AliTRDCalibraVector *calvect)
1095 {
1096   //
1097   // Reconstruct the 1D histo (pad response function) from the vectorPRD for
1098   // each calibration group
1099   // Fit with a gaussian to reconstruct the sigma of the pad response function
1100   //
1101
1102   // Set the calibra mode
1103   //const char *name = calvect->GetNamePRF();
1104   TString name = calvect->GetNamePRF();
1105   if(!SetModeCalibration(name,2)) return kFALSE;
1106   //printf("test0 %s\n",name);
1107   Int_t    nbg     = GetNumberOfGroupsPRF((const char *)name);
1108   //printf("test1 %d\n",nbg);
1109   if(nbg == -1) return kFALSE;
1110   if(nbg > 0) fMethod = 1;
1111   else fMethod = 0;
1112   // Number of Xbins (detectors or groups of pads)
1113   if (!InitFit((432*calvect->GetDetCha0(2)+108*calvect->GetDetCha2(2)),2)) {
1114     //printf("test2\n");
1115     return kFALSE;
1116   }
1117   if (!InitFitPRF()) {
1118     //printf("test3\n");
1119     return kFALSE;
1120   }
1121   fStatisticMean        = 0.0;
1122   fNumberFit            = 0;
1123   fNumberFitSuccess     = 0;
1124   fNumberEnt            = 0;
1125   // Variables
1126   Int_t nbins           = 0;
1127   Double_t *arrayx       = 0;
1128   Double_t *arraye       = 0;
1129   Double_t *arraym       = 0;
1130   Double_t *arrayme      = 0;
1131   Float_t lowedge       = 0.0;
1132   Float_t upedge        = 0.0;
1133   // Init fCountDet and fCount
1134   InitfCountDetAndfCount(2);
1135   // Beginning of the loop
1136   for (Int_t idect = fDect1; idect < fDect2; idect++) {
1137     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi......
1138     UpdatefCountDetAndfCount(idect,2);
1139     ReconstructFitRowMinRowMax(idect,2);
1140     // Take the histo
1141     fEntriesCurrent  = 0;
1142     if(!calvect->GetPRFEntries(fCountDet)) {
1143       NotEnoughStatisticPRF(idect);
1144       continue;
1145     }
1146     TString tname("PRF");
1147     tname += idect;
1148     TGraphErrors *projprftree  = calvect->ConvertVectorPRFTGraphErrors(fCountDet,(idect-(fCount-(fCalibraMode->GetNfragZ(1)*fCalibraMode->GetNfragRphi(1)))),(const char *) tname);
1149     nbins   = projprftree->GetN();
1150     arrayx  = (Double_t *)projprftree->GetX();
1151     arraye  = (Double_t *)projprftree->GetEX();
1152     arraym  = (Double_t *)projprftree->GetY();
1153     arrayme = (Double_t *)projprftree->GetEY();
1154     Float_t step = arrayx[1]-arrayx[0];
1155     lowedge = arrayx[0] - step/2.0;
1156     upedge  = arrayx[(nbins-1)] + step/2.0;
1157     //printf("nbins est %d\n",nbins);
1158     for(Int_t k = 0; k < nbins; k++){
1159       fEntriesCurrent += (Int_t)arraye[k];
1160       //printf("for %d we have %f, %f\n",k,arraye[k],((projprftree->GetEX())[k]));
1161       if(arraye[k]>0.0) arrayme[k] = TMath::Sqrt(TMath::Abs(arrayme[k]-arraym[k]*arraym[k])/arraye[k]);
1162     }
1163     if(fEntriesCurrent > 0) fNumberEnt++;
1164     //printf("The number of entries for the group %d is %d\n",idect,fEntriesCurrent);
1165     // This detector has not enough statistics or was off
1166     if (fEntriesCurrent <= fMinEntries) {
1167       NotEnoughStatisticPRF(idect);
1168       continue;
1169     }
1170     // Statistic of the histos fitted
1171     fNumberFit++;
1172     fStatisticMean += fEntriesCurrent;
1173     // Calcul of "real" coef
1174     CalculPRFCoefMean();
1175     //Method choosen
1176     switch(fMethod)
1177       {
1178       case 0: FitPRFGausMI(arraye,arraym,arrayme,nbins,lowedge,upedge); break;
1179       case 1: FitTnpRange(arraye,arraym,arrayme,nbg,nbins); break;
1180       default: return kFALSE;
1181       }    
1182     // Fill the tree if end of a detector or only the pointer to the branch!!!
1183     FillInfosFitPRF(idect);
1184   } // Boucle object
1185   // Mean Statistics
1186   if (fNumberFit > 0) {
1187     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
1188   }
1189   else {
1190     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
1191   }
1192   delete fDebugStreamer;
1193   fDebugStreamer = 0x0;
1194   return kTRUE;
1195 }
1196 //____________Functions fit Online CH2d________________________________________
1197 Bool_t AliTRDCalibraFit::AnalyseLinearFitters(AliTRDCalibraVdriftLinearFit *calivdli)
1198 {
1199   //
1200   // The linear method
1201   //
1202
1203   fStatisticMean        = 0.0;
1204   fNumberFit            = 0;
1205   fNumberFitSuccess     = 0;
1206   fNumberEnt            = 0;
1207   if(!InitFitLinearFitter()) return kFALSE;
1208
1209   
1210   for(Int_t idet = 0; idet < 540; idet++){
1211
1212
1213     //printf("detector number %d\n",idet);
1214
1215     // Take the result
1216     TVectorD param(2);
1217     TVectorD error(3);
1218     Double_t entriesCurrent = 0;
1219     fCountDet       = idet;
1220     Bool_t here     = calivdli->GetParam(idet,&param);
1221     Bool_t heree    = calivdli->GetError(idet,&error);
1222     //printf("here %d and heree %d\n",here, heree);
1223     if(heree) {
1224       entriesCurrent = error[2];
1225       fNumberEnt++;
1226     }
1227     //printf("Number of entries %d\n",fEntriesCurrent);
1228     // Nothing found or not enough statistic
1229     if((!heree) || (!here) || (entriesCurrent <= fMinEntries)) {
1230       NotEnoughStatisticLinearFitter();
1231       continue;
1232     }
1233     //param.Print();
1234     //error.Print();
1235     //Statistics
1236     fNumberFit++;
1237     fStatisticMean += entriesCurrent;     
1238
1239     // Check the fit
1240     if((-(param[1])) <= 0.000001) {
1241       NotEnoughStatisticLinearFitter();
1242       continue;
1243     }
1244
1245     // CalculDatabaseVdriftandTan
1246     CalculVdriftLorentzCoef();
1247     //printf("AliTRDCalibraFit::AnalyzeVdriftLinearFit detector %d, vdrift %f and %f and exB %f and %f\n",idet,fCalDetVdriftUsed->GetValue(idet),fCurrentCoef[1],fCalDetExBUsed->GetValue(idet),fCurrentCoef2[1]);
1248
1249     // Statistics   
1250     fNumberFitSuccess ++;
1251
1252     // Put the fCurrentCoef
1253     fCurrentCoef[0]  = -param[1];
1254     // here the database must be the one of the reconstruction for the lorentz angle....
1255     fCurrentCoef2[0] = (param[0]+fCurrentCoef[1]*fCurrentCoef2[1])/fCurrentCoef[0];
1256     fCurrentCoefE    = error[1];
1257     fCurrentCoefE2   = error[0];
1258     if((TMath::Abs(fCurrentCoef2[0]) > 0.0000001) && (TMath::Abs(param[0]) > 0.0000001)){
1259       fCurrentCoefE2 = (fCurrentCoefE2/param[0]+fCurrentCoefE/fCurrentCoef[0])*fCurrentCoef2[0];
1260     }    
1261
1262     // Fill
1263     FillInfosFitLinearFitter();
1264
1265     
1266   }
1267   // Mean Statistics
1268   if (fNumberFit > 0) {
1269     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
1270   }
1271   else {
1272     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
1273   }
1274   delete fDebugStreamer;
1275   fDebugStreamer = 0x0;
1276   return kTRUE;
1277   
1278 }
1279 //______________________________________________________________________________________
1280 Bool_t AliTRDCalibraFit::AnalyseExbAltFit(AliTRDCalibraExbAltFit *calivdli)
1281 {
1282   //
1283   // The linear method
1284   //
1285
1286   fStatisticMean        = 0.0;
1287   fNumberFit            = 0;
1288   fNumberFitSuccess     = 0;
1289   fNumberEnt            = 0;
1290   if(!InitFitExbAlt()) return kFALSE;
1291
1292   
1293   for(Int_t idet = 0; idet < 540; idet++){
1294
1295
1296     //printf("detector number %d\n",idet);
1297
1298     // Take the result
1299     TVectorD param(3);
1300     TVectorD error(3);
1301     Double_t entriesCurrent = 0;
1302     fCountDet       = idet;
1303     Bool_t here     = calivdli->GetParam(idet,&param);
1304     Bool_t heree    = calivdli->GetError(idet,&error);
1305     //printf("here %d and heree %d\n",here, heree);
1306     if(heree) {
1307       entriesCurrent =  error[2];
1308       fNumberEnt++;
1309     }
1310     //printf("Number of entries %d\n",fEntriesCurrent);
1311     // Nothing found or not enough statistic
1312     if((!heree) || (!here) || (entriesCurrent <= fMinEntries)) {
1313       NotEnoughStatisticExbAlt();
1314       continue;
1315     }
1316     //param.Print();
1317     //error.Print();
1318     //Statistics
1319     fNumberFit++;
1320     fStatisticMean += entriesCurrent;
1321
1322     // Statistics   
1323     fNumberFitSuccess ++;
1324
1325     // Put the fCurrentCoef
1326     if(TMath::Abs(param[2])>0.0001){
1327       fCurrentCoef2[0]  = -param[1]/2/param[2];
1328       fCurrentCoefE2    = 0;//error[1];
1329     }else{
1330       fCurrentCoef2[0]  = 100;
1331       fCurrentCoefE2    = 0;//error[1];
1332     }
1333     
1334     // Fill
1335     FillInfosFitExbAlt();
1336     
1337   }
1338   // Mean Statistics
1339   if (fNumberFit > 0) {
1340     AliInfo(Form("There are %d with at least one entries. %d fits have been proceeded (sucessfully or not...). There is a mean statistic of: %f over these fitted histograms and %d successfulled fits",fNumberEnt, fNumberFit, (Double_t) (fStatisticMean/fNumberFit),fNumberFitSuccess));
1341   }
1342   else {
1343     AliInfo(Form("There are %d with at least one entries. There is no fit!",fNumberEnt));
1344   }
1345   delete fDebugStreamer;
1346   fDebugStreamer = 0x0;
1347   return kTRUE;
1348   
1349 }
1350 //____________Functions fit Online CH2d________________________________________
1351 void AliTRDCalibraFit::AnalyseLinearFittersAllTogether(AliTRDCalibraVdriftLinearFit *calivdli, Double_t &vdriftoverall, Double_t &exboverall)
1352 {
1353   //
1354   // The linear method
1355   //
1356
1357   // Get the mean vdrift and exb used
1358   Double_t meanvdriftused = 0.0;
1359   Double_t meanexbused = 0.0;
1360   Double_t counterdet = 0.0;
1361   if((!fCalDetVdriftUsed) || (!fCalDetExBUsed)) {
1362     vdriftoverall = -100.0;
1363     exboverall = 100.0;
1364     return;
1365   }  
1366
1367   // Add histos
1368
1369   TH2S *linearfitterhisto = 0x0;
1370   
1371   for(Int_t idet = 0; idet < 540; idet++){
1372     
1373     TH2S * u = calivdli->GetLinearFitterHistoForce(idet);
1374     Double_t detectorentries = u->Integral();
1375     meanvdriftused += fCalDetVdriftUsed->GetValue(idet)*detectorentries;
1376     meanexbused += fCalDetExBUsed->GetValue(idet)*detectorentries;
1377     counterdet += detectorentries;
1378
1379     //printf("detectorentries %f\n",detectorentries);
1380     
1381     //printf("AliTRDCalibraFit::AnalyzeVdriftLinearFitsAllTogether detector %d, vdrift %f and exB %f\n",idet,fCalDetVdriftUsed->GetValue(idet),fCalDetExBUsed->GetValue(idet));
1382
1383     if(idet == 0) linearfitterhisto = u;
1384     else linearfitterhisto->Add(u);
1385
1386   }
1387   if(counterdet > 0.0){
1388     meanvdriftused = meanvdriftused/counterdet;
1389     meanexbused = meanexbused/counterdet;    
1390   }
1391   else {
1392     vdriftoverall = -100.0;
1393     exboverall = 100.0;
1394     return;
1395   }
1396   
1397   
1398   //printf("AliTRDCalibraFit::AnalyzeVdriftLinearFitsAllTogether MEAN vdrift %f and exB %f\n",meanvdriftused,meanexbused);
1399
1400   // Fit
1401
1402   Double_t entries = 0;
1403   TAxis *xaxis = linearfitterhisto->GetXaxis();
1404   TAxis *yaxis = linearfitterhisto->GetYaxis();
1405   TLinearFitter linearfitter = TLinearFitter(2,"pol1");
1406   //printf("test\n");
1407   Double_t integral = linearfitterhisto->Integral();
1408   //printf("Integral is %f\n",integral);
1409   Bool_t securitybreaking = kFALSE;
1410   if(TMath::Abs(integral-1199) < 0.00001) securitybreaking = kTRUE;
1411   for(Int_t ibinx = 0; ibinx < linearfitterhisto->GetNbinsX(); ibinx++){
1412     for(Int_t ibiny = 0; ibiny < linearfitterhisto->GetNbinsY(); ibiny++){
1413       if(linearfitterhisto->GetBinContent(ibinx+1,ibiny+1)>0){
1414         Double_t x = xaxis->GetBinCenter(ibinx+1);
1415         Double_t y = yaxis->GetBinCenter(ibiny+1);
1416         
1417         for(Int_t k = 0; k < (Int_t)linearfitterhisto->GetBinContent(ibinx+1,ibiny+1); k++){
1418           if(!securitybreaking){
1419             linearfitter.AddPoint(&x,y);
1420             entries = entries+1.;
1421           }
1422           else {
1423             if(entries< 1198.0){
1424               linearfitter.AddPoint(&x,y);
1425               entries = entries + 1.; 
1426             }
1427           }
1428         }
1429         
1430       }
1431     }
1432   }
1433       
1434   //printf("AnalyseLinearFittersAllTogether::Find %d entries\n",entries);
1435   //printf("Minstats %d\n",fMinEntries);
1436
1437   
1438
1439   // Eval the linear fitter
1440   if(entries > fMinEntries){
1441     TVectorD  par  = TVectorD(2);
1442     //printf("Fit\n");
1443     if((linearfitter.EvalRobust(0.8)==0)) {
1444       //printf("Take the param\n");
1445       linearfitter.GetParameters(par);
1446       //printf("Done\n");
1447       //par.Print();
1448       //printf("Finish\n");
1449       // Put the fCurrentCoef
1450       fCurrentCoef[0]  = -par[1];
1451       // here the database must be the one of the reconstruction for the lorentz angle....
1452       if(fCurrentCoef[0] > 0.00001) fCurrentCoef2[0] = (par[0]+meanvdriftused*meanexbused)/fCurrentCoef[0];
1453       else fCurrentCoef2[0] = 100.0;      
1454
1455     }
1456     else {
1457       
1458       fCurrentCoef[0] = -100.0;
1459       fCurrentCoef2[0] = 100.0;
1460       
1461     }
1462     
1463     
1464   }
1465   else {
1466
1467     fCurrentCoef[0] = -100.0;
1468     fCurrentCoef2[0] = 100.0;
1469     
1470   }
1471   
1472   vdriftoverall = fCurrentCoef[0];
1473   exboverall = fCurrentCoef2[0];
1474   
1475
1476   delete linearfitterhisto;
1477   delete fDebugStreamer;
1478   fDebugStreamer = 0x0;
1479   
1480 }
1481 //____________Functions for seeing if the pad is really okey___________________
1482 //_____________________________________________________________________________
1483 Int_t AliTRDCalibraFit::GetNumberOfGroupsPRF(TString nametitle)
1484 {
1485   //
1486   // Get numberofgroupsprf
1487   //
1488   
1489   // Some patterns
1490   const Char_t *pattern0 = "Ngp0";
1491   const Char_t *pattern1 = "Ngp1";
1492   const Char_t *pattern2 = "Ngp2";
1493   const Char_t *pattern3 = "Ngp3";
1494   const Char_t *pattern4 = "Ngp4";
1495   const Char_t *pattern5 = "Ngp5";
1496   const Char_t *pattern6 = "Ngp6";
1497
1498   // Nrphi mode
1499   if (strstr(nametitle.Data(),pattern0)) {
1500     return 0;
1501   }
1502   if (strstr(nametitle.Data(),pattern1)) {
1503     return 1;
1504   }
1505   if (strstr(nametitle.Data(),pattern2)) {
1506     return 2;
1507   }
1508   if (strstr(nametitle.Data(),pattern3)) {
1509     return 3;
1510   }
1511   if (strstr(nametitle.Data(),pattern4)) {
1512     return 4;
1513   }
1514   if (strstr(nametitle.Data(),pattern5)) {
1515     return 5;
1516   }
1517   if (strstr(nametitle.Data(),pattern6)){
1518     return 6;
1519   }
1520   else return -1;
1521  
1522
1523 }
1524 //_____________________________________________________________________________
1525 Bool_t AliTRDCalibraFit::SetModeCalibration(TString name, Int_t i)
1526 {
1527   //
1528   // Set fNz[i] and fNrphi[i] of the AliTRDCalibraFit::Instance()
1529   // corresponding to the given name
1530   //
1531
1532   if(!SetNzFromTObject(name,i)) return kFALSE;
1533   if(!SetNrphiFromTObject(name,i)) return kFALSE;
1534   
1535   return kTRUE; 
1536
1537 }
1538 //_____________________________________________________________________________
1539 Bool_t AliTRDCalibraFit::SetNrphiFromTObject(TString name, Int_t i)
1540 {
1541   //
1542   // Set fNrphi[i] of the AliTRDCalibraFit::Instance()
1543   // corresponding to the given TObject
1544   //
1545   
1546   // Some patterns
1547   const Char_t *patternrphi0 = "Nrphi0";
1548   const Char_t *patternrphi1 = "Nrphi1";
1549   const Char_t *patternrphi2 = "Nrphi2";
1550   const Char_t *patternrphi3 = "Nrphi3";
1551   const Char_t *patternrphi4 = "Nrphi4";
1552   const Char_t *patternrphi5 = "Nrphi5";
1553   const Char_t *patternrphi6 = "Nrphi6";
1554
1555   
1556   const Char_t *patternrphi10 = "Nrphi10";
1557   const Char_t *patternrphi100 = "Nrphi100";
1558   const Char_t *patternz10 = "Nz10";
1559   const Char_t *patternz100 = "Nz100";
1560
1561   // Nrphi mode
1562   if ((strstr(name.Data(),patternrphi100)) && (strstr(name.Data(),patternz100))) {
1563     fCalibraMode->SetAllTogether(i);
1564     fNbDet = 540;
1565     if (fDebugLevel > 1) {
1566       AliInfo(Form("fNbDet %d and 100",fNbDet));
1567     }
1568     return kTRUE;
1569   }
1570   if ((strstr(name.Data(),patternrphi10)) && (strstr(name.Data(),patternz10))) {
1571     fCalibraMode->SetPerSuperModule(i);
1572     fNbDet = 30;
1573     if (fDebugLevel > 1) {
1574       AliInfo(Form("fNDet %d and 100",fNbDet));
1575     }
1576     return kTRUE;
1577   }
1578   
1579   if (strstr(name.Data(),patternrphi0)) {
1580     fCalibraMode->SetNrphi(i ,0);
1581     if (fDebugLevel > 1) {
1582       AliInfo(Form("fNbDet %d and 0",fNbDet));
1583     }
1584     return kTRUE;
1585   }
1586   if (strstr(name.Data(),patternrphi1)) {
1587     fCalibraMode->SetNrphi(i, 1);
1588     if (fDebugLevel > 1) {
1589       AliInfo(Form("fNbDet %d and 1",fNbDet));
1590     }
1591     return kTRUE;
1592   }
1593   if (strstr(name.Data(),patternrphi2)) {
1594     fCalibraMode->SetNrphi(i, 2);
1595     if (fDebugLevel > 1) {
1596       AliInfo(Form("fNbDet %d and 2",fNbDet));
1597     }    
1598     return kTRUE;
1599   }
1600   if (strstr(name.Data(),patternrphi3)) {
1601     fCalibraMode->SetNrphi(i, 3);
1602     if (fDebugLevel > 1) {
1603       AliInfo(Form("fNbDet %d and 3",fNbDet));
1604     }   
1605     return kTRUE;
1606   }
1607   if (strstr(name.Data(),patternrphi4)) {
1608     fCalibraMode->SetNrphi(i, 4);
1609     if (fDebugLevel > 1) {
1610       AliInfo(Form("fNbDet %d and 4",fNbDet));
1611     }   
1612     return kTRUE;
1613   }
1614   if (strstr(name.Data(),patternrphi5)) {
1615     fCalibraMode->SetNrphi(i, 5);
1616     if (fDebugLevel > 1) {
1617       AliInfo(Form("fNbDet %d and 5",fNbDet));
1618     }
1619     return kTRUE;
1620   }
1621   if (strstr(name.Data(),patternrphi6)) {
1622     fCalibraMode->SetNrphi(i, 6);
1623     if (fDebugLevel > 1) {
1624       AliInfo(Form("fNbDet %d and 6",fNbDet));
1625     }
1626     return kTRUE;
1627   }
1628   
1629   if (fDebugLevel > 1) {
1630     AliInfo(Form("fNbDet %d and rest",fNbDet));
1631   }
1632   fCalibraMode->SetNrphi(i ,0);
1633   return kFALSE;
1634   
1635 }
1636 //_____________________________________________________________________________
1637 Bool_t AliTRDCalibraFit::SetNzFromTObject(TString name, Int_t i)
1638 {
1639   //
1640   // Set fNz[i] of the AliTRDCalibraFit::Instance()
1641   // corresponding to the given TObject
1642   //
1643
1644   // Some patterns
1645   const Char_t *patternz0    = "Nz0";
1646   const Char_t *patternz1    = "Nz1";
1647   const Char_t *patternz2    = "Nz2";
1648   const Char_t *patternz3    = "Nz3";
1649   const Char_t *patternz4    = "Nz4";
1650
1651   const Char_t *patternrphi10 = "Nrphi10";
1652   const Char_t *patternrphi100 = "Nrphi100";
1653   const Char_t *patternz10 = "Nz10";
1654   const Char_t *patternz100 = "Nz100";
1655
1656   if ((strstr(name.Data(),patternrphi100)) && (strstr(name.Data(),patternz100))) {
1657     fCalibraMode->SetAllTogether(i);
1658     fNbDet = 540;
1659     if (fDebugLevel > 1) {
1660       AliInfo(Form("fNbDet %d and 100",fNbDet));
1661     }
1662     return kTRUE;
1663   }
1664   if ((strstr(name.Data(),patternrphi10)) && (strstr(name.Data(),patternz10))) {
1665     fCalibraMode->SetPerSuperModule(i);
1666     fNbDet = 30;
1667     if (fDebugLevel > 1) {
1668       AliInfo(Form("fNbDet %d and 10",fNbDet));
1669     }
1670     return kTRUE;
1671   }
1672   if (strstr(name.Data(),patternz0)) {
1673     fCalibraMode->SetNz(i, 0);
1674     if (fDebugLevel > 1) {
1675       AliInfo(Form("fNbDet %d and 0",fNbDet));
1676     }
1677     return kTRUE;
1678   }
1679   if (strstr(name.Data(),patternz1)) {
1680     fCalibraMode->SetNz(i ,1);
1681     if (fDebugLevel > 1) {
1682       AliInfo(Form("fNbDet %d and 1",fNbDet));
1683     }
1684     return kTRUE;
1685   }
1686   if (strstr(name.Data(),patternz2)) {
1687     fCalibraMode->SetNz(i ,2);
1688     if (fDebugLevel > 1) {    
1689       AliInfo(Form("fNbDet %d and 2",fNbDet));
1690     }
1691     return kTRUE;
1692   }
1693   if (strstr(name.Data(),patternz3)) {
1694     fCalibraMode->SetNz(i ,3);
1695     if (fDebugLevel > 1) {
1696       AliInfo(Form("fNbDet %d and 3",fNbDet));
1697     }
1698     return kTRUE;  
1699   }
1700   if (strstr(name.Data(),patternz4)) {
1701     fCalibraMode->SetNz(i ,4);
1702     if (fDebugLevel > 1) {    
1703       AliInfo(Form("fNbDet %d and 4",fNbDet));
1704     }
1705     return kTRUE;
1706   }
1707  
1708   if (fDebugLevel > 1) {
1709     AliInfo(Form("fNbDet %d and rest",fNbDet));
1710   }
1711   fCalibraMode->SetNz(i ,0);
1712   return kFALSE;
1713 }
1714 //______________________________________________________________________
1715 void AliTRDCalibraFit::RemoveOutliers(Int_t type, Bool_t perdetector){
1716   //
1717   // Remove the results too far from the mean value and rms
1718   // type: 0 gain, 1 vdrift
1719   // perdetector
1720   //
1721
1722   Int_t loop = (Int_t) fVectorFit.GetEntriesFast();
1723   if(loop != 540) {
1724     AliInfo("The Vector Fit is not complete!");
1725     return;
1726   }
1727   Int_t detector = -1;
1728   Int_t sector = -1;
1729   Float_t value  = 0.0;
1730
1731   /////////////////////////////////
1732   // Calculate the mean values
1733   ////////////////////////////////
1734   // Initialisation
1735   ////////////////////////
1736   Double_t meanAll = 0.0;
1737   Double_t rmsAll = 0.0;
1738   Int_t countAll = 0;
1739   ////////////
1740   // compute
1741   ////////////
1742   for (Int_t k = 0; k < loop; k++) {
1743     detector  = ((AliTRDFitInfo *) fVectorFit.At(k))->GetDetector();
1744     sector = GetSector(detector);
1745     if(perdetector){
1746       value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[0];
1747       if(value > 0.0) {
1748         rmsAll += value*value;
1749         meanAll += value;
1750         countAll++;
1751       }
1752     }
1753     else {
1754       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
1755       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
1756       for (Int_t row = 0; row < rowMax; row++) {
1757         for (Int_t col = 0; col < colMax; col++) {
1758           value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
1759           if(value > 0.0) {
1760             rmsAll += value*value;
1761             meanAll += value;
1762             countAll++;
1763           }
1764           
1765         } // Col
1766       } // Row
1767     }
1768   }  
1769   if(countAll > 0) {
1770     meanAll = meanAll/countAll;
1771     rmsAll = TMath::Sqrt(TMath::Abs(rmsAll/countAll - (meanAll*meanAll)));
1772   }
1773   //printf("RemoveOutliers: meanAll %f and rmsAll %f\n",meanAll,rmsAll);
1774   /////////////////////////////////////////////////
1775   // Remove outliers
1776   ////////////////////////////////////////////////
1777   Double_t defaultvalue = -1.0;
1778   if(type==1) defaultvalue = -1.5;
1779   for (Int_t k = 0; k < loop; k++) {
1780     detector  = ((AliTRDFitInfo *) fVectorFit.At(k))->GetDetector();
1781     sector = GetSector(detector);
1782     Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
1783     Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
1784     Float_t *coef = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef();
1785   
1786     // remove the results too far away  
1787     for (Int_t row = 0; row < rowMax; row++) {
1788       for (Int_t col = 0; col < colMax; col++) {
1789         value = coef[(Int_t)(col*rowMax+row)];
1790         if((value > 0.0) && (rmsAll > 0.0) && (TMath::Abs(value-meanAll) > (2*rmsAll))) {
1791           coef[(Int_t)(col*rowMax+row)] = defaultvalue;
1792         }
1793       } // Col
1794     } // Row
1795   }
1796 }
1797 //______________________________________________________________________
1798 void AliTRDCalibraFit::RemoveOutliers2(Bool_t perdetector){
1799   //
1800   // Remove the results too far from the mean and rms
1801   // perdetector
1802   //
1803
1804   Int_t loop = (Int_t) fVectorFit2.GetEntriesFast();
1805   if(loop != 540) {
1806     AliInfo("The Vector Fit is not complete!");
1807     return;
1808   }
1809   Int_t detector = -1;
1810   Int_t sector = -1;
1811   Float_t value  = 0.0;
1812
1813   /////////////////////////////////
1814   // Calculate the mean values
1815   ////////////////////////////////
1816   // Initialisation
1817   ////////////////////////
1818   Double_t meanAll = 0.0;
1819   Double_t rmsAll = 0.0;
1820   Int_t countAll = 0;
1821   /////////////
1822   // compute
1823   ////////////
1824   for (Int_t k = 0; k < loop; k++) {
1825     detector  = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetDetector();
1826     sector = GetSector(detector);
1827     if(perdetector){
1828       value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[0];
1829       if(value < 70.0) {
1830         meanAll += value;
1831         rmsAll += value*value;
1832         countAll++;
1833       }
1834     }
1835     else {
1836       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
1837       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
1838       for (Int_t row = 0; row < rowMax; row++) {
1839         for (Int_t col = 0; col < colMax; col++) {
1840           value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
1841           if(value < 70.0) {
1842             rmsAll += value*value;
1843             meanAll += value;
1844             countAll++;
1845           }       
1846         } // Col
1847       } // Row
1848     }
1849   }  
1850   if(countAll > 0) {
1851     meanAll = meanAll/countAll;
1852     rmsAll = TMath::Sqrt(TMath::Abs(rmsAll/countAll - (meanAll*meanAll)));
1853   }
1854   //printf("Remove outliers 2: meanAll %f, rmsAll %f\n",meanAll,rmsAll);
1855   /////////////////////////////////////////////////
1856   // Remove outliers
1857   ////////////////////////////////////////////////
1858   for (Int_t k = 0; k < loop; k++) {
1859     detector  = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetDetector();
1860     sector = GetSector(detector);
1861     Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
1862     Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
1863     Float_t *coef = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef();
1864   
1865     // remove the results too far away  
1866     for (Int_t row = 0; row < rowMax; row++) {
1867       for (Int_t col = 0; col < colMax; col++) {
1868         value = coef[(Int_t)(col*rowMax+row)];
1869         if((value < 70.0) && (rmsAll > 0.0) && (TMath::Abs(value-meanAll) > (2.5*rmsAll))) {
1870           //printf("value outlier %f\n",value);
1871           coef[(Int_t)(col*rowMax+row)] = 100.0;
1872         }
1873       } // Col
1874     } // Row
1875   }
1876 }
1877 //______________________________________________________________________
1878 void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetector){
1879   //
1880   // ofwhat is equaled to 0: mean value of all passing detectors
1881   // ofwhat is equaled to 1: mean value of the detector, otherwise supermodule, otherwise all
1882   //
1883
1884   Int_t loop = (Int_t) fVectorFit.GetEntriesFast();
1885   if(loop != 540) {
1886     AliInfo("The Vector Fit is not complete!");
1887     return;
1888   }
1889   Int_t detector = -1;
1890   Int_t sector = -1;
1891   Float_t value  = 0.0;
1892
1893   /////////////////////////////////
1894   // Calculate the mean values
1895   ////////////////////////////////
1896   // Initialisation
1897   ////////////////////////
1898   Double_t meanAll = 0.0;
1899   Double_t meanSupermodule[18];
1900   Double_t meanDetector[540];
1901   Double_t rmsAll = 0.0;
1902   Double_t rmsSupermodule[18];
1903   Double_t rmsDetector[540];
1904   Int_t countAll = 0;
1905   Int_t countSupermodule[18];
1906   Int_t countDetector[540];
1907   for(Int_t sm = 0; sm < 18; sm++){
1908     rmsSupermodule[sm] = 0.0;
1909     meanSupermodule[sm] = 0.0;
1910     countSupermodule[sm] = 0;
1911   }
1912   for(Int_t det = 0; det < 540; det++){
1913     rmsDetector[det] = 0.0;
1914     meanDetector[det] = 0.0;
1915     countDetector[det] = 0;
1916   }
1917   ////////////
1918   // compute
1919   ////////////
1920   for (Int_t k = 0; k < loop; k++) {
1921     detector  = ((AliTRDFitInfo *) fVectorFit.At(k))->GetDetector();
1922     sector = GetSector(detector);
1923     if(perdetector){
1924       value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[0];
1925       if(value > 0.0) {
1926         rmsDetector[detector] += value*value;
1927         meanDetector[detector] += value;
1928         countDetector[detector]++;
1929         rmsSupermodule[sector] += value*value;
1930         meanSupermodule[sector] += value;
1931         countSupermodule[sector]++;
1932         rmsAll += value*value;
1933         meanAll += value;
1934         countAll++;
1935       }
1936     }
1937     else {
1938       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
1939       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
1940       for (Int_t row = 0; row < rowMax; row++) {
1941         for (Int_t col = 0; col < colMax; col++) {
1942           value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
1943           if(value > 0.0) {
1944             rmsDetector[detector] += value*value;
1945             meanDetector[detector] += value;
1946             countDetector[detector]++;
1947             rmsSupermodule[sector] += value*value;
1948             meanSupermodule[sector] += value;
1949             countSupermodule[sector]++;
1950             rmsAll += value*value;
1951             meanAll += value;
1952             countAll++;
1953           }
1954           
1955         } // Col
1956       } // Row
1957     }
1958   }  
1959   if(countAll > 0) {
1960     meanAll = meanAll/countAll;
1961     rmsAll = TMath::Abs(rmsAll/countAll - (meanAll*meanAll));
1962   }
1963   for(Int_t sm = 0; sm < 18; sm++){
1964     if(countSupermodule[sm] > 0) {
1965       meanSupermodule[sm] = meanSupermodule[sm]/countSupermodule[sm];
1966       rmsSupermodule[sm] = TMath::Abs(rmsSupermodule[sm]/countSupermodule[sm] - (meanSupermodule[sm]*meanSupermodule[sm]));
1967     }
1968   }
1969   for(Int_t det = 0; det < 540; det++){
1970     if(countDetector[det] > 0) {
1971       meanDetector[det] = meanDetector[det]/countDetector[det];
1972       rmsDetector[det] = TMath::Abs(rmsDetector[det]/countDetector[det] - (meanDetector[det]*meanDetector[det]));
1973     }
1974   }
1975   //printf("Put mean value, meanAll %f, rmsAll %f\n",meanAll,rmsAll);
1976   ///////////////////////////////////////////////
1977   // Put the mean value for the no-fitted
1978   /////////////////////////////////////////////  
1979   for (Int_t k = 0; k < loop; k++) {
1980     detector  = ((AliTRDFitInfo *) fVectorFit.At(k))->GetDetector();
1981     sector = GetSector(detector);
1982     Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
1983     Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
1984     Float_t *coef = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef();
1985
1986     for (Int_t row = 0; row < rowMax; row++) {
1987       for (Int_t col = 0; col < colMax; col++) {
1988         value = coef[(Int_t)(col*rowMax+row)];
1989         if(value < 0.0) {
1990           if((ofwhat == 0) && (meanAll > 0.0) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanAll);
1991           if(ofwhat == 1){
1992             if((meanDetector[detector] > 0.0) && (countDetector[detector] > 20)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanDetector[detector]);
1993             else if((meanSupermodule[sector] > 0.0) && (countSupermodule[sector] > 15)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanSupermodule[sector]);
1994             else if((meanAll > 0.0) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanAll);
1995           }  
1996         }
1997         // Debug
1998         if(fDebugLevel > 1){
1999           
2000           if ( !fDebugStreamer ) {
2001             //debug stream
2002             TDirectory *backup = gDirectory;
2003             fDebugStreamer = new TTreeSRedirector("TRDDebugFit.root");
2004             if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
2005           } 
2006           
2007           Float_t coefnow      = coef[(Int_t)(col*rowMax+row)]; 
2008           
2009           (* fDebugStreamer) << "PutMeanValueOtherVectorFit"<<
2010             "detector="<<detector<<
2011             "sector="<<sector<<
2012             "row="<<row<<
2013             "col="<<col<<
2014             "before="<<value<<
2015             "after="<<coefnow<<
2016             "\n";  
2017         }
2018       } // Col
2019     } // Row
2020   }
2021 }
2022 //______________________________________________________________________
2023 void AliTRDCalibraFit::PutMeanValueOtherVectorFit2(Int_t ofwhat, Bool_t perdetector){
2024   //
2025   // ofwhat is equaled to 0: mean value of all passing detectors
2026   // ofwhat is equaled to 1: mean value of the detector, otherwise supermodule, otherwise all
2027   //
2028
2029   Int_t loop = (Int_t) fVectorFit2.GetEntriesFast();
2030   if(loop != 540) {
2031     AliInfo("The Vector Fit is not complete!");
2032     return;
2033   }
2034   Int_t detector = -1;
2035   Int_t sector = -1;
2036   Float_t value  = 0.0;
2037
2038   /////////////////////////////////
2039   // Calculate the mean values
2040   ////////////////////////////////
2041   // Initialisation
2042   ////////////////////////
2043   Double_t meanAll = 0.0;
2044   Double_t rmsAll = 0.0;
2045   Double_t meanSupermodule[18];
2046   Double_t rmsSupermodule[18];
2047   Double_t meanDetector[540];
2048   Double_t rmsDetector[540];
2049   Int_t countAll = 0;
2050   Int_t countSupermodule[18];
2051   Int_t countDetector[540];
2052   for(Int_t sm = 0; sm < 18; sm++){
2053     rmsSupermodule[sm] = 0.0;
2054     meanSupermodule[sm] = 0.0;
2055     countSupermodule[sm] = 0;
2056   }
2057   for(Int_t det = 0; det < 540; det++){
2058     rmsDetector[det] = 0.0;
2059     meanDetector[det] = 0.0;
2060     countDetector[det] = 0;
2061   }
2062   // compute
2063   ////////////
2064   for (Int_t k = 0; k < loop; k++) {
2065     detector  = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetDetector();
2066     sector = GetSector(detector);
2067     if(perdetector){
2068       value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[0];
2069       if(value < 70.0) {
2070         rmsDetector[detector] += value*value;
2071         meanDetector[detector] += value;
2072         countDetector[detector]++;
2073         rmsSupermodule[sector] += value*value;
2074         meanSupermodule[sector] += value;
2075         countSupermodule[sector]++;
2076         meanAll += value;
2077         rmsAll += value*value;
2078         countAll++;
2079       }
2080     }
2081     else {
2082       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
2083       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
2084       for (Int_t row = 0; row < rowMax; row++) {
2085         for (Int_t col = 0; col < colMax; col++) {
2086           value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2087           if(value < 70.0) {
2088             rmsDetector[detector] += value*value;
2089             meanDetector[detector] += value;
2090             countDetector[detector]++;
2091             rmsSupermodule[sector] += value*value;
2092             meanSupermodule[sector] += value;
2093             countSupermodule[sector]++;
2094             rmsAll += value*value;
2095             meanAll += value;
2096             countAll++;
2097           }
2098           
2099         } // Col
2100       } // Row
2101     }
2102   }  
2103   if(countAll > 0) {
2104     meanAll = meanAll/countAll;
2105     rmsAll = TMath::Abs(rmsAll/countAll - (meanAll*meanAll));
2106   }
2107   for(Int_t sm = 0; sm < 18; sm++){
2108     if(countSupermodule[sm] > 0) {
2109       meanSupermodule[sm] = meanSupermodule[sm]/countSupermodule[sm];
2110       rmsSupermodule[sm] = TMath::Abs(rmsSupermodule[sm]/countSupermodule[sm] - (meanSupermodule[sm]*meanSupermodule[sm]));
2111     }
2112   }
2113   for(Int_t det = 0; det < 540; det++){
2114     if(countDetector[det] > 0) {
2115       meanDetector[det] = meanDetector[det]/countDetector[det];
2116       rmsDetector[det] = TMath::Abs(rmsDetector[det]/countDetector[det] - (meanDetector[det]*meanDetector[det]));
2117     }
2118   }
2119   //printf("Put mean value 2: meanAll %f, rmsAll %f\n",meanAll,rmsAll);
2120   ////////////////////////////////////////////
2121   // Put the mean value for the no-fitted
2122   /////////////////////////////////////////////  
2123   for (Int_t k = 0; k < loop; k++) {
2124     detector  = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetDetector();
2125     sector = GetSector(detector);
2126     Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
2127     Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
2128     Float_t *coef = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef();
2129
2130     for (Int_t row = 0; row < rowMax; row++) {
2131       for (Int_t col = 0; col < colMax; col++) {
2132         value = coef[(Int_t)(col*rowMax+row)];
2133         if(value > 70.0) {
2134           if((ofwhat == 0) && (meanAll > -3.0) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = meanAll+100.0;
2135           if(ofwhat == 1){
2136             if((meanDetector[detector] > -3.0) && (countDetector[detector] > 20)) coef[(Int_t)(col*rowMax+row)] = meanDetector[detector]+100.0;
2137             else if((meanSupermodule[sector] > -3.0) && (countSupermodule[sector] > 15)) coef[(Int_t)(col*rowMax+row)] = meanSupermodule[sector]+100.0;
2138             else if((meanAll > -3.0) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = meanAll+100.0;
2139           }  
2140         }
2141         // Debug
2142         if(fDebugLevel > 1){
2143           
2144           if ( !fDebugStreamer ) {
2145             //debug stream
2146             TDirectory *backup = gDirectory;
2147             fDebugStreamer = new TTreeSRedirector("TRDDebugFit.root");
2148             if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
2149           } 
2150           
2151           Float_t coefnow      = coef[(Int_t)(col*rowMax+row)]; 
2152           
2153           (* fDebugStreamer) << "PutMeanValueOtherVectorFit2"<<
2154             "detector="<<detector<<
2155             "sector="<<sector<<
2156             "row="<<row<<
2157             "col="<<col<<
2158             "before="<<value<<
2159             "after="<<coefnow<<
2160             "\n";  
2161         }
2162       } // Col
2163     } // Row
2164   }
2165   
2166 }
2167 //_____________________________________________________________________________
2168 AliTRDCalDet *AliTRDCalibraFit::CreateDetObjectVdrift(const TObjArray *vectorFit, Bool_t perdetector)
2169 {
2170   //
2171   // It creates the AliTRDCalDet object from the AliTRDFitInfo
2172   // It takes the mean value of the coefficients per detector 
2173   // This object has to be written in the database
2174   //
2175   
2176   // Create the DetObject
2177   AliTRDCalDet *object = new AliTRDCalDet("ChamberVdrift","TRD drift velocities (detector value)");
2178
2179   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2180   if(loop != 540) AliInfo("The Vector Fit is not complete!");
2181   Int_t detector = -1;
2182   Float_t value  = 0.0;
2183   
2184   //
2185   for (Int_t k = 0; k < loop; k++) {
2186     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2187     Float_t mean  = 0.0;
2188     if(perdetector){
2189       mean = TMath::Abs(((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[0]);
2190     }
2191     else {
2192       Int_t   count = 0;
2193       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
2194       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
2195       for (Int_t row = 0; row < rowMax; row++) {
2196         for (Int_t col = 0; col < colMax; col++) {
2197           value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2198           mean += TMath::Abs(value);
2199           count++;       
2200         } // Col
2201       } // Row
2202       if(count > 0) mean = mean/count;
2203     }
2204     object->SetValue(detector,mean);
2205   }
2206   
2207   return object;
2208 }
2209 //_____________________________________________________________________________
2210 AliTRDCalDet *AliTRDCalibraFit::CreateDetObjectGain(const TObjArray *vectorFit, Bool_t meanOtherBefore, Double_t scaleFitFactor, Bool_t perdetector)
2211 {
2212   //
2213   // It creates the AliTRDCalDet object from the AliTRDFitInfo
2214   // It takes the mean value of the coefficients per detector 
2215   // This object has to be written in the database
2216   //
2217   
2218   // Create the DetObject
2219   AliTRDCalDet *object = new AliTRDCalDet("ChamberGainFactor","GainFactor (detector value)");
2220   
2221   fScaleGain = scaleFitFactor;
2222  
2223   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2224   if(loop != 540) AliInfo("The Vector Fit is not complete!");
2225   Int_t detector = -1;
2226   Float_t value  = 0.0;
2227
2228   for (Int_t k = 0; k < loop; k++) {
2229     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();  
2230     Float_t mean  = 0.0;
2231     if(perdetector){
2232       value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[0];
2233       if(!meanOtherBefore){
2234         if(value > 0) value = value*scaleFitFactor;
2235       }
2236       else value = value*scaleFitFactor;
2237       mean = TMath::Abs(value);
2238     }
2239     else{
2240       Int_t   count = 0;
2241       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
2242       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
2243       for (Int_t row = 0; row < rowMax; row++) {
2244         for (Int_t col = 0; col < colMax; col++) {
2245           value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2246           if(!meanOtherBefore) {
2247             if(value > 0) value = value*scaleFitFactor;
2248           }
2249           else value = value*scaleFitFactor;
2250           mean += TMath::Abs(value);
2251           count++;       
2252         } // Col
2253       } // Row
2254       if(count > 0) mean = mean/count;
2255     }
2256     if(mean < 0.1) mean = 0.1;
2257     object->SetValue(detector,mean);
2258   }
2259  
2260   return object;
2261 }
2262 //_____________________________________________________________________________
2263 AliTRDCalDet *AliTRDCalibraFit::CreateDetObjectT0(const TObjArray *vectorFit, Bool_t perdetector)
2264 {
2265   //
2266   // It creates the AliTRDCalDet object from the AliTRDFitInfo2
2267   // It takes the min value of the coefficients per detector 
2268   // This object has to be written in the database
2269   //
2270   
2271   // Create the DetObject
2272   AliTRDCalDet *object = new AliTRDCalDet("ChamberT0","T0 (detector value)");
2273   
2274   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2275   if(loop != 540) AliInfo("The Vector Fit is not complete!");
2276   Int_t detector = -1;
2277   Float_t value  = 0.0;
2278
2279   for (Int_t k = 0; k < loop; k++) {
2280     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();   
2281     Float_t min  = 100.0;
2282     if(perdetector){
2283       value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[0];
2284       //printf("Create det object %f for %d\n",value,k);
2285       // check successful
2286       if(value > 70.0) value = value-100.0;
2287       //
2288       min = value;
2289     }
2290     else{
2291       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
2292       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
2293       for (Int_t row = 0; row < rowMax; row++) {
2294         for (Int_t col = 0; col < colMax; col++) {
2295           value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2296           // check successful
2297           if(value > 70.0) value = value-100.0;
2298           //
2299           if(min > value) min = value;
2300         } // Col
2301       } // Row
2302     }
2303     object->SetValue(detector,min);
2304   }
2305
2306   return object;
2307
2308 }
2309 //_____________________________________________________________________________
2310 AliTRDCalDet *AliTRDCalibraFit::CreateDetObjectLorentzAngle(const TObjArray *vectorFit)
2311 {
2312   //
2313   // It creates the AliTRDCalDet object from the AliTRDFitInfo2
2314   // It takes the min value of the coefficients per detector 
2315   // This object has to be written in the database
2316   //
2317   
2318   // Create the DetObject
2319   AliTRDCalDet *object = new AliTRDCalDet("tan(lorentzangle)","tan(lorentzangle) (detector value)");
2320   
2321   
2322   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2323   if(loop != 540) AliInfo("The Vector Fit is not complete!");
2324   Int_t detector = -1;
2325   Float_t value  = 0.0;
2326
2327   for (Int_t k = 0; k < loop; k++) {
2328     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2329     /*
2330       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
2331       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
2332       Float_t min  = 100.0;
2333       for (Int_t row = 0; row < rowMax; row++) {
2334       for (Int_t col = 0; col < colMax; col++) {
2335       value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2336       mean += -TMath::Abs(value);
2337       count++;       
2338       } // Col
2339       } // Row
2340       if(count > 0) mean = mean/count;
2341     */
2342     value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[0];
2343     if(value > 70.0) value = value-100.0;
2344     object->SetValue(detector,value);
2345   }
2346
2347   return object;
2348   
2349 }
2350 //_____________________________________________________________________________
2351 AliTRDCalDet *AliTRDCalibraFit::CreateDetObjectExbAlt(const TObjArray *vectorFit)
2352 {
2353   //
2354   // It creates the AliTRDCalDet object from the AliTRDFitInfo2
2355   // It takes the min value of the coefficients per detector 
2356   // This object has to be written in the database
2357   //
2358   
2359   // Create the DetObject
2360   AliTRDCalDet *object = new AliTRDCalDet("tan(lorentzangle)","tan(lorentzangle) (detector value)");
2361   
2362   
2363   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2364   if(loop != 540) AliInfo("The Vector Fit is not complete!");
2365   Int_t detector = -1;
2366   Float_t value  = 0.0;
2367
2368   for (Int_t k = 0; k < loop; k++) {
2369     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2370     /*
2371       Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
2372       Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
2373       Float_t min  = 100.0;
2374       for (Int_t row = 0; row < rowMax; row++) {
2375       for (Int_t col = 0; col < colMax; col++) {
2376       value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2377       mean += -TMath::Abs(value);
2378       count++;       
2379       } // Col
2380       } // Row
2381       if(count > 0) mean = mean/count;
2382     */
2383     value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[0];
2384     //if(value > 70.0) value = value-100.0;
2385     object->SetValue(detector,value);
2386   }
2387
2388   return object;
2389   
2390 }
2391 //_____________________________________________________________________________
2392 TObject *AliTRDCalibraFit::CreatePadObjectGain(const TObjArray *vectorFit, Double_t scaleFitFactor, const AliTRDCalDet *detobject)
2393 {
2394   //
2395   // It Creates the AliTRDCalPad object from AliTRDFitInfo
2396   // You need first to create the object for the detectors,
2397   // where the mean value is put.
2398   // This object has to be written in the database
2399   //
2400   
2401   // Create the DetObject
2402   AliTRDCalPad *object = new AliTRDCalPad("GainFactor","GainFactor (local variations)");
2403   
2404   if(!vectorFit){
2405     for(Int_t k = 0; k < 540; k++){
2406       AliTRDCalROC *calROC = object->GetCalROC(k);
2407       Int_t nchannels = calROC->GetNchannels();
2408       for(Int_t ch = 0; ch < nchannels; ch++){
2409         calROC->SetValue(ch,1.0);
2410       }
2411     }
2412   }
2413   else{
2414
2415     Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2416     if(loop != 540) AliInfo("The Vector Fit is not complete!");
2417     Int_t detector = -1;
2418     Float_t value  = 0.0;
2419     
2420     for (Int_t k = 0; k < loop; k++) {
2421       detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2422       AliTRDCalROC *calROC = object->GetCalROC(detector);
2423       Float_t mean         = detobject->GetValue(detector);
2424       if(TMath::Abs(mean) <= 0.0000000001) continue;
2425       Int_t rowMax    = calROC->GetNrows();
2426       Int_t colMax    = calROC->GetNcols();
2427       for (Int_t row = 0; row < rowMax; row++) {
2428         for (Int_t col = 0; col < colMax; col++) {
2429           value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2430           if(value > 0) value = value*scaleFitFactor;
2431           calROC->SetValue(col,row,TMath::Abs(value)/mean);
2432         } // Col
2433       } // Row
2434     } 
2435   }
2436
2437   return object;  
2438 }
2439 //_____________________________________________________________________________
2440 TObject *AliTRDCalibraFit::CreatePadObjectVdrift(const TObjArray *vectorFit, const AliTRDCalDet *detobject)
2441 {
2442   //
2443   // It Creates the AliTRDCalPad object from AliTRDFitInfo
2444   // You need first to create the object for the detectors,
2445   // where the mean value is put.
2446   // This object has to be written in the database
2447   //
2448
2449   // Create the DetObject
2450   AliTRDCalPad *object = new AliTRDCalPad("LocalVdrift","TRD drift velocities (local variations)");
2451
2452   if(!vectorFit){
2453     for(Int_t k = 0; k < 540; k++){
2454       AliTRDCalROC *calROC = object->GetCalROC(k);
2455       Int_t nchannels = calROC->GetNchannels();
2456       for(Int_t ch = 0; ch < nchannels; ch++){
2457         calROC->SetValue(ch,1.0);
2458       }
2459     }
2460   }
2461   else {
2462     
2463     Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2464     if(loop != 540) AliInfo("The Vector Fit is not complete!");
2465     Int_t detector = -1;
2466     Float_t value  = 0.0;
2467     
2468     for (Int_t k = 0; k < loop; k++) {
2469       detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2470       AliTRDCalROC *calROC = object->GetCalROC(detector);
2471       Float_t mean         = detobject->GetValue(detector);
2472       if(mean == 0) continue;
2473       Int_t rowMax    = calROC->GetNrows();
2474       Int_t colMax    = calROC->GetNcols();
2475       for (Int_t row = 0; row < rowMax; row++) {
2476         for (Int_t col = 0; col < colMax; col++) {
2477           value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2478           calROC->SetValue(col,row,TMath::Abs(value)/mean);
2479         } // Col
2480       } // Row
2481     } 
2482   }
2483   return object;    
2484
2485 }
2486 //_____________________________________________________________________________
2487 TObject *AliTRDCalibraFit::CreatePadObjectT0(const TObjArray *vectorFit, const AliTRDCalDet *detobject)
2488 {
2489   //
2490   // It Creates the AliTRDCalPad object from AliTRDFitInfo2
2491   // You need first to create the object for the detectors,
2492   // where the mean value is put.
2493   // This object has to be written in the database
2494   //
2495   
2496   // Create the DetObject
2497   AliTRDCalPad *object = new AliTRDCalPad("LocalT0","T0 (local variations)");
2498
2499   if(!vectorFit){
2500     for(Int_t k = 0; k < 540; k++){
2501       AliTRDCalROC *calROC = object->GetCalROC(k);
2502       Int_t nchannels = calROC->GetNchannels();
2503       for(Int_t ch = 0; ch < nchannels; ch++){
2504         calROC->SetValue(ch,0.0);
2505       }
2506     }
2507   }
2508   else {
2509     
2510     Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2511     if(loop != 540) AliInfo("The Vector Fit is not complete!");
2512     Int_t detector = -1;
2513     Float_t value  = 0.0;
2514     
2515     for (Int_t k = 0; k < loop; k++) {
2516       detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2517       AliTRDCalROC *calROC = object->GetCalROC(detector);
2518       Float_t min          = detobject->GetValue(detector);
2519       Int_t rowMax    = calROC->GetNrows();
2520       Int_t colMax    = calROC->GetNcols();
2521       for (Int_t row = 0; row < rowMax; row++) {
2522         for (Int_t col = 0; col < colMax; col++) {
2523           value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2524           // check successful
2525           if(value > 70.0) value = value - 100.0;
2526           //
2527           calROC->SetValue(col,row,value-min);
2528         } // Col
2529       } // Row
2530     } 
2531   }
2532   return object;    
2533
2534 }
2535 //_____________________________________________________________________________
2536 TObject *AliTRDCalibraFit::CreatePadObjectPRF(const TObjArray *vectorFit)
2537 {
2538   //
2539   // It Creates the AliTRDCalPad object from AliTRDFitInfo
2540   // This object has to be written in the database
2541   //
2542   
2543   // Create the DetObject
2544   AliTRDCalPad *object = new AliTRDCalPad("PRFWidth","PRFWidth");
2545
2546   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2547   if(loop != 540) AliInfo("The Vector Fit is not complete!");
2548   Int_t detector = -1;
2549   Float_t value  = 0.0;
2550
2551   for (Int_t k = 0; k < loop; k++) {
2552     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2553     AliTRDCalROC *calROC = object->GetCalROC(detector);
2554     Int_t rowMax    = calROC->GetNrows();
2555     Int_t colMax    = calROC->GetNcols();
2556     for (Int_t row = 0; row < rowMax; row++) {
2557       for (Int_t col = 0; col < colMax; col++) {
2558         value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
2559         calROC->SetValue(col,row,TMath::Abs(value));
2560       } // Col
2561     } // Row
2562   } 
2563
2564   return object;  
2565
2566 }
2567 //_____________________________________________________________________________
2568 AliTRDCalDet *AliTRDCalibraFit::MakeOutliersStatDet(const TObjArray *vectorFit, const char *name, Double_t &mean)
2569 {
2570   //
2571   // It Creates the AliTRDCalDet object from AliTRDFitInfo
2572   // 0 successful fit 1 not successful fit
2573   // mean is the mean value over the successful fit
2574   // do not use it for t0: no meaning
2575   //
2576   
2577   // Create the CalObject
2578   AliTRDCalDet *object = new AliTRDCalDet(name,name);
2579   mean = 0.0;
2580   Int_t count = 0;
2581   
2582   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2583   if(loop != 540) {
2584     AliInfo("The Vector Fit is not complete! We initialise all outliers");
2585     for(Int_t k = 0; k < 540; k++){
2586       object->SetValue(k,1.0);
2587     }
2588   }
2589   Int_t detector = -1;
2590   Float_t value  = 0.0;
2591   
2592   for (Int_t k = 0; k < loop; k++) {
2593     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2594     value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[0];
2595     if(value <= 0) object->SetValue(detector,1.0);
2596     else {
2597       object->SetValue(detector,0.0);
2598       mean += value;
2599       count++;
2600     }
2601   }
2602   if(count > 0) mean /= count;
2603   return object;  
2604 }
2605 //_____________________________________________________________________________
2606 TObject *AliTRDCalibraFit::MakeOutliersStatPad(const TObjArray *vectorFit, const char *name, Double_t &mean)
2607 {
2608   //
2609   // It Creates the AliTRDCalPad object from AliTRDFitInfo
2610   // 0 not successful fit 1 successful fit
2611   // mean mean value over the successful fit
2612   //
2613   
2614   // Create the CalObject
2615   AliTRDCalPad *object = new AliTRDCalPad(name,name);
2616   mean = 0.0;
2617   Int_t count = 0;
2618   
2619   Int_t loop = (Int_t) vectorFit->GetEntriesFast();
2620   if(loop != 540) {
2621     AliInfo("The Vector Fit is not complete! We initialise all outliers");
2622     for(Int_t k = 0; k < 540; k++){
2623       AliTRDCalROC *calROC = object->GetCalROC(k);
2624       Int_t nchannels = calROC->GetNchannels();
2625       for(Int_t ch = 0; ch < nchannels; ch++){
2626         calROC->SetValue(ch,1.0);
2627       }
2628     }
2629   }
2630   Int_t detector = -1;
2631   Float_t value  = 0.0;
2632   
2633   for (Int_t k = 0; k < loop; k++) {
2634     detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
2635     AliTRDCalROC *calROC = object->GetCalROC(detector);
2636     Int_t nchannels    = calROC->GetNchannels();
2637     for (Int_t ch = 0; ch < nchannels; ch++) {
2638       value = ((AliTRDFitInfo *) vectorFit->At(k))->GetCoef()[ch];
2639       if(value <= 0) calROC->SetValue(ch,1.0);
2640       else {
2641         calROC->SetValue(ch,0.0);
2642         mean += value;
2643         count++;
2644       }
2645     } // channels
2646   }
2647   if(count > 0) mean /= count;
2648   return object;  
2649 }
2650 //_____________________________________________________________________________
2651 void AliTRDCalibraFit::SetPeriodeFitPH(Int_t periodeFitPH)
2652
2653   //
2654   // Set FitPH if 1 then each detector will be fitted
2655   //
2656
2657   if (periodeFitPH > 0) {
2658     fFitPHPeriode   = periodeFitPH; 
2659   }
2660   else {
2661     AliInfo("periodeFitPH must be higher than 0!");
2662   }
2663
2664 }
2665 //_____________________________________________________________________________
2666 void AliTRDCalibraFit::SetBeginFitCharge(Float_t beginFitCharge)
2667
2668   //
2669   // The fit of the deposited charge distribution begins at
2670   // histo->Mean()/beginFitCharge
2671   // You can here set beginFitCharge
2672   //
2673
2674   if (beginFitCharge > 0) {
2675     fBeginFitCharge = beginFitCharge; 
2676   }
2677   else {
2678     AliInfo("beginFitCharge must be strict positif!");
2679   }
2680
2681 }
2682
2683 //_____________________________________________________________________________
2684 void AliTRDCalibraFit::SetT0Shift0(Float_t t0Shift) 
2685
2686   //
2687   // The t0 calculated with the maximum positif slope is shift from t0Shift0
2688   // You can here set t0Shift0
2689   //
2690
2691   if (t0Shift > 0) {
2692     fT0Shift0 = t0Shift; 
2693   } 
2694   else {
2695     AliInfo("t0Shift0 must be strict positif!");
2696   }
2697
2698 }
2699
2700 //_____________________________________________________________________________
2701 void AliTRDCalibraFit::SetT0Shift1(Float_t t0Shift) 
2702
2703   //
2704   // The t0 calculated with the maximum of the amplification region is shift from t0Shift1
2705   // You can here set t0Shift1
2706   //
2707
2708   if (t0Shift > 0) {
2709     fT0Shift1 = t0Shift; 
2710   } 
2711   else {
2712     AliInfo("t0Shift must be strict positif!");
2713   }
2714
2715 }
2716
2717 //_____________________________________________________________________________
2718 void AliTRDCalibraFit::SetRangeFitPRF(Float_t rangeFitPRF)
2719
2720   //
2721   // The fit of the PRF is from -rangeFitPRF to rangeFitPRF
2722   // You can here set rangeFitPRF
2723   //
2724
2725   if ((rangeFitPRF >    0) && 
2726       (rangeFitPRF <= 1.5)) {
2727     fRangeFitPRF = rangeFitPRF;
2728   } 
2729   else {
2730     AliInfo("rangeFitPRF must be between 0 and 1.0");
2731   }
2732
2733 }
2734
2735 //_____________________________________________________________________________
2736 void AliTRDCalibraFit::SetMinEntries(Int_t minEntries)
2737
2738   //
2739   // Minimum entries for fitting
2740   //
2741
2742   if (minEntries >    0) {
2743     fMinEntries = minEntries;
2744   } 
2745   else {
2746     AliInfo("fMinEntries must be >= 0.");
2747   }
2748
2749 }
2750
2751 //_____________________________________________________________________________
2752 void AliTRDCalibraFit::SetRebin(Short_t rebin)
2753
2754   //
2755   // Rebin with rebin time less bins the Ch histo
2756   // You can set here rebin that should divide the number of bins of CH histo
2757   //
2758
2759   if (rebin > 0) {
2760     fRebin = rebin; 
2761     AliInfo("You have to be sure that fRebin divides fNumberBinCharge used!");
2762   } 
2763   else {
2764     AliInfo("You have to choose a positiv value!");
2765   }
2766
2767 }
2768 //_____________________________________________________________________________
2769 Bool_t AliTRDCalibraFit::FillVectorFit()
2770 {
2771   //
2772   // For the Fit functions fill the vector Fit
2773   //
2774
2775   AliTRDFitInfo *fitInfo = new AliTRDFitInfo();
2776
2777   Int_t ntotal = 1;
2778   if (GetStack(fCountDet) == 2) {
2779     ntotal = 1728;
2780   }
2781   else {
2782     ntotal = 2304;
2783   }
2784
2785   //printf("For the detector %d , ntotal %d and fCoefCH[0] %f\n",countdet,ntotal,fCoefCH[0]);
2786   Float_t *coef = new Float_t[ntotal];
2787   for (Int_t i = 0; i < ntotal; i++) {
2788     coef[i] = fCurrentCoefDetector[i];
2789   }
2790   
2791   Int_t detector = fCountDet;
2792   // Set
2793   fitInfo->SetCoef(coef);
2794   fitInfo->SetDetector(detector);
2795   fVectorFit.Add((TObject *) fitInfo);
2796
2797   return kTRUE;
2798
2799 }
2800 //_____________________________________________________________________________
2801 Bool_t AliTRDCalibraFit::FillVectorFit2()
2802 {
2803   //
2804   // For the Fit functions fill the vector Fit
2805   //
2806
2807   AliTRDFitInfo *fitInfo = new AliTRDFitInfo();
2808
2809   Int_t ntotal = 1;
2810   if (GetStack(fCountDet) == 2) {
2811     ntotal = 1728;
2812   }
2813   else {
2814     ntotal = 2304;
2815   }
2816
2817   //printf("For the detector %d , ntotal %d and fCoefCH[0] %f\n",countdet,ntotal,fCoefCH[0]);
2818   Float_t *coef = new Float_t[ntotal];
2819   for (Int_t i = 0; i < ntotal; i++) {
2820     coef[i] = fCurrentCoefDetector2[i];
2821   }
2822   
2823   Int_t detector = fCountDet;
2824   // Set
2825   fitInfo->SetCoef(coef);
2826   fitInfo->SetDetector(detector);
2827   fVectorFit2.Add((TObject *) fitInfo);
2828
2829   return kTRUE;
2830
2831 }
2832 //____________Functions for initialising the AliTRDCalibraFit in the code_________
2833 Bool_t AliTRDCalibraFit::InitFit(Int_t nbins, Int_t i)
2834 {
2835   //
2836   // Init the number of expected bins and fDect1[i] fDect2[i] 
2837   //
2838
2839   gStyle->SetPalette(1);
2840   gStyle->SetOptStat(1111);
2841   gStyle->SetPadBorderMode(0);
2842   gStyle->SetCanvasColor(10);
2843   gStyle->SetPadLeftMargin(0.13);
2844   gStyle->SetPadRightMargin(0.01);
2845   
2846   // Mode groups of pads: the total number of bins!
2847   CalculNumberOfBinsExpected(i);
2848   
2849   // Quick verification that we have the good pad calibration mode!
2850   if (fNumberOfBinsExpected != nbins) {
2851     AliInfo(Form("It doesn't correspond to the mode of pad group calibration: expected %d and seen %d!",fNumberOfBinsExpected,nbins));
2852     return kFALSE;
2853   }
2854   
2855   // Security for fDebug 3 and 4
2856   if ((fDebugLevel >= 3) && 
2857       ((fDet[0] >  5) || 
2858        (fDet[1] >  4) || 
2859        (fDet[2] > 17))) {
2860     AliInfo("This detector doesn't exit!");
2861     return kFALSE;
2862   }
2863
2864   // Determine fDet1 and fDet2 and set the fNfragZ and fNfragRphi for debug 3 and 4
2865   CalculDect1Dect2(i);
2866
2867  
2868   return kTRUE;
2869 }
2870 //____________Functions for initialising the AliTRDCalibraFit in the code_________
2871 Bool_t AliTRDCalibraFit::InitFitCH()
2872 {
2873   //
2874   // Init the fVectorFitCH for normalisation
2875   // Init the histo for debugging 
2876   //
2877
2878   gDirectory = gROOT;
2879  
2880   fScaleFitFactor = 0.0;
2881   if( fCurrentCoefDetector ) delete [] fCurrentCoefDetector;
2882   fCurrentCoefDetector   = new Float_t[2304];
2883   for (Int_t k = 0; k < 2304; k++) {
2884     fCurrentCoefDetector[k] = 0.0;    
2885   }
2886   fVectorFit.SetName("gainfactorscoefficients");
2887
2888   // fDebug == 0 nothing
2889   // fDebug == 1 and fFitVoir no histo
2890   if (fDebugLevel == 1) {
2891     if(!CheckFitVoir()) return kFALSE;
2892   }
2893   //Get the CalDet object
2894   if(fAccCDB){
2895     AliTRDcalibDB     *cal    = AliTRDcalibDB::Instance();
2896     if (!cal) {
2897       AliInfo("Could not get calibDB");
2898       return kFALSE;
2899     }
2900     if(fCalDet) delete fCalDet;
2901     fCalDet = new AliTRDCalDet(*(cal->GetGainFactorDet()));
2902   }
2903   else{
2904     Float_t devalue = 1.0;
2905     if(fCalDet) delete fCalDet;
2906     fCalDet = new AliTRDCalDet("ChamberGainFactor","GainFactor (detector value)");
2907     for(Int_t k = 0; k < 540; k++){
2908       fCalDet->SetValue(k,devalue);
2909     }
2910   }
2911   return kTRUE;
2912   
2913 }
2914 //____________Functions for initialising the AliTRDCalibraFit in the code_________
2915 Bool_t AliTRDCalibraFit::InitFitPH()
2916 {
2917   //
2918   // Init the arrays of results 
2919   // Init the histos for debugging 
2920   //
2921
2922   gDirectory = gROOT;
2923   fVectorFit.SetName("driftvelocitycoefficients");
2924   fVectorFit2.SetName("t0coefficients");
2925
2926   if( fCurrentCoefDetector ) delete [] fCurrentCoefDetector;
2927   fCurrentCoefDetector   = new Float_t[2304];
2928   for (Int_t k = 0; k < 2304; k++) {
2929     fCurrentCoefDetector[k] = 0.0;    
2930   }
2931   if( fCurrentCoefDetector2 ) delete [] fCurrentCoefDetector2;
2932   fCurrentCoefDetector2   = new Float_t[2304];
2933   for (Int_t k = 0; k < 2304; k++) {
2934     fCurrentCoefDetector2[k] = 0.0;    
2935   }
2936  
2937   //fDebug == 0 nothing
2938   // fDebug == 1 and fFitVoir no histo
2939   if (fDebugLevel == 1) {
2940     if(!CheckFitVoir()) return kFALSE;
2941   }
2942   //Get the CalDet object
2943   if(fAccCDB){
2944     AliTRDcalibDB     *cal    = AliTRDcalibDB::Instance();
2945     if (!cal) {
2946       AliInfo("Could not get calibDB");
2947       return kFALSE;
2948     }
2949     if(fCalDet) delete fCalDet;
2950     if(fCalDet2) delete fCalDet2;
2951     fCalDet  = new AliTRDCalDet(*(cal->GetVdriftDet()));
2952     fCalDet2 = new AliTRDCalDet(*(cal->GetT0Det())); 
2953   }
2954   else{
2955     Float_t devalue  = 1.5;
2956     Float_t devalue2 = 0.0; 
2957     if(fCalDet) delete fCalDet;
2958     if(fCalDet2) delete fCalDet2;
2959     fCalDet  = new AliTRDCalDet("ChamberVdrift","TRD drift velocities (detector value)");
2960     fCalDet2 = new AliTRDCalDet("ChamberT0","T0 (detector value)");
2961     for(Int_t k = 0; k < 540; k++){
2962       fCalDet->SetValue(k,devalue);
2963       fCalDet2->SetValue(k,devalue2);
2964     }
2965   }
2966   return kTRUE;
2967 }
2968 //____________Functions for initialising the AliTRDCalibraFit in the code_________
2969 Bool_t AliTRDCalibraFit::InitFitPRF()
2970 {
2971   //
2972   // Init the calibration mode (Nz, Nrphi), the histograms for
2973   // debugging the fit methods if fDebug > 0, 
2974   //
2975   
2976   gDirectory = gROOT;
2977   fVectorFit.SetName("prfwidthcoefficients");
2978  
2979   if( fCurrentCoefDetector ) delete [] fCurrentCoefDetector;
2980   fCurrentCoefDetector   = new Float_t[2304];
2981   for (Int_t k = 0; k < 2304; k++) {
2982     fCurrentCoefDetector[k] = 0.0;    
2983   }
2984   
2985   // fDebug == 0 nothing
2986   // fDebug == 1 and fFitVoir no histo
2987   if (fDebugLevel == 1) {
2988     if(!CheckFitVoir()) return kFALSE;
2989   }
2990   return kTRUE;
2991 }
2992 //____________Functions for initialising the AliTRDCalibraFit in the code_________
2993 Bool_t AliTRDCalibraFit::InitFitLinearFitter()
2994 {
2995   //
2996   // Init the fCalDet, fVectorFit fCurrentCoefDetector 
2997   //
2998   
2999   gDirectory = gROOT;
3000  
3001  if( fCurrentCoefDetector ) delete [] fCurrentCoefDetector;
3002  if( fCurrentCoefDetector2 ) delete [] fCurrentCoefDetector2;
3003   fCurrentCoefDetector   = new Float_t[2304];
3004   fCurrentCoefDetector2  = new Float_t[2304];
3005   for (Int_t k = 0; k < 2304; k++) {
3006     fCurrentCoefDetector[k]  = 0.0;
3007     fCurrentCoefDetector2[k] = 0.0;    
3008   }
3009
3010   if((!fCalDetVdriftUsed) || (!fCalDetExBUsed)) return kFALSE; 
3011
3012   return kTRUE;
3013 }
3014 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3015 Bool_t AliTRDCalibraFit::InitFitExbAlt()
3016 {
3017   //
3018   // Init the fCalDet, fVectorFit fCurrentCoefDetector 
3019   //
3020   
3021   gDirectory = gROOT;
3022  
3023   if( fCurrentCoefDetector2 ) delete [] fCurrentCoefDetector2;
3024   fCurrentCoefDetector2   = new Float_t[2304];
3025   for (Int_t k = 0; k < 2304; k++) {
3026     fCurrentCoefDetector2[k]  = 0.0;
3027   }
3028
3029   return kTRUE;
3030 }
3031 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3032 void AliTRDCalibraFit::InitfCountDetAndfCount(Int_t i)
3033 {
3034   //
3035   // Init the current detector where we are fCountDet and the
3036   // next fCount for the functions Fit... 
3037   //
3038
3039   // Loop on the Xbins of ch!!
3040   fCountDet = -1; // Current detector
3041   fCount    =  0; // To find the next detector
3042   
3043   // If fDebug >= 3
3044   if (fDebugLevel >= 3) {
3045     // Set countdet to the detector
3046     fCountDet = AliTRDgeometry::GetDetector(fDet[0],fDet[1],fDet[2]);
3047     // Set counter to write at the end of the detector
3048     fCount = fDect2;
3049     // Get the right calib objects
3050     SetCalROC(i);
3051   }
3052   if(fDebugLevel == 1) {
3053     fCountDet = 0;
3054     fCalibraMode->CalculXBins(fCountDet,i);
3055     if((fCalibraMode->GetNz(i)!=100) && (fCalibraMode->GetNrphi(i)!=100)){
3056       while(fCalibraMode->GetXbins(i) <=fFitVoir){
3057         fCountDet++;
3058         fCalibraMode->CalculXBins(fCountDet,i);
3059         //printf("GetXBins %d\n",fCalibraMode->GetXbins(i));
3060       }      
3061     }
3062     else {
3063       fCountDet++;
3064     }
3065     fCount    = fCalibraMode->GetXbins(i);
3066     fCountDet--;
3067     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3068     fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),i);
3069     fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3070                                       ,(Int_t) GetStack(fCountDet)
3071                                       ,(Int_t) GetSector(fCountDet),i);
3072   }
3073 }
3074 //_______________________________________________________________________________
3075 void AliTRDCalibraFit::CalculNumberOfBinsExpected(Int_t i)
3076 {
3077   //
3078   // Calculate the number of bins expected (calibration groups)
3079   //
3080   
3081   fNumberOfBinsExpected = 0;
3082   // All
3083   if((fCalibraMode->GetNz(i) == 100) && (fCalibraMode->GetNrphi(i) == 100)){
3084     fNumberOfBinsExpected = 1;
3085     return;
3086   }
3087   // Per supermodule
3088   if((fCalibraMode->GetNz(i) == 10) && (fCalibraMode->GetNrphi(i) == 10)){
3089     fNumberOfBinsExpected = 18;
3090     return;
3091   }
3092   // More
3093   fCalibraMode->ModePadCalibration(2,i);
3094   fCalibraMode->ModePadFragmentation(0,2,0,i);
3095   fCalibraMode->SetDetChamb2(i);
3096   if (fDebugLevel > 1) {
3097     AliInfo(Form("For the chamber 2: %d",fCalibraMode->GetDetChamb2(i)));
3098   }
3099   fNumberOfBinsExpected += 6 * 18 * fCalibraMode->GetDetChamb2(i);
3100   fCalibraMode->ModePadCalibration(0,i);
3101   fCalibraMode->ModePadFragmentation(0,0,0,i);
3102   fCalibraMode->SetDetChamb0(i);
3103   if (fDebugLevel > 1) {
3104     AliInfo(Form("For the other chamber 0: %d",fCalibraMode->GetDetChamb0(i)));
3105   }
3106   fNumberOfBinsExpected += 6 * 4 * 18 * fCalibraMode->GetDetChamb0(i);
3107  
3108 }
3109 //_______________________________________________________________________________
3110 void AliTRDCalibraFit::CalculDect1Dect2(Int_t i)
3111 {
3112   //
3113   // Calculate the range of fits
3114   //
3115   
3116   fDect1 = -1;
3117   fDect2 = -1;
3118   if (fDebugLevel == 1) {
3119     fDect1 = fFitVoir;
3120     fDect2 = fDect1 +1;
3121   }
3122   if ((fDebugLevel == 2) || (fDebugLevel == 0)) {
3123     fDect1 = 0;
3124     fDect2 = fNumberOfBinsExpected;
3125   }
3126   if (fDebugLevel >= 3) {
3127     fCountDet = AliTRDgeometry::GetDetector(fDet[0],fDet[1],fDet[2]);
3128     fCalibraMode->CalculXBins(fCountDet,i);
3129     fDect1 = fCalibraMode->GetXbins(i);
3130     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3131     fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),i);
3132     fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3133                                       ,(Int_t) GetStack(fCountDet)
3134                                       ,(Int_t) GetSector(fCountDet),i);
3135     // Set for the next detector
3136     fDect2 = fDect1 + fCalibraMode->GetNfragZ(i)*fCalibraMode->GetNfragRphi(i);
3137   }
3138 }
3139 //_______________________________________________________________________________
3140 Bool_t AliTRDCalibraFit::CheckFitVoir()
3141 {
3142   //
3143   // Check if fFitVoir is in the range
3144   //
3145   
3146   if (fFitVoir < fNumberOfBinsExpected) {
3147     AliInfo(Form("We will see the fit of the object %d",fFitVoir));
3148   }
3149   else {
3150     AliInfo("fFitVoir is out of range of the histo!");
3151     return kFALSE;
3152   }
3153   return kTRUE;
3154 }
3155 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3156 void AliTRDCalibraFit::UpdatefCountDetAndfCount(Int_t idect, Int_t i)
3157 {
3158   //
3159   // See if we are in a new detector and update the
3160   // variables fNfragZ and fNfragRphi if yes 
3161   // Will never happen for only one detector (3 and 4)
3162   // Doesn't matter for 2
3163   //
3164   if (fCount == idect) {
3165     // On en est au detector (or first detector in the group)
3166     fCountDet += 1;
3167     AliDebug(2,Form("We are at the detector %d\n",fCountDet));
3168     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3169     fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),i);
3170     fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3171                                        ,(Int_t) GetStack(fCountDet)
3172                                        ,(Int_t) GetSector(fCountDet),i);
3173     // Set for the next detector
3174     fCount += fCalibraMode->GetNfragZ(i)*fCalibraMode->GetNfragRphi(i);
3175     // calib objects
3176     SetCalROC(i);
3177   }
3178 }
3179 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3180 void AliTRDCalibraFit::ReconstructFitRowMinRowMax(Int_t idect, Int_t i)
3181 {
3182   //
3183   // Reconstruct the min pad row, max pad row, min pad col and
3184   // max pad col of the calibration group for the Fit functions
3185   // idect is the calibration group inside the detector
3186   //
3187   if (fDebugLevel !=  1) {
3188     fCalibraMode->ReconstructionRowPadGroup((Int_t) (idect-(fCount-(fCalibraMode->GetNfragZ(i)*fCalibraMode->GetNfragRphi(i)))),i);
3189   }
3190   AliDebug(2,Form("AliTRDCalibraFit::ReconstructFitRowMinRowMax: the local calibration group is %d",idect-(fCount-(fCalibraMode->GetNfragZ(i)*fCalibraMode->GetNfragRphi(i)))));
3191   AliDebug(2,Form("AliTRDCalibraFit::ReconstructFitRowMinRowMax: the number of group per detector is %d",fCalibraMode->GetNfragZ(i)*fCalibraMode->GetNfragRphi(i)));
3192 }
3193 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3194 Bool_t AliTRDCalibraFit::NotEnoughStatisticCH(Int_t idect)
3195 {
3196   //
3197   // For the case where there are not enough entries in the histograms
3198   // of the calibration group, the value present in the choosen database
3199   // will be put. A negativ sign enables to know that a fit was not possible.
3200   //
3201   
3202   if (fDebugLevel == 1) {
3203     AliInfo("The element has not enough statistic to be fitted");
3204   }
3205   else if (fNbDet > 0){
3206     Int_t firstdetector = fCountDet;
3207     Int_t lastdetector  = fCountDet+fNbDet;
3208     //AliInfo(Form("The element %d containing the detectors %d to %d has not enough statistic to be fitted",idect,firstdetector,lastdetector));
3209     // loop over detectors
3210     for(Int_t det = firstdetector; det < lastdetector; det++){
3211
3212       //Set the calibration object again
3213       fCountDet = det;
3214       SetCalROC(0);   
3215
3216       // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3217       // Put them at 1
3218       fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),0);
3219       fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3220                                          ,(Int_t) GetStack(fCountDet)
3221                                          ,(Int_t) GetSector(fCountDet),0);
3222       // Reconstruct row min row max
3223       ReconstructFitRowMinRowMax(idect,0);      
3224
3225       // Calcul the coef from the database choosen for the detector
3226       CalculChargeCoefMean(kFALSE);
3227       
3228       //stack 2, not stack 2
3229       Int_t factor = 0;
3230       if(GetStack(fCountDet) == 2) factor = 12;
3231       else factor = 16;
3232       
3233       // Fill the fCurrentCoefDetector with negative value to say: not fitted
3234       for (Int_t k = fCalibraMode->GetRowMin(0); k < fCalibraMode->GetRowMax(0); k++) {
3235         for (Int_t j = fCalibraMode->GetColMin(0); j < fCalibraMode->GetColMax(0); j++) {
3236           fCurrentCoefDetector[(Int_t)(j*factor+k)] = -TMath::Abs(fCurrentCoef[1]);
3237         }
3238       }
3239       
3240       //Put default value negative
3241       fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
3242       fCurrentCoefE   = 0.0;
3243       
3244       // Fill the stuff
3245       FillVectorFit();
3246       // Debug
3247       if(fDebugLevel > 1){ 
3248         
3249         if ( !fDebugStreamer ) {
3250           //debug stream
3251           TDirectory *backup = gDirectory;
3252           fDebugStreamer = new TTreeSRedirector("TRDDebugFitCH.root");
3253           if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
3254         } 
3255         
3256         Int_t   detector   = fCountDet;
3257         Int_t   caligroup  = idect;
3258         Short_t rowmin     = fCalibraMode->GetRowMin(0);
3259         Short_t rowmax     = fCalibraMode->GetRowMax(0);
3260         Short_t colmin     = fCalibraMode->GetColMin(0);
3261         Short_t colmax     = fCalibraMode->GetColMax(0);
3262         Float_t gf         = fCurrentCoef[0]; 
3263         Float_t gfs        = fCurrentCoef[1]; 
3264         Float_t gfE        = fCurrentCoefE;
3265         
3266         (*fDebugStreamer) << "FillFillCH" <<
3267           "detector=" << detector <<
3268           "caligroup=" << caligroup <<
3269           "rowmin=" << rowmin <<
3270           "rowmax=" << rowmax <<
3271           "colmin=" << colmin <<
3272           "colmax=" << colmax <<
3273           "gf=" << gf <<
3274           "gfs=" << gfs <<
3275           "gfE=" << gfE <<
3276           "\n"; 
3277         
3278       }
3279       // Reset
3280       for (Int_t k = 0; k < 2304; k++) {
3281         fCurrentCoefDetector[k] = 0.0;
3282       }
3283       
3284     }// loop detector
3285     AliDebug(2,Form("Check the count now: fCountDet %d",fCountDet));
3286   }
3287   else {
3288
3289 //AliInfo(Form("The element %d in this detector %d has not enough statistic to be fitted",idect-(fCount-(fCalibraMode->GetNfragZ(0)*fCalibraMode->GetNfragRphi(0))),fCountDet));
3290     
3291     // Calcul the coef from the database choosen
3292     CalculChargeCoefMean(kFALSE);
3293
3294     //stack 2, not stack 2
3295     Int_t factor = 0;
3296     if(GetStack(fCountDet) == 2) factor = 12;
3297     else factor = 16;
3298     
3299     // Fill the fCurrentCoefDetector with negative value to say: not fitted
3300     for (Int_t k = fCalibraMode->GetRowMin(0); k < fCalibraMode->GetRowMax(0); k++) {
3301       for (Int_t j = fCalibraMode->GetColMin(0); j < fCalibraMode->GetColMax(0); j++) {
3302         fCurrentCoefDetector[(Int_t)(j*factor+k)] = -TMath::Abs(fCurrentCoef[1]);
3303       }
3304     }
3305     
3306     //Put default value negative
3307     fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
3308     fCurrentCoefE   = 0.0;
3309    
3310     FillFillCH(idect);
3311   }
3312   
3313   return kTRUE;
3314 }
3315
3316
3317 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3318 Bool_t AliTRDCalibraFit::NotEnoughStatisticPH(Int_t idect,Double_t nentries)
3319 {
3320   //
3321   // For the case where there are not enough entries in the histograms
3322   // of the calibration group, the value present in the choosen database
3323   // will be put. A negativ sign enables to know that a fit was not possible.
3324   //
3325   if (fDebugLevel == 1) {
3326     AliInfo("The element has not enough statistic to be fitted");
3327   }
3328   else if (fNbDet > 0) {
3329
3330     Int_t firstdetector = fCountDet;
3331     Int_t lastdetector  = fCountDet+fNbDet;
3332 //AliInfo(Form("The element %d containing the detectors %d to %d has not enough statistic to be fitted",idect,firstdetector,lastdetector));
3333     // loop over detectors
3334     for(Int_t det = firstdetector; det < lastdetector; det++){
3335
3336       //Set the calibration object again
3337       fCountDet = det;
3338       SetCalROC(1);   
3339
3340       // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3341       // Put them at 1
3342       fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),1);
3343       fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3344                                          ,(Int_t) GetStack(fCountDet)
3345                                          ,(Int_t) GetSector(fCountDet),1);
3346       // Reconstruct row min row max
3347       ReconstructFitRowMinRowMax(idect,1);      
3348
3349       // Calcul the coef from the database choosen for the detector
3350       CalculVdriftCoefMean();
3351       CalculT0CoefMean();
3352       
3353       //stack 2, not stack 2
3354       Int_t factor = 0;
3355       if(GetStack(fCountDet) == 2) factor = 12;
3356       else factor = 16;
3357       
3358       // Fill the fCurrentCoefDetector with negative value to say: not fitted
3359       for (Int_t k = fCalibraMode->GetRowMin(1); k < fCalibraMode->GetRowMax(1); k++) {
3360         for (Int_t j = fCalibraMode->GetColMin(1); j < fCalibraMode->GetColMax(1); j++) {
3361           fCurrentCoefDetector[(Int_t)(j*factor+k)] = -TMath::Abs(fCurrentCoef[1]);
3362           fCurrentCoefDetector2[(Int_t)(j*factor+k)] = fCurrentCoef2[1] + 100.0;
3363         }
3364       }
3365       
3366       //Put default value negative
3367       fCurrentCoef[0]  = -TMath::Abs(fCurrentCoef[1]);
3368       fCurrentCoefE    = 0.0;
3369       fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
3370       fCurrentCoefE2   = 0.0;
3371             
3372       // Fill the stuff
3373       FillVectorFit();
3374       FillVectorFit2();
3375       // Debug
3376       if(fDebugLevel > 1){ 
3377
3378         if ( !fDebugStreamer ) {
3379           //debug stream
3380           TDirectory *backup = gDirectory;
3381           fDebugStreamer = new TTreeSRedirector("TRDDebugFitPH.root");
3382           if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
3383         } 
3384         
3385         
3386         Int_t   detector     = fCountDet;
3387         Int_t   caligroup    = idect;
3388         Short_t rowmin       = fCalibraMode->GetRowMin(1);
3389         Short_t rowmax       = fCalibraMode->GetRowMax(1);
3390         Short_t colmin       = fCalibraMode->GetColMin(1);
3391         Short_t colmax       = fCalibraMode->GetColMax(1);
3392         Float_t vf           = fCurrentCoef[0]; 
3393         Float_t vs           = fCurrentCoef[1]; 
3394         Float_t vfE          = fCurrentCoefE;
3395         Float_t t0f          = fCurrentCoef2[0]; 
3396         Float_t t0s          = fCurrentCoef2[1]; 
3397         Float_t t0E          = fCurrentCoefE2;
3398         
3399         
3400         
3401         (* fDebugStreamer) << "FillFillPH"<<
3402         "detector="<<detector<<
3403           "nentries="<<nentries<<
3404           "caligroup="<<caligroup<<
3405           "rowmin="<<rowmin<<
3406           "rowmax="<<rowmax<<
3407           "colmin="<<colmin<<
3408           "colmax="<<colmax<<
3409           "vf="<<vf<<
3410           "vs="<<vs<<
3411           "vfE="<<vfE<<
3412           "t0f="<<t0f<<
3413           "t0s="<<t0s<<
3414           "t0E="<<t0E<<
3415           "\n";  
3416       }
3417       // Reset
3418       for (Int_t k = 0; k < 2304; k++) {
3419         fCurrentCoefDetector[k] = 0.0;
3420         fCurrentCoefDetector2[k] = 0.0;
3421       }
3422       
3423     }// loop detector
3424     AliDebug(2,Form("Check the count now: fCountDet %d",fCountDet));
3425   }    
3426   else {
3427
3428 //AliInfo(Form("The element %d in this detector %d has not enough statistic to be fitted",idect-(fCount-(fCalibraMode->GetNfragZ(1)*fCalibraMode->GetNfragRphi(1))),fCountDet));
3429
3430     CalculVdriftCoefMean();
3431     CalculT0CoefMean();
3432   
3433     //stack 2 and not stack 2
3434     Int_t factor = 0;
3435     if(GetStack(fCountDet) == 2) factor = 12;
3436     else factor = 16;
3437
3438
3439     // Fill the fCurrentCoefDetector 2
3440     for (Int_t k = fCalibraMode->GetRowMin(1); k < fCalibraMode->GetRowMax(1); k++) {
3441       for (Int_t j = fCalibraMode->GetColMin(1); j < fCalibraMode->GetColMax(1); j++) {
3442         fCurrentCoefDetector[(Int_t)(j*factor+k)] = -TMath::Abs(fCurrentCoef[1]);
3443         fCurrentCoefDetector2[(Int_t)(j*factor+k)] = fCurrentCoef2[1] + 100.0;
3444       }
3445     }
3446
3447     // Put the default value
3448     fCurrentCoef[0]  = -TMath::Abs(fCurrentCoef[1]);
3449     fCurrentCoefE    = 0.0;
3450     fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
3451     fCurrentCoefE2   = 0.0;
3452      
3453     FillFillPH(idect,nentries);
3454     
3455   }
3456   
3457   return kTRUE;
3458   
3459 }
3460
3461
3462 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3463 Bool_t AliTRDCalibraFit::NotEnoughStatisticPRF(Int_t idect)
3464 {
3465   //
3466   // For the case where there are not enough entries in the histograms
3467   // of the calibration group, the value present in the choosen database
3468   // will be put. A negativ sign enables to know that a fit was not possible.
3469   //
3470   
3471   if (fDebugLevel == 1) {
3472     AliInfo("The element has not enough statistic to be fitted");
3473   }
3474   else if (fNbDet > 0){
3475   
3476     Int_t firstdetector = fCountDet;
3477     Int_t lastdetector  = fCountDet+fNbDet;
3478 //  AliInfo(Form("The element %d containing the detectors %d to %d has not enough statistic to be fitted",idect,firstdetector,lastdetector));
3479     
3480     // loop over detectors
3481     for(Int_t det = firstdetector; det < lastdetector; det++){
3482
3483       //Set the calibration object again
3484       fCountDet = det;
3485       SetCalROC(2);   
3486
3487       // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3488       // Put them at 1
3489       fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),2);
3490       fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3491                                          ,(Int_t) GetStack(fCountDet)
3492                                          ,(Int_t) GetSector(fCountDet),2);
3493       // Reconstruct row min row max
3494       ReconstructFitRowMinRowMax(idect,2);      
3495
3496       // Calcul the coef from the database choosen for the detector
3497       CalculPRFCoefMean();
3498       
3499       //stack 2, not stack 2
3500       Int_t factor = 0;
3501       if(GetStack(fCountDet) == 2) factor = 12;
3502       else factor = 16;
3503       
3504       // Fill the fCurrentCoefDetector with negative value to say: not fitted
3505       for (Int_t k = fCalibraMode->GetRowMin(2); k < fCalibraMode->GetRowMax(2); k++) {
3506         for (Int_t j = fCalibraMode->GetColMin(2); j < fCalibraMode->GetColMax(2); j++) {
3507           fCurrentCoefDetector[(Int_t)(j*factor+k)] = -TMath::Abs(fCurrentCoef[1]);
3508         }
3509       }
3510       
3511       //Put default value negative
3512       fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
3513       fCurrentCoefE   = 0.0;
3514       
3515       // Fill the stuff
3516       FillVectorFit();
3517       // Debug
3518       if(fDebugLevel > 1){
3519         
3520         if ( !fDebugStreamer ) {
3521           //debug stream
3522           TDirectory *backup = gDirectory;
3523           fDebugStreamer = new TTreeSRedirector("TRDDebugFitPRF.root");
3524           if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
3525         } 
3526         
3527         Int_t   detector     = fCountDet;
3528         Int_t   layer        = GetLayer(fCountDet);
3529         Int_t   caligroup    = idect;
3530         Short_t rowmin       = fCalibraMode->GetRowMin(2);
3531         Short_t rowmax       = fCalibraMode->GetRowMax(2);
3532         Short_t colmin       = fCalibraMode->GetColMin(2);
3533         Short_t colmax       = fCalibraMode->GetColMax(2);
3534         Float_t widf         = fCurrentCoef[0]; 
3535         Float_t wids         = fCurrentCoef[1]; 
3536         Float_t widfE        = fCurrentCoefE;
3537         
3538         (* fDebugStreamer) << "FillFillPRF"<<
3539           "detector="<<detector<<
3540           "layer="<<layer<<
3541           "caligroup="<<caligroup<<
3542           "rowmin="<<rowmin<<
3543           "rowmax="<<rowmax<<
3544           "colmin="<<colmin<<
3545           "colmax="<<colmax<<
3546           "widf="<<widf<<
3547           "wids="<<wids<<
3548           "widfE="<<widfE<<
3549           "\n";  
3550       }
3551       // Reset
3552       for (Int_t k = 0; k < 2304; k++) {
3553         fCurrentCoefDetector[k] = 0.0;
3554       }
3555       
3556     }// loop detector
3557     AliDebug(2,Form("Check the count now: fCountDet %d",fCountDet));
3558   }
3559   else {
3560     
3561 //  AliInfo(Form("The element %d in this detector %d has not enough statistic to be fitted",idect-(fCount-(fCalibraMode->GetNfragZ(2)*fCalibraMode->GetNfragRphi(2))),fCountDet));
3562     
3563     CalculPRFCoefMean();
3564     
3565     // stack 2 and not stack 2
3566     Int_t factor = 0;
3567     if(GetStack(fCountDet) == 2) factor = 12;
3568     else factor = 16;
3569
3570     
3571     // Fill the fCurrentCoefDetector
3572     for (Int_t k = fCalibraMode->GetRowMin(2); k < fCalibraMode->GetRowMax(2); k++) {
3573       for (Int_t j = fCalibraMode->GetColMin(2); j < fCalibraMode->GetColMax(2); j++) {
3574         fCurrentCoefDetector[(Int_t)(j*factor+k)] = -TMath::Abs(fCurrentCoef[1]);
3575       }
3576     }
3577
3578     // Put the default value
3579     fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
3580     fCurrentCoefE   = 0.0;
3581     
3582     FillFillPRF(idect);
3583   }
3584   
3585   return kTRUE;
3586
3587 }
3588 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3589 Bool_t AliTRDCalibraFit::NotEnoughStatisticLinearFitter()
3590 {
3591   //
3592   // For the case where there are not enough entries in the histograms
3593   // of the calibration group, the value present in the choosen database
3594   // will be put. A negativ sign enables to know that a fit was not possible.
3595   //
3596   
3597   // Calcul the coef from the database choosen
3598   CalculVdriftLorentzCoef();
3599
3600   Int_t factor = 0;
3601   if(GetStack(fCountDet) == 2) factor = 1728;
3602   else factor = 2304;
3603     
3604     
3605   // Fill the fCurrentCoefDetector
3606   for (Int_t k = 0; k < factor; k++) {
3607     fCurrentCoefDetector[k] = -TMath::Abs(fCurrentCoef[1]);
3608     // should be negative
3609     fCurrentCoefDetector2[k] = fCurrentCoef2[1]+100.0;
3610   }
3611    
3612   
3613   //Put default opposite sign only for vdrift
3614   fCurrentCoef[0]  = -TMath::Abs(fCurrentCoef[1]);
3615   fCurrentCoefE    = 0.0;
3616   fCurrentCoef2[0] = fCurrentCoef2[1]+100.0;
3617   fCurrentCoefE2 = 0.0; 
3618   
3619   FillFillLinearFitter();
3620     
3621   return kTRUE;
3622 }
3623
3624 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3625 Bool_t AliTRDCalibraFit::NotEnoughStatisticExbAlt()
3626 {
3627   //
3628   // For the case where there are not enough entries in the histograms
3629   // of the calibration group, the value present in the choosen database
3630   // will be put. A negativ sign enables to know that a fit was not possible.
3631   //
3632   
3633   Int_t factor = 0;
3634   if(GetStack(fCountDet) == 2) factor = 1728;
3635   else factor = 2304;
3636     
3637     
3638   // Fill the fCurrentCoefDetector
3639   for (Int_t k = 0; k < factor; k++) {
3640     fCurrentCoefDetector2[k] = 100.0;
3641   }
3642    
3643   fCurrentCoef2[0] = 100.0;
3644   fCurrentCoefE2 = 0.0; 
3645   
3646   FillFillExbAlt();
3647     
3648   return kTRUE;
3649 }
3650
3651 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3652 Bool_t AliTRDCalibraFit::FillInfosFitCH(Int_t idect)
3653 {
3654   //
3655   // Fill the coefficients found with the fits or other
3656   // methods from the Fit functions
3657   //
3658
3659   if (fDebugLevel != 1) {
3660     if (fNbDet > 0){
3661       Int_t firstdetector = fCountDet;
3662       Int_t lastdetector  = fCountDet+fNbDet;
3663       //    AliInfo(Form("The element %d containing the detectors %d to %d has been fitted",idect,firstdetector,lastdetector));
3664       // loop over detectors
3665       for(Int_t det = firstdetector; det < lastdetector; det++){
3666         
3667         //Set the calibration object again
3668         fCountDet = det;
3669         SetCalROC(0);   
3670         
3671         // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3672         // Put them at 1
3673         fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),0);
3674         fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3675                                            ,(Int_t) GetStack(fCountDet)
3676                                            ,(Int_t) GetSector(fCountDet),0);
3677         // Reconstruct row min row max
3678         ReconstructFitRowMinRowMax(idect,0);      
3679         
3680         // Calcul the coef from the database choosen for the detector
3681         if(fCurrentCoef[0] < 0.0) CalculChargeCoefMean(kFALSE);
3682         else CalculChargeCoefMean(kTRUE);
3683         
3684         //stack 2, not stack 2
3685         Int_t factor = 0;
3686         if(GetStack(fCountDet) == 2) factor = 12;
3687         else factor = 16;
3688         
3689         // Fill the fCurrentCoefDetector with negative value to say: not fitted
3690         Double_t coeftoput = 1.0;
3691         if(fCurrentCoef[0] < 0.0) coeftoput = - TMath::Abs(fCurrentCoef[1]);
3692         else coeftoput = fCurrentCoef[0];
3693         for (Int_t k = fCalibraMode->GetRowMin(0); k < fCalibraMode->GetRowMax(0); k++) {
3694           for (Int_t j = fCalibraMode->GetColMin(0); j < fCalibraMode->GetColMax(0); j++) {
3695             fCurrentCoefDetector[(Int_t)(j*factor+k)] = coeftoput;
3696           }
3697         }
3698         
3699         // Fill the stuff
3700         FillVectorFit();
3701         // Debug
3702         if(fDebugLevel > 1){ 
3703           
3704           if ( !fDebugStreamer ) {
3705             //debug stream
3706             TDirectory *backup = gDirectory;
3707             fDebugStreamer = new TTreeSRedirector("TRDDebugFitCH.root");
3708             if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
3709           } 
3710           
3711           Int_t   detector   = fCountDet;
3712           Int_t   caligroup  = idect;
3713           Short_t rowmin     = fCalibraMode->GetRowMin(0);
3714           Short_t rowmax     = fCalibraMode->GetRowMax(0);
3715           Short_t colmin     = fCalibraMode->GetColMin(0);
3716           Short_t colmax     = fCalibraMode->GetColMax(0);
3717           Float_t gf         = fCurrentCoef[0]; 
3718           Float_t gfs        = fCurrentCoef[1]; 
3719           Float_t gfE        = fCurrentCoefE;
3720           
3721           (*fDebugStreamer) << "FillFillCH" <<
3722             "detector=" << detector <<
3723             "caligroup=" << caligroup <<
3724             "rowmin=" << rowmin <<
3725             "rowmax=" << rowmax <<
3726             "colmin=" << colmin <<
3727             "colmax=" << colmax <<
3728             "gf=" << gf <<
3729             "gfs=" << gfs <<
3730             "gfE=" << gfE <<
3731             "\n"; 
3732           
3733         }
3734         // Reset
3735         for (Int_t k = 0; k < 2304; k++) {
3736           fCurrentCoefDetector[k] = 0.0;
3737         }
3738         
3739       }// loop detector
3740       //printf("Check the count now: fCountDet %d\n",fCountDet);
3741     }
3742     else{
3743       
3744       Int_t factor = 0;
3745       if(GetStack(fCountDet) == 2) factor = 12;
3746       else factor = 16; 
3747       
3748       for (Int_t k = fCalibraMode->GetRowMin(0); k < fCalibraMode->GetRowMax(0); k++) {
3749         for (Int_t j = fCalibraMode->GetColMin(0); j < fCalibraMode->GetColMax(0); j++) {
3750           fCurrentCoefDetector[(Int_t)(j*factor+k)] = fCurrentCoef[0];
3751         }
3752       }
3753       
3754       FillFillCH(idect);
3755     }
3756   }
3757
3758   return kTRUE;
3759
3760 }
3761 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3762 Bool_t AliTRDCalibraFit::FillInfosFitPH(Int_t idect,Double_t nentries)
3763 {
3764   //
3765   // Fill the coefficients found with the fits or other
3766   // methods from the Fit functions
3767   //
3768
3769   if (fDebugLevel != 1) {
3770     if (fNbDet > 0){
3771       
3772       Int_t firstdetector = fCountDet;
3773       Int_t lastdetector  = fCountDet+fNbDet;
3774 // AliInfo(Form("The element %d containing the detectors %d to %d has been fitted",idect,firstdetector,lastdetector));
3775       
3776       // loop over detectors
3777       for(Int_t det = firstdetector; det < lastdetector; det++){
3778         
3779         //Set the calibration object again
3780         fCountDet = det;
3781         SetCalROC(1);   
3782         
3783         // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3784         // Put them at 1
3785         fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),1);
3786         fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3787                                            ,(Int_t) GetStack(fCountDet)
3788                                            ,(Int_t) GetSector(fCountDet),1);
3789         // Reconstruct row min row max
3790         ReconstructFitRowMinRowMax(idect,1);      
3791         
3792         // Calcul the coef from the database choosen for the detector
3793         CalculVdriftCoefMean();
3794         CalculT0CoefMean();
3795                 
3796         //stack 2, not stack 2
3797         Int_t factor = 0;
3798         if(GetStack(fCountDet) == 2) factor = 12;
3799         else factor = 16;
3800         
3801         // Fill the fCurrentCoefDetector with negative value to say: not fitted
3802         Double_t coeftoput  = 1.5;
3803         Double_t coeftoput2 = 0.0; 
3804
3805         if(fCurrentCoef[0] < 0.0) coeftoput = - TMath::Abs(fCurrentCoef[1]);
3806         else coeftoput = fCurrentCoef[0];
3807
3808         if(fCurrentCoef2[0] > 70.0) coeftoput2 = fCurrentCoef2[1] + 100.0;
3809         else coeftoput2 = fCurrentCoef2[0];
3810
3811         for (Int_t k = fCalibraMode->GetRowMin(1); k < fCalibraMode->GetRowMax(1); k++) {
3812           for (Int_t j = fCalibraMode->GetColMin(1); j < fCalibraMode->GetColMax(1); j++) {
3813             fCurrentCoefDetector[(Int_t)(j*factor+k)]  = coeftoput;
3814             fCurrentCoefDetector2[(Int_t)(j*factor+k)] = coeftoput2;
3815           }
3816         }
3817         
3818         // Fill the stuff
3819         FillVectorFit();
3820         FillVectorFit2();
3821         // Debug
3822         if(fDebugLevel > 1){ 
3823           
3824           if ( !fDebugStreamer ) {
3825             //debug stream
3826             TDirectory *backup = gDirectory;
3827             fDebugStreamer = new TTreeSRedirector("TRDDebugFitPH.root");
3828             if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
3829           } 
3830           
3831           
3832           Int_t   detector     = fCountDet;
3833           Int_t   caligroup    = idect;
3834           Short_t rowmin       = fCalibraMode->GetRowMin(1);
3835           Short_t rowmax       = fCalibraMode->GetRowMax(1);
3836           Short_t colmin       = fCalibraMode->GetColMin(1);
3837           Short_t colmax       = fCalibraMode->GetColMax(1);
3838           Float_t vf           = fCurrentCoef[0]; 
3839           Float_t vs           = fCurrentCoef[1]; 
3840           Float_t vfE          = fCurrentCoefE;
3841           Float_t t0f          = fCurrentCoef2[0]; 
3842           Float_t t0s          = fCurrentCoef2[1]; 
3843           Float_t t0E          = fCurrentCoefE2;
3844           
3845           
3846           
3847           (* fDebugStreamer) << "FillFillPH"<<
3848             "detector="<<detector<<
3849             "nentries="<<nentries<<
3850             "caligroup="<<caligroup<<
3851             "rowmin="<<rowmin<<
3852             "rowmax="<<rowmax<<
3853             "colmin="<<colmin<<
3854             "colmax="<<colmax<<
3855             "vf="<<vf<<
3856             "vs="<<vs<<
3857             "vfE="<<vfE<<
3858             "t0f="<<t0f<<
3859             "t0s="<<t0s<<
3860             "t0E="<<t0E<<
3861             "\n";  
3862         }
3863         // Reset
3864         for (Int_t k = 0; k < 2304; k++) {
3865           fCurrentCoefDetector[k] = 0.0;
3866           fCurrentCoefDetector2[k] = 0.0;
3867         }
3868         
3869       }// loop detector
3870       //printf("Check the count now: fCountDet %d\n",fCountDet);
3871     }
3872     else {
3873       
3874       Int_t factor = 0;
3875       if(GetStack(fCountDet) == 2) factor = 12;
3876       else factor = 16; 
3877       
3878       for (Int_t k = fCalibraMode->GetRowMin(1); k < fCalibraMode->GetRowMax(1); k++) {
3879         for (Int_t j = fCalibraMode->GetColMin(1); j < fCalibraMode->GetColMax(1); j++) {
3880           fCurrentCoefDetector[(Int_t)(j*factor+k)]  = fCurrentCoef[0];
3881           fCurrentCoefDetector2[(Int_t)(j*factor+k)] = fCurrentCoef2[0];
3882         }
3883       }  
3884       
3885       FillFillPH(idect,nentries);
3886     }
3887   }
3888   return kTRUE;
3889 }
3890 //____________Functions for initialising the AliTRDCalibraFit in the code_________
3891 Bool_t AliTRDCalibraFit::FillInfosFitPRF(Int_t idect)
3892 {
3893   //
3894   // Fill the coefficients found with the fits or other
3895   // methods from the Fit functions
3896   //
3897   
3898   if (fDebugLevel != 1) {
3899     if (fNbDet > 0){
3900     
3901       Int_t firstdetector = fCountDet;
3902       Int_t lastdetector  = fCountDet+fNbDet;
3903 //    AliInfo(Form("The element %d containing the detectors %d to %d has been fitted",idect,firstdetector,lastdetector));
3904       
3905       // loop over detectors
3906       for(Int_t det = firstdetector; det < lastdetector; det++){
3907         
3908         //Set the calibration object again
3909         fCountDet = det;
3910         SetCalROC(2);   
3911         
3912         // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
3913         // Put them at 1
3914         fCalibraMode->ModePadCalibration((Int_t) GetStack(fCountDet),2);
3915         fCalibraMode->ModePadFragmentation((Int_t) GetLayer(fCountDet)
3916                                            ,(Int_t) GetStack(fCountDet)
3917                                            ,(Int_t) GetSector(fCountDet),2);
3918         // Reconstruct row min row max
3919         ReconstructFitRowMinRowMax(idect,2);      
3920         
3921         // Calcul the coef from the database choosen for the detector
3922         CalculPRFCoefMean();
3923         
3924         //stack 2, not stack 2
3925         Int_t factor = 0;
3926         if(GetStack(fCountDet) == 2) factor = 12;
3927         else factor = 16;
3928         
3929         // Fill the fCurrentCoefDetector with negative value to say: not fitted
3930         Double_t coeftoput = 1.0;
3931         if(fCurrentCoef[0] < 0.0) coeftoput = - TMath::Abs(fCurrentCoef[1]);
3932         else coeftoput = fCurrentCoef[0];
3933         for (Int_t k = fCalibraMode->GetRowMin(2); k < fCalibraMode->GetRowMax(2); k++) {
3934           for (Int_t j = fCalibraMode->GetColMin(2); j < fCalibraMode->GetColMax(2); j++) {
3935             fCurrentCoefDetector[(Int_t)(j*factor+k)] = coeftoput;
3936           }
3937         }
3938         
3939         // Fill the stuff
3940         FillVectorFit();
3941         // Debug
3942         if(fDebugLevel > 1){
3943           
3944           if ( !fDebugStreamer ) {
3945             //debug stream
3946             TDirectory *backup = gDirectory;
3947             fDebugStreamer = new TTreeSRedirector("TRDDebugFitPRF.root");
3948             if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
3949           } 
3950           
3951           Int_t   detector     = fCountDet;
3952           Int_t   layer        = GetLayer(fCountDet);
3953           Int_t   caligroup    = idect;
3954           Short_t rowmin       = fCalibraMode->GetRowMin(2);
3955           Short_t rowmax       = fCalibraMode->GetRowMax(2);
3956           Short_t colmin       = fCalibraMode->GetColMin(2);
3957           Short_t colmax       = fCalibraMode->GetColMax(2);
3958           Float_t widf         = fCurrentCoef[0]; 
3959           Float_t wids         = fCurrentCoef[1]; 
3960           Float_t widfE        = fCurrentCoefE;
3961           
3962           (* fDebugStreamer) << "FillFillPRF"<<
3963             "detector="<<detector<<
3964             "layer="<<layer<<
3965             "caligroup="<<caligroup<<
3966             "rowmin="<<rowmin<<
3967             "rowmax="<<rowmax<<
3968             "colmin="<<colmin<<
3969             "colmax="<<colmax<<
3970             "widf="<<widf<<
3971             "wids="<<wids<<
3972             "widfE="<<widfE<<
3973             "\n";  
3974         }
3975         // Reset
3976         for (Int_t k = 0; k < 2304; k++) {
3977           fCurrentCoefDetector[k] = 0.0;
3978         }
3979         
3980       }// loop detector
3981       //printf("Check the count now: fCountDet %d\n",fCountDet);
3982     }
3983     else {
3984       
3985       Int_t factor = 0;
3986       if(GetStack(fCountDet) == 2) factor = 12;
3987       else factor = 16; 
3988       
3989       // Pointer to the branch
3990       for (Int_t k = fCalibraMode->GetRowMin(2); k < fCalibraMode->GetRowMax(2); k++) {
3991         for (Int_t j = fCalibraMode->GetColMin(2); j < fCalibraMode->GetColMax(2); j++) {
3992           fCurrentCoefDetector[(Int_t)(j*factor+k)] = fCurrentCoef[0];
3993         }
3994       }
3995       FillFillPRF(idect);   
3996     }
3997   }
3998   
3999   return kTRUE;
4000
4001 }
4002 //____________Functions for initialising the AliTRDCalibraFit in the code_________
4003 Bool_t AliTRDCalibraFit::FillInfosFitLinearFitter()
4004 {
4005   //
4006   // Fill the coefficients found with the fits or other
4007   // methods from the Fit functions
4008   //
4009   
4010   Int_t factor = 0;
4011   if(GetStack(fCountDet) == 2) factor = 1728;
4012   else factor = 2304; 
4013   
4014   // Pointer to the branch
4015   for (Int_t k = 0; k < factor; k++) {
4016     fCurrentCoefDetector[k]  = fCurrentCoef[0];
4017     fCurrentCoefDetector2[k] = fCurrentCoef2[0];
4018   }
4019   
4020   FillFillLinearFitter();
4021   
4022   return kTRUE;
4023
4024 }
4025 //____________Functions for initialising the AliTRDCalibraFit in the code_________
4026 Bool_t AliTRDCalibraFit::FillInfosFitExbAlt()
4027 {
4028   //
4029   // Fill the coefficients found with the fits or other
4030   // methods from the Fit functions
4031   //
4032   
4033   Int_t factor = 0;
4034   if(GetStack(fCountDet) == 2) factor = 1728;
4035   else factor = 2304; 
4036   
4037   // Pointer to the branch
4038   for (Int_t k = 0; k < factor; k++) {
4039     fCurrentCoefDetector2[k]  = fCurrentCoef2[0];
4040   }
4041   
4042   FillFillExbAlt();
4043   
4044   return kTRUE;
4045
4046 }
4047 //________________________________________________________________________________
4048 void AliTRDCalibraFit::FillFillCH(Int_t idect)
4049 {
4050   //
4051   // DebugStream and fVectorFit
4052   //
4053
4054   // End of one detector
4055   if (idect == (fCount-1)) {
4056     FillVectorFit();
4057     // Reset
4058     for (Int_t k = 0; k < 2304; k++) {
4059       fCurrentCoefDetector[k] = 0.0;
4060     }
4061   }
4062
4063   if(fDebugLevel > 1){ 
4064
4065     if ( !fDebugStreamer ) {
4066       //debug stream
4067       TDirectory *backup = gDirectory;
4068       fDebugStreamer = new TTreeSRedirector("TRDDebugFitCH.root");
4069       if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
4070     } 
4071     
4072     Int_t   detector   = fCountDet;
4073     Int_t   caligroup  = idect;
4074     Short_t rowmin     = fCalibraMode->GetRowMin(0);
4075     Short_t rowmax     = fCalibraMode->GetRowMax(0);
4076     Short_t colmin     = fCalibraMode->GetColMin(0);
4077     Short_t colmax     = fCalibraMode->GetColMax(0);
4078     Float_t gf         = fCurrentCoef[0]; 
4079     Float_t gfs        = fCurrentCoef[1]; 
4080     Float_t gfE        = fCurrentCoefE;
4081     
4082     (*fDebugStreamer) << "FillFillCH" <<
4083       "detector=" << detector <<
4084       "caligroup=" << caligroup <<
4085       "rowmin=" << rowmin <<
4086       "rowmax=" << rowmax <<
4087       "colmin=" << colmin <<
4088       "colmax=" << colmax <<
4089       "gf=" << gf <<
4090       "gfs=" << gfs <<
4091       "gfE=" << gfE <<
4092       "\n"; 
4093     
4094   }
4095 }
4096 //________________________________________________________________________________
4097 void AliTRDCalibraFit::FillFillPH(Int_t idect,Double_t nentries)
4098 {
4099   //
4100   // DebugStream and fVectorFit and fVectorFit2
4101   //
4102   
4103   // End of one detector
4104     if (idect == (fCount-1)) {
4105       FillVectorFit();
4106       FillVectorFit2();
4107       // Reset
4108       for (Int_t k = 0; k < 2304; k++) {
4109         fCurrentCoefDetector[k] = 0.0;
4110         fCurrentCoefDetector2[k] = 0.0;
4111       }
4112     }
4113
4114     if(fDebugLevel > 1){ 
4115
4116       if ( !fDebugStreamer ) {
4117         //debug stream
4118         TDirectory *backup = gDirectory;
4119         fDebugStreamer = new TTreeSRedirector("TRDDebugFitPH.root");
4120         if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
4121       } 
4122       
4123        
4124       Int_t   detector     = fCountDet;
4125       Int_t   caligroup    = idect;
4126       Short_t rowmin       = fCalibraMode->GetRowMin(1);
4127       Short_t rowmax       = fCalibraMode->GetRowMax(1);
4128       Short_t colmin       = fCalibraMode->GetColMin(1);
4129       Short_t colmax       = fCalibraMode->GetColMax(1);
4130       Float_t vf           = fCurrentCoef[0]; 
4131       Float_t vs           = fCurrentCoef[1]; 
4132       Float_t vfE          = fCurrentCoefE;
4133       Float_t t0f          = fCurrentCoef2[0]; 
4134       Float_t t0s          = fCurrentCoef2[1]; 
4135       Float_t t0E          = fCurrentCoefE2;
4136    
4137
4138
4139       (* fDebugStreamer) << "FillFillPH"<<
4140         "detector="<<detector<<
4141         "nentries="<<nentries<<
4142         "caligroup="<<caligroup<<
4143         "rowmin="<<rowmin<<
4144         "rowmax="<<rowmax<<
4145         "colmin="<<colmin<<
4146         "colmax="<<colmax<<
4147         "vf="<<vf<<
4148         "vs="<<vs<<
4149         "vfE="<<vfE<<
4150         "t0f="<<t0f<<
4151         "t0s="<<t0s<<
4152         "t0E="<<t0E<<
4153         "\n";  
4154     }
4155
4156 }
4157 //________________________________________________________________________________
4158 void AliTRDCalibraFit::FillFillPRF(Int_t idect)
4159 {
4160   //
4161   // DebugStream and fVectorFit
4162   //
4163
4164     // End of one detector
4165     if (idect == (fCount-1)) {
4166       FillVectorFit();
4167       // Reset
4168       for (Int_t k = 0; k < 2304; k++) {
4169         fCurrentCoefDetector[k] = 0.0;
4170       }
4171     }
4172
4173     
4174     if(fDebugLevel > 1){
4175
4176       if ( !fDebugStreamer ) {
4177         //debug stream
4178         TDirectory *backup = gDirectory;
4179         fDebugStreamer = new TTreeSRedirector("TRDDebugFitPRF.root");
4180         if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
4181       } 
4182       
4183       Int_t   detector     = fCountDet;
4184       Int_t   layer        = GetLayer(fCountDet);
4185       Int_t   caligroup    = idect;
4186       Short_t rowmin       = fCalibraMode->GetRowMin(2);
4187       Short_t rowmax       = fCalibraMode->GetRowMax(2);
4188       Short_t colmin       = fCalibraMode->GetColMin(2);
4189       Short_t colmax       = fCalibraMode->GetColMax(2);
4190       Float_t widf         = fCurrentCoef[0]; 
4191       Float_t wids         = fCurrentCoef[1]; 
4192       Float_t widfE        = fCurrentCoefE;
4193
4194       (* fDebugStreamer) << "FillFillPRF"<<
4195         "detector="<<detector<<
4196         "layer="<<layer<<
4197         "caligroup="<<caligroup<<
4198         "rowmin="<<rowmin<<
4199         "rowmax="<<rowmax<<
4200         "colmin="<<colmin<<
4201         "colmax="<<colmax<<
4202         "widf="<<widf<<
4203         "wids="<<wids<<
4204         "widfE="<<widfE<<
4205         "\n";  
4206     }
4207
4208 }
4209 //________________________________________________________________________________
4210 void AliTRDCalibraFit::FillFillLinearFitter()
4211 {
4212   //
4213   // DebugStream and fVectorFit
4214   //
4215
4216   // End of one detector
4217   FillVectorFit();
4218   FillVectorFit2();
4219   
4220   
4221   // Reset
4222   for (Int_t k = 0; k < 2304; k++) {
4223   fCurrentCoefDetector[k]  = 0.0;
4224   fCurrentCoefDetector2[k] = 0.0;
4225   }
4226   
4227
4228   if(fDebugLevel > 1){
4229
4230     if ( !fDebugStreamer ) {
4231       //debug stream
4232       TDirectory *backup = gDirectory;
4233       fDebugStreamer = new TTreeSRedirector("TRDDebugFitLinearFitter.root");
4234       if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
4235     } 
4236     
4237     //Debug: comparaison of the different methods (okey for first time but not for iterative procedure)
4238     AliTRDpadPlane *padplane = fGeo->GetPadPlane(GetLayer(fCountDet),GetStack(fCountDet));
4239     Float_t rowmd            = (padplane->GetRow0()+padplane->GetRowEnd())/2.;
4240     Float_t r                = AliTRDgeometry::GetTime0(GetLayer(fCountDet)); 
4241     Float_t tiltangle        = padplane->GetTiltingAngle();
4242     Int_t   detector         = fCountDet;
4243     Int_t   stack            = GetStack(fCountDet);
4244     Int_t   layer            = GetLayer(fCountDet);
4245     Float_t vf               = fCurrentCoef[0]; 
4246     Float_t vs               = fCurrentCoef[1]; 
4247     Float_t vfE              = fCurrentCoefE;
4248     Float_t lorentzangler    = fCurrentCoef2[0];
4249     Float_t elorentzangler   = fCurrentCoefE2;
4250     Float_t lorentzangles    = fCurrentCoef2[1];
4251    
4252     (* fDebugStreamer) << "FillFillLinearFitter"<<
4253       "detector="<<detector<<
4254       "stack="<<stack<<
4255       "layer="<<layer<<
4256       "rowmd="<<rowmd<<
4257       "r="<<r<<
4258       "tiltangle="<<tiltangle<<
4259       "vf="<<vf<<
4260       "vs="<<vs<<
4261       "vfE="<<vfE<<
4262       "lorentzangler="<<lorentzangler<<
4263       "Elorentzangler="<<elorentzangler<<
4264       "lorentzangles="<<lorentzangles<<
4265       "\n";  
4266   }
4267   
4268 }
4269 //________________________________________________________________________________
4270 void AliTRDCalibraFit::FillFillExbAlt()
4271 {
4272   //
4273   // DebugStream and fVectorFit
4274   //
4275
4276   // End of one detector
4277   FillVectorFit2();
4278   
4279   
4280   // Reset
4281   for (Int_t k = 0; k < 2304; k++) {
4282     fCurrentCoefDetector2[k]  = 0.0;
4283   }
4284   
4285
4286   if(fDebugLevel > 1){
4287
4288     if ( !fDebugStreamer ) {
4289       //debug stream
4290       TDirectory *backup = gDirectory;
4291       fDebugStreamer = new TTreeSRedirector("TRDDebugFitExbAlt.root");
4292       if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
4293     } 
4294     
4295     //Debug: comparaison of the different methods (okey for first time but not for iterative procedure)
4296     AliTRDpadPlane *padplane = fGeo->GetPadPlane(GetLayer(fCountDet),GetStack(fCountDet));
4297     Float_t rowmd            = (padplane->GetRow0()+padplane->GetRowEnd())/2.;
4298     Float_t r                = AliTRDgeometry::GetTime0(GetLayer(fCountDet)); 
4299     Float_t tiltangle        = padplane->GetTiltingAngle();
4300     Int_t   detector         = fCountDet;
4301     Int_t   stack            = GetStack(fCountDet);
4302     Int_t   layer            = GetLayer(fCountDet);
4303     Float_t vf               = fCurrentCoef2[0]; 
4304     Float_t vfE              = fCurrentCoefE2;
4305    
4306     (* fDebugStreamer) << "FillFillLinearFitter"<<
4307       "detector="<<detector<<
4308       "stack="<<stack<<
4309       "layer="<<layer<<
4310       "rowmd="<<rowmd<<
4311       "r="<<r<<
4312       "tiltangle="<<tiltangle<<
4313       "vf="<<vf<<
4314       "vfE="<<vfE<<
4315       "\n";  
4316   }
4317   
4318 }
4319 //
4320 //____________Calcul Coef Mean_________________________________________________
4321 //
4322 //_____________________________________________________________________________
4323 Bool_t AliTRDCalibraFit::CalculT0CoefMean()
4324 {
4325   //
4326   // For the detector Dect calcul the mean time 0
4327   // for the calibration group idect from the choosen database
4328   //
4329
4330   fCurrentCoef2[1] = 0.0;
4331   if(fDebugLevel != 1){
4332     if(((fCalibraMode->GetNz(1) > 0) ||
4333        (fCalibraMode->GetNrphi(1) > 0)) && ((fCalibraMode->GetNz(1)    != 10) && (fCalibraMode->GetNz(1)    != 100))) {
4334
4335       for (Int_t row = fCalibraMode->GetRowMin(1); row < fCalibraMode->GetRowMax(1); row++) {
4336         for (Int_t col = fCalibraMode->GetColMin(1); col < fCalibraMode->GetColMax(1); col++) {
4337           fCurrentCoef2[1] += (Float_t) (fCalROC2->GetValue(col,row)+fCalDet2->GetValue(fCountDet));
4338         }
4339       }
4340       
4341       fCurrentCoef2[1] = fCurrentCoef2[1] / ((fCalibraMode->GetColMax(1)-fCalibraMode->GetColMin(1))*(fCalibraMode->GetRowMax(1)-fCalibraMode->GetRowMin(1)));
4342     
4343     }
4344     else {
4345      
4346       if(!fAccCDB){
4347         fCurrentCoef2[1] = fCalDet2->GetValue(fCountDet);
4348       }
4349       else{
4350         
4351         for(Int_t row = 0; row < fGeo->GetRowMax(GetLayer(fCountDet),GetStack(fCountDet),GetSector(fCountDet)); row++){
4352           for(Int_t col = 0; col < fGeo->GetColMax(GetLayer(fCountDet)); col++){
4353             fCurrentCoef2[1] += (Float_t) (fCalROC2->GetValue(col,row)+fCalDet2->GetValue(fCountDet));
4354           }
4355         }
4356         fCurrentCoef2[1] = fCurrentCoef2[1] / ((fGeo->GetRowMax(GetLayer(fCountDet),GetStack(fCountDet),GetSector(fCountDet)))*(fGeo->GetColMax(GetLayer(fCountDet))));
4357       
4358       }
4359     }
4360   }
4361   return kTRUE;
4362 }
4363
4364 //_____________________________________________________________________________
4365 Bool_t AliTRDCalibraFit::CalculChargeCoefMean(Bool_t vrai)
4366 {
4367   //
4368   // For the detector Dect calcul the mean gain factor
4369   // for the calibration group idect from the choosen database
4370   //
4371
4372   fCurrentCoef[1] = 0.0;
4373   if(fDebugLevel != 1){
4374     if (((fCalibraMode->GetNz(0)    > 0) || 
4375         (fCalibraMode->GetNrphi(0) > 0)) && ((fCalibraMode->GetNz(0)    != 10) && (fCalibraMode->GetNz(0)    != 100))) {
4376       for (Int_t row = fCalibraMode->GetRowMin(0); row < fCalibraMode->GetRowMax(0); row++) {
4377         for (Int_t col = fCalibraMode->GetColMin(0); col < fCalibraMode->GetColMax(0); col++) {
4378           fCurrentCoef[1] += (Float_t) (fCalROC->GetValue(col,row)*fCalDet->GetValue(fCountDet));
4379           if (vrai) fScaleFitFactor += (Float_t) (fCalROC->GetValue(col,row)*fCalDet->GetValue(fCountDet));
4380         }
4381       }
4382       fCurrentCoef[1] = fCurrentCoef[1] / ((fCalibraMode->GetColMax(0)-fCalibraMode->GetColMin(0))*(fCalibraMode->GetRowMax(0)-fCalibraMode->GetRowMin(0)));
4383     }
4384     else {
4385       //Per detectors
4386       fCurrentCoef[1] = (Float_t) fCalDet->GetValue(fCountDet);
4387       if (vrai) fScaleFitFactor += ((Float_t) fCalDet->GetValue(fCountDet))*(fCalibraMode->GetColMax(0)-fCalibraMode->GetColMin(0))*(fCalibraMode->GetRowMax(0)-fCalibraMode->GetRowMin(0));
4388     }    
4389   }
4390   return kTRUE;
4391 }
4392 //_____________________________________________________________________________
4393 Bool_t AliTRDCalibraFit::CalculPRFCoefMean()
4394 {
4395   //
4396   // For the detector Dect calcul the mean sigma of pad response
4397   // function for the calibration group idect from the choosen database
4398   //
4399   
4400   fCurrentCoef[1] = 0.0;
4401   if(fDebugLevel != 1){
4402     for (Int_t row = fCalibraMode->GetRowMin(2); row < fCalibraMode->GetRowMax(2); row++) {
4403       for (Int_t col = fCalibraMode->GetColMin(2); col < fCalibraMode->GetColMax(2); col++) {
4404         fCurrentCoef[1] += (Float_t) fCalROC->GetValue(col,row);
4405       }
4406     }
4407     fCurrentCoef[1] = fCurrentCoef[1] / ((fCalibraMode->GetColMax(2)-fCalibraMode->GetColMin(2))*(fCalibraMode->GetRowMax(2)-fCalibraMode->GetRowMin(2)));
4408   }
4409   return kTRUE;
4410 }
4411 //_____________________________________________________________________________
4412 Bool_t AliTRDCalibraFit::CalculVdriftCoefMean()
4413 {
4414   //
4415   // For the detector dect calcul the mean drift velocity for the
4416   // calibration group idect from the choosen database
4417   //
4418
4419   fCurrentCoef[1] = 0.0;
4420   if(fDebugLevel != 1){
4421     if (((fCalibraMode->GetNz(1)    > 0) || 
4422         (fCalibraMode->GetNrphi(1) > 0)) && ((fCalibraMode->GetNz(1)    != 10) && (fCalibraMode->GetNz(1)    != 100))) {
4423       
4424       for (Int_t row = fCalibraMode->GetRowMin(1); row < fCalibraMode->GetRowMax(1); row++) {
4425         for (Int_t col = fCalibraMode->GetColMin(1); col < fCalibraMode->GetColMax(1); col++) {
4426           fCurrentCoef[1] += (Float_t) (fCalROC->GetValue(col,row)*fCalDet->GetValue(fCountDet));
4427         }
4428       }
4429       
4430       fCurrentCoef[1] = fCurrentCoef[1] / ((fCalibraMode->GetColMax(1)-fCalibraMode->GetColMin(1))*(fCalibraMode->GetRowMax(1)-fCalibraMode->GetRowMin(1)));
4431     
4432     }
4433     else {
4434       //per detectors
4435       fCurrentCoef[1] = (Float_t) fCalDet->GetValue(fCountDet);
4436     }  
4437   }
4438   return kTRUE;
4439 }
4440 //_____________________________________________________________________________
4441 Bool_t AliTRDCalibraFit::CalculVdriftLorentzCoef()
4442 {
4443   //
4444   // For the detector fCountDet, mean drift velocity and tan lorentzangle
4445   //
4446
4447   fCurrentCoef[1]  = fCalDetVdriftUsed->GetValue(fCountDet);
4448   fCurrentCoef2[1] = fCalDetExBUsed->GetValue(fCountDet); 
4449
4450   return kTRUE;
4451 }
4452 //_____________________________________________________________________________
4453 Float_t AliTRDCalibraFit::GetPRFDefault(Int_t layer) const
4454 {
4455   //
4456   // Default width of the PRF if there is no database as reference
4457   //
4458   switch(layer)
4459     {
4460       // default database
4461       //case 0:  return 0.515;
4462       //case 1:  return 0.502;
4463       //case 2:  return 0.491;
4464       //case 3:  return 0.481;
4465       //case 4:  return 0.471;
4466       //case 5:  return 0.463;
4467       //default: return 0.0;
4468
4469       // fit database
4470     case 0:  return 0.538429;
4471     case 1:  return 0.524302;
4472     case 2:  return 0.511591;
4473     case 3:  return 0.500140;
4474     case 4:  return 0.489821;
4475     case 5:  return 0.480524;
4476     default: return 0.0;
4477   }
4478 }
4479 //________________________________________________________________________________
4480 void AliTRDCalibraFit::SetCalROC(Int_t i)
4481 {
4482   //
4483   // Set the calib object for fCountDet
4484   //
4485
4486   Float_t value = 0.0;
4487   
4488   //Get the CalDet object
4489   if(fAccCDB){
4490     AliTRDcalibDB     *cal    = AliTRDcalibDB::Instance();
4491     if (!cal) {
4492       AliInfo("Could not get calibDB");
4493       return;
4494     }
4495     switch (i)
4496       {
4497       case 0: 
4498         if( fCalROC ){ 
4499           fCalROC->~AliTRDCalROC();
4500           new(fCalROC) AliTRDCalROC(*(cal->GetGainFactorROC(fCountDet)));
4501         }else fCalROC = new AliTRDCalROC(*(cal->GetGainFactorROC(fCountDet)));
4502         break;
4503       case 1:
4504         if( fCalROC ){ 
4505           fCalROC->~AliTRDCalROC();
4506           new(fCalROC) AliTRDCalROC(*(cal->GetVdriftROC(fCountDet)));
4507         }else fCalROC = new AliTRDCalROC(*(cal->GetVdriftROC(fCountDet)));
4508         if( fCalROC2 ){ 
4509           fCalROC2->~AliTRDCalROC();
4510           new(fCalROC2) AliTRDCalROC(*(cal->GetT0ROC(fCountDet)));
4511         }else fCalROC2 = new AliTRDCalROC(*(cal->GetT0ROC(fCountDet)));
4512         break;
4513       case 2:
4514         if( fCalROC ){ 
4515           fCalROC->~AliTRDCalROC();
4516           new(fCalROC) AliTRDCalROC(*(cal->GetPRFROC(fCountDet)));
4517         }else fCalROC = new AliTRDCalROC(*(cal->GetPRFROC(fCountDet)));
4518         break; 
4519       default: return;
4520       }
4521   }
4522   else{
4523     switch (i)
4524       {
4525       case 0:
4526         if(fCalROC) delete fCalROC;
4527         fCalROC = new AliTRDCalROC(GetLayer(fCountDet),GetStack(fCountDet)); 
4528         for(Int_t k = 0; k < fCalROC->GetNchannels(); k++){
4529           fCalROC->SetValue(k,1.0);
4530         }
4531         break;
4532       case 1:
4533         if(fCalROC)  delete fCalROC;
4534         if(fCalROC2) delete fCalROC2;
4535         fCalROC  = new AliTRDCalROC(GetLayer(fCountDet),GetStack(fCountDet));
4536         fCalROC2 = new AliTRDCalROC(GetLayer(fCountDet),GetStack(fCountDet));
4537         for(Int_t k = 0; k < fCalROC->GetNchannels(); k++){
4538           fCalROC->SetValue(k,1.0);
4539           fCalROC2->SetValue(k,0.0);
4540         }
4541         break;
4542       case 2:
4543         if(fCalROC) delete fCalROC;
4544         value = GetPRFDefault(GetLayer(fCountDet));
4545         fCalROC = new AliTRDCalROC(GetLayer(fCountDet),GetStack(fCountDet)); 
4546         for(Int_t k = 0; k < fCalROC->GetNchannels(); k++){
4547           fCalROC->SetValue(k,value);
4548         }
4549         break;
4550       default: return; 
4551       }
4552   }
4553   
4554 }
4555 //____________Fit Methods______________________________________________________
4556
4557 //_____________________________________________________________________________
4558 void AliTRDCalibraFit::FitPente(TH1* projPH)
4559 {
4560   //
4561   // Slope methode for the drift velocity
4562   //
4563   
4564   // Constants
4565   const Float_t kDrWidth = AliTRDgeometry::DrThick();
4566   Int_t binmax           = 0;
4567   Int_t binmin           = 0;
4568   fPhd[0]                = 0.0;
4569   fPhd[1]                = 0.0;
4570   fPhd[2]                = 0.0;
4571   Int_t ju               = 0;
4572   fCurrentCoefE          = 0.0;
4573   fCurrentCoefE2         = 0.0;
4574   fCurrentCoef[0]        = 0.0;
4575   fCurrentCoef2[0]       = 0.0;
4576   TLine *line            = new TLine();
4577
4578   // Some variables
4579   TAxis   *xpph    = projPH->GetXaxis();
4580   Int_t    nbins   = xpph->GetNbins();
4581   Double_t lowedge = xpph->GetBinLowEdge(1);
4582   Double_t upedge  = xpph->GetBinUpEdge(xpph->GetNbins());
4583   Double_t widbins = (upedge - lowedge) / nbins;
4584   Double_t limit   = upedge + 0.5 * widbins; 
4585   Bool_t put = kTRUE;
4586
4587   // Beginning of the signal
4588   TH1D *pentea = new TH1D("pentea","pentea",projPH->GetNbinsX(),0,(Float_t) limit);
4589   for (Int_t k = 1; k <  projPH->GetNbinsX(); k++) {
4590     pentea->SetBinContent(k,(Double_t) (projPH->GetBinContent(k+1) - projPH->GetBinContent(k)));
4591   }
4592   binmax = (Int_t) pentea->GetMaximumBin();
4593   if (binmax <= 1) {
4594     binmax = 2;
4595     AliInfo("Put the binmax from 1 to 2 to enable the fit");
4596   }
4597   if (binmax >= nbins) {
4598     binmax = nbins-1;
4599     put = kFALSE;
4600     AliInfo("Put the binmax from nbins-1 to nbins-2 to enable the fit");
4601   }
4602   pentea->Fit("pol2","0MR","",TMath::Max(pentea->GetBinCenter(binmax-1),0.0),pentea->GetBinCenter(binmax+1));
4603   Float_t l3P1am = pentea->GetFunction("pol2")->GetParameter(1);
4604   Float_t l3P2am = pentea->GetFunction("pol2")->GetParameter(2);
4605   Float_t l3P1amE = pentea->GetFunction("pol2")->GetParError(1);
4606   Float_t l3P2amE = pentea->GetFunction("pol2")->GetParError(2);
4607   if (TMath::Abs(l3P2am) > 0.00000001) {
4608     fPhd[0] = -(l3P1am / (2 * l3P2am));
4609   }
4610   if(!fTakeTheMaxPH){
4611     if((TMath::Abs(l3P1am) > 0.0000000001) && (TMath::Abs(l3P2am) > 0.00000000001)){
4612       fCurrentCoefE2 = (l3P1amE/l3P1am + l3P2amE/l3P2am)*fPhd[0];
4613     }
4614   }
4615   // Amplification region
4616   binmax = 0;
4617   ju     = 0;
4618   for (Int_t kbin = 1; kbin < projPH->GetNbinsX(); kbin ++) {
4619     if (((projPH->GetBinContent(kbin+1) - projPH->GetBinContent(kbin)) <= 0.0) && (ju == 0) && (kbin > (fPhd[0]/widbins))) {
4620       binmax = kbin;
4621       ju     = 1;
4622     }
4623   }
4624   if (binmax <= 1) {
4625     binmax = 2;
4626     AliInfo("Put the binmax from 1 to 2 to enable the fit");
4627   }
4628   if (binmax >= nbins) {
4629     binmax = nbins-1;
4630     put = kFALSE;
4631     AliInfo("Put the binmax from nbins-1 to nbins-2 to enable the fit");
4632   }
4633   projPH->Fit("pol2","0MR","",TMath::Max(projPH->GetBinCenter(binmax-1),0.0),projPH->GetBinCenter(binmax+1));
4634   Float_t l3P1amf = projPH->GetFunction("pol2")->GetParameter(1);
4635   Float_t l3P2amf = projPH->GetFunction("pol2")->GetParameter(2);
4636   Float_t l3P1amfE = projPH->GetFunction("pol2")->GetParError(1);
4637   Float_t l3P2amfE = projPH->GetFunction("pol2")->GetParError(2);
4638   if (TMath::Abs(l3P2amf) > 0.00000000001) {
4639     fPhd[1] = -(l3P1amf / (2 * l3P2amf));
4640   }
4641   if((TMath::Abs(l3P1amf) > 0.0000000001) && (TMath::Abs(l3P2amf) > 0.000000001)){
4642     fCurrentCoefE = (l3P1amfE/l3P1amf + l3P2amfE/l3P2amf)*fPhd[1];
4643   }
4644   if(fTakeTheMaxPH){
4645     fCurrentCoefE2 = fCurrentCoefE;
4646   }
4647   // Drift region
4648   TH1D *pente = new TH1D("pente","pente",projPH->GetNbinsX(),0,(Float_t) limit);
4649   for (Int_t k = TMath::Min(binmax+4,projPH->GetNbinsX()); k <  projPH->GetNbinsX(); k++) {
4650     pente->SetBinContent(k,(Double_t) (projPH->GetBinContent(k+1) - projPH->GetBinContent(k)));
4651   }
4652   binmin = 0;
4653   if(pente->GetEntries() > 0) binmin = (Int_t) pente->GetMinimumBin();
4654   if (binmin <= 1) {
4655     binmin = 2;
4656     AliInfo("Put the binmax from 1 to 2 to enable the fit");
4657   }
4658   if (binmin >= nbins) {
4659     binmin = nbins-1;
4660     put = kFALSE;
4661     AliInfo("Put the binmax from nbins-1 to nbins-2 to enable the fit");
4662   }
4663   pente->Fit("pol2"
4664             ,"0MR"
4665             ,""
4666             ,TMath::Max(pente->GetBinCenter(binmin-1),             0.0)
4667             ,TMath::Min(pente->GetBinCenter(binmin+1),(Double_t) limit));
4668   Float_t l3P1dr = pente->GetFunction("pol2")->GetParameter(1);
4669   Float_t l3P2dr = pente->GetFunction("pol2")->GetParameter(2);
4670   Float_t l3P1drE = pente->GetFunction("pol2")->GetParError(1);
4671   Float_t l3P2drE = pente->GetFunction("pol2")->GetParError(2);
4672   if (TMath::Abs(l3P2dr) > 0.00000001) {
4673     fPhd[2] = -(l3P1dr / (2 * l3P2dr));
4674   }
4675   if((TMath::Abs(l3P1dr) > 0.0000000001) && (TMath::Abs(l3P2dr) > 0.00000000001)){
4676     fCurrentCoefE += (l3P1drE/l3P1dr + l3P2drE/l3P2dr)*fPhd[2]; 
4677   }
4678   Float_t fPhdt0  = 0.0;
4679   Float_t t0Shift = 0.0;
4680   if(fTakeTheMaxPH) {
4681     fPhdt0 = fPhd[1];
4682     t0Shift = fT0Shift1;
4683   }
4684   else {
4685     fPhdt0 = fPhd[0];
4686     t0Shift = fT0Shift0;
4687   }
4688
4689   if ((fPhd[2] > fPhd[0]) && 
4690       (fPhd[2] > fPhd[1]) && 
4691       (fPhd[1] > fPhd[0]) &&
4692       (put)) {
4693     fCurrentCoef[0] = (kDrWidth) / (fPhd[2]-fPhd[1]);
4694     fNumberFitSuccess++;
4695
4696     if (fPhdt0 >= 0.0) {
4697       fCurrentCoef2[0] = (fPhdt0 - t0Shift) / widbins;
4698       if (fCurrentCoef2[0] < -3.0) {
4699         fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
4700       }
4701     }
4702     else {
4703       fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
4704     }
4705
4706   }
4707   else {
4708     fCurrentCoef[0]  = -TMath::Abs(fCurrentCoef[1]);
4709     fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
4710   }
4711
4712   if (fDebugLevel == 1) {
4713     TCanvas *cpentei = new TCanvas("cpentei","cpentei",50,50,600,800);
4714     cpentei->cd();
4715     projPH->Draw();
4716     line->SetLineColor(2);
4717     line->DrawLine(fPhd[0],0,fPhd[0],projPH->GetMaximum());
4718     line->DrawLine(fPhd[1],0,fPhd[1],projPH->GetMaximum());
4719     line->DrawLine(fPhd[2],0,fPhd[2],projPH->GetMaximum());
4720     AliInfo(Form("fPhd[0] (beginning of the signal): %f"                  ,(Float_t) fPhd[0]));
4721     AliInfo(Form("fPhd[1] (end of the amplification region): %f"          ,(Float_t) fPhd[1]));
4722     AliInfo(Form("fPhd[2] (end of the drift region): %f"                  ,(Float_t) fPhd[2]));
4723     AliInfo(Form("fVriftCoef[1] (with only the drift region(default)): %f",(Float_t) fCurrentCoef[0]));
4724     TCanvas *cpentei2 = new TCanvas("cpentei2","cpentei2",50,50,600,800);
4725     cpentei2->cd();
4726     pentea->Draw();
4727     TCanvas *cpentei3 = new TCanvas("cpentei3","cpentei3",50,50,600,800);
4728     cpentei3->cd();
4729     pente->Draw();
4730   }
4731   else {
4732     delete pentea;
4733     delete pente;
4734   }
4735 }
4736 //_____________________________________________________________________________
4737 void AliTRDCalibraFit::FitLagrangePoly(TH1* projPH)
4738 {
4739   //
4740   // Slope methode but with polynomes de Lagrange
4741   //
4742
4743   // Constants
4744   const Float_t kDrWidth = AliTRDgeometry::DrThick();
4745   Int_t binmax      = 0;
4746   Int_t binmin      = 0;
4747   //Double_t    *x    = new Double_t[5];
4748   //Double_t    *y    = new Double_t[5];
4749   Double_t x[5];
4750   Double_t y[5];
4751   x[0]              = 0.0;
4752   x[1]              = 0.0;
4753   x[2]              = 0.0;
4754   x[3]              = 0.0;
4755   x[4]              = 0.0;
4756   y[0]              = 0.0;
4757   y[1]              = 0.0;
4758   y[2]              = 0.0;
4759   y[3]              = 0.0;
4760   y[4]              = 0.0;
4761   fPhd[0]           = 0.0;
4762   fPhd[1]           = 0.0;
4763   fPhd[2]           = 0.0;
4764   Int_t ju          = 0;
4765   fCurrentCoefE     = 0.0;
4766   fCurrentCoefE2    = 1.0;
4767   fCurrentCoef[0]   = 0.0;
4768   fCurrentCoef2[0]  = 0.0;
4769   TLine *line = new TLine();
4770   TF1 * polynome = 0x0;
4771   TF1 * polynomea = 0x0;
4772   TF1 * polynomeb = 0x0;
4773   Double_t c0 = 0.0;
4774   Double_t c1 = 0.0;
4775   Double_t c2 = 0.0;
4776   Double_t c3 = 0.0;
4777   Double_t c4 = 0.0;
4778   
4779   // Some variables
4780   TAxis   *xpph    = projPH->GetXaxis();
4781   Int_t    nbins   = xpph->GetNbins();
4782   Double_t lowedge = xpph->GetBinLowEdge(1);
4783   Double_t upedge  = xpph->GetBinUpEdge(xpph->GetNbins());
4784   Double_t widbins = (upedge - lowedge) / nbins;
4785   Double_t limit   = upedge + 0.5 * widbins;
4786
4787   
4788   Bool_t put = kTRUE;
4789
4790   ///////////////////////////////
4791   // Beginning of the signal
4792   //////////////////////////////
4793   TH1D *pentea = new TH1D("pentea","pentea",projPH->GetNbinsX(),0,(Float_t) limit);
4794   for (Int_t k = 1; k <  projPH->GetNbinsX(); k++) {
4795     pentea->SetBinContent(k,(Double_t) (projPH->GetBinContent(k+1) - projPH->GetBinContent(k)));
4796   }
4797
4798   binmax = (Int_t) pentea->GetMaximumBin();
4799
4800   Double_t minnn = 0.0;
4801   Double_t maxxx = 0.0;
4802
4803   Int_t kase = nbins-binmax;
4804   
4805   switch(kase)
4806     {
4807     case 0:
4808       put = kFALSE;
4809       break;
4810     case 1:
4811       minnn = pentea->GetBinCenter(binmax-2);
4812       maxxx = pentea->GetBinCenter(binmax);
4813       x[0] = pentea->GetBinCenter(binmax-2);
4814       x[1] = pentea->GetBinCenter(binmax-1);
4815       x[2] = pentea->GetBinCenter(binmax);
4816       y[0] = pentea->GetBinContent(binmax-2);
4817       y[1] = pentea->GetBinContent(binmax-1);
4818       y[2] = pentea->GetBinContent(binmax);
4819       CalculPolynomeLagrange2(x,y,c0,c1,c2,c3,c4);
4820       //AliInfo("At the limit for beginning!");
4821       break;  
4822     case 2:
4823       minnn = pentea->GetBinCenter(binmax-2);
4824       maxxx = pentea->GetBinCenter(binmax+1);
4825       x[0] = pentea->GetBinCenter(binmax-2);
4826       x[1] = pentea->GetBinCenter(binmax-1);
4827       x[2] = pentea->GetBinCenter(binmax);
4828       x[3] = pentea->GetBinCenter(binmax+1);
4829       y[0] = pentea->GetBinContent(binmax-2);
4830       y[1] = pentea->GetBinContent(binmax-1);
4831       y[2] = pentea->GetBinContent(binmax);
4832       y[3] = pentea->GetBinContent(binmax+1);
4833       CalculPolynomeLagrange3(x,y,c0,c1,c2,c3,c4);
4834       break;
4835     default:
4836       switch(binmax){
4837       case 0:
4838         put = kFALSE;
4839         break;
4840       case 1:
4841         minnn = pentea->GetBinCenter(binmax);
4842         maxxx = pentea->GetBinCenter(binmax+2);
4843         x[0] = pentea->GetBinCenter(binmax);
4844         x[1] = pentea->GetBinCenter(binmax+1);
4845         x[2] = pentea->GetBinCenter(binmax+2);
4846         y[0] = pentea->GetBinContent(binmax);
4847         y[1] = pentea->GetBinContent(binmax+1);
4848         y[2] = pentea->GetBinContent(binmax+2);
4849         CalculPolynomeLagrange2(x,y,c0,c1,c2,c3,c4);
4850         break;
4851       case 2:
4852         minnn = pentea->GetBinCenter(binmax-1);
4853         maxxx = pentea->GetBinCenter(binmax+2);
4854         x[0] = pentea->GetBinCenter(binmax-1);
4855         x[1] = pentea->GetBinCenter(binmax);
4856         x[2] = pentea->GetBinCenter(binmax+1);
4857         x[3] = pentea->GetBinCenter(binmax+2);
4858         y[0] = pentea->GetBinContent(binmax-1);
4859         y[1] = pentea->GetBinContent(binmax);
4860         y[2] = pentea->GetBinContent(binmax+1);
4861         y[3] = pentea->GetBinContent(binmax+2);
4862         CalculPolynomeLagrange3(x,y,c0,c1,c2,c3,c4);
4863         break;
4864       default:
4865         minnn = pentea->GetBinCenter(binmax-2);
4866         maxxx = pentea->GetBinCenter(binmax+2);
4867         x[0] = pentea->GetBinCenter(binmax-2);
4868         x[1] = pentea->GetBinCenter(binmax-1);
4869         x[2] = pentea->GetBinCenter(binmax);
4870         x[3] = pentea->GetBinCenter(binmax+1);
4871         x[4] = pentea->GetBinCenter(binmax+2);
4872         y[0] = pentea->GetBinContent(binmax-2);
4873         y[1] = pentea->GetBinContent(binmax-1);
4874         y[2] = pentea->GetBinContent(binmax);
4875         y[3] = pentea->GetBinContent(binmax+1);
4876         y[4] = pentea->GetBinContent(binmax+2);
4877         CalculPolynomeLagrange4(x,y,c0,c1,c2,c3,c4);
4878         break;
4879       }
4880       break;
4881     }
4882   
4883   
4884   if(put) {
4885     polynomeb = new TF1("polb","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",minnn,maxxx);
4886     polynomeb->SetParameters(c0,c1,c2,c3,c4);
4887       
4888     Double_t step = (maxxx-minnn)/10000;
4889     Double_t l = minnn;
4890     Double_t maxvalue = 0.0;
4891     Double_t placemaximum = minnn;
4892     for(Int_t o = 0; o < 10000; o++){
4893       if(o == 0) maxvalue = polynomeb->Eval(l);
4894       if(maxvalue < (polynomeb->Eval(l))){
4895         maxvalue = polynomeb->Eval(l);
4896         placemaximum = l;
4897       }
4898       l += step;
4899     }
4900     fPhd[0] = placemaximum;
4901   }
4902   
4903   /////////////////////////////
4904   // Amplification region
4905   /////////////////////////////
4906   binmax = 0;
4907   ju     = 0;
4908   for (Int_t kbin = 1; kbin < projPH->GetNbinsX(); kbin ++) {
4909     if (((projPH->GetBinContent(kbin+1) - projPH->GetBinContent(kbin)) <= 0.0) && (ju == 0) && (kbin > (fPhd[0]/widbins))) {
4910       binmax = kbin;
4911       ju     = 1;
4912     }
4913   }
4914    
4915   Double_t minn = 0.0;
4916   Double_t maxx = 0.0;
4917   x[0] = 0.0;
4918   x[1] = 0.0;
4919   x[2] = 0.0;
4920   x[3] = 0.0;
4921   x[4] = 0.0;
4922   y[0] = 0.0;
4923   y[1] = 0.0;
4924   y[2] = 0.0;
4925   y[3] = 0.0;
4926   y[4] = 0.0;
4927
4928   Int_t    kase1 = nbins - binmax;
4929
4930   //Determination of minn and maxx
4931   //case binmax = nbins
4932   //pol2
4933   switch(kase1)
4934     {
4935     case 0:
4936       minn = projPH->GetBinCenter(binmax-2);
4937       maxx = projPH->GetBinCenter(binmax);
4938       x[0] = projPH->GetBinCenter(binmax-2);
4939       x[1] = projPH->GetBinCenter(binmax-1);
4940       x[2] = projPH->GetBinCenter(binmax);
4941       y[0] = projPH->GetBinContent(binmax-2);
4942       y[1] = projPH->GetBinContent(binmax-1);
4943       y[2] = projPH->GetBinContent(binmax);
4944       CalculPolynomeLagrange2(x,y,c0,c1,c2,c3,c4);
4945       //AliInfo("At the limit for the drift!");
4946       break;
4947     case 1:
4948       minn = projPH->GetBinCenter(binmax-2);
4949       maxx = projPH->GetBinCenter(binmax+1);
4950       x[0] = projPH->GetBinCenter(binmax-2);
4951       x[1] = projPH->GetBinCenter(binmax-1);
4952       x[2] = projPH->GetBinCenter(binmax);
4953       x[3] = projPH->GetBinCenter(binmax+1);
4954       y[0] = projPH->GetBinContent(binmax-2);
4955       y[1] = projPH->GetBinContent(binmax-1);
4956       y[2] = projPH->GetBinContent(binmax);
4957       y[3] = projPH->GetBinContent(binmax+1);
4958       CalculPolynomeLagrange3(x,y,c0,c1,c2,c3,c4);
4959       break;
4960     default:
4961       switch(binmax)
4962         {
4963         case 0:
4964           put = kFALSE;
4965           break;
4966         case 1:
4967           minn = projPH->GetBinCenter(binmax);
4968           maxx = projPH->GetBinCenter(binmax+2);
4969           x[0] = projPH->GetBinCenter(binmax);
4970           x[1] = projPH->GetBinCenter(binmax+1);
4971           x[2] = projPH->GetBinCenter(binmax+2);
4972           y[0] = projPH->GetBinContent(binmax);
4973           y[1] = projPH->GetBinContent(binmax+1);
4974           y[2] = projPH->GetBinContent(binmax+2);
4975           CalculPolynomeLagrange2(x,y,c0,c1,c2,c3,c4);
4976           break;
4977         case 2:
4978           minn = projPH->GetBinCenter(binmax-1);
4979           maxx = projPH->GetBinCenter(binmax+2);
4980           x[0] = projPH->GetBinCenter(binmax-1);
4981           x[1] = projPH->GetBinCenter(binmax);
4982           x[2] = projPH->GetBinCenter(binmax+1);
4983           x[3] = projPH->GetBinCenter(binmax+2);
4984           y[0] = projPH->GetBinContent(binmax-1);
4985           y[1] = projPH->GetBinContent(binmax);
4986           y[2] = projPH->GetBinContent(binmax+1);
4987           y[3] = projPH->GetBinContent(binmax+2);
4988           CalculPolynomeLagrange3(x,y,c0,c1,c2,c3,c4);
4989           break;
4990         default:
4991           minn = projPH->GetBinCenter(binmax-2);
4992           maxx = projPH->GetBinCenter(binmax+2);
4993           x[0] = projPH->GetBinCenter(binmax-2);
4994           x[1] = projPH->GetBinCenter(binmax-1);
4995           x[2] = projPH->GetBinCenter(binmax);
4996           x[3] = projPH->GetBinCenter(binmax+1);
4997           x[4] = projPH->GetBinCenter(binmax+2);
4998           y[0] = projPH->GetBinContent(binmax-2);
4999           y[1] = projPH->GetBinContent(binmax-1);
5000           y[2] = projPH->GetBinContent(binmax);
5001           y[3] = projPH->GetBinContent(binmax+1);
5002           y[4] = projPH->GetBinContent(binmax+2);
5003           CalculPolynomeLagrange4(x,y,c0,c1,c2,c3,c4);
5004           break;
5005         }
5006       break;
5007     }
5008   
5009   if(put) {
5010     polynomea = new TF1("pola","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",minn,maxx);
5011     polynomea->SetParameters(c0,c1,c2,c3,c4);
5012        
5013     Double_t step = (maxx-minn)/1000;
5014     Double_t l = minn;
5015     Double_t maxvalue = 0.0;
5016     Double_t placemaximum = minn;
5017     for(Int_t o = 0; o < 1000; o++){
5018       if(o == 0) maxvalue = polynomea->Eval(l);
5019       if(maxvalue < (polynomea->Eval(l))){
5020         maxvalue = polynomea->Eval(l);
5021         placemaximum = l;
5022       }
5023       l += step;
5024     }
5025     fPhd[1] = placemaximum;
5026   }
5027
5028   //////////////////  
5029   // Drift region
5030   //////////////////
5031   Bool_t putd = kTRUE;
5032   TH1D *pente = new TH1D("pente","pente", projPH->GetNbinsX(),0,(Float_t) limit);
5033   for (Int_t k = TMath::Min(binmax+4, projPH->GetNbinsX()); k <  projPH->GetNbinsX(); k++) {
5034     pente->SetBinContent(k,(Double_t) (projPH->GetBinContent(k+1) - projPH->GetBinContent(k)));
5035   }
5036   binmin = 0;
5037   if(pente->GetEntries() > 0) binmin = (Int_t) pente->GetMinimumBin();
5038
5039   //should not happen
5040   if (binmin <= 1) {
5041     binmin = 2;
5042     putd = 1;
5043     //AliInfo("Put the binmax from 1 to 2 to enable the fit");
5044   }
5045   
5046   //check
5047   if((projPH->GetBinContent(binmin)-projPH->GetBinError(binmin)) < (projPH->GetBinContent(binmin+1))) {
5048     //AliInfo("Too many fluctuations at the end!");
5049     putd = kFALSE;
5050   }
5051   if((projPH->GetBinContent(binmin)+projPH->GetBinError(binmin)) > (projPH->GetBinContent(binmin-1))) {
5052     //AliInfo("Too many fluctuations at the end!");
5053     putd = kFALSE;
5054   }
5055   if(TMath::Abs(pente->GetBinContent(binmin+1)) <= 0.0000000000001){
5056     //AliInfo("No entries for the next bin!");
5057     pente->SetBinContent(binmin,0);
5058     if(pente->GetEntries() > 0) binmin = (Int_t) pente->GetMinimumBin();
5059   }
5060
5061   
5062   x[0] = 0.0;
5063   x[1] = 0.0;
5064   x[2] = 0.0;
5065   x[3] = 0.0;
5066   x[4] = 0.0;
5067   y[0] = 0.0;
5068   y[1] = 0.0;
5069   y[2] = 0.0;
5070   y[3] = 0.0;
5071   y[4] = 0.0;
5072   Double_t min = 0.0;
5073   Double_t max = 0.0;
5074   Bool_t case1 = kFALSE;
5075   Bool_t case2 = kFALSE;
5076   Bool_t case4 = kFALSE;
5077
5078   //Determination of min and max
5079   //case binmin <= nbins-3
5080   //pol4 case 3
5081   if((binmin <= (nbins-3)) && ((binmin-2) >= TMath::Min(binmax+4, projPH->GetNbinsX()))){
5082     min = pente->GetBinCenter(binmin-2);
5083     max = pente->GetBinCenter(binmin+2);
5084     x[0] = pente->GetBinCenter(binmin-2);
5085     x[1] = pente->GetBinCenter(binmin-1);
5086     x[2] = pente->GetBinCenter(binmin);
5087     x[3] = pente->GetBinCenter(binmin+1);
5088     x[4] = pente->GetBinCenter(binmin+2);
5089     y[0] = pente->GetBinContent(binmin-2);
5090     y[1] = pente->GetBinContent(binmin-1);
5091     y[2] = pente->GetBinContent(binmin);
5092     y[3] = pente->GetBinContent(binmin+1);
5093     y[4] = pente->GetBinContent(binmin+2);
5094     //Calcul the polynome de Lagrange
5095     CalculPolynomeLagrange4(x,y,c0,c1,c2,c3,c4);
5096     //richtung +/-
5097     if((pente->GetBinContent(binmin+2) <= pente->GetBinContent(binmin+1)) &&
5098        (pente->GetBinContent(binmin-2) <= pente->GetBinContent(binmin-1))) {
5099       //AliInfo("polynome 4 false 1");
5100       putd = kFALSE;
5101     }
5102     if(((binmin+3) <= (nbins-1)) &&
5103        (pente->GetBinContent(binmin+3) <= pente->GetBinContent(binmin+2)) &&
5104        ((binmin-3) >= TMath::Min(binmax+4, projPH->GetNbinsX())) &&
5105        (pente->GetBinContent(binmin-3) <= pente->GetBinContent(binmin-2))) {
5106       //AliInfo("polynome 4 false 2");
5107       putd = kFALSE;
5108     }
5109     // poly 3
5110     if((pente->GetBinContent(binmin+2) <= pente->GetBinContent(binmin+1)) &&
5111        (pente->GetBinContent(binmin-2) > pente->GetBinContent(binmin-1))) {
5112       //AliInfo("polynome 4 case 1");
5113       case1 = kTRUE;
5114     }
5115     if((pente->GetBinContent(binmin+2) > pente->GetBinContent(binmin+1)) &&
5116        (pente->GetBinContent(binmin-2) <= pente->GetBinContent(binmin-1))) {
5117       //AliInfo("polynome 4 case 4");
5118       case4 = kTRUE;
5119     }
5120     
5121   }
5122   //case binmin = nbins-2
5123   //pol3 case 1
5124   if(((binmin == (nbins-2)) && ((binmin-2) >= TMath::Min(binmax+4, projPH->GetNbinsX()))) ||
5125      (case1)){
5126     min = pente->GetBinCenter(binmin-2);
5127     max = pente->GetBinCenter(binmin+1);
5128     x[0] = pente->GetBinCenter(binmin-2);
5129     x[1] = pente->GetBinCenter(binmin-1);
5130     x[2] = pente->GetBinCenter(binmin);
5131     x[3] = pente->GetBinCenter(binmin+1);
5132     y[0] = pente->GetBinContent(binmin-2);
5133     y[1] = pente->GetBinContent(binmin-1);
5134     y[2] = pente->GetBinContent(binmin);
5135     y[3] = pente->GetBinContent(binmin+1);
5136     //Calcul the polynome de Lagrange
5137     CalculPolynomeLagrange3(x,y,c0,c1,c2,c3,c4);
5138     //richtung +: nothing
5139     //richtung -
5140     if((pente->GetBinContent(binmin-2) <= pente->GetBinContent(binmin-1))) {
5141       //AliInfo("polynome 3- case 2");
5142       case2 = kTRUE;
5143     }
5144   }
5145   //pol3 case 4
5146   if(((binmin <= (nbins-3)) && ((binmin-1) == TMath::Min(binmax+4, projPH->GetNbinsX()))) ||
5147      (case4)){
5148     min = pente->GetBinCenter(binmin-1);
5149     max = pente->GetBinCenter(binmin+2);
5150     x[0] = pente->GetBinCenter(binmin-1);
5151     x[1] = pente->GetBinCenter(binmin);
5152     x[2] = pente->GetBinCenter(binmin+1);
5153     x[3] = pente->GetBinCenter(binmin+2);
5154     y[0] = pente->GetBinContent(binmin-1);
5155     y[1] = pente->GetBinContent(binmin);
5156     y[2] = pente->GetBinContent(binmin+1);
5157     y[3] = pente->GetBinContent(binmin+2);
5158     //Calcul the polynome de Lagrange
5159     CalculPolynomeLagrange3(x,y,c0,c1,c2,c3,c4);
5160     //richtung +
5161     if((pente->GetBinContent(binmin+2) <= pente->GetBinContent(binmin+1))) {
5162       //AliInfo("polynome 3+ case 2");      
5163       case2 = kTRUE;
5164     }
5165   }
5166   //pol2 case 5
5167   if((binmin <= (nbins-3)) && (binmin == TMath::Min(binmax+4, projPH->GetNbinsX()))){
5168     min = pente->GetBinCenter(binmin);
5169     max = pente->GetBinCenter(binmin+2);
5170     x[0] = pente->GetBinCenter(binmin);
5171     x[1] = pente->GetBinCenter(binmin+1);
5172     x[2] = pente->GetBinCenter(binmin+2);
5173     y[0] = pente->GetBinContent(binmin);
5174     y[1] = pente->GetBinContent(binmin+1);
5175     y[2] = pente->GetBinContent(binmin+2);
5176     //Calcul the polynome de Lagrange
5177     CalculPolynomeLagrange2(x,y,c0,c1,c2,c3,c4);
5178     //richtung +
5179     if((pente->GetBinContent(binmin+2) <= pente->GetBinContent(binmin+1))) {
5180       //AliInfo("polynome 2+ false");
5181       putd = kFALSE;
5182     }
5183   }
5184   //pol2 case 2
5185   if(((binmin == (nbins-2)) && ((binmin-1) == TMath::Min(binmax+4, projPH->GetNbinsX()))) ||
5186      (case2)){
5187     min = pente->GetBinCenter(binmin-1);
5188     max = pente->GetBinCenter(binmin+1);
5189     x[0] = pente->GetBinCenter(binmin-1);
5190     x[1] = pente->GetBinCenter(binmin);
5191     x[2] = pente->GetBinCenter(binmin+1);
5192     y[0] = pente->GetBinContent(binmin-1);
5193     y[1] = pente->GetBinContent(binmin);
5194     y[2] = pente->GetBinContent(binmin+1);
5195     //Calcul the polynome de Lagrange
5196     CalculPolynomeLagrange2(x,y,c0,c1,c2,c3,c4);
5197     //richtung +: nothing
5198     //richtung -: nothing
5199   }
5200   //case binmin = nbins-1
5201   //pol2 case 0
5202   if((binmin == (nbins-1)) && ((binmin-2) >= TMath::Min(binmax+4, projPH->GetNbinsX()))){
5203     min = pente->GetBinCenter(binmin-2);
5204     max = pente->GetBinCenter(binmin);
5205     x[0] = pente->GetBinCenter(binmin-2);
5206     x[1] = pente->GetBinCenter(binmin-1);
5207     x[2] = pente->GetBinCenter(binmin);
5208     y[0] = pente->GetBinContent(binmin-2);
5209     y[1] = pente->GetBinContent(binmin-1);
5210     y[2] = pente->GetBinContent(binmin);
5211     //Calcul the polynome de Lagrange
5212     CalculPolynomeLagrange2(x,y,c0,c1,c2,c3,c4);
5213     //AliInfo("At the limit for the drift!");
5214     //fluctuation too big!
5215     //richtung +: nothing
5216     //richtung -
5217     if((pente->GetBinContent(binmin-2) <= pente->GetBinContent(binmin-1))) {
5218       //AliInfo("polynome 2- false ");
5219       putd = kFALSE;
5220     }
5221   }
5222   if((binmin == (nbins-1)) && ((binmin-2) < TMath::Min(binmax+4, projPH->GetNbinsX()))) {
5223     putd = kFALSE;
5224     //AliInfo("At the limit for the drift and not usable!");
5225   }
5226
5227   //pass
5228   if((binmin == (nbins-2)) && ((binmin-1) < TMath::Min(binmax+4, projPH->GetNbinsX()))){
5229     putd = kFALSE;
5230     //AliInfo("For the drift...problem!");
5231   }
5232   //pass but should not happen
5233   if((binmin <= (nbins-3)) && (binmin < TMath::Min(binmax+6, projPH->GetNbinsX()))){
5234     putd = kFALSE;
5235     //AliInfo("For the drift...problem!");
5236   }
5237   
5238   if(putd) {
5239     polynome = new TF1("pol","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x*x",min,max);
5240     polynome->SetParameters(c0,c1,c2,c3,c4);
5241     //AliInfo(Form("GetMinimum of the function %f",polynome->GetMinimumX()));
5242     Double_t step = (max-min)/1000;
5243     Double_t l = min;
5244     Double_t minvalue = 0.0;
5245     Double_t placeminimum = min;
5246     for(Int_t o = 0; o < 1000; o++){
5247       if(o == 0) minvalue = polynome->Eval(l);
5248       if(minvalue > (polynome->Eval(l))){
5249         minvalue = polynome->Eval(l);
5250         placeminimum = l;
5251       }
5252       l += step;
5253     }
5254     fPhd[2] = placeminimum;
5255   }
5256   //printf("La fin %d\n",((Int_t)(fPhd[2]*10.0))+2);
5257   if((((Int_t)(fPhd[2]*10.0))+2) >= projPH->GetNbinsX()) fPhd[2] = 0.0;
5258   if(((((Int_t)(fPhd[2]*10.0))+2) < projPH->GetNbinsX()) && (projPH->GetBinContent(((Int_t)(fPhd[2]*10.0))+2)==0)) fPhd[2] = 0.0;
5259   
5260   Float_t fPhdt0  = 0.0;
5261   Float_t t0Shift = 0.0;
5262   if(fTakeTheMaxPH) {
5263     fPhdt0 = fPhd[1];
5264     t0Shift = fT0Shift1;
5265   }
5266   else {
5267     fPhdt0 = fPhd[0];
5268     t0Shift = fT0Shift0;
5269   }
5270
5271   if ((fPhd[2] > fPhd[0]) && 
5272       (fPhd[2] > fPhd[1]) && 
5273       (fPhd[1] > fPhd[0]) &&
5274       (put) &&
5275       (putd)) {
5276     fCurrentCoef[0] = (kDrWidth) / (fPhd[2]-fPhd[1]);
5277     if(fCurrentCoef[0] > 2.5) fCurrentCoef[0] =  -TMath::Abs(fCurrentCoef[1]);
5278     else fNumberFitSuccess++;
5279     if (fPhdt0 >= 0.0) {
5280       fCurrentCoef2[0] = (fPhdt0 - t0Shift) / widbins;
5281       //printf("Value of timeoffset %f\n",fCurrentCoef2[0]);
5282       if (fCurrentCoef2[0] < -3.0) {
5283         fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
5284       }
5285     }
5286     else {
5287       fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
5288     }
5289   }
5290   else {
5291     ////printf("Put default %f\n",-TMath::Abs(fCurrentCoef[1]));
5292     fCurrentCoef[0]      = -TMath::Abs(fCurrentCoef[1]);
5293     
5294     if((fPhd[1] > fPhd[0]) &&
5295        (put)) {
5296       if (fPhdt0 >= 0.0) {
5297         fCurrentCoef2[0] = (fPhdt0 - t0Shift) / widbins;
5298         if (fCurrentCoef2[0] < -3.0) {
5299           fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
5300         }
5301         else fNumberFitSuccess++;
5302       }
5303       else {
5304         fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
5305       }
5306     }
5307     else{
5308       fCurrentCoef2[0]     = fCurrentCoef2[1] + 100.0;
5309       //printf("Fit failed!\n");
5310     }
5311   }
5312   
5313   if (fDebugLevel == 1) {
5314     TCanvas *cpentei = new TCanvas("cpentei","cpentei",50,50,600,800);
5315     cpentei->cd();
5316     projPH->Draw();
5317     if(polynomea) polynomea->Draw("same");
5318     line->SetLineColor(2);
5319     line->DrawLine(fPhd[0],0,fPhd[0],projPH->GetMaximum());
5320     line->DrawLine(fPhd[1],0,fPhd[1],projPH->GetMaximum());
5321     line->DrawLine(fPhd[2],0,fPhd[2],projPH->GetMaximum());
5322     AliInfo(Form("fPhd[0] (beginning of the signal): %f"                  ,(Float_t) fPhd[0]));
5323     AliInfo(Form("fPhd[1] (end of the amplification region): %f"          ,(Float_t) fPhd[1]));
5324     AliInfo(Form("fPhd[2] (end of the drift region): %f"                  ,(Float_t) fPhd[2]));
5325     AliInfo(Form("Vdrift (with only the drift region(default)): %f",(Float_t) fCurrentCoef[0]));
5326     AliInfo(Form("Timeoffset (with only the drift region(default)): %f",(Float_t) fCurrentCoef2[0]));
5327     TCanvas *cpentei2 = new TCanvas("cpentei2","cpentei2",50,50,600,800);
5328     cpentei2->cd();
5329     pentea->Draw();
5330     TCanvas *cpentei3 = new TCanvas("cpentei3","cpentei3",50,50,600,800);
5331     cpentei3->cd();
5332     pente->Draw();
5333   }
5334   else {
5335     delete pentea;
5336     delete pente;
5337     if(polynome) delete polynome;
5338     if(polynomea) delete polynomea;
5339     if(polynomeb) delete polynomeb;
5340     //if(x) delete [] x;
5341     //if(y) delete [] y;
5342     if(line) delete line;
5343
5344   }
5345
5346   //Provisoire
5347   //if(fCurrentCoef[0] > 1.7) fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
5348   //if((fCurrentCoef2[0] > 2.6) || (fCurrentCoef2[0] < 2.1)) fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
5349   //printf("Value of timeoffset final %f\n",fCurrentCoef2[0]);
5350   projPH->SetDirectory(0);
5351
5352 }
5353
5354 //_____________________________________________________________________________
5355 void AliTRDCalibraFit::FitPH(TH1* projPH, Int_t idect)
5356 {
5357   //
5358   // Fit methode for the drift velocity
5359   //
5360   
5361   // Constants
5362   const Float_t kDrWidth = AliTRDgeometry::DrThick();  
5363
5364   // Some variables
5365   TAxis   *xpph   = projPH->GetXaxis();
5366   Double_t upedge = xpph->GetBinUpEdge(xpph->GetNbins());
5367
5368   TF1 *fPH = new TF1("fPH",AliTRDCalibraFit::PH,-0.05,3.2,6);
5369   fPH->SetParameter(0,0.469);     // Scaling
5370   fPH->SetParameter(1,0.18);      // Start 
5371   fPH->SetParameter(2,0.0857325); // AR
5372   fPH->SetParameter(3,1.89);      // DR
5373   fPH->SetParameter(4,0.08);      // QA/QD
5374   fPH->SetParameter(5,0.0);       // Baseline
5375
5376   TLine *line = new TLine();
5377
5378   fCurrentCoef[0]     = 0.0;
5379   fCurrentCoef2[0]    = 0.0;
5380   fCurrentCoefE       = 0.0;
5381   fCurrentCoefE2      = 0.0;
5382  
5383   if (idect%fFitPHPeriode == 0) {
5384
5385     AliInfo(Form("The detector %d will be fitted",idect));
5386     fPH->SetParameter(0,(projPH->Integral()-(projPH->GetBinContent(1)*projPH->GetNbinsX())) * 0.00028); // Scaling
5387     fPH->SetParameter(1,fPhd[0] - 0.1);                                                                 // Start 
5388     fPH->SetParameter(2,fPhd[1] - fPhd[0]);                                                             // AR
5389     fPH->SetParameter(3,fPhd[2] - fPhd[1]);                                                             // DR
5390     fPH->SetParameter(4,0.225);                                                                         // QA/QD
5391     fPH->SetParameter(5,(Float_t) projPH->GetBinContent(1));
5392     
5393     if (fDebugLevel != 1) {
5394       projPH->Fit(fPH,"0M","",0.0,upedge);
5395     }
5396     else {
5397       TCanvas *cpente = new TCanvas("cpente","cpente",50,50,600,800);
5398       cpente->cd();
5399       projPH->Fit(fPH,"M+","",0.0,upedge);
5400       projPH->Draw("E0");
5401       line->SetLineColor(4);
5402       line->DrawLine(fPH->GetParameter(1)
5403                     ,0
5404                     ,fPH->GetParameter(1)
5405                     ,projPH->GetMaximum());
5406       line->DrawLine(fPH->GetParameter(1)+fPH->GetParameter(2)
5407                     ,0
5408                     ,fPH->GetParameter(1)+fPH->GetParameter(2)
5409                     ,projPH->GetMaximum());
5410       line->DrawLine(fPH->GetParameter(1)+fPH->GetParameter(2)+fPH->GetParameter(3)
5411                     ,0
5412                     ,fPH->GetParameter(1)+fPH->GetParameter(2)+fPH->GetParameter(3)
5413                     ,projPH->GetMaximum());
5414     }
5415
5416     if (fPH->GetParameter(3) != 0) {
5417       fNumberFitSuccess++;
5418       fCurrentCoef[0]    = kDrWidth / (fPH->GetParameter(3));
5419       fCurrentCoefE      = (fPH->GetParError(3)/fPH->GetParameter(3))*fCurrentCoef[0];
5420       fCurrentCoef2[0]   = fPH->GetParameter(1);
5421       fCurrentCoefE2     = fPH->GetParError(1);
5422     } 
5423     else {
5424       fCurrentCoef[0]     = -TMath::Abs(fCurrentCoef[1]);
5425       fCurrentCoef2[0]    = fCurrentCoef2[1] + 100.0;
5426     }
5427  
5428   }
5429   else {
5430
5431     // Put the default value
5432     fCurrentCoef[0]  = -TMath::Abs(fCurrentCoef[1]);
5433     fCurrentCoef2[0] = fCurrentCoef2[1] + 100.0;
5434   }
5435
5436   if (fDebugLevel != 1) {
5437     delete fPH;
5438   }
5439   
5440 }
5441 //_____________________________________________________________________________
5442 Bool_t AliTRDCalibraFit::FitPRFGausMI(Double_t *arraye, Double_t *arraym, Double_t *arrayme, Int_t nBins, Float_t xMin, Float_t xMax)
5443 {
5444   //
5445   // Fit methode for the sigma of the pad response function
5446   //
5447
5448   TVectorD param(3);
5449   
5450   fCurrentCoef[0]  = 0.0;
5451   fCurrentCoefE = 0.0;
5452
5453   Double_t ret = FitGausMI(arraye, arraym, arrayme, nBins, xMin, xMax,&param); 
5454
5455   if(TMath::Abs(ret+4) <= 0.000000001){
5456     fCurrentCoef[0] = -fCurrentCoef[1];
5457     return kFALSE;
5458   }
5459   else {
5460     fNumberFitSuccess++;
5461     fCurrentCoef[0] = param[2];
5462     fCurrentCoefE   = ret;
5463     return kTRUE;
5464   }
5465 }
5466 //_____________________________________________________________________________
5467 Double_t AliTRDCalibraFit::FitGausMI(Double_t *arraye, Double_t *arraym, Double_t *arrayme, Int_t nBins, Float_t xMin, Float_t xMax, TVectorD *param, Bool_t bError)
5468 {
5469   //
5470   // Fit methode for the sigma of the pad response function
5471   //
5472
5473   //We should have at least 3 points
5474   if(nBins <=3) return -4.0;
5475
5476   TLinearFitter fitter(3,"pol2");
5477   fitter.StoreData(kFALSE);
5478   fitter.ClearPoints();
5479   TVectorD  par(3);
5480   Float_t binWidth = (xMax-xMin)/(Float_t)nBins;
5481   Float_t entries = 0;
5482   Int_t   nbbinwithentries = 0;
5483   for (Int_t i=0; i<nBins; i++){
5484     entries+=arraye[i];
5485     if(arraye[i] > 15) nbbinwithentries++;
5486     //printf("entries for i %d: %f\n",i,arraye[i]);
5487   }
5488   if ((entries<700) || (nbbinwithentries < ((Int_t)(nBins/2)))) return -4;
5489   //printf("entries %f\n",entries);
5490   //printf("nbbinwithentries %d\n",nbbinwithentries);  
5491
5492   Int_t npoints=0;
5493   Float_t errorm = 0.0;
5494   Float_t errorn = 0.0;
5495   Float_t error  = 0.0;
5496   
5497   //
5498   for (Int_t ibin=0;ibin<nBins; ibin++){
5499       Float_t entriesI = arraye[ibin];
5500       Float_t valueI   = arraym[ibin];
5501       Double_t xcenter = 0.0;
5502       Float_t  val     = 0.0;
5503       if ((entriesI>15) && (valueI>0.0)){
5504         xcenter = xMin+(ibin+0.5)*binWidth;
5505         errorm   = 0.0;
5506         errorn   = 0.0;
5507         error    = 0.0;
5508         if(!bError){
5509           if((valueI + 0.01) > 0.0) errorm = TMath::Log((valueI + 0.01)/valueI);
5510           if((valueI - 0.01) > 0.0) errorn = TMath::Log((valueI - 0.01)/valueI);
5511           error = TMath::Max(TMath::Abs(errorm),TMath::Abs(errorn));
5512         }
5513         else{
5514           if((valueI + arrayme[ibin]) > 0.0) errorm = TMath::Log((valueI + arrayme[ibin])/valueI);
5515           if((valueI - arrayme[ibin]) > 0.0) errorn = TMath::Log((valueI - arrayme[ibin])/valueI);
5516           error = TMath::Max(TMath::Abs(errorm),TMath::Abs(errorn));
5517         }
5518         if(TMath::Abs(error) < 0.000000001) continue;
5519         val      = TMath::Log(Float_t(valueI));
5520         fitter.AddPoint(&xcenter,val,error);
5521         npoints++;
5522       }
5523
5524       if(fDebugLevel > 1){
5525
5526       if ( !fDebugStreamer ) {
5527         //debug stream
5528         TDirectory *backup = gDirectory;
5529         fDebugStreamer = new TTreeSRedirector("TRDDebugFitPRF.root");
5530         if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
5531       } 
5532       
5533       Int_t    detector     = fCountDet;
5534       Int_t    layer        = GetLayer(fCountDet);
5535       Int_t    group        = ibin;    
5536      
5537       (* fDebugStreamer) << "FitGausMIFill"<<
5538         "detector="<<detector<<
5539         "layer="<<layer<<
5540         "nbins="<<nBins<<
5541         "group="<<group<<
5542         "entriesI="<<entriesI<<
5543         "valueI="<<valueI<<
5544         "val="<<val<<
5545         "xcenter="<<xcenter<<
5546         "errorm="<<errorm<<
5547         "errorn="<<errorn<<
5548         "error="<<error<<
5549         "bError="<<bError<<
5550         "\n";  
5551     }
5552
5553   }
5554
5555   if(npoints <=3) return -4.0;  
5556
5557   Double_t chi2 = 0;
5558   if (npoints>3){
5559     fitter.Eval();
5560     fitter.GetParameters(par);
5561     chi2 = fitter.GetChisquare()/Float_t(npoints);
5562     
5563         
5564     if (!param)  param  = new TVectorD(3);
5565     if(TMath::Abs(par[2]) <= 0.000000001) return -4.0;
5566     Double_t  x      = TMath::Sqrt(TMath::Abs(-2*par[2])); 
5567     Double_t deltax = (fitter.GetParError(2))/x;
5568     Double_t errorparam2 = TMath::Abs(deltax)/(x*x);
5569     chi2 = errorparam2;
5570     
5571     (*param)[1] = par[1]/(-2.*par[2]);
5572     (*param)[2] = 1./TMath::Sqrt(TMath::Abs(-2.*par[2]));
5573     Double_t lnparam0 = par[0]+ par[1]* (*param)[1] +  par[2]*(*param)[1]*(*param)[1];
5574     if ( lnparam0>307 ) return -4;
5575     (*param)[0] = TMath::Exp(lnparam0);
5576
5577     if(fDebugLevel > 1){
5578
5579       if ( !fDebugStreamer ) {
5580         //debug stream
5581         TDirectory *backup = gDirectory;
5582         fDebugStreamer = new TTreeSRedirector("TRDDebugFitPRF.root");
5583         if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
5584       } 
5585       
5586       Int_t    detector     = fCountDet;
5587       Int_t    layer        = GetLayer(fCountDet);
5588            
5589      
5590       (* fDebugStreamer) << "FitGausMIFit"<<
5591         "detector="<<detector<<
5592         "layer="<<layer<<
5593         "nbins="<<nBins<<
5594         "errorsigma="<<chi2<<
5595         "mean="<<(*param)[1]<<
5596         "sigma="<<(*param)[2]<<
5597         "constant="<<(*param)[0]<<
5598         "\n";  
5599     }
5600   }
5601
5602   if((chi2/(*param)[2]) > 0.1){
5603     if(bError){
5604       chi2 = FitGausMI(arraye,arraym,arrayme,nBins,xMin,xMax,param,kFALSE);
5605     }
5606     else return -4.0;
5607   }
5608
5609   if(fDebugLevel == 1){
5610     TString name("PRF");
5611     name += (Int_t)xMin;
5612     name += (Int_t)xMax;  
5613     TCanvas *c1 = new TCanvas((const char *)name,(const char *)name,50,50,600,800);  
5614     c1->cd();
5615     name += "histo";
5616     TH1F *histo = new TH1F((const char *)name,(const char *)name,nBins,xMin,xMax);
5617     for(Int_t k = 0; k < nBins; k++){
5618       histo->SetBinContent(k+1,arraym[k]);
5619       histo->SetBinError(k+1,arrayme[k]);
5620     }
5621     histo->Draw();
5622     name += "functionf";
5623     TF1 *f1= new TF1((const char*)name,"[0]*exp(-(x-[1])^2/(2*[2]*[2]))",xMin,xMax);
5624     f1->SetParameter(0, (*param)[0]);
5625     f1->SetParameter(1, (*param)[1]);
5626     f1->SetParameter(2, (*param)[2]);
5627     f1->Draw("same");
5628   }
5629
5630   
5631   return chi2;
5632  
5633 }
5634 //_____________________________________________________________________________
5635 void AliTRDCalibraFit::FitPRF(TH1 *projPRF)
5636 {
5637   //
5638   // Fit methode for the sigma of the pad response function
5639   //
5640   
5641   fCurrentCoef[0]  = 0.0;
5642   fCurrentCoefE = 0.0;
5643
5644   if (fDebugLevel != 1) {
5645     projPRF->Fit("gaus","0M","",-fRangeFitPRF,fRangeFitPRF);
5646   }
5647   else {
5648     TCanvas *cfit = new TCanvas("cfit","cfit",50,50,600,800);
5649     cfit->cd();
5650     projPRF->Fit("gaus","M+","",-fRangeFitPRF,fRangeFitPRF);
5651     projPRF->Draw();
5652   }
5653   fCurrentCoef[0]  = projPRF->GetFunction("gaus")->GetParameter(2);
5654   fCurrentCoefE = projPRF->GetFunction("gaus")->GetParError(2);
5655   if(fCurrentCoef[0] <= 0.0) fCurrentCoef[0] = -fCurrentCoef[1];
5656   else {
5657     fNumberFitSuccess++;
5658   }
5659 }
5660 //_____________________________________________________________________________
5661 void AliTRDCalibraFit::RmsPRF(TH1 *projPRF)
5662 {
5663   //
5664   // Fit methode for the sigma of the pad response function
5665   //
5666   fCurrentCoef[0]   = 0.0;
5667   fCurrentCoefE  = 0.0;
5668   if (fDebugLevel == 1) {
5669     TCanvas *cfit = new TCanvas("cfit","cfit",50,50,600,800);
5670     cfit->cd();
5671     projPRF->Draw();
5672   }
5673   fCurrentCoef[0] = projPRF->GetRMS();
5674   if(fCurrentCoef[0] <= 0.0) fCurrentCoef[0] = -fCurrentCoef[1];
5675   else {
5676     fNumberFitSuccess++;
5677   }
5678 }
5679 //_____________________________________________________________________________
5680 void AliTRDCalibraFit::FitTnpRange(Double_t *arraye, Double_t *arraym, Double_t *arrayme, Int_t nbg, Int_t nybins)
5681 {
5682   //
5683   // Fit methode for the sigma of the pad response function with 2*nbg tan bins
5684   //
5685   
5686   TLinearFitter linearfitter = TLinearFitter(3,"pol2");
5687  
5688
5689   Int_t   nbins    = (Int_t)(nybins/(2*nbg));
5690   Float_t lowedge  = -3.0*nbg;
5691   Float_t upedge   = lowedge + 3.0; 
5692   Int_t   offset   = 0;
5693   Int_t   npoints  = 0;
5694   Double_t xvalues = -0.2*nbg+0.1;
5695   Double_t y       = 0.0;
5696   Int_t   total    = 2*nbg;
5697
5698   
5699   for(Int_t k = 0; k < total; k++){
5700     if(FitPRFGausMI(arraye+offset, arraym+offset, arrayme+offset, nbins, lowedge, upedge)){
5701       npoints++;
5702       y = fCurrentCoef[0]*fCurrentCoef[0];
5703       linearfitter.AddPoint(&xvalues,y,2*fCurrentCoefE*fCurrentCoef[0]);
5704     }
5705     
5706     if(fDebugLevel > 1){
5707
5708       if ( !fDebugStreamer ) {
5709         //debug stream
5710         TDirectory *backup = gDirectory;
5711         fDebugStreamer = new TTreeSRedirector("TRDDebugFitPRF.root");
5712         if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
5713       } 
5714       
5715       Int_t    detector     = fCountDet;
5716       Int_t    layer        = GetLayer(fCountDet);
5717       Int_t    nbtotal      = total;  
5718       Int_t    group        = k;    
5719       Float_t  low          = lowedge;
5720       Float_t  up           = upedge;
5721       Float_t  tnp          = xvalues;
5722       Float_t  wid          = fCurrentCoef[0];
5723       Float_t  widfE        = fCurrentCoefE;
5724
5725       (* fDebugStreamer) << "FitTnpRange0"<<
5726         "detector="<<detector<<
5727         "layer="<<layer<<
5728         "nbtotal="<<nbtotal<<
5729         "group="<<group<<
5730         "low="<<low<<
5731         "up="<<up<<
5732         "offset="<<offset<<
5733         "tnp="<<tnp<<
5734         "wid="<<wid<<
5735         "widfE="<<widfE<<
5736         "\n";  
5737     }
5738     
5739     offset  += nbins;
5740     lowedge += 3.0;
5741     upedge  += 3.0;
5742     xvalues += 0.2;
5743
5744   }
5745
5746   fCurrentCoefE = 0.0;
5747   fCurrentCoef[0] = 0.0;
5748
5749   //printf("npoints\n",npoints);
5750
5751   if(npoints < 3){
5752     fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
5753   }
5754   else{
5755   
5756     TVectorD pars0;
5757     linearfitter.Eval();
5758     linearfitter.GetParameters(pars0);
5759     Double_t pointError0  =  TMath::Sqrt(linearfitter.GetChisquare()/npoints);
5760     Double_t errorsx0     =  linearfitter.GetParError(2)*pointError0;
5761     Double_t min0         = 0.0;
5762     Double_t ermin0       = 0.0;
5763     //Double_t prfe0      = 0.0;
5764     Double_t prf0         = 0.0;
5765     if((pars0[2] > 0.000000000001) && (TMath::Abs(pars0[1]) >= 0.000000000001)) {
5766       min0 = -pars0[1]/(2*pars0[2]);
5767       ermin0 = TMath::Abs(min0*(errorsx0/pars0[2]+linearfitter.GetParError(1)*pointError0/pars0[1]));
5768       prf0 = pars0[0]+pars0[1]*min0+pars0[2]*min0*min0;
5769       if(prf0 > 0.0) {
5770         /*
5771           prfe0 = linearfitter->GetParError(0)*pointError0
5772           +(linearfitter->GetParError(1)*pointError0/pars0[1]+ermin0/min0)*pars0[1]*min0
5773           +(linearfitter->GetParError(2)*pointError0/pars0[2]+2*ermin0/min0)*pars0[2]*min0*min0;
5774           prfe0 = prfe0/(2*TMath::Sqrt(prf0));
5775           fCurrentCoefE   = (Float_t) prfe0;
5776         */
5777         fCurrentCoef[0] = (Float_t) TMath::Sqrt(TMath::Abs(prf0));
5778       }
5779       else{
5780         fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
5781       }
5782     }
5783     else {
5784       fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
5785     }
5786
5787     if(fDebugLevel > 1){
5788
5789       if ( !fDebugStreamer ) {
5790         //debug stream
5791         TDirectory *backup = gDirectory;
5792         fDebugStreamer = new TTreeSRedirector("TRDDebugFitPRF.root");
5793         if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
5794       } 
5795       
5796       Int_t    detector     = fCountDet;
5797       Int_t    layer        = GetLayer(fCountDet);
5798       Int_t    nbtotal      = total;
5799       Double_t colsize[6]   = {0.635,0.665,0.695,0.725,0.755,0.785};  
5800       Double_t sigmax       = TMath::Sqrt(TMath::Abs(pars0[2]))*10000*colsize[layer];      
5801
5802       (* fDebugStreamer) << "FitTnpRange1"<<
5803         "detector="<<detector<<
5804         "layer="<<layer<<
5805         "nbtotal="<<nbtotal<<
5806         "par0="<<pars0[0]<<
5807         "par1="<<pars0[1]<<
5808         "par2="<<pars0[2]<<
5809         "npoints="<<npoints<<
5810         "sigmax="<<sigmax<<
5811         "tan="<<min0<<
5812         "sigmaprf="<<fCurrentCoef[0]<<
5813         "sigprf="<<fCurrentCoef[1]<<
5814         "\n";  
5815     }
5816     
5817   }
5818   
5819 }
5820 //_____________________________________________________________________________
5821 void AliTRDCalibraFit::FitMean(TH1 *projch, Double_t nentries, Double_t mean)
5822 {
5823   //
5824   // Only mean methode for the gain factor
5825   //
5826  
5827   fCurrentCoef[0] = mean;
5828   fCurrentCoefE   = 0.0;
5829   if(nentries > 0) fCurrentCoefE = projch->GetRMS()/TMath::Sqrt(nentries);
5830   if (fDebugLevel == 1) {
5831     TCanvas *cpmean = new TCanvas("cpmean","cpmean",50,50,600,800);
5832     cpmean->cd();
5833     projch->Draw();
5834   }
5835   CalculChargeCoefMean(kTRUE);
5836   fNumberFitSuccess++;
5837 }
5838 //_____________________________________________________________________________
5839 void AliTRDCalibraFit::FitMeanW(TH1 *projch, Double_t nentries)
5840 {
5841   //
5842   // mean w methode for the gain factor
5843   //
5844
5845   //Number of bins
5846   Int_t nybins = projch->GetNbinsX();
5847  
5848   //The weight function
5849   Double_t a = 0.00228515;
5850   Double_t b = -0.00231487;
5851   Double_t c = 0.00044298;
5852   Double_t d = -0.00379239;
5853   Double_t e = 0.00338349;
5854
5855 //   0 |0.00228515
5856 //    1 |-0.00231487
5857 //    2 |0.00044298
5858 //    3 |-0.00379239
5859 //    4 |0.00338349
5860
5861
5862
5863   //A arbitrary error for the moment
5864   fCurrentCoefE = 0.0;
5865   fCurrentCoef[0] = 0.0;
5866   
5867   //Calcul 
5868   Double_t sumw = 0.0;
5869   Double_t sum = 0.0; 
5870   Float_t sumAll   = (Float_t) nentries;
5871   Int_t sumCurrent = 0;
5872   for(Int_t k = 0; k <nybins; k++){
5873     Double_t fraction = Float_t(sumCurrent)/Float_t(sumAll);
5874     if (fraction<fOutliersFitChargeLow) {
5875       sumCurrent += (Int_t) projch->GetBinContent(k+1);
5876       //printf("Take only after bin %d\n",k);
5877       continue;
5878     }
5879     if (fraction>fOutliersFitChargeHigh) {
5880       //printf("Break by the bin %d\n",k);
5881       break;
5882     }
5883     Double_t weight = a + b*fraction + c*fraction*fraction + d *fraction*fraction*fraction+
5884       e*fraction*fraction*fraction*fraction;
5885     sumw += weight*projch->GetBinContent(k+1)*projch->GetBinCenter(k+1);
5886     sum  += weight*projch->GetBinContent(k+1); 
5887     sumCurrent += (Int_t) projch->GetBinContent(k+1);
5888     //printf("fraction %f, weight %f, bincontent %f\n",fraction,weight,projch->GetBinContent(k+1));   
5889   }
5890   if(sum > 0.0) fCurrentCoef[0] = (sumw/sum);
5891
5892   if (fDebugLevel == 1) {
5893     TCanvas *cpmeanw = new TCanvas("cpmeanw","cpmeanw",50,50,600,800);
5894     cpmeanw->cd();
5895     projch->Draw();
5896     TLine *line = new TLine(fCurrentCoef[0],0.0,fCurrentCoef[0],20000.0);
5897     line->Draw("same");
5898   }
5899   fNumberFitSuccess++;
5900   CalculChargeCoefMean(kTRUE);
5901 }
5902 //_____________________________________________________________________________
5903 void AliTRDCalibraFit::FitMeanWSm(TH1 *projch, Float_t sumAll)
5904 {
5905   //
5906   // mean w methode for the gain factor
5907   //
5908
5909   //Number of bins
5910   Int_t nybins = projch->GetNbinsX();
5911  
5912   //The weight function
5913   Double_t a = 0.00228515;
5914   Double_t b = -0.00231487;
5915   Double_t c = 0.00044298;
5916   Double_t d = -0.00379239;
5917   Double_t e = 0.00338349;
5918
5919 //   0 |0.00228515
5920 //    1 |-0.00231487
5921 //    2 |0.00044298
5922 //    3 |-0.00379239
5923 //    4 |0.00338349
5924
5925
5926
5927   //A arbitrary error for the moment
5928   fCurrentCoefE = 0.0;
5929   fCurrentCoef[0] = 0.0;
5930   
5931   //Calcul 
5932   Double_t sumw = 0.0;
5933   Double_t sum = 0.0; 
5934   Int_t sumCurrent = 0;
5935   for(Int_t k = 0; k <nybins; k++){
5936     Double_t fraction = Float_t(sumCurrent)/Float_t(sumAll);
5937     if (fraction>0.95) break;
5938     Double_t weight = a + b*fraction + c*fraction*fraction + d *fraction*fraction*fraction+
5939       e*fraction*fraction*fraction*fraction;
5940     sumw += weight*projch->GetBinContent(k+1)*projch->GetBinCenter(k+1);
5941     sum  += weight*projch->GetBinContent(k+1); 
5942     sumCurrent += (Int_t) projch->GetBinContent(k+1);
5943     //printf("fraction %f, weight %f, bincontent %f\n",fraction,weight,projch->GetBinContent(k+1));   
5944   }
5945   if(sum > 0.0) fCurrentCoef[0] = (sumw/sum);
5946
5947   if (fDebugLevel == 1) {
5948     TCanvas *cpmeanw = new TCanvas("cpmeanw","cpmeanw",50,50,600,800);
5949     cpmeanw->cd();
5950     projch->Draw();
5951     TLine *line = new TLine(fCurrentCoef[0],0.0,fCurrentCoef[0],20000.0);
5952     line->Draw("same");
5953   }
5954   fNumberFitSuccess++;
5955 }
5956 //_____________________________________________________________________________
5957 void AliTRDCalibraFit::FitLandau(TH1 *projch, Double_t mean, Double_t nentries)
5958 {
5959   //
5960   // Fit methode for the gain factor
5961   //
5962
5963   
5964   //Calcul Range of the fit 
5965   Double_t lastvalue = 0.0;
5966   Float_t sumAll   = (Float_t) nentries;
5967   Int_t sumCurrent = 0;
5968   //printf("There are %d bins\n",nybins);
5969   for(Int_t k = 0; k <projch->GetNbinsX(); k++){
5970     Double_t fraction = Float_t(sumCurrent)/Float_t(sumAll);
5971     if (fraction>fOutliersFitChargeHigh) {
5972       lastvalue = projch->GetBinCenter(k+1); 
5973       //printf("Break by %f\n",lastvalue);
5974       break;
5975     }
5976     sumCurrent += (Int_t) projch->GetBinContent(k+1);
5977   }
5978   //
5979  
5980   fCurrentCoef[0]  = 0.0;
5981   fCurrentCoefE    = 0.0;
5982   Double_t chisqrl = 0.0;
5983   
5984   projch->Fit("landau","WWQ+",""
5985              ,(Double_t) mean/fBeginFitCharge
5986              ,lastvalue);
5987   chisqrl = projch->GetFunction("landau")->GetChisquare();
5988  
5989   if (fDebugLevel == 1) {
5990     TCanvas *cp = new TCanvas("cp","cp",50,50,600,800);
5991     cp->cd();
5992     projch->Draw();
5993     TLine *line = new TLine( projch->GetFunction("landau")->GetParameter(1),0.0,projch->GetFunction("landau")->GetParameter(1),20000.0);
5994     line->Draw("same");
5995   }
5996   
5997   if ((projch->GetFunction("landau")->GetParameter(1) > 0) && (projch->GetFunction("landau")->GetParError(1) < (0.05*projch->GetFunction("landau")->GetParameter(1)))) {
5998     fNumberFitSuccess++;
5999     CalculChargeCoefMean(kTRUE);
6000     fCurrentCoef[0]  = projch->GetFunction("landau")->GetParameter(1);
6001     fCurrentCoefE    = projch->GetFunction("landau")->GetParError(1);
6002   }
6003   else {
6004     CalculChargeCoefMean(kFALSE);
6005     fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
6006   }
6007    
6008   
6009
6010 }
6011 //_____________________________________________________________________________
6012 void AliTRDCalibraFit::FitCH(TH1 *projch, Double_t mean, Double_t nentries)
6013 {
6014   //
6015   // Fit methode for the gain factor
6016   //
6017
6018   //Calcul Range of the fit 
6019   Double_t lastvalue = 0.0;
6020   Float_t sumAll   = (Float_t) nentries;
6021   Int_t sumCurrent = 0;
6022   //printf("There are %d bins\n",nybins);
6023   for(Int_t k = 0; k <projch->GetNbinsX(); k++){
6024     Double_t fraction = Float_t(sumCurrent)/Float_t(sumAll);
6025     if (fraction>fOutliersFitChargeHigh) {
6026       lastvalue = projch->GetBinCenter(k+1); 
6027       //printf("Break by %f\n",lastvalue);
6028       break;
6029     }
6030     sumCurrent += (Int_t) projch->GetBinContent(k+1);
6031   }
6032   //
6033  
6034   fCurrentCoef[0]  = 0.0;
6035   fCurrentCoefE    = 0.0;
6036   Double_t chisqrl = 0.0;
6037   Double_t chisqrg = 0.0;
6038   Double_t chisqr  = 0.0;
6039   TF1 *fLandauGaus = new TF1("fLandauGaus",FuncLandauGaus,(Double_t) mean/fBeginFitCharge,lastvalue,5);
6040
6041   projch->Fit("landau","WWQ0",""
6042              ,(Double_t) mean/fBeginFitCharge
6043              ,lastvalue);
6044   Double_t l3P0         = projch->GetFunction("landau")->GetParameter(0);
6045   Double_t l3P1         = projch->GetFunction("landau")->GetParameter(1);
6046   Double_t l3P2         = projch->GetFunction("landau")->GetParameter(2);
6047   chisqrl = projch->GetFunction("landau")->GetChisquare();
6048     
6049   projch->Fit("gaus","WWQ0",""
6050               ,(Double_t) mean/fBeginFitCharge
6051               ,lastvalue);
6052   Double_t g3P0         = projch->GetFunction("gaus")->GetParameter(0);
6053   Double_t g3P2         = projch->GetFunction("gaus")->GetParameter(2);
6054   chisqrg = projch->GetFunction("gaus")->GetChisquare();
6055         
6056   fLandauGaus->SetParameters(l3P0,l3P1,l3P2,g3P0,g3P2);
6057   if (fDebugLevel != 1) {
6058     projch->Fit("fLandauGaus","WWQ0",""
6059                 ,(Double_t) mean/fBeginFitCharge
6060                 ,lastvalue);
6061     chisqr = projch->GetFunction("fLandauGaus")->GetChisquare();
6062   } 
6063   else  {
6064     TCanvas *cp = new TCanvas("cp","cp",50,50,600,800);
6065     cp->cd();
6066     projch->Fit("fLandauGaus","WWQ+",""
6067                 ,(Double_t) mean/fBeginFitCharge
6068                 ,lastvalue);
6069     chisqr = projch->GetFunction("fLandauGaus")->GetChisquare();
6070     projch->Draw();
6071     fLandauGaus->Draw("same");
6072     TLine *line = new TLine(projch->GetFunction("fLandauGaus")->GetParameter(1),0.0,projch->GetFunction("fLandauGaus")->GetParameter(1),20000.0);
6073     line->Draw("same");
6074   }
6075   
6076   if ((projch->GetFunction("fLandauGaus")->GetParameter(1) > 0) && (projch->GetFunction("fLandauGaus")->GetParError(1) < (0.05*projch->GetFunction("fLandauGaus")->GetParameter(1))) && (chisqr < chisqrl) && (chisqr < chisqrg)) {
6077     fNumberFitSuccess++;
6078     CalculChargeCoefMean(kTRUE);
6079     fCurrentCoef[0]  = projch->GetFunction("fLandauGaus")->GetParameter(1);
6080     fCurrentCoefE    = projch->GetFunction("fLandauGaus")->GetParError(1);
6081   }
6082   else {
6083     CalculChargeCoefMean(kFALSE);
6084     fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
6085   }
6086    
6087   if (fDebugLevel != 1) {
6088     delete fLandauGaus;
6089   }
6090
6091 }
6092 //_____________________________________________________________________________
6093 void AliTRDCalibraFit::FitBisCH(TH1* projch, Double_t mean, Double_t nentries)
6094 {
6095   //
6096   // Fit methode for the gain factor more time consuming
6097   //
6098
6099   //Calcul Range of the fit 
6100   Double_t lastvalue = 0.0;
6101   Float_t sumAll   = (Float_t) nentries;
6102   Int_t sumCurrent = 0;
6103   //printf("There are %d bins\n",nybins);
6104   for(Int_t k = 0; k <projch->GetNbinsX(); k++){
6105     Double_t fraction = Float_t(sumCurrent)/Float_t(sumAll);
6106     if (fraction>fOutliersFitChargeHigh) {
6107       lastvalue = projch->GetBinCenter(k+1); 
6108       //printf("Break by %f\n",lastvalue);
6109       break;
6110     }
6111     sumCurrent += (Int_t) projch->GetBinContent(k+1);
6112   }
6113   //
6114
6115   //Some parameters to initialise
6116   Double_t widthLandau, widthGaus, mPV, integral;
6117   Double_t chisquarel = 0.0;
6118   Double_t chisquareg = 0.0;
6119   projch->Fit("landau","WWQ0M+",""
6120               ,(Double_t) mean/fBeginFitCharge
6121               ,lastvalue);
6122   widthLandau  = projch->GetFunction("landau")->GetParameter(2);
6123   chisquarel = projch->GetFunction("landau")->GetChisquare();
6124   projch->Fit("gaus","WWQ0M+",""
6125               ,(Double_t) mean/fBeginFitCharge
6126               ,lastvalue);
6127   widthGaus    = projch->GetFunction("gaus")->GetParameter(2);
6128   chisquareg = projch->GetFunction("gaus")->GetChisquare();
6129     
6130   mPV = (projch->GetFunction("landau")->GetParameter(1))/2;
6131   integral = (projch->GetFunction("gaus")->Integral(0.3*mean,3*mean)+projch->GetFunction("landau")->Integral(0.3*mean,3*mean))/2;
6132   
6133   // Setting fit range and start values
6134   Double_t fr[2];
6135   Double_t sv[4]   = { widthLandau, mPV, integral, widthGaus};
6136   Double_t pllo[4] = { 0.001, 0.001, projch->Integral()/3, 0.001};
6137   Double_t plhi[4] = { 300.0, 300.0, 30*projch->Integral(), 300.0};
6138   Double_t fp[4]   = { 1.0, 1.0, 1.0, 1.0 };
6139   Double_t fpe[4]  = { 1.0, 1.0, 1.0, 1.0 };
6140   fr[0]            = mean/fBeginFitCharge;
6141   fr[1]            = lastvalue;
6142   fCurrentCoef[0]  = 0.0;
6143   fCurrentCoefE    = 0.0;
6144
6145   Double_t chisqr;
6146   Int_t    ndf;
6147   TF1 *fitsnr = LanGauFit(projch,&fr[0],&sv[0]
6148                                 ,&pllo[0],&plhi[0]
6149                                 ,&fp[0],&fpe[0]
6150                                 ,&chisqr,&ndf);
6151     
6152   //Double_t projchPeak;
6153   //Double_t projchFWHM;
6154   //LanGauPro(fp,projchPeak,projchFWHM);
6155
6156   if ((fp[1] > 0) && ((fpe[1] < (0.05*fp[1])) && (chisqr < chisquarel) && (chisqr < chisquareg))) {
6157     //if ((fp[1] > 0) && ((chisqr < chisquarel) && (chisqr < chisquareg))) {
6158     fNumberFitSuccess++;
6159     CalculChargeCoefMean(kTRUE);
6160     fCurrentCoef[0]  = fp[1];
6161     fCurrentCoefE = fpe[1];
6162     //chargeCoefE2 = chisqr;
6163   } 
6164   else {
6165     CalculChargeCoefMean(kFALSE);
6166     fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
6167   }
6168   if (fDebugLevel == 1) {
6169     AliInfo(Form("fChargeCoef[0]: %f",(Float_t) fCurrentCoef[0]));
6170     TCanvas *cpy = new TCanvas("cpy","cpy",50,50,600,800);
6171     cpy->cd();
6172     projch->Draw();
6173     fitsnr->Draw("same");
6174     TLine *line = new TLine(fp[1],0.0,fp[1],20000.0);
6175     line->Draw("same");
6176   }
6177   else {
6178     delete fitsnr;
6179   }
6180
6181 //_____________________________________________________________________________
6182 void AliTRDCalibraFit::FitBisCHEx(TH1* projch, Double_t mean, Double_t nentries)
6183 {
6184   //
6185   // Fit methode for the gain factor more time consuming
6186   //
6187
6188   //Calcul Range of the fit 
6189   Double_t lastvalue = 0.0;
6190   Float_t sumAll   = (Float_t) nentries;
6191   Int_t sumCurrent = 0;
6192   //printf("There are %d bins\n",nybins);
6193   for(Int_t k = 0; k <projch->GetNbinsX(); k++){
6194     Double_t fraction = Float_t(sumCurrent)/Float_t(sumAll);
6195     if (fraction>fOutliersFitChargeHigh) {
6196       lastvalue = projch->GetBinCenter(k+1); 
6197       //printf("Break by %f\n",lastvalue);
6198       break;
6199     }
6200     sumCurrent += (Int_t) projch->GetBinContent(k+1);
6201   }
6202   //
6203
6204
6205   //Some parameters to initialise
6206   Double_t widthLandau, widthGaus, mPV, integral;
6207   Double_t chisquarel = 0.0;
6208   Double_t chisquareg = 0.0;
6209   projch->Fit("landau","WWQM+",""
6210               ,(Double_t) mean/fBeginFitCharge
6211               ,lastvalue);
6212   widthLandau  = projch->GetFunction("landau")->GetParameter(2);
6213   chisquarel = projch->GetFunction("landau")->GetChisquare();
6214   projch->Fit("gaus","WWQM+",""
6215               ,(Double_t) mean/fBeginFitCharge
6216               ,lastvalue);
6217   widthGaus    = projch->GetFunction("gaus")->GetParameter(2);
6218   chisquareg = projch->GetFunction("gaus")->GetChisquare();
6219     
6220   mPV = (projch->GetFunction("landau")->GetParameter(1))/2;
6221   integral = (projch->GetFunction("gaus")->Integral(0.3*mean,3*mean)+projch->GetFunction("landau")->Integral(0.3*mean,3*mean))/2;
6222   
6223   // Setting fit range and start values
6224   Double_t fr[2];
6225   //Double_t sv[4] = { l3P2, fChargeCoef[1], projch->Integral("width"), fG3P2 };
6226   //Double_t sv[4]   = { fL3P2, fChargeCoef[1], fL3P0, fG3P2 };
6227   Double_t sv[5]   = { widthLandau, mPV, integral, widthGaus, 0.0};
6228   Double_t pllo[5] = { 0.001, 0.001, projch->Integral()/3, 0.001, 0.0};
6229   Double_t plhi[5] = { 300.0, 300.0, 30*projch->Integral(), 300.0, 2.0};
6230   Double_t fp[5]   = { 1.0, 1.0, 1.0, 1.0, 1.0};
6231   Double_t fpe[5]  = { 1.0, 1.0, 1.0, 1.0, 1.0};
6232   //
6233   //fr[0]            = 0.3 * mean;
6234   //fr[1]            = 3.0 * mean;
6235   //
6236   fr[0]            = mean/fBeginFitCharge;
6237   fr[1]            = lastvalue;
6238   
6239   fCurrentCoef[0]  = 0.0;
6240   fCurrentCoefE    = 0.0;
6241
6242   Double_t chisqr = 100.0;
6243   Int_t    ndf = 1;
6244   
6245   TF1 *fitsnr = 0x0;
6246   
6247   if((mPV > 0.0) && (projch->GetFunction("gaus")->GetParameter(1) > 0.0)) {
6248     fitsnr = LanGauFitEx(projch,&fr[0],&sv[0]
6249                          ,&pllo[0],&plhi[0]
6250                          ,&fp[0],&fpe[0]
6251                          ,&chisqr,&ndf);
6252   }  
6253
6254   //Double_t projchPeak;
6255   //Double_t projchFWHM;
6256   //LanGauProEx(fp,projchPeak,projchFWHM);
6257
6258   if ((fp[1] > 0) && ((fpe[1] < (0.05*fp[1])) && (chisqr < chisquarel) && (chisqr < chisquareg))) {
6259     //if ((fp[1] > 0) && ((chisqr < chisquarel) && (chisqr < chisquareg))) {
6260     fNumberFitSuccess++;
6261     CalculChargeCoefMean(kTRUE);
6262     fCurrentCoef[0]  = fp[1];
6263     fCurrentCoefE = fpe[1];
6264     //chargeCoefE2 = chisqr;
6265   } 
6266   else {
6267     CalculChargeCoefMean(kFALSE);
6268     fCurrentCoef[0] = -TMath::Abs(fCurrentCoef[1]);
6269   }
6270   if (fDebugLevel == 1) {
6271     AliInfo(Form("fChargeCoef[0]: %f",(Float_t) fCurrentCoef[0]));
6272     TCanvas *cpy = new TCanvas("cpy","cpy",50,50,600,800);
6273     cpy->cd();
6274     projch->Draw();
6275     if(fitsnr) fitsnr->Draw("same");
6276     TLine *line = new TLine(fp[1],0.0,fp[1],20000.0);
6277     line->Draw("same");
6278   }
6279   else {
6280     delete fitsnr;
6281   }
6282
6283 //_____________________________________________________________________________
6284 void AliTRDCalibraFit::CalculPolynomeLagrange2(const Double_t *x, const Double_t *y, Double_t &c0, Double_t &c1, Double_t &c2, Double_t &c3, Double_t &c4) const
6285 {
6286   //
6287   // Calcul the coefficients of the polynome passant par ces trois points de degre 2
6288   //
6289   Double_t x0 = y[0]/((x[0]-x[1])*(x[0]-x[2]));
6290   Double_t x1 = y[1]/((x[1]-x[0])*(x[1]-x[2]));
6291   Double_t x2 = y[2]/((x[2]-x[0])*(x[2]-x[1]));
6292
6293   c4 = 0.0;
6294   c3 = 0.0;
6295   c2 = x0+x1+x2;
6296   c1 = -(x0*(x[1]+x[2])+x1*(x[0]+x[2])+x2*(x[0]+x[1]));
6297   c0 = x0*x[1]*x[2]+x1*x[0]*x[2]+x2*x[0]*x[1];
6298
6299 }
6300
6301 //_____________________________________________________________________________
6302 void AliTRDCalibraFit::CalculPolynomeLagrange3(const Double_t *x, const Double_t *y, Double_t &c0, Double_t &c1, Double_t &c2, Double_t &c3, Double_t &c4) const
6303 {
6304   //
6305   // Calcul the coefficients of the polynome passant par ces quatre points de degre 3
6306   //
6307   Double_t x0 = y[0]/((x[0]-x[1])*(x[0]-x[2])*(x[0]-x[3]));
6308   Double_t x1 = y[1]/((x[1]-x[0])*(x[1]-x[2])*(x[1]-x[3]));
6309   Double_t x2 = y[2]/((x[2]-x[0])*(x[2]-x[1])*(x[2]-x[3]));
6310   Double_t x3 = y[3]/((x[3]-x[0])*(x[3]-x[1])*(x[3]-x[2]));
6311
6312   c4 = 0.0;
6313   c3 = x0+x1+x2+x3;
6314   c2 = -(x0*(x[1]+x[2]+x[3])
6315            +x1*(x[0]+x[2]+x[3])
6316            +x2*(x[0]+x[1]+x[3])
6317            +x3*(x[0]+x[1]+x[2]));
6318   c1 = (x0*(x[1]*x[2]+x[1]*x[3]+x[2]*x[3])
6319           +x1*(x[0]*x[2]+x[0]*x[3]+x[2]*x[3])
6320           +x2*(x[0]*x[1]+x[0]*x[3]+x[1]*x[3])
6321           +x3*(x[0]*x[1]+x[0]*x[2]+x[1]*x[2]));
6322   
6323   c0 = -(x0*x[1]*x[2]*x[3]
6324           +x1*x[0]*x[2]*x[3]
6325           +x2*x[0]*x[1]*x[3]
6326           +x3*x[0]*x[1]*x[2]);  
6327
6328
6329 }
6330
6331 //_____________________________________________________________________________
6332 void AliTRDCalibraFit::CalculPolynomeLagrange4(const Double_t *x, const Double_t *y, Double_t &c0, Double_t &c1, Double_t &c2, Double_t &c3, Double_t &c4) const
6333 {
6334   //
6335   // Calcul the coefficients of the polynome passant par ces cinqs points de degre 4
6336   //
6337   Double_t x0 = y[0]/((x[0]-x[1])*(x[0]-x[2])*(x[0]-x[3])*(x[0]-x[4]));
6338   Double_t x1 = y[1]/((x[1]-x[0])*(x[1]-x[2])*(x[1]-x[3])*(x[1]-x[4]));
6339   Double_t x2 = y[2]/((x[2]-x[0])*(x[2]-x[1])*(x[2]-x[3])*(x[2]-x[4]));
6340   Double_t x3 = y[3]/((x[3]-x[0])*(x[3]-x[1])*(x[3]-x[2])*(x[3]-x[4]));
6341   Double_t x4 = y[4]/((x[4]-x[0])*(x[4]-x[1])*(x[4]-x[2])*(x[4]-x[3]));
6342  
6343
6344   c4 = x0+x1+x2+x3+x4;
6345   c3 = -(x0*(x[1]+x[2]+x[3]+x[4])
6346            +x1*(x[0]+x[2]+x[3]+x[4])
6347            +x2*(x[0]+x[1]+x[3]+x[4])
6348            +x3*(x[0]+x[1]+x[2]+x[4])
6349            +x4*(x[0]+x[1]+x[2]+x[3]));
6350   c2 = (x0*(x[1]*x[2]+x[1]*x[3]+x[1]*x[4]+x[2]*x[3]+x[2]*x[4]+x[3]*x[4])
6351           +x1*(x[0]*x[2]+x[0]*x[3]+x[0]*x[4]+x[2]*x[3]+x[2]*x[4]+x[3]*x[4])
6352           +x2*(x[0]*x[1]+x[0]*x[3]+x[0]*x[4]+x[1]*x[3]+x[1]*x[4]+x[3]*x[4])
6353           +x3*(x[0]*x[1]+x[0]*x[2]+x[0]*x[4]+x[1]*x[2]+x[1]*x[4]+x[2]*x[4])
6354           +x4*(x[0]*x[1]+x[0]*x[2]+x[0]*x[3]+x[1]*x[2]+x[1]*x[3]+x[2]*x[3]));
6355
6356   c1 = -(x0*(x[1]*x[2]*x[3]+x[1]*x[2]*x[4]+x[1]*x[3]*x[4]+x[2]*x[3]*x[4])
6357           +x1*(x[0]*x[2]*x[3]+x[0]*x[2]*x[4]+x[0]*x[3]*x[4]+x[2]*x[3]*x[4])
6358           +x2*(x[0]*x[1]*x[3]+x[0]*x[1]*x[4]+x[0]*x[3]*x[4]+x[1]*x[3]*x[4])
6359           +x3*(x[0]*x[1]*x[2]+x[0]*x[1]*x[4]+x[0]*x[2]*x[4]+x[1]*x[2]*x[4])
6360           +x4*(x[0]*x[1]*x[2]+x[0]*x[1]*x[3]+x[0]*x[2]*x[3]+x[1]*x[2]*x[3]));
6361
6362   c0 = (x0*x[1]*x[2]*x[3]*x[4]
6363           +x1*x[0]*x[2]*x[3]*x[4]
6364           +x2*x[0]*x[1]*x[3]*x[4]
6365           +x3*x[0]*x[1]*x[2]*x[4]
6366           +x4*x[0]*x[1]*x[2]*x[3]);
6367
6368 }
6369 //_____________________________________________________________________________
6370 void AliTRDCalibraFit::NormierungCharge()
6371 {
6372   //
6373   // Normalisation of the gain factor resulting for the fits
6374   //
6375   
6376   // Calcul of the mean of choosen method by fFitChargeNDB
6377   Double_t sum         = 0.0;
6378   //printf("total number of entries %d\n",fVectorFitCH->GetEntriesFast());
6379   for (Int_t k = 0; k < (Int_t) fVectorFit.GetEntriesFast(); k++) {
6380     Int_t    total    = 0;
6381     Int_t    detector = ((AliTRDFitInfo *) fVectorFit.At(k))->GetDetector();
6382     Float_t *coef     = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef();
6383     //printf("detector %d coef[0] %f\n",detector,coef[0]);
6384     if (GetStack(detector) == 2) {
6385       total = 1728;
6386     }
6387     if (GetStack(detector) != 2) {
6388       total = 2304;
6389     }
6390     for (Int_t j = 0; j < total; j++) {
6391       if (coef[j] >= 0) {
6392         sum += coef[j];
6393       }
6394     }
6395   }
6396
6397   if (sum > 0) {
6398     fScaleFitFactor = fScaleFitFactor / sum;
6399   }
6400   else {
6401     fScaleFitFactor = 1.0;
6402   }  
6403
6404   //methode de boeuf mais bon...
6405   Double_t scalefactor = fScaleFitFactor;
6406   
6407   if(fDebugLevel > 1){
6408     
6409     if ( !fDebugStreamer ) {
6410       //debug stream
6411       TDirectory *backup = gDirectory;
6412       fDebugStreamer = new TTreeSRedirector("TRDDebugFitCH.root");
6413       if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
6414     } 
6415     (* fDebugStreamer) << "NormierungCharge"<<
6416       "scalefactor="<<scalefactor<<
6417       "\n";  
6418     }
6419 }
6420 //_____________________________________________________________________________
6421 TH1I *AliTRDCalibraFit::ReBin(const TH1I *hist) const
6422 {
6423   //
6424   // Rebin of the 1D histo for the gain calibration if needed.
6425   // you have to choose fRebin, divider of fNumberBinCharge
6426   //
6427
6428  TAxis *xhist  = hist->GetXaxis();
6429  TH1I  *rehist = new TH1I("projrebin","",(Int_t) xhist->GetNbins()/fRebin
6430                                         ,xhist->GetBinLowEdge(1)
6431                                         ,xhist->GetBinUpEdge(xhist->GetNbins()));
6432
6433  AliInfo(Form("fRebin: %d",fRebin));
6434  Int_t i = 1;
6435  for (Int_t k = 1; k <= (Int_t) xhist->GetNbins()/fRebin; k++) {
6436    Double_t sum = 0.0;
6437    for (Int_t ji = i; ji < i+fRebin; ji++) {
6438      sum += hist->GetBinContent(ji);
6439    }
6440    sum = sum / fRebin;
6441    rehist->SetBinContent(k,sum);
6442    i += fRebin;
6443  }
6444
6445  return rehist;
6446
6447 }
6448
6449 //_____________________________________________________________________________
6450 TH1F *AliTRDCalibraFit::ReBin(const TH1F *hist) const
6451 {
6452   //
6453   // Rebin of the 1D histo for the gain calibration if needed
6454   // you have to choose fRebin divider of fNumberBinCharge
6455   //
6456
6457   TAxis *xhist  = hist->GetXaxis();
6458   TH1F  *rehist = new TH1F("projrebin","",(Int_t) xhist->GetNbins()/fRebin
6459                                          ,xhist->GetBinLowEdge(1)
6460                                          ,xhist->GetBinUpEdge(xhist->GetNbins()));
6461
6462   AliInfo(Form("fRebin: %d",fRebin));
6463   Int_t i = 1;
6464   for (Int_t k = 1; k <= (Int_t) xhist->GetNbins()/fRebin; k++) {
6465     Double_t sum = 0.0;
6466     for (Int_t ji = i; ji < i+fRebin; ji++) {
6467       sum += hist->GetBinContent(ji);
6468     }
6469     sum = sum/fRebin;
6470     rehist->SetBinContent(k,sum);
6471     i += fRebin;
6472   }
6473
6474   return rehist;
6475   
6476 }
6477 //
6478 //____________Some basic geometry function_____________________________________
6479 //
6480
6481 //_____________________________________________________________________________
6482 Int_t AliTRDCalibraFit::GetLayer(Int_t d) const
6483 {
6484   //
6485   // Reconstruct the plane number from the detector number
6486   //
6487
6488   return ((Int_t) (d % 6));
6489
6490 }
6491
6492 //_____________________________________________________________________________
6493 Int_t AliTRDCalibraFit::GetStack(Int_t d) const
6494 {
6495   //
6496   // Reconstruct the stack number from the detector number
6497   //
6498   const Int_t kNlayer = 6;
6499
6500   return ((Int_t) (d % 30) / kNlayer);
6501
6502 }
6503
6504 //_____________________________________________________________________________
6505 Int_t AliTRDCalibraFit::GetSector(Int_t d) const
6506 {
6507   //
6508   // Reconstruct the sector number from the detector number
6509   //
6510   Int_t fg = 30;
6511
6512   return ((Int_t) (d / fg));
6513
6514 }
6515
6516 //
6517 //____________Fill and Init tree Gain, PRF, Vdrift and T0______________________
6518 //
6519 //_______________________________________________________________________________
6520 void AliTRDCalibraFit::ResetVectorFit()
6521 {
6522   //
6523   // Reset the VectorFits
6524   //
6525
6526   fVectorFit.SetOwner();
6527   fVectorFit.Clear();
6528   fVectorFit2.SetOwner();
6529   fVectorFit2.Clear();
6530   
6531 }
6532 //
6533 //____________Private Functions________________________________________________
6534 //
6535
6536 //_____________________________________________________________________________
6537 Double_t AliTRDCalibraFit::PH(const Double_t *x, const Double_t *par) 
6538 {
6539   //
6540   // Function for the fit
6541   //
6542
6543   //TF1 *fAsymmGauss = new TF1("fAsymmGauss",AsymmGauss,0,4,6);
6544
6545   //PARAMETERS FOR FIT PH
6546   // PASAv.4
6547   //fAsymmGauss->SetParameter(0,0.113755);
6548   //fAsymmGauss->SetParameter(1,0.350706);
6549   //fAsymmGauss->SetParameter(2,0.0604244);
6550   //fAsymmGauss->SetParameter(3,7.65596);
6551   //fAsymmGauss->SetParameter(4,1.00124);
6552   //fAsymmGauss->SetParameter(5,0.870597);  // No tail cancelation
6553
6554   Double_t xx = x[0];
6555   
6556   if (xx < par[1]) {
6557     return par[5];
6558   }
6559
6560   Double_t dx       = 0.005;
6561   Double_t xs       = par[1];
6562   Double_t ss       = 0.0;
6563   Double_t paras[2] = { 0.0, 0.0 };
6564
6565   while (xs < xx) {
6566     if ((xs >= par[1]) &&
6567         (xs < (par[1]+par[2]))) {
6568       //fAsymmGauss->SetParameter(0,par[0]);
6569       //fAsymmGauss->SetParameter(1,xs);
6570       //ss += fAsymmGauss->Eval(xx);
6571       paras[0] = par[0];
6572       paras[1] = xs;
6573       ss += AsymmGauss(&xx,paras);
6574     }
6575     if ((xs >= (par[1]+par[2])) && 
6576         (xs <  (par[1]+par[2]+par[3]))) {
6577       //fAsymmGauss->SetParameter(0,par[0]*par[4]);
6578       //fAsymmGauss->SetParameter(1,xs);
6579       //ss += fAsymmGauss->Eval(xx);
6580       paras[0] = par[0]*par[4];
6581       paras[1] = xs;
6582       ss += AsymmGauss(&xx,paras);
6583     }
6584     xs += dx;
6585   }
6586   
6587   return ss + par[5];
6588
6589 }
6590
6591 //_____________________________________________________________________________
6592 Double_t AliTRDCalibraFit::AsymmGauss(const Double_t *x, const Double_t *par)
6593 {
6594   //
6595   // Function for the fit
6596   //
6597
6598   //par[0] = normalization
6599   //par[1] = mean
6600   //par[2] = sigma
6601   //norm0  = 1
6602   //par[3] = lambda0
6603   //par[4] = norm1
6604   //par[5] = lambda1
6605   
6606   Double_t par1save = par[1];    
6607   //Double_t par2save = par[2];
6608   Double_t par2save = 0.0604244;
6609   //Double_t par3save = par[3];
6610   Double_t par3save = 7.65596;
6611   //Double_t par5save = par[5];
6612   Double_t par5save = 0.870597;
6613   Double_t dx       = x[0] - par1save;
6614
6615   Double_t  sigma2  = par2save*par2save;
6616   Double_t  sqrt2   = TMath::Sqrt(2.0);
6617   Double_t  exp1    = par3save * TMath::Exp(-par3save * (dx - 0.5 * par3save * sigma2))
6618                                * (1.0 - AliMathBase::ErfFast((par3save * sigma2 - dx) / (sqrt2 * par2save)));
6619   Double_t  exp2    = par5save * TMath::Exp(-par5save * (dx - 0.5 * par5save * sigma2))
6620                                * (1.0 - AliMathBase::ErfFast((par5save * sigma2 - dx) / (sqrt2 * par2save)));
6621
6622   //return par[0]*(exp1+par[4]*exp2);
6623   return par[0] * (exp1 + 1.00124 * exp2);
6624
6625 }
6626
6627 //_____________________________________________________________________________
6628 Double_t AliTRDCalibraFit::FuncLandauGaus(const Double_t *x, const Double_t *par)
6629 {
6630   //
6631   // Sum Landau + Gaus with identical mean
6632   //
6633
6634   Double_t valLandau = par[0] * TMath::Landau(x[0],par[1],par[2]);
6635   //Double_t valGaus   = par[3] * TMath::Gaus(x[0],par[4],par[5]);
6636   Double_t valGaus   = par[3] * TMath::Gaus(x[0],par[1],par[4]);
6637   Double_t val       = valLandau + valGaus;
6638
6639   return val;
6640
6641 }
6642
6643 //_____________________________________________________________________________
6644 Double_t AliTRDCalibraFit::LanGauFun(const Double_t *x, const Double_t *par) 
6645 {
6646   //
6647   // Function for the fit
6648   //
6649   // Fit parameters:
6650   // par[0]=Width (scale) parameter of Landau density
6651   // par[1]=Most Probable (MP, location) parameter of Landau density
6652   // par[2]=Total area (integral -inf to inf, normalization constant)
6653   // par[3]=Width (sigma) of convoluted Gaussian function
6654   //
6655   // In the Landau distribution (represented by the CERNLIB approximation), 
6656   // the maximum is located at x=-0.22278298 with the location parameter=0.
6657   // This shift is corrected within this function, so that the actual
6658   // maximum is identical to the MP parameter.
6659   //  
6660
6661   // Numeric constants
6662   Double_t invsq2pi = 0.3989422804014;   // (2 pi)^(-1/2)
6663   Double_t mpshift  = -0.22278298;       // Landau maximum location
6664   
6665   // Control constants
6666   Double_t np       = 100.0;             // Number of convolution steps
6667   Double_t sc       =   5.0;             // Convolution extends to +-sc Gaussian sigmas
6668   
6669   // Variables
6670   Double_t xx = 0.0;
6671   Double_t mpc = 0.0;
6672   Double_t fland = 0.0;
6673   Double_t sum = 0.0;
6674   Double_t xlow = 0.0;
6675   Double_t xupp = 0.0;
6676   Double_t step = 0.0;
6677   Double_t i = 0.0;
6678   
6679   // MP shift correction
6680   mpc = par[1] - mpshift * par[0]; 
6681
6682   // Range of convolution integral
6683   xlow = x[0] - sc * par[3];
6684   xupp = x[0] + sc * par[3];
6685   
6686   step = (xupp - xlow) / np;
6687
6688   // Convolution integral of Landau and Gaussian by sum
6689   for (i = 1.0; i <= np/2; i++) {
6690
6691     xx    = xlow + (i-.5) * step;
6692     if(par[0] > 0.0) fland = TMath::Landau(xx,mpc,par[0]) / par[0];
6693     sum  += fland * TMath::Gaus(x[0],xx,par[3]);
6694     
6695     xx    = xupp - (i-.5) * step;
6696     if(par[0] > 0.0) fland = TMath::Landau(xx,mpc,par[0]) / par[0];
6697     sum  += fland * TMath::Gaus(x[0],xx,par[3]);
6698
6699   }
6700
6701   if(par[3] > 0.0) return (par[2] * step * sum * invsq2pi / par[3]);
6702   else return 0.0;
6703
6704 }
6705 //_____________________________________________________________________________
6706 Double_t AliTRDCalibraFit::LanGauFunEx(const Double_t *x, const Double_t *par) 
6707 {
6708   //
6709   // Function for the fit
6710   //
6711   // Fit parameters:
6712   // par[0]=Width (scale) parameter of Landau density
6713   // par[1]=Most Probable (MP, location) parameter of Landau density
6714   // par[2]=Total area (integral -inf to inf, normalization constant)
6715   // par[3]=Width (sigma) of convoluted Gaussian function
6716   // par[4]=Exponential Slope Parameter
6717   //
6718   // In the Landau distribution (represented by the CERNLIB approximation), 
6719   // the maximum is located at x=-0.22278298 with the location parameter=0.
6720   // This shift is corrected within this function, so that the actual
6721   // maximum is identical to the MP parameter.
6722   //  
6723
6724   // Numeric constants
6725   Double_t invsq2pi = 0.3989422804014;   // (2 pi)^(-1/2)
6726   Double_t mpshift  = -0.22278298;       // Landau maximum location
6727   
6728   // Control constants
6729   Double_t np       = 100.0;             // Number of convolution steps
6730   Double_t sc       =   5.0;             // Convolution extends to +-sc Gaussian sigmas
6731   
6732   // Variables
6733   Double_t xx= 0.0;
6734   Double_t mpc= 0.0;
6735   Double_t fland = 0.0;
6736   Double_t sum = 0.0;
6737   Double_t xlow= 0.0;
6738   Double_t xupp= 0.0;
6739   Double_t step= 0.0;
6740   Double_t i= 0.0;
6741   
6742   // MP shift correction
6743   mpc = par[1] - mpshift * par[0]; 
6744
6745   // Range of convolution integral
6746   xlow = x[0] - sc * par[3];
6747   xupp = x[0] + sc * par[3];
6748   
6749   step = (xupp - xlow) / np;
6750
6751   // Convolution integral of Landau and Gaussian by sum
6752   for (i = 1.0; i <= np/2; i++) {
6753
6754     xx    = xlow + (i-.5) * step;
6755     if(par[0] > 0.0) fland = TMath::Landau(xx,mpc,par[0])*TMath::Exp(-par[4]*xx) / par[0];
6756     sum  += fland * TMath::Gaus(x[0],xx,par[3]);
6757     
6758     xx    = xupp - (i-.5) * step;
6759     if(par[0] > 0.0) fland = TMath::Landau(xx,mpc,par[0])*TMath::Exp(-par[4]*xx) / par[0];
6760     sum  += fland * TMath::Gaus(x[0],xx,par[3]);
6761
6762   }
6763
6764   if(par[3] > 0.0) return (par[2] * step * sum * invsq2pi / par[3]);
6765   else return 0.0;
6766
6767 }
6768 //_____________________________________________________________________________
6769 TF1 *AliTRDCalibraFit::LanGauFit(TH1 *his, const Double_t *fitrange, const Double_t *startvalues
6770                                       , const Double_t *parlimitslo, const Double_t *parlimitshi
6771                                       , Double_t *fitparams, Double_t *fiterrors
6772                                       , Double_t *chiSqr, Int_t *ndf) const
6773 {
6774   //
6775   // Function for the fit
6776   //
6777   
6778   Int_t i;
6779   Char_t funname[100];
6780   
6781   TF1 *ffitold = (TF1 *) gROOT->GetListOfFunctions()->FindObject(funname);
6782   if (ffitold) {
6783     delete ffitold;
6784   }  
6785
6786   TF1 *ffit    = new TF1(funname,LanGauFun,fitrange[0],fitrange[1],4);
6787   ffit->SetParameters(startvalues);
6788   ffit->SetParNames("Width","MP","Area","GSigma");
6789   
6790   for (i = 0; i < 4; i++) {
6791     ffit->SetParLimits(i,parlimitslo[i],parlimitshi[i]);
6792   }
6793   
6794   his->Fit(funname,"WWQRB0");                   // Fit within specified range, use ParLimits, do not plot
6795   
6796   ffit->GetParameters(fitparams);            // Obtain fit parameters
6797   for (i = 0; i < 4; i++) {
6798     fiterrors[i] = ffit->GetParError(i);     // Obtain fit parameter errors
6799   }
6800   chiSqr[0] = ffit->GetChisquare();          // Obtain chi^2
6801   ndf[0]    = ffit->GetNDF();                // Obtain ndf
6802
6803   return (ffit);                             // Return fit function
6804    
6805 }
6806 //_____________________________________________________________________________
6807 TF1 *AliTRDCalibraFit::LanGauFitEx(TH1 *his, const Double_t *fitrange, const Double_t *startvalues
6808                                       , const Double_t *parlimitslo, const Double_t *parlimitshi
6809                                       , Double_t *fitparams, Double_t *fiterrors
6810                                       , Double_t *chiSqr, Int_t *ndf) const
6811 {
6812   //
6813   // Function for the fit
6814   //
6815   
6816   Int_t i;
6817   Char_t funname[100];
6818   
6819   TF1 *ffitold = (TF1 *) gROOT->GetListOfFunctions()->FindObject(funname);
6820   if (ffitold) {
6821     delete ffitold;
6822   }  
6823
6824   TF1 *ffit    = new TF1(funname,LanGauFunEx,fitrange[0],fitrange[1],5);
6825   ffit->SetParameters(startvalues);
6826   ffit->SetParNames("Width","MP","Area","GSigma","Ex");
6827   
6828   for (i = 0; i < 5; i++) {
6829     ffit->SetParLimits(i,parlimitslo[i],parlimitshi[i]);
6830   }
6831   
6832   his->Fit(funname,"WWQRB0");                   // Fit within specified range, use ParLimits, do not plot
6833   
6834   ffit->GetParameters(fitparams);            // Obtain fit parameters
6835   for (i = 0; i < 5; i++) {
6836     fiterrors[i] = ffit->GetParError(i);     // Obtain fit parameter errors
6837   }
6838   chiSqr[0] = ffit->GetChisquare();          // Obtain chi^2
6839   ndf[0]    = ffit->GetNDF();                // Obtain ndf
6840
6841   return (ffit);                             // Return fit function
6842    
6843 }
6844
6845
6846
6847