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 12/2000 (tbretz@uni-sw.gwdg.de)
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2001
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | // //
|
---|
27 | // MTaskList //
|
---|
28 | // //
|
---|
29 | // Collection of tasks. //
|
---|
30 | // //
|
---|
31 | // A tasklist is necessary to run the eventloop. It contains the scheduled //
|
---|
32 | // tasks, which should be executed in your program. //
|
---|
33 | // //
|
---|
34 | // To add a task use AddToList. //
|
---|
35 | // //
|
---|
36 | // The tasklist itself is a task, too. You can add a tasklist to another //
|
---|
37 | // tasklist. This makes sense, if you want to filter the execution of //
|
---|
38 | // more than one task of your tasklist using the same filter. //
|
---|
39 | // //
|
---|
40 | // The tasks in the list are idetified by their names. If more than one //
|
---|
41 | // task has the same name, the tasklist will still work correctly, but //
|
---|
42 | // you might run into trouble trying to get a pointer to a task by name //
|
---|
43 | // from the list. //
|
---|
44 | // //
|
---|
45 | /////////////////////////////////////////////////////////////////////////////
|
---|
46 |
|
---|
47 | #include "MTaskList.h"
|
---|
48 |
|
---|
49 | #include "MLog.h"
|
---|
50 | #include "MLogManip.h"
|
---|
51 | #include "MFilter.h"
|
---|
52 | #include "MParList.h"
|
---|
53 | #include "MInputStreamID.h"
|
---|
54 |
|
---|
55 | ClassImp(MTaskList);
|
---|
56 |
|
---|
57 | // --------------------------------------------------------------------------
|
---|
58 | //
|
---|
59 | // the name for the task list must be the same for all task lists
|
---|
60 | // because the task list (at the moment) is identified by exactly
|
---|
61 | // this name in the parameter list (by MEvtLoop::SetParList)
|
---|
62 | //
|
---|
63 | MTaskList::MTaskList(const char *name, const char *title)
|
---|
64 | {
|
---|
65 | *fName = name ? name : "MTaskList";
|
---|
66 | *fTitle = title ? title : "A list for tasks to be executed";
|
---|
67 | }
|
---|
68 |
|
---|
69 | // --------------------------------------------------------------------------
|
---|
70 | //
|
---|
71 | // CopyConstructor
|
---|
72 | // creates a new TaskList and put the contents of an existing
|
---|
73 | // TaskList in the new TaskList.
|
---|
74 | //
|
---|
75 | MTaskList::MTaskList(MTaskList &ts)
|
---|
76 | {
|
---|
77 | fTasks.AddAll(&ts.fTasks);
|
---|
78 | }
|
---|
79 |
|
---|
80 | // --------------------------------------------------------------------------
|
---|
81 | //
|
---|
82 | // Set the logging stream for the all tasks in the list and the tasklist
|
---|
83 | // itself.
|
---|
84 | //
|
---|
85 | void MTaskList::SetLogStream(MLog *log)
|
---|
86 | {
|
---|
87 | //
|
---|
88 | // create the Iterator over the tasklist
|
---|
89 | //
|
---|
90 | TIter Next(&fTasks);
|
---|
91 |
|
---|
92 | MTask *task=NULL;
|
---|
93 |
|
---|
94 | //
|
---|
95 | // loop over all tasks for preproccesing
|
---|
96 | //
|
---|
97 | while ((task=(MTask*)Next()))
|
---|
98 | task->SetLogStream(log);
|
---|
99 |
|
---|
100 | MParContainer::SetLogStream(log);
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | // --------------------------------------------------------------------------
|
---|
105 | //
|
---|
106 | // schedule task for execution, whether as first task, or after
|
---|
107 | // 'where'. 'tType' is the event type which should be processed
|
---|
108 | //
|
---|
109 | Bool_t MTaskList::AddToList(MTask *task, const char *type, MTask *where)
|
---|
110 | {
|
---|
111 | // FIXME: We agreed to put the task into list in an ordered way.
|
---|
112 |
|
---|
113 | //
|
---|
114 | // Sanity check
|
---|
115 | //
|
---|
116 | if (!task)
|
---|
117 | return kFALSE;
|
---|
118 |
|
---|
119 | //
|
---|
120 | // Get Name of new task
|
---|
121 | //
|
---|
122 | const char *name = task->GetName();
|
---|
123 |
|
---|
124 | //
|
---|
125 | // Check if the new task is already existing in the list
|
---|
126 | //
|
---|
127 | const TObject *objn = fTasks.FindObject(name);
|
---|
128 | const TObject *objt = fTasks.FindObject(task);
|
---|
129 |
|
---|
130 | if (objn || objt)
|
---|
131 | {
|
---|
132 | //
|
---|
133 | // If the task is already in the list ignore it.
|
---|
134 | //
|
---|
135 | if (objt || objn==task)
|
---|
136 | {
|
---|
137 | *fLog << dbginf << "Warning: Task '" << task->GetName() << ", 0x" << (void*)task;
|
---|
138 | *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
|
---|
139 | return kTRUE;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //
|
---|
143 | // Otherwise add it to the list, but print a warning message
|
---|
144 | //
|
---|
145 | *fLog << dbginf << "Warning: Task with the same name '" << task->GetName();
|
---|
146 | *fLog << "' already existing in '" << GetName() << "'." << endl;
|
---|
147 | *fLog << "You may not be able to get a pointer to this task by name." << endl;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (where)
|
---|
151 | {
|
---|
152 | if (!fTasks.FindObject(where))
|
---|
153 | {
|
---|
154 | *fLog << dbginf << "Error: Cannot find task after which the new task should be scheduled!" << endl;
|
---|
155 | return kFALSE;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | *fLog << "Adding " << name << " to " << GetName() << " for " << type << "... " << flush;
|
---|
160 |
|
---|
161 | task->SetStreamId(type);
|
---|
162 | fTasks.Add(task);
|
---|
163 |
|
---|
164 | *fLog << "Done." << endl;
|
---|
165 |
|
---|
166 | return kTRUE;
|
---|
167 | }
|
---|
168 |
|
---|
169 | // --------------------------------------------------------------------------
|
---|
170 | //
|
---|
171 | // do pre processing (before eventloop) of all tasks in the task-list
|
---|
172 | //
|
---|
173 | Bool_t MTaskList::PreProcess(MParList *pList)
|
---|
174 | {
|
---|
175 | *fLog << "Preprocessing... " << flush;
|
---|
176 |
|
---|
177 | fParList = pList;
|
---|
178 |
|
---|
179 | //
|
---|
180 | // create the Iterator over the tasklist
|
---|
181 | //
|
---|
182 | TIter Next(&fTasks);
|
---|
183 |
|
---|
184 | MTask *task=NULL;
|
---|
185 |
|
---|
186 | //
|
---|
187 | // loop over all tasks for preproccesing
|
---|
188 | //
|
---|
189 | while ( (task=(MTask*)Next()) )
|
---|
190 | {
|
---|
191 | *fLog << task->GetName() << "... " << flush;
|
---|
192 |
|
---|
193 | if (!task->PreProcess(fParList))
|
---|
194 | return kFALSE;
|
---|
195 |
|
---|
196 | task->SetIsPreprocessed();
|
---|
197 | }
|
---|
198 |
|
---|
199 | *fLog << endl;
|
---|
200 |
|
---|
201 | return kTRUE;
|
---|
202 | }
|
---|
203 |
|
---|
204 | // --------------------------------------------------------------------------
|
---|
205 | //
|
---|
206 | // do the event execution of all tasks in the task-list
|
---|
207 | //
|
---|
208 | Bool_t MTaskList::Process()
|
---|
209 | {
|
---|
210 | //
|
---|
211 | // Reset the ReadyToSave flag.
|
---|
212 | // Reset all containers.
|
---|
213 | //
|
---|
214 | // FIXME: To run a tasklist as a single task in another tasklist we
|
---|
215 | // have to make sure, that the Parameter list isn't reset.
|
---|
216 | //
|
---|
217 | fParList->SetReadyToSave(kFALSE);
|
---|
218 | fParList->Reset();
|
---|
219 |
|
---|
220 | //
|
---|
221 | // create the Iterator for the TaskList
|
---|
222 | //
|
---|
223 | TIter Next(&fTasks);
|
---|
224 | MTask *task=NULL;
|
---|
225 |
|
---|
226 | //
|
---|
227 | // loop over all tasks for processing
|
---|
228 | //
|
---|
229 | while ( (task=(MTask*)Next()) )
|
---|
230 | {
|
---|
231 | //
|
---|
232 | // if the task has the wrong stream id skip it.
|
---|
233 | //
|
---|
234 | if (strcmp(GetStreamId(), task->GetStreamId()) &&
|
---|
235 | strcmp(task->GetStreamId(), "All"))
|
---|
236 | continue;
|
---|
237 |
|
---|
238 | //
|
---|
239 | // Check for the existance of a filter. If a filter is existing
|
---|
240 | // check for its value. If the value is kFALSE don't execute
|
---|
241 | // this task.
|
---|
242 | //
|
---|
243 | const MFilter *filter = task->GetFilter();
|
---|
244 |
|
---|
245 | const Bool_t rc = filter ? filter->IsExpressionTrue() : kTRUE;
|
---|
246 |
|
---|
247 | if (!rc)
|
---|
248 | continue;
|
---|
249 |
|
---|
250 | //
|
---|
251 | // if it has the right stream id execute the Process() function
|
---|
252 | // and check what the result of it is.
|
---|
253 | //
|
---|
254 | switch (task->Process())
|
---|
255 | {
|
---|
256 | case kTRUE:
|
---|
257 | //
|
---|
258 | // everything was OK: go on
|
---|
259 | //
|
---|
260 | continue;
|
---|
261 |
|
---|
262 | case kFALSE:
|
---|
263 | //
|
---|
264 | // an error occured: stop eventloop
|
---|
265 | //
|
---|
266 | return kFALSE;
|
---|
267 |
|
---|
268 | case kCONTINUE:
|
---|
269 | //
|
---|
270 | // something occured: skip the rest of the tasks for this event
|
---|
271 | //
|
---|
272 | break;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | return kTRUE;
|
---|
276 | }
|
---|
277 |
|
---|
278 | // --------------------------------------------------------------------------
|
---|
279 | //
|
---|
280 | // do post processing (before eventloop) of all tasks in the task-list
|
---|
281 | //
|
---|
282 | Bool_t MTaskList::PostProcess()
|
---|
283 | {
|
---|
284 | *fLog << "Postprocessing... " << flush;
|
---|
285 |
|
---|
286 | // FIXME: At the moment all tasks are post processed independ of
|
---|
287 | // whether it was preprocessed or not.
|
---|
288 |
|
---|
289 | //
|
---|
290 | // Reset the ReadyToSave flag.
|
---|
291 | // Reset all containers.
|
---|
292 | //
|
---|
293 | // FIXME: To run a tasklist as a single task in another tasklist we
|
---|
294 | // have to make sure, that the Parameter list isn't reset.
|
---|
295 | //
|
---|
296 | fParList->SetReadyToSave(kFALSE);
|
---|
297 | fParList->Reset();
|
---|
298 |
|
---|
299 | //
|
---|
300 | // create the Iterator for the TaskList
|
---|
301 | //
|
---|
302 | TIter Next(&fTasks);
|
---|
303 |
|
---|
304 | MTask *task=NULL;
|
---|
305 |
|
---|
306 | //
|
---|
307 | // loop over all tasks for postprocessing
|
---|
308 | //
|
---|
309 | while ( (task=(MTask*)Next()) )
|
---|
310 | {
|
---|
311 | if (!task->IsPreprocessed())
|
---|
312 | continue;
|
---|
313 |
|
---|
314 | *fLog << task->GetName() << "... " << flush;
|
---|
315 |
|
---|
316 | if (!task->PostProcess())
|
---|
317 | return kFALSE;
|
---|
318 | }
|
---|
319 |
|
---|
320 | *fLog << endl;
|
---|
321 |
|
---|
322 | return kTRUE;
|
---|
323 | }
|
---|
324 |
|
---|
325 | // --------------------------------------------------------------------------
|
---|
326 | void MTaskList::Print(Option_t *t)
|
---|
327 | {
|
---|
328 | *fLog << "TaskList: " << this->GetName() << " <" << this->GetTitle() << ">" << endl;
|
---|
329 |
|
---|
330 | fTasks.Print();
|
---|
331 |
|
---|
332 | *fLog << endl;
|
---|
333 | }
|
---|
334 |
|
---|