Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TTask.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Rene Brun 02/09/2000
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class TTask
13\ingroup Base
14
15\legacy{TTask}
16
17TTask is a base class that can be used to build a complex tree of Tasks.
18Each TTask derived class may contain other TTasks that can be executed
19recursively, such that a complex program can be dynamically built and executed
20by invoking the services of the top level Task or one of its subtasks.
21
22Use the TTask::Add function to add a subtask to an existing TTask.
23To execute a TTask, one calls the ExecuteTask function. ExecuteTask will
24call recursively:
25
26 - the TTask::Exec function of the derived class
27 - TTask::ExecuteTasks to execute for each task the list of its subtasks.
28
29If the top level task (see example below) is added to the list of Root
30browsable objects, the tree of tasks can be visualized by the Root browser.
31The browser can be used to start a task, set break points at the beginning
32of a task or when the task has completed. At a breakpoint, data structures
33generated by the execution up this point may be inspected asynchronously
34and then the execution can be resumed by selecting the "Continue" function
35of a task.
36//
37A Task may be active or inactive (controlled by TTask::SetActive).
38When a task is not active, its sub tasks are not executed.
39//
40A TTask tree may be made persistent, saving the status of all the tasks.
41//
42The Root browser's picture below has been generated by executing
43the following script:
44~~~ {.cpp}
45{
46 TTask *aliroot = new TTask("aliroot","ALICE reconstruction main task");
47 TTask *geominit = new TTask("geomInit","Initialize ALICE geometry");
48 TTask *matinit = new TTask("matInit","Initialize ALICE materials");
49 TTask *physinit = new TTask("physInit","Initialize Physics processes");
50 TTask *tracker = new TTask("tracker","Track reconstruction manager");
51 TTask *tpcrec = new TTask("tpcrec","TPC reconstruction");
52 TTask *itsrec = new TTask("itsrec","ITS reconstruction");
53 TTask *muonrec = new TTask("muonRec","Muon Reconstruction");
54 TTask *phosrec = new TTask("phosRec","Phos Reconstruction");
55 TTask *richrec = new TTask("richRec","Rich Reconstruction");
56 TTask *trdrec = new TTask("trdRec","TRD Reconstruction");
57 TTask *globrec = new TTask("globRec","Global Track Reconstruction");
58 TTask *pstats = new TTask("printStats","Print Run Statistics");
59 TTask *run = new TTask("run","Process one run");
60 TTask *event = new TTask("event","Process one event");
61 aliroot->Add(geominit);
62 aliroot->Add(matinit);
63 aliroot->Add(physinit);
64 aliroot->Add(run);
65 run->Add(event);
66 event->Add(tracker);
67 event->Add(muonrec);
68 event->Add(phosrec);
69 event->Add(richrec);
70 event->Add(trdrec);
71 event->Add(globrec);
72 tracker->Add(tpcrec);
73 tracker->Add(itsrec);
74 run->Add(pstats);
75
76 gROOT->GetListOfBrowsables()->Add(aliroot,"aliroot");
77 new TBrowser;
78}
79~~~
80\image html base_tasks.png
81*/
82
83#include <iostream>
84#include "TTask.h"
85#include "TBrowser.h"
86#include "TList.h"
87#include "TROOT.h"
88#include "TRegexp.h"
89
90TTask *TTask::fgBeginTask = nullptr;
91TTask *TTask::fgBreakPoint = nullptr;
92
94
95////////////////////////////////////////////////////////////////////////////////
96/// Default constructor invoked when reading a TTask object from a file.
97
99{
101 fActive = kTRUE;
102 fBreakin = 0;
103 fBreakout = 0;
104 fTasks = nullptr;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Standard constructor.
109
110TTask::TTask(const char* name, const char *title)
111 : TNamed(name,title)
112{
114 fActive = kTRUE;
115 fBreakin = 0;
116 fBreakout = 0;
117 fTasks = new TList();
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Assignment operator.
122
124{
125 if (this != &tt) {
127 if (fTasks)
128 fTasks->Delete();
129 else
130 fTasks = new TList;
131 TIter next(tt.fTasks);
132 while (auto element = next())
133 if (auto task = dynamic_cast<TTask *>(element))
134 fTasks->Add(new TTask(*task));
135 fOption = tt.fOption;
136 fBreakin = tt.fBreakin;
137 fBreakout = tt.fBreakout;
138 fHasExecuted = tt.fHasExecuted;
139 fActive = tt.fActive;
140 }
141 return *this;
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Copy constructor.
146
147TTask::TTask(const TTask &other) : TNamed(other)
148{
149 fTasks = new TList();
150 TIter next(other.fTasks);
151 while (auto element = next())
152 if (auto task = dynamic_cast<TTask *>(element))
153 fTasks->Add(new TTask(*task));
154 fOption = other.fOption;
155 fBreakin = other.fBreakin;
156 fBreakout = other.fBreakout;
158 fActive = other.fActive;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Delete a task and its subtasks.
163
165{
166 if (!fTasks) return;
167 fTasks->Delete();
168 delete fTasks;
169}
170
171
172////////////////////////////////////////////////////////////////////////////////
173/// Add TTask to this.
174
175void TTask::Add(TTask *task)
176{
177 if (!fTasks)
178 fTasks = new TList;
179 fTasks->Add(task);
180}
181
182
183////////////////////////////////////////////////////////////////////////////////
184/// Abort current tree of tasks.
185/// After this call, the tree of tasks is ready to be executed again.
186/// The application must take care of cleaning data structures created
187/// by previous executions.
188
190{
191 if (!fgBeginTask) {
192 printf(" Nothing to abort: No task currently running\n");
193 return;
194 }
195 CleanTasks();
196 fgBeginTask = nullptr;
197 fgBreakPoint = nullptr;
198}
199
200////////////////////////////////////////////////////////////////////////////////
201/// Browse the list of tasks.
202/// It is recommended to add the top level task to the list of
203/// ROOT browsables by:
204/// ~~~{.cpp}
205/// gROOT->GetListOfBrowsables()->Add(myTopLevelTask)
206/// ~~~
207
209{
210 if (fTasks)
211 fTasks->Browse(b);
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Reset tasks state: breakpoints and execute flags
216/// also invokes the Clear function of each task to clear all data
217/// structures created by a previous execution of a task.
218
220{
221 if (fBreakin) fBreakin = 1;
222 if (fBreakout) fBreakout = 1;
224 Clear();
225 TIter next(fTasks);
226 while (auto element = next())
227 if (auto task = dynamic_cast<TTask *>(element))
228 task->CleanTasks();
229}
230
231////////////////////////////////////////////////////////////////////////////////
232/// Recursively call the Clear function of this task and its subtasks.
233/// The Clear function must be implemented for each derived class
234/// to clear all data structures created by a previous execution of a task.
235/// This function is automatically called by the CleanTasks function.
236
238{
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Resume execution at the current break point.
243
245{
246 if (!fgBeginTask) {
247 printf(" No task to continue\n");
248 return;
249 }
250 fgBreakPoint = nullptr;
251
253
254 if (!fgBreakPoint) {
256 fgBeginTask = nullptr;
257 }
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Dummy Execute.
262/// This function must be redefined in the derived classes.
263
265{
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// Execute main task and its subtasks.
270/// When calling this function, the Exec function of the corresponding class
271/// is invoked, then the list of its subtasks is executed calling recursively
272/// all the subtasks, etc.
273///
274/// The option parameter may be used to select different execution steps
275/// within a task. This parameter is passed also to all the subtasks.
276
278{
279 if (fgBeginTask) {
280 Error("ExecuteTask","Cannot execute task:%s, already running task: %s",GetName(),fgBeginTask->GetName());
281 return;
282 }
283 if (!IsActive()) return;
284
285 fOption = option;
286 fgBeginTask = this;
287 fgBreakPoint = nullptr;
288
289 if (fBreakin) return;
290 if (gDebug > 1) {
292 std::cout<<"Execute task:"<<GetName()<<" : "<<GetTitle()<<std::endl;
294 }
295 Exec(option);
296
299
301 if (fBreakout) return;
302
303 if (!fgBreakPoint) {
305 fgBeginTask = nullptr;
306 }
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// Execute all the subtasks of a task.
311
313{
314 TIter next(fTasks);
315 while (auto element = next()) {
316 if (fgBreakPoint)
317 return;
318 auto task = dynamic_cast<TTask *>(element);
319 if (!task)
320 continue;
321 if (!task->IsActive())
322 continue;
323 if (task->fHasExecuted) {
324 task->ExecuteTasks(option);
325 continue;
326 }
327 if (task->fBreakin == 1) {
328 printf("Break at entry of task: %s\n",task->GetName());
329 fgBreakPoint = this;
330 task->fBreakin++;
331 return;
332 }
333
334 if (gDebug > 1) {
336 std::cout<<"Execute task:"<<task->GetName()<<" : "<<task->GetTitle()<<std::endl;
338 }
339 task->Exec(option);
340 task->fHasExecuted = kTRUE;
341 task->ExecuteTasks(option);
343 if (task->fBreakout == 1) {
344 printf("Break at exit of task: %s\n",task->GetName());
345 fgBreakPoint = this;
346 task->fBreakout++;
347 return;
348 }
349 }
350}
351
352////////////////////////////////////////////////////////////////////////////////
353/// List the tree of tasks.
354/// Indentation is used to identify the task tree.
355
357{
359 std::cout <<GetName()<<"\t"<<GetTitle()<<std::endl;
361
362 TString opta = option;
363 TString opt = opta.Strip(TString::kBoth);
364
365 TRegexp re(opt, kTRUE);
366
367 TIter nextobj(fTasks);
368 while (auto obj = nextobj()) {
369 TString s = obj->GetName();
370 if (s.Index(re) == kNPOS) continue;
371 obj->ls(option);
372 }
374}
#define b(i)
Definition RSha256.hxx:100
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
Int_t gDebug
Definition TROOT.cxx:597
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
void Browse(TBrowser *b) override
Browse this collection (called by TBrowser).
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition TNamed.cxx:51
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
static Int_t IncreaseDirLevel()
Increase the indentation level for ls().
Definition TROOT.cxx:2862
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2870
static Int_t DecreaseDirLevel()
Decrease the indentation level for ls().
Definition TROOT.cxx:2745
Regular expression class.
Definition TRegexp.h:31
Basic string class.
Definition TString.h:139
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition TString.cxx:1151
const char * Data() const
Definition TString.h:380
@ kBoth
Definition TString.h:278
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
<div class="legacybox"><h2>Legacy Code</h2> TTask is a legacy interface: there will be no bug fixes n...
Definition TTask.h:35
virtual void Exec(Option_t *option)
Dummy Execute.
Definition TTask.cxx:264
Int_t fBreakin
Definition TTask.h:40
TTask & operator=(const TTask &tt)
Assignment operator.
Definition TTask.cxx:123
TString fOption
Definition TTask.h:39
void ls(Option_t *option="*") const override
List the tree of tasks.
Definition TTask.cxx:356
static TTask * fgBeginTask
Definition TTask.h:45
static TTask * fgBreakPoint
Definition TTask.h:46
virtual ~TTask()
Delete a task and its subtasks.
Definition TTask.cxx:164
Bool_t IsActive() const
Definition TTask.h:68
void Browse(TBrowser *b) override
Browse the list of tasks.
Definition TTask.cxx:208
virtual void CleanTasks()
Reset tasks state: breakpoints and execute flags also invokes the Clear function of each task to clea...
Definition TTask.cxx:219
Int_t fBreakout
Definition TTask.h:41
virtual void ExecuteTasks(Option_t *option)
Execute all the subtasks of a task.
Definition TTask.cxx:312
Bool_t fActive
Definition TTask.h:43
TTask()
Default constructor invoked when reading a TTask object from a file.
Definition TTask.cxx:98
Bool_t fHasExecuted
Definition TTask.h:42
virtual void Abort()
Abort current tree of tasks.
Definition TTask.cxx:189
virtual void Add(TTask *task)
Add TTask to this.
Definition TTask.cxx:175
void Clear(Option_t *option="") override
Recursively call the Clear function of this task and its subtasks.
Definition TTask.cxx:237
TList * fTasks
Definition TTask.h:38
virtual void Continue()
Resume execution at the current break point.
Definition TTask.cxx:244
virtual void ExecuteTask(Option_t *option="0")
Execute main task and its subtasks.
Definition TTask.cxx:277
auto * tt
Definition textangle.C:16