]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/corrs/CorrExtractor.C
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / corrs / CorrExtractor.C
1 #include <TFile.h>
2 #include <TList.h>
3 #include <TParameter.h>
4 #include <TError.h>
5 #include "AliCorrectionManagerBase.h"
6
7 /**
8  * Extract corrections from result file 
9  * 
10  */
11 struct CorrExtractor 
12 {
13   /** 
14    * Constructor 
15    * 
16    * @param manager Correction manager
17    */
18   CorrExtractor(AliCorrectionManagerBase* manager)
19     : fFile(0), 
20       fTop(0), 
21       fOut(""),
22       fRunNo(0), 
23       fSys(0), 
24       fSNN(0), 
25       fField(999), 
26       fMC(false), 
27       fSatellite(false),
28       fManager(manager)
29   {}
30   TCollection* GetCollection(TCollection* p, 
31                              const TString& name)
32   {
33     TObject* o = 0;
34     if (p == 0) { 
35       o = fFile->Get(name);
36       if (!o) { 
37         Warning("CorrExtractor", "Object %s not found in file", name.Data());
38         return 0;
39       }
40     }
41     else {
42       o = p->FindObject(name);
43       if (!o) { 
44         Warning("CorrExtractor", "Object %s not found in %s", 
45                 name.Data(), p->GetName());
46         return 0;
47       }
48     }      
49     if (!o->IsA()->InheritsFrom(TCollection::Class())) {
50       Warning("CorrExtractor", "%s in %s is not a collection, but a %s", 
51               name.Data(), (p ? p->GetName() : "file"), o->ClassName());
52       return 0;
53     }
54     return static_cast<TCollection*>(o);
55   }
56   /** 
57    * Find a collection in a file 
58    * 
59    * @param path Path to collection
60    * 
61    * @return Found collection or null
62    */
63   TCollection* FindCollection(const TString& path)
64   {
65     if (path.IsNull()) return 0;
66     TObjArray*   tokens = path.Tokenize("/");
67     TIter        next(tokens);
68     TObjString*  token = 0;
69     TCollection* p     = 0;
70     while ((token = static_cast<TObjString*>(next()))) {
71       const TString& t = token->String();
72       if (t.IsNull()) continue;
73       p = GetCollection(p, t);
74       if (!p) break;
75     }
76     tokens->Delete();
77     return p;
78     
79   }
80   /** 
81    * Find an object 
82    * 
83    * @param path Path to object 
84    * @param name Name of object
85    * 
86    * @return Found object or null
87    */  
88   TObject* FindObject(const TString& path, 
89                       const TString& name) 
90   {
91     if (path.IsNull()) { 
92       TObject* o = fFile->Get(name);
93       if (!o) { 
94         Warning("CorrExtractor", "Object %s not found in file", 
95                 name.Data());
96         return 0;
97       }
98       return o;
99     }
100     TCollection* p     = FindCollection(path);
101     if (!p) { 
102       Warning("CorrExtractor", "Path %s invalid", path.Data());
103       return 0;
104     }
105     return p->FindObject(name);
106   }
107   /** 
108    * Initialize this extactor
109    * 
110    * @param fileName  File to extract from 
111    * @param sumFolder The summed folder 
112    * @param out       The result folder
113    * 
114    * @return true on success
115    */
116   Bool_t Init(const TString&        fileName, 
117               const TString&        sumFolder, 
118               const TString&        out)
119   {
120     fOut  = out;
121     Clear();
122
123     fFile = TFile::Open(fileName, "READ");
124     if (!fFile) {
125       Error("CorrExtractor", "Failed to open \"%s\"", fileName.Data());
126       Clear();
127       return false;
128     }
129     TCollection* c = FindCollection(Form("%s/fmdEventInspector", 
130                                          sumFolder.Data()));
131     if (!c) { 
132       Error("CorrExtractor", "Couldn't get event inspector list from %s",
133             fileName.Data());
134       Clear();
135       return false;
136     }
137     TObject* oSys        = c->FindObject("sys");
138     TObject* oSNN        = c->FindObject("sNN");
139     TObject* oFld        = c->FindObject("field");
140     TObject* oRun        = c->FindObject("runNo");
141     TObject* oSat        = c->FindObject("satellite");
142     if (oSys && fSys   <= 0)   fSys       = oSys->GetUniqueID();
143     if (oSNN && fSNN   <= 0)   fSNN       = oSNN->GetUniqueID();
144     if (oFld && fField >= 999) fField     = oFld->GetUniqueID();
145     if (oRun && fRunNo <= 0)   fRunNo     = oRun->GetUniqueID();
146     if (oSat)                  fSatellite = oSat->GetUniqueID();
147
148     if (fSys <= 0 || fSys > 3 || fSNN <= 0 || fField >= 999 || fRunNo <= 0 ){
149       Error("CorrExtractor", "Failed to get settings");
150       Clear();
151       return false;
152     } 
153     return true;
154   }
155   /** 
156    * Set whether this is MC or not
157    * 
158    * @param mc If true, consider this MC 
159    */
160   void SetMC(Bool_t mc=true) { fMC = mc; }
161   /** 
162    * Extract the stuff 
163    * 
164    * @param cls    Class of object
165    * @param parent Parent folder 
166    * 
167    * @return 
168    */
169   Bool_t Extract(const TClass* cls, const TString& parent)
170   {
171     return Extract(cls->GetName(), parent);
172   }
173   /** 
174    * Extract the stuff
175    * 
176    * @param objName  Object name 
177    * @param parent   Parent folder 
178    * 
179    * @return 
180    */
181   Bool_t Extract(const TString& objName, 
182                  const TString& parent="") 
183   {
184     if (!fFile) { 
185       Warning("Extract", "No file opened");
186       return false;
187     }   
188     TObject* o = FindObject(parent, objName);
189     if (!o) { 
190       Warning("Extract", "Object %s not found in collection %s", 
191               objName.Data(), parent.Data());
192       return false;
193     }
194     return fManager->Store(o, 
195                            fRunNo, 
196                            fSys, 
197                            fSNN, 
198                            fField, 
199                            fMC, 
200                            fSatellite, 
201                            fOut.Data());
202   }
203   /** 
204    * Clear this extractor 
205    * 
206    */
207   void Clear()
208   {
209     if (!fFile) return;
210     fFile->Close();
211     fFile      = 0;
212     fTop       = 0;
213     fRunNo     = 0;
214     fSys       = 0;
215     fSNN       = 0;
216     fField     = 999;
217     fMC        = false;
218     fSatellite = false;
219   }
220   TFile*                    fFile;          // Our file
221   TList*                    fTop;           // Top list
222   TString                   fOut;           // Output 
223   ULong_t                   fRunNo;         // Run number
224   UShort_t                  fSys;           // System
225   UShort_t                  fSNN;           // Collision energy in GeV
226   Short_t                   fField;         // L3 field in kG
227   Bool_t                    fMC;            // Simulation flag
228   Bool_t                    fSatellite;     // Satellite interaction flag
229   AliCorrectionManagerBase* fManager;       // Correction manager to use 
230 };
231
232 //
233 // EOF
234 //
235
236