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) //
14 // - task DtectorName+Name defined //
16 // EStdBasicLoaders idx Object Type Description //
17 // kData 0 TTree or TObject main data itself (hits,digits,...) //
18 // kTask 1 TTask object producing main data //
19 // kQA 2 TTree quality assurance tree //
20 // kQATask 3 TTask task producing QA object //
23 // User can define and add more basic loaders even Run Time. //
24 // Caution: in order to save information about added base loader //
25 // user must rewrite Run Loader to galice.file, overwriting old setup //
27 /////////////////////////////////////////////////////////////////////////////////////////////
36 #include "AliDataLoader.h"
38 #include "AliObjectLoader.h"
39 #include "AliRunLoader.h"
40 #include "AliTreeLoader.h"
42 ClassImp(AliDataLoader)
44 //______________________________________________________________________________
45 AliDataLoader::AliDataLoader():
62 //______________________________________________________________________________
63 AliDataLoader::AliDataLoader(const char* filename, const char* contname,
64 const char* name, Option_t* opt):
72 fBaseLoaders(new TObjArray(4)),
80 // creates a 0 loader, depending on option, default "T" is specialized loader for trees
81 // else standard object loader
82 // trees needs special care, becouse we need to set dir before writing
83 AliDebug(1, Form("File name is %s",fFileName.Data()));
87 if (option.CompareTo("T",TString::kIgnoreCase) == 0)
88 bl = new AliTreeLoader(contname,this);
90 bl = new AliObjectLoader(contname,this);
91 fBaseLoaders->AddAt(bl,kData);
95 //______________________________________________________________________________
96 AliDataLoader::~AliDataLoader()
104 //______________________________________________________________________________
105 Int_t AliDataLoader::SetEvent()
108 // The same that GetEvent but do not post data to folders
110 AliRunLoader* rl = GetRunLoader();
113 AliError("Can not get RunGettr");
117 Int_t evno = rl->GetEventNumber();
119 TIter next(fBaseLoaders);
121 while ((bl = (AliBaseLoader*)next()))
123 if (bl->DoNotReload() == kFALSE) bl->Clean();
132 AliDebug(1, Form("Reloading new file. File opt is %s",fFileOption.Data()));
133 OpenFile(fFileOption);
136 fDirectory = AliLoader::ChangeDir(fFile,evno);
137 if (fDirectory == 0x0)
139 AliError(Form("Can not chage directory in file %s",fFile->GetName()));
146 //______________________________________________________________________________
147 Int_t AliDataLoader::GetEvent()
149 // posts all loaded data from files to White Board
150 // event number is defined in RunLoader
153 // 0 - in case of no error
154 // 1 - event not found
156 //for each base laoder post, if was loaded before GetEvent
158 //call set event to switch to new directory in file
161 //post all data that were loaded before
162 // ->SetEvent does not call Unload, but only cleans White Board
163 // such IsLoaded flag stays untached
165 if ( AliLoader::TestFileOption(fFileOption) == kTRUE ) //if file is read or update mode try to post
166 { //in other case there is no sense to post: file is new
167 TIter nextbl(fBaseLoaders);
169 while ((bl = (AliBaseLoader*)nextbl()))
173 if (bl->DoNotReload() == kFALSE) bl->Post();
180 //______________________________________________________________________________
181 Int_t AliDataLoader::OpenFile(Option_t* opt)
183 //Opens file named 'filename', and assigns pointer to it to 'file'
184 //jumps to fDirectoryectory corresponding to current event and stores the pointer to it in 'fDirectory'
185 //option 'opt' is passed to TFile::Open
188 if(fFile->IsOpen() == kTRUE)
190 AliWarning(Form(" File %s already opened. First close it.",fFile->GetName()));
195 AliWarning(Form("Pointer to file %s is not null, but file is not opened",
202 TString fname(SetFileOffset(fFileName));
204 fFile = (TFile *)(gROOT->GetListOfFiles()->FindObject(fname));
207 if(fFile->IsOpen() == kTRUE)
209 AliWarning(Form("File %s already opened by sombody else. First close it.",
216 fFile = TFile::Open(fname,fFileOption);//open the file
219 AliError(Form("Can not open file %s",fname.Data()));
222 if (fFile->IsOpen() == kFALSE)
224 AliError(Form("Can not open file %s",fname.Data()));
228 fFile->SetCompressionLevel(fCompressionLevel);
230 AliRunLoader* rg = GetRunLoader();
233 AliError("Can not find Run-Loader in folder.");
236 Int_t evno = rg->GetEventNumber();
238 fDirectory = AliLoader::ChangeDir(fFile,evno);
239 if (fDirectory == 0x0)
241 AliError(Form("Can not chage fDirectory in file %s.",fFile->GetName()));
247 //______________________________________________________________________________
248 void AliDataLoader::Unload()
251 //unloads main data - shortcut method
253 GetBaseLoader(0)->Unload();
256 //______________________________________________________________________________
257 void AliDataLoader::UnloadAll()
260 // Unloads all data and tasks
262 if ( fFile == 0x0 ) return; //nothing loaded
264 TIter next(fBaseLoaders);
266 while ((bl = (AliBaseLoader*)next()))
273 //______________________________________________________________________________
274 Int_t AliDataLoader::Reload()
277 // Unloads and loads data again
279 if ( fFile == 0x0 ) return 0;
281 TBits loaded(fBaseLoaders->GetEntries());
282 TIter next(fBaseLoaders);
286 while ((bl = (AliBaseLoader*)next()))
290 loaded.SetBitNumber(i++,kTRUE);
298 while ((bl = (AliBaseLoader*)next()))
300 if (loaded.TestBitNumber(i++))
302 retval = bl->Load(fFileOption);
305 AliError(Form("Error occur while loading %s",bl->GetName()));
314 //______________________________________________________________________________
315 Int_t AliDataLoader::WriteData(Option_t* opt)
318 // Writes primary data == first BaseLoader
320 AliDebug(1, Form("Writing %s container for %s data. Option is %s.",
321 GetBaseLoader(0)->GetName(),GetName(),opt));
322 return GetBaseLoader(0)->WriteData(opt);
325 //______________________________________________________________________________
326 Int_t AliDataLoader::Load(Option_t* opt)
329 // Writes primary data == first BaseLoader
331 return GetBaseLoader(0)->Load(opt);
334 //______________________________________________________________________________
335 Int_t AliDataLoader::SetEventFolder(TFolder* eventfolder)
338 // Sets the event folder
340 if (eventfolder == 0x0)
342 AliError("Stupid joke. Argument is NULL");
345 AliDebug(1, Form("name = %s Setting Event Folder named %s.",
346 GetName(),eventfolder->GetName()));
348 fEventFolder = eventfolder;
353 //______________________________________________________________________________
354 Int_t AliDataLoader::SetFolder(TFolder* folder)
356 // Sets the folder and the data loaders
359 AliError("Stupid joke. Argument is NULL");
363 AliDebug(1, Form("name = %s Setting folder named %s.",GetName(),folder->GetName()));
366 TIter next(fBaseLoaders);
369 while ((bl = (AliBaseLoader*)next()))
371 bl->SetDataLoader(this);
377 //______________________________________________________________________________
378 TFolder* AliDataLoader::GetEventFolder()
382 // Data that are changing from event to event, even in single run
384 AliDebug(1, "EF = %#x");
388 //______________________________________________________________________________
389 AliRunLoader* AliDataLoader::GetRunLoader()
392 // Gets the run-loader from event folder
394 AliRunLoader* rg = 0x0;
395 TFolder* ef = GetEventFolder();
398 AliError("Can not get event folder.");
401 rg = dynamic_cast<AliRunLoader*>(ef->FindObject(AliRunLoader::GetRunLoaderName()));
405 //______________________________________________________________________________
406 void AliDataLoader::CloseFile()
411 TIter next(fBaseLoaders);
413 while ((bl = (AliBaseLoader*)next()))
415 if (bl->IsLoaded()) return;
418 AliDebug(1, "Closing and deleting (object) file.");
426 //______________________________________________________________________________
427 void AliDataLoader::Clean()
432 GetBaseLoader(0)->Clean();
435 //______________________________________________________________________________
436 void AliDataLoader::CleanAll()
439 // Cleans all folders and tasks
441 TIter next(fBaseLoaders);
443 while ((bl = (AliBaseLoader*)next()))
449 //______________________________________________________________________________
450 void AliDataLoader::SetFileNameSuffix(const TString& suffix)
453 // adds the suffix before ".root",
454 // e.g. TPC.Digits.root -> TPC.DigitsMerged.root
455 // made on Jiri Chudoba demand
457 AliDebug(1, Form("suffix=%s",suffix.Data()));
458 AliDebug(1, Form(" Digits File Name before: %s",fFileName.Data()));
460 static TString dotroot(".root");
461 const TString& suffixdotroot = suffix + dotroot;
462 fFileName = fFileName.ReplaceAll(dotroot,suffixdotroot);
464 AliDebug(1, Form(" after : %s",fFileName.Data()));
467 //______________________________________________________________________________
468 Bool_t AliDataLoader::CheckReload()
471 // Checks if we have to reload given file
473 if (fFile == 0x0) return kFALSE;
474 TString tmp = SetFileOffset(fFileName);
475 if (tmp.CompareTo(fFile->GetName())) return kTRUE; //file must be reloaded
479 //______________________________________________________________________________
480 const TString AliDataLoader::SetFileOffset(const TString& fname)
485 Long_t offset = (Long_t)GetRunLoader()->GetFileOffset();
486 if (fNEventsPerFile > 0) {
487 offset = GetRunLoader()->GetEventNumber()/fNEventsPerFile;
489 if (offset < 1) return fname;
492 soffset += offset;//automatic conversion to string
493 TString dotroot(".root");
494 const TString& offfsetdotroot = offset + dotroot;
496 out = out.ReplaceAll(dotroot,offfsetdotroot);
497 AliDebug(1, Form("in=%s out=%s.",fname.Data(),out.Data()));
501 //______________________________________________________________________________
502 void AliDataLoader::SetFileOption(Option_t* newopt)
507 if (fFileOption.CompareTo(newopt) == 0) return;
508 fFileOption = newopt;
512 //______________________________________________________________________________
513 void AliDataLoader::SetCompressionLevel(Int_t cl)
516 // Sets comression level for data defined by di
518 fCompressionLevel = cl;
519 if (fFile) fFile->SetCompressionLevel(cl);
522 //______________________________________________________________________________
523 void AliDataLoader::MakeTree()
526 // Makes tree for the current data loader
528 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(fBaseLoaders->At(0));
531 AliError("Can not make a tree because main base loader is not a tree loader");
537 //______________________________________________________________________________
538 Bool_t AliDataLoader::IsFileWritable() const
541 // Returns true if file is writable
543 return (fFile)?fFile->IsWritable():kFALSE;
546 //______________________________________________________________________________
547 Bool_t AliDataLoader::IsFileOpen() const
550 // Returns true if file is writable
552 return (fFile)?fFile->IsOpen():kFALSE;
555 //______________________________________________________________________________
556 Bool_t AliDataLoader::IsOptionContrary(const TString& option) const
558 // Checks if passed option is contrary with file open option
559 // which is passed option "writable" and existing option not wriable
560 // in reverse case it is no harm so it is NOT contrary
561 if (fFile == 0x0) return kFALSE; //file is not opened - no problem
563 if ( ( AliLoader::IsOptionWritable(option) == kTRUE ) && // passed option is writable and
564 ( AliLoader::IsOptionWritable(fFileOption) == kFALSE ) ) // existing one is not writable
573 //______________________________________________________________________________
574 void AliDataLoader::AddBaseLoader(AliBaseLoader* bl)
576 //Adds a base loader to lits of base loaders managed by this data loader
577 //Managed data/task will be stored in proper root directory,
579 // - in case of tree/object - data folder connected with detector associated with this data loader
580 // - in case of task - parental task which defined in this AliTaskLoader
584 AliWarning("Pointer is null.");
588 TObject* obj = fBaseLoaders->FindObject(bl->GetName());
591 AliError("Can not add this base loader.");
592 AliError(Form("There exists already base loader which manages data named %s for this detector.",obj->GetName()));
596 fBaseLoaders->Add(bl);
599 //______________________________________________________________________________
600 AliBaseLoader* AliDataLoader::GetBaseLoader(const TString& name) const
603 // Return pointer to base loader
605 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->FindObject(name));
608 //______________________________________________________________________________
609 AliBaseLoader* AliDataLoader::GetBaseLoader(Int_t n) const
612 // Gets the n-th base loader (what is n?)
614 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->At(n));
617 //______________________________________________________________________________
618 TTree* AliDataLoader::Tree() const
620 // Returns tree from the main base loader
621 // it is just shortcut method for comfort of user
622 // main storage object does not have to be Tree -
623 // that is why first we need to check if it is a TreeLoader
624 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(GetBaseLoader(0));
625 if (tl == 0x0) return 0x0;
629 //______________________________________________________________________________
630 void AliDataLoader::SetDirName(TString& dirname)
633 // Sets the directory name where the files will be stored
635 AliDebug(10, Form("FileName before %s",fFileName.Data()));
636 Int_t n = fFileName.Last('/');
637 AliDebug(10, Form("Slash found on pos %d",n));
638 if (n > 0) fFileName = fFileName.Remove(0,n+1);
639 AliDebug(10, Form("Core FileName %s",fFileName.Data()));
640 fFileName = dirname + fFileName;
641 AliDebug(10, Form("FileName after %s",fFileName.Data()));
644 //______________________________________________________________________________
645 AliObjectLoader* AliDataLoader::GetBaseDataLoader()
648 // Gets the base data loader
650 return dynamic_cast<AliObjectLoader*>(GetBaseLoader(kData));
653 //______________________________________________________________________________
654 AliTaskLoader* AliDataLoader::GetBaseTaskLoader()
657 // Gets the base task loader
659 return dynamic_cast<AliTaskLoader*>(GetBaseLoader(kTask));
662 //______________________________________________________________________________
663 AliBaseLoader* AliDataLoader::GetBaseQALoader()
666 // Gets the base QA loader
668 return GetBaseLoader(kQA);
671 //______________________________________________________________________________
672 AliTaskLoader* AliDataLoader::GetBaseQATaskLoader()
675 // Returns pointer to QA base loader
677 return dynamic_cast<AliTaskLoader*>(GetBaseLoader(kQATask));
680 //______________________________________________________________________________
681 void AliDataLoader::SetBaseDataLoader(AliBaseLoader* bl)
684 // Sets data base loader
688 AliError("Parameter is null");
691 if (GetBaseDataLoader()) delete GetBaseDataLoader();
692 fBaseLoaders->AddAt(bl,kData);
695 //______________________________________________________________________________
696 void AliDataLoader::SetBaseTaskLoader(AliTaskLoader* bl)
699 // Sets Task base loader
703 AliError("Parameter is null");
706 if (GetBaseTaskLoader()) delete GetBaseTaskLoader();
707 fBaseLoaders->AddAt(bl,kTask);
710 //______________________________________________________________________________
711 void AliDataLoader::SetBaseQALoader(AliBaseLoader* bl)
714 // Sets QA base loader
718 AliError("Parameter is null");
721 if (GetBaseQALoader()) delete GetBaseQALoader();
722 fBaseLoaders->AddAt(bl,kQA);
725 //______________________________________________________________________________
726 void AliDataLoader::SetBaseQATaskLoader(AliTaskLoader* bl)
729 // Sets QA Task base loader
733 AliError("Parameter is null");
736 if (GetBaseQATaskLoader()) delete GetBaseQATaskLoader();
737 fBaseLoaders->AddAt(bl,kQATask);
740 //______________________________________________________________________________
741 void AliDataLoader::Synchronize()
744 // Synchronizes all writable files
746 if ( fFile ) fFile->Flush();