1 |
// $Id: Selector.cc,v 1.13 2011/03/11 04:03:54 bendavid Exp $
|
2 |
|
3 |
#include "MitAna/TreeMod/interface/Selector.h"
|
4 |
#include "MitAna/DataTree/interface/Names.h"
|
5 |
#include "MitAna/TreeMod/interface/OutputMod.h"
|
6 |
#include <TProcessID.h>
|
7 |
#include <TFile.h>
|
8 |
#include <TTree.h>
|
9 |
#include <TROOT.h>
|
10 |
|
11 |
using namespace mithep;
|
12 |
|
13 |
ClassImp(mithep::Selector)
|
14 |
|
15 |
//--------------------------------------------------------------------------------------------------
|
16 |
Selector::Selector() :
|
17 |
fDoRunInfo(kTRUE),
|
18 |
fEvtHdrName(Names::gkEvtHeaderBrn),
|
19 |
fRunTreeName(Names::gkRunTreeName),
|
20 |
fRunInfoName(Names::gkRunInfoBrn),
|
21 |
fAllEvtHdrBrn(Names::gkAllEvtHeaderBrn),
|
22 |
fLATreeName(Names::gkLATreeName),
|
23 |
fLAHdrName(Names::gkLAHeaderBrn),
|
24 |
fAllEvtTreeName(Names::gkAllEvtTreeName),
|
25 |
fRunTree(0),
|
26 |
fEventHeader(0),
|
27 |
fRunInfo(0),
|
28 |
fLATree(0),
|
29 |
fLAHeader(0),
|
30 |
fCurRunNum(UInt_t(-1)),
|
31 |
fTrash(0)
|
32 |
{
|
33 |
// Constructor.
|
34 |
|
35 |
fOutputMods.SetOwner(kFALSE);
|
36 |
fTrash.SetOwner(kTRUE);
|
37 |
gROOT->GetListOfSpecials()->Add(this);
|
38 |
}
|
39 |
|
40 |
//--------------------------------------------------------------------------------------------------
|
41 |
Selector::~Selector()
|
42 |
{
|
43 |
// Destructor.
|
44 |
|
45 |
fRunTree = 0;
|
46 |
fEventHeader = 0;
|
47 |
fRunInfo = 0;
|
48 |
fLATree = 0;
|
49 |
fLAHeader = 0;
|
50 |
fCurRunNum = UInt_t(-1);
|
51 |
gROOT->GetListOfSpecials()->Remove(this);
|
52 |
}
|
53 |
|
54 |
//--------------------------------------------------------------------------------------------------
|
55 |
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 |
LoadBranch(fEvtHdrName);
|
63 |
if (!fEventHeader)
|
64 |
return kFALSE;
|
65 |
|
66 |
if (ConsistentRunNum())
|
67 |
return kFALSE;
|
68 |
|
69 |
UpdateRunInfo();
|
70 |
return ValidRunNum();
|
71 |
}
|
72 |
|
73 |
//--------------------------------------------------------------------------------------------------
|
74 |
Bool_t Selector::EndRun()
|
75 |
{
|
76 |
// Determines whether we are at the end of a run. Also, do treat special case of output module
|
77 |
// 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 |
|
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 |
" assuming end of run %d reached!", fCurRunNum);
|
101 |
return kTRUE;
|
102 |
}
|
103 |
|
104 |
return (fLAHeader->RunNum()!=fCurRunNum);
|
105 |
}
|
106 |
|
107 |
//--------------------------------------------------------------------------------------------------
|
108 |
Bool_t Selector::Notify()
|
109 |
{
|
110 |
// The Notify() function is called when a new file is opened. Here, we check for a new run info
|
111 |
// tree.
|
112 |
|
113 |
if (!GetCurrentFile())
|
114 |
return kTRUE;
|
115 |
|
116 |
if (fDoRunInfo)
|
117 |
UpdateRunInfoTree();
|
118 |
|
119 |
return TAMSelector::Notify();
|
120 |
}
|
121 |
|
122 |
//--------------------------------------------------------------------------------------------------
|
123 |
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 |
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 |
void Selector::SlaveBegin(TTree *tree)
|
159 |
{
|
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 |
ReqBranch(fEvtHdrName, fEventHeader);
|
165 |
}
|
166 |
|
167 |
SearchOutputMods(GetTopModule());
|
168 |
|
169 |
TAMSelector::SlaveBegin(tree);
|
170 |
}
|
171 |
|
172 |
//--------------------------------------------------------------------------------------------------
|
173 |
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 |
Error("UpdateRunInfo", "Error updating run info for run %d, entry %d, return value %d",
|
185 |
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 |
//--------------------------------------------------------------------------------------------------
|
198 |
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 |
fRunTree = dynamic_cast<TTree*>(f->Get(fRunTreeName));
|
218 |
if (!fRunTree) {
|
219 |
Fatal("UpdateRunInfoTree", "Cannot find run info tree with name %s", fRunTreeName.Data());
|
220 |
}
|
221 |
|
222 |
// set branches
|
223 |
if (fRunTree->GetBranch(fRunInfoName)) {
|
224 |
fRunTree->SetBranchAddress(fRunInfoName, &fRunInfo);
|
225 |
} else {
|
226 |
Fatal("UpdateRunInfoTree", "Cannot find run info branch with name %s", fRunInfoName.Data());
|
227 |
}
|
228 |
|
229 |
// look-ahead tree
|
230 |
fLATree = dynamic_cast<TTree*>(f->Get(fLATreeName));
|
231 |
if (!fLATree) {
|
232 |
Fatal("UpdateRunInfoTree", "Cannot find look-ahead tree with name %s", fLATreeName.Data());
|
233 |
}
|
234 |
|
235 |
// set branches
|
236 |
if (fLATree->GetBranch(fLAHdrName)) {
|
237 |
fLATree->SetBranchAddress(fLAHdrName, &fLAHeader);
|
238 |
} else {
|
239 |
Fatal("UpdateRunInfoTree", "Cannot find look-ahead branch with name %s", fLAHdrName.Data());
|
240 |
}
|
241 |
}
|