]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDcalibDB.cxx
Fix for coverity (AdC)
[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 <TROOT.h>
32 #include <TClonesArray.h>
33 #include <TObjArray.h>
34
35 #include "AliCDBManager.h"
36 #include "AliCDBEntry.h"
37 #include "AliLog.h"
38
39 #include "AliTRDPIDReference.h"
40 #include "AliTRDPIDResponseObject.h"
41 #include "AliTRDcalibDB.h"
42 #include "AliTRDtrapConfig.h"
43 #include "AliTRDtrapConfigHandler.h"
44 #include "AliTRDCommonParam.h"
45 #include "AliTRDgeometry.h"
46
47 #include "Cal/AliTRDCalROC.h"
48 #include "Cal/AliTRDCalPad.h"
49 #include "Cal/AliTRDCalDet.h"
50 #include "Cal/AliTRDCalDCS.h"
51 #include "Cal/AliTRDCalDCSFEE.h"
52 #include "Cal/AliTRDCalDCSv2.h"
53 #include "Cal/AliTRDCalDCSFEEv2.h"
54 #include "Cal/AliTRDCalPID.h"
55 #include "Cal/AliTRDCalMonitoring.h"
56 #include "Cal/AliTRDCalChamberStatus.h"
57 #include "Cal/AliTRDCalPadStatus.h"
58 #include "Cal/AliTRDCalSingleChamberStatus.h"
59 #include "Cal/AliTRDCalTrkAttach.h"
60 #include "Cal/AliTRDCalOnlineGainTable.h"
61
62 ClassImp(AliTRDcalibDB)
63
64 AliTRDcalibDB *AliTRDcalibDB::fgInstance   = 0;
65 Bool_t         AliTRDcalibDB::fgTerminated = kFALSE;
66
67 //_ singleton implementation __________________________________________________
68 AliTRDcalibDB* AliTRDcalibDB::Instance()
69 {
70   //
71   // Singleton implementation
72   // Returns an instance of this class, it is created if neccessary
73   //
74   
75   if (fgTerminated != kFALSE) {
76     return 0;
77   }
78
79   if (fgInstance == 0) {
80     fgInstance = new AliTRDcalibDB();
81   }
82
83   return fgInstance;
84
85 }
86
87 //_____________________________________________________________________________
88 void AliTRDcalibDB::Terminate()
89 {
90   //
91   // Singleton implementation
92   // Deletes the instance of this class and sets the terminated flag,
93   // instances cannot be requested anymore
94   // This function can be called several times.
95   //
96   
97   fgTerminated = kTRUE;
98   
99   if (fgInstance != 0) {
100     delete fgInstance;
101     fgInstance = 0;
102   }
103
104 }
105
106 //_____________________________________________________________________________
107 AliTRDcalibDB::AliTRDcalibDB()
108   :TObject()
109   ,fRun(-1)
110   ,fPRFsmp(0)
111   ,fPRFbin(0)
112   ,fPRFlo(0)
113   ,fPRFhi(0)
114   ,fPRFwid(0)
115   ,fPRFpad(0)
116   ,fPIDResponse(NULL)
117   ,fOnlineGainTableID(0)
118   ,fTrapConfig(0x0)
119   ,fTrapConfigName("")
120   ,fTrapConfigVersion("")
121 {
122   //
123   // Default constructor
124   //
125   // TODO Default runnumber is set to 0, this should be changed later
126   //      to an invalid value (e.g. -1) to prevent
127   // TODO invalid calibration data to be used.
128   //
129
130   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
131     fCDBCache[i]   = 0;
132     fCDBEntries[i] = 0;
133   }
134   
135   // Create the sampled PRF
136   SamplePRF();
137   
138 }
139
140 //_____________________________________________________________________________
141 AliTRDcalibDB::AliTRDcalibDB(const AliTRDcalibDB &c)
142   :TObject(c)
143   ,fRun(-1)
144   ,fPRFsmp(0)
145   ,fPRFbin(0)
146   ,fPRFlo(0)
147   ,fPRFhi(0)
148   ,fPRFwid(0)
149   ,fPRFpad(0)
150   ,fPIDResponse(NULL)
151   ,fOnlineGainTableID(0)
152   ,fTrapConfig(0x0)
153   ,fTrapConfigName("")
154   ,fTrapConfigVersion("")
155 {
156   //
157   // Copy constructor (not that it make any sense for a singleton...)
158   //
159
160   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
161     fCDBCache[i]   = 0;
162     fCDBEntries[i] = 0;
163   }
164   
165   // Create the sampled PRF
166   SamplePRF();
167
168 }
169
170 //_____________________________________________________________________________
171 AliTRDcalibDB &AliTRDcalibDB::operator=(const AliTRDcalibDB &c) 
172 {
173   //
174   // Assignment operator (same as above ...)
175   //
176
177   if (this != &c) {
178     AliFatal("No assignment operator defined");
179   }
180
181   return *this;
182
183 }
184
185 //_____________________________________________________________________________
186 AliTRDcalibDB::~AliTRDcalibDB() 
187 {
188   //
189   // destructor
190   //
191   
192   if (fPRFsmp) {
193     delete [] fPRFsmp;
194     fPRFsmp = 0;
195   }
196
197   if (fPIDResponse) {
198     delete fPIDResponse;
199     fPIDResponse = 0x0;
200   }
201
202   Invalidate();
203
204 }
205
206 //_caching functions____________________________________________________________
207 const TObject *AliTRDcalibDB::GetCachedCDBObject(Int_t id)
208 {
209   //
210   // Retrieves a cdb object with the given id. The objects are cached as
211   // long as the run number is not changed.
212   //
213   // Put together the available objects here by using the lines
214   //   a) For usual calibration objects:
215   //      case kID<Name> : 
216   //        return CacheCDBEntry(kID<Name>,"TRD/Calib/<Path>"); 
217   //        break;
218   //      See function CacheCDBEntry for details.
219   //   and
220   //   b) For calibration data which depends on two objects: One containing 
221   //      a value per detector and one the local fluctuations per pad:
222   //      case kID<Name> :
223   //        return CacheMergeCDBEntry(kID<Name>,"TRD/Calib/<padPath>","TRD/Calib/<chamberPath>"); 
224   //        break;
225   //      See function CacheMergeCDBEntry for details.
226   //
227     
228   switch (id) {
229
230     // Parameters defined per pad and chamber
231     case kIDVdriftPad : 
232       return CacheCDBEntry(kIDVdriftPad         ,"TRD/Calib/LocalVdrift"); 
233       break;
234     case kIDVdriftChamber : 
235       return CacheCDBEntry(kIDVdriftChamber     ,"TRD/Calib/ChamberVdrift"); 
236       break;
237     case kIDExBChamber : 
238       return CacheCDBEntry(kIDExBChamber        ,"TRD/Calib/ChamberExB"); 
239       break;
240     case kIDT0Pad : 
241       return CacheCDBEntry(kIDT0Pad             ,"TRD/Calib/LocalT0"); 
242       break;
243     case kIDT0Chamber : 
244       return CacheCDBEntry(kIDT0Chamber         ,"TRD/Calib/ChamberT0"); 
245       break;
246     case kIDGainFactorPad : 
247       return CacheCDBEntry(kIDGainFactorPad     ,"TRD/Calib/LocalGainFactor"); 
248       break;
249     case kIDGainFactorChamber : 
250       return CacheCDBEntry(kIDGainFactorChamber ,"TRD/Calib/ChamberGainFactor"); 
251       break;
252
253     case kIDOnlineGainFactor : 
254       switch(GetOnlineGainTableID()) {
255         case 0:
256           // For testing purposes only !!!
257           AliInfo("No gain table name from OCDB. Use default table!");
258           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-01"); 
259           break;
260         case 1:
261           // Online gain table ID 1
262           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-01"); 
263           break;
264         case 2:
265           // Online gain table ID 2
266           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN0_2011-01"); 
267           break;
268         case 3:
269           // Online gain table ID 3
270           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN8_2011-01"); 
271           break;
272         case 4:
273           // Online gain table ID 4
274           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-02"); 
275           break;
276         case 5:
277           // Online gain table ID 5
278           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2011-03"); 
279           break;
280         case 6:
281           // Online gain table ID 6
282           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN0_2012-01"); 
283           break;
284         case 7:
285           // Online gain table ID 7
286           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Gaintbl_Uniform_FGAN8_2012-01");
287           break; 
288         case 8:
289           // Online gain table ID 8
290           return CacheCDBEntry(kIDOnlineGainFactor  ,"TRD/Calib/Krypton_2012-01"); 
291           break;
292       default:
293         AliError(Form("unknown gaintable requested with ID"));
294       }
295       break;
296
297     case kIDNoiseChamber : 
298       return CacheCDBEntry(kIDNoiseChamber      ,"TRD/Calib/DetNoise"); 
299       break;
300     case kIDNoisePad : 
301       return CacheCDBEntry(kIDNoisePad          ,"TRD/Calib/PadNoise"); 
302       break;
303
304     // Parameters defined per pad
305     case kIDPRFWidth : 
306       return CacheCDBEntry(kIDPRFWidth          ,"TRD/Calib/PRFWidth"); 
307       break;
308
309     // Status values
310     case kIDChamberStatus : 
311       return CacheCDBEntry(kIDChamberStatus     ,"TRD/Calib/ChamberStatus"); 
312       break;
313     case kIDPadStatus : 
314       return CacheCDBEntry(kIDPadStatus         ,"TRD/Calib/PadStatus"); 
315       break;
316
317     // Global parameters
318     case kIDMonitoringData : 
319       return CacheCDBEntry(kIDMonitoringData    ,"TRD/Calib/MonitoringData"); 
320       break;
321     case kIDFEE : 
322       return CacheCDBEntry(kIDFEE               ,"TRD/Calib/FEE"); 
323       break;
324     case kIDTrapConfig :
325       return CacheCDBEntry(kIDTrapConfig        ,"TRD/Calib/TrapConfig"); 
326       break;
327     case kIDDCS :
328       return CacheCDBEntry(kIDDCS               ,"TRD/Calib/DCS");
329       break;
330     case kIDPIDNN : 
331       return CacheCDBEntry(kIDPIDNN             ,"TRD/Calib/PIDNN");
332       break;
333     case kIDPIDLQ : 
334       return CacheCDBEntry(kIDPIDLQ             ,"TRD/Calib/PIDLQ"); 
335       break;
336     case kIDPIDLQ1D:
337       return CacheCDBEntry(kIDPIDLQ1D           ,"TRD/Calib/PIDLQ1D");
338       break;
339     case kIDRecoParam : 
340       return CacheCDBEntry(kIDRecoParam         ,"TRD/Calib/RecoParam"); 
341       break;
342     case kIDAttach : 
343       return CacheCDBEntry(kIDAttach            ,"TRD/Calib/TrkAttach"); 
344       break;
345     case kIDPHQ :
346       return CacheCDBEntry(kIDPHQ               ,"TRD/Calib/PHQ");
347       break;
348   }
349
350   return 0;
351
352 }
353
354 //_____________________________________________________________________________
355 AliCDBEntry *AliTRDcalibDB::GetCDBEntry(const char *cdbPath)
356 {
357   // 
358   // Retrieves an entry with path <cdbPath> from the CDB.
359   //
360     
361   AliCDBEntry *entry = AliCDBManager::Instance()->Get(cdbPath,fRun);
362   if (!entry) { 
363     AliError(Form("Failed to get entry: %s",cdbPath));
364     return 0; 
365   }
366   
367   return entry;
368
369 }
370
371 //_____________________________________________________________________________
372 const TObject *AliTRDcalibDB::CacheCDBEntry(Int_t id, const char *cdbPath)
373 {
374   //
375   // Caches the entry <id> with cdb path <cdbPath>
376   //
377
378   if (!fCDBCache[id]) {
379     fCDBEntries[id] = GetCDBEntry(cdbPath);
380     if (fCDBEntries[id]) {
381       fCDBCache[id] = fCDBEntries[id]->GetObject();
382       if (id == kIDOnlineGainFactor)
383         AliInfo(Form("loaded gain table: %s", fCDBEntries[id]->GetId().GetAliCDBPath().GetPath().Data()));
384     }
385   } 
386   
387   return fCDBCache[id];
388
389 }
390
391 //_____________________________________________________________________________
392 void AliTRDcalibDB::SetRun(Long64_t run)
393 {
394   //
395   // Sets current run number. Calibration data is read from the corresponding file.
396   // When the run number changes the caching is invalidated.
397   //
398
399   if (fRun == run) {
400     return;
401   }
402
403   fRun = run;
404
405   Invalidate();
406
407 }
408
409 //_____________________________________________________________________________
410 void AliTRDcalibDB::Invalidate()
411 {
412   //
413   // Invalidates cache (when run number is changed).
414   //
415   
416   for (Int_t i = 0; i < kCDBCacheSize; ++i) {
417     if (fCDBEntries[i]) {
418       if (AliCDBManager::Instance()->GetCacheFlag() == kFALSE) {
419         if ((fCDBEntries[i]->IsOwner() == kFALSE) && 
420             (fCDBCache[i])) {
421           delete fCDBCache[i];
422         }
423         delete fCDBEntries[i];
424       }
425       fCDBEntries[i] = 0;
426       fCDBCache[i]   = 0;
427     }
428   }
429
430   fOnlineGainTableID = 0;
431
432 }
433
434 //_____________________________________________________________________________
435 Float_t AliTRDcalibDB::GetNoise(Int_t det, Int_t col, Int_t row)
436 {
437   //
438   // Returns the noise level in ADC counts for the given pad.
439   //
440
441   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
442                                    (GetCachedCDBObject(kIDNoisePad));
443   if (!calPad) {
444     return -1;
445   }
446
447   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
448   if (!roc) {
449     return -1;
450   }
451
452   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
453                                    (GetCachedCDBObject(kIDNoiseChamber));
454   if (!calChamber) {
455     return -1;
456   }
457
458   return calChamber->GetValue(det) * roc->GetValue(col,row);
459
460 }
461  
462 //_____________________________________________________________________________
463 AliTRDCalROC *AliTRDcalibDB::GetNoiseROC(Int_t det)
464 {
465   //
466   // Returns the Vdrift calibration object for a given ROC
467   // containing one number per pad 
468   //
469   
470   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
471                                        (GetCachedCDBObject(kIDNoisePad));
472   if (!calPad) {
473     return 0;
474   }
475
476   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
477   if (!roc) {
478     return 0;
479   }
480   else {
481     return roc;
482   }
483
484 }
485
486 //_____________________________________________________________________________
487 const AliTRDCalDet *AliTRDcalibDB::GetNoiseDet()
488 {
489   //
490   // Returns the Vdrift calibration object
491   // containing one number per detector
492   //
493   
494   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
495                                        (GetCachedCDBObject(kIDNoiseChamber));
496   if (!calChamber) {
497     return 0;
498   }
499   else {
500     return calChamber;
501   }
502
503 }
504
505 //_____________________________________________________________________________
506 Float_t AliTRDcalibDB::GetVdrift(Int_t det, Int_t col, Int_t row)
507 {
508   //
509   // Returns the drift velocity for the given pad.
510   //
511
512   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
513                                    (GetCachedCDBObject(kIDVdriftPad));
514   if (!calPad) {
515     return -1;
516   }
517
518   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
519   if (!roc) {
520     return -1;
521   }
522
523   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
524                                    (GetCachedCDBObject(kIDVdriftChamber));
525   if (!calChamber) {
526     return -1;
527   }
528
529   return calChamber->GetValue(det) * roc->GetValue(col,row);
530
531 }
532  
533 //_____________________________________________________________________________
534 AliTRDCalROC *AliTRDcalibDB::GetVdriftROC(Int_t det)
535 {
536   //
537   // Returns the Vdrift calibration object for a given ROC
538   // containing one number per pad 
539   //
540   
541   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
542                                        (GetCachedCDBObject(kIDVdriftPad));
543   if (!calPad) {
544     return 0;
545   }
546
547   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
548   if (!roc) {
549     return 0;
550   }
551   else {
552     return roc;
553   }
554
555 }
556
557 //_____________________________________________________________________________
558 const AliTRDCalDet *AliTRDcalibDB::GetVdriftDet()
559 {
560   //
561   // Returns the Vdrift calibration object
562   // containing one number per detector
563   //
564   
565   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
566                                        (GetCachedCDBObject(kIDVdriftChamber));
567   if (!calChamber) {
568     return 0;
569   }
570   else {
571     return calChamber;
572   }
573
574 }
575
576 //_____________________________________________________________________________
577 TObjArray * AliTRDcalibDB::GetPHQ()
578 {
579   //
580   //return PHQ calibration object
581   //
582   TObjArray *arr = (TObjArray *) (GetCachedCDBObject(kIDPHQ));
583   return arr;
584 }
585
586 //_____________________________________________________________________________
587 Float_t AliTRDcalibDB::GetVdriftAverage(Int_t det)
588 {
589   //
590   // Returns the average drift velocity for the given detector
591   //
592
593   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
594                                    (GetCachedCDBObject(kIDVdriftChamber));
595   if (!calDet) {
596     return -1;
597   }
598
599   return calDet->GetValue(det);
600
601 }
602 //_____________________________________________________________________________
603 const AliTRDCalDet *AliTRDcalibDB::GetExBDet()
604 {
605   //
606   // Returns the exB calibration object
607   // containing one number per detector
608   //
609   
610   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> (GetCachedCDBObject(kIDExBChamber));
611   
612   Double_t meanexb = 100.0;
613   if (calChamber) meanexb = calChamber->GetMean();
614   //printf("mean %f\n",meanexb);  
615
616   if ((!calChamber) || (meanexb > 70.0)) {
617     
618     const AliTRDCalDet     *calChambervdrift = dynamic_cast<const AliTRDCalDet *> 
619         (GetCachedCDBObject(kIDVdriftChamber));
620       if (!calChambervdrift) {
621         return 0;
622       }
623       else {
624         AliTRDCalDet *calDetExB = new AliTRDCalDet("lorentz angle tan","lorentz angle tan (detector value)");
625         for(Int_t k = 0; k < 540; k++){
626           calDetExB->SetValue(k,AliTRDCommonParam::Instance()->GetOmegaTau(calChambervdrift->GetValue(k)));
627         }
628         return calDetExB;
629       }
630   }
631   else return calChamber;
632
633 }
634 //_____________________________________________________________________________
635 Float_t AliTRDcalibDB::GetT0(Int_t det, Int_t col, Int_t row)
636 {
637   //
638   // Returns t0 for the given pad.
639   //
640   
641   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
642                                        (GetCachedCDBObject(kIDT0Pad));
643   if (!calPad) {
644     return -1;
645   }
646
647   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
648   if (!roc) {
649     return -1;
650   }
651
652   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
653                                        (GetCachedCDBObject(kIDT0Chamber));
654   if (!calChamber) {
655     return -1;
656   }
657
658   return calChamber->GetValue(det) + roc->GetValue(col,row);
659
660 }
661  
662 //_____________________________________________________________________________
663 AliTRDCalROC *AliTRDcalibDB::GetT0ROC(Int_t det)
664 {
665   //
666   // Returns the t0 calibration object for a given ROC
667   // containing one number per pad 
668   //
669   
670   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
671                                        (GetCachedCDBObject(kIDT0Pad));
672   if (!calPad) {
673     return 0;
674   }
675
676   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
677   if (!roc) {
678     return 0;
679   }
680   else {
681     return roc;
682   }
683
684 }
685
686 //_____________________________________________________________________________
687 const AliTRDCalDet *AliTRDcalibDB::GetT0Det()
688 {
689   //
690   // Returns the t0 calibration object
691   // containing one number per detector
692   //
693   
694   const AliTRDCalDet     *calChamber = dynamic_cast<const AliTRDCalDet *> 
695                                        (GetCachedCDBObject(kIDT0Chamber));
696   if (!calChamber) {
697     return 0;
698   }
699   else {
700     return calChamber;
701   }
702
703 }
704
705 //_____________________________________________________________________________
706 Float_t AliTRDcalibDB::GetT0Average(Int_t det)
707 {
708   //
709   // Returns the average t0 for the given detector
710   //
711
712   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
713                                    (GetCachedCDBObject(kIDT0Pad));
714   if (!calPad) {
715     return -1;
716   }
717
718   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
719   if (!roc) {
720     return -1;
721   }
722
723   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
724                                    (GetCachedCDBObject(kIDT0Chamber));
725   if (!calDet) {
726     return -1;
727   }
728
729   Double_t sum = 0.0; 
730   for (Int_t channel = 0; channel < roc->GetNchannels(); ++channel) {
731     sum += roc->GetValue(channel);
732   }
733   sum /= roc->GetNchannels();
734   sum += calDet->GetValue(det);
735   return sum;
736
737 }
738
739 //_____________________________________________________________________________
740 AliTRDCalOnlineGainTableROC* AliTRDcalibDB::GetOnlineGainTableROC(Int_t det)
741 {
742   //
743   // Returns the online gain factor table for a given ROC.
744   //
745
746   if (!HasOnlineFilterGain()) {
747     return 0x0;
748   }
749   
750   const AliTRDCalOnlineGainTable *calOnline 
751      = dynamic_cast<const AliTRDCalOnlineGainTable *> 
752                                    (GetCachedCDBObject(kIDOnlineGainFactor));
753   if (!calOnline) {
754     return 0x0;
755   }
756
757   return calOnline->GetGainTableROC(det);
758
759 }
760
761 //_____________________________________________________________________________
762 Float_t AliTRDcalibDB::GetOnlineGainFactor(Int_t det, Int_t col, Int_t row)
763 {
764   //
765   // Returns the online gain factor for the given pad.
766   //
767
768   if (!HasOnlineFilterGain()) {
769     return -1;
770   }
771   
772   const AliTRDCalOnlineGainTable *calOnline 
773      = dynamic_cast<const AliTRDCalOnlineGainTable *> 
774                                    (GetCachedCDBObject(kIDOnlineGainFactor));
775   if (!calOnline) {
776     return -1;
777   }
778
779   return calOnline->GetGainCorrectionFactor(det,row,col);
780
781 }
782
783 //_____________________________________________________________________________
784 AliTRDCalROC *AliTRDcalibDB::GetGainFactorROC(Int_t det)
785 {
786   //
787   // Returns the gain factor calibration object for a given ROC
788   //
789   
790   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
791                                    (GetCachedCDBObject(kIDGainFactorPad));
792   if (!calPad) {
793     return 0;
794   }
795
796   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
797   if (!roc) {
798     return 0;
799   }
800   else {
801     return roc;
802   }
803
804 }
805
806 //_____________________________________________________________________________
807 Float_t AliTRDcalibDB::GetGainFactor(Int_t det, Int_t col, Int_t row)
808 {
809   //
810   // Returns the gain factor for the given pad.
811   //
812   
813   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
814                                    (GetCachedCDBObject(kIDGainFactorPad));
815   if (!calPad) {
816     return -1;
817   }
818
819   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
820   if (!roc) {
821     return -1;
822   }
823
824   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
825                                    (GetCachedCDBObject(kIDGainFactorChamber));
826   if (!calChamber) {
827     return -1;
828   }
829
830   return calChamber->GetValue(det) * roc->GetValue(col,row);
831
832 }
833
834 //_____________________________________________________________________________
835 const AliTRDCalDet *AliTRDcalibDB::GetGainFactorDet()
836 {
837   //
838   // Returns the gain factor calibration object
839   // containing one number per detector
840   //
841
842   const AliTRDCalDet *calChamber = dynamic_cast<const AliTRDCalDet *> 
843                                    (GetCachedCDBObject(kIDGainFactorChamber));
844   if (!calChamber) {
845     return 0;
846   }
847   else {
848     return calChamber;
849   }
850
851 }
852
853 //_____________________________________________________________________________
854 Float_t AliTRDcalibDB::GetGainFactorAverage(Int_t det)
855 {
856   //
857   // Returns the average gain factor for the given detector
858   //
859
860   const AliTRDCalDet *calDet     = dynamic_cast<const AliTRDCalDet *> 
861                                    (GetCachedCDBObject(kIDGainFactorChamber));
862   if (!calDet) {
863     return -1;
864   }
865
866   return calDet->GetValue(det);
867
868 }
869
870 //_____________________________________________________________________________
871 AliTRDCalROC *AliTRDcalibDB::GetPRFROC(Int_t det)
872 {
873   //
874   // Returns the PRF calibration object for a given ROC
875   // containing one number per pad 
876   //
877   
878   const AliTRDCalPad     *calPad     = dynamic_cast<const AliTRDCalPad *> 
879                                        (GetCachedCDBObject(kIDPRFWidth));
880   if (!calPad) {
881     return 0;
882   }
883
884   AliTRDCalROC           *roc        = calPad->GetCalROC(det);
885   if (!roc) {
886     return 0;
887   }
888   else {
889     return roc;
890   }
891
892 }
893
894 //_____________________________________________________________________________
895 Float_t AliTRDcalibDB::GetPRFWidth(Int_t det, Int_t col, Int_t row)
896 {
897   //
898   // Returns the PRF width for the given pad.
899   //
900   
901   const AliTRDCalPad *calPad     = dynamic_cast<const AliTRDCalPad *> 
902                                    (GetCachedCDBObject(kIDPRFWidth));
903   if (!calPad) {
904     return -1;
905   }
906
907   AliTRDCalROC       *roc        = calPad->GetCalROC(det);
908   if (!roc) {
909     return -1;
910   }
911
912   return roc->GetValue(col,row);
913
914 }
915   
916 //_____________________________________________________________________________
917 Int_t AliTRDcalibDB::ExtractTimeBinsFromString(TString tbstr)
918 {
919   // Check if there is any content in the string first
920   if (tbstr.Length() == 0) {
921     AliError("Parameter for number of timebins is empty!");
922     return -1;
923   }
924
925   // Check if we have the correct config parameter
926   TString tbident  = "tb";
927   TString tbsubstr = tbstr(0,2);
928   if (!tbsubstr.EqualTo(tbident)) {
929     AliError(Form("Parameter for number of timebins is corrupted (%s)!", tbstr.Data()));
930     return -1;
931   }
932
933   tbstr.Remove(0,2);
934   // check if there is more than a number
935   if (!tbstr.IsDigit()) {
936     AliError(Form("Parameter for number of timebins is corrupted (%s)!", tbstr.Data()));
937     return -1;
938   }
939
940   return tbstr.Atoi();
941
942 }
943
944 //_____________________________________________________________________________
945 Int_t AliTRDcalibDB::GetNumberOfTimeBinsDCSBoard(){
946   // This is an old way to extract the number of time bins,
947   // only to be used as a fallback to see whether there's
948   // a patched OCDB object!
949   
950   Int_t nTB=-1;
951   // Get the array with SOR and EOR
952   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
953   if(!dcsArr)
954     return -1; // Error
955   // Check CalDCS version
956   Int_t calver = 0;
957   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS")) calver = 1;
958   else if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
959   // CalDCS version 1
960   if (calver == 1) {
961     // DCS object
962     AliTRDCalDCS *calSOR = dynamic_cast<AliTRDCalDCS *>(dcsArr->At(0));
963     // SOR mandantory
964     if(!calSOR)
965       return -1; // Error
966     else
967       nTB=calSOR->GetGlobalNumberOfTimeBins();
968     AliTRDCalDCS *calEOR = dynamic_cast<AliTRDCalDCS *>(dcsArr->At(1));
969     if(calEOR && calEOR->GetGlobalNumberOfTimeBins()!=nTB)
970       return -2; // Mixed
971   }
972   else if (calver == 2) {
973     // DCS object
974     AliTRDCalDCSv2 *calSOR = dynamic_cast<AliTRDCalDCSv2 *>(dcsArr->At(0));
975     // SOR mandantory
976     if(!calSOR)
977       return -1; // Error
978     else
979       nTB=calSOR->GetGlobalNumberOfTimeBins();
980     AliTRDCalDCSv2 *calEOR = dynamic_cast<AliTRDCalDCSv2 *>(dcsArr->At(1));
981     if(calEOR && calEOR->GetGlobalNumberOfTimeBins()!=nTB)
982       return -2; // Mixed
983   }
984   else{
985     // No version 1 nor version 2 object
986     return -1; // Error
987   }
988
989   // All well
990   return nTB;
991 }
992 //_____________________________________________________________________________
993 Int_t AliTRDcalibDB::GetNumberOfTimeBinsDCS()
994 {
995   //
996   // Returns number of time bins from the DCS
997   //
998
999   // Initialize with values indicating no information
1000   TString cfg="";
1001   Int_t nTB = -1;
1002   // Extract the global configuration name
1003   GetGlobalConfigurationByChamber(cfg,kTimebin);
1004   // Extract the number of time bins from
1005   // the global configuration 
1006   if(cfg.Length()>0){
1007     nTB = ExtractTimeBinsFromString(cfg);
1008   }
1009   if(nTB>0){
1010     // All well, we could extract the number
1011     // of time bins from the configuration name
1012     return nTB;
1013   }
1014   else{
1015     // No number of time bins from config name.
1016     // No board responded or similar.
1017     // We patched some OCDB entries with 
1018     // only the global number of time bins set.
1019     // We should get these here
1020     nTB=GetNumberOfTimeBinsDCSBoard();
1021     if(nTB>0){
1022       AliWarning("Using old method for number of time bins."
1023                  " This is probably a patched OCDB entry");
1024       return nTB;
1025     }
1026     else{
1027       // Error
1028       AliError("No number of time bins either"
1029                " from config name or patched OCDB entry");
1030       return -1;
1031     }
1032   }
1033
1034   
1035   // // Get the corresponding parameter
1036   // TString cfgstr = "", cfgname = "";
1037   // GetGlobalConfiguration(cfgname);
1038   // if(cfgname.Length()==0)
1039   //   return -1;
1040   // GetDCSConfigParOption(cfgname, kTimebin, 0, cfgstr);
1041   // if(cfgstr.Length()==0)
1042   //   return -1;
1043   // return ExtractTimeBinsFromString(cfgstr);
1044
1045 }
1046
1047 //_____________________________________________________________________________
1048 void AliTRDcalibDB::GetFilterType(TString &filterType)
1049 {
1050   //
1051   // Returns the filter type
1052   //
1053
1054   TString cfgname = "";
1055   GetGlobalConfiguration(cfgname);
1056   GetDCSConfigParOption(cfgname, kFltrSet, 0, filterType);
1057
1058 }
1059
1060 //_____________________________________________________________________________
1061 Int_t AliTRDcalibDB::GetOnlineGainTableID()
1062 {
1063   //
1064   // Get the gain table ID from the DCS
1065   //
1066
1067   if (fOnlineGainTableID > 0) {
1068     return fOnlineGainTableID;
1069   }
1070
1071   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
1072   if (!dcsArr){
1073     return -1;
1074   }
1075
1076   Int_t esor   = 0; // Take SOR
1077   Int_t calver = 0; // Check CalDCS version
1078   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS"))   calver = 1;
1079   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
1080
1081   if      (calver == 1) {
1082
1083     // No data for old DCS object available, anyway
1084     return -1;
1085
1086   } 
1087   else if (calver == 2) {
1088
1089     // DCSv2 object
1090     const AliTRDCalDCSv2 *calDCSv2 = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(esor));
1091     if(!calDCSv2){
1092       return -1;
1093     }
1094
1095     TString tableName = "";
1096     for (Int_t i = 0; i < 540; i++) {
1097       const AliTRDCalDCSFEEv2 *calDCSFEEv2 = calDCSv2->GetCalDCSFEEObj(0);
1098       tableName = calDCSFEEv2->GetGainTableName();
1099       if (tableName.Length() > 0) {
1100         break;
1101       }
1102     }
1103     if (tableName.CompareTo("Krypton_2011-01")               == 0)
1104       fOnlineGainTableID = 1;
1105     else if (tableName.CompareTo("Gaintbl_Uniform_FGAN0_2011-01") == 0)
1106       fOnlineGainTableID = 2;
1107     else if (tableName.CompareTo("Gaintbl_Uniform_FGAN8_2011-01") == 0)
1108       fOnlineGainTableID = 3;
1109     else if (tableName.CompareTo("Krypton_2011-02")               == 0)
1110       fOnlineGainTableID = 4;
1111     else if (tableName.CompareTo("Krypton_2011-03")               == 0)
1112       fOnlineGainTableID = 5;
1113     else if (tableName.CompareTo("Gaintbl_Uniform_FGAN0_2012-01") == 0)
1114       fOnlineGainTableID = 6;
1115     else if (tableName.CompareTo("Gaintbl_Uniform_FGAN8_2012-01") == 0)
1116       fOnlineGainTableID = 7;
1117     else if (tableName.CompareTo("Krypton_2012-01")               == 0)
1118       fOnlineGainTableID = 8;
1119     else
1120       AliFatal(Form("unknown gaintable <%s> requested", tableName.Data()));
1121
1122     AliInfo(Form("looking for gaintable: %s (id %i)",
1123                  tableName.Data(), fOnlineGainTableID));
1124
1125     if (fOnlineGainTableID > 0)
1126       return fOnlineGainTableID;
1127   } 
1128   else {
1129
1130     AliError("NO DCS/DCSv2 OCDB entry found!");
1131     return -1;
1132
1133   }
1134
1135   return -1;
1136
1137 }
1138
1139 //_____________________________________________________________________________
1140 void AliTRDcalibDB::GetGlobalConfiguration(TString &config)
1141 {
1142   //
1143   // Get Configuration from the DCS
1144   //
1145
1146   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
1147   if(!dcsArr){
1148     AliError("No DCS CDB Object available!");
1149     config = "";
1150     return;
1151   }
1152
1153   Int_t idSOR = 0, idEOR=1; // The index of SOR and EOR
1154   Bool_t hasSOR = (dcsArr->At(idSOR));
1155   Bool_t hasEOR = (dcsArr->At(idEOR));
1156   TString cfgSOR = "", cfgEOR = ""; // The configuration at SOR/EOR
1157
1158   // The SOR object is mandatory
1159   if (!hasSOR) {
1160     AliError("NO SOR object found in CDB file!");
1161     config = "";
1162     return;
1163   }
1164   if (!hasEOR) AliWarning("NO EOR object found in CDB file!");
1165
1166   // Check CalDCS version
1167   Int_t calver = 0;
1168   if (!strcmp(dcsArr->At(idSOR)->ClassName(),"AliTRDCalDCS")) calver = 1;
1169   else if (!strcmp(dcsArr->At(idSOR)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
1170
1171   // Get the configuration strings
1172   if (calver == 1) {
1173     // DCS object
1174     const AliTRDCalDCS *calSOR = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(idSOR));
1175     cfgSOR = calSOR->GetGlobalConfigName();
1176     if (hasEOR) {
1177       const AliTRDCalDCS *calEOR = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(idEOR));
1178       cfgEOR = calEOR->GetGlobalConfigName();
1179     }
1180   } 
1181   else if (calver == 2) {
1182     // DCSv2 object
1183     const AliTRDCalDCSv2 *calv2SOR = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(idSOR));
1184     cfgSOR = calv2SOR->GetGlobalConfigName();
1185     if (hasEOR) {
1186       const AliTRDCalDCSv2 *calv2EOR = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(idEOR));
1187       cfgEOR = calv2EOR->GetGlobalConfigName();
1188     }
1189   } 
1190   else {
1191     AliError("NO DCS/DCSv2 OCDB entry found!");
1192     config = "";
1193     return;
1194   }
1195
1196   // If there is no EOR entry, return the SOR value
1197   if (!hasEOR || cfgEOR.Length()==0) {
1198     config = cfgSOR;
1199     return;
1200   }
1201
1202   // Check if the configuration is the same for both
1203   if (cfgSOR.EqualTo(cfgEOR)) {
1204     config = cfgSOR;
1205     return;
1206   }
1207
1208   // When both SOR and EOR have an entry but are different, the config is not defined
1209   AliError("Inconsistent configuration at start and end of run found!");
1210   config = "";
1211   return;
1212
1213 }
1214 //_____________________________________________________________________________
1215 void AliTRDcalibDB::GetGlobalConfigurationByChamber(TString &config,Int_t par, Int_t opt)
1216 {
1217   //
1218   // Get Configuration from the DCS
1219   //
1220
1221   // par is the enumeration from kFltrSet = 1 ... kAddOpti 6
1222   //   example:
1223   //      cf_p_nozs_tb30_csmtrk_ptrg
1224   //   par   1   2    3    4     5
1225   //      kFltrSet  kTimebin   kTrigSet
1226   //          kReadout  kTrkMode
1227   // opt is 0 for the value itself and 1,2,3 for the first, second option
1228   // opt has standard value of 0 in header file
1229
1230
1231   // The point is that we extract the parameter/option for 
1232   // each of the 540 chambers and compare only this 
1233   // parameter/option for a global value, not the full
1234   // configuration name with all parameters
1235
1236   // Get the array with SOR and EOR
1237   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
1238   if(!dcsArr){
1239     AliError("No DCS CDB Object available!");
1240     config = "";
1241     return;
1242   }
1243
1244   // Check CalDCS version
1245   Int_t calver = 0;
1246   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS")) calver = 1;
1247   else if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
1248   //
1249   // Get the configuration strings
1250   //
1251   // CalDCS version 1
1252   if (calver == 1) {
1253
1254     // Loop over SOR/EOR
1255     for(Int_t iSEOR=0;iSEOR<2;iSEOR++){
1256
1257       // DCS object
1258       AliTRDCalDCS *cal = dynamic_cast<AliTRDCalDCS *>(dcsArr->At(iSEOR));
1259       if(!cal){
1260         // SOR mandantory
1261         if(iSEOR==0){
1262           AliError("NO SOR object found in CDB file!");
1263           config = "";
1264           return;
1265         }
1266         else{
1267           AliWarning("NO EOR object found in CDB file!");
1268           continue;
1269         }
1270       } // Check DCS object is there
1271       
1272       // Loop over 540 chambers
1273       {
1274         Int_t iDet=0;
1275         // Find the first chamber in SOR
1276         if(iSEOR==0){
1277           // Loop until we find the first chamber
1278           for(;iDet<kNdet;iDet++){
1279             const AliTRDCalDCSFEE *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
1280             // Check it's an installed chamber that responded
1281             if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
1282
1283               // We ask for a valid configuration name starting with cf_
1284               if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
1285                 // Set the parameter of the first good ROC as global,
1286                 // we take only the requested part!
1287                 GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, config);
1288                 // Increase counter to not look at this chamber again
1289                 // and break loop
1290                 iDet++;
1291                 break;
1292               } // End: valid config name
1293             } // End: first good chamber
1294           } // End: find the first chamber
1295         } // End: is SOR
1296
1297         // Now compare the other chambers with the first one
1298         for(;iDet<kNdet;iDet++){
1299           const AliTRDCalDCSFEE *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
1300           // Check it's an installed chamber that responded
1301           if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
1302             
1303             // We ask for a valid configuration name starting with cf_
1304             if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
1305
1306               // Get the parameter/option of this chamber
1307               TString tmpcfg;
1308               GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, tmpcfg);
1309               // Compare with the global value
1310               if(config.CompareTo(tmpcfg)){
1311                 // Two cases mixed or changed during run
1312                 if(iSEOR==0){
1313                   AliError("Mixed DCS configuration for different chambers!");
1314                   config="mixed";
1315                   return;
1316                 } else {
1317                   AliError("Inconsistent configuration at start and end of run found!");
1318                   config = "";
1319                   return;
1320                 }
1321               }
1322             }// Valid config name 
1323           }// Good chamber
1324         } // Second half loop 
1325       } // Loop over 540 chambers
1326     } // Loop over SOR / EOR 
1327   } // End calver 1
1328   //
1329   // CalDCS version 2
1330   //
1331   else if (calver == 2) {
1332
1333     // Loop over SOR/EOR
1334     for(Int_t iSEOR=0;iSEOR<2;iSEOR++){
1335
1336       // DCS object
1337       AliTRDCalDCSv2 *cal = dynamic_cast<AliTRDCalDCSv2 *>(dcsArr->At(iSEOR));
1338       if(!cal){
1339         // SOR mandantory
1340         if(iSEOR==0){
1341           AliError("NO SOR object found in CDB file!");
1342           config = "";
1343           return;
1344         }
1345         else{
1346           AliWarning("NO EOR object found in CDB file!");
1347           continue;
1348         }
1349       } // Check DCS object is there
1350       
1351       // Loop over 540 chambers
1352       {
1353         Int_t iDet=0;
1354         // Find the first chamber in SOR
1355         if(iSEOR==0){
1356           // Loop until we find the first chamber
1357           for(;iDet<kNdet;iDet++){
1358             const AliTRDCalDCSFEEv2 *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
1359             // Check it's an installed chamber that responded
1360             if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
1361               // Check for a valid config name
1362               if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
1363
1364                 // Set the parameter of the first good ROC as global,
1365                 // we take only the requested part!
1366                 GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, config);
1367                 // Increase counter to not look at this chamber again
1368                 // and break loop
1369                 iDet++;
1370                 break;
1371               } // End: valid config name
1372             } // End: first good chamber
1373           } // End: find the first chamber
1374         } // End: is SOR
1375
1376         // Now compare the other chambers with the first one
1377         for(;iDet<kNdet;iDet++){
1378           const AliTRDCalDCSFEEv2 *DCSFEEObj = cal->GetCalDCSFEEObj(iDet);
1379           // Check it's an installed chamber that responded
1380           if(DCSFEEObj && !DCSFEEObj->GetStatusBit()) {
1381
1382             // Check for a valid config name
1383             if(DCSFEEObj->GetConfigName().BeginsWith("cf_")){
1384
1385               // Get the parameter/option of this chamber
1386               TString tmpcfg;
1387               GetDCSConfigParOption(DCSFEEObj->GetConfigName(), par, opt, tmpcfg);
1388               // Compare with the global value
1389               if(config.CompareTo(tmpcfg)){
1390                 // Two cases mixed or changed during run
1391                 if(iSEOR==0){
1392                   AliError("Mixed DCS configuration for different chambers!");
1393                   config="mixed";
1394                   return;
1395                 } else {
1396                   AliError("Inconsistent configuration at start and end of run found!");
1397                   config = "";
1398                   return;
1399                 }
1400               }
1401             } // End: valid config name
1402           } // End: Good chamber
1403         } // End: Second half loop 
1404       } // Loop over 540 chambers
1405     } // Loop over SOR / EOR 
1406   } // End calver 2
1407   else {
1408     AliError("NO DCS/DCSv2 OCDB entry found!");
1409     config = "";
1410     return;
1411   }
1412
1413 }
1414 //_____________________________________________________________________________
1415 void AliTRDcalibDB::GetGlobalConfigurationVersion(TString &version)
1416 {
1417   //
1418   // Get Version of Configuration from the DCS
1419   //
1420
1421   const TObjArray *dcsArr = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDDCS));
1422   if(!dcsArr){
1423     version = "";
1424     return;
1425   }
1426
1427   Int_t esor   = 0; // Take SOR
1428   Int_t calver = 0; // Check CalDCS version
1429   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCS"))   calver = 1;
1430   if (!strcmp(dcsArr->At(0)->ClassName(),"AliTRDCalDCSv2")) calver = 2;
1431
1432   if      (calver == 1) {
1433
1434     // DCS object
1435     const AliTRDCalDCS   *calDCS   = dynamic_cast<const AliTRDCalDCS *>(dcsArr->At(esor));
1436     if(!calDCS){
1437       version = "";
1438       return;
1439     } 
1440     version = calDCS->GetGlobalConfigVersion();
1441
1442   } 
1443   else if (calver == 2) {
1444
1445     // DCSv2 object
1446     const AliTRDCalDCSv2 *calDCSv2 = dynamic_cast<const AliTRDCalDCSv2 *>(dcsArr->At(esor));
1447     if(!calDCSv2){
1448       version = "";
1449       return;
1450     } 
1451     version = calDCSv2->GetGlobalConfigVersion();
1452
1453   } 
1454   else {
1455
1456     AliError("NO DCS/DCSv2 OCDB entry found!");
1457
1458   }
1459
1460 }
1461
1462 //_____________________________________________________________________________
1463 Int_t AliTRDcalibDB::GetNumberOfParsDCS(TString cname, Char_t delimiter)
1464 {
1465   // Get the number of configuration parameters from the DCS config
1466
1467   //AliInfo(Form("\"%s\" tokenized by \"%c\"", cname.Data(), delimiter));
1468   if(!cname.Length()) return -1;  // -1 for the "cf"
1469   Int_t nconf(0);
1470   for(Int_t ich(1); ich<cname.Length()-1; ich++){ if(cname[ich]==delimiter) nconf++;}
1471   return nconf;
1472 }
1473
1474 //_____________________________________________________________________________
1475 Int_t AliTRDcalibDB::GetNumberOfOptsDCS(TString cname, Int_t cfgType)
1476 {
1477   // Get the number of options of a given configuration parameter from DCS
1478
1479   Char_t cdelim = '_', // define the delimiters
1480          odelim = '-';
1481   Int_t nconfig = GetNumberOfParsDCS(cname, cdelim);
1482
1483   // protect
1484   if ((nconfig == -1) || (nconfig < cfgType)) {
1485     AliError("Not enough parameters in DCS configuration name!");
1486     return 0;
1487   }
1488
1489   TObjArray *carr = cname.Tokenize(cdelim);
1490   Int_t nopt = GetNumberOfParsDCS(((TObjString*)carr->At(cfgType))->GetString(), odelim);
1491   carr->Delete(); delete carr;
1492   return nopt;
1493 }
1494
1495 //_____________________________________________________________________________
1496 void AliTRDcalibDB::GetDCSConfigParOption(TString cname, Int_t cfgType, Int_t option, TString &cfgo)
1497 {
1498   //
1499   // Get a configuration (see enum in header file) or the options of a configuration
1500   // option == 0 returns the configuration itself
1501   // option >  0 returns the optional parameter Nr. (option) of the configuration (cfgType)
1502   //
1503
1504   Char_t cdelim = '_', // define the delimiters
1505          odelim = '-';
1506
1507   Int_t nconfig = GetNumberOfParsDCS(cname, cdelim);
1508   // protect
1509   if (nconfig == -1) {
1510     AliError("DCS configuration name empty!");
1511     cfgo = "";
1512     return;
1513   } else if (nconfig < cfgType) {
1514     AliError(Form("Not enough parameters in DCS configuration name!"
1515                   " Name %s",cname.Data()));
1516     cfgo = "";
1517     return;
1518   }
1519
1520   TObjArray *carr = cname.Tokenize(cdelim);
1521   TString cfgString(((TObjString*)carr->At(cfgType))->GetString());
1522   Int_t noptions = GetNumberOfParsDCS(cfgString, odelim);
1523   // protect
1524   if (noptions < option) {
1525     AliError(Form("Not enough options in DCS configuration name!"
1526                   " Name %s",cname.Data()));
1527     cfgo = "";
1528     carr->Delete(); delete carr;
1529     return;
1530   }
1531   TObjArray *oarr = cfgString.Tokenize(odelim);
1532   cfgo = ((TObjString*)oarr->At(option))->GetString();
1533   carr->Delete(); delete carr;
1534   oarr->Delete(); delete oarr;
1535   return;
1536
1537 }
1538
1539 //_____________________________________________________________________________
1540 Bool_t AliTRDcalibDB::HasOnlineFilterPedestal()
1541 {
1542   //
1543   // Checks whether pedestal filter was applied online
1544   //
1545
1546   TString filterconfig;
1547   GetFilterType(filterconfig);
1548
1549   return filterconfig.Contains("p");
1550
1551 }
1552
1553 //_____________________________________________________________________________
1554 Bool_t AliTRDcalibDB::HasOnlineFilterGain()
1555 {
1556   //
1557   // Checks whether online gain filter was applied
1558   //
1559
1560   TString filterconfig;
1561   GetFilterType(filterconfig);
1562
1563   return filterconfig.Contains("g");
1564
1565 }
1566
1567 //_____________________________________________________________________________
1568 Bool_t AliTRDcalibDB::HasOnlineTailCancellation()
1569 {
1570   //
1571   // Checks whether online tail cancellation was applied
1572   //
1573
1574   TString filterconfig;
1575   GetFilterType(filterconfig);
1576
1577   return filterconfig.Contains("t");
1578
1579 }
1580
1581 //_____________________________________________________________________________
1582 Char_t AliTRDcalibDB::GetPadStatus(Int_t det, Int_t col, Int_t row)
1583 {
1584   //
1585   // Returns the status of the given pad
1586   //
1587
1588   const AliTRDCalPadStatus *cal  = dynamic_cast<const AliTRDCalPadStatus *> 
1589                                    (GetCachedCDBObject(kIDPadStatus));
1590   if (!cal) {
1591     return -1;
1592   }
1593
1594   const AliTRDCalSingleChamberStatus *roc = cal->GetCalROC(det);
1595   if (!roc) {
1596     return -1;
1597   }
1598
1599   return roc->GetStatus(col,row);
1600
1601 }
1602
1603 //_____________________________________________________________________________
1604 AliTRDCalSingleChamberStatus* AliTRDcalibDB::GetPadStatusROC(Int_t det)
1605 {
1606   //
1607   // Returns the pad status calibration object for a given ROC
1608   //
1609
1610   const AliTRDCalPadStatus *cal  = dynamic_cast<const AliTRDCalPadStatus *> 
1611                                    (GetCachedCDBObject(kIDPadStatus));
1612   if (!cal) {
1613     return 0;
1614   }
1615
1616   AliTRDCalSingleChamberStatus *roc = cal->GetCalROC(det);
1617   if (!roc) {
1618     return 0;
1619   }
1620   else {
1621     return roc;
1622   }
1623
1624 }
1625
1626 //_____________________________________________________________________________
1627 Char_t AliTRDcalibDB::GetChamberStatus(Int_t det)
1628 {
1629   //
1630   // Returns the status of the given chamber
1631   //
1632
1633   const AliTRDCalChamberStatus *cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1634                                       (GetCachedCDBObject(kIDChamberStatus));
1635   if (!cal) {
1636     return -1;
1637   }
1638
1639   return cal->GetStatus(det);
1640
1641 }
1642
1643 //_____________________________________________________________________________
1644 AliTRDrecoParam* AliTRDcalibDB::GetRecoParam(Int_t */*eventtype*/)
1645 {
1646   //
1647   // Returns the TRD reconstruction parameters from the OCDB
1648   //
1649
1650   const TClonesArray *recos = dynamic_cast<const TClonesArray*>(GetCachedCDBObject(kIDRecoParam));
1651   if (!recos) return 0x0;
1652
1653   // calculate entry based on event type info
1654   Int_t n = 0; //f(eventtype[0], eventtype[1], ....)
1655
1656   return (AliTRDrecoParam *) recos->UncheckedAt(n);
1657
1658 }
1659
1660 //_____________________________________________________________________________
1661 Bool_t AliTRDcalibDB::IsPadMasked(Int_t det, Int_t col, Int_t row)
1662 {
1663   //
1664   // Returns status, see name of functions for details ;-)
1665   //
1666
1667   const AliTRDCalPadStatus         *cal = dynamic_cast<const AliTRDCalPadStatus *> 
1668                                           (GetCachedCDBObject(kIDPadStatus));
1669   if (!cal) {
1670     return -1;
1671   }
1672
1673   return cal->IsMasked(det,col,row);
1674
1675 }
1676
1677 //_____________________________________________________________________________
1678 Bool_t AliTRDcalibDB::IsPadBridgedLeft(Int_t det, Int_t col, Int_t row)
1679 {
1680   //
1681   // Returns status, see name of functions for details ;-)
1682   //
1683
1684   const AliTRDCalPadStatus         *cal = dynamic_cast<const AliTRDCalPadStatus *> 
1685                                           (GetCachedCDBObject(kIDPadStatus));
1686   if (!cal) {
1687     return -1;
1688   }
1689
1690   return cal->IsBridgedLeft(det,col,row);
1691
1692 }
1693
1694 //_____________________________________________________________________________
1695 Bool_t AliTRDcalibDB::IsPadBridgedRight(Int_t det, Int_t col, Int_t row)
1696 {
1697   //
1698   // Returns status, see name of functions for details ;-)
1699   //
1700
1701   const AliTRDCalPadStatus         * cal = dynamic_cast<const AliTRDCalPadStatus *> 
1702                                            (GetCachedCDBObject(kIDPadStatus));
1703   if (!cal) {
1704     return -1;
1705   }
1706
1707   return cal->IsBridgedRight(det,col,row);
1708
1709 }
1710
1711 //_____________________________________________________________________________
1712 Bool_t AliTRDcalibDB::IsPadNotConnected(Int_t det, Int_t col, Int_t row)
1713 {
1714   //
1715   // Returns status, see name of functions for details ;-)
1716   //
1717
1718   const AliTRDCalPadStatus         * cal = dynamic_cast<const AliTRDCalPadStatus *> 
1719                                            (GetCachedCDBObject(kIDPadStatus));
1720   if (!cal) {
1721     return -1;
1722   }
1723
1724   return cal->IsNotConnected(det,col,row);
1725
1726 }
1727
1728 //_____________________________________________________________________________
1729 Bool_t AliTRDcalibDB::IsChamberGood(Int_t det)
1730 {
1731   //
1732   // Returns status, see name of functions for details ;-)
1733   //
1734
1735   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1736                                            (GetCachedCDBObject(kIDChamberStatus));
1737   if (!cal) {
1738     return -1;
1739   }
1740
1741   return cal->IsGood(det);
1742
1743 }
1744
1745 //_____________________________________________________________________________
1746 Bool_t AliTRDcalibDB::IsChamberNoData(Int_t det)
1747 {
1748   //
1749   // Returns status, see name of functions for details ;-)
1750   //
1751
1752   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1753                                            (GetCachedCDBObject(kIDChamberStatus));
1754   if (!cal) {
1755     return -1;
1756   }
1757
1758   return cal->IsNoData(det);
1759
1760 }
1761
1762 //_____________________________________________________________________________
1763 Bool_t AliTRDcalibDB::IsHalfChamberNoData(Int_t det, Int_t side)
1764 {
1765   //
1766   // Returns status, see name of functions for details ;-)
1767   //
1768
1769   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1770                                            (GetCachedCDBObject(kIDChamberStatus));
1771   if (!cal) {
1772     return -1;
1773   }
1774
1775   return side > 0 ? cal->IsNoDataSideB(det) : cal->IsNoDataSideA(det);
1776
1777 }
1778
1779 //_____________________________________________________________________________
1780 Bool_t AliTRDcalibDB::IsChamberBadCalibrated(Int_t det)
1781 {
1782   //
1783   // Returns status, see name of functions for details ;-)
1784   //
1785
1786   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1787                                            (GetCachedCDBObject(kIDChamberStatus));
1788   if (!cal) {
1789     return -1;
1790   }
1791
1792   return cal->IsBadCalibrated(det);
1793
1794 }
1795
1796 //_____________________________________________________________________________
1797 Bool_t AliTRDcalibDB::IsChamberNotCalibrated(Int_t det)
1798 {
1799   //
1800   // Returns status, see name of functions for details ;-)
1801   //
1802
1803   const AliTRDCalChamberStatus     * cal = dynamic_cast<const AliTRDCalChamberStatus *> 
1804                                            (GetCachedCDBObject(kIDChamberStatus));
1805   if (!cal) {
1806     return -1;
1807   }
1808
1809   return cal->IsNotCalibrated(det);
1810
1811 }
1812
1813 //_____________________________________________________________________________
1814 const AliTRDCalPID *AliTRDcalibDB::GetPIDObject(AliTRDpidUtil::ETRDPIDMethod method)
1815 {
1816   //
1817   // Returns the object storing the distributions for PID with likelihood
1818   //
1819
1820   switch(method) {
1821   case AliTRDpidUtil::kLQ: 
1822     return dynamic_cast<const AliTRDCalPID *>(GetCachedCDBObject(kIDPIDLQ));
1823   case AliTRDpidUtil::kNN: 
1824     return dynamic_cast<const AliTRDCalPID *>(GetCachedCDBObject(kIDPIDNN));
1825   case AliTRDpidUtil::kESD:
1826     return 0x0; // To avoid compiler warnings 
1827   }
1828
1829   return 0x0;
1830
1831 }
1832
1833 //_____________________________________________________________________________
1834 AliTRDPIDResponse *AliTRDcalibDB::GetPIDResponse(AliTRDPIDResponse::ETRDPIDMethod /*method*/)
1835 {
1836   //
1837   // Returns the PID response object for 1D-LQ
1838   //
1839
1840   if (!fPIDResponse) {
1841     AliDebug(2, "Setting new PID response.");
1842
1843     fPIDResponse = new AliTRDPIDResponse;
1844
1845     // Load Reference Histos from OCDB
1846 //    if(method == AliTRDPIDResponse::kLQ1D){
1847     //fPIDResponse->SetPIDmethod(AliTRDPIDResponse::kLQ1D);
1848     const TObjArray *references = dynamic_cast<const TObjArray *>(GetCachedCDBObject(kIDPIDLQ1D));
1849
1850     TIter refs(references);
1851     TObject *obj = NULL;
1852     AliTRDPIDReference *ref = NULL;
1853     Bool_t hasReference = kFALSE;
1854     while ((obj = refs())){
1855       if ((ref = dynamic_cast<AliTRDPIDReference *>(obj))){
1856         AliDebug(2, "Setting new PID response object.");
1857         TDirectory *bkpdir = gDirectory;
1858         gROOT->cd();
1859         AliTRDPIDResponseObject *ro = new AliTRDPIDResponseObject;
1860         ro->SetPIDReference(ref);
1861         fPIDResponse->SetPIDResponseObject(ro);
1862         hasReference = kTRUE;
1863         gDirectory = bkpdir;
1864         break;
1865       }
1866     }
1867
1868     if (!hasReference) {
1869       AliError("Reference histograms not found in the OCDB");
1870     }
1871   }
1872
1873 //  }
1874
1875   return fPIDResponse;
1876
1877 }
1878
1879 //_____________________________________________________________________________
1880 const AliTRDCalTrkAttach* AliTRDcalibDB::GetAttachObject()
1881 {
1882   //
1883   // Returns the object storing likelihood distributions for cluster to track attachment
1884   //
1885
1886   return dynamic_cast<const AliTRDCalTrkAttach*>(GetCachedCDBObject(kIDAttach));
1887
1888 }
1889
1890 //_____________________________________________________________________________
1891 const AliTRDCalMonitoring *AliTRDcalibDB::GetMonitoringObject()
1892 {
1893   //
1894   // Returns the object storing the monitoring data
1895   //
1896
1897   return dynamic_cast<const AliTRDCalMonitoring *> 
1898          (GetCachedCDBObject(kIDMonitoringData));
1899    
1900 }
1901
1902 //_____________________________________________________________________________
1903 void AliTRDcalibDB::SamplePRF()
1904 {
1905   //
1906   // Samples the pad response function (should maybe go somewhere else ...)
1907   //
1908
1909   const Int_t kPRFbin = 61;
1910
1911   Float_t prf[kNlayer][kPRFbin] = { 
1912                    {2.9037e-02, 3.3608e-02, 3.9020e-02, 4.5292e-02,
1913                     5.2694e-02, 6.1362e-02, 7.1461e-02, 8.3362e-02,
1914                     9.7063e-02, 1.1307e-01, 1.3140e-01, 1.5235e-01,
1915                     1.7623e-01, 2.0290e-01, 2.3294e-01, 2.6586e-01,
1916                     3.0177e-01, 3.4028e-01, 3.8077e-01, 4.2267e-01,
1917                     4.6493e-01, 5.0657e-01, 5.4655e-01, 5.8397e-01,
1918                     6.1767e-01, 6.4744e-01, 6.7212e-01, 6.9188e-01,
1919                     7.0627e-01, 7.1499e-01, 7.1851e-01, 7.1499e-01,
1920                     7.0627e-01, 6.9188e-01, 6.7212e-01, 6.4744e-01,
1921                     6.1767e-01, 5.8397e-01, 5.4655e-01, 5.0657e-01,
1922                     4.6493e-01, 4.2267e-01, 3.8077e-01, 3.4028e-01,
1923                     3.0177e-01, 2.6586e-01, 2.3294e-01, 2.0290e-01,
1924                     1.7623e-01, 1.5235e-01, 1.3140e-01, 1.1307e-01,
1925                     9.7063e-02, 8.3362e-02, 7.1461e-02, 6.1362e-02,
1926                     5.2694e-02, 4.5292e-02, 3.9020e-02, 3.3608e-02,
1927                     2.9037e-02},
1928                    {2.5478e-02, 2.9695e-02, 3.4655e-02, 4.0454e-02,
1929                     4.7342e-02, 5.5487e-02, 6.5038e-02, 7.6378e-02,
1930                     8.9696e-02, 1.0516e-01, 1.2327e-01, 1.4415e-01,
1931                     1.6794e-01, 1.9516e-01, 2.2573e-01, 2.5959e-01,
1932                     2.9694e-01, 3.3719e-01, 3.7978e-01, 4.2407e-01,
1933                     4.6889e-01, 5.1322e-01, 5.5569e-01, 5.9535e-01,
1934                     6.3141e-01, 6.6259e-01, 6.8882e-01, 7.0983e-01,
1935                     7.2471e-01, 7.3398e-01, 7.3761e-01, 7.3398e-01,
1936                     7.2471e-01, 7.0983e-01, 6.8882e-01, 6.6259e-01,
1937                     6.3141e-01, 5.9535e-01, 5.5569e-01, 5.1322e-01,
1938                     4.6889e-01, 4.2407e-01, 3.7978e-01, 3.3719e-01,
1939                     2.9694e-01, 2.5959e-01, 2.2573e-01, 1.9516e-01,
1940                     1.6794e-01, 1.4415e-01, 1.2327e-01, 1.0516e-01,
1941                     8.9696e-02, 7.6378e-02, 6.5038e-02, 5.5487e-02,
1942                     4.7342e-02, 4.0454e-02, 3.4655e-02, 2.9695e-02,
1943                     2.5478e-02},
1944                    {2.2363e-02, 2.6233e-02, 3.0782e-02, 3.6140e-02,
1945                     4.2535e-02, 5.0157e-02, 5.9197e-02, 6.9900e-02,
1946                     8.2707e-02, 9.7811e-02, 1.1548e-01, 1.3601e-01,
1947                     1.5998e-01, 1.8739e-01, 2.1840e-01, 2.5318e-01,
1948                     2.9182e-01, 3.3373e-01, 3.7837e-01, 4.2498e-01,
1949                     4.7235e-01, 5.1918e-01, 5.6426e-01, 6.0621e-01,
1950                     6.4399e-01, 6.7700e-01, 7.0472e-01, 7.2637e-01,
1951                     7.4206e-01, 7.5179e-01, 7.5551e-01, 7.5179e-01,
1952                     7.4206e-01, 7.2637e-01, 7.0472e-01, 6.7700e-01,
1953                     6.4399e-01, 6.0621e-01, 5.6426e-01, 5.1918e-01,
1954                     4.7235e-01, 4.2498e-01, 3.7837e-01, 3.3373e-01,
1955                     2.9182e-01, 2.5318e-01, 2.1840e-01, 1.8739e-01,
1956                     1.5998e-01, 1.3601e-01, 1.1548e-01, 9.7811e-02,
1957                     8.2707e-02, 6.9900e-02, 5.9197e-02, 5.0157e-02,
1958                     4.2535e-02, 3.6140e-02, 3.0782e-02, 2.6233e-02,
1959                     2.2363e-02},
1960                    {1.9635e-02, 2.3167e-02, 2.7343e-02, 3.2293e-02,
1961                     3.8224e-02, 4.5335e-02, 5.3849e-02, 6.4039e-02,
1962                     7.6210e-02, 9.0739e-02, 1.0805e-01, 1.2841e-01,
1963                     1.5216e-01, 1.7960e-01, 2.1099e-01, 2.4671e-01,
1964                     2.8647e-01, 3.2996e-01, 3.7660e-01, 4.2547e-01,
1965                     4.7536e-01, 5.2473e-01, 5.7215e-01, 6.1632e-01,
1966                     6.5616e-01, 6.9075e-01, 7.1939e-01, 7.4199e-01,
1967                     7.5838e-01, 7.6848e-01, 7.7227e-01, 7.6848e-01,
1968                     7.5838e-01, 7.4199e-01, 7.1939e-01, 6.9075e-01,
1969                     6.5616e-01, 6.1632e-01, 5.7215e-01, 5.2473e-01,
1970                     4.7536e-01, 4.2547e-01, 3.7660e-01, 3.2996e-01,
1971                     2.8647e-01, 2.4671e-01, 2.1099e-01, 1.7960e-01,
1972                     1.5216e-01, 1.2841e-01, 1.0805e-01, 9.0739e-02,
1973                     7.6210e-02, 6.4039e-02, 5.3849e-02, 4.5335e-02,
1974                     3.8224e-02, 3.2293e-02, 2.7343e-02, 2.3167e-02,
1975                     1.9635e-02},
1976                    {1.7224e-02, 2.0450e-02, 2.4286e-02, 2.8860e-02,
1977                     3.4357e-02, 4.0979e-02, 4.8966e-02, 5.8612e-02,
1978                     7.0253e-02, 8.4257e-02, 1.0102e-01, 1.2094e-01,
1979                     1.4442e-01, 1.7196e-01, 2.0381e-01, 2.4013e-01,
1980                     2.8093e-01, 3.2594e-01, 3.7450e-01, 4.2563e-01,
1981                     4.7796e-01, 5.2991e-01, 5.7974e-01, 6.2599e-01,
1982                     6.6750e-01, 7.0344e-01, 7.3329e-01, 7.5676e-01,
1983                     7.7371e-01, 7.8410e-01, 7.8793e-01, 7.8410e-01,
1984                     7.7371e-01, 7.5676e-01, 7.3329e-01, 7.0344e-01,
1985                     6.6750e-01, 6.2599e-01, 5.7974e-01, 5.2991e-01,
1986                     4.7796e-01, 4.2563e-01, 3.7450e-01, 3.2594e-01,
1987                     2.8093e-01, 2.4013e-01, 2.0381e-01, 1.7196e-01,
1988                     1.4442e-01, 1.2094e-01, 1.0102e-01, 8.4257e-02,
1989                     7.0253e-02, 5.8612e-02, 4.8966e-02, 4.0979e-02,
1990                     3.4357e-02, 2.8860e-02, 2.4286e-02, 2.0450e-02,
1991                     1.7224e-02},
1992                    {1.5096e-02, 1.8041e-02, 2.1566e-02, 2.5793e-02,
1993                     3.0886e-02, 3.7044e-02, 4.4515e-02, 5.3604e-02,
1994                     6.4668e-02, 7.8109e-02, 9.4364e-02, 1.1389e-01,
1995                     1.3716e-01, 1.6461e-01, 1.9663e-01, 2.3350e-01,
1996                     2.7527e-01, 3.2170e-01, 3.7214e-01, 4.2549e-01,
1997                     4.8024e-01, 5.3460e-01, 5.8677e-01, 6.3512e-01,
1998                     6.7838e-01, 7.1569e-01, 7.4655e-01, 7.7071e-01,
1999                     7.8810e-01, 7.9871e-01, 8.0255e-01, 7.9871e-01,
2000                     7.8810e-01, 7.7071e-01, 7.4655e-01, 7.1569e-01,
2001                     6.7838e-01, 6.3512e-01, 5.8677e-01, 5.3460e-01,
2002                     4.8024e-01, 4.2549e-01, 3.7214e-01, 3.2170e-01,
2003                     2.7527e-01, 2.3350e-01, 1.9663e-01, 1.6461e-01,
2004                     1.3716e-01, 1.1389e-01, 9.4364e-02, 7.8109e-02,
2005                     6.4668e-02, 5.3604e-02, 4.4515e-02, 3.7044e-02,
2006                     3.0886e-02, 2.5793e-02, 2.1566e-02, 1.8041e-02,
2007                     1.5096e-02}};
2008
2009   // More sampling precision with linear interpolation
2010   fPRFlo  = -1.5;
2011   fPRFhi  =  1.5;
2012   Float_t pad[kPRFbin];
2013   Int_t   sPRFbin = kPRFbin;  
2014   Float_t sPRFwid = (fPRFhi - fPRFlo) / ((Float_t) sPRFbin);
2015   for (Int_t iPad = 0; iPad < sPRFbin; iPad++) {
2016     pad[iPad] = ((Float_t) iPad + 0.5) * sPRFwid + fPRFlo;
2017   }
2018   fPRFbin = 500;  
2019   fPRFwid = (fPRFhi - fPRFlo) / ((Float_t) fPRFbin);
2020   fPRFpad = ((Int_t) (1.0 / fPRFwid));
2021
2022   if (fPRFsmp) delete [] fPRFsmp;
2023   fPRFsmp = new Float_t[kNlayer*fPRFbin];
2024
2025   Int_t   ipos1;
2026   Int_t   ipos2;
2027   Float_t diff;
2028
2029   for (Int_t iLayer = 0; iLayer < kNlayer; iLayer++) {
2030
2031     for (Int_t iBin = 0; iBin < fPRFbin; iBin++) {
2032
2033       Float_t bin = (((Float_t) iBin) + 0.5) * fPRFwid + fPRFlo;
2034       ipos1 = ipos2 = 0;
2035       diff  = 0;
2036       do {
2037         diff = bin - pad[ipos2++];
2038       } while ((diff > 0) && (ipos2 < kPRFbin));
2039       if      (ipos2 == kPRFbin) {
2040         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2-1];
2041       }
2042       else if (ipos2 == 1) {
2043         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2-1];
2044       }
2045       else {
2046         ipos2--;
2047         if (ipos2 >= kPRFbin) ipos2 = kPRFbin - 1;
2048         ipos1 = ipos2 - 1;
2049         fPRFsmp[iLayer*fPRFbin+iBin] = prf[iLayer][ipos2] 
2050                                      + diff * (prf[iLayer][ipos2] - prf[iLayer][ipos1]) 
2051                                      / sPRFwid;
2052       }
2053
2054     }
2055   } 
2056
2057 }
2058
2059 //_____________________________________________________________________________
2060 Int_t AliTRDcalibDB::PadResponse(Double_t signal, Double_t dist
2061                                , Int_t layer, Double_t *pad) const
2062 {
2063   //
2064   // Applies the pad response
2065   // So far this is the fixed parametrization and should be replaced by
2066   // something dependent on calibration values
2067   //
2068
2069   Int_t iBin  = ((Int_t) ((-dist - fPRFlo) / fPRFwid));
2070   Int_t iOff  = layer * fPRFbin;
2071
2072   Int_t iBin0 = iBin - fPRFpad + iOff;
2073   Int_t iBin1 = iBin           + iOff;
2074   Int_t iBin2 = iBin + fPRFpad + iOff;
2075
2076   pad[0] = 0.0;
2077   pad[1] = 0.0;
2078   pad[2] = 0.0;
2079   if ((iBin1 >= 0) && (iBin1 < (fPRFbin*kNlayer))) {
2080
2081     if (iBin0 >= 0) {
2082       pad[0] = signal * fPRFsmp[iBin0];
2083     }
2084     pad[1] = signal * fPRFsmp[iBin1];
2085     if (iBin2 < (fPRFbin*kNlayer)) {
2086       pad[2] = signal * fPRFsmp[iBin2];
2087     }
2088
2089     return 1;
2090
2091   }
2092   else {
2093
2094     return 0;
2095
2096   }
2097
2098 }
2099
2100
2101 AliTRDtrapConfig* AliTRDcalibDB::GetTrapConfig()
2102 {
2103   // return an existing TRAPconfig or load it from the OCDB
2104   // in case of failure, a default TRAPconfig is created
2105
2106   if (fTrapConfig)
2107     return fTrapConfig;
2108   else {
2109     if ((fTrapConfigName.Length() <= 0) || (fTrapConfigVersion.Length() <= 0)) {
2110       // query the configuration to be used
2111       this->GetGlobalConfiguration(fTrapConfigName);
2112       this->GetGlobalConfigurationVersion(fTrapConfigVersion);
2113     }
2114
2115     // try to load the requested configuration
2116     this->LoadTrapConfig(fTrapConfigName, fTrapConfigVersion);
2117
2118     // if we still don't have a valid TRAPconfig, create a default one
2119     if (!fTrapConfig) {
2120       AliWarning("Falling back to default configuration");
2121       static AliTRDtrapConfig trapConfigDefault("default", "default TRAP configuration");
2122       fTrapConfig = &trapConfigDefault;
2123       AliTRDtrapConfigHandler cfgHandler(fTrapConfig);
2124       cfgHandler.Init();
2125       cfgHandler.LoadConfig();
2126     }
2127
2128     AliInfo(Form("using TRAPconfig \"%s\"", fTrapConfig->GetTitle()));
2129
2130     // we still have to load the gain tables
2131     // if the gain filter is active
2132     if (HasOnlineFilterGain()) {
2133       const Int_t nDets = AliTRDgeometry::Ndet();
2134       const Int_t nMcms = AliTRDgeometry::MCMmax();
2135       const Int_t nChs  = AliTRDgeometry::ADCmax();
2136
2137       // gain factors are per MCM
2138       // allocate the registers accordingly
2139       for (Int_t ch = 0; ch < nChs; ++ch) {
2140         AliTRDtrapConfig::TrapReg_t regFGAN = (AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kFGA0 + ch);
2141         AliTRDtrapConfig::TrapReg_t regFGFN = (AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kFGF0 + ch);
2142         fTrapConfig->SetTrapRegAlloc(regFGAN, AliTRDtrapConfig::kAllocByMCM);
2143         fTrapConfig->SetTrapRegAlloc(regFGFN, AliTRDtrapConfig::kAllocByMCM);
2144       }
2145
2146       for (Int_t iDet = 0; iDet < nDets; ++iDet) {
2147         AliTRDCalOnlineGainTableROC *gainTbl = GetOnlineGainTableROC(iDet);
2148         if (gainTbl) {
2149           const Int_t nRobs = AliTRDgeometry::GetStack(iDet) == 2 ?
2150             AliTRDgeometry::ROBmaxC0() : AliTRDgeometry::ROBmaxC1();
2151           for (Int_t rob = 0; rob < nRobs; ++rob) {
2152             for (Int_t mcm = 0; mcm < nMcms; ++mcm) {
2153               AliTRDCalOnlineGainTableMCM *gainTblMCM = gainTbl->GetGainTableMCM(rob, mcm);
2154
2155               // set ADC reference voltage
2156               fTrapConfig->SetTrapReg(AliTRDtrapConfig::kADCDAC, gainTblMCM->GetAdcdac(), iDet, rob, mcm);
2157
2158               // set constants channel-wise
2159               for (Int_t ch = 0; ch < nChs; ++ch) {
2160                 AliTRDtrapConfig::TrapReg_t regFGAN = (AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kFGA0 + ch);
2161                 AliTRDtrapConfig::TrapReg_t regFGFN = (AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kFGF0 + ch);
2162                 fTrapConfig->SetTrapReg(regFGAN, gainTblMCM->GetFGAN(ch), iDet, rob, mcm);
2163                 fTrapConfig->SetTrapReg(regFGFN, gainTblMCM->GetFGFN(ch), iDet, rob, mcm);
2164               }
2165             }
2166           }
2167         }
2168       }
2169     }
2170
2171     return fTrapConfig;
2172   }
2173 }
2174
2175
2176 AliTRDtrapConfig* AliTRDcalibDB::LoadTrapConfig(const TString &name, const TString &version)
2177 {
2178   // try to load the specified configuration from the OCDB
2179   // if it fails, or it does not exist, return null
2180
2181   AliInfo(Form("looking for TRAPconfig \"%s.%s\"", name.Data(), version.Data()));
2182
2183   const AliTRDCalTrapConfig *caltrap = dynamic_cast<const AliTRDCalTrapConfig*> (GetCachedCDBObject(kIDTrapConfig));
2184
2185   if (caltrap) {
2186     TString configName(name);
2187     configName.Append(".");
2188     configName.Append(version);
2189     fTrapConfig = caltrap->Get(configName);
2190   }
2191   else {
2192     fTrapConfig = 0x0;
2193     AliError("No TRAPconfig entry found!");
2194   }
2195
2196   return fTrapConfig;
2197 }