#include <TKey.h>
#include <TH1.h>
#include <TTree.h>
#include <TNtuple.h>
#include <TFile.h>
#include "AliCDBStorage.h"
#include "AliCDBGrid.h"
#include "AliCDBEntry.h"
#include "AliLog.h"
ClassImp(AliCDBStorage)
AliCDBStorage::AliCDBStorage():
fValidFileIds(),
fRun(-1),
fPathFilter(),
fVersion(-1),
fMetaDataFilter(0),
fSelections(),
fURI(),
fType(),
fBaseFolder(),
fNretry(0),
fInitRetrySeconds(0)
{
fValidFileIds.SetOwner(1);
fSelections.SetOwner(1);
}
AliCDBStorage::~AliCDBStorage() {
RemoveAllSelections();
fValidFileIds.Clear();
delete fMetaDataFilter;
}
void AliCDBStorage::GetSelection( AliCDBId* id) {
TIter iter(&fSelections);
AliCDBId* aSelection;
while ((aSelection = (AliCDBId*) iter.Next())) {
if (aSelection->Comprises(*id)) {
AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
id->SetVersion(aSelection->GetVersion());
id->SetSubVersion(aSelection->GetSubVersion());
return;
}
}
AliDebug(2,"Looking for objects with most recent version");
return;
}
void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
RemoveAllSelections();
TList *list = GetIdListFromFile(fileName);
if(!list) return;
list->SetOwner();
Int_t nId = list->GetEntries();
AliCDBId *id;
TKey *key;
for(int i=nId-1;i>=0;i--){
key = (TKey*) list->At(i);
id = (AliCDBId*) key->ReadObj();
if(id) AddSelection(*id);
}
delete list;
AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
PrintSelectionList();
}
void AliCDBStorage::AddSelection(const AliCDBId& selection) {
AliCDBPath path = selection.GetPath();
if(!path.IsValid()) return;
TIter iter(&fSelections);
const AliCDBId *anId;
while((anId = (AliCDBId*) iter.Next())){
if(selection.Comprises(*anId)){
AliWarning("This selection is more general than a previous one and will hide it!");
AliWarning(Form("%s", (anId->ToString()).Data()));
fSelections.AddBefore(anId, new AliCDBId(selection));
return;
}
}
fSelections.AddFirst(new AliCDBId(selection));
}
void AliCDBStorage::AddSelection(const AliCDBPath& path,
const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
AddSelection(AliCDBId(path, runRange, version, subVersion));
}
void AliCDBStorage::AddSelection(const AliCDBPath& path,
Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
}
void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
TIter iter(&fSelections);
AliCDBId* aSelection;
while ((aSelection = (AliCDBId*) iter.Next())) {
if (selection.Comprises(*aSelection)) {
fSelections.Remove(aSelection);
}
}
}
void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
const AliCDBRunRange& runRange){
RemoveSelection(AliCDBId(path, runRange, -1, -1));
}
void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
Int_t firstRun, Int_t lastRun){
RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
}
void AliCDBStorage::RemoveSelection(int position){
delete fSelections.RemoveAt(position);
}
void AliCDBStorage::RemoveAllSelections(){
fSelections.Clear();
}
void AliCDBStorage::PrintSelectionList(){
TIter iter(&fSelections);
AliCDBId* aSelection;
int index=0;
while ((aSelection = (AliCDBId*) iter.Next())) {
AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
}
}
AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
if (!query.IsValid()) {
AliError(Form("Invalid query: %s", query.ToString().Data()));
return NULL;
}
if (!query.IsSpecified()) {
AliError(Form("Unspecified query: %s",
query.ToString().Data()));
return NULL;
}
Bool_t oldStatus = TH1::AddDirectoryStatus();
TH1::AddDirectory(kFALSE);
AliCDBEntry* entry = GetEntry(query);
if (oldStatus != kFALSE)
TH1::AddDirectory(kTRUE);
if(entry && (AliCDBManager::Instance())->IsDrainSet())
AliCDBManager::Instance()->Drain(entry);
return entry;
}
AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber,
Int_t version, Int_t subVersion) {
return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
}
AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
const AliCDBRunRange& runRange, Int_t version,
Int_t subVersion) {
return Get(AliCDBId(path, runRange, version, subVersion));
}
TList* AliCDBStorage::GetAll(const AliCDBId& query) {
if (!query.IsValid()) {
AliError(Form("Invalid query: %s", query.ToString().Data()));
return NULL;
}
if (query.IsAnyRange()) {
AliError(Form("Unspecified run or runrange: %s",
query.ToString().Data()));
return NULL;
}
Bool_t oldStatus = TH1::AddDirectoryStatus();
TH1::AddDirectory(kFALSE);
TList *result = GetEntries(query);
if (oldStatus != kFALSE)
TH1::AddDirectory(kTRUE);
Int_t nEntries = result->GetEntries();
AliInfo(Form("%d objects retrieved. Request was: %s",
nEntries, query.ToString().Data()));
for(int i=0; i<nEntries;i++){
AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
AliInfo(Form("%s",entry->GetId().ToString().Data()));
}
if((AliCDBManager::Instance())->IsDrainSet()){
for(int i = 0; i<result->GetEntries(); i++){
AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
AliCDBManager::Instance()->Drain(entry);
}
}
return result;
}
TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber,
Int_t version, Int_t subVersion) {
return GetAll(AliCDBId(path, runNumber, runNumber, version,
subVersion));
}
TList* AliCDBStorage::GetAll(const AliCDBPath& path,
const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
return GetAll(AliCDBId(path, runRange, version, subVersion));
}
AliCDBId* AliCDBStorage::GetId(const AliCDBId& query) {
if (!query.IsValid()) {
AliError(Form("Invalid query: %s", query.ToString().Data()));
return NULL;
}
if (!query.IsSpecified()) {
AliError(Form("Unspecified query: %s",
query.ToString().Data()));
return NULL;
}
AliCDBId* id = GetEntryId(query);
return id;
}
AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path, Int_t runNumber,
Int_t version, Int_t subVersion) {
return GetId(AliCDBId(path, runNumber, runNumber, version, subVersion));
}
AliCDBId* AliCDBStorage::GetId(const AliCDBPath& path,
const AliCDBRunRange& runRange, Int_t version,
Int_t subVersion) {
return GetId(AliCDBId(path, runRange, version, subVersion));
}
Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, AliCDBManager::DataType type) {
if (object==0x0) {
AliError("Null Entry! No storage will be done!");
return kFALSE;
}
AliCDBEntry anEntry(object, id, metaData);
return Put(&anEntry, mirrors, type);
}
Bool_t AliCDBStorage::Put(AliCDBEntry* entry, const char* mirrors, AliCDBManager::DataType type) {
if (!entry){
AliError("No entry!");
return kFALSE;
}
if (entry->GetObject()==0x0){
AliError("No valid object in CDB entry!");
return kFALSE;
}
if (!entry->GetId().IsValid()) {
AliError(Form("Invalid entry ID: %s",
entry->GetId().ToString().Data()));
return kFALSE;
}
if (!entry->GetId().IsSpecified()) {
AliError(Form("Unspecified entry ID: %s",
entry->GetId().ToString().Data()));
return kFALSE;
}
AliCDBManager::DataType expectedType = GetDataType();
if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
AliError(Form("It is forbidden to store %s data into a folder of type %s!",
AliCDBManager::GetDataTypeName(type),
AliCDBManager::GetDataTypeName(expectedType)));
return 0;
}
TString strMirrors(mirrors);
if(!strMirrors.IsNull() && !strMirrors.IsWhitespace())
return PutEntry(entry, mirrors);
else
return PutEntry(entry);
}
void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
Int_t version, AliCDBMetaData* md){
fRun = run;
fPathFilter = pathFilter;
if(!fPathFilter.IsValid()) {
AliError(Form("Filter not valid: %s", pathFilter));
fPathFilter = "*";
return;
}
fVersion = version;
AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage \"%s://%s\"",
fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
AliDebug(3, Form("Clearing list of CDB Id's previously loaded for path \"%s\"", pathFilter));
AliCDBPath filter(pathFilter);
for (Int_t i=fValidFileIds.GetEntries()-1; i>=0; --i) {
AliCDBId *rmMe = dynamic_cast<AliCDBId*>(fValidFileIds.At(i));
AliCDBPath rmPath = rmMe->GetAliCDBPath();
if (filter.Comprises(rmPath)) {
AliDebug(3,Form("Removing id \"%s\" matching: \"%s\"", rmPath.GetPath().Data(), pathFilter));
delete fValidFileIds.RemoveAt(i);
}
}
if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
QueryValidFiles();
AliInfo(Form("%d valid files found!", fValidFileIds.GetEntries()));
}
void AliCDBStorage::PrintQueryCDB(){
AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
paramId.ToString().Data()));
if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
TString message = "**** Id's of valid objects found *****\n";
TIter iter(&fValidFileIds);
AliCDBId* anId=0;
while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
message += Form("\t%s\n", anId->ToString().Data());
}
message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
AliInfo(Form("%s", message.Data()));
}
AliCDBManager::DataType AliCDBStorage::GetDataType() const {
if(GetType() != "alien") return AliCDBManager::kPrivate;
TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
return AliCDBManager::kPrivate;
}
void AliCDBStorage::SetMirrorSEs(const char* mirrors) {
TString storageType = GetType();
if(storageType != "alien"){
AliWarning(Form("The current storage is of type \"%s\". Setting of SEs to \"%s\" skipped!",storageType.Data(),mirrors));
return;
}
AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
return;
}
const char* AliCDBStorage::GetMirrorSEs() const {
TString storageType = GetType();
if(storageType != "alien"){
AliWarning(Form("The current storage is of type \"%s\" and cannot handle SEs. Returning empty string!",storageType.Data()));
return "";
}
AliError("We should never get here!! AliCDBGrid must have masked this virtual method!");
return "";
}
void AliCDBStorage::LoadTreeFromFile(AliCDBEntry *entry) const {
TObject *obj = (TObject*) entry->GetObject();
if (!obj) {
AliError("Cannot retrieve the object:");
entry->PrintMetaData();
return;
}
if (!strcmp(obj->ClassName(),TTree::Class_Name())) {
AliWarning("Entry contains a TTree! Loading baskets...");
TTree* tree = dynamic_cast<TTree*> (obj);
if(!tree) return;
tree->LoadBaskets();
tree->SetDirectory(0);
}
else if (!strcmp(obj->ClassName(),TNtuple::Class_Name())){
AliWarning("Entry contains a TNtuple! Loading baskets...");
TNtuple* ntu = dynamic_cast<TNtuple*> (obj);
if(!ntu) return;
ntu->LoadBaskets();
ntu->SetDirectory(0);
}
return;
}