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