AliPhysics  db95e02 (db95e02)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliMCAuxHandler.cxx
Go to the documentation of this file.
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 <TSystem.h>
9 #include <AliStack.h>
10 #include <AliMCEvent.h>
11 
13 #if 0 // For Emacs - do not remove
14 ;
15 #endif
16 
17 //____________________________________________________________________
18 AliMCAuxHandler::AliMCAuxHandler(const char* name,
19  const char* what,
20  AliMCEventHandler* parent)
21  : AliMCEventHandler(name, what),
22  fParent(parent),
23  fFile(0),
24  fTree(0),
25  fDir(0),
26  fArray(0),
27  fNEvents(0),
28  fNEventsPerFile(0),
29  fNEventsInContainer(0),
30  fEvent(0),
31  fFileNumber(0),
32  fTreeName(""),
33  fFileBase("")
34 {
35  // Constructor
36  //
37  // Parameters:
38  // name The name
39 }
40 
41 //____________________________________________________________________
42 TString*
44 {
45  if (!fParent) {
46  AliWarning("No parent");
47  return 0;
48  }
49  return fParent->GetInputPath();
50 }
51 
52 //____________________________________________________________________
53 Bool_t
55 {
56  // Initialize the input
57  //
58  // @param opt Options
59  //
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());
64 
65  TString option(opt);
66  if (option.EqualTo("proof") ||
67  option.EqualTo("local") ||
68  option.EqualTo("lite")) return true;
69 
70  TString t = "Tree";
71  TString b = "";
72  TClass* cl = gROOT->GetClass(GetTitle());
73  if (cl) {
74  if (cl->InheritsFrom("AliHit")) {
75  t += "H";
76  b = "Hits";
77  }
78  else if (cl->InheritsFrom("AliSDigit")) {
79  t += "S";
80  b = "SDigits";
81  }
82  else if (cl->InheritsFrom("AliDigit")) {
83  t += "D";
84  b = "Digits";
85  }
86  else
87  t = "";
88  }
89  else {
90  AliWarningF("Couldn't get the class description of %s", GetTitle());
91  AliWarning("The list of loaded libraries is");
92  gSystem->ListLibraries("");
93  return false;
94  }
95 
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;
99 
100 
101  fArray = new TClonesArray(GetTitle());
102 
103  TTree* treeE = fParent->GetTree();
104  if (!treeE) {
105  AliError("Parent does not have an events tree");
106  return false;
107  }
108 
109  // Get number of events in this directory
110  fNEventsPerFile = -1;
111  fNEvents = treeE->GetEntries();
112  fEvent = 0;
113  fFileNumber = 0;
114 
115  if (!OpenFile(fFileNumber)) return false;
116 
117  return true;
118 }
119 //____________________________________________________________________
120 Bool_t
122 {
123  // Called at the beginning of an event
124  //
125  // @param entry Entry in tree
126  //
127  // @return true on success
128  AliDebugF(10,"AliMCAuxHandler::BeginEvent(%lld)", entry);
129  // Info("","AliMCAuxHandler::BeginEvent(%lld)", entry);
130 
131  if (entry == -1)
132  fEvent++;
133  else
134  fEvent = entry;
135 
136  if (fEvent >= fNEvents) {
137  AliWarningF("Event number out of range %d/%d", fEvent, fNEvents);
138  return false;
139  }
140 
141  if (fNEventsPerFile < 0) {
142  TTree* treeK = fParent->TreeK();
143  if (!treeK) {
144  AliError("Parent does not have a kinematics tree");
145  return false;
146  }
147 
148  TFile* fileK = treeK->GetCurrentFile();
149  if (!fileK) {
150  AliError("Kinematics tree has no associated file");
151  return false;
152  }
153  // Get the number of events per file
154  fNEventsPerFile = fileK->GetNkeys() - fileK->GetNProcessIDs();
155  }
156  return LoadEvent(fEvent);
157 }
158 //____________________________________________________________________
159 Bool_t
160 AliMCAuxHandler::Notify(const char* path)
161 {
162  // Called when the input file is changed
163  //
164  // @param path New path
165  //
166  // @return true on success
167  AliDebugF(10,"AliMCAuxHandler::Notify(\"%s\")", path);
168  // Info("","AliMCAuxHandler::Notify(\"%s\")", path);
169  return true;
170 }
171 //____________________________________________________________________
172 Bool_t
174 {
175  // Called at the end of an event
176  //
177  // @return true on success
178  // AliDebug(10,"AliMCAuxHandler::FinishEvent()");
179  return true;
180 }
181 //____________________________________________________________________
182 Bool_t
184 {
185  // Called at the end of a job
186  //
187  // @return true on success
188  AliDebug(10,"AliMCAuxHandler::Terminate()");
189  return true;
190 }
191 //____________________________________________________________________
192 Bool_t
194 {
195  // Called at the end of a sub-job
196  //
197  // @return true on success
198  AliDebug(10,"AliMCAuxHandler::TerminateIO()");
199  return true;
200 }
201 
202 //____________________________________________________________________
203 void
205 {
206  // Reset the I/O
207  //
208  //
209  AliDebug(10,"AliMCAuxHandler::ResetIO()");
210 
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");
214 
215  if (fFile) {
216  delete fFile;
217  fFile = 0;
218  }
219 }
220 //____________________________________________________________________
221 Bool_t
223 {
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;
228 
229  TString ext("");
230  if (fileNo > 0) ext = TString::Format("%d", fileNo);
231 
232  TString w(GetTitle());
233  if (w.EndsWith("s")) w.Chop();
234 
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");
241  if (!fFile) {
242  AliErrorF("Failed to open %s", fn.Data());
243  return false;
244  }
245 
246  return true;
247 }
248 
249 //____________________________________________________________________
250 Bool_t
252 {
253  // Load an event
254  //
255  // @param iev Event number
256  //
257  // @return true on success
258  AliDebugF(10,"AliMCAuxHandler::LoadEvent(%d)", iev);
259  // Info("","AliMCAuxHandler::LoadEvent(%d)", iev);
260 
261  Int_t iNew = iev / fNEventsPerFile;
262  if (iNew != fFileNumber) {
263  fFileNumber = iNew;
264  if (!OpenFile(fFileNumber)) return false;
265  }
266  if (!fFile) return false;
267 
268  TString folder = TString::Format("Event%d", iev);
269  fFile->GetObject(folder, fDir);
270  if (!fDir) {
271  AliWarningF("Folder %s not found in file", folder.Data());
272  return false;
273  }
274 
275  fDir->GetObject(fTreeName, fTree);
276  if (!fTree) {
277  AliWarningF("Folder %s does not contain the %s tree %s",
278  folder.Data(), GetTitle(), fTreeName.Data());
279  return false;
280  }
281 
282  fTree->SetBranchAddress(GetName(), &fArray);
283  return true;
284 }
285 
286 //____________________________________________________________________
287 Int_t
289 {
290  if (!fTree) return 0;
291  return fTree->GetEntries();
292 }
293 
294 //____________________________________________________________________
295 TClonesArray*
297 {
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());
303  return 0;
304  }
305  fArray->Clear();
306 
307  if (fTree->GetEntry(entry) <= 0) return 0;
308 
309  return fArray;
310 }
311 
312 
313 //____________________________________________________________________
315 AliMCAuxHandler::Create(const char* name, const char* what)
316 {
317  AliAnalysisManager* mgr = AliAnalysisManager::GetAnalysisManager();
318  if (!mgr) {
319  ::Error("AliMCAuxHandler::Create", "No analysis manager");
320  return 0;
321  }
322 
323  AliVEventHandler* vmc = mgr->GetMCtruthEventHandler();
324  if (!vmc) {
325  ::Error("AliMCAuxHandler::Create", "No MC truth handler");
326  return 0;
327  }
328 
329  AliMCEventHandler* mc = dynamic_cast<AliMCEventHandler*>(vmc);
330  if (!mc) {
331  ::Error("AliMCAuxHandler::Create",
332  "MC truth handler not a AliMCEventHandler, but %s",
333  vmc->ClassName());
334  return 0;
335  }
336 
337  AliMCAuxHandler* ret = new AliMCAuxHandler(name, what, mc);
338  mc->AddSubsidiaryHandler(ret);
339 
340  return ret;
341 }
342 
343 //____________________________________________________________________
344 TClonesArray*
346  Int_t particle)
347 {
348  if (!handler) {
349  ::Error("AliMCAuxHandler::GetArray", "No handler passed");
350  return 0;
351  }
352 
353  AliMCEventHandler* mc = handler->GetParent();
354  if (!mc) {
355  ::Error("AliMCAuxHandler::GetArray", "Handler has no parent");
356  return 0;
357  }
358 
359  AliMCEvent* event = mc->MCEvent();
360  if (!event) {
361  ::Error("AliMCAuxHandler::GetArray", "No MC event");
362  return 0;
363  }
364 
365  AliStack* stack = event->Stack();
366  if (!stack) {
367  ::Error("AliMCAuxHandler::GetArray", "Event has no stack");
368  return 0;
369  }
370 
371  handler->GetArray()->Clear();
372  TTree* tree = handler->GetTree();
373  if (!tree) {
374  ::Error("AliMCAuxHandler::GetArray", "Handler has no tree");
375  return 0;
376  }
377 
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);
383  return 0;
384  }
385 
386  tree->GetEntry(treeIdx);
387 
388  return handler->GetArray();
389 }
390 
391 
392 
393 //____________________________________________________________________
394 //
395 // EOF
396 //
virtual Bool_t FinishEvent()
long long Long64_t
Definition: External.C:43
TSystem * gSystem
TClonesArray * fArray
virtual Bool_t OpenFile(Int_t ev)
virtual Bool_t LoadEvent(Int_t iev)
ClassImp(AliMCAuxHandler) AliMCAuxHandler
TDirectory * fDir
static TClonesArray * GetParticleArray(AliMCAuxHandler *handler, Int_t particle)
int Int_t
Definition: External.C:63
virtual void ResetIO()
TClonesArray * GetArray() const
virtual Bool_t Init(TTree *t, Option_t *o)
TString * GetParentPath() const
virtual Bool_t Notify()
AliMCEventHandler * fParent
virtual Bool_t BeginEvent(Long64_t entry)
virtual Bool_t TerminateIO()
virtual TTree * GetTree() const
Int_t GetNEntry() const
AliMCAuxHandler(const char *name="FMD", const char *clsName="AliFMDHit", AliMCEventHandler *parent=0)
const char Option_t
Definition: External.C:48
virtual Bool_t Terminate()
bool Bool_t
Definition: External.C:53
static AliMCAuxHandler * Create(const char *name="FMD", const char *what="Hits")
TClonesArray * GetEntryArray(Int_t entry)
AliMCEventHandler * GetParent()