]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCDBGrid.cxx
Added functionality to get TTree's from the OCDB. The tree is loaded in memory
[u/mrichter/AliRoot.git] / STEER / AliCDBGrid.cxx
index fc8fd41111146fa0db3eb91486b817b725f3b618..3725967ccf875ccba2a1924cb1cbe35c40ad138c 100644 (file)
@@ -26,7 +26,7 @@
 #include <TFile.h>
 #include <TKey.h>
 #include <TROOT.h>
-#include <TSystem.h>
+//#include <TSystem.h>
 #include <TObjArray.h>
 #include <TObjString.h>
 #include <TRegexp.h>
@@ -101,7 +101,7 @@ AliCDBGrid::~AliCDBGrid()
 
 //_____________________________________________________________________________
 Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
-// build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z.root)
+// build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
 
        if(filename.Contains(fDBFolder)){
                filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
@@ -114,13 +114,26 @@ Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
        filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
 
         Ssiz_t mSize;
-       // valid filename: Run#firstRun_#lastRun_v#version.root
-        TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
+       // valid filename: Run#firstRun_#lastRun_v#version_s0.root
+        TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$");
         keyPattern.Index(filename, &mSize);
         if (!mSize) {
-               AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
-                return kFALSE;
-        }
+
+               // TODO backward compatibility ... maybe remove later!
+               Ssiz_t oldmSize;
+               TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
+               oldKeyPattern.Index(filename, &oldmSize);
+               if(!oldmSize) {
+                       AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
+                       return kFALSE;
+               } else {
+                       AliDebug(2,Form("Old filename format <%s>.", filename.Data()));
+                       id.SetSubVersion(-11); // TODO trick to ensure backward compatibility
+               }
+
+        } else {
+               id.SetSubVersion(-1); // TODO trick to ensure backward compatibility
+       }
 
        filename.Resize(filename.Length() - sizeof(".root") + 1);
 
@@ -139,7 +152,7 @@ Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
 }
 
 //_____________________________________________________________________________
-Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) {
+Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
 // build file name from AliCDBId (path, run range, version) and fDBFolder
 
        if (!id.GetAliCDBRunRange().IsValid()) {
@@ -153,11 +166,14 @@ Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) {
                 return kFALSE;
        }
 
-       filename = Form("Run%d_%d_v%d.root",
+       filename = Form("Run%d_%d_v%d",
                                id.GetFirstRun(),
                                id.GetLastRun(),
                                id.GetVersion());
 
+       if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
+       filename += ".root";
+
        filename.Prepend(fDBFolder + id.GetPath() + '/');
 
         return kTRUE;
@@ -173,7 +189,7 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
        TString dirName(fDBFolder);
 
        Bool_t dirExist=kFALSE;
+
        // go to the path; if directory does not exist, create it
        TObjArray *arrName=pathName.Tokenize("/");
        for(int i=0;i<arrName->GetEntries();i++){
@@ -209,9 +225,10 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
                }
 
        }
-       delete res; 
+       delete res;
+
        id.SetVersion(lastVersion + 1);
+       id.SetSubVersion(0);
 
        TString lastStorage = id.GetLastStorage();
        if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
@@ -221,20 +238,20 @@ Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
                                        id.GetVersion()-1));
        }
 
-       if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange()))) 
+       if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
                AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
                        lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
-    
+
        return kTRUE;
 }
 
 //_____________________________________________________________________________
