#include "AliBaseLoader.h"
#include "AliTreeLoader.h"
#include "AliDataLoader.h"
#include "AliLoader.h"
#include "AliLog.h"
#include <TFile.h>
#include <TString.h>
ClassImp(AliBaseLoader)
AliBaseLoader::AliBaseLoader():
fIsLoaded(kFALSE),
fStoreInTopOfFile(kFALSE),
fDoNotReload(kFALSE),
fDataLoader(0x0)
{
}
AliBaseLoader::AliBaseLoader(const TString& name, AliDataLoader* dl, Bool_t storeontop):
TNamed(name,name+" Base Loader"),
fIsLoaded(kFALSE),
fStoreInTopOfFile(storeontop),
fDoNotReload(storeontop),
fDataLoader(dl)
{
}
Int_t AliBaseLoader::Load(Option_t* opt)
{
AliDebug(1, Form("data type = %s, option = %s",GetName(),opt));
if (Get())
{
AliDebug(1,Form("Data <<%s>> are already loaded. Use ReloadData to force reload. Nothing done",GetName()));
return 0;
}
Int_t retval;
if (GetDataLoader()->IsFileOpen() == kTRUE)
{
if (GetDataLoader()->IsOptionContrary(opt) == kTRUE)
{
AliError(Form("Data Type %s, Container Name %s", GetDataLoader()->GetName(),GetName()));
AliError("File was already opened in READ-ONLY mode, while now WRITEABLE access is requested.");
AliError("Use AliDataLoader::SetOption to enforce change of access mode OR");
AliError("Load previosly loaded data with coherent option.");
return 10;
}
}
else
{
retval = GetDataLoader()->OpenFile(opt);
if (retval)
{
AliError(Form("Error occured while opening <<%s>> file",GetName()));
return retval;
}
}
if (AliLoader::TestFileOption(opt) == kFALSE)
{
AliTreeLoader* tl = dynamic_cast<AliTreeLoader*>(this);
if (tl) tl->MakeTree();
fIsLoaded = kTRUE;
return 0;
}
retval = Post();
if (retval)
{
AliError(Form("Error occured while posting %s from file to folder.",GetName()));
return retval;
}
fIsLoaded = kTRUE;
return 0;
}
Int_t AliBaseLoader::Post()
{
if ( GetDirectory() == 0x0)
{
AliError(Form("%s directory is NULL. Load before.",GetDataLoader()->GetName()));
return 2;
}
TObject* data = GetFromDirectory(fName);
if(data)
{
return Post(data);
}
else
{
Int_t fileupdate = GetDataLoader()->GetFileOption().CompareTo("update",TString::kIgnoreCase);
if ( fileupdate == 0)
{
AliDebug(1, Form("Can not find %s in file %s (file is opened in UPDATE mode).",
GetName(),GetDataLoader()->GetFile()->GetName()));
}
else
{
AliError(Form("Can not find %s in file %s", GetName(),GetDataLoader()->GetFile()->GetName()));
return 5;
}
}
return 0;
}
Int_t AliBaseLoader::Post(TObject* data)
{
if (data == 0x0)
{
AliError("Pointer to object is NULL");
return 1;
}
if ( fName.CompareTo(data->GetName()) != 0)
{
AliFatal(Form("Object name is <<%s>> while <<%s>> expected",data->GetName(),GetName()));
return -1;
}
TObject* obj = Get();
if (data == obj)
{
AliWarning("This object was already posted.");
return 0;
}
if (obj)
{
AliWarning(Form("Object named %s already exitsts in data folder. Removing it",GetName()));
Clean();
}
return AddToBoard(data);
}
Int_t AliBaseLoader::WriteData(Option_t* opt)
{
AliDebug(1, Form("Writing %s container for %s data. Option is %s.",
GetName(),GetDataLoader()->GetName(),opt));
TObject *data = Get();
if(data == 0x0)
{
AliWarning(Form("Tree named %s not found in folder. Nothing to write.",GetName()));
return 0;
}
if (GetDirectory() == 0x0)
{
GetDataLoader()->SetFileOption("UPDATE");
if (GetDataLoader()->OpenFile("UPDATE"))
{
AliError(Form("Can not open hits file. %s ARE NOT WRITTEN",GetName()));
return 1;
}
}
if (GetDataLoader()->IsFileWritable() == kFALSE)
{
AliError(Form("File %s is not writable",GetDataLoader()->GetFileName().Data()));
return 2;
}
GetDirectory()->cd();
TObject* obj = GetFromDirectory(GetName());
if (obj)
{
const char *oOverWrite = strstr(opt,"OVERWRITE");
if(!oOverWrite)
{
AliError("Tree already exisists. Use option \"OVERWRITE\" to overwrite previous data");
return 3;
}
}
AliDebug(1, Form("DataName = %s, opt = %s, data object name = %s",
GetName(),opt,data->GetName()));
AliDebug(1, Form("File Name = %s, Directory Name = %s Directory's File Name = %s",
GetDataLoader()->GetFile()->GetName(),GetDirectory()->GetName(),
GetDirectory()->GetFile()->GetName()));
AliDebug(1, "Writing data");
data->Write(0,TObject::kOverwrite);
fIsLoaded = kTRUE;
return 0;
}
Int_t AliBaseLoader::Reload()
{
if (IsLoaded())
{
Unload();
return Load(GetDataLoader()->GetFileOption());
}
return 0;
}
void AliBaseLoader::Clean()
{
AliDebug(1, Form("%s %s",GetName(),GetDataLoader()->GetName()));
TObject* obj = Get();
if(obj)
{
AliDebug(1, Form("cleaning %s.",GetName()));
RemoveFromBoard(obj);
delete obj;
}
}
void AliBaseLoader::Unload()
{
Clean();
fIsLoaded = kFALSE;
GetDataLoader()->CloseFile();
}
AliDataLoader* AliBaseLoader::GetDataLoader() const
{
if (fDataLoader == 0x0)
{
AliFatal("Pointer to Data Loader is NULL");
}
return fDataLoader;
}
TDirectory* AliBaseLoader::GetDirectory() const
{
return (fStoreInTopOfFile)?GetDataLoader()->GetFile():GetDataLoader()->GetDirectory();
}