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

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