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