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

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