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 TString option1 = fFile->GetOption();
210 if (option1.CompareTo("read",TString::kIgnoreCase) == 0)
212 AliInfo(Form("File %s already opened in read mode.",fFile->GetName()));
216 TString option2 = opt;
217 if (option2.CompareTo("read",TString::kIgnoreCase) == 0)
219 AliInfo(Form("Open already opened file %s in read mode.",fFile->GetName()));
222 AliWarning(Form("File %s already opened by sombody else. First close it.",
231 fFile = TFile::Open(fname,fFileOption);//open the file
234 AliError(Form("Can not open file %s",fname.Data()));
237 if (fFile->IsOpen() == kFALSE)
239 AliError(Form("Can not open file %s",fname.Data()));
243 fFile->SetCompressionLevel(fCompressionLevel);
245 AliRunLoader* rg = GetRunLoader();
248 AliError("Can not find Run-Loader in folder.");
251 Int_t evno = rg->GetEventNumber();
253 fDirectory = AliLoader::ChangeDir(fFile,evno);
254 if (fDirectory == 0x0)
256 AliError(Form("Can not chage fDirectory in file %s.",fFile->GetName()));
262 //______________________________________________________________________________
263 void AliDataLoader::Unload()
266 //unloads main data - shortcut method
268 GetBaseLoader(0)->Unload();
271 //______________________________________________________________________________
272 void AliDataLoader::UnloadAll()
275 // Unloads all data and tasks
277 if ( fFile == 0x0 ) return; //nothing loaded
279 TIter next(fBaseLoaders);
281 while ((bl = (AliBaseLoader*)next()))
288 //______________________________________________________________________________
289 Int_t AliDataLoader::Reload()
292 // Unloads and loads data again
294 if ( fFile == 0x0 ) return 0;
296 TBits loaded(fBaseLoaders->GetEntries());
297 TIter next(fBaseLoaders);
301 while ((bl = (AliBaseLoader*)next()))
305 loaded.SetBitNumber(i++,kTRUE);
313 while ((bl = (AliBaseLoader*)next()))
315 if (loaded.TestBitNumber(i++))
317 retval = bl->Load(fFileOption);
320 AliError(Form("Error occur while loading %s",bl->GetName()));
329 //______________________________________________________________________________
330 Int_t AliDataLoader::WriteData(Option_t* opt)
333 // Writes primary data == first BaseLoader
335 AliDebug(1, Form("Writing %s container for %s data. Option is %s.",
336 GetBaseLoader(0)->GetName(),GetName(),opt));
337 return GetBaseLoader(0)->WriteData(opt);
340 //______________________________________________________________________________
341 Int_t AliDataLoader::Load(Option_t* opt)
344 // Writes primary data == first BaseLoader
346 return GetBaseLoader(0)->Load(opt);
349 //______________________________________________________________________________
350 Int_t AliDataLoader::SetEventFolder(TFolder* eventfolder)
353 // Sets the event folder
355 if (eventfolder == 0x0)
357 AliError("Stupid joke. Argument is NULL");
360 AliDebug(1, Form("name = %s Setting Event Folder named %s.",
361 GetName(),eventfolder->GetName()));
363 fEventFolder = eventfolder;
368 //______________________________________________________________________________
369 Int_t AliDataLoader::SetFolder(TFolder* folder)
371 // Sets the folder and the data loaders
374 AliError("Stupid joke. Argument is NULL");
378 AliDebug(1, Form("name = %s Setting folder named %s.",GetName(),folder->GetName()));
381 TIter next(fBaseLoaders);
384 while ((bl = (AliBaseLoader*)next()))
386 bl->SetDataLoader(this);
392 //______________________________________________________________________________
393 TFolder* AliDataLoader::GetEventFolder()
397 // Data that are changing from event to event, even in single run
399 AliDebug(1, "EF = %#x");
403 //______________________________________________________________________________
404 AliRunLoader* AliDataLoader::GetRunLoader()
407 // Gets the run-loader from event folder
409 AliRunLoader* rg = 0x0;
410 TFolder* ef = GetEventFolder();
413 AliError("Can not get event folder.");
416 rg = dynamic_cast<AliRunLoader*>(ef->FindObject(AliRunLoader::GetRunLoaderName()));
420 //______________________________________________________________________________
421 void AliDataLoader::CloseFile()
426 TIter next(fBaseLoaders);
428 while ((bl = (AliBaseLoader*)next()))
430 if (bl->IsLoaded()) return;
433 AliDebug(1, "Closing and deleting (object) file.");
441 //______________________________________________________________________________
442 void AliDataLoader::Clean()
447 GetBaseLoader(0)->Clean();
450 //______________________________________________________________________________
451 void AliDataLoader::CleanAll()
454 // Cleans all folders and tasks
456 TIter next(fBaseLoaders);
458 while ((bl = (AliBaseLoader*)next()))
464 //______________________________________________________________________________
465 void AliDataLoader::SetFileNameSuffix(const TString& suffix)
468 // adds the suffix before ".root",
469 // e.g. TPC.Digits.root -> TPC.DigitsMerged.root
470 // made on Jiri Chudoba demand
472 AliDebug(1, Form("suffix=%s",suffix.Data()));
473 AliDebug(1, Form(" Digits File Name before: %s",fFileName.Data()));
475 static const TString dotroot(".root");
476 const TString& suffixdotroot = suffix + dotroot;
477 fFileName = fFileName.ReplaceAll(dotroot,suffixdotroot);
479 AliDebug(1, Form(" after : %s",fFileName.Data()));
482 //______________________________________________________________________________
483 Bool_t AliDataLoader::CheckReload()
486 // Checks if we have to reload given file
488 if (fFile == 0x0) return kFALSE;
489 TString tmp = SetFileOffset(fFileName);
490 if (tmp.CompareTo(fFile->GetName())) return kTRUE; //file must be reloaded
494 //______________________________________________________________________________
495 const TString AliDataLoader::SetFileOffset(const TString& fname)
500 Long_t offset = (Long_t)GetRunLoader()->GetFileOffset();
501 if (fNEventsPerFile > 0) {
502 offset = GetRunLoader()->GetEventNumber()/fNEventsPerFile;
504 if (offset < 1) return fname;
507 soffset += offset;//automatic conversion to string
508 TString dotroot(".root");
509 const TString& offfsetdotroot = offset + dotroot;
511 out = out.ReplaceAll(dotroot,offfsetdotroot);
512 AliDebug(1, Form("in=%s out=%s.",fname.Data(),out.Data()));
516 //______________________________________________________________________________
517 void AliDataLoader::SetFileOption(Option_t* newopt)
522 if (fFileOption.CompareTo(newopt) == 0) return;
523 fFileOption = newopt;
527 //______________________________________________________________________________
528 void AliDataLoader::SetCompressionLevel(Int_t cl)
531 // Sets comression level for data defined by di
533 fCompressionLevel = cl;
534 if (fFile) fFile->SetCompressionLevel(cl);
537 //______________________________________________________________________________
538 void AliDataLoader::MakeTree()
541 // Makes tree for the current data loader
543 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(fBaseLoaders->At(0));
546 AliError("Can not make a tree because main base loader is not a tree loader");
552 //______________________________________________________________________________
553 Bool_t AliDataLoader::IsFileWritable() const
556 // Returns true if file is writable
558 return (fFile)?fFile->IsWritable():kFALSE;
561 //______________________________________________________________________________
562 Bool_t AliDataLoader::IsFileOpen() const
565 // Returns true if file is writable
567 return (fFile)?fFile->IsOpen():kFALSE;
570 //______________________________________________________________________________
571 Bool_t AliDataLoader::IsOptionContrary(const TString& option) const
573 // Checks if passed option is contrary with file open option
574 // which is passed option "writable" and existing option not wriable
575 // in reverse case it is no harm so it is NOT contrary
576 if (fFile == 0x0) return kFALSE; //file is not opened - no problem
578 if ( ( AliLoader::IsOptionWritable(option) == kTRUE ) && // passed option is writable and
579 ( AliLoader::IsOptionWritable(fFileOption) == kFALSE ) ) // existing one is not writable
588 //______________________________________________________________________________
589 void AliDataLoader::AddBaseLoader(AliBaseLoader* bl)
591 //Adds a base loader to lits of base loaders managed by this data loader
592 //Managed data/task will be stored in proper root directory,
594 // - in case of tree/object - data folder connected with detector associated with this data loader
595 // - in case of task - parental task which defined in this AliTaskLoader
599 AliWarning("Pointer is null.");
603 TObject* obj = fBaseLoaders->FindObject(bl->GetName());
606 AliError("Can not add this base loader.");
607 AliError(Form("There exists already base loader which manages data named %s for this detector.",obj->GetName()));
611 fBaseLoaders->Add(bl);
614 //______________________________________________________________________________
615 AliBaseLoader* AliDataLoader::GetBaseLoader(const TString& name) const
618 // Return pointer to base loader
620 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->FindObject(name));
623 //______________________________________________________________________________
624 AliBaseLoader* AliDataLoader::GetBaseLoader(Int_t n) const
627 // Gets the n-th base loader (what is n?)
629 return dynamic_cast<AliBaseLoader*>(fBaseLoaders->At(n));
632 //______________________________________________________________________________
633 TTree* AliDataLoader::Tree() const
635 // Returns tree from the main base loader
636 // it is just shortcut method for comfort of user
637 // main storage object does not have to be Tree -
638 // that is why first we need to check if it is a TreeLoader
639 AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(GetBaseLoader(0));
640 if (tl == 0x0) return 0x0;
644 //______________________________________________________________________________
645 void AliDataLoader::SetDirName(TString& dirname)
648 // Sets the directory name where the files will be stored
650 AliDebug(10, Form("FileName before %s",fFileName.Data()));
651 Int_t n = fFileName.Last('/');
652 AliDebug(10, Form("Slash found on pos %d",n));
653 if (n > 0) fFileName = fFileName.Remove(0,n+1);
654 AliDebug(10, Form("Core FileName %s",fFileName.Data()));
655 fFileName = dirname + fFileName;
656 AliDebug(10, Form("FileName after %s",fFileName.Data()));
659 //______________________________________________________________________________
660 AliObjectLoader* AliDataLoader::GetBaseDataLoader()
663 // Gets the base data loader
665 return dynamic_cast<AliObjectLoader*>(GetBaseLoader(kData));
668 //______________________________________________________________________________
669 AliTaskLoader* AliDataLoader::GetBaseTaskLoader()
672 // Gets the base task loader
674 return dynamic_cast<AliTaskLoader*>(GetBaseLoader(kTask));
677 //______________________________________________________________________________
678 AliBaseLoader* AliDataLoader::GetBaseQALoader()
681 // Gets the base QA loader
683 return GetBaseLoader(kQA);
686 //______________________________________________________________________________
687 AliTaskLoader* AliDataLoader::GetBaseQATaskLoader()
690 // Returns pointer to QA base loader
692 return dynamic_cast<AliTaskLoader*>(GetBaseLoader(kQATask));
695 //______________________________________________________________________________
696 void AliDataLoader::SetBaseDataLoader(AliBaseLoader* bl)
699 // Sets data base loader
703 AliError("Parameter is null");
706 if (GetBaseDataLoader()) delete GetBaseDataLoader();
707 fBaseLoaders->AddAt(bl,kData);
710 //______________________________________________________________________________
711 void AliDataLoader::SetBaseTaskLoader(AliTaskLoader* bl)
714 // Sets Task base loader
718 AliError("Parameter is null");
721 if (GetBaseTaskLoader()) delete GetBaseTaskLoader();
722 fBaseLoaders->AddAt(bl,kTask);
725 //______________________________________________________________________________
726 void AliDataLoader::SetBaseQALoader(AliBaseLoader* bl)
729 // Sets QA base loader
733 AliError("Parameter is null");
736 if (GetBaseQALoader()) delete GetBaseQALoader();
737 fBaseLoaders->AddAt(bl,kQA);
740 //______________________________________________________________________________
741 void AliDataLoader::SetBaseQATaskLoader(AliTaskLoader* bl)
744 // Sets QA Task base loader
748 AliError("Parameter is null");
751 if (GetBaseQATaskLoader()) delete GetBaseQATaskLoader();
752 fBaseLoaders->AddAt(bl,kQATask);
755 //______________________________________________________________________________
756 void AliDataLoader::Synchronize()
759 // Synchronizes all writable files
761 if ( fFile ) fFile->Flush();