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