]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDPreprocessorOffline.cxx
AliTRDCalib.cxx .h -> skip all the event if the initialization is not ok for the...
[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 "TProfile2D.h"
50 #include "AliTRDCalDet.h"
51 #include "AliTRDCalPad.h"
52 #include "AliCDBMetaData.h"
53 #include "AliCDBId.h"
54 #include "AliCDBManager.h"
55 #include "AliCDBStorage.h"
56 #include "AliTRDCalibraMode.h"
57 #include "AliTRDCalibraFit.h"
58 #include "AliTRDCalibraVdriftLinearFit.h"
59 #include "AliTRDPreprocessorOffline.h"
60 #include "AliTRDCalChamberStatus.h"
61
62
63 ClassImp(AliTRDPreprocessorOffline)
64
65 AliTRDPreprocessorOffline::AliTRDPreprocessorOffline():
66   TNamed("TPCPreprocessorOffline","TPCPreprocessorOffline"),
67   fMethodSecond(kTRUE),
68   fNameList("TRDCalib"),
69   fCalDetGainUsed(0x0),
70   fCalDetVdriftUsed(0x0),
71   fCH2d(0x0),
72   fPH2d(0x0),
73   fPRF2d(0x0),
74   fAliTRDCalibraVdriftLinearFit(0x0),
75   fNEvents(0x0),
76   fAbsoluteGain(0x0),
77   fPlots(new TObjArray(8)),
78   fCalibObjects(new TObjArray(8)),
79   fVersionGainUsed(0),
80   fSubVersionGainUsed(0),
81   fFirstRunVdriftUsed(0),
82   fVersionVdriftUsed(0), 
83   fSubVersionVdriftUsed(0),
84   fSwitchOnValidation(kTRUE),
85   fVdriftValidated(kFALSE),
86   fT0Validated(kFALSE),
87   fMinStatsVdriftT0PH(800*20),
88   fMinStatsVdriftLinear(800),
89   fMinStatsGain(800),
90   fMinStatsPRF(600)
91 {
92   //
93   // default constructor
94   //
95 }
96 //_________________________________________________________________________________________________________________
97 AliTRDPreprocessorOffline::~AliTRDPreprocessorOffline() {
98   //
99   // Destructor
100   //
101
102   if(fCalDetGainUsed) delete fCalDetGainUsed;
103   if(fCalDetVdriftUsed) delete fCalDetVdriftUsed;
104   if(fCH2d) delete fCH2d;
105   if(fPH2d) delete fPH2d;
106   if(fPRF2d) delete fPRF2d;
107   if(fAliTRDCalibraVdriftLinearFit) delete fAliTRDCalibraVdriftLinearFit;
108   if(fNEvents) delete fNEvents;
109   if(fAbsoluteGain) delete fAbsoluteGain;
110   if(fPlots) delete fPlots;
111   if(fCalibObjects) delete fCalibObjects;
112   
113 }
114 //___________________________________________________________________________________________________________________
115
116 void AliTRDPreprocessorOffline::CalibVdriftT0(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
117   //
118   // make calibration of the drift velocity
119   // Input parameters:
120   //      file                             - the location of input file
121   //      startRunNumber, endRunNumber     - run validity period 
122   //      ocdbStorage                      - path to the OCDB storage
123   //                                       - if empty - local storage 'pwd' uesed
124   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
125   //
126   // 1. Initialization 
127   //
128   fVdriftValidated = kTRUE;
129   fT0Validated = kTRUE;
130   //
131   // 2. extraction of the information
132   //
133   if(ReadVdriftT0Global(file)) AnalyzeVdriftT0();
134   //printf("Finish PH\n");
135   if(ReadVdriftLinearFitGlobal(file)) AnalyzeVdriftLinearFit();
136   //
137   // 3. Append QA plots
138   //
139   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
140   //
141   //
142   // 4. validate OCDB entries
143   //
144   if(fSwitchOnValidation==kTRUE && ValidateVdrift()==kFALSE) { 
145     AliError("TRD vdrift OCDB parameters out of range!");
146     fVdriftValidated = kFALSE;
147   }
148   if(fSwitchOnValidation==kTRUE && ValidateT0()==kFALSE) { 
149     AliError("TRD t0 OCDB parameters out of range!");
150     fT0Validated = kFALSE;
151   }
152   //
153   // 5. update of OCDB
154   //
155   //
156   if(fVdriftValidated) UpdateOCDBVdrift(startRunNumber,endRunNumber,ocdbStorage);
157   if(fT0Validated) UpdateOCDBT0(startRunNumber,endRunNumber,ocdbStorage);
158   
159 }
160 //_________________________________________________________________________________________________________________
161
162 void AliTRDPreprocessorOffline::CalibGain(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
163   //
164   // make calibration of the drift velocity
165   // Input parameters:
166   //      file                             - the location of input file
167   //      startRunNumber, endRunNumber     - run validity period 
168   //      ocdbStorage                      - path to the OCDB storage
169   //                                       - if empty - local storage 'pwd' uesed
170   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
171   //
172   // 1. Initialization 
173   if(!ReadGainGlobal(file)) return;
174   //
175   //
176   // 2. extraction of the information
177   //
178   AnalyzeGain();
179   if(fCalDetGainUsed) CorrectFromDetGainUsed();
180   if(fCalDetVdriftUsed) CorrectFromDetVdriftUsed();
181   //
182   // 3. Append QA plots
183   //
184   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
185   //
186   //
187   // 4. validate OCDB entries
188   //
189   if(fSwitchOnValidation==kTRUE && ValidateGain()==kFALSE) { 
190     AliError("TRD gain OCDB parameters out of range!");
191     return;
192   }
193   //
194   // 5. update of OCDB
195   //
196   //
197   if((!fCalDetVdriftUsed) || (fCalDetVdriftUsed && fVdriftValidated)) UpdateOCDBGain(startRunNumber,endRunNumber,ocdbStorage);
198   
199   
200 }
201 //________________________________________________________________________________________________________________
202
203 void AliTRDPreprocessorOffline::CalibPRF(const Char_t* file, Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
204   //
205   // make calibration of the drift velocity
206   // Input parameters:
207   //      file                             - the location of input file
208   //      startRunNumber, endRunNumber     - run validity period 
209   //      ocdbStorage                      - path to the OCDB storage
210   //                                       - if empty - local storage 'pwd' uesed
211   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
212   //
213   // 1. Initialization 
214   if(!ReadPRFGlobal(file)) return;
215   //
216   //
217   // 2. extraction of the information
218   //
219   AnalyzePRF();
220   //
221   // 3. Append QA plots
222   //
223   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
224   //
225   //
226   //
227   // 4. validate OCDB entries
228   //
229   if(fSwitchOnValidation==kTRUE && ValidatePRF()==kFALSE) { 
230     AliError("TRD prf OCDB parameters out of range!");
231     return;
232   }
233   //
234   // 5. update of OCDB
235   //
236   //
237   UpdateOCDBPRF(startRunNumber,endRunNumber,ocdbStorage);
238   
239 }
240 //________________________________________________________________________________________________________________
241
242 void AliTRDPreprocessorOffline::CalibChamberStatus(Int_t startRunNumber, Int_t endRunNumber, TString ocdbStorage){
243   //
244   // make calibration of the chamber status
245   // Input parameters:
246   //      startRunNumber, endRunNumber     - run validity period 
247   //      ocdbStorage                      - path to the OCDB storage
248   //                                       - if empty - local storage 'pwd' uesed
249   if (ocdbStorage.Length()<=0) ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
250   //
251   //
252   // 2. extraction of the information
253   //
254   if(!AnalyzeChamberStatus()) return;
255   //
256   // 3. Append QA plots
257   //
258   //MakeDefaultPlots(fVdriftArray,fVdriftArray);
259   //
260   //
261   //
262   // 4. validate OCDB entries
263   //
264   if(fSwitchOnValidation==kTRUE && ValidateChamberStatus()==kFALSE) { 
265     AliError("TRD Chamber status OCDB parameters not ok!");
266     return;
267   }
268   //
269   // 5. update of OCDB
270   //
271   //
272   UpdateOCDBChamberStatus(startRunNumber,endRunNumber,ocdbStorage);
273   
274 }
275 //______________________________________________________________________________________________________
276 Bool_t AliTRDPreprocessorOffline::Init(const Char_t* fileName){
277   //
278   // read the calibration used during the reconstruction
279   // 
280
281   if(ReadVdriftT0Global(fileName)) {
282     
283     TString nameph = fPH2d->GetTitle();
284     fFirstRunVdriftUsed = GetFirstRun(nameph); 
285     fVersionVdriftUsed = GetVersion(nameph);  
286     fSubVersionVdriftUsed = GetSubVersion(nameph);    
287
288     //printf("Found Version %d, Subversion %d for vdrift\n",fVersionVdriftUsed,fSubVersionVdriftUsed);
289   
290   }
291
292   if(ReadGainGlobal(fileName)) {
293
294     TString namech = fCH2d->GetTitle();
295     fVersionGainUsed = GetVersion(namech);  
296     fSubVersionGainUsed = GetSubVersion(namech);    
297
298     //printf("Found Version %d, Subversion %d for gain\n",fVersionGainUsed,fSubVersionGainUsed);
299
300   }
301    
302   return kTRUE;
303   
304 }
305 //___________________________________________________________________________________________________________________
306
307 Bool_t AliTRDPreprocessorOffline::ReadGainGlobal(const Char_t* fileName){
308   //
309   // read calibration entries from file
310   // 
311   if(fCH2d) return kTRUE;
312   TFile fcalib(fileName);
313   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
314   if (array){
315     TH2I *ch2d = (TH2I *) array->FindObject("CH2d");
316     if(!ch2d) return kFALSE;
317     fCH2d = (TH2I*)ch2d->Clone();
318     //fNEvents = (TH1I *) array->FindObject("NEvents");
319     //fAbsoluteGain = (TH2F *) array->FindObject("AbsoluteGain");
320   }else{
321     TH2I *ch2d = (TH2I *) fcalib.Get("CH2d");
322     if(!ch2d) return kFALSE;
323     fCH2d = (TH2I*)ch2d->Clone();
324     //fNEvents = (TH1I *) fcalib.Get("NEvents");
325     //fAbsoluteGain = (TH2F *) fcalib.Get("AbsoluteGain");
326   }
327   fCH2d->SetDirectory(0);
328   //printf("title of CH2d %s\n",fCH2d->GetTitle());
329
330   return kTRUE;
331   
332 }
333 //_________________________________________________________________________________________________________________
334
335 Bool_t AliTRDPreprocessorOffline::ReadVdriftT0Global(const Char_t* fileName){
336   //
337   // read calibration entries from file
338   // 
339   if(fPH2d) return kTRUE;
340   TFile fcalib(fileName);
341   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
342   if (array){
343     TProfile2D *ph2d = (TProfile2D *) array->FindObject("PH2d");
344     if(!ph2d) return kFALSE;
345     fPH2d = (TProfile2D*)ph2d->Clone();
346     //fNEvents = (TH1I *) array->FindObject("NEvents");
347   }else{
348     TProfile2D *ph2d = (TProfile2D *) fcalib.Get("PH2d");
349     if(!ph2d) return kFALSE;
350     fPH2d = (TProfile2D*)ph2d->Clone();
351     //fNEvents = (TH1I *) fcalib.Get("NEvents");
352   }
353   fPH2d->SetDirectory(0);
354   //printf("title of PH2d %s\n",fPH2d->GetTitle());
355   
356   return kTRUE;
357   
358 }
359 //___________________________________________________________________________________________________________________
360
361 Bool_t AliTRDPreprocessorOffline::ReadVdriftLinearFitGlobal(const Char_t* fileName){
362   //
363   // read calibration entries from file
364   // 
365   if(fAliTRDCalibraVdriftLinearFit) return kTRUE;
366   TFile fcalib(fileName);
367   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
368   if (array){
369     fAliTRDCalibraVdriftLinearFit = (AliTRDCalibraVdriftLinearFit *) array->FindObject("AliTRDCalibraVdriftLinearFit");
370     //fNEvents = (TH1I *) array->FindObject("NEvents");
371   }else{
372     fAliTRDCalibraVdriftLinearFit = (AliTRDCalibraVdriftLinearFit *) fcalib.Get("AliTRDCalibraVdriftLinearFit");
373     //fNEvents = (TH1I *) fcalib.Get("NEvents");
374   }
375   if(!fAliTRDCalibraVdriftLinearFit) {
376     //printf("No AliTRDCalibraVdriftLinearFit\n");
377     return kFALSE;
378   }
379   return kTRUE;
380   
381 }
382 //_____________________________________________________________________________________________________________
383
384 Bool_t AliTRDPreprocessorOffline::ReadPRFGlobal(const Char_t* fileName){
385   //
386   // read calibration entries from file
387   // 
388   if(fPRF2d) return kTRUE;
389   TFile fcalib(fileName);
390   TObjArray * array = (TObjArray*)fcalib.Get(fNameList);
391   if (array){
392     TProfile2D *prf2d = (TProfile2D *) array->FindObject("PRF2d");
393     if(!prf2d) return kFALSE;
394     fPRF2d = (TProfile2D*)prf2d->Clone();
395     //fNEvents = (TH1I *) array->FindObject("NEvents");
396   }else{
397     TProfile2D *prf2d = (TProfile2D *) fcalib.Get("PRF2d");
398     if(!prf2d) return kFALSE;
399     fPRF2d = (TProfile2D*)prf2d->Clone();
400     //fNEvents = (TH1I *) fcalib.Get("NEvents");
401   }
402   fPRF2d->SetDirectory(0);
403   //printf("title of PRF2d %s\n",fPRF2d->GetTitle());
404   
405   return kTRUE;
406
407 }
408 //__________________________________________________________________________________________________________
409
410 Bool_t AliTRDPreprocessorOffline::AnalyzeGain(){
411   //
412   // Analyze gain - produce the calibration objects
413   //
414
415   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
416   calibra->SetMinEntries(fMinStatsGain); // If there is less than 1000 entries in the histo: no fit
417   calibra->AnalyseCH(fCH2d);
418
419   Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(0))
420     + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(0));
421   Int_t nbfit       = calibra->GetNumberFit();
422   Int_t nbE         = calibra->GetNumberEnt();
423
424
425   Bool_t ok = kFALSE;
426   Bool_t meanother = kFALSE;
427   // enough statistics
428   if ((nbtg >                  0) && 
429       (nbfit        >= 0.5*nbE) && (nbE > 30)) {
430     // create the cal objects
431     if(!fCalDetGainUsed) {
432       calibra->PutMeanValueOtherVectorFit(1,kTRUE);
433       meanother = kTRUE;
434     }
435     TObjArray object           = calibra->GetVectorFit();
436     AliTRDCalDet *calDetGain   = calibra->CreateDetObjectGain(&object,meanother);
437     TH1F *coefGain  = calDetGain->MakeHisto1DAsFunctionOfDet();
438     // Put them in the array
439     fCalibObjects->AddAt(calDetGain,kGain);
440     fPlots->AddAt(coefGain,kGain);
441     //
442     ok = kTRUE;
443   }
444   
445   calibra->ResetVectorFit();
446   
447   return ok;
448   
449 }
450 //_____________________________________________________________________________________________________
451 Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftT0(){
452   //
453   // Analyze VdriftT0 - produce the calibration objects
454   //
455
456   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
457   calibra->SetMinEntries(fMinStatsVdriftT0PH); // If there is less than 1000 entries in the histo: no fit
458   calibra->AnalysePH(fPH2d);
459
460   Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
461     + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
462   Int_t nbfit       = calibra->GetNumberFit();
463   Int_t nbfitSuccess = calibra->GetNumberFitSuccess();
464   Int_t nbE         = calibra->GetNumberEnt();
465
466   //printf("nbtg %d, nbfit %d, nbE %d, nbfitSuccess %d\n",nbtg,nbfit,nbE,nbfitSuccess);
467
468   Bool_t ok = kFALSE;
469   if ((nbtg >                  0) && 
470       (nbfit        >= 0.5*nbE) && (nbE > 30) && (nbfitSuccess > 30)) {
471     //printf("Pass the cut for VdriftT0\n");
472     // create the cal objects
473     calibra->RemoveOutliers(1,kFALSE);
474     calibra->PutMeanValueOtherVectorFit(1,kFALSE);
475     calibra->RemoveOutliers2(kFALSE);
476     calibra->PutMeanValueOtherVectorFit2(1,kFALSE);
477     //
478     TObjArray object = calibra->GetVectorFit();
479     AliTRDCalDet *calDetVdrift = calibra->CreateDetObjectVdrift(&object);
480     TH1F *coefVdriftPH  = calDetVdrift->MakeHisto1DAsFunctionOfDet();
481     AliTRDCalPad *calPadVdrift = (AliTRDCalPad *)calibra->CreatePadObjectVdrift(&object,calDetVdrift);
482     TH1F *coefPadVdrift   = calPadVdrift->MakeHisto1D();
483     object       = calibra->GetVectorFit2();
484     AliTRDCalDet *calDetT0  = calibra->CreateDetObjectT0(&object);
485     TH1F *coefT0  = calDetT0->MakeHisto1DAsFunctionOfDet();
486     AliTRDCalPad *calPadT0 = (AliTRDCalPad *)calibra->CreatePadObjectT0(&object,calDetT0);
487     TH1F *coefPadT0  = calPadT0->MakeHisto1D();
488     // Put them in the array
489     fCalibObjects->AddAt(calDetT0,kT0PHDet);
490     fCalibObjects->AddAt(calDetVdrift,kVdriftPHDet);
491     fCalibObjects->AddAt(calPadT0,kT0PHPad);
492     fCalibObjects->AddAt(calPadVdrift,kVdriftPHPad);
493     fPlots->AddAt(coefVdriftPH,kVdriftPHDet);
494     fPlots->AddAt(coefT0,kT0PHDet);
495     fPlots->AddAt(coefPadVdrift,kVdriftPHPad);
496     fPlots->AddAt(coefPadT0,kT0PHPad);
497     //
498     ok = kTRUE;
499   }
500   calibra->ResetVectorFit();
501  
502   return ok;
503   
504 }
505 //____________________________________________________________________________________________________________________
506 Bool_t AliTRDPreprocessorOffline::AnalyzeVdriftLinearFit(){
507   //
508   // Analyze vdrift linear fit - produce the calibration objects
509   //
510
511   //printf("Analyse linear fit\n");
512
513   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
514   calibra->SetMinEntries(fMinStatsVdriftLinear); // If there is less than 1000 entries in the histo: no fit
515   //printf("Fill PE Array\n");
516   fAliTRDCalibraVdriftLinearFit->FillPEArray();
517   //printf("AliTRDCalibraFit\n");
518   calibra->AnalyseLinearFitters(fAliTRDCalibraVdriftLinearFit);
519   //printf("After\n");
520
521   //Int_t nbtg        = 540;
522   Int_t nbfit       = calibra->GetNumberFit();
523   Int_t nbE         = calibra->GetNumberEnt();
524
525   
526   Bool_t ok = kFALSE;
527   // enough statistics
528   if ((nbfit        >= 0.5*nbE) && (nbE > 30)) {
529     // create the cal objects
530     //calibra->RemoveOutliers(1,kTRUE);
531     calibra->PutMeanValueOtherVectorFit(1,kTRUE);
532     //calibra->RemoveOutliers2(kTRUE);
533     calibra->PutMeanValueOtherVectorFit2(1,kTRUE);
534     //
535     TObjArray object  = calibra->GetVectorFit();
536     AliTRDCalDet *calDetVdrift = calibra->CreateDetObjectVdrift(&object,kTRUE);
537     TH1F *coefDriftLinear      = calDetVdrift->MakeHisto1DAsFunctionOfDet();
538     object                     = calibra->GetVectorFit2();
539     AliTRDCalDet *calDetLorentz = calibra->CreateDetObjectLorentzAngle(&object);
540     TH1F *coefLorentzAngle = calDetLorentz->MakeHisto1DAsFunctionOfDet();
541     // Put them in the array
542     fCalibObjects->AddAt(calDetVdrift,kVdriftLinear);
543     fCalibObjects->AddAt(calDetLorentz,kLorentzLinear);
544     fPlots->AddAt(coefDriftLinear,kVdriftLinear);
545     fPlots->AddAt(coefLorentzAngle,kLorentzLinear);
546     //
547     ok = kTRUE;
548   }
549   
550   calibra->ResetVectorFit();
551   
552   return ok;
553   
554 }
555 //________________________________________________________________________________________________________________
556
557 Bool_t AliTRDPreprocessorOffline::AnalyzePRF(){
558   //
559   // Analyze PRF - produce the calibration objects
560   //
561
562   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
563   calibra->SetMinEntries(fMinStatsPRF); // If there is less than 1000 entries in the histo: no fit
564   calibra->AnalysePRFMarianFit(fPRF2d);
565
566   Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(2))
567     + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(2));
568   Int_t nbfit       = calibra->GetNumberFit();
569   Int_t nbE         = calibra->GetNumberEnt();
570
571   
572   Bool_t ok = kFALSE;
573   // enough statistics
574   if ((nbtg >                  0) && 
575       (nbfit        >= 0.95*nbE) && (nbE > 30)) {
576     // create the cal objects
577     TObjArray object  = calibra->GetVectorFit();
578     AliTRDCalPad *calPadPRF = (AliTRDCalPad*) calibra->CreatePadObjectPRF(&object);
579     TH1F *coefPRF           = calPadPRF->MakeHisto1D();
580     // Put them in the array
581     fCalibObjects->AddAt(calPadPRF,kPRF);
582     fPlots->AddAt(coefPRF,kPRF);
583     //
584     ok = kTRUE;
585   }
586   
587   calibra->ResetVectorFit();
588   
589   return ok;
590   
591 }
592
593 //_____________________________________________________________________________
594 Bool_t AliTRDPreprocessorOffline::AnalyzeChamberStatus()
595 {
596   //
597   // Produce AliTRDCalChamberStatus out of calibration results
598   //
599   
600   // set up AliTRDCalChamberStatus
601   AliTRDCalChamberStatus *CalChamberStatus = new AliTRDCalChamberStatus();
602   for(Int_t det = 0; det < 540; det++) CalChamberStatus->SetStatus(det,1);
603
604   // get calibration objects
605   AliTRDCalDet *calDetGain   = (AliTRDCalDet *) fCalibObjects->At(kGain);
606   AliTRDCalDet *calDetVDrift = (AliTRDCalDet *) fCalibObjects->At(kVdriftLinear);
607
608   // Check
609   if((!calDetGain) || (!calDetVDrift) || (!fCH2d)) return kFALSE;
610
611   // Gain
612   Double_t gainmean = calDetGain->GetMean();
613   Double_t vdriftmean = calDetVDrift->GetMean();
614
615   Double_t gainrms = calDetGain->GetRMSRobust();
616   Double_t vdriftrms = calDetVDrift->GetRMSRobust();
617
618   //printf("Gain mean: %f, rms: %f\n",gainmean,gainrms);
619   //printf("Vdrift mean: %f, rms: %f\n",vdriftmean,vdriftrms);
620   
621   // Check
622   if((TMath::Abs(gainrms) < 0.001) || (TMath::Abs(vdriftrms) < 0.001)) return kFALSE;
623
624   // mask chambers with empty gain entries
625   //Int_t counter = 0;
626   for (Int_t idet = 0; idet < 540; idet++) {
627
628     // ch2d
629     TH1I *projch =  (TH1I *) fCH2d->ProjectionX("projch",idet+1,idet+1,(Option_t *)"e");
630     Double_t entries = projch->GetEntries();
631
632     // gain
633     Double_t gain = calDetGain->GetValue(idet);
634
635     // vdrift
636     Double_t vdrift = calDetVDrift->GetValue(idet);
637
638
639     if(entries<=0.5 ||
640        TMath::Abs(gainmean-gain) > (15.0*gainrms) ||
641        TMath::Abs(vdriftmean-vdrift) > (15.0*vdriftrms)) {
642      
643       //printf(" chamber det %03d masked \n",idet);
644       //printf(" gainmean %f and gain %f, gainrms %f \n",gainmean,gain,gainrms);
645       //printf(" vdriftmean %f and vdrift %f, vdriftrms %f \n",vdriftmean,vdrift,vdriftrms);
646       CalChamberStatus->SetStatus(idet,AliTRDCalChamberStatus::kMasked);
647       //counter++;
648     }
649
650      /*
651      // installed supermodules+1 -> abort
652      if(counter > (7+1)*30) {
653        printf("ERROR: more than one SM to be masked!! \n Abort...\n");
654        if(projch) delete projch;
655        return 0x0;
656      }
657      */
658
659     delete projch;
660     
661    }
662
663    // Security
664    for(Int_t sm=0; sm < 18; sm++) {
665      Int_t counter = 0;
666      for(Int_t det = 0; det < 30; det++){
667        Int_t detector = sm*30+det;
668        if(CalChamberStatus->IsMasked(detector)) counter++;
669      }
670      if(counter >= 10) {
671        for(Int_t det = 0; det < 30; det++){
672          Int_t detector = sm*30+det;
673          CalChamberStatus->SetStatus(detector,AliTRDCalChamberStatus::kInstalled);
674        }
675      }
676    }
677
678    fCalibObjects->AddAt(CalChamberStatus,kChamberStatus);
679    return kTRUE;
680
681  }
682
683
684  //________________________________________________________________________________________________
685  void AliTRDPreprocessorOffline::CorrectFromDetGainUsed() {
686    //
687    // Correct from the gas gain used afterwards
688    //
689    AliTRDCalDet *calDetGain = (AliTRDCalDet *) fCalibObjects->At(kGain);
690    if(!calDetGain) return;
691
692    // Calculate mean
693    Double_t mean = 0.0;
694    Int_t nbdet = 0;
695
696    for(Int_t det = 0; det < 540; det++) {
697
698      Float_t gaininit = fCalDetGainUsed->GetValue(det);
699      Float_t gainout = calDetGain->GetValue(det);
700
701
702      if(TMath::Abs(gainout-1.0) > 0.000001) {
703        mean += (gaininit*gainout);
704        nbdet++;
705      }  
706    }
707    if(nbdet > 0) mean = mean/nbdet;
708
709    for(Int_t det = 0; det < 540; det++) {
710
711      Float_t gaininit = fCalDetGainUsed->GetValue(det);
712      Float_t gainout = calDetGain->GetValue(det);
713
714      if(TMath::Abs(gainout-1.0) > 0.000001) calDetGain->SetValue(det,gaininit*gainout);
715      else calDetGain->SetValue(det,mean);
716    }
717
718
719  }
720  //________________________________________________________________________________________________
721  void AliTRDPreprocessorOffline::CorrectFromDetVdriftUsed() {
722    //
723    // Correct from the drift velocity
724    //
725
726    //printf("Correct for vdrift\n");
727
728    AliTRDCalDet *calDetGain = (AliTRDCalDet *) fCalibObjects->At(kGain);
729    if(!calDetGain) return;
730
731    Int_t detVdrift = kVdriftPHDet;
732    if(fMethodSecond) detVdrift = kVdriftLinear;
733    AliTRDCalDet *calDetVdrift = (AliTRDCalDet *) fCalibObjects->At(detVdrift);
734    if(!calDetVdrift) return;
735
736    // Calculate mean
737
738    for(Int_t det = 0; det < 540; det++) {
739
740      Float_t vdriftinit = fCalDetVdriftUsed->GetValue(det);
741      Float_t vdriftout = calDetVdrift->GetValue(det);
742
743      Float_t gain = calDetGain->GetValue(det);
744      if(vdriftout > 0.0) gain = gain*vdriftinit/vdriftout;
745      calDetGain->SetValue(det,gain);
746
747
748    }
749
750  }
751  //_________________________________________________________________________________________________________________
752  void AliTRDPreprocessorOffline::UpdateOCDBGain(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
753    //
754    // Update OCDB entry
755    //
756
757    AliCDBMetaData *metaData= new AliCDBMetaData();
758    metaData->SetObjectClassName("AliTRDCalDet");
759    metaData->SetResponsible("Raphaelle Bailhache");
760    metaData->SetBeamPeriod(1);
761
762    AliCDBId id1("TRD/Calib/ChamberGainFactor", startRunNumber, endRunNumber);
763    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
764    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kGain);
765    if(calDet) gStorage->Put(calDet, id1, metaData);
766
767
768  }
769  //___________________________________________________________________________________________________________________
770  void AliTRDPreprocessorOffline::UpdateOCDBVdrift(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
771    //
772    // Update OCDB entry
773    //
774
775    Int_t detVdrift = kVdriftPHDet;
776
777    if(fMethodSecond) detVdrift = kVdriftLinear;
778
779    AliCDBMetaData *metaData= new AliCDBMetaData();
780    metaData->SetObjectClassName("AliTRDCalDet");
781    metaData->SetResponsible("Raphaelle Bailhache");
782    metaData->SetBeamPeriod(1);
783
784    AliCDBId id1("TRD/Calib/ChamberVdrift", startRunNumber, endRunNumber);
785    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
786    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detVdrift);
787    if(calDet) gStorage->Put(calDet, id1, metaData);
788
789    //
790
791    if(!fMethodSecond) {
792
793      AliCDBMetaData *metaDataPad= new AliCDBMetaData();
794      metaDataPad->SetObjectClassName("AliTRDCalPad");
795      metaDataPad->SetResponsible("Raphaelle Bailhache");
796      metaDataPad->SetBeamPeriod(1);
797
798      AliCDBId id1Pad("TRD/Calib/LocalVdrift", startRunNumber, endRunNumber);
799      AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kVdriftPHPad);
800      if(calPad) gStorage->Put(calPad, id1Pad, metaDataPad);
801
802    }
803
804  }
805  //________________________________________________________________________________________________________________________
806  void AliTRDPreprocessorOffline::UpdateOCDBT0(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
807    //
808    // Update OCDB entry
809    //
810
811    AliCDBMetaData *metaData= new AliCDBMetaData();
812    metaData->SetObjectClassName("AliTRDCalDet");
813    metaData->SetResponsible("Raphaelle Bailhache");
814    metaData->SetBeamPeriod(1);
815
816    AliCDBId id1("TRD/Calib/ChamberT0", startRunNumber, endRunNumber);
817    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
818    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kT0PHDet);
819    if(calDet) gStorage->Put(calDet, id1, metaData);
820
821    //
822
823    AliCDBMetaData *metaDataPad= new AliCDBMetaData();
824    metaDataPad->SetObjectClassName("AliTRDCalPad");
825    metaDataPad->SetResponsible("Raphaelle Bailhache");
826    metaDataPad->SetBeamPeriod(1);
827
828    AliCDBId id1Pad("TRD/Calib/LocalT0", startRunNumber, endRunNumber);
829    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kT0PHPad);
830    if(calPad) gStorage->Put(calPad, id1Pad, metaDataPad);
831
832
833
834  }
835  //_________________________________________________________________________________________________________________
836  void AliTRDPreprocessorOffline::UpdateOCDBPRF(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
837    //
838    // Update OCDB entry
839    //
840
841    AliCDBMetaData *metaData= new AliCDBMetaData();
842    metaData->SetObjectClassName("AliTRDCalPad");
843    metaData->SetResponsible("Raphaelle Bailhache");
844    metaData->SetBeamPeriod(1);
845
846    AliCDBId id1("TRD/Calib/PRFWidth", startRunNumber, endRunNumber);
847    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
848    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kPRF);
849    if(calPad) gStorage->Put(calPad, id1, metaData);
850
851
852  }
853  //_________________________________________________________________________________________________________________
854  void AliTRDPreprocessorOffline::UpdateOCDBChamberStatus(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
855    //
856    // Update OCDB entry
857    //
858
859    AliCDBMetaData *metaData= new AliCDBMetaData();
860    metaData->SetObjectClassName("AliTRDCalChamberStatus");
861    metaData->SetResponsible("Raphaelle Bailhache");
862    metaData->SetBeamPeriod(1);
863
864    AliCDBId id1("TRD/Calib/ChamberStatus", startRunNumber, endRunNumber);
865    AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
866    AliTRDCalChamberStatus *calChamberStatus = (AliTRDCalChamberStatus *) fCalibObjects->At(kChamberStatus);
867    if(calChamberStatus) gStorage->Put(calChamberStatus, id1, metaData);
868
869
870  }
871  //__________________________________________________________________________________________________________________________
872  Bool_t AliTRDPreprocessorOffline::ValidateGain() const {
873    //
874    // Validate OCDB entry
875    //
876
877    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kGain);
878    if(calDet) {
879      Double_t mean = calDet->GetMean();
880      Double_t rms = calDet->GetRMSRobust();
881      if((mean > 0.2) && (mean < 1.4) && (rms < 0.5)) return kTRUE;
882      //if((mean > 0.2) && (mean < 1.4)) return kTRUE;
883      else return kFALSE;
884    }
885    else return kFALSE;
886
887
888  }
889  //__________________________________________________________________________________________________________________________
890  Bool_t AliTRDPreprocessorOffline::ValidateVdrift(){
891    //
892    // Update OCDB entry
893    //
894
895    Int_t detVdrift = kVdriftPHDet;
896    Bool_t ok = kTRUE;
897
898    if(fMethodSecond) detVdrift = kVdriftLinear;
899
900    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(detVdrift);
901    if(calDet) {
902      Double_t mean = calDet->GetMean();
903      Double_t rms = calDet->GetRMSRobust();
904      //printf("Vdrift::mean %f, rms %f\n",mean,rms);
905      if(!((mean > 1.0) && (mean < 2.0) && (rms < 0.5))) ok = kFALSE;
906    }
907    else return kFALSE; 
908
909    if(!fMethodSecond) {
910      AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kVdriftPHPad);
911      if(calPad) {
912        Double_t mean = calPad->GetMean();
913        Double_t rms = calPad->GetRMS();
914        //printf("Vdrift::meanpad %f, rmspad %f\n",mean,rms);
915        if(!((mean > 0.9) && (mean < 1.1) && (rms < 0.6))) ok = kFALSE;
916      }
917      else return kFALSE;
918    }
919
920    return ok;
921
922  }
923  //__________________________________________________________________________________________________________________________
924  Bool_t AliTRDPreprocessorOffline::ValidateT0(){
925    //
926    // Update OCDB entry
927    //
928
929    AliTRDCalDet *calDet = (AliTRDCalDet *) fCalibObjects->At(kT0PHDet);
930    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kT0PHPad);
931    if(calDet && calPad) {
932      Double_t meandet = calDet->GetMean();
933      Double_t rmsdet = calDet->GetRMSRobust();
934      Double_t meanpad = calPad->GetMean();
935      //Double_t rmspad = calPad->GetRMS();
936      //printf("T0::minimum %f, rmsdet %f,meanpad %f, rmspad %f\n",meandet,rmsdet,meanpad,rmspad);
937      if((meandet > -1.5) && (meandet < 5.0) && (rmsdet < 4.0) && (meanpad < 5.0) && (meanpad > -0.5)) return kTRUE;
938      else return kFALSE;
939    }
940    else return kFALSE;
941
942  }
943  //__________________________________________________________________________________________________________________________
944  Bool_t AliTRDPreprocessorOffline::ValidatePRF() const{
945    //
946    // Update OCDB entry
947    //
948
949    AliTRDCalPad *calPad = (AliTRDCalPad *) fCalibObjects->At(kPRF);
950    if(calPad) {
951      Double_t meanpad = calPad->GetMean();
952      Double_t rmspad = calPad->GetRMS();
953      //printf("PRF::meanpad %f, rmspad %f\n",meanpad,rmspad);
954      if((meanpad < 1.0) && (rmspad < 0.8)) return kTRUE;
955      else return kFALSE;
956    }
957    else return kFALSE;
958
959
960  }
961  //__________________________________________________________________________________________________________________________
962 Bool_t AliTRDPreprocessorOffline::ValidateChamberStatus() const{
963   //
964   // Update OCDB entry
965   //
966   
967   AliTRDCalChamberStatus *calChamberStatus = (AliTRDCalChamberStatus *) fCalibObjects->At(kChamberStatus);
968   if(calChamberStatus) {
969     Int_t detectormasked = 0;
970     for(Int_t det = 0; det < 540; det++) {
971       if(calChamberStatus->IsMasked(det)) detectormasked++;
972     }
973     //printf("Number of chambers masked %d\n",detectormasked);
974     if(detectormasked > 29) return kFALSE;
975     else return kTRUE;
976   }
977   else return kFALSE;
978  
979 }
980 //_____________________________________________________________________________
981 Int_t AliTRDPreprocessorOffline::GetVersion(TString name) const
982 {
983   //
984   // Get version from the title
985   //
986   
987   // Some patterns
988   const Char_t *version = "Ver";
989   if(!strstr(name.Data(),version)) return -1;
990   
991   for(Int_t ver = 0; ver < 999999999; ver++) {
992
993     TString vertry(version);
994     vertry += ver;
995     vertry += "Subver";
996
997     //printf("vertry %s and name %s\n",vertry.Data(),name.Data());
998
999     if(strstr(name.Data(),vertry.Data())) return ver;
1000     
1001   }
1002   
1003   return -1;
1004
1005 }
1006
1007 //_____________________________________________________________________________
1008 Int_t AliTRDPreprocessorOffline::GetSubVersion(TString name) const
1009 {
1010   //
1011   // Get subversion from the title
1012   //
1013   
1014   // Some patterns
1015   const Char_t *subversion = "Subver";
1016   if(!strstr(name.Data(),subversion)) return -1;
1017   
1018   for(Int_t ver = 0; ver < 999999999; ver++) {
1019     
1020     TString vertry(subversion);
1021     vertry += ver;
1022     vertry += "FirstRun";
1023
1024     //printf("vertry %s and name %s\n",vertry.Data(),name.Data());
1025
1026     if(strstr(name.Data(),vertry.Data())) return ver;
1027     
1028   }
1029   
1030   return -1;
1031
1032 }
1033
1034 //_____________________________________________________________________________
1035 Int_t AliTRDPreprocessorOffline::GetFirstRun(TString name) const
1036 {
1037   //
1038   // Get first run from the title
1039   //
1040   
1041   // Some patterns
1042   const Char_t *firstrun = "FirstRun";
1043   if(!strstr(name.Data(),firstrun)) return -1;
1044   
1045   for(Int_t ver = 0; ver < 999999999; ver++) {
1046
1047     TString vertry(firstrun);
1048     vertry += ver;
1049     vertry += "Nz";
1050
1051     //printf("vertry %s and name %s\n",vertry.Data(),name.Data());
1052
1053     if(strstr(name.Data(),vertry.Data())) return ver;
1054     
1055   }
1056   
1057   return -1;
1058
1059 }
1060
1061
1062