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

Last change on this file since 4667 was 4601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 22.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 // FIXME: We agreed to put the task into list in an ordered way.
232 if (!CheckAddToList(task, type, where))
233 return kFALSE;
234
235 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
236 task->SetStreamId(type);
237 task->SetBit(kMustCleanup);
238 fTasks->AddBefore((TObject*)where, task);
239 *fLog << "Done." << endl;
240
241 return kTRUE;
242}
243
244// --------------------------------------------------------------------------
245//
246// schedule task for execution, after 'where'.
247// 'type' is the event type which should be processed
248//
249Bool_t MTaskList::AddToListAfter(MTask *task, const MTask *where, const char *type)
250{
251 // FIXME: We agreed to put the task into list in an ordered way.
252
253 if (!CheckAddToList(task, type, where))
254 return kFALSE;
255
256 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
257 task->SetStreamId(type);
258 task->SetBit(kMustCleanup);
259 fTasks->AddAfter((TObject*)where, task);
260 *fLog << "Done." << endl;
261
262 return kTRUE;
263}
264
265// --------------------------------------------------------------------------
266//
267// schedule task for execution, 'type' is the event type which should
268// be processed
269//
270Bool_t MTaskList::AddToList(MTask *task, const char *type)
271{
272 // FIXME: We agreed to put the task into list in an ordered way.
273
274 if (!CheckAddToList(task, type))
275 return kFALSE;
276
277 *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
278 task->SetStreamId(type);
279 task->SetBit(kMustCleanup);
280 fTasks->Add(task);
281 *fLog << "Done." << endl;
282
283 return kTRUE;
284}
285
286// --------------------------------------------------------------------------
287//
288// Find an object in the list.
289// 'name' is the name of the object you are searching for.
290//
291TObject *MTaskList::FindObject(const char *name) const
292{
293 return fTasks->FindObject(name);
294}
295
296// --------------------------------------------------------------------------
297//
298// check if the object is in the list or not
299//
300TObject *MTaskList::FindObject(const TObject *obj) const
301{
302 return fTasks->FindObject(obj);
303}
304
305// --------------------------------------------------------------------------
306//
307// removes a task from the list (used in PreProcess).
308// if kIsOwner is set the task is deleted. (see SetOwner())
309//
310void MTaskList::Remove(MTask *task)
311{
312 TObject *obj = fTasks->Remove(task);
313
314 if (TestBit(kIsOwner))
315 delete obj;
316}
317
318// --------------------------------------------------------------------------
319//
320// do pre processing (before eventloop) of all tasks in the task-list
321// Only if a task overwrites the Process function the task is
322// added to the fTaskProcess-List. This makes the execution of the
323// tasklist a little bit (only a little bit) faster, bacause tasks
324// doing no Processing are not Processed.
325//
326Int_t MTaskList::PreProcess(MParList *pList)
327{
328 *fLog << all << "Preprocessing... " << flush;
329 if (fDisplay)
330 {
331 // Set status lines
332 fDisplay->SetStatusLine1("PreProcessing...");
333 fDisplay->SetStatusLine2("");
334 }
335
336 fParList = pList;
337
338 //
339 // Make sure, that the ReadyToSave flag is not reset from a tasklist
340 // running as a task in another tasklist.
341 //
342 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
343 if (!noreset)
344 fParList->SetBit(MParList::kDoNotReset);
345
346 //
347 // create the Iterator over the tasklist
348 //
349 TIter Next(fTasks);
350
351 MTask *task=NULL;
352
353 //
354 // loop over all tasks for preproccesing
355 //
356 while ((task=(MTask*)Next()))
357 {
358 //
359 // PreProcess the task and check for it's return value.
360 //
361 switch (task->CallPreProcess(fParList))
362 {
363 case kFALSE:
364 return kFALSE;
365
366 case kTRUE:
367 // Handle GUI events (display changes, mouse clicks)
368 if (fDisplay)
369 gSystem->ProcessEvents();
370 continue;
371
372 case kSKIP:
373 Remove(task);
374 continue;
375 }
376
377 *fLog << err << dbginf << "PreProcess of " << task->GetDescriptor();
378 *fLog << " returned an unknown value... aborting." << endl;
379 return kFALSE;
380 }
381
382 *fLog << all << endl;
383
384 //
385 // Reset the ReadyToSave flag.
386 //
387 if (!noreset)
388 {
389 fParList->SetReadyToSave(kFALSE);
390 fParList->ResetBit(MParList::kDoNotReset);
391 }
392
393 //
394 // loop over all tasks to fill fTasksProcess
395 //
396 Next.Reset();
397 fTasksProcess.Clear();
398 while ((task=(MTask*)Next()))
399 if (task->IsA()==MTask::Class() || task->OverwritesProcess())
400 fTasksProcess.Add(task);
401
402 return kTRUE;
403}
404
405// --------------------------------------------------------------------------
406//
407// do reinit of all tasks in the task-list
408//
409Bool_t MTaskList::ReInit(MParList *pList)
410{
411 *fLog << all << "Reinit... " << flush;
412
413 if (!pList)
414 pList = fParList;
415
416 //
417 // Make sure, that the ReadyToSave flag is not reset from a tasklist
418 // running as a task in another tasklist.
419 //
420 const Bool_t noreset = pList->TestBit(MParList::kDoNotReset);
421 if (!noreset)
422 pList->SetBit(MParList::kDoNotReset);
423
424 //
425 // create the Iterator over the tasklist
426 //
427 TIter Next(fTasks);
428
429 MTask *task=NULL;
430
431 //
432 // loop over all tasks for reinitialization
433 //
434 while ((task=(MTask*)Next()))
435 {
436 *fLog << all << task->GetName() << "... " << flush;
437
438 if (!task->ReInit(pList/*?pList:fParList*/))
439 {
440 *fLog << err << "ERROR - ReInit of Task '" << task->GetDescriptor() << "' failed." << endl;
441 return kFALSE;
442 }
443 }
444
445 *fLog << all << endl;
446
447 //
448 // Reset the ReadyToSave flag.
449 //
450 if (!noreset)
451 {
452 pList->SetReadyToSave(kFALSE);
453 pList->ResetBit(MParList::kDoNotReset);
454 }
455
456 return kTRUE;
457}
458
459// --------------------------------------------------------------------------
460//
461// do the event execution of all tasks in the task-list
462//
463Int_t MTaskList::Process()
464{
465 //
466 // Check whether there is something which can be processed, otherwise
467 // stop the eventloop.
468 //
469 if (fTasksProcess.GetSize()==0)
470 {
471 *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
472 return kFALSE;
473 }
474
475 //
476 // Reset the ReadyToSave flag.
477 // Reset all containers.
478 //
479 // Make sure, that the parameter list is not reset from a tasklist
480 // running as a task in another tasklist.
481 //
482 const Bool_t noreset = fParList->TestBit(MParList::kIsProcessing);
483 if (!noreset)
484 {
485 fParList->SetBit(MParList::kIsProcessing);
486 fParList->Reset();
487 }
488
489 //
490 // create the Iterator for the TaskList
491 //
492 TIter Next(&fTasksProcess);
493 MTask *task=NULL;
494
495 //
496 // loop over all tasks for processing
497 //
498 Bool_t rc = kTRUE;
499 while ( (task=(MTask*)Next()) )
500 {
501 //
502 // if the task has the wrong stream id skip it.
503 //
504 if (GetStreamId() != task->GetStreamId() &&
505 task->GetStreamId() != "All")
506 continue;
507
508 //
509 // if it has the right stream id execute the CallProcess() function
510 // and check what the result of it is.
511 // The CallProcess() function increases the execution counter and
512 // calls the Process() function dependent on the existance and
513 // return value of a filter.
514 //
515 switch (task->CallProcess())
516 {
517 case kTRUE:
518 //
519 // everything was OK: go on with the next task
520 //
521 continue;
522
523 case kFALSE:
524 //
525 // an error occured: stop eventloop
526 //
527 rc = kFALSE;
528 *fLog << inf << task->GetDescriptor() << " has stopped execution of " << GetDescriptor() << "." << endl;
529 break;
530
531 case kERROR:
532 //
533 // an error occured: stop eventloop and return: failed
534 //
535 *fLog << err << dbginf << "Fatal error occured... stopped." << endl;
536 rc = kERROR;
537 break;
538
539 case kCONTINUE:
540 //
541 // something occured: skip the rest of the tasks for this event
542 //
543 rc = kTRUE;
544 break;
545
546 default:
547 *fLog << warn << dbginf << "Unknown return value from MTask::Process()... ignored." << endl;
548 continue;
549 }
550 break;
551 }
552
553 if (!noreset)
554 {
555 fParList->SetReadyToSave(kFALSE);
556 fParList->ResetBit(MParList::kIsProcessing);
557 }
558
559 return rc;
560}
561
562// --------------------------------------------------------------------------
563//
564// do post processing (before eventloop) of all tasks in the task-list
565// only tasks which have successfully been preprocessed are postprocessed.
566//
567Int_t MTaskList::PostProcess()
568{
569 *fLog << all << "Postprocessing... " << flush;
570 if (fDisplay)
571 {
572 // Set status lines
573 fDisplay->SetStatusLine1("PostProcessing...");
574 fDisplay->SetStatusLine2("");
575 }
576
577 //
578 // Make sure, that the ReadyToSave flag is not reset from a tasklist
579 // running as a task in another tasklist.
580 //
581 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
582 if (!noreset)
583 {
584 fParList->SetBit(MParList::kDoNotReset);
585 fParList->Reset();
586 }
587
588 //
589 // create the Iterator for the TaskList
590 //
591 TIter Next(fTasks);
592
593 MTask *task=NULL;
594
595 //
596 // loop over all tasks for postprocessing
597 // only tasks which have successfully been preprocessed are postprocessed.
598 //
599 while ( (task=(MTask*)Next()) )
600 {
601 if (!task->CallPostProcess())
602 return kFALSE;
603
604 // Handle GUI events (display changes, mouse clicks)
605 if (fDisplay)
606 gSystem->ProcessEvents();
607 }
608
609 *fLog << all << endl;
610
611 //
612 // Reset the ReadyToSave flag.
613 //
614 if (!noreset)
615 {
616 fParList->SetReadyToSave(kFALSE);
617 fParList->ResetBit(MParList::kDoNotReset);
618 }
619
620 return kTRUE;
621}
622
623// --------------------------------------------------------------------------
624//
625// Prints the number of times all the tasks in the list has been.
626// For convinience the lvl argument results in a number of spaces at the
627// beginning of the line. So that the structur of a tasklist can be
628// identified. If a Tasklist or task has filter applied the name of the
629// filter is printer in <>-brackets behind the number of executions.
630// Use MTaskList::PrintStatistics without an argument.
631//
632void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
633{
634 if (lvl==0)
635 {
636 *fLog << all << underline << "Process execution Statistics:" << endl;
637 *fLog << GetDescriptor();
638 if (GetFilter())
639 *fLog << " <" << GetFilter()->GetName() << ">";
640 if (title)
641 *fLog << "\t" << fTitle;
642 if (time>=0)
643 *fLog << Form(" %5.1f", GetCpuTime()/time*100) << "%";
644 else
645 *fLog << " 100.0%";
646 *fLog << endl;
647 }
648 else
649 MTask::PrintStatistics(lvl, title, time);
650
651 //
652 // create the Iterator for the TaskList
653 //
654 fTasks->ForEach(MTask, PrintStatistics)(lvl+1, title, GetCpuTime());
655
656 if (lvl==0)
657 *fLog << endl;
658}
659
660// --------------------------------------------------------------------------
661//
662// Call 'Print()' of all tasks
663//
664void MTaskList::Print(Option_t *t) const
665{
666 *fLog << all << underline << GetDescriptor() << ":" << endl;
667
668 fTasks->Print();
669
670 *fLog << endl;
671}
672
673// --------------------------------------------------------------------------
674//
675// Implementation of SavePrimitive. Used to write the call to a constructor
676// to a macro. In the original root implementation it is used to write
677// gui elements to a macro-file.
678//
679void MTaskList::StreamPrimitive(ofstream &out) const
680{
681 out << " MTaskList " << GetUniqueName();
682 if (fName!=gsDefName || fTitle!=gsDefTitle)
683 {
684 out << "(\"" << fName << "\"";
685 if (fTitle!=gsDefTitle)
686 out << ", \"" << fTitle << "\"";
687 out <<")";
688 }
689 out << ";" << endl << endl;
690
691 MIter Next(fTasks);
692
693 MParContainer *cont = NULL;
694 while ((cont=Next()))
695 {
696 cont->SavePrimitive(out, "");
697 out << " " << GetUniqueName() << ".AddToList(&";
698 out << cont->GetUniqueName() << ");" << endl << endl;
699 }
700}
701
702void MTaskList::GetNames(TObjArray &arr) const
703{
704 MParContainer::GetNames(arr);
705 fTasks->ForEach(MParContainer, GetNames)(arr);
706}
707
708void MTaskList::SetNames(TObjArray &arr)
709{
710 MParContainer::SetNames(arr);
711 fTasks->ForEach(MParContainer, SetNames)(arr);
712}
713
714// --------------------------------------------------------------------------
715//
716// Read the contents/setup of a parameter container/task from a TEnv
717// instance (steering card/setup file).
718// The key to search for in the file should be of the syntax:
719// prefix.vname
720// While vname is a name which is specific for a single setup date
721// (variable) of this container and prefix is something like:
722// evtloopname.name
723// While name is the name of the containers/tasks in the parlist/tasklist
724//
725// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
726// Job4.MImgCleanStd.CleaningLevel2: 2.5
727//
728// If this cannot be found the next step is to search for
729// MImgCleanStd.CleaningLevel1: 3.0
730// And if this doesn't exist, too, we should search for:
731// CleaningLevel1: 3.0
732//
733// Warning: The programmer is responsible for the names to be unique in
734// all Mars classes.
735//
736Int_t MTaskList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
737{
738 if (print)
739 *fLog << all << "MTaskList::ReadEnv: " << prefix << " (" << (int)print << ")" << endl;
740
741 MParContainer *cont = NULL;
742
743 MIter Next(fTasks);
744 while ((cont=Next()))
745 {
746 if (cont->InheritsFrom("MTaskList"))
747 {
748 if (cont->ReadEnv(env, prefix, print)==kERROR)
749 return kERROR;
750 continue;
751 }
752
753 if (cont->TestEnv(env, prefix, print)==kERROR)
754 return kERROR;
755 }
756
757 return kTRUE;
758}
759
760// --------------------------------------------------------------------------
761//
762// Write the contents/setup of a parameter container/task to a TEnv
763// instance (steering card/setup file).
764// The key to search for in the file should be of the syntax:
765// prefix.vname
766// While vname is a name which is specific for a single setup date
767// (variable) of this container and prefix is something like:
768// evtloopname.name
769// While name is the name of the containers/tasks in the parlist/tasklist
770//
771// eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
772// Job4.MImgCleanStd.CleaningLevel2: 2.5
773//
774// If this cannot be found the next step is to search for
775// MImgCleanStd.CleaningLevel1: 3.0
776// And if this doesn't exist, too, we should search for:
777// CleaningLevel1: 3.0
778//
779// Warning: The programmer is responsible for the names to be unique in
780// all Mars classes.
781//
782Bool_t MTaskList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
783{
784 MParContainer *cont = NULL;
785
786 MIter Next(fTasks);
787 while ((cont=Next()))
788 if (!cont->WriteEnv(env, prefix, print))
789 return kFALSE;
790 return kTRUE;
791}
792
793// --------------------------------------------------------------------------
794//
795// Removes a task from the tasklist. Returns kFALSE if the object was not
796// found in the list.
797//
798Bool_t MTaskList::RemoveFromList(MTask *task)
799{
800 TObject *obj = fTasks->Remove(task);
801
802 //
803 // If the task was found in the list try to remove it from the second
804 // list, too.
805 //
806 if (obj)
807 fTasksProcess.Remove(task);
808
809 return obj ? kTRUE : kFALSE;
810
811}
Note: See TracBrowser for help on using the repository browser.