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

Last change on this file since 4539 was 4183, checked in by reyes, 20 years ago
*** empty log message ***
File size: 10.2 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 "MTaskList.h"
73
74#include "MReadMarsFile.h"
75
76ClassImp(MReadReports);
77
78using namespace std;
79
80// --------------------------------------------------------------------------
81//
82// Default constructor. Set fName and fTitle. Instatiate fTrees and fChains.
83// Call SetOwner for fTrees and fChains
84//
85MReadReports::MReadReports() : fEnableAutoScheme(kFALSE)
86{
87 fName = "MRead";
88 fTitle = "Reads events and reports from a root file ordered in time";
89
90 fTrees = new MTaskList("MReadReports");
91 fChains = new TList;
92
93 fTrees->SetOwner();
94 fChains->SetOwner();
95}
96
97// --------------------------------------------------------------------------
98//
99// Destructor, delete everything which was allocated by this task...
100//
101MReadReports::~MReadReports()
102{
103 TObject *o=0;
104 TIter NextC(fChains);
105 while ((o=NextC()))
106 {
107 delete *GetTime((TChain*)o);
108 delete GetTime((TChain*)o);
109 }
110
111 delete fTrees;
112 delete fChains;
113}
114
115void MReadReports::AddToBranchList(const char *name)
116{
117 MTask::AddToBranchList(name);
118}
119
120// --------------------------------------------------------------------------
121//
122// Schedule the contents of this tree for reading. As a default the time
123// branch which is used for the ordering is assumed to by "MTime"+tree.
124// If this is not the case you can overwrite the default specifying the
125// name in time.
126//
127// All calls to AddTree _must_ be BEFORE the calls to AddFile!
128//
129// To be done: A flag(?) telling whether the headers can be skipped.
130//
131void MReadReports::AddTree(const char *tree, const char *time, Bool_t master)
132{
133 /*
134 if (fTrees->GetNumTasks()>0)
135 {
136 *fLog << warn << "WARNING - AddTree must be called before AddFile... ignored." << endl;
137 *fLog << dbg << fTrees->GetNumTasks() << endl;
138 return kFALSE;
139 }
140 */
141
142 if (master && TestBit(kHasMaster))
143 {
144 *fLog << warn << GetDescriptor() << " already has a master tree... ignored." << endl;
145 master = kFALSE;
146 }
147
148 MReadTree *t = master ? new MReadMarsFile(tree) : new MReadTree(tree);
149 t->SetName(tree);
150 t->SetTitle(time?time:"");
151 if (master)
152 t->SetBit(kHasMaster);
153
154 if (!fEnableAutoScheme)
155 t->DisableAutoScheme();
156
157 //FIXME!
158 //t->DisableAutoScheme();
159
160 fTrees->AddToList(t);
161 // return kTRUE;
162}
163
164// --------------------------------------------------------------------------
165//
166// Schedule a file or several files (using widcards) for reading.
167//
168// All calls to AddTree _must_ be BEFORE the calls to AddFile!
169//
170Int_t MReadReports::AddFile(const char *fname, Int_t entries)
171{
172 Int_t n=0;
173
174 TIter NextT(fTrees->GetList());
175 MReadTree *tree=0;
176 while ((tree=(MReadTree*)NextT()))
177 n += tree->AddFile(fname, entries);
178
179 return n;
180}
181
182// --------------------------------------------------------------------------
183//
184// Find MTaskList and store a pointer to it in fList.
185// Delete all entries in fChains.
186// Create all chains to read the time in the trees in advance.
187// Enable only the time-branch in this chains.
188// PreProcess fTrees (a MTaskList storing MReadTree tasks for reading)
189//
190Int_t MReadReports::PreProcess(MParList *plist)
191{
192 fList = (MTask*)plist->FindObject("MTaskList");
193
194 fChains->Delete();
195
196 Int_t i=0;
197
198 TIter NextT(fTrees->GetList());
199 MReadTree *tree=0;
200 while ((tree=(MReadTree*)NextT()))
201 {
202 if (!((TChain*)tree->fChain)->GetFile())
203 {
204 *fLog << warn << "No files or no tree '" << tree->GetName() << "'... skipped." << endl;
205 fTrees->RemoveFromList(tree);
206 continue;
207 }
208
209 if (tree->GetEntries()==0)
210 {
211 *fLog << warn << "No events in tree '" << tree->GetName() << "'... skipped." << endl;
212 fTrees->RemoveFromList(tree);
213 continue;
214 }
215
216 TString tn(tree->GetTitle());
217 if (tn.IsNull())
218 {
219 tn += "MTime";
220 tn += tree->GetName();
221 tn += ".";
222 }
223
224 TString tn2(tn);
225 tn2 += "*";
226
227 // FIXME: Should be tree->AddToBranchList such that
228 // each run a new 'table' is created, but
229 // MRead is searching for MTaskList in the
230 // parameter list.
231 //AddToBranchList((const char*)tn2);
232
233 //
234 // SetBranchStatus wants to have a pointer to a pointer
235 //
236 MTime **tx = new MTime*;
237 *tx = new MTime;
238
239 TChain *c=new TChain(tree->GetName());
240 c->SetBranchStatus("*", 0);
241 c->SetBranchAddress(tn, tx);
242 tn+="*";
243 c->SetBranchStatus(tn, 1);
244 c->Add((TChain*)tree->fChain);
245 c->GetEntry(0);
246
247 fChains->Add(c);
248
249 i++;
250 }
251
252 if (i==0)
253 {
254 *fLog << err << "Files do not contain any valid tree... abort." << endl;
255 return kFALSE;
256 }
257
258 fPosEntry.Set(i);
259
260 return fTrees->CallPreProcess(plist);
261}
262
263// --------------------------------------------------------------------------
264//
265// Return the MTime corresponding to this TChain...
266//
267MTime** MReadReports::GetTime(TChain *c) const
268{
269 TChainElement *e=(TChainElement*)c->GetStatus()->At(1);
270 return (MTime**)e->GetBaddress();
271}
272
273// --------------------------------------------------------------------------
274//
275// Do not use if fChains->GetSize()==0 !!!
276//
277Int_t MReadReports::FindNextTime()
278{
279 Int_t i=0;
280
281 TIter NextC(fChains);
282 TChain *c=0;
283
284 Int_t nmin=0;
285 MTime tmin(**GetTime((TChain*)NextC()));
286
287 while ((c=(TChain*)NextC()))
288 {
289 MTime &t = **GetTime(c);
290 i++;
291
292 if (t >= tmin)
293 continue;
294
295 tmin = t;
296 nmin = i;
297 }
298 return nmin;
299}
300
301/*
302Bool_t MReadReports::Notify()
303{
304 Bool_t same = kTRUE;
305 for (int i=1; i<fPosTree.GetSize(); i++)
306 if (fPosTree[i]!=fPosTree[0])
307 {
308 same = kFALSE;
309 break;
310 }
311
312 Int_t tn = chain->GetTreeNumber();
313
314 Bool_t read=kFALSE;
315 if (fPosTree[nmin] != tn)
316 {
317 fPosTree[nmin] = tn;
318 read = kTRUE;
319 }
320
321 if (!same || !read)
322 return kTRUE;
323
324
325 *fLog << dbg << "Read Run Headers!" << endl;
326
327 return kTRUE;
328}
329*/
330
331// --------------------------------------------------------------------------
332//
333// Check which is the next tree to read from. Read an event from this tree.
334// Sets the StreamId accordingly.
335//
336Int_t MReadReports::Process()
337{
338 while (fChains->GetSize())
339 {
340 const Int_t nmin=FindNextTime();
341
342 TChain *chain = (TChain*)fChains->At(nmin);
343
344 MTask *task = (MTask*)fTrees->GetList()->At(nmin);
345
346 //Int_t before = chain->GetTreeNumber();
347 if (chain->GetEntry(++fPosEntry[nmin])>0)
348 {
349 const Int_t rc = task->CallProcess();
350 if (rc)
351 {
352 fList->SetStreamId(task->GetName());
353 return rc;
354 }
355 }
356
357 *fLog << dbg << "Removing chain " << chain->GetName() << " from list." << endl;
358
359 delete *GetTime(chain); // Delete MTime*
360 delete GetTime(chain); // Delete MTime-instance
361 delete fChains->Remove(chain); // Remove chain from TList
362
363 // FIXME: Maybe MTaskList should have a member function to
364 // reorder the tasks?
365
366 // Move this task to the end of the list so that nmin still
367 // corresponds to the correct task in the list.
368 const_cast<TList*>(fTrees->GetList())->Remove(task);
369 const_cast<TList*>(fTrees->GetList())->AddLast(task);
370 }
371
372 return kFALSE;
373}
374
375// --------------------------------------------------------------------------
376//
377// PostProcess all MReadTree tasks in fTrees.
378//
379Int_t MReadReports::PostProcess()
380{
381 return fTrees->CallPostProcess();
382}
383
384// --------------------------------------------------------------------------
385//
386// PrintStatistics of this task and of the MReadTree tasks in fTress
387//
388void MReadReports::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
389{
390 MRead::PrintStatistics(lvl, title, time);
391 fTrees->PrintStatistics(lvl, title, GetCpuTime());
392}
Note: See TracBrowser for help on using the repository browser.