source: tags/Mars-V0.9.5/mbase/MTask.cc

Last change on this file was 7553, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 16.5 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//
81// Remark: Using a MTask in your tasklist doesn't make much sense,
82// because it is doing nothing. However it is a nice tool
83// to count something (exspecially if used together with a
84// filter)
85//
86//
87// Version 1:
88// ----------
89// - first version
90//
91// Version 2:
92// ----------
93// - added fSerialNumber
94//
95/////////////////////////////////////////////////////////////////////////////
96#include "MTask.h"
97
98#include <fstream>
99
100#include <TBaseClass.h> // OverwritesProcess
101#include <TStopwatch.h> // TStopwatch
102
103#include "MString.h"
104
105#include "MLog.h"
106#include "MLogManip.h"
107
108#include "MFilter.h"
109#include "MStatusDisplay.h"
110
111ClassImp(MTask);
112
113using namespace std;
114
115MTask::MTask(const char *name, const char *title)
116 : fFilter(NULL), fSerialNumber(0), fIsPreprocessed(kFALSE),
117 fStopwatch(0), fNumExec0(0), fAccelerator(0)
118{
119 fName = name ? name : "MTask";
120 fTitle = title ? title : "Base class for all tasks (dummy task).";
121
122 fListOfBranches = new TList;
123 fListOfBranches->SetOwner();
124
125 fStopwatch = new TStopwatch;
126}
127
128MTask::~MTask()
129{
130 delete fStopwatch;
131 delete fListOfBranches;
132}
133
134void MTask::SetFilter(MFilter *filter)
135{
136 fFilter=filter;
137 if (filter)
138 AddToBranchList(filter->GetDataMember());
139}
140
141// --------------------------------------------------------------------------
142//
143// This adds a branch to the list for the auto enabeling schmeme.
144// This makes it possible for MReadTree to decide which branches
145// are really needed for the eventloop. Only the necessary branches
146// are read from disk which speeds up the calculation enormously.
147//
148// You can use TRegExp expressions like "*.fEnergy", but the
149// recommended method is to call this function for exactly all
150// branches you want to have, eg:
151// AddToBranchList("MMcTrig.fNumFirstLevel");
152// AddToBranchList("MMcTrig;1.fNumFirstLevel");
153// AddToBranchList("MMcTrig;2.fNumFirstLevel");
154//
155// We agreed on the convetion, that all branches are stored with
156// a trailing dot '.' so that always the Master Branch name
157// (eg. MMcTrig) is part of the branch name.
158//
159// Remark: The common place to call AddToBranchList is the
160// constructor of the derived classes (tasks)
161//
162void MTask::AddToBranchList(const char *b)
163{
164 if (fListOfBranches->FindObject(b))
165 return;
166
167 fListOfBranches->Add(new TNamed(b, ""));
168}
169
170// --------------------------------------------------------------------------
171//
172// Using this overloaded member function you may cascade several branches
173// in acomma seperated list, eg: "MMcEvt.fTheta,MMcEvt.fEnergy"
174//
175// For moredetailed information see AddToBranchList(const char *b);
176//
177void MTask::AddToBranchList(const TString &str)
178{
179 TString s = str;
180
181 while (!s.IsNull())
182 {
183 Int_t fst = s.First(',');
184
185 if (fst<0)
186 fst = s.Length();
187
188 AddToBranchList((const char*)TString(s(0, fst)));
189
190 s.Remove(0, fst+1);
191 }
192}
193
194// --------------------------------------------------------------------------
195//
196// Copy constructor.
197//
198MTask::MTask(MTask &t)
199{
200 fFilter = t.fFilter;
201 fListOfBranches->AddAll(t.fListOfBranches);
202}
203
204// --------------------------------------------------------------------------
205//
206// Mapper function for PreProcess.
207// Sets the preprocessed flag dependend on the return value of PreProcess.
208// Resets number of executions and cpu consumtion timer.
209// If task has already been preprocessed return kTRUE.
210//
211Int_t MTask::CallPreProcess(MParList *plist)
212{
213 if (fIsPreprocessed)
214 return kTRUE;
215
216 // This does not reset the counter!
217 fStopwatch->Reset();
218 fNumExec0 = GetNumExecutionsTotal();
219
220 *fLog << all << GetDescriptor() << "... " << flush;
221 if (fDisplay)
222 fDisplay->SetStatusLine2(*this);
223
224 switch (PreProcess(plist))
225 {
226 case kFALSE:
227 return kFALSE;
228
229 case kTRUE:
230 fIsPreprocessed = kTRUE;
231 return kTRUE;
232
233 case kSKIP:
234 return kSKIP;
235 }
236
237 *fLog << err << dbginf << "PreProcess of " << GetDescriptor();
238 *fLog << " returned an unknown value... aborting." << endl;
239
240 return kFALSE;
241}
242
243// --------------------------------------------------------------------------
244//
245// Mapper function for Process.
246// Executes Process dependent on the existance of a filter and its possible
247// return value.
248// If Process is executed, the execution counter is increased.
249// Count cpu consumtion time.
250//
251Int_t MTask::CallProcess()
252{
253 //
254 // Check for the existance of a filter. If a filter is existing
255 // check for its value. If the value is kFALSE don't execute
256 // this task.
257 //
258 const Bool_t exec = fFilter ? fFilter->IsConditionTrue() : kTRUE;
259 if (!exec)
260 return kTRUE;
261
262 if (!HasAccelerator(kAccDontCount|kAccDontTime))
263 fStopwatch->Start(kFALSE);
264 const Int_t rc = Process();
265 if (!HasAccelerator(kAccDontTime))
266 fStopwatch->Stop();
267
268 return rc;
269}
270
271// --------------------------------------------------------------------------
272//
273// Mapper function for PreProcess.
274// Calls Postprocess dependent on the state of the preprocessed flag,
275// resets this flag.
276//
277Int_t MTask::CallPostProcess()
278{
279 if (!fIsPreprocessed)
280 return kTRUE;
281
282 fIsPreprocessed = kFALSE;
283
284 *fLog << all << GetDescriptor() << "... " << flush;
285 if (fDisplay)
286 fDisplay->SetStatusLine2(*this);
287
288 return PostProcess();
289}
290
291// --------------------------------------------------------------------------
292//
293// This is reinit function
294//
295// This function is called asynchronously if the tasks in the tasklist need
296// reinitialization. This for example happens when the eventloop switches
297// from one group of events to another one (eg. switching between events
298// of different runs means reading a new run header and a new run header
299// may mean that some value must be reinitialized)
300//
301// the virtual implementation returns kTRUE
302//
303Bool_t MTask::ReInit(MParList *pList)
304{
305 return kTRUE;
306}
307
308// --------------------------------------------------------------------------
309//
310// This is processed before the eventloop starts
311//
312// It is the job of the PreProcess to connect the tasks
313// with the right container in the parameter list.
314//
315// the virtual implementation returns kTRUE
316//
317Int_t MTask::PreProcess(MParList *pList)
318{
319 return kTRUE;
320}
321
322// --------------------------------------------------------------------------
323//
324// This is processed for every event in the eventloop
325//
326// the virtual implementation returns kTRUE
327//
328Int_t MTask::Process()
329{
330 return kTRUE;
331}
332
333// --------------------------------------------------------------------------
334//
335// This is processed after the eventloop starts
336//
337// the virtual implementation returns kTRUE
338//
339Int_t MTask::PostProcess()
340{
341 return kTRUE;
342}
343
344// --------------------------------------------------------------------------
345//
346// Returns the name of the object. If the name of the object is not the
347// class name it returns the object name and in []-brackets the class name.
348// If a serial number is set (!=0) the serial number is added to the
349// name (eg. ;1)
350//
351const TString MTask::GetDescriptor() const
352{
353 //
354 // Because it returns a (const char*) we cannot return a casted
355 // local TString. The pointer would - immediatly after return -
356 // point to a random memory segment, because the TString has gone.
357 //
358 if (fName==ClassName())
359 return fSerialNumber==0 ? (TString)ClassName() : MString::Form("%s;%d", ClassName(), fSerialNumber);
360
361 return fSerialNumber>0 ?
362 MString::Form("%s;%d [%s]", fName.Data(), fSerialNumber, ClassName()) :
363 MString::Form("%s [%s]", fName.Data(), ClassName());
364}
365
366// --------------------------------------------------------------------------
367//
368// Return the total number of calls to since PreProcess(). If Process() was
369// not called due to a set filter this is not counted.
370//
371UInt_t MTask::GetNumExecutions() const
372{
373 return GetNumExecutionsTotal()-fNumExec0;
374}
375
376// --------------------------------------------------------------------------
377//
378// Return the total number of calls to Process(). If Process() was not
379// called due to a set filter this is not counted.
380//
381UInt_t MTask::GetNumExecutionsTotal() const
382{
383 return (UInt_t)fStopwatch->Counter()-1;
384}
385
386// --------------------------------------------------------------------------
387//
388// Return total CPU execution time in seconds of calls to Process().
389// If Process() was not called due to a set filter this is not counted.
390//
391Double_t MTask::GetCpuTime() const
392{
393 return fStopwatch->CpuTime();
394}
395
396// --------------------------------------------------------------------------
397//
398// Return total real execution time in seconds of calls to Process().
399// If Process() was not called due to a set filter this is not counted.
400//
401Double_t MTask::GetRealTime() const
402{
403 return fStopwatch->RealTime();
404}
405
406// --------------------------------------------------------------------------
407//
408// Prints the relative time spent in Process() (relative means relative to
409// its parent Tasklist) and the number of times Process() was executed.
410// Don't wonder if the sum of the tasks in a tasklist is not 100%,
411// because only the call to Process() of the task is measured. The
412// time of the support structure is ignored. The faster your analysis is
413// the more time is 'wasted' in the support structure.
414// Only the CPU time is displayed. This means that exspecially task
415// which have a huge part of file i/o will be underestimated in their
416// relative wasted time.
417// For convinience the lvl argument results in a number of spaces at the
418// beginning of the line. So that the structur of a tasklist can be
419// identified. If a Tasklist or task has filter applied the name of the
420// filter is printer in <>-brackets behind the number of executions.
421// Use MTaskList::PrintStatistics without an argument.
422// For tasks which don't overwrite Process() no action is perfomed.
423//
424void MTask::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
425{
426 if (!OverwritesProcess() && IsA()!=MTask::Class())
427 return;
428
429 *fLog << all << setfill(' ') << setw(lvl) << " ";
430
431 if (GetCpuTime()>0 && time>0 && GetCpuTime()>=0.001*time && !HasAccelerator(kAccDontTime))
432 *fLog << Form("%5.1f", GetCpuTime()/time*100) << "% ";
433 else
434 *fLog << " ";
435
436 if (HasStreamId())
437 *fLog << GetStreamId() << ":";
438 *fLog << GetDescriptor() << "\t";
439
440 if (HasAccelerator(kAccDontCount))
441 *fLog << "-/-";
442 else
443 *fLog << dec << GetNumExecutions();
444
445 if (fFilter)
446 *fLog << " <" << fFilter->GetName() << ">";
447 if (title)
448 *fLog << "\t" << fTitle;
449 *fLog << endl;
450}
451
452// --------------------------------------------------------------------------
453//
454// First call MParContainer::SavePrimitive which should stream the primitive
455// to the output stream. Then, if a filter is set, stream first the filter
456// and afterwards set the filter for this task.
457//
458void MTask::SavePrimitive(ofstream &out, Option_t *o)
459{
460 MParContainer::SavePrimitive(out);
461 if (!fFilter)
462 return;
463
464 /*
465 If we don't stream filter which are not in the task list itself
466 (which means: already streamed) we may be able to use
467 SavePrimitive as some kind of validity check for the macros
468
469 fFilter->SavePrimitive(out);
470 */
471 out << " " << GetUniqueName() << ".SetFilter(&" << fFilter->GetUniqueName() <<");" << endl;
472 if (fSerialNumber>0)
473 out << " " << GetUniqueName() << ".SetSerialNumber(" << fSerialNumber <<");" << endl;
474}
475
476// --------------------------------------------------------------------------
477//
478// Check whether the class given in the argument overwrites MTask::Process.
479// This function calls itself recursively. If you want to call it,
480// leave out the argument.
481//
482Bool_t MTask::OverwritesProcess(TClass *cls) const
483{
484 if (!cls)
485 cls = IsA();
486
487 //
488 // Check whether we reached the base class MTask
489 //
490 if (cls==MTask::Class())
491 return kFALSE;
492
493 //
494 // Check whether the class cls overwrites Process
495 //
496 if (cls->GetMethodAny("Process"))
497 return kTRUE;
498
499 //
500 // If the class itself doesn't overload it check all it's base classes
501 //
502 TBaseClass *base=NULL;
503 TIter NextBase(cls->GetListOfBases());
504 while ((base=(TBaseClass*)NextBase()))
505 {
506 if (OverwritesProcess(base->GetClassPointer()))
507 return kTRUE;
508 }
509
510 return kFALSE;
511}
512
513void MTask::SetDisplay(MStatusDisplay *d)
514{
515 if (fFilter)
516 fFilter->SetDisplay(d);
517 MParContainer::SetDisplay(d);
518}
519
520// --------------------------------------------------------------------------
521//
522// This is used to print the output in the PostProcess/Finalize.
523// Or everywhere else in a nice fashioned and unified way.
524//
525void MTask::PrintSkipped(UInt_t n, const char *str)
526{
527 *fLog << " " << setw(7) << n << " (";
528 *fLog << Form("%5.1f", 100.*n/GetNumExecutions());
529 *fLog << "%) Evts skipped: " << str << endl;
530}
Note: See TracBrowser for help on using the repository browser.