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

Last change on this file since 2473 was 2473, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 12.2 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// - PostProcess(): executed after the eventloop. Here you can close
63// output files, start display of the run parameter,
64// etc. PostProcess is only executed in case of
65// PreProcess was successfull (returned kTRUE)
66//
67// Version 1:
68// ----------
69// - first version
70//
71// Version 2:
72// ----------
73// - added fSerialNumber
74//
75/////////////////////////////////////////////////////////////////////////////
76#include "MTask.h"
77
78#include <fstream>
79#include <TBaseClass.h>
80
81#include "MLog.h"
82#include "MLogManip.h"
83
84#include "MFilter.h"
85#include "MGGroupFrame.h"
86#include "MStatusDisplay.h"
87
88ClassImp(MTask);
89
90using namespace std;
91
92MTask::MTask(const char *name, const char *title)
93 : fFilter(NULL), fSerialNumber(0), fIsPreprocessed(kFALSE), fNumExecutions(0)
94{
95 fName = name ? name : "MTask";
96 fTitle = title ? title : "Base class for all tasks (dummy task).";
97
98 fListOfBranches = new TList;
99 fListOfBranches->SetOwner();
100}
101
102MTask::~MTask()
103{
104 delete fListOfBranches;
105}
106
107// --------------------------------------------------------------------------
108//
109// This adds a branch to the list for the auto enabeling schmeme.
110// This makes it possible for MReadTree to decide which branches
111// are really needed for the eventloop. Only the necessary branches
112// are read from disk which speeds up the calculation enormously.
113//
114// You can use TRegExp expressions like "*.fEnergy", but the
115// recommended method is to call this function for exactly all
116// branches you want to have, eg:
117// AddToBranchList("MMcTrig.fNumFirstLevel");
118// AddToBranchList("MMcTrig;1.fNumFirstLevel");
119// AddToBranchList("MMcTrig;2.fNumFirstLevel");
120//
121// We agreed on the convetion, that all branches are stored with
122// a trailing dot '.' so that always the Master Branch name
123// (eg. MMcTrig) is part of the branch name.
124//
125// Remark: The common place to call AddToBranchList is the
126// constructor of the derived classes (tasks)
127//
128void MTask::AddToBranchList(const char *b)
129{
130 if (fListOfBranches->FindObject(b))
131 return;
132
133 fListOfBranches->Add(new TNamed(b, ""));
134}
135
136// --------------------------------------------------------------------------
137//
138// Using this overloaded member function you may cascade several branches
139// in acomma seperated list, eg: "MMcEvt.fTheta,MMcEvt.fEnergy"
140//
141// For moredetailed information see AddToBranchList(const char *b);
142//
143void MTask::AddToBranchList(const TString &str)
144{
145 TString s = str;
146
147 while (1)
148 {
149 Int_t fst = s.First(',');
150
151 if (fst<0)
152 return;
153
154 AddToBranchList(TString(s(0, fst)));
155
156 s.Remove(0, fst+1);
157 }
158}
159
160// --------------------------------------------------------------------------
161//
162// Copy constructor.
163//
164MTask::MTask(MTask &t)
165{
166 fFilter = t.fFilter;
167 fListOfBranches->AddAll(t.fListOfBranches);
168}
169
170// --------------------------------------------------------------------------
171//
172// Mapper function for PreProcess.
173// Sets the preprocessed flag dependend on the return value of PreProcess.
174//
175Int_t MTask::CallPreProcess(MParList *plist)
176{
177 fNumExecutions = 0;
178
179 *fLog << all << fName << "... " << flush;
180 if (fDisplay)
181 fDisplay->SetStatusLine2(*this);
182
183 switch (PreProcess(plist))
184 {
185 case kFALSE:
186 return kFALSE;
187
188 case kTRUE:
189 fIsPreprocessed = kTRUE;
190 return kTRUE;
191
192 case kSKIP:
193 return kSKIP;
194 }
195
196 *fLog << err << dbginf << "PreProcess of " << GetDescriptor();
197 *fLog << " returned an unknown value... aborting." << endl;
198
199 return kFALSE;
200}
201
202// --------------------------------------------------------------------------
203//
204// Mapper function for Process.
205// Executes Process dependent on the existance of a filter and its possible
206// return value.
207// If Process is executed, the execution counter is increased.
208//
209Int_t MTask::CallProcess()
210{
211 //
212 // Check for the existance of a filter. If a filter is existing
213 // check for its value. If the value is kFALSE don't execute
214 // this task.
215 //
216 const Bool_t exec = fFilter ? fFilter->IsConditionTrue() : kTRUE;
217
218 if (!exec)
219 return kTRUE;
220
221 fNumExecutions++;
222 return Process();
223}
224
225// --------------------------------------------------------------------------
226//
227// Mapper function for PreProcess.
228// Calls Postprocess dependent on the state of the preprocessed flag,
229// resets this flag.
230//
231Int_t MTask::CallPostProcess()
232{
233 if (!fIsPreprocessed)
234 return kTRUE;
235
236 fIsPreprocessed = kFALSE;
237
238 *fLog << all << fName << "... " << flush;
239 if (fDisplay)
240 fDisplay->SetStatusLine2(*this);
241
242 return PostProcess();
243}
244
245// --------------------------------------------------------------------------
246//
247// This is reinit function
248//
249// This function is called asynchronously if the tasks in the tasklist need
250// reinitialization. This for example happens when the eventloop switches
251// from one group of events to another one (eg. switching between events
252// of different runs means reading a new run header and a new run header
253// may mean that some value must be reinitialized)
254//
255// the virtual implementation returns kTRUE
256//
257Bool_t MTask::ReInit(MParList *pList)
258{
259 return kTRUE;
260}
261
262// --------------------------------------------------------------------------
263//
264// This is processed before the eventloop starts
265//
266// It is the job of the PreProcess to connect the tasks
267// with the right container in the parameter list.
268//
269// the virtual implementation returns kTRUE
270//
271Int_t MTask::PreProcess(MParList *pList)
272{
273 return kTRUE;
274}
275
276// --------------------------------------------------------------------------
277//
278// This is processed for every event in the eventloop
279//
280// the virtual implementation returns kTRUE
281//
282Int_t MTask::Process()
283{
284 return kTRUE;
285}
286
287// --------------------------------------------------------------------------
288//
289// This is processed after the eventloop starts
290//
291// the virtual implementation returns kTRUE
292//
293Int_t MTask::PostProcess()
294{
295 return kTRUE;
296}
297
298// --------------------------------------------------------------------------
299//
300// Returns the name of the object. If the name of the object is not the
301// class name it returns the object name and in []-brackets the class name.
302// If a serial number is set (!=0) the serial number is added to the
303// name (eg. ;1)
304//
305const char *MTask::GetDescriptor() const
306{
307 //
308 // Because it returns a (const char*) we cannot return a casted
309 // local TString. The pointer would - immediatly after return -
310 // point to a random memory segment, because the TString has gone.
311 //
312 if (fName==ClassName())
313 return fSerialNumber==0 ? ClassName() : Form("%s;%d", ClassName(), fSerialNumber);
314
315 return fSerialNumber>0 ?
316 Form("%s;%d [%s]", fName.Data(), fSerialNumber, ClassName()) :
317 Form("%s [%s]", fName.Data(), ClassName());
318}
319
320// --------------------------------------------------------------------------
321//
322// Prints the number of times all the tasks in the list has been.
323// For convinience the lvl argument results in a number of spaces at the
324// beginning of the line. So that the structur of a tasklist can be
325// identified. If a Tasklist or task has filter applied the name of the
326// filter is printer in <>-brackets behind the number of executions.
327// Use MTaskList::PrintStatistics without an argument.
328//
329void MTask::PrintStatistics(const Int_t lvl, Bool_t title) const
330{
331 *fLog << all << setw(lvl) << " " << GetDescriptor() << "\t";
332 *fLog << dec << fNumExecutions;
333 if (fFilter)
334 *fLog << " <" << fFilter->GetName() << ">";
335 if (title)
336 *fLog << "\t" << fTitle;
337 *fLog << endl;
338}
339
340// --------------------------------------------------------------------------
341//
342// First call MParContainer::SavePrimitive which should stream the primitive
343// to the output stream. Then, if a filter is set, stream first the filter
344// and afterwards set the filter for this task.
345//
346void MTask::SavePrimitive(ofstream &out, Option_t *o)
347{
348 MParContainer::SavePrimitive(out);
349 if (!fFilter)
350 return;
351
352 /*
353 If we don't stream filter which are not in the task list itself
354 (which means: already streamed) we may be able to use
355 SavePrimitive as some kind of validity check for the macros
356
357 fFilter->SavePrimitive(out);
358 */
359 out << " " << GetUniqueName() << ".SetFilter(&" << fFilter->GetUniqueName() <<");" << endl;
360 if (fSerialNumber>0)
361 out << " " << GetUniqueName() << ".SetSerialNumber(" << fSerialNumber <<");" << endl;
362}
363
364// --------------------------------------------------------------------------
365//
366// Check whether the class given in the argument overwrites MTask::Process.
367// This function calls itself recursively. If you want to call it,
368// leave out the argument.
369//
370Bool_t MTask::OverwritesProcess(TClass *cls) const
371{
372 if (!cls)
373 cls = IsA();
374
375 //
376 // Check whether we reached the base class MTask
377 //
378 if (TString(cls->GetName())=="MTask")
379 return kFALSE;
380
381 //
382 // Check whether the class cls overwrites Process
383 //
384 if (cls->GetMethodAny("Process"))
385 return kTRUE;
386
387 //
388 // If the class itself doesn't overload it check all it's base classes
389 //
390 TBaseClass *base=NULL;
391 TIter NextBase(cls->GetListOfBases());
392 while ((base=(TBaseClass*)NextBase()))
393 {
394 if (OverwritesProcess(base->GetClassPointer()))
395 return kTRUE;
396 }
397
398 return kFALSE;
399}
400
401void MTask::SetDisplay(MStatusDisplay *d)
402{
403 if (fFilter)
404 fFilter->SetDisplay(d);
405 MParContainer::SetDisplay(d);
406}
Note: See TracBrowser for help on using the repository browser.