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