]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDPreprocessorOffline.cxx
AddTimeStamp was always increasing track length but accounting
[u/mrichter/AliRoot.git] / TRD / AliTRDPreprocessorOffline.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
17
18 /*
19   Responsible: Raphaelle Bailhache (rbailhache@ikf.uni-frankfurt.de) 
20   Code to analyze the TRD calibration and to produce OCDB entries  
21
22
23    .x ~/rootlogon.C
24    gSystem->Load("libANALYSIS");
25    gSystem->Load("libTRDcalib");
26
27    AliTRDPreprocessorOffline proces;
28    TString ocdbPath="local:////"
29    ocdbPath+=gSystem->GetFromPipe("pwd");
30
31    proces.CalibTimeGain("CalibObjects.root",run0,run1,ocdbPath);
32    proces.CalibTimeVdrift("CalibObjects.root",run0,run1,ocdbPath);
33   // take the raw calibration data from the file CalibObjects.root 
34   // and make a OCDB entry with run  validity run0-run1
35   // results are stored at the ocdbPath - local or alien ...
36   // default storage ""- data stored at current working directory 
37  
38 */
39 #include "AliLog.h"
40 #include "Riostream.h"
41 #include <fstream>
42 #include "TFile.h"
43 #include "TCanvas.h"
44 #include "TLegend.h"
45 #include "TH2I.h"
46 #include "TH1I.h"
47 #include "TH2F.h"
48 #include "TH1F.h"
49 #include "TMath.h"
50 #include "THnSparse.h"
51 #include "TProfile2D.h"
52 #include "AliTRDCalDet.h"
53 #include "AliTRDCalPad.h"
54 #include "AliCDBMetaData.h"
55 #include "AliCDBId.h"
56 #include "AliCDBManager.h"
57 #include "AliCDBStorage.h"
58 #include "AliTRDCalibraMode.h"
59 #include "AliTRDCalibraFit.h"
60 #include "AliTRDCalibraVdriftLinearFit.h"
61 #include "AliTRDCalibraExbAltFit.h"
62 #include "AliTRDPreprocessorOffline.h"
63 #include "AliTRDCalChamberStatus.h"
64 #include "AliTRDCalibChamberStatus.h"
65 #include "AliTRDCommonParam.h"
66 #include "AliCDBManager.h"
67 #include "AliCDBEntry.h"
68 #include "AliTRDdEdxBaseUtils.h"
69 #include "AliTRDdEdxCalibHistArray.h"
70 #include "AliTRDdEdxCalibUtils.h"
71
72 ClassImp(AliTRDPreprocessorOffline)
73
74   AliTRDPreprocessorOffline::AliTRDPreprocessorOffline():
75   TNamed("TPCPreprocessorOffline","TPCPreprocessorOffline"),
76   fMethodSecond(kTRUE),
77   fNameList("TRDCalib"),
78   fCalDetGainUsed(0x0),
79   fCalDetVdriftUsed(0x0),
80   fCalDetExBUsed(0x0),
81   fCH2d(0x0),
82   fPH2d(0x0),
83   fPRF2d(0x0),
84   fSparse(0x0),
85   fAliTRDCalibraVdriftLinearFit(0x0),
86   fAliTRDCalibraExbAltFit(0x0),
87   fNEvents(0x0),
88   fAbsoluteGain(0x0),
89   fPlots(new TObjArray(kNumCalibObjs)),
90   fCalibObjects(new TObjArray(kNumCalibObjs)),
91   fFirstRunGainUsed(0),
92   fVersionGainUsed(0),
93   fSubVersionGainUsed(0),
94   fFirstRunVdriftUsed(0),
95   fVersionVdriftUsed(0), 
96   fSubVersionVdriftUsed(0),
97   fFirstRunExBUsed(0),
98   fVersionExBUsed(0), 
99   fSubVersionExBUsed(0),
100   fNoExBUsedInReco(kFALSE),
101   fSwitchOnValidation(kTRUE),
102   fVdriftValidated(kFALSE),
103   fExBValidated(kFALSE),
104   fT0Validated(kFALSE),
105   fMinStatsVdriftT0PH(800*20),
106   fMinStatsVdriftLinear(800),
107   fMinStatsGain(800),
108   fMinStatsPRF(600),
109   fMinStatsChamberStatus(20),
110   fMinSingleStatsChamberStatus(0.05),
111   fBackCorrectGain(kFALSE),  
112   fBackCorrectVdrift(kTRUE),
113   fNotEnoughStatisticsForTheGain(kFALSE),
114   fNotEnoughStatisticsForTheVdriftLinear(kFALSE),
115   fStatusNeg(0),
116   fStatusPos(0),
117   fBadCalibValidate(40),
118   fNoDataValidate(40),
119   fRMSBadCalibratedGain(20.0),
120   fRMSBadCalibratedVdrift(20.0),
121   fRMSBadCalibratedExB(20.0),
122   fRobustFitDriftVelocity(kTRUE),
123   fRobustFitExbAlt(kFALSE),
124   fAlternativeVdrfitFit(kFALSE),
125   fAlternativeExbAltFit(kFALSE),
126   fMinNbOfPointVdriftFit(11),
127   fMethodeGain(0),
128   fOutliersFitChargeLow(0.03),
129   fOutliersFitChargeHigh(0.7),
130   fBeginFitCharge(3.5),
131   fPHQon(kTRUE),
132   fDebugPHQon(kFALSE)
133 {
134   //
135   // default constructor
136   //
137
138   memset(fBadCalib, 0, sizeof(Int_t) * 18);
139   memset(fNoData, 0, sizeof(Int_t) * 18);
140 }
141 //_________________________________________________________________________________________________________________
142 AliTRDPreprocessorOffline::~AliTRDPreprocessorOffline() {
143   //
144   // Destructor
145   //
146
147   if(fCalDetGainUsed) delete fCalDetGainUsed;
148   if(fCalDetVdriftUsed) delete fCalDetVdriftUsed;
149   if(fCalDetExBUsed) delete fCalDetExBUsed;
150   if(fCH2d) delete fCH2d;
151   if(fPH2d) delete fPH2d;
152   if(fPRF2d) delete fPRF2d;
153   if(fSparse) delete fSparse;
154
155   if(IsPHQon()){
156     AliTRDdEdxCalibUtils::DeleteHistArray();
157     AliTRDdEdxCalibUtils::DeleteObjArray();
158   }
159
160   if(fAliTRDCalibraVdriftLinearFit) delete fAliTRDCalibraVdriftLinearFit;
161   if(fAliTRDCalibraExbAltFit) delete fAliTRDCalibraExbAltFit;
162   if(fNEvents) delete fNEvents;
163   if(fAbsoluteGain) delete fAbsoluteGain;
164   if(fPlots) delete fPlots;
165   if(fCalibObjects) delete fCalibObjects;
166   
167 }
168 //___________________________________________________________________________________
169 void AliTRDPreprocessorOffline::Process(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage) 
170 {
171   //
172   // Process to the gain, vdrift, timeoffset, exb and chamber status calibration
173   //
174   
175   if(SetCalDetGain(startRunNumber,fVersionGainUsed,fSubVersionGainUsed) && SetCalDetVdriftExB(startRunNumber,fVersionVdriftUsed,fSubVersionVdriftUsed,fVersionExBUsed,fSubVersionExBUsed)) {
176     
177     CalibVdriftT0(file,startRunNumber,endRunNumber,ocdbStorage);
178     CalibGain(file,startRunNumber,endRunNumber,ocdbStorage);
179     CalibChamberStatus(file,startRunNumber,endRunNumber,ocdbStorage);
180     CalibExbAlt(file,startRunNumber,endRunNumber,ocdbStorage);
181
182   }
183
184   if(IsPHQon()){
185     printf("\n                  AliTRDPreprocessorOffline PHQ on!!\n\n");
186     AliTRDdEdxBaseUtils::PrintControl();
187     CalibPHQ(file, startRunNumber, endRunNumber, ocdbStorage);
188   }
189   else{
190     printf("\n                  AliTRDPreprocessorOffline PHQ off!!\n\n");
191   }
192
193   PrintStatus();
194   
195 }
196 //___________________________________________________________________________________________________________________
197
198 void AliTRDPreprocessorOffline::CalibVdriftT0(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
199   //
200   // make calibration of the drift velocity
201   // Input parameters:
202   //      file                             - the location of input file
203   //      startRunNumber, endRunNumber     - run validity period 
204   //      ocdbStorage                      - path to the OCDB storage
205   //                                       - if empty - local storage 'pwd' uesed
206   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
207   //
208   // 1. Initialization 
209   //
210   fVdriftValidated = kTRUE;
211   fT0Validated = kTRUE;
212   fExBValidated = kTRUE;
213   fNotEnoughStatisticsForTheVdriftLinear = kFALSE;
214   //
215   // 2. extraction of the information
216   //
217   if(ReadVdriftLinearFitGlobal(file) && fCalDetVdriftUsed && fCalDetExBUsed) AnalyzeVdriftLinearFit();
218   if(ReadVdriftT0Global(file)) AnalyzeVdriftT0();
219   //
220   // 3. Append QA plots
221   //
222   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
223   //
224   //
225   // 4. validate OCDB entries
226   //
227   if(fSwitchOnValidation==kTRUE && ValidateVdrift()==kFALSE) { 
228     //AliError("TRD vdrift OCDB parameters out of range!");
229     fVdriftValidated = kFALSE;
230   }
231   if(fSwitchOnValidation==kTRUE && ValidateT0()==kFALSE) { 
232     //AliError("TRD t0 OCDB parameters out of range!");
233     fT0Validated = kFALSE;
234   }
235   if(fSwitchOnValidation==kTRUE && ValidateExB()==kFALSE) { 
236     //AliError("TRD t0 OCDB parameters out of range!");
237     fExBValidated = kFALSE;
238   }
239   //
240   // 5. update of OCDB
241   //
242   //
243   if(fVdriftValidated) UpdateOCDBVdrift(startRunNumber,endRunNumber,ocdbStorage);
244   if(fT0Validated) UpdateOCDBT0(startRunNumber,endRunNumber,ocdbStorage);
245   if(fExBValidated) UpdateOCDBExB(startRunNumber,endRunNumber,ocdbStorage);
246   
247 }
248 //___________________________________________________________________________________________________________________
249
250 void AliTRDPreprocessorOffline::CalibExbAlt(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
251   //
252   // make calibration of the drift velocity
253   // Input parameters:
254   //      file                             - the location of input file
255   //      startRunNumber, endRunNumber     - run validity period 
256   //      ocdbStorage                      - path to the OCDB storage
257   //                                       - if empty - local storage 'pwd' uesed
258   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
259   //
260   // 1. Initialization 
261   //
262
263   //
264   // 2. extraction of the information
265   //
266   if(ReadExbAltFitGlobal(file)) AnalyzeExbAltFit();
267   //
268   // 3. Append QA plots
269   //
270   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
271   //
272   //
273   // 4. validate OCDB entries
274   //
275   //
276   // 5. update of OCDB
277   //
278   //
279   UpdateOCDBExBAlt(startRunNumber,endRunNumber,ocdbStorage);
280   
281 }
282
283 //_________________________________________________________________________________________________________________
284
285 void AliTRDPreprocessorOffline::CalibGain(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
286   //
287   // make calibration of the drift velocity
288   // Input parameters:
289   //      file                             - the location of input file
290   //      startRunNumber, endRunNumber     - run validity period 
291   //      ocdbStorage                      - path to the OCDB storage
292   //                                       - if empty - local storage 'pwd' uesed
293   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
294   //
295   fNotEnoughStatisticsForTheGain = kFALSE;
296   //
297   // 1. Initialization 
298   if(!ReadGainGlobal(file)) return;
299   //
300   //
301   // 2. extraction of the information
302   //
303   AnalyzeGain();
304   if(fBackCorrectGain) CorrectFromDetGainUsed();
305   //if(fBackCorrectVdrift) CorrectFromDetVdriftUsed();
306   //
307   // 3. Append QA plots
308   //
309   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
310   //
311   //
312   // 4. validate OCDB entries
313   //
314   if(fSwitchOnValidation==kTRUE && ValidateGain()==kFALSE) { 
315     //AliError("TRD gain OCDB parameters out of range!");
316     return;
317   }
318   //
319   // 5. update of OCDB
320   //
321   //
322   if((!fCalDetVdriftUsed) || (fCalDetVdriftUsed && fVdriftValidated)) UpdateOCDBGain(startRunNumber,endRunNumber,ocdbStorage);
323   
324   
325 }
326 //________________________________________________________________________________________________________________
327
328 void AliTRDPreprocessorOffline::CalibPRF(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
329   //
330   // make calibration of the drift velocity
331   // Input parameters:
332   //      file                             - the location of input file
333   //      startRunNumber, endRunNumber     - run validity period 
334   //      ocdbStorage                      - path to the OCDB storage
335   //                                       - if empty - local storage 'pwd' uesed
336   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
337   //
338   // 1. Initialization 
339   if(!ReadPRFGlobal(file)) return;
340   //
341   //
342   // 2. extraction of the information
343   //
344   AnalyzePRF();
345   //
346   // 3. Append QA plots
347   //
348   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
349   //
350   //
351   //
352   // 4. validate OCDB entries
353   //
354   if(fSwitchOnValidation==kTRUE && ValidatePRF()==kFALSE) { 
355     //AliError("TRD prf OCDB parameters out of range!");
356     return;
357   }
358   //
359   // 5. update of OCDB
360   //
361   //
362   UpdateOCDBPRF(startRunNumber,endRunNumber,ocdbStorage);
363   
364 }
365 //________________________________________________________________________________________________________________
366 void AliTRDPreprocessorOffline::CalibPHQ(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage)
367 {
368   //
369   // make calibration of puls height Q
370   // Input parameters:
371   //      startRunNumber, endRunNumber     - run validity period 
372   //      ocdbStorage                      - path to the OCDB storage
373   //                                       - if empty - local storage 'pwd' uesed
374   //
375
376   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
377   //printf("test %s\n", ocdbStorage.Data());
378
379   if(!ReadPHQGlobal(file)) return;
380
381   if(!AnalyzePHQ(startRunNumber)) return;
382
383   UpdateOCDBPHQ(startRunNumber,endRunNumber,ocdbStorage);
384 }
385
386 //________________________________________________________________________________________________________________
387
388 void AliTRDPreprocessorOffline::CalibChamberStatus(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
389   //
390   // make calibration of the chamber status
391   // Input parameters:
392   //      startRunNumber, endRunNumber     - run validity period 
393   //      ocdbStorage                      - path to the OCDB storage
394   //                                       - if empty - local storage 'pwd' uesed
395   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
396   //
397   //
398   // 1. Initialization  
399   if(!ReadStatusGlobal(file)) return;
400   //
401   //
402   //
403   // 2. extraction of the information
404   //
405   if(!AnalyzeChamberStatus()) return;
406   //
407   // 3. Append QA plots
408   //
409   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
410   //
411   //
412   //
413   // 4. validate OCDB entries
414   //
415   if(fSwitchOnValidation==kTRUE && ValidateChamberStatus()==kFALSE) { 
416     //AliError("TRD Chamber status OCDB parameters not ok!");
417     return;
418   }
419   //
420   // 5. update of OCDB
421   //
422   //
423   if((!fNotEnoughStatisticsForTheVdriftLinear) && (!fNotEnoughStatisticsForTheGain)) UpdateOCDBChamberStatus(startRunNumber,endRunNumber,ocdbStorage);
424   //UpdateOCDBChamberStatus(startRunNumber,endRunNumber,ocdbStorage);
425   
426 }
427 //______________________________________________________________________________________________________
428 Bool_t AliTRDPreprocessorOffline::Init(const Char_t* fileName){
429   //
430   // read the calibration used during the reconstruction
431   // 
432
433   if(ReadVdriftT0Global(fileName)) {
434     
435     TString nameph = fPH2d->GetTitle();
436     fFirstRunVdriftUsed = GetFirstRun(nameph); 
437     fVersionVdriftUsed = GetVersion(nameph);  
438     fSubVersionVdriftUsed = GetSubVersion(nameph);    
439
440     //printf("Found Version %d, Subversion %d for vdrift\n",fVersionVdriftUsed,fSubVersionVdriftUsed);
441   
442   }
443
444   if(ReadGainGlobal(fileName)) {
445
446     TString namech = fCH2d->GetTitle();
447     fFirstRunGainUsed = GetFirstRun(namech); 
448     fVersionGainUsed = GetVersion(namech);  
449     fSubVersionGainUsed = GetSubVersion(namech);    
450
451     //printf("Found Version %d, Subversion %d for gain\n",fVersionGainUsed,fSubVersionGainUsed);
452
453   }
454   
455   if(ReadVdriftLinearFitGlobal(fileName)) {
456
457     TString namelinear = fAliTRDCalibraVdriftLinearFit->GetNameCalibUsed();
458     fFirstRunExBUsed = GetFirstRun(namelinear); 
459     fVersionExBUsed = GetVersion(namelinear);  
460     fSubVersionExBUsed = GetSubVersion(namelinear);   
461
462     //printf("Found Version %d, Subversion %d, run %d for ExB\n",fVersionExBUsed,fSubVersionExBUsed,fFirstRunExBUsed);
463     
464   }
465    
466   if(fVersionVdriftUsed == 0) fStatusPos = fStatusPos |kVdriftErrorOld;
467   if(fVersionGainUsed == 0) fStatusPos = fStatusPos | kGainErrorOld;
468
469   return kTRUE;
470   
471 }
472 //___________________________________________________________________________________________________________________
473
474 Bool_t AliTRDPreprocessorOffline::ReadStatusGlobal(const Char_t* fileName){
475   //
476   // read calibration entries from file
477   // 
478   if(fSparse) return kTRUE;
479   TFile fcalib(fileName);
480   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
481   if (array){
482     fSparse = (THnSparseI *) array->FindObject("NumberOfEntries");
483     if(!fSparse) return kFALSE;
484   }
485   else 
486     return kFALSE;
487   
488   return kTRUE;
489   
490 }
491 //___________________________________________________________________________________________________________________
492
493 Bool_t AliTRDPreprocessorOffline::ReadPHQGlobal(const Char_t* fileName)
494 {
495   //
496   // read calibration entries from file
497   //
498
499   return AliTRDdEdxCalibUtils::ReadHistArray(fileName, fNameList);
500 }
501
502 //___________________________________________________________________________________________________________________
503
504 Bool_t AliTRDPreprocessorOffline::ReadGainGlobal(const Char_t* fileName){
505   //
506   // read calibration entries from file
507   // 
508   if(fCH2d) return kTRUE;
509   TFile fcalib(fileName);
510   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
511   if (array){
512     TH2I *ch2d = (TH2I *) array->FindObject("CH2d");
513     if(!ch2d) return kFALSE;
514     fCH2d = (TH2I*)ch2d->Clone();
515     //fNEvents = (TH1I *) array->FindObject("NEvents");
516     //fAbsoluteGain = (TH2F *) array->FindObject("AbsoluteGain");
517   }else{
518     TH2I *ch2d = (TH2I *) fcalib.Get("CH2d");
519     if(!ch2d) return kFALSE;
520     fCH2d = (TH2I*)ch2d->Clone();
521     //fNEvents = (TH1I *) fcalib.Get("NEvents");
522     //fAbsoluteGain = (TH2F *) fcalib.Get("AbsoluteGain");
523   }
524   fCH2d->SetDirectory(0);
525   //printf("title of CH2d %s\n",fCH2d->GetTitle());
526
527   return kTRUE;
528   
529 }
530 //_________________________________________________________________________________________________________________
531
532 Bool_t AliTRDPreprocessorOffline::ReadVdriftT0Global(const Char_t* fileName){
533   //
534   // read calibration entries from file
535   // 
536   if(fPH2d) return kTRUE;
537   TFile fcalib(fileName);
538   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
539   if (array){
540     TProfile2D *ph2d = (TProfile2D *) array->FindObject("PH2d");
541     if(!ph2d) return kFALSE;
542     fPH2d = (TProfile2D*)ph2d->Clone();
543     //fNEvents = (TH1I *) array->FindObject("NEvents");
544   }else{
545     TProfile2D *ph2d = (TProfile2D *) fcalib.Get("PH2d");
546     if(!ph2d) return kFALSE;
547     fPH2d = (TProfile2D*)ph2d->Clone();
548     //fNEvents = (TH1I *) fcalib.Get("NEvents");
549   }
550   fPH2d->SetDirectory(0);
551   //printf("title of PH2d %s\n",fPH2d->GetTitle());
552   
553   return kTRUE;
554   
555 }
556 //___________________________________________________________________________________________________________________
557
558 Bool_t AliTRDPreprocessorOffline::ReadVdriftLinearFitGlobal(const Char_t* fileName){
559   //
560   // read calibration entries from file
561   // 
562   if(fAliTRDCalibraVdriftLinearFit) return kTRUE;
563   TFile fcalib(fileName);
564   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
565   if (array){
566     fAliTRDCalibraVdriftLinearFit = (AliTRDCalibraVdriftLinearFit *) array->FindObject("AliTRDCalibraVdriftLinearFit");
567     //fNEvents = (TH1I *) array->FindObject("NEvents");
568   }else{
569     fAliTRDCalibraVdriftLinearFit = (AliTRDCalibraVdriftLinearFit *) fcalib.Get("AliTRDCalibraVdriftLinearFit");
570     //fNEvents = (TH1I *) fcalib.Get("NEvents");
571   }
572   if(!fAliTRDCalibraVdriftLinearFit) {
573     //printf("No AliTRDCalibraVdriftLinearFit\n");
574     return kFALSE;
575   }
576   return kTRUE;
577   
578 }
579 //_____________________________________________________________________________________________________________
580 Bool_t AliTRDPreprocessorOffline::ReadExbAltFitGlobal(const Char_t* fileName){
581   //
582   // read calibration entries from file
583   // 
584   if(fAliTRDCalibraExbAltFit) return kTRUE;
585   TFile fcalib(fileName);
586   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
587   if (array){
588     fAliTRDCalibraExbAltFit = (AliTRDCalibraExbAltFit *) array->FindObject("AliTRDCalibraExbAltFit");
589     //fNEvents = (TH1I *) array->FindObject("NEvents");
590   }else{
591     fAliTRDCalibraExbAltFit = (AliTRDCalibraExbAltFit *) fcalib.Get("AliTRDCalibraExbAltFit");
592     //fNEvents = (TH1I *) fcalib.Get("NEvents");
593   }
594   if(!fAliTRDCalibraExbAltFit) {
595     //printf("No AliTRDCalibraExbAltFit\n");
596     return kFALSE;
597   }
598   return kTRUE;
599   
600 }
601 //_____________________________________________________________________________________________________________
602
603 Bool_t AliTRDPreprocessorOffline::ReadPRFGlobal(const Char_t* fileName){
604   //
605   // read calibration entries from file
606   // 
607   if(fPRF2d) return kTRUE;
608   TFile fcalib(fileName);
609   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
610   if (array){
611     TProfile2D *prf2d = (TProfile2D *) array->FindObject("PRF2d");
612     if(!prf2d) return kFALSE;
613     fPRF2d = (TProfile2D*)prf2d->Clone();
614     //fNEvents = (TH1I *) array->FindObject("NEvents");
615   }else{
616     TProfile2D *prf2d = (TProfile2D *) fcalib.Get("PRF2d");
617     if(!prf2d) return kFALSE;
618     fPRF2d = (TProfile2D*)prf2d->Clone();
619     //fNEvents = (TH1I *) fcalib.Get("NEvents");
620   }
621   fPRF2d->SetDirectory(0);
622   //printf("title of PRF2d %s\n",fPRF2d->GetTitle());
623   
624   return kTRUE;
625
626 }
627 //__________________________________________________________________________________________________________
628
629 Bool_t AliTRDPreprocessorOffline::AnalyzeGain(){
630   //
631   // Analyze gain - produce the calibration objects
632   //
633
634   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
635   calibra->ChooseMethod(fMethodeGain);
636   calibra->SetBeginFitCharge(fBeginFitCharge);
637   calibra->SetFitOutliersChargeLow(fOutliersFitChargeLow);
638   calibra->SetFitOutliersChargeHigh(fOutliersFitChargeHigh);
639   calibra->SetMinEntries(fMinStatsGain); // If there is less than 1000 entries in the histo: no fit
640   calibra->AnalyseCH(fCH2d);
641
642   Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(0))
643     + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(0));
644   Int_t nbfit       = calibra->GetNumberFit();
645   Int_t nbE         = calibra->GetNumberEnt();
646
647
648   Bool_t ok = kFALSE;
649   Bool_t meanother = kFALSE;
650   // enough statistics
651   if ((nbtg >                  0) && 
652       (nbfit        >= 0.5*nbE) && (nbE > 30)) {
653     // create the cal objects
654     if(!fBackCorrectGain) {
655       calibra->PutMeanValueOtherVectorFit(1,kTRUE);
656       meanother = kTRUE;
657     }
658     TObjArray object           = calibra->GetVectorFit();
659     AliTRDCalDet *calDetGain   = calibra->CreateDetObjectGain(&object,meanother);
660     TH1F *coefGain  = calDetGain->MakeHisto1DAsFunctionOfDet();
661     // Put them in the array
662     fCalibObjects->AddAt(calDetGain,kGain);
663     fPlots->AddAt(coefGain,kGain);
664     //
665     ok = kTRUE;
666   }
667   else {
668     fNotEnoughStatisticsForTheGain = kTRUE;
669     Int_t minStatsGain = fMinStatsGain*30;
670     calibra->SetMinEntries(minStatsGain); // Because we do it for all, we increase this
671     Double_t gainoverallnotnormalized =  calibra->AnalyseCHAllTogether(fCH2d);
672     if(fCalDetGainUsed && (gainoverallnotnormalized > 0.0)) {
673       AliTRDCalDet *calDetGain = new AliTRDCalDet(*fCalDetGainUsed);
674       Double_t oldmean = fCalDetGainUsed->CalcMean(kFALSE);
675       //printf("oldmean %f\n",oldmean);
676       if(oldmean > 0.0)  {
677         Double_t scalefactor = calibra->GetScaleFactorGain();
678         //printf("Correction factor %f\n",gainoverallnotnormalized*scalefactor);
679         calDetGain->Multiply(gainoverallnotnormalized*scalefactor/oldmean);
680         //printf("newmean %f\n",calDetGain->CalcMean(kFALSE));
681         TH1F *coefGain  = calDetGain->MakeHisto1DAsFunctionOfDet();
682         fCalibObjects->AddAt(calDetGain,kGain);
683         fPlots->AddAt(coefGain,kGain);
684         // 
685         ok = kTRUE;
686         fStatusNeg = fStatusNeg | kGainNotEnoughStatsButFill;
687       }
688       else {
689         fStatusPos = fStatusPos | kGainErrorOld;
690       }      
691     }
692     else {
693       if(gainoverallnotnormalized <= 0.0) fStatusNeg = fStatusNeg | kGainNotEnoughStatsNotFill;
694       if(!fCalDetGainUsed) fStatusPos = fStatusPos | kGainErrorOld;
695     }
696   }
697   
698   calibra->ResetVectorFit();
699   
700   return ok;
701   
702 }
703 //_____________________________________________________________________________________________________
704 Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftT0(){
705   //
706   // Analyze VdriftT0 - produce the calibration objects
707   //
708
709   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
710   calibra->SetMinEntries(fMinStatsVdriftT0PH); // If there is less than 1000 entries in the histo: no fit
711   calibra->AnalysePH(fPH2d);
712
713   Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
714     + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
715   Int_t nbfit       = calibra->GetNumberFit();
716   Int_t nbfitSuccess = calibra->GetNumberFitSuccess();
717   Int_t nbE         = calibra->GetNumberEnt();
718
719   //printf("nbtg %d, nbfit %d, nbE %d, nbfitSuccess %d\n",nbtg,nbfit,nbE,nbfitSuccess);
720
721   Bool_t ok = kFALSE;
722   if ((nbtg >                  0) && 
723       (nbfit        >= 0.5*nbE) && (nbE > 30) && (nbfitSuccess > 30)) {
724     //printf("Pass the cut for VdriftT0\n");
725     // create the cal objects
726     calibra->RemoveOutliers(1,kFALSE);
727     calibra->PutMeanValueOtherVectorFit(1,kFALSE);
728     calibra->RemoveOutliers2(kFALSE);
729     calibra->PutMeanValueOtherVectorFit2(1,kFALSE);
730     //
731     TObjArray object = calibra->GetVectorFit();
732     AliTRDCalDet *calDetVdrift = calibra->CreateDetObjectVdrift(&object);
733     TH1F *coefVdriftPH  = calDetVdrift->MakeHisto1DAsFunctionOfDet();
734     AliTRDCalPad *calPadVdrift = (AliTRDCalPad *)calibra->CreatePadObjectVdrift(&object,calDetVdrift);
735     TH1F *coefPadVdrift   = calPadVdrift->MakeHisto1D();
736     object       = calibra->GetVectorFit2();
737     AliTRDCalDet *calDetT0  = calibra->CreateDetObjectT0(&object);
738     TH1F *coefT0  = calDetT0->MakeHisto1DAsFunctionOfDet();
739     AliTRDCalPad *calPadT0 = (AliTRDCalPad *)calibra->CreatePadObjectT0(&object,calDetT0);
740     TH1F *coefPadT0  = calPadT0->MakeHisto1D();
741     // Put them in the array
742     fCalibObjects->AddAt(calDetT0,kT0PHDet);
743     fCalibObjects->AddAt(calDetVdrift,kVdriftPHDet);
744     fCalibObjects->AddAt(calPadT0,kT0PHPad);
745     fCalibObjects->AddAt(calPadVdrift,kVdriftPHPad);
746     fPlots->AddAt(coefVdriftPH,kVdriftPHDet);
747     fPlots->AddAt(coefT0,kT0PHDet);
748     fPlots->AddAt(coefPadVdrift,kVdriftPHPad);
749     fPlots->AddAt(coefPadT0,kT0PHPad);
750     //
751     ok = kTRUE;
752   }
753   else {
754     //printf("Not enough stats timeoffset\n");
755     fStatusNeg = fStatusNeg | kTimeOffsetNotEnoughStatsNotFill;
756   }
757   calibra->ResetVectorFit();
758  
759   return ok;
760   
761 }
762 //____________________________________________________________________________________________________________________
763 Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftLinearFit(){
764   //
765   // Analyze vdrift linear fit - produce the calibration objects
766   //
767
768   //printf("Analyse linear fit\n");
769
770   
771   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
772   calibra->SetCalDetVdriftExB(fCalDetVdriftUsed,fCalDetExBUsed);
773   calibra->SetMinEntries(fMinStatsVdriftLinear); // If there is less than 1000 entries in the histo: no fit
774   printf("The mean stat is by %d for VdriftLinear\n",fMinStatsVdriftLinear);
775   //printf("Fill PE Array\n");
776   fAliTRDCalibraVdriftLinearFit->SetRobustFit(fRobustFitDriftVelocity);
777   fAliTRDCalibraVdriftLinearFit->SetMinNumberOfPointsForFit(fMinNbOfPointVdriftFit);
778   if(!fAlternativeVdrfitFit)
779     fAliTRDCalibraVdriftLinearFit->FillPEArray();
780   else
781     fAliTRDCalibraVdriftLinearFit->FillPEArray2();
782   //printf("AliTRDCalibraFit\n");
783   calibra->AnalyseLinearFitters(fAliTRDCalibraVdriftLinearFit);
784   //printf("After\n");
785
786   //Int_t nbtg        = 540;
787   Int_t nbfit       = calibra->GetNumberFit();
788   Int_t nbE         = calibra->GetNumberEnt();
789
790   
791   Bool_t ok = kFALSE;
792   // enough statistics
793   if ((nbfit        >= 0.5*nbE) && (nbE > 30)) {
794     // create the cal objects
795     //calibra->RemoveOutliers(1,kTRUE);
796     calibra->PutMeanValueOtherVectorFit(1,kTRUE);
797     //calibra->RemoveOutliers2(kTRUE);
798     calibra->PutMeanValueOtherVectorFit2(1,kTRUE);
799     //
800     TObjArray object  = calibra->GetVectorFit();
801     AliTRDCalDet *calDetVdrift = calibra->CreateDetObjectVdrift(&object,kTRUE);
802     TH1F *coefDriftLinear      = calDetVdrift->MakeHisto1DAsFunctionOfDet();
803     object                     = calibra->GetVectorFit2();
804     AliTRDCalDet *calDetLorentz = calibra->CreateDetObjectLorentzAngle(&object);
805     TH1F *coefLorentzAngle = calDetLorentz->MakeHisto1DAsFunctionOfDet();
806     //if(!calDetLorentz) printf("No lorentz created\n");
807     // Put them in the array
808     fCalibObjects->AddAt(calDetVdrift,kVdriftLinear);
809     fCalibObjects->AddAt(calDetLorentz,kLorentzLinear);
810     fPlots->AddAt(coefDriftLinear,kVdriftLinear);
811     fPlots->AddAt(coefLorentzAngle,kLorentzLinear);
812     //
813     ok = kTRUE;
814   }
815   else {
816     fNotEnoughStatisticsForTheVdriftLinear = kTRUE;
817     Int_t minNumberOfEntriesForAll = fMinStatsVdriftLinear*30;
818     calibra->SetMinEntries(minNumberOfEntriesForAll); // Because we do it for all, we increase this
819     Double_t vdriftoverall = -100.0;
820     Double_t exboverall = 100.0;
821     calibra->AnalyseLinearFittersAllTogether(fAliTRDCalibraVdriftLinearFit,vdriftoverall,exboverall);
822     //printf("Found mean vdrift %f and exb %f\n",vdriftoverall,exboverall);
823     if(fCalDetVdriftUsed && (vdriftoverall > 0.0) && (exboverall < 70.0)) {
824       AliTRDCalDet *calDetVdrift = new AliTRDCalDet(*fCalDetVdriftUsed);
825       AliTRDCalDet *calDetLorentz = new AliTRDCalDet(*fCalDetExBUsed);
826       Double_t oldmeanvdrift = fCalDetVdriftUsed->CalcMean(kFALSE);
827       Double_t oldmeanexb = fCalDetExBUsed->CalcMean(kFALSE);
828       //printf("oldmean %f\n",oldmean);
829       if((oldmeanvdrift > 0.0) && (oldmeanexb < 70.0))  {
830         //printf("Correction factor %f\n",vdriftoverall);
831         calDetVdrift->Multiply(vdriftoverall/oldmeanvdrift);
832         if(TMath::Abs(oldmeanexb) > 0.0001) calDetLorentz->Multiply(exboverall/oldmeanexb);
833         //printf("newmean %f\n",calDetVdrift->CalcMean(kFALSE));
834         TH1F *coefDriftLinear  = calDetVdrift->MakeHisto1DAsFunctionOfDet();
835         TH1F *coefLorentzAngle = calDetLorentz->MakeHisto1DAsFunctionOfDet();
836         // Put them in the array
837         fCalibObjects->AddAt(calDetVdrift,kVdriftLinear);
838         fCalibObjects->AddAt(calDetLorentz,kLorentzLinear);
839         fPlots->AddAt(coefDriftLinear,kVdriftLinear);
840         fPlots->AddAt(coefLorentzAngle,kLorentzLinear);
841         // 
842         ok = kTRUE;
843         fStatusNeg = fStatusNeg | kVdriftNotEnoughStatsButFill;
844       }
845       else {
846         if(oldmeanvdrift) fStatusPos = fStatusPos | kVdriftErrorOld;
847         if(oldmeanexb) fStatusPos = fStatusPos | kExBErrorOld;
848       }      
849     }
850     else {
851       if((vdriftoverall <= 0.0) && (exboverall > 70.0)) fStatusNeg = fStatusNeg | kVdriftNotEnoughStatsNotFill;
852       if(!fCalDetVdriftUsed) fStatusPos = fStatusPos | kVdriftErrorOld;
853       if(!fCalDetExBUsed) fStatusPos = fStatusPos | kExBErrorOld;
854     }
855   }
856   
857   calibra->ResetVectorFit();
858   
859   return ok;
860   
861 }
862 //________________________________________________________________________________________________________________
863
864 Bool_t AliTRDPreprocessorOffline::AnalyzeExbAltFit(){
865   //
866   // Analyze vdrift linear fit - produce the calibration objects
867   //
868
869   //printf("Analyse linear fit\n");
870
871   
872   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
873   calibra->SetMinEntries(fMinStatsVdriftLinear); // If there is less than 1000 entries in the histo: no fit
874   //printf("Fill PE Array\n");
875   fAliTRDCalibraExbAltFit->SetRobustFit(fRobustFitExbAlt);
876   if(!fAlternativeExbAltFit)
877     fAliTRDCalibraExbAltFit->FillPEArray();
878   else
879     fAliTRDCalibraExbAltFit->FillPEArray2();
880   //printf("AliTRDCalibraFit\n");
881   calibra->AnalyseExbAltFit(fAliTRDCalibraExbAltFit);
882   //printf("After\n");
883
884   //Int_t nbtg        = 540;
885   Int_t nbfit       = calibra->GetNumberFit();
886   Int_t nbE         = calibra->GetNumberEnt();
887
888   
889   Bool_t ok = kFALSE;
890   // enough statistics
891   if ((nbfit        >= 0.5*nbE) && (nbE > 30)) {
892     // create the cal objects
893     //calibra->RemoveOutliers(1,kTRUE);
894     calibra->PutMeanValueOtherVectorFit2(1,kTRUE);
895     //
896     TObjArray object  = calibra->GetVectorFit2();
897     AliTRDCalDet *calDetLorentz = calibra->CreateDetObjectExbAlt(&object);
898     TH1F *coefLorentzAngle = calDetLorentz->MakeHisto1DAsFunctionOfDet();
899     //if(!calDetLorentz) printf("No lorentz created\n");
900     // Put them in the array
901     fCalibObjects->AddAt(calDetLorentz,kExbAlt);
902     fPlots->AddAt(coefLorentzAngle,kExbAlt);
903     //
904     ok = kTRUE;
905   }
906   
907   calibra->ResetVectorFit();
908   
909   return ok;
910   
911 }
912 //________________________________________________________________________________________________________________
913
914 Bool_t AliTRDPreprocessorOffline::AnalyzePRF(){
915   //
916   // Analyze PRF - produce the calibration objects
917   //
918
919   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
920   calibra->SetMinEntries(fMinStatsPRF); // If there is less than 1000 entries in the histo: no fit
921   calibra->AnalysePRFMarianFit(fPRF2d);
922
923   Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(2))
924     + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(2));
925   Int_t nbfit       = calibra->GetNumberFit();
926   Int_t nbE         = calibra->GetNumberEnt();
927
928   
929   Bool_t ok = kFALSE;
930   // enough statistics
931   if ((nbtg >                  0) && 
932       (nbfit        >= 0.95*nbE) && (nbE > 30)) {
933     // create the cal objects
934     TObjArray object  = calibra->GetVectorFit();
935     AliTRDCalPad *calPadPRF = (AliTRDCalPad*) calibra->CreatePadObjectPRF(&object);
936     TH1F *coefPRF           = calPadPRF->MakeHisto1D();
937     // Put them in the array
938     fCalibObjects->AddAt(calPadPRF,kPRF);
939     fPlots->AddAt(coefPRF,kPRF);
940     //
941     ok = kTRUE;
942   }
943   
944   calibra->ResetVectorFit();
945   
946   return ok;
947   
948 }
949
950 //_____________________________________________________________________________
951 Bool_t AliTRDPreprocessorOffline::AnalyzePHQ(Int_t startRunNumber)
952 {
953   //
954   //Produce PHQ calibration results
955   //
956   TList *lout = 0x0;
957   TTreeSRedirector *calibStream = 0x0;
958   if(IsDebugPHQon()){
959     lout = new TList;
960     lout->SetOwner();
961
962     calibStream = new TTreeSRedirector(Form("TRDCalibStream_%010d.root", startRunNumber));
963   }
964
965   for(Int_t iter=0; iter<AliTRDdEdxCalibUtils::GetHistArray()->GetSize(); iter++){
966     THnBase *hi = (THnBase*) AliTRDdEdxCalibUtils::GetHistAt(iter);
967     TObjArray *obji = AliTRDdEdxCalibUtils::HistToObj(hi, startRunNumber, lout, calibStream);
968     //printf("test analyze %s\n", obji->GetName());
969     AliTRDdEdxCalibUtils::GetObjArray()->AddAt(obji, iter);
970   }
971
972   fCalibObjects->AddAt(AliTRDdEdxCalibUtils::GetObjArray(), kPHQ);
973
974   if(lout){
975     TFile *fout=new TFile(Form("TRDCalibList_%010d.root", startRunNumber),"recreate");
976     fout->cd();
977     lout->Write();
978     fout->Save();
979     fout->Close();
980     delete fout;
981   }
982   delete calibStream;
983   delete lout;
984
985   return kTRUE;
986 }
987
988 //_____________________________________________________________________________
989 Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
990 {
991   //
992   // Produce AliTRDCalChamberStatus out of calibration results
993   //
994   
995   // set up AliTRDCalibChamberStatus
996   AliTRDCalibChamberStatus *chamberStatus = new AliTRDCalibChamberStatus();
997   chamberStatus->SetSparseI(fSparse);
998   chamberStatus->AnalyseHisto(fMinStatsChamberStatus, fMinSingleStatsChamberStatus);
999   // get AliTRDCalChamberStatus
1000   AliTRDCalChamberStatus *calChamberStatus = chamberStatus->GetCalChamberStatus();
1001
1002   // get calibration objects
1003   AliTRDCalDet *calDetGain   = (AliTRDCalDet *) fCalibObjects->At(kGain);
1004   AliTRDCalDet *calDetVDrift = (AliTRDCalDet *) fCalibObjects->At(kVdriftLinear);
1005   AliTRDCalDet *calDetExB    = (AliTRDCalDet *) fCalibObjects->At(kLorentzLinear);
1006
1007   // Check
1008   if((!calDetGain) || (!calDetVDrift) || (!fCH2d) || (!calDetExB) || (!calChamberStatus)) return kFALSE;
1009
1010   // Gain
1011   Double_t gainmean   = calDetGain->GetMean();
1012   Double_t vdriftmean = calDetVDrift->GetMean();
1013   Double_t exbmean    = calDetExB->GetMean();
1014
1015   Double_t gainrms    = calDetGain->GetRMSRobust();
1016   Double_t vdriftrms  = calDetVDrift->GetRMSRobust();
1017   Double_t exbrms     = calDetExB->GetRMSRobust();
1018
1019   //printf("Gain mean: %f, rms: %f\n",gainmean,gainrms);
1020   //printf("Vdrift mean: %f, rms: %f\n",vdriftmean,vdriftrms);
1021   //printf("ExB mean: %f, rms: %f\n",exbmean,exbrms);
1022
1023   // Check
1024   if((TMath::Abs(gainrms) < 0.001) || (TMath::Abs(vdriftrms) < 0.001) || (TMath::Abs(exbrms) < 0.0000001)) return kFALSE;
1025
1026   // mask chambers with empty gain entries
1027   //Int_t counter = 0;
1028   for (Int_t idet = 0; idet < 540; idet++) {
1029
1030     // ch2d
1031     TH1I *projch =  (TH1I *) fCH2d->ProjectionX("projch",idet+1,idet+1,(Option_t *)"e");
1032     Double_t entries = projch->GetEntries();
1033     //printf("Number of entries %f for det %d\n",entries,idet);
1034
1035     // gain
1036     Double_t gain = calDetGain->GetValue(idet);
1037
1038     // vdrift
1039     Double_t vdrift = calDetVDrift->GetValue(idet);
1040
1041     // exb
1042     Double_t exb = calDetExB->GetValue(idet);
1043
1044
1045     if( (entries<50 && !calChamberStatus->IsNoData(idet))  ||
1046         TMath::Abs(gainmean-gain) > (fRMSBadCalibratedGain*gainrms)          ||
1047         TMath::Abs(vdriftmean-vdrift) > (fRMSBadCalibratedVdrift*vdriftrms)    ||
1048         TMath::Abs(exbmean-exb) > (fRMSBadCalibratedExB*exbrms) ) {
1049      
1050       //printf(" chamber det %03d masked \n",idet);
1051       //printf(" gainmean %f and gain %f, gainrms %f \n",gainmean,gain,gainrms);
1052       //printf(" vdriftmean %f and vdrift %f, vdriftrms %f \n",vdriftmean,vdrift,vdriftrms);
1053       //printf(" exbmean %f and exb %f, exbrms %f \n",exbmean,exb,exbrms);
1054       
1055       calChamberStatus->SetStatus(idet,AliTRDCalChamberStatus::kBadCalibrated);
1056       //counter++;
1057     }
1058
1059     delete projch;
1060     
1061    }
1062
1063   // Security
1064   for(Int_t sm=0; sm < 18; sm++) {
1065     Int_t smnodata   = 0;
1066     Int_t smbadcalib = 0;
1067     for(Int_t det = 0; det < 30; det++){
1068       Int_t detector = sm*30+det;
1069       if(calChamberStatus->IsNoData(detector)) smnodata++;
1070       else {
1071         if(calChamberStatus->IsBadCalibrated(detector)) smbadcalib++;
1072       }
1073     }
1074     fNoData[sm]  = smnodata;
1075     fBadCalib[sm]= smbadcalib;
1076     //printf("No Data %d, bad calibrated %d for %d\n",fNoData[sm],fBadCalib[sm],sm);
1077   }
1078   
1079   // Security
1080   //   for(Int_t sm=0; sm < 18; sm++) {
1081   //     Int_t counter = 0;
1082   //     for(Int_t det = 0; det < 30; det++){
1083   //       Int_t detector = sm*30+det;
1084   //       if(calChamberStatus->IsBadCalibrated(detector)) counter++;
1085   //     }
1086   //     if(counter >= 20) {
1087   //       for(Int_t det = 0; det < 30; det++){
1088   //    Int_t detector = sm*30+det;
1089   //    calChamberStatus->SetStatus(detector,AliTRDCalChamberStatus::kGood);
1090   //       }
1091   //     }
1092   //  }
1093
1094    fCalibObjects->AddAt(calChamberStatus,kChamberStatus);
1095    return kTRUE;
1096
1097  }
1098
1099
1100  //________________________________________________________________________________________________
1101  void AliTRDPreprocessorOffline::CorrectFromDetGainUsed() {
1102    //
1103    // Correct from the gas gain used afterwards
1104    //
1105    AliTRDCalDet *calDetGain = (AliTRDCalDet *) fCalibObjects->At(kGain);
1106    if(!calDetGain) return;
1107
1108    // Calculate mean
1109    Double_t mean = 0.0;
1110    Int_t nbdet = 0;
1111
1112    for(Int_t det = 0; det < 540; det++) {
1113
1114      Float_t gaininit = fCalDetGainUsed->GetValue(det);
1115      Float_t gainout = calDetGain->GetValue(det);
1116
1117
1118      if(TMath::Abs(gainout-1.0) > 0.000001) {
1119        mean += (gaininit*gainout);
1120        nbdet++;
1121      }  
1122    }
1123    if(nbdet > 0) mean = mean/nbdet;
1124
1125    for(Int_t det = 0; det < 540; det++) {
1126
1127      Float_t gaininit = fCalDetGainUsed->GetValue(det);
1128      Float_t gainout = calDetGain->GetValue(det);
1129
1130      if(TMath::Abs(gainout-1.0) > 0.000001) {
1131        Double_t newgain = gaininit*gainout;
1132        if(newgain < 0.1) newgain = 0.1;
1133        if(newgain > 1.9) newgain = 1.9;
1134        calDetGain->SetValue(det,newgain);
1135      }
1136      else {
1137        Double_t newgain = mean;
1138        if(newgain < 0.1) newgain = 0.1;
1139        if(newgain > 1.9) newgain = 1.9;
1140        calDetGain->SetValue(det,newgain);
1141      }
1142    }
1143
1144
1145  }
1146  //________________________________________________________________________________________________
1147  void AliTRDPreprocessorOffline::CorrectFromDetVdriftUsed() {
1148    //
1149    // Correct from the drift velocity
1150    //
1151
1152    //printf("Correct for vdrift\n");
1153
1154    AliTRDCalDet *calDetGain = (AliTRDCalDet *) fCalibObjects->At(kGain);
1155    if(!calDetGain) return;
1156
1157    Int_t detVdrift = kVdriftPHDet;
1158    if(fMethodSecond) detVdrift = kVdriftLinear;
1159    AliTRDCalDet *calDetVdrift = (AliTRDCalDet *) fCalibObjects->At(detVdrift);
1160    if(!calDetVdrift) return;
1161
1162    // Calculate mean
1163    if(!fNotEnoughStatisticsForTheVdriftLinear) {
1164      for(Int_t det = 0; det < 540; det++) {
1165        
1166        Float_t vdriftinit = fCalDetVdriftUsed->GetValue(det);
1167        Float_t vdriftout = calDetVdrift->GetValue(det);
1168        
1169        Float_t gain = calDetGain->GetValue(det);
1170        if(vdriftout > 0.0) gain = gain*vdriftinit/vdriftout;
1171        if(gain < 0.1) gain = 0.1;
1172        if(gain > 1.9) gain = 1.9;
1173        calDetGain->SetValue(det,gain);
1174      }
1175    }
1176    else {
1177      
1178      Float_t vdriftinit = fCalDetVdriftUsed->CalcMean(kFALSE);
1179      Float_t vdriftout = calDetVdrift->CalcMean(kFALSE);
1180      Float_t factorcorrectif = 1.0;
1181      if(vdriftout > 0.0) factorcorrectif = vdriftinit/vdriftout;
1182      for(Int_t det = 0; det < 540; det++) {
1183        Float_t gain = calDetGain->GetValue(det);
1184        gain = gain*factorcorrectif;
1185        if(gain < 0.1) gain = 0.1;
1186        if(gain > 1.9) gain = 1.9;
1187        calDetGain->SetValue(det,gain);
1188      }
1189      
1190    }
1191    
1192  }
1193 //_________________________________________________________________________________________________________________
1194  void AliTRDPreprocessorOffline::UpdateOCDBGain(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
1195    //
1196    // Update OCDB entry
1197    //
1198
1199    AliCDBMetaData *metaData= new AliCDBMetaData();
1200    metaData->SetObjectClassName("AliTRDCalDet");
1201    metaData->SetResponsible("Raphaelle Bailhache");
1202    metaData->AddDateToComment();
1203    metaData->SetBeamPeriod(1);
1204
1205    AliCDBId id1("TRD/Calib/ChamberGainFactor", startRunNumber, endRunNumber);
1206    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1207    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kGain);
1208    if(calDet) gStorage->Put(calDet, id1, metaData);
1209
1210
1211  }
1212  //___________________________________________________________________________________________________________________
1213  void AliTRDPreprocessorOffline::UpdateOCDBExB(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
1214    //
1215    // Update OCDB entry
1216    //
1217
1218    Int_t detExB = kLorentzLinear;
1219    if(!fMethodSecond) return;
1220
1221    //printf("Pass\n");
1222
1223    AliCDBMetaData *metaData= new AliCDBMetaData();
1224    metaData->SetObjectClassName("AliTRDCalDet");
1225    metaData->SetResponsible("Raphaelle Bailhache");
1226    metaData->AddDateToComment();
1227    metaData->SetBeamPeriod(1);
1228
1229    AliCDBId id1("TRD/Calib/ChamberExB", startRunNumber, endRunNumber);
1230    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1231    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detExB);
1232    if(calDet) gStorage->Put(calDet, id1, metaData);
1233    //if(!calDet) printf("No caldet\n");
1234
1235  }
1236 //___________________________________________________________________________________________________________________
1237 void AliTRDPreprocessorOffline::UpdateOCDBExBAlt(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
1238   //
1239   // Update OCDB entry
1240   //
1241
1242   Int_t detExB = kExbAlt;
1243   if(!fMethodSecond) return;
1244
1245   //printf("Pass\n");
1246
1247   AliCDBMetaData *metaData= new AliCDBMetaData();
1248   metaData->SetObjectClassName("AliTRDCalDet");
1249   metaData->SetResponsible("Theo Rascanu");
1250   metaData->AddDateToComment();
1251   metaData->SetBeamPeriod(1);
1252
1253   AliCDBId id1("TRD/Calib/ChamberExBAlt", startRunNumber, endRunNumber);
1254   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1255   AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detExB);
1256   if(calDet) gStorage->Put(calDet, id1, metaData);
1257   //if(!calDet) printf("No caldet\n");
1258
1259 }
1260  //___________________________________________________________________________________________________________________
1261  void AliTRDPreprocessorOffline::UpdateOCDBVdrift(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
1262    //
1263    // Update OCDB entry
1264    //
1265
1266    Int_t detVdrift = kVdriftPHDet;
1267
1268    if(fMethodSecond) detVdrift = kVdriftLinear;
1269
1270    AliCDBMetaData *metaData= new AliCDBMetaData();
1271    metaData->SetObjectClassName("AliTRDCalDet");
1272    metaData->SetResponsible("Raphaelle Bailhache");
1273    metaData->AddDateToComment();
1274    metaData->SetBeamPeriod(1);
1275
1276    AliCDBId id1("TRD/Calib/ChamberVdrift", startRunNumber, endRunNumber);
1277    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1278    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detVdrift);
1279    if(calDet) gStorage->Put(calDet, id1, metaData);
1280
1281    //
1282
1283    if(!fMethodSecond) {
1284
1285      AliCDBMetaData *metaDataPad= new AliCDBMetaData();
1286      metaDataPad->SetObjectClassName("AliTRDCalPad");
1287      metaDataPad->SetResponsible("Raphaelle Bailhache");
1288      metaDataPad->AddDateToComment();
1289      metaDataPad->SetBeamPeriod(1);
1290
1291      AliCDBId id1Pad("TRD/Calib/LocalVdrift", startRunNumber, endRunNumber);
1292      AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kVdriftPHPad);
1293      if(calPad) gStorage->Put(calPad, id1Pad, metaDataPad);
1294
1295    }
1296
1297  }
1298  //________________________________________________________________________________________________________________________
1299  void AliTRDPreprocessorOffline::UpdateOCDBT0(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
1300    //
1301    // Update OCDB entry
1302    //
1303
1304    AliCDBMetaData *metaData= new AliCDBMetaData();
1305    metaData->SetObjectClassName("AliTRDCalDet");
1306    metaData->SetResponsible("Raphaelle Bailhache");
1307    metaData->AddDateToComment();
1308    metaData->SetBeamPeriod(1);
1309
1310    AliCDBId id1("TRD/Calib/ChamberT0", startRunNumber, endRunNumber);
1311    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1312    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kT0PHDet);
1313    if(calDet) gStorage->Put(calDet, id1, metaData);
1314
1315    //
1316
1317    AliCDBMetaData *metaDataPad= new AliCDBMetaData();
1318    metaDataPad->SetObjectClassName("AliTRDCalPad");
1319    metaDataPad->SetResponsible("Raphaelle Bailhache");
1320    metaDataPad->AddDateToComment();
1321    metaDataPad->SetBeamPeriod(1);
1322
1323    AliCDBId id1Pad("TRD/Calib/LocalT0", startRunNumber, endRunNumber);
1324    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kT0PHPad);
1325    if(calPad) gStorage->Put(calPad, id1Pad, metaDataPad);
1326
1327
1328
1329  }
1330  //_________________________________________________________________________________________________________________
1331  void AliTRDPreprocessorOffline::UpdateOCDBPRF(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
1332    //
1333    // Update OCDB entry
1334    //
1335
1336    AliCDBMetaData *metaData= new AliCDBMetaData();
1337    metaData->SetObjectClassName("AliTRDCalPad");
1338    metaData->SetResponsible("Raphaelle Bailhache");
1339    metaData->AddDateToComment();
1340    metaData->SetBeamPeriod(1);
1341
1342
1343    AliCDBId id1("TRD/Calib/PRFWidth", startRunNumber, endRunNumber);
1344    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1345    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kPRF);
1346    if(calPad) gStorage->Put(calPad, id1, metaData);
1347
1348
1349  }
1350 //_________________________________________________________________________________________________________________
1351 void AliTRDPreprocessorOffline::UpdateOCDBPHQ(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath)
1352 {
1353   //
1354   // Update OCDB entry
1355   //
1356   AliCDBMetaData *metaData= new AliCDBMetaData();
1357   metaData->SetObjectClassName("TObjArray");
1358   metaData->SetResponsible("Raphaelle Bailhache and Xianguo Lu");
1359   metaData->AddDateToComment();
1360   metaData->SetBeamPeriod(1);
1361
1362   AliCDBId id1("TRD/Calib/PHQ", startRunNumber, endRunNumber);
1363   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1364   TObjArray *cobj = (TObjArray *) fCalibObjects->At(kPHQ);
1365   if(cobj){
1366     //cobj->Print();
1367     gStorage->Put(cobj, id1, metaData);
1368   }
1369 }
1370
1371  //_________________________________________________________________________________________________________________
1372  void AliTRDPreprocessorOffline::UpdateOCDBChamberStatus(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
1373    //
1374    // Update OCDB entry
1375    //
1376
1377    AliCDBMetaData *metaData= new AliCDBMetaData();
1378    metaData->SetObjectClassName("AliTRDCalChamberStatus");
1379    metaData->SetResponsible("Raphaelle Bailhache and Julian Book");
1380    metaData->AddDateToComment();
1381    metaData->SetBeamPeriod(1);
1382
1383    AliCDBId id1("TRD/Calib/ChamberStatus", startRunNumber, endRunNumber);
1384    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
1385    AliTRDCalChamberStatus *calChamberStatus = (AliTRDCalChamberStatus *) fCalibObjects->At(kChamberStatus);
1386    if(calChamberStatus) gStorage->Put(calChamberStatus, id1, metaData);
1387
1388
1389  }
1390  //__________________________________________________________________________________________________________________________
1391  Bool_t AliTRDPreprocessorOffline::ValidateGain() {
1392    //
1393    // Validate OCDB entry
1394    //
1395
1396    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kGain);
1397    if(calDet) {
1398      Double_t mean = calDet->GetMean();
1399      Double_t rms = calDet->GetRMSRobust();
1400      if((mean > 0.2) && (mean < 1.4) && (rms < 0.5)) return kTRUE;
1401      //if((mean > 0.2) && (mean < 1.4)) return kTRUE;
1402      else {
1403        fStatusPos = fStatusPos | kGainErrorRange;
1404        return kFALSE;
1405      }
1406    }
1407    else return kFALSE;
1408    
1409
1410
1411  }
1412  //__________________________________________________________________________________________________________________________
1413  Bool_t AliTRDPreprocessorOffline::ValidateVdrift(){
1414    //
1415    // Update OCDB entry
1416    //
1417
1418    Int_t detVdrift = kVdriftPHDet;
1419    Bool_t ok = kTRUE;
1420
1421    if(fMethodSecond) detVdrift = kVdriftLinear;
1422
1423    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detVdrift);
1424    if(calDet) {
1425      Double_t mean = calDet->GetMean();
1426      Double_t rms = calDet->GetRMSRobust();
1427      //printf("Vdrift::mean %f, rms %f\n",mean,rms);
1428      if(!((mean > 1.0) && (mean < 2.0) && (rms < 0.5))) {
1429        fStatusPos = fStatusPos | kVdriftErrorRange;
1430        ok = kFALSE;
1431      }
1432    }
1433    else return kFALSE; 
1434
1435    if(!fMethodSecond) {
1436      AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kVdriftPHPad);
1437      if(calPad) {
1438        Double_t mean = calPad->GetMean();
1439        Double_t rms = calPad->GetRMS();
1440        //printf("Vdrift::meanpad %f, rmspad %f\n",mean,rms);
1441        if(!((mean > 0.9) && (mean < 1.1) && (rms < 0.6))) {
1442          fStatusPos = fStatusPos | kVdriftErrorRange;
1443          ok = kFALSE;
1444        }
1445      }
1446      else return kFALSE;
1447    }
1448
1449    return ok;
1450
1451  }
1452  //__________________________________________________________________________________________________________________________
1453  Bool_t AliTRDPreprocessorOffline::ValidateExB(){
1454    //
1455    // Update OCDB entry
1456    //
1457
1458    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kLorentzLinear);
1459    if(calDet) {
1460      Double_t mean = calDet->GetMean();
1461      Double_t rms = calDet->GetRMSRobust();
1462      //printf("Vdrift::mean %f, rms %f\n",mean,rms);
1463      if(!((mean > -1.0) && (mean < 1.0) && (rms < 0.5))) {
1464        fStatusNeg = fStatusNeg | kExBErrorRange;
1465        return kFALSE;
1466      }
1467      else return kTRUE;
1468    }
1469    else return kFALSE; 
1470    
1471  }
1472  //__________________________________________________________________________________________________________________________
1473  Bool_t AliTRDPreprocessorOffline::ValidateT0(){
1474    //
1475    // Update OCDB entry
1476    //
1477
1478    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kT0PHDet);
1479    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kT0PHPad);
1480    if(calDet && calPad) {
1481      Double_t meandet = calDet->GetMean();
1482      Double_t rmsdet = calDet->GetRMSRobust();
1483      Double_t meanpad = calPad->GetMean();
1484      //Double_t rmspad = calPad->GetRMS();
1485      //printf("T0::minimum %f, rmsdet %f,meanpad %f, rmspad %f\n",meandet,rmsdet,meanpad,rmspad);
1486      if((meandet > -1.5) && (meandet < 5.0) && (rmsdet < 4.0) && (meanpad < 5.0) && (meanpad > -0.5)) return kTRUE;
1487      else {
1488        fStatusPos = fStatusPos | kTimeOffsetErrorRange;
1489        return kFALSE;
1490      }
1491    }
1492    else return kFALSE;
1493
1494  }
1495  //__________________________________________________________________________________________________________________________
1496  Bool_t AliTRDPreprocessorOffline::ValidatePRF() const{
1497    //
1498    // Update OCDB entry
1499    //
1500
1501    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kPRF);
1502    if(calPad) {
1503      Double_t meanpad = calPad->GetMean();
1504      Double_t rmspad = calPad->GetRMS();
1505      //printf("PRF::meanpad %f, rmspad %f\n",meanpad,rmspad);
1506      if((meanpad < 1.0) && (rmspad < 0.8)) return kTRUE;
1507      else return kFALSE;
1508    }
1509    else return kFALSE;
1510
1511
1512  }
1513  //__________________________________________________________________________________________________________________________
1514 Bool_t AliTRDPreprocessorOffline::ValidateChamberStatus(){
1515   //
1516   // Update OCDB entry
1517   //
1518   
1519   AliTRDCalChamberStatus *calChamberStatus = (AliTRDCalChamberStatus *) fCalibObjects->At(kChamberStatus);
1520   if(calChamberStatus) {
1521
1522     Int_t detectornodata   = 0;
1523     Int_t detectorbadcalib = 0;
1524     
1525     for(Int_t sm=0; sm < 18; sm++) {
1526       //printf("%d chambers w/o data in sm %d\n",fNoData[sm],sm);
1527       //printf("%d bad calibrated chambers in sm %d\n",fBadCalib[sm],sm);
1528       if(fNoData[sm] != 30) detectornodata += fNoData[sm];
1529       detectorbadcalib+=fBadCalib[sm];
1530     }
1531     //printf("Number of chambers w/o data %d\n",detectornodata);
1532     //printf("Number of chambers bad calibrated %d\n",detectorbadcalib);
1533
1534     if((detectornodata > fNoDataValidate) ||
1535        (detectorbadcalib > fBadCalibValidate)){
1536       fStatusPos = fStatusPos | kChamberStatusErrorRange;
1537       return kFALSE;
1538     }
1539     return kTRUE;
1540   }
1541   else return kFALSE;
1542   
1543 }
1544 //_____________________________________________________________________________
1545 Int_t AliTRDPreprocessorOffline::GetVersion(TString name) const
1546 {
1547   //
1548   // Get version from the title
1549   //
1550   
1551   // Some patterns
1552   const Char_t *version = "Ver";
1553   if(!strstr(name.Data(),version)) return -1;
1554   const Char_t *after = "Subver";  
1555   if(!strstr(name.Data(),after)) return -1;
1556
1557   for(Int_t ver = 0; ver < 999999999; ver++) {
1558
1559     TString vertry(version);
1560     vertry += ver;
1561     vertry += after;
1562
1563     //printf("vertry %s and name %s\n",vertry.Data(),name.Data());
1564
1565     if(strstr(name.Data(),vertry.Data())) return ver;
1566     
1567   }
1568   
1569   return -1;
1570
1571 }
1572
1573 //_____________________________________________________________________________
1574 Int_t AliTRDPreprocessorOffline::GetSubVersion(TString name) const
1575 {
1576   //
1577   // Get subversion from the title
1578   //
1579   
1580   // Some patterns
1581   const Char_t *subversion = "Subver";
1582   if(!strstr(name.Data(),subversion)) return -1;
1583   const Char_t *after = "FirstRun";
1584   if(!strstr(name.Data(),after)) {
1585     after = "Nz";
1586   }
1587   if(!strstr(name.Data(),after)) return -1;
1588
1589   
1590   for(Int_t ver = 0; ver < 999999999; ver++) {
1591     
1592     TString vertry(subversion);
1593     vertry += ver;
1594     vertry += after;
1595
1596     //printf("vertry %s and name %s\n",vertry.Data(),name.Data());
1597
1598     if(strstr(name.Data(),vertry.Data())) return ver;
1599     
1600   }
1601   
1602   return -1;
1603
1604 }
1605
1606 //_____________________________________________________________________________
1607 Int_t AliTRDPreprocessorOffline::GetFirstRun(TString name) const
1608 {
1609   //
1610   // Get first run from the title
1611   //
1612   
1613   // Some patterns
1614   const Char_t *firstrun = "FirstRun";
1615   if(!strstr(name.Data(),firstrun)) return -1;
1616   const Char_t *after = "Nz";  
1617   if(!strstr(name.Data(),after)) return -1;
1618   
1619   
1620   for(Int_t ver = 0; ver < 999999999; ver++) {
1621
1622     TString vertry(firstrun);
1623     vertry += ver;
1624     vertry += after;
1625
1626     //printf("vertry %s and name %s\n",vertry.Data(),name.Data());
1627
1628     if(strstr(name.Data(),vertry.Data())) return ver;
1629     
1630   }
1631   
1632   return -1;
1633
1634 }
1635 //_____________________________________________________________________________
1636 Bool_t AliTRDPreprocessorOffline::CheckStatus(Int_t status, Int_t bitMask) const
1637 {
1638   //
1639   // Checks the status
1640   //
1641
1642   return (status & bitMask) ? kTRUE : kFALSE;
1643   
1644 }
1645 //_____________________________________________________________________________
1646 Int_t AliTRDPreprocessorOffline::GetStatus() const
1647 {
1648   //
1649   // Checks the status
1650   // fStatusPos: errors
1651   // fStatusNeg: only info
1652   //
1653
1654   if(fStatusPos > 0) return fStatusPos;
1655   else return (-TMath::Abs(fStatusNeg));
1656   
1657 }
1658 //_____________________________________________________________________________
1659 void AliTRDPreprocessorOffline::PrintStatus() const
1660 {
1661   //
1662   // Do Summary
1663   //
1664
1665   AliInfo(Form("The error status is %d",fStatusPos));
1666   AliInfo(Form("IsGainErrorOld? %d",(Int_t)IsGainErrorOld()));
1667   AliInfo(Form("IsVdriftErrorOld? %d",(Int_t)IsVdriftErrorOld()));
1668   AliInfo(Form("IsGainErrorRange? %d",(Int_t)IsGainErrorRange()));
1669   AliInfo(Form("IsVdriftErrorRange? %d",(Int_t)IsVdriftErrorRange()));
1670   AliInfo(Form("IsTimeOffsetErrorRange? %d",(Int_t)IsTimeOffsetErrorRange()));
1671   AliInfo(Form("IsChamberStatusErrorRange? %d",(Int_t)IsChamberStatusErrorRange()));
1672
1673  
1674   AliInfo(Form("The info status is %d",fStatusNeg));
1675   AliInfo(Form("IsGainNotEnoughStatsButFill? %d",(Int_t)IsGainNotEnoughStatsButFill()));
1676   AliInfo(Form("IsVdriftNotEnoughStatsButFill? %d",(Int_t)IsVdriftNotEnoughStatsButFill()));
1677   AliInfo(Form("IsGainNotEnoughStatsNotFill? %d",(Int_t)IsGainNotEnoughStatsNotFill()));
1678   AliInfo(Form("IsVdriftNotEnoughStatsNotFill? %d",(Int_t)IsVdriftNotEnoughStatsNotFill()));
1679   AliInfo(Form("IsTimeOffsetNotEnoughStatsNotFill? %d",(Int_t)IsTimeOffsetNotEnoughStatsNotFill()));
1680
1681   AliInfo(Form("IsExBErrorRange? %d",(Int_t)IsExBErrorRange()));
1682   AliInfo(Form("IsExBErrorOld? %d",(Int_t)IsExBErrorOld()));
1683   
1684 }
1685 //___________________________________________________________________________________
1686 void AliTRDPreprocessorOffline::SetCalDetVdrift(AliTRDCalDet *calDetVdriftUsed) 
1687 {
1688
1689   fCalDetVdriftUsed = calDetVdriftUsed;
1690
1691   fCalDetExBUsed = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
1692   for(Int_t k = 0; k < 540; k++){
1693     fCalDetExBUsed->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(fCalDetVdriftUsed->GetValue(k)));
1694     //printf("Set the exb object for detector %d, vdrift %f and exb %f\n",k,fCalDetVdriftUsed->GetValue(k),fCalDetExBUsed->GetValue(k));
1695   }
1696   
1697 };
1698 //___________________________________________________________________________________
1699 Bool_t AliTRDPreprocessorOffline::SetCalDetGain(Int_t runNumber, Int_t version, Int_t subversion) 
1700 {
1701   //
1702   // Set the fCalDetGainUsed
1703   //
1704
1705   if((version == 0) && (subversion == 0)) return kFALSE;
1706
1707   AliCDBEntry *entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberGainFactor",runNumber, version, subversion);
1708   if(!entry) {
1709     AliError("Found no entry\n");
1710     fStatusPos = fStatusPos | kGainErrorOld;
1711     return kFALSE;
1712   }
1713   //const AliCDBId id = entry->GetId();
1714   //version = id.GetVersion();
1715   //subversion = id.GetSubVersion();
1716   //printf("Found version %d and subversion %d for vdrift\n",version,subversion);
1717   AliTRDCalDet* calDet = (AliTRDCalDet *)entry->GetObject();
1718   if(calDet) fCalDetGainUsed = calDet;
1719   else {
1720     fStatusPos = fStatusPos | kGainErrorOld;
1721     return kFALSE;
1722   }
1723   
1724   return kTRUE;
1725
1726 }
1727 //___________________________________________________________________________________
1728 Bool_t AliTRDPreprocessorOffline::SetCalDetVdriftExB(Int_t runNumber, Int_t versionv, Int_t subversionv, Int_t versionexb, Int_t subversionexb) 
1729 {
1730   //
1731   // Set the fCalDetVdriftUsed and fCalDetExBUsed
1732   //
1733
1734   if((versionv == 0) && (subversionv == 0)) return kFALSE;
1735
1736   AliCDBEntry *entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberVdrift",runNumber, versionv, subversionv);
1737   if(!entry) {
1738     AliError("Found no entry\n");
1739     fStatusPos = fStatusPos | kVdriftErrorOld;
1740     return kFALSE;
1741   }
1742   AliTRDCalDet* calDet = (AliTRDCalDet *)entry->GetObject();
1743   if(calDet) fCalDetVdriftUsed = calDet;
1744   else {
1745     fStatusPos = fStatusPos | kVdriftErrorOld;
1746     return kFALSE;
1747   }
1748
1749   // ExB object
1750
1751   if((versionexb == 0) && (subversionexb == 0)) {
1752     
1753     fCalDetExBUsed = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
1754     for(Int_t k = 0; k < 540; k++){
1755       fCalDetExBUsed->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(fCalDetVdriftUsed->GetValue(k)));
1756       //printf("Nothing found: set the exb object for detector %d, vdrift %f and exb %f\n",k,fCalDetVdriftUsed->GetValue(k),fCalDetExBUsed->GetValue(k));
1757     }
1758   }
1759   else {
1760
1761     entry = 0x0;
1762     entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberExB",runNumber, versionexb, subversionexb);
1763     if(!entry) {
1764       //printf("Found no entry\n");
1765       fStatusPos = fStatusPos | kExBErrorOld;   
1766       return kFALSE;
1767     }
1768     AliTRDCalDet* calDetexb = (AliTRDCalDet *)entry->GetObject();
1769     if(!calDetexb) {
1770       fStatusPos = fStatusPos | kExBErrorOld;   
1771       return kFALSE;
1772     }
1773     
1774     Double_t meanexb = calDetexb->GetMean();
1775     //printf("Mean value %f\n",meanexb);
1776     if((meanexb > 70) || (fNoExBUsedInReco)) {
1777       fCalDetExBUsed = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
1778       for(Int_t k = 0; k < 540; k++){
1779         fCalDetExBUsed->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(fCalDetVdriftUsed->GetValue(k)));
1780         //printf("Found but: set the exb object for detector %d, vdrift %f and exb %f\n",k,fCalDetVdriftUsed->GetValue(k),fCalDetExBUsed->GetValue(k));
1781       }
1782     }
1783     else {
1784       fCalDetExBUsed = calDetexb;
1785     }
1786     
1787   }
1788
1789   
1790   return kTRUE;
1791
1792 }
1793