-AliCDBId* AliCDBGrid::GetId(const TList& validFileIds, const AliCDBId& query) {
+AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
 // look for the Id that matches query's requests (highest or exact version)
 
-       if(validFileIds.GetEntries() < 1) {
+       if(validFileIds.GetEntriesFast() < 1) {
                return NULL;
-       } else if (validFileIds.GetEntries() == 1) {
+       } else if (validFileIds.GetEntriesFast() == 1) {
                return dynamic_cast<AliCDBId*> (validFileIds.At(0));
        }
 
@@ -244,7 +261,6 @@ AliCDBId* AliCDBGrid::GetId(const TList& validFileIds, const AliCDBId& query) {
        AliCDBId* result=0;
 
        while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
-
                if(anIdPtr->GetPath() != query.GetPath()) continue;
 
                //if(!CheckVersion(query, anIdPtr, result)) return NULL;
@@ -273,41 +289,6 @@ AliCDBId* AliCDBGrid::GetId(const TList& validFileIds, const AliCDBId& query) {
        return result;
 }
 
-/* TODO remove
-//_____________________________________________________________________________
-Bool_t AliCDBGrid::CheckVersion(const AliCDBId& query, AliCDBId* idToCheck, AliCDBId* result){
-// Check if idToCheck has the "right" requested version and return it in result
-
-       if (!result) {
-               result = idToCheck;
-               AliInfo(Form("all'inizio: result = %s", result->ToString().Data()));
-               return kTRUE;
-       }
-
-       AliInfo(Form("result = %s", result->ToString().Data()));
-
-       if (!query.HasVersion()){ // look for highest version
-               if(result->GetVersion() > idToCheck->GetVersion()) return kTRUE;
-               if(result->GetVersion() == idToCheck->GetVersion()) {
-                       AliDebug(2,Form("More than one object valid for run %d, version %d!",
-                               query.GetFirstRun(), idToCheck->GetVersion()));
-                       return kFALSE;
-               }
-
-       } else { // look for specified version
-               if(query.GetVersion() != idToCheck->GetVersion()) return kTRUE;
-               if(result->GetVersion() == idToCheck->GetVersion()){
-                       AliDebug(2,Form("More than one object valid for run %d, version %d!",
-                               query.GetFirstRun(), idToCheck->GetVersion()));
-                       return kFALSE;
-               }
-       }
-       result = idToCheck;
-       AliInfo(Form("alla fine: result = %s", result->ToString().Data()));
-       return kTRUE;
-}
-*/
-
 //_____________________________________________________________________________
 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
 // get AliCDBEntry from the database
@@ -320,12 +301,12 @@ AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
                GetSelection(&selectedId);
        }
 
-       TList validFileIds;
+       TObjArray validFileIds;
        validFileIds.SetOwner(1);
 
        // look for file matching query requests (path, runRange, version)
        if(selectedId.GetFirstRun() == fRun &&
-                       fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0){
+                       fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
                // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
                AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
                                        selectedId.GetFirstRun(), selectedId.GetPath().Data()));
@@ -337,13 +318,18 @@ AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
                                        selectedId.GetFirstRun(), selectedId.GetPath().Data()));
 
                TString filter;
-               MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(),
-                               selectedId.GetAliCDBPath(), selectedId.GetVersion(), 0, filter);
+               MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
 
-               TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
+               TString pattern = Form("%s/Run*", selectedId.GetPath().Data());
+               if(selectedId.GetVersion() >= 0) pattern += Form("_v%d*",selectedId.GetVersion());
+               pattern += ".root";
+               AliDebug(2,Form("pattern: %s", pattern.Data()));
+
+               TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
                AliCDBId validFileId;
                for(int i=0; i<res->GetEntries(); i++){
                        TString filename = res->GetKey(i, "lfn");
+                       if(filename == "") continue;
                        if(FilenameToId(filename, validFileId))
                                        validFileIds.AddLast(validFileId.Clone());
                }
@@ -365,7 +351,7 @@ AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
 }
 
 //_____________________________________________________________________________
-AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, const AliCDBId* dataId){
+AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
 // Get AliCBEntry object from file "filename"
 
        AliDebug(2,Form("Opening file: %s",filename.Data()));
@@ -394,16 +380,21 @@ AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, const AliCDBId* dat
 
        if(anEntry){
                AliCDBId entryId = anEntry->GetId();
-               if(!((entryId.GetAliCDBRunRange()).IsEqual(&(dataId->GetAliCDBRunRange()))) ||
-                       entryId.GetVersion() != dataId->GetVersion()){
-                       AliWarning(Form("Either RunRange or gridVersion in the object's metadata"));
-                       AliWarning(Form("do noth match with fileName numbers:"));
-                       AliWarning(Form("someone renamed file by hand!"));
+               Int_t tmpSubVersion = dataId->GetSubVersion();
+               dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
+               if(!entryId.IsEqual(dataId)){
+                       AliWarning(Form("Mismatch between file name and object's Id!"));
+                       AliWarning(Form("File name: %s", dataId->ToString().Data()));
+                       AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
                }
+               dataId->SetSubVersion(tmpSubVersion);
        }
 
        anEntry->SetLastStorage("grid");
 
+       // Check whether entry contains a TTree. In case load the tree in memory!
+       LoadTreeFromFile(anEntry);
+
        // close file, return retieved entry
        file->Close(); delete file; file=0;
 
@@ -417,14 +408,14 @@ TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
        TList* result = new TList();
        result->SetOwner();
 
-       TList validFileIds;
+       TObjArray validFileIds;
        validFileIds.SetOwner(1);
 
        Bool_t alreadyLoaded = kFALSE;
 
        // look for file matching query requests (path, runRange)
        if(queryId.GetFirstRun() == fRun &&
-                       fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0){
+                       fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
                // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
                AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
                                        queryId.GetFirstRun(), queryId.GetPath().Data()));
@@ -437,13 +428,17 @@ TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
                                        queryId.GetFirstRun(), queryId.GetPath().Data()));
 
                TString filter;
-               MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(),
-                               queryId.GetAliCDBPath(), queryId.GetVersion(), 0, filter);
+               MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
+
+               TString pattern = Form("%s/Run*.root", queryId.GetPath().Data());
+               AliDebug(2,Form("pattern: %s", pattern.Data()));
+
+               TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
 
-               TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
                AliCDBId validFileId;
                for(int i=0; i<res->GetEntries(); i++){
                        TString filename = res->GetKey(i, "lfn");
+                       if(filename == "") continue;
                        if(FilenameToId(filename, validFileId))
                                        validFileIds.AddLast(validFileId.Clone());
                }
@@ -457,7 +452,7 @@ TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
                iter = new TIter(&validFileIds);
        }
 
-       TList selectedIds;
+       TObjArray selectedIds;
        selectedIds.SetOwner(1);
 
        // loop on list of valid Ids to select the right version to get.
@@ -528,39 +523,49 @@ Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
 
        // add CDB and CDB_MD tag to folder
        // TODO how to check that folder has already tags?
-       Bool_t tagCDB = AddTag(folderToTag,"CDB");
-       Bool_t tagCDBmd = AddTag(folderToTag,"CDB_MD");
+       AddTag(folderToTag,"CDB");
+       AddTag(folderToTag,"CDB_MD");
 
        TDirectory* saveDir = gDirectory;
 
+       TString fullFilename = Form("/alien%s", filename.Data());
        // specify SE to filename
-       TString fullFilename = Form("/alien%s?se=%s", filename.Data(), fSE.Data());
+       if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
 
        // open file
-       TFile *file = TFile::Open(fullFilename,"CREATE");
-       if(!file || !file->IsWritable()){
-               AliError(Form("Can't open file <%s>!", filename.Data()));
-               if(file && !file->IsWritable()) file->Close(); delete file; file=0;
-               return kFALSE;
-       }
+       TFile *file = TFile::Open(fullFilename,"CREATE");
+       if(!file || !file->IsWritable()){
+               AliError(Form("Can't open file <%s>!", filename.Data()));
+               if(file && !file->IsWritable()) file->Close(); delete file; file=0;
+               return kFALSE;
+       }
 
        file->cd();
 
+       SetTreeToFile(entry, file);
+
        entry->SetVersion(id.GetVersion());
 
        // write object (key name: "AliCDBEntry")
-       Bool_t result = (entry->Write("AliCDBEntry") != 0); 
+       Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
        if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
 
 
        if (saveDir) saveDir->cd(); else gROOT->cd();
        file->Close(); delete file; file=0;
