]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDcalibDB.h
First implementation of calibration scheme by Jan Fiete
[u/mrichter/AliRoot.git] / TRD / AliTRDcalibDB.h
1 #ifndef ALITRDCALIBDB_H
2 #define ALITRDCALIBDB_H
3 /* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  * See cxx source for full Copyright notice                               */
5
6 ///////////////////////////////////////////////////////////////////////////////
7 //                                                                           //
8 // Class providing the calibration parameters by accessing the CDB           //
9 //                                                                           //
10 ///////////////////////////////////////////////////////////////////////////////
11
12 /* $Id$ */
13
14 #include <iostream>
15 #include "TObject.h"
16
17 #include "AliLog.h"
18 #include "AliTRDgeometry.h"
19
20 #include <AliCDBStorage.h>
21 #include <AliCDBEntry.h>
22
23 //includes neccessary here for compiliation of dynamic_cast
24 #include "AliTRDCalPad.h"
25 #include "AliTRDCalDet.h"
26
27 class AliTRDCalChamber;
28 class AliTRDCalStack;
29 class AliTRDCalGlobals;
30
31   // defines call to function by providing plane, chamber, sector instead of detector
32 #define HEADER_PAD(funcname) \
33   Float_t funcname(Int_t plane, Int_t chamber, Int_t sector, Int_t col, Int_t row) \
34   { return funcname(AliTRDgeometry::GetDetector(plane, chamber, sector), col, row); }
35
36   // defines call to function by providing plane, chamber, sector instead of detector
37 #define HEADER_CHAMBER(funcname) \
38   Bool_t funcname(Int_t plane, Int_t chamber, Int_t sector, Float_t* xyz) \
39   { return funcname(AliTRDgeometry::GetDetector(plane, chamber, sector), xyz); }
40
41   // This macro creates a piece of code in the function GetCDBObject which retrieves the object with path <cdbPath> from
42   // the CDB when GetCDBObject(cdbEntryId) is called.
43   /* EXAMPLE: When called e.g. CACHE_CDB_OBJECT(kIDVdrift, "TRD/Calib/LocalVdrift") it produces the following piece of code:
44       case kIDVdrift : { \
45         if (!fCDBCache[kIDVdrift]) \
46         { \
47           fCDBEntries[kIDVdrift] = GetCDBEntry(TRD/Calib/LocalVdrift");  \
48           if (fCDBEntries[kIDVdrift]) \
49             fCDBCache[kIDVdrift] = fCDBEntries[kIDVdrift]->GetObject(); \
50         } \
51         return fCDBCache[kIDVdrift]; \
52       } \
53       break;
54    */
55 #define CACHE_CDB_OBJECT(cdbEntryId, cdbPath) \
56       case cdbEntryId : { \
57         if (!fCDBCache[cdbEntryId]) \
58         { \
59           fCDBEntries[cdbEntryId] = GetCDBEntry(cdbPath);  \
60           if (fCDBEntries[cdbEntryId]) \
61             fCDBCache[cdbEntryId] = fCDBEntries[cdbEntryId]->GetObject(); \
62         } \
63         return fCDBCache[cdbEntryId]; \
64       } \
65       break;  
66
67 // The following macro retrieves and caches an object from the CDB. The macro is specialized for parameters which are stored
68 // as local variation at pad level of a global variable defined per detector chamber. It uses the classes AliTRDCalPad and AliTRDCalDet.
69 // Before storing the object it retrieves the local variations (cdbPath) and the global variable (cdbMergePath) and merges them using
70 // the AliTRDCalPad::ScaleROCs.
71 #define CACHE_MERGE_CDB_OBJECT(cdbEntryId, cdbPath, cdbMergePath) \
72       case cdbEntryId : { \
73         if (!fCDBCache[cdbEntryId]) { \
74           AliTRDCalDet* detObject = 0; \
75           AliTRDCalPad* padObject = 0; \
76           \
77           fCDBEntries[cdbEntryId] = GetCDBEntry(cdbPath); \
78           if (fCDBEntries[cdbEntryId]) \
79             padObject = dynamic_cast<AliTRDCalPad*>(fCDBEntries[cdbEntryId]->GetObject()); \
80           \
81           AliCDBEntry* mergeEntry = GetCDBEntry(cdbMergePath); \
82           if (mergeEntry) \
83             detObject = dynamic_cast<AliTRDCalDet*>(mergeEntry->GetObject()); \
84           if (!padObject || !detObject) { \
85             if (fCDBEntries[cdbEntryId]) { \
86               delete fCDBEntries[cdbEntryId]; \
87               fCDBEntries[cdbEntryId] = 0; \
88             } \
89             if (mergeEntry) \
90               delete mergeEntry; \
91             return 0; \
92           } \
93           padObject->ScaleROCs(detObject); \
94           delete mergeEntry; \
95           fCDBCache[cdbEntryId] = padObject; \
96         } \
97         return fCDBCache[cdbEntryId]; \
98       } \
99       break;  
100       
101 class AliTRDcalibDB : public TObject
102 {
103 public:
104   static AliTRDcalibDB* Instance();
105   static void Terminate();
106
107   void SetRun(Long64_t run);
108   
109   Bool_t GetChamberPos(Int_t det, Float_t* xyz);
110   HEADER_CHAMBER(GetChamberPos);
111   
112   Bool_t GetChamberRot(Int_t det, Float_t* xyz);
113   HEADER_CHAMBER(GetChamberRot);
114   
115   Bool_t GetStackPos(Int_t chamber, Int_t sector, Float_t* xyz);
116   Bool_t GetStackRot(Int_t chamber, Int_t sector, Float_t* xyz);
117    
118   Float_t GetVdrift(Int_t det, Int_t col, Int_t row);
119   HEADER_PAD(GetVdrift);
120     
121   Float_t GetT0(Int_t det, Int_t col, Int_t row);
122   HEADER_PAD(GetT0);
123   
124   Float_t GetGainFactor(Int_t det, Int_t col, Int_t row);
125   HEADER_PAD(GetGainFactor);
126
127   Float_t GetSamplingFrequency(); 
128   Int_t GetNumberOfTimeBins();
129   
130   //Related functions, these depend on calibration data
131   virtual Float_t GetOmegaTau(Float_t vdrift);
132   
133 protected:
134   void Invalidate();
135   
136   static AliTRDcalibDB* fgInstance;     // Instance of this class (singleton implementation)
137   static Bool_t fgTerminated;               // Defines if this class has already been terminated and therefore does not return instances in GetInstance anymore
138
139   AliCDBStorage* fLocator;                  // Storage locator retrieved from AliCDBManager
140   
141   enum { kCDBCacheSize = 7 };   // Number of cached objects
142   enum { kIDVdrift = 0, kIDT0 = 1, kIDGainFactor = 2, kIDPRFWidth = 3, kIDGlobals = 4, kIDChamber = 5, kIDStack = 6 };    // IDs of cached objects
143   
144   AliCDBEntry* fCDBEntries[kCDBCacheSize];    // Cache for CDB entries 
145   TObject* fCDBCache[kCDBCacheSize];          // Cache for calibration objects.
146       
147   AliCDBEntry* GetCDBEntry(const char* cdbPath)
148   {
149     // 
150     // Retrieves an entry with path <cdbPath> from the CDB.
151     //
152     
153     if (fRun < 0)
154     {
155       AliFatal("AliTRDcalibDB: Run number not set! Use AliTRDcalibDB::SetRun.");
156       //std::cerr << "AliTRDcalibDB: Run number not set! Use AliTRDcalibDB::SetRun." << std::endl;
157       return 0;
158     }
159     if (!fLocator) 
160     { 
161       std::cerr << "AliTRDcalibDB: Storage Locator not available." << std::endl; 
162       return 0; 
163     } 
164     AliCDBEntry* entry = fLocator->Get(cdbPath, fRun); 
165     if (!entry) 
166     { 
167       std::cerr << "AliTRDcalibDB: Failed to get entry: " << cdbPath << std::endl; 
168       return 0; 
169     }
170     std::cout << "AliTRDcalibDB: Retrieved object: " << cdbPath << std::endl;
171     return entry;
172   }
173   
174   TObject* GetCachedCDBObject(Int_t id)
175   {
176     //
177     // Retrieves a cdb object with the given id. The objects are cached as long as the run number is not changed.
178     //
179     // Put together the available objects by using the macros CACHE_CDB_OBJECT and CACHE_MERGE_CDB_OBJECT.
180     // The maximal number of cached objects is given above in kCDBCacheSize, the ids of the objects in the enum below kCDBCacheSize.
181     // CACHE_CDB_OBJECT can be used for usual calibration objects.
182     // CACHE_MERGE_CDB_OBJECT for calibration data which depends on two objects: One containing a value per detector and one the local
183     //   fluctuations per pad.
184     //
185     
186     switch (id)
187     {
188       // parameters defined per pad
189       CACHE_MERGE_CDB_OBJECT(kIDVdrift, "TRD/Calib/LocalVdrift", "TRD/Calib/ChamberVdrift");
190       CACHE_MERGE_CDB_OBJECT(kIDT0, "TRD/Calib/LocalT0", "TRD/Calib/ChamberT0");
191       CACHE_CDB_OBJECT(kIDGainFactor, "TRD/Calib/GainFactor");
192       CACHE_CDB_OBJECT(kIDPRFWidth, "TRD/Calib/PRFWidth");
193     
194       // global parameters
195       CACHE_CDB_OBJECT(kIDGlobals, "TRD/Calib/Globals");
196       CACHE_CDB_OBJECT(kIDChamber, "TRD/Calib/Chamber");
197       CACHE_CDB_OBJECT(kIDStack, "TRD/Calib/Stack");
198     }
199     return 0;
200   }
201   
202   Long64_t fRun;
203   
204 private:
205   // this is a singleton, constructor is private!  
206   AliTRDcalibDB();
207   virtual ~AliTRDcalibDB();
208
209   ClassDef(AliTRDcalibDB, 0)
210 };
211
212 #undef HEADER_PAD
213 #undef HEADER_CHAMBER
214 #undef CACHE_CDB_OBJECT
215 #undef CACHE_MERGE_CDB_OBJECT
216
217 #endif