]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RALICE/AliJob.cxx
08e928f4fa0f9c2502e2e34d91b457adc5d3d3a6
[u/mrichter/AliRoot.git] / RALICE / AliJob.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 // $Id$
17
18 ///////////////////////////////////////////////////////////////////////////
19 // Class AliJob
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.
29 //
30 // The working environment consists of (a.o.) :
31 // * A folder which may serve as a whiteboard for transferring pointers to
32 //   objects which are posted there by the top level processor or any
33 //   of its subtasks.
34 //   Objects can be posted in the job folder via the AddObject() facility
35 //   of this AliJob class.
36 //   Access to the job folder is obtained via the GetFolder() memberfunction
37 //   and from this the various objects can be accessed via the usual TFolder
38 //   FindObject and/or iterator facilities.
39 // * An array containing pointers to the objects which are stored
40 //   via the AddObject() facility of this AliJob class.
41 //   From this storage the objects can be (more directly) accessed via the
42 //   GetObject() and GetObjects() memberfunctions.
43 // * A pointer to the main object structure during job processing.
44 //   This pointer can be initiated/updated only by the derived top level
45 //   processor via the SetMainObject() facility but all sub-tasks can access
46 //   it via the above folder/array facilities or the GetMainObject() memberfunction.
47 //   The latter provides faster access to the main object structure than
48 //   the GetObject() or TFolder search based procedures.
49 //
50 // Notes :
51 // -------
52 // 1) This AliJob class is derived from TTask, which implies that every
53 //    (user defined) top level processor class is itself also a TTask.
54 //    This allows sub-tasks to be introduced to the top level processor
55 //    using the standard TTask facilities.
56 //
57 // 2) Only references to the various introduced objects are stored.
58 //    It is the user's responsibility to delete all introduced objects,
59 //    either in the destructor of the derived top level processor class
60 //    or via Clear() facility as provided by the TTask machinery.
61 //
62 // 3) The top level processor instance is entered into the standard ROOT
63 //    ListOfTasks under the name which was provided by the user in the
64 //    constructor of the top level processor.
65 //    The name of the top level processor is passed automatically as the
66 //    opt argument to the Exec(Option_t* opt) memberfunctions of the 
67 //    various sub-tasks by the ExecuteJob() memberfunction (see below).
68 //    This allows all sub-tasks to obtain the pointer to the top level
69 //    processor instance from its name via the statement :
70 //
71 //      AliJob* parent=(AliJob*)gROOT->GetListOfTasks()->FindObject(opt)
72 //
73 // 4) The job-specific folder will be created in the generic folder called
74 //    "AliJob-folders" as a sub-folder under the same name as the one
75 //    introduced in the constructor of the derived top level processor class.
76 //    The folder will only be created when the first object is posted via
77 //    the AddObject() or SetMainObject() facilities.
78 //
79 // Execution of the (user defined) top level processor has to be invoked via
80 // the memberfunction ExecuteJob() of this AliJob base class.
81 // This will invoke the (user written) Exec() memberfunction of the top level
82 // processor class with as argument the name of the top level processor instance
83 // as specified by the user in the top level processor constructor.
84 // This will allow stepwise (e.g. event-by-event) execution of the various sub-tasks.
85 //
86 // It is the user's responsibility to invoke the sub-tasks via the
87 // ExecuteTasks() statement at the appropriate location in the top level
88 // processor class. 
89 //
90 //--- Author: Nick van Eijndhoven 07-may-2005 Utrecht University
91 //- Modified: NvE $Date$ Utrecht University
92 ///////////////////////////////////////////////////////////////////////////
93  
94 #include "AliJob.h"
95 #include "Riostream.h"
96
97 ClassImp(AliJob) // Class implementation to enable ROOT I/O
98
99 AliJob::AliJob(const char* name,const char* title) : TTask(name,title)
100 {
101 // Default constructor.
102 // Initialise the working environment for general data access
103 // by the derived task and its subtasks.
104
105  fMainObject=0;
106  fFolder=0;
107  fObjects=0;
108  fSelect=0;
109
110  // Introduce this AliJob based instance into the ROOT task list
111  TSeqCollection* tasks=gROOT->GetListOfTasks();
112  if (tasks) tasks->Add(this);
113 }
114 ///////////////////////////////////////////////////////////////////////////
115 AliJob::~AliJob()
116 {
117 // Default destructor.
118 // Note : The objects belonging to the various pointers in the folder
119 //        and the main processing object are NOT deleted by this base class.
120
121  if (fObjects)
122  {
123   delete fObjects;
124   fObjects=0;
125  }
126  if (fFolder)
127  {
128   delete fFolder;
129   fFolder=0;
130  }
131  if (fSelect)
132  {
133   delete fSelect;
134   fSelect=0;
135  }
136 }
137 ///////////////////////////////////////////////////////////////////////////
138 void AliJob::ListEnvironment()
139 {
140 // Provide listing of the job environment. 
141
142  cout << " ***" << endl;
143  cout << " *** Environment of job " << GetName() << " ***" << endl;
144  cout << " ***" << endl;
145  cout << " === Available (sub)tasks : " << endl;
146  ls();
147  cout << " === Current job-folder contents : " << endl;
148  fFolder->ls();
149  cout << endl;
150 }
151 ///////////////////////////////////////////////////////////////////////////
152 void AliJob::ExecuteJob()
153 {
154 // Invokation of the top level processor via its Exec() memberfunction.
155  Exec(GetName());
156 }
157 ///////////////////////////////////////////////////////////////////////////
158 TFolder* AliJob::GetFolder() const
159 {
160 // Provide pointer to the whiteboard folder.
161  return fFolder;
162 }
163 ///////////////////////////////////////////////////////////////////////////
164 TObject* AliJob::GetMainObject() const
165 {
166 // Provide pointer to the main object structure.
167  return fMainObject;
168 }
169 ///////////////////////////////////////////////////////////////////////////
170 void AliJob::SetMainObject(TObject* obj)
171 {
172 // Store pointer to the main object structure.
173  if (obj)
174  {
175   fMainObject=obj;
176   AddObject(obj);
177  }
178 }
179 ///////////////////////////////////////////////////////////////////////////
180 void AliJob::AddObject(TObject* obj)
181 {
182 // Store pointer of specified object in the working environment.
183
184  if (!obj) return;
185
186  if (!fObjects) fObjects=new TObjArray();
187
188  if (!fFolder)
189  {
190   // Create the top level environment folder for all AliJobs if needed 
191   TList* list=gROOT->GetListOfBrowsables();
192   if (list)
193   {
194    TFolder* top=(TFolder*)list->FindObject("AliJob-folders");
195    if (!top)
196    {
197     top=new TFolder("AliJob-folders","Environment for all AliJob derived tasks");
198     list->Add(top,"AliJob-folders");
199    }
200    // Create the task-specific folder as a sub-folder in the top folder 
201    fFolder=top->AddFolder(GetName(),GetTitle());
202   }
203  }
204
205  // Add object pointer to array and folder if it doesn't already exist 
206  Int_t exist=0;
207  for (Int_t i=0; i<fObjects->GetEntries(); i++)
208  {
209   if (obj==fObjects->At(i))
210   {
211    exist=1;
212    break;
213   }
214  }
215  if (!exist)
216  {
217   fObjects->Add(obj);
218   fFolder->Add(obj);
219  }
220 }
221 ///////////////////////////////////////////////////////////////////////////
222 void AliJob::AddObjects(TObjArray* arr)
223 {
224 // Store pointers of all the array objects individually in the working environment.
225
226  if (!arr) return;
227
228  TObject* obj=0;
229  for (Int_t i=0; i<arr->GetSize(); i++)
230  {
231   obj=arr->At(i);
232   if (obj) AddObject(obj);
233  }
234 }
235 ///////////////////////////////////////////////////////////////////////////
236 void AliJob::RemoveObject(TObject* obj)
237 {
238 // Remove pointer of specified object from the working environment.
239
240  if (!obj) return;
241
242  if (fObjects)
243  {
244   TObject* test=fObjects->Remove(obj);
245   if (test) fObjects->Compress();
246  }
247
248  if (fFolder) fFolder->Remove(obj);
249 }
250 ///////////////////////////////////////////////////////////////////////////
251 void AliJob::RemoveObjects(const char* classname)
252 {
253 // Remove all stored objects inheriting from classname.
254
255  if (!fObjects) return;
256
257  Int_t remove=0;
258  for (Int_t i=0; i<fObjects->GetEntries(); i++)
259  {
260   TObject* obj=fObjects->At(i);
261   if (obj->InheritsFrom(classname))
262   {
263    TObject* test=fObjects->Remove(obj);
264    if (test) remove=1;
265    if (fFolder) fFolder->Remove(obj);
266   }
267  }
268  if (remove) fObjects->Compress();
269 }
270 ///////////////////////////////////////////////////////////////////////////
271 TObject* AliJob::GetObject(Int_t j) const
272 {
273 // Provide pointer to j-th stored object.
274 // Note : j=1 indicates the first object.
275
276  if (!fObjects || j<1) return 0;
277
278  TObject* obj=0;
279
280  if (j<=fObjects->GetEntries()) obj=fObjects->At(j-1);
281  return obj;
282 }
283 ///////////////////////////////////////////////////////////////////////////
284 TObject* AliJob::GetObject(const char* classname) const
285 {
286 // Provide pointer to the first stored object which inherits from classname.
287
288  if (!fObjects) return 0;
289
290  TObject* obj=0;
291  for (Int_t i=0; i<fObjects->GetEntries(); i++)
292  {
293   TObject* obx=fObjects->At(i);
294   if (obx->InheritsFrom(classname))
295   {
296    obj=obx;
297    break;
298   }
299  }
300  return obj;
301 }
302 ///////////////////////////////////////////////////////////////////////////
303 TObjArray* AliJob::GetObjects() const
304 {
305 // Provide pointers of all the stored objects.
306  return fObjects;
307 }
308 ///////////////////////////////////////////////////////////////////////////
309 TObjArray* AliJob::GetObjects(const char* classname)
310 {
311 // Provide pointers to all stored objects inheriting from classname.
312
313  if (!fObjects) return 0;
314
315  if (fSelect)
316  {
317   fSelect->Clear();
318  }
319  else
320  {
321   fSelect=new TObjArray();
322  }
323
324  for (Int_t i=0; i<fObjects->GetEntries(); i++)
325  {
326   TObject* obj=fObjects->At(i);
327   if (obj->InheritsFrom(classname)) fSelect->Add(obj);
328  }
329  return fSelect;
330 }
331 ///////////////////////////////////////////////////////////////////////////