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

Last change on this file since 1187 was 1118, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 9.1 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 should be executed only if //
63// PreProcess was successfull (returned kTRUE) //
64// //
65/////////////////////////////////////////////////////////////////////////////
66
67#include "MTask.h"
68
69#include "MLog.h"
70#include "MLogManip.h"
71
72#include "MFilter.h"
73#include "MGGroupFrame.h"
74
75ClassImp(MTask);
76
77MTask::MTask(const char *name, const char *title)
78 : fFilter(NULL), fIsPreprocessed(kFALSE), fNumExecutions(0)
79{
80 fName = name ? name : "MTask";
81 fTitle = title ? title : "Base class for all tasks (dummy task).";
82
83 fListOfBranches = new TList;
84 fListOfBranches->SetOwner();
85}
86
87MTask::~MTask()
88{
89 delete fListOfBranches;
90}
91
92// --------------------------------------------------------------------------
93//
94// This adds a branch to the list for the auto enabeling schmeme.
95// This makes it possible for MReadTree to decide which branches
96// are really needed for the eventloop. Only the necessary branches
97// are read from disk which speeds up the calculation enormously.
98//
99// You can use TRegExp expressions like "*.fEnergy", but the
100// recommended method is to call this function for exactly all
101// branches you want to have, eg:
102// AddToBranchList("MMcTrig.fNumFirstLevel");
103// AddToBranchList("MMcTrig;1.fNumFirstLevel");
104// AddToBranchList("MMcTrig;2.fNumFirstLevel");
105//
106// We agreed on the convetion, that all branches are stored with
107// a trailing dot '.' so that always the Master Branch name
108// (eg. MMcTrig) is part of the branch name.
109//
110// Remark: The common place to call AddToBranchList is the
111// constructor of the derived classes (tasks)
112//
113void MTask::AddToBranchList(const char *b)
114{
115 if (fListOfBranches->FindObject(b))
116 return;
117
118 fListOfBranches->Add(new TNamed(b, ""));
119}
120
121// --------------------------------------------------------------------------
122//
123// Copy constructor.
124//
125MTask::MTask(MTask &t)
126{
127 fFilter = t.fFilter;
128 fListOfBranches->AddAll(t.fListOfBranches);
129}
130
131// --------------------------------------------------------------------------
132//
133// Mapper function for PreProcess.
134// Sets the preprocessed flag dependend on the return value of PreProcess.
135//
136Bool_t MTask::CallPreProcess(MParList *plist)
137{
138 fNumExecutions = 0;
139
140 switch (PreProcess(plist))
141 {
142 case kFALSE:
143 return kFALSE;
144
145 case kTRUE:
146 fIsPreprocessed = kTRUE;
147 return kTRUE;
148
149 case kSKIP:
150 return kSKIP;
151 }
152
153 *fLog << err << dbginf << "PreProcess of " << GetDescriptor();
154 *fLog << " returned an unknown value... aborting." << endl;
155
156 return kFALSE;
157}
158
159// --------------------------------------------------------------------------
160//
161// Mapper function for Process.
162// Executes Process dependent on the existance of a filter and its possible
163// return value.
164// If Process is executed, the execution counter is increased.
165//
166Bool_t MTask::CallProcess()
167{
168 //
169 // Check for the existance of a filter. If a filter is existing
170 // check for its value. If the value is kFALSE don't execute
171 // this task.
172 //
173 const Bool_t exec = fFilter ? fFilter->IsExpressionTrue() : kTRUE;
174
175 if (!exec)
176 return kTRUE;
177
178 fNumExecutions++;
179 return Process();
180}
181
182// --------------------------------------------------------------------------
183//
184// Mapper function for PreProcess.
185// Calls Postprocess dependent on the state of the preprocessed flag,
186// resets this flag.
187//
188Bool_t MTask::CallPostProcess()
189{
190 if (!fIsPreprocessed)
191 return kTRUE;
192
193 fIsPreprocessed = kFALSE;
194
195 return PostProcess();
196}
197
198// --------------------------------------------------------------------------
199//
200// This is reinit function
201//
202// This function is called asynchronously if the tasks in the tasklist need
203// reinitialization. This for example happens when the eventloop switches
204// from one group of events to another one (eg. switching between events
205// of different runs means reading a new run header and a new run header
206// may mean that some value must be reinitialized)
207//
208// the virtual implementation returns kTRUE
209//
210Bool_t MTask::ReInit(MParList *pList)
211{
212 return kTRUE;
213}
214
215// --------------------------------------------------------------------------
216//
217// This is processed before the eventloop starts
218//
219// It is the job of the PreProcess to connect the tasks
220// with the right container in the parameter list.
221//
222// the virtual implementation returns kTRUE
223//
224Bool_t MTask::PreProcess(MParList *pList)
225{
226 return kTRUE;
227}
228
229// --------------------------------------------------------------------------
230//
231// This is processed for every event in the eventloop
232//
233// the virtual implementation returns kTRUE
234//
235Bool_t MTask::Process()
236{
237 return kTRUE;
238}
239
240// --------------------------------------------------------------------------
241//
242// This is processed after the eventloop starts
243//
244// the virtual implementation returns kTRUE
245//
246Bool_t MTask::PostProcess()
247{
248 return kTRUE;
249}
250
251// --------------------------------------------------------------------------
252//
253// Prints the number of times this task has been processed.
254// For convinience the lvl argument results in a number of spaces at the
255// beginning of the line. So that the structur of a tasklist can be
256// identified.
257//
258void MTask::PrintStatistics(const Int_t lvl) const
259{
260 *fLog << all << setw(lvl) << " " << GetDescriptor() << "\t";
261 *fLog << dec << fNumExecutions << endl;
262}
263
Note: See TracBrowser for help on using the repository browser.