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