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

Last change on this file since 4823 was 4766, 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::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 fList = (MTask*)plist->FindObject("MTaskList");
229
230 fChains->Delete();
231
232 Int_t i=0;
233
234 TIter NextT(fTrees->GetList());
235 MReadTree *tree=0;
236 while ((tree=(MReadTree*)NextT()))
237 {
238 if (!((TChain*)tree->fChain)->GetFile())
239 {
240 *fLog << warn << "No files or no tree '" << tree->GetName() << "'... skipped." << endl;
241 fTrees->RemoveFromList(tree);
242 continue;
243 }
244
245 if (tree->GetEntries()==0)
246 {
247 *fLog << warn << "No events in tree '" << tree->GetName() << "'... skipped." << endl;
248 fTrees->RemoveFromList(tree);
249 continue;
250 }
251
252 TString tn(tree->GetTitle());
253 if (tn.IsNull())
254 {
255 tn += "MTime";
256 tn += tree->GetName();
257 tn += ".";
258 }
259
260 TString tn2(tn);
261 tn2 += "*";
262
263 // FIXME: Should be tree->AddToBranchList such that
264 // each run a new 'table' is created, but
265 // MRead is searching for MTaskList in the
266 // parameter list.
267 //AddToBranchList((const char*)tn2);
268
269 //
270 // SetBranchStatus wants to have a pointer to a pointer
271 //
272 MTime **tx = new MTime*;
273 *tx = new MTime;
274
275 TChain *c=new TChain(tree->GetName());
276 c->SetBranchStatus("*", 0);
277 c->SetBranchAddress(tn, tx);
278 tn+="*";
279 c->SetBranchStatus(tn, 1);
280 c->Add((TChain*)tree->fChain);
281 c->GetEntry(0);
282
283 fChains->Add(c);
284
285 i++;
286 }
287
288 if (i==0)
289 {
290 *fLog << err << "Files do not contain any valid tree... abort." << endl;
291 return kFALSE;
292 }
293
294 fPosEntry.Set(i);
295
296 return fTrees->CallPreProcess(plist);
297}
298
299// --------------------------------------------------------------------------
300//
301// Return the MTime corresponding to this TChain...
302//
303MTime** MReadReports::GetTime(TChain *c) const
304{
305 TChainElement *e=(TChainElement*)c->GetStatus()->At(1);
306 return (MTime**)e->GetBaddress();
307}
308
309// --------------------------------------------------------------------------
310//
311// Do not use if fChains->GetSize()==0 !!!
312//
313Int_t MReadReports::FindNextTime()
314{
315 Int_t i=0;
316
317 TIter NextC(fChains);
318 TChain *c=0;
319
320 Int_t nmin=0;
321 MTime tmin(**GetTime((TChain*)NextC()));
322
323 while ((c=(TChain*)NextC()))
324 {
325 MTime &t = **GetTime(c);
326 i++;
327
328 if (t >= tmin)
329 continue;
330
331 tmin = t;
332 nmin = i;
333 }
334 return nmin;
335}
336
337/*
338Bool_t MReadReports::Notify()
339{
340 Bool_t same = kTRUE;
341 for (int i=1; i<fPosTree.GetSize(); i++)
342 if (fPosTree[i]!=fPosTree[0])
343 {
344 same = kFALSE;
345 break;
346 }
347
348 Int_t tn = chain->GetTreeNumber();
349
350 Bool_t read=kFALSE;
351 if (fPosTree[nmin] != tn)
352 {
353 fPosTree[nmin] = tn;
354 read = kTRUE;
355 }
356
357 if (!same || !read)
358 return kTRUE;
359
360
361 *fLog << dbg << "Read Run Headers!" << endl;
362
363 return kTRUE;
364}
365*/
366
367// --------------------------------------------------------------------------
368//
369// Check which is the next tree to read from. Read an event from this tree.
370// Sets the StreamId accordingly.
371//
372Int_t MReadReports::Process()
373{
374 while (fChains->GetSize())
375 {
376 const Int_t nmin=FindNextTime();
377
378 TChain *chain = (TChain*)fChains->At(nmin);
379
380 MTask *task = (MTask*)fTrees->GetList()->At(nmin);
381
382 //Int_t before = chain->GetTreeNumber();
383 if (chain->GetEntry(++fPosEntry[nmin])>0)
384 {
385 const Int_t rc = task->CallProcess();
386 if (rc)
387 {
388 fList->SetStreamId(task->GetName());
389 return rc;
390 }
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, title, GetCpuTime());
428}
Note: See TracBrowser for help on using the repository browser.