More code clean up.
[u/mrichter/AliRoot.git] / PWG2 / FORWARD / analysis2 / AliForwardCorrectionManager.cxx
1 #include "AliForwardCorrectionManager.h"
2 #include "AliForwardUtil.h"
3 #include <TString.h>
4 #include <AliLog.h>
5 #include <TFile.h>
6 #include <TSystem.h>
7
8     
9 //____________________________________________________________________
10 AliForwardCorrectionManager* AliForwardCorrectionManager::fgInstance = 0;
11 const char* AliForwardCorrectionManager::fgkSecondaryMapSkel = "secondary";
12 const char* AliForwardCorrectionManager::fgkDoubleHitSkel    = "doublehit";
13 const char* AliForwardCorrectionManager::fgkELossFitsSkel    = "elossfits";
14 const char* AliForwardCorrectionManager::fgkVertexBiasSkel   = "vertexbias";
15 const char* AliForwardCorrectionManager::fgkMergingEffSkel   = "merging";
16
17 #define PREFIX "$(ALICE_ROOT)/PWG2/FORWARD/corrections/"
18
19 //____________________________________________________________________
20 AliForwardCorrectionManager& AliForwardCorrectionManager::Instance()
21 {
22   if (!fgInstance) fgInstance= new AliForwardCorrectionManager;
23   return *fgInstance;
24 }
25
26 //____________________________________________________________________
27 AliForwardCorrectionManager::AliForwardCorrectionManager()
28   : TObject(), 
29     fInit(kFALSE),
30     fSys(0),
31     fSNN(0),
32     fField(999),
33     fELossFitsPath(PREFIX "ELossFits"),
34     fMergingEffPath(PREFIX "MergingEfficiency"), 
35     fSecondaryMapPath(PREFIX "SecondaryMap"),
36     fDoubleHitPath(PREFIX "DoubleHit"),
37     fVertexBiasPath(PREFIX "VertexBias"),
38     fELossFit(0),
39     fSecondaryMap(0),
40     fDoubleHit(0),
41     fVertexBias(0),
42     fMergingEfficiency(0)
43 {
44 }
45 //____________________________________________________________________
46 AliForwardCorrectionManager::AliForwardCorrectionManager(const AliForwardCorrectionManager& o)
47   : TObject(o),
48     fInit(o.fInit),
49     fSys(o.fSys),
50     fSNN(o.fSNN),
51     fField(o.fField),
52     fELossFitsPath(o.fELossFitsPath),
53     fMergingEffPath(o.fMergingEffPath), 
54     fSecondaryMapPath(o.fSecondaryMapPath),
55     fDoubleHitPath(o.fDoubleHitPath),
56     fVertexBiasPath(o.fVertexBiasPath),
57     fELossFit(o.fELossFit),
58     fSecondaryMap(o.fSecondaryMap),
59     fDoubleHit(o.fDoubleHit),
60     fVertexBias(o.fVertexBias),
61     fMergingEfficiency(o.fMergingEfficiency)
62
63 {
64 }
65 //____________________________________________________________________
66 AliForwardCorrectionManager&
67 AliForwardCorrectionManager::operator=(const AliForwardCorrectionManager& o)
68 {
69   fInit             = o.fInit;
70   fSys              = o.fSys;
71   fSNN              = o.fSNN;
72   fField            = o.fField;
73   fELossFitsPath    = o.fELossFitsPath;
74   fMergingEffPath   = o.fMergingEffPath;
75   fSecondaryMapPath = o.fSecondaryMapPath;
76   fDoubleHitPath    = o.fDoubleHitPath;
77   fVertexBiasPath   = o.fVertexBiasPath;
78   fELossFit         = o.fELossFit;
79   fSecondaryMap     = o.fSecondaryMap;
80   fDoubleHit        = o.fDoubleHit;
81   fVertexBias       = o.fVertexBias;
82   fMergingEfficiency= o.fMergingEfficiency;
83   return *this;
84 }
85
86 //____________________________________________________________________
87 Bool_t
88 AliForwardCorrectionManager::Init(const char* cms, 
89                                   Float_t     sNN, 
90                                   Float_t     field,
91                                   Bool_t      mc,
92                                   UInt_t      what,
93                                   Bool_t      force)
94 {
95   UShort_t col = AliForwardUtil::ParseCollisionSystem(cms);
96   return Init(col, 
97               AliForwardUtil::ParseCenterOfMassEnergy(col, sNN),
98               AliForwardUtil::ParseMagneticField(field), 
99               mc, what, force);
100 }
101
102 //____________________________________________________________________
103 Bool_t
104 AliForwardCorrectionManager::Init(UShort_t cms, 
105                                   UShort_t sNN, 
106                                   Short_t  field,
107                                   Bool_t   mc,
108                                   UInt_t   what,
109                                   Bool_t   force)
110 {
111   if (force) fInit = kFALSE;
112   if (fInit) return kTRUE;
113
114   Bool_t ret = kTRUE;
115   if (fSys == cms && TMath::Abs(fSNN - sNN) < 10 && fField == field) { 
116     // We're already initialised for these settings - do nothing and return
117     fInit = kTRUE;
118     return ret;
119   }
120   // Set cached parameters 
121   fSys   = cms;
122   fSNN   = sNN;
123   fField = field;
124                
125   // Read secondary map if requested 
126   if (what & kSecondaryMap) {
127     if (!ReadSecondaryMap(cms, sNN, field)) {
128       AliWarning(Form("Failed to read in secondary map for "
129                       "cms=%d, sNN=%dGeV, field=%dkG", cms, sNN, field));
130       ret = kFALSE;
131     }
132   }
133   // Read double hit if requested 
134   if (what & kDoubleHit) {
135     if (!ReadDoubleHit(cms, sNN, field)) {
136       AliWarning(Form("Failed to read in double hit correction for "
137                       "cms=%d, sNN=%dGeV, field=%dkG", cms, sNN, field));
138       ret = kFALSE;
139     }
140   }
141   // Read energy loss fits if requested 
142   if (what & kELossFits) {
143     if (!ReadELossFits(cms, sNN, field, mc)) {
144       AliWarning(Form("Failed to read in energy loss fits for "
145                       "cms=%d, sNN=%dGeV, field=%dkG, %s", 
146                       cms, sNN, field, mc ? "MC" : "real"));
147       ret = kFALSE;
148     }
149   }
150   // Read event selection efficiencies if requested 
151   if (what & kVertexBias) {
152     if (!ReadVertexBias(cms, sNN, field)) {
153       AliWarning(Form("Failed to read in event selection efficiency for "
154                       "cms=%d, sNN=%dGeV, field=%dkG", cms, sNN, field));
155       ret = kFALSE;
156     }
157   }
158   // Read merging efficiencies if requested 
159   if (what & kMergingEfficiency) {
160     if (!ReadMergingEfficiency(cms, sNN, field)) {
161       AliWarning(Form("Failed to read in hit merging efficiency for "
162                       "cms=%d, sNN=%dGeV, field=%dkG", 
163                       cms, sNN, field));
164       ret = kFALSE;
165     }
166   }
167   fInit = kTRUE;
168   return ret;
169 }
170
171 //____________________________________________________________________
172 TString 
173 AliForwardCorrectionManager::GetFileName(ECorrection what, 
174                                          UShort_t    sys, 
175                                          UShort_t    sNN,
176                                          Short_t     field,
177                                          Bool_t      mc) const
178 {
179   TString fname = "";
180   fname = GetObjectName(what);
181   fname.Append(Form("_%s_%04dGeV_%c%1dkG_%s.root", 
182                     AliForwardUtil::CollisionSystemString(sys), 
183                     sNN, (field < 0 ? 'm' : 'p'), TMath::Abs(field), 
184                     (mc ? "MC" : "real")));
185   return fname;
186 }
187 //____________________________________________________________________
188 TString
189 AliForwardCorrectionManager::GetFileName(ECorrection what) const
190 {
191   if (!fInit) { 
192     AliWarning("Corrections manager initialised, do a forced Init(...)");
193     return "";
194   }
195   return GetFileName(what, fSys, fSNN, fField, false);
196 }
197
198 //____________________________________________________________________
199 const Char_t*
200 AliForwardCorrectionManager::GetFileDir(ECorrection what) const
201 {
202   if      (what & kSecondaryMap)        return fSecondaryMapPath;
203   else if (what & kDoubleHit)           return fDoubleHitPath;
204   else if (what & kELossFits)           return fELossFitsPath;
205   else if (what & kVertexBias)          return fVertexBiasPath;
206   else if (what & kMergingEfficiency)   return fMergingEffPath;
207
208   AliWarning(Form("Unknown correction: %d", what));
209   return 0;
210 }
211
212 //____________________________________________________________________
213 TString 
214 AliForwardCorrectionManager::GetFilePath(ECorrection what, 
215                                          UShort_t    sys, 
216                                          UShort_t    sNN,
217                                          Short_t     field,
218                                          Bool_t      mc) const
219 {
220   TString path = "";
221   const Char_t* dir = GetFileDir(what);
222   if (!dir) return path;
223   
224   TString fname(GetFileName(what, sys, sNN, field, mc));
225   if (fname.IsNull()) return path;
226
227   path = gSystem->ConcatFileName(gSystem->ExpandPathName(dir), fname);
228   
229   return path;
230 }
231 //____________________________________________________________________
232 TString
233 AliForwardCorrectionManager::GetFilePath(ECorrection what) const
234 {
235   if (!fInit) { 
236     AliWarning("Corrections manager initialised, do a forced Init(...)");
237     return "";
238   }
239   return GetFilePath(what, fSys, fSNN, fField, false);
240 }
241
242 //____________________________________________________________________
243 TFile*
244 AliForwardCorrectionManager::GetFile(ECorrection what, 
245                                      UShort_t    sys, 
246                                      UShort_t    sNN, 
247                                      Short_t     field, 
248                                      Bool_t      mc, 
249                                      Bool_t      rw, 
250                                      Bool_t      newfile) const
251 {
252   TString path = GetFilePath(what, sys, sNN, field, mc);
253   if (path.IsNull()) return 0;
254   
255   TString opt;
256   if (newfile) opt="RECREATE";
257   else {
258     if (gSystem->AccessPathName(path.Data(), 
259                                 (rw ? kWritePermission : kReadPermission))) {
260       AliWarning(Form("file %s cannot be found or insufficient permissions", 
261                       path.Data()));
262       return 0;
263     }
264     opt=(rw ? "UPDATE" : "READ");
265   }
266   TFile* file = TFile::Open(path.Data(), opt.Data());
267   if (!file) { 
268     AliWarning(Form("file %s cannot be opened in mode %s", 
269                     path.Data(), opt.Data()));
270     return 0;
271   }
272   return file;
273 }
274 //____________________________________________________________________
275 TFile*
276 AliForwardCorrectionManager::GetFile(ECorrection what) const
277 {
278   if (!fInit) { 
279     AliWarning("Corrections manager initialised, do a forced Init(...)");
280     return 0;
281   }
282   return GetFile(what, fSys, fSNN, fField, false);
283 }
284
285 //____________________________________________________________________
286 const Char_t*
287 AliForwardCorrectionManager::GetObjectName(ECorrection what) const
288 {
289   if      (what & kSecondaryMap)       return fgkSecondaryMapSkel;
290   else if (what & kDoubleHit)          return fgkDoubleHitSkel;
291   else if (what & kELossFits)          return fgkELossFitsSkel;
292   else if (what & kVertexBias)         return fgkVertexBiasSkel;
293   else if (what & kMergingEfficiency)  return fgkMergingEffSkel;
294   return 0;
295 }
296
297 //____________________________________________________________________
298 TObject*
299 AliForwardCorrectionManager::CheckObject(TFile* file, ECorrection what) const
300 {
301   TObject* o = file->Get(GetObjectName(what));
302   if (!o) { 
303     AliWarning(Form("Object %s not found in %s", 
304                     GetObjectName(what), file->GetName()));
305     file->Close();
306     return 0;
307   }
308   return o;
309 }
310   
311 //____________________________________________________________________
312 TObject*
313 AliForwardCorrectionManager::GetObject(ECorrection what, 
314                                        UShort_t    sys, 
315                                        UShort_t    sNN, 
316                                        Short_t     field,
317                                        Bool_t      mc) const
318 {
319   TFile* file = GetFile(what, sys, sNN, field, mc, false, false);
320   if (!file) return 0;
321   
322   return CheckObject(file, what);
323 }
324 //____________________________________________________________________
325 TObject*
326 AliForwardCorrectionManager::GetObject(ECorrection what) const
327 {
328   if (!fInit) { 
329     AliWarning("Corrections manager initialised, do a forced Init(...)");
330     return 0;
331   }
332   return GetObject(what, fSys, fSNN, fField, false);
333 }
334
335
336 //____________________________________________________________________
337 Bool_t 
338 AliForwardCorrectionManager::ReadSecondaryMap(UShort_t sys, UShort_t sNN, 
339                                               Short_t field)
340 {
341   if (fInit) { 
342     AliWarning("Corrections manager initialised, do a forced Init(...)");
343     return kFALSE;
344   }
345
346   TObject* o = GetObject(kSecondaryMap, sys, sNN, field, false);
347   if (!o) return kFALSE;
348
349   fSecondaryMap = dynamic_cast<AliFMDCorrSecondaryMap*>(o);
350   if (!fSecondaryMap) {
351     AliWarning(Form("Object %s (%p) is not an AliFMDCorrSecondaryMap object, "
352                     "but %s", fgkSecondaryMapSkel, o, o->ClassName())); 
353     return kFALSE;
354   }
355
356   // file->Close();
357   return kTRUE;
358 }
359 //____________________________________________________________________
360 Bool_t 
361 AliForwardCorrectionManager::ReadDoubleHit(UShort_t sys, UShort_t sNN, 
362                                            Short_t field)
363 {
364   if (fInit) { 
365     AliWarning("Corrections manager initialised, do a forced Init(...)");
366     return kFALSE;
367   }
368
369   TObject* o = GetObject(kDoubleHit, sys, sNN, field, false);
370   if (!o) return kFALSE;
371
372   fDoubleHit = dynamic_cast<AliFMDCorrDoubleHit*>(o);
373   if (!fDoubleHit) {
374     AliWarning(Form("Object %s (%p) is not an AliFMDCorrDoubleHit object, "
375                     "but %s", fgkDoubleHitSkel, o, o->ClassName())); 
376     return kFALSE;
377   }
378
379   // file->Close();
380   return kTRUE;
381 }
382
383 //____________________________________________________________________
384 Bool_t 
385 AliForwardCorrectionManager::ReadELossFits(UShort_t sys, UShort_t sNN, 
386                                            Short_t field, Bool_t mc)
387 {
388   if (fInit) { 
389     AliWarning("Corrections manager initialised, do a forced Init(...)");
390     return kFALSE;
391   }
392
393   TObject* o = GetObject(kELossFits, sys, sNN, field, mc);
394   if (!o) return kFALSE;
395
396   fELossFit = dynamic_cast<AliFMDCorrELossFit*>(o);
397   if (!fELossFit) {
398     AliWarning(Form("Object %s (%p) is not an AliFMDCorrELossFit object, "
399                     "but %s", fgkELossFitsSkel, o, o->ClassName()));
400     return kFALSE;
401   }
402
403   // file->Close();
404   return kTRUE;
405 }
406
407 //____________________________________________________________________
408 Bool_t 
409 AliForwardCorrectionManager::ReadVertexBias(UShort_t sys, 
410                                             UShort_t sNN, 
411                                             Short_t field)
412 {
413   if (fInit) { 
414     AliWarning("Corrections manager initialised, do a forced Init(...)");
415     return kFALSE;
416   }
417
418   TObject* o = GetObject(kVertexBias, sys, sNN, field, false);
419   if (!o) return kFALSE;
420
421   fVertexBias = dynamic_cast<AliFMDCorrVertexBias*>(o);
422   if (!fVertexBias) {
423     AliWarning(Form("Object %s (%p) is not an AliFMDCorrVertexBias object, "
424                     "but %s", fgkVertexBiasSkel, o, o->ClassName()));
425     return kFALSE;
426   }
427
428   // file->Close();
429   return kTRUE;
430 }
431
432 //____________________________________________________________________
433 Bool_t 
434 AliForwardCorrectionManager::ReadMergingEfficiency(UShort_t sys, 
435                                                    UShort_t sNN, 
436                                                    Short_t field)
437 {
438   if (fInit) { 
439     AliWarning("Corrections manager initialised, do a forced Init(...)");
440     return kFALSE;
441   }
442
443   TObject* o = GetObject(kMergingEfficiency, sys, sNN, field, false);
444   if (!o) return kFALSE;
445
446   fMergingEfficiency = dynamic_cast<AliFMDCorrMergingEfficiency*>(o);
447   if (!fMergingEfficiency) {
448     AliWarning(Form("Object %s (%p) is not an AliFMDCorrMergingEfficiency "
449                     "object, but %s", fgkMergingEffSkel, o, o->ClassName()));
450     return kFALSE;
451   }
452
453   // file->Close();
454   return kTRUE;
455 }
456
457 //____________________________________________________________________
458 //
459 // EOF
460 //