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 |
|
---|
78 | ClassImp(MTaskList);
|
---|
79 |
|
---|
80 | using namespace std;
|
---|
81 |
|
---|
82 | const TString MTaskList::gsDefName = "MTaskList";
|
---|
83 | const 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 | //
|
---|
91 | MTaskList::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 | //
|
---|
110 | MTaskList::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 | //
|
---|
120 | MTaskList::~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 | //
|
---|
133 | void 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 | //
|
---|
144 | void 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 | //
|
---|
154 | void MTaskList::SetDisplay(MStatusDisplay *d)
|
---|
155 | {
|
---|
156 | fTasks->ForEach(MTask, SetDisplay)(d);
|
---|
157 | MTask::SetDisplay(d);
|
---|
158 | }
|
---|
159 |
|
---|
160 | void MTaskList::SetAccelerator(Byte_t acc)
|
---|
161 | {
|
---|
162 | fTasks->ForEach(MTask, SetAccelerator)(acc);
|
---|
163 | MTask::SetAccelerator(acc);
|
---|
164 | }
|
---|
165 |
|
---|
166 | // --------------------------------------------------------------------------
|
---|
167 | //
|
---|
168 | // Set the serial number for the all tasks in the list and the tasklist
|
---|
169 | // itself.
|
---|
170 | //
|
---|
171 | void MTaskList::SetSerialNumber(Byte_t num)
|
---|
172 | {
|
---|
173 | fTasks->ForEach(MTask, SetSerialNumber)(num);
|
---|
174 | MTask::SetSerialNumber(num);
|
---|
175 | }
|
---|
176 |
|
---|
177 | Bool_t MTaskList::CheckAddToList(MTask *task, const char *type, const MTask *where) const
|
---|
178 | {
|
---|
179 | //
|
---|
180 | // Sanity check
|
---|
181 | //
|
---|
182 | if (!task)
|
---|
183 | {
|
---|
184 | *fLog << err << "ERROR - task argument=NULL." << endl;
|
---|
185 | return kFALSE;
|
---|
186 | }
|
---|
187 |
|
---|
188 | //
|
---|
189 | // Get Name of new task
|
---|
190 | //
|
---|
191 | const char *name = task->GetName();
|
---|
192 |
|
---|
193 | //
|
---|
194 | // Check if the new task is already existing in the list
|
---|
195 | //
|
---|
196 | const TObject *objn = fTasks->FindObject(name);
|
---|
197 | const TObject *objt = fTasks->FindObject(task);
|
---|
198 |
|
---|
199 | if (objn || objt)
|
---|
200 | {
|
---|
201 | //
|
---|
202 | // If the task is already in the list ignore it.
|
---|
203 | //
|
---|
204 | if (objt || objn==task)
|
---|
205 | {
|
---|
206 | *fLog << warn << dbginf << "Warning: Task '" << task->GetName() << ", 0x" << (void*)task;
|
---|
207 | *fLog << "' already existing in '" << GetName() << "'... ignoring." << endl;
|
---|
208 | return kTRUE;
|
---|
209 | }
|
---|
210 |
|
---|
211 | //
|
---|
212 | // Otherwise add it to the list, but print a warning message
|
---|
213 | //
|
---|
214 | *fLog << warn << dbginf << "Warning: Task '" << task->GetName();
|
---|
215 | *fLog << "' already existing in '" << GetName() << "'." << endl;
|
---|
216 | *fLog << "You may not be able to get a pointer to this task by name." << endl;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (!where)
|
---|
220 | return kTRUE;
|
---|
221 |
|
---|
222 | if (fTasks->FindObject(where))
|
---|
223 | return kTRUE;
|
---|
224 |
|
---|
225 | *fLog << err << dbginf << "Error: Cannot find task after which the new task should be scheduled!" << endl;
|
---|
226 | return kFALSE;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | // --------------------------------------------------------------------------
|
---|
231 | //
|
---|
232 | // schedule task for execution, before 'where'.
|
---|
233 | // 'type' is the event type which should be processed
|
---|
234 | //
|
---|
235 | Bool_t MTaskList::AddToListBefore(MTask *task, const MTask *where, const char *type)
|
---|
236 | {
|
---|
237 | if (task==this)
|
---|
238 | {
|
---|
239 | *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
|
---|
240 | *fLog << " would create infinite recursions...ignored." << endl;
|
---|
241 | return kFALSE;
|
---|
242 |
|
---|
243 | }
|
---|
244 |
|
---|
245 | // FIXME: We agreed to put the task into list in an ordered way.
|
---|
246 | if (!CheckAddToList(task, type, where))
|
---|
247 | return kFALSE;
|
---|
248 |
|
---|
249 | *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
|
---|
250 | task->SetStreamId(type);
|
---|
251 | task->SetBit(kMustCleanup);
|
---|
252 | fTasks->AddBefore((TObject*)where, task);
|
---|
253 | *fLog << "Done." << endl;
|
---|
254 |
|
---|
255 | return kTRUE;
|
---|
256 | }
|
---|
257 |
|
---|
258 | // --------------------------------------------------------------------------
|
---|
259 | //
|
---|
260 | // schedule task for execution, after 'where'.
|
---|
261 | // 'type' is the event type which should be processed
|
---|
262 | //
|
---|
263 | Bool_t MTaskList::AddToListAfter(MTask *task, const MTask *where, const char *type)
|
---|
264 | {
|
---|
265 | if (task==this)
|
---|
266 | {
|
---|
267 | *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
|
---|
268 | *fLog << " would create infinite recursions...ignored." << endl;
|
---|
269 | return kFALSE;
|
---|
270 |
|
---|
271 | }
|
---|
272 |
|
---|
273 | // FIXME: We agreed to put the task into list in an ordered way.
|
---|
274 | if (!CheckAddToList(task, type, where))
|
---|
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->AddAfter((TObject*)where, task);
|
---|
281 | *fLog << "Done." << endl;
|
---|
282 |
|
---|
283 | return kTRUE;
|
---|
284 | }
|
---|
285 |
|
---|
286 | // --------------------------------------------------------------------------
|
---|
287 | //
|
---|
288 | // schedule task for execution, 'type' is the event type which should
|
---|
289 | // be processed
|
---|
290 | //
|
---|
291 | Bool_t MTaskList::AddToList(MTask *task, const char *type)
|
---|
292 | {
|
---|
293 | if (task==this)
|
---|
294 | {
|
---|
295 | *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
|
---|
296 | *fLog << " would create infinite recursions...ignored." << endl;
|
---|
297 | return kFALSE;
|
---|
298 |
|
---|
299 | }
|
---|
300 |
|
---|
301 | // FIXME: We agreed to put the task into list in an ordered way.
|
---|
302 | if (!CheckAddToList(task, type))
|
---|
303 | return kFALSE;
|
---|
304 |
|
---|
305 | *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << type << "... " << flush;
|
---|
306 | task->SetStreamId(type);
|
---|
307 | task->SetBit(kMustCleanup);
|
---|
308 | fTasks->Add(task);
|
---|
309 | *fLog << "Done." << endl;
|
---|
310 |
|
---|
311 | return kTRUE;
|
---|
312 | }
|
---|
313 |
|
---|
314 | // --------------------------------------------------------------------------
|
---|
315 | //
|
---|
316 | // Find an object in the list.
|
---|
317 | // 'name' is the name of the object you are searching for.
|
---|
318 | //
|
---|
319 | TObject *MTaskList::FindObject(const char *name) const
|
---|
320 | {
|
---|
321 | return fTasks->FindObject(name);
|
---|
322 | }
|
---|
323 |
|
---|
324 | // --------------------------------------------------------------------------
|
---|
325 | //
|
---|
326 | // check if the object is in the list or not
|
---|
327 | //
|
---|
328 | TObject *MTaskList::FindObject(const TObject *obj) const
|
---|
329 | {
|
---|
330 | return fTasks->FindObject(obj);
|
---|
331 | }
|
---|
332 |
|
---|
333 | // --------------------------------------------------------------------------
|
---|
334 | //
|
---|
335 | // find recursively a tasklist which contains a task with name task
|
---|
336 | //
|
---|
337 | MTaskList *MTaskList::FindTaskList(const char *task)
|
---|
338 | {
|
---|
339 | if (FindObject(task))
|
---|
340 | return this;
|
---|
341 |
|
---|
342 | TIter Next(fTasks);
|
---|
343 | TObject *o = 0;
|
---|
344 | while ((o=Next()))
|
---|
345 | {
|
---|
346 | MTaskList *l = dynamic_cast<MTaskList*>(o);
|
---|
347 | if (!l)
|
---|
348 | continue;
|
---|
349 |
|
---|
350 | if (l->FindObject(task))
|
---|
351 | return l;
|
---|
352 | }
|
---|
353 | return 0;
|
---|
354 | }
|
---|
355 |
|
---|
356 | // --------------------------------------------------------------------------
|
---|
357 | //
|
---|
358 | // find recursively a tasklist which contains a task task
|
---|
359 | //
|
---|
360 | MTaskList *MTaskList::FindTaskList(const MTask *task)
|
---|
361 | {
|
---|
362 | if (FindObject(task))
|
---|
363 | return this;
|
---|
364 |
|
---|
365 | TIter Next(fTasks);
|
---|
366 | TObject *o = 0;
|
---|
367 | while ((o=Next()))
|
---|
368 | {
|
---|
369 | MTaskList *l = dynamic_cast<MTaskList*>(o);
|
---|
370 | if (!l)
|
---|
371 | continue;
|
---|
372 |
|
---|
373 | if (l->FindObject(task))
|
---|
374 | return l;
|
---|
375 | }
|
---|
376 |
|
---|
377 | return 0;
|
---|
378 | }
|
---|
379 |
|
---|
380 | // --------------------------------------------------------------------------
|
---|
381 | //
|
---|
382 | // removes a task from the list (used in PreProcess).
|
---|
383 | // if kIsOwner is set the task is deleted. (see SetOwner())
|
---|
384 | //
|
---|
385 | void MTaskList::Remove(MTask *task)
|
---|
386 | {
|
---|
387 | TObject *obj = fTasks->Remove(task);
|
---|
388 |
|
---|
389 | if (TestBit(kIsOwner))
|
---|
390 | delete obj;
|
---|
391 | }
|
---|
392 |
|
---|
393 | // --------------------------------------------------------------------------
|
---|
394 | //
|
---|
395 | // do pre processing (before eventloop) of all tasks in the task-list
|
---|
396 | // Only if a task overwrites the Process function the task is
|
---|
397 | // added to the fTaskProcess-List. This makes the execution of the
|
---|
398 | // tasklist a little bit (only a little bit) faster, bacause tasks
|
---|
399 | // doing no Processing are not Processed.
|
---|
400 | //
|
---|
401 | Int_t MTaskList::PreProcess(MParList *pList)
|
---|
402 | {
|
---|
403 | *fLog << all << "Preprocessing... " << flush;
|
---|
404 | if (fDisplay)
|
---|
405 | {
|
---|
406 | // Set status lines
|
---|
407 | fDisplay->SetStatusLine1("PreProcessing...");
|
---|
408 | fDisplay->SetStatusLine2("");
|
---|
409 | }
|
---|
410 |
|
---|
411 | fParList = pList;
|
---|
412 |
|
---|
413 | //
|
---|
414 | // Make sure, that the ReadyToSave flag is not reset from a tasklist
|
---|
415 | // running as a task in another tasklist.
|
---|
416 | //
|
---|
417 | const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
|
---|
418 | if (!noreset)
|
---|
419 | fParList->SetBit(MParList::kDoNotReset);
|
---|
420 |
|
---|
421 | //
|
---|
422 | // create the Iterator over the tasklist
|
---|
423 | //
|
---|
424 | TIter Next(fTasks);
|
---|
425 |
|
---|
426 | MTask *task=NULL;
|
---|
427 |
|
---|
428 | //
|
---|
429 | // loop over all tasks for preproccesing
|
---|
430 | //
|
---|
431 | while ((task=(MTask*)Next()))
|
---|
432 | {
|
---|
433 | //
|
---|
434 | // PreProcess the task and check for it's return value.
|
---|
435 | //
|
---|
436 | switch (task->CallPreProcess(fParList))
|
---|
437 | {
|
---|
438 | case kFALSE:
|
---|
439 | return kFALSE;
|
---|
440 |
|
---|
441 | case kTRUE:
|
---|
442 | // Handle GUI events (display changes, mouse clicks)
|
---|
443 | if (fDisplay)
|
---|
444 | gSystem->ProcessEvents();
|
---|
445 | continue;
|
---|
446 |
|
---|
447 | case kSKIP:
|
---|
448 | Remove(task);
|
---|
449 | continue;
|
---|
450 | }
|
---|
451 |
|
---|
452 | *fLog << err << dbginf << "PreProcess of " << task->GetDescriptor();
|
---|
453 | *fLog << " returned an unknown value... aborting." << endl;
|
---|
454 | return kFALSE;
|
---|
455 | }
|
---|
456 |
|
---|
457 | *fLog << all << endl;
|
---|
458 |
|
---|
459 | //
|
---|
460 | // Reset the ReadyToSave flag.
|
---|
461 | //
|
---|
462 | if (!noreset)
|
---|
463 | {
|
---|
464 | fParList->SetReadyToSave(kFALSE);
|
---|
465 | fParList->ResetBit(MParList::kDoNotReset);
|
---|
466 | }
|
---|
467 |
|
---|
468 | //
|
---|
469 | // loop over all tasks to fill fTasksProcess
|
---|
470 | //
|
---|
471 | Next.Reset();
|
---|
472 | fTasksProcess.Clear();
|
---|
473 | while ((task=(MTask*)Next()))
|
---|
474 | if (task->IsA()==MTask::Class() || task->OverwritesProcess())
|
---|
475 | fTasksProcess.Add(task);
|
---|
476 |
|
---|
477 | return kTRUE;
|
---|
478 | }
|
---|
479 |
|
---|
480 | // --------------------------------------------------------------------------
|
---|
481 | //
|
---|
482 | // do reinit of all tasks in the task-list
|
---|
483 | //
|
---|
484 | Bool_t MTaskList::ReInit(MParList *pList)
|
---|
485 | {
|
---|
486 | *fLog << all << "Reinit... " << flush;
|
---|
487 |
|
---|
488 | if (!pList)
|
---|
489 | pList = fParList;
|
---|
490 |
|
---|
491 | //
|
---|
492 | // Make sure, that the ReadyToSave flag is not reset from a tasklist
|
---|
493 | // running as a task in another tasklist.
|
---|
494 | //
|
---|
495 | const Bool_t noreset = pList->TestBit(MParList::kDoNotReset);
|
---|
496 | if (!noreset)
|
---|
497 | pList->SetBit(MParList::kDoNotReset);
|
---|
498 |
|
---|
499 | //
|
---|
500 | // create the Iterator over the tasklist
|
---|
501 | //
|
---|
502 | TIter Next(fTasks);
|
---|
503 |
|
---|
504 | MTask *task=NULL;
|
---|
505 |
|
---|
506 | //
|
---|
507 | // loop over all tasks for reinitialization
|
---|
508 | //
|
---|
509 | while ((task=(MTask*)Next()))
|
---|
510 | {
|
---|
511 | *fLog << all << task->GetName() << "... " << flush;
|
---|
512 |
|
---|
513 | if (!task->ReInit(pList/*?pList:fParList*/))
|
---|
514 | {
|
---|
515 | *fLog << err << "ERROR - ReInit of Task '" << task->GetDescriptor() << "' failed." << endl;
|
---|
516 | return kFALSE;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | *fLog << all << endl;
|
---|
521 |
|
---|
522 | //
|
---|
523 | // Reset the ReadyToSave flag.
|
---|
524 | //
|
---|
525 | if (!noreset)
|
---|
526 | {
|
---|
527 | pList->SetReadyToSave(kFALSE);
|
---|
528 | pList->ResetBit(MParList::kDoNotReset);
|
---|
529 | }
|
---|
530 |
|
---|
531 | return kTRUE;
|
---|
532 | }
|
---|
533 |
|
---|
534 | // --------------------------------------------------------------------------
|
---|
535 | //
|
---|
536 | // do the event execution of all tasks in the task-list
|
---|
537 | //
|
---|
538 | Int_t MTaskList::Process()
|
---|
539 | {
|
---|
540 | //
|
---|
541 | // Check whether there is something which can be processed, otherwise
|
---|
542 | // stop the eventloop.
|
---|
543 | //
|
---|
544 | if (fTasksProcess.GetSize()==0)
|
---|
545 | {
|
---|
546 | *fLog << warn << "Warning: No entries in " << GetDescriptor() << " for Processing." << endl;
|
---|
547 | return kFALSE;
|
---|
548 | }
|
---|
549 |
|
---|
550 | //
|
---|
551 | // Reset the ReadyToSave flag.
|
---|
552 | // Reset all containers.
|
---|
553 | //
|
---|
554 | // Make sure, that the parameter list is not reset from a tasklist
|
---|
555 | // running as a task in another tasklist.
|
---|
556 | //
|
---|
557 | const Bool_t noreset = fParList->TestBit(MParList::kIsProcessing);
|
---|
558 | if (!noreset)
|
---|
559 | {
|
---|
560 | fParList->SetBit(MParList::kIsProcessing);
|
---|
561 | if (!HasAccelerator(kAccDontReset))
|
---|
562 | fParList->Reset();
|
---|
563 | }
|
---|
564 |
|
---|
565 | //
|
---|
566 | // create the Iterator for the TaskList
|
---|
567 | //
|
---|
568 | TIter Next(&fTasksProcess);
|
---|
569 | MTask *task=NULL;
|
---|
570 |
|
---|
571 | //
|
---|
572 | // loop over all tasks for processing
|
---|
573 | //
|
---|
574 | Int_t rc = kTRUE;
|
---|
575 | while ( (task=(MTask*)Next()) )
|
---|
576 | {
|
---|
577 | //
|
---|
578 | // if the task has the wrong stream id skip it.
|
---|
579 | //
|
---|
580 | if (GetStreamId() != task->GetStreamId() &&
|
---|
581 | task->GetStreamId() != "All")
|
---|
582 | continue;
|
---|
583 |
|
---|
584 | //
|
---|
585 | // if it has the right stream id execute the CallProcess() function
|
---|
586 | // and check what the result of it is.
|
---|
587 | // The CallProcess() function increases the execution counter and
|
---|
588 | // calls the Process() function dependent on the existance and
|
---|
589 | // return value of a filter.
|
---|
590 | //
|
---|
591 | switch (task->CallProcess())
|
---|
592 | {
|
---|
593 | case kTRUE:
|
---|
594 | //
|
---|
595 | // everything was OK: go on with the next task
|
---|
596 | //
|
---|
597 | continue;
|
---|
598 |
|
---|
599 | case kFALSE:
|
---|
600 | //
|
---|
601 | // an error occured: stop eventloop
|
---|
602 | //
|
---|
603 | rc = kFALSE;
|
---|
604 | *fLog << inf << task->GetDescriptor() << " has stopped execution of " << GetDescriptor() << "." << endl;
|
---|
605 | break;
|
---|
606 |
|
---|
607 | case kERROR:
|
---|
608 | //
|
---|
609 | // an error occured: stop eventloop and return: failed
|
---|
610 | //
|
---|
611 | *fLog << err << "Fatal error occured while Process() of " << task->GetDescriptor() << "... stopped." << endl;
|
---|
612 | rc = kERROR;
|
---|
613 | break;
|
---|
614 |
|
---|
615 | case kCONTINUE:
|
---|
616 | //
|
---|
617 | // something occured: skip the rest of the tasks for this event
|
---|
618 | //
|
---|
619 | rc = kCONTINUE;
|
---|
620 | break;
|
---|
621 |
|
---|
622 | default:
|
---|
623 | *fLog << warn << dbginf << "Unknown return value from MTask::Process()... ignored." << endl;
|
---|
624 | continue;
|
---|
625 | }
|
---|
626 | break;
|
---|
627 | }
|
---|
628 |
|
---|
629 | if (!noreset)
|
---|
630 | {
|
---|
631 | fParList->SetReadyToSave(kFALSE);
|
---|
632 | fParList->ResetBit(MParList::kIsProcessing);
|
---|
633 | }
|
---|
634 |
|
---|
635 | return rc;
|
---|
636 | }
|
---|
637 |
|
---|
638 | // --------------------------------------------------------------------------
|
---|
639 | //
|
---|
640 | // do post processing (before eventloop) of all tasks in the task-list
|
---|
641 | // only tasks which have successfully been preprocessed are postprocessed.
|
---|
642 | //
|
---|
643 | Int_t MTaskList::PostProcess()
|
---|
644 | {
|
---|
645 | *fLog << all << "Postprocessing... " << flush;
|
---|
646 | if (fDisplay)
|
---|
647 | {
|
---|
648 | // Set status lines
|
---|
649 | fDisplay->SetStatusLine1("PostProcessing...");
|
---|
650 | fDisplay->SetStatusLine2("");
|
---|
651 | }
|
---|
652 |
|
---|
653 | //
|
---|
654 | // Make sure, that the ReadyToSave flag is not reset from a tasklist
|
---|
655 | // running as a task in another tasklist.
|
---|
656 | //
|
---|
657 | const Bool_t noreset = fParList->TestBit(MParList::kDoNotReset);
|
---|
658 | if (!noreset)
|
---|
659 | {
|
---|
660 | fParList->SetBit(MParList::kDoNotReset);
|
---|
661 | fParList->Reset();
|
---|
662 | }
|
---|
663 |
|
---|
664 | //
|
---|
665 | // create the Iterator for the TaskList
|
---|
666 | //
|
---|
667 | TIter Next(fTasks);
|
---|
668 |
|
---|
669 | MTask *task=NULL;
|
---|
670 |
|
---|
671 | //
|
---|
672 | // loop over all tasks for postprocessing
|
---|
673 | // only tasks which have successfully been preprocessed are postprocessed.
|
---|
674 | //
|
---|
675 | while ( (task=(MTask*)Next()) )
|
---|
676 | {
|
---|
677 | if (!task->CallPostProcess())
|
---|
678 | return kFALSE;
|
---|
679 |
|
---|
680 | // Handle GUI events (display changes, mouse clicks)
|
---|
681 | if (fDisplay)
|
---|
682 | gSystem->ProcessEvents();
|
---|
683 | }
|
---|
684 |
|
---|
685 | *fLog << all << endl;
|
---|
686 |
|
---|
687 | //
|
---|
688 | // Reset the ReadyToSave flag.
|
---|
689 | //
|
---|
690 | if (!noreset)
|
---|
691 | {
|
---|
692 | fParList->SetReadyToSave(kFALSE);
|
---|
693 | fParList->ResetBit(MParList::kDoNotReset);
|
---|
694 | }
|
---|
695 |
|
---|
696 | return kTRUE;
|
---|
697 | }
|
---|
698 |
|
---|
699 | // --------------------------------------------------------------------------
|
---|
700 | //
|
---|
701 | // Prints the number of times all the tasks in the list has been.
|
---|
702 | // For convinience the lvl argument results in a number of spaces at the
|
---|
703 | // beginning of the line. So that the structur of a tasklist can be
|
---|
704 | // identified. If a Tasklist or task has filter applied the name of the
|
---|
705 | // filter is printer in <>-brackets behind the number of executions.
|
---|
706 | // Use MTaskList::PrintStatistics without an argument.
|
---|
707 | //
|
---|
708 | void MTaskList::PrintStatistics(const Int_t lvl, Bool_t title, Double_t time) const
|
---|
709 | {
|
---|
710 | if (lvl==0)
|
---|
711 | {
|
---|
712 | *fLog << all << underline << "Process execution Statistics:" << endl;
|
---|
713 | *fLog << GetDescriptor();
|
---|
714 | if (GetFilter())
|
---|
715 | *fLog << " <" << GetFilter()->GetName() << ">";
|
---|
716 | if (title)
|
---|
717 | *fLog << "\t" << fTitle;
|
---|
718 | if (time>=0)
|
---|
719 | *fLog << Form(" %5.1f", GetCpuTime()/time*100) << "%";
|
---|
720 | else
|
---|
721 | *fLog << " 100.0%";
|
---|
722 | *fLog << endl;
|
---|
723 | }
|
---|
724 | else
|
---|
725 | MTask::PrintStatistics(lvl, title, time);
|
---|
726 |
|
---|
727 | //
|
---|
728 | // create the Iterator for the TaskList
|
---|
729 | //
|
---|
730 | fTasks->ForEach(MTask, PrintStatistics)(lvl+1, title, GetCpuTime());
|
---|
731 |
|
---|
732 | if (lvl==0)
|
---|
733 | *fLog << endl;
|
---|
734 | }
|
---|
735 |
|
---|
736 | // --------------------------------------------------------------------------
|
---|
737 | //
|
---|
738 | // Call 'Print()' of all tasks
|
---|
739 | //
|
---|
740 | void MTaskList::Print(Option_t *t) const
|
---|
741 | {
|
---|
742 | *fLog << all << underline << GetDescriptor() << ":" << endl;
|
---|
743 |
|
---|
744 | fTasks->Print();
|
---|
745 |
|
---|
746 | *fLog << endl;
|
---|
747 | }
|
---|
748 |
|
---|
749 | // --------------------------------------------------------------------------
|
---|
750 | //
|
---|
751 | // Implementation of SavePrimitive. Used to write the call to a constructor
|
---|
752 | // to a macro. In the original root implementation it is used to write
|
---|
753 | // gui elements to a macro-file.
|
---|
754 | //
|
---|
755 | void MTaskList::StreamPrimitive(ofstream &out) const
|
---|
756 | {
|
---|
757 | out << " MTaskList " << GetUniqueName();
|
---|
758 | if (fName!=gsDefName || fTitle!=gsDefTitle)
|
---|
759 | {
|
---|
760 | out << "(\"" << fName << "\"";
|
---|
761 | if (fTitle!=gsDefTitle)
|
---|
762 | out << ", \"" << fTitle << "\"";
|
---|
763 | out <<")";
|
---|
764 | }
|
---|
765 | out << ";" << endl << endl;
|
---|
766 |
|
---|
767 | MIter Next(fTasks);
|
---|
768 |
|
---|
769 | MParContainer *cont = NULL;
|
---|
770 | while ((cont=Next()))
|
---|
771 | {
|
---|
772 | cont->SavePrimitive(out, "");
|
---|
773 | out << " " << GetUniqueName() << ".AddToList(&";
|
---|
774 | out << cont->GetUniqueName() << ");" << endl << endl;
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | void MTaskList::GetNames(TObjArray &arr) const
|
---|
779 | {
|
---|
780 | MParContainer::GetNames(arr);
|
---|
781 | fTasks->ForEach(MParContainer, GetNames)(arr);
|
---|
782 | }
|
---|
783 |
|
---|
784 | void MTaskList::SetNames(TObjArray &arr)
|
---|
785 | {
|
---|
786 | MParContainer::SetNames(arr);
|
---|
787 | fTasks->ForEach(MParContainer, SetNames)(arr);
|
---|
788 | }
|
---|
789 |
|
---|
790 | // --------------------------------------------------------------------------
|
---|
791 | //
|
---|
792 | // Read the contents/setup of a parameter container/task from a TEnv
|
---|
793 | // instance (steering card/setup file).
|
---|
794 | // The key to search for in the file should be of the syntax:
|
---|
795 | // prefix.vname
|
---|
796 | // While vname is a name which is specific for a single setup date
|
---|
797 | // (variable) of this container and prefix is something like:
|
---|
798 | // evtloopname.name
|
---|
799 | // While name is the name of the containers/tasks in the parlist/tasklist
|
---|
800 | //
|
---|
801 | // eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
|
---|
802 | // Job4.MImgCleanStd.CleaningLevel2: 2.5
|
---|
803 | //
|
---|
804 | // If this cannot be found the next step is to search for
|
---|
805 | // MImgCleanStd.CleaningLevel1: 3.0
|
---|
806 | // And if this doesn't exist, too, we should search for:
|
---|
807 | // CleaningLevel1: 3.0
|
---|
808 | //
|
---|
809 | // Warning: The programmer is responsible for the names to be unique in
|
---|
810 | // all Mars classes.
|
---|
811 | //
|
---|
812 | Int_t MTaskList::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
813 | {
|
---|
814 | if (print)
|
---|
815 | *fLog << all << "MTaskList::ReadEnv: " << prefix << " (" << (int)print << ")" << endl;
|
---|
816 |
|
---|
817 | MParContainer *cont = NULL;
|
---|
818 |
|
---|
819 | MIter Next(fTasks);
|
---|
820 | while ((cont=Next()))
|
---|
821 | {
|
---|
822 | if (cont->InheritsFrom("MTaskList"))
|
---|
823 | {
|
---|
824 | if (cont->ReadEnv(env, prefix, print)==kERROR)
|
---|
825 | return kERROR;
|
---|
826 | continue;
|
---|
827 | }
|
---|
828 |
|
---|
829 | if (cont->TestEnv(env, prefix, print)==kERROR)
|
---|
830 | return kERROR;
|
---|
831 | }
|
---|
832 |
|
---|
833 | return kTRUE;
|
---|
834 | }
|
---|
835 |
|
---|
836 | // --------------------------------------------------------------------------
|
---|
837 | //
|
---|
838 | // Write the contents/setup of a parameter container/task to a TEnv
|
---|
839 | // instance (steering card/setup file).
|
---|
840 | // The key to search for in the file should be of the syntax:
|
---|
841 | // prefix.vname
|
---|
842 | // While vname is a name which is specific for a single setup date
|
---|
843 | // (variable) of this container and prefix is something like:
|
---|
844 | // evtloopname.name
|
---|
845 | // While name is the name of the containers/tasks in the parlist/tasklist
|
---|
846 | //
|
---|
847 | // eg. Job4.MImgCleanStd.CleaningLevel1: 3.0
|
---|
848 | // Job4.MImgCleanStd.CleaningLevel2: 2.5
|
---|
849 | //
|
---|
850 | // If this cannot be found the next step is to search for
|
---|
851 | // MImgCleanStd.CleaningLevel1: 3.0
|
---|
852 | // And if this doesn't exist, too, we should search for:
|
---|
853 | // CleaningLevel1: 3.0
|
---|
854 | //
|
---|
855 | // Warning: The programmer is responsible for the names to be unique in
|
---|
856 | // all Mars classes.
|
---|
857 | //
|
---|
858 | Bool_t MTaskList::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
|
---|
859 | {
|
---|
860 | MParContainer *cont = NULL;
|
---|
861 |
|
---|
862 | MIter Next(fTasks);
|
---|
863 | while ((cont=Next()))
|
---|
864 | if (!cont->WriteEnv(env, prefix, print))
|
---|
865 | return kFALSE;
|
---|
866 | return kTRUE;
|
---|
867 | }
|
---|
868 |
|
---|
869 | // --------------------------------------------------------------------------
|
---|
870 | //
|
---|
871 | // Removes a task from the tasklist. Returns kFALSE if the object was not
|
---|
872 | // found in the list.
|
---|
873 | //
|
---|
874 | Bool_t MTaskList::RemoveFromList(MTask *task)
|
---|
875 | {
|
---|
876 | TObject *obj = fTasks->Remove(task);
|
---|
877 |
|
---|
878 | //
|
---|
879 | // If the task was found in the list try to remove it from the second
|
---|
880 | // list, too.
|
---|
881 | //
|
---|
882 | if (obj)
|
---|
883 | fTasksProcess.Remove(task);
|
---|
884 |
|
---|
885 | return obj ? kTRUE : kFALSE;
|
---|
886 |
|
---|
887 | }
|
---|
888 |
|
---|
889 | // --------------------------------------------------------------------------
|
---|
890 | //
|
---|
891 | // Find an object with the same name in the list and replace it with
|
---|
892 | // the new one. If the kIsOwner flag is set and the object was not
|
---|
893 | // created automatically, the object is deleted.
|
---|
894 | //
|
---|
895 | Bool_t MTaskList::Replace(MTask *task)
|
---|
896 | {
|
---|
897 | //
|
---|
898 | // check if the object (you want to add) exists
|
---|
899 | //
|
---|
900 | if (!task)
|
---|
901 | return kFALSE;
|
---|
902 |
|
---|
903 | if (task==this)
|
---|
904 | {
|
---|
905 | *fLog << warn << "WARNING - You cannot add a tasklist to itself. This" << endl;
|
---|
906 | *fLog << " would create infinite recursions...ignored." << endl;
|
---|
907 | return kFALSE;
|
---|
908 |
|
---|
909 | }
|
---|
910 |
|
---|
911 | MTask *obj = (MTask*)FindObject(task->GetName());
|
---|
912 | if (!obj)
|
---|
913 | {
|
---|
914 | *fLog << warn << "No object with the same name '";
|
---|
915 | *fLog << task->GetName() << "' in list... adding." << endl;
|
---|
916 | return AddToList(task);
|
---|
917 | }
|
---|
918 |
|
---|
919 | if (task==obj)
|
---|
920 | return kTRUE;
|
---|
921 |
|
---|
922 | *fLog << inf << "Adding " << task->GetName() << " to " << GetName() << " for " << obj->GetStreamId() << "... " << flush;
|
---|
923 | task->SetStreamId(obj->GetStreamId());
|
---|
924 | task->SetBit(kMustCleanup);
|
---|
925 | fTasks->AddAfter((TObject*)obj, task);
|
---|
926 | *fLog << "Done." << endl;
|
---|
927 |
|
---|
928 | RemoveFromList(obj);
|
---|
929 |
|
---|
930 | if (TestBit(kIsOwner))
|
---|
931 | delete obj;
|
---|
932 |
|
---|
933 | //*fLog << inf << "MTask '" << task->GetName() << "' found and replaced..." << endl;
|
---|
934 |
|
---|
935 | return kTRUE;
|
---|
936 | }
|
---|
937 |
|
---|
938 | // --------------------------------------------------------------------------
|
---|
939 | //
|
---|
940 | // Can be used to create an iterator over all tasks, eg:
|
---|
941 | // MTaskList tlist;
|
---|
942 | // TIter Next(tlist); // Be aware: Use a object here rather than a pointer!
|
---|
943 | // TObject *o=0;
|
---|
944 | // while ((o=Next()))
|
---|
945 | // {
|
---|
946 | // [...]
|
---|
947 | // }
|
---|
948 | //
|
---|
949 | MTaskList::operator TIterator*() const
|
---|
950 | {
|
---|
951 | return new TListIter(fTasks);
|
---|
952 | }
|
---|