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

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