ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Selector.cc
Revision: 1.10
Committed: Mon Mar 2 12:35:29 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre2, Mit_008pre1
Changes since 1.9: +9 -6 lines
Log Message:
Expose selector to ROOT specials

File Contents

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