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