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

Last change on this file since 8619 was 8618, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 30.1 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@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
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// Remark: The Process function is only executed if the class of your task
46// overloads Process() or if the task itself is a MTask. This
47// means if you have a task without Process() (only PreProcess
48// and PostProcess no time is lost during execution)
49//
50// Warning:
51// Be carefull if you are writing your tasklist
52// (eg. MWriteRootFile("file.root", "MTaskList")) to a file. You may
53// not be able to initialize a new working tasklist from a file if
54// a) Two Paramerer containers with the same names are existing in the
55// MParList.
56// b) You used a container somewhere which is not part of MParList.
57// (eg. You specified a pointer to a MH container in MFillH which is
58// not added to the parameter list.
59//
60/////////////////////////////////////////////////////////////////////////////
61#include "MTaskList.h"
62
63#include <fstream> // ofstream, SavePrimitive
64
65#include <TSystem.h> // gSystem
66#include <TOrdCollection.h> // TOrdCollection
67
68#include "MLog.h"
69#include "MLogManip.h"
70
71#include "MIter.h"
72#include "MFilter.h"
73#include "MParList.h"
74#include "MInputStreamID.h"
75
76#include "MStatusDisplay.h"
77
78ClassImp(MTaskList);
79
80using namespace std;
81
82const TString MTaskList::gsDefName = "MTaskList";
83const TString MTaskList::gsDefTitle = "A list for tasks to be executed";
84
85// --------------------------------------------------------------------------
86//
87// the name for the task list must be the same for all task lists
88// because the task list (at the moment) is identified by exactly
89// this name in the parameter list (by MEvtLoop::SetParList)
90//
91MTaskList::MTaskList(const char *name, const char *title) : fNumPasses(0), fNumPass(0)
92{
93 fName = name ? name : gsDefName.Data();
94 fTitle = title ? title : gsDefTitle.Data();
95
96 fTasks = new TList;
97
98 gROOT->GetListOfCleanups()->Add(fTasks);
99 gROOT->GetListOfCleanups()->Add(&fTasksProcess);
100 fTasks->SetBit(kMustCleanup);
101 fTasksProcess.SetBit(kMustCleanup);
102}
103
104// --------------------------------------------------------------------------
105//
106// CopyConstructor
107// creates a new TaskList and put the contents of an existing
108// TaskList in the new TaskList.
109//
110MTaskList::MTaskList(const MTaskList &ts) : MTask(), fNumPasses(ts.fNumPasses)
111{
112 fTasks->AddAll(ts.fTasks);
113}
114
115// --------------------------------------------------------------------------
116//
117// If the 'IsOwner' bit is set (via SetOwner()) all tasks are deleted
118// by the destructor
119//
120MTaskList::~MTaskList()
121{
122 if (TestBit(kIsOwner))
123 fTasks->SetOwner();
124
125 delete fTasks;
126}
127
128// --------------------------------------------------------------------------
129//
130// If the 'IsOwner' bit is set (via SetOwner()) all containers are deleted
131// by the destructor
132//
133void MTaskList::SetOwner(Bool_t enable)
134{
135 enable ? SetBit(kIsOwner) : ResetBit(kIsOwner);
136}
137
138
139// --------------------------------------------------------------------------
140//
141// Set the logging stream for the all tasks in the list and the tasklist
142// itself.
143//
144void MTaskList::SetLogStream(MLog *log)
145{
146 fTasks->R__FOR_EACH(MTask, SetLogStream)(log);
147 MTask::SetLogStream(log);
148}
149
150// --------------------------------------------------------------------------
151//
152// Set the display for the all tasks in the list and the tasklist itself.
153//
154void MTaskList::SetDisplay(MStatusDisplay *d)
155{
156 fTasks->R__FOR_EACH(MTask, SetDisplay)(d);
157 MTask::SetDisplay(d);
158}
159
160void MTaskList::SetAccelerator(Byte_t acc)
161{
162 fTasks->R__FOR_EACH(MTask, SetAccelerator)(acc);
163 MTask::SetAccelerator(acc);
164}
165
166// --------------------------------------------------------------------------
167//
168// Set the serial number for the all tasks in the list and the tasklist
169// itself.
170//
171void MTaskList::SetSerialNumber(Byte_t num)
172{
173 fTasks->R__FOR_EACH(MTask, SetSerialNumber)(num);
174 MTask::SetSerialNumber(num);
175}
176
177Bool_t MTaskList::CheckAddToList(MTask *task, /*const char *type,*/ const MTask *where) const
178{
179 //
180 // Sanity check
181 //
182 if (!task)
183 {
184 *fLog << err << "ERROR - task argument=NULL." << endl;
185 return kFALSE;
186 }
187
188 //
189 // Get Name of new task
190 //
191 const char *name = task->GetName();
192
193 //
194 // Check if the new task is already existing in the list
195 //
196 const TObject *objn = fTasks->FindObject(name);
197 const TObject *objt = fTasks->FindObject(task);
198
199 if (objn || objt)
200 {
201 //
202 // If the task is already in the list ignore it.
203 //
204 if (objt || objn==task)
205 {
206 *fLog << warn << dbginf << "Warning: Task '" << task->GetName() << ", 0x" << (void*)task;
207 *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
208 return kTRUE;
209 }
210
211 //
212 // Otherwise add it to the list, but print a warning message
213 //
214 *fLog << warn << dbginf << "Warning: Task '" << task->GetName();
215 *fLog << "' already existing in '" << GetName() << "'." << endl;
216 *fLog << "You may not be able to get a pointer to this task by name." << endl;
217 }
218
219 if (!where)
220 return kTRUE;
221
222 if (fTasks->FindObject(where))
223 return kTRUE;
224
225 *fLog << err << dbginf << "Error: Cannot find task after which the new task should be scheduled!" << endl;
226 return kFALSE;
227}
228
229
230// --------------------------------------------------------------------------
231//
232// schedule task for execution, before 'where'.
233// 'type' is the event type which should be processed
234//
235Bool_t MTaskList::AddToListBefore(MTask *task, const MTask *where, const char *type)
236{
237 if (task==this)
238 {
239 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
240 *fLog << " would create infinite recursions...ignored." << endl;
241 return kFALSE;
242
243 }
244
245 // FIXME: We agreed to put the task into list in an ordered way.
246 if (!CheckAddToList(task, /*type,*/ where))
247 return kFALSE;
248
249 const TString stream = type ? (TString)type : task->GetStreamId();
250
251 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << stream << "... " << flush;
252 task->SetStreamId(stream);
253 task->SetBit(kMustCleanup);
254 fTasks->AddBefore((TObject*)where, task);
255 *fLog << "Done." << endl;
256
257 return kTRUE;
258}
259
260// --------------------------------------------------------------------------
261//
262// schedule task for execution, after 'where'.
263// 'type' is the event type which should be processed
264//
265Bool_t MTaskList::AddToListAfter(MTask *task, const MTask *where, const char *type)
266{
267 if (task==this)
268 {
269 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
270 *fLog << " would create infinite recursions...ignored." << endl;
271 return kFALSE;
272
273 }
274
275 // FIXME: We agreed to put the task into list in an ordered way.
276 if (!CheckAddToList(task, /*type,*/ where))
277 return kFALSE;
278
279 const TString stream = type ? (TString)type : task->GetStreamId();
280
281 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << stream << "... " << flush;
282 task->SetStreamId(stream);
283 task->SetBit(kMustCleanup);
284 fTasks->AddAfter((TObject*)where, task);
285 *fLog << "Done." << endl;
286
287 return kTRUE;
288}
289
290// --------------------------------------------------------------------------
291//
292// schedule task for execution, 'type' is the event type which should
293// be processed
294//
295Bool_t MTaskList::AddToList(MTask *task, const char *type)
296{
297 if (task==this)
298 {
299 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
300 *fLog << " would create infinite recursions...ignored." << endl;
301 return kFALSE;
302
303 }
304
305 // FIXME: We agreed to put the task into list in an ordered way.
306 if (!CheckAddToList(task/*, type*/))
307 return kFALSE;
308
309 const TString stream = type ? (TString)type : task->GetStreamId();
310
311 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << stream << "... " << flush;
312 task->SetStreamId(stream);
313 task->SetBit(kMustCleanup);
314 fTasks->Add(task);
315 *fLog << "Done." << endl;
316
317 return kTRUE;
318}
319
320// --------------------------------------------------------------------------
321//
322// Add all objects in list to the tasklist. If some of them do not
323// inherit from MTask return kFALSE, also if AddToList returns an error
324// for one of the tasks
325//
326Bool_t MTaskList::AddToList(const TList &list, const char *tType)
327{
328 TIter Next(&list);
329 TObject *obj=0;
330 while ((obj=Next()))
331 {
332 if (!obj->InheritsFrom(MTask::Class()))
333 {
334 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
335 return kFALSE;
336 }
337
338 if (!AddToList(static_cast<MTask*>(obj), tType))
339 return kFALSE;
340 }
341 return kTRUE;
342}
343
344// --------------------------------------------------------------------------
345//
346// Add all objects in list to the tasklist after task where. If some of
347// them do not inherit from MTask return kFALSE, also if AddToListAfter
348// returns an error for one of the tasks
349//
350Bool_t MTaskList::AddToListAfter(const TList &list, const MTask *where, const char *tType)
351{
352 TIter Next(&list);
353 TObject *obj=0;
354 while ((obj=Next()))
355 {
356 if (!obj->InheritsFrom(MTask::Class()))
357 {
358 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
359 return kFALSE;
360 }
361
362 if (!AddToListAfter(static_cast<MTask*>(obj), where, tType))
363 return kFALSE;
364
365 where = static_cast<MTask*>(obj);
366 }
367 return kTRUE;
368}
369
370// --------------------------------------------------------------------------
371//
372// Add all objects in list to the tasklist before task where. If some of
373// them do not inherit from MTask return kFALSE, also if AddToListBefore
374// returns an error for one of the tasks
375//
376Bool_t MTaskList::AddToListBefore(const TList &list, const MTask *where, const char *tType)
377{
378 TIter Next(&list);
379 TObject *obj=0;
380 while ((obj=Next()))
381 {
382 if (!obj->InheritsFrom(MTask::Class()))
383 {
384 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
385 return kFALSE;
386 }
387
388 if (!AddToListBefore(static_cast<MTask*>(obj), where, tType))
389 return kFALSE;
390 }
391 return kTRUE;
392}
393
394// --------------------------------------------------------------------------
395//
396// Find an object in the list.
397// 'name' is the name of the object you are searching for.
398//
399TObject *MTaskList::FindObject(const char *name) const
400{
401 return fTasks->FindObject(name);
402}
403
404// --------------------------------------------------------------------------
405//
406// check if the object is in the list or not
407//
408TObject *MTaskList::FindObject(const TObject *obj) const
409{
410 return fTasks->FindObject(obj);
411}
412
413// --------------------------------------------------------------------------
414//
415// find recursively a tasklist which contains a task with name task
416//
417MTaskList *MTaskList::FindTaskList(const char *task)
418{
419 if (FindObject(task))
420 return this;
421
422 TIter Next(fTasks);
423 TObject *o = 0;
424 while ((o=Next()))
425 {
426 MTaskList *l = dynamic_cast<MTaskList*>(o);
427 if (!l)
428 continue;
429
430 if (l->FindObject(task))
431 return l;
432 }
433 return 0;
434}
435
436// --------------------------------------------------------------------------
437//
438// find recursively a tasklist which contains a task task
439//
440MTaskList *MTaskList::FindTaskList(const MTask *task)
441{
442 if (FindObject(task))
443 return this;
444
445 TIter Next(fTasks);
446 TObject *o = 0;
447 while ((o=Next()))
448 {
449 MTaskList *l = dynamic_cast<MTaskList*>(o);
450 if (!l)
451 continue;
452
453 if (l->FindObject(task))
454 return l;
455 }
456
457 return 0;
458}
459
460// --------------------------------------------------------------------------
461//
462// removes a task from the list (used in PreProcess).
463// if kIsOwner is set the task is deleted. (see SetOwner())
464//
465void MTaskList::Remove(MTask *task)
466{
467 TObject *obj = fTasks->Remove(task);
468
469 if (TestBit(kIsOwner))
470 delete obj;
471}
472
473// --------------------------------------------------------------------------
474//
475// do pre processing (before eventloop) of all tasks in the task-list
476// Only if a task overwrites the Process function the task is
477// added to the fTaskProcess-List. This makes the execution of the
478// tasklist a little bit (only a little bit) faster, bacause tasks
479// doing no Processing are not Processed.
480//
481Int_t MTaskList::PreProcess(MParList *pList)
482{
483 *fLog << all << "Preprocessing... " << flush;
484 if (fDisplay)
485 {
486 // Set status lines
487 fDisplay->SetStatusLine1("PreProcessing...");
488 fDisplay->SetStatusLine2("");
489 }
490
491 fParList = pList;
492
493 //
494 // Make sure, that the ReadyToSave flag is not reset from a tasklist
495 // running as a task in another tasklist.
496 //
497 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
498 if (!noreset)
499 fParList->SetBit(MParList::kDoNotReset);
500
501 //
502 // create the Iterator over the tasklist
503 //
504 TIter Next(fTasks);
505
506 MTask *task=NULL;
507
508 //
509 // loop over all tasks for preproccesing
510 //
511 while ((task=(MTask*)Next()))
512 {
513 //
514 // PreProcess the task and check for it's return value.
515 //
516 switch (task->CallPreProcess(fParList))
517 {
518 case kFALSE:
519 return kFALSE;
520
521 case kTRUE:
522 // Handle GUI events (display changes, mouse clicks)
523 if (fDisplay)
524 gSystem->ProcessEvents();
525 continue;
526
527 case kSKIP:
528 Remove(task);
529 continue;
530 }
531
532 *fLog << err << dbginf << "PreProcess of " << task->GetDescriptor();
533 *fLog << " returned an unknown value... aborting." << endl;
534 return kFALSE;
535 }
536
537 *fLog << all << endl;
538
539 //
540 // Reset the ReadyToSave flag.
541 //
542 if (!noreset)
543 {
544 fParList->SetReadyToSave(kFALSE);
545 fParList->ResetBit(MParList::kDoNotReset);
546 }
547
548 //
549 // loop over all tasks to fill fTasksProcess
550 //
551 Next.Reset();
552 fTasksProcess.Clear();
553 while ((task=(MTask*)Next()))
554 if (task->IsA()==MTask::Class() || task->OverwritesProcess())
555 fTasksProcess.Add(task);
556
557 return kTRUE;
558}
559
560// --------------------------------------------------------------------------
561//
562// do reinit of all tasks in the task-list
563//
564Bool_t MTaskList::ReInit(MParList *pList)
565{
566 *fLog << all << "Reinit... " << flush;
567
568 if (!pList)
569 pList = fParList;
570
571 //
572 // Make sure, that the ReadyToSave flag is not reset from a tasklist
573 // running as a task in another tasklist.
574 //
575 const Bool_t noreset = pList->TestBit(MParList::kDoNotReset);
576 if (!noreset)
577 pList->SetBit(MParList::kDoNotReset);
578
579 //
580 // create the Iterator over the tasklist
581 //
582 TIter Next(fTasks);
583
584 MTask *task=NULL;
585
586 //
587 // loop over all tasks for reinitialization
588 //
589 while ((task=(MTask*)Next()))
590 {
591 *fLog << all << task->GetName() << "... " << flush;
592
593 if (!task->ReInit(pList/*?pList:fParList*/))
594 {
595 *fLog << err << "ERROR - ReInit of Task '" << task->GetDescriptor() << "' failed." << endl;
596 return kFALSE;
597 }
598 }
599
600 *fLog << all << endl;
601
602 //
603 // Reset the ReadyToSave flag.
604 //
605 if (!noreset)
606 {
607 pList->SetReadyToSave(kFALSE);
608 pList->ResetBit(MParList::kDoNotReset);
609 }
610
611 return kTRUE;
612}
613
614Int_t MTaskList::ProcessTaskList()
615{
616 //
617 // create the Iterator for the TaskList
618 //
619 TIter Next(&fTasksProcess);
620 MTask *task=NULL;
621
622 //
623 // loop over all tasks for processing
624 //
625 Int_t rc = kTRUE;
626 while ( (task=(MTask*)Next()) )
627 {
628 //
629 // if the task has the wrong stream id skip it.
630 //
631 if (GetStreamId() != task->GetStreamId() &&
632 task->GetStreamId() != "All")
633 continue;
634
635 //
636 // if it has the right stream id execute the CallProcess() function
637 // and check what the result of it is.
638 // The CallProcess() function increases the execution counter and
639 // calls the Process() function dependent on the existance and
640 // return value of a filter.
641 //
642 switch (task->CallProcess())
643 {
644 case kTRUE:
645 //
646 // everything was OK: go on with the next task
647 //
648 continue;
649
650 case kFALSE:
651 //
652 // an error occured: stop eventloop
653 //
654 rc = kFALSE;
655 *fLog << inf << task->GetDescriptor() << " has stopped execution of " << GetDescriptor() << "." << endl;
656 break;
657
658 case kERROR:
659 //
660 // an error occured: stop eventloop and return: failed
661 //
662 *fLog << err << "Fatal error occured while Process() of " << task->GetDescriptor() << "... stopped." << endl;
663 rc = kERROR;
664 break;
665
666 case kCONTINUE:
667 //
668 // something occured: skip the rest of the tasks for this event
669 //
670 rc = kCONTINUE;
671 break;
672
673 default:
674 *fLog << warn << dbginf << "Unknown return value from MTask::Process()... ignored." << endl;
675 continue;
676 }
677 break;
678 }
679
680 return rc;
681}
682
683// --------------------------------------------------------------------------
684//
685// do the event execution of all tasks in the task-list.
686//
687// If a task returns kCONTINUE, kCONTINUE is propagated if the
688// number of passes==0, otherwise kTRUE is returned instead.
689//
690Int_t MTaskList::Process()
691{
692 //
693 // Check whether there is something which can be processed, otherwise
694 // stop the eventloop.
695 //
696 if (fTasksProcess.GetSize()==0)
697 {
698 *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
699 return kFALSE;
700 }
701
702 //
703 // Reset the ReadyToSave flag.
704 // Reset all containers.
705 //
706 // Make sure, that the parameter list is not reset from a tasklist
707 // running as a task in another tasklist.
708 //
709 const Bool_t noreset = fParList->TestBit(MParList::kIsProcessing);
710 if (!noreset)
711 {
712 fParList->SetBit(MParList::kIsProcessing);
713 if (!HasAccelerator(kAccDontReset))
714 fParList->Reset();
715 }
716
717 // Initialize storage variable for the return code
718 Int_t rc = kTRUE;
719
720 // Execute the task list at least once
721 const UInt_t cnt = fNumPasses==0 ? 1 : fNumPasses;
722 for (fNumPass=0; fNumPass<cnt; fNumPass++)
723 {
724 // Execute the tasklist once
725 rc = ProcessTaskList();
726 // In cae of error or a stop-request leave loop
727 if (rc==kERROR || rc==kFALSE)
728 break;
729 }
730
731 // Reset to the default
732 if (!noreset)
733 {
734 fParList->SetReadyToSave(kFALSE);
735 fParList->ResetBit(MParList::kIsProcessing);
736 }
737
738 // If the number of passes==0 <default> propagete kCONTINUE
739 if (rc==kCONTINUE)
740 return fNumPasses==0 ? kCONTINUE : kTRUE;
741
742 // Return return code
743 return rc;
744}
745
746// --------------------------------------------------------------------------
747//
748// do post processing (before eventloop) of all tasks in the task-list
749// only tasks which have successfully been preprocessed are postprocessed.
750//
751Int_t MTaskList::PostProcess()
752{
753 *fLog << all << "Postprocessing... " << flush;
754 if (fDisplay)
755 {
756 // Set status lines
757 fDisplay->SetStatusLine1("PostProcessing...");
758 fDisplay->SetStatusLine2("");
759 }
760
761 //
762 // Make sure, that the ReadyToSave flag is not reset from a tasklist
763 // running as a task in another tasklist.
764 //
765 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
766 if (!noreset)
767 {
768 fParList->SetBit(MParList::kDoNotReset);
769 fParList->Reset();
770 }
771
772 //
773 // create the Iterator for the TaskList
774 //
775 TIter Next(fTasks);
776
777 MTask *task=NULL;
778
779 //
780 // loop over all tasks for postprocessing
781 // only tasks which have successfully been preprocessed are postprocessed.
782 //
783 while ( (task=(MTask*)Next()) )
784 {
785 if (!task->CallPostProcess())
786 return kFALSE;
787
788 // Handle GUI events (display changes, mouse clicks)
789 if (fDisplay)
790 gSystem->ProcessEvents();
791 }
792
793 *fLog << all << endl;
794
795 //
796 // Reset the ReadyToSave flag.
797 //
798 if (!noreset)
799 {
800 fParList->SetReadyToSave(kFALSE);
801 fParList->ResetBit(MParList::kDoNotReset);
802 }
803
804 return kTRUE;
805}
806
807// --------------------------------------------------------------------------
808//
809// Prints the number of times all the tasks in the list has been.
810// For convinience the lvl argument results in a number of spaces at the
811// beginning of the line. So that the structur of a tasklist can be
812// identified. If a Tasklist or task has filter applied the name of the
813// filter is printer in <>-brackets behind the number of executions.
814// Use MTaskList::PrintStatistics without an argument.
815//
816void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
817{
818 if (lvl==0)
819 {
820 *fLog << all << underline << "Process execution Statistics:" << endl;
821 *fLog << GetDescriptor();
822 if (GetFilter())
823 *fLog << " <" << GetFilter()->GetName() << ">";
824 if (title)
825 *fLog << "\t" << fTitle;
826 if (time>=0)
827 *fLog << Form(" %5.1f", GetCpuTime()/time*100) << "%";
828 else
829 *fLog << " 100.0%";
830 *fLog << endl;
831 }
832 else
833 MTask::PrintStatistics(lvl, title, time);
834
835 //
836 // create the Iterator for the TaskList
837 //
838 fTasks->R__FOR_EACH(MTask, PrintStatistics)(lvl+1, title, GetCpuTime());
839
840 if (lvl==0)
841 *fLog << endl;
842}
843
844// --------------------------------------------------------------------------
845//
846// Call 'Print()' of all tasks
847//
848void MTaskList::Print(Option_t *) const
849{
850 *fLog << all << underline << GetDescriptor() << ":" << endl;
851
852 fTasks->Print();
853
854 *fLog << endl;
855}
856
857// --------------------------------------------------------------------------
858//
859// Implementation of SavePrimitive. Used to write the call to a constructor
860// to a macro. In the original root implementation it is used to write
861// gui elements to a macro-file.
862//
863void MTaskList::StreamPrimitive(ostream &out) const
864{
865 out << " MTaskList " << GetUniqueName();
866 if (fName!=gsDefName || fTitle!=gsDefTitle)
867 {
868 out << "(\"" << fName << "\"";
869 if (fTitle!=gsDefTitle)
870 out << ", \"" << fTitle << "\"";
871 out <<")";
872 }
873 out << ";" << endl << endl;
874
875 MIter Next(fTasks);
876
877 MParContainer *cont = NULL;
878 while ((cont=Next()))
879 {
880 cont->SavePrimitive(out, "");
881 out << " " << GetUniqueName() << ".AddToList(&";
882 out << cont->GetUniqueName() << ");" << endl << endl;
883 }
884}
885
886void MTaskList::GetNames(TObjArray &arr) const
887{
888 MParContainer::GetNames(arr);
889 fTasks->R__FOR_EACH(MParContainer, GetNames)(arr);
890}
891
892void MTaskList::SetNames(TObjArray &arr)
893{
894 MParContainer::SetNames(arr);
895 fTasks->R__FOR_EACH(MParContainer, SetNames)(arr);
896}
897
898// --------------------------------------------------------------------------
899//
900// Read the contents/setup of a parameter container/task from a TEnv
901// instance (steering card/setup file).
902// The key to search for in the file should be of the syntax:
903// prefix.vname
904// While vname is a name which is specific for a single setup date
905// (variable) of this container and prefix is something like:
906// evtloopname.name
907// While name is the name of the containers/tasks in the parlist/tasklist
908//
909// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
910// Job4.MImgCleanStd.CleaningLevel2: 2.5
911//
912// If this cannot be found the next step is to search for
913// MImgCleanStd.CleaningLevel1: 3.0
914// And if this doesn't exist, too, we should search for:
915// CleaningLevel1: 3.0
916//
917// Warning: The programmer is responsible for the names to be unique in
918// all Mars classes.
919//
920Int_t MTaskList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
921{
922 if (print)
923 *fLog << all << "MTaskList::ReadEnv: " << prefix << " (" << (int)print << ")" << endl;
924
925 MParContainer *cont = NULL;
926
927 MIter Next(fTasks);
928 while ((cont=Next()))
929 {
930 if (cont->InheritsFrom("MTaskList"))
931 {
932 if (cont->ReadEnv(env, prefix, print)==kERROR)
933 return kERROR;
934 continue;
935 }
936
937 if (cont->TestEnv(env, prefix, print)==kERROR)
938 return kERROR;
939 }
940
941 return kTRUE;
942}
943
944// --------------------------------------------------------------------------
945//
946// Write the contents/setup of a parameter container/task to a TEnv
947// instance (steering card/setup file).
948// The key to search for in the file should be of the syntax:
949// prefix.vname
950// While vname is a name which is specific for a single setup date
951// (variable) of this container and prefix is something like:
952// evtloopname.name
953// While name is the name of the containers/tasks in the parlist/tasklist
954//
955// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
956// Job4.MImgCleanStd.CleaningLevel2: 2.5
957//
958// If this cannot be found the next step is to search for
959// MImgCleanStd.CleaningLevel1: 3.0
960// And if this doesn't exist, too, we should search for:
961// CleaningLevel1: 3.0
962//
963// Warning: The programmer is responsible for the names to be unique in
964// all Mars classes.
965//
966Bool_t MTaskList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
967{
968 MParContainer *cont = NULL;
969
970 MIter Next(fTasks);
971 while ((cont=Next()))
972 if (!cont->WriteEnv(env, prefix, print))
973 return kFALSE;
974 return kTRUE;
975}
976
977// --------------------------------------------------------------------------
978//
979// Removes a task from the tasklist. Returns kFALSE if the object was not
980// found in the list.
981//
982Bool_t MTaskList::RemoveFromList(MTask *task)
983{
984 TObject *obj = fTasks->Remove(task);
985
986 //
987 // If the task was found in the list try to remove it from the second
988 // list, too.
989 //
990 if (obj)
991 fTasksProcess.Remove(task);
992
993 return obj ? kTRUE : kFALSE;
994
995}
996
997// --------------------------------------------------------------------------
998//
999// Removes all task of the TList from the tasklist. Returns kFALSE if any
1000// of the objects was not an MTask or not found in the list.
1001//
1002Bool_t MTaskList::RemoveFromList(const TList &list)
1003{
1004 Bool_t rc = kTRUE;
1005
1006 TIter Next(&list);
1007 TObject *obj=0;
1008 while ((obj=Next()))
1009 {
1010 if (!obj->InheritsFrom(MTask::Class()))
1011 {
1012 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
1013 rc = kFALSE;
1014 continue;
1015 }
1016
1017 if (!RemoveFromList(static_cast<MTask*>(obj)))
1018 rc = kFALSE;
1019 }
1020 return rc;
1021}
1022
1023// --------------------------------------------------------------------------
1024//
1025// Find an object with the same name in the list and replace it with
1026// the new one. If the kIsOwner flag is set and the object was not
1027// created automatically, the object is deleted.
1028//
1029Bool_t MTaskList::Replace(MTask *task)
1030{
1031 //
1032 // check if the object (you want to add) exists
1033 //
1034 if (!task)
1035 return kFALSE;
1036
1037 if (task==this)
1038 {
1039 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
1040 *fLog << " would create infinite recursions...ignored." << endl;
1041 return kFALSE;
1042
1043 }
1044
1045 MTask *obj = (MTask*)FindObject(task->GetName());
1046 if (!obj)
1047 {
1048 *fLog << warn << "No object with the same name '";
1049 *fLog << task->GetName() << "' in list... adding." << endl;
1050 return AddToList(task);
1051 }
1052
1053 if (task==obj)
1054 return kTRUE;
1055
1056 *fLog << inf << "Replacing " << task->GetName() << " in " << GetName() << " for " << obj->GetStreamId() << "... " << flush;
1057 task->SetStreamId(obj->GetStreamId());
1058 task->SetBit(kMustCleanup);
1059 fTasks->AddAfter((TObject*)obj, task);
1060 *fLog << "Done." << endl;
1061
1062 RemoveFromList(obj);
1063
1064 if (TestBit(kIsOwner))
1065 delete obj;
1066
1067 //*fLog << inf << "MTask '" << task->GetName() << "' found and replaced..." << endl;
1068
1069 return kTRUE;
1070}
1071
1072// --------------------------------------------------------------------------
1073//
1074// Can be used to create an iterator over all tasks, eg:
1075// MTaskList tlist;
1076// TIter Next(tlist); // Be aware: Use a object here rather than a pointer!
1077// TObject *o=0;
1078// while ((o=Next()))
1079// {
1080// [...]
1081// }
1082//
1083MTaskList::operator TIterator*() const
1084{
1085 return new TListIter(fTasks);
1086}
Note: See TracBrowser for help on using the repository browser.