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