]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDcalibDB.cxx
Introduce an enumerator for PID methods
[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 <TMath.h>
32 #include <TRandom.h>
33
34 #include "AliCDBManager.h"
35 #include "AliCDBStorage.h"
36 #include "AliCDBEntry.h"
37 #include "AliLog.h"
38
39 #include "AliTRDcalibDB.h"
40 #include "AliTRDgeometry.h"
41 #include "AliTRDpadPlane.h"
42 #include "AliTRDCommonParam.h"
43
44 #include "Cal/AliTRDCalROC.h"
45 #include "Cal/AliTRDCalPad.h"
46 #include "Cal/AliTRDCalDet.h"
47 #include "Cal/AliTRDCalFEE.h"
48 #include "Cal/AliTRDCalPID.h"
49 #include "Cal/AliTRDCalMonitoring.h"
50 #include "Cal/AliTRDCalChamberStatus.h"
51 #include "Cal/AliTRDCalPadStatus.h"
52 #include "Cal/AliTRDCalSingleChamberStatus.h"
53
54 ClassImp(AliTRDcalibDB)
55
56 AliTRDcalibDB *AliTRDcalibDB::fgInstance   = 0;
57 Bool_t         AliTRDcalibDB::fgTerminated = kFALSE;
58
59 //_ singleton implementation __________________________________________________
60 AliTRDcalibDB* AliTRDcalibDB::Instance()
61 {
62   //
63   // Singleton implementation
64   // Returns an instance of this class, it is created if neccessary
65   //
66   
67   if (fgTerminated != kFALSE) {
68     return 0;
69   }
70
71   if (fgInstance == 0) {
72     fgInstance = new AliTRDcalibDB();
73   }
74
75   return fgInstance;
76
77 }
78
79 //_____________________________________________________________________________
80 void AliTRDcalibDB::Terminate()
81 {
82   //
83   // Singleton implementation
84   // Deletes the instance of this class and sets the terminated flag,
85   // instances cannot be requested anymore
86   // This function can be called several times.
87   //
88   
89   fgTerminated = kTRUE;
90   
91   if (fgInstance != 0) {
92     delete fgInstance;
93     fgInstance = 0;
94   }
95
96 }
97
98 //_____________________________________________________________________________
99 AliTRDcalibDB::AliTRDcalibDB()
100   :TObject()
101   ,fRun(-1)
102   ,fPRFsmp(0)
103   ,fPRFbin(0)
104   ,fPRFlo(0)
105   ,fPRFhi(0)
106   ,fPRFwid(0)
107   ,fPRFpad(0)
108 {
109   //
110   // Default constructor
111   //
112   // TODO Default runnumber is set to 0, this should be changed later
113   //      to an invalid value (e.g. -1) to prevent
114   // TODO invalid calibration data to be used.
115   //
116
117   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
118     fCDBCache[i]   = 0;
119     fCDBEntries[i] = 0;
120   }
121   
122   // Create the sampled PRF
123   SamplePRF();
124   
125 }
126
127 //_____________________________________________________________________________
128 AliTRDcalibDB::AliTRDcalibDB(const AliTRDcalibDB &c)
129   :TObject(c)
130   ,fRun(-1)
131   ,fPRFsmp(0)
132   ,fPRFbin(0)
133   ,fPRFlo(0)
134   ,fPRFhi(0)
135   ,fPRFwid(0)
136   ,fPRFpad(0)
137 {
138   //
139   // Copy constructor (not that it make any sense for a singleton...)
140   //
141
142   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
143     fCDBCache[i]   = 0;
144     fCDBEntries[i] = 0;
145   }
146   
147   // Create the sampled PRF
148   SamplePRF();
149
150 }
151
152 //_____________________________________________________________________________
153 AliTRDcalibDB &AliTRDcalibDB::operator=(const AliTRDcalibDB &c) 
154 {
155   //
156   // Assignment operator (same as above ...)
157   //
158
159   if (this != &c) {
160     AliFatal("No assignment operator defined");
161   }
162
163   return *this;
164
165 }
166
167 //_____________________________________________________________________________
168 AliTRDcalibDB::~AliTRDcalibDB() 
169 {
170   //
171   // destructor
172   //
173   
174   if (fPRFsmp) {
175     delete [] fPRFsmp;
176     fPRFsmp = 0;
177   }
178
179   Invalidate();
180
181 }
182
183 //_caching functions____________________________________________________________
184 const TObject *AliTRDcalibDB::GetCachedCDBObject(Int_t id)
185 {
186   //
187   // Retrieves a cdb object with the given id. The objects are cached as
188   // long as the run number is not changed.
189   //
190   // Put together the available objects here by using the lines
191   //   a) For usual calibration objects:
192   //      case kID<Name> : 
193   //        return CacheCDBEntry(kID<Name>,"TRD/Calib/<Path>"); 
194   //        break;
195   //      See function CacheCDBEntry for details.
196   //   and
197   //   b) For calibration data which depends on two objects: One containing 
198   //      a value per detector and one the local fluctuations per pad:
199   //      case kID<Name> :
200   //        return CacheMergeCDBEntry(kID<Name>,"TRD/Calib/<padPath>","TRD/Calib/<chamberPath>"); 
201   //        break;
202   //      See function CacheMergeCDBEntry for details.
203   //
204     
205   switch (id) {
206
207     // Parameters defined per pad and chamber
208     case kIDVdriftPad : 
209       return CacheCDBEntry(kIDVdriftPad         ,"TRD/Calib/LocalVdrift"); 
210       break;
211     case kIDVdriftChamber : 
212       return CacheCDBEntry(kIDVdriftChamber     ,"TRD/Calib/ChamberVdrift"); 
213       break;
214     case kIDT0Pad : 
215       return CacheCDBEntry(kIDT0Pad             ,"TRD/Calib/LocalT0"); 
216       break;
217     case kIDT0Chamber : 
218       return CacheCDBEntry(kIDT0Chamber         ,"TRD/Calib/ChamberT0"); 
219       break;
220     case kIDGainFactorPad : 
221       return CacheCDBEntry(kIDGainFactorPad     ,"TRD/Calib/LocalGainFactor"); 
222       break;
223     case kIDGainFactorChamber : 
224       return CacheCDBEntry(kIDGainFactorChamber ,"TRD/Calib/ChamberGainFactor"); 
225       break;
226     case kIDNoiseChamber : 
227       return CacheCDBEntry(kIDNoiseChamber ,"TRD/Calib/DetNoise"); 
228       break;
229     case kIDNoisePad : 
230       return CacheCDBEntry(kIDNoisePad ,"TRD/Calib/PadNoise"); 
231       break;
232
233
234     // Parameters defined per pad
235     case kIDPRFWidth : 
236       return CacheCDBEntry(kIDPRFWidth          ,"TRD/Calib/PRFWidth"); 
237       break;
238
239     // Status values
240     case kIDChamberStatus : 
241       return CacheCDBEntry(kIDChamberStatus     ,"TRD/Calib/ChamberStatus"); 
242       break;
243     case kIDPadStatus : 
244       return CacheCDBEntry(kIDPadStatus         ,"TRD/Calib/PadStatus"); 
245       break;
246
247     // Global parameters
248     case kIDMonitoringData : 
249       return CacheCDBEntry(kIDMonitoringData    ,"TRD/Calib/MonitoringData"); 
250       break;
251     case kIDFEE : 
252       return CacheCDBEntry(kIDFEE               ,"TRD/Calib/FEE"); 
253       break;
254     case kIDPIDNN : 
255       return CacheCDBEntry(kIDPIDNN             ,"TRD/Calib/PIDNN");
256     case kIDPIDLQ : 
257       return CacheCDBEntry(kIDPIDLQ             ,"TRD/Calib/PIDLQ"); 
258       break;
259
260   }
261
262   return 0;
263
264 }
265
266 //_____________________________________________________________________________
267 AliCDBEntry *AliTRDcalibDB::GetCDBEntry(const char *cdbPath)
268 {
269   // 
270   // Retrieves an entry with path <cdbPath> from the CDB.
271   //
272     
273   AliCDBEntry *entry = AliCDBManager::Instance()->Get(cdbPath,fRun);
274   if (!entry) { 
275     AliFatal(Form("Failed to get entry: %s",cdbPath));
276     return 0; 
277   }
278   
279   return entry;
280
281 }
282
283 //_____________________________________________________________________________
284 const TObject *AliTRDcalibDB::CacheCDBEntry(Int_t id, const char *cdbPath)
285 {
286   //
287   // Caches the entry <id> with cdb path <cdbPath>
288   //
289   
290   if (!fCDBCache[id]) {
291     fCDBEntries[id] = GetCDBEntry(cdbPath);
292     if (fCDBEntries[id]) {
293       fCDBCache[id] = fCDBEntries[id]->GetObject();
294     }
295   }
296
297   return fCDBCache[id];
298
299 }
300
301 //_____________________________________________________________________________
302 void AliTRDcalibDB::SetRun(Long64_t run)
303 {
304   //
305   // Sets current run number. Calibration data is read from the corresponding file.
306   // When the run number changes the caching is invalidated.
307   //
308
309   if (fRun == run) {
310     return;
311   }
312
313   fRun = run;
314
315   Invalidate();
316
317 }
318
319 //_____________________________________________________________________________
320 void AliTRDcalibDB::Invalidate()
321 {
322   //
323   // Invalidates cache (when run number is changed).
324   //
325   
326   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
327     if (fCDBEntries[i]) {
328       if (AliCDBManager::Instance()->GetCacheFlag() == kFALSE) {
329         if ((fCDBEntries[i]->IsOwner() == kFALSE) && 
330             (fCDBCache[i])) {
331           delete fCDBCache[i];
332         }
333         delete fCDBEntries[i];
334       }
335       fCDBEntries[i] = 0;
336       fCDBCache[i]   = 0;
337     }
338   }
339
340 }
341 //_____________________________________________________________________________
342 Float_t AliTRDcalibDB::GetNoise(Int_t det, Int_t col, Int_t row)
343 {
344   //
345   // Returns the noise level in ADC counts for the given pad.
346   //
347
348   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
349                                    (GetCachedCDBObject(kIDNoisePad));
350   if (!calPad) {
351     return -1;
352   }
353
354   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
355   if (!roc) {
356     return -1;
357   }
358
359   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
360                                    (GetCachedCDBObject(kIDNoiseChamber));
361   if (!calChamber) {
362     return -1;
363   }
364
365   return calChamber->GetValue(det) * roc->GetValue(col,row);
366
367 }
368  
369 //_____________________________________________________________________________
370 AliTRDCalROC *AliTRDcalibDB::GetNoiseROC(Int_t det)
371 {
372   //
373   // Returns the Vdrift calibration object for a given ROC
374   // containing one number per pad 
375   //
376   
377   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
378                                        (GetCachedCDBObject(kIDNoisePad));
379   if (!calPad) {
380     return 0;
381   }
382
383   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
384   if (!roc) {
385     return 0;
386   }
387   else {
388     return roc;
389   }
390
391 }
392
393 //_____________________________________________________________________________
394 const AliTRDCalDet *AliTRDcalibDB::GetNoiseDet()
395 {
396   //
397   // Returns the Vdrift calibration object
398   // containing one number per detector
399   //
400   
401   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
402                                        (GetCachedCDBObject(kIDNoiseChamber));
403   if (!calChamber) {
404     return 0;
405   }
406   else {
407     return calChamber;
408   }
409
410 }
411
412 //_____________________________________________________________________________
413 Float_t AliTRDcalibDB::GetVdrift(Int_t det, Int_t col, Int_t row)
414 {
415   //
416   // Returns the drift velocity for the given pad.
417   //
418
419   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
420                                    (GetCachedCDBObject(kIDVdriftPad));
421   if (!calPad) {
422     return -1;
423   }
424
425   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
426   if (!roc) {
427     return -1;
428   }
429
430   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
431                                    (GetCachedCDBObject(kIDVdriftChamber));
432   if (!calChamber) {
433     return -1;
434   }
435
436   return calChamber->GetValue(det) * roc->GetValue(col,row);
437
438 }
439  
440 //_____________________________________________________________________________
441 AliTRDCalROC *AliTRDcalibDB::GetVdriftROC(Int_t det)
442 {
443   //
444   // Returns the Vdrift calibration object for a given ROC
445   // containing one number per pad 
446   //
447   
448   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
449                                        (GetCachedCDBObject(kIDVdriftPad));
450   if (!calPad) {
451     return 0;
452   }
453
454   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
455   if (!roc) {
456     return 0;
457   }
458   else {
459     return roc;
460   }
461
462 }
463
464 //_____________________________________________________________________________
465 const AliTRDCalDet *AliTRDcalibDB::GetVdriftDet()
466 {
467   //
468   // Returns the Vdrift calibration object
469   // containing one number per detector
470   //
471   
472   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
473                                        (GetCachedCDBObject(kIDVdriftChamber));
474   if (!calChamber) {
475     return 0;
476   }
477   else {
478     return calChamber;
479   }
480
481 }
482
483 //_____________________________________________________________________________
484 Float_t AliTRDcalibDB::GetVdriftAverage(Int_t det)
485 {
486   //
487   // Returns the average drift velocity for the given detector
488   //
489
490   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
491                                    (GetCachedCDBObject(kIDVdriftChamber));
492   if (!calDet) {
493     return -1;
494   }
495
496   return calDet->GetValue(det);
497
498 }
499
500 //_____________________________________________________________________________
501 Float_t AliTRDcalibDB::GetT0(Int_t det, Int_t col, Int_t row)
502 {
503   //
504   // Returns t0 for the given pad.
505   //
506   
507   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
508                                        (GetCachedCDBObject(kIDT0Pad));
509   if (!calPad) {
510     return -1;
511   }
512
513   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
514   if (!roc) {
515     return -1;
516   }
517
518   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
519                                        (GetCachedCDBObject(kIDT0Chamber));
520   if (!calChamber) {
521     return -1;
522   }
523
524   return calChamber->GetValue(det) + roc->GetValue(col,row);
525
526 }
527  
528 //_____________________________________________________________________________
529 AliTRDCalROC *AliTRDcalibDB::GetT0ROC(Int_t det)
530 {
531   //
532   // Returns the t0 calibration object for a given ROC
533   // containing one number per pad 
534   //
535   
536   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
537                                        (GetCachedCDBObject(kIDT0Pad));
538   if (!calPad) {
539     return 0;
540   }
541
542   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
543   if (!roc) {
544     return 0;
545   }
546   else {
547     return roc;
548   }
549
550 }
551
552 //_____________________________________________________________________________
553 const AliTRDCalDet *AliTRDcalibDB::GetT0Det()
554 {
555   //
556   // Returns the t0 calibration object
557   // containing one number per detector
558   //
559   
560   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
561                                        (GetCachedCDBObject(kIDT0Chamber));
562   if (!calChamber) {
563     return 0;
564   }
565   else {
566     return calChamber;
567   }
568
569 }
570
571 //_____________________________________________________________________________
572 Float_t AliTRDcalibDB::GetT0Average(Int_t det)
573 {
574   //
575   // Returns the average t0 for the given detector
576   //
577
578   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
579                                    (GetCachedCDBObject(kIDT0Pad));
580   if (!calPad) {
581     return -1;
582   }
583
584   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
585   if (!roc) {
586     return -1;
587   }
588
589   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
590                                    (GetCachedCDBObject(kIDT0Chamber));
591   if (!calDet) {
592     return -1;
593   }
594
595   Double_t mean = 0.0; 
596   for (Int_t channel = 0; channel < roc->GetNchannels(); ++channel) {
597     mean += (calDet->GetValue(det) + roc->GetValue(channel)) / roc->GetNchannels();
598   }
599
600   return mean;
601
602 }
603
604 //_____________________________________________________________________________
605 Float_t AliTRDcalibDB::GetGainFactor(Int_t det, Int_t col, Int_t row)
606 {
607   //
608   // Returns the gain factor for the given pad.
609   //
610   
611   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
612                                    (GetCachedCDBObject(kIDGainFactorPad));
613   if (!calPad) {
614     return -1;
615   }
616
617   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
618   if (!roc) {
619     return -1;
620   }
621
622   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
623                                    (GetCachedCDBObject(kIDGainFactorChamber));
624   if (!calChamber) {
625     return -1;
626   }
627
628   return calChamber->GetValue(det) * roc->GetValue(col,row);
629
630 }
631
632 //_____________________________________________________________________________
633 AliTRDCalROC *AliTRDcalibDB::GetGainFactorROC(Int_t det)
634 {
635   //
636   // Returns the gain factor calibration object for a given ROC
637   //
638   
639   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
640                                    (GetCachedCDBObject(kIDGainFactorPad));
641   if (!calPad) {
642     return 0;
643   }
644
645   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
646   if (!roc) {
647     return 0;
648   }
649   else {
650     return roc;
651   }
652
653 }
654
655 //_____________________________________________________________________________
656 const AliTRDCalDet *AliTRDcalibDB::GetGainFactorDet()
657 {
658   //
659   // Returns the gain factor calibration object
660   // containing one number per detector
661   //
662
663   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
664                                    (GetCachedCDBObject(kIDGainFactorChamber));
665   if (!calChamber) {
666     return 0;
667   }
668   else {
669     return calChamber;
670   }
671
672 }
673
674 //_____________________________________________________________________________
675 Float_t AliTRDcalibDB::GetGainFactorAverage(Int_t det)
676 {
677   //
678   // Returns the average gain factor for the given detector
679   //
680
681   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
682                                    (GetCachedCDBObject(kIDGainFactorChamber));
683   if (!calDet) {
684     return -1;
685   }
686
687   return calDet->GetValue(det);
688
689 }
690
691 //_____________________________________________________________________________
692 AliTRDCalROC *AliTRDcalibDB::GetPRFROC(Int_t det)
693 {
694   //
695   // Returns the PRF calibration object for a given ROC
696   // containing one number per pad 
697   //
698   
699   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
700                                        (GetCachedCDBObject(kIDPRFWidth));
701   if (!calPad) {
702     return 0;
703   }
704
705   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
706   if (!roc) {
707     return 0;
708   }
709   else {
710     return roc;
711   }
712
713 }
714
715 //_____________________________________________________________________________
716 Float_t AliTRDcalibDB::GetPRFWidth(Int_t det, Int_t col, Int_t row)
717 {
718   //
719   // Returns the PRF width for the given pad.
720   //
721   
722   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
723                                    (GetCachedCDBObject(kIDPRFWidth));
724   if (!calPad) {
725     return -1;
726   }
727
728   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
729   if (!roc) {
730     return -1;
731   }
732
733   return roc->GetValue(col,row);
734
735 }
736   
737 //_____________________________________________________________________________
738 Int_t AliTRDcalibDB::GetNumberOfTimeBins()
739 {
740   //
741   // Returns the number of time bins which are read-out.
742   //
743
744   const AliTRDCalFEE *calFEE     = dynamic_cast<const AliTRDCalFEE *> 
745                                    (GetCachedCDBObject(kIDFEE));
746   if (!calFEE) {
747     return -1;
748   }
749
750   return calFEE->GetNumberOfTimeBins();
751
752 }
753
754 //_____________________________________________________________________________
755 Char_t AliTRDcalibDB::GetPadStatus(Int_t det, Int_t col, Int_t row)
756 {
757   //
758   // Returns the status of the given pad
759   //
760
761   const AliTRDCalPadStatus *cal  = dynamic_cast<const AliTRDCalPadStatus *> 
762                                    (GetCachedCDBObject(kIDPadStatus));
763   if (!cal) {
764     return -1;
765   }
766
767   const AliTRDCalSingleChamberStatus *roc = cal->GetCalROC(det);
768   if (!roc) {
769     return -1;
770   }
771
772   return roc->GetStatus(col,row);
773
774 }
775
776 //_____________________________________________________________________________
777 AliTRDCalSingleChamberStatus* AliTRDcalibDB::GetPadStatusROC(Int_t det)
778 {
779   //
780   // Returns the pad status calibration object for a given ROC
781   //
782
783   const AliTRDCalPadStatus *cal  = dynamic_cast<const AliTRDCalPadStatus *> 
784                                    (GetCachedCDBObject(kIDPadStatus));
785   if (!cal) {
786     return 0;
787   }
788
789   AliTRDCalSingleChamberStatus *roc = cal->GetCalROC(det);
790   if (!roc) {
791     return 0;
792   }
793   else {
794     return roc;
795   }
796
797 }
798
799 //_____________________________________________________________________________
800 Char_t AliTRDcalibDB::GetChamberStatus(Int_t det)
801 {
802   //
803   // Returns the status of the given chamber
804   //
805
806   const AliTRDCalChamberStatus *cal = dynamic_cast<const AliTRDCalChamberStatus *> 
807                                       (GetCachedCDBObject(kIDChamberStatus));
808   if (!cal) {
809     return -1;
810   }
811
812   return cal->GetStatus(det);
813
814 }
815
816 //_____________________________________________________________________________
817 Bool_t AliTRDcalibDB::IsPadMasked(Int_t det, Int_t col, Int_t row)
818 {
819   //
820   // Returns status, see name of functions for details ;-)
821   //
822
823   const AliTRDCalPadStatus         *cal = dynamic_cast<const AliTRDCalPadStatus *> 
824                                           (GetCachedCDBObject(kIDPadStatus));
825   if (!cal) {
826     return -1;
827   }
828
829   return cal->IsMasked(det,col,row);
830
831 }
832
833 //_____________________________________________________________________________
834 Bool_t AliTRDcalibDB::IsPadBridgedLeft(Int_t det, Int_t col, Int_t row)
835 {
836   //
837   // Returns status, see name of functions for details ;-)
838   //
839
840   const AliTRDCalPadStatus         *cal = dynamic_cast<const AliTRDCalPadStatus *> 
841                                           (GetCachedCDBObject(kIDPadStatus));
842   if (!cal) {
843     return -1;
844   }
845
846   return cal->IsBridgedLeft(det,col,row);
847
848 }
849
850 //_____________________________________________________________________________
851 Bool_t AliTRDcalibDB::IsPadBridgedRight(Int_t det, Int_t col, Int_t row)
852 {
853   //
854   // Returns status, see name of functions for details ;-)
855   //
856
857   const AliTRDCalPadStatus         * cal = dynamic_cast<const AliTRDCalPadStatus *> 
858                                            (GetCachedCDBObject(kIDPadStatus));
859   if (!cal) {
860     return -1;
861   }
862
863   return cal->IsBridgedRight(det,col,row);
864
865 }
866
867 //_____________________________________________________________________________
868 Bool_t AliTRDcalibDB::IsPadNotConnected(Int_t det, Int_t col, Int_t row)
869 {
870   //
871   // Returns status, see name of functions for details ;-)
872   //
873   const AliTRDCalPadStatus         * cal = dynamic_cast<const AliTRDCalPadStatus *> 
874                                            (GetCachedCDBObject(kIDPadStatus));
875   if (!cal) {
876     return -1;
877   }
878
879   return cal->IsNotConnected(det,col,row);
880
881 }
882
883 //_____________________________________________________________________________
884 Bool_t AliTRDcalibDB::IsChamberInstalled(Int_t det)
885 {
886   //
887   // Returns status, see name of functions for details ;-)
888   //
889
890   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
891                                            (GetCachedCDBObject(kIDChamberStatus));
892   if (!cal) {
893     return -1;
894   }
895
896   return cal->IsInstalled(det);
897
898 }
899
900 //_____________________________________________________________________________
901 Bool_t AliTRDcalibDB::IsChamberMasked(Int_t det)
902 {
903   //
904   // Returns status, see name of functions for details ;-)
905   //
906
907   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
908                                            (GetCachedCDBObject(kIDChamberStatus));
909   if (!cal) {
910     return -1;
911   }
912
913   return cal->IsMasked(det);
914
915 }
916
917 //_____________________________________________________________________________
918 const AliTRDCalPID *AliTRDcalibDB::GetPIDObject(AliTRDrecoParam::AliTRDpidMethod method)
919 {
920   //
921   // Returns the object storing the distributions for PID with likelihood
922   //
923
924   switch(method) {
925   case AliTRDrecoParam::kLQPID: 
926     return dynamic_cast<const AliTRDCalPID *>(GetCachedCDBObject(kIDPIDLQ));
927   case AliTRDrecoParam::kNNPID: 
928     return dynamic_cast<const AliTRDCalPID *>(GetCachedCDBObject(kIDPIDNN));
929   }
930
931   return 0x0;
932
933 }
934
935 //_____________________________________________________________________________
936 const AliTRDCalMonitoring *AliTRDcalibDB::GetMonitoringObject()
937 {
938   //
939   // Returns the object storing the monitoring data
940   //
941
942   return dynamic_cast<const AliTRDCalMonitoring *> 
943          (GetCachedCDBObject(kIDMonitoringData));
944    
945 }
946
947 //_____________________________________________________________________________
948 Float_t AliTRDcalibDB::GetOmegaTau(Float_t vdrift, Float_t bz)
949 {
950   //
951   // Returns omega*tau (tan(Lorentz-angle)) for a given drift velocity <vdrift> 
952   // and a B-field <bz> for Xe/CO2 (15%).
953   // The values are according to a GARFIELD simulation.
954   //
955   // This function basically does not belong to the calibration class.
956   // It should be moved somewhere else. 
957   // However, currently it is in use by simulation and reconstruction.
958   //
959   
960   Float_t fieldAbs = TMath::Abs(bz);
961   Float_t fieldSgn = (bz > 0.0) ? 1.0 : -1.0;
962
963   const Int_t kNb = 5;
964   Float_t p0[kNb] = {  0.004810,  0.007412,  0.010252,  0.013409,  0.016888 };
965   Float_t p1[kNb] = {  0.054875,  0.081534,  0.107333,  0.131983,  0.155455 };
966   Float_t p2[kNb] = { -0.008682, -0.012896, -0.016987, -0.020880, -0.024623 };
967   Float_t p3[kNb] = {  0.000155,  0.000238,  0.000330,  0.000428,  0.000541 };
968
969   // No ExB if field is too small (or zero)
970   if (fieldAbs < 0.01) {
971
972     return 0.0;
973
974   }
975   // Calculate ExB from parametrization
976   else {
977
978     Int_t ib = ((Int_t) (10 * (fieldAbs - 0.15)));
979     ib       = TMath::Max(  0,ib);
980     ib       = TMath::Min(kNb,ib);
981
982     Float_t alphaL = p0[ib] 
983                    + p1[ib] * vdrift
984                    + p2[ib] * vdrift*vdrift
985                    + p3[ib] * vdrift*vdrift*vdrift;
986
987     return TMath::Tan(fieldSgn * alphaL);
988
989   }
990
991 }
992
993 //_____________________________________________________________________________
994 void AliTRDcalibDB::SamplePRF()
995 {
996   //
997   // Samples the pad response function (should maybe go somewhere else ...)
998   //
999
1000   const Int_t kPRFbin = 61;
1001
1002   Float_t prf[kNlayer][kPRFbin] = { 
1003                    {2.9037e-02, 3.3608e-02, 3.9020e-02, 4.5292e-02,
1004                     5.2694e-02, 6.1362e-02, 7.1461e-02, 8.3362e-02,
1005                     9.7063e-02, 1.1307e-01, 1.3140e-01, 1.5235e-01,
1006                     1.7623e-01, 2.0290e-01, 2.3294e-01, 2.6586e-01,
1007                     3.0177e-01, 3.4028e-01, 3.8077e-01, 4.2267e-01,
1008                     4.6493e-01, 5.0657e-01, 5.4655e-01, 5.8397e-01,
1009                     6.1767e-01, 6.4744e-01, 6.7212e-01, 6.9188e-01,
1010                     7.0627e-01, 7.1499e-01, 7.1851e-01, 7.1499e-01,
1011                     7.0627e-01, 6.9188e-01, 6.7212e-01, 6.4744e-01,
1012                     6.1767e-01, 5.8397e-01, 5.4655e-01, 5.0657e-01,
1013                     4.6493e-01, 4.2267e-01, 3.8077e-01, 3.4028e-01,
1014                     3.0177e-01, 2.6586e-01, 2.3294e-01, 2.0290e-01,
1015                     1.7623e-01, 1.5235e-01, 1.3140e-01, 1.1307e-01,
1016                     9.7063e-02, 8.3362e-02, 7.1461e-02, 6.1362e-02,
1017                     5.2694e-02, 4.5292e-02, 3.9020e-02, 3.3608e-02,
1018                     2.9037e-02},
1019                    {2.5478e-02, 2.9695e-02, 3.4655e-02, 4.0454e-02,
1020                     4.7342e-02, 5.5487e-02, 6.5038e-02, 7.6378e-02,
1021                     8.9696e-02, 1.0516e-01, 1.2327e-01, 1.4415e-01,
1022                     1.6794e-01, 1.9516e-01, 2.2573e-01, 2.5959e-01,
1023                     2.9694e-01, 3.3719e-01, 3.7978e-01, 4.2407e-01,
1024                     4.6889e-01, 5.1322e-01, 5.5569e-01, 5.9535e-01,
1025                     6.3141e-01, 6.6259e-01, 6.8882e-01, 7.0983e-01,
1026                     7.2471e-01, 7.3398e-01, 7.3761e-01, 7.3398e-01,
1027                     7.2471e-01, 7.0983e-01, 6.8882e-01, 6.6259e-01,
1028                     6.3141e-01, 5.9535e-01, 5.5569e-01, 5.1322e-01,
1029                     4.6889e-01, 4.2407e-01, 3.7978e-01, 3.3719e-01,
1030                     2.9694e-01, 2.5959e-01, 2.2573e-01, 1.9516e-01,
1031                     1.6794e-01, 1.4415e-01, 1.2327e-01, 1.0516e-01,
1032                     8.9696e-02, 7.6378e-02, 6.5038e-02, 5.5487e-02,
1033                     4.7342e-02, 4.0454e-02, 3.4655e-02, 2.9695e-02,
1034                     2.5478e-02},
1035                    {2.2363e-02, 2.6233e-02, 3.0782e-02, 3.6140e-02,
1036                     4.2535e-02, 5.0157e-02, 5.9197e-02, 6.9900e-02,
1037                     8.2707e-02, 9.7811e-02, 1.1548e-01, 1.3601e-01,
1038                     1.5998e-01, 1.8739e-01, 2.1840e-01, 2.5318e-01,
1039                     2.9182e-01, 3.3373e-01, 3.7837e-01, 4.2498e-01,
1040                     4.7235e-01, 5.1918e-01, 5.6426e-01, 6.0621e-01,
1041                     6.4399e-01, 6.7700e-01, 7.0472e-01, 7.2637e-01,
1042                     7.4206e-01, 7.5179e-01, 7.5551e-01, 7.5179e-01,
1043                     7.4206e-01, 7.2637e-01, 7.0472e-01, 6.7700e-01,
1044                     6.4399e-01, 6.0621e-01, 5.6426e-01, 5.1918e-01,
1045                     4.7235e-01, 4.2498e-01, 3.7837e-01, 3.3373e-01,
1046                     2.9182e-01, 2.5318e-01, 2.1840e-01, 1.8739e-01,
1047                     1.5998e-01, 1.3601e-01, 1.1548e-01, 9.7811e-02,
1048                     8.2707e-02, 6.9900e-02, 5.9197e-02, 5.0157e-02,
1049                     4.2535e-02, 3.6140e-02, 3.0782e-02, 2.6233e-02,
1050                     2.2363e-02},
1051                    {1.9635e-02, 2.3167e-02, 2.7343e-02, 3.2293e-02,
1052                     3.8224e-02, 4.5335e-02, 5.3849e-02, 6.4039e-02,
1053                     7.6210e-02, 9.0739e-02, 1.0805e-01, 1.2841e-01,
1054                     1.5216e-01, 1.7960e-01, 2.1099e-01, 2.4671e-01,
1055                     2.8647e-01, 3.2996e-01, 3.7660e-01, 4.2547e-01,
1056                     4.7536e-01, 5.2473e-01, 5.7215e-01, 6.1632e-01,
1057                     6.5616e-01, 6.9075e-01, 7.1939e-01, 7.4199e-01,
1058                     7.5838e-01, 7.6848e-01, 7.7227e-01, 7.6848e-01,
1059                     7.5838e-01, 7.4199e-01, 7.1939e-01, 6.9075e-01,
1060                     6.5616e-01, 6.1632e-01, 5.7215e-01, 5.2473e-01,
1061                     4.7536e-01, 4.2547e-01, 3.7660e-01, 3.2996e-01,
1062                     2.8647e-01, 2.4671e-01, 2.1099e-01, 1.7960e-01,
1063                     1.5216e-01, 1.2841e-01, 1.0805e-01, 9.0739e-02,
1064                     7.6210e-02, 6.4039e-02, 5.3849e-02, 4.5335e-02,
1065                     3.8224e-02, 3.2293e-02, 2.7343e-02, 2.3167e-02,
1066                     1.9635e-02},
1067                    {1.7224e-02, 2.0450e-02, 2.4286e-02, 2.8860e-02,
1068                     3.4357e-02, 4.0979e-02, 4.8966e-02, 5.8612e-02,
1069                     7.0253e-02, 8.4257e-02, 1.0102e-01, 1.2094e-01,
1070                     1.4442e-01, 1.7196e-01, 2.0381e-01, 2.4013e-01,
1071                     2.8093e-01, 3.2594e-01, 3.7450e-01, 4.2563e-01,
1072                     4.7796e-01, 5.2991e-01, 5.7974e-01, 6.2599e-01,
1073                     6.6750e-01, 7.0344e-01, 7.3329e-01, 7.5676e-01,
1074                     7.7371e-01, 7.8410e-01, 7.8793e-01, 7.8410e-01,
1075                     7.7371e-01, 7.5676e-01, 7.3329e-01, 7.0344e-01,
1076                     6.6750e-01, 6.2599e-01, 5.7974e-01, 5.2991e-01,
1077                     4.7796e-01, 4.2563e-01, 3.7450e-01, 3.2594e-01,
1078                     2.8093e-01, 2.4013e-01, 2.0381e-01, 1.7196e-01,
1079                     1.4442e-01, 1.2094e-01, 1.0102e-01, 8.4257e-02,
1080                     7.0253e-02, 5.8612e-02, 4.8966e-02, 4.0979e-02,
1081                     3.4357e-02, 2.8860e-02, 2.4286e-02, 2.0450e-02,
1082                     1.7224e-02},
1083                    {1.5096e-02, 1.8041e-02, 2.1566e-02, 2.5793e-02,
1084                     3.0886e-02, 3.7044e-02, 4.4515e-02, 5.3604e-02,
1085                     6.4668e-02, 7.8109e-02, 9.4364e-02, 1.1389e-01,
1086                     1.3716e-01, 1.6461e-01, 1.9663e-01, 2.3350e-01,
1087                     2.7527e-01, 3.2170e-01, 3.7214e-01, 4.2549e-01,
1088                     4.8024e-01, 5.3460e-01, 5.8677e-01, 6.3512e-01,
1089                     6.7838e-01, 7.1569e-01, 7.4655e-01, 7.7071e-01,
1090                     7.8810e-01, 7.9871e-01, 8.0255e-01, 7.9871e-01,
1091                     7.8810e-01, 7.7071e-01, 7.4655e-01, 7.1569e-01,
1092                     6.7838e-01, 6.3512e-01, 5.8677e-01, 5.3460e-01,
1093                     4.8024e-01, 4.2549e-01, 3.7214e-01, 3.2170e-01,
1094                     2.7527e-01, 2.3350e-01, 1.9663e-01, 1.6461e-01,
1095                     1.3716e-01, 1.1389e-01, 9.4364e-02, 7.8109e-02,
1096                     6.4668e-02, 5.3604e-02, 4.4515e-02, 3.7044e-02,
1097                     3.0886e-02, 2.5793e-02, 2.1566e-02, 1.8041e-02,
1098                     1.5096e-02}};
1099
1100   // More sampling precision with linear interpolation
1101   fPRFlo  = -1.5;
1102   fPRFhi  =  1.5;
1103   Float_t pad[kPRFbin];
1104   Int_t   sPRFbin = kPRFbin;  
1105   Float_t sPRFwid = (fPRFhi - fPRFlo) / ((Float_t) sPRFbin);
1106   for (Int_t iPad = 0; iPad < sPRFbin; iPad++) {
1107     pad[iPad] = ((Float_t) iPad + 0.5) * sPRFwid + fPRFlo;
1108   }
1109   fPRFbin = 500;  
1110   fPRFwid = (fPRFhi - fPRFlo) / ((Float_t) fPRFbin);
1111   fPRFpad = ((Int_t) (1.0 / fPRFwid));
1112
1113   if (fPRFsmp) delete [] fPRFsmp;
1114   fPRFsmp = new Float_t[kNlayer*fPRFbin];
1115
1116   Int_t   ipos1;
1117   Int_t   ipos2;
1118   Float_t diff;
1119
1120   for (Int_t iLayer = 0; iLayer < kNlayer; iLayer++) {
1121
1122     for (Int_t iBin = 0; iBin < fPRFbin; iBin++) {
1123
1124       Float_t bin = (((Float_t) iBin) + 0.5) * fPRFwid + fPRFlo;
1125       ipos1 = ipos2 = 0;
1126       diff  = 0;
1127       do {
1128         diff = bin - pad[ipos2++];
1129       } while ((diff > 0) && (ipos2 < kPRFbin));
1130       if      (ipos2 == kPRFbin) {
1131         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2-1];
1132       }
1133       else if (ipos2 == 1) {
1134         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2-1];
1135       }
1136       else {
1137         ipos2--;
1138         if (ipos2 >= kPRFbin) ipos2 = kPRFbin - 1;
1139         ipos1 = ipos2 - 1;
1140         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2] 
1141                                      + diff * (prf[iLayer][ipos2] - prf[iLayer][ipos1]) 
1142                                      / sPRFwid;
1143       }
1144
1145     }
1146   } 
1147
1148 }
1149
1150 //_____________________________________________________________________________
1151 Int_t AliTRDcalibDB::PadResponse(Double_t signal, Double_t dist
1152                                 , Int_t layer, Double_t *pad) const
1153 {
1154   //
1155   // Applies the pad response
1156   // So far this is the fixed parametrization and should be replaced by
1157   // something dependent on calibration values
1158   //
1159
1160   Int_t iBin  = ((Int_t) (( - dist - fPRFlo) / fPRFwid));
1161   Int_t iOff  = layer * fPRFbin;
1162
1163   Int_t iBin0 = iBin - fPRFpad + iOff;
1164   Int_t iBin1 = iBin           + iOff;
1165   Int_t iBin2 = iBin + fPRFpad + iOff;
1166
1167   pad[0] = 0.0;
1168   pad[1] = 0.0;
1169   pad[2] = 0.0;
1170   if ((iBin1 >= 0) && (iBin1 < (fPRFbin*kNlayer))) {
1171
1172     if (iBin0 >= 0) {
1173       pad[0] = signal * fPRFsmp[iBin0];
1174     }
1175     pad[1] = signal * fPRFsmp[iBin1];
1176     if (iBin2 < (fPRFbin*kNlayer)) {
1177       pad[2] = signal * fPRFsmp[iBin2];
1178     }
1179
1180     return 1;
1181
1182   }
1183   else {
1184
1185     return 0;
1186
1187   }
1188
1189 }