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