ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Selector.cc
Revision: 1.7
Committed: Thu Dec 4 13:50:15 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.6: +40 -7 lines
Log Message:
Have list of output mods.

File Contents

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