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

Last change on this file since 7710 was 7688, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 29.0 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)
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(MTaskList &ts)
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->ForEach(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->ForEach(MTask, SetDisplay)(d);
157 MTask::SetDisplay(d);
158}
159
160void MTaskList::SetAccelerator(Byte_t acc)
161{
162 fTasks->ForEach(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->ForEach(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 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
250 task->SetStreamId(type);
251 task->SetBit(kMustCleanup);
252 fTasks->AddBefore((TObject*)where, task);
253 *fLog << "Done." << endl;
254
255 return kTRUE;
256}
257
258// --------------------------------------------------------------------------
259//
260// schedule task for execution, after 'where'.
261// 'type' is the event type which should be processed
262//
263Bool_t MTaskList::AddToListAfter(MTask *task, const MTask *where, const char *type)
264{
265 if (task==this)
266 {
267 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
268 *fLog << " would create infinite recursions...ignored." << endl;
269 return kFALSE;
270
271 }
272
273 // FIXME: We agreed to put the task into list in an ordered way.
274 if (!CheckAddToList(task, type, where))
275 return kFALSE;
276
277 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
278 task->SetStreamId(type);
279 task->SetBit(kMustCleanup);
280 fTasks->AddAfter((TObject*)where, task);
281 *fLog << "Done." << endl;
282
283 return kTRUE;
284}
285
286// --------------------------------------------------------------------------
287//
288// schedule task for execution, 'type' is the event type which should
289// be processed
290//
291Bool_t MTaskList::AddToList(MTask *task, const char *type)
292{
293 if (task==this)
294 {
295 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
296 *fLog << " would create infinite recursions...ignored." << endl;
297 return kFALSE;
298
299 }
300
301 // FIXME: We agreed to put the task into list in an ordered way.
302 if (!CheckAddToList(task, type))
303 return kFALSE;
304
305 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
306 task->SetStreamId(type);
307 task->SetBit(kMustCleanup);
308 fTasks->Add(task);
309 *fLog << "Done." << endl;
310
311 return kTRUE;
312}
313
314// --------------------------------------------------------------------------
315//
316// Add all objects in list to the tasklist. If some of them do not
317// inherit from MTask return kFALSE, also if AddToList returns an error
318// for one of the tasks
319//
320Bool_t MTaskList::AddToList(const TList &list, const char *tType)
321{
322 TIter Next(&list);
323 TObject *obj=0;
324 while ((obj=Next()))
325 {
326 if (!obj->InheritsFrom(MTask::Class()))
327 {
328 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
329 return kFALSE;
330 }
331
332 if (!AddToList(static_cast<MTask*>(obj), tType))
333 return kFALSE;
334 }
335 return kTRUE;
336}
337
338// --------------------------------------------------------------------------
339//
340// Add all objects in list to the tasklist after task where. If some of
341// them do not inherit from MTask return kFALSE, also if AddToListAfter
342// returns an error for one of the tasks
343//
344Bool_t MTaskList::AddToListAfter(const TList &list, const MTask *where, const char *tType)
345{
346 TIter Next(&list);
347 TObject *obj=0;
348 while ((obj=Next()))
349 {
350 if (!obj->InheritsFrom(MTask::Class()))
351 {
352 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
353 return kFALSE;
354 }
355
356 if (!AddToListAfter(static_cast<MTask*>(obj), where, tType))
357 return kFALSE;
358
359 where = static_cast<MTask*>(obj);
360 }
361 return kTRUE;
362}
363
364// --------------------------------------------------------------------------
365//
366// Add all objects in list to the tasklist before task where. If some of
367// them do not inherit from MTask return kFALSE, also if AddToListBefore
368// returns an error for one of the tasks
369//
370Bool_t MTaskList::AddToListBefore(const TList &list, const MTask *where, const char *tType)
371{
372 TIter Next(&list);
373 TObject *obj=0;
374 while ((obj=Next()))
375 {
376 if (!obj->InheritsFrom(MTask::Class()))
377 {
378 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
379 return kFALSE;
380 }
381
382 if (!AddToListBefore(static_cast<MTask*>(obj), where, tType))
383 return kFALSE;
384 }
385 return kTRUE;
386}
387
388// --------------------------------------------------------------------------
389//
390// Find an object in the list.
391// 'name' is the name of the object you are searching for.
392//
393TObject *MTaskList::FindObject(const char *name) const
394{
395 return fTasks->FindObject(name);
396}
397
398// --------------------------------------------------------------------------
399//
400// check if the object is in the list or not
401//
402TObject *MTaskList::FindObject(const TObject *obj) const
403{
404 return fTasks->FindObject(obj);
405}
406
407// --------------------------------------------------------------------------
408//
409// find recursively a tasklist which contains a task with name task
410//
411MTaskList *MTaskList::FindTaskList(const char *task)
412{
413 if (FindObject(task))
414 return this;
415
416 TIter Next(fTasks);
417 TObject *o = 0;
418 while ((o=Next()))
419 {
420 MTaskList *l = dynamic_cast<MTaskList*>(o);
421 if (!l)
422 continue;
423
424 if (l->FindObject(task))
425 return l;
426 }
427 return 0;
428}
429
430// --------------------------------------------------------------------------
431//
432// find recursively a tasklist which contains a task task
433//
434MTaskList *MTaskList::FindTaskList(const MTask *task)
435{
436 if (FindObject(task))
437 return this;
438
439 TIter Next(fTasks);
440 TObject *o = 0;
441 while ((o=Next()))
442 {
443 MTaskList *l = dynamic_cast<MTaskList*>(o);
444 if (!l)
445 continue;
446
447 if (l->FindObject(task))
448 return l;
449 }
450
451 return 0;
452}
453
454// --------------------------------------------------------------------------
455//
456// removes a task from the list (used in PreProcess).
457// if kIsOwner is set the task is deleted. (see SetOwner())
458//
459void MTaskList::Remove(MTask *task)
460{
461 TObject *obj = fTasks->Remove(task);
462
463 if (TestBit(kIsOwner))
464 delete obj;
465}
466
467// --------------------------------------------------------------------------
468//
469// do pre processing (before eventloop) of all tasks in the task-list
470// Only if a task overwrites the Process function the task is
471// added to the fTaskProcess-List. This makes the execution of the
472// tasklist a little bit (only a little bit) faster, bacause tasks
473// doing no Processing are not Processed.
474//
475Int_t MTaskList::PreProcess(MParList *pList)
476{
477 *fLog << all << "Preprocessing... " << flush;
478 if (fDisplay)
479 {
480 // Set status lines
481 fDisplay->SetStatusLine1("PreProcessing...");
482 fDisplay->SetStatusLine2("");
483 }
484
485 fParList = pList;
486
487 //
488 // Make sure, that the ReadyToSave flag is not reset from a tasklist
489 // running as a task in another tasklist.
490 //
491 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
492 if (!noreset)
493 fParList->SetBit(MParList::kDoNotReset);
494
495 //
496 // create the Iterator over the tasklist
497 //
498 TIter Next(fTasks);
499
500 MTask *task=NULL;
501
502 //
503 // loop over all tasks for preproccesing
504 //
505 while ((task=(MTask*)Next()))
506 {
507 //
508 // PreProcess the task and check for it's return value.
509 //
510 switch (task->CallPreProcess(fParList))
511 {
512 case kFALSE:
513 return kFALSE;
514
515 case kTRUE:
516 // Handle GUI events (display changes, mouse clicks)
517 if (fDisplay)
518 gSystem->ProcessEvents();
519 continue;
520
521 case kSKIP:
522 Remove(task);
523 continue;
524 }
525
526 *fLog << err << dbginf << "PreProcess of " << task->GetDescriptor();
527 *fLog << " returned an unknown value... aborting." << endl;
528 return kFALSE;
529 }
530
531 *fLog << all << endl;
532
533 //
534 // Reset the ReadyToSave flag.
535 //
536 if (!noreset)
537 {
538 fParList->SetReadyToSave(kFALSE);
539 fParList->ResetBit(MParList::kDoNotReset);
540 }
541
542 //
543 // loop over all tasks to fill fTasksProcess
544 //
545 Next.Reset();
546 fTasksProcess.Clear();
547 while ((task=(MTask*)Next()))
548 if (task->IsA()==MTask::Class() || task->OverwritesProcess())
549 fTasksProcess.Add(task);
550
551 return kTRUE;
552}
553
554// --------------------------------------------------------------------------
555//
556// do reinit of all tasks in the task-list
557//
558Bool_t MTaskList::ReInit(MParList *pList)
559{
560 *fLog << all << "Reinit... " << flush;
561
562 if (!pList)
563 pList = fParList;
564
565 //
566 // Make sure, that the ReadyToSave flag is not reset from a tasklist
567 // running as a task in another tasklist.
568 //
569 const Bool_t noreset = pList->TestBit(MParList::kDoNotReset);
570 if (!noreset)
571 pList->SetBit(MParList::kDoNotReset);
572
573 //
574 // create the Iterator over the tasklist
575 //
576 TIter Next(fTasks);
577
578 MTask *task=NULL;
579
580 //
581 // loop over all tasks for reinitialization
582 //
583 while ((task=(MTask*)Next()))
584 {
585 *fLog << all << task->GetName() << "... " << flush;
586
587 if (!task->ReInit(pList/*?pList:fParList*/))
588 {
589 *fLog << err << "ERROR - ReInit of Task '" << task->GetDescriptor() << "' failed." << endl;
590 return kFALSE;
591 }
592 }
593
594 *fLog << all << endl;
595
596 //
597 // Reset the ReadyToSave flag.
598 //
599 if (!noreset)
600 {
601 pList->SetReadyToSave(kFALSE);
602 pList->ResetBit(MParList::kDoNotReset);
603 }
604
605 return kTRUE;
606}
607
608// --------------------------------------------------------------------------
609//
610// do the event execution of all tasks in the task-list
611//
612Int_t MTaskList::Process()
613{
614 //
615 // Check whether there is something which can be processed, otherwise
616 // stop the eventloop.
617 //
618 if (fTasksProcess.GetSize()==0)
619 {
620 *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
621 return kFALSE;
622 }
623
624 //
625 // Reset the ReadyToSave flag.
626 // Reset all containers.
627 //
628 // Make sure, that the parameter list is not reset from a tasklist
629 // running as a task in another tasklist.
630 //
631 const Bool_t noreset = fParList->TestBit(MParList::kIsProcessing);
632 if (!noreset)
633 {
634 fParList->SetBit(MParList::kIsProcessing);
635 if (!HasAccelerator(kAccDontReset))
636 fParList->Reset();
637 }
638
639 //
640 // create the Iterator for the TaskList
641 //
642 TIter Next(&fTasksProcess);
643 MTask *task=NULL;
644
645 //
646 // loop over all tasks for processing
647 //
648 Int_t rc = kTRUE;
649 while ( (task=(MTask*)Next()) )
650 {
651 //
652 // if the task has the wrong stream id skip it.
653 //
654 if (GetStreamId() != task->GetStreamId() &&
655 task->GetStreamId() != "All")
656 continue;
657
658 //
659 // if it has the right stream id execute the CallProcess() function
660 // and check what the result of it is.
661 // The CallProcess() function increases the execution counter and
662 // calls the Process() function dependent on the existance and
663 // return value of a filter.
664 //
665 switch (task->CallProcess())
666 {
667 case kTRUE:
668 //
669 // everything was OK: go on with the next task
670 //
671 continue;
672
673 case kFALSE:
674 //
675 // an error occured: stop eventloop
676 //
677 rc = kFALSE;
678 *fLog << inf << task->GetDescriptor() << " has stopped execution of " << GetDescriptor() << "." << endl;
679 break;
680
681 case kERROR:
682 //
683 // an error occured: stop eventloop and return: failed
684 //
685 *fLog << err << "Fatal error occured while Process() of " << task->GetDescriptor() << "... stopped." << endl;
686 rc = kERROR;
687 break;
688
689 case kCONTINUE:
690 //
691 // something occured: skip the rest of the tasks for this event
692 //
693 rc = kCONTINUE;
694 break;
695
696 default:
697 *fLog << warn << dbginf << "Unknown return value from MTask::Process()... ignored." << endl;
698 continue;
699 }
700 break;
701 }
702
703 if (!noreset)
704 {
705 fParList->SetReadyToSave(kFALSE);
706 fParList->ResetBit(MParList::kIsProcessing);
707 }
708
709 return rc;
710}
711
712// --------------------------------------------------------------------------
713//
714// do post processing (before eventloop) of all tasks in the task-list
715// only tasks which have successfully been preprocessed are postprocessed.
716//
717Int_t MTaskList::PostProcess()
718{
719 *fLog << all << "Postprocessing... " << flush;
720 if (fDisplay)
721 {
722 // Set status lines
723 fDisplay->SetStatusLine1("PostProcessing...");
724 fDisplay->SetStatusLine2("");
725 }
726
727 //
728 // Make sure, that the ReadyToSave flag is not reset from a tasklist
729 // running as a task in another tasklist.
730 //
731 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
732 if (!noreset)
733 {
734 fParList->SetBit(MParList::kDoNotReset);
735 fParList->Reset();
736 }
737
738 //
739 // create the Iterator for the TaskList
740 //
741 TIter Next(fTasks);
742
743 MTask *task=NULL;
744
745 //
746 // loop over all tasks for postprocessing
747 // only tasks which have successfully been preprocessed are postprocessed.
748 //
749 while ( (task=(MTask*)Next()) )
750 {
751 if (!task->CallPostProcess())
752 return kFALSE;
753
754 // Handle GUI events (display changes, mouse clicks)
755 if (fDisplay)
756 gSystem->ProcessEvents();
757 }
758
759 *fLog << all << endl;
760
761 //
762 // Reset the ReadyToSave flag.
763 //
764 if (!noreset)
765 {
766 fParList->SetReadyToSave(kFALSE);
767 fParList->ResetBit(MParList::kDoNotReset);
768 }
769
770 return kTRUE;
771}
772
773// --------------------------------------------------------------------------
774//
775// Prints the number of times all the tasks in the list has been.
776// For convinience the lvl argument results in a number of spaces at the
777// beginning of the line. So that the structur of a tasklist can be
778// identified. If a Tasklist or task has filter applied the name of the
779// filter is printer in <>-brackets behind the number of executions.
780// Use MTaskList::PrintStatistics without an argument.
781//
782void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
783{
784 if (lvl==0)
785 {
786 *fLog << all << underline << "Process execution Statistics:" << endl;
787 *fLog << GetDescriptor();
788 if (GetFilter())
789 *fLog << " <" << GetFilter()->GetName() << ">";
790 if (title)
791 *fLog << "\t" << fTitle;
792 if (time>=0)
793 *fLog << Form(" %5.1f", GetCpuTime()/time*100) << "%";
794 else
795 *fLog << " 100.0%";
796 *fLog << endl;
797 }
798 else
799 MTask::PrintStatistics(lvl, title, time);
800
801 //
802 // create the Iterator for the TaskList
803 //
804 fTasks->ForEach(MTask, PrintStatistics)(lvl+1, title, GetCpuTime());
805
806 if (lvl==0)
807 *fLog << endl;
808}
809
810// --------------------------------------------------------------------------
811//
812// Call 'Print()' of all tasks
813//
814void MTaskList::Print(Option_t *t) const
815{
816 *fLog << all << underline << GetDescriptor() << ":" << endl;
817
818 fTasks->Print();
819
820 *fLog << endl;
821}
822
823// --------------------------------------------------------------------------
824//
825// Implementation of SavePrimitive. Used to write the call to a constructor
826// to a macro. In the original root implementation it is used to write
827// gui elements to a macro-file.
828//
829void MTaskList::StreamPrimitive(ofstream &out) const
830{
831 out << " MTaskList " << GetUniqueName();
832 if (fName!=gsDefName || fTitle!=gsDefTitle)
833 {
834 out << "(\"" << fName << "\"";
835 if (fTitle!=gsDefTitle)
836 out << ", \"" << fTitle << "\"";
837 out <<")";
838 }
839 out << ";" << endl << endl;
840
841 MIter Next(fTasks);
842
843 MParContainer *cont = NULL;
844 while ((cont=Next()))
845 {
846 cont->SavePrimitive(out, "");
847 out << " " << GetUniqueName() << ".AddToList(&";
848 out << cont->GetUniqueName() << ");" << endl << endl;
849 }
850}
851
852void MTaskList::GetNames(TObjArray &arr) const
853{
854 MParContainer::GetNames(arr);
855 fTasks->ForEach(MParContainer, GetNames)(arr);
856}
857
858void MTaskList::SetNames(TObjArray &arr)
859{
860 MParContainer::SetNames(arr);
861 fTasks->ForEach(MParContainer, SetNames)(arr);
862}
863
864// --------------------------------------------------------------------------
865//
866// Read the contents/setup of a parameter container/task from a TEnv
867// instance (steering card/setup file).
868// The key to search for in the file should be of the syntax:
869// prefix.vname
870// While vname is a name which is specific for a single setup date
871// (variable) of this container and prefix is something like:
872// evtloopname.name
873// While name is the name of the containers/tasks in the parlist/tasklist
874//
875// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
876// Job4.MImgCleanStd.CleaningLevel2: 2.5
877//
878// If this cannot be found the next step is to search for
879// MImgCleanStd.CleaningLevel1: 3.0
880// And if this doesn't exist, too, we should search for:
881// CleaningLevel1: 3.0
882//
883// Warning: The programmer is responsible for the names to be unique in
884// all Mars classes.
885//
886Int_t MTaskList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
887{
888 if (print)
889 *fLog << all << "MTaskList::ReadEnv: " << prefix << " (" << (int)print << ")" << endl;
890
891 MParContainer *cont = NULL;
892
893 MIter Next(fTasks);
894 while ((cont=Next()))
895 {
896 if (cont->InheritsFrom("MTaskList"))
897 {
898 if (cont->ReadEnv(env, prefix, print)==kERROR)
899 return kERROR;
900 continue;
901 }
902
903 if (cont->TestEnv(env, prefix, print)==kERROR)
904 return kERROR;
905 }
906
907 return kTRUE;
908}
909
910// --------------------------------------------------------------------------
911//
912// Write the contents/setup of a parameter container/task to a TEnv
913// instance (steering card/setup file).
914// The key to search for in the file should be of the syntax:
915// prefix.vname
916// While vname is a name which is specific for a single setup date
917// (variable) of this container and prefix is something like:
918// evtloopname.name
919// While name is the name of the containers/tasks in the parlist/tasklist
920//
921// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
922// Job4.MImgCleanStd.CleaningLevel2: 2.5
923//
924// If this cannot be found the next step is to search for
925// MImgCleanStd.CleaningLevel1: 3.0
926// And if this doesn't exist, too, we should search for:
927// CleaningLevel1: 3.0
928//
929// Warning: The programmer is responsible for the names to be unique in
930// all Mars classes.
931//
932Bool_t MTaskList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
933{
934 MParContainer *cont = NULL;
935
936 MIter Next(fTasks);
937 while ((cont=Next()))
938 if (!cont->WriteEnv(env, prefix, print))
939 return kFALSE;
940 return kTRUE;
941}
942
943// --------------------------------------------------------------------------
944//
945// Removes a task from the tasklist. Returns kFALSE if the object was not
946// found in the list.
947//
948Bool_t MTaskList::RemoveFromList(MTask *task)
949{
950 TObject *obj = fTasks->Remove(task);
951
952 //
953 // If the task was found in the list try to remove it from the second
954 // list, too.
955 //
956 if (obj)
957 fTasksProcess.Remove(task);
958
959 return obj ? kTRUE : kFALSE;
960
961}
962
963// --------------------------------------------------------------------------
964//
965// Removes all task of the TList from the tasklist. Returns kFALSE if any
966// of the objects was not an MTask or not found in the list.
967//
968Bool_t MTaskList::RemoveFromList(const TList &list)
969{
970 Bool_t rc = kTRUE;
971
972 TIter Next(&list);
973 TObject *obj=0;
974 while ((obj=Next()))
975 {
976 if (!obj->InheritsFrom(MTask::Class()))
977 {
978 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
979 rc = kFALSE;
980 continue;
981 }
982
983 if (!RemoveFromList(static_cast<MTask*>(obj)))
984 rc = kFALSE;
985 }
986 return rc;
987}
988
989// --------------------------------------------------------------------------
990//
991// Find an object with the same name in the list and replace it with
992// the new one. If the kIsOwner flag is set and the object was not
993// created automatically, the object is deleted.
994//
995Bool_t MTaskList::Replace(MTask *task)
996{
997 //
998 // check if the object (you want to add) exists
999 //
1000 if (!task)
1001 return kFALSE;
1002
1003 if (task==this)
1004 {
1005 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
1006 *fLog << " would create infinite recursions...ignored." << endl;
1007 return kFALSE;
1008
1009 }
1010
1011 MTask *obj = (MTask*)FindObject(task->GetName());
1012 if (!obj)
1013 {
1014 *fLog << warn << "No object with the same name '";
1015 *fLog << task->GetName() << "' in list... adding." << endl;
1016 return AddToList(task);
1017 }
1018
1019 if (task==obj)
1020 return kTRUE;
1021
1022 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << obj->GetStreamId() << "... " << flush;
1023 task->SetStreamId(obj->GetStreamId());
1024 task->SetBit(kMustCleanup);
1025 fTasks->AddAfter((TObject*)obj, task);
1026 *fLog << "Done." << endl;
1027
1028 RemoveFromList(obj);
1029
1030 if (TestBit(kIsOwner))
1031 delete obj;
1032
1033 //*fLog << inf << "MTask '" << task->GetName() << "' found and replaced..." << endl;
1034
1035 return kTRUE;
1036}
1037
1038// --------------------------------------------------------------------------
1039//
1040// Can be used to create an iterator over all tasks, eg:
1041// MTaskList tlist;
1042// TIter Next(tlist); // Be aware: Use a object here rather than a pointer!
1043// TObject *o=0;
1044// while ((o=Next()))
1045// {
1046// [...]
1047// }
1048//
1049MTaskList::operator TIterator*() const
1050{
1051 return new TListIter(fTasks);
1052}
Note: See TracBrowser for help on using the repository browser.