]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/AliFMDMCHitHandler.cxx
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliFMDMCHitHandler.cxx
1 #include "AliFMDMCHitHandler.h"
2 #include "AliAnalysisManager.h"
3 #include <TError.h>
4 #include <AliLog.h>
5 #include <TFile.h>
6 #include <TClonesArray.h>
7 #include <TROOT.h>
8 #include <AliStack.h>
9 #include <AliMCEvent.h>
10
11 ClassImp(AliFMDMCHitHandler)
12 #if 0 // For Emacs - do not remove
13 ;
14 #endif
15
16 //____________________________________________________________________
17 AliFMDMCHitHandler::AliFMDMCHitHandler(const char* name,
18                                        const char* what,
19                                        AliMCEventHandler* parent)
20   : AliMCEventHandler(name, what),
21     fParent(parent), 
22     fFile(0),
23     fTree(0),
24     fDir(0),
25     fArray(0),
26     fNEvents(0), 
27     fNEventsPerFile(0),
28     fNEventsInContainer(0),
29     fEvent(0), 
30     fFileNumber(0),
31     fTreeName(""),
32     fFileBase("")
33 {
34   // Constructor 
35   // 
36   // Parameters: 
37   //   name The name 
38 }
39
40 //____________________________________________________________________
41 TString*
42 AliFMDMCHitHandler::GetParentPath() const
43 {
44   if (!fParent) { 
45     AliWarning("No parent");
46     return 0;
47   }
48   return fParent->GetInputPath();
49 }
50
51 //____________________________________________________________________
52 Bool_t
53 AliFMDMCHitHandler::Init(Option_t* opt)
54 {
55   // Initialize the input
56   // 
57   // @param opt Options 
58   // 
59   // @return true on success 
60   AliDebugF(10,"AliFMDMCHitHandler::Init(\"%s\")", opt);
61
62   TString option(opt);
63   if (option.EqualTo("proof") || option.EqualTo("local")) return true;
64
65   TString t = "Tree";
66   TString b = "";
67   TClass* cl = gROOT->GetClass(GetTitle());
68   if (cl) { 
69     if (cl->InheritsFrom("AliHit")) { 
70       t += "H";
71       b =  "Hits";
72     }
73     else if (cl->InheritsFrom("AliSDigit")) {
74       t += "S";
75       b =  "SDigits";
76     }
77     else if (cl->InheritsFrom("AliDigit")) {
78       t += "D";
79       b =  "Digits";
80     }
81     else 
82       t = "";
83   }
84   if (!t.IsNull()) fTreeName = t;
85   if (!b.IsNull()) fFileBase = b;
86
87
88   fArray = new TClonesArray(GetTitle());
89   
90   TTree* treeE = fParent->GetTree();
91   if (!treeE) { 
92     AliError("Parent does not have an events tree");
93     return false;
94   }
95
96   // Get number of events in this directory 
97   fNEventsPerFile = -1;
98   fNEvents        = treeE->GetEntries();
99   fEvent          = 0;
100   fFileNumber     = 0;
101
102   if (!OpenFile(fFileNumber)) return false;
103
104   return true;
105 }
106 //____________________________________________________________________
107 Bool_t
108 AliFMDMCHitHandler::BeginEvent(Long64_t entry)
109 {
110   // Called at the beginning of an event 
111   // 
112   // @param entry Entry in tree 
113   // 
114   // @return true on success
115   AliDebugF(10,"AliFMDMCHitHandler::BeginEvent(%lld)", entry);
116
117   if (entry == -1) 
118     fEvent++;
119   else 
120     fEvent = entry;
121
122   if (fEvent >= fNEvents) { 
123     AliWarningF("Event number out of range %d/%d", fEvent, fNEvents);
124     return false;
125   }
126
127   if (fNEventsPerFile < 0) {
128     TTree* treeK = fParent->TreeK();
129     if (!treeK) { 
130       AliError("Parent does not have a kinematics tree");
131       return false;
132     }
133   
134     TFile* fileK = treeK->GetCurrentFile();
135     if (!fileK) { 
136       AliError("Kinematics tree has no associated file");
137       return false;
138     }
139     // Get the number of events per file 
140     fNEventsPerFile = fileK->GetNkeys() - fileK->GetNProcessIDs();
141   }
142   return LoadEvent(fEvent);
143 }
144 //____________________________________________________________________
145 Bool_t
146 AliFMDMCHitHandler::Notify(const char* path)
147 {
148   // Called when the input file is changed 
149   // 
150   // @param path New path 
151   //
152   // @return true on success
153   AliDebugF(10,"AliFMDMCHitHandler::Notify(\"%s\")", path);
154   return true;
155 }
156 //____________________________________________________________________
157 Bool_t
158 AliFMDMCHitHandler::FinishEvent()
159 {
160   // Called at the end of an event 
161   // 
162   // @return true on success
163   AliDebug(10,"AliFMDMCHitHandler::FinishEvent()");
164   return true;
165 }
166 //____________________________________________________________________
167 Bool_t
168 AliFMDMCHitHandler::Terminate()
169 {
170   // Called at the end of a job 
171   // 
172   // @return true on success 
173   AliDebug(10,"AliFMDMCHitHandler::Terminate()");
174   return true;
175 }
176 //____________________________________________________________________
177 Bool_t
178 AliFMDMCHitHandler::TerminateIO()
179 {
180   // Called at the end of a sub-job
181   // 
182   // @return true on success
183   AliDebug(10,"AliFMDMCHitHandler::TerminateIO()");
184   return true;
185 }
186
187 //____________________________________________________________________
188 void
189 AliFMDMCHitHandler::ResetIO()
190 {
191   // Reset the I/O
192   // 
193   //
194   AliDebug(10,"AliFMDMCHitHandler::ResetIO()");
195
196   TString* path = GetParentPath();
197   AliDebugF(10,"Got parent path %s", path ? path->Data() : "null");
198
199   if (fFile) { 
200     delete fFile;
201     fFile = 0;
202   }
203 }
204 //____________________________________________________________________
205 Bool_t
206 AliFMDMCHitHandler::OpenFile(Int_t fileNo)
207 {
208   TString* path = GetParentPath();
209   AliDebugF(10,"Got parent path %s", path ? path->Data() : "null");
210   if (!path) return false;
211
212   TString ext("");
213   if (fileNo > 0) ext = TString::Format("%d", fileNo);
214
215   TString w(GetTitle());
216   if (w.EndsWith("s")) w.Chop();
217     
218   TString fn = TString::Format("%s%s.%s%s.root", 
219                                path->Data(), GetName(), 
220                                fFileBase.Data(), ext.Data());
221   Info("Init", "Opening %s", fn.Data());
222   fFile = TFile::Open(fn, "READ");
223   if (!fFile) { 
224     AliErrorF("Failed to open %s", fn.Data());
225     return false;
226   }
227
228   return true;
229 }
230
231 //____________________________________________________________________
232 Bool_t
233 AliFMDMCHitHandler::LoadEvent(Int_t iev)
234 {
235   // Load an event 
236   // 
237   // @param iev Event number 
238   // 
239   // @return true on success 
240   AliDebugF(10,"AliFMDMCHitHandler::LoadEvent(%d)", iev);
241
242   Int_t iNew = iev / fNEventsPerFile;
243   if (iNew != fFileNumber) { 
244     fFileNumber = iNew;
245     if (!OpenFile(fFileNumber)) return false;
246   }
247   if (!fFile) return false;
248
249   TString folder = TString::Format("Event%d", iev);
250   fFile->GetObject(folder, fDir);
251   if (!fDir) { 
252     AliWarningF("Folder %s not found in file", folder.Data());
253     return false;
254   }
255
256   fDir->GetObject(fTreeName, fTree);
257   if (!fTree) { 
258     AliWarningF("Folder %s does not contain the %s tree %s", 
259                 folder.Data(), GetTitle(), fTreeName.Data());
260     return false;
261   }
262
263   fTree->SetBranchAddress(GetName(), &fArray);
264   return true;
265 }
266
267
268 //____________________________________________________________________
269 AliFMDMCHitHandler*
270 AliFMDMCHitHandler::Create(const char* name, const char* what)
271 {
272   AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
273   if (!mgr) { 
274     ::Error("AliFMDMCHitHandler::Create", "No analysis manager");
275     return 0;
276   }
277   
278   AliVEventHandler* vmc = mgr->GetMCtruthEventHandler();
279   if (!vmc) { 
280     ::Error("AliFMDMCHitHandler::Create", "No MC truth handler");
281     return 0;
282   }
283   
284   AliMCEventHandler* mc = dynamic_cast<AliMCEventHandler*>(vmc);
285   if (!mc) { 
286     ::Error("AliFMDMCHitHandler::Create", 
287             "MC truth handler not a AliMCEventHandler, but %s", 
288             vmc->ClassName());
289     return 0;
290   }
291
292   AliFMDMCHitHandler* ret = new AliFMDMCHitHandler(name, what, mc);
293   mc->AddSubsidiaryHandler(ret);
294   
295   return ret;
296 }
297
298 //____________________________________________________________________
299 TClonesArray*
300 AliFMDMCHitHandler::GetParticleArray(AliFMDMCHitHandler* handler, 
301                                      Int_t particle)
302 {
303   if (!handler) { 
304     ::Error("AliFMDMCHitHandler::GetArray", "No handler passed");
305     return 0;
306   }
307
308   AliMCEventHandler* mc = handler->GetParent();
309   if (!mc) {
310     ::Error("AliFMDMCHitHandler::GetArray", "Handler has no parent");
311     return 0;
312   }
313   
314   AliMCEvent* event = mc->MCEvent();
315   if (!event) { 
316     ::Error("AliFMDMCHitHandler::GetArray", "No MC event");
317     return 0;
318   }
319   
320   AliStack* stack = event->Stack();
321   if (!event) { 
322     ::Error("AliFMDMCHitHandler::GetArray", "Event has no stack");
323     return 0;
324   }
325   
326   TTree* tree = handler->GetTree();
327   if (!tree) {
328     ::Error("AliFMDMCHitHandler::GetArray", "Handler has no tree");
329     return 0;
330   }
331     
332   Int_t treeIdx = stack->TreeKEntry(particle);
333   if (treeIdx < 0 || treeIdx >= tree->GetEntries()) { 
334     ::Error("AliFMDMCHitHandler::GetArray", 
335             "Index %d of %d out of bounds [0,%lld]", treeIdx, particle, 
336             tree->GetEntries()-1);
337     return 0;
338   }
339   
340   tree->GetEntry(treeIdx);
341   
342   return handler->GetArray();
343 }
344
345   
346     
347 //____________________________________________________________________
348 //
349 // EOF
350 //