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