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