+
        if(result) {
                AliInfo(Form("CDB object stored into file %s", filename.Data()));
-               AliInfo(Form("using S.E. %s", fSE.Data()));
+               AliInfo(Form("using S.E.: %s", fSE.Data()));
+
+               if(!TagFileId(filename, &id)){
+                       AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
+                       if(!gGrid->Rm(filename.Data()))
+                               AliError("Can't delete file!");
+                       return kFALSE;
+               }
 
-               if(tagCDB) TagFileId(filename, &id);
-               if(tagCDBmd) TagFileMetaData(filename, entry->GetMetaData());
+               TagFileMetaData(filename, entry->GetMetaData());
        }
 
        return result;
@@ -584,64 +589,70 @@ Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
 }
 
 //_____________________________________________________________________________
-void AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
+Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
 // tag stored object in CDB table using object Id's parameters
 
-       TString addTagValue_1 = Form("addTagValue %s CDB ", filename.Data());
-       TString addTagValue_2 = Form("first_run=%d last_run=%d version=%d ",
+       TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
+       TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
                                        id->GetFirstRun(),
                                        id->GetLastRun(),
                                        id->GetVersion());
-       TString addTagValue_3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"",
+       TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"",
                                        id->GetLevel0().Data(),
                                        id->GetLevel1().Data(),
                                        id->GetLevel2().Data());
        TString addTagValue = Form("%s%s%s",
-                                       addTagValue_1.Data(),
-                                       addTagValue_2.Data(),
-                                       addTagValue_3.Data());
+                                       addTagValue1.Data(),
+                                       addTagValue2.Data(),
+                                       addTagValue3.Data());
 
+       Bool_t result = kFALSE;
        AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
        TGridResult* res = gGrid->Command(addTagValue.Data());
        const char* resCode = res->GetKey(0,"__result__"); // '1' if success
        if(resCode[0] != '1') {
-               AliWarning(Form("Couldn't add CDB tag value to file %s !",
+               AliError(Form("Couldn't add CDB tag value to file %s !",
                                                filename.Data()));
+               result = kFALSE;
        } else {
                AliInfo("Object successfully tagged.");
+               result = kTRUE;
        }
        delete res;
-
+       return result;
 
 }
 
 //_____________________________________________________________________________
-void AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
+Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
 // tag stored object in CDB table using object Id's parameters
 
-       TString addTagValue_1 = Form("addTagValue %s CDB_MD ", filename.Data());
-       TString addTagValue_2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
+       TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
+       TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
                                        md->GetObjectClassName(),
                                        md->GetResponsible(),
                                        md->GetBeamPeriod());
-       TString addTagValue_3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
+       TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
                                        md->GetAliRootVersion(),
                                        md->GetComment());
        TString addTagValue = Form("%s%s%s",
-                                       addTagValue_1.Data(),
-                                       addTagValue_2.Data(),
-                                       addTagValue_3.Data());
+                                       addTagValue1.Data(),
+                                       addTagValue2.Data(),
+                                       addTagValue3.Data());
 
+       Bool_t result = kFALSE;
        AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
        TGridResult* res = gGrid->Command(addTagValue.Data());
        const char* resCode = res->GetKey(0,"__result__"); // '1' if success
        if(resCode[0] != '1') {
                AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
                                                filename.Data()));
+               result = kFALSE;
        } else {
                AliInfo("Object successfully tagged.");
+               result = kTRUE;
        }
-
+       return result;
 }
 
 //_____________________________________________________________________________
@@ -698,12 +709,19 @@ void AliCDBGrid::QueryValidFiles()
 // fills list fValidFileIds with AliCDBId objects created from file name
 
        TString filter;
-       MakeQueryFilter(fRun, fRun, fPathFilter, fVersion, fMetaDataFilter, filter);
+       MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
+
+       TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data());
+       if(fVersion >= 0) pattern += Form("_v%d*", fVersion);
+       pattern += ".root";
+       AliDebug(2,Form("pattern: %s", pattern.Data()));
+
+       TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
 
