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