]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDcalibDB.cxx
Update 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       
150       delete fCDBEntries[i];
151       fCDBEntries[i] = 0;
152       fCDBCache[i] = 0;
153     }
154   }
155 }
156
157 //_____________________________________________________________________________
158 Bool_t AliTRDcalibDB::GetChamberPos(Int_t det, Float_t* xyz)
159 {
160   //
161   // Returns the deviation of the chamber position from the nominal position.
162   //
163   
164   AliTRDCalChamber* chamber = dynamic_cast<AliTRDCalChamber*>(GetCachedCDBObject(kIDChamber));
165   if (!chamber)
166     return kFALSE;
167   
168   const Float_t* kvalues = chamber->GetChamberPos(det);
169   if (!kvalues)
170     return kFALSE;
171   
172   xyz[0] = kvalues[0];
173   xyz[1] = kvalues[1];
174   xyz[2] = kvalues[2];
175   
176   return kTRUE;
177 }
178
179 //_____________________________________________________________________________
180 Bool_t AliTRDcalibDB::GetChamberRot(Int_t det, Float_t* xyz)
181 {
182   //
183   // Returns the rotation of the chamber from the nominal position.
184   //
185   
186   AliTRDCalChamber* chamber = dynamic_cast<AliTRDCalChamber*>(GetCachedCDBObject(kIDChamber));
187   if (!chamber)
188     return kFALSE;
189   
190   const Float_t* kvalues = chamber->GetChamberRot(det);
191   if (!kvalues)
192     return kFALSE;
193   
194   xyz[0] = kvalues[0];
195   xyz[1] = kvalues[1];
196   xyz[2] = kvalues[2];
197   
198   return kTRUE;
199 }
200
201 //_____________________________________________________________________________
202 Bool_t AliTRDcalibDB::GetStackPos(Int_t chamber, Int_t sector, Float_t* xyz)
203 {
204   //
205   // Returns the deviation of the stack position from the nominal position.
206   //
207   
208   AliTRDCalStack* stack = dynamic_cast<AliTRDCalStack*>(GetCachedCDBObject(kIDStack));
209   if (!stack)
210     return kFALSE;
211   
212   const Float_t* kvalues = stack->GetStackPos(chamber, sector);
213   if (!kvalues)
214     return kFALSE;
215   
216   xyz[0] = kvalues[0];
217   xyz[1] = kvalues[1];
218   xyz[2] = kvalues[2];
219   
220   return kTRUE;
221 }
222
223 //_____________________________________________________________________________
224 Bool_t AliTRDcalibDB::GetStackRot(Int_t chamber, Int_t sector, Float_t* xyz)
225 {
226   //
227   // Returns the rotation of the stack from the nominal position.
228   //
229   
230   AliTRDCalStack* stack = dynamic_cast<AliTRDCalStack*>(GetCachedCDBObject(kIDStack));
231   if (!stack)
232     return kFALSE;
233   
234   const Float_t* kvalues = stack->GetStackRot(chamber, sector);
235   if (!kvalues)
236     return kFALSE;
237   
238   xyz[0] = kvalues[0];
239   xyz[1] = kvalues[1];
240   xyz[2] = kvalues[2];
241   
242   return kTRUE;
243 }
244
245 //_____________________________________________________________________________
246 Float_t AliTRDcalibDB::GetVdrift(Int_t det, Int_t col, Int_t row)
247 {
248   //
249   // Returns the drift velocity for the given pad.
250   //
251   
252   AliTRDCalPad* calPad = dynamic_cast<AliTRDCalPad*> (GetCachedCDBObject(kIDVdrift));
253   if (!calPad)
254     return -1;
255
256   AliTRDCalROC* roc = calPad->GetCalROC(det);
257   if (!roc)
258     return -1;
259
260   return roc->GetValue(col, row);
261 };
262
263 //_____________________________________________________________________________
264 Float_t AliTRDcalibDB::GetT0(Int_t det, Int_t col, Int_t row)
265 {
266   //
267   // Returns t0 for the given pad.
268   //
269   
270   AliTRDCalPad* calPad = dynamic_cast<AliTRDCalPad*> (GetCachedCDBObject(kIDT0));
271   if (!calPad)
272     return -1;
273
274   AliTRDCalROC* roc = calPad->GetCalROC(det);
275   if (!roc)
276     return -1;
277
278   return roc->GetValue(col, row);
279 };
280
281 //_____________________________________________________________________________
282 Float_t AliTRDcalibDB::GetGainFactor(Int_t det, Int_t col, Int_t row)
283 {
284   //
285   // Returns the gain factor for the given pad.
286   //
287   
288   AliTRDCalPad* calPad = dynamic_cast<AliTRDCalPad*> (GetCachedCDBObject(kIDGainFactor));
289   if (!calPad)
290     return -1;
291
292   AliTRDCalROC* roc = calPad->GetCalROC(det);
293   if (!roc)
294     return -1;
295
296   return roc->GetValue(col, row);
297 };
298
299 //_____________________________________________________________________________
300 Float_t AliTRDcalibDB::GetSamplingFrequency()
301 {
302   //
303   // Returns the sampling frequency of the TRD read-out.
304   //
305   
306   AliTRDCalGlobals* calGlobal = dynamic_cast<AliTRDCalGlobals*> (GetCachedCDBObject(kIDGlobals));
307   if (!calGlobal)
308     return -1;  
309   
310   return calGlobal->GetSamplingFrequency();
311 }
312   
313 //_____________________________________________________________________________
314 Int_t AliTRDcalibDB::GetNumberOfTimeBins()
315 {
316   //
317   // Returns the number of time bins which are read-out.
318   //
319   
320   AliTRDCalGlobals* calGlobal = dynamic_cast<AliTRDCalGlobals*> (GetCachedCDBObject(kIDGlobals));
321   if (!calGlobal)
322     return -1;  
323   
324   return calGlobal->GetNumberOfTimeBins();
325 }
326
327 //_____________________________________________________________________________
328 Float_t AliTRDcalibDB::GetOmegaTau(Float_t vdrift)
329 {
330   //
331   // Returns omega*tau (tan(Lorentz-angle)) for a given drift velocity <vd> 
332   // and a B-field <b> for Xe/CO2 (15%).
333   // The values are according to a GARFIELD simulation.
334   //
335   // This function basically does not belong to the calibration class. It should be moved somewhere else. 
336   // However, currently it is in use by simulation and reconstruction.
337   //
338   
339   AliTRDCommonParam* commonParam = AliTRDCommonParam::Instance();
340   if (!commonParam)
341     return -1;
342   Float_t field = commonParam->GetField();
343
344   const Int_t kNb = 5;
345   Float_t p0[kNb] = {  0.004810,  0.007412,  0.010252,  0.013409,  0.016888 };
346   Float_t p1[kNb] = {  0.054875,  0.081534,  0.107333,  0.131983,  0.155455 };
347   Float_t p2[kNb] = { -0.008682, -0.012896, -0.016987, -0.020880, -0.024623 };
348   Float_t p3[kNb] = {  0.000155,  0.000238,  0.000330,  0.000428,  0.000541 };
349
350   Int_t ib = ((Int_t) (10 * (field - 0.15)));
351   ib       = TMath::Max(  0,ib);
352   ib       = TMath::Min(kNb,ib);
353
354   Float_t alphaL = p0[ib] 
355       + p1[ib] * vdrift
356       + p2[ib] * vdrift*vdrift
357       + p3[ib] * vdrift*vdrift*vdrift;
358
359   return TMath::Tan(alphaL);
360 }
361