]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDcalibDB.cxx
First implementation of calibration scheme by Jan Fiete
[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 ///////////////////////////////////////////////////////////////////////////////
27
28 #include <TRandom.h>
29
30 #include <AliCDBManager.h>
31
32 #include "AliTRDcalibDB.h"
33 #include "AliTRDgeometry.h"
34 #include "AliTRDpadPlane.h"
35 #include "AliTRDCommonParam.h"
36
37 #include "AliTRDCalROC.h"
38 #include "AliTRDCalChamber.h"
39 #include "AliTRDCalStack.h"
40 #include "AliTRDCalPad.h"
41 #include "AliTRDCalDet.h"
42 #include "AliTRDCalGlobals.h"
43
44 ClassImp(AliTRDcalibDB)
45
46 AliTRDcalibDB* AliTRDcalibDB::fgInstance = 0;
47 Bool_t AliTRDcalibDB::fgTerminated = kFALSE;
48
49 //_ singleton implementation __________________________________________________
50 AliTRDcalibDB* AliTRDcalibDB::Instance()
51 {
52   //
53   // Singleton implementation
54   // Returns an instance of this class, it is created if neccessary
55   // 
56   
57   if (fgTerminated != kFALSE)
58     return 0;
59
60   if (fgInstance == 0)
61     fgInstance = new AliTRDcalibDB();
62   
63   return fgInstance;
64 }
65
66 void AliTRDcalibDB::Terminate()
67 {
68   //
69   // Singleton implementation
70   // Deletes the instance of this class and sets the terminated flag, instances cannot be requested anymore
71   // This function can be called several times.
72   //
73   
74   fgTerminated = kTRUE;
75   
76   if (fgInstance != 0)
77   {
78     delete fgInstance;
79     fgInstance = 0;
80   }
81 }
82
83 //_____________________________________________________________________________
84 AliTRDcalibDB::AliTRDcalibDB()
85 {
86   //
87   // constructor
88   //
89   
90   fLocator = 0;
91   // TODO Default runnumber is set to 0, this should be changed later to an invalid value (e.g. -1) to prevent
92   // TODO invalid calibration data to be used.
93   fRun = 0;
94   
95   AliCDBManager* manager = AliCDBManager::Instance();
96   if (!manager)
97   {
98     std::cout << "Failed to get instance of AliCDBManager." << std::endl;
99     return;
100   }
101   
102   fLocator = manager->GetStorage("local://$ALICE_ROOT");
103   
104   for (Int_t i=0; i<kCDBCacheSize; ++i)
105   {
106     fCDBCache[i] = 0;
107     fCDBEntries[i] = 0;
108   }
109 };
110
111 //_____________________________________________________________________________
112 AliTRDcalibDB::~AliTRDcalibDB() 
113 {
114   //
115   // destructor
116   //
117   
118   Invalidate();
119 };
120
121 //_____________________________________________________________________________
122 void AliTRDcalibDB::SetRun(Long64_t run)
123 {
124   //
125   // Sets current run number. Calibration data is read from the corresponding file. 
126   // When the run number changes the caching is invalidated.
127   //
128   
129   if (fRun == run)
130     return;
131   
132   fRun = run;
133   Invalidate();
134 }
135   
136 //_____________________________________________________________________________
137 void AliTRDcalibDB::Invalidate()
138 {
139   //
140   // Invalidates cache (when run number is changed).
141   //
142   
143   for (Int_t i=0; i<kCDBCacheSize; ++i)
144   {
145     if (fCDBEntries[i])
146     {
147       if (fCDBEntries[i]->IsOwner() != kFALSE && fCDBCache[i])
148         delete fCDBCache[i];
149       delete fCDBEntries[i];
150       fCDBEntries[i] = 0;
151       fCDBCache[i] = 0;
152     }
153   }
154 }
155
156 //_____________________________________________________________________________
157 Bool_t AliTRDcalibDB::GetChamberPos(Int_t det, Float_t* xyz)
158 {
159   //
160   // Returns the deviation of the chamber position from the nominal position.
161   //
162   
163   AliTRDCalChamber* chamber = dynamic_cast<AliTRDCalChamber*>(GetCachedCDBObject(kIDChamber));
164   if (!chamber)
165     return kFALSE;
166   
167   const Float_t* kvalues = chamber->GetChamberPos(det);
168   if (!kvalues)
169     return kFALSE;
170   
171   xyz[0] = kvalues[0];
172   xyz[1] = kvalues[1];
173   xyz[2] = kvalues[2];
174   
175   return kTRUE;
176 }
177
178 //_____________________________________________________________________________
179 Bool_t AliTRDcalibDB::GetChamberRot(Int_t det, Float_t* xyz)
180 {
181   //
182   // Returns the rotation of the chamber from the nominal position.
183   //
184   
185   AliTRDCalChamber* chamber = dynamic_cast<AliTRDCalChamber*>(GetCachedCDBObject(kIDChamber));
186   if (!chamber)
187     return kFALSE;
188   
189   const Float_t* kvalues = chamber->GetChamberRot(det);
190   if (!kvalues)
191     return kFALSE;
192   
193   xyz[0] = kvalues[0];
194   xyz[1] = kvalues[1];
195   xyz[2] = kvalues[2];
196   
197   return kTRUE;
198 }
199
200 //_____________________________________________________________________________
201 Bool_t AliTRDcalibDB::GetStackPos(Int_t chamber, Int_t sector, Float_t* xyz)
202 {
203   //
204   // Returns the deviation of the stack position from the nominal position.
205   //
206   
207   AliTRDCalStack* stack = dynamic_cast<AliTRDCalStack*>(GetCachedCDBObject(kIDStack));
208   if (!stack)
209     return kFALSE;
210   
211   const Float_t* kvalues = stack->GetStackPos(chamber, sector);
212   if (!kvalues)
213     return kFALSE;
214   
215   xyz[0] = kvalues[0];
216   xyz[1] = kvalues[1];
217   xyz[2] = kvalues[2];
218   
219   return kTRUE;
220 }
221
222 //_____________________________________________________________________________
223 Bool_t AliTRDcalibDB::GetStackRot(Int_t chamber, Int_t sector, Float_t* xyz)
224 {
225   //
226   // Returns the rotation of the stack from the nominal position.
227   //
228   
229   AliTRDCalStack* stack = dynamic_cast<AliTRDCalStack*>(GetCachedCDBObject(kIDStack));
230   if (!stack)
231     return kFALSE;
232   
233   const Float_t* kvalues = stack->GetStackRot(chamber, sector);
234   if (!kvalues)
235     return kFALSE;
236   
237   xyz[0] = kvalues[0];
238   xyz[1] = kvalues[1];
239   xyz[2] = kvalues[2];
240   
241   return kTRUE;
242 }
243
244 //_____________________________________________________________________________
245 Float_t AliTRDcalibDB::GetVdrift(Int_t det, Int_t col, Int_t row)
246 {
247   //
248   // Returns the drift velocity for the given pad.
249   //
250   
251   AliTRDCalPad* calPad = dynamic_cast<AliTRDCalPad*> (GetCachedCDBObject(kIDVdrift));
252   if (!calPad)
253     return -1;
254
255   AliTRDCalROC* roc = calPad->GetCalROC(det);
256   if (!roc)
257     return -1;
258
259   return roc->GetValue(col, row);
260 };
261
262 //_____________________________________________________________________________
263 Float_t AliTRDcalibDB::GetT0(Int_t det, Int_t col, Int_t row)
264 {
265   //
266   // Returns t0 for the given pad.
267   //
268   
269   AliTRDCalPad* calPad = dynamic_cast<AliTRDCalPad*> (GetCachedCDBObject(kIDT0));
270   if (!calPad)
271     return -1;
272
273   AliTRDCalROC* roc = calPad->GetCalROC(det);
274   if (!roc)
275     return -1;
276
277   return roc->GetValue(col, row);
278 };
279
280 //_____________________________________________________________________________
281 Float_t AliTRDcalibDB::GetGainFactor(Int_t det, Int_t col, Int_t row)
282 {
283   //
284   // Returns the gain factor for the given pad.
285   //
286   
287   AliTRDCalPad* calPad = dynamic_cast<AliTRDCalPad*> (GetCachedCDBObject(kIDGainFactor));
288   if (!calPad)
289     return -1;
290
291   AliTRDCalROC* roc = calPad->GetCalROC(det);
292   if (!roc)
293     return -1;
294
295   return roc->GetValue(col, row);
296 };
297
298 //_____________________________________________________________________________
299 Float_t AliTRDcalibDB::GetSamplingFrequency()
300 {
301   //
302   // Returns the sampling frequency of the TRD read-out.
303   //
304   
305   AliTRDCalGlobals* calGlobal = dynamic_cast<AliTRDCalGlobals*> (GetCachedCDBObject(kIDGlobals));
306   if (!calGlobal)
307     return -1;  
308   
309   return calGlobal->GetSamplingFrequency();
310 }
311   
312 //_____________________________________________________________________________
313 Int_t AliTRDcalibDB::GetNumberOfTimeBins()
314 {
315   //
316   // Returns the number of time bins which are read-out.
317   //
318   
319   AliTRDCalGlobals* calGlobal = dynamic_cast<AliTRDCalGlobals*> (GetCachedCDBObject(kIDGlobals));
320   if (!calGlobal)
321     return -1;  
322   
323   return calGlobal->GetNumberOfTimeBins();
324 }
325
326 //_____________________________________________________________________________
327 Float_t AliTRDcalibDB::GetOmegaTau(Float_t vdrift)
328 {
329   //
330   // Returns omega*tau (tan(Lorentz-angle)) for a given drift velocity <vd> 
331   // and a B-field <b> for Xe/CO2 (15%).
332   // The values are according to a GARFIELD simulation.
333   //
334   // This function basically does not belong to the calibration class. It should be moved somewhere else. 
335   // However, currently it is in use by simulation and reconstruction.
336   //
337   
338   AliTRDCommonParam* commonParam = AliTRDCommonParam::Instance();
339   if (!commonParam)
340     return -1;
341   Float_t field = commonParam->GetField();
342
343   const Int_t kNb = 5;
344   Float_t p0[kNb] = {  0.004810,  0.007412,  0.010252,  0.013409,  0.016888 };
345   Float_t p1[kNb] = {  0.054875,  0.081534,  0.107333,  0.131983,  0.155455 };
346   Float_t p2[kNb] = { -0.008682, -0.012896, -0.016987, -0.020880, -0.024623 };
347   Float_t p3[kNb] = {  0.000155,  0.000238,  0.000330,  0.000428,  0.000541 };
348
349   Int_t ib = ((Int_t) (10 * (field - 0.15)));
350   ib       = TMath::Max(  0,ib);
351   ib       = TMath::Min(kNb,ib);
352
353   Float_t alphaL = p0[ib] 
354       + p1[ib] * vdrift
355       + p2[ib] * vdrift*vdrift
356       + p3[ib] * vdrift*vdrift*vdrift;
357
358   return TMath::Tan(alphaL);
359 }
360