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

Last change on this file since 8339 was 7808, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 29.2 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) : MTask()
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
614// --------------------------------------------------------------------------
615//
616// do the event execution of all tasks in the task-list
617//
618Int_t MTaskList::Process()
619{
620 //
621 // Check whether there is something which can be processed, otherwise
622 // stop the eventloop.
623 //
624 if (fTasksProcess.GetSize()==0)
625 {
626 *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
627 return kFALSE;
628 }
629
630 //
631 // Reset the ReadyToSave flag.
632 // Reset all containers.
633 //
634 // Make sure, that the parameter list is not reset from a tasklist
635 // running as a task in another tasklist.
636 //
637 const Bool_t noreset = fParList->TestBit(MParList::kIsProcessing);
638 if (!noreset)
639 {
640 fParList->SetBit(MParList::kIsProcessing);
641 if (!HasAccelerator(kAccDontReset))
642 fParList->Reset();
643 }
644
645 //
646 // create the Iterator for the TaskList
647 //
648 TIter Next(&fTasksProcess);
649 MTask *task=NULL;
650
651 //
652 // loop over all tasks for processing
653 //
654 Int_t rc = kTRUE;
655 while ( (task=(MTask*)Next()) )
656 {
657 //
658 // if the task has the wrong stream id skip it.
659 //
660 if (GetStreamId() != task->GetStreamId() &&
661 task->GetStreamId() != "All")
662 continue;
663
664 //
665 // if it has the right stream id execute the CallProcess() function
666 // and check what the result of it is.
667 // The CallProcess() function increases the execution counter and
668 // calls the Process() function dependent on the existance and
669 // return value of a filter.
670 //
671 switch (task->CallProcess())
672 {
673 case kTRUE:
674 //
675 // everything was OK: go on with the next task
676 //
677 continue;
678
679 case kFALSE:
680 //
681 // an error occured: stop eventloop
682 //
683 rc = kFALSE;
684 *fLog << inf << task->GetDescriptor() << " has stopped execution of " << GetDescriptor() << "." << endl;
685 break;
686
687 case kERROR:
688 //
689 // an error occured: stop eventloop and return: failed
690 //
691 *fLog << err << "Fatal error occured while Process() of " << task->GetDescriptor() << "... stopped." << endl;
692 rc = kERROR;
693 break;
694
695 case kCONTINUE:
696 //
697 // something occured: skip the rest of the tasks for this event
698 //
699 rc = kCONTINUE;
700 break;
701
702 default:
703 *fLog << warn << dbginf << "Unknown return value from MTask::Process()... ignored." << endl;
704 continue;
705 }
706 break;
707 }
708
709 if (!noreset)
710 {
711 fParList->SetReadyToSave(kFALSE);
712 fParList->ResetBit(MParList::kIsProcessing);
713 }
714
715 return rc;
716}
717
718// --------------------------------------------------------------------------
719//
720// do post processing (before eventloop) of all tasks in the task-list
721// only tasks which have successfully been preprocessed are postprocessed.
722//
723Int_t MTaskList::PostProcess()
724{
725 *fLog << all << "Postprocessing... " << flush;
726 if (fDisplay)
727 {
728 // Set status lines
729 fDisplay->SetStatusLine1("PostProcessing...");
730 fDisplay->SetStatusLine2("");
731 }
732
733 //
734 // Make sure, that the ReadyToSave flag is not reset from a tasklist
735 // running as a task in another tasklist.
736 //
737 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
738 if (!noreset)
739 {
740 fParList->SetBit(MParList::kDoNotReset);
741 fParList->Reset();
742 }
743
744 //
745 // create the Iterator for the TaskList
746 //
747 TIter Next(fTasks);
748
749 MTask *task=NULL;
750
751 //
752 // loop over all tasks for postprocessing
753 // only tasks which have successfully been preprocessed are postprocessed.
754 //
755 while ( (task=(MTask*)Next()) )
756 {
757 if (!task->CallPostProcess())
758 return kFALSE;
759
760 // Handle GUI events (display changes, mouse clicks)
761 if (fDisplay)
762 gSystem->ProcessEvents();
763 }
764
765 *fLog << all << endl;
766
767 //
768 // Reset the ReadyToSave flag.
769 //
770 if (!noreset)
771 {
772 fParList->SetReadyToSave(kFALSE);
773 fParList->ResetBit(MParList::kDoNotReset);
774 }
775
776 return kTRUE;
777}
778
779// --------------------------------------------------------------------------
780//
781// Prints the number of times all the tasks in the list has been.
782// For convinience the lvl argument results in a number of spaces at the
783// beginning of the line. So that the structur of a tasklist can be
784// identified. If a Tasklist or task has filter applied the name of the
785// filter is printer in <>-brackets behind the number of executions.
786// Use MTaskList::PrintStatistics without an argument.
787//
788void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
789{
790 if (lvl==0)
791 {
792 *fLog << all << underline << "Process execution Statistics:" << endl;
793 *fLog << GetDescriptor();
794 if (GetFilter())
795 *fLog << " <" << GetFilter()->GetName() << ">";
796 if (title)
797 *fLog << "\t" << fTitle;
798 if (time>=0)
799 *fLog << Form(" %5.1f", GetCpuTime()/time*100) << "%";
800 else
801 *fLog << " 100.0%";
802 *fLog << endl;
803 }
804 else
805 MTask::PrintStatistics(lvl, title, time);
806
807 //
808 // create the Iterator for the TaskList
809 //
810 fTasks->R__FOR_EACH(MTask, PrintStatistics)(lvl+1, title, GetCpuTime());
811
812 if (lvl==0)
813 *fLog << endl;
814}
815
816// --------------------------------------------------------------------------
817//
818// Call 'Print()' of all tasks
819//
820void MTaskList::Print(Option_t *) const
821{
822 *fLog << all << underline << GetDescriptor() << ":" << endl;
823
824 fTasks->Print();
825
826 *fLog << endl;
827}
828
829// --------------------------------------------------------------------------
830//
831// Implementation of SavePrimitive. Used to write the call to a constructor
832// to a macro. In the original root implementation it is used to write
833// gui elements to a macro-file.
834//
835void MTaskList::StreamPrimitive(ostream &out) const
836{
837 out << " MTaskList " << GetUniqueName();
838 if (fName!=gsDefName || fTitle!=gsDefTitle)
839 {
840 out << "(\"" << fName << "\"";
841 if (fTitle!=gsDefTitle)
842 out << ", \"" << fTitle << "\"";
843 out <<")";
844 }
845 out << ";" << endl << endl;
846
847 MIter Next(fTasks);
848
849 MParContainer *cont = NULL;
850 while ((cont=Next()))
851 {
852 cont->SavePrimitive(out, "");
853 out << " " << GetUniqueName() << ".AddToList(&";
854 out << cont->GetUniqueName() << ");" << endl << endl;
855 }
856}
857
858void MTaskList::GetNames(TObjArray &arr) const
859{
860 MParContainer::GetNames(arr);
861 fTasks->R__FOR_EACH(MParContainer, GetNames)(arr);
862}
863
864void MTaskList::SetNames(TObjArray &arr)
865{
866 MParContainer::SetNames(arr);
867 fTasks->R__FOR_EACH(MParContainer, SetNames)(arr);
868}
869
870// --------------------------------------------------------------------------
871//
872// Read the contents/setup of a parameter container/task from a TEnv
873// instance (steering card/setup file).
874// The key to search for in the file should be of the syntax:
875// prefix.vname
876// While vname is a name which is specific for a single setup date
877// (variable) of this container and prefix is something like:
878// evtloopname.name
879// While name is the name of the containers/tasks in the parlist/tasklist
880//
881// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
882// Job4.MImgCleanStd.CleaningLevel2: 2.5
883//
884// If this cannot be found the next step is to search for
885// MImgCleanStd.CleaningLevel1: 3.0
886// And if this doesn't exist, too, we should search for:
887// CleaningLevel1: 3.0
888//
889// Warning: The programmer is responsible for the names to be unique in
890// all Mars classes.
891//
892Int_t MTaskList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
893{
894 if (print)
895 *fLog << all << "MTaskList::ReadEnv: " << prefix << " (" << (int)print << ")" << endl;
896
897 MParContainer *cont = NULL;
898
899 MIter Next(fTasks);
900 while ((cont=Next()))
901 {
902 if (cont->InheritsFrom("MTaskList"))
903 {
904 if (cont->ReadEnv(env, prefix, print)==kERROR)
905 return kERROR;
906 continue;
907 }
908
909 if (cont->TestEnv(env, prefix, print)==kERROR)
910 return kERROR;
911 }
912
913 return kTRUE;
914}
915
916// --------------------------------------------------------------------------
917//
918// Write the contents/setup of a parameter container/task to a TEnv
919// instance (steering card/setup file).
920// The key to search for in the file should be of the syntax:
921// prefix.vname
922// While vname is a name which is specific for a single setup date
923// (variable) of this container and prefix is something like:
924// evtloopname.name
925// While name is the name of the containers/tasks in the parlist/tasklist
926//
927// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
928// Job4.MImgCleanStd.CleaningLevel2: 2.5
929//
930// If this cannot be found the next step is to search for
931// MImgCleanStd.CleaningLevel1: 3.0
932// And if this doesn't exist, too, we should search for:
933// CleaningLevel1: 3.0
934//
935// Warning: The programmer is responsible for the names to be unique in
936// all Mars classes.
937//
938Bool_t MTaskList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
939{
940 MParContainer *cont = NULL;
941
942 MIter Next(fTasks);
943 while ((cont=Next()))
944 if (!cont->WriteEnv(env, prefix, print))
945 return kFALSE;
946 return kTRUE;
947}
948
949// --------------------------------------------------------------------------
950//
951// Removes a task from the tasklist. Returns kFALSE if the object was not
952// found in the list.
953//
954Bool_t MTaskList::RemoveFromList(MTask *task)
955{
956 TObject *obj = fTasks->Remove(task);
957
958 //
959 // If the task was found in the list try to remove it from the second
960 // list, too.
961 //
962 if (obj)
963 fTasksProcess.Remove(task);
964
965 return obj ? kTRUE : kFALSE;
966
967}
968
969// --------------------------------------------------------------------------
970//
971// Removes all task of the TList from the tasklist. Returns kFALSE if any
972// of the objects was not an MTask or not found in the list.
973//
974Bool_t MTaskList::RemoveFromList(const TList &list)
975{
976 Bool_t rc = kTRUE;
977
978 TIter Next(&list);
979 TObject *obj=0;
980 while ((obj=Next()))
981 {
982 if (!obj->InheritsFrom(MTask::Class()))
983 {
984 *fLog << err << "ERROR - Object " << obj->GetName() << " doesn't inherit from MTask..." << endl;
985 rc = kFALSE;
986 continue;
987 }
988
989 if (!RemoveFromList(static_cast<MTask*>(obj)))
990 rc = kFALSE;
991 }
992 return rc;
993}
994
995// --------------------------------------------------------------------------
996//
997// Find an object with the same name in the list and replace it with
998// the new one. If the kIsOwner flag is set and the object was not
999// created automatically, the object is deleted.
1000//
1001Bool_t MTaskList::Replace(MTask *task)
1002{
1003 //
1004 // check if the object (you want to add) exists
1005 //
1006 if (!task)
1007 return kFALSE;
1008
1009 if (task==this)
1010 {
1011 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
1012 *fLog << " would create infinite recursions...ignored." << endl;
1013 return kFALSE;
1014
1015 }
1016
1017 MTask *obj = (MTask*)FindObject(task->GetName());
1018 if (!obj)
1019 {
1020 *fLog << warn << "No object with the same name '";
1021 *fLog << task->GetName() << "' in list... adding." << endl;
1022 return AddToList(task);
1023 }
1024
1025 if (task==obj)
1026 return kTRUE;
1027
1028 *fLog << inf << "Replacing " << task->GetName() << " in " << GetName() << " for " << obj->GetStreamId() << "... " << flush;
1029 task->SetStreamId(obj->GetStreamId());
1030 task->SetBit(kMustCleanup);
1031 fTasks->AddAfter((TObject*)obj, task);
1032 *fLog << "Done." << endl;
1033
1034 RemoveFromList(obj);
1035
1036 if (TestBit(kIsOwner))
1037 delete obj;
1038
1039 //*fLog << inf << "MTask '" << task->GetName() << "' found and replaced..." << endl;
1040
1041 return kTRUE;
1042}
1043
1044// --------------------------------------------------------------------------
1045//
1046// Can be used to create an iterator over all tasks, eg:
1047// MTaskList tlist;
1048// TIter Next(tlist); // Be aware: Use a object here rather than a pointer!
1049// TObject *o=0;
1050// while ((o=Next()))
1051// {
1052// [...]
1053// }
1054//
1055MTaskList::operator TIterator*() const
1056{
1057 return new TListIter(fTasks);
1058}
Note: See TracBrowser for help on using the repository browser.