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