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
7 //Author: Alice Offline Group http://alisoft.cern.ch
8 //Responsible: Piotr.Skowronski@cern.ch
10 #include "AliLoader.h"
19 #include "AliConfig.h"
20 #include "AliDetector.h"
23 #include "AliRunLoader.h"
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");
34 //___________________________________________________________________
35 /////////////////////////////////////////////////////////////////////
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 //
44 /////////////////////////////////////////////////////////////////////
46 /*****************************************************************************/
48 AliLoader::AliLoader():
53 fDetectorDataFolder(0x0),
59 /******************************************************************/
61 AliLoader::AliLoader(const Char_t* detname,const Char_t* eventfoldername):
62 fDataLoaders(new TObjArray(kNDataTypes)),
66 fDetectorDataFolder(0x0),
70 AliDebug(1, Form("detname = %s eventfoldername = %s",detname,eventfoldername));
72 //try to find folder eventfoldername in top alice folder
73 //safe because GetTopFolder will abort in case of failure
75 fDetectorName = detname;
76 fName = fDetectorName+"Loader";
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);
84 /*****************************************************************************/
86 AliLoader::AliLoader(const Char_t * detname,TFolder* eventfolder):
87 fDataLoaders(new TObjArray(kNDataTypes)),
88 fDetectorName(detname),
91 fDetectorDataFolder(0x0),
95 fDetectorName = detname;
96 fName = fDetectorName+"Loader";
98 SetEventFolder(eventfolder);
99 //fileoption's don't need to initialized because default TString ctor does it correctly
102 /*****************************************************************************/
103 AliLoader::~AliLoader()
106 if (fDataLoaders) fDataLoaders->SetOwner();
109 /*****************************************************************************/
111 void AliLoader::InitDefaults()
115 dl = new AliDataLoader(fDetectorName + ".Hits.root",fgkDefaultHitsContainerName, "Hits" );
116 fDataLoaders->AddAt(dl,kHits);
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);
124 dl = new AliDataLoader(fDetectorName + ".Digits.root",fgkDefaultDigitsContainerName, "Digits");
125 fDataLoaders->AddAt(dl,kDigits);
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);
132 dl = new AliDataLoader(fDetectorName + ".Tracks.root",fgkDefaultTracksContainerName, "Tracks");
133 fDataLoaders->AddAt(dl,kTracks);
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);
140 /*****************************************************************************/
142 AliDataLoader* AliLoader::GetDataLoader(const char* name)
144 //returns Data Loader with specified name
145 return dynamic_cast<AliDataLoader*>(fDataLoaders->FindObject(name));
147 /*****************************************************************************/
148 void AliLoader::AddDataLoader(AliDataLoader* dl)
151 // Adds a data loader
155 AliError("Pointer is NULL");
158 if (fDataLoaders->FindObject(dl->GetName()))
160 AliError("Such a loader exists");
163 fDataLoaders->AddLast(dl);
164 dl->SetEventFolder(fEventFolder);
165 dl->SetFolder(GetDetectorDataFolder()); //Must exists - ensure register is called before
167 /*****************************************************************************/
169 Int_t AliLoader::SetEvent()
171 //basically the same that GetEvent but do not post data to folders
172 TIter next(fDataLoaders);
174 while ((dl = (AliDataLoader*)next()))
180 /******************************************************************/
182 void AliLoader::UnloadAll()
184 //calls UnloadAll for all base laoders
186 TIter next(fDataLoaders);
188 while ((dl = (AliDataLoader*)next()))
193 /******************************************************************/
195 Int_t AliLoader::GetEvent()
197 //changes to proper root directory and tries to load data from files to folders
198 // event number is defined in RunLoader
201 // 0 - in case of no error
202 // 1 - event not found
206 TIter next(fDataLoaders);
208 while ((dl = (AliDataLoader*)next()))
210 retval = dl->GetEvent();
213 AliError(Form("Error occured while GetEvent for %s",dl->GetName()));
221 /******************************************************************/
223 TFolder* AliLoader::GetTopFolder()
225 //returns TOP aliroot folder, just a simple interface to AliConfig (gives shorter notation)
226 return AliConfig::Instance()->GetTopFolder();
229 /******************************************************************/
231 TFolder* AliLoader::GetEventFolder()
233 //get EVENT folder (data that are changing from event to event, even in single run)
236 /******************************************************************/
237 TFolder* AliLoader::GetDataFolder()
239 //returns the folder speciofic to given detector e.g. /Folders/Event/Data/
242 fDataFolder = dynamic_cast<TFolder*>(GetEventFolder()->FindObject(AliConfig::Instance()->GetDataFolderName()));
246 AliFatal("Can not find AliRoot data folder. Aborting");
253 /*****************************************************************************/
255 TFolder* AliLoader::GetModulesFolder()
257 //returns pointer to the folder containing modules
260 fModuleFolder = dynamic_cast<TFolder*>(GetEventFolder()->FindObjectAny(AliConfig::GetModulesFolderName()));
264 AliFatal("Can not find modules folder. Aborting");
268 return fModuleFolder;
272 /*****************************************************************************/
274 TDirectory* AliLoader::ChangeDir(TFile* file, Int_t eventno)
276 //changes the root directory in "file" to "dirname" which corresponds to event 'eventno'
277 //in case of success returns the pointer to directory
282 AliErrorClass("File is null");
285 if (file->IsOpen() == kFALSE)
287 AliErrorClass("File is not opened");
291 TString dirname("Event");
293 AliDebugClass(1, Form("Changing Dir to %s in file %s.",dirname.Data(),file->GetName()));
297 TDirectory* dir = dynamic_cast<TDirectory*>(file->Get(dirname));
301 AliDebugClass(1, Form("Can not find directory %s in file %s, creating...",
302 dirname.Data(),file->GetName()));
304 if (file->IsWritable() == kFALSE)
306 AliErrorClass(Form("Can not create directory. File %s in not writable.",
311 TDirectory* newdir = file->mkdir(dirname);
314 AliErrorClass(Form("Failed to create new directory in file %s.",
318 result = file->cd(dirname);
319 if (result == kFALSE)
326 file->cd();//make a file active
327 file->cd(dirname);//cd event dir
332 /*****************************************************************************/
334 TString AliLoader::GetUnixDir() const
336 //This Method will manage jumping through unix directories in case of
337 //run with more events per run than defined in gAlice
343 /*****************************************************************************/
344 /************************************************************/
346 void AliLoader::MakeTree(Option_t *option)
348 //Makes a tree depending on option
351 // S: - Summable Digits
352 // R: - Reconstructed Points (clusters)
353 // T: - Tracks (tracklets)
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");
362 if (oH) MakeHitsContainer();
363 if (oD) MakeDigitsContainer();
364 if (oS) MakeSDigitsContainer();
365 if (oR) MakeRecPointsContainer();
366 if (oT) MakeTracksContainer();
367 if (oP) MakeRecParticlesContainer();
370 /*****************************************************************************/
371 Int_t AliLoader::WriteHits(Option_t* opt) const
374 AliDataLoader* dl = GetHitsDataLoader();
375 Int_t ret = dl->WriteData(opt);
378 /*****************************************************************************/
380 Int_t AliLoader::WriteSDigits(Option_t* opt) const
382 // Writes summable digits
383 AliDataLoader* dl = GetSDigitsDataLoader();
384 Int_t ret = dl->WriteData(opt);
388 /*****************************************************************************/
390 TObject** AliLoader::GetDetectorDataRef(TObject *obj)
392 // Returns pointer to an entry in the list of folders pointing to "obj"
397 return GetDetectorDataFolder()->GetListOfFolders()->GetObjectRef(obj) ;
400 /*****************************************************************************/
401 void AliLoader::CleanFolders()
403 //Cleans all posted objects == removes from folders and deletes them
404 TIter next(fDataLoaders);
406 while ((dl = (AliDataLoader*)next()))
408 AliDebug(1, Form("name = %s cleaning",dl->GetName()));
413 /*****************************************************************************/
415 Int_t AliLoader::ReloadAll()
417 // Calling Reload function for all the data loaders
418 TIter next(fDataLoaders);
421 while((dl = (AliDataLoader*)next()))
423 Int_t err = dl->Reload();
426 AliError(Form("Reload returned error for %s",dl->GetName()));
432 /*****************************************************************************/
434 void AliLoader::CloseFiles()
436 //close files for data loaders
437 TIter next(fDataLoaders);
439 while((dl = (AliDataLoader*)next()))
444 /*****************************************************************************/
446 Int_t AliLoader::SetEventFolder(TFolder* eventfolder)
448 //sets the event folder
449 if (eventfolder == 0x0)
451 AliError("Stupid joke. Argument is NULL");
455 fEventFolder = eventfolder;
456 TIter next(fDataLoaders);
459 while((dl = (AliDataLoader*)next()))
461 dl->SetEventFolder(fEventFolder);
462 dl->SetFolder(GetDetectorDataFolder()); //Must exists - ensure register is called before
466 }//sets the event folder
467 /*****************************************************************************/
469 Int_t AliLoader::Register(TFolder* eventFolder)
471 //triggers creation of subfolders for a given detector
472 //this method is called when session is read from disk
474 //warning: AliDetector in constructor (not default) calls
475 //creation of folder structure as well (some detectors needs folders
476 //alrady in constructors)
478 AliDebug(1, Form("Name is %s.",GetName()));
479 if (eventFolder == 0x0)
481 AliError("Event folder is not set.");
484 Int_t retval = AliConfig::Instance()->AddDetector(eventFolder,fDetectorName,fDetectorName);
487 AliError(Form("Can not create folders for %s. Event folder name is %s",
488 fDetectorName.Data(),eventFolder->GetName()));
491 SetEventFolder(eventFolder);
494 /*****************************************************************************/
495 AliRunLoader* AliLoader::GetRunLoader()
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);
503 /*****************************************************************************/
504 Bool_t AliLoader::TestFileOption(Option_t* opt)
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)
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;
515 /*****************************************************************************/
516 void AliLoader::SetDirName(TString& dirname)
518 //adds "dirname" to each file. Dirname should end with "#","/", or ":"
519 TIter next(fDataLoaders);
521 while((dl = (AliDataLoader*)next()))
523 dl->SetDirName(dirname);
527 /*****************************************************************************/
529 void AliLoader::SetDigitsFileNameSuffix(const TString& suffix) const
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);
536 /*****************************************************************************/
538 void AliLoader::SetCompressionLevel(Int_t cl)
540 //sets comression level for data defined by di
541 TIter next(fDataLoaders);
543 while((dl = (AliDataLoader*)next()))
545 dl->SetCompressionLevel(cl);
548 /*****************************************************************************/
550 void AliLoader::Clean()
552 //Cleans all data loaders
553 TIter next(fDataLoaders);
555 while((dl = (AliDataLoader*)next()))
560 /*****************************************************************************/
562 void AliLoader::Clean(const TString& name)
564 // Removes object with "name" from the detector's data folder
565 // and from the memory
566 TObject* obj = GetDetectorDataFolder()->FindObject(name);
569 AliDebug(1, Form("name=%s, cleaning %s.",GetName(),name.Data()));
570 GetDetectorDataFolder()->Remove(obj);
575 /*****************************************************************************/
577 Bool_t AliLoader::IsOptionWritable(const TString& opt)
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;
587 /*****************************************************************************/
589 void AliLoader::SetDebug(Int_t deb)
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);
600 /*****************************************************************************/
602 void AliLoader::SetTAddrInDet()
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();
613 /*****************************************************************************/
615 void AliLoader::Synchronize()
617 //synchrinizes all writtable files
618 TIter next(fDataLoaders);
620 while ((dl = (AliDataLoader*)next()))
626 /*****************************************************************************/
627 /*****************************************************************************/
628 /*****************************************************************************/