ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Selector.cc
Revision: 1.15
Committed: Mon Mar 21 15:58:37 2011 UTC (14 years, 1 month ago) by paus
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_025c_branch2, Mit_025c_branch1, Mit_025c_branch0, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1
Branch point for: Mit_025c_branch
Changes since 1.14: +2 -5 lines
Log Message:
Improved caching.

File Contents

# User Rev Content
1 bendavid 1.14 // $Id: Selector.cc,v 1.13 2011/03/11 04:03:54 bendavid Exp $
2 loizides 1.1
3     #include "MitAna/TreeMod/interface/Selector.h"
4 loizides 1.2 #include "MitAna/DataTree/interface/Names.h"
5 loizides 1.6 #include "MitAna/TreeMod/interface/OutputMod.h"
6 loizides 1.2 #include <TProcessID.h>
7     #include <TFile.h>
8     #include <TTree.h>
9 loizides 1.10 #include <TROOT.h>
10 loizides 1.2
11 loizides 1.1 using namespace mithep;
12    
13     ClassImp(mithep::Selector)
14    
15 loizides 1.4 //--------------------------------------------------------------------------------------------------
16 loizides 1.11 Selector::Selector() :
17 loizides 1.2 fDoRunInfo(kTRUE),
18 loizides 1.5 fEvtHdrName(Names::gkEvtHeaderBrn),
19     fRunTreeName(Names::gkRunTreeName),
20     fRunInfoName(Names::gkRunInfoBrn),
21 loizides 1.6 fAllEvtHdrBrn(Names::gkAllEvtHeaderBrn),
22 loizides 1.5 fLATreeName(Names::gkLATreeName),
23     fLAHdrName(Names::gkLAHeaderBrn),
24 loizides 1.6 fAllEvtTreeName(Names::gkAllEvtTreeName),
25 loizides 1.2 fRunTree(0),
26     fEventHeader(0),
27     fRunInfo(0),
28     fLATree(0),
29 loizides 1.3 fLAHeader(0),
30 loizides 1.12 fCurRunNum(UInt_t(-1)),
31     fTrash(0)
32 loizides 1.1 {
33 loizides 1.5 // Constructor.
34 loizides 1.7
35     fOutputMods.SetOwner(kFALSE);
36 loizides 1.12 fTrash.SetOwner(kTRUE);
37 loizides 1.10 gROOT->GetListOfSpecials()->Add(this);
38 loizides 1.1 }
39    
40 loizides 1.4 //--------------------------------------------------------------------------------------------------
41 loizides 1.1 Selector::~Selector()
42     {
43 loizides 1.5 // Destructor.
44    
45 loizides 1.10 fRunTree = 0;
46 loizides 1.5 fEventHeader = 0;
47 loizides 1.10 fRunInfo = 0;
48     fLATree = 0;
49     fLAHeader = 0;
50     fCurRunNum = UInt_t(-1);
51     gROOT->GetListOfSpecials()->Remove(this);
52 loizides 1.1 }
53 loizides 1.2
54 loizides 1.4 //--------------------------------------------------------------------------------------------------
55 loizides 1.2 Bool_t Selector::BeginRun()
56     {
57     // Determines whether we are at beginning of a new run.
58    
59     if (!fDoRunInfo)
60     return kFALSE;
61    
62 loizides 1.5 LoadBranch(fEvtHdrName);
63 loizides 1.2 if (!fEventHeader)
64     return kFALSE;
65    
66     if (ConsistentRunNum())
67     return kFALSE;
68    
69     UpdateRunInfo();
70     return ValidRunNum();
71     }
72    
73 loizides 1.4 //--------------------------------------------------------------------------------------------------
74 loizides 1.2 Bool_t Selector::EndRun()
75     {
76 loizides 1.6 // Determines whether we are at the end of a run. Also, do treat special case of output module
77 loizides 1.7 // here so that in any case it can process the event.
78    
79     if (IsAModAborted() || IsEventAborted()) { // deal with output module if needed: Do this here,
80     TIter it(fOutputMods.MakeIterator()); // avoids having to copy/rewrite large parts of
81     OutputMod *o = 0; // TAMSelector::Process and interaction with TAModule
82     while ((o=static_cast<OutputMod*>(it.Next())) != 0)
83     o->ProcessAll();
84     }
85 loizides 1.2
86     if (!fDoRunInfo)
87     return kFALSE;
88    
89     if (!ValidRunNum())
90     return kFALSE;
91    
92     // determine if run will end
93     if (fCurEvt+1==fTree->GetTree()->GetEntries())
94     return kTRUE; // we are at last entry in current file
95    
96     fLAHeader=0;
97     Int_t ret = fLATree->GetEvent(fCurEvt);
98     if (ret<0 || fLAHeader==0) {
99     Error("EndRun", "Could not get entry lookahead entry for next event,"
100 bendavid 1.13 " assuming end of run %d reached!", fCurRunNum);
101 loizides 1.2 return kTRUE;
102     }
103    
104     return (fLAHeader->RunNum()!=fCurRunNum);
105     }
106    
107 loizides 1.4 //--------------------------------------------------------------------------------------------------
108 loizides 1.2 Bool_t Selector::Notify()
109     {
110 paus 1.15 // The Notify() function is called when a new file is opened. Here, we check for a new run info
111     // tree.
112 loizides 1.2
113     if (!GetCurrentFile())
114     return kTRUE;
115    
116     if (fDoRunInfo)
117     UpdateRunInfoTree();
118 bendavid 1.9
119 loizides 1.2 return TAMSelector::Notify();
120     }
121    
122 loizides 1.4 //--------------------------------------------------------------------------------------------------
123 loizides 1.12 Bool_t Selector::Process(Long64_t entry)
124     {
125     // Process an element of the tree.
126    
127     Bool_t ret = TAMSelector::Process(entry);
128     fTrash.Delete();
129     return ret;
130     }
131    
132     //--------------------------------------------------------------------------------------------------
133 loizides 1.7 void Selector::SearchOutputMods(const TAModule *mod)
134     {
135     // Search for output module among list of modules.
136    
137     if (!mod)
138     return;
139    
140     const OutputMod *o = dynamic_cast<const OutputMod*>(mod);
141     if (o)
142     fOutputMods.Add(const_cast<OutputMod*>(o));
143    
144     const TList *tasks = mod->GetSubModules();
145     if (!tasks)
146     return;
147    
148     TIter it(tasks->MakeIterator());
149     TObject *ob = 0;
150     while ((ob=it.Next()) != 0) {
151     TAModule *nmod = dynamic_cast<TAModule*>(ob);
152     if (nmod)
153     SearchOutputMods(nmod);
154     }
155     }
156    
157     //--------------------------------------------------------------------------------------------------
158 loizides 1.6 void Selector::SlaveBegin(TTree *tree)
159 loizides 1.2 {
160     // The SlaveBegin() function is called after the Begin() function and can be used to setup
161     // analysis on the slaves. Here, we request the event header branch.
162    
163     if (fDoRunInfo) {
164 loizides 1.7 ReqBranch(fEvtHdrName, fEventHeader);
165 loizides 1.2 }
166    
167 loizides 1.7 SearchOutputMods(GetTopModule());
168    
169 loizides 1.2 TAMSelector::SlaveBegin(tree);
170     }
171    
172 loizides 1.4 //--------------------------------------------------------------------------------------------------
173 loizides 1.2 void Selector::UpdateRunInfo()
174     {
175     // Update the run info to be consistent with the information from the event header.
176    
177     fRunInfo = 0;
178    
179     if (!fRunTree)
180     return;
181    
182     Int_t ret = fRunTree->GetEvent(fEventHeader->RunEntry());
183     if (ret<0 || fRunInfo==0) {
184 bendavid 1.13 Error("UpdateRunInfo", "Error updating run info for run %d, entry %d, return value %d",
185 loizides 1.2 fEventHeader->RunNum(), fEventHeader->RunEntry(), ret);
186     return;
187     }
188    
189     fCurRunNum = fEventHeader->RunNum();
190     if (!ValidRunInfo()) {
191     Error("UpdateRunInfo", "Error updating run info, run values do not match %d %d",
192     fCurRunNum, fRunInfo->RunNum());
193     return;
194     }
195     }
196    
197 loizides 1.4 //--------------------------------------------------------------------------------------------------
198 loizides 1.2 void Selector::UpdateRunInfoTree()
199     {
200     // Get the run info tree from current file and set our branches.
201    
202     // first erase previous entries
203     fRunTree = 0;
204     fRunInfo = 0;
205     fCurRunNum = UInt_t(-1);
206     fLATree = 0;
207     fLAHeader = 0;
208    
209     // get current file and retrieve trees
210     TFile *f = GetCurrentFile();
211     if (!f) {
212     Error("UpdateRunInfoTree", "Ptr to current file is null");
213     return;
214     }
215    
216     // run info tree
217 loizides 1.5 fRunTree = dynamic_cast<TTree*>(f->Get(fRunTreeName));
218 loizides 1.2 if (!fRunTree) {
219 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find run info tree with name %s", fRunTreeName.Data());
220 loizides 1.2 }
221    
222     // set branches
223 loizides 1.5 if (fRunTree->GetBranch(fRunInfoName)) {
224     fRunTree->SetBranchAddress(fRunInfoName, &fRunInfo);
225 loizides 1.2 } else {
226 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find run info branch with name %s", fRunInfoName.Data());
227 loizides 1.2 }
228    
229     // look-ahead tree
230 loizides 1.5 fLATree = dynamic_cast<TTree*>(f->Get(fLATreeName));
231 loizides 1.2 if (!fLATree) {
232 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find look-ahead tree with name %s", fLATreeName.Data());
233 loizides 1.2 }
234    
235     // set branches
236 loizides 1.5 if (fLATree->GetBranch(fLAHdrName)) {
237     fLATree->SetBranchAddress(fLAHdrName, &fLAHeader);
238 loizides 1.2 } else {
239 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find look-ahead branch with name %s", fLAHdrName.Data());
240 loizides 1.2 }
241     }