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

Last change on this file since 7051 was 5779, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 25.5 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@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
160// --------------------------------------------------------------------------
161//
162// Set the serial number for the all tasks in the list and the tasklist
163// itself.
164//
165void MTaskList::SetSerialNumber(Byte_t num)
166{
167 fTasks->ForEach(MTask, SetSerialNumber)(num);
168 MTask::SetSerialNumber(num);
169}
170
171Bool_t MTaskList::CheckAddToList(MTask *task, const char *type, const MTask *where) const
172{
173 //
174 // Sanity check
175 //
176 if (!task)
177 {
178 *fLog << err << "ERROR - task argument=NULL." << endl;
179 return kFALSE;
180 }
181
182 //
183 // Get Name of new task
184 //
185 const char *name = task->GetName();
186
187 //
188 // Check if the new task is already existing in the list
189 //
190 const TObject *objn = fTasks->FindObject(name);
191 const TObject *objt = fTasks->FindObject(task);
192
193 if (objn || objt)
194 {
195 //
196 // If the task is already in the list ignore it.
197 //
198 if (objt || objn==task)
199 {
200 *fLog << warn << dbginf << "Warning: Task '" << task->GetName() << ", 0x" << (void*)task;
201 *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
202 return kTRUE;
203 }
204
205 //
206 // Otherwise add it to the list, but print a warning message
207 //
208 *fLog << warn << dbginf << "Warning: Task '" << task->GetName();
209 *fLog << "' already existing in '" << GetName() << "'." << endl;
210 *fLog << "You may not be able to get a pointer to this task by name." << endl;
211 }
212
213 if (!where)
214 return kTRUE;
215
216 if (fTasks->FindObject(where))
217 return kTRUE;
218
219 *fLog << err << dbginf << "Error: Cannot find task after which the new task should be scheduled!" << endl;
220 return kFALSE;
221}
222
223
224// --------------------------------------------------------------------------
225//
226// schedule task for execution, before 'where'.
227// 'type' is the event type which should be processed
228//
229Bool_t MTaskList::AddToListBefore(MTask *task, const MTask *where, const char *type)
230{
231 if (task==this)
232 {
233 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
234 *fLog << " would create infinite recursions...ignored." << endl;
235 return kFALSE;
236
237 }
238
239 // FIXME: We agreed to put the task into list in an ordered way.
240 if (!CheckAddToList(task, type, where))
241 return kFALSE;
242
243 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
244 task->SetStreamId(type);
245 task->SetBit(kMustCleanup);
246 fTasks->AddBefore((TObject*)where, task);
247 *fLog << "Done." << endl;
248
249 return kTRUE;
250}
251
252// --------------------------------------------------------------------------
253//
254// schedule task for execution, after 'where'.
255// 'type' is the event type which should be processed
256//
257Bool_t MTaskList::AddToListAfter(MTask *task, const MTask *where, const char *type)
258{
259 if (task==this)
260 {
261 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
262 *fLog << " would create infinite recursions...ignored." << endl;
263 return kFALSE;
264
265 }
266
267 // FIXME: We agreed to put the task into list in an ordered way.
268 if (!CheckAddToList(task, type, where))
269 return kFALSE;
270
271 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
272 task->SetStreamId(type);
273 task->SetBit(kMustCleanup);
274 fTasks->AddAfter((TObject*)where, task);
275 *fLog << "Done." << endl;
276
277 return kTRUE;
278}
279
280// --------------------------------------------------------------------------
281//
282// schedule task for execution, 'type' is the event type which should
283// be processed
284//
285Bool_t MTaskList::AddToList(MTask *task, const char *type)
286{
287 if (task==this)
288 {
289 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
290 *fLog << " would create infinite recursions...ignored." << endl;
291 return kFALSE;
292
293 }
294
295 // FIXME: We agreed to put the task into list in an ordered way.
296 if (!CheckAddToList(task, type))
297 return kFALSE;
298
299 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
300 task->SetStreamId(type);
301 task->SetBit(kMustCleanup);
302 fTasks->Add(task);
303 *fLog << "Done." << endl;
304
305 return kTRUE;
306}
307
308// --------------------------------------------------------------------------
309//
310// Find an object in the list.
311// 'name' is the name of the object you are searching for.
312//
313TObject *MTaskList::FindObject(const char *name) const
314{
315 return fTasks->FindObject(name);
316}
317
318// --------------------------------------------------------------------------
319//
320// check if the object is in the list or not
321//
322TObject *MTaskList::FindObject(const TObject *obj) const
323{
324 return fTasks->FindObject(obj);
325}
326
327// --------------------------------------------------------------------------
328//
329// find recursively a tasklist which contains a task with name task
330//
331MTaskList *MTaskList::FindTaskList(const char *task)
332{
333 if (FindObject(task))
334 return this;
335
336 TIter Next(fTasks);
337 TObject *o = 0;
338 while ((o=Next()))
339 {
340 MTaskList *l = dynamic_cast<MTaskList*>(o);
341 if (!l)
342 continue;
343
344 if (l->FindObject(task))
345 return l;
346 }
347 return 0;
348}
349
350// --------------------------------------------------------------------------
351//
352// find recursively a tasklist which contains a task task
353//
354MTaskList *MTaskList::FindTaskList(const MTask *task)
355{
356 if (FindObject(task))
357 return this;
358
359 TIter Next(fTasks);
360 TObject *o = 0;
361 while ((o=Next()))
362 {
363 MTaskList *l = dynamic_cast<MTaskList*>(o);
364 if (!l)
365 continue;
366
367 if (l->FindObject(task))
368 return l;
369 }
370
371 return 0;
372}
373
374// --------------------------------------------------------------------------
375//
376// removes a task from the list (used in PreProcess).
377// if kIsOwner is set the task is deleted. (see SetOwner())
378//
379void MTaskList::Remove(MTask *task)
380{
381 TObject *obj = fTasks->Remove(task);
382
383 if (TestBit(kIsOwner))
384 delete obj;
385}
386
387// --------------------------------------------------------------------------
388//
389// do pre processing (before eventloop) of all tasks in the task-list
390// Only if a task overwrites the Process function the task is
391// added to the fTaskProcess-List. This makes the execution of the
392// tasklist a little bit (only a little bit) faster, bacause tasks
393// doing no Processing are not Processed.
394//
395Int_t MTaskList::PreProcess(MParList *pList)
396{
397 *fLog << all << "Preprocessing... " << flush;
398 if (fDisplay)
399 {
400 // Set status lines
401 fDisplay->SetStatusLine1("PreProcessing...");
402 fDisplay->SetStatusLine2("");
403 }
404
405 fParList = pList;
406
407 //
408 // Make sure, that the ReadyToSave flag is not reset from a tasklist
409 // running as a task in another tasklist.
410 //
411 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
412 if (!noreset)
413 fParList->SetBit(MParList::kDoNotReset);
414
415 //
416 // create the Iterator over the tasklist
417 //
418 TIter Next(fTasks);
419
420 MTask *task=NULL;
421
422 //
423 // loop over all tasks for preproccesing
424 //
425 while ((task=(MTask*)Next()))
426 {
427 //
428 // PreProcess the task and check for it's return value.
429 //
430 switch (task->CallPreProcess(fParList))
431 {
432 case kFALSE:
433 return kFALSE;
434
435 case kTRUE:
436 // Handle GUI events (display changes, mouse clicks)
437 if (fDisplay)
438 gSystem->ProcessEvents();
439 continue;
440
441 case kSKIP:
442 Remove(task);
443 continue;
444 }
445
446 *fLog << err << dbginf << "PreProcess of " << task->GetDescriptor();
447 *fLog << " returned an unknown value... aborting." << endl;
448 return kFALSE;
449 }
450
451 *fLog << all << endl;
452
453 //
454 // Reset the ReadyToSave flag.
455 //
456 if (!noreset)
457 {
458 fParList->SetReadyToSave(kFALSE);
459 fParList->ResetBit(MParList::kDoNotReset);
460 }
461
462 //
463 // loop over all tasks to fill fTasksProcess
464 //
465 Next.Reset();
466 fTasksProcess.Clear();
467 while ((task=(MTask*)Next()))
468 if (task->IsA()==MTask::Class() || task->OverwritesProcess())
469 fTasksProcess.Add(task);
470
471 return kTRUE;
472}
473
474// --------------------------------------------------------------------------
475//
476// do reinit of all tasks in the task-list
477//
478Bool_t MTaskList::ReInit(MParList *pList)
479{
480 *fLog << all << "Reinit... " << flush;
481
482 if (!pList)
483 pList = fParList;
484
485 //
486 // Make sure, that the ReadyToSave flag is not reset from a tasklist
487 // running as a task in another tasklist.
488 //
489 const Bool_t noreset = pList->TestBit(MParList::kDoNotReset);
490 if (!noreset)
491 pList->SetBit(MParList::kDoNotReset);
492
493 //
494 // create the Iterator over the tasklist
495 //
496 TIter Next(fTasks);
497
498 MTask *task=NULL;
499
500 //
501 // loop over all tasks for reinitialization
502 //
503 while ((task=(MTask*)Next()))
504 {
505 *fLog << all << task->GetName() << "... " << flush;
506
507 if (!task->ReInit(pList/*?pList:fParList*/))
508 {
509 *fLog << err << "ERROR - ReInit of Task '" << task->GetDescriptor() << "' failed." << endl;
510 return kFALSE;
511 }
512 }
513
514 *fLog << all << endl;
515
516 //
517 // Reset the ReadyToSave flag.
518 //
519 if (!noreset)
520 {
521 pList->SetReadyToSave(kFALSE);
522 pList->ResetBit(MParList::kDoNotReset);
523 }
524
525 return kTRUE;
526}
527
528// --------------------------------------------------------------------------
529//
530// do the event execution of all tasks in the task-list
531//
532Int_t MTaskList::Process()
533{
534 //
535 // Check whether there is something which can be processed, otherwise
536 // stop the eventloop.
537 //
538 if (fTasksProcess.GetSize()==0)
539 {
540 *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
541 return kFALSE;
542 }
543
544 //
545 // Reset the ReadyToSave flag.
546 // Reset all containers.
547 //
548 // Make sure, that the parameter list is not reset from a tasklist
549 // running as a task in another tasklist.
550 //
551 const Bool_t noreset = fParList->TestBit(MParList::kIsProcessing);
552 if (!noreset)
553 {
554 fParList->SetBit(MParList::kIsProcessing);
555 fParList->Reset();
556 }
557
558 //
559 // create the Iterator for the TaskList
560 //
561 TIter Next(&fTasksProcess);
562 MTask *task=NULL;
563
564 //
565 // loop over all tasks for processing
566 //
567 Int_t rc = kTRUE;
568 while ( (task=(MTask*)Next()) )
569 {
570 //
571 // if the task has the wrong stream id skip it.
572 //
573 if (GetStreamId() != task->GetStreamId() &&
574 task->GetStreamId() != "All")
575 continue;
576
577 //
578 // if it has the right stream id execute the CallProcess() function
579 // and check what the result of it is.
580 // The CallProcess() function increases the execution counter and
581 // calls the Process() function dependent on the existance and
582 // return value of a filter.
583 //
584 switch (task->CallProcess())
585 {
586 case kTRUE:
587 //
588 // everything was OK: go on with the next task
589 //
590 continue;
591
592 case kFALSE:
593 //
594 // an error occured: stop eventloop
595 //
596 rc = kFALSE;
597 *fLog << inf << task->GetDescriptor() << " has stopped execution of " << GetDescriptor() << "." << endl;
598 break;
599
600 case kERROR:
601 //
602 // an error occured: stop eventloop and return: failed
603 //
604 *fLog << err << "Fatal error occured while Process() of " << task->GetDescriptor() << "... stopped." << endl;
605 rc = kERROR;
606 break;
607
608 case kCONTINUE:
609 //
610 // something occured: skip the rest of the tasks for this event
611 //
612 rc = kCONTINUE;
613 break;
614
615 default:
616 *fLog << warn << dbginf << "Unknown return value from MTask::Process()... ignored." << endl;
617 continue;
618 }
619 break;
620 }
621
622 if (!noreset)
623 {
624 fParList->SetReadyToSave(kFALSE);
625 fParList->ResetBit(MParList::kIsProcessing);
626 }
627
628 return rc;
629}
630
631// --------------------------------------------------------------------------
632//
633// do post processing (before eventloop) of all tasks in the task-list
634// only tasks which have successfully been preprocessed are postprocessed.
635//
636Int_t MTaskList::PostProcess()
637{
638 *fLog << all << "Postprocessing... " << flush;
639 if (fDisplay)
640 {
641 // Set status lines
642 fDisplay->SetStatusLine1("PostProcessing...");
643 fDisplay->SetStatusLine2("");
644 }
645
646 //
647 // Make sure, that the ReadyToSave flag is not reset from a tasklist
648 // running as a task in another tasklist.
649 //
650 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
651 if (!noreset)
652 {
653 fParList->SetBit(MParList::kDoNotReset);
654 fParList->Reset();
655 }
656
657 //
658 // create the Iterator for the TaskList
659 //
660 TIter Next(fTasks);
661
662 MTask *task=NULL;
663
664 //
665 // loop over all tasks for postprocessing
666 // only tasks which have successfully been preprocessed are postprocessed.
667 //
668 while ( (task=(MTask*)Next()) )
669 {
670 if (!task->CallPostProcess())
671 return kFALSE;
672
673 // Handle GUI events (display changes, mouse clicks)
674 if (fDisplay)
675 gSystem->ProcessEvents();
676 }
677
678 *fLog << all << endl;
679
680 //
681 // Reset the ReadyToSave flag.
682 //
683 if (!noreset)
684 {
685 fParList->SetReadyToSave(kFALSE);
686 fParList->ResetBit(MParList::kDoNotReset);
687 }
688
689 return kTRUE;
690}
691
692// --------------------------------------------------------------------------
693//
694// Prints the number of times all the tasks in the list has been.
695// For convinience the lvl argument results in a number of spaces at the
696// beginning of the line. So that the structur of a tasklist can be
697// identified. If a Tasklist or task has filter applied the name of the
698// filter is printer in <>-brackets behind the number of executions.
699// Use MTaskList::PrintStatistics without an argument.
700//
701void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
702{
703 if (lvl==0)
704 {
705 *fLog << all << underline << "Process execution Statistics:" << endl;
706 *fLog << GetDescriptor();
707 if (GetFilter())
708 *fLog << " <" << GetFilter()->GetName() << ">";
709 if (title)
710 *fLog << "\t" << fTitle;
711 if (time>=0)
712 *fLog << Form(" %5.1f", GetCpuTime()/time*100) << "%";
713 else
714 *fLog << " 100.0%";
715 *fLog << endl;
716 }
717 else
718 MTask::PrintStatistics(lvl, title, time);
719
720 //
721 // create the Iterator for the TaskList
722 //
723 fTasks->ForEach(MTask, PrintStatistics)(lvl+1, title, GetCpuTime());
724
725 if (lvl==0)
726 *fLog << endl;
727}
728
729// --------------------------------------------------------------------------
730//
731// Call 'Print()' of all tasks
732//
733void MTaskList::Print(Option_t *t) const
734{
735 *fLog << all << underline << GetDescriptor() << ":" << endl;
736
737 fTasks->Print();
738
739 *fLog << endl;
740}
741
742// --------------------------------------------------------------------------
743//
744// Implementation of SavePrimitive. Used to write the call to a constructor
745// to a macro. In the original root implementation it is used to write
746// gui elements to a macro-file.
747//
748void MTaskList::StreamPrimitive(ofstream &out) const
749{
750 out << " MTaskList " << GetUniqueName();
751 if (fName!=gsDefName || fTitle!=gsDefTitle)
752 {
753 out << "(\"" << fName << "\"";
754 if (fTitle!=gsDefTitle)
755 out << ", \"" << fTitle << "\"";
756 out <<")";
757 }
758 out << ";" << endl << endl;
759
760 MIter Next(fTasks);
761
762 MParContainer *cont = NULL;
763 while ((cont=Next()))
764 {
765 cont->SavePrimitive(out, "");
766 out << " " << GetUniqueName() << ".AddToList(&";
767 out << cont->GetUniqueName() << ");" << endl << endl;
768 }
769}
770
771void MTaskList::GetNames(TObjArray &arr) const
772{
773 MParContainer::GetNames(arr);
774 fTasks->ForEach(MParContainer, GetNames)(arr);
775}
776
777void MTaskList::SetNames(TObjArray &arr)
778{
779 MParContainer::SetNames(arr);
780 fTasks->ForEach(MParContainer, SetNames)(arr);
781}
782
783// --------------------------------------------------------------------------
784//
785// Read the contents/setup of a parameter container/task from a TEnv
786// instance (steering card/setup file).
787// The key to search for in the file should be of the syntax:
788// prefix.vname
789// While vname is a name which is specific for a single setup date
790// (variable) of this container and prefix is something like:
791// evtloopname.name
792// While name is the name of the containers/tasks in the parlist/tasklist
793//
794// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
795// Job4.MImgCleanStd.CleaningLevel2: 2.5
796//
797// If this cannot be found the next step is to search for
798// MImgCleanStd.CleaningLevel1: 3.0
799// And if this doesn't exist, too, we should search for:
800// CleaningLevel1: 3.0
801//
802// Warning: The programmer is responsible for the names to be unique in
803// all Mars classes.
804//
805Int_t MTaskList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
806{
807 if (print)
808 *fLog << all << "MTaskList::ReadEnv: " << prefix << " (" << (int)print << ")" << endl;
809
810 MParContainer *cont = NULL;
811
812 MIter Next(fTasks);
813 while ((cont=Next()))
814 {
815 if (cont->InheritsFrom("MTaskList"))
816 {
817 if (cont->ReadEnv(env, prefix, print)==kERROR)
818 return kERROR;
819 continue;
820 }
821
822 if (cont->TestEnv(env, prefix, print)==kERROR)
823 return kERROR;
824 }
825
826 return kTRUE;
827}
828
829// --------------------------------------------------------------------------
830//
831// Write the contents/setup of a parameter container/task to a TEnv
832// instance (steering card/setup file).
833// The key to search for in the file should be of the syntax:
834// prefix.vname
835// While vname is a name which is specific for a single setup date
836// (variable) of this container and prefix is something like:
837// evtloopname.name
838// While name is the name of the containers/tasks in the parlist/tasklist
839//
840// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
841// Job4.MImgCleanStd.CleaningLevel2: 2.5
842//
843// If this cannot be found the next step is to search for
844// MImgCleanStd.CleaningLevel1: 3.0
845// And if this doesn't exist, too, we should search for:
846// CleaningLevel1: 3.0
847//
848// Warning: The programmer is responsible for the names to be unique in
849// all Mars classes.
850//
851Bool_t MTaskList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
852{
853 MParContainer *cont = NULL;
854
855 MIter Next(fTasks);
856 while ((cont=Next()))
857 if (!cont->WriteEnv(env, prefix, print))
858 return kFALSE;
859 return kTRUE;
860}
861
862// --------------------------------------------------------------------------
863//
864// Removes a task from the tasklist. Returns kFALSE if the object was not
865// found in the list.
866//
867Bool_t MTaskList::RemoveFromList(MTask *task)
868{
869 TObject *obj = fTasks->Remove(task);
870
871 //
872 // If the task was found in the list try to remove it from the second
873 // list, too.
874 //
875 if (obj)
876 fTasksProcess.Remove(task);
877
878 return obj ? kTRUE : kFALSE;
879
880}
881
882// --------------------------------------------------------------------------
883//
884// Find an object with the same name in the list and replace it with
885// the new one. If the kIsOwner flag is set and the object was not
886// created automatically, the object is deleted.
887//
888Bool_t MTaskList::Replace(MTask *task)
889{
890 //
891 // check if the object (you want to add) exists
892 //
893 if (!task)
894 return kFALSE;
895
896 if (task==this)
897 {
898 *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
899 *fLog << " would create infinite recursions...ignored." << endl;
900 return kFALSE;
901
902 }
903
904 MTask *obj = (MTask*)FindObject(task->GetName());
905 if (!obj)
906 {
907 *fLog << warn << "No object with the same name '";
908 *fLog << task->GetName() << "' in list... adding." << endl;
909 return AddToList(task);
910 }
911
912 if (task==obj)
913 return kTRUE;
914
915 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << obj->GetStreamId() << "... " << flush;
916 task->SetStreamId(obj->GetStreamId());
917 task->SetBit(kMustCleanup);
918 fTasks->AddAfter((TObject*)obj, task);
919 *fLog << "Done." << endl;
920
921 RemoveFromList(obj);
922
923 if (TestBit(kIsOwner))
924 delete obj;
925
926 //*fLog << inf << "MTask '" << task->GetName() << "' found and replaced..." << endl;
927
928 return kTRUE;
929}
Note: See TracBrowser for help on using the repository browser.