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

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