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

Last change on this file since 2124 was 2120, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 22.0 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 MTask::SetLogStream(log);
137}
138
139void MTaskList::SetDisplay(MStatusDisplay *d)
140{
141 fTasks->ForEach(MTask, SetDisplay)(d);
142 MTask::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 << underline << "Execution Statistics:" << endl;
552 *fLog << GetDescriptor();
553 if (GetFilter())
554 *fLog << " <" << GetFilter()->GetName() << ">";
555 if (title)
556 *fLog << "\t" << fTitle;
557 *fLog << endl;
558 }
559 else
560 {
561 *fLog << setw(lvl) << " " << GetDescriptor();
562 if (title)
563 *fLog << "\t" << fTitle;
564 *fLog << endl;
565 }
566
567 //
568 // create the Iterator for the TaskList
569 //
570 fTasks->ForEach(MTask, PrintStatistics)(lvl+1, title);
571
572 if (lvl==0)
573 *fLog << endl;
574}
575
576
577// --------------------------------------------------------------------------
578void MTaskList::Print(Option_t *t) const
579{
580 *fLog << all << underline << GetDescriptor() << ":" << endl;
581
582 fTasks->Print();
583
584 *fLog << endl;
585}
586
587// --------------------------------------------------------------------------
588//
589// Implementation of SavePrimitive. Used to write the call to a constructor
590// to a macro. In the original root implementation it is used to write
591// gui elements to a macro-file.
592//
593void MTaskList::StreamPrimitive(ofstream &out) const
594{
595 out << " MTaskList " << GetUniqueName();
596 if (fName!=gsDefName || fTitle!=gsDefTitle)
597 {
598 out << "(\"" << fName << "\"";
599 if (fTitle!=gsDefTitle)
600 out << ", \"" << fTitle << "\"";
601 out <<")";
602 }
603 out << ";" << endl << endl;
604
605 MIter Next(fTasks);
606
607 MParContainer *cont = NULL;
608 while ((cont=Next()))
609 {
610 cont->SavePrimitive(out, "");
611 out << " " << GetUniqueName() << ".AddToList(&";
612 out << cont->GetUniqueName() << ");" << endl << endl;
613 }
614}
615
616void MTaskList::GetNames(TObjArray &arr) const
617{
618 MParContainer::GetNames(arr);
619 fTasks->ForEach(MParContainer, GetNames)(arr);
620}
621
622void MTaskList::SetNames(TObjArray &arr)
623{
624 MParContainer::SetNames(arr);
625 fTasks->ForEach(MParContainer, SetNames)(arr);
626}
627
628// --------------------------------------------------------------------------
629//
630// Read the contents/setup of a parameter container/task from a TEnv
631// instance (steering card/setup file).
632// The key to search for in the file should be of the syntax:
633// prefix.vname
634// While vname is a name which is specific for a single setup date
635// (variable) of this container and prefix is something like:
636// evtloopname.name
637// While name is the name of the containers/tasks in the parlist/tasklist
638//
639// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
640// Job4.MImgCleanStd.CleaningLevel2: 2.5
641//
642// If this cannot be found the next step is to search for
643// MImgCleanStd.CleaningLevel1: 3.0
644// And if this doesn't exist, too, we should search for:
645// CleaningLevel1: 3.0
646//
647// Warning: The programmer is responsible for the names to be unique in
648// all Mars classes.
649//
650Bool_t MTaskList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
651{
652 if (print)
653 *fLog << all << "MTaskList::ReadEnv: " << prefix << " (" << (int)print << ")" << endl;
654
655 MParContainer *cont = NULL;
656
657 MIter Next(fTasks);
658 while ((cont=Next()))
659 {
660 if (cont->InheritsFrom("MTaskList"))
661 {
662 if (cont->ReadEnv(env, prefix, print)==kERROR)
663 return kERROR;
664 continue;
665 }
666
667 // Check For: Job4.ContainerName.Varname
668 if (print)
669 *fLog << all << "Testing: " << prefix+cont->GetName() << endl;
670 Bool_t rc = cont->ReadEnv(env, prefix+cont->GetName(), print);
671 if (rc==kERROR)
672 return kERROR;
673 if (rc==kTRUE)
674 continue;
675
676 // Check For: Job4.MClassName.Varname
677 if (print)
678 *fLog << all << "Testing: " << prefix+cont->ClassName() << endl;
679 rc = cont->ReadEnv(env, prefix+cont->ClassName(), print);
680 if (rc==kERROR)
681 return kERROR;
682 if (rc==kTRUE)
683 continue;
684
685 // Check For: ContainerName.Varname
686 if (print)
687 *fLog << all << "Testing: " << cont->GetName() << endl;
688 rc = cont->ReadEnv(env, cont->GetName(), print);
689 if (rc==kERROR)
690 return kERROR;
691 if (rc==kTRUE)
692 continue;
693
694 // Check For: MClassName.Varname
695 if (print)
696 *fLog << all << "Testing: " << cont->ClassName() << endl;
697 rc = cont->ReadEnv(env, cont->ClassName(), print);
698 if (rc==kERROR)
699 return kERROR;
700 if (rc==kTRUE)
701 continue;
702 }
703
704 return kTRUE;
705}
706
707// --------------------------------------------------------------------------
708//
709// Write the contents/setup of a parameter container/task to a TEnv
710// instance (steering card/setup file).
711// The key to search for in the file should be of the syntax:
712// prefix.vname
713// While vname is a name which is specific for a single setup date
714// (variable) of this container and prefix is something like:
715// evtloopname.name
716// While name is the name of the containers/tasks in the parlist/tasklist
717//
718// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
719// Job4.MImgCleanStd.CleaningLevel2: 2.5
720//
721// If this cannot be found the next step is to search for
722// MImgCleanStd.CleaningLevel1: 3.0
723// And if this doesn't exist, too, we should search for:
724// CleaningLevel1: 3.0
725//
726// Warning: The programmer is responsible for the names to be unique in
727// all Mars classes.
728//
729Bool_t MTaskList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
730{
731 MParContainer *cont = NULL;
732
733 MIter Next(fTasks);
734 while ((cont=Next()))
735 if (!cont->WriteEnv(env, prefix, print))
736 return kFALSE;
737 return kTRUE;
738}
739
740// --------------------------------------------------------------------------
741//
742// Removes a task from the tasklist. Returns kFALSE if the object was not
743// found in the list.
744//
745Bool_t MTaskList::RemoveFromList(MTask *task)
746{
747 TObject *obj = fTasks->Remove(task);
748
749 //
750 // If the task was found in the list try to remove it from the second
751 // list, too.
752 //
753 if (obj)
754 fTasksProcess.Remove(task);
755
756 return obj ? kTRUE : kFALSE;
757
758}
Note: See TracBrowser for help on using the repository browser.