source: trunk/MagicSoft/Mars/mbase/MTask.cc@ 3740

Last change on this file since 3740 was 3666, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 14.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, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MTask
28//
29// Base class for all tasks which can perfomed in a tasklist
30// For each event processed in the eventloop all the different
31// tasks in the tasklist will be processed.
32//
33// So all tasks must inherit from this baseclass.
34//
35// The inheritance from MInputStreamID is used to indicate the
36// type of event that this task is for. If it is "All" it is executed
37// independantly of the actual ID of the task list.
38//
39// Inside this abstract class, there are three fundamental function:
40//
41// - PreProcess(): executed before the eventloop starts. Here you
42// can initiate different things, open files, etc.
43// As an argument this function gets a pointer to the
44// parameter list. You can stop the execution by
45// returning kFALSE instead of kTRUE. If an error
46// occured and you return kFALSE make sure, that
47// any action is closed correctly and all newly
48// created object are deleted. The PostProcess in
49// such a case won't be executed by the Tasklist or
50// Eventloop.
51//
52// - Process(): executed for each event in the eventloop. Do it
53// one task after the other (as they occur in the
54// tasklist). Only the tasks with a Stream ID
55// which matches the actual ID of the tasklist
56// are executed. A task can return kFALSE to
57// stop the execuition of the tasklist or
58// kCONTINUE to skip the pending tasks. If you want
59// to stop the eventloop and wants the eventloop to
60// return the status 'failed' return kERROR.
61//
62// - ReInit() The idea is, that
63// a) we have one file per run
64// b) each file contains so called run-headers which
65// stores information 'per run', eg MRawRunHeader
66// or the bad pixels
67// c) this information must be evaluated somehow each
68// time a new file is opened.
69//
70// If you use MReadMarsFile or MCT1ReadPreProc it is
71// called each time a new file has been opened and the
72// new run headers have been read before the first
73// event of these file is preprocessed.
74//
75// - PostProcess(): executed after the eventloop. Here you can close
76// output files, start display of the run parameter,
77// etc. PostProcess is only executed in case of
78// PreProcess was successfull (returned kTRUE)
79//
80// Version 1:
81// ----------
82// - first version
83//
84// Version 2:
85// ----------
86// - added fSerialNumber
87//
88/////////////////////////////////////////////////////////////////////////////
89#include "MTask.h"
90
91#include <fstream>
92
93#include <TBaseClass.h> // OverwritesProcess
94#include <TStopwatch.h> // TStopwatch
95
96#include "MLog.h"
97#include "MLogManip.h"
98
99#include "MFilter.h"
100#include "MStatusDisplay.h"
101
102ClassImp(MTask);
103
104using namespace std;
105
106MTask::MTask(const char *name, const char *title)
107 : fFilter(NULL), fSerialNumber(0), fIsPreprocessed(kFALSE),
108 fStopwatch(0)
109{
110 fName = name ? name : "MTask";
111 fTitle = title ? title : "Base class for all tasks (dummy task).";
112
113 fListOfBranches = new TList;
114 fListOfBranches->SetOwner();
115
116 fStopwatch = new TStopwatch;
117}
118
119MTask::~MTask()
120{
121 delete fStopwatch;
122 delete fListOfBranches;
123}
124
125// --------------------------------------------------------------------------
126//
127// This adds a branch to the list for the auto enabeling schmeme.
128// This makes it possible for MReadTree to decide which branches
129// are really needed for the eventloop. Only the necessary branches
130// are read from disk which speeds up the calculation enormously.
131//
132// You can use TRegExp expressions like "*.fEnergy", but the
133// recommended method is to call this function for exactly all
134// branches you want to have, eg:
135// AddToBranchList("MMcTrig.fNumFirstLevel");
136// AddToBranchList("MMcTrig;1.fNumFirstLevel");
137// AddToBranchList("MMcTrig;2.fNumFirstLevel");
138//
139// We agreed on the convetion, that all branches are stored with
140// a trailing dot '.' so that always the Master Branch name
141// (eg. MMcTrig) is part of the branch name.
142//
143// Remark: The common place to call AddToBranchList is the
144// constructor of the derived classes (tasks)
145//
146void MTask::AddToBranchList(const char *b)
147{
148 if (fListOfBranches->FindObject(b))
149 return;
150
151 fListOfBranches->Add(new TNamed(b, ""));
152}
153
154// --------------------------------------------------------------------------
155//
156// Using this overloaded member function you may cascade several branches
157// in acomma seperated list, eg: "MMcEvt.fTheta,MMcEvt.fEnergy"
158//
159// For moredetailed information see AddToBranchList(const char *b);
160//
161void MTask::AddToBranchList(const TString &str)
162{
163 TString s = str;
164
165 while (!s.IsNull())
166 {
167 Int_t fst = s.First(',');
168
169 if (fst<0)
170 fst = s.Length();
171
172 AddToBranchList((const char*)TString(s(0, fst)));
173
174 s.Remove(0, fst+1);
175 }
176}
177
178// --------------------------------------------------------------------------
179//
180// Copy constructor.
181//
182MTask::MTask(MTask &t)
183{
184 fFilter = t.fFilter;
185 fListOfBranches->AddAll(t.fListOfBranches);
186}
187
188// --------------------------------------------------------------------------
189//
190// Mapper function for PreProcess.
191// Sets the preprocessed flag dependend on the return value of PreProcess.
192// Resets number of executions and cpu consumtion timer.
193//
194Int_t MTask::CallPreProcess(MParList *plist)
195{
196 fStopwatch->Reset();
197
198 *fLog << all << fName << "... " << flush;
199 if (fDisplay)
200 fDisplay->SetStatusLine2(*this);
201
202 switch (PreProcess(plist))
203 {
204 case kFALSE:
205 return kFALSE;
206
207 case kTRUE:
208 fIsPreprocessed = kTRUE;
209 return kTRUE;
210
211 case kSKIP:
212 return kSKIP;
213 }
214
215 *fLog << err << dbginf << "PreProcess of " << GetDescriptor();
216 *fLog << " returned an unknown value... aborting." << endl;
217
218 return kFALSE;
219}
220
221// --------------------------------------------------------------------------
222//
223// Mapper function for Process.
224// Executes Process dependent on the existance of a filter and its possible
225// return value.
226// If Process is executed, the execution counter is increased.
227// Count cpu consumtion time.
228//
229Int_t MTask::CallProcess()
230{
231 //
232 // Check for the existance of a filter. If a filter is existing
233 // check for its value. If the value is kFALSE don't execute
234 // this task.
235 //
236 const Bool_t exec = fFilter ? fFilter->IsConditionTrue() : kTRUE;
237
238 if (!exec)
239 return kTRUE;
240
241 fStopwatch->Start(kFALSE);
242 const Int_t rc = Process();
243 fStopwatch->Stop();
244
245 return rc;
246}
247
248// --------------------------------------------------------------------------
249//
250// Mapper function for PreProcess.
251// Calls Postprocess dependent on the state of the preprocessed flag,
252// resets this flag.
253//
254Int_t MTask::CallPostProcess()
255{
256 if (!fIsPreprocessed)
257 return kTRUE;
258
259 fIsPreprocessed = kFALSE;
260
261 *fLog << all << fName << "... " << flush;
262 if (fDisplay)
263 fDisplay->SetStatusLine2(*this);
264
265 return PostProcess();
266}
267
268// --------------------------------------------------------------------------
269//
270// This is reinit function
271//
272// This function is called asynchronously if the tasks in the tasklist need
273// reinitialization. This for example happens when the eventloop switches
274// from one group of events to another one (eg. switching between events
275// of different runs means reading a new run header and a new run header
276// may mean that some value must be reinitialized)
277//
278// the virtual implementation returns kTRUE
279//
280Bool_t MTask::ReInit(MParList *pList)
281{
282 return kTRUE;
283}
284
285// --------------------------------------------------------------------------
286//
287// This is processed before the eventloop starts
288//
289// It is the job of the PreProcess to connect the tasks
290// with the right container in the parameter list.
291//
292// the virtual implementation returns kTRUE
293//
294Int_t MTask::PreProcess(MParList *pList)
295{
296 return kTRUE;
297}
298
299// --------------------------------------------------------------------------
300//
301// This is processed for every event in the eventloop
302//
303// the virtual implementation returns kTRUE
304//
305Int_t MTask::Process()
306{
307 return kTRUE;
308}
309
310// --------------------------------------------------------------------------
311//
312// This is processed after the eventloop starts
313//
314// the virtual implementation returns kTRUE
315//
316Int_t MTask::PostProcess()
317{
318 return kTRUE;
319}
320
321// --------------------------------------------------------------------------
322//
323// Returns the name of the object. If the name of the object is not the
324// class name it returns the object name and in []-brackets the class name.
325// If a serial number is set (!=0) the serial number is added to the
326// name (eg. ;1)
327//
328const char *MTask::GetDescriptor() const
329{
330 //
331 // Because it returns a (const char*) we cannot return a casted
332 // local TString. The pointer would - immediatly after return -
333 // point to a random memory segment, because the TString has gone.
334 //
335 if (fName==ClassName())
336 return fSerialNumber==0 ? ClassName() : Form("%s;%d", ClassName(), fSerialNumber);
337
338 return fSerialNumber>0 ?
339 Form("%s;%d [%s]", fName.Data(), fSerialNumber, ClassName()) :
340 Form("%s [%s]", fName.Data(), ClassName());
341}
342
343// --------------------------------------------------------------------------
344//
345// Return the total number of calls to Process(). If Process() was not
346// called due to a set filter this is not counted.
347//
348UInt_t MTask::GetNumExecutions() const
349{
350 return (UInt_t)fStopwatch->Counter();
351}
352
353// --------------------------------------------------------------------------
354//
355// Return total CPU execution time in seconds of calls to Process().
356// If Process() was not called due to a set filter this is not counted.
357//
358Double_t MTask::GetCpuTime() const
359{
360 return fStopwatch->CpuTime();
361}
362
363// --------------------------------------------------------------------------
364//
365// Return total real execution time in seconds of calls to Process().
366// If Process() was not called due to a set filter this is not counted.
367//
368Double_t MTask::GetRealTime() const
369{
370 return fStopwatch->RealTime();
371}
372
373// --------------------------------------------------------------------------
374//
375// Prints the relative time spent in Process() (relative means relative to
376// its parent Tasklist) and the number of times Process() was executed.
377// Don't wonder if the sum of the tasks in a tasklist is not 100%,
378// because only the call to Process() of the task is measured. The
379// time of the support structure is ignored. The faster your analysis is
380// the more time is 'wasted' in the support structure.
381// Only the CPU time is displayed. This means that exspecially task
382// which have a huge part of file i/o will be underestimated in their
383// relative wasted time.
384// For convinience the lvl argument results in a number of spaces at the
385// beginning of the line. So that the structur of a tasklist can be
386// identified. If a Tasklist or task has filter applied the name of the
387// filter is printer in <>-brackets behind the number of executions.
388// Use MTaskList::PrintStatistics without an argument.
389// For tasks which don't overwrite Process() no action is perfomed.
390//
391void MTask::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
392{
393 if (!OverwritesProcess())
394 return;
395
396 *fLog << all << setfill(' ') << setw(lvl) << " ";
397
398 if (GetCpuTime()>0 && time>0 && GetCpuTime()>=0.001*time)
399 *fLog << Form("%5.1f", GetCpuTime()/time*100) << "% ";
400 else
401 *fLog << " ";
402 *fLog << GetDescriptor() << "\t";
403 *fLog << dec << GetNumExecutions();
404 if (fFilter)
405 *fLog << " <" << fFilter->GetName() << ">";
406 if (title)
407 *fLog << "\t" << fTitle;
408 *fLog << endl;
409}
410
411// --------------------------------------------------------------------------
412//
413// First call MParContainer::SavePrimitive which should stream the primitive
414// to the output stream. Then, if a filter is set, stream first the filter
415// and afterwards set the filter for this task.
416//
417void MTask::SavePrimitive(ofstream &out, Option_t *o)
418{
419 MParContainer::SavePrimitive(out);
420 if (!fFilter)
421 return;
422
423 /*
424 If we don't stream filter which are not in the task list itself
425 (which means: already streamed) we may be able to use
426 SavePrimitive as some kind of validity check for the macros
427
428 fFilter->SavePrimitive(out);
429 */
430 out << " " << GetUniqueName() << ".SetFilter(&" << fFilter->GetUniqueName() <<");" << endl;
431 if (fSerialNumber>0)
432 out << " " << GetUniqueName() << ".SetSerialNumber(" << fSerialNumber <<");" << endl;
433}
434
435// --------------------------------------------------------------------------
436//
437// Check whether the class given in the argument overwrites MTask::Process.
438// This function calls itself recursively. If you want to call it,
439// leave out the argument.
440//
441Bool_t MTask::OverwritesProcess(TClass *cls) const
442{
443 if (!cls)
444 cls = IsA();
445
446 //
447 // Check whether we reached the base class MTask
448 //
449 if (TString(cls->GetName())=="MTask")
450 return kFALSE;
451
452 //
453 // Check whether the class cls overwrites Process
454 //
455 if (cls->GetMethodAny("Process"))
456 return kTRUE;
457
458 //
459 // If the class itself doesn't overload it check all it's base classes
460 //
461 TBaseClass *base=NULL;
462 TIter NextBase(cls->GetListOfBases());
463 while ((base=(TBaseClass*)NextBase()))
464 {
465 if (OverwritesProcess(base->GetClassPointer()))
466 return kTRUE;
467 }
468
469 return kFALSE;
470}
471
472void MTask::SetDisplay(MStatusDisplay *d)
473{
474 if (fFilter)
475 fFilter->SetDisplay(d);
476 MParContainer::SetDisplay(d);
477}
Note: See TracBrowser for help on using the repository browser.