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 <mailto: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 | // Warning: //
|
---|
46 | // Be carefull if you are writing your tasklist //
|
---|
47 | // (eg. MWriteRootFile("file.root", "MTaskList")) to a file. You may //
|
---|
48 | // not be able to initialize a new working tasklist from a file if //
|
---|
49 | // a) Two Paramerer containers with the same names are existing in the //
|
---|
50 | // MParList. //
|
---|
51 | // b) You used a container somewhere which is not part of MParList. //
|
---|
52 | // (eg. You specified a pointer to a MH container in MFillH which is //
|
---|
53 | // not added to the parameter list. //
|
---|
54 | // //
|
---|
55 | /////////////////////////////////////////////////////////////////////////////
|
---|
56 |
|
---|
57 | #include "MTaskList.h"
|
---|
58 |
|
---|
59 | #include <fstream.h> // ofstream, SavePrimitive
|
---|
60 |
|
---|
61 | #include <TClass.h>
|
---|
62 | #include <TBaseClass.h>
|
---|
63 | #include <TOrdCollection.h>
|
---|
64 |
|
---|
65 | #include "MLog.h"
|
---|
66 | #include "MLogManip.h"
|
---|
67 |
|
---|
68 | #include "MParList.h"
|
---|
69 | #include "MInputStreamID.h"
|
---|
70 |
|
---|
71 | ClassImp(MTaskList);
|
---|
72 |
|
---|
73 | static const TString gsDefName = "MTaskList";
|
---|
74 | static const TString gsDefTitle = "A list for tasks to be executed";
|
---|
75 | // --------------------------------------------------------------------------
|
---|
76 | //
|
---|
77 | // the name for the task list must be the same for all task lists
|
---|
78 | // because the task list (at the moment) is identified by exactly
|
---|
79 | // this name in the parameter list (by MEvtLoop::SetParList)
|
---|
80 | //
|
---|
81 | MTaskList::MTaskList(const char *name, const char *title)
|
---|
82 | {
|
---|
83 | fName = name ? name : gsDefName.Data();
|
---|
84 | fTitle = title ? title : gsDefTitle.Data();
|
---|
85 |
|
---|
86 | fTasks = new TList;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // --------------------------------------------------------------------------
|
---|
90 | //
|
---|
91 | // CopyConstructor
|
---|
92 | // creates a new TaskList and put the contents of an existing
|
---|
93 | // TaskList in the new TaskList.
|
---|
94 | //
|
---|
95 | MTaskList::MTaskList(MTaskList &ts)
|
---|
96 | {
|
---|
97 | fTasks->AddAll(ts.fTasks);
|
---|
98 | }
|
---|
99 |
|
---|
100 | // --------------------------------------------------------------------------
|
---|
101 | //
|
---|
102 | // If the 'IsOwner' bit is set (via SetOwner()) all tasks are deleted
|
---|
103 | // by the destructor
|
---|
104 | //
|
---|
105 | MTaskList::~MTaskList()
|
---|
106 | {
|
---|
107 | if (TestBit(kIsOwner))
|
---|
108 | fTasks->SetOwner();
|
---|
109 |
|
---|
110 | delete fTasks;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // --------------------------------------------------------------------------
|
---|
114 | //
|
---|
115 | // If the 'IsOwner' bit is set (via SetOwner()) all containers are deleted
|
---|
116 | // by the destructor
|
---|
117 | //
|
---|
118 | void MTaskList::SetOwner(Bool_t enable)
|
---|
119 | {
|
---|
120 | enable ? SetBit(kIsOwner) : ResetBit(kIsOwner);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | // --------------------------------------------------------------------------
|
---|
125 | //
|
---|
126 | // Set the logging stream for the all tasks in the list and the tasklist
|
---|
127 | // itself.
|
---|
128 | //
|
---|
129 | void MTaskList::SetLogStream(MLog *log)
|
---|
130 | {
|
---|
131 | fTasks->ForEach(MTask, SetLogStream)(log);
|
---|
132 | MParContainer::SetLogStream(log);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | // --------------------------------------------------------------------------
|
---|
137 | //
|
---|
138 | // schedule task for execution, whether as first task, or after
|
---|
139 | // 'where'. 'tType' is the event type which should be processed
|
---|
140 | //
|
---|
141 | Bool_t MTaskList::AddToList(MTask *task, const char *type, MTask *where)
|
---|
142 | {
|
---|
143 | // FIXME: We agreed to put the task into list in an ordered way.
|
---|
144 |
|
---|
145 | //
|
---|
146 | // Sanity check
|
---|
147 | //
|
---|
148 | if (!task)
|
---|
149 | return kFALSE;
|
---|
150 |
|
---|
151 | //
|
---|
152 | // Get Name of new task
|
---|
153 | //
|
---|
154 | const char *name = task->GetName();
|
---|
155 |
|
---|
156 | //
|
---|
157 | // Check if the new task is already existing in the list
|
---|
158 | //
|
---|
159 | const TObject *objn = fTasks->FindObject(name);
|
---|
160 | const TObject *objt = fTasks->FindObject(task);
|
---|
161 |
|
---|
162 | if (objn || objt)
|
---|
163 | {
|
---|
164 | //
|
---|
165 | // If the task is already in the list ignore it.
|
---|
166 | //
|
---|
167 | if (objt || objn==task)
|
---|
168 | {
|
---|
169 | *fLog << warn << dbginf << "Warning: Task '" << task->GetName() << ", 0x" << (void*)task;
|
---|
170 | *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
|
---|
171 | return kTRUE;
|
---|
172 | }
|
---|
173 |
|
---|
174 | //
|
---|
175 | // Otherwise add it to the list, but print a warning message
|
---|
176 | //
|
---|
177 | *fLog << warn << dbginf << "Warning: Task '" << task->GetName();
|
---|
178 | *fLog << "' already existing in '" << GetName() << "'." << endl;
|
---|
179 | *fLog << "You may not be able to get a pointer to this task by name." << endl;
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (where)
|
---|
183 | {
|
---|
184 | if (!fTasks->FindObject(where))
|
---|
185 | {
|
---|
186 | *fLog << err << dbginf << "Error: Cannot find task after which the new task should be scheduled!" << endl;
|
---|
187 | return kFALSE;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | *fLog << inf << "Adding " << name << " to " << GetName() << " for " << type << "... " << flush;
|
---|
192 |
|
---|
193 | task->SetStreamId(type);
|
---|
194 | fTasks->Add(task);
|
---|
195 |
|
---|
196 | *fLog << "Done." << endl;
|
---|
197 |
|
---|
198 | return kTRUE;
|
---|
199 | }
|
---|
200 |
|
---|
201 | // --------------------------------------------------------------------------
|
---|
202 | //
|
---|
203 | // Find an object in the list.
|
---|
204 | // 'name' is the name of the object you are searching for.
|
---|
205 | //
|
---|
206 | TObject *MTaskList::FindObject(const char *name) const
|
---|
207 | {
|
---|
208 | return fTasks->FindObject(name);
|
---|
209 | }
|
---|
210 |
|
---|
211 | // --------------------------------------------------------------------------
|
---|
212 | //
|
---|
213 | // check if the object is in the list or not
|
---|
214 | //
|
---|
215 | TObject *MTaskList::FindObject(const TObject *obj) const
|
---|
216 | {
|
---|
217 | return fTasks->FindObject(obj);
|
---|
218 | }
|
---|
219 |
|
---|
220 | // --------------------------------------------------------------------------
|
---|
221 | //
|
---|
222 | // do reinit of all tasks in the task-list
|
---|
223 | //
|
---|
224 | Bool_t MTaskList::ReInit(MParList *pList)
|
---|
225 | {
|
---|
226 | *fLog << all << "Reinit... " << flush;
|
---|
227 |
|
---|
228 | //
|
---|
229 | // create the Iterator over the tasklist
|
---|
230 | //
|
---|
231 | TIter Next(fTasks);
|
---|
232 |
|
---|
233 | MTask *task=NULL;
|
---|
234 | //
|
---|
235 | // loop over all tasks for preproccesing
|
---|
236 | //
|
---|
237 | while ((task=(MTask*)Next()))
|
---|
238 | {
|
---|
239 | *fLog << all << task->GetName() << "... " << flush;
|
---|
240 |
|
---|
241 | if (!task->ReInit(pList?pList:fParList))
|
---|
242 | {
|
---|
243 | *fLog << err << "ERROR - ReInit if Task " << task->GetDescriptor() << " failed." << endl;
|
---|
244 | return kFALSE;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | *fLog << all << endl;
|
---|
249 |
|
---|
250 | return kTRUE;
|
---|
251 | }
|
---|
252 |
|
---|
253 | // --------------------------------------------------------------------------
|
---|
254 | //
|
---|
255 | // removes a task from the list (used in PreProcess).
|
---|
256 | // if kIsOwner is set the task is deleted. (see SetOwner())
|
---|
257 | //
|
---|
258 | void MTaskList::Remove(MTask *task)
|
---|
259 | {
|
---|
260 | TObject *obj = fTasks->Remove(task);
|
---|
261 |
|
---|
262 | if (TestBit(kIsOwner))
|
---|
263 | delete obj;
|
---|
264 | }
|
---|
265 |
|
---|
266 | // --------------------------------------------------------------------------
|
---|
267 | //
|
---|
268 | // Check whether this task (or one of it's base classes) overloads
|
---|
269 | // MTask::Process. Only if this function is overloaded this task is
|
---|
270 | // added to the fTaskProcess-List. This makes the execution of the
|
---|
271 | // tasklist a little bit (only a little bit) faster, bacause tasks
|
---|
272 | // doing no Processing are not Processed.
|
---|
273 | //
|
---|
274 | Bool_t MTaskList::CheckClassForProcess(TClass *cls)
|
---|
275 | {
|
---|
276 | //
|
---|
277 | // Check whether the class itself overloads the Process function
|
---|
278 | //
|
---|
279 | if (cls->GetName()=="MTask")
|
---|
280 | return kFALSE;
|
---|
281 |
|
---|
282 | if (cls->GetMethodAny("Process"))
|
---|
283 | return kTRUE;
|
---|
284 |
|
---|
285 | //
|
---|
286 | // If the class itself doesn't overload it check all it's base classes
|
---|
287 | //
|
---|
288 | TBaseClass *base=NULL;
|
---|
289 | TIter NextBase(cls->GetListOfBases());
|
---|
290 | while ((base=(TBaseClass*)NextBase()))
|
---|
291 | {
|
---|
292 | if (CheckClassForProcess(base->GetClassPointer()))
|
---|
293 | return kTRUE;
|
---|
294 | }
|
---|
295 |
|
---|
296 | return kFALSE;
|
---|
297 | }
|
---|
298 |
|
---|
299 | // --------------------------------------------------------------------------
|
---|
300 | //
|
---|
301 | // do pre processing (before eventloop) of all tasks in the task-list
|
---|
302 | //
|
---|
303 | Bool_t MTaskList::PreProcess(MParList *pList)
|
---|
304 | {
|
---|
305 | *fLog << all << "Preprocessing... " << flush;
|
---|
306 |
|
---|
307 | fParList = pList;
|
---|
308 |
|
---|
309 | fTasksProcess.Clear();
|
---|
310 |
|
---|
311 | //
|
---|
312 | // create the Iterator over the tasklist
|
---|
313 | //
|
---|
314 | TIter Next(fTasks);
|
---|
315 |
|
---|
316 | MTask *task=NULL;
|
---|
317 |
|
---|
318 | //
|
---|
319 | // loop over all tasks for preproccesing
|
---|
320 | //
|
---|
321 | while ((task=(MTask*)Next()))
|
---|
322 | {
|
---|
323 | *fLog << all << task->GetName() << "... " << flush;
|
---|
324 |
|
---|
325 | if (CheckClassForProcess(task->IsA()))
|
---|
326 | fTasksProcess.Add(task);
|
---|
327 |
|
---|
328 | //
|
---|
329 | // PreProcess the task and check for it's return value.
|
---|
330 | //
|
---|
331 | switch (task->CallPreProcess(fParList))
|
---|
332 | {
|
---|
333 | case kFALSE:
|
---|
334 | return kFALSE;
|
---|
335 |
|
---|
336 | case kTRUE:
|
---|
337 | continue;
|
---|
338 |
|
---|
339 | case kSKIP:
|
---|
340 | Remove(task);
|
---|
341 | continue;
|
---|
342 | }
|
---|
343 |
|
---|
344 | *fLog << err << dbginf << "PreProcess of " << task->GetDescriptor();
|
---|
345 | *fLog << " returned an unknown value... aborting." << endl;
|
---|
346 | return kFALSE;
|
---|
347 | }
|
---|
348 |
|
---|
349 | *fLog << all << endl;
|
---|
350 |
|
---|
351 | return kTRUE;
|
---|
352 | }
|
---|
353 |
|
---|
354 | // --------------------------------------------------------------------------
|
---|
355 | //
|
---|
356 | // do the event execution of all tasks in the task-list
|
---|
357 | //
|
---|
358 | Bool_t MTaskList::Process()
|
---|
359 | {
|
---|
360 | //
|
---|
361 | // Check whether there is something which can be processed, otherwise
|
---|
362 | // stop the eventloop.
|
---|
363 | //
|
---|
364 | if (fTasksProcess.GetSize()==0)
|
---|
365 | {
|
---|
366 | *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
|
---|
367 | return kFALSE;
|
---|
368 | }
|
---|
369 |
|
---|
370 | //
|
---|
371 | // Reset the ReadyToSave flag.
|
---|
372 | // Reset all containers.
|
---|
373 | //
|
---|
374 | // FIXME: To run a tasklist as a single task in another tasklist we
|
---|
375 | // have to make sure, that the Parameter list isn't reset.
|
---|
376 | //
|
---|
377 | fParList->SetReadyToSave(kFALSE);
|
---|
378 | fParList->Reset();
|
---|
379 |
|
---|
380 | //
|
---|
381 | // create the Iterator for the TaskList
|
---|
382 | //
|
---|
383 | TIter Next(&fTasksProcess);
|
---|
384 | MTask *task=NULL;
|
---|
385 |
|
---|
386 | //
|
---|
387 | // loop over all tasks for processing
|
---|
388 | //
|
---|
389 | while ( (task=(MTask*)Next()) )
|
---|
390 | {
|
---|
391 | //
|
---|
392 | // if the task has the wrong stream id skip it.
|
---|
393 | //
|
---|
394 | if (GetStreamId() != task->GetStreamId() &&
|
---|
395 | task->GetStreamId() != "All")
|
---|
396 | continue;
|
---|
397 |
|
---|
398 | //
|
---|
399 | // if it has the right stream id execute the CallProcess() function
|
---|
400 | // and check what the result of it is.
|
---|
401 | // The CallProcess() function increases the execution counter and
|
---|
402 | // calls the Process() function dependent on the existance and
|
---|
403 | // return value of a filter.
|
---|
404 | //
|
---|
405 | switch (task->CallProcess())
|
---|
406 | {
|
---|
407 | case kTRUE:
|
---|
408 | //
|
---|
409 | // everything was OK: go on with the next task
|
---|
410 | //
|
---|
411 | continue;
|
---|
412 |
|
---|
413 | case kFALSE:
|
---|
414 | //
|
---|
415 | // an error occured: stop eventloop
|
---|
416 | //
|
---|
417 | return kFALSE;
|
---|
418 |
|
---|
419 | case kCONTINUE:
|
---|
420 | //
|
---|
421 | // something occured: skip the rest of the tasks for this event
|
---|
422 | //
|
---|
423 | return kTRUE;
|
---|
424 |
|
---|
425 | default:
|
---|
426 | *fLog << warn << "MTaskList::Process: Unknown return value from MTask::Process()... ignored." << endl;
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | return kTRUE;
|
---|
431 | }
|
---|
432 |
|
---|
433 | // --------------------------------------------------------------------------
|
---|
434 | //
|
---|
435 | // do post processing (before eventloop) of all tasks in the task-list
|
---|
436 | // only tasks which have successfully been preprocessed are postprocessed.
|
---|
437 | //
|
---|
438 | Bool_t MTaskList::PostProcess()
|
---|
439 | {
|
---|
440 | *fLog << all << "Postprocessing... " << flush;
|
---|
441 |
|
---|
442 | //
|
---|
443 | // Reset the ReadyToSave flag.
|
---|
444 | // Reset all containers.
|
---|
445 | //
|
---|
446 | // FIXME: To run a tasklist as a single task in another tasklist we
|
---|
447 | // have to make sure, that the Parameter list isn't reset.
|
---|
448 | //
|
---|
449 | fParList->SetReadyToSave(kFALSE);
|
---|
450 | fParList->Reset();
|
---|
451 |
|
---|
452 | //
|
---|
453 | // create the Iterator for the TaskList
|
---|
454 | //
|
---|
455 | TIter Next(fTasks);
|
---|
456 |
|
---|
457 | MTask *task=NULL;
|
---|
458 |
|
---|
459 | //
|
---|
460 | // loop over all tasks for postprocessing
|
---|
461 | // only tasks which have successfully been preprocessed are postprocessed.
|
---|
462 | //
|
---|
463 | while ( (task=(MTask*)Next()) )
|
---|
464 | {
|
---|
465 | *fLog << all << task->GetName() << "... " << flush;
|
---|
466 |
|
---|
467 | if (!task->CallPostProcess())
|
---|
468 | return kFALSE;
|
---|
469 | }
|
---|
470 |
|
---|
471 | *fLog << all << endl;
|
---|
472 |
|
---|
473 | return kTRUE;
|
---|
474 | }
|
---|
475 |
|
---|
476 | // --------------------------------------------------------------------------
|
---|
477 | //
|
---|
478 | // Prints the number of times all the tasks in the list has been.
|
---|
479 | // For convinience the lvl argument results in a number of spaces at the
|
---|
480 | // beginning of the line. So that the structur of a tasklist can be
|
---|
481 | // identified. Use MTaskList::PrintStatistics without an argument.
|
---|
482 | //
|
---|
483 | void MTaskList::PrintStatistics(const Int_t lvl) const
|
---|
484 | {
|
---|
485 | if (lvl==0)
|
---|
486 | {
|
---|
487 | *fLog << all << endl;
|
---|
488 | *fLog << "Execution Statistics: " << endl;
|
---|
489 | *fLog << "---------------------" << endl;
|
---|
490 | *fLog << GetDescriptor() << endl;
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | *fLog << setw(lvl) << " " << GetDescriptor() << endl;
|
---|
495 | }
|
---|
496 |
|
---|
497 | //
|
---|
498 | // create the Iterator for the TaskList
|
---|
499 | //
|
---|
500 | fTasks->ForEach(MTask, PrintStatistics)(lvl+1);
|
---|
501 |
|
---|
502 | if (lvl==0)
|
---|
503 | *fLog << endl;
|
---|
504 | }
|
---|
505 |
|
---|
506 | // --------------------------------------------------------------------------
|
---|
507 | void MTaskList::Print(Option_t *t) const
|
---|
508 | {
|
---|
509 | *fLog << all << endl;
|
---|
510 | *fLog << GetDescriptor() << endl;
|
---|
511 | *fLog << setfill('-') << setw(strlen(GetDescriptor())) << "" << endl;
|
---|
512 |
|
---|
513 | fTasks->Print();
|
---|
514 |
|
---|
515 | *fLog << endl;
|
---|
516 | }
|
---|
517 |
|
---|
518 | // --------------------------------------------------------------------------
|
---|
519 | //
|
---|
520 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
---|
521 | // to a macro. In the original root implementation it is used to write
|
---|
522 | // gui elements to a macro-file.
|
---|
523 | //
|
---|
524 | void MTaskList::StreamPrimitive(ofstream &out) const
|
---|
525 | {
|
---|
526 | out << " MTaskList " << GetUniqueName();
|
---|
527 | if (fName!=gsDefName || fTitle!=gsDefTitle)
|
---|
528 | {
|
---|
529 | out << "(\"" << fName << "\"";
|
---|
530 | if (fTitle!=gsDefTitle)
|
---|
531 | out << ", \"" << fTitle << "\"";
|
---|
532 | out <<")";
|
---|
533 | }
|
---|
534 | out << ";" << endl << endl;
|
---|
535 |
|
---|
536 | TIter Next(fTasks);
|
---|
537 |
|
---|
538 | MParContainer *cont = NULL;
|
---|
539 | while ((cont=(MParContainer*)Next()))
|
---|
540 | {
|
---|
541 | cont->SavePrimitive(out, "");
|
---|
542 | out << " " << GetUniqueName() << ".AddToList(&";
|
---|
543 | out << cont->GetUniqueName() << ");" << endl << endl;
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | void MTaskList::GetNames(TObjArray &arr) const
|
---|
548 | {
|
---|
549 | MParContainer::GetNames(arr);
|
---|
550 | fTasks->ForEach(MParContainer, GetNames)(arr);
|
---|
551 | }
|
---|
552 |
|
---|
553 | void MTaskList::SetNames(TObjArray &arr)
|
---|
554 | {
|
---|
555 | MParContainer::SetNames(arr);
|
---|
556 | fTasks->ForEach(MParContainer, SetNames)(arr);
|
---|
557 | }
|
---|
558 |
|
---|