source: trunk/MagicSoft/Mars/mfileio/MReadTree.cc@ 9034

Last change on this file since 9034 was 9034, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 36.2 KB
Line 
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-2008
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// ToDo:
52// -----
53// - Auto Scheme and Branch choosing doesn't work for memory trees
54//
55/////////////////////////////////////////////////////////////////////////////
56#include "MReadTree.h"
57
58#include <fstream>
59
60#include <TFile.h> // TFile::GetName
61#include <TSystem.h> // gSystem->ExpandPath
62#include <TLeafElement.h>
63#include <TChainElement.h>
64#include <TFriendElement.h>
65#include <TOrdCollection.h>
66
67#include "MChain.h"
68#include "MFilter.h"
69#include "MParList.h"
70#include "MTaskList.h"
71#include "MParameters.h"
72#include "MStatusDisplay.h"
73
74#include "MLog.h"
75#include "MLogManip.h"
76
77ClassImp(MReadTree);
78
79using namespace std;
80
81// --------------------------------------------------------------------------
82//
83// Default constructor. Use this constructor (ONLY) if you want to
84// access a memory tree (a TTree not stored in a file) created by
85// MWriteRootFile or manually.
86//
87MReadTree::MReadTree(TTree *tree)
88 : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
89{
90 fName = "MRead";
91 fTitle = "Task to loop over all events in one single tree";
92
93 fVetoList = new TList;
94 fVetoList->SetOwner();
95
96 fNotify = new TList;
97
98 fChain = NULL;
99
100 fTree = tree;
101 SetBit(kChainWasChanged);
102
103 if (fTree)
104 fAutoEnable = kFALSE;
105 /*
106 if (!fTree)
107 return;
108
109 TIter Next(fTree->GetListOfBranches());
110 TBranch *b=0;
111 while ((b=(TBranch*)Next()))
112 b->ResetAddress();
113 */
114}
115
116// --------------------------------------------------------------------------
117//
118// Constructor. It creates an TChain instance which represents the
119// the Tree you want to read and adds the given file (if you gave one).
120// More files can be added using MReadTree::AddFile.
121// Also an empty veto list is created. This list is used if you want to
122// veto (disable or "don't enable") a branch in the tree, it vetos also
123// the creation of the corresponding object.
124// An empty list of TObjects are also created. This objects are called
125// at any time the TChain starts to read from another file.
126//
127MReadTree::MReadTree(const char *tname, const char *fname,
128 const char *name, const char *title)
129 : fNumEntry(0), fNumEntries(0), fBranchChoosing(kFALSE), fAutoEnable(kTRUE)
130{
131 fName = name ? name : "MRead";
132 fTitle = title ? title : "Task to loop over all events in one single tree";
133
134 fVetoList = new TList;
135 fVetoList->SetOwner();
136
137 fNotify = new TList;
138
139 //
140 // open the input stream
141 //
142 fChain = new MChain(tname);
143 fTree = fChain;
144
145 // root 3.02:
146 // In TChain::Addfile remove the limitation that the file name must contain
147 // the string ".root". ".root" is necessary only in case one wants to specify
148 // a Tree in a subdirectory of a Root file with eg, the format:
149
150 if (fname)
151 AddFile(fname);
152}
153
154Bool_t MReadTree::IsOwned(const TChainElement &e) const
155{
156 // We cannot be the owner, because it is a basic type like Int, etc.
157 if (!e.GetBaddressIsPtr())
158 return kFALSE;
159
160 // We cannot be the owener, because there is no class
161 // we could have created
162 if (TString(e.GetBaddressClassName()).IsNull())
163 return kFALSE;
164
165 // Let's hope the best that we are the owner
166 return kTRUE;
167}
168
169// --------------------------------------------------------------------------
170//
171// Destructor. It deletes the TChain and veto list object
172//
173MReadTree::~MReadTree()
174{
175 //
176 // Delete all the pointers to pointers to the objects where the
177 // branche data gets stored. FIXME: When PreProcessed twice this
178 // creates a memory leak!
179 //
180 if (fChain) // FIXME: MEMORY LEAK for fTree!=0
181 {
182 TIter Next(fChain->GetStatus());
183
184 // Pointers which we delete must be
185 // 1) Really pointers
186 // 2) Have a class name (belong to a loaded class)
187 TChainElement *element = NULL;
188 while ((element=(TChainElement*)Next()))
189 if (IsOwned(*element))
190 delete (void**)element->GetBaddress();
191
192 //
193 // Delete the chain and the veto list
194 //
195 delete fChain;
196 }
197
198 /* This is AND MUST be done in PostProcess. See there for more details
199 else
200 {
201 TIter Next(fTree->GetListOfBranches());
202
203 TBranch *element = NULL;
204 while ((element=(TBranch*)Next()))
205 if (element->GetAddress())
206 delete (MParContainer**)element->GetAddress();
207 }
208 */
209
210 delete fNotify;
211 delete fVetoList;
212}
213
214Byte_t MReadTree::IsFileValid(const char *name)
215{
216 ifstream fin(name);
217 if (!fin)
218 return 0;
219
220 Char_t c[4];
221 fin.read(c, 4);
222 if (!fin)
223 return 0;
224
225 return c[0]=='r'&& c[1]=='o' && c[2]=='o' && c[3]=='t' ? 1 : 0;
226}
227
228// --------------------------------------------------------------------------
229//
230// This check whether all branches in the tree have the same size. If
231// this is not the case the events in the different branches cannot
232// be ordered correctly.
233//
234Bool_t MReadTree::CheckBranchSize()
235{
236 TArrayI entries(fTree->GetListOfBranches()->GetSize());
237 Int_t num=0;
238
239 TIter Next(fTree->GetListOfBranches());
240
241 TBranch *element = NULL;
242 while ((element=(TBranch*)Next()))
243 entries[num++] = (Int_t)element->GetEntries();
244
245 // Check the number of entries of the branches pair-wise
246 for (int i=0; i<num; i++)
247 for (int j=i; j<num; j++)
248 {
249 if (entries[i]==entries[j])
250 continue;
251
252 *fLog << err << "ERROR - File corruption detected:" << endl;
253 *fLog << " Due to several circumstances (such at a bug in MReadTree or wrong" << endl;
254 *fLog << " usage of the file UPDATE mode) you may have produced a file in which" << endl;
255 *fLog << " at least two branches in the same tree (" << fTree->GetName() << ") have different" << endl;
256 *fLog << " number of entries. Sorry, but this file (" << GetFileName() << ")" << endl;
257 *fLog << " is unusable." << endl;
258 return kFALSE;
259 }
260
261 return kTRUE;
262}
263
264// --------------------------------------------------------------------------
265//
266// If the owner flag is set all TObjects which are scheduled via
267// AddNotify are deleted by the destructor of MReadTree
268//
269void MReadTree::SetOwner(Bool_t flag)
270{
271 flag ? fNotify->SetBit(kIsOwner) : fNotify->ResetBit(kIsOwner);
272}
273
274// --------------------------------------------------------------------------
275//
276// This function is called each time MReadTree changes the file to read
277// from. It calls all TObject::Notify() functions which are scheduled
278// via AddNotify.
279//
280// For unknown reasons root stores the SetAutoDelete-Flag in
281// a branch having a custom streamer (eg. MRawEvtData).
282// It is not allowed that a container which is in the parameter
283// list gets a new address. Therefor we reset all the autodel flags.
284//
285// MAKE SURE THAT ALL YOUR CUSTOM STREAMERS TAKE CARE OF ALL MEMORY
286//
287Bool_t MReadTree::Notify()
288{
289 //
290 // Do a consistency check for all branches
291 //
292 if (!CheckBranchSize())
293 return kFALSE;
294
295 *fLog << inf << GetDescriptor() << ": Next file #" << GetFileIndex();
296 *fLog << " '" << GetFileName() << "' (next evt #";
297 *fLog << GetNumEntry()-1 << ")" << endl;
298
299 if (fDisplay)
300 {
301 TString txt = GetFileName();
302 txt += " @ ";
303 txt += GetNumEntry()-1;
304 fDisplay->SetStatusLine2(txt);
305 }
306
307 //
308 // For unknown reasons root stores the SetAutoDelete-Flag in
309 // a branch having a custom streamer (eg. MRawEvtData).
310 // It is not allowed that a container which is in the parameter
311 // list gets a new address. Therefor we reset all the autodel flags.
312 //
313 // MAKE SURE THAT ALL YOUR CUSTOM STREAMERS TAKE CARE OF ALL MEMORY
314 //
315 TIter NextB(fTree->GetListOfBranches());
316 TBranch *b=0;
317 while ((b=(TBranch*)NextB()))
318 if (b->IsAutoDelete())
319 {
320 *fLog << warn << "Branch " << b->GetName() << "->IsAutoDelete() set... resetting." << endl;
321 b->SetAutoDelete(kFALSE);
322 }
323
324 if (!fNotify)
325 return kTRUE;
326
327 TIter NextN(fNotify);
328 TObject *o=NULL;
329 while ((o=NextN()))
330 if (!o->Notify())
331 {
332 *fLog << err << "Calling Notify() for object " << o->GetName() << " failed... abort." << endl;
333 return kFALSE;
334 }
335
336 return kTRUE;
337}
338
339// --------------------------------------------------------------------------
340//
341// If you want to read the given tree over several files you must add
342// the files here before PreProcess is called. Be careful: If the tree
343// doesn't have the same contents (branches) it may confuse your
344// program (trees which are are not existing in later files are not read
345// anymore, tree wich are not existing in the first file are never read)
346//
347// Name may use the wildcarding notation, eg "xxx*.root" means all files
348// starting with xxx in the current file system directory.
349//
350// AddFile returns the number of files added to the chain.
351//
352// For more information see TChain::Add
353//
354Int_t MReadTree::AddFile(const char *fname, Int_t entries)
355{
356 if (!fChain)
357 {
358 *fLog << err << "MReadTree::AddFile - ERROR: You cannot add a file, because MReadTree" << endl;
359 *fLog << " is supposed to read its tree from memory or its default or its" << endl;
360 *fLog << " default constructor was called (no tree name was given)." << endl;
361 return 0;
362 }
363
364 TString newname(fname); // char-array must overcome comming block
365 gSystem->ExpandPathName(newname);
366
367 //
368 // FIXME! A check is missing whether the file already exists or not.
369 //
370 const Int_t numfiles = fChain->Add(newname, entries<0?TChain::kBigNumber:entries);
371
372 //TIter Next(fChain->GetListOfFriends());
373 //TFriendElement *el = 0;
374 //while ((el=(TFriendElement*)Next()))
375 // static_cast<TChain*>(el->GetTree())->Add(newname, entries<0?TChain::kBigNumber:entries);
376
377 if (numfiles>0)
378 {
379 SetBit(kChainWasChanged);
380 if (numfiles>1)
381 *fLog << inf3 << GetDescriptor() << ": Added " << fname << " <" << numfiles << ">" << endl;
382 }
383 else
384 *fLog << warn << "WARNING: '" << newname << "' not added to " << GetDescriptor() << endl;
385
386 return numfiles;
387}
388
389Bool_t MReadTree::AddFriend(TChain &c)
390{
391// if (!fChain)
392// {
393// *fLog << err << "MReadTree::AddFriend - ERROR: You cannot add a friend, because MReadTree" << endl;
394// *fLog << " is supposed to read its tree from memory or its default or its" << endl;
395// *fLog << " default constructor was called (no tree name was given)." << endl;
396// return 0;
397// }
398
399 return fChain->AddFriend(&c, "")!=0;
400}
401
402/*
403 // --------------------------------------------------------------------------
404 //
405 //
406 Int_t MReadTree::AddFile(const TChainElement &obj)
407 {
408 return AddFile(obj.GetTitle(), obj.GetEntries());
409 }
410*/
411
412// --------------------------------------------------------------------------
413//
414// Adds all files from another MReadTree to this instance
415//
416// Returns the number of file which were added
417//
418Int_t MReadTree::AddFiles(const MReadTree &read)
419{
420 if (!fChain)
421 {
422 *fLog << err << "MReadTree::AddFiles - ERROR: You cannot add a file, because MReadTree" << endl;
423 *fLog << " handles a memory based tree or its default" << endl;
424 *fLog << " constructor was called." << endl;
425 return 0;
426 }
427
428 const Int_t rc = fChain->Add(read.fChain);
429
430 if (rc>0)
431 SetBit(kChainWasChanged);
432
433 /*
434 Int_t rc = 0;
435
436 TIter Next(read.fChain->GetListOfFiles());
437 TObject *obj = NULL;
438 while ((obj=Next()))
439 rc += AddFile(*(TChainElement*)obj);
440 */
441
442 return rc;
443}
444
445// --------------------------------------------------------------------------
446//
447// Sort the files by their file-names
448//
449void MReadTree::SortFiles()
450{
451 if (fChain)
452 fChain->GetListOfFiles()->Sort();
453}
454
455// --------------------------------------------------------------------------
456//
457// This function is called if Branch choosing method should get enabled.
458// Branch choosing means, that only the enabled branches are read into
459// memory. To use an enableing scheme we have to disable all branches first.
460// This is done, if this function is called the first time.
461//
462void MReadTree::EnableBranchChoosing()
463{
464 if (fBranchChoosing)
465 return;
466
467 *fLog << inf << GetDescriptor() << ": Branch choosing enabled (only enabled branches are read)." << endl;
468 fTree->SetBranchStatus("*", kFALSE); // *CHANGED-fChain-to-fTree*
469 fBranchChoosing = kTRUE;
470}
471
472// --------------------------------------------------------------------------
473//
474// The first time this function is called all branches are disabled.
475// The given branch is enabled. By enabling only the branches you
476// are processing you can speed up your calculation many times (up to
477// a factor of 10 or 20)
478//
479void MReadTree::EnableBranch(const char *name)
480{
481 if (!fChain)
482 {
483 *fLog << warn << "MReadTree::EnableBranch - WARNING: EnableBranch doesn't work with memory based trees... ignored." << endl;
484 return;
485 }
486
487 if (fChain->GetListOfFiles()->GetEntries()==0)
488 {
489 *fLog << err << "Chain contains no file... Branch '";
490 *fLog << name << "' ignored." << endl;
491 return;
492 }
493
494 EnableBranchChoosing();
495
496 TNamed branch(name, "");
497 SetBranchStatus(&branch, kTRUE);
498}
499
500// --------------------------------------------------------------------------
501//
502// Set branch status of branch name
503//
504void MReadTree::SetBranchStatus(const char *name, Bool_t status)
505{
506 fTree->SetBranchStatus(name, status); // *CHANGED-fChain-to-fTree*
507
508 *fLog << inf << (status ? "Enabled" : "Disabled");
509 *fLog << " subbranch '" << name << "'." << endl;
510}
511
512// --------------------------------------------------------------------------
513//
514// Checks whether a branch with the given name exists in the chain
515// and sets the branch status of this branch corresponding to status.
516//
517void MReadTree::SetBranchStatus(TObject *branch, Bool_t status)
518{
519 //
520 // Get branch name
521 //
522 const char *name = branch->GetName();
523
524 //
525 // Check whether this branch really exists
526 //
527 TString bn(name);
528 if (bn.EndsWith("*"))
529 bn.Remove(bn.Length()-1);
530
531 if (fTree->GetBranch(bn)) // *CHANGED-fChain-to-fTree*
532 SetBranchStatus(name, status);
533
534 //
535 // Remove trailing '.' if one and try to enable the subbranch without
536 // the master branch name. This is to be compatible with older mars
537 // and camera files.
538 //
539 const char *dot = strrchr(name, '.');
540 if (!dot)
541 return;
542
543 if (fTree->GetBranch(dot+1)) // *CHANGED-fChain-to-fTree*
544 SetBranchStatus(dot+1, status);
545}
546
547// --------------------------------------------------------------------------
548//
549// Set the status of all branches in the list to status.
550//
551void MReadTree::SetBranchStatus(const TList *list, Bool_t status)
552{
553 //
554 // Loop over all subbranches in this master branch
555 //
556 TIter Next(list);
557
558 TObject *obj;
559 while ((obj=Next()))
560 SetBranchStatus(obj, status);
561}
562
563// --------------------------------------------------------------------------
564//
565// This is the implementation of the Auto Enabling Scheme.
566// For more information see MTask::AddBranchToList.
567// This function loops over all tasks and its filters in the tasklist
568// and enables all branches which are requested by the tasks and its
569// filters.
570//
571// To enable 'unknown' branches which are not in the branchlist of
572// the tasks you can call EnableBranch
573//
574void MReadTree::EnableBranches(MParList *plist)
575{
576 //
577 // check whether branch choosing must be switched on
578 //
579 EnableBranchChoosing();
580
581 //
582 // request the tasklist from the parameter list.
583 // FIXME: Tasklist can have a different name
584 //
585 const MTaskList *tlist = (MTaskList*)plist->FindObject("MTaskList");
586 if (!tlist)
587 {
588 *fLog << warn << GetDescriptor() << "Cannot use auto enabeling scheme for branches. 'MTaskList' not found." << endl;
589 return;
590 }
591
592 //
593 // This loop is not necessary. We could do it like in the commented
594 // loop below. But this loop makes sure, that we don't try to enable
595 // one branch several times. This would not harm, but we would get
596 // an output for each attempt. To have several outputs for one subbranch
597 // may confuse the user, this we don't want.
598 // This loop creates a new list of subbranches and for each branch
599 // which is added we check before whether it already exists or not.
600 //
601 TList list;
602
603 MTask *task;
604 TIter NextTask(tlist->GetList());
605 while ((task=(MTask*)NextTask()))
606 {
607 TObject *obj;
608
609 TIter NextTBranch(task->GetListOfBranches());
610 while ((obj=NextTBranch()))
611 if (!list.FindObject(obj->GetName()))
612 list.Add(obj);
613
614 const MFilter *filter = task->GetFilter();
615
616 if (!filter)
617 continue;
618
619 TIter NextFBranch(filter->GetListOfBranches());
620 while ((obj=NextFBranch()))
621 if (!list.FindObject(obj->GetName()))
622 list.Add(obj);
623 }
624
625 SetBranchStatus(&list, kTRUE);
626/*
627 //
628 // Loop over all tasks iand its filters n the task list.
629 //
630 MTask *task;
631 TIter NextTask(tlist->GetList());
632 while ((task=(MTask*)NextTask()))
633 {
634 SetBranchStatus(task->GetListOfBranches(), kTRUE);
635
636 const MFilter *filter = task->GetFilter();
637 if (!filter)
638 continue;
639
640 SetBranchStatus(filter->GetListOfBranches(), kTRUE);
641
642 }
643*/
644}
645
646// --------------------------------------------------------------------------
647//
648// If the chain has been changed (by calling AddFile or using a file
649// in the constructors argument) the number of entries is newly
650// calculated from the files in the chain - this may take a while.
651// The number of entries is returned.
652//
653UInt_t MReadTree::GetEntries()
654{
655 if (TestBit(kChainWasChanged))
656 {
657 // *CHANGED-fChain-to-fTree*
658 *fLog << inf << "Scanning chain " << fTree->GetName() << "... " << flush;
659 fNumEntries = (UInt_t)fTree->GetEntries();
660 *fLog << fNumEntries << " events found." << endl;
661 ResetBit(kChainWasChanged);
662 }
663 return fNumEntries==TChain::kBigNumber ? 0 : fNumEntries;
664}
665
666// --------------------------------------------------------------------------
667//
668// The disables all subbranches of the given master branch.
669//
670void MReadTree::DisableSubBranches(TBranch *branch)
671{
672 //
673 // This is not necessary, it would work without. But the output
674 // may confuse the user...
675 //
676 if (fAutoEnable || fBranchChoosing)
677 return;
678
679 SetBranchStatus(branch->GetListOfBranches(), kFALSE);
680}
681
682// --------------------------------------------------------------------------
683//
684// The PreProcess loops (till now) over the branches in the given tree.
685// It checks if the corresponding containers (containers with the same
686// name than the branch name) are existing in the Parameter Container List.
687// If not, a container of objec type 'branch-name' is created (everything
688// after the last semicolon in the branch name is stripped). Only
689// branches which don't have a veto (see VetoBranch) are enabled If the
690// object isn't found in the root dictionary (a list of classes known by the
691// root environment) the branch is skipped and an error message is printed
692// out.
693// If a selector is specified it is preprocessed after the
694// MReadTree::PreProcess
695//
696Int_t MReadTree::PreProcess(MParList *pList)
697{
698 fTaskList = (MTaskList*)pList->FindObject("MTaskList");
699 if (!fTaskList)
700 *fLog << warn << "WARNING - Standard tasklist MTaskList not found... ignoring Stream-ID." << endl;
701
702 if (!fTree || !fChain)
703 {
704 *fLog << err << "ERROR - Something went terribly wrong!" << endl;
705 *fLog << " Maybe you called the default constructor?" << endl;
706 *fLog << " Did you forget to give a tree name in the constructor?" << endl;
707 return kFALSE;
708 }
709
710 //
711 // Make sure, that all the following calls doesn't result in
712 // Notifications. This may be dangerous, because the notified
713 // tasks are not preprocessed.
714 //
715 fTree->SetNotify(NULL); //*CHANGED-fChain-to-fTree*
716
717 //
718 // It seems, that TFile and TTree are not completely independant if
719 // many times the same file is opened (MReadReports) and some of
720 // the files in the chains don't have one tree. TChain::fTreeNumber
721 // is already set before LoadTree from GetFile is called and
722 // it crashes. ResetTree makes sure, that the tree number is -1
723 //
724 if (fChain) // *CHANGED-fChain-to-fTree*
725 fChain->ResetTree();
726 // Maybe this would be enough, but the above might be safer...
727 //if (fChain->GetTreeNumber()==0)
728 // fChain->ResetTree();
729
730 //
731 // check for files and for the tree!
732 //
733 if (fChain && !fChain->GetFile()) // *CHANGED-fChain-to-fTree*
734 {
735 *fLog << err << GetDescriptor() << ": No file or no tree with name " << fChain->GetName() << " in file." << endl;
736 return kFALSE;
737 }
738
739 //
740 // get number of events in this tree
741 //
742 if (!GetEntries())
743 {
744 *fLog << err << GetDescriptor() << ": No entries found in file(s)" << endl;
745 return kFALSE;
746 }
747
748 //
749 // output logging information
750 //
751 *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
752
753 //
754 // Get all branches of this tree and
755 // create the Iterator to loop over all branches
756 //
757 TIter Next(fTree->GetListOfBranches()); // *CHANGED-fChain-to-fTree*
758 TBranch *branch=NULL;
759
760 Int_t num=0;
761
762 //
763 // loop over all tasks for processing
764 //
765 while ( (branch=(TBranch*)Next()) )
766 {
767 //
768 // Get Name of Branch and Object
769 //
770 const char *bname = branch->GetName();
771
772 TString oname(bname);
773 if (oname.EndsWith("."))
774 oname.Remove(oname.Length()-1);
775
776 //
777 // Check if enabeling the branch is allowed
778 //
779 if (fVetoList->FindObject(oname))
780 {
781 *fLog << inf << "Master branch " << bname << " has veto... skipped." << endl;
782 DisableSubBranches(branch);
783 continue;
784 }
785
786 //
787 // Create a pointer to the pointer to the object in which the
788 // branch data is stored. The pointers are stored in the TChain
789 // object and we get the pointers from there to delete it.
790 //
791 MParContainer **pcont= new MParContainer*;
792
793#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
794 const char *classname = oname;
795#else
796 const char *classname = branch->GetClassName();
797#endif
798
799 //
800 // check if object is existing in the list
801 //
802 *pcont=pList->FindCreateObj(classname, oname);
803
804 if (!*pcont)
805 {
806 //
807 // if class is not existing in the (root) environment
808 // we cannot proceed reading this branch
809 //
810 *fLog << warn << dbginf << "Warning: Class '" << classname;
811 *fLog << "' for " << oname << " not existing in dictionary. Branch skipped." << endl;
812 DisableSubBranches(branch);
813 continue;
814 }
815
816 //
817 // Check whether a Pointer to a pointer already exists.
818 // If we created one already, delete it.
819 //
820 // *CHANGED-fChain-to-fTree*
821 if (fChain)
822 {
823 TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
824 if (element)
825 delete (MParContainer**)element->GetBaddress();
826 }
827 /* This can't be done here for memory trees - see
828 PostProcess for more details.
829 else
830 {
831 TBranch *branch = (TBranch*)fTree->GetBranch(bname);
832 if (branch)
833 {
834 *fLog << bname << " " << (void*)branch->GetAddress() << " " << pcont << endl;
835 delete (MParContainer**)branch->GetAddress();
836 }
837 }
838 */
839
840 //
841 // here pcont is a pointer the to container in which the data from
842 // the actual branch should be stored - enable branch.
843 //
844 fTree->SetBranchAddress(bname, pcont); // *CHANGED-fChain-to-fTree*
845
846 *fLog << inf2 << "Master branch address '" << bname << "' ";
847 if ((TString)bname!=(TString)classname)
848 *fLog << "[" << classname << "] ";
849 *fLog << "setup for reading." << endl;
850
851 //*fLog << "Branch " << bname << " autodel: " << (int)branch->IsAutoDelete() << endl;
852 //branch->SetAutoDelete();
853
854 num++;
855 }
856
857 *fLog << inf2 << GetDescriptor() << " setup " << num << " master branches addresses." << endl;
858
859 //
860 // If auto enabling scheme isn't disabled, do auto enabling
861 //
862 if (fAutoEnable)
863 EnableBranches(pList);
864
865 //
866 // Now we can start notifying. Reset tree makes sure, that TChain thinks
867 // that the correct file is not yet initialized and reinitilizes it
868 // as soon as the first event is read. This is necessary to call
869 // the notifiers when the first event is read, but after the
870 // PreProcess-function.
871 //
872 if (fChain)
873 fChain->ResetTree(); // *CHANGED-fChain-to-fTree*
874
875 fTree->SetNotify(this); // *CHANGED-fChain-to-fTree*
876
877 const Int_t rc = GetSelector() ? GetSelector()->CallPreProcess(pList) : kTRUE;
878 if (rc!=kTRUE || fChain)
879 return rc;
880
881 return Notify();
882}
883
884// --------------------------------------------------------------------------
885//
886// Set the ready to save flag of all containers which branchaddresses are
887// set for. This is necessary to copy data.
888//
889void MReadTree::SetReadyToSave(Bool_t flag)
890{
891 if (!fChain)
892 return;
893
894 TIter Next(fChain->GetStatus());
895
896 TChainElement *element = NULL;
897 while ((element=(TChainElement*)Next()))
898 {
899 //
900 // Check whether the branch is enabled
901 //
902 if (!element->GetStatus())
903 continue;
904
905 //
906 // Get the pointer to the pointer of the corresponding container
907 //
908 MParContainer **pcont = (MParContainer**)element->GetBaddress();
909
910 //
911 // Check whether the pointer is not NULL
912 //
913 if (!pcont || !*pcont)
914 continue;
915
916 //
917 // Set the ready to save status of the container.
918 //
919 (*pcont)->SetReadyToSave(flag);
920 }
921
922 MTask::SetReadyToSave(flag);
923}
924
925// --------------------------------------------------------------------------
926//
927// The Process-function reads one event from the tree (this contains all
928// enabled branches) and increases the position in the file by one event.
929// (Remark: The position can also be set by some member functions
930// If the end of the file is reached the Eventloop is stopped.
931// In case an event selector is given its value is checked before
932// reading the event. If it returns kAFLSE the event is skipped.
933//
934Int_t MReadTree::Process()
935{
936 if (GetSelector())
937 {
938 //
939 // Make sure selector is processed
940 //
941 if (!GetSelector()->CallProcess())
942 {
943 *fLog << err << dbginf << "Processing Selector failed." << endl;
944 return kFALSE;
945 }
946
947 //
948 // Skip Event
949 //
950 if (!GetSelector()->IsConditionTrue())
951 {
952 fNumEntry++;
953 return kCONTINUE;
954 }
955 }
956
957 const Bool_t rc = fTree->GetEntry(fNumEntry++) != 0; // *CHANGED-fChain-to-fTree*
958
959 if (fTaskList)
960 fTaskList->SetStreamId(fTree->GetName()); // *CHANGED-fChain-to-fTree*
961
962 if (rc)
963 {
964 SetReadyToSave();
965 return kTRUE;
966 }
967
968 if (fChain)
969 switch (fChain->GetLastError())
970 {
971 case MChain::kFatalError:
972 *fLog << err << GetDescriptor() << " - ERROR: Notify() failed." << endl;
973 return kERROR;
974 case MChain::kCannotAccessFile:
975 *fLog << err << GetDescriptor() << " - ERROR: TChain::LoadTree is unable to access requested file." << endl;
976 return kERROR;
977 case MChain::kCannotAccessTree:
978 *fLog << err << GetDescriptor() << " - ERROR: TChain::LoadTree is unable to access requested tree." << endl;
979 return kERROR;
980 case MChain::kOutOfRange: // no more events available
981 return kFALSE;
982 case MChain::kNoError: // go on!
983 return kTRUE;
984 }
985
986 return rc;
987}
988
989// --------------------------------------------------------------------------
990//
991// If a selector is given the selector is post processed
992//
993Int_t MReadTree::PostProcess()
994{
995 // In the case of a memory tree I don't know how we can
996 // make a decision in PreProcess between a self allocated
997 // memory address or a pending address set long before.
998 // So we delete the stuff in PostProcess and not the destructor
999 // (which might seg fault if PreProcess has never been called)
1000 if (!fChain)
1001 {
1002 TIter Next(fTree->GetListOfBranches());
1003 TBranch *b=0;
1004 while ((b=(TBranch*)Next()))
1005 {
1006 if (b->GetAddress())
1007 {
1008 delete b->GetAddress();
1009 b->ResetAddress();
1010 }
1011 }
1012 }
1013
1014 return GetSelector() ? GetSelector()->CallPostProcess() : kTRUE;
1015}
1016
1017// --------------------------------------------------------------------------
1018//
1019// Get the Event with the current EventNumber fNumEntry
1020//
1021Bool_t MReadTree::GetEvent()
1022{
1023 Bool_t rc = fTree->GetEntry(fNumEntry) != 0; // *CHANGED-fChain-to-fTree*
1024
1025 if (rc)
1026 SetReadyToSave();
1027
1028 return rc;
1029}
1030
1031// --------------------------------------------------------------------------
1032//
1033// Decrease the number of the event which is read by Process() next
1034// by one or more
1035//
1036Bool_t MReadTree::DecEventNum(UInt_t dec)
1037{
1038 if (fNumEntry-dec >= GetEntries())
1039 {
1040 *fLog << warn << GetDescriptor() << ": DecEventNum, WARNING - Event " << fNumEntry << "-";
1041 *fLog << dec << "=" << (Int_t)fNumEntry-dec << " out of Range (>=";
1042 *fLog << GetEntries() << ")." << endl;
1043 return kFALSE;
1044 }
1045
1046 fNumEntry -= dec;
1047 return kTRUE;
1048}
1049
1050// --------------------------------------------------------------------------
1051//
1052// Increase the number of the event which is read by Process() next
1053// by one or more
1054//
1055Bool_t MReadTree::IncEventNum(UInt_t inc)
1056{
1057 if (fNumEntry+inc >= GetEntries())
1058 {
1059 *fLog << warn << GetDescriptor() << ": IncEventNum, WARNING - Event " << fNumEntry << "+";
1060 *fLog << inc << "=" << (Int_t)fNumEntry+inc << " out of Range (>=";
1061 *fLog << GetEntries() << ")." << endl;
1062 return kFALSE;
1063 }
1064
1065 fNumEntry += inc;
1066 return kTRUE;
1067}
1068
1069// --------------------------------------------------------------------------
1070//
1071// This function makes Process() read event number nr next
1072//
1073// Remark: You can use this function after instatiating you MReadTree-object
1074// to set the event number from which you want to start reading.
1075//
1076Bool_t MReadTree::SetEventNum(UInt_t nr)
1077{
1078 if (nr!=0 && nr >= GetEntries())
1079 {
1080 *fLog << warn << GetDescriptor() << ": SetEventNum, WARNING - " << nr << " out of Range." << endl;
1081 return kFALSE;
1082 }
1083
1084 fNumEntry = nr;
1085 return kTRUE;
1086}
1087
1088// --------------------------------------------------------------------------
1089//
1090// For the branch with the given name:
1091// 1) no object is automatically created
1092// 2) the branch address for this branch is not set
1093// (because we lack the object, see 1)
1094// 3) The whole branch (exactly: all its subbranches) are disabled
1095// this means are not read in memory by TTree:GetEntry
1096//
1097void MReadTree::VetoBranch(const char *name)
1098{
1099 fVetoList->Add(new TNamed(name, ""));
1100}
1101
1102// --------------------------------------------------------------------------
1103//
1104// Return the name of the file we are actually reading from.
1105//
1106TString MReadTree::GetFullFileName() const
1107{
1108 const TFile *file = fChain ? fChain->GetFile() : fTree->GetCurrentFile();
1109
1110 if (!file)
1111 return "<unknown>";
1112
1113 return file->GetName();
1114}
1115
1116// --------------------------------------------------------------------------
1117//
1118// Get number of files in chain. (-1 is returned if chain is not
1119// initialized.
1120//
1121Int_t MReadTree::GetNumFiles() const
1122{
1123 if (!fChain)
1124 return -1;
1125
1126 return fChain->GetListOfFiles()->GetEntries();
1127}
1128
1129// --------------------------------------------------------------------------
1130//
1131// Return the number of the file in the chain, -1 in case of an error
1132//
1133Int_t MReadTree::GetFileIndex() const
1134{
1135 return fChain ? fChain->GetTreeNumber() : 0;
1136 /*
1137 const TString filename = fChain->GetFile()->GetName();
1138
1139 int i=0;
1140 TObject *file = NULL;
1141
1142 TIter Next(fChain->GetListOfFiles());
1143 while ((file=Next()))
1144 {
1145 if (filename==gSystem->ExpandPathName(file->GetTitle()))
1146 return i;
1147 i++;
1148 }
1149 return -1;
1150 */
1151}
1152
1153// --------------------------------------------------------------------------
1154//
1155// This schedules a TObject which Notify(9 function is called in case
1156// of MReadTree (TChain) switches from one file in the chain to another
1157// one.
1158//
1159void MReadTree::AddNotify(TObject *obj)
1160{
1161 fNotify->Add(obj);
1162}
1163
1164void MReadTree::Print(Option_t *o) const
1165{
1166 *fLog << all << underline << GetDescriptor() << ":" << endl << dec;
1167 *fLog << " Files [Tree]:" << endl;
1168
1169 int i = 0;
1170 TIter Next(fChain->GetListOfFiles());
1171 TObject *obj = NULL;
1172 while ((obj=Next()))
1173 *fLog << " " << i++ << ") " << obj->GetTitle() << " [" << obj->GetName() << "]" << endl;
1174
1175 *fLog << " Total Number of Entries: " << fNumEntries << endl;
1176 *fLog << " Next Entry to read: " << fNumEntry << endl;
1177}
1178
1179// --------------------------------------------------------------------------
1180//
1181// Implementation of SavePrimitive. Used to write the call to a constructor
1182// to a macro. In the original root implementation it is used to write
1183// gui elements to a macro-file.
1184//
1185void MReadTree::StreamPrimitive(ostream &out) const
1186{
1187 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
1188 out << fChain->GetName() << "\", \"" << fName << "\", \"" << fTitle << "\");" << endl;
1189
1190 TIter Next(fChain->GetListOfFiles());
1191 TObject *obj = NULL;
1192 while ((obj=Next()))
1193 out << " " << GetUniqueName() << ".AddFile(\"" << obj->GetTitle() << "\");" << endl;
1194
1195 if (!fAutoEnable)
1196 out << " " << GetUniqueName() << ".DisableAutoScheme();" << endl;
1197
1198 if (fNumEntry!=0)
1199 out << " " << GetUniqueName() << ".SetEventNum(" << fNumEntry << ");" << endl;
1200
1201
1202}
Note: See TracBrowser for help on using the repository browser.