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

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