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

Last change on this file since 3497 was 3497, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 14.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, 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 fNumExecutions(0), 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 fNumExecutions = 0;
197 fStopwatch->Reset();
198
199 *fLog << all << fName << "... " << flush;
200 if (fDisplay)
201 fDisplay->SetStatusLine2(*this);
202
203 switch (PreProcess(plist))
204 {
205 case kFALSE:
206 return kFALSE;
207
208 case kTRUE:
209 fIsPreprocessed = kTRUE;
210 return kTRUE;
211
212 case kSKIP:
213 return kSKIP;
214 }
215
216 *fLog << err << dbginf << "PreProcess of " << GetDescriptor();
217 *fLog << " returned an unknown value... aborting." << endl;
218
219 return kFALSE;
220}
221
222// --------------------------------------------------------------------------
223//
224// Mapper function for Process.
225// Executes Process dependent on the existance of a filter and its possible
226// return value.
227// If Process is executed, the execution counter is increased.
228// Count cpu consumtion time.
229//
230Int_t MTask::CallProcess()
231{
232 //
233 // Check for the existance of a filter. If a filter is existing
234 // check for its value. If the value is kFALSE don't execute
235 // this task.
236 //
237 const Bool_t exec = fFilter ? fFilter->IsConditionTrue() : kTRUE;
238
239 if (!exec)
240 return kTRUE;
241
242 fNumExecutions++;
243
244 fStopwatch->Start(kFALSE);
245 const Int_t rc = Process();
246 fStopwatch->Stop();
247
248 return rc;
249}
250
251// --------------------------------------------------------------------------
252//
253// Mapper function for PreProcess.
254// Calls Postprocess dependent on the state of the preprocessed flag,
255// resets this flag.
256//
257Int_t MTask::CallPostProcess()
258{
259 if (!fIsPreprocessed)
260 return kTRUE;
261
262 fIsPreprocessed = kFALSE;
263
264 *fLog << all << fName << "... " << flush;
265 if (fDisplay)
266 fDisplay->SetStatusLine2(*this);
267
268 return PostProcess();
269}
270
271// --------------------------------------------------------------------------
272//
273// This is reinit function
274//
275// This function is called asynchronously if the tasks in the tasklist need
276// reinitialization. This for example happens when the eventloop switches
277// from one group of events to another one (eg. switching between events
278// of different runs means reading a new run header and a new run header
279// may mean that some value must be reinitialized)
280//
281// the virtual implementation returns kTRUE
282//
283Bool_t MTask::ReInit(MParList *pList)
284{
285 return kTRUE;
286}
287
288// --------------------------------------------------------------------------
289//
290// This is processed before the eventloop starts
291//
292// It is the job of the PreProcess to connect the tasks
293// with the right container in the parameter list.
294//
295// the virtual implementation returns kTRUE
296//
297Int_t MTask::PreProcess(MParList *pList)
298{
299 return kTRUE;
300}
301
302// --------------------------------------------------------------------------
303//
304// This is processed for every event in the eventloop
305//
306// the virtual implementation returns kTRUE
307//
308Int_t MTask::Process()
309{
310 return kTRUE;
311}
312
313// --------------------------------------------------------------------------
314//
315// This is processed after the eventloop starts
316//
317// the virtual implementation returns kTRUE
318//
319Int_t MTask::PostProcess()
320{
321 return kTRUE;
322}
323
324// --------------------------------------------------------------------------
325//
326// Returns the name of the object. If the name of the object is not the
327// class name it returns the object name and in []-brackets the class name.
328// If a serial number is set (!=0) the serial number is added to the
329// name (eg. ;1)
330//
331const char *MTask::GetDescriptor() const
332{
333 //
334 // Because it returns a (const char*) we cannot return a casted
335 // local TString. The pointer would - immediatly after return -
336 // point to a random memory segment, because the TString has gone.
337 //
338 if (fName==ClassName())
339 return fSerialNumber==0 ? ClassName() : Form("%s;%d", ClassName(), fSerialNumber);
340
341 return fSerialNumber>0 ?
342 Form("%s;%d [%s]", fName.Data(), fSerialNumber, ClassName()) :
343 Form("%s [%s]", fName.Data(), ClassName());
344}
345
346// --------------------------------------------------------------------------
347//
348// Return total CPU execution time of task
349//
350Double_t MTask::GetCpuTime() const
351{
352 return fStopwatch->CpuTime();
353}
354
355// --------------------------------------------------------------------------
356//
357// Return total real execution time of task
358//
359Double_t MTask::GetRealTime() const
360{
361 return fStopwatch->RealTime();
362}
363
364// --------------------------------------------------------------------------
365//
366// Prints the relative time spent in Process() (relative means relative to
367// its parent Tasklist) and the number of times Process() was executed.
368// For convinience the lvl argument results in a number of spaces at the
369// beginning of the line. So that the structur of a tasklist can be
370// identified. If a Tasklist or task has filter applied the name of the
371// filter is printer in <>-brackets behind the number of executions.
372// Use MTaskList::PrintStatistics without an argument.
373// For tasks which don't overwrite Process() no action is perfomed.
374//
375void MTask::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
376{
377 if (!OverwritesProcess())
378 return;
379
380 *fLog << all << setfill(' ') << setw(lvl) << " ";
381 if (fStopwatch->CpuTime()>0 && time>0)
382 *fLog << setw(3) << (Int_t)(fStopwatch->CpuTime()/time*100+.5) << "% ";
383 else
384 *fLog << " ";
385 *fLog << GetDescriptor() << "\t";
386 *fLog << dec << fNumExecutions;
387 if (fFilter)
388 *fLog << " <" << fFilter->GetName() << ">";
389 if (title)
390 *fLog << "\t" << fTitle;
391 *fLog << endl;
392}
393
394// --------------------------------------------------------------------------
395//
396// First call MParContainer::SavePrimitive which should stream the primitive
397// to the output stream. Then, if a filter is set, stream first the filter
398// and afterwards set the filter for this task.
399//
400void MTask::SavePrimitive(ofstream &out, Option_t *o)
401{
402 MParContainer::SavePrimitive(out);
403 if (!fFilter)
404 return;
405
406 /*
407 If we don't stream filter which are not in the task list itself
408 (which means: already streamed) we may be able to use
409 SavePrimitive as some kind of validity check for the macros
410
411 fFilter->SavePrimitive(out);
412 */
413 out << " " << GetUniqueName() << ".SetFilter(&" << fFilter->GetUniqueName() <<");" << endl;
414 if (fSerialNumber>0)
415 out << " " << GetUniqueName() << ".SetSerialNumber(" << fSerialNumber <<");" << endl;
416}
417
418// --------------------------------------------------------------------------
419//
420// Check whether the class given in the argument overwrites MTask::Process.
421// This function calls itself recursively. If you want to call it,
422// leave out the argument.
423//
424Bool_t MTask::OverwritesProcess(TClass *cls) const
425{
426 if (!cls)
427 cls = IsA();
428
429 //
430 // Check whether we reached the base class MTask
431 //
432 if (TString(cls->GetName())=="MTask")
433 return kFALSE;
434
435 //
436 // Check whether the class cls overwrites Process
437 //
438 if (cls->GetMethodAny("Process"))
439 return kTRUE;
440
441 //
442 // If the class itself doesn't overload it check all it's base classes
443 //
444 TBaseClass *base=NULL;
445 TIter NextBase(cls->GetListOfBases());
446 while ((base=(TBaseClass*)NextBase()))
447 {
448 if (OverwritesProcess(base->GetClassPointer()))
449 return kTRUE;
450 }
451
452 return kFALSE;
453}
454
455void MTask::SetDisplay(MStatusDisplay *d)
456{
457 if (fFilter)
458 fFilter->SetDisplay(d);
459 MParContainer::SetDisplay(d);
460}
Note: See TracBrowser for help on using the repository browser.