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

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