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

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