source: trunk/MagicSoft/Mars/mfileio/MReadReports.cc@ 2606

Last change on this file since 2606 was 2604, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.1 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 11/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MReadReports
28//
29// Read from a file events from different trees ordered in time, eg:
30//
31// Having a file with:
32//
33// Tree1 Tree2 Tree3
34// ------------ ------------ -----------
35// (0) MTime[0]
36// (0) MTime[1]
37// (1) MTime[2]
38// (2) MTime[3]
39// (0) MTime[1]
40// (3) MTime[4]
41//
42// MReadReports will read the events in the tree in the following order:
43// <0> (0) from Tree1
44// <1> (0) from Tree2
45// <2> (1) from Tree1
46// <3> (2) from Tree1
47// <4> (0) from Tree3
48// <5> (3) from Tree1
49// ...
50//
51// To tell MReadReports which Trees to read use: MReadReports::AddTree()
52// To schedule a file for reading use MReadReports::AddFile()
53//
54// All calls to AddTree _must_ be before the calls to AddFile!
55//
56// After reading from a tree with the name 'TreeName' the stream id of
57// the main tasklist ('MTaskList' found in MParList in PreProcess) is
58// set to this name. This means that only tasks having this stream id
59// are executed.
60//
61/////////////////////////////////////////////////////////////////////////////
62#include "MReadReports.h"
63
64#include <TChain.h>
65#include <TChainElement.h>
66
67#include "MLog.h"
68#include "MLogManip.h"
69
70#include "MTime.h"
71#include "MParList.h"
72#include "MReadTree.h"
73#include "MTaskList.h"
74
75ClassImp(MReadReports);
76
77using namespace std;
78
79// --------------------------------------------------------------------------
80//
81// Default constructor. Set fName and fTitle. Instatiate fTrees and fChains.
82// Call SetOwner for fTrees and fChains
83//
84MReadReports::MReadReports() : fEnableAutoScheme(kFALSE)
85{
86 fName = "MRead";
87 fTitle = "Reads events and reports from a root file ordered in time";
88
89 fTrees = new MTaskList("MReadReports");
90 fChains = new TList;
91
92 fTrees->SetOwner();
93 fChains->SetOwner();
94}
95
96// --------------------------------------------------------------------------
97//
98// Destructor, delete everything which was allocated by this task...
99//
100MReadReports::~MReadReports()
101{
102 TObject *o=0;
103 TIter NextC(fChains);
104 while ((o=NextC()))
105 {
106 delete *GetTime((TChain*)o);
107 delete GetTime((TChain*)o);
108 }
109
110 delete fTrees;
111 delete fChains;
112}
113
114void MReadReports::AddToBranchList(const char *name)
115{
116 MTask::AddToBranchList(name);
117}
118
119// --------------------------------------------------------------------------
120//
121// Schedule the contents of this tree for reading. As a default the time
122// branch which is used for the ordering is assumed to by "MTime"+tree.
123// If this is not the case you can overwrite the default specifying the
124// name in time.
125//
126// All calls to AddTree _must_ be before the calls to AddFile!
127//
128void MReadReports::AddTree(const char *tree, const char *time)
129{
130 /*
131 if (fTrees->GetNumTasks()>0)
132 {
133 *fLog << warn << "WARNING - AddTree must be called before AddFile... ignored." << endl;
134 *fLog << dbg << fTrees->GetNumTasks() << endl;
135 return kFALSE;
136 }
137 */
138 MReadTree *t = new MReadTree(tree);
139 t->SetName(tree);
140 t->SetTitle(time?time:"");
141
142 if (!fEnableAutoScheme)
143 t->DisableAutoScheme();
144
145 //FIXME!
146 //t->DisableAutoScheme();
147
148 fTrees->AddToList(t);
149 // return kTRUE;
150}
151
152// --------------------------------------------------------------------------
153//
154// Schedule a file or several files (using widcards) for reading.
155//
156// All calls to AddTree _must_ be before the calls to AddFile!
157//
158Int_t MReadReports::AddFile(const char *fname, Int_t entries)
159{
160 Int_t n=0;
161
162 TIter NextT(fTrees->GetList());
163 MReadTree *tree=0;
164 while ((tree=(MReadTree*)NextT()))
165 n += tree->AddFile(fname, entries);
166
167 return n;
168}
169
170// --------------------------------------------------------------------------
171//
172// Find MTaskList and store a pointer to it in fList.
173// Delete all entries in fChains.
174// Create all chains to read the time in the trees in advance.
175// Enable only the time-branch in this chains.
176// PreProcess fTrees (a MTaskList storing MReadTree tasks for reading)
177//
178Int_t MReadReports::PreProcess(MParList *plist)
179{
180 fList = (MTask*)plist->FindObject("MTaskList");
181
182 fChains->Delete();
183
184 Int_t i=0;
185
186 TIter NextT(fTrees->GetList());
187 MReadTree *tree=0;
188 while ((tree=(MReadTree*)NextT()))
189 {
190 TString tn(tree->GetTitle());
191 if (tn.IsNull())
192 {
193 tn += "MTime";
194 tn += tree->GetName();
195 tn += ".";
196 }
197
198 TString tn2(tn);
199 tn2 += "*";
200
201 // FIXME: Should be tree->AddToBranchList such that
202 // each run a new 'table' is created, but
203 // MRead is searching for MTaskList in the
204 // parameter list.
205 //AddToBranchList((const char*)tn2);
206
207 //
208 // SetBranchStatus wants to have a pointer to a pointer
209 //
210 MTime **tx = new MTime*;
211 *tx = new MTime;
212
213 TChain *c=new TChain(tree->GetName());
214 c->SetBranchStatus("*", 0);
215 c->SetBranchAddress(tn, tx);
216 tn+="*";
217 c->SetBranchStatus(tn, 1);
218 c->Add((TChain*)tree->fChain);
219 c->GetEntry(0);
220
221 fChains->Add(c);
222
223 i++;
224 }
225
226 fPos.Set(i);
227
228 return fTrees->CallPreProcess(plist);
229}
230
231// --------------------------------------------------------------------------
232//
233// Return the MTime corresponding to this TChain...
234//
235MTime** MReadReports::GetTime(TChain *c) const
236{
237 TChainElement *e=(TChainElement*)c->GetStatus()->At(1);
238 return (MTime**)e->GetBaddress();
239}
240
241// --------------------------------------------------------------------------
242//
243// Check which is the next tree to read from. Read an event from this tree.
244// Sets the StreamId accordingly.
245//
246Int_t MReadReports::Process()
247{
248 while (fChains->GetSize())
249 {
250 Int_t i=0;
251
252 MTime tmin;
253
254 Int_t nmin=0;
255
256 TIter NextC(fChains);
257 TChain *c=0;
258 while ((c=(TChain*)NextC()))
259 {
260 MTime &t = **GetTime(c);
261
262 if (i==0)
263 tmin = t;
264
265 if (t < tmin)
266 {
267 tmin = t;
268 nmin = i;
269 }
270 i++;
271 }
272
273 TChain *chain = (TChain*)fChains->At(nmin);
274
275 chain->GetEntry(++fPos[nmin]);
276
277 // FIXME: Use Stream ID and call CallProcess() ?
278 Bool_t rc = ((MTask*)fTrees->GetList()->At(nmin))->CallProcess();
279 if (rc)
280 {
281 fList->SetStreamId(fTrees->GetList()->At(nmin)->GetName());
282 return kTRUE;
283 }
284
285 delete *GetTime(chain);
286 delete GetTime(chain);
287 delete fChains->Remove(chain);
288 }
289
290 return kFALSE;
291}
292
293// --------------------------------------------------------------------------
294//
295// PostProcess all MReadTree tasks in fTrees.
296//
297Int_t MReadReports::PostProcess()
298{
299 return fTrees->CallPostProcess();
300}
301
302// --------------------------------------------------------------------------
303//
304// PrintStatistics of this task and of the MReadTree tasks in fTress
305//
306void MReadReports::PrintStatistics(const Int_t lvl, Bool_t title) const
307{
308 MRead::PrintStatistics(lvl, title);
309 fTrees->PrintStatistics(lvl, title);
310}
Note: See TracBrowser for help on using the repository browser.