]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDCalibraFit.cxx
Adding missing semicolons, better structured if statements
[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);