1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////
20 // Base class for the top level processor class in a task based procedure.
21 // It allows stepwise invokation of various sub-tasks by the derived
22 // (user defined) top level processor, based on looping over a certain
23 // main object structure (e.g. AliEvent for event-by-event processing).
24 // The main object structure (if needed) may be specified by the derived
25 // top level processor class and will be stored automatically in the
26 // working environment (see below).
27 // This base class provides a working environment for the derived
28 // (user defined) processor class and all of its subtasks.
30 // The default working environment consists of :
32 // * An array containing pointers to the objects which are stored
33 // via the AddObject() facility of this AliJob class.
34 // From this storage the objects can be directly accessed via the
35 // GetObject() and GetObjects() memberfunctions.
36 // * A pointer to the main object structure during job processing.
37 // This pointer can be initiated/updated only by the derived top level
38 // processor via the SetMainObject() facility but all sub-tasks can access
39 // it via the above array facilities or the GetMainObject() memberfunction.
40 // The latter provides faster access to the main object structure than
41 // the GetObject (search) based procedures.
43 // Optionally one may invoke the MakeFolder() memberfunction or use the
44 // "mode" argument of ExecuteJob (see below) to provide in addition to the above
45 // the following job-specific folder structure :
47 // * A folder which may serve as a whiteboard for transferring pointers to
48 // objects which are posted there by the top level processor or any
50 // Objects can be posted in the job folder via the AddObject() facility
51 // of this AliJob class.
52 // Access to the job folder is obtained via the GetFolder() memberfunction
53 // and from this the various objects can be accessed via the usual TFolder
54 // FindObject and/or iterator facilities.
58 // 1) This AliJob class is derived from TTask, which implies that every
59 // (user defined) top level processor class is itself also a TTask.
60 // This allows sub-tasks to be introduced to the top level processor
61 // using the standard TTask facilities.
63 // 2) Only references to the various introduced objects are stored.
64 // It is the user's responsibility to delete all introduced objects,
65 // either in the Exec() or destructor of the derived top level processor class
66 // or via Clear() facility as provided by the TTask machinery.
68 // 3) The top level processor instance is entered into the standard ROOT
69 // ListOfTasks under the name which was provided by the user in the
70 // constructor of the top level processor.
71 // The name of the top level processor is passed automatically as the
72 // opt argument to the Exec(Option_t* opt) memberfunctions of the
73 // various sub-tasks by the ExecuteJob() memberfunction (see below).
74 // This allows all sub-tasks to obtain the pointer to the top level
75 // processor instance from its name via the statement :
77 // AliJob* parent=(AliJob*)gROOT->GetListOfTasks()->FindObject(opt)
79 // 4) If selected, the job-specific folder will be created in the generic folder
80 // called "AliJob-folders" as a sub-folder under the same name as the one
81 // introduced in the constructor of the derived top level processor class.
82 // The folder will only be created if the MakeFolder() member function has been
83 // invoked or when selected explicitly by the "mode" argument of ExecuteJob().
84 // Actual creation of the folder environment (and internal array storage as well)
85 // only takes place when the first object is posted via the AddObject()
86 // or SetMainObject() facilities.
88 // Execution of the (user defined) top level processor has to be invoked via
89 // the memberfunction ExecuteJob() of this AliJob base class.
90 // This will set the default gROOT as the global working directory and then
91 // invoke the (user written) Exec() memberfunction of the top level
92 // processor class with as argument the name of the top level processor instance
93 // as specified by the user in the top level processor constructor.
94 // This will allow stepwise (e.g. event-by-event) execution of the various sub-tasks.
95 // In addition the "mode" argument of ExecuteJob() may be used to select/overrule
96 // creation of the folder environment for the complete job.
97 // See the docs of ExecuteJob() for further details.
99 // It is the user's responsibility to invoke the sub-tasks via the
100 // ExecuteTasks() statement at the appropriate location in the top level
103 //--- Author: Nick van Eijndhoven 07-may-2005 Utrecht University
104 //- Modified: NvE $Date$ Utrecht University
105 ///////////////////////////////////////////////////////////////////////////
108 #include "Riostream.h"
110 ClassImp(AliJob) // Class implementation to enable ROOT I/O
112 AliJob::AliJob(const char* name,const char* title) : TTask(name,title)
114 // Default constructor.
115 // Initialise the working environment for general data access
116 // by the derived task and its subtasks.
124 // Introduce this AliJob based instance into the ROOT task list
125 TSeqCollection* tasks=gROOT->GetListOfTasks();
126 if (tasks) tasks->Add(this);
128 ///////////////////////////////////////////////////////////////////////////
131 // Default destructor.
132 // The internal array and job specific folder (if any) holding the various
133 // references are deleted.
134 // Note : The objects belonging to the various pointers in the array/folder
135 // and the main processing object are NOT deleted by this base class.
137 // Remove this AliJob based instance into the ROOT task list
138 TSeqCollection* tasks=gROOT->GetListOfTasks();
139 if (tasks) tasks->Remove(this);
148 TList* list=gROOT->GetListOfBrowsables();
151 TFolder* top=(TFolder*)list->FindObject("AliJob-folders");
152 if (top) RecursiveRemove(fFolder);
163 ///////////////////////////////////////////////////////////////////////////
164 void AliJob::ListEnvironment()
166 // Provide listing of the job environment.
168 cout << " ***" << endl;
169 cout << " *** Environment of job " << GetName() << " ***" << endl;
170 cout << " ***" << endl;
171 cout << " === Available (sub)tasks : " << endl;
175 cout << " === Current job-folder contents : " << endl;
180 ///////////////////////////////////////////////////////////////////////////
181 void AliJob::ExecuteJob(Int_t mode)
183 // Invokation of the top level processor via its Exec() memberfunction.
184 // The input argument "mode" can be used to explicitly specify the
185 // (de)selection of the folder environment creation.
187 // mode = -1 : Explicitly prohibit folder creation for the complete job
188 // 0 : Folder creation selection steered by MakeFolder()
189 // 1 : Explicitly select creation of the folder environment
190 // for the complete job.
192 // The default is mode=0.
194 // Note : Before execution gROOT is set as the global working directory.
196 if (mode<0) fMakefolder=-1;
197 if (mode>0) fMakefolder=1;
202 ///////////////////////////////////////////////////////////////////////////
203 void AliJob::MakeFolder()
205 // Select creation of the folder structure in addition to the internal
206 // array storage of objects.
207 // Creation of the folder structure is only activated if it was not
208 // explicitly forbidden by the specified "mode" on invokation of the
209 // ExecuteJob() memberfunction.
211 if (!fMakefolder) fMakefolder=1;
213 ///////////////////////////////////////////////////////////////////////////
214 TFolder* AliJob::GetFolder() const
216 // Provide pointer to the whiteboard folder.
219 ///////////////////////////////////////////////////////////////////////////
220 TObject* AliJob::GetMainObject() const
222 // Provide pointer to the main object structure.
225 ///////////////////////////////////////////////////////////////////////////
226 void AliJob::SetMainObject(TObject* obj)
228 // Store pointer to the main object structure.
235 ///////////////////////////////////////////////////////////////////////////
236 void AliJob::AddObject(TObject* obj)
238 // Store pointer of specified object in the working environment.
242 if (!fObjects) fObjects=new TObjArray();
244 if (fMakefolder>0 && !fFolder)
246 // Create the top level environment folder for all AliJobs if needed
247 TList* list=gROOT->GetListOfBrowsables();
250 TFolder* top=(TFolder*)list->FindObject("AliJob-folders");
253 top=new TFolder("AliJob-folders","Environment for all AliJob derived tasks");
254 list->Add(top,"AliJob-folders");
256 // Create the task-specific folder as a sub-folder in the top folder
257 fFolder=top->AddFolder(GetName(),GetTitle());
261 // Add object pointer to array and folder if it doesn't already exist
263 for (Int_t i=0; i<fObjects->GetEntries(); i++)
265 if (obj==fObjects->At(i))
274 if (fFolder) fFolder->Add(obj);
277 ///////////////////////////////////////////////////////////////////////////
278 void AliJob::AddObjects(TObjArray* arr)
280 // Store pointers of all the array objects individually in the working environment.
285 for (Int_t i=0; i<arr->GetSize(); i++)
288 if (obj) AddObject(obj);
291 ///////////////////////////////////////////////////////////////////////////
292 void AliJob::RemoveObject(TObject* obj)
294 // Remove pointer of specified object from the working environment.
295 // In case the specified object is also the main object, the main object
296 // pointer will be set to 0 as well.
302 TObject* test=fObjects->Remove(obj);
305 fObjects->Compress();
306 if (test==fMainObject) fMainObject=0;
310 if (fFolder) fFolder->Remove(obj);
312 ///////////////////////////////////////////////////////////////////////////
313 void AliJob::RemoveObjects(const char* classname)
315 // Remove all stored objects inheriting from classname.
316 // In case one of the removed objects is also the main object, the main object
317 // pointer will be set to 0 as well.
319 if (!fObjects) return;
322 for (Int_t i=0; i<fObjects->GetEntries(); i++)
324 TObject* obj=fObjects->At(i);
325 if (obj->InheritsFrom(classname))
327 TObject* test=fObjects->Remove(obj);
331 if (test==fMainObject) fMainObject=0;
333 if (fFolder) fFolder->Remove(obj);
336 if (remove) fObjects->Compress();
338 ///////////////////////////////////////////////////////////////////////////
339 TObject* AliJob::GetObject(Int_t j) const
341 // Provide pointer to j-th stored object.
342 // Note : j=1 indicates the first object.
344 if (!fObjects || j<1) return 0;
348 if (j<=fObjects->GetEntries()) obj=fObjects->At(j-1);
351 ///////////////////////////////////////////////////////////////////////////
352 TObject* AliJob::GetObject(const char* classname) const
354 // Provide pointer to the first stored object which inherits from classname.
356 if (!fObjects) return 0;
359 for (Int_t i=0; i<fObjects->GetEntries(); i++)
361 TObject* obx=fObjects->At(i);
362 if (obx->InheritsFrom(classname))
370 ///////////////////////////////////////////////////////////////////////////
371 TObjArray* AliJob::GetObjects() const
373 // Provide pointers of all the stored objects.
376 ///////////////////////////////////////////////////////////////////////////
377 TObjArray* AliJob::GetObjects(const char* classname)
379 // Provide pointers to all stored objects inheriting from classname.
381 if (!fObjects) return 0;
389 fSelect=new TObjArray();
392 for (Int_t i=0; i<fObjects->GetEntries(); i++)
394 TObject* obj=fObjects->At(i);
395 if (obj->InheritsFrom(classname)) fSelect->Add(obj);
399 ///////////////////////////////////////////////////////////////////////////