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

Last change on this file since 1527 was 1527, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 16.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@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 <TBaseClass.h>
63#include <TOrdCollection.h>
64
65#include "MLog.h"
66#include "MLogManip.h"
67
68#include "MParList.h"
69#include "MInputStreamID.h"
70
71ClassImp(MTaskList);
72
73static const TString gsDefName = "MTaskList";
74static const TString gsDefTitle = "A list for tasks to be executed";
75// --------------------------------------------------------------------------
76//
77// the name for the task list must be the same for all task lists
78// because the task list (at the moment) is identified by exactly
79// this name in the parameter list (by MEvtLoop::SetParList)
80//
81MTaskList::MTaskList(const char *name, const char *title)
82{
83 fName = name ? name : gsDefName.Data();
84 fTitle = title ? title : gsDefTitle.Data();
85
86 fTasks = new TList;
87}
88
89// --------------------------------------------------------------------------
90//
91// CopyConstructor
92// creates a new TaskList and put the contents of an existing
93// TaskList in the new TaskList.
94//
95MTaskList::MTaskList(MTaskList &ts)
96{
97 fTasks->AddAll(ts.fTasks);
98}
99
100// --------------------------------------------------------------------------
101//
102// If the 'IsOwner' bit is set (via SetOwner()) all tasks are deleted
103// by the destructor
104//
105MTaskList::~MTaskList()
106{
107 if (TestBit(kIsOwner))
108 fTasks->SetOwner();
109
110 delete fTasks;
111}
112
113// --------------------------------------------------------------------------
114//
115// If the 'IsOwner' bit is set (via SetOwner()) all containers are deleted
116// by the destructor
117//
118void MTaskList::SetOwner(Bool_t enable)
119{
120 enable ? SetBit(kIsOwner) : ResetBit(kIsOwner);
121}
122
123
124// --------------------------------------------------------------------------
125//
126// Set the logging stream for the all tasks in the list and the tasklist
127// itself.
128//
129void MTaskList::SetLogStream(MLog *log)
130{
131 fTasks->ForEach(MTask, SetLogStream)(log);
132 MParContainer::SetLogStream(log);
133}
134
135
136// --------------------------------------------------------------------------
137//
138// schedule task for execution, whether as first task, or after
139// 'where'. 'tType' is the event type which should be processed
140//
141Bool_t MTaskList::AddToList(MTask *task, const char *type, MTask *where)
142{
143 // FIXME: We agreed to put the task into list in an ordered way.
144
145 //
146 // Sanity check
147 //
148 if (!task)
149 return kFALSE;
150
151 //
152 // Get Name of new task
153 //
154 const char *name = task->GetName();
155
156 //
157 // Check if the new task is already existing in the list
158 //
159 const TObject *objn = fTasks->FindObject(name);
160 const TObject *objt = fTasks->FindObject(task);
161
162 if (objn || objt)
163 {
164 //
165 // If the task is already in the list ignore it.
166 //
167 if (objt || objn==task)
168 {
169 *fLog << warn << dbginf << "Warning: Task '" << task->GetName() << ", 0x" << (void*)task;
170 *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
171 return kTRUE;
172 }
173
174 //
175 // Otherwise add it to the list, but print a warning message
176 //
177 *fLog << warn << dbginf << "Warning: Task '" << task->GetName();
178 *fLog << "' already existing in '" << GetName() << "'." << endl;
179 *fLog << "You may not be able to get a pointer to this task by name." << endl;
180 }
181
182 if (where)
183 {
184 if (!fTasks->FindObject(where))
185 {
186 *fLog << err << dbginf << "Error: Cannot find task after which the new task should be scheduled!" << endl;
187 return kFALSE;
188 }
189 }
190
191 *fLog << inf << "Adding " << name << " to " << GetName() << " for " << type << "... " << flush;
192
193 task->SetStreamId(type);
194 fTasks->Add(task);
195
196 *fLog << "Done." << endl;
197
198 return kTRUE;
199}
200
201// --------------------------------------------------------------------------
202//
203// Find an object in the list.
204// 'name' is the name of the object you are searching for.
205//
206TObject *MTaskList::FindObject(const char *name) const
207{
208 return fTasks->FindObject(name);
209}
210
211// --------------------------------------------------------------------------
212//
213// check if the object is in the list or not
214//
215TObject *MTaskList::FindObject(const TObject *obj) const
216{
217 return fTasks->FindObject(obj);
218}
219
220// --------------------------------------------------------------------------
221//
222// do reinit of all tasks in the task-list
223//
224Bool_t MTaskList::ReInit(MParList *pList)
225{
226 *fLog << all << "Reinit... " << flush;
227
228 //
229 // create the Iterator over the tasklist
230 //
231 TIter Next(fTasks);
232
233 MTask *task=NULL;
234 //
235 // loop over all tasks for preproccesing
236 //
237 while ((task=(MTask*)Next()))
238 {
239 *fLog << all << task->GetName() << "... " << flush;
240
241 if (!task->ReInit(pList?pList:fParList))
242 {
243 *fLog << err << "ERROR - ReInit if Task " << task->GetDescriptor() << " failed." << endl;
244 return kFALSE;
245 }
246 }
247
248 *fLog << all << endl;
249
250 return kTRUE;
251}
252
253// --------------------------------------------------------------------------
254//
255// removes a task from the list (used in PreProcess).
256// if kIsOwner is set the task is deleted. (see SetOwner())
257//
258void MTaskList::Remove(MTask *task)
259{
260 TObject *obj = fTasks->Remove(task);
261
262 if (TestBit(kIsOwner))
263 delete obj;
264}
265
266// --------------------------------------------------------------------------
267//
268// Check whether this task (or one of it's base classes) overloads
269// MTask::Process. Only if this function is overloaded this task is
270// added to the fTaskProcess-List. This makes the execution of the
271// tasklist a little bit (only a little bit) faster, bacause tasks
272// doing no Processing are not Processed.
273//
274Bool_t MTaskList::CheckClassForProcess(TClass *cls)
275{
276 //
277 // Check whether the class itself overloads the Process function
278 //
279 if (cls->GetName()=="MTask")
280 return kFALSE;
281
282 if (cls->GetMethodAny("Process"))
283 return kTRUE;
284
285 //
286 // If the class itself doesn't overload it check all it's base classes
287 //
288 TBaseClass *base=NULL;
289 TIter NextBase(cls->GetListOfBases());
290 while ((base=(TBaseClass*)NextBase()))
291 {
292 if (CheckClassForProcess(base->GetClassPointer()))
293 return kTRUE;
294 }
295
296 return kFALSE;
297}
298
299// --------------------------------------------------------------------------
300//
301// do pre processing (before eventloop) of all tasks in the task-list
302//
303Bool_t MTaskList::PreProcess(MParList *pList)
304{
305 *fLog << all << "Preprocessing... " << flush;
306
307 fParList = pList;
308
309 fTasksProcess.Clear();
310
311 //
312 // create the Iterator over the tasklist
313 //
314 TIter Next(fTasks);
315
316 MTask *task=NULL;
317
318 //
319 // loop over all tasks for preproccesing
320 //
321 while ((task=(MTask*)Next()))
322 {
323 *fLog << all << task->GetName() << "... " << flush;
324
325 if (CheckClassForProcess(task->IsA()))
326 fTasksProcess.Add(task);
327
328 //
329 // PreProcess the task and check for it's return value.
330 //
331 switch (task->CallPreProcess(fParList))
332 {
333 case kFALSE:
334 return kFALSE;
335
336 case kTRUE:
337 continue;
338
339 case kSKIP:
340 Remove(task);
341 continue;
342 }
343
344 *fLog << err << dbginf << "PreProcess of " << task->GetDescriptor();
345 *fLog << " returned an unknown value... aborting." << endl;
346 return kFALSE;
347 }
348
349 *fLog << all << endl;
350
351 return kTRUE;
352}
353
354// --------------------------------------------------------------------------
355//
356// do the event execution of all tasks in the task-list
357//
358Bool_t MTaskList::Process()
359{
360 //
361 // Check whether there is something which can be processed, otherwise
362 // stop the eventloop.
363 //
364 if (fTasksProcess.GetSize()==0)
365 {
366 *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
367 return kFALSE;
368 }
369
370 //
371 // Reset the ReadyToSave flag.
372 // Reset all containers.
373 //
374 // Make sure, that the parameter list is not reset from a tasklist
375 // running as a task in another tasklist.
376 //
377 const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
378 if (!noreset)
379 {
380 fParList->SetReadyToSave();
381 fParList->Reset();
382 fParList->SetBit(MParList::kDoNotReset);
383 }
384
385 //
386 // create the Iterator for the TaskList
387 //
388 TIter Next(&fTasksProcess);
389 MTask *task=NULL;
390
391 //
392 // loop over all tasks for processing
393 //
394 Bool_t rc = kTRUE;
395 while ( (task=(MTask*)Next()) )
396 {
397 //
398 // if the task has the wrong stream id skip it.
399 //
400 if (GetStreamId() != task->GetStreamId() &&
401 task->GetStreamId() != "All")
402 continue;
403
404 //
405 // if it has the right stream id execute the CallProcess() function
406 // and check what the result of it is.
407 // The CallProcess() function increases the execution counter and
408 // calls the Process() function dependent on the existance and
409 // return value of a filter.
410 //
411 switch (task->CallProcess())
412 {
413 case kTRUE:
414 //
415 // everything was OK: go on with the next task
416 //
417 continue;
418
419 case kFALSE:
420 //
421 // an error occured: stop eventloop
422 //
423 rc = kFALSE;
424 break;
425
426 case kCONTINUE:
427 //
428 // something occured: skip the rest of the tasks for this event
429 //
430 rc = kTRUE;
431 break;
432
433 default:
434 *fLog << warn << "MTaskList::Process: Unknown return value from MTask::Process()... ignored." << endl;
435 continue;
436 }
437 break;
438 }
439
440 if (!noreset)
441 fParList->ResetBit(MParList::kDoNotReset);
442
443 return rc;
444}
445
446// --------------------------------------------------------------------------
447//
448// do post processing (before eventloop) of all tasks in the task-list
449// only tasks which have successfully been preprocessed are postprocessed.
450//
451Bool_t MTaskList::PostProcess()
452{
453 *fLog << all << "Postprocessing... " << flush;
454
455 //
456 // Reset the ReadyToSave flag.
457 // Reset all containers.
458 //
459 // FIXME: To run a tasklist as a single task in another tasklist we
460 // have to make sure, that the Parameter list isn't reset.
461 //
462 fParList->SetReadyToSave(kFALSE);
463 fParList->Reset();
464
465 //
466 // create the Iterator for the TaskList
467 //
468 TIter Next(fTasks);
469
470 MTask *task=NULL;
471
472 //
473 // loop over all tasks for postprocessing
474 // only tasks which have successfully been preprocessed are postprocessed.
475 //
476 while ( (task=(MTask*)Next()) )
477 {
478 *fLog << all << task->GetName() << "... " << flush;
479
480 if (!task->CallPostProcess())
481 return kFALSE;
482 }
483
484 *fLog << all << endl;
485
486 return kTRUE;
487}
488
489// --------------------------------------------------------------------------
490//
491// Prints the number of times all the tasks in the list has been.
492// For convinience the lvl argument results in a number of spaces at the
493// beginning of the line. So that the structur of a tasklist can be
494// identified. Use MTaskList::PrintStatistics without an argument.
495//
496void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title) const
497{
498 if (lvl==0)
499 {
500 *fLog << all << endl;
501 *fLog << "Execution Statistics: " << endl;
502 *fLog << "---------------------" << endl;
503 *fLog << GetDescriptor();
504 if (title)
505 *fLog << "\t" << fTitle;
506 *fLog << endl;
507 }
508 else
509 {
510 *fLog << setw(lvl) << " " << GetDescriptor();
511 if (title)
512 *fLog << "\t" << fTitle;
513 *fLog << endl;
514 }
515
516 //
517 // create the Iterator for the TaskList
518 //
519 fTasks->ForEach(MTask, PrintStatistics)(lvl+1, title);
520
521 if (lvl==0)
522 *fLog << endl;
523}
524
525
526// --------------------------------------------------------------------------
527void MTaskList::Print(Option_t *t) const
528{
529 *fLog << all << endl;
530 *fLog << GetDescriptor() << endl;
531 *fLog << setfill('-') << setw(strlen(GetDescriptor())) << "" << endl;
532
533 fTasks->Print();
534
535 *fLog << endl;
536}
537
538// --------------------------------------------------------------------------
539//
540// Implementation of SavePrimitive. Used to write the call to a constructor
541// to a macro. In the original root implementation it is used to write
542// gui elements to a macro-file.
543//
544void MTaskList::StreamPrimitive(ofstream &out) const
545{
546 out << " MTaskList " << GetUniqueName();
547 if (fName!=gsDefName || fTitle!=gsDefTitle)
548 {
549 out << "(\"" << fName << "\"";
550 if (fTitle!=gsDefTitle)
551 out << ", \"" << fTitle << "\"";
552 out <<")";
553 }
554 out << ";" << endl << endl;
555
556 TIter Next(fTasks);
557
558 MParContainer *cont = NULL;
559 while ((cont=(MParContainer*)Next()))
560 {
561 cont->SavePrimitive(out, "");
562 out << " " << GetUniqueName() << ".AddToList(&";
563 out << cont->GetUniqueName() << ");" << endl << endl;
564 }
565}
566
567void MTaskList::GetNames(TObjArray &arr) const
568{
569 MParContainer::GetNames(arr);
570 fTasks->ForEach(MParContainer, GetNames)(arr);
571}
572
573void MTaskList::SetNames(TObjArray &arr)
574{
575 MParContainer::SetNames(arr);
576 fTasks->ForEach(MParContainer, SetNames)(arr);
577}
578
Note: See TracBrowser for help on using the repository browser.