]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBGrid.cxx
Macros to load separately the libraries needed for simulation and reconstruction
[u/mrichter/AliRoot.git] / STEER / AliCDBGrid.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /////////////////////////////////////////////////////////////////////////////////////////////////
17 //                                                                                             //
18 // AliCDBGrid                                                                                  //
19 // access class to a DataBase in an AliEn storage                                              //
20 //                                                                                             //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
22
23
24 #include <TGrid.h>
25 #include <TGridResult.h>
26 #include <TFile.h>
27 #include <TKey.h>
28 #include <TROOT.h>
29 #include <TSystem.h>
30 #include <TObjArray.h>
31 #include <TObjString.h>
32 #include <TRegexp.h>
33
34 #include "AliLog.h"
35 #include "AliCDBEntry.h"
36 #include "AliCDBGrid.h"
37
38
39 ClassImp(AliCDBGrid)
40
41 //_____________________________________________________________________________
42 AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder, const char *se) :
43 AliCDBStorage(),
44 fGridUrl(gridUrl),
45 fUser(user),
46 fDBFolder(dbFolder),
47 fSE(se)
48 {
49 // constructor //
50
51         // if the same Grid is alreay active, skip connection
52         if (!gGrid || fGridUrl != gGrid->GridUrl()  
53              || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
54                 // connection to the Grid
55                 AliInfo("Connection to the Grid...");
56                 if(gGrid){
57                         AliInfo(Form("gGrid = %x; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
58                         AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
59                 }
60                 TGrid::Connect(fGridUrl.Data(),fUser.Data());
61         }
62
63         if(!gGrid) {
64                 AliError("Connection failed!");
65                 return;
66         }
67
68         TString initDir(gGrid->Pwd(0));
69         if (fDBFolder[0] != '/') {
70                 fDBFolder.Prepend(initDir);
71         }
72
73         // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
74         if(!gGrid->Cd(fDBFolder.Data(),0)){
75                 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
76                 if(!gGrid->Mkdir(fDBFolder.Data(),"",0)){
77                         AliError(Form("Cannot create folder <%s> !",fDBFolder.Data())); 
78                 }
79         } else {
80                 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
81         }
82
83         // removes any '/' at the end of path, then append one '/'
84         while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/')); 
85         fDBFolder+="/";
86
87         fType="alien";
88         fBaseFolder = fDBFolder;
89
90         // return to the initial directory
91         gGrid->Cd(initDir.Data(),0);
92 }
93
94 //_____________________________________________________________________________
95 AliCDBGrid::~AliCDBGrid()
96 {
97 // destructor
98         delete gGrid; gGrid=0;
99
100 }
101
102 //_____________________________________________________________________________
103 Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
104 // build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z.root)
105
106         if(filename.Contains(fDBFolder)){
107                 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
108         }
109
110         TString idPath = filename(0,filename.Last('/'));
111         id.SetPath(idPath);
112         if(!id.IsValid()) return kFALSE;
113
114         filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
115
116         Ssiz_t mSize;
117         // valid filename: Run#firstRun_#lastRun_v#version.root
118         TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
119         keyPattern.Index(filename, &mSize);
120         if (!mSize) {
121                 AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
122                 return kFALSE;
123         }
124
125         filename.Resize(filename.Length() - sizeof(".root") + 1);
126
127         TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
128
129         TString firstRunString(((TObjString*) strArray->At(0))->GetString());
130         id.SetFirstRun(atoi(firstRunString.Data() + 3));
131         id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
132
133         TString verString(((TObjString*) strArray->At(2))->GetString());
134         id.SetVersion(atoi(verString.Data() + 1));
135
136         delete strArray;
137
138         return kTRUE;
139 }
140
141 //_____________________________________________________________________________
142 Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) {
143 // build file name from AliCDBId (path, run range, version) and fDBFolder
144
145         if (!id.GetAliCDBRunRange().IsValid()) {
146                 AliDebug(2,Form("Invalid run range <%d, %d>.",
147                         id.GetFirstRun(), id.GetLastRun()));
148                 return kFALSE;
149         }
150
151         if (id.GetVersion() < 0) {
152                 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
153                 return kFALSE;
154         }
155
156         filename = Form("Run%d_%d_v%d.root",
157                                 id.GetFirstRun(),
158                                 id.GetLastRun(),
159                                 id.GetVersion());
160
161         filename.Prepend(fDBFolder + id.GetPath() + '/');
162
163         return kTRUE;
164 }
165
166 //_____________________________________________________________________________
167 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
168 // prepare id (version) of the object that will be stored (called by PutEntry)
169
170         TString initDir(gGrid->Pwd(0));
171         TString pathName= id.GetPath();
172
173         TString dirName(fDBFolder);
174
175         Bool_t dirExist=kFALSE;
176  
177         // go to the path; if directory does not exist, create it
178         TObjArray *arrName=pathName.Tokenize("/");
179         for(int i=0;i<arrName->GetEntries();i++){
180                 TString buffer((arrName->At(i))->GetName());
181                 dirName+=buffer; dirName+="/";
182                 dirExist=gGrid->Cd(dirName,0);
183                 if (!dirExist) {
184                         AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
185                         if(!gGrid->Mkdir(dirName,"",0)){
186                                 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
187                                 gGrid->Cd(initDir.Data());
188                         return kFALSE;
189                         }
190                 }
191         }
192         delete arrName;
193         gGrid->Cd(initDir,0);
194
195         TString filename;
196         AliCDBId anId; // the id got from filename
197         AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
198         Int_t lastVersion=0; // highest version found
199
200         TGridResult *res = gGrid->Ls(dirName);
201
202         //loop on the files in the directory, look for highest version
203         for(int i=0; i < res->GetEntries(); i++){
204                 filename=res->GetFileNamePath(i);
205                 if (!FilenameToId(filename, anId)) continue;
206                 if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) {
207                         lastVersion = anId.GetVersion();
208                         lastRunRange = anId.GetAliCDBRunRange();
209                 }
210
211         }
212         delete res; 
213  
214         id.SetVersion(lastVersion + 1);
215
216         TString lastStorage = id.GetLastStorage();
217         if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
218                 AliDebug(2, Form("A NEW object is being stored with version %d",
219                                         id.GetVersion()));
220                 AliDebug(2, Form("and it will hide previously stored object with version %d!",
221                                         id.GetVersion()-1));
222         }
223
224         if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange()))) 
225                 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
226                         lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
227     
228         return kTRUE;
229 }
230
231 //_____________________________________________________________________________
232 AliCDBId* AliCDBGrid::GetId(const TList& validFileIds, const AliCDBId& query) {
233 // look for the Id that matches query's requests (highest or exact version)
234
235         if(validFileIds.GetEntries() < 1) {
236                 return NULL;
237         } else if (validFileIds.GetEntries() == 1) {
238                 return dynamic_cast<AliCDBId*> (validFileIds.At(0));
239         }
240
241         TIter iter(&validFileIds);
242
243         AliCDBId *anIdPtr=0;
244         AliCDBId* result=0;
245
246         while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
247
248                 if(anIdPtr->GetPath() != query.GetPath()) continue;
249
250                 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
251
252                 if (!query.HasVersion()){ // look for highest version
253                         if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
254                         if(result && result->GetVersion() == anIdPtr->GetVersion()) {
255                                 AliDebug(2,Form("More than one object valid for run %d, version %d!",
256                                         query.GetFirstRun(), anIdPtr->GetVersion()));
257                                 return NULL;
258                         }
259                         result = anIdPtr;
260                 } else { // look for specified version
261                         if(query.GetVersion() != anIdPtr->GetVersion()) continue;
262                         if(result && result->GetVersion() == anIdPtr->GetVersion()){
263                                 AliDebug(2,Form("More than one object valid for run %d, version %d!",
264                                         query.GetFirstRun(), anIdPtr->GetVersion()));
265                                 return NULL;
266                         }
267                         result = anIdPtr;
268                 }
269
270         }
271
272
273         return result;
274 }
275
276 /* TODO remove
277 //_____________________________________________________________________________
278 Bool_t AliCDBGrid::CheckVersion(const AliCDBId& query, AliCDBId* idToCheck, AliCDBId* result){
279 // Check if idToCheck has the "right" requested version and return it in result
280
281         if (!result) {
282                 result = idToCheck;
283                 AliInfo(Form("all'inizio: result = %s", result->ToString().Data()));
284                 return kTRUE;
285         }
286
287         AliInfo(Form("result = %s", result->ToString().Data()));
288
289         if (!query.HasVersion()){ // look for highest version
290                 if(result->GetVersion() > idToCheck->GetVersion()) return kTRUE;
291                 if(result->GetVersion() == idToCheck->GetVersion()) {
292                         AliDebug(2,Form("More than one object valid for run %d, version %d!",
293                                 query.GetFirstRun(), idToCheck->GetVersion()));
294                         return kFALSE;
295                 }
296
297         } else { // look for specified version
298                 if(query.GetVersion() != idToCheck->GetVersion()) return kTRUE;
299                 if(result->GetVersion() == idToCheck->GetVersion()){
300                         AliDebug(2,Form("More than one object valid for run %d, version %d!",
301                                 query.GetFirstRun(), idToCheck->GetVersion()));
302                         return kFALSE;
303                 }
304         }
305         result = idToCheck;
306         AliInfo(Form("alla fine: result = %s", result->ToString().Data()));
307         return kTRUE;
308 }
309 */
310
311 //_____________________________________________________________________________
312 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
313 // get AliCDBEntry from the database
314
315         AliCDBId* dataId=0;
316
317         AliCDBId selectedId(queryId);
318         if (!selectedId.HasVersion()) {
319                 // if version is not specified, first check the selection criteria list
320                 GetSelection(&selectedId);
321         }
322
323         TList validFileIds;
324         validFileIds.SetOwner(1);
325
326         // look for file matching query requests (path, runRange, version)
327         if(selectedId.GetFirstRun() == fRun &&
328                         fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0){
329                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
330                 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
331                                         selectedId.GetFirstRun(), selectedId.GetPath().Data()));
332                 dataId = GetId(fValidFileIds, selectedId);
333
334         } else {
335                 // List of files valid for reqested run was not loaded. Looking directly into CDB
336                 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
337                                         selectedId.GetFirstRun(), selectedId.GetPath().Data()));
338
339                 TString filter;
340                 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(),
341                                 selectedId.GetAliCDBPath(), selectedId.GetVersion(), 0, filter);
342
343                 TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
344                 AliCDBId validFileId;
345                 for(int i=0; i<res->GetEntries(); i++){
346                         TString filename = res->GetKey(i, "lfn");
347                         if(FilenameToId(filename, validFileId))
348                                         validFileIds.AddLast(validFileId.Clone());
349                 }
350                 delete res;
351                 dataId = GetId(validFileIds, selectedId);
352         }
353
354         if (!dataId) return NULL;
355
356         TString filename;
357         if (!IdToFilename(*dataId, filename)) {
358                 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
359                 return NULL;
360         }
361
362         AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
363
364         return anEntry;
365 }
366
367 //_____________________________________________________________________________
368 AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, const AliCDBId* dataId){
369 // Get AliCBEntry object from file "filename"
370
371         AliDebug(2,Form("Opening file: %s",filename.Data()));
372
373         filename.Prepend("/alien");
374         TFile *file = TFile::Open(filename);
375         if (!file) {
376                 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
377                 return NULL;
378         }
379
380         // get the only AliCDBEntry object from the file
381         // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
382
383         AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
384
385         if (!anEntry) {
386                 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
387                 file->Close();
388                 return NULL;
389         }
390
391         // The object's Id is not reset during storage
392         // If object's Id runRange or version do not match with filename,
393         // it means that someone renamed file by hand. In this case a warning msg is issued.
394
395         if(anEntry){
396                 AliCDBId entryId = anEntry->GetId();
397                 if(!((entryId.GetAliCDBRunRange()).IsEqual(&(dataId->GetAliCDBRunRange()))) ||
398                         entryId.GetVersion() != dataId->GetVersion()){
399                         AliWarning(Form("Either RunRange or gridVersion in the object's metadata"));
400                         AliWarning(Form("do noth match with fileName numbers:"));
401                         AliWarning(Form("someone renamed file by hand!"));
402                 }
403         }
404
405         anEntry->SetLastStorage("grid");
406
407         // close file, return retieved entry
408         file->Close(); delete file; file=0;
409
410         return anEntry;
411 }
412
413 //_____________________________________________________________________________
414 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
415 // multiple request (AliCDBStorage::GetAll)
416
417         TList* result = new TList();
418         result->SetOwner();
419
420         TList validFileIds;
421         validFileIds.SetOwner(1);
422
423         Bool_t alreadyLoaded = kFALSE;
424
425         // look for file matching query requests (path, runRange)
426         if(queryId.GetFirstRun() == fRun &&
427                         fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0){
428                 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
429                 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
430                                         queryId.GetFirstRun(), queryId.GetPath().Data()));
431
432                 alreadyLoaded = kTRUE;
433
434         } else {
435                 // List of files valid for reqested run was not loaded. Looking directly into CDB
436                 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
437                                         queryId.GetFirstRun(), queryId.GetPath().Data()));
438
439                 TString filter;
440                 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(),
441                                 queryId.GetAliCDBPath(), queryId.GetVersion(), 0, filter);
442
443                 TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
444                 AliCDBId validFileId;
445                 for(int i=0; i<res->GetEntries(); i++){
446                         TString filename = res->GetKey(i, "lfn");
447                         if(FilenameToId(filename, validFileId))
448                                         validFileIds.AddLast(validFileId.Clone());
449                 }
450                 delete res;
451         }
452
453         TIter *iter=0;
454         if(alreadyLoaded){
455                 iter = new TIter(&fValidFileIds);
456         } else {
457                 iter = new TIter(&validFileIds);
458         }
459
460         TList selectedIds;
461         selectedIds.SetOwner(1);
462
463         // loop on list of valid Ids to select the right version to get.
464         // According to query and to the selection criteria list, version can be the highest or exact
465         AliCDBPath pathCopy;
466         AliCDBId* anIdPtr=0;
467         AliCDBId* dataId=0;
468         AliCDBPath queryPath = queryId.GetAliCDBPath();
469         while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
470                 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
471                 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
472                 pathCopy = thisCDBPath;
473
474                 // check the selection criteria list for this query
475                 AliCDBId thisId(*anIdPtr);
476                 thisId.SetVersion(queryId.GetVersion());
477                 if(!thisId.HasVersion()) GetSelection(&thisId);
478
479                 if(alreadyLoaded){
480                         dataId = GetId(fValidFileIds, thisId);
481                 } else {
482                         dataId = GetId(validFileIds, thisId);
483                 }
484                 if(dataId) selectedIds.Add(dataId->Clone());
485         }
486
487         delete iter; iter=0;
488
489         // selectedIds contains the Ids of the files matching all requests of query!
490         // All the objects are now ready to be retrieved
491         iter = new TIter(&selectedIds);
492         while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
493                 TString filename;
494                 if (!IdToFilename(*anIdPtr, filename)) {
495                         AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
496                         continue;
497                 }
498
499                 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
500
501                 if(anEntry) result->Add(anEntry);
502
503         }
504         delete iter; iter=0;
505
506         return result;
507 }
508
509 //_____________________________________________________________________________
510 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
511 // put an AliCDBEntry object into the database
512
513         AliCDBId& id = entry->GetId();
514
515         // set version for the entry to be stored
516         if (!PrepareId(id)) return kFALSE;
517
518         // build filename from entry's id
519         TString filename;
520         if (!IdToFilename(id, filename)) {
521                 AliError("Bad ID encountered! Subnormal error!");
522                 return kFALSE;
523         }
524
525         TString folderToTag = Form("%s%s",
526                                         fDBFolder.Data(),
527                                         id.GetPath().Data());
528
529         // add CDB and CDB_MD tag to folder
530         // TODO how to check that folder has already tags?
531         Bool_t tagCDB = AddTag(folderToTag,"CDB");
532         Bool_t tagCDBmd = AddTag(folderToTag,"CDB_MD");
533
534         TDirectory* saveDir = gDirectory;
535
536         // specify SE to filename
537         TString fullFilename = Form("/alien%s?se=%s", filename.Data(), fSE.Data());
538
539         // open file
540         TFile *file = TFile::Open(fullFilename,"CREATE");
541         if(!file || !file->IsWritable()){
542                 AliError(Form("Can't open file <%s>!", filename.Data()));
543                 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
544                 return kFALSE;
545         }
546
547         file->cd();
548
549         entry->SetVersion(id.GetVersion());
550
551         // write object (key name: "AliCDBEntry")
552         Bool_t result = (entry->Write("AliCDBEntry") != 0); 
553         if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
554
555
556         if (saveDir) saveDir->cd(); else gROOT->cd();
557         file->Close(); delete file; file=0;
558         if(result) {
559                 AliInfo(Form("CDB object stored into file %s", filename.Data()));
560                 AliInfo(Form("using S.E. %s", fSE.Data()));
561
562                 if(tagCDB) TagFileId(filename, &id);
563                 if(tagCDBmd) TagFileMetaData(filename, entry->GetMetaData());
564         }
565
566         return result;
567 }
568 //_____________________________________________________________________________
569 Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
570 // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
571
572         Bool_t result = kTRUE;
573         AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
574         TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
575         TGridResult *gridres = gGrid->Command(addTag.Data());
576         const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
577         if(resCode[0] != '1') {
578                 AliError(Form("Couldn't add %s tags to folder %s !",
579                                                 tagname, folderToTag.Data()));
580                 result = kFALSE;
581         }
582         delete gridres;
583         return result;
584 }
585
586 //_____________________________________________________________________________
587 void AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
588 // tag stored object in CDB table using object Id's parameters
589
590         TString addTagValue_1 = Form("addTagValue %s CDB ", filename.Data());
591         TString addTagValue_2 = Form("first_run=%d last_run=%d version=%d ",
592                                         id->GetFirstRun(),
593                                         id->GetLastRun(),
594                                         id->GetVersion());
595         TString addTagValue_3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"",
596                                         id->GetLevel0().Data(),
597                                         id->GetLevel1().Data(),
598                                         id->GetLevel2().Data());
599         TString addTagValue = Form("%s%s%s",
600                                         addTagValue_1.Data(),
601                                         addTagValue_2.Data(),
602                                         addTagValue_3.Data());
603
604         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
605         TGridResult* res = gGrid->Command(addTagValue.Data());
606         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
607         if(resCode[0] != '1') {
608                 AliWarning(Form("Couldn't add CDB tag value to file %s !",
609                                                 filename.Data()));
610         } else {
611                 AliInfo("Object successfully tagged.");
612         }
613         delete res;
614
615
616 }
617
618 //_____________________________________________________________________________
619 void AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
620 // tag stored object in CDB table using object Id's parameters
621
622         TString addTagValue_1 = Form("addTagValue %s CDB_MD ", filename.Data());
623         TString addTagValue_2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
624                                         md->GetObjectClassName(),
625                                         md->GetResponsible(),
626                                         md->GetBeamPeriod());
627         TString addTagValue_3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
628                                         md->GetAliRootVersion(),
629                                         md->GetComment());
630         TString addTagValue = Form("%s%s%s",
631                                         addTagValue_1.Data(),
632                                         addTagValue_2.Data(),
633                                         addTagValue_3.Data());
634
635         AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
636         TGridResult* res = gGrid->Command(addTagValue.Data());
637         const char* resCode = res->GetKey(0,"__result__"); // '1' if success
638         if(resCode[0] != '1') {
639                 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
640                                                 filename.Data()));
641         } else {
642                 AliInfo("Object successfully tagged.");
643         }
644
645 }
646
647 //_____________________________________________________________________________
648 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
649
650         TString turl(fileName);
651         turl.Prepend("/alien" + fDBFolder);
652         turl += "?se="; turl += fSE.Data();
653         TFile *file = TFile::Open(turl);
654         if (!file) {
655                 AliError(Form("Can't open selection file <%s>!", turl.Data()));
656                 return NULL;
657         }
658
659         TList *list = new TList();
660         list->SetOwner();
661         int i=0;
662         TString keycycle;
663
664         AliCDBId *id;
665         while(1){
666                 i++;
667                 keycycle = "AliCDBId;";
668                 keycycle+=i;
669                 
670                 id = (AliCDBId*) file->Get(keycycle);
671                 if(!id) break;
672                 list->AddFirst(id);
673         }
674         file->Close(); delete file; file=0;
675         
676         return list;
677
678
679 }
680
681 //_____________________________________________________________________________
682 Bool_t AliCDBGrid::Contains(const char* path) const{
683 // check for path in storage's DBFolder
684
685         TString initDir(gGrid->Pwd(0));
686         TString dirName(fDBFolder);
687         dirName += path; // dirName = fDBFolder/path
688         Bool_t result=kFALSE;
689         if (gGrid->Cd(dirName,0)) result=kTRUE;
690         gGrid->Cd(initDir.Data(),0);
691         return result;
692 }
693
694 //_____________________________________________________________________________
695 void AliCDBGrid::QueryValidFiles()
696 {
697 // Query the CDB for files valid for AliCDBStorage::fRun
698 // fills list fValidFileIds with AliCDBId objects created from file name
699
700         TString filter;
701         MakeQueryFilter(fRun, fRun, fPathFilter, fVersion, fMetaDataFilter, filter);
702
703         TGridResult *res = gGrid->Query(fDBFolder, "Run*.root", filter, "");
704         AliCDBId validFileId;
705         for(int i=0; i<res->GetEntries(); i++){
706                 TString filename = res->GetKey(i, "lfn");
707                 AliDebug(2,Form("Found valid file: %s", filename.Data()));
708                 Bool_t result = FilenameToId(filename, validFileId);
709                 if(result) {
710                         fValidFileIds.AddLast(validFileId.Clone());
711                 }
712         }
713         delete res;
714
715 }
716
717 //_____________________________________________________________________________
718 void AliCDBGrid::MakeQueryFilter(Long64_t firstRun, Long64_t lastRun,
719                                         const AliCDBPath& pathFilter, Int_t version,
720                                         const AliCDBMetaData* md, TString& result) const
721 {
722 // create filter for file query
723
724         result = Form("CDB:first_run<=%ld and CDB:last_run>=%ld", (long) firstRun, (long) lastRun);
725
726         if(version >= 0) {
727                 result += Form(" and CDB:version=%d", version);
728         }
729         if(pathFilter.GetLevel0() != "*") {
730                 result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
731         }
732         if(pathFilter.GetLevel1() != "*") {
733                 result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
734         }
735         if(pathFilter.GetLevel2() != "*") {
736                 result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
737         }
738
739         if(md){
740                 if(md->GetObjectClassName()[0] != '\0') {
741                         result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
742                 }
743                 if(md->GetResponsible()[0] != '\0') {
744                         result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
745                 }
746                 if(md->GetBeamPeriod() != 0) {
747                         result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
748                 }
749                 if(md->GetAliRootVersion()[0] != '\0') {
750                         result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
751                 }
752                 if(md->GetComment()[0] != '\0') {
753                         result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
754                 }
755         }
756         AliDebug(2, Form("filter: %s",result.Data()));
757
758 }
759
760 /////////////////////////////////////////////////////////////////////////////////////////////////
761 //                                                                                             //
762 // AliCDBGrid factory                                                                          //
763 //                                                                                             //
764 /////////////////////////////////////////////////////////////////////////////////////////////////
765
766 ClassImp(AliCDBGridFactory)
767
768 //_____________________________________________________________________________
769 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
770 // check if the string is valid Grid URI
771
772         // pattern: alien://hostName:Port;user;dbPath;SE
773         // example of a valid pattern:
774         // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
775 //        TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
776         TRegexp gridPattern("^alien://.+$");
777
778         return TString(gridString).Contains(gridPattern);
779 }
780
781 //_____________________________________________________________________________
782 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
783 // create AliCDBGridParam class from the URI string
784
785         if (!Validate(gridString)) {
786                 return NULL;
787         }
788         //TString buffer(gridString + sizeof("alien://") - 1);
789         TString buffer(gridString);
790  
791         TString gridUrl         = "alien://"; 
792         TString user            = "";
793         TString dbFolder        = "DBGrid";
794         TString se              = "ALICE::CERN::se01";
795
796         TObjArray *arr = buffer.Tokenize('?');
797         TIter iter(arr);
798         TObjString *str = 0;
799         
800         while((str = (TObjString*) iter.Next())){
801                 TString entry(str->String());
802                 Int_t indeq = entry.Index('=');
803                 if(indeq == -1) {
804                         if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
805                                 gridUrl = entry;
806                                 continue;
807                         } else {
808                                 AliError(Form("Invalid entry! %s",entry.Data()));
809                                 continue;
810                         }
811                 }
812                 
813                 TString key = entry(0,indeq);
814                 TString value = entry(indeq+1,entry.Length()-indeq);
815
816                 if(key.Contains("grid",TString::kIgnoreCase)) {
817                         gridUrl += value;
818                 } 
819                 else if (key.Contains("user",TString::kIgnoreCase)){
820                         user = value;
821                 }
822                 else if (key.Contains("folder",TString::kIgnoreCase)){
823                         dbFolder = value;
824                 }
825                 else if (key.Contains("se",TString::kIgnoreCase)){
826                         se = value;
827                 }
828                 else{
829                         AliError(Form("Invalid entry! %s",entry.Data()));
830                 }
831         }
832         delete arr; arr=0;
833                 
834         AliDebug(2, Form("gridUrl:      %s",gridUrl.Data()));
835         AliDebug(2, Form("user: %s",user.Data()));
836         AliDebug(2, Form("dbFolder:     %s",dbFolder.Data()));
837         AliDebug(2, Form("s.e.: %s",se.Data()));
838
839         return new AliCDBGridParam(gridUrl.Data(), user.Data(), dbFolder.Data(), se.Data());
840 }
841
842 //_____________________________________________________________________________
843 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
844 // create AliCDBGrid storage instance from parameters
845         
846         AliCDBGrid *grid = 0;
847         if (AliCDBGridParam::Class() == param->IsA()) {
848
849                 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
850                 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
851                                       gridParam->GetUser().Data(),
852                                       gridParam->GetDBFolder().Data(),
853                                       gridParam->GetSE().Data());
854
855         }
856
857         return grid;
858 }
859
860 /////////////////////////////////////////////////////////////////////////////////////////////////
861 //                                                                                             //
862 // AliCDBGrid Parameter class                                                                  //                                          //
863 //                                                                                             //
864 /////////////////////////////////////////////////////////////////////////////////////////////////
865
866 ClassImp(AliCDBGridParam)
867
868 //_____________________________________________________________________________
869 AliCDBGridParam::AliCDBGridParam():
870  AliCDBParam(),
871  fGridUrl(),
872  fUser(),
873  fDBFolder(),
874  fSE()
875  {
876 // default constructor
877
878 }
879
880 //_____________________________________________________________________________
881 AliCDBGridParam::AliCDBGridParam(const char* gridUrl, 
882                                 const char* user,
883                                 const char* dbFolder, 
884                                 const char* se):
885  AliCDBParam(),
886  fGridUrl(gridUrl),
887  fUser(user),
888  fDBFolder(dbFolder),
889  fSE(se)
890 {
891 // constructor
892         
893         SetType("alien");
894
895         TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s",
896                         fGridUrl.Data(), fUser.Data(),
897                         fDBFolder.Data(), fSE.Data());
898
899         SetURI(uri.Data());
900 }
901
902 //_____________________________________________________________________________
903 AliCDBGridParam::~AliCDBGridParam() {
904 // destructor
905
906 }
907
908 //_____________________________________________________________________________
909 AliCDBParam* AliCDBGridParam::CloneParam() const {
910 // clone parameter
911
912         return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
913                                         fDBFolder.Data(), fSE.Data());
914 }
915
916 //_____________________________________________________________________________
917 ULong_t AliCDBGridParam::Hash() const {
918 // return Hash function
919
920         return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash();
921 }
922
923 //_____________________________________________________________________________
924 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
925 // check if this object is equal to AliCDBParam obj
926
927         if (this == obj) {
928                 return kTRUE;
929         }
930
931         if (AliCDBGridParam::Class() != obj->IsA()) {
932                 return kFALSE;
933         }
934
935         AliCDBGridParam* other = (AliCDBGridParam*) obj;
936
937         if(fGridUrl != other->fGridUrl) return kFALSE;
938         if(fUser != other->fUser) return kFALSE;
939         if(fDBFolder != other->fDBFolder) return kFALSE;
940         if(fSE != other->fSE) return kFALSE;
941         return kTRUE;
942 }
943