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