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