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 
15 TTask is a base class that can be used to build a complex tree of Tasks.
16 Each TTask derived class may contain other TTasks that can be executed
17 recursively, such that a complex program can be dynamically built and executed
18 by invoking the services of the top level Task or one of its subtasks.
19 
20 Use the TTask::Add function to add a subtask to an existing TTask.
21 To execute a TTask, one calls the ExecuteTask function. ExecuteTask will
22 call 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 
27 If the top level task (see example below) is added to the list of Root
28 browsable objects, the tree of tasks can be visualized by the Root browser.
29 The browser can be used to start a task, set break points at the beginning
30 of a task or when the task has completed. At a breakpoint, data structures
31 generated by the execution up this point may be inspected asynchronously
32 and then the execution can be resumed by selecting the "Continue" function
33 of a task.
34 //
35 A Task may be active or inactive (controlled by TTask::SetActive).
36 When a task is not active, its sub tasks are not executed.
37 //
38 A TTask tree may be made persistent, saving the status of all the tasks.
39 //
40 The Root browser's picture below has been generated by executing
41 the 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 
88 TTask *TTask::fgBeginTask = nullptr;
89 TTask *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 = 0;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Standard constructor.
107 
108 TTask::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 
143 TTask::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 
172 void 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 = 0;
192  fgBreakPoint = 0;
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 = 0;
246 
248 
249  if (!fgBreakPoint) {
251  fgBeginTask = 0;
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 = 0;
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 
295  if (gDebug > 1) TROOT::DecreaseDirLevel();
296  if (fBreakout) return;
297 
298  if (!fgBreakPoint) {
300  fgBeginTask = 0;
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);
333  if (gDebug > 1) TROOT::DecreaseDirLevel();
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 
347 void 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 }
TTask::Add
virtual void Add(TTask *task)
Add TTask to this.
Definition: TTask.cxx:172
TBrowser
Definition: TBrowser.h:37
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
tt
auto * tt
Definition: textangle.C:16
TTask::fTasks
TList * fTasks
Definition: TTask.h:38
TTask::Clear
virtual void Clear(Option_t *option="")
Recursively call the Clear function of this task and its subtasks.
Definition: TTask.cxx:232
TTask::fHasExecuted
Bool_t fHasExecuted
Definition: TTask.h:42
TROOT::DecreaseDirLevel
static Int_t DecreaseDirLevel()
Decrease the indentation level for ls().
Definition: TROOT.cxx:2694
TString::Strip
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1106
kNPOS
const Ssiz_t kNPOS
Definition: RtypesCore.h:115
TTask::fgBeginTask
static TTask * fgBeginTask
Definition: TTask.h:45
TTask::TTask
TTask()
Default constructor invoked when reading a TTask object from a file.
Definition: TTask.cxx:96
TList::Delete
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
TString::Data
const char * Data() const
Definition: TString.h:369
TNamed::operator=
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
TTask::fBreakin
Int_t fBreakin
Definition: TTask.h:40
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
TTask::ExecuteTask
virtual void ExecuteTask(Option_t *option="0")
Execute main task and its subtasks.
Definition: TTask.cxx:272
TBrowser.h
TList.h
TTask::fBreakout
Int_t fBreakout
Definition: TTask.h:41
TString
Definition: TString.h:136
b
#define b(i)
Definition: RSha256.hxx:118
TString::kBoth
@ kBoth
Definition: TString.h:267
TROOT.h
TObject::ls
virtual void ls(Option_t *option="") const
The ls function lists the contents of a class on stdout.
Definition: TObject.cxx:494
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
TTask::IsActive
Bool_t IsActive() const
Definition: TTask.h:68
TTask::ls
virtual void ls(Option_t *option="*") const
List the tree of tasks.
Definition: TTask.cxx:347
TRegexp.h
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TTask
Definition: TTask.h:35
TTask::fActive
Bool_t fActive
Definition: TTask.h:43
TNamed
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
gDebug
R__EXTERN Int_t gDebug
Definition: RtypesCore.h:119
TTask::Exec
virtual void Exec(Option_t *option)
Dummy Execute.
Definition: TTask.cxx:259
TCollection::Browse
void Browse(TBrowser *b)
Browse this collection (called by TBrowser).
Definition: TCollection.cxx:248
TTask::ExecuteTasks
virtual void ExecuteTasks(Option_t *option)
Execute all the subtasks of a task.
Definition: TTask.cxx:307
TTask::fOption
TString fOption
Definition: TTask.h:39
TROOT::IncreaseDirLevel
static Int_t IncreaseDirLevel()
Increase the indentation level for ls().
Definition: TROOT.cxx:2790
TROOT::IndentLevel
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2798
TTask.h
TRegexp
Definition: TRegexp.h:31
TTask::CleanTasks
virtual void CleanTasks()
Reset tasks state: breakpoints and execute flags also invokes the Clear function of each task to clea...
Definition: TTask.cxx:213
TTask::Browse
virtual void Browse(TBrowser *b)
Browse the list of tasks.
Definition: TTask.cxx:203
TTask::Continue
virtual void Continue()
Resume execution at the current break point.
Definition: TTask.cxx:239
TTask::~TTask
virtual ~TTask()
Delete a task and its subtasks.
Definition: TTask.cxx:161
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TObject
Definition: TObject.h:37
name
char name[80]
Definition: TGX11.cxx:110
TIter
Definition: TCollection.h:233
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TTask::fgBreakPoint
static TTask * fgBreakPoint
Definition: TTask.h:46
TTask::operator=
TTask & operator=(const TTask &tt)
Assignment operator.
Definition: TTask.cxx:121
TTask::Abort
virtual void Abort()
Abort current tree of tasks.
Definition: TTask.cxx:184
TList
Definition: TList.h:44