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

Last change on this file since 7601 was 7601, checked in by tbretz, 14 years ago
*** empty log message ***
File size: 16.4 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), fNumExecutions(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    fNumExecutions = 0;
219    fNumExec0 = GetNumExecutionsTotal();
220
221    *fLog << all << GetDescriptor() << "... " << flush;
222    if (fDisplay)
223        fDisplay->SetStatusLine2(*this);
224
225    switch (PreProcess(plist))
226    {
227    case kFALSE:
228        return kFALSE;
229
230    case kTRUE:
231        fIsPreprocessed = kTRUE;
232        return kTRUE;
233
234    case kSKIP:
235        return kSKIP;
236    }
237
238    *fLog << err << dbginf << "PreProcess of " << GetDescriptor();
239    *fLog << " returned an unknown value... aborting." << endl;
240
241    return kFALSE;
242}
243
244// --------------------------------------------------------------------------
245//
246// Mapper function for Process.
247// Executes Process dependent on the existance of a filter and its possible
248// return value.
249// If Process is executed, the execution counter is increased.
250// Count cpu consumption time.
251//
252Int_t MTask::CallProcess()
253{
254    //
255    // Check for the existance of a filter. If a filter is existing
256    // check for its value. If the value is kFALSE don't execute
257    // this task.
258    //
259    if (fFilter && !fFilter->IsConditionTrue())
260        return kTRUE;
261
262    if (!HasAccelerator(kAccDontTime))
263        fStopwatch->Start(kFALSE);
264
265    fNumExecutions++;
266
267    const Int_t rc = Process();
268
269    if (!HasAccelerator(kAccDontTime))
270        fStopwatch->Stop();
271
272    return rc;
273}
274
275// --------------------------------------------------------------------------
276//
277// Mapper function for PreProcess.
278// Calls Postprocess dependent on the state of the preprocessed flag,
279// resets this flag.
280//
281Int_t MTask::CallPostProcess()
282{
283    if (!fIsPreprocessed)
284        return kTRUE;
285
286    fIsPreprocessed = kFALSE;
287
288    *fLog << all << GetDescriptor() << "... " << flush;
289    if (fDisplay)
290        fDisplay->SetStatusLine2(*this);
291
292    return PostProcess();
293}
294
295// --------------------------------------------------------------------------
296//
297// This is reinit function
298//
299// This function is called asynchronously if the tasks in the tasklist need
300// reinitialization. This for example happens when the eventloop switches
301// from one group of events to another one (eg. switching between events
302// of different runs means reading a new run header and a new run header
303// may mean that some value must be reinitialized)
304//
305// the virtual implementation returns kTRUE
306//
307Bool_t MTask::ReInit(MParList *pList)
308{
309    return kTRUE;
310}
311
312// --------------------------------------------------------------------------
313//
314// This is processed before the eventloop starts
315//
316// It is the job of the PreProcess to connect the tasks
317// with the right container in the parameter list.
318//
319// the virtual implementation returns kTRUE
320//
321Int_t MTask::PreProcess(MParList *pList)
322{
323    return kTRUE;
324}
325
326// --------------------------------------------------------------------------
327//
328// This is processed for every event in the eventloop
329//
330// the virtual implementation returns kTRUE
331//
332Int_t MTask::Process()
333{
334    return kTRUE;
335}
336
337// --------------------------------------------------------------------------
338//
339// This is processed after the eventloop starts
340//
341// the virtual implementation returns kTRUE
342//
343Int_t MTask::PostProcess()
344{
345    return kTRUE;
346}
347
348// --------------------------------------------------------------------------
349//
350// Returns the name of the object. If the name of the object is not the
351// class name it returns the object name and in []-brackets the class name.
352// If a serial number is set (!=0) the serial number is added to the
353// name (eg. ;1)
354//
355const TString MTask::GetDescriptor() const
356{
357    //
358    // Because it returns a (const char*) we cannot return a casted
359    // local TString. The pointer would - immediatly after return -
360    // point to a random memory segment, because the TString has gone.
361    //
362    if (fName==ClassName())
363        return fSerialNumber==0 ? (TString)ClassName() : MString::Form("%s;%d", ClassName(), fSerialNumber);
364
365    return fSerialNumber>0 ?
366        MString::Form("%s;%d [%s]", fName.Data(), fSerialNumber, ClassName()) :
367        MString::Form("%s [%s]",    fName.Data(), ClassName());
368}
369
370// --------------------------------------------------------------------------
371//
372//  Return the total number of calls to since PreProcess(). If Process() was
373//  not called due to a set filter this is not counted.
374//
375UInt_t MTask::GetNumExecutions() const
376{
377    return GetNumExecutionsTotal()-fNumExec0;
378}
379
380// --------------------------------------------------------------------------
381//
382//  Return the total number of calls to Process(). If Process() was not
383//  called due to a set filter this is not counted.
384//
385UInt_t MTask::GetNumExecutionsTotal() const
386{
387    return fNumExecutions-1;
388}
389
390// --------------------------------------------------------------------------
391//
392//  Return total CPU execution time in seconds of calls to Process().
393//  If Process() was not called due to a set filter this is not counted.
394//
395Double_t MTask::GetCpuTime() const
396{
397    return fStopwatch->CpuTime();
398}
399
400// --------------------------------------------------------------------------
401//
402//  Return total real execution time in seconds of calls to Process().
403//  If Process() was not called due to a set filter this is not counted.
404//
405Double_t MTask::GetRealTime() const
406{
407    return fStopwatch->RealTime();
408}
409
410// --------------------------------------------------------------------------
411//
412//  Prints the relative time spent in Process() (relative means relative to
413//  its parent Tasklist) and the number of times Process() was executed.
414//  Don't wonder if the sum of the tasks in a tasklist is not 100%,
415//  because only the call to Process() of the task is measured. The
416//  time of the support structure is ignored. The faster your analysis is
417//  the more time is 'wasted' in the support structure.
418//  Only the CPU time is displayed. This means that exspecially task
419//  which have a huge part of file i/o will be underestimated in their
420//  relative wasted time.
421//  For convinience the lvl argument results in a number of spaces at the
422//  beginning of the line. So that the structur of a tasklist can be
423//  identified. If a Tasklist or task has filter applied the name of the
424//  filter is printer in <>-brackets behind the number of executions.
425//  Use MTaskList::PrintStatistics without an argument.
426//  For tasks which don't overwrite Process() no action is perfomed.
427//
428void MTask::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
429{
430    if (!OverwritesProcess() && IsA()!=MTask::Class())
431        return;
432
433    *fLog << all << setfill(' ') << setw(lvl) << " ";
434
435    if (GetCpuTime()>0 && time>0 && GetCpuTime()>=0.001*time && !HasAccelerator(kAccDontTime))
436        *fLog << Form("%5.1f", GetCpuTime()/time*100) << "% ";
437    else
438        *fLog << "       ";
439
440    if (HasStreamId())
441        *fLog << GetStreamId() << ":";
442    *fLog << GetDescriptor() << "\t" << dec << GetNumExecutions();
443
444    if (fFilter)
445        *fLog << " <" << fFilter->GetName() << ">";
446    if (title)
447        *fLog << "\t" << fTitle;
448    *fLog << endl;
449}
450
451// --------------------------------------------------------------------------
452//
453// First call MParContainer::SavePrimitive which should stream the primitive
454// to the output stream. Then, if a filter is set, stream first the filter
455// and afterwards set the filter for this task.
456//
457void MTask::SavePrimitive(ofstream &out, Option_t *o)
458{
459    MParContainer::SavePrimitive(out);
460    if (!fFilter)
461        return;
462
463    /*
464     If we don't stream filter which are not in the task list itself
465     (which means: already streamed) we may be able to use
466     SavePrimitive as some kind of validity check for the macros
467
468     fFilter->SavePrimitive(out);
469     */
470    out << "   " << GetUniqueName() << ".SetFilter(&" << fFilter->GetUniqueName() <<");" << endl;
471    if (fSerialNumber>0)
472        out << "   " << GetUniqueName() << ".SetSerialNumber(" << fSerialNumber <<");" << endl;
473}
474
475// --------------------------------------------------------------------------
476//
477// Check whether the class given in the argument overwrites MTask::Process.
478// This function calls itself recursively. If you want to call it,
479// leave out the argument.
480//
481Bool_t MTask::OverwritesProcess(TClass *cls) const
482{
483    if (!cls)
484        cls = IsA();
485
486    //
487    // Check whether we reached the base class MTask
488    //
489    if (cls==MTask::Class())
490        return kFALSE;
491
492    //
493    // Check whether the class cls overwrites Process
494    //
495    if (cls->GetMethodAny("Process"))
496        return kTRUE;
497
498    //
499    // If the class itself doesn't overload it check all it's base classes
500    //
501    TBaseClass *base=NULL;
502    TIter NextBase(cls->GetListOfBases());
503    while ((base=(TBaseClass*)NextBase()))
504    {
505        if (OverwritesProcess(base->GetClassPointer()))
506            return kTRUE;
507    }
508
509    return kFALSE;
510}
511
512void MTask::SetDisplay(MStatusDisplay *d)
513{
514    if (fFilter)
515        fFilter->SetDisplay(d);
516    MParContainer::SetDisplay(d);
517}
518
519// --------------------------------------------------------------------------
520//
521//  This is used to print the output in the PostProcess/Finalize.
522//  Or everywhere else in a nice fashioned and unified way.
523//
524void MTask::PrintSkipped(UInt_t n, const char *str)
525{
526    *fLog << " " << setw(7) << n << " (";
527    *fLog << Form("%5.1f", 100.*n/GetNumExecutions());
528    *fLog << "%) Evts skipped: " << str << endl;
529}
Note: See TracBrowser for help on using the repository browser.