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