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

Last change on this file since 4909 was 4895, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 10.9 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::GetFileName() 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
200// --------------------------------------------------------------------------
201//
202// Schedule a file or several files (using widcards) for reading.
203//
204// All calls to AddTree _must_ be BEFORE the calls to AddFile!
205//
206Int_t MReadReports::AddFile(const char *fname, Int_t entries)
207{
208 Int_t n=0;
209
210 TIter NextT(fTrees->GetList());
211 MReadTree *tree=0;
212 while ((tree=(MReadTree*)NextT()))
213 n += tree->AddFile(fname, entries);
214
215 return n;
216}
217
218// --------------------------------------------------------------------------
219//
220// Find MTaskList and store a pointer to it in fList.
221// Delete all entries in fChains.
222// Create all chains to read the time in the trees in advance.
223// Enable only the time-branch in this chains.
224// PreProcess fTrees (a MTaskList storing MReadTree tasks for reading)
225//
226Int_t MReadReports::PreProcess(MParList *plist)
227{
228 fChains->Delete();
229
230 Int_t i=0;
231
232 TIter NextT(fTrees->GetList());
233 MReadTree *tree=0;
234 while ((tree=(MReadTree*)NextT()))
235 {
236 if (!((TChain*)tree->fChain)->GetFile())
237 {
238 *fLog << warn << "No files or no tree '" << tree->GetName() << "'... skipped." << endl;
239 fTrees->RemoveFromList(tree);
240 continue;
241 }
242
243 if (tree->GetEntries()==0)
244 {
245 *fLog << warn << "No events in tree '" << tree->GetName() << "'... skipped." << endl;
246 fTrees->RemoveFromList(tree);
247 continue;
248 }
249
250 TString tn(tree->GetTitle());
251 if (tn.IsNull())
252 {
253 tn += "MTime";
254 tn += tree->GetName();
255 tn += ".";
256 }
257
258 TString tn2(tn);
259 tn2 += "*";
260
261 // FIXME: Should be tree->AddToBranchList such that
262 // each run a new 'table' is created, but
263 // MRead is searching for MTaskList in the
264 // parameter list.
265 //AddToBranchList((const char*)tn2);
266
267 //
268 // SetBranchStatus wants to have a pointer to a pointer
269 //
270 MTime **tx = new MTime*;
271 *tx = new MTime;
272
273 TChain *c=new TChain(tree->GetName());
274 c->SetBranchStatus("*", 0);
275 c->SetBranchAddress(tn, tx);
276 tn+="*";
277 c->SetBranchStatus(tn, 1);
278 c->Add((TChain*)tree->fChain);
279 c->GetEntry(0);
280
281 fChains->Add(c);
282
283 i++;
284 }
285
286 if (i==0)
287 {
288 *fLog << err << "Files do not contain any valid tree... abort." << endl;
289 return kFALSE;
290 }
291
292 fPosEntry.Set(i);
293
294 return fTrees->CallPreProcess(plist);
295}
296
297// --------------------------------------------------------------------------
298//
299// Return the MTime corresponding to this TChain...
300//
301MTime** MReadReports::GetTime(TChain *c) const
302{
303 TChainElement *e=(TChainElement*)c->GetStatus()->At(1);
304 return (MTime**)e->GetBaddress();
305}
306
307// --------------------------------------------------------------------------
308//
309// Do not use if fChains->GetSize()==0 !!!
310//
311Int_t MReadReports::FindNextTime()
312{
313 Int_t i=0;
314
315 TIter NextC(fChains);
316 TChain *c=0;
317
318 Int_t nmin=0;
319 MTime tmin(**GetTime((TChain*)NextC()));
320
321 while ((c=(TChain*)NextC()))
322 {
323 MTime &t = **GetTime(c);
324 i++;
325
326 if (t >= tmin)
327 continue;
328
329 tmin = t;
330 nmin = i;
331 }
332 return nmin;
333}
334
335/*
336Bool_t MReadReports::Notify()
337{
338 Bool_t same = kTRUE;
339 for (int i=1; i<fPosTree.GetSize(); i++)
340 if (fPosTree[i]!=fPosTree[0])
341 {
342 same = kFALSE;
343 break;
344 }
345
346 Int_t tn = chain->GetTreeNumber();
347
348 Bool_t read=kFALSE;
349 if (fPosTree[nmin] != tn)
350 {
351 fPosTree[nmin] = tn;
352 read = kTRUE;
353 }
354
355 if (!same || !read)
356 return kTRUE;
357
358
359 *fLog << dbg << "Read Run Headers!" << endl;
360
361 return kTRUE;
362}
363*/
364
365// --------------------------------------------------------------------------
366//
367// Check which is the next tree to read from. Read an event from this tree.
368// Sets the StreamId accordingly.
369//
370Int_t MReadReports::Process()
371{
372 while (fChains->GetSize())
373 {
374 const Int_t nmin=FindNextTime();
375
376 TChain *chain = (TChain*)fChains->At(nmin);
377
378 MTask *task = (MTask*)fTrees->GetList()->At(nmin);
379
380 //Int_t before = chain->GetTreeNumber();
381 if (chain->GetEntry(++fPosEntry[nmin])>0)
382 {
383 const Int_t rc = task->CallProcess();
384 if (rc)
385 return rc;
386 }
387
388 *fLog << dbg << "Removing chain " << chain->GetName() << " from list." << endl;
389
390 delete *GetTime(chain); // Delete MTime*
391 delete GetTime(chain); // Delete MTime-instance
392 delete fChains->Remove(chain); // Remove chain from TList
393
394 // FIXME: Maybe MTaskList should have a member function to
395 // reorder the tasks?
396
397 // Move this task to the end of the list so that nmin still
398 // corresponds to the correct task in the list.
399 const_cast<TList*>(fTrees->GetList())->Remove(task);
400 const_cast<TList*>(fTrees->GetList())->AddLast(task);
401 }
402
403 return kFALSE;
404}
405
406// --------------------------------------------------------------------------
407//
408// PostProcess all MReadTree tasks in fTrees.
409//
410Int_t MReadReports::PostProcess()
411{
412 return fTrees->CallPostProcess();
413}
414
415// --------------------------------------------------------------------------
416//
417// PrintStatistics of this task and of the MReadTree tasks in fTress
418//
419void MReadReports::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
420{
421 MRead::PrintStatistics(lvl, title, time);
422 fTrees->PrintStatistics(lvl, title, GetCpuTime());
423}
Note: See TracBrowser for help on using the repository browser.