]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEER/AliLoader.cxx
CMake: STEER, adding AliConst.h to the installed headers
[u/mrichter/AliRoot.git] / STEER / STEER / AliLoader.cxx
1 //
2 //Class (base) responsible for management of data:
3 //    - opening and closing proper files
4 //    - posting data to folders
5 //    - writing data from folders to proper files
6 //
7 //Author: Alice Offline Group http://alisoft.cern.ch
8 //Responsible: Piotr.Skowronski@cern.ch
9 //
10 #include "AliLoader.h"
11
12 //Root includes
13 #include <TROOT.h>
14 #include <TFile.h>
15 #include <TFolder.h>
16 #include <TString.h>
17
18 //AliRoot includes
19 #include "AliConfig.h"
20 #include "AliDetector.h"
21 #include "AliLog.h"
22 #include "AliRun.h"
23 #include "AliRunLoader.h"
24
25 const TString AliLoader::fgkDefaultHitsContainerName("TreeH");
26 const TString AliLoader::fgkDefaultDigitsContainerName = "TreeD";
27 const TString AliLoader::fgkDefaultSDigitsContainerName = "TreeS";
28 const TString AliLoader::fgkDefaultRecPointsContainerName = "TreeR";
29 const TString AliLoader::fgkDefaultTracksContainerName = "TreeT";
30 const TString AliLoader::fgkDefaultRecParticlesContainerName = "TreeP";
31 const TString AliLoader::fgkLoaderBaseName("Loader");
32
33 ClassImp(AliLoader)
34 //___________________________________________________________________
35 /////////////////////////////////////////////////////////////////////
36 //                                                                 //
37 //  class AliLoader                                                //
38 //                                                                 //
39 //  Base class for Loaders.                                        //
40 //  Loader provides base I/O fascilities for "standard" data.      //
41 //  Each detector has a laoder data member                         //
42 //  loader is accessible via folder structure as well              //
43 //                                                                 //
44 /////////////////////////////////////////////////////////////////////
45  
46 /*****************************************************************************/ 
47
48 AliLoader::AliLoader():
49  fDataLoaders(0x0),
50  fDetectorName(""),
51  fEventFolder(0x0),
52  fDataFolder(0x0),
53  fDetectorDataFolder(0x0),
54  fModuleFolder(0x0)
55 {
56   //default constructor
57   
58 }
59 /******************************************************************/
60
61 AliLoader::AliLoader(const Char_t* detname,const Char_t* eventfoldername):
62  fDataLoaders(new TObjArray(kNDataTypes)),
63  fDetectorName(""),
64  fEventFolder(0x0),
65  fDataFolder(0x0),
66  fDetectorDataFolder(0x0),
67  fModuleFolder(0x0)
68 {
69   //ctor
70    AliDebug(1, Form("detname = %s eventfoldername = %s",detname,eventfoldername));
71
72    //try to find folder eventfoldername in top alice folder
73    //safe because GetTopFolder will abort in case of failure
74
75    fDetectorName = detname;
76    fName = fDetectorName+"Loader";
77    InitDefaults();
78
79    TObject* fobj = GetTopFolder()->FindObject(eventfoldername);
80    fEventFolder = (fobj)?dynamic_cast<TFolder*>(fobj):0x0;//in case FindObject returns NULL dynamic cast cause seg. fault
81    SetEventFolder(fEventFolder);
82    
83  }
84 /*****************************************************************************/ 
85
86 AliLoader::AliLoader(const Char_t * detname,TFolder* eventfolder):
87  fDataLoaders(new TObjArray(kNDataTypes)),
88  fDetectorName(detname),
89  fEventFolder(0x0),
90  fDataFolder(0x0),
91  fDetectorDataFolder(0x0),
92  fModuleFolder(0x0)
93 {
94 //constructor
95    fDetectorName = detname;
96    fName = fDetectorName+"Loader";
97    InitDefaults();
98    SetEventFolder(eventfolder);
99    //fileoption's don't need to initialized because default TString ctor does it correctly
100 }
101
102 /*****************************************************************************/ 
103 AliLoader::~AliLoader()
104 {
105 //detructor
106   if (fDataLoaders) fDataLoaders->SetOwner();
107   delete fDataLoaders;
108 }
109 /*****************************************************************************/ 
110
111 void AliLoader::InitDefaults()
112 {
113   // H I T S 
114   AliDataLoader* dl;
115   dl = new AliDataLoader(fDetectorName + ".Hits.root",fgkDefaultHitsContainerName, "Hits" );
116   fDataLoaders->AddAt(dl,kHits);
117   
118   
119   // S U M M A B L E   D I G I T S
120   dl = new AliDataLoader(fDetectorName + ".SDigits.root",fgkDefaultSDigitsContainerName, "Summable Digits");
121   fDataLoaders->AddAt(dl,kSDigits);
122
123   // D I G I T S  
124   dl = new AliDataLoader(fDetectorName + ".Digits.root",fgkDefaultDigitsContainerName, "Digits");
125   fDataLoaders->AddAt(dl,kDigits);
126   
127   // R E C O N S T R U C T E D   P O I N T S aka C L U S T E R S 
128   dl = new AliDataLoader(fDetectorName + ".RecPoints.root",fgkDefaultRecPointsContainerName, "Reconstructed Points");
129   fDataLoaders->AddAt(dl,kRecPoints);
130   
131   // T R A C K S
132   dl = new AliDataLoader(fDetectorName + ".Tracks.root",fgkDefaultTracksContainerName, "Tracks");
133   fDataLoaders->AddAt(dl,kTracks);
134   
135   // R E C O N S T R U C T E D   P O I N T S aka C L U S T E R S 
136   dl = new AliDataLoader(fDetectorName + ".RecParticles.root",fgkDefaultRecParticlesContainerName, "Reconstructed Particles");
137   fDataLoaders->AddAt(dl,kRecParticles);
138
139  }
140 /*****************************************************************************/ 
141
142 AliDataLoader* AliLoader::GetDataLoader(const char* name)
143 {
144 //returns Data Loader with specified name
145   return dynamic_cast<AliDataLoader*>(fDataLoaders->FindObject(name));
146 }
147 /*****************************************************************************/ 
148 void AliLoader::AddDataLoader(AliDataLoader* dl)
149 {
150   //
151   // Adds a data loader
152   //
153   if (dl == 0x0)
154    {
155      AliError("Pointer is NULL");
156      return;
157    }
158   if (fDataLoaders->FindObject(dl->GetName()))
159    {
160      AliError("Such a loader exists");
161      return;
162    }
163   fDataLoaders->AddLast(dl);
164   dl->SetEventFolder(fEventFolder);
165   dl->SetFolder(GetDetectorDataFolder()); //Must exists - ensure register is called before
166 }
167 /*****************************************************************************/ 
168
169 Int_t AliLoader::SetEvent()
170 {
171  //basically the same that GetEvent but do not post data to folders
172  TIter next(fDataLoaders);
173  AliDataLoader* dl;
174  while ((dl = (AliDataLoader*)next()))
175   {
176     dl->SetEvent();
177   }
178  return 0;
179 }
180 /******************************************************************/
181
182 void AliLoader::UnloadAll()
183 {
184  //calls UnloadAll for all base laoders
185  //Unloads everything
186  TIter next(fDataLoaders);
187  AliDataLoader* dl;
188  while ((dl = (AliDataLoader*)next()))
189   {
190     dl->UnloadAll();
191   }
192 }
193 /******************************************************************/
194
195 Int_t AliLoader::GetEvent()
196 {
197  //changes to proper root  directory and tries to load data from files to folders
198  // event number is defined in RunLoader
199  // 
200  //returns:
201  //     0  - in case of no error
202  //     1  - event not found
203  //     
204  
205  Int_t retval;   
206  TIter next(fDataLoaders);
207  AliDataLoader* dl;
208  while ((dl = (AliDataLoader*)next()))
209   {
210     retval = dl->GetEvent();
211     if (retval)
212      {
213        AliError(Form("Error occured while GetEvent for %s",dl->GetName()));
214        return retval;
215      }
216   }
217
218  return 0;
219 }
220
221 /******************************************************************/
222
223 TFolder* AliLoader::GetTopFolder()
224 {
225 //returns TOP aliroot folder, just a simple interface to AliConfig (gives shorter notation)
226  return AliConfig::Instance()->GetTopFolder();
227 }
228
229 /******************************************************************/
230
231 TFolder* AliLoader::GetEventFolder()
232 {
233 //get EVENT folder (data that are changing from event to event, even in single run)
234   return fEventFolder;
235 }
236 /******************************************************************/
237 TFolder* AliLoader::GetDataFolder()
238 {
239 //returns the folder speciofic to given detector e.g. /Folders/Event/Data/
240  if (!fDataFolder)
241   {
242    fDataFolder =  dynamic_cast<TFolder*>(GetEventFolder()->FindObject(AliConfig::Instance()->GetDataFolderName()));
243    
244    if (!fDataFolder)
245     {
246      AliFatal("Can not find AliRoot data folder. Aborting");
247      return 0x0;
248     }
249   }
250   return fDataFolder;
251 }
252
253 /*****************************************************************************/ 
254
255 TFolder* AliLoader::GetModulesFolder()
256 {
257   //returns pointer to the folder containing modules
258  if (!fModuleFolder)
259   {
260    fModuleFolder =  dynamic_cast<TFolder*>(GetEventFolder()->FindObjectAny(AliConfig::GetModulesFolderName()));
261    
262    if (!fModuleFolder)
263     {
264      AliFatal("Can not find modules folder. Aborting");
265      return 0x0;
266     }
267   }
268  return fModuleFolder;
269    
270 }
271
272 /*****************************************************************************/ 
273
274 TDirectory* AliLoader::ChangeDir(TFile* file, Int_t eventno)
275 {
276 //changes the root directory in "file" to "dirname" which corresponds to event 'eventno'
277 //in case of success returns the pointer to directory
278 //else NULL
279  
280  if (file == 0x0)
281   {
282     AliErrorClass("File is null");
283     return 0x0;
284   }
285  if (file->IsOpen() == kFALSE)
286   {
287     AliErrorClass("File is not opened");
288     return 0x0;
289   }
290
291  TString dirname("Event");
292  dirname+=eventno;
293  AliDebugClass(1, Form("Changing Dir to %s in file %s.",dirname.Data(),file->GetName()));
294
295  Bool_t result;
296  
297  TDirectory* dir = dynamic_cast<TDirectory*>(file->Get(dirname));
298
299  if (dir == 0x0)
300   {
301     AliDebugClass(1, Form("Can not find directory %s in file %s, creating...",
302                      dirname.Data(),file->GetName()));
303     
304     if (file->IsWritable() == kFALSE)
305      {
306        AliErrorClass(Form("Can not create directory. File %s in not writable.",
307                      file->GetName()));
308        return 0x0;
309      }
310             
311     TDirectory* newdir = file->mkdir(dirname);
312     if (newdir == 0x0)
313      {
314        AliErrorClass(Form("Failed to create new directory in file %s.",
315                      file->GetName()));
316        return 0x0;
317      }
318     result = file->cd(dirname);
319     if (result == kFALSE)
320      {
321        return 0x0;
322      }
323   }
324  else
325   {
326    file->cd();//make a file active 
327    file->cd(dirname);//cd event dir
328   }
329
330  return gDirectory;
331 }
332 /*****************************************************************************/ 
333
334 TString AliLoader::GetUnixDir() const
335  {
336  //This Method will manage jumping through unix directories in case of 
337  //run with more events per run than defined in gAlice
338  
339    TString dir;
340    
341    return dir;
342  }
343 /*****************************************************************************/ 
344 /************************************************************/
345
346 void AliLoader::MakeTree(Option_t *option)
347  {
348 //Makes a tree depending on option 
349 //   H: - Hits
350 //   D: - Digits
351 //   S: - Summable Digits
352 //   R: - Reconstructed Points (clusters)
353 //   T: - Tracks (tracklets)
354
355   const char *oH = strstr(option,"H");
356   const char *oD = strstr(option,"D");
357   const char *oS = strstr(option,"S");
358   const char *oR = strstr(option,"R");
359   const char *oT = strstr(option,"T");
360   const char *oP = strstr(option,"P");
361   
362   if (oH) MakeHitsContainer();
363   if (oD) MakeDigitsContainer();
364   if (oS) MakeSDigitsContainer();
365   if (oR) MakeRecPointsContainer();
366   if (oT) MakeTracksContainer();
367   if (oP) MakeRecParticlesContainer();
368  }
369
370 /*****************************************************************************/ 
371 Int_t  AliLoader::WriteHits(Option_t* opt) const
372  {
373    // Writes hits
374    AliDataLoader* dl = GetHitsDataLoader();
375    Int_t ret = dl->WriteData(opt);
376    return ret;
377  }
378 /*****************************************************************************/ 
379
380 Int_t AliLoader::WriteSDigits(Option_t* opt) const
381  {
382    // Writes summable digits
383    AliDataLoader* dl = GetSDigitsDataLoader();
384    Int_t ret = dl->WriteData(opt);
385    return ret;
386  }
387  
388 /*****************************************************************************/ 
389
390 TObject** AliLoader::GetDetectorDataRef(TObject *obj)
391 {
392   // Returns pointer to an entry in the list of folders pointing to "obj"
393  if (obj == 0x0)
394   {
395     return 0x0;
396   }
397  return GetDetectorDataFolder()->GetListOfFolders()->GetObjectRef(obj) ;
398 }
399
400 /*****************************************************************************/ 
401 void AliLoader::CleanFolders()
402  {
403  //Cleans all posted objects == removes from folders and deletes them
404    TIter next(fDataLoaders);
405    AliDataLoader* dl;
406    while ((dl = (AliDataLoader*)next()))
407     { 
408       AliDebug(1, Form("name = %s cleaning",dl->GetName()));
409       dl->Clean();
410     }
411  }
412
413 /*****************************************************************************/ 
414
415 Int_t AliLoader::ReloadAll()
416 {
417   // Calling Reload function for all the data loaders
418  TIter next(fDataLoaders);
419  AliDataLoader* dl;
420  
421  while((dl = (AliDataLoader*)next()))
422   {
423    Int_t err = dl->Reload();
424    if (err)
425     {
426       AliError(Form("Reload returned error for %s",dl->GetName()));
427       return err;
428     }
429   }
430  return 0;
431 }
432 /*****************************************************************************/ 
433
434 void AliLoader::CloseFiles()
435 {
436 //close files for data loaders
437  TIter next(fDataLoaders);
438  AliDataLoader* dl;
439  while((dl = (AliDataLoader*)next()))
440   {
441    dl->CloseFile();
442   }
443 }
444 /*****************************************************************************/ 
445
446 Int_t  AliLoader::SetEventFolder(TFolder* eventfolder)
447 {
448   //sets the event folder
449  if (eventfolder == 0x0)
450   {
451     AliError("Stupid joke. Argument is NULL");
452     return 1;
453   }
454
455  fEventFolder = eventfolder;
456  TIter next(fDataLoaders);
457  AliDataLoader* dl;
458  
459  while((dl = (AliDataLoader*)next()))
460   {
461     dl->SetEventFolder(fEventFolder);
462     dl->SetFolder(GetDetectorDataFolder()); //Must exists - ensure register is called before
463   }
464
465  return 0;
466 }//sets the event folder
467 /*****************************************************************************/ 
468
469 Int_t AliLoader::Register(TFolder* eventFolder)
470 {
471 //triggers creation of subfolders for a given detector
472 //this method is called when session is read from disk
473 //
474 //warning: AliDetector in constructor (not default) calls
475 //creation of folder structure as well (some detectors needs folders 
476 //alrady in constructors)
477
478  AliDebug(1, Form("Name is %s.",GetName()));
479  if (eventFolder == 0x0)
480   {
481     AliError("Event folder is not set.");
482     return 1;
483   }
484  Int_t retval = AliConfig::Instance()->AddDetector(eventFolder,fDetectorName,fDetectorName);
485  if(retval)
486   {
487     AliError(Form("Can not create folders for %s. Event folder name is %s",
488                   fDetectorName.Data(),eventFolder->GetName()));
489     return retval;
490   }
491  SetEventFolder(eventFolder);
492  return 0;
493 }
494 /*****************************************************************************/ 
495 AliRunLoader* AliLoader::GetRunLoader()
496 {
497 //gets the run-loader from event folder
498   AliRunLoader* rg = 0x0;
499   TObject * obj = GetEventFolder()->FindObject(AliRunLoader::GetRunLoaderName());
500   if (obj) rg = dynamic_cast<AliRunLoader*>(obj);
501   return rg;
502 }
503 /*****************************************************************************/ 
504 Bool_t  AliLoader::TestFileOption(Option_t* opt)
505 {
506 //tests the TFile::Option
507 //if file is truncated at opening moment ("recreate", "new" or "create") returns kFALSE;
508 //else kTRUE (means opened with "read" or "update" mode)
509   TString option(opt);
510   if (option.CompareTo("recreate",TString::kIgnoreCase) == 0) return kFALSE;
511   if (option.CompareTo("new",TString::kIgnoreCase) == 0) return kFALSE;
512   if (option.CompareTo("create",TString::kIgnoreCase) == 0) return kFALSE;
513   return kTRUE;
514 }
515 /*****************************************************************************/ 
516 void  AliLoader::SetDirName(TString& dirname)
517 {
518 //adds "dirname" to each file. Dirname should end with "#","/", or ":" 
519   TIter next(fDataLoaders);
520   AliDataLoader* dl;
521   while((dl = (AliDataLoader*)next()))
522    {
523     dl->SetDirName(dirname);
524    }
525 }
526
527 /*****************************************************************************/ 
528
529 void AliLoader::SetDigitsFileNameSuffix(const TString& suffix) const
530 {
531   //adds the suffix before ".root", 
532   //e.g. TPC.Digits.root -> TPC.DigitsMerged.root
533   //made on Jiri Chudoba demand
534   GetDigitsDataLoader()->SetFileNameSuffix(suffix);
535 }
536 /*****************************************************************************/ 
537
538 void AliLoader::SetCompressionLevel(Int_t cl)
539 {
540 //sets comression level for data defined by di
541   TIter next(fDataLoaders);
542   AliDataLoader* dl;
543   while((dl = (AliDataLoader*)next()))
544    {
545      dl->SetCompressionLevel(cl);
546    }
547 }
548 /*****************************************************************************/ 
549
550 void AliLoader::Clean()
551 {
552 //Cleans all data loaders
553   TIter next(fDataLoaders);
554   AliDataLoader* dl;
555   while((dl = (AliDataLoader*)next()))
556    {
557      dl->Clean();
558    }
559 }
560 /*****************************************************************************/
561
562 void AliLoader::Clean(const TString& name)
563 {
564   // Removes object with "name" from the detector's data folder
565   // and from the memory
566   TObject* obj = GetDetectorDataFolder()->FindObject(name);
567   if(obj)
568    {
569      AliDebug(1, Form("name=%s, cleaning %s.",GetName(),name.Data()));
570      GetDetectorDataFolder()->Remove(obj);
571      delete obj;
572    }
573 }
574
575 /*****************************************************************************/ 
576
577 Bool_t AliLoader::IsOptionWritable(const TString& opt)
578 {
579   // Returns "true" if the option means also "writable"
580   if (opt.CompareTo("recreate",TString::kIgnoreCase)) return kTRUE;
581   if (opt.CompareTo("new",TString::kIgnoreCase)) return kTRUE;
582   if (opt.CompareTo("create",TString::kIgnoreCase)) return kTRUE;
583   if (opt.CompareTo("update",TString::kIgnoreCase)) return kTRUE;
584   return kFALSE;
585 }
586
587 /*****************************************************************************/ 
588
589 void AliLoader::SetDebug(Int_t deb)
590 {
591   // Sets debug level
592   AliLog::SetClassDebugLevel("AliRunLoader", deb);
593   AliLog::SetClassDebugLevel("AliLoader", deb);
594   AliLog::SetClassDebugLevel("AliDataLoader", deb);
595   AliLog::SetClassDebugLevel("AliBaseLoader", deb);
596   AliLog::SetClassDebugLevel("AliObjectLoader", deb);
597   AliLog::SetClassDebugLevel("AliTreeLoader", deb);
598   AliLog::SetClassDebugLevel("AliConfig", deb);
599 }
600 /*****************************************************************************/ 
601
602 void AliLoader::SetTAddrInDet()
603 {
604   //calls SetTreeAddress for corresponding detector
605   AliRunLoader* rl = GetRunLoader();   
606   if (rl == 0x0) return;
607   AliRun* ar = rl->GetAliRun();
608   if (ar == 0x0) return;
609   AliDetector* det = ar->GetDetector(fDetectorName);  
610   if (det == 0x0) return;
611   det->SetTreeAddress();
612 }
613 /*****************************************************************************/ 
614
615 void AliLoader::Synchronize()
616 {
617   //synchrinizes all writtable files 
618  TIter next(fDataLoaders);
619  AliDataLoader* dl;
620  while ((dl = (AliDataLoader*)next()))
621   {
622     dl->Synchronize();
623   }
624   
625 }
626 /*****************************************************************************/ 
627 /*****************************************************************************/ 
628 /*****************************************************************************/ 
629