#include "AliDataLoader.h"
#include "AliRunLoader.h"
#include "AliLoader.h"
#include "AliObjectLoader.h"
#include "AliTreeLoader.h"
#include "AliLog.h"
#include <TFile.h>
#include <TROOT.h>
#include <TString.h>
#include <TBits.h>
ClassImp(AliDataLoader)
AliDataLoader::AliDataLoader():
fFileName(0),
fFile(0x0),
fDirectory(0x0),
fFileOption(),
fCompressionLevel(2),
fNEventsPerFile(0),
fBaseLoaders(0x0),
fEventFolder(0x0),
fFolder(0x0)
{
}
AliDataLoader::AliDataLoader(const char* filename, const char* contname,
const char* name, Option_t* opt):
TNamed(name,name),
fFileName(filename),
fFile(0x0),
fDirectory(0x0),
fFileOption(0),
fCompressionLevel(2),
fNEventsPerFile(0),
fBaseLoaders(new TObjArray(4)),
fEventFolder(0x0),
fFolder(0x0)
{
AliDebug(1, Form("File name is %s",fFileName.Data()));
TString option(opt);
AliBaseLoader* bl;
if (option.CompareTo("T",TString::kIgnoreCase) == 0)
bl = new AliTreeLoader(contname,this);
else
bl = new AliObjectLoader(contname,this);
fBaseLoaders->AddAt(bl,kData);
}
AliDataLoader::~AliDataLoader()
{
UnloadAll();
}
Int_t AliDataLoader::SetEvent()
{
AliRunLoader* rl = GetRunLoader();
if (rl == 0x0)
{
AliError("Can not get RunGettr");
return 1;
}
Int_t evno = rl->GetEventNumber();
TIter next(fBaseLoaders);
AliBaseLoader* bl;
while ((bl = (AliBaseLoader*)next()))
{
if (bl->DoNotReload() == kFALSE) bl->Clean();
}
if(fFile)
{
if (CheckReload())
{
delete fFile;
fFile = 0x0;
AliDebug(1, Form("Reloading new file. File opt is %s",fFileOption.Data()));
OpenFile(fFileOption);
}
fDirectory = AliLoader::ChangeDir(fFile,evno);
if (fDirectory == 0x0)
{
AliError(Form("Can not chage directory in file %s",fFile->GetName()));
return 1;
}
}
return 0;
}
Int_t AliDataLoader::GetEvent()
{
if ( AliLoader::TestFileOption(fFileOption) == kTRUE )
{
TIter nextbl(fBaseLoaders);
AliBaseLoader* bl;
while ((bl = (AliBaseLoader*)nextbl()))
{
if (bl->IsLoaded())
{
if (bl->DoNotReload() == kFALSE) bl->Post();
}
}
}
return 0;
}
Int_t AliDataLoader::OpenFile(Option_t* opt)
{
if (fFile)
{
if(fFile->IsOpen() == kTRUE)
{
AliWarning(Form(" File %s already opened. First close it.",fFile->GetName()));
return 0;
}
else
{
AliWarning(Form("Pointer to file %s is not null, but file is not opened",
fFile->GetName()));
delete fFile;
fFile = 0x0;
}
}
TString fname(SetFileOffset(fFileName));
fFile = (TFile *)(gROOT->GetListOfFiles()->FindObject(fname));
if (fFile)
{
if(fFile->IsOpen() == kTRUE)
{
TString option1 = fFile->GetOption();
if (option1.CompareTo("read",TString::kIgnoreCase) == 0)
{
AliInfo(Form("File %s already opened in read mode.",fFile->GetName()));
}
else
{
TString option2 = opt;
if (option2.CompareTo("read",TString::kIgnoreCase) == 0)
{
AliInfo(Form("Open already opened file %s in read mode.",fFile->GetName()));
}
else {
AliWarning(Form("File %s already opened by sombody else. First close it.",
fFile->GetName()));
return 0;
}
}
}
}
fFileOption = opt;
fFile = TFile::Open(fname,fFileOption);
if (fFile == 0x0)
{
AliError(Form("Can not open file %s",fname.Data()));
return 1;
}
if (fFile->IsOpen() == kFALSE)
{
AliError(Form("Can not open file %s",fname.Data()));
return 1;
}
fFile->SetBit(TFile::kDevNull);
fFile->SetCompressionLevel(fCompressionLevel);
AliRunLoader* rg = GetRunLoader();
if (rg == 0x0)
{
AliError("Can not find Run-Loader in folder.");
return 2;
}
Int_t evno = rg->GetEventNumber();
fDirectory = AliLoader::ChangeDir(fFile,evno);
if (fDirectory == 0x0)
{
AliError(Form("Can not chage fDirectory in file %s.",fFile->GetName()));
return 3;
}
return 0;
}
void AliDataLoader::Unload()
{
GetBaseLoader(0)->Unload();
}
void AliDataLoader::UnloadAll()
{
if ( fFile == 0x0 ) return;
TIter next(fBaseLoaders);
AliBaseLoader* bl;
while ((bl = (AliBaseLoader*)next()))
{
bl->Unload();
}
}
Int_t AliDataLoader::Reload()
{
if ( fFile == 0x0 ) return 0;
TBits loaded(fBaseLoaders->GetEntries());
TIter next(fBaseLoaders);
AliBaseLoader* bl;
Int_t i = 0;
while ((bl = (AliBaseLoader*)next()))
{
if (bl->IsLoaded())
{
loaded.SetBitNumber(i++,kTRUE);
bl->Unload();
}
}
Int_t retval;
i = 0;
next.Reset();
while ((bl = (AliBaseLoader*)next()))
{
if (loaded.TestBitNumber(i++))
{
retval = bl->Load(fFileOption);
if (retval)
{
AliError(Form("Error occur while loading %s",bl->GetName()));
return retval;
}
}
}
return 0;
}
Int_t AliDataLoader::WriteData(Option_t* opt)
{
AliDebug(1, Form("Writing %s container for %s data. Option is %s.",
GetBaseLoader(0)->GetName(),GetName(),opt));
return GetBaseLoader(0)->WriteData(opt);
}
Int_t AliDataLoader::Load(Option_t* opt)
{
return GetBaseLoader(0)->Load(opt);
}
Int_t AliDataLoader::SetEventFolder(TFolder* eventfolder)
{
if (eventfolder == 0x0)
{
AliError("Stupid joke. Argument is NULL");
return 1;
}
AliDebug(1, Form("name = %s Setting Event Folder named %s.",
GetName(),eventfolder->GetName()));
fEventFolder = eventfolder;
return 0;
}
Int_t AliDataLoader::SetFolder(TFolder* folder)
{
if (folder == 0x0)
{
AliError("Stupid joke. Argument is NULL");
return 1;
}
AliDebug(1, Form("name = %s Setting folder named %s.",GetName(),folder->GetName()));
fFolder = folder;
TIter next(fBaseLoaders);
AliBaseLoader* bl;
while ((bl = (AliBaseLoader*)next()))
{
bl->SetDataLoader(this);
}
return 0;
}
TFolder* AliDataLoader::GetEventFolder()
{
AliDebug(1, "EF = %#x");
return fEventFolder;
}
AliRunLoader* AliDataLoader::GetRunLoader()
{
AliRunLoader* rg = 0x0;
TFolder* ef = GetEventFolder();
if (ef == 0x0)
{
AliError("Can not get event folder.");
return 0;
}
rg = dynamic_cast<AliRunLoader*>(ef->FindObject(AliRunLoader::GetRunLoaderName()));
return rg;
}
void AliDataLoader::CloseFile()
{
TIter next(fBaseLoaders);
AliBaseLoader* bl;
while ((bl = (AliBaseLoader*)next()))
{
if (bl->IsLoaded()) return;
}
AliDebug(1, "Closing (object) file.");
if (fFile) {
fFile->Close("R");
delete fFile;
fFile = 0x0;
}
fDirectory = 0x0;
}
void AliDataLoader::Clean()
{
GetBaseLoader(0)->Clean();
}
void AliDataLoader::CleanAll()
{
TIter next(fBaseLoaders);
AliBaseLoader* bl;
while ((bl = (AliBaseLoader*)next()))
{
bl->Clean();
}
}
void AliDataLoader::SetFileNameSuffix(const TString& suffix)
{
AliDebug(1, Form("suffix=%s",suffix.Data()));
AliDebug(1, Form(" Digits File Name before: %s",fFileName.Data()));
static const TString dotroot(".root");
const TString& suffixdotroot = suffix + dotroot;
fFileName = fFileName.ReplaceAll(dotroot,suffixdotroot);
AliDebug(1, Form(" after : %s",fFileName.Data()));
}
Bool_t AliDataLoader::CheckReload()
{
if (fFile == 0x0) return kFALSE;
TString tmp = SetFileOffset(fFileName);
if (tmp.CompareTo(fFile->GetName())) return kTRUE;
return kFALSE;
}
const TString AliDataLoader::SetFileOffset(const TString& fname)
{
Long_t offset = (Long_t)GetRunLoader()->GetFileOffset();
if (fNEventsPerFile > 0) {
offset = GetRunLoader()->GetEventNumber()/fNEventsPerFile;
}
if (offset < 1) return fname;
TString soffset;
soffset += offset;
TString dotroot(".root");
const TString& offfsetdotroot = offset + dotroot;
TString out = fname;
out = out.ReplaceAll(dotroot,offfsetdotroot);
AliDebug(1, Form("in=%s out=%s.",fname.Data(),out.Data()));
return out;
}
void AliDataLoader::SetFileOption(Option_t* newopt)
{
if (fFileOption.CompareTo(newopt) == 0) return;
fFileOption = newopt;
Reload();
}
void AliDataLoader::SetCompressionLevel(Int_t cl)
{
fCompressionLevel = cl;
if (fFile) fFile->SetCompressionLevel(cl);
}
void AliDataLoader::MakeTree()
{
AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(fBaseLoaders->At(0));
if (tl == 0x0)
{
AliError("Can not make a tree because main base loader is not a tree loader");
return;
}
tl->MakeTree();
}
Bool_t AliDataLoader::IsFileWritable() const
{
return (fFile)?fFile->IsWritable():kFALSE;
}
Bool_t AliDataLoader::IsFileOpen() const
{
return (fFile)?fFile->IsOpen():kFALSE;
}
Bool_t AliDataLoader::IsOptionContrary(const TString& option) const
{
if (fFile == 0x0) return kFALSE;
if ( ( AliLoader::IsOptionWritable(option) == kTRUE ) &&
( AliLoader::IsOptionWritable(fFileOption) == kFALSE ) )
{
return kTRUE;
}
return kFALSE;
}
void AliDataLoader::AddBaseLoader(AliBaseLoader* bl)
{
if (bl == 0x0)
{
AliWarning("Pointer is null.");
return;
}
TObject* obj = fBaseLoaders->FindObject(bl->GetName());
if (obj)
{
AliError("Can not add this base loader.");
AliError(Form("There exists already base loader which manages data named %s for this detector.",obj->GetName()));
return;
}
fBaseLoaders->Add(bl);
}
AliBaseLoader* AliDataLoader::GetBaseLoader(const TString& name) const
{
return dynamic_cast<AliBaseLoader*>(fBaseLoaders->FindObject(name));
}
AliBaseLoader* AliDataLoader::GetBaseLoader(Int_t n) const
{
return dynamic_cast<AliBaseLoader*>(fBaseLoaders->At(n));
}
TTree* AliDataLoader::Tree() const
{
AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(GetBaseLoader(0));
if (tl == 0x0) return 0x0;
return tl->Tree();
}
void AliDataLoader::SetDirName(TString& dirname)
{
AliDebug(10, Form("FileName before %s",fFileName.Data()));
Int_t n = fFileName.Last('/');
AliDebug(10, Form("Slash found on pos %d",n));
if (n > 0) fFileName = fFileName.Remove(0,n+1);
AliDebug(10, Form("Core FileName %s",fFileName.Data()));
fFileName = dirname + fFileName;
AliDebug(10, Form("FileName after %s",fFileName.Data()));
}
AliObjectLoader* AliDataLoader::GetBaseDataLoader()
{
return dynamic_cast<AliObjectLoader*>(GetBaseLoader(kData));
}
void AliDataLoader::SetBaseDataLoader(AliBaseLoader* bl)
{
if (bl == 0x0)
{
AliError("Parameter is null");
return;
}
if (GetBaseDataLoader()) delete GetBaseDataLoader();
fBaseLoaders->AddAt(bl,kData);
}
void AliDataLoader::Synchronize()
{
if ( fFile ) fFile->Flush();
}