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