ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Selector.cc
Revision: 1.14
Committed: Sun Mar 20 23:48:01 2011 UTC (14 years, 1 month ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.13: +4 -4 lines
Log Message:
possible workaround for run tree cache read crash

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 loizides 1.5 // The Notify() function is called when a new file is opened.
111     // Here, we check for a new run info tree.
112 loizides 1.2
113     if (!GetCurrentFile())
114     return kTRUE;
115    
116 bendavid 1.14 //needed to force caching to occur for all files
117     fTree->GetTree()->SetCacheSize(fTree->GetCacheSize());
118    
119 loizides 1.2 if (fDoRunInfo)
120     UpdateRunInfoTree();
121 bendavid 1.9
122 loizides 1.2 return TAMSelector::Notify();
123     }
124    
125 loizides 1.4 //--------------------------------------------------------------------------------------------------
126 loizides 1.12 Bool_t Selector::Process(Long64_t entry)
127     {
128     // Process an element of the tree.
129    
130     Bool_t ret = TAMSelector::Process(entry);
131     fTrash.Delete();
132     return ret;
133     }
134    
135     //--------------------------------------------------------------------------------------------------
136 loizides 1.7 void Selector::SearchOutputMods(const TAModule *mod)
137     {
138     // Search for output module among list of modules.
139    
140     if (!mod)
141     return;
142    
143     const OutputMod *o = dynamic_cast<const OutputMod*>(mod);
144     if (o)
145     fOutputMods.Add(const_cast<OutputMod*>(o));
146    
147     const TList *tasks = mod->GetSubModules();
148     if (!tasks)
149     return;
150    
151     TIter it(tasks->MakeIterator());
152     TObject *ob = 0;
153     while ((ob=it.Next()) != 0) {
154     TAModule *nmod = dynamic_cast<TAModule*>(ob);
155     if (nmod)
156     SearchOutputMods(nmod);
157     }
158     }
159    
160     //--------------------------------------------------------------------------------------------------
161 loizides 1.6 void Selector::SlaveBegin(TTree *tree)
162 loizides 1.2 {
163     // The SlaveBegin() function is called after the Begin() function and can be used to setup
164     // analysis on the slaves. Here, we request the event header branch.
165    
166     if (fDoRunInfo) {
167 loizides 1.7 ReqBranch(fEvtHdrName, fEventHeader);
168 loizides 1.2 }
169    
170 loizides 1.7 SearchOutputMods(GetTopModule());
171    
172 loizides 1.2 TAMSelector::SlaveBegin(tree);
173     }
174    
175 loizides 1.4 //--------------------------------------------------------------------------------------------------
176 loizides 1.2 void Selector::UpdateRunInfo()
177     {
178     // Update the run info to be consistent with the information from the event header.
179    
180     fRunInfo = 0;
181    
182     if (!fRunTree)
183     return;
184    
185     Int_t ret = fRunTree->GetEvent(fEventHeader->RunEntry());
186     if (ret<0 || fRunInfo==0) {
187 bendavid 1.13 Error("UpdateRunInfo", "Error updating run info for run %d, entry %d, return value %d",
188 loizides 1.2 fEventHeader->RunNum(), fEventHeader->RunEntry(), ret);
189     return;
190     }
191    
192     fCurRunNum = fEventHeader->RunNum();
193     if (!ValidRunInfo()) {
194     Error("UpdateRunInfo", "Error updating run info, run values do not match %d %d",
195     fCurRunNum, fRunInfo->RunNum());
196     return;
197     }
198     }
199    
200 loizides 1.4 //--------------------------------------------------------------------------------------------------
201 loizides 1.2 void Selector::UpdateRunInfoTree()
202     {
203     // Get the run info tree from current file and set our branches.
204    
205     // first erase previous entries
206     fRunTree = 0;
207     fRunInfo = 0;
208     fCurRunNum = UInt_t(-1);
209     fLATree = 0;
210     fLAHeader = 0;
211    
212     // get current file and retrieve trees
213     TFile *f = GetCurrentFile();
214     if (!f) {
215     Error("UpdateRunInfoTree", "Ptr to current file is null");
216     return;
217     }
218    
219     // run info tree
220 loizides 1.5 fRunTree = dynamic_cast<TTree*>(f->Get(fRunTreeName));
221 loizides 1.2 if (!fRunTree) {
222 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find run info tree with name %s", fRunTreeName.Data());
223 loizides 1.2 }
224    
225     // set branches
226 loizides 1.5 if (fRunTree->GetBranch(fRunInfoName)) {
227     fRunTree->SetBranchAddress(fRunInfoName, &fRunInfo);
228 loizides 1.2 } else {
229 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find run info branch with name %s", fRunInfoName.Data());
230 loizides 1.2 }
231    
232     // look-ahead tree
233 loizides 1.5 fLATree = dynamic_cast<TTree*>(f->Get(fLATreeName));
234 loizides 1.2 if (!fLATree) {
235 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find look-ahead tree with name %s", fLATreeName.Data());
236 loizides 1.2 }
237    
238     // set branches
239 loizides 1.5 if (fLATree->GetBranch(fLAHdrName)) {
240     fLATree->SetBranchAddress(fLAHdrName, &fLAHeader);
241 loizides 1.2 } else {
242 loizides 1.11 Fatal("UpdateRunInfoTree", "Cannot find look-ahead branch with name %s", fLAHdrName.Data());
243 loizides 1.2 }
244     }