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