1 #include "AliMCAuxHandler.h"
2 #include "AliAnalysisManager.h"
6 #include <TClonesArray.h>
10 #include <AliMCEvent.h>
12 ClassImp(AliMCAuxHandler)
13 #if 0 // For Emacs - do not remove
17 //____________________________________________________________________
18 AliMCAuxHandler::AliMCAuxHandler(const char* name,
20 AliMCEventHandler* parent)
21 : AliMCEventHandler(name, what),
29 fNEventsInContainer(0),
41 //____________________________________________________________________
43 AliMCAuxHandler::GetParentPath() const
46 AliWarning("No parent");
49 return fParent->GetInputPath();
52 //____________________________________________________________________
54 AliMCAuxHandler::Init(Option_t* opt)
56 // Initialize the input
60 // @return true on success
61 AliDebugF(10,"AliMCAuxHandler::Init(\"%s\")", opt);
62 // Info("","AliMCAuxHandler::Init(\"%s\") - detector: %s, class: %s",
63 // opt, GetName(), GetTitle());
66 if (option.EqualTo("proof") ||
67 option.EqualTo("local") ||
68 option.EqualTo("lite")) return true;
72 TClass* cl = gROOT->GetClass(GetTitle());
74 if (cl->InheritsFrom("AliHit")) {
78 else if (cl->InheritsFrom("AliSDigit")) {
82 else if (cl->InheritsFrom("AliDigit")) {
90 AliWarningF("Couldn't get the class description of %s", GetTitle());
91 AliWarning("The list of loaded libraries is");
92 gSystem->ListLibraries("");
96 // Info("Init", "Tree-name: %s, file-base: %s", t.Data(), b.Data());
97 if (!t.IsNull()) fTreeName = t;
98 if (!b.IsNull()) fFileBase = b;
101 fArray = new TClonesArray(GetTitle());
103 TTree* treeE = fParent->GetTree();
105 AliError("Parent does not have an events tree");
109 // Get number of events in this directory
110 fNEventsPerFile = -1;
111 fNEvents = treeE->GetEntries();
115 if (!OpenFile(fFileNumber)) return false;
119 //____________________________________________________________________
121 AliMCAuxHandler::BeginEvent(Long64_t entry)
123 // Called at the beginning of an event
125 // @param entry Entry in tree
127 // @return true on success
128 AliDebugF(10,"AliMCAuxHandler::BeginEvent(%lld)", entry);
129 // Info("","AliMCAuxHandler::BeginEvent(%lld)", entry);
136 if (fEvent >= fNEvents) {
137 AliWarningF("Event number out of range %d/%d", fEvent, fNEvents);
141 if (fNEventsPerFile < 0) {
142 TTree* treeK = fParent->TreeK();
144 AliError("Parent does not have a kinematics tree");
148 TFile* fileK = treeK->GetCurrentFile();
150 AliError("Kinematics tree has no associated file");
153 // Get the number of events per file
154 fNEventsPerFile = fileK->GetNkeys() - fileK->GetNProcessIDs();
156 return LoadEvent(fEvent);
158 //____________________________________________________________________
160 AliMCAuxHandler::Notify(const char* path)
162 // Called when the input file is changed
164 // @param path New path
166 // @return true on success
167 AliDebugF(10,"AliMCAuxHandler::Notify(\"%s\")", path);
168 // Info("","AliMCAuxHandler::Notify(\"%s\")", path);
171 //____________________________________________________________________
173 AliMCAuxHandler::FinishEvent()
175 // Called at the end of an event
177 // @return true on success
178 // AliDebug(10,"AliMCAuxHandler::FinishEvent()");
181 //____________________________________________________________________
183 AliMCAuxHandler::Terminate()
185 // Called at the end of a job
187 // @return true on success
188 AliDebug(10,"AliMCAuxHandler::Terminate()");
191 //____________________________________________________________________
193 AliMCAuxHandler::TerminateIO()
195 // Called at the end of a sub-job
197 // @return true on success
198 AliDebug(10,"AliMCAuxHandler::TerminateIO()");
202 //____________________________________________________________________
204 AliMCAuxHandler::ResetIO()
209 AliDebug(10,"AliMCAuxHandler::ResetIO()");
211 TString* path = GetParentPath();
212 AliDebugF(10,"Got parent path %s", path ? path->Data() : "null");
213 // Info("ResetIO","Got parent path %s", path ? path->Data() : "null");
220 //____________________________________________________________________
222 AliMCAuxHandler::OpenFile(Int_t fileNo)
224 TString* path = GetParentPath();
225 AliDebugF(10,"Got parent path %s", path ? path->Data() : "null");
226 // Info("OpenFile","Got parent path %s", path ? path->Data() : "null");
227 if (!path) return false;
230 if (fileNo > 0) ext = TString::Format("%d", fileNo);
232 TString w(GetTitle());
233 if (w.EndsWith("s")) w.Chop();
235 TString fn = TString::Format("%s%s.%s%s.root",
236 path->Data(), GetName(),
237 fFileBase.Data(), ext.Data());
238 AliDebugF(10,"Opening %s", fn.Data());
239 // Info("OpenFile", "Opening %s", fn.Data());
240 fFile = TFile::Open(fn, "READ");
242 AliErrorF("Failed to open %s", fn.Data());
249 //____________________________________________________________________
251 AliMCAuxHandler::LoadEvent(Int_t iev)
255 // @param iev Event number
257 // @return true on success
258 AliDebugF(10,"AliMCAuxHandler::LoadEvent(%d)", iev);
259 // Info("","AliMCAuxHandler::LoadEvent(%d)", iev);
261 Int_t iNew = iev / fNEventsPerFile;
262 if (iNew != fFileNumber) {
264 if (!OpenFile(fFileNumber)) return false;
266 if (!fFile) return false;
268 TString folder = TString::Format("Event%d", iev);
269 fFile->GetObject(folder, fDir);
271 AliWarningF("Folder %s not found in file", folder.Data());
275 fDir->GetObject(fTreeName, fTree);
277 AliWarningF("Folder %s does not contain the %s tree %s",
278 folder.Data(), GetTitle(), fTreeName.Data());
282 fTree->SetBranchAddress(GetName(), &fArray);
286 //____________________________________________________________________
288 AliMCAuxHandler::GetNEntry() const
290 if (!fTree) return 0;
291 return fTree->GetEntries();
294 //____________________________________________________________________
296 AliMCAuxHandler::GetEntryArray(Int_t entry)
298 if (!fTree) return 0;
299 if (!fArray) return 0;
300 if (entry < 0 || entry >= fTree->GetEntries()) {
301 AliErrorF("Entry # %d out of bounds [0,%lld]",
302 entry, fTree->GetEntries());
307 if (fTree->GetEntry(entry) <= 0) return 0;
313 //____________________________________________________________________
315 AliMCAuxHandler::Create(const char* name, const char* what)
317 AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
319 ::Error("AliMCAuxHandler::Create", "No analysis manager");
323 AliVEventHandler* vmc = mgr->GetMCtruthEventHandler();
325 ::Error("AliMCAuxHandler::Create", "No MC truth handler");
329 AliMCEventHandler* mc = dynamic_cast<AliMCEventHandler*>(vmc);
331 ::Error("AliMCAuxHandler::Create",
332 "MC truth handler not a AliMCEventHandler, but %s",
337 AliMCAuxHandler* ret = new AliMCAuxHandler(name, what, mc);
338 mc->AddSubsidiaryHandler(ret);
343 //____________________________________________________________________
345 AliMCAuxHandler::GetParticleArray(AliMCAuxHandler* handler,
349 ::Error("AliMCAuxHandler::GetArray", "No handler passed");
353 AliMCEventHandler* mc = handler->GetParent();
355 ::Error("AliMCAuxHandler::GetArray", "Handler has no parent");
359 AliMCEvent* event = mc->MCEvent();
361 ::Error("AliMCAuxHandler::GetArray", "No MC event");
365 AliStack* stack = event->Stack();
367 ::Error("AliMCAuxHandler::GetArray", "Event has no stack");
371 handler->GetArray()->Clear();
372 TTree* tree = handler->GetTree();
374 ::Error("AliMCAuxHandler::GetArray", "Handler has no tree");
378 Int_t treeIdx = stack->TreeKEntry(particle);
379 if (treeIdx < 0 || treeIdx >= tree->GetEntries()) {
380 ::Error("AliMCAuxHandler::GetArray",
381 "Index %d of %d out of bounds [0,%lld]", treeIdx, particle,
382 tree->GetEntries()-1);
386 tree->GetEntry(treeIdx);
388 return handler->GetArray();
393 //____________________________________________________________________