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