1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2002
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | // //
|
---|
27 | // MReadTree //
|
---|
28 | // //
|
---|
29 | // This tasks opens all branches in a specified tree and creates the //
|
---|
30 | // corresponding parameter containers if not already existing in the //
|
---|
31 | // parameter list. //
|
---|
32 | // //
|
---|
33 | // The Process function reads one events from the tree. To go through the //
|
---|
34 | // events of one tree make sure that the event number is increased from //
|
---|
35 | // outside. It makes also possible to go back by decreasing the number. //
|
---|
36 | // //
|
---|
37 | // If you don't want to start reading the first event you have to call //
|
---|
38 | // MReadTree::SetEventNum after instantiating your MReadTree-object. //
|
---|
39 | // //
|
---|
40 | // To make reading much faster (up to a factor of 10 to 20) you can //
|
---|
41 | // ensure that only the data you are really processing is enabled by //
|
---|
42 | // calling MReadTree::UseLeaf. //
|
---|
43 | // //
|
---|
44 | // If the chain switches from one file to another file all //
|
---|
45 | // TObject::Notify() functions are called of TObject objects which were //
|
---|
46 | // added to the Notifier list view MReadTree::AddNotify. If MReadTree //
|
---|
47 | // is the owner (viw MReadTree::SetOwner) all this objects are deleted //
|
---|
48 | // by the destructor of MReadTree //
|
---|
49 | // //
|
---|
50 | /////////////////////////////////////////////////////////////////////////////
|
---|
51 | #include "MReadTree.h"
|
---|
52 |
|
---|
53 | #include <fstream.h>
|
---|
54 |
|
---|
55 | #include <TFile.h> // TFile::GetName
|
---|
56 | #include <TSystem.h> // gSystem->ExpandPath
|
---|
57 | #include <TChainElement.h>
|
---|
58 | #include <TOrdCollection.h>
|
---|
59 |
|
---|
60 | #include "MChain.h"
|
---|
61 | #include "MFilter.h"
|
---|
62 | #include "MParList.h"
|
---|
63 | #include "MTaskList.h"
|
---|
64 |
|
---|
65 | #include "MLog.h"
|
---|
66 | #include "MLogManip.h"
|
---|
67 |
|
---|
68 |
|
---|
69 | ClassImp(MReadTree);
|
---|
70 |
|
---|
71 | // --------------------------------------------------------------------------
|
---|
72 | //
|
---|
73 | // Default constructor. Don't use it.
|
---|
74 | //
|
---|
75 | MReadTree::MReadTree()
|
---|
76 | : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
|
---|
77 | {
|
---|
78 | fName = "MRead";
|
---|
79 | fTitle = "Task to loop over all events in one single tree";
|
---|
80 |
|
---|
81 | fVetoList = NULL;
|
---|
82 | fNotify = NULL;
|
---|
83 |
|
---|
84 | fChain = NULL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | // --------------------------------------------------------------------------
|
---|
88 | //
|
---|
89 | // Constructor. It creates an TChain instance which represents the
|
---|
90 | // the Tree you want to read and adds the given file (if you gave one).
|
---|
91 | // More files can be added using MReadTree::AddFile.
|
---|
92 | // Also an empty veto list is created. This list is used if you want to
|
---|
93 | // veto (disable or "don't enable") a branch in the tree, it vetos also
|
---|
94 | // the creation of the corresponding object.
|
---|
95 | // An empty list of TObjects are also created. This objects are called
|
---|
96 | // at any time the TChain starts to read from another file.
|
---|
97 | //
|
---|
98 | MReadTree::MReadTree(const char *tname, const char *fname,
|
---|
99 | const char *name, const char *title)
|
---|
100 | : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
|
---|
101 | {
|
---|
102 | fName = name ? name : "MRead";
|
---|
103 | fTitle = title ? title : "Task to loop over all events in one single tree";
|
---|
104 |
|
---|
105 | fVetoList = new TList;
|
---|
106 | fVetoList->SetOwner();
|
---|
107 |
|
---|
108 | fNotify = new TList;
|
---|
109 |
|
---|
110 | //
|
---|
111 | // open the input stream
|
---|
112 | //
|
---|
113 | fChain = new MChain(tname);
|
---|
114 |
|
---|
115 | // root 3.02:
|
---|
116 | // In TChain::Addfile remove the limitation that the file name must contain
|
---|
117 | // the string ".root". ".root" is necessary only in case one wants to specify
|
---|
118 | // a Tree in a subdirectory of a Root file with eg, the format:
|
---|
119 |
|
---|
120 | if (fname)
|
---|
121 | if (fChain->Add(fname)>0)
|
---|
122 | SetBit(kChainWasChanged);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // --------------------------------------------------------------------------
|
---|
126 | //
|
---|
127 | // Destructor. It deletes the TChain and veto list object
|
---|
128 | //
|
---|
129 | MReadTree::~MReadTree()
|
---|
130 | {
|
---|
131 | //
|
---|
132 | // Delete all the pointers to pointers to the objects where the
|
---|
133 | // branche data gets stored.
|
---|
134 | //
|
---|
135 | TIter Next(fChain->GetStatus());
|
---|
136 |
|
---|
137 | TChainElement *element = NULL;
|
---|
138 | while ((element=(TChainElement*)Next()))
|
---|
139 | delete (MParContainer**)element->GetBaddress();
|
---|
140 |
|
---|
141 | //
|
---|
142 | // Delete the chain and the veto list
|
---|
143 | //
|
---|
144 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,00)
|
---|
145 | if (fChain->GetFile())
|
---|
146 | delete fChain->GetFile();
|
---|
147 | #endif
|
---|
148 | delete fChain;
|
---|
149 |
|
---|
150 | delete fNotify;
|
---|
151 | delete fVetoList;
|
---|
152 | }
|
---|
153 |
|
---|
154 | // --------------------------------------------------------------------------
|
---|
155 | //
|
---|
156 | // If the owner flag is set all TObjects which are scheduled via
|
---|
157 | // AddNotify are deleted by the destructor of MReadTree
|
---|
158 | //
|
---|
159 | void MReadTree::SetOwner(Bool_t flag)
|
---|
160 | {
|
---|
161 | flag ? fNotify->SetBit(kIsOwner) : fNotify->ResetBit(kIsOwner);
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --------------------------------------------------------------------------
|
---|
165 | //
|
---|
166 | // This function is called each time MReadTree changes the file to read
|
---|
167 | // from. It calls all TObject::Notify() functions which are scheduled
|
---|
168 | // via AddNotify.
|
---|
169 | //
|
---|
170 | Bool_t MReadTree::Notify()
|
---|
171 | {
|
---|
172 | *fLog << inf << GetDescriptor() << ": Notify '" << fChain->GetName();
|
---|
173 | *fLog << "' (before processing event #" << GetEventNum()-1 << ")" << endl;
|
---|
174 |
|
---|
175 | //fNotify->Notify();
|
---|
176 |
|
---|
177 | return kTRUE;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // --------------------------------------------------------------------------
|
---|
181 | //
|
---|
182 | // If you want to read the given tree over several files you must add
|
---|
183 | // the files here before PreProcess is called. Be careful: If the tree
|
---|
184 | // doesn't have the same contents (branches) it may confuse your
|
---|
185 | // program (trees which are are not existing in later files are not read
|
---|
186 | // anymore, tree wich are not existing in the first file are never read)
|
---|
187 | //
|
---|
188 | // Name may use the wildcarding notation, eg "xxx*.root" means all files
|
---|
189 | // starting with xxx in the current file system directory.
|
---|
190 | //
|
---|
191 | // AddFile returns the number of files added to the chain.
|
---|
192 | //
|
---|
193 | // For more information see TChain::Add
|
---|
194 | //
|
---|
195 | Int_t MReadTree::AddFile(const char *fname, Int_t entries)
|
---|
196 | {
|
---|
197 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
|
---|
198 | //
|
---|
199 | // This is a workaround to get rid of crashed if the file doesn't
|
---|
200 | // exist due to non initialized TFile::fProcessIDs
|
---|
201 | //
|
---|
202 | // (Code taken from TFile::TFile
|
---|
203 | //
|
---|
204 | const char *name;
|
---|
205 |
|
---|
206 | TString newname;
|
---|
207 |
|
---|
208 | if ((name = gSystem->ExpandPathName(fname)))
|
---|
209 | {
|
---|
210 | newname = name;
|
---|
211 | delete [] name;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (newname.IsNull())
|
---|
215 | {
|
---|
216 | *fLog << err << dbginf << "Error expanding path " << fname << "." << endl;
|
---|
217 | return 0;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (gSystem->AccessPathName(newname, kFileExists))
|
---|
221 | {
|
---|
222 | *fLog << err << "ERROR - File '" << fname << "' does not exist." << endl;
|
---|
223 | return 0;
|
---|
224 | }
|
---|
225 |
|
---|
226 | fname = newname.Data();
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | //
|
---|
230 | // FIXME! A check is missing whether the file already exists or not.
|
---|
231 | //
|
---|
232 | const Int_t numfiles = fChain->Add(fname, entries);
|
---|
233 |
|
---|
234 | if (numfiles>0)
|
---|
235 | SetBit(kChainWasChanged);
|
---|
236 |
|
---|
237 | return numfiles;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /*
|
---|
241 | // --------------------------------------------------------------------------
|
---|
242 | //
|
---|
243 | //
|
---|
244 | Int_t MReadTree::AddFile(const TChainElement &obj)
|
---|
245 | {
|
---|
246 | return AddFile(obj.GetTitle(), obj.GetEntries());
|
---|
247 | }
|
---|
248 | */
|
---|
249 |
|
---|
250 | // --------------------------------------------------------------------------
|
---|
251 | //
|
---|
252 | // Adds all files from another MReadTree to this instance
|
---|
253 | //
|
---|
254 | // Returns the number of file which were added
|
---|
255 | //
|
---|
256 | Int_t MReadTree::AddFiles(const MReadTree &read)
|
---|
257 | {
|
---|
258 | const Int_t rc = fChain->Add(read.fChain);
|
---|
259 |
|
---|
260 | if (rc>0)
|
---|
261 | SetBit(kChainWasChanged);
|
---|
262 |
|
---|
263 | /*
|
---|
264 | Int_t rc = 0;
|
---|
265 |
|
---|
266 | TIter Next(read.fChain->GetListOfFiles());
|
---|
267 | TObject *obj = NULL;
|
---|
268 | while ((obj=Next()))
|
---|
269 | rc += AddFile(*(TChainElement*)obj);
|
---|
270 | */
|
---|
271 |
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 | // --------------------------------------------------------------------------
|
---|
276 | //
|
---|
277 | // This function is called if Branch choosing method should get enabled.
|
---|
278 | // Branch choosing means, that only the enabled branches are read into
|
---|
279 | // memory. To use an enableing scheme we have to disable all branches first.
|
---|
280 | // This is done, if this function is called the first time.
|
---|
281 | //
|
---|
282 | void MReadTree::EnableBranchChoosing()
|
---|
283 | {
|
---|
284 | if (fBranchChoosing)
|
---|
285 | return;
|
---|
286 |
|
---|
287 | *fLog << inf << GetDescriptor() << ": Branch choosing enabled (only enabled branches are read)." << endl;
|
---|
288 | fChain->SetBranchStatus("*", kFALSE);
|
---|
289 | fBranchChoosing = kTRUE;
|
---|
290 | }
|
---|
291 |
|
---|
292 | // --------------------------------------------------------------------------
|
---|
293 | //
|
---|
294 | // The first time this function is called all branches are disabled.
|
---|
295 | // The given branch is enabled. By enabling only the branches you
|
---|
296 | // are processing you can speed up your calculation many times (up to
|
---|
297 | // a factor of 10 or 20)
|
---|
298 | //
|
---|
299 | void MReadTree::EnableBranch(const char *name)
|
---|
300 | {
|
---|
301 | if (fChain->GetListOfFiles()->GetEntries()==0)
|
---|
302 | {
|
---|
303 | *fLog << err << "Chain contains no file... Branch '";
|
---|
304 | *fLog << name << "' ignored." << endl;
|
---|
305 | return;
|
---|
306 | }
|
---|
307 |
|
---|
308 | EnableBranchChoosing();
|
---|
309 |
|
---|
310 | TNamed branch(name, "");
|
---|
311 | SetBranchStatus(&branch, kTRUE);
|
---|
312 | }
|
---|
313 |
|
---|
314 | // --------------------------------------------------------------------------
|
---|
315 | //
|
---|
316 | // Set branch status of branch name
|
---|
317 | //
|
---|
318 | void MReadTree::SetBranchStatus(const char *name, Bool_t status)
|
---|
319 | {
|
---|
320 | fChain->SetBranchStatus(name, status);
|
---|
321 |
|
---|
322 | *fLog << inf << (status ? "Enabled" : "Disabled");
|
---|
323 | *fLog << " subbranch '" << name << "'." << endl;
|
---|
324 | }
|
---|
325 |
|
---|
326 | // --------------------------------------------------------------------------
|
---|
327 | //
|
---|
328 | // Checks whether a branch with the given name exists in the chain
|
---|
329 | // and sets the branch status of this branch corresponding to status.
|
---|
330 | //
|
---|
331 | void MReadTree::SetBranchStatus(TObject *branch, Bool_t status)
|
---|
332 | {
|
---|
333 | //
|
---|
334 | // Get branch name
|
---|
335 | //
|
---|
336 | const char *name = branch->GetName();
|
---|
337 |
|
---|
338 | //
|
---|
339 | // Check whether this branch really exists
|
---|
340 | //
|
---|
341 | if (fChain->GetBranch(name))
|
---|
342 | SetBranchStatus(name, status);
|
---|
343 |
|
---|
344 | //
|
---|
345 | // Remove trailing '.' if one and try to enable the subbranch without
|
---|
346 | // the master branch name. This is to be compatible with older mars
|
---|
347 | // and camera files.
|
---|
348 | //
|
---|
349 | const char *dot = strrchr(name, '.');
|
---|
350 | if (!dot)
|
---|
351 | return;
|
---|
352 |
|
---|
353 | if (fChain->GetBranch(dot+1))
|
---|
354 | SetBranchStatus(dot+1, status);
|
---|
355 | }
|
---|
356 |
|
---|
357 | // --------------------------------------------------------------------------
|
---|
358 | //
|
---|
359 | // Set the status of all branches in the list to status.
|
---|
360 | //
|
---|
361 | void MReadTree::SetBranchStatus(const TList *list, Bool_t status)
|
---|
362 | {
|
---|
363 | //
|
---|
364 | // Loop over all subbranches in this master branch
|
---|
365 | //
|
---|
366 | TIter Next(list);
|
---|
367 |
|
---|
368 | TObject *obj;
|
---|
369 | while ((obj=Next()))
|
---|
370 | SetBranchStatus(obj, status);
|
---|
371 | }
|
---|
372 |
|
---|
373 | // --------------------------------------------------------------------------
|
---|
374 | //
|
---|
375 | // This is the implementation of the Auto Enabling Scheme.
|
---|
376 | // For more information see MTask::AddBranchToList.
|
---|
377 | // This function loops over all tasks and its filters in the tasklist
|
---|
378 | // and enables all branches which are requested by the tasks and its
|
---|
379 | // filters.
|
---|
380 | //
|
---|
381 | // To enable 'unknown' branches which are not in the branchlist of
|
---|
382 | // the tasks you can call EnableBranch
|
---|
383 | //
|
---|
384 | void MReadTree::EnableBranches(MParList *plist)
|
---|
385 | {
|
---|
386 | //
|
---|
387 | // check whether branch choosing must be switched on
|
---|
388 | //
|
---|
389 | EnableBranchChoosing();
|
---|
390 |
|
---|
391 | //
|
---|
392 | // request the tasklist from the parameter list.
|
---|
393 | // FIXME: Tasklist can have a different name
|
---|
394 | //
|
---|
395 | const MTaskList *tlist = (MTaskList*)plist->FindObject("MTaskList");
|
---|
396 | if (!tlist)
|
---|
397 | {
|
---|
398 | *fLog << warn << GetDescriptor() << "Cannot use auto enabeling scheme for branches. 'MTaskList' not found." << endl;
|
---|
399 | return;
|
---|
400 | }
|
---|
401 |
|
---|
402 | //
|
---|
403 | // This loop is not necessary. We could do it like in the commented
|
---|
404 | // loop below. But this loop makes sure, that we don't try to enable
|
---|
405 | // one branch several times. This would not harm, but we would get
|
---|
406 | // an output for each attempt. To have several outputs for one subbranch
|
---|
407 | // may confuse the user, this we don't want.
|
---|
408 | // This loop creates a new list of subbranches and for each branch
|
---|
409 | // which is added we check before whether it already exists or not.
|
---|
410 | //
|
---|
411 | TList list;
|
---|
412 |
|
---|
413 | MTask *task;
|
---|
414 | TIter NextTask(tlist->GetList());
|
---|
415 | while ((task=(MTask*)NextTask()))
|
---|
416 | {
|
---|
417 | TObject *obj;
|
---|
418 |
|
---|
419 | TIter NextTBranch(task->GetListOfBranches());
|
---|
420 | while ((obj=NextTBranch()))
|
---|
421 | if (!list.FindObject(obj->GetName()))
|
---|
422 | list.Add(obj);
|
---|
423 |
|
---|
424 | const MFilter *filter = task->GetFilter();
|
---|
425 |
|
---|
426 | if (!filter)
|
---|
427 | continue;
|
---|
428 |
|
---|
429 | TIter NextFBranch(filter->GetListOfBranches());
|
---|
430 | while ((obj=NextFBranch()))
|
---|
431 | if (!list.FindObject(obj->GetName()))
|
---|
432 | list.Add(obj);
|
---|
433 | }
|
---|
434 |
|
---|
435 | SetBranchStatus(&list, kTRUE);
|
---|
436 | /*
|
---|
437 | //
|
---|
438 | // Loop over all tasks iand its filters n the task list.
|
---|
439 | //
|
---|
440 | MTask *task;
|
---|
441 | TIter NextTask(tlist->GetList());
|
---|
442 | while ((task=(MTask*)NextTask()))
|
---|
443 | {
|
---|
444 | SetBranchStatus(task->GetListOfBranches(), kTRUE);
|
---|
445 |
|
---|
446 | const MFilter *filter = task->GetFilter();
|
---|
447 | if (!filter)
|
---|
448 | continue;
|
---|
449 |
|
---|
450 | SetBranchStatus(filter->GetListOfBranches(), kTRUE);
|
---|
451 |
|
---|
452 | }
|
---|
453 | */
|
---|
454 | }
|
---|
455 |
|
---|
456 | // --------------------------------------------------------------------------
|
---|
457 | //
|
---|
458 | // If the chain has been changed (by calling AddFile or using a file
|
---|
459 | // in the constructors argument) the number of entries is newly
|
---|
460 | // calculated from the files in the chain - this may take a while.
|
---|
461 | // The number of entries is returned.
|
---|
462 | //
|
---|
463 | UInt_t MReadTree::GetEntries()
|
---|
464 | {
|
---|
465 | if (TestBit(kChainWasChanged))
|
---|
466 | {
|
---|
467 | *fLog << inf << "Scanning chain... " << flush;
|
---|
468 | fNumEntries = (UInt_t)fChain->GetEntries();
|
---|
469 | *fLog << fNumEntries << " events found." << endl;
|
---|
470 | ResetBit(kChainWasChanged);
|
---|
471 | }
|
---|
472 | return fNumEntries;
|
---|
473 | }
|
---|
474 |
|
---|
475 | // --------------------------------------------------------------------------
|
---|
476 | //
|
---|
477 | // The disables all subbranches of the given master branch.
|
---|
478 | //
|
---|
479 | void MReadTree::DisableSubBranches(TBranch *branch)
|
---|
480 | {
|
---|
481 | //
|
---|
482 | // This is not necessary, it would work without. But the output
|
---|
483 | // may confuse the user...
|
---|
484 | //
|
---|
485 | if (fAutoEnable || fBranchChoosing)
|
---|
486 | return;
|
---|
487 |
|
---|
488 | SetBranchStatus(branch->GetListOfBranches(), kFALSE);
|
---|
489 | }
|
---|
490 |
|
---|
491 | // --------------------------------------------------------------------------
|
---|
492 | //
|
---|
493 | // The PreProcess loops (till now) over the branches in the given tree.
|
---|
494 | // It checks if the corresponding containers (containers with the same
|
---|
495 | // name than the branch name) are existing in the Parameter Container List.
|
---|
496 | // If not, a container of objec type 'branch-name' is created (everything
|
---|
497 | // after the last semicolon in the branch name is stripped). Only
|
---|
498 | // branches which don't have a veto (see VetoBranch) are enabled If the
|
---|
499 | // object isn't found in the root dictionary (a list of classes known by the
|
---|
500 | // root environment) the branch is skipped and an error message is printed
|
---|
501 | // out.
|
---|
502 | // If a selector is specified it is preprocessed after the
|
---|
503 | // MReadTree::PreProcess
|
---|
504 | //
|
---|
505 | Bool_t MReadTree::PreProcess(MParList *pList)
|
---|
506 | {
|
---|
507 | //
|
---|
508 | // Make sure, that all the following calls doesn't result in
|
---|
509 | // Notifications. This may be dangerous, because the notified
|
---|
510 | // tasks are not preprocessed.
|
---|
511 | //
|
---|
512 | fChain->SetNotify(NULL);
|
---|
513 |
|
---|
514 | //
|
---|
515 | // get number of events in this tree
|
---|
516 | //
|
---|
517 | if (!GetEntries())
|
---|
518 | {
|
---|
519 | *fLog << warn << dbginf << "No entries found in file(s)" << endl;
|
---|
520 | return kFALSE;
|
---|
521 | }
|
---|
522 |
|
---|
523 | //
|
---|
524 | // output logging information
|
---|
525 | //
|
---|
526 | *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
|
---|
527 |
|
---|
528 | //
|
---|
529 | // Get all branches of this tree and
|
---|
530 | // create the Iterator to loop over all branches
|
---|
531 | //
|
---|
532 | TIter Next(fChain->GetListOfBranches());
|
---|
533 | TBranch *branch=NULL;
|
---|
534 |
|
---|
535 | Int_t num=0;
|
---|
536 | //
|
---|
537 | // loop over all tasks for processing
|
---|
538 | //
|
---|
539 | while ( (branch=(TBranch*)Next()) )
|
---|
540 | {
|
---|
541 | //
|
---|
542 | // Get Name of Branch and Object
|
---|
543 | //
|
---|
544 | const char *bname = branch->GetName();
|
---|
545 |
|
---|
546 | TString oname(bname);
|
---|
547 | if (oname.EndsWith("."))
|
---|
548 | oname.Remove(oname.Length()-1);
|
---|
549 |
|
---|
550 | //
|
---|
551 | // Check if enabeling the branch is allowed
|
---|
552 | //
|
---|
553 | if (fVetoList->FindObject(oname))
|
---|
554 | {
|
---|
555 | *fLog << inf << "Master branch " << bname << " has veto... skipped." << endl;
|
---|
556 | DisableSubBranches(branch);
|
---|
557 | continue;
|
---|
558 | }
|
---|
559 |
|
---|
560 | //
|
---|
561 | // Create a pointer to the pointer to the object in which the
|
---|
562 | // branch data is stored. The pointers are stored in the TChain
|
---|
563 | // object and we get the pointers from there to delete it.
|
---|
564 | //
|
---|
565 | MParContainer **pcont= new MParContainer*;
|
---|
566 |
|
---|
567 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
|
---|
568 | const char *classname = oname;
|
---|
569 | #else
|
---|
570 | const char *classname = branch->GetClassName();
|
---|
571 | #endif
|
---|
572 |
|
---|
573 | //
|
---|
574 | // check if object is existing in the list
|
---|
575 | //
|
---|
576 | *pcont=pList->FindCreateObj(classname, oname);
|
---|
577 |
|
---|
578 | if (!*pcont)
|
---|
579 | {
|
---|
580 | //
|
---|
581 | // if class is not existing in the (root) environment
|
---|
582 | // we cannot proceed reading this branch
|
---|
583 | //
|
---|
584 | *fLog << warn << dbginf << "Warning: Class '" << classname;
|
---|
585 | *fLog << "' for " << oname << " not existing in dictionary. Branch skipped." << endl;
|
---|
586 | DisableSubBranches(branch);
|
---|
587 | continue;
|
---|
588 | }
|
---|
589 |
|
---|
590 | //
|
---|
591 | // Check whether a Pointer to a pointer already exists.
|
---|
592 | // If we created one already, delete it.
|
---|
593 | //
|
---|
594 | TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
|
---|
595 | if (element)
|
---|
596 | delete (MParContainer**)element->GetBaddress();
|
---|
597 |
|
---|
598 | //
|
---|
599 | // here pcont is a pointer the to container in which the data from
|
---|
600 | // the actual branch should be stored - enable branch.
|
---|
601 | //
|
---|
602 | fChain->SetBranchAddress(bname, pcont);
|
---|
603 |
|
---|
604 | *fLog << inf << "Master branch address " << bname << " [";
|
---|
605 | *fLog << classname << "] setup for reading." << endl;
|
---|
606 |
|
---|
607 | //*fLog << "Branch " << bname << " autodel: " << (int)branch->IsAutoDelete() << endl;
|
---|
608 | //branch->SetAutoDelete();
|
---|
609 |
|
---|
610 | num++;
|
---|
611 | }
|
---|
612 |
|
---|
613 | *fLog << inf << GetDescriptor() << " setup " << num << " master branches addresses." << endl;
|
---|
614 |
|
---|
615 | //
|
---|
616 | // If auto enabling scheme isn't disabled, do auto enabling
|
---|
617 | //
|
---|
618 | if (fAutoEnable)
|
---|
619 | EnableBranches(pList);
|
---|
620 |
|
---|
621 | //
|
---|
622 | // Now we can start notifying. Reset tree makes sure, that TChain thinks
|
---|
623 | // that the correct file is not yet initialized and reinitilizes it
|
---|
624 | // as soon as the first event is read. This is necessary to call
|
---|
625 | // the notifiers when the first event is read, but after the
|
---|
626 | // PreProcess-function.
|
---|
627 | //
|
---|
628 | fChain->ResetTree();
|
---|
629 | fChain->SetNotify(this);
|
---|
630 |
|
---|
631 | return GetSelector() ? GetSelector()->CallPreProcess(pList) : kTRUE;
|
---|
632 | }
|
---|
633 |
|
---|
634 | // --------------------------------------------------------------------------
|
---|
635 | //
|
---|
636 | // Set the ready to save flag of all containers which branchaddresses are
|
---|
637 | // set for. This is necessary to copy data.
|
---|
638 | //
|
---|
639 | void MReadTree::SetReadyToSave(Bool_t flag)
|
---|
640 | {
|
---|
641 | TIter Next(fChain->GetStatus());
|
---|
642 |
|
---|
643 | TChainElement *element = NULL;
|
---|
644 | while ((element=(TChainElement*)Next()))
|
---|
645 | {
|
---|
646 | //
|
---|
647 | // Check whether the branch is enabled
|
---|
648 | //
|
---|
649 | if (!element->GetStatus())
|
---|
650 | continue;
|
---|
651 |
|
---|
652 | //
|
---|
653 | // Get the pointer to the pointer of the corresponding container
|
---|
654 | //
|
---|
655 | MParContainer **pcont = (MParContainer**)element->GetBaddress();
|
---|
656 |
|
---|
657 | //
|
---|
658 | // Check whether the pointer is not NULL
|
---|
659 | //
|
---|
660 | if (!pcont || !*pcont)
|
---|
661 | continue;
|
---|
662 |
|
---|
663 | //
|
---|
664 | // Set the ready to save status of the container.
|
---|
665 | //
|
---|
666 | (*pcont)->SetReadyToSave(flag);
|
---|
667 | }
|
---|
668 |
|
---|
669 | //
|
---|
670 | // Set the ready to save status of this task (used?), too
|
---|
671 | //
|
---|
672 | MTask::SetReadyToSave(flag);
|
---|
673 | }
|
---|
674 |
|
---|
675 | // --------------------------------------------------------------------------
|
---|
676 | //
|
---|
677 | // The Process-function reads one event from the tree (this contains all
|
---|
678 | // enabled branches) and increases the position in the file by one event.
|
---|
679 | // (Remark: The position can also be set by some member functions
|
---|
680 | // If the end of the file is reached the Eventloop is stopped.
|
---|
681 | // In case an event selector is given its value is checked before
|
---|
682 | // reading the event. If it returns kAFLSE the event is skipped.
|
---|
683 | //
|
---|
684 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
|
---|
685 | #include "MRawEvtData.h"
|
---|
686 | #endif
|
---|
687 | Bool_t MReadTree::Process()
|
---|
688 | {
|
---|
689 | //
|
---|
690 | // This is necessary due to a bug in TChain::LoadTree in root.
|
---|
691 | // will be fixed in 3.03
|
---|
692 | //
|
---|
693 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
|
---|
694 | if (fNumEntry >= GetEntries())
|
---|
695 | return kFALSE;
|
---|
696 | #endif
|
---|
697 |
|
---|
698 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
|
---|
699 | //
|
---|
700 | // This fixes 99.9% of a memory leak using a root version prior
|
---|
701 | // to 3.02/??
|
---|
702 | //
|
---|
703 | TChainElement *element=NULL;
|
---|
704 | TIter Next(fChain->GetStatus());
|
---|
705 | while ((element=(TChainElement*)Next()))
|
---|
706 | {
|
---|
707 | MParContainer **c = (MParContainer**)element->GetBaddress();
|
---|
708 | if (!c) continue;
|
---|
709 | if ((*c)->InheritsFrom(MRawEvtData::Class()))
|
---|
710 | ((MRawEvtData*)(*c))->DeletePixels(kFALSE);
|
---|
711 |
|
---|
712 | }
|
---|
713 | #endif
|
---|
714 |
|
---|
715 | if (GetSelector())
|
---|
716 | {
|
---|
717 | //
|
---|
718 | // Make sure selector is processed
|
---|
719 | //
|
---|
720 | if (!GetSelector()->CallProcess())
|
---|
721 | {
|
---|
722 | *fLog << err << dbginf << "Processing Selector failed." << endl;
|
---|
723 | return kFALSE;
|
---|
724 | }
|
---|
725 |
|
---|
726 | //
|
---|
727 | // Skip Event
|
---|
728 | //
|
---|
729 | if (!GetSelector()->IsExpressionTrue())
|
---|
730 | {
|
---|
731 | fNumEntry++;
|
---|
732 | return kCONTINUE;
|
---|
733 | }
|
---|
734 | }
|
---|
735 |
|
---|
736 | const Bool_t rc = fChain->GetEntry(fNumEntry++) != 0;
|
---|
737 |
|
---|
738 | if (rc)
|
---|
739 | SetReadyToSave();
|
---|
740 |
|
---|
741 | return rc;
|
---|
742 | }
|
---|
743 |
|
---|
744 | // --------------------------------------------------------------------------
|
---|
745 | //
|
---|
746 | // If a selector is given the selector is post processed
|
---|
747 | //
|
---|
748 | Bool_t MReadTree::PostProcess()
|
---|
749 | {
|
---|
750 | return GetSelector() ? GetSelector()->CallPostProcess() : kTRUE;
|
---|
751 | }
|
---|
752 |
|
---|
753 | // --------------------------------------------------------------------------
|
---|
754 | //
|
---|
755 | // Get the Event with the current EventNumber fNumEntry
|
---|
756 | //
|
---|
757 | Bool_t MReadTree::GetEvent()
|
---|
758 | {
|
---|
759 | Bool_t rc = fChain->GetEntry(fNumEntry) != 0;
|
---|
760 |
|
---|
761 | if (rc)
|
---|
762 | SetReadyToSave();
|
---|
763 |
|
---|
764 | return rc;
|
---|
765 | }
|
---|
766 |
|
---|
767 | // --------------------------------------------------------------------------
|
---|
768 | //
|
---|
769 | // Decrease the number of the event which is read by Process() next
|
---|
770 | // by one or more
|
---|
771 | //
|
---|
772 | Bool_t MReadTree::DecEventNum(UInt_t dec)
|
---|
773 | {
|
---|
774 | if (fNumEntry-dec >= GetEntries())
|
---|
775 | {
|
---|
776 | *fLog << warn << GetDescriptor() << ": DecEventNum, WARNING - Event " << fNumEntry << "-";
|
---|
777 | *fLog << dec << "=" << (Int_t)fNumEntry-dec << " out of Range." << endl;
|
---|
778 | return kFALSE;
|
---|
779 | }
|
---|
780 |
|
---|
781 | fNumEntry -= dec;
|
---|
782 | return kTRUE;
|
---|
783 | }
|
---|
784 |
|
---|
785 | // --------------------------------------------------------------------------
|
---|
786 | //
|
---|
787 | // Increase the number of the event which is read by Process() next
|
---|
788 | // by one or more
|
---|
789 | //
|
---|
790 | Bool_t MReadTree::IncEventNum(UInt_t inc)
|
---|
791 | {
|
---|
792 | if (fNumEntry+inc >= GetEntries())
|
---|
793 | {
|
---|
794 | *fLog << warn << GetDescriptor() << ": IncEventNum, WARNING - Event " << fNumEntry << "+";
|
---|
795 | *fLog << inc << "=" << (Int_t)fNumEntry+inc << " out of Range." << endl;
|
---|
796 | return kFALSE;
|
---|
797 | }
|
---|
798 |
|
---|
799 | fNumEntry += inc;
|
---|
800 | return kTRUE;
|
---|
801 | }
|
---|
802 |
|
---|
803 | // --------------------------------------------------------------------------
|
---|
804 | //
|
---|
805 | // This function makes Process() read event number nr next
|
---|
806 | //
|
---|
807 | // Remark: You can use this function after instatiating you MReadTree-object
|
---|
808 | // to set the event number from which you want to start reading.
|
---|
809 | //
|
---|
810 | Bool_t MReadTree::SetEventNum(UInt_t nr)
|
---|
811 | {
|
---|
812 | if (nr >= GetEntries())
|
---|
813 | {
|
---|
814 | *fLog << warn << GetDescriptor() << ": SetEventNum, WARNING - " << nr << " out of Range." << endl;
|
---|
815 | return kFALSE;
|
---|
816 | }
|
---|
817 |
|
---|
818 | fNumEntry = nr;
|
---|
819 | return kTRUE;
|
---|
820 | }
|
---|
821 |
|
---|
822 | // --------------------------------------------------------------------------
|
---|
823 | //
|
---|
824 | // For the branch with the given name:
|
---|
825 | // 1) no object is automatically created
|
---|
826 | // 2) the branch address for this branch is not set
|
---|
827 | // (because we lack the object, see 1)
|
---|
828 | // 3) The whole branch (exactly: all its subbranches) are disabled
|
---|
829 | // this means are not read in memory by TTree:GetEntry
|
---|
830 | //
|
---|
831 | void MReadTree::VetoBranch(const char *name)
|
---|
832 | {
|
---|
833 | fVetoList->Add(new TNamed(name, ""));
|
---|
834 | }
|
---|
835 |
|
---|
836 | // --------------------------------------------------------------------------
|
---|
837 | //
|
---|
838 | // Return the name of the file we are actually reading from.
|
---|
839 | //
|
---|
840 | TString MReadTree::GetFileName() const
|
---|
841 | {
|
---|
842 | const TFile *file = fChain->GetFile();
|
---|
843 |
|
---|
844 | if (!file)
|
---|
845 | return TString("<unknown>");
|
---|
846 |
|
---|
847 | TString name(file->GetName());
|
---|
848 | name.Remove(0, name.Last('/')+1);
|
---|
849 | return name;
|
---|
850 | }
|
---|
851 |
|
---|
852 | // --------------------------------------------------------------------------
|
---|
853 | //
|
---|
854 | // Return the number of the file in the chain, -1 in case of an error
|
---|
855 | //
|
---|
856 | Int_t MReadTree::GetFileIndex() const
|
---|
857 | {
|
---|
858 | return fChain->GetTreeNumber();
|
---|
859 | /*
|
---|
860 | const TString filename = fChain->GetFile()->GetName();
|
---|
861 |
|
---|
862 | int i=0;
|
---|
863 | TObject *file = NULL;
|
---|
864 |
|
---|
865 | TIter Next(fChain->GetListOfFiles());
|
---|
866 | while ((file=Next()))
|
---|
867 | {
|
---|
868 | if (filename==gSystem->ExpandPathName(file->GetTitle()))
|
---|
869 | return i;
|
---|
870 | i++;
|
---|
871 | }
|
---|
872 | return -1;
|
---|
873 | */
|
---|
874 | }
|
---|
875 |
|
---|
876 | // --------------------------------------------------------------------------
|
---|
877 | //
|
---|
878 | // This schedules a TObject which Notify(9 function is called in case
|
---|
879 | // of MReadTree (TChain) switches from one file in the chain to another
|
---|
880 | // one.
|
---|
881 | //
|
---|
882 | void MReadTree::AddNotify(TObject *obj)
|
---|
883 | {
|
---|
884 | fNotify->Add(obj);
|
---|
885 | }
|
---|
886 |
|
---|
887 | void MReadTree::Print(Option_t *o) const
|
---|
888 | {
|
---|
889 | *fLog << all << GetDescriptor() << dec << endl;
|
---|
890 | *fLog << setfill('-') << setw(strlen(GetDescriptor())) << "" << endl;
|
---|
891 | *fLog << " Files [Tree]:" << endl;
|
---|
892 |
|
---|
893 | int i = 0;
|
---|
894 | TIter Next(fChain->GetListOfFiles());
|
---|
895 | TObject *obj = NULL;
|
---|
896 | while ((obj=Next()))
|
---|
897 | *fLog << " " << i++ << ") " << obj->GetTitle() << " [" << obj->GetName() << "]" << endl;
|
---|
898 |
|
---|
899 | *fLog << " Total Number of Entries: " << fNumEntries << endl;
|
---|
900 | *fLog << " Next Entry to read: " << fNumEntry << endl;
|
---|
901 | }
|
---|
902 |
|
---|
903 | // --------------------------------------------------------------------------
|
---|
904 | //
|
---|
905 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
---|
906 | // to a macro. In the original root implementation it is used to write
|
---|
907 | // gui elements to a macro-file.
|
---|
908 | //
|
---|
909 | void MReadTree::StreamPrimitive(ofstream &out) const
|
---|
910 | {
|
---|
911 | out << " " << ClassName() << " " << GetUniqueName() << "(\"";
|
---|
912 | out << fChain->GetName() << "\", \"" << fName << "\", \"" << fTitle << "\");" << endl;
|
---|
913 |
|
---|
914 | TIter Next(fChain->GetListOfFiles());
|
---|
915 | TObject *obj = NULL;
|
---|
916 | while ((obj=Next()))
|
---|
917 | out << " " << GetUniqueName() << ".AddFile(\"" << obj->GetTitle() << "\");" << endl;
|
---|
918 |
|
---|
919 | if (!fAutoEnable)
|
---|
920 | out << " " << GetUniqueName() << ".DisableAutoScheme();" << endl;
|
---|
921 |
|
---|
922 | if (fNumEntry!=0)
|
---|
923 | out << " " << GetUniqueName() << ".SetEventNum(" << fNumEntry << ");" << endl;
|
---|
924 |
|
---|
925 |
|
---|
926 | }
|
---|