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

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