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

Last change on this file since 2124 was 2123, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 29.9 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-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
69ClassImp(MReadTree);
70
71// --------------------------------------------------------------------------
72//
73// Default constructor. Don't use it.
74//
75MReadTree::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//
98MReadTree::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//
129MReadTree::~MReadTree()
130{
131 //
132 // Delete all the pointers to pointers to the objects where the
133 // branche data gets stored. FIXME: When PreProcessed twice this
134 // creates a memory leak!
135 //
136 TIter Next(fChain->GetStatus());
137
138 TChainElement *element = NULL;
139 while ((element=(TChainElement*)Next()))
140 if (element->GetBaddress())
141 delete (MParContainer**)element->GetBaddress();
142
143 //
144 // Delete the chain and the veto list
145 //
146#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,00)
147 if (fChain->GetFile())
148 delete fChain->GetFile();
149#endif
150 delete fChain;
151
152 delete fNotify;
153 delete fVetoList;
154}
155
156// --------------------------------------------------------------------------
157//
158// This check whether all branches in the tree have the same size. If
159// this is not the case the events in the different branches cannot
160// be ordered correctly.
161//
162Bool_t MReadTree::CheckBranchSize()
163{
164 TArrayI entries(fChain->GetStatus()->GetSize());
165 Int_t num=0;
166
167 // Loop over all branches which have a corresponding container
168 TIter Next(fChain->GetStatus());
169
170 TChainElement *element = NULL;
171 while ((element=(TChainElement*)Next()))
172 {
173 // Get branch name and find pointer to corresponding branch
174 const TString name = element->GetName();
175 const TBranch *b = fChain->FindBranch(name);
176
177 // Skip element without corresponding branches (like "*")
178 if (!b)
179 continue;
180
181 entries[num++] = (Int_t)b->GetEntries();
182 }
183
184 // Check the number of entries of the branches pair-wise
185 for (int i=0; i<num; i++)
186 for (int j=i; j<num; j++)
187 {
188 if (entries[i]==entries[j])
189 continue;
190
191 *fLog << err << "ERROR - File corrupttion detected:" << endl;
192 *fLog << " Due to several circumstances (such at a bug in MReadTree or wrong" << endl;
193 *fLog << " usage of the file UPDATE mode) you may have produced a file in which" << endl;
194 *fLog << " at least two branches in the same tree (" << fChain->GetName() << ") have different" << endl;
195 *fLog << " number of entries. Sorry, but this file (" << GetFileName() << ")" << endl;
196 *fLog << " is unusable." << endl;
197 return kFALSE;
198 }
199
200 return kTRUE;
201}
202
203// --------------------------------------------------------------------------
204//
205// If the owner flag is set all TObjects which are scheduled via
206// AddNotify are deleted by the destructor of MReadTree
207//
208void MReadTree::SetOwner(Bool_t flag)
209{
210 flag ? fNotify->SetBit(kIsOwner) : fNotify->ResetBit(kIsOwner);
211}
212
213// --------------------------------------------------------------------------
214//
215// This function is called each time MReadTree changes the file to read
216// from. It calls all TObject::Notify() functions which are scheduled
217// via AddNotify.
218//
219Bool_t MReadTree::Notify()
220{
221 //
222 // Do a consistency check for all branches
223 //
224 if (!CheckBranchSize())
225 return kFALSE;
226
227 *fLog << inf << GetDescriptor() << ": Switching to #" << GetFileIndex();
228 *fLog << " '" << GetFileName() << "' (before event #";
229 *fLog << GetNumEntry()-1 << ")" << endl;
230
231 //fNotify->Notify();
232
233 return kTRUE;
234}
235
236// --------------------------------------------------------------------------
237//
238// If you want to read the given tree over several files you must add
239// the files here before PreProcess is called. Be careful: If the tree
240// doesn't have the same contents (branches) it may confuse your
241// program (trees which are are not existing in later files are not read
242// anymore, tree wich are not existing in the first file are never read)
243//
244// Name may use the wildcarding notation, eg "xxx*.root" means all files
245// starting with xxx in the current file system directory.
246//
247// AddFile returns the number of files added to the chain.
248//
249// For more information see TChain::Add
250//
251Int_t MReadTree::AddFile(const char *fname, Int_t entries)
252{
253#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
254 //
255 // This is a workaround to get rid of crashed if the file doesn't
256 // exist due to non initialized TFile::fProcessIDs
257 //
258 // (Code taken from TFile::TFile
259 //
260 TString newname; // char-array must overcome comming block
261
262 if (strrchr(fname, '?') || strrchr(fname, '*'))
263 *fLog << warn << "WARNING: You may encounter crashes closing the files..." << endl;
264 else
265 {
266 const char *name;
267
268 if ((name = gSystem->ExpandPathName(fname)))
269 {
270 newname = name;
271 delete [] name;
272 }
273
274 if (newname.IsNull())
275 {
276 *fLog << err << dbginf << "Error expanding path " << fname << "." << endl;
277 return 0;
278 }
279
280 if (gSystem->AccessPathName(newname, kFileExists))
281 {
282 *fLog << err << "ERROR - File '" << fname << "' does not exist." << endl;
283 return 0;
284 }
285
286 fname = newname.Data();
287 }
288#endif
289
290 //
291 // FIXME! A check is missing whether the file already exists or not.
292 //
293 const Int_t numfiles = fChain->Add(fname, entries);
294
295 if (numfiles>0)
296 SetBit(kChainWasChanged);
297 else
298 *fLog << warn << "WARNING: '" << fname << "' not added to " << GetDescriptor() << endl;
299
300 return numfiles;
301}
302
303/*
304 // --------------------------------------------------------------------------
305 //
306 //
307 Int_t MReadTree::AddFile(const TChainElement &obj)
308 {
309 return AddFile(obj.GetTitle(), obj.GetEntries());
310 }
311*/
312
313// --------------------------------------------------------------------------
314//
315// Adds all files from another MReadTree to this instance
316//
317// Returns the number of file which were added
318//
319Int_t MReadTree::AddFiles(const MReadTree &read)
320{
321 const Int_t rc = fChain->Add(read.fChain);
322
323 if (rc>0)
324 SetBit(kChainWasChanged);
325
326 /*
327 Int_t rc = 0;
328
329 TIter Next(read.fChain->GetListOfFiles());
330 TObject *obj = NULL;
331 while ((obj=Next()))
332 rc += AddFile(*(TChainElement*)obj);
333 */
334
335 return rc;
336}
337
338// --------------------------------------------------------------------------
339//
340// This function is called if Branch choosing method should get enabled.
341// Branch choosing means, that only the enabled branches are read into
342// memory. To use an enableing scheme we have to disable all branches first.
343// This is done, if this function is called the first time.
344//
345void MReadTree::EnableBranchChoosing()
346{
347 if (fBranchChoosing)
348 return;
349
350 *fLog << inf << GetDescriptor() << ": Branch choosing enabled (only enabled branches are read)." << endl;
351 fChain->SetBranchStatus("*", kFALSE);
352 fBranchChoosing = kTRUE;
353}
354
355// --------------------------------------------------------------------------
356//
357// The first time this function is called all branches are disabled.
358// The given branch is enabled. By enabling only the branches you
359// are processing you can speed up your calculation many times (up to
360// a factor of 10 or 20)
361//
362void MReadTree::EnableBranch(const char *name)
363{
364 if (fChain->GetListOfFiles()->GetEntries()==0)
365 {
366 *fLog << err << "Chain contains no file... Branch '";
367 *fLog << name << "' ignored." << endl;
368 return;
369 }
370
371 EnableBranchChoosing();
372
373 TNamed branch(name, "");
374 SetBranchStatus(&branch, kTRUE);
375}
376
377// --------------------------------------------------------------------------
378//
379// Set branch status of branch name
380//
381void MReadTree::SetBranchStatus(const char *name, Bool_t status)
382{
383 fChain->SetBranchStatus(name, status);
384
385 *fLog << inf << (status ? "Enabled" : "Disabled");
386 *fLog << " subbranch '" << name << "'." << endl;
387}
388
389// --------------------------------------------------------------------------
390//
391// Checks whether a branch with the given name exists in the chain
392// and sets the branch status of this branch corresponding to status.
393//
394void MReadTree::SetBranchStatus(TObject *branch, Bool_t status)
395{
396 //
397 // Get branch name
398 //
399 const char *name = branch->GetName();
400
401 //
402 // Check whether this branch really exists
403 //
404 if (fChain->GetBranch(name))
405 SetBranchStatus(name, status);
406
407 //
408 // Remove trailing '.' if one and try to enable the subbranch without
409 // the master branch name. This is to be compatible with older mars
410 // and camera files.
411 //
412 const char *dot = strrchr(name, '.');
413 if (!dot)
414 return;
415
416 if (fChain->GetBranch(dot+1))
417 SetBranchStatus(dot+1, status);
418}
419
420// --------------------------------------------------------------------------
421//
422// Set the status of all branches in the list to status.
423//
424void MReadTree::SetBranchStatus(const TList *list, Bool_t status)
425{
426 //
427 // Loop over all subbranches in this master branch
428 //
429 TIter Next(list);
430
431 TObject *obj;
432 while ((obj=Next()))
433 SetBranchStatus(obj, status);
434}
435
436// --------------------------------------------------------------------------
437//
438// This is the implementation of the Auto Enabling Scheme.
439// For more information see MTask::AddBranchToList.
440// This function loops over all tasks and its filters in the tasklist
441// and enables all branches which are requested by the tasks and its
442// filters.
443//
444// To enable 'unknown' branches which are not in the branchlist of
445// the tasks you can call EnableBranch
446//
447void MReadTree::EnableBranches(MParList *plist)
448{
449 //
450 // check whether branch choosing must be switched on
451 //
452 EnableBranchChoosing();
453
454 //
455 // request the tasklist from the parameter list.
456 // FIXME: Tasklist can have a different name
457 //
458 const MTaskList *tlist = (MTaskList*)plist->FindObject("MTaskList");
459 if (!tlist)
460 {
461 *fLog << warn << GetDescriptor() << "Cannot use auto enabeling scheme for branches. 'MTaskList' not found." << endl;
462 return;
463 }
464
465 //
466 // This loop is not necessary. We could do it like in the commented
467 // loop below. But this loop makes sure, that we don't try to enable
468 // one branch several times. This would not harm, but we would get
469 // an output for each attempt. To have several outputs for one subbranch
470 // may confuse the user, this we don't want.
471 // This loop creates a new list of subbranches and for each branch
472 // which is added we check before whether it already exists or not.
473 //
474 TList list;
475
476 MTask *task;
477 TIter NextTask(tlist->GetList());
478 while ((task=(MTask*)NextTask()))
479 {
480 TObject *obj;
481
482 TIter NextTBranch(task->GetListOfBranches());
483 while ((obj=NextTBranch()))
484 if (!list.FindObject(obj->GetName()))
485 list.Add(obj);
486
487 const MFilter *filter = task->GetFilter();
488
489 if (!filter)
490 continue;
491
492 TIter NextFBranch(filter->GetListOfBranches());
493 while ((obj=NextFBranch()))
494 if (!list.FindObject(obj->GetName()))
495 list.Add(obj);
496 }
497
498 SetBranchStatus(&list, kTRUE);
499/*
500 //
501 // Loop over all tasks iand its filters n the task list.
502 //
503 MTask *task;
504 TIter NextTask(tlist->GetList());
505 while ((task=(MTask*)NextTask()))
506 {
507 SetBranchStatus(task->GetListOfBranches(), kTRUE);
508
509 const MFilter *filter = task->GetFilter();
510 if (!filter)
511 continue;
512
513 SetBranchStatus(filter->GetListOfBranches(), kTRUE);
514
515 }
516*/
517}
518
519// --------------------------------------------------------------------------
520//
521// If the chain has been changed (by calling AddFile or using a file
522// in the constructors argument) the number of entries is newly
523// calculated from the files in the chain - this may take a while.
524// The number of entries is returned.
525//
526UInt_t MReadTree::GetEntries()
527{
528 if (TestBit(kChainWasChanged))
529 {
530 *fLog << inf << "Scanning chain... " << flush;
531 fNumEntries = (UInt_t)fChain->GetEntries();
532 *fLog << fNumEntries << " events found." << endl;
533 ResetBit(kChainWasChanged);
534 }
535 return fNumEntries;
536}
537
538// --------------------------------------------------------------------------
539//
540// The disables all subbranches of the given master branch.
541//
542void MReadTree::DisableSubBranches(TBranch *branch)
543{
544 //
545 // This is not necessary, it would work without. But the output
546 // may confuse the user...
547 //
548 if (fAutoEnable || fBranchChoosing)
549 return;
550
551 SetBranchStatus(branch->GetListOfBranches(), kFALSE);
552}
553
554// --------------------------------------------------------------------------
555//
556// The PreProcess loops (till now) over the branches in the given tree.
557// It checks if the corresponding containers (containers with the same
558// name than the branch name) are existing in the Parameter Container List.
559// If not, a container of objec type 'branch-name' is created (everything
560// after the last semicolon in the branch name is stripped). Only
561// branches which don't have a veto (see VetoBranch) are enabled If the
562// object isn't found in the root dictionary (a list of classes known by the
563// root environment) the branch is skipped and an error message is printed
564// out.
565// If a selector is specified it is preprocessed after the
566// MReadTree::PreProcess
567//
568Bool_t MReadTree::PreProcess(MParList *pList)
569{
570 //
571 // Make sure, that all the following calls doesn't result in
572 // Notifications. This may be dangerous, because the notified
573 // tasks are not preprocessed.
574 //
575 fChain->SetNotify(NULL);
576
577 //
578 // get number of events in this tree
579 //
580 if (!GetEntries())
581 {
582 *fLog << warn << dbginf << "No entries found in file(s)" << endl;
583 return kFALSE;
584 }
585
586 //
587 // output logging information
588 //
589 *fLog << inf << fNumEntries << " entries found in file(s)." << endl;
590
591 //
592 // Get all branches of this tree and
593 // create the Iterator to loop over all branches
594 //
595 TIter Next(fChain->GetListOfBranches());
596 TBranch *branch=NULL;
597
598 Int_t num=0;
599
600 //
601 // loop over all tasks for processing
602 //
603 while ( (branch=(TBranch*)Next()) )
604 {
605 //
606 // Get Name of Branch and Object
607 //
608 const char *bname = branch->GetName();
609
610 TString oname(bname);
611 if (oname.EndsWith("."))
612 oname.Remove(oname.Length()-1);
613
614 //
615 // Check if enabeling the branch is allowed
616 //
617 if (fVetoList->FindObject(oname))
618 {
619 *fLog << inf << "Master branch " << bname << " has veto... skipped." << endl;
620 DisableSubBranches(branch);
621 continue;
622 }
623
624 //
625 // Create a pointer to the pointer to the object in which the
626 // branch data is stored. The pointers are stored in the TChain
627 // object and we get the pointers from there to delete it.
628 //
629 MParContainer **pcont= new MParContainer*;
630
631#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
632 const char *classname = oname;
633#else
634 const char *classname = branch->GetClassName();
635#endif
636
637 //
638 // check if object is existing in the list
639 //
640 *pcont=pList->FindCreateObj(classname, oname);
641
642 if (!*pcont)
643 {
644 //
645 // if class is not existing in the (root) environment
646 // we cannot proceed reading this branch
647 //
648 *fLog << warn << dbginf << "Warning: Class '" << classname;
649 *fLog << "' for " << oname << " not existing in dictionary. Branch skipped." << endl;
650 DisableSubBranches(branch);
651 continue;
652 }
653
654 //
655 // Check whether a Pointer to a pointer already exists.
656 // If we created one already, delete it.
657 //
658 TChainElement *element = (TChainElement*)fChain->GetStatus()->FindObject(bname);
659 if (element)
660 delete (MParContainer**)element->GetBaddress();
661
662 //
663 // here pcont is a pointer the to container in which the data from
664 // the actual branch should be stored - enable branch.
665 //
666 fChain->SetBranchAddress(bname, pcont);
667
668 *fLog << inf << "Master branch address " << bname << " [";
669 *fLog << classname << "] setup for reading." << endl;
670
671 //*fLog << "Branch " << bname << " autodel: " << (int)branch->IsAutoDelete() << endl;
672 //branch->SetAutoDelete();
673
674 num++;
675 }
676
677 *fLog << inf << GetDescriptor() << " setup " << num << " master branches addresses." << endl;
678
679 //
680 // Do a consistency check for all branches
681 //
682// if (!CheckBranchSize())
683// return kFALSE;
684
685 //
686 // If auto enabling scheme isn't disabled, do auto enabling
687 //
688 if (fAutoEnable)
689 EnableBranches(pList);
690
691 //
692 // Now we can start notifying. Reset tree makes sure, that TChain thinks
693 // that the correct file is not yet initialized and reinitilizes it
694 // as soon as the first event is read. This is necessary to call
695 // the notifiers when the first event is read, but after the
696 // PreProcess-function.
697 //
698 fChain->ResetTree();
699 fChain->SetNotify(this);
700
701 return GetSelector() ? GetSelector()->CallPreProcess(pList) : kTRUE;
702}
703
704// --------------------------------------------------------------------------
705//
706// Set the ready to save flag of all containers which branchaddresses are
707// set for. This is necessary to copy data.
708//
709void MReadTree::SetReadyToSave(Bool_t flag)
710{
711 TIter Next(fChain->GetStatus());
712
713 TChainElement *element = NULL;
714 while ((element=(TChainElement*)Next()))
715 {
716 //
717 // Check whether the branch is enabled
718 //
719 if (!element->GetStatus())
720 continue;
721
722 //
723 // Get the pointer to the pointer of the corresponding container
724 //
725 MParContainer **pcont = (MParContainer**)element->GetBaddress();
726
727 //
728 // Check whether the pointer is not NULL
729 //
730 if (!pcont || !*pcont)
731 continue;
732
733 //
734 // Set the ready to save status of the container.
735 //
736 (*pcont)->SetReadyToSave(flag);
737 }
738
739 //
740 // Set the ready to save status of this task (used?), too
741 //
742 MTask::SetReadyToSave(flag);
743}
744
745// --------------------------------------------------------------------------
746//
747// The Process-function reads one event from the tree (this contains all
748// enabled branches) and increases the position in the file by one event.
749// (Remark: The position can also be set by some member functions
750// If the end of the file is reached the Eventloop is stopped.
751// In case an event selector is given its value is checked before
752// reading the event. If it returns kAFLSE the event is skipped.
753//
754#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
755#include "MRawEvtData.h"
756#endif
757Bool_t MReadTree::Process()
758{
759 //
760 // This is necessary due to a bug in TChain::LoadTree in root.
761 // will be fixed in 3.03
762 //
763#if ROOT_VERSION_CODE < ROOT_VERSION(3,03,01)
764 if (fNumEntry >= GetEntries())
765 return kFALSE;
766#endif
767
768#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,06)
769 //
770 // This fixes 99.9% of a memory leak using a root version prior
771 // to 3.02/??
772 //
773 TChainElement *element=NULL;
774 TIter Next(fChain->GetStatus());
775 while ((element=(TChainElement*)Next()))
776 {
777 MParContainer **c = (MParContainer**)element->GetBaddress();
778 if (!c) continue;
779 if ((*c)->InheritsFrom(MRawEvtData::Class()))
780 ((MRawEvtData*)(*c))->DeletePixels(kFALSE);
781
782 }
783#endif
784
785 if (GetSelector())
786 {
787 //
788 // Make sure selector is processed
789 //
790 if (!GetSelector()->CallProcess())
791 {
792 *fLog << err << dbginf << "Processing Selector failed." << endl;
793 return kFALSE;
794 }
795
796 //
797 // Skip Event
798 //
799 if (!GetSelector()->IsConditionTrue())
800 {
801 fNumEntry++;
802 return kCONTINUE;
803 }
804 }
805
806 const Bool_t rc = fChain->GetEntry(fNumEntry++) != 0;
807
808 if (rc)
809 SetReadyToSave();
810
811 return rc;
812}
813
814// --------------------------------------------------------------------------
815//
816// If a selector is given the selector is post processed
817//
818Bool_t MReadTree::PostProcess()
819{
820 return GetSelector() ? GetSelector()->CallPostProcess() : kTRUE;
821}
822
823// --------------------------------------------------------------------------
824//
825// Get the Event with the current EventNumber fNumEntry
826//
827Bool_t MReadTree::GetEvent()
828{
829 Bool_t rc = fChain->GetEntry(fNumEntry) != 0;
830
831 if (rc)
832 SetReadyToSave();
833
834 return rc;
835}
836
837// --------------------------------------------------------------------------
838//
839// Decrease the number of the event which is read by Process() next
840// by one or more
841//
842Bool_t MReadTree::DecEventNum(UInt_t dec)
843{
844 if (fNumEntry-dec >= GetEntries())
845 {
846 *fLog << warn << GetDescriptor() << ": DecEventNum, WARNING - Event " << fNumEntry << "-";
847 *fLog << dec << "=" << (Int_t)fNumEntry-dec << " out of Range." << endl;
848 return kFALSE;
849 }
850
851 fNumEntry -= dec;
852 return kTRUE;
853}
854
855// --------------------------------------------------------------------------
856//
857// Increase the number of the event which is read by Process() next
858// by one or more
859//
860Bool_t MReadTree::IncEventNum(UInt_t inc)
861{
862 if (fNumEntry+inc >= GetEntries())
863 {
864 *fLog << warn << GetDescriptor() << ": IncEventNum, WARNING - Event " << fNumEntry << "+";
865 *fLog << inc << "=" << (Int_t)fNumEntry+inc << " out of Range." << endl;
866 return kFALSE;
867 }
868
869 fNumEntry += inc;
870 return kTRUE;
871}
872
873// --------------------------------------------------------------------------
874//
875// This function makes Process() read event number nr next
876//
877// Remark: You can use this function after instatiating you MReadTree-object
878// to set the event number from which you want to start reading.
879//
880Bool_t MReadTree::SetEventNum(UInt_t nr)
881{
882 if (nr >= GetEntries())
883 {
884 *fLog << warn << GetDescriptor() << ": SetEventNum, WARNING - " << nr << " out of Range." << endl;
885 return kFALSE;
886 }
887
888 fNumEntry = nr;
889 return kTRUE;
890}
891
892// --------------------------------------------------------------------------
893//
894// For the branch with the given name:
895// 1) no object is automatically created
896// 2) the branch address for this branch is not set
897// (because we lack the object, see 1)
898// 3) The whole branch (exactly: all its subbranches) are disabled
899// this means are not read in memory by TTree:GetEntry
900//
901void MReadTree::VetoBranch(const char *name)
902{
903 fVetoList->Add(new TNamed(name, ""));
904}
905
906// --------------------------------------------------------------------------
907//
908// Return the name of the file we are actually reading from.
909//
910TString MReadTree::GetFileName() const
911{
912 const TFile *file = fChain->GetFile();
913
914 if (!file)
915 return TString("<unknown>");
916
917 TString name(file->GetName());
918 name.Remove(0, name.Last('/')+1);
919 return name;
920}
921
922// --------------------------------------------------------------------------
923//
924// Return the number of the file in the chain, -1 in case of an error
925//
926Int_t MReadTree::GetFileIndex() const
927{
928 return fChain->GetTreeNumber();
929 /*
930 const TString filename = fChain->GetFile()->GetName();
931
932 int i=0;
933 TObject *file = NULL;
934
935 TIter Next(fChain->GetListOfFiles());
936 while ((file=Next()))
937 {
938 if (filename==gSystem->ExpandPathName(file->GetTitle()))
939 return i;
940 i++;
941 }
942 return -1;
943 */
944}
945
946// --------------------------------------------------------------------------
947//
948// This schedules a TObject which Notify(9 function is called in case
949// of MReadTree (TChain) switches from one file in the chain to another
950// one.
951//
952void MReadTree::AddNotify(TObject *obj)
953{
954 fNotify->Add(obj);
955}
956
957void MReadTree::Print(Option_t *o) const
958{
959 *fLog << all << underline << GetDescriptor() << ":" << endl << dec;
960 *fLog << " Files [Tree]:" << endl;
961
962 int i = 0;
963 TIter Next(fChain->GetListOfFiles());
964 TObject *obj = NULL;
965 while ((obj=Next()))
966 *fLog << " " << i++ << ") " << obj->GetTitle() << " [" << obj->GetName() << "]" << endl;
967
968 *fLog << " Total Number of Entries: " << fNumEntries << endl;
969 *fLog << " Next Entry to read: " << fNumEntry << endl;
970}
971
972// --------------------------------------------------------------------------
973//
974// Implementation of SavePrimitive. Used to write the call to a constructor
975// to a macro. In the original root implementation it is used to write
976// gui elements to a macro-file.
977//
978void MReadTree::StreamPrimitive(ofstream &out) const
979{
980 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
981 out << fChain->GetName() << "\", \"" << fName << "\", \"" << fTitle << "\");" << endl;
982
983 TIter Next(fChain->GetListOfFiles());
984 TObject *obj = NULL;
985 while ((obj=Next()))
986 out << " " << GetUniqueName() << ".AddFile(\"" << obj->GetTitle() << "\");" << endl;
987
988 if (!fAutoEnable)
989 out << " " << GetUniqueName() << ".DisableAutoScheme();" << endl;
990
991 if (fNumEntry!=0)
992 out << " " << GetUniqueName() << ".SetEventNum(" << fNumEntry << ");" << endl;
993
994
995}
Note: See TracBrowser for help on using the repository browser.