Removing the tasks from the digitization (Ruben)
[u/mrichter/AliRoot.git] / STEER / STEER / AliConfig.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 //  Description:
19 //  This class is responsible for creating folder structure
20 //  All stuff of aliroot sits in one folder with name defined by
21 //  fgkTopFolderName data wich do not very trough event to event are
22 //  sitting in directly in "top folder" all data which changes from
23 //  event to event are sitting in one folder (which has more subfolders)
24 //  Idea is to have more than one event in folder structure which allows
25 //  usage of standard procedures in merging
26 //  Add(AliDetector*) calls Add(AliModule*) as AliDetector is a AliModule
27 //  as well and should be listed in module list
28
29 #include <TDatabasePDG.h>
30 #include <TFolder.h>
31 #include <TInterpreter.h>
32 #include <TObjString.h>
33 #include <TROOT.h>
34 #include <TString.h>
35 #include <TSystem.h>
36 #include <TVirtualMC.h>
37
38 #include "AliConfig.h"
39 #include "AliDetector.h"
40 #include "AliGenerator.h" 
41 #include "AliLoader.h"
42 #include "AliLog.h"
43
44 enum
45  {
46    kDetFolderData = 0,
47    kDetFolderCalibration,
48    kDetFolderAligmnet,
49    kDetFolderLast
50  };
51 ClassImp(AliConfig)
52
53 AliConfig* AliConfig::fgInstance = 0;
54
55 //0 level folder
56 const TString AliConfig::fgkTopFolderName("Folders");
57
58 //1st level folder
59 const TString AliConfig::fgkConstantsFolderName("Constants");
60 const TString AliConfig::fgkDefaultEventFolderName("Event");  //default folder for event, always used except merging
61
62 //2st level folder
63 //subfolder of event folder
64 const TString AliConfig::fgkDataFolderName("Data");//folder for data (hits, digits, points, tracks) grouped by detectors
65 const TString AliConfig::fgkModuleFolderName("Modules");//folder with modules objects
66 const TString AliConfig::fgkConditionsFolderName("Conditions");//folder with conditions (mag. field etc.)
67 const TString AliConfig::fgkConfigurationFolderName("Configuration");//folder with configuration (setup) of the detector
68 const TString AliConfig::fgkHeaderFolderName("Header");//folder with header and other MC information
69
70 //3rd level folder
71 //fgkConditionsFolderName subfolders
72 const TString AliConfig::fgkCalibrationFolderName("Calibration");
73 const TString AliConfig::fgkAligmentFolderName("Aligment");
74   
75 //3rd level folder
76 //fgkConfigurationFolderName subfolders
77 const TString AliConfig::fgkFieldFolderName("Field");
78 const TString AliConfig::fgkGeneratorsFolderName("Generators");
79 const TString AliConfig::fgkVirtualMCFolderName("VirtualMC");
80
81
82 const TString AliConfig::fgkPDGFolderName("Constants/DatabasePDG");//folder with PDG Database
83 const TString AliConfig::fgkGeneratorFolderName("Configuration/Generators");//folder with generators
84 const TString AliConfig::fgkMCFolderName("Configuration/VirtualMC");
85
86 //____________________________________________________________________________
87 AliConfig* AliConfig::Instance ()
88 {
89   //
90   // Instance method for singleton class
91   //
92    if(fgInstance == 0) 
93     {
94      fgInstance = new AliConfig (fgkTopFolderName,"Alice data exchange board");
95     }
96    return fgInstance;
97 }
98 //____________________________________________________________________________
99
100 AliConfig::AliConfig(const char *name, const char *title): 
101   TNamed(name,title), 
102   fTopFolder(gROOT->GetRootFolder()->AddFolder(name,title)),
103   fConstFolder(0x0),
104   fDetectorFolder(new TString[kDetFolderLast+1])
105 {
106 // Constructor
107
108   //Main AliRoot Folder
109   if (fTopFolder == 0x0)
110    {
111      AliFatal("Can not create Top Alice Folder.");
112      return;//never reached
113    }
114   fTopFolder->SetOwner();
115   
116   fDetectorFolder[kDetFolderData] = fgkDataFolderName;
117   fDetectorFolder[kDetFolderCalibration] = fgkConditionsFolderName+"/"+fgkCalibrationFolderName;
118   fDetectorFolder[kDetFolderAligmnet] = fgkConditionsFolderName+"/"+fgkAligmentFolderName;
119   fDetectorFolder[kDetFolderLast] = "";
120   
121   gROOT->GetListOfBrowsables()->Add(fTopFolder, name);
122
123   //Constants folder
124   fConstFolder = fTopFolder->AddFolder (fgkConstantsFolderName, "Constant parameters");
125   fConstFolder->AddFolder("DatabasePDG", "PDG database");
126   
127   fgInstance=this;
128 }
129
130 //____________________________________________________________________________
131 AliConfig::~AliConfig()
132
133   // destructor
134   delete [] fDetectorFolder ;  
135   if (fTopFolder)
136    {
137     fTopFolder->SetOwner();
138     delete fTopFolder; 
139    }
140 }
141 //____________________________________________________________________________
142
143 void AliConfig::AddInFolder (const char *dir, TObject *obj)
144 {
145   // Adds object "obj" to folder "dir"
146   TFolder *folder = dynamic_cast<TFolder *>(fTopFolder->FindObject(dir));
147   if (folder)
148     folder->Add (static_cast<TObject *>(obj));
149 }
150
151 //____________________________________________________________________________
152 TObject* AliConfig::FindInFolder (const char *dir, const char *name)
153 {
154   // Finds object with name "name" in folder "dir"
155   if(!name) return(fTopFolder->FindObject(name));
156   TFolder * folder = dynamic_cast<TFolder *>(fTopFolder->FindObject(dir));
157   if (!folder) return (NULL);
158   return(folder->FindObject(name));
159 }
160
161 //____________________________________________________________________________
162 void    AliConfig::Add (AliGenerator * obj,const char* eventfolder)
163 {
164   // Adds generator "obj" to the event folder "eventfolder"
165   TString path(eventfolder);
166   path = path + "/" + fgkGeneratorsFolderName;
167   AddInFolder(path,obj);
168 }
169
170 //____________________________________________________________________________
171 void AliConfig::Add (TVirtualMC * obj,const char* eventfolder)
172 {
173   // Adds TVirtualMC object to the event folder
174   TString path(eventfolder);
175   path = path + "/" + fgkMCFolderName;
176   AddInFolder(path, obj);
177 }
178
179 //____________________________________________________________________________
180 void  AliConfig::Add (TDatabasePDG * obj)
181 {
182   // Adds TDataBase object
183   AddInFolder(fgkPDGFolderName, obj);
184 }
185
186 //____________________________________________________________________________
187 void AliConfig::Add(AliModule* obj,const char* eventfolder)
188 {
189   // Adds module to the event folder
190   TString path(eventfolder);
191   path = path + "/" + fgkModuleFolderName;
192   AliDebug(1, Form("module name = %s, Ev. Fold. Name is %s.",
193                    obj->GetName(),eventfolder));
194   AddInFolder(path, obj);
195 }
196 //____________________________________________________________________________
197
198 Int_t AliConfig::AddDetector(TFolder* evntfolder, const char *name, const char* title)
199 {
200 //creates folders for the detector 'name'
201  Int_t retval;//returned value
202  retval = CreateDetectorFolders(evntfolder,name,title);
203  if (retval)
204   {
205     AliError(Form("CreateDetectorFolders returned error for detector %s",name));
206     return retval;
207   }
208  return 0; 
209 }
210 //____________________________________________________________________________
211
212 Int_t AliConfig::AddDetector(const char* evntfoldername,const char *name, const char* title)
213 {
214 //creates folders for the detector 'name'
215  Int_t retval;//returned value
216  retval = CreateDetectorFolders(evntfoldername,name,title);
217  if (retval)
218   {
219     AliError(Form("CreateDetectorFolders returned error for detector %s",name));
220     return retval;
221   }
222  return 0; 
223 }
224 //____________________________________________________________________________
225
226 void  AliConfig::Add(AliDetector * obj,const char* eventfolder)
227 {
228   // Adds new AliDetector objest to the correspondent event folder
229   AliDebug(1, Form("detector name = %s, Ev. Fold. Name is %s.",
230                    obj->GetName(),eventfolder));
231
232   TObject* foundobj = GetTopFolder()->FindObject(eventfolder);
233   TFolder* evfolder = (foundobj)?dynamic_cast<TFolder*>(foundobj):0x0;
234   if (evfolder == 0x0)
235    {
236      AliFatal(Form("Can not find folder %s while adding detector %s",eventfolder,obj->GetName()));
237      return;
238    } 
239   CreateDetectorFolders(evfolder, obj->GetName(), obj->GetTitle());
240   
241 }
242 //____________________________________________________________________________
243
244 Int_t  AliConfig::CreateDetectorFolders(const char* evntfoldername,const char *name, const char* title)
245 {
246 //creates a folders for detector named 'name' and titled 'title'
247 //in a event folder named 'evntfoldername'
248 //list of folder names where new folders are created is defined in fDetectorFolder array 
249 //detector folders are named 'name' and titled 'title' as well
250
251  TFolder* evfolder = dynamic_cast<TFolder*>(GetTopFolder()->FindObject(evntfoldername));
252  if (evfolder == 0x0)
253   {
254    AliError(Form("Can not find folder %s while adding detector %s",evntfoldername,name));
255    return 1;
256   }
257  return CreateDetectorFolders(evfolder,name,title);
258 }
259 //____________________________________________________________________________
260 Int_t  AliConfig::CreateDetectorFolders(TFolder* evntfolder,const char *name, const char* title)
261 {
262 //creates a folders for detector named 'name' and titled 'title'
263 //in a event folder 'evntfolder'
264 //list of folder names where new folders are created is defined in fDetectorFolder array 
265 //detector folders are named 'name' and titled 'title' as well
266 //Here we add only detector not an modules
267  
268  Int_t tmp;
269  Int_t i = 0;//iterator
270  while(!fDetectorFolder[i].IsNull())
271   {
272     tmp = AddSubFolder(evntfolder,fDetectorFolder[i],name,title);
273     if (tmp)
274      {
275       AliError(Form("Failed to create subfolder of %s for detector %s",fDetectorFolder[i].Data(),name));
276       return 1;
277      }
278     i++;
279   }
280  return 0;
281 }
282
283 /*****************************************************************************/
284
285 TFolder* AliConfig::BuildEventFolder(const char* name,const char* title)
286 {
287 /*
288  creates the folder structure for one event
289  TopFolder
290          |_
291          | \
292          |  Constants
293          |_
294          | \
295          |  Event_
296          |      | \
297          |      |  Modules(detector objects)
298          |      |_
299          |      | \              
300          |      |  Header
301          |      |_
302          |      | \              
303          |      |  Data_
304          |      |     | \ 
305          |      |     |  TPC_
306          |      |     |    | \
307          |      |     |    |  Hits(object;e.g. tree)
308          |      |     |    |_  
309          |      |     |    | \ 
310          |      |     |    |  SDigits(object)
311          |      |     |    |_
312          |      |     |    | \ 
313          |      |     |    |  Digits(object)
314          |      |     |    |_
315          |      |     |    | \ 
316          |      |     |    |  RecPoints(object)
317          |      |     |    |_
318          |      |     |      \ 
319          |      |     |       Tracks(object)
320          |      |     |_ 
321          |      |       \
322          |      |        ITS_
323          |      |          | \
324          |      |          |  Hits(object;e.g. tree)
325          |      |          |_  
326          |      |          | \ 
327          |      |          |  SDigits(object)
328          |      |          |_
329          |      |          | \ 
330          |      |          |  Digits(object)
331          |      |          |_
332          |      |          | \ 
333          |      |          |  RecPoints(object)
334          |      |          |_
335          |      |            \ 
336          |      |             Tracks(object)
337          |      |_         
338          |        \       
339          |         Configuration
340          |               
341          |_
342            \
343             Event2_  (to be merged with event)
344                 |  \
345                 |   Modules(detector objects)
346                 |_
347                 | \              
348                 |  Header
349                 |_
350                 | \              
351                 |  Data_
352                 |     | \ 
353                 |     |  TPC_
354                 |     |    | \
355                 |     |    |  Hits(object;e.g. tree)
356                 |     |    |_  
357                 |     |    | \ 
358                 |     |    |  SDigits(object)
359                 |     |    |_
360                 |     |    | \ 
361                 |     |    |  Digits(object)
362                 |     |    |_
363                 |     |    | \ 
364                 |     |    |  RecPoints(object)
365                 |     |    |_
366                 |     |      \ 
367                 |     |       Tracks(object)
368                 |     |_ 
369                 |       \
370                 |        ITS_
371                 |          | \
372                 |          |  Hits(object;e.g. tree)
373                 |          |_  
374                 |          | \ 
375                 |          |  SDigits(object)
376                 |          |_
377                 |          | \ 
378                 |          |  Digits(object)
379                 |          |_
380                 |          | \ 
381                 |          |  RecPoints(object)
382                 |          |_
383                 |            \ 
384                 |             Tracks(object)
385                 |_         
386                   \       
387                    Configuration
388                          
389 */
390   TFolder* eventfolder = fTopFolder->AddFolder(name,title); 
391    
392   //modules
393   eventfolder->AddFolder(fgkModuleFolderName, "Detector objects");
394   //event data
395   eventfolder->AddFolder(fgkDataFolderName, "Detector data");
396   
397     //Conditions
398   TFolder *conditions = eventfolder->AddFolder(fgkConditionsFolderName, "Run conditions");
399   conditions->AddFolder(fgkCalibrationFolderName,"Detector calibration data");
400   conditions->AddFolder(fgkAligmentFolderName,"Detector aligment");
401   //Configuration
402   TFolder *configuration = eventfolder->AddFolder(fgkConfigurationFolderName, "Run configuration");
403   configuration->AddFolder(fgkFieldFolderName, "Magnetic field maps");
404   configuration->AddFolder(fgkGeneratorsFolderName,"list of generator objects");
405   //PH configuration->AddFolder(fgkVirtualMCFolderName,"the Virtual MC");
406
407   eventfolder->AddFolder(fgkHeaderFolderName,"MonteCarlo event header");
408
409   eventfolder->SetOwner();
410
411   return eventfolder;
412 }
413
414 /*****************************************************************************/
415
416 const TString& AliConfig::GetDataFolderName() const
417 {
418 //returns name of data folder path relative to event folder
419  return fgkDataFolderName;
420 }
421
422 /*****************************************************************************/
423
424 Int_t AliConfig::AddSubFolder(TFolder* topfolder, const char* infoler, 
425                      const char* newfoldname, const char* newfoldtitle)
426 {
427 //helper method
428 //in topfolder looks for folder named 'infolder'
429 //and if it exist it creates inside a new folder named 'newfoldname' and titled 'newfoldtitle'
430
431  if (topfolder == 0x0)//check if exists top folder
432   {
433    AliError("Parameter TFolder* points to NULL.");
434    return 1;
435   }
436  
437  TObject *obj;
438  TFolder* folder;
439  
440  folder = dynamic_cast<TFolder*>(topfolder->FindObject(infoler));
441  if (folder == 0x0) //check if we got inolder
442   {
443    AliError(Form("Can not find folder %s in folder %s.",infoler,topfolder->GetName()));
444    return 1;
445   }
446  obj = folder->FindObject(newfoldname); //see if such a subfolder already exists
447  if (obj == 0x0) //nope
448   {
449    TFolder *newfolder = folder->AddFolder(newfoldname,newfoldtitle);//add the desired subfolder
450    if (newfolder == 0x0) //check if we managed
451     {
452      AliError(Form("Can not create folder %s in folder %s",newfoldname,infoler));
453      return 2;
454     }
455    else return 0;//success
456   }
457  //such an object already exists
458  TFolder* fol = dynamic_cast<TFolder*>(obj);
459  if (fol == 0x0)
460    {
461      AliError(Form("Object named %s already exists in folder %s AND IT IS NOT A FOLDER",newfoldname,infoler));
462      return 3;
463    }
464  AliWarning(Form("Folder named %s already exists in folder %s",newfoldname,infoler));
465  return 0;
466 }