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