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

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