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

Last change on this file since 1524 was 1524, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 16.4 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 while ( (task=(MTask*)Next()) )
395 {
396 //
397 // if the task has the wrong stream id skip it.
398 //
399 if (GetStreamId() != task->GetStreamId() &&
400 task->GetStreamId() != "All")
401 continue;
402
403 //
404 // if it has the right stream id execute the CallProcess() function
405 // and check what the result of it is.
406 // The CallProcess() function increases the execution counter and
407 // calls the Process() function dependent on the existance and
408 // return value of a filter.
409 //
410 switch (task->CallProcess())
411 {
412 case kTRUE:
413 //
414 // everything was OK: go on with the next task
415 //
416 continue;
417
418 case kFALSE:
419 //
420 // an error occured: stop eventloop
421 //
422 return kFALSE;
423
424 case kCONTINUE:
425 //
426 // something occured: skip the rest of the tasks for this event
427 //
428 return kTRUE;
429
430 default:
431 *fLog << warn << "MTaskList::Process: Unknown return value from MTask::Process()... ignored." << endl;
432 }
433 }
434
435 if (!noreset)
436 fParList->ResetBit(MParList::kDoNotReset);
437
438 return kTRUE;
439}
440
441// --------------------------------------------------------------------------
442//
443// do post processing (before eventloop) of all tasks in the task-list
444// only tasks which have successfully been preprocessed are postprocessed.
445//
446Bool_t MTaskList::PostProcess()
447{
448 *fLog << all << "Postprocessing... " << flush;
449
450 //
451 // Reset the ReadyToSave flag.
452 // Reset all containers.
453 //
454 // FIXME: To run a tasklist as a single task in another tasklist we
455 // have to make sure, that the Parameter list isn't reset.
456 //
457 fParList->SetReadyToSave(kFALSE);
458 fParList->Reset();
459
460 //
461 // create the Iterator for the TaskList
462 //
463 TIter Next(fTasks);
464
465 MTask *task=NULL;
466
467 //
468 // loop over all tasks for postprocessing
469 // only tasks which have successfully been preprocessed are postprocessed.
470 //
471 while ( (task=(MTask*)Next()) )
472 {
473 *fLog << all << task->GetName() << "... " << flush;
474
475 if (!task->CallPostProcess())
476 return kFALSE;
477 }
478
479 *fLog << all << endl;
480
481 return kTRUE;
482}
483
484// --------------------------------------------------------------------------
485//
486// Prints the number of times all the tasks in the list has been.
487// For convinience the lvl argument results in a number of spaces at the
488// beginning of the line. So that the structur of a tasklist can be
489// identified. Use MTaskList::PrintStatistics without an argument.
490//
491void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title) const
492{
493 if (lvl==0)
494 {
495 *fLog << all << endl;
496 *fLog << "Execution Statistics: " << endl;
497 *fLog << "---------------------" << endl;
498 *fLog << GetDescriptor();
499 if (title)
500 *fLog << "\t" << fTitle;
501 *fLog << endl;
502 }
503 else
504 {
505 *fLog << setw(lvl) << " " << GetDescriptor();
506 if (title)
507 *fLog << "\t" << fTitle;
508 *fLog << endl;
509 }
510
511 //
512 // create the Iterator for the TaskList
513 //
514 fTasks->ForEach(MTask, PrintStatistics)(lvl+1, title);
515
516 if (lvl==0)
517 *fLog << endl;
518}
519
520
521// --------------------------------------------------------------------------
522void MTaskList::Print(Option_t *t) const
523{
524 *fLog << all << endl;
525 *fLog << GetDescriptor() << endl;
526 *fLog << setfill('-') << setw(strlen(GetDescriptor())) << "" << endl;
527
528 fTasks->Print();
529
530 *fLog << endl;
531}
532
533// --------------------------------------------------------------------------
534//
535// Implementation of SavePrimitive. Used to write the call to a constructor
536// to a macro. In the original root implementation it is used to write
537// gui elements to a macro-file.
538//
539void MTaskList::StreamPrimitive(ofstream &out) const
540{
541 out << " MTaskList " << GetUniqueName();
542 if (fName!=gsDefName || fTitle!=gsDefTitle)
543 {
544 out << "(\"" << fName << "\"";
545 if (fTitle!=gsDefTitle)
546 out << ", \"" << fTitle << "\"";
547 out <<")";
548 }
549 out << ";" << endl << endl;
550
551 TIter Next(fTasks);
552
553 MParContainer *cont = NULL;
554 while ((cont=(MParContainer*)Next()))
555 {
556 cont->SavePrimitive(out, "");
557 out << " " << GetUniqueName() << ".AddToList(&";
558 out << cont->GetUniqueName() << ");" << endl << endl;
559 }
560}
561
562void MTaskList::GetNames(TObjArray &arr) const
563{
564 MParContainer::GetNames(arr);
565 fTasks->ForEach(MParContainer, GetNames)(arr);
566}
567
568void MTaskList::SetNames(TObjArray &arr)
569{
570 MParContainer::SetNames(arr);
571 fTasks->ForEach(MParContainer, SetNames)(arr);
572}
573
Note: See TracBrowser for help on using the repository browser.