-       TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
        AliCDBId validFileId;
        for(int i=0; i<res->GetEntries(); i++){
                TString filename = res->GetKey(i, "lfn");
+               if(filename == "") continue;
                AliDebug(2,Form("Found valid file: %s", filename.Data()));
                Bool_t result = FilenameToId(filename, validFileId);
                if(result) {
@@ -715,26 +733,25 @@ void AliCDBGrid::QueryValidFiles()
 }
 
 //_____________________________________________________________________________
-void AliCDBGrid::MakeQueryFilter(Long64_t firstRun, Long64_t lastRun,
-                                       const AliCDBPath& pathFilter, Int_t version,
+void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
                                        const AliCDBMetaData* md, TString& result) const
 {
 // create filter for file query
 
-       result = Form("CDB:first_run<=%ld and CDB:last_run>=%ld", (long) firstRun, (long) lastRun);
-
-       if(version >= 0) {
-               result += Form(" and CDB:version=%d", version);
-       }
-       if(pathFilter.GetLevel0() != "*") {
-               result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
-       }
-       if(pathFilter.GetLevel1() != "*") {
-               result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
-       }
-       if(pathFilter.GetLevel2() != "*") {
-               result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
-       }
+       result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
+
+//     if(version >= 0) {
+//             result += Form(" and CDB:version=%d", version);
+//     }
+//     if(pathFilter.GetLevel0() != "*") {
+//             result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
+//     }
+//     if(pathFilter.GetLevel1() != "*") {
+//             result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
+//     }
+//     if(pathFilter.GetLevel2() != "*") {
+//             result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
+//     }
 
        if(md){
                if(md->GetObjectClassName()[0] != '\0') {
@@ -757,6 +774,66 @@ void AliCDBGrid::MakeQueryFilter(Long64_t firstRun, Long64_t lastRun,
 
 }
 
+//_____________________________________________________________________________
+Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
+// get last version found in the database valid for run and path
+
+       TObjArray validFileIds;
+       validFileIds.SetOwner(1);
+
+       AliCDBPath aCDBPath(path);
+       if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
+               AliError(Form("Invalid path in request: %s", path));
+               return -1;
+       }
+       AliCDBId query(path, run, run, -1, -1);
+       AliCDBId* dataId = 0;
+
+       // look for file matching query requests (path, runRange, version)
+       if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
+               // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
+               AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
+                                       run, path));
+               dataId = GetId(fValidFileIds, query);
+               if (!dataId) return -1;
+               return dataId->GetVersion();
+
+       }
+       // List of files valid for reqested run was not loaded. Looking directly into CDB
+       AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
+                               run, path));
+
+       TString filter;
+       MakeQueryFilter(run, run, 0, filter);
+
+       TString pattern = Form("%s/Run*.root", path);
+       AliDebug(2,Form("pattern: %s", pattern.Data()));
+
+       TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
+       AliCDBId validFileId;
+       for(int i=0; i<res->GetEntries(); i++){
+               TString filename = res->GetKey(i, "lfn");
+               if(filename == "") continue;
+               if(FilenameToId(filename, validFileId))
+                               validFileIds.AddLast(validFileId.Clone());
+       }
+       delete res;
+
+       dataId = GetId(validFileIds, query);
+       if (!dataId) return -1;
+
+       return dataId->GetVersion();
+
+}
+
+//_____________________________________________________________________________
+Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
+// get last subversion found in the database valid for run and path
+       AliError("Objects in GRID storage have no sub version!");
+       return -1;
+}
+
+
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                                             //
 // AliCDBGrid factory                                                                                 //
@@ -787,11 +864,11 @@ AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
        }
        //TString buffer(gridString + sizeof("alien://") - 1);
        TString buffer(gridString);
-       TString gridUrl         = "alien://"; 
+
+       TString gridUrl         = "alien://";
        TString user            = "";
        TString dbFolder        = "DBGrid";
-       TString se              = "ALICE::CERN::se01";
+       TString se              = "default";
 
        TObjArray *arr = buffer.Tokenize('?');
        TIter iter(arr);
@@ -854,6 +931,10 @@ AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
 
        }
 
+       if(!gGrid && grid) {
+               delete grid; grid=0;
+       }
+
        return grid;
 }