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();
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Assignment operator.
123
125{
126 if (this != &tt) {
128 if (fTasks)
129 fTasks->Clear();
130 else {
131 fTasks = new TList;
133 }
134 TIter next(tt.fTasks);
135 while (auto element = next())
136 if (auto task = dynamic_cast<TTask *>(element))
137 fTasks->Add(new TTask(*task));
138 fOption = tt.fOption;
139 fBreakin = tt.fBreakin;
140 fBreakout = tt.fBreakout;
141 fHasExecuted = tt.fHasExecuted;
142 fActive = tt.fActive;
143 }
144 return *this;
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Copy constructor.
149
150TTask::TTask(const TTask &other) : TNamed(other)
151{
152 fTasks = new TList();
154 TIter next(other.fTasks);
155 while (auto element = next())
156 if (auto task = dynamic_cast<TTask *>(element))
157 fTasks->Add(new TTask(*task));
158 fOption = other.fOption;
159 fBreakin = other.fBreakin;
160 fBreakout = other.fBreakout;
162 fActive = other.fActive;
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Delete a task and its subtasks.
167
169{
170 delete fTasks;
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Add TTask to this.
175
176void TTask::Add(TTask *task)
177{
178 if (!fTasks) {
179 fTasks = new TList;
181 }
182 fTasks->Add(task);
183}
184
185
186////////////////////////////////////////////////////////////////////////////////
187/// Abort current tree of tasks.
188/// After this call, the tree of tasks is ready to be executed again.
189/// The application must take care of cleaning data structures created
190/// by previous executions.
191
193{
194 if (!fgBeginTask) {
195 printf(" Nothing to abort: No task currently running\n");
196 return;
197 }
198 CleanTasks();
199 fgBeginTask = nullptr;
200 fgBreakPoint = nullptr;
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// Browse the list of tasks.
205/// It is recommended to add the top level task to the list of
206/// ROOT browsables by:
207/// ~~~{.cpp}
208/// gROOT->GetListOfBrowsables()->Add(myTopLevelTask)
209/// ~~~
210
212{
213 if (fTasks)
214 fTasks->Browse(b);
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Reset tasks state: breakpoints and execute flags
219/// also invokes the Clear function of each task to clear all data
220/// structures created by a previous execution of a task.
221
223{
224 if (fBreakin) fBreakin = 1;
225 if (fBreakout) fBreakout = 1;
227 Clear();
228 TIter next(fTasks);
229 while (auto element = next())
230 if (auto task = dynamic_cast<TTask *>(element))
231 task->CleanTasks();
232}
233
234////////////////////////////////////////////////////////////////////////////////
235/// Recursively call the Clear function of this task and its subtasks.
236/// The Clear function must be implemented for each derived class
237/// to clear all data structures created by a previous execution of a task.
238/// This function is automatically called by the CleanTasks function.
239
241{
242}
243
244////////////////////////////////////////////////////////////////////////////////
245/// Resume execution at the current break point.
246
248{
249 if (!fgBeginTask) {
250 printf(" No task to continue\n");
251 return;
252 }
253 fgBreakPoint = nullptr;
254
256
257 if (!fgBreakPoint) {
259 fgBeginTask = nullptr;
260 }
261}
262
263////////////////////////////////////////////////////////////////////////////////
264/// Dummy Execute.
265/// This function must be redefined in the derived classes.
266
268{
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Execute main task and its subtasks.
273/// When calling this function, the Exec function of the corresponding class
274/// is invoked, then the list of its subtasks is executed calling recursively
275/// all the subtasks, etc.
276///
277/// The option parameter may be used to select different execution steps
278/// within a task. This parameter is passed also to all the subtasks.
279
281{
282 if (fgBeginTask) {
283 Error("ExecuteTask","Cannot execute task:%s, already running task: %s",GetName(),fgBeginTask->GetName());
284 return;
285 }
286 if (!IsActive()) return;
287
288 fOption = option;
289 fgBeginTask = this;
290 fgBreakPoint = nullptr;
291
292 if (fBreakin) return;
293 if (gDebug > 1) {
295 std::cout<<"Execute task:"<<GetName()<<" : "<<GetTitle()<<std::endl;
297 }
298 Exec(option);
299
302
304 if (fBreakout) return;
305
306 if (!fgBreakPoint) {
308 fgBeginTask = nullptr;
309 }
310}
311
312////////////////////////////////////////////////////////////////////////////////
313/// Execute all the subtasks of a task.
314
316{
317 TIter next(fTasks);
318 while (auto element = next()) {
319 if (fgBreakPoint)
320 return;
321 auto task = dynamic_cast<TTask *>(element);
322 if (!task)
323 continue;
324 if (!task->IsActive())
325 continue;
326 if (task->fHasExecuted) {
327 task->ExecuteTasks(option);
328 continue;
329 }
330 if (task->fBreakin == 1) {
331 printf("Break at entry of task: %s\n",task->GetName());
332 fgBreakPoint = this;
333 task->fBreakin++;
334 return;
335 }
336
337 if (gDebug > 1) {
339 std::cout<<"Execute task:"<<task->GetName()<<" : "<<task->GetTitle()<<std::endl;
341 }
342 task->Exec(option);
343 task->fHasExecuted = kTRUE;
344 task->ExecuteTasks(option);
346 if (task->fBreakout == 1) {
347 printf("Break at exit of task: %s\n",task->GetName());
348 fgBreakPoint = this;
349 task->fBreakout++;
350 return;
351 }
352 }
353}
354
355////////////////////////////////////////////////////////////////////////////////
356/// List the tree of tasks.
357/// Indentation is used to identify the task tree.
358
360{
362 std::cout <<GetName()<<"\t"<<GetTitle()<<std::endl;
364
365 TString opta = option;
366 TString opt = opta.Strip(TString::kBoth);
367
368 TRegexp re(opt, kTRUE);
369
370 TIter nextobj(fTasks);
371 while (auto obj = nextobj()) {
372 TString s = obj->GetName();
373 if (s.Index(re) == kNPOS) continue;
374 obj->ls(option);
375 }
377}
#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:595
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:400
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: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:976
static Int_t IncreaseDirLevel()
Increase the indentation level for ls().
Definition TROOT.cxx:2836
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2844
static Int_t DecreaseDirLevel()
Decrease the indentation level for ls().
Definition TROOT.cxx:2718
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:1163
const char * Data() const
Definition TString.h:376
@ kBoth
Definition TString.h:276
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:267
Int_t fBreakin
Definition TTask.h:40
TTask & operator=(const TTask &tt)
Assignment operator.
Definition TTask.cxx:124
TString fOption
Definition TTask.h:39
void ls(Option_t *option="*") const override
List the tree of tasks.
Definition TTask.cxx:359
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:168
Bool_t IsActive() const
Definition TTask.h:68
void Browse(TBrowser *b) override
Browse the list of tasks.
Definition TTask.cxx:211
virtual void CleanTasks()
Reset tasks state: breakpoints and execute flags also invokes the Clear function of each task to clea...
Definition TTask.cxx:222
Int_t fBreakout
Definition TTask.h:41
virtual void ExecuteTasks(Option_t *option)
Execute all the subtasks of a task.
Definition TTask.cxx:315
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:192
virtual void Add(TTask *task)
Add TTask to this.
Definition TTask.cxx:176
void Clear(Option_t *option="") override
Recursively call the Clear function of this task and its subtasks.
Definition TTask.cxx:240
TList * fTasks
Definition TTask.h:38
virtual void Continue()
Resume execution at the current break point.
Definition TTask.cxx:247
virtual void ExecuteTask(Option_t *option="0")
Execute main task and its subtasks.
Definition TTask.cxx:280
auto * tt
Definition textangle.C:16