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

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