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