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