source: trunk/MagicSoft/Mars/mbase/MTaskList.cc@ 1666

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