1 /////////////////////////////////////////////////////////////////////////////////////////////
3 // class AliDataLoader //
5 // Container of all data needed for full //
6 // description of each data type //
7 // (Hits, Kine, ...) //
9 // Each data loader has a basic standard setup of BaseLoaders //
10 // which can be identuified by indexes (defined by EStdBasicLoaders) //
11 // Data managed by these standard base loaders has fixed naming convention //
12 // e.g. - tree with hits is always named TreeH //
13 // (defined in AliLoader::fgkDefaultHitsContainerName) //
15 // EStdBasicLoaders idx Object Type Description //
16 // kData 0 TTree or TObject main data itself (hits,digits,...) //
19 // User can define and add more basic loaders even Run Time. //
20 // Caution: in order to save information about added base loader //
21 // user must rewrite Run Loader to galice.file, overwriting old setup //
23 /////////////////////////////////////////////////////////////////////////////////////////////
27 #include "AliDataLoader.h"
28 #include "AliRunLoader.h"
29 #include "AliLoader.h"
30 #include "AliObjectLoader.h"
31 #include "AliTreeLoader.h"
39 ClassImp(AliDataLoader)
41 //______________________________________________________________________________
42 AliDataLoader::AliDataLoader():
56 //______________________________________________________________________________
57 AliDataLoader::AliDataLoader(const char* filename, const char* contname,
58 const char* name, Option_t* opt):
66 fBaseLoaders(new TObjArray(4)),
71 // creates a 0 loader, depending on option, default "T" is specialized loader for trees
72 // else standard object loader
73 // trees needs special care, becouse we need to set dir before writing
74 AliDebug(1, Form("File name is %s",fFileName.Data()));
78 if (option.CompareTo("T",TString::kIgnoreCase) == 0)
79 bl = new AliTreeLoader(contname,this);
81 bl = new AliObjectLoader(contname,this);
82 fBaseLoaders->AddAt(bl,kData);
86 //______________________________________________________________________________
87 AliDataLoader::~AliDataLoader()
95 //______________________________________________________________________________
96 Int_t AliDataLoader::SetEvent()
99 // The same that GetEvent but do not post data to folders
101 AliRunLoader* rl = GetRunLoader();
104 AliError("Can not get RunGettr");
108 Int_t evno = rl->GetEventNumber();
110 TIter next(fBaseLoaders);
112 while ((bl = (AliBaseLoader*)next()))
114 if (bl->DoNotReload() == kFALSE) bl->Clean();
123 AliDebug(1, Form("Reloading new file. File opt is %s",fFileOption.Data()));
124 OpenFile(fFileOption);
127 fDirectory = AliLoader::ChangeDir(fFile,evno);
128 if (fDirectory == 0x0)
130 AliError(Form("Can not chage directory in file %s",fFile->GetName()));
137 //______________________________________________________________________________
138 Int_t AliDataLoader::GetEvent()
140 // posts all loaded data from files to White Board
141 // event number is defined in RunLoader
144 // 0 - in case of no error
145 // 1 - event not found
147 //for each base laoder post, if was loaded before GetEvent
149 //call set event to switch to new directory in file
152 //post all data that were loaded before
153 // ->SetEvent does not call Unload, but only cleans White Board
154 // such IsLoaded flag stays untached
156 if ( AliLoader::TestFileOption(fFileOption) == kTRUE ) //if file is read or update mode try to post
157 { //in other case there is no sense to post: file is new
158 TIter nextbl(fBaseLoaders);
160 while ((bl = (AliBaseLoader*)nextbl()))
164 if (bl->DoNotReload() == kFALSE) bl->Post();
171 //______________________________________________________________________________
172 Int_t AliDataLoader::OpenFile(Option_t* opt)
174 //Opens file named 'filename', and assigns pointer to it to 'file'
175 //jumps to fDirectoryectory corresponding to current event and stores the pointer to it in 'fDirectory'
176 //option 'opt' is passed to TFile::Open
179 if(fFile->IsOpen() == kTRUE)
181 AliWarning(Form(" File %s already opened. First close it.",fFile->GetName()));
186 AliWarning(Form("Pointer to file %s is not null, but file is not opened",
193 TString fname(SetFileOffset(fFileName));
195 fFile = (TFile *)(gROOT->GetListOfFiles()->FindObject(fname));
198 if(fFile->IsOpen() == kTRUE)
200 TString option1 = fFile->GetOption();
201 if (option1.CompareTo("read",TString::kIgnoreCase) == 0)
203 AliInfo(Form("File %s already opened in read mode.",fFile->GetName()));
207 TString option2 = opt;
208 if (option2.CompareTo("read",TString::kIgnoreCase) == 0)
210 AliInfo(Form("Open already opened file %s in read mode.",fFile->GetName()));
213 AliWarning(Form("File %s already opened by sombody else. First close it.",
222 fFile = TFile::Open(fname,fFileOption);//open the file
225 AliError(Form("Can not open file %s",fname.Data()));
228 if (fFile->IsOpen() == kFALSE)
230 AliError(Form("Can not open file %s",fname.Data()));
234 fFile->SetBit(TFile::kDevNull);
235 fFile->SetCompressionLevel(fCompressionLevel);
237 AliRunLoader* rg = GetRunLoader();
240 AliError("Can not find Run-Loader in folder.");
243 Int_t evno = rg->GetEventNumber();
245 fDirectory = AliLoader::ChangeDir(fFile,evno);
246 if (fDirectory == 0x0)
248 AliError(Form("Can not chage fDirectory in file %s.",fFile->GetName()));
254 //______________________________________________________________________________
255 void AliDataLoader::Unload()
258 //unloads main data - shortcut method
260 GetBaseLoader(0)->Unload();
263 //______________________________________________________________________________
264 void AliDataLoader::UnloadAll()
269 if ( fFile == 0x0 ) return; //nothing loaded
271 TIter next(fBaseLoaders);
273 while ((bl = (AliBaseLoader*)next()))
280 //______________________________________________________________________________
281 Int_t AliDataLoader::Reload()
284 // Unloads and loads data again
286 if ( fFile == 0x0 ) return 0;
288 TBits loaded(fBaseLoaders->GetEntries());
289 TIter next(fBaseLoaders);
293 while ((bl = (AliBaseLoader*)next()))
297 loaded.SetBitNumber(i++,kTRUE);
305 while ((bl = (AliBaseLoader*)next()))
307 if (loaded.TestBitNumber(i++))
309 retval = bl->Load(fFileOption);
312 AliError(Form("Error occur while loading %s",bl->GetName()));
321 //______________________________________________________________________________
322 Int_t AliDataLoader::WriteData(Option_t* opt)
325 // Writes primary data == first BaseLoader
327 AliDebug(1, Form("Writing %s container for %s data. Option is %s.",
328 GetBaseLoader(0)->GetName(),GetName(),opt));
329 return GetBaseLoader(0)->WriteData(opt);
332 //______________________________________________________________________________
333 Int_t AliDataLoader::Load(Option_t* opt)
336 // Writes primary data == first BaseLoader
338 return GetBaseLoader(0)->Load(opt);
341 //______________________________________________________________________________
342 Int_t AliDataLoader::SetEventFolder(TFolder* eventfolder)
345 // Sets the event folder
347 if (eventfolder == 0x0)
349 AliError("Stupid joke. Argument is NULL");
352 AliDebug(1, Form("name = %s Setting Event Folder named %s.",
353 GetName(),eventfolder->GetName()));
355 fEventFolder = eventfolder;
360 //______________________________________________________________________________
361 Int_t AliDataLoader::SetFolder(TFolder* folder)
363 // Sets the folder and the data loaders
366 AliError("Stupid joke. Argument is NULL");
370 AliDebug(1, Form("name = %s Setting folder named %s.",GetName(),folder->GetName()));
373 TIter next(fBaseLoaders);
376 while ((bl = (AliBaseLoader*)next()))
378 bl->SetDataLoader(this);
384 //______________________________________________________________________________
385 TFolder* AliDataLoader::GetEventFolder()
389 // Data that are changing from event to event, even in single run
391 AliDebug(1, "EF = %#x");
395 //______________________________________________________________________________
396 AliRunLoader* AliDataLoader::GetRunLoader()
399 // Gets the run-loader from event folder
401 AliRunLoader* rg = 0x0;
402 TFolder* ef = GetEventFolder();
405 AliError("Can not get event folder.");
408 rg = dynamic_cast<AliRunLoader*>(ef->FindObject(AliRunLoader::GetRunLoaderName()));
412 //______________________________________________________________________________
413 void AliDataLoader::CloseFile()
418 TIter next(fBaseLoaders);
420 while ((bl = (AliBaseLoader*)next()))
422 if (bl->IsLoaded()) return;
425 AliDebug(1, "Closing (object) file.");
436 //______________________________________________________________________________
437 void AliDataLoader::Clean()
442 GetBaseLoader(0)->Clean();
445 //______________________________________________________________________________
446 void AliDataLoader::CleanAll()
449 // Cleans all folders
451 TIter next(fBaseLoaders);
453 while ((bl = (AliBaseLoader*)next()))
459 //______________________________________________________________________________
460 void AliDataLoader::SetFileNameSuffix(const TString& suffix)
463 // adds the suffix before ".root",
464 // e.g. TPC.Digits.root -> TPC.DigitsMerged.root
465 // made on Jiri Chudoba demand
467 AliDebug(1, Form("suffix=%s",suffix.Data()));
468 AliDebug(1, Form(" Digits File Name before: %s",fFileName.Data()));
470 static const TString dotroot(".root");
471 const TString& suffixdotroot = suffix + dotroot;
472 fFileName = fFileName.ReplaceAll(dotroot,suffixdotroot);
474 AliDebug(1, Form(" after : %s",fFileName.Data()));
477 //______________________________________________________________________________
478 Bool_t AliDataLoader::CheckReload()
481 // Checks if we have to reload given file
483 if (fFile == 0x0) return kFALSE;
484 TString tmp = SetFileOffset(fFileName);
485 if (tmp.CompareTo(fFile->GetName())) return kTRUE; //file must be reloaded
489 //______________________________________________________________________________
490 const TString AliDataLoader::SetFileOffset(const TString& fname)
495 Long_t offset = (Long_t)GetRunLoader()->GetFileOffset();
496 if (fNEventsPerFile > 0) {
497 offset = GetRunLoader()->GetEventNumber()/fNEventsPerFile;
499 if (offset < 1) return fname;
502 soffset += offset;//automatic conversion to string
503 TString dotroot(".root");
504 const TString& offfsetdotroot = offset + dotroot;
506 out = out.ReplaceAll(dotroot,offfsetdotroot);
507 AliDebug(1, Form("in=%s out=%s.",fname.Data(),out.Data()));
511 //______________________________________________________________________________
512 void AliDataLoader::SetFileOption(Option_t* newopt)
517 if (fFileOption.CompareTo(newopt) == 0) return;
518 fFileOption = newopt;
522 //______________________________________________________________________________
523 void AliDataLoader::SetCompressionLevel(Int_t cl)
526 // Sets comression level for data defined by di
528 fCompressionLevel = cl;
529 if (fFile) fFile->SetCompressionLevel(cl);
532 //______________________________________________________________________________
533 void AliDataLoader::MakeTree()
536 // Makes tree for the current data loader
538 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(fBaseLoaders->At(0));
541 AliError("Can not make a tree because main base loader is not a tree loader");
547 //______________________________________________________________________________
548 Bool_t AliDataLoader::IsFileWritable() const
551 // Returns true if file is writable
553 return (fFile)?fFile->IsWritable():kFALSE;
556 //______________________________________________________________________________
557 Bool_t AliDataLoader::IsFileOpen() const
560 // Returns true if file is writable
562 return (fFile)?fFile->IsOpen():kFALSE;
565 //______________________________________________________________________________
566 Bool_t AliDataLoader::IsOptionContrary(const TString& option) const
568 // Checks if passed option is contrary with file open option
569 // which is passed option "writable" and existing option not wriable
570 // in reverse case it is no harm so it is NOT contrary
571 if (fFile == 0x0) return kFALSE; //file is not opened - no problem
573 if ( ( AliLoader::IsOptionWritable(option) == kTRUE ) && // passed option is writable and
574 ( AliLoader::IsOptionWritable(fFileOption) == kFALSE ) ) // existing one is not writable
583 //______________________________________________________________________________
584 void AliDataLoader::AddBaseLoader(AliBaseLoader* bl)
586 //Adds a base loader to lits of base loaders managed by this data loader
587 //Managed data will be stored in proper root directory,
589 // - in case of tree/object - data folder connected with detector associated with this data loader
593 AliWarning("Pointer is null.");
597 TObject* obj = fBaseLoaders->FindObject(bl->GetName());
600 AliError("Can not add this base loader.");
601 AliError(Form("There exists already base loader which manages data named %s for this detector.",obj->GetName()));
605 fBaseLoaders->Add(bl);
608 //______________________________________________________________________________
609 AliBaseLoader* AliDataLoader::GetBaseLoader(const TString& name) const
612 // Return pointer to base loader
614 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->FindObject(name));
617 //______________________________________________________________________________
618 AliBaseLoader* AliDataLoader::GetBaseLoader(Int_t n) const
621 // Gets the n-th base loader (what is n?)
623 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->At(n));
626 //______________________________________________________________________________
627 TTree* AliDataLoader::Tree() const
629 // Returns tree from the main base loader
630 // it is just shortcut method for comfort of user
631 // main storage object does not have to be Tree -
632 // that is why first we need to check if it is a TreeLoader
633 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(GetBaseLoader(0));
634 if (tl == 0x0) return 0x0;
638 //______________________________________________________________________________
639 void AliDataLoader::SetDirName(TString& dirname)
642 // Sets the directory name where the files will be stored
644 AliDebug(10, Form("FileName before %s",fFileName.Data()));
645 Int_t n = fFileName.Last('/');
646 AliDebug(10, Form("Slash found on pos %d",n));
647 if (n > 0) fFileName = fFileName.Remove(0,n+1);
648 AliDebug(10, Form("Core FileName %s",fFileName.Data()));
649 fFileName = dirname + fFileName;
650 AliDebug(10, Form("FileName after %s",fFileName.Data()));
653 //______________________________________________________________________________
654 AliObjectLoader* AliDataLoader::GetBaseDataLoader()
657 // Gets the base data loader
659 return dynamic_cast<AliObjectLoader*>(GetBaseLoader(kData));
662 //______________________________________________________________________________
663 void AliDataLoader::SetBaseDataLoader(AliBaseLoader* bl)
666 // Sets data base loader
670 AliError("Parameter is null");
673 if (GetBaseDataLoader()) delete GetBaseDataLoader();
674 fBaseLoaders->AddAt(bl,kData);
677 //______________________________________________________________________________
678 void AliDataLoader::Synchronize()
681 // Synchronizes all writable files
683 if ( fFile ) fFile->Flush();