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

Last change on this file since 1492 was 1489, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 9.9 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// Copy constructor.
125//
126MTask::MTask(MTask &t)
127{
128 fFilter = t.fFilter;
129 fListOfBranches->AddAll(t.fListOfBranches);
130}
131
132// --------------------------------------------------------------------------
133//
134// Mapper function for PreProcess.
135// Sets the preprocessed flag dependend on the return value of PreProcess.
136//
137Bool_t MTask::CallPreProcess(MParList *plist)
138{
139 fNumExecutions = 0;
140
141 switch (PreProcess(plist))
142 {
143 case kFALSE:
144 return kFALSE;
145
146 case kTRUE:
147 fIsPreprocessed = kTRUE;
148 return kTRUE;
149
150 case kSKIP:
151 return kSKIP;
152 }
153
154 *fLog << err << dbginf << "PreProcess of " << GetDescriptor();
155 *fLog << " returned an unknown value... aborting." << endl;
156
157 return kFALSE;
158}
159
160// --------------------------------------------------------------------------
161//
162// Mapper function for Process.
163// Executes Process dependent on the existance of a filter and its possible
164// return value.
165// If Process is executed, the execution counter is increased.
166//
167Bool_t MTask::CallProcess()
168{
169 //
170 // Check for the existance of a filter. If a filter is existing
171 // check for its value. If the value is kFALSE don't execute
172 // this task.
173 //
174 const Bool_t exec = fFilter ? fFilter->IsExpressionTrue() : kTRUE;
175
176 if (!exec)
177 return kTRUE;
178
179 fNumExecutions++;
180 return Process();
181}
182
183// --------------------------------------------------------------------------
184//
185// Mapper function for PreProcess.
186// Calls Postprocess dependent on the state of the preprocessed flag,
187// resets this flag.
188//
189Bool_t MTask::CallPostProcess()
190{
191 if (!fIsPreprocessed)
192 return kTRUE;
193
194 fIsPreprocessed = kFALSE;
195
196 return PostProcess();
197}
198
199// --------------------------------------------------------------------------
200//
201// This is reinit function
202//
203// This function is called asynchronously if the tasks in the tasklist need
204// reinitialization. This for example happens when the eventloop switches
205// from one group of events to another one (eg. switching between events
206// of different runs means reading a new run header and a new run header
207// may mean that some value must be reinitialized)
208//
209// the virtual implementation returns kTRUE
210//
211Bool_t MTask::ReInit(MParList *pList)
212{
213 return kTRUE;
214}
215
216// --------------------------------------------------------------------------
217//
218// This is processed before the eventloop starts
219//
220// It is the job of the PreProcess to connect the tasks
221// with the right container in the parameter list.
222//
223// the virtual implementation returns kTRUE
224//
225Bool_t MTask::PreProcess(MParList *pList)
226{
227 return kTRUE;
228}
229
230// --------------------------------------------------------------------------
231//
232// This is processed for every event in the eventloop
233//
234// the virtual implementation returns kTRUE
235//
236Bool_t MTask::Process()
237{
238 return kTRUE;
239}
240
241// --------------------------------------------------------------------------
242//
243// This is processed after the eventloop starts
244//
245// the virtual implementation returns kTRUE
246//
247Bool_t MTask::PostProcess()
248{
249 return kTRUE;
250}
251
252// --------------------------------------------------------------------------
253//
254// Prints the number of times this task has been processed.
255// For convinience the lvl argument results in a number of spaces at the
256// beginning of the line. So that the structur of a tasklist can be
257// identified.
258//
259void MTask::PrintStatistics(const Int_t lvl) const
260{
261 *fLog << all << setw(lvl) << " " << GetDescriptor() << "\t";
262 *fLog << dec << fNumExecutions << endl;
263}
264
265// --------------------------------------------------------------------------
266//
267// First call MParContainer::SavePrimitive which should stream the primitive
268// to the output stream. Then, if a filter is set, stream first the filter
269// and afterwards set the filter for this task.
270//
271void MTask::SavePrimitive(ofstream &out, Option_t *o)
272{
273 MParContainer::SavePrimitive(out);
274 if (!fFilter)
275 return;
276
277 /*
278 If we don't stream filter which are not in the task list itself
279 (which means: alrteady streamed) we may be able to use the
280 primitive streamer as some kind of validity check for the macros
281
282 fFilter->SavePrimitive(out);
283 */
284 out << " " << GetUniqueName() << ".SetFilter(&" << fFilter->GetUniqueName() <<");" << endl;
285}
Note: See TracBrowser for help on using the repository browser.