]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGLF/FORWARD/analysis2/AliMCAuxHandler.cxx
adding functionality to merge balance functions from several files (used for 2010...
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliMCAuxHandler.cxx
CommitLineData
2e658fb9 1#include "AliMCAuxHandler.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
11ClassImp(AliMCAuxHandler)
12#if 0 // For Emacs - do not remove
13;
14#endif
15
16//____________________________________________________________________
17AliMCAuxHandler::AliMCAuxHandler(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//____________________________________________________________________
41TString*
42AliMCAuxHandler::GetParentPath() const
43{
44 if (!fParent) {
45 AliWarning("No parent");
46 return 0;
47 }
48 return fParent->GetInputPath();
49}
50
51//____________________________________________________________________
52Bool_t
53AliMCAuxHandler::Init(Option_t* opt)
54{
55 // Initialize the input
56 //
57 // @param opt Options
58 //
59 // @return true on success
60 AliDebugF(10,"AliMCAuxHandler::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//____________________________________________________________________
107Bool_t
108AliMCAuxHandler::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,"AliMCAuxHandler::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//____________________________________________________________________
145Bool_t
146AliMCAuxHandler::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,"AliMCAuxHandler::Notify(\"%s\")", path);
154 return true;
155}
156//____________________________________________________________________
157Bool_t
158AliMCAuxHandler::FinishEvent()
159{
160 // Called at the end of an event
161 //
162 // @return true on success
163 AliDebug(10,"AliMCAuxHandler::FinishEvent()");
164 return true;
165}
166//____________________________________________________________________
167Bool_t
168AliMCAuxHandler::Terminate()
169{
170 // Called at the end of a job
171 //
172 // @return true on success
173 AliDebug(10,"AliMCAuxHandler::Terminate()");
174 return true;
175}
176//____________________________________________________________________
177Bool_t
178AliMCAuxHandler::TerminateIO()
179{
180 // Called at the end of a sub-job
181 //
182 // @return true on success
183 AliDebug(10,"AliMCAuxHandler::TerminateIO()");
184 return true;
185}
186
187//____________________________________________________________________
188void
189AliMCAuxHandler::ResetIO()
190{
191 // Reset the I/O
192 //
193 //
194 AliDebug(10,"AliMCAuxHandler::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//____________________________________________________________________
205Bool_t
206AliMCAuxHandler::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//____________________________________________________________________
232Bool_t
233AliMCAuxHandler::LoadEvent(Int_t iev)
234{
235 // Load an event
236 //
237 // @param iev Event number
238 //
239 // @return true on success
240 AliDebugF(10,"AliMCAuxHandler::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//____________________________________________________________________
268Int_t
269AliMCAuxHandler::GetNEntry() const
270{
271 if (!fTree) return 0;
272 return fTree->GetEntries();
273}
274
275//____________________________________________________________________
276TClonesArray*
277AliMCAuxHandler::GetEntryArray(Int_t entry)
278{
279 if (!fTree) return 0;
280 if (!fArray) return 0;
281 if (entry < 0 || entry >= fTree->GetEntries()) {
282 AliErrorF("Entry # %d out of bounds [0,%lld]",
283 entry, fTree->GetEntries());
284 return 0;
285 }
286 fArray->Clear();
287
288 if (fTree->GetEntry(entry) <= 0) return 0;
289
290 return fArray;
291}
292
293
294//____________________________________________________________________
295AliMCAuxHandler*
296AliMCAuxHandler::Create(const char* name, const char* what)
297{
298 AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
299 if (!mgr) {
300 ::Error("AliMCAuxHandler::Create", "No analysis manager");
301 return 0;
302 }
303
304 AliVEventHandler* vmc = mgr->GetMCtruthEventHandler();
305 if (!vmc) {
306 ::Error("AliMCAuxHandler::Create", "No MC truth handler");
307 return 0;
308 }
309
310 AliMCEventHandler* mc = dynamic_cast<AliMCEventHandler*>(vmc);
311 if (!mc) {
312 ::Error("AliMCAuxHandler::Create",
313 "MC truth handler not a AliMCEventHandler, but %s",
314 vmc->ClassName());
315 return 0;
316 }
317
318 AliMCAuxHandler* ret = new AliMCAuxHandler(name, what, mc);
319 mc->AddSubsidiaryHandler(ret);
320
321 return ret;
322}
323
324//____________________________________________________________________
325TClonesArray*
326AliMCAuxHandler::GetParticleArray(AliMCAuxHandler* handler,
327 Int_t particle)
328{
329 if (!handler) {
330 ::Error("AliMCAuxHandler::GetArray", "No handler passed");
331 return 0;
332 }
333
334 AliMCEventHandler* mc = handler->GetParent();
335 if (!mc) {
336 ::Error("AliMCAuxHandler::GetArray", "Handler has no parent");
337 return 0;
338 }
339
340 AliMCEvent* event = mc->MCEvent();
341 if (!event) {
342 ::Error("AliMCAuxHandler::GetArray", "No MC event");
343 return 0;
344 }
345
346 AliStack* stack = event->Stack();
289715c8 347 if (!stack) {
2e658fb9 348 ::Error("AliMCAuxHandler::GetArray", "Event has no stack");
349 return 0;
350 }
351
352 handler->GetArray()->Clear();
353 TTree* tree = handler->GetTree();
354 if (!tree) {
355 ::Error("AliMCAuxHandler::GetArray", "Handler has no tree");
356 return 0;
357 }
358
359 Int_t treeIdx = stack->TreeKEntry(particle);
360 if (treeIdx < 0 || treeIdx >= tree->GetEntries()) {
361 ::Error("AliMCAuxHandler::GetArray",
362 "Index %d of %d out of bounds [0,%lld]", treeIdx, particle,
363 tree->GetEntries()-1);
364 return 0;
365 }
366
367 tree->GetEntry(treeIdx);
368
369 return handler->GetArray();
370}
371
372
373
374//____________________________________________________________________
375//
376// EOF
377//