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
93
94////////////////////////////////////////////////////////////////////////////////
95/// Default constructor invoked when reading a TTask object from a file.
96
98{
100 fActive = kTRUE;
101 fBreakin = 0;
102 fBreakout = 0;
103 fTasks = nullptr;
104}
105
106////////////////////////////////////////////////////////////////////////////////
107/// Standard constructor.
108
109TTask::TTask(const char* name, const char *title)
110 : TNamed(name,title)
111{
113 fActive = kTRUE;
114 fBreakin = 0;
115 fBreakout = 0;
116 fTasks = new TList();
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Assignment operator.
122
124{
125 if (this != &tt) {
127 if (fTasks)
128 fTasks->Clear();
129 else {
130 fTasks = new TList;
132 }
133 TIter next(tt.fTasks);
134 while (auto element = next())
135 if (auto task = dynamic_cast<TTask *>(element))
136 fTasks->Add(new TTask(*task));
137 fOption = tt.fOption;
138 fBreakin = tt.fBreakin;
139 fBreakout = tt.fBreakout;
140 fHasExecuted = tt.fHasExecuted;
141 fActive = tt.fActive;
142 }
143 return *this;
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Copy constructor.
148
150{
151 fTasks = new TList();
153 TIter next(other.fTasks);
154 while (auto element = next())
155 if (auto task = dynamic_cast<TTask *>(element))
156 fTasks->Add(new TTask(*task));
157 fOption = other.fOption;
158 fBreakin = other.fBreakin;
159 fBreakout = other.fBreakout;
161 fActive = other.fActive;
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Delete a task and its subtasks.
166
168{
169 delete fTasks;
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Add TTask to this.
174
176{
177 if (!fTasks) {
178 fTasks = new TList;
180 }
181 fTasks->Add(task);
182}
183
184
185////////////////////////////////////////////////////////////////////////////////
186/// Abort current tree of tasks.
187/// After this call, the tree of tasks is ready to be executed again.
188/// The application must take care of cleaning data structures created
189/// by previous executions.
190
192{
193 if (!fgBeginTask) {
194 printf(" Nothing to abort: No task currently running\n");
195 return;
196 }
197 CleanTasks();
198 fgBeginTask = nullptr;
199 fgBreakPoint = nullptr;
200}
201
202////////////////////////////////////////////////////////////////////////////////
203/// Browse the list of tasks.
204/// It is recommended to add the top level task to the list of
205/// ROOT browsables by:
206/// ~~~{.cpp}
207/// gROOT->GetListOfBrowsables()->Add(myTopLevelTask)
208/// ~~~
209
211{
212 if (fTasks)
213 fTasks->Browse(b);
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Reset tasks state: breakpoints and execute flags
218/// also invokes the Clear function of each task to clear all data
219/// structures created by a previous execution of a task.
220
222{
223 if (fBreakin) fBreakin = 1;
224 if (fBreakout) fBreakout = 1;
226 Clear();
227 TIter next(fTasks);
228 while (auto element = next())
229 if (auto task = dynamic_cast<TTask *>(element))
230 task->CleanTasks();
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// Recursively call the Clear function of this task and its subtasks.
235/// The Clear function must be implemented for each derived class
236/// to clear all data structures created by a previous execution of a task.
237/// This function is automatically called by the CleanTasks function.
238
240{
241}
242
243////////////////////////////////////////////////////////////////////////////////
244/// Resume execution at the current break point.
245
247{
248 if (!fgBeginTask) {
249 printf(" No task to continue\n");
250 return;
251 }
252 fgBreakPoint = nullptr;
253
254 fgBeginTask->ExecuteTasks(fOption.Data());
255
256 if (!fgBreakPoint) {
257 fgBeginTask->CleanTasks();
258 fgBeginTask = nullptr;
259 }
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Dummy Execute.
264/// This function must be redefined in the derived classes.
265
267{
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// Execute main task and its subtasks.
272/// When calling this function, the Exec function of the corresponding class
273/// is invoked, then the list of its subtasks is executed calling recursively
274/// all the subtasks, etc.
275///
276/// The option parameter may be used to select different execution steps
277/// within a task. This parameter is passed also to all the subtasks.
278
280{
281 if (fgBeginTask) {
282 Error("ExecuteTask","Cannot execute task:%s, already running task: %s",GetName(),fgBeginTask->GetName());
283 return;
284 }
285 if (!IsActive()) return;
286
287 fOption = option;
288 fgBeginTask = this;
289 fgBreakPoint = nullptr;
290
291 if (fBreakin) return;
292 if (gDebug > 1) {
294 std::cout<<"Execute task:"<<GetName()<<" : "<<GetTitle()<<std::endl;
296 }
297 Exec(option);
298
301
303 if (fBreakout) return;
304
305 if (!fgBreakPoint) {
306 fgBeginTask->CleanTasks();
307 fgBeginTask = nullptr;
308 }
309}
310
311////////////////////////////////////////////////////////////////////////////////
312/// Execute all the subtasks of a task.
313
315{
316 TIter next(fTasks);
317 while (auto element = next()) {
318 if (fgBreakPoint)
319 return;
320 auto task = dynamic_cast<TTask *>(element);
321 if (!task)
322 continue;
323 if (!task->IsActive())
324 continue;
325 if (task->fHasExecuted) {
326 task->ExecuteTasks(option);
327 continue;
328 }
329 if (task->fBreakin == 1) {
330 printf("Break at entry of task: %s\n",task->GetName());
331 fgBreakPoint = this;
332 task->fBreakin++;
333 return;
334 }
335
336 if (gDebug > 1) {
338 std::cout<<"Execute task:"<<task->GetName()<<" : "<<task->GetTitle()<<std::endl;
340 }
341 task->Exec(option);
342 task->fHasExecuted = kTRUE;
343 task->ExecuteTasks(option);
345 if (task->fBreakout == 1) {
346 printf("Break at exit of task: %s\n",task->GetName());
347 fgBreakPoint = this;
348 task->fBreakout++;
349 return;
350 }
351 }
352}
353
354////////////////////////////////////////////////////////////////////////////////
355/// List the tree of tasks.
356/// Indentation is used to identify the task tree.
357
359{
361 std::cout <<GetName()<<"\t"<<GetTitle()<<std::endl;
363
365 TString opt = opta.Strip(TString::kBoth);
366
367 TRegexp re(opt, kTRUE);
368
370 while (auto obj = nextobj()) {
371 TString s = obj->GetName();
372 if (s.Index(re) == kNPOS) continue;
373 obj->ls(option);
374 }
376}
#define b(i)
Definition RSha256.hxx:100
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Ssiz_t kNPOS
The equivalent of std::string::npos for the ROOT class TString.
Definition RtypesCore.h:131
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
char name[80]
Definition TGX11.cxx:110
Int_t gDebug
Global variable setting the debug level. Set to 0 to disable, increase it in steps of 1 to increase t...
Definition TROOT.cxx:627
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
void Browse(TBrowser *b) override
Browse this collection (called by TBrowser).
A doubly linked list.
Definition TList.h:38
void Clear(Option_t *option="") override
Remove all objects from the list.
Definition TList.cxx:399
void Add(TObject *obj) override
Definition TList.h:81
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:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition TNamed.cxx:50
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
static Int_t IncreaseDirLevel()
Increase the indentation level for ls().
Definition TROOT.cxx:2890
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2898
static Int_t DecreaseDirLevel()
Decrease the indentation level for ls().
Definition TROOT.cxx:2748
Regular expression class.
Definition TRegexp.h:31
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
@ kBoth
Definition TString.h:284
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:659
<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:266
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:358
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:167
Bool_t IsActive() const
Definition TTask.h:68
void Browse(TBrowser *b) override
Browse the list of tasks.
Definition TTask.cxx:210
virtual void CleanTasks()
Reset tasks state: breakpoints and execute flags also invokes the Clear function of each task to clea...
Definition TTask.cxx:221
Int_t fBreakout
Definition TTask.h:41
virtual void ExecuteTasks(Option_t *option)
Execute all the subtasks of a task.
Definition TTask.cxx:314
Bool_t fActive
Definition TTask.h:43
TTask()
Default constructor invoked when reading a TTask object from a file.
Definition TTask.cxx:97
Bool_t fHasExecuted
Definition TTask.h:42
virtual void Abort()
Abort current tree of tasks.
Definition TTask.cxx:191
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:239
TList * fTasks
Definition TTask.h:38
virtual void Continue()
Resume execution at the current break point.
Definition TTask.cxx:246
virtual void ExecuteTask(Option_t *option="0")
Execute main task and its subtasks.
Definition TTask.cxx:279
auto * tt
Definition textangle.C:16