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 to provide
44 // in addition to the above the following job-specific folder structure :
46 // * A folder which may serve as a whiteboard for transferring pointers to
47 // objects which are posted there by the top level processor or any
49 // Objects can be posted in the job folder via the AddObject() facility
50 // of this AliJob class.
51 // Access to the job folder is obtained via the GetFolder() memberfunction
52 // and from this the various objects can be accessed via the usual TFolder
53 // FindObject and/or iterator facilities.
57 // 1) This AliJob class is derived from TTask, which implies that every
58 // (user defined) top level processor class is itself also a TTask.
59 // This allows sub-tasks to be introduced to the top level processor
60 // using the standard TTask facilities.
62 // 2) Only references to the various introduced objects are stored.
63 // It is the user's responsibility to delete all introduced objects,
64 // either in the Exec() or destructor of the derived top level processor class
65 // or via Clear() facility as provided by the TTask machinery.
67 // 3) The top level processor instance is entered into the standard ROOT
68 // ListOfTasks under the name which was provided by the user in the
69 // constructor of the top level processor.
70 // The name of the top level processor is passed automatically as the
71 // opt argument to the Exec(Option_t* opt) memberfunctions of the
72 // various sub-tasks by the ExecuteJob() memberfunction (see below).
73 // This allows all sub-tasks to obtain the pointer to the top level
74 // processor instance from its name via the statement :
76 // AliJob* parent=(AliJob*)gROOT->GetListOfTasks()->FindObject(opt)
78 // 4) If selected, the job-specific folder will be created in the generic folder
79 // called "AliJob-folders" as a sub-folder under the same name as the one
80 // introduced in the constructor of the derived top level processor class.
81 // The folder will only be created if the MakeFolder() member function has been
82 // invoked explicitly and when the first object is posted via the AddObject()
83 // or SetMainObject() facilities.
85 // Execution of the (user defined) top level processor has to be invoked via
86 // the memberfunction ExecuteJob() of this AliJob base class.
87 // This will set the default gROOT as the global working directory and then
88 // invoke the (user written) Exec() memberfunction of the top level
89 // processor class with as argument the name of the top level processor instance
90 // as specified by the user in the top level processor constructor.
91 // This will allow stepwise (e.g. event-by-event) execution of the various sub-tasks.
93 // It is the user's responsibility to invoke the sub-tasks via the
94 // ExecuteTasks() statement at the appropriate location in the top level
97 //--- Author: Nick van Eijndhoven 07-may-2005 Utrecht University
98 //- Modified: NvE $Date$ Utrecht University
99 ///////////////////////////////////////////////////////////////////////////
102 #include "Riostream.h"
104 ClassImp(AliJob) // Class implementation to enable ROOT I/O
106 AliJob::AliJob(const char* name,const char* title) : TTask(name,title)
108 // Default constructor.
109 // Initialise the working environment for general data access
110 // by the derived task and its subtasks.
118 // Introduce this AliJob based instance into the ROOT task list
119 TSeqCollection* tasks=gROOT->GetListOfTasks();
120 if (tasks) tasks->Add(this);
122 ///////////////////////////////////////////////////////////////////////////
125 // Default destructor.
126 // The internal array and job specific folder (if any) holding the various
127 // references are deleted.
128 // Note : The objects belonging to the various pointers in the array/folder
129 // and the main processing object are NOT deleted by this base class.
138 TList* list=gROOT->GetListOfBrowsables();
141 TFolder* top=(TFolder*)list->FindObject("AliJob-folders");
142 if (top) RecursiveRemove(fFolder);
153 ///////////////////////////////////////////////////////////////////////////
154 void AliJob::ListEnvironment()
156 // Provide listing of the job environment.
158 cout << " ***" << endl;
159 cout << " *** Environment of job " << GetName() << " ***" << endl;
160 cout << " ***" << endl;
161 cout << " === Available (sub)tasks : " << endl;
165 cout << " === Current job-folder contents : " << endl;
170 ///////////////////////////////////////////////////////////////////////////
171 void AliJob::ExecuteJob()
173 // Invokation of the top level processor via its Exec() memberfunction.
174 // Note : Before execution gROOT is set as the global working directory.
178 ///////////////////////////////////////////////////////////////////////////
179 void AliJob::MakeFolder()
181 // Select creation of the folder structure in addition to the internal
182 // array storage of objects.
185 ///////////////////////////////////////////////////////////////////////////
186 TFolder* AliJob::GetFolder() const
188 // Provide pointer to the whiteboard folder.
191 ///////////////////////////////////////////////////////////////////////////
192 TObject* AliJob::GetMainObject() const
194 // Provide pointer to the main object structure.
197 ///////////////////////////////////////////////////////////////////////////
198 void AliJob::SetMainObject(TObject* obj)
200 // Store pointer to the main object structure.
207 ///////////////////////////////////////////////////////////////////////////
208 void AliJob::AddObject(TObject* obj)
210 // Store pointer of specified object in the working environment.
214 if (!fObjects) fObjects=new TObjArray();
216 if (fMakefolder && !fFolder)
218 // Create the top level environment folder for all AliJobs if needed
219 TList* list=gROOT->GetListOfBrowsables();
222 TFolder* top=(TFolder*)list->FindObject("AliJob-folders");
225 top=new TFolder("AliJob-folders","Environment for all AliJob derived tasks");
226 list->Add(top,"AliJob-folders");
228 // Create the task-specific folder as a sub-folder in the top folder
229 fFolder=top->AddFolder(GetName(),GetTitle());
233 // Add object pointer to array and folder if it doesn't already exist
235 for (Int_t i=0; i<fObjects->GetEntries(); i++)
237 if (obj==fObjects->At(i))
246 if (fFolder) fFolder->Add(obj);
249 ///////////////////////////////////////////////////////////////////////////
250 void AliJob::AddObjects(TObjArray* arr)
252 // Store pointers of all the array objects individually in the working environment.
257 for (Int_t i=0; i<arr->GetSize(); i++)
260 if (obj) AddObject(obj);
263 ///////////////////////////////////////////////////////////////////////////
264 void AliJob::RemoveObject(TObject* obj)
266 // Remove pointer of specified object from the working environment.
267 // In case the specified object is also the main object, the main object
268 // pointer will be set to 0 as well.
274 TObject* test=fObjects->Remove(obj);
277 fObjects->Compress();
278 if (test==fMainObject) fMainObject=0;
282 if (fFolder) fFolder->Remove(obj);
284 ///////////////////////////////////////////////////////////////////////////
285 void AliJob::RemoveObjects(const char* classname)
287 // Remove all stored objects inheriting from classname.
288 // In case one of the removed objects is also the main object, the main object
289 // pointer will be set to 0 as well.
291 if (!fObjects) return;
294 for (Int_t i=0; i<fObjects->GetEntries(); i++)
296 TObject* obj=fObjects->At(i);
297 if (obj->InheritsFrom(classname))
299 TObject* test=fObjects->Remove(obj);
303 if (test==fMainObject) fMainObject=0;
305 if (fFolder) fFolder->Remove(obj);
308 if (remove) fObjects->Compress();
310 ///////////////////////////////////////////////////////////////////////////
311 TObject* AliJob::GetObject(Int_t j) const
313 // Provide pointer to j-th stored object.
314 // Note : j=1 indicates the first object.
316 if (!fObjects || j<1) return 0;
320 if (j<=fObjects->GetEntries()) obj=fObjects->At(j-1);
323 ///////////////////////////////////////////////////////////////////////////
324 TObject* AliJob::GetObject(const char* classname) const
326 // Provide pointer to the first stored object which inherits from classname.
328 if (!fObjects) return 0;
331 for (Int_t i=0; i<fObjects->GetEntries(); i++)
333 TObject* obx=fObjects->At(i);
334 if (obx->InheritsFrom(classname))
342 ///////////////////////////////////////////////////////////////////////////
343 TObjArray* AliJob::GetObjects() const
345 // Provide pointers of all the stored objects.
348 ///////////////////////////////////////////////////////////////////////////
349 TObjArray* AliJob::GetObjects(const char* classname)
351 // Provide pointers to all stored objects inheriting from classname.
353 if (!fObjects) return 0;
361 fSelect=new TObjArray();
364 for (Int_t i=0; i<fObjects->GetEntries(); i++)
366 TObject* obj=fObjects->At(i);
367 if (obj->InheritsFrom(classname)) fSelect->Add(obj);
371 ///////////////////////////////////////////////////////////////////////////