]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDcalibDB.cxx
Typo
[u/mrichter/AliRoot.git] / TRD / AliTRDcalibDB.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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // Class providing the calibration parameters by accessing the CDB           //
21 //                                                                           //
22 // Request an instance with AliTRDcalibDB::Instance()                        //
23 // If a new event is processed set the event number with SetRun              //
24 // Then request the calibration data                                         // 
25 //                                                                           //
26 // Author:                                                                   //
27 //   Jan Fiete Grosse-Oetringhaus (Jan.Fiete.Grosse-Oetringhaus@cern.ch)     //
28 //                                                                           //
29 ///////////////////////////////////////////////////////////////////////////////
30
31 #include <TROOT.h>
32 #include <TClonesArray.h>
33 #include <TObjArray.h>
34
35 #include "AliCDBManager.h"
36 #include "AliCDBEntry.h"
37 #include "AliLog.h"
38
39 #include "AliTRDPIDReference.h"
40 #include "AliTRDPIDResponseObject.h"
41 #include "AliTRDcalibDB.h"
42 #include "AliTRDtrapConfig.h"
43 #include "AliTRDtrapConfigHandler.h"
44 #include "AliTRDCommonParam.h"
45
46 #include "Cal/AliTRDCalROC.h"
47 #include "Cal/AliTRDCalPad.h"
48 #include "Cal/AliTRDCalDet.h"
49 #include "Cal/AliTRDCalDCS.h"
50 #include "Cal/AliTRDCalDCSv2.h"
51 #include "Cal/AliTRDCalDCSFEEv2.h"
52 #include "Cal/AliTRDCalPID.h"
53 #include "Cal/AliTRDCalMonitoring.h"
54 #include "Cal/AliTRDCalChamberStatus.h"
55 #include "Cal/AliTRDCalPadStatus.h"
56 #include "Cal/AliTRDCalSingleChamberStatus.h"
57 #include "Cal/AliTRDCalTrkAttach.h"
58 #include "Cal/AliTRDCalOnlineGainTable.h"
59
60 ClassImp(AliTRDcalibDB)
61
62 AliTRDcalibDB *AliTRDcalibDB::fgInstance   = 0;
63 Bool_t         AliTRDcalibDB::fgTerminated = kFALSE;
64
65 //_ singleton implementation __________________________________________________
66 AliTRDcalibDB* AliTRDcalibDB::Instance()
67 {
68   //
69   // Singleton implementation
70   // Returns an instance of this class, it is created if neccessary
71   //
72   
73   if (fgTerminated != kFALSE) {
74     return 0;
75   }
76
77   if (fgInstance == 0) {
78     fgInstance = new AliTRDcalibDB();
79   }
80
81   return fgInstance;
82
83 }
84
85 //_____________________________________________________________________________
86 void AliTRDcalibDB::Terminate()
87 {
88   //
89   // Singleton implementation
90   // Deletes the instance of this class and sets the terminated flag,
91   // instances cannot be requested anymore
92   // This function can be called several times.
93   //
94   
95   fgTerminated = kTRUE;
96   
97   if (fgInstance != 0) {
98     delete fgInstance;
99     fgInstance = 0;
100   }
101
102 }
103
104 //_____________________________________________________________________________
105 AliTRDcalibDB::AliTRDcalibDB()
106   :TObject()
107   ,fRun(-1)
108   ,fPRFsmp(0)
109   ,fPRFbin(0)
110   ,fPRFlo(0)
111   ,fPRFhi(0)
112   ,fPRFwid(0)
113   ,fPRFpad(0)
114   ,fPIDResponse(NULL)
115   ,fOnlineGainTableID(0)
116   ,fTrapConfig(0x0)
117   ,fTrapConfigName("")
118   ,fTrapConfigVersion("")
119 {
120   //
121   // Default constructor
122   //
123   // TODO Default runnumber is set to 0, this should be changed later
124   //      to an invalid value (e.g. -1) to prevent
125   // TODO invalid calibration data to be used.
126   //
127
128   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
129     fCDBCache[i]   = 0;
130     fCDBEntries[i] = 0;
131   }
132   
133   // Create the sampled PRF
134   SamplePRF();
135   
136 }
137
138 //_____________________________________________________________________________
139 AliTRDcalibDB::AliTRDcalibDB(const AliTRDcalibDB &c)
140   :TObject(c)
141   ,fRun(-1)
142   ,fPRFsmp(0)
143   ,fPRFbin(0)
144   ,fPRFlo(0)
145   ,fPRFhi(0)
146   ,fPRFwid(0)
147   ,fPRFpad(0)
148   ,fPIDResponse(NULL)
149   ,fOnlineGainTableID(0)
150   ,fTrapConfig(0x0)
151   ,fTrapConfigName("")
152   ,fTrapConfigVersion("")
153 {
154   //
155   // Copy constructor (not that it make any sense for a singleton...)
156   //
157
158   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
159     fCDBCache[i]   = 0;
160     fCDBEntries[i] = 0;
161   }
162   
163   // Create the sampled PRF
164   SamplePRF();
165
166 }
167
168 //_____________________________________________________________________________
169 AliTRDcalibDB &AliTRDcalibDB::operator=(const AliTRDcalibDB &c) 
170 {
171   //
172   // Assignment operator (same as above ...)
173   //
174
175   if (this != &c) {
176     AliFatal("No assignment operator defined");
177   }
178
179   return *this;
180
181 }
182
183 //_____________________________________________________________________________
184 AliTRDcalibDB::~AliTRDcalibDB() 
185 {
186   //
187   // destructor
188   //
189   
190   if (fPRFsmp) {
191     delete [] fPRFsmp;
192     fPRFsmp = 0;
193   }
194
195   if (fPIDResponse) {
196     delete fPIDResponse;
197     fPIDResponse = 0x0;
198   }
199
200   Invalidate();
201
202 }
203
204 //_caching functions____________________________________________________________
205 const TObject *AliTRDcalibDB::GetCachedCDBObject(Int_t id)
206 {
207   //
208   // Retrieves a cdb object with the given id. The objects are cached as
209   // long as the run number is not changed.
210   //
211   // Put together the available objects here by using the lines
212   //   a) For usual calibration objects:
213   //      case kID<Name> : 
214   //        return CacheCDBEntry(kID<Name>,"TRD/Calib/<Path>"); 
215   //        break;
216   //      See function CacheCDBEntry for details.
217   //   and
218   //   b) For calibration data which depends on two objects: One containing 
219   //      a value per detector and one the local fluctuations per pad:
220   //      case kID<Name> :
221   //        return CacheMergeCDBEntry(kID<Name>,"TRD/Calib/<padPath>","TRD/Calib/<chamberPath>"); 
222   //        break;
223   //      See function CacheMergeCDBEntry for details.
224   //
225     
226   switch (id) {
227
228     // Parameters defined per pad and chamber
229     case kIDVdriftPad : 
230       return CacheCDBEntry(kIDVdriftPad         ,"TRD/Calib/LocalVdrift"); 
231       break;
232     case kIDVdriftChamber : 
233       return CacheCDBEntry(kIDVdriftChamber     ,"TRD/Calib/ChamberVdrift"); 
234       break;
235     case kIDExBChamber : 
236       return CacheCDBEntry(kIDExBChamber        ,"TRD/Calib/ChamberExB"); 
237       break;
238     case kIDT0Pad : 
239       return CacheCDBEntry(kIDT0Pad             ,"TRD/Calib/LocalT0"); 
240       break;
241     case kIDT0Chamber : 
242       return CacheCDBEntry(kIDT0Chamber         ,"TRD/Calib/ChamberT0"); 
243       break;
244     case kIDGainFactorPad : 
245       return CacheCDBEntry(kIDGainFactorPad     ,"TRD/Calib/LocalGainFactor"); 
246       break;
247     case kIDGainFactorChamber : 
248       return CacheCDBEntry(kIDGainFactorChamber ,"TRD/Calib/ChamberGainFactor"); 
249       break;
250
251     case kIDOnlineGainFactor : 
252       switch(GetOnlineGainTableID()) {
253         case 0:
254           // For testing purposes only !!!
255           AliInfo("No gain table name from OCDB. Use default table!");
256           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-01"); 
257           break;
258         case 1:
259           // Online gain table ID 1
260           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-01"); 
261           break;
262         case 2:
263           // Online gain table ID 2
264           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN0_2011-01"); 
265           break;
266         case 3:
267           // Online gain table ID 3
268           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN8_2011-01"); 
269           break;
270         case 4:
271           // Online gain table ID 4
272           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-02"); 
273           break;
274         case 5:
275           // Online gain table ID 5
276           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-03"); 
277           break;
278         case 6:
279           // Online gain table ID 6
280           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN0_2012-01"); 
281           break;
282         case 7:
283           // Online gain table ID 7
284           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN8_2012-01");
285           break; 
286         case 8:
287           // Online gain table ID 8
288           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2012-01"); 
289           break;
290       }
291       break;
292
293     case kIDNoiseChamber : 
294       return CacheCDBEntry(kIDNoiseChamber      ,"TRD/Calib/DetNoise"); 
295       break;
296     case kIDNoisePad : 
297       return CacheCDBEntry(kIDNoisePad          ,"TRD/Calib/PadNoise"); 
298       break;
299
300     // Parameters defined per pad
301     case kIDPRFWidth : 
302       return CacheCDBEntry(kIDPRFWidth          ,"TRD/Calib/PRFWidth"); 
303       break;
304
305     // Status values
306     case kIDChamberStatus : 
307       return CacheCDBEntry(kIDChamberStatus     ,"TRD/Calib/ChamberStatus"); 
308       break;
309     case kIDPadStatus : 
310       return CacheCDBEntry(kIDPadStatus         ,"TRD/Calib/PadStatus"); 
311       break;
312
313     // Global parameters
314     case kIDMonitoringData : 
315       return CacheCDBEntry(kIDMonitoringData    ,"TRD/Calib/MonitoringData"); 
316       break;
317     case kIDFEE : 
318       return CacheCDBEntry(kIDFEE               ,"TRD/Calib/FEE"); 
319       break;
320     case kIDTrapConfig :
321       return CacheCDBEntry(kIDFEE               ,"TRD/Calib/TrapConfig"); 
322       break;
323     case kIDDCS :
324       return CacheCDBEntry(kIDDCS               ,"TRD/Calib/DCS");
325       break;
326     case kIDPIDNN : 
327       return CacheCDBEntry(kIDPIDNN             ,"TRD/Calib/PIDNN");
328       break;
329     case kIDPIDLQ : 
330       return CacheCDBEntry(kIDPIDLQ             ,"TRD/Calib/PIDLQ"); 
331       break;
332     case kIDPIDLQ1D:
333       return CacheCDBEntry(kIDPIDLQ1D           ,"TRD/Calib/PIDLQ1D");
334       break;
335     case kIDRecoParam : 
336       return CacheCDBEntry(kIDRecoParam         ,"TRD/Calib/RecoParam"); 
337       break;
338     case kIDAttach : 
339       return CacheCDBEntry(kIDAttach            ,"TRD/Calib/TrkAttach"); 
340       break;
341     case kIDPHQ :
342       return CacheCDBEntry(kIDPHQ               ,"TRD/Calib/PHQ");
343       break;
344   }
345
346   return 0;
347
348 }
349
350 //_____________________________________________________________________________
351 AliCDBEntry *AliTRDcalibDB::GetCDBEntry(const char *cdbPath)
352 {
353   // 
354   // Retrieves an entry with path <cdbPath> from the CDB.
355   //
356     
357   AliCDBEntry *entry = AliCDBManager::Instance()->Get(cdbPath,fRun);
358   if (!entry) { 
359     AliError(Form("Failed to get entry: %s",cdbPath));
360     return 0; 
361   }
362   
363   return entry;
364
365 }
366
367 //_____________________________________________________________________________
368 const TObject *AliTRDcalibDB::CacheCDBEntry(Int_t id, const char *cdbPath)
369 {
370   //
371   // Caches the entry <id> with cdb path <cdbPath>
372   //
373
374   if (!fCDBCache[id]) {
375     fCDBEntries[id] = GetCDBEntry(cdbPath);
376     if (fCDBEntries[id]) {
377       fCDBCache[id] = fCDBEntries[id]->GetObject();
378     }
379   } 
380   
381   return fCDBCache[id];
382
383 }
384
385 //_____________________________________________________________________________
386 void AliTRDcalibDB::SetRun(Long64_t run)
387 {
388   //
389   // Sets current run number. Calibration data is read from the corresponding file.
390   // When the run number changes the caching is invalidated.
391   //
392
393   if (fRun == run) {
394     return;
395   }
396
397   fRun = run;
398
399   Invalidate();
400
401 }
402
403 //_____________________________________________________________________________
404 void AliTRDcalibDB::Invalidate()
405 {
406   //
407   // Invalidates cache (when run number is changed).
408   //
409   
410   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
411     if (fCDBEntries[i]) {
412       if (AliCDBManager::Instance()->GetCacheFlag() == kFALSE) {
413         if ((fCDBEntries[i]->IsOwner() == kFALSE) && 
414             (fCDBCache[i])) {
415           delete fCDBCache[i];
416         }
417         delete fCDBEntries[i];
418       }
419       fCDBEntries[i] = 0;
420       fCDBCache[i]   = 0;
421     }
422   }
423
424   fOnlineGainTableID = 0;
425
426 }
427
428 //_____________________________________________________________________________
429 Float_t AliTRDcalibDB::GetNoise(Int_t det, Int_t col, Int_t row)
430 {
431   //
432   // Returns the noise level in ADC counts for the given pad.
433   //
434
435   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
436                                    (GetCachedCDBObject(kIDNoisePad));
437   if (!calPad) {
438     return -1;
439   }
440
441   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
442   if (!roc) {
443     return -1;
444   }
445
446   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
447                                    (GetCachedCDBObject(kIDNoiseChamber));
448   if (!calChamber) {
449     return -1;
450   }
451
452   return calChamber->GetValue(det) * roc->GetValue(col,row);
453
454 }
455  
456 //_____________________________________________________________________________
457 AliTRDCalROC *AliTRDcalibDB::GetNoiseROC(Int_t det)
458 {
459   //
460   // Returns the Vdrift calibration object for a given ROC
461   // containing one number per pad 
462   //
463   
464   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
465                                        (GetCachedCDBObject(kIDNoisePad));
466   if (!calPad) {
467     return 0;
468   }
469
470   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
471   if (!roc) {
472     return 0;
473   }
474   else {
475     return roc;
476   }
477
478 }
479
480 //_____________________________________________________________________________
481 const AliTRDCalDet *AliTRDcalibDB::GetNoiseDet()
482 {
483   //
484   // Returns the Vdrift calibration object
485   // containing one number per detector
486   //
487   
488   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
489                                        (GetCachedCDBObject(kIDNoiseChamber));
490   if (!calChamber) {
491     return 0;
492   }
493   else {
494     return calChamber;
495   }
496
497 }
498
499 //_____________________________________________________________________________
500 Float_t AliTRDcalibDB::GetVdrift(Int_t det, Int_t col, Int_t row)
501 {
502   //
503   // Returns the drift velocity for the given pad.
504   //
505
506   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
507                                    (GetCachedCDBObject(kIDVdriftPad));
508   if (!calPad) {
509     return -1;
510   }
511
512   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
513   if (!roc) {
514     return -1;
515   }
516
517   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
518                                    (GetCachedCDBObject(kIDVdriftChamber));
519   if (!calChamber) {
520     return -1;
521   }
522
523   return calChamber->GetValue(det) * roc->GetValue(col,row);
524
525 }
526  
527 //_____________________________________________________________________________
528 AliTRDCalROC *AliTRDcalibDB::GetVdriftROC(Int_t det)
529 {
530   //
531   // Returns the Vdrift calibration object for a given ROC
532   // containing one number per pad 
533   //
534   
535   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
536                                        (GetCachedCDBObject(kIDVdriftPad));
537   if (!calPad) {
538     return 0;
539   }
540
541   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
542   if (!roc) {
543     return 0;
544   }
545   else {
546     return roc;
547   }
548
549 }
550
551 //_____________________________________________________________________________
552 const AliTRDCalDet *AliTRDcalibDB::GetVdriftDet()
553 {
554   //
555   // Returns the Vdrift calibration object
556   // containing one number per detector
557   //
558   
559   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
560                                        (GetCachedCDBObject(kIDVdriftChamber));
561   if (!calChamber) {
562     return 0;
563   }
564   else {
565     return calChamber;
566   }
567
568 }
569
570 //_____________________________________________________________________________
571 TObjArray * AliTRDcalibDB::GetPHQ()
572 {
573   //
574   //return PHQ calibration object
575   //
576   TObjArray *arr = (TObjArray *) (GetCachedCDBObject(kIDPHQ));
577   return arr;
578 }
579
580 //_____________________________________________________________________________
581 Float_t AliTRDcalibDB::GetVdriftAverage(Int_t det)
582 {
583   //
584   // Returns the average drift velocity for the given detector
585   //
586
587   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
588                                    (GetCachedCDBObject(kIDVdriftChamber));
589   if (!calDet) {
590     return -1;
591   }
592
593   return calDet->GetValue(det);
594
595 }
596 //_____________________________________________________________________________
597 const AliTRDCalDet *AliTRDcalibDB::GetExBDet()
598 {
599   //
600   // Returns the exB calibration object
601   // containing one number per detector
602   //
603   
604   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> (GetCachedCDBObject(kIDExBChamber));
605   
606   Double_t meanexb = 100.0;
607   if (calChamber) meanexb = calChamber->GetMean();
608   //printf("mean %f\n",meanexb);  
609
610   if ((!calChamber) || (meanexb > 70.0)) {
611     
612     const AliTRDCalDet     *calChambervdrift = dynamic_cast<const AliTRDCalDet *> 
613         (GetCachedCDBObject(kIDVdriftChamber));
614       if (!calChambervdrift) {
615         return 0;
616       }
617       else {
618         AliTRDCalDet *calDetExB = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
619         for(Int_t k = 0; k < 540; k++){
620           calDetExB->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(calChambervdrift->GetValue(k)));
621         }
622         return calDetExB;
623       }
624   }
625   else return calChamber;
626
627 }
628 //_____________________________________________________________________________
629 Float_t AliTRDcalibDB::GetT0(Int_t det, Int_t col, Int_t row)
630 {
631   //
632   // Returns t0 for the given pad.
633   //
634   
635   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
636                                        (GetCachedCDBObject(kIDT0Pad));
637   if (!calPad) {
638     return -1;
639   }
640
641   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
642   if (!roc) {
643     return -1;
644   }
645
646   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
647                                        (GetCachedCDBObject(kIDT0Chamber));
648   if (!calChamber) {
649     return -1;
650   }
651
652   return calChamber->GetValue(det) + roc->GetValue(col,row);
653
654 }
655  
656 //_____________________________________________________________________________
657 AliTRDCalROC *AliTRDcalibDB::GetT0ROC(Int_t det)
658 {
659   //
660   // Returns the t0 calibration object for a given ROC
661   // containing one number per pad 
662   //
663   
664   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
665                                        (GetCachedCDBObject(kIDT0Pad));
666   if (!calPad) {
667     return 0;
668   }
669
670   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
671   if (!roc) {
672     return 0;
673   }
674   else {
675     return roc;
676   }
677
678 }
679
680 //_____________________________________________________________________________
681 const AliTRDCalDet *AliTRDcalibDB::GetT0Det()
682 {
683   //
684   // Returns the t0 calibration object
685   // containing one number per detector
686   //
687   
688   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
689                                        (GetCachedCDBObject(kIDT0Chamber));
690   if (!calChamber) {
691     return 0;
692   }
693   else {
694     return calChamber;
695   }
696
697 }
698
699 //_____________________________________________________________________________
700 Float_t AliTRDcalibDB::GetT0Average(Int_t det)
701 {
702   //
703   // Returns the average t0 for the given detector
704   //
705
706   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
707                                    (GetCachedCDBObject(kIDT0Pad));
708   if (!calPad) {
709     return -1;
710   }
711
712   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
713   if (!roc) {
714     return -1;
715   }
716
717   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
718                                    (GetCachedCDBObject(kIDT0Chamber));
719   if (!calDet) {
720     return -1;
721   }
722
723   Double_t sum = 0.0; 
724   for (Int_t channel = 0; channel < roc->GetNchannels(); ++channel) {
725     sum += roc->GetValue(channel);
726   }
727   sum /= roc->GetNchannels();
728   sum += calDet->GetValue(det);
729   return sum;
730
731 }
732
733 //_____________________________________________________________________________
734 AliTRDCalOnlineGainTableROC* AliTRDcalibDB::GetOnlineGainTableROC(Int_t det)
735 {
736   //
737   // Returns the online gain factor table for a given ROC.
738   //
739
740   if (!HasOnlineFilterGain()) {
741     return 0x0;
742   }
743   
744   const AliTRDCalOnlineGainTable *calOnline 
745      = dynamic_cast<const AliTRDCalOnlineGainTable *> 
746                                    (GetCachedCDBObject(kIDOnlineGainFactor));
747   if (!calOnline) {
748     return 0x0;
749   }
750
751   return calOnline->GetGainTableROC(det);
752
753 }
754
755 //_____________________________________________________________________________
756 Float_t AliTRDcalibDB::GetOnlineGainFactor(Int_t det, Int_t col, Int_t row)
757 {
758   //
759   // Returns the online gain factor for the given pad.
760   //
761
762   if (!HasOnlineFilterGain()) {
763     return -1;
764   }
765   
766   const AliTRDCalOnlineGainTable *calOnline 
767      = dynamic_cast<const AliTRDCalOnlineGainTable *> 
768                                    (GetCachedCDBObject(kIDOnlineGainFactor));
769   if (!calOnline) {
770     return -1;
771   }
772
773   return calOnline->GetGainCorrectionFactor(det,row,col);
774
775 }
776
777 //_____________________________________________________________________________
778 AliTRDCalROC *AliTRDcalibDB::GetGainFactorROC(Int_t det)
779 {
780   //
781   // Returns the gain factor calibration object for a given ROC
782   //
783   
784   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
785                                    (GetCachedCDBObject(kIDGainFactorPad));
786   if (!calPad) {
787     return 0;
788   }
789
790   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
791   if (!roc) {
792     return 0;
793   }
794   else {
795     return roc;
796   }
797
798 }
799
800 //_____________________________________________________________________________
801 Float_t AliTRDcalibDB::GetGainFactor(Int_t det, Int_t col, Int_t row)
802 {
803   //
804   // Returns the gain factor for the given pad.
805   //
806   
807   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
808                                    (GetCachedCDBObject(kIDGainFactorPad));
809   if (!calPad) {
810     return -1;
811   }
812
813   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
814   if (!roc) {
815     return -1;
816   }
817
818   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
819                                    (GetCachedCDBObject(kIDGainFactorChamber));
820   if (!calChamber) {
821     return -1;
822   }
823
824   return calChamber->GetValue(det) * roc->GetValue(col,row);
825
826 }
827
828 //_____________________________________________________________________________
829 const AliTRDCalDet *AliTRDcalibDB::GetGainFactorDet()
830 {
831   //
832   // Returns the gain factor calibration object
833   // containing one number per detector
834   //
835
836   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
837                                    (GetCachedCDBObject(kIDGainFactorChamber));
838   if (!calChamber) {
839     return 0;
840   }
841   else {
842     return calChamber;
843   }
844
845 }
846
847 //_____________________________________________________________________________
848 Float_t AliTRDcalibDB::GetGainFactorAverage(Int_t det)
849 {
850   //
851   // Returns the average gain factor for the given detector
852   //
853
854   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
855                                    (GetCachedCDBObject(kIDGainFactorChamber));
856   if (!calDet) {
857     return -1;
858   }
859
860   return calDet->GetValue(det);
861
862 }
863
864 //_____________________________________________________________________________
865 AliTRDCalROC *AliTRDcalibDB::GetPRFROC(Int_t det)
866 {
867   //
868   // Returns the PRF calibration object for a given ROC
869   // containing one number per pad 
870   //
871   
872   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
873                                        (GetCachedCDBObject(kIDPRFWidth));
874   if (!calPad) {
875     return 0;
876   }
877
878   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
879   if (!roc) {
880     return 0;
881   }
882   else {
883     return roc;
884   }
885
886 }
887
888 //_____________________________________________________________________________
889 Float_t AliTRDcalibDB::GetPRFWidth(Int_t det, Int_t col, Int_t row)
890 {
891   //
892   // Returns the PRF width for the given pad.
893   //
894   
895   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
896                                    (GetCachedCDBObject(kIDPRFWidth));
897   if (!calPad) {
898     return -1;
899   }
900
901   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
902   if (!roc) {
903     return -1;
904   }
905
906   return roc->GetValue(col,row);
907
908 }
909   
910 //_____________________________________________________________________________
911 Int_t AliTRDcalibDB::ExtractTimeBinsFromString(TString tbstr)
912 {
913   // Check if there is any content in the string first
914   if (tbstr.Length() == 0) {
915     AliError("Parameter for number of timebins is empty!");
916     return -1;
917   }
918
919   // Check if we have the correct config parameter
920   TString tbident  = "tb";
921   TString tbsubstr = tbstr(0,2);
922   if (!tbsubstr.EqualTo(tbident)) {
923     AliError(Form("Parameter for number of timebins is corrupted (%s)!", tbstr.Data()));
924     return -1;
925   }
926
927   tbstr.Remove(0,2);
928   // check if there is more than a number
929   if (!tbstr.IsDigit()) {
930     AliError(Form("Parameter for number of timebins is corrupted (%s)!", tbstr.Data()));
931     return -1;
932   }
933
934   return tbstr.Atoi();
935
936 }
937
938 //_____________________________________________________________________________
939 Int_t AliTRDcalibDB::GetNumberOfTimeBinsDCS()
940 {
941   //
942   // Returns number of time bins from the DCS
943   //
944
945   // Get the corresponding parameter
946   TString cfgstr = "", cfgname = "";
947   GetGlobalConfiguration(cfgname);
948   if(cfgname.Length()==0)
949     return -1;
950   GetDCSConfigParOption(cfgname, kTimebin, 0, cfgstr);
951   if(cfgstr.Length()==0)
952     return -1;
953   return ExtractTimeBinsFromString(cfgstr);
954
955 }
956
957 //_____________________________________________________________________________
958 void AliTRDcalibDB::GetFilterType(TString &filterType)
959 {
960   //
961   // Returns the filter type
962   //
963
964   TString cfgname = "";
965   GetGlobalConfiguration(cfgname);
966   GetDCSConfigParOption(cfgname, kFltrSet, 0, filterType);
967
968 }
969
970 //_____________________________________________________________________________
971 Int_t AliTRDcalibDB::GetOnlineGainTableID()
972 {
973   //
974   // Get the gain table ID from the DCS
975   //
976
977   if (fOnlineGainTableID > 0) {
978     return fOnlineGainTableID;
979   }
980
981   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
982   if (!dcsArr){
983     return -1;
984   }
985
986   Int_t esor   = 0; // Take SOR
987   Int_t calver = 0; // Check CalDCS version
988   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS"))   calver = 1;
989   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
990
991   if      (calver == 1) {
992
993     // No data for old DCS object available, anyway
994     return -1;
995
996   } 
997   else if (calver == 2) {
998
999     // DCSv2 object
1000     const AliTRDCalDCSv2 *calDCSv2 = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(esor));
1001     if(!calDCSv2){
1002       return -1;
1003     }
1004
1005     TString tableName = "";
1006     for (Int_t i = 0; i < 540; i++) {
1007       const AliTRDCalDCSFEEv2 *calDCSFEEv2 = calDCSv2->GetCalDCSFEEObj(0);
1008       tableName = calDCSFEEv2->GetGainTableName();
1009       if (tableName.Length() > 0) {
1010         break;
1011       }
1012     }
1013     if (tableName.CompareTo("Krypton_2011-01")               == 0) {
1014       fOnlineGainTableID = 1;
1015       return fOnlineGainTableID;
1016     }
1017     if (tableName.CompareTo("Gaintbl_Uniform_FGAN0_2011-01") == 0) {
1018       fOnlineGainTableID = 2;
1019       return fOnlineGainTableID;
1020     }
1021     if (tableName.CompareTo("Gaintbl_Uniform_FGAN8_2011-01") == 0) {
1022       fOnlineGainTableID = 3;
1023       return fOnlineGainTableID;
1024     }
1025     if (tableName.CompareTo("Krypton_2011-02")               == 0) {
1026       fOnlineGainTableID = 4;
1027       return fOnlineGainTableID;
1028     }
1029     if (tableName.CompareTo("Krypton_2011-03")               == 0) {
1030       fOnlineGainTableID = 5;
1031       return fOnlineGainTableID;
1032     }
1033     if (tableName.CompareTo("Gaintbl_Uniform_FGAN0_2012-01") == 0) {
1034       fOnlineGainTableID = 6;
1035       return fOnlineGainTableID;
1036     }
1037     if (tableName.CompareTo("Gaintbl_Uniform_FGAN8_2012-01") == 0) {
1038       fOnlineGainTableID = 7;
1039       return fOnlineGainTableID;
1040     }
1041     if (tableName.CompareTo("Krypton_2011-03")               == 0) {
1042       fOnlineGainTableID = 8;
1043       return fOnlineGainTableID;
1044     }
1045
1046   } 
1047   else {
1048
1049     AliError("NO DCS/DCSv2 OCDB entry found!");
1050     return -1;
1051
1052   }
1053
1054   return -1;
1055
1056 }
1057
1058 //_____________________________________________________________________________
1059 void AliTRDcalibDB::GetGlobalConfiguration(TString &config)
1060 {
1061   //
1062   // Get Configuration from the DCS
1063   //
1064
1065   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
1066   if(!dcsArr){
1067     AliError("No DCS CDB Object available!");
1068     config = "";
1069     return;
1070   }
1071
1072   Int_t idSOR = 0, idEOR=1; // The index of SOR and EOR
1073   Bool_t hasSOR = (dcsArr->At(idSOR));
1074   Bool_t hasEOR = (dcsArr->At(idEOR));
1075   TString cfgSOR = "", cfgEOR = ""; // The configuration at SOR/EOR
1076
1077   // The SOR object is mandatory
1078   if (!hasSOR) {
1079     AliError("NO SOR object found in CDB file!");
1080     config = "";
1081     return;
1082   }
1083   if (!hasEOR) AliWarning("NO EOR object found in CDB file!");
1084
1085   // Check CalDCS version
1086   Int_t calver = 0;
1087   if (!strcmp(dcsArr->At(idSOR)->ClassName(),"AliTRDCalDCS")) calver = 1;
1088   else if (!strcmp(dcsArr->At(idSOR)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
1089
1090   // Get the configuration strings
1091   if (calver == 1) {
1092     // DCS object
1093     const AliTRDCalDCS *calSOR = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(idSOR));
1094     cfgSOR = calSOR->GetGlobalConfigName();
1095     if (hasEOR) {
1096       const AliTRDCalDCS *calEOR = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(idEOR));
1097       cfgEOR = calEOR->GetGlobalConfigName();
1098     }
1099   } 
1100   else if (calver == 2) {
1101     // DCSv2 object
1102     const AliTRDCalDCSv2 *calv2SOR = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(idSOR));
1103     cfgSOR = calv2SOR->GetGlobalConfigName();
1104     if (hasEOR) {
1105       const AliTRDCalDCSv2 *calv2EOR = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(idEOR));
1106       cfgEOR = calv2EOR->GetGlobalConfigName();
1107     }
1108   } 
1109   else {
1110     AliError("NO DCS/DCSv2 OCDB entry found!");
1111     config = "";
1112     return;
1113   }
1114
1115   // If there is no EOR entry, return the SOR value
1116   if (!hasEOR || cfgEOR.Length()==0) {
1117     config = cfgSOR;
1118     return;
1119   }
1120
1121   // Check if the configuration is the same for both
1122   if (cfgSOR.EqualTo(cfgEOR)) {
1123     config = cfgSOR;
1124     return;
1125   }
1126
1127   // When both SOR and EOR have an entry but are different, the config is not defined
1128   AliError("Inconsistent configuration at start and end of run found!");
1129   config = "";
1130   return;
1131
1132 }
1133
1134 //_____________________________________________________________________________
1135 void AliTRDcalibDB::GetGlobalConfigurationVersion(TString &version)
1136 {
1137   //
1138   // Get Version of Configuration from the DCS
1139   //
1140
1141   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
1142   if(!dcsArr){
1143     version = "";
1144     return;
1145   }
1146
1147   Int_t esor   = 0; // Take SOR
1148   Int_t calver = 0; // Check CalDCS version
1149   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS"))   calver = 1;
1150   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
1151
1152   if      (calver == 1) {
1153
1154     // DCS object
1155     const AliTRDCalDCS   *calDCS   = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(esor));
1156     if(!calDCS){
1157       version = "";
1158       return;
1159     } 
1160     version = calDCS->GetGlobalConfigVersion();
1161
1162   } 
1163   else if (calver == 2) {
1164
1165     // DCSv2 object
1166     const AliTRDCalDCSv2 *calDCSv2 = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(esor));
1167     if(!calDCSv2){
1168       version = "";
1169       return;
1170     } 
1171     version = calDCSv2->GetGlobalConfigVersion();
1172
1173   } 
1174   else {
1175
1176     AliError("NO DCS/DCSv2 OCDB entry found!");
1177
1178   }
1179
1180 }
1181
1182 //_____________________________________________________________________________
1183 Int_t AliTRDcalibDB::GetNumberOfParsDCS(TString cname, Char_t delimiter)
1184 {
1185   // Get the number of configuration parameters from the DCS config
1186
1187   //AliInfo(Form("\"%s\" tokenized by \"%c\"", cname.Data(), delimiter));
1188   if(!cname.Length()) return -1;  // -1 for the "cf"
1189   Int_t nconf(0);
1190   for(Int_t ich(1); ich<cname.Length()-1; ich++){ if(cname[ich]==delimiter) nconf++;}
1191   return nconf;
1192 }
1193
1194 //_____________________________________________________________________________
1195 Int_t AliTRDcalibDB::GetNumberOfOptsDCS(TString cname, Int_t cfgType)
1196 {
1197   // Get the number of options of a given configuration parameter from DCS
1198
1199   Char_t cdelim = '_', // define the delimiters
1200          odelim = '-';
1201   Int_t nconfig = GetNumberOfParsDCS(cname, cdelim);
1202
1203   // protect
1204   if ((nconfig == -1) || (nconfig < cfgType)) {
1205     AliError("Not enough parameters in DCS configuration name!");
1206     return 0;
1207   }
1208
1209   TObjArray *carr = cname.Tokenize(cdelim);
1210   Int_t nopt = GetNumberOfParsDCS(((TObjString*)carr->At(cfgType))->GetString(), odelim);
1211   carr->Delete(); delete carr;
1212   return nopt;
1213 }
1214
1215 //_____________________________________________________________________________
1216 void AliTRDcalibDB::GetDCSConfigParOption(TString cname, Int_t cfgType, Int_t option, TString &cfgo)
1217 {
1218   //
1219   // Get a configuration (see enum in header file) or the options of a configuration
1220   // option == 0 returns the configuration itself
1221   // option >  0 returns the optional parameter Nr. (option) of the configuration (cfgType)
1222   //
1223
1224   Char_t cdelim = '_', // define the delimiters
1225          odelim = '-';
1226
1227   Int_t nconfig = GetNumberOfParsDCS(cname, cdelim);
1228   // protect
1229   if (nconfig == -1) {
1230     AliError("DCS configuration name empty!");
1231     cfgo = "";
1232     return;
1233   } else if (nconfig < cfgType) {
1234     AliError("Not enough parameters in DCS configuration name!");
1235     cfgo = "";
1236     return;
1237   }
1238
1239   TObjArray *carr = cname.Tokenize(cdelim);
1240   TString cfgString(((TObjString*)carr->At(cfgType))->GetString());
1241   Int_t noptions = GetNumberOfParsDCS(cfgString, odelim);
1242   // protect
1243   if (noptions < option) {
1244     AliError("Not enough options in DCS configuration name!");
1245     cfgo = "";
1246     carr->Delete(); delete carr;
1247     return;
1248   }
1249   TObjArray *oarr = cfgString.Tokenize(odelim);
1250   cfgo = ((TObjString*)oarr->At(option))->GetString();
1251   carr->Delete(); delete carr;
1252   oarr->Delete(); delete oarr;
1253   return;
1254
1255 }
1256
1257 //_____________________________________________________________________________
1258 Bool_t AliTRDcalibDB::HasOnlineFilterPedestal()
1259 {
1260   //
1261   // Checks whether pedestal filter was applied online
1262   //
1263
1264   TString filterconfig;
1265   GetFilterType(filterconfig);
1266
1267   return filterconfig.Contains("p");
1268
1269 }
1270
1271 //_____________________________________________________________________________
1272 Bool_t AliTRDcalibDB::HasOnlineFilterGain()
1273 {
1274   //
1275   // Checks whether online gain filter was applied
1276   //
1277
1278   TString filterconfig;
1279   GetFilterType(filterconfig);
1280
1281   return filterconfig.Contains("g");
1282
1283 }
1284
1285 //_____________________________________________________________________________
1286 Bool_t AliTRDcalibDB::HasOnlineTailCancellation()
1287 {
1288   //
1289   // Checks whether online tail cancellation was applied
1290   //
1291
1292   TString filterconfig;
1293   GetFilterType(filterconfig);
1294
1295   return filterconfig.Contains("t");
1296
1297 }
1298
1299 //_____________________________________________________________________________
1300 Char_t AliTRDcalibDB::GetPadStatus(Int_t det, Int_t col, Int_t row)
1301 {
1302   //
1303   // Returns the status of the given pad
1304   //
1305
1306   const AliTRDCalPadStatus *cal  = dynamic_cast<const AliTRDCalPadStatus *> 
1307                                    (GetCachedCDBObject(kIDPadStatus));
1308   if (!cal) {
1309     return -1;
1310   }
1311
1312   const AliTRDCalSingleChamberStatus *roc = cal->GetCalROC(det);
1313   if (!roc) {
1314     return -1;
1315   }
1316
1317   return roc->GetStatus(col,row);
1318
1319 }
1320
1321 //_____________________________________________________________________________
1322 AliTRDCalSingleChamberStatus* AliTRDcalibDB::GetPadStatusROC(Int_t det)
1323 {
1324   //
1325   // Returns the pad status calibration object for a given ROC
1326   //
1327
1328   const AliTRDCalPadStatus *cal  = dynamic_cast<const AliTRDCalPadStatus *> 
1329                                    (GetCachedCDBObject(kIDPadStatus));
1330   if (!cal) {
1331     return 0;
1332   }
1333
1334   AliTRDCalSingleChamberStatus *roc = cal->GetCalROC(det);
1335   if (!roc) {
1336     return 0;
1337   }
1338   else {
1339     return roc;
1340   }
1341
1342 }
1343
1344 //_____________________________________________________________________________
1345 Char_t AliTRDcalibDB::GetChamberStatus(Int_t det)
1346 {
1347   //
1348   // Returns the status of the given chamber
1349   //
1350
1351   const AliTRDCalChamberStatus *cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1352                                       (GetCachedCDBObject(kIDChamberStatus));
1353   if (!cal) {
1354     return -1;
1355   }
1356
1357   return cal->GetStatus(det);
1358
1359 }
1360
1361 //_____________________________________________________________________________
1362 AliTRDrecoParam* AliTRDcalibDB::GetRecoParam(Int_t */*eventtype*/)
1363 {
1364   //
1365   // Returns the TRD reconstruction parameters from the OCDB
1366   //
1367
1368   const TClonesArray *recos = dynamic_cast<const TClonesArray*>(GetCachedCDBObject(kIDRecoParam));
1369   if (!recos) return 0x0;
1370
1371   // calculate entry based on event type info
1372   Int_t n = 0; //f(eventtype[0], eventtype[1], ....)
1373
1374   return (AliTRDrecoParam *) recos->UncheckedAt(n);
1375
1376 }
1377
1378 //_____________________________________________________________________________
1379 Bool_t AliTRDcalibDB::IsPadMasked(Int_t det, Int_t col, Int_t row)
1380 {
1381   //
1382   // Returns status, see name of functions for details ;-)
1383   //
1384
1385   const AliTRDCalPadStatus         *cal = dynamic_cast<const AliTRDCalPadStatus *> 
1386                                           (GetCachedCDBObject(kIDPadStatus));
1387   if (!cal) {
1388     return -1;
1389   }
1390
1391   return cal->IsMasked(det,col,row);
1392
1393 }
1394
1395 //_____________________________________________________________________________
1396 Bool_t AliTRDcalibDB::IsPadBridgedLeft(Int_t det, Int_t col, Int_t row)
1397 {
1398   //
1399   // Returns status, see name of functions for details ;-)
1400   //
1401
1402   const AliTRDCalPadStatus         *cal = dynamic_cast<const AliTRDCalPadStatus *> 
1403                                           (GetCachedCDBObject(kIDPadStatus));
1404   if (!cal) {
1405     return -1;
1406   }
1407
1408   return cal->IsBridgedLeft(det,col,row);
1409
1410 }
1411
1412 //_____________________________________________________________________________
1413 Bool_t AliTRDcalibDB::IsPadBridgedRight(Int_t det, Int_t col, Int_t row)
1414 {
1415   //
1416   // Returns status, see name of functions for details ;-)
1417   //
1418
1419   const AliTRDCalPadStatus         * cal = dynamic_cast<const AliTRDCalPadStatus *> 
1420                                            (GetCachedCDBObject(kIDPadStatus));
1421   if (!cal) {
1422     return -1;
1423   }
1424
1425   return cal->IsBridgedRight(det,col,row);
1426
1427 }
1428
1429 //_____________________________________________________________________________
1430 Bool_t AliTRDcalibDB::IsPadNotConnected(Int_t det, Int_t col, Int_t row)
1431 {
1432   //
1433   // Returns status, see name of functions for details ;-)
1434   //
1435
1436   const AliTRDCalPadStatus         * cal = dynamic_cast<const AliTRDCalPadStatus *> 
1437                                            (GetCachedCDBObject(kIDPadStatus));
1438   if (!cal) {
1439     return -1;
1440   }
1441
1442   return cal->IsNotConnected(det,col,row);
1443
1444 }
1445
1446 //_____________________________________________________________________________
1447 Bool_t AliTRDcalibDB::IsChamberGood(Int_t det)
1448 {
1449   //
1450   // Returns status, see name of functions for details ;-)
1451   //
1452
1453   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1454                                            (GetCachedCDBObject(kIDChamberStatus));
1455   if (!cal) {
1456     return -1;
1457   }
1458
1459   return cal->IsGood(det);
1460
1461 }
1462
1463 //_____________________________________________________________________________
1464 Bool_t AliTRDcalibDB::IsChamberNoData(Int_t det)
1465 {
1466   //
1467   // Returns status, see name of functions for details ;-)
1468   //
1469
1470   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1471                                            (GetCachedCDBObject(kIDChamberStatus));
1472   if (!cal) {
1473     return -1;
1474   }
1475
1476   return cal->IsNoData(det);
1477
1478 }
1479
1480 //_____________________________________________________________________________
1481 Bool_t AliTRDcalibDB::IsHalfChamberNoData(Int_t det, Int_t side)
1482 {
1483   //
1484   // Returns status, see name of functions for details ;-)
1485   //
1486
1487   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1488                                            (GetCachedCDBObject(kIDChamberStatus));
1489   if (!cal) {
1490     return -1;
1491   }
1492
1493   return side > 0 ? cal->IsNoDataSideB(det) : cal->IsNoDataSideA(det);
1494
1495 }
1496
1497 //_____________________________________________________________________________
1498 Bool_t AliTRDcalibDB::IsChamberBadCalibrated(Int_t det)
1499 {
1500   //
1501   // Returns status, see name of functions for details ;-)
1502   //
1503
1504   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1505                                            (GetCachedCDBObject(kIDChamberStatus));
1506   if (!cal) {
1507     return -1;
1508   }
1509
1510   return cal->IsBadCalibrated(det);
1511
1512 }
1513
1514 //_____________________________________________________________________________
1515 Bool_t AliTRDcalibDB::IsChamberNotCalibrated(Int_t det)
1516 {
1517   //
1518   // Returns status, see name of functions for details ;-)
1519   //
1520
1521   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1522                                            (GetCachedCDBObject(kIDChamberStatus));
1523   if (!cal) {
1524     return -1;
1525   }
1526
1527   return cal->IsNotCalibrated(det);
1528
1529 }
1530
1531 //_____________________________________________________________________________
1532 const AliTRDCalPID *AliTRDcalibDB::GetPIDObject(AliTRDpidUtil::ETRDPIDMethod method)
1533 {
1534   //
1535   // Returns the object storing the distributions for PID with likelihood
1536   //
1537
1538   switch(method) {
1539   case AliTRDpidUtil::kLQ: 
1540     return dynamic_cast<const AliTRDCalPID *>(GetCachedCDBObject(kIDPIDLQ));
1541   case AliTRDpidUtil::kNN: 
1542     return dynamic_cast<const AliTRDCalPID *>(GetCachedCDBObject(kIDPIDNN));
1543   case AliTRDpidUtil::kESD:
1544     return 0x0; // To avoid compiler warnings 
1545   }
1546
1547   return 0x0;
1548
1549 }
1550
1551 //_____________________________________________________________________________
1552 AliTRDPIDResponse *AliTRDcalibDB::GetPIDResponse(AliTRDPIDResponse::ETRDPIDMethod /*method*/)
1553 {
1554   //
1555   // Returns the PID response object for 1D-LQ
1556   //
1557
1558   if (!fPIDResponse) {
1559     AliDebug(2, "Setting new PID response.");
1560
1561     fPIDResponse = new AliTRDPIDResponse;
1562
1563     // Load Reference Histos from OCDB
1564 //    if(method == AliTRDPIDResponse::kLQ1D){
1565     //fPIDResponse->SetPIDmethod(AliTRDPIDResponse::kLQ1D);
1566     const TObjArray *references = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDPIDLQ1D));
1567
1568     TIter refs(references);
1569     TObject *obj = NULL;
1570     AliTRDPIDReference *ref = NULL;
1571     Bool_t hasReference = kFALSE;
1572     while ((obj = refs())){
1573       if ((ref = dynamic_cast<AliTRDPIDReference *>(obj))){
1574         AliDebug(2, "Setting new PID response object.");
1575         TDirectory *bkpdir = gDirectory;
1576         gROOT->cd();
1577         AliTRDPIDResponseObject *ro = new AliTRDPIDResponseObject;
1578         ro->SetPIDReference(ref);
1579         fPIDResponse->SetPIDResponseObject(ro);
1580         hasReference = kTRUE;
1581         gDirectory = bkpdir;
1582         break;
1583       }
1584     }
1585
1586     if (!hasReference) {
1587       AliError("Reference histograms not found in the OCDB");
1588     }
1589   }
1590
1591 //  }
1592
1593   return fPIDResponse;
1594
1595 }
1596
1597 //_____________________________________________________________________________
1598 const AliTRDCalTrkAttach* AliTRDcalibDB::GetAttachObject()
1599 {
1600   //
1601   // Returns the object storing likelihood distributions for cluster to track attachment
1602   //
1603
1604   return dynamic_cast<const AliTRDCalTrkAttach*>(GetCachedCDBObject(kIDAttach));
1605
1606 }
1607
1608 //_____________________________________________________________________________
1609 const AliTRDCalMonitoring *AliTRDcalibDB::GetMonitoringObject()
1610 {
1611   //
1612   // Returns the object storing the monitoring data
1613   //
1614
1615   return dynamic_cast<const AliTRDCalMonitoring *> 
1616          (GetCachedCDBObject(kIDMonitoringData));
1617    
1618 }
1619
1620 //_____________________________________________________________________________
1621 void AliTRDcalibDB::SamplePRF()
1622 {
1623   //
1624   // Samples the pad response function (should maybe go somewhere else ...)
1625   //
1626
1627   const Int_t kPRFbin = 61;
1628
1629   Float_t prf[kNlayer][kPRFbin] = { 
1630                    {2.9037e-02, 3.3608e-02, 3.9020e-02, 4.5292e-02,
1631                     5.2694e-02, 6.1362e-02, 7.1461e-02, 8.3362e-02,
1632                     9.7063e-02, 1.1307e-01, 1.3140e-01, 1.5235e-01,
1633                     1.7623e-01, 2.0290e-01, 2.3294e-01, 2.6586e-01,
1634                     3.0177e-01, 3.4028e-01, 3.8077e-01, 4.2267e-01,
1635                     4.6493e-01, 5.0657e-01, 5.4655e-01, 5.8397e-01,
1636                     6.1767e-01, 6.4744e-01, 6.7212e-01, 6.9188e-01,
1637                     7.0627e-01, 7.1499e-01, 7.1851e-01, 7.1499e-01,
1638                     7.0627e-01, 6.9188e-01, 6.7212e-01, 6.4744e-01,
1639                     6.1767e-01, 5.8397e-01, 5.4655e-01, 5.0657e-01,
1640                     4.6493e-01, 4.2267e-01, 3.8077e-01, 3.4028e-01,
1641                     3.0177e-01, 2.6586e-01, 2.3294e-01, 2.0290e-01,
1642                     1.7623e-01, 1.5235e-01, 1.3140e-01, 1.1307e-01,
1643                     9.7063e-02, 8.3362e-02, 7.1461e-02, 6.1362e-02,
1644                     5.2694e-02, 4.5292e-02, 3.9020e-02, 3.3608e-02,
1645                     2.9037e-02},
1646                    {2.5478e-02, 2.9695e-02, 3.4655e-02, 4.0454e-02,
1647                     4.7342e-02, 5.5487e-02, 6.5038e-02, 7.6378e-02,
1648                     8.9696e-02, 1.0516e-01, 1.2327e-01, 1.4415e-01,
1649                     1.6794e-01, 1.9516e-01, 2.2573e-01, 2.5959e-01,
1650                     2.9694e-01, 3.3719e-01, 3.7978e-01, 4.2407e-01,
1651                     4.6889e-01, 5.1322e-01, 5.5569e-01, 5.9535e-01,
1652                     6.3141e-01, 6.6259e-01, 6.8882e-01, 7.0983e-01,
1653                     7.2471e-01, 7.3398e-01, 7.3761e-01, 7.3398e-01,
1654                     7.2471e-01, 7.0983e-01, 6.8882e-01, 6.6259e-01,
1655                     6.3141e-01, 5.9535e-01, 5.5569e-01, 5.1322e-01,
1656                     4.6889e-01, 4.2407e-01, 3.7978e-01, 3.3719e-01,
1657                     2.9694e-01, 2.5959e-01, 2.2573e-01, 1.9516e-01,
1658                     1.6794e-01, 1.4415e-01, 1.2327e-01, 1.0516e-01,
1659                     8.9696e-02, 7.6378e-02, 6.5038e-02, 5.5487e-02,
1660                     4.7342e-02, 4.0454e-02, 3.4655e-02, 2.9695e-02,
1661                     2.5478e-02},
1662                    {2.2363e-02, 2.6233e-02, 3.0782e-02, 3.6140e-02,
1663                     4.2535e-02, 5.0157e-02, 5.9197e-02, 6.9900e-02,
1664                     8.2707e-02, 9.7811e-02, 1.1548e-01, 1.3601e-01,
1665                     1.5998e-01, 1.8739e-01, 2.1840e-01, 2.5318e-01,
1666                     2.9182e-01, 3.3373e-01, 3.7837e-01, 4.2498e-01,
1667                     4.7235e-01, 5.1918e-01, 5.6426e-01, 6.0621e-01,
1668                     6.4399e-01, 6.7700e-01, 7.0472e-01, 7.2637e-01,
1669                     7.4206e-01, 7.5179e-01, 7.5551e-01, 7.5179e-01,
1670                     7.4206e-01, 7.2637e-01, 7.0472e-01, 6.7700e-01,
1671                     6.4399e-01, 6.0621e-01, 5.6426e-01, 5.1918e-01,
1672                     4.7235e-01, 4.2498e-01, 3.7837e-01, 3.3373e-01,
1673                     2.9182e-01, 2.5318e-01, 2.1840e-01, 1.8739e-01,
1674                     1.5998e-01, 1.3601e-01, 1.1548e-01, 9.7811e-02,
1675                     8.2707e-02, 6.9900e-02, 5.9197e-02, 5.0157e-02,
1676                     4.2535e-02, 3.6140e-02, 3.0782e-02, 2.6233e-02,
1677                     2.2363e-02},
1678                    {1.9635e-02, 2.3167e-02, 2.7343e-02, 3.2293e-02,
1679                     3.8224e-02, 4.5335e-02, 5.3849e-02, 6.4039e-02,
1680                     7.6210e-02, 9.0739e-02, 1.0805e-01, 1.2841e-01,
1681                     1.5216e-01, 1.7960e-01, 2.1099e-01, 2.4671e-01,
1682                     2.8647e-01, 3.2996e-01, 3.7660e-01, 4.2547e-01,
1683                     4.7536e-01, 5.2473e-01, 5.7215e-01, 6.1632e-01,
1684                     6.5616e-01, 6.9075e-01, 7.1939e-01, 7.4199e-01,
1685                     7.5838e-01, 7.6848e-01, 7.7227e-01, 7.6848e-01,
1686                     7.5838e-01, 7.4199e-01, 7.1939e-01, 6.9075e-01,
1687                     6.5616e-01, 6.1632e-01, 5.7215e-01, 5.2473e-01,
1688                     4.7536e-01, 4.2547e-01, 3.7660e-01, 3.2996e-01,
1689                     2.8647e-01, 2.4671e-01, 2.1099e-01, 1.7960e-01,
1690                     1.5216e-01, 1.2841e-01, 1.0805e-01, 9.0739e-02,
1691                     7.6210e-02, 6.4039e-02, 5.3849e-02, 4.5335e-02,
1692                     3.8224e-02, 3.2293e-02, 2.7343e-02, 2.3167e-02,
1693                     1.9635e-02},
1694                    {1.7224e-02, 2.0450e-02, 2.4286e-02, 2.8860e-02,
1695                     3.4357e-02, 4.0979e-02, 4.8966e-02, 5.8612e-02,
1696                     7.0253e-02, 8.4257e-02, 1.0102e-01, 1.2094e-01,
1697                     1.4442e-01, 1.7196e-01, 2.0381e-01, 2.4013e-01,
1698                     2.8093e-01, 3.2594e-01, 3.7450e-01, 4.2563e-01,
1699                     4.7796e-01, 5.2991e-01, 5.7974e-01, 6.2599e-01,
1700                     6.6750e-01, 7.0344e-01, 7.3329e-01, 7.5676e-01,
1701                     7.7371e-01, 7.8410e-01, 7.8793e-01, 7.8410e-01,
1702                     7.7371e-01, 7.5676e-01, 7.3329e-01, 7.0344e-01,
1703                     6.6750e-01, 6.2599e-01, 5.7974e-01, 5.2991e-01,
1704                     4.7796e-01, 4.2563e-01, 3.7450e-01, 3.2594e-01,
1705                     2.8093e-01, 2.4013e-01, 2.0381e-01, 1.7196e-01,
1706                     1.4442e-01, 1.2094e-01, 1.0102e-01, 8.4257e-02,
1707                     7.0253e-02, 5.8612e-02, 4.8966e-02, 4.0979e-02,
1708                     3.4357e-02, 2.8860e-02, 2.4286e-02, 2.0450e-02,
1709                     1.7224e-02},
1710                    {1.5096e-02, 1.8041e-02, 2.1566e-02, 2.5793e-02,
1711                     3.0886e-02, 3.7044e-02, 4.4515e-02, 5.3604e-02,
1712                     6.4668e-02, 7.8109e-02, 9.4364e-02, 1.1389e-01,
1713                     1.3716e-01, 1.6461e-01, 1.9663e-01, 2.3350e-01,
1714                     2.7527e-01, 3.2170e-01, 3.7214e-01, 4.2549e-01,
1715                     4.8024e-01, 5.3460e-01, 5.8677e-01, 6.3512e-01,
1716                     6.7838e-01, 7.1569e-01, 7.4655e-01, 7.7071e-01,
1717                     7.8810e-01, 7.9871e-01, 8.0255e-01, 7.9871e-01,
1718                     7.8810e-01, 7.7071e-01, 7.4655e-01, 7.1569e-01,
1719                     6.7838e-01, 6.3512e-01, 5.8677e-01, 5.3460e-01,
1720                     4.8024e-01, 4.2549e-01, 3.7214e-01, 3.2170e-01,
1721                     2.7527e-01, 2.3350e-01, 1.9663e-01, 1.6461e-01,
1722                     1.3716e-01, 1.1389e-01, 9.4364e-02, 7.8109e-02,
1723                     6.4668e-02, 5.3604e-02, 4.4515e-02, 3.7044e-02,
1724                     3.0886e-02, 2.5793e-02, 2.1566e-02, 1.8041e-02,
1725                     1.5096e-02}};
1726
1727   // More sampling precision with linear interpolation
1728   fPRFlo  = -1.5;
1729   fPRFhi  =  1.5;
1730   Float_t pad[kPRFbin];
1731   Int_t   sPRFbin = kPRFbin;  
1732   Float_t sPRFwid = (fPRFhi - fPRFlo) / ((Float_t) sPRFbin);
1733   for (Int_t iPad = 0; iPad < sPRFbin; iPad++) {
1734     pad[iPad] = ((Float_t) iPad + 0.5) * sPRFwid + fPRFlo;
1735   }
1736   fPRFbin = 500;  
1737   fPRFwid = (fPRFhi - fPRFlo) / ((Float_t) fPRFbin);
1738   fPRFpad = ((Int_t) (1.0 / fPRFwid));
1739
1740   if (fPRFsmp) delete [] fPRFsmp;
1741   fPRFsmp = new Float_t[kNlayer*fPRFbin];
1742
1743   Int_t   ipos1;
1744   Int_t   ipos2;
1745   Float_t diff;
1746
1747   for (Int_t iLayer = 0; iLayer < kNlayer; iLayer++) {
1748
1749     for (Int_t iBin = 0; iBin < fPRFbin; iBin++) {
1750
1751       Float_t bin = (((Float_t) iBin) + 0.5) * fPRFwid + fPRFlo;
1752       ipos1 = ipos2 = 0;
1753       diff  = 0;
1754       do {
1755         diff = bin - pad[ipos2++];
1756       } while ((diff > 0) && (ipos2 < kPRFbin));
1757       if      (ipos2 == kPRFbin) {
1758         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2-1];
1759       }
1760       else if (ipos2 == 1) {
1761         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2-1];
1762       }
1763       else {
1764         ipos2--;
1765         if (ipos2 >= kPRFbin) ipos2 = kPRFbin - 1;
1766         ipos1 = ipos2 - 1;
1767         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2] 
1768                                      + diff * (prf[iLayer][ipos2] - prf[iLayer][ipos1]) 
1769                                      / sPRFwid;
1770       }
1771
1772     }
1773   } 
1774
1775 }
1776
1777 //_____________________________________________________________________________
1778 Int_t AliTRDcalibDB::PadResponse(Double_t signal, Double_t dist
1779                                , Int_t layer, Double_t *pad) const
1780 {
1781   //
1782   // Applies the pad response
1783   // So far this is the fixed parametrization and should be replaced by
1784   // something dependent on calibration values
1785   //
1786
1787   Int_t iBin  = ((Int_t) ((-dist - fPRFlo) / fPRFwid));
1788   Int_t iOff  = layer * fPRFbin;
1789
1790   Int_t iBin0 = iBin - fPRFpad + iOff;
1791   Int_t iBin1 = iBin           + iOff;
1792   Int_t iBin2 = iBin + fPRFpad + iOff;
1793
1794   pad[0] = 0.0;
1795   pad[1] = 0.0;
1796   pad[2] = 0.0;
1797   if ((iBin1 >= 0) && (iBin1 < (fPRFbin*kNlayer))) {
1798
1799     if (iBin0 >= 0) {
1800       pad[0] = signal * fPRFsmp[iBin0];
1801     }
1802     pad[1] = signal * fPRFsmp[iBin1];
1803     if (iBin2 < (fPRFbin*kNlayer)) {
1804       pad[2] = signal * fPRFsmp[iBin2];
1805     }
1806
1807     return 1;
1808
1809   }
1810   else {
1811
1812     return 0;
1813
1814   }
1815
1816 }
1817
1818
1819 AliTRDtrapConfig* AliTRDcalibDB::GetTrapConfig()
1820 {
1821   // return an existing TRAPconfig or load it from the OCDB
1822   // in case of failure, a default TRAPconfig is created
1823
1824   if (fTrapConfig)
1825     return fTrapConfig;
1826   else {
1827     if ((fTrapConfigName.Length() <= 0) || (fTrapConfigVersion.Length() <= 0)) {
1828       // query the configuration to be used
1829       this->GetGlobalConfiguration(fTrapConfigName);
1830       this->GetGlobalConfigurationVersion(fTrapConfigVersion);
1831     }
1832
1833     // try to load the requested configuration
1834     this->LoadTrapConfig(fTrapConfigName, fTrapConfigVersion);
1835
1836     // if we still don't have a valid TRAPconfig, create a default one
1837     if (!fTrapConfig) {
1838       AliWarning("Falling back to default configuration");
1839       static AliTRDtrapConfig trapConfigDefault("default", "default TRAP configuration");
1840       fTrapConfig = &trapConfigDefault;
1841       AliTRDtrapConfigHandler cfgHandler(fTrapConfig);
1842       cfgHandler.Init();
1843       cfgHandler.LoadConfig();
1844     }
1845
1846     AliInfo(Form("using TRAPconfig \"%s\"", fTrapConfig->GetTitle()));
1847
1848     return fTrapConfig;
1849   }
1850 }
1851
1852
1853 AliTRDtrapConfig* AliTRDcalibDB::LoadTrapConfig(const TString &name, const TString &version)
1854 {
1855   // try to load the specified configuration from the OCDB
1856   // if it fails, or it does not exist, return null
1857
1858   AliInfo(Form("looking for TRAPconfig \"%s.%s\"", name.Data(), version.Data()));
1859
1860   const AliTRDCalTrapConfig *caltrap = dynamic_cast<const AliTRDCalTrapConfig*> (GetCachedCDBObject(kIDTrapConfig));
1861
1862   if (caltrap) {
1863     TString configName(name);
1864     configName.Append(".");
1865     configName.Append(version);
1866     fTrapConfig = caltrap->Get(configName);
1867   }
1868   else {
1869     fTrapConfig = 0x0;
1870     AliError("No TRAPconfig entry found!");
1871   }
1872
1873   return fTrapConfig;
1874 }