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

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