]> git.uio.no Git - u/mrichter/AliRoot.git/blame_incremental - STEER/AliCDBGrid.cxx
Cast to AliMCParticle*
[u/mrichter/AliRoot.git] / STEER / AliCDBGrid.cxx
... / ...
CommitLineData
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#include <cstdlib>
24#include <TGrid.h>
25#include <TGridResult.h>
26#include <TFile.h>
27#include <TKey.h>
28#include <TROOT.h>
29#include <TList.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#include "AliCDBManager.h"
38
39
40ClassImp(AliCDBGrid)
41
42//_____________________________________________________________________________
43AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder,
44 const char *se, const char* cacheFolder, Bool_t operateDisconnected,
45 Long64_t cacheSize, Long_t cleanupInterval) :
46AliCDBStorage(),
47fGridUrl(gridUrl),
48fUser(user),
49fDBFolder(dbFolder),
50fSE(se),
51fCacheFolder(cacheFolder),
52fOperateDisconnected(operateDisconnected),
53fCacheSize(cacheSize),
54fCleanupInterval(cleanupInterval)
55{
56// constructor //
57
58 // if the same Grid is alreay active, skip connection
59 if (!gGrid || fGridUrl != gGrid->GridUrl()
60 || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
61 // connection to the Grid
62 AliInfo("Connection to the Grid...");
63 if(gGrid){
64 AliInfo(Form("gGrid = %x; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
65 AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
66 }
67 AliDebug(2,Form("1. Grid Url = %s",fGridUrl.Data()));
68 if (fGridUrl == "raw://") fGridUrl = "alien://";
69 AliDebug(2,Form("2. Grid Url = %s (should be different from previous in cas eof raw)", fGridUrl.Data()));
70 TGrid::Connect(fGridUrl.Data(),fUser.Data());
71 }
72
73 if(!gGrid) {
74 AliError("Connection failed!");
75 return;
76 }
77
78 TString initDir(gGrid->Pwd(0));
79 if (fDBFolder[0] != '/') {
80 fDBFolder.Prepend(initDir);
81 }
82
83 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
84 if(!gGrid->Cd(fDBFolder.Data(),0)){
85 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
86 TGridResult* res = gGrid->Command(Form("mkdir -p %s",fDBFolder.Data()));
87 TString result = res->GetKey(0,"__result__");
88 if(result == "0"){
89 AliFatal(Form("Cannot create folder <%s> !",fDBFolder.Data()));
90 return;
91 }
92 } else {
93 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
94 }
95
96 // removes any '/' at the end of path, then append one '/'
97 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
98 fDBFolder+="/";
99
100 fType="alien";
101 fBaseFolder = fDBFolder;
102
103 // Setting the cache
104
105 // Check if local cache folder is already defined
106 TString origCache(TFile::GetCacheFileDir());
107 if(fCacheFolder.Length() > 0) {
108 if(origCache.Length() == 0) {
109 AliInfo(Form("Setting local cache to: %s", fCacheFolder.Data()));
110 } else if(fCacheFolder != origCache) {
111 AliWarning(Form("Local cache folder was already defined, changing it to: %s",
112 fCacheFolder.Data()));
113 }
114
115 // default settings are: operateDisconnected=kTRUE, forceCacheread = kFALSE
116 if(!TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected)) {
117 AliError(Form("Could not set cache folder %s !", fCacheFolder.Data()));
118 fCacheFolder = "";
119 } else {
120 // reset fCacheFolder because the function may have
121 // slightly changed the folder name (e.g. '/' added)
122 fCacheFolder = TFile::GetCacheFileDir();
123 }
124
125 // default settings are: cacheSize=1GB, cleanupInterval = 0
126 if(!TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval)) {
127 AliError(Form("Could not set following values "
128 "to ShrinkCacheFileDir: cacheSize = %d, cleanupInterval = %d !",
129 fCacheSize, fCleanupInterval));
130 }
131 }
132
133 // return to the initial directory
134 gGrid->Cd(initDir.Data(),0);
135}
136
137//_____________________________________________________________________________
138AliCDBGrid::~AliCDBGrid()
139{
140// destructor
141 delete gGrid; gGrid=0;
142
143}
144
145//_____________________________________________________________________________
146Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
147// build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
148
149 if(filename.Contains(fDBFolder)){
150 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
151 }
152
153 TString idPath = filename(0,filename.Last('/'));
154 id.SetPath(idPath);
155 if(!id.IsValid()) return kFALSE;
156
157 filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
158
159 Ssiz_t mSize;
160 // valid filename: Run#firstRun_#lastRun_v#version_s0.root
161 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$");
162 keyPattern.Index(filename, &mSize);
163 if (!mSize) {
164
165 // TODO backward compatibility ... maybe remove later!
166 Ssiz_t oldmSize;
167 TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
168 oldKeyPattern.Index(filename, &oldmSize);
169 if(!oldmSize) {
170 AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
171 return kFALSE;
172 } else {
173 AliDebug(2,Form("Old filename format <%s>.", filename.Data()));
174 id.SetSubVersion(-11); // TODO trick to ensure backward compatibility
175 }
176
177 } else {
178 id.SetSubVersion(-1); // TODO trick to ensure backward compatibility
179 }
180
181 filename.Resize(filename.Length() - sizeof(".root") + 1);
182
183 TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
184
185 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
186 id.SetFirstRun(atoi(firstRunString.Data() + 3));
187 id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
188
189 TString verString(((TObjString*) strArray->At(2))->GetString());
190 id.SetVersion(atoi(verString.Data() + 1));
191
192 delete strArray;
193
194 return kTRUE;
195}
196
197//_____________________________________________________________________________
198Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
199// build file name from AliCDBId (path, run range, version) and fDBFolder
200
201 if (!id.GetAliCDBRunRange().IsValid()) {
202 AliDebug(2,Form("Invalid run range <%d, %d>.",
203 id.GetFirstRun(), id.GetLastRun()));
204 return kFALSE;
205 }
206
207 if (id.GetVersion() < 0) {
208 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
209 return kFALSE;
210 }
211
212 filename = Form("Run%d_%d_v%d",
213 id.GetFirstRun(),
214 id.GetLastRun(),
215 id.GetVersion());
216
217 if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
218 filename += ".root";
219
220 filename.Prepend(fDBFolder + id.GetPath() + '/');
221
222 return kTRUE;
223}
224
225//_____________________________________________________________________________
226Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
227// prepare id (version) of the object that will be stored (called by PutEntry)
228
229 TString initDir(gGrid->Pwd(0));
230
231 TString dirName(fDBFolder);
232
233 Bool_t dirExist=kFALSE;
234
235
236
237 // go to the path; if directory does not exist, create it
238 for(int i=0;i<3;i++){
239 dirName+=Form("%s/",id.GetPathLevel(i).Data());
240 dirExist=gGrid->Cd(dirName,0);
241 if (!dirExist) {
242 AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
243 if(!gGrid->Mkdir(dirName,"",0)){
244 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
245 gGrid->Cd(initDir.Data());
246 return kFALSE;
247 }
248
249 // if folders are new add tags to them
250 if(i == 1) {
251 // TODO Currently disabled
252 // add short lived tag!
253 // AliInfo("Tagging level 1 folder with \"ShortLived\" tag");
254 // if(!AddTag(dirName,"ShortLived_try")){
255 // AliError(Form("Could not tag folder %s !", dirName.Data()));
256 // if(!gGrid->Rmdir(dirName.Data())){
257 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
258 // }
259 // return 0;
260 //}
261
262 } else if(i == 2) {
263 AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag");
264 if(!AddTag(dirName,"CDB")){
265 AliError(Form("Could not tag folder %s !", dirName.Data()));
266 if(!gGrid->Rmdir(dirName.Data())){
267 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
268 }
269 return 0;
270 }
271 if(!AddTag(dirName,"CDB_MD")){
272 AliError(Form("Could not tag folder %s !", dirName.Data()));
273 if(!gGrid->Rmdir(dirName.Data())){
274 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
275 }
276 return 0;
277 }
278
279 // TODO Currently disabled
280 // add short lived tag!
281 // TString path=id.GetPath();
282 // if(AliCDBManager::Instance()->IsShortLived(path.Data())) {
283 // AliInfo(Form("Tagging %s as short lived", dirName.Data()));
284 // if(!TagShortLived(dirName, kTRUE)){
285 // AliError(Form("Could not tag folder %s !", dirName.Data()));
286 // if(!gGrid->Rmdir(dirName.Data())){
287 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
288 // }
289 // return 0;
290 // }
291 // } else {
292 // AliInfo(Form("Tagging %s as long lived", dirName.Data()));
293 // if(!TagShortLived(dirName, kFALSE)){
294 // AliError(Form("Could not tag folder %s !", dirName.Data()));
295 // if(!gGrid->Rmdir(dirName.Data())){
296 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
297 // }
298 // return 0;
299 // }
300 // }
301 }
302 }
303 }
304 gGrid->Cd(initDir,0);
305
306 TString filename;
307 AliCDBId anId; // the id got from filename
308 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
309 Int_t lastVersion=0; // highest version found
310
311 TGridResult *res = gGrid->Ls(dirName);
312
313 //loop on the files in the directory, look for highest version
314 for(int i=0; i < res->GetEntries(); i++){
315 filename=res->GetFileNamePath(i);
316 if (!FilenameToId(filename, anId)) continue;
317 if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) {
318 lastVersion = anId.GetVersion();
319 lastRunRange = anId.GetAliCDBRunRange();
320 }
321
322 }
323 delete res;
324
325 // GRP entries with explicitly set version escape default incremental versioning
326 if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0)
327 {
328 AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data()));
329 return kFALSE;
330 }
331
332 id.SetVersion(lastVersion + 1);
333 id.SetSubVersion(0);
334
335 TString lastStorage = id.GetLastStorage();
336 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
337 AliDebug(2, Form("A NEW object is being stored with version %d",
338 id.GetVersion()));
339 AliDebug(2, Form("and it will hide previously stored object with version %d!",
340 id.GetVersion()-1));
341 }
342
343 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
344 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
345 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
346
347 return kTRUE;
348}
349
350//_____________________________________________________________________________
351AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
352// look for the Id that matches query's requests (highest or exact version)
353
354 if(validFileIds.GetEntriesFast() < 1) {
355 return NULL;
356 } else if (validFileIds.GetEntriesFast() == 1) {
357 return dynamic_cast<AliCDBId*> (validFileIds.At(0)->Clone());
358 }
359
360 TIter iter(&validFileIds);
361
362 AliCDBId *anIdPtr=0;
363 AliCDBId* result=0;
364
365 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
366 if(anIdPtr->GetPath() != query.GetPath()) continue;
367
368 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
369
370 if (!query.HasVersion()){ // look for highest version
371 if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
372 if(result && result->GetVersion() == anIdPtr->GetVersion()) {
373 AliError(Form("More than one object valid for run %d, version %d!",
374 query.GetFirstRun(), anIdPtr->GetVersion()));
375 return NULL;
376 }
377 result = anIdPtr;
378 } else { // look for specified version
379 if(query.GetVersion() != anIdPtr->GetVersion()) continue;
380 if(result && result->GetVersion() == anIdPtr->GetVersion()){
381 AliError(Form("More than one object valid for run %d, version %d!",
382 query.GetFirstRun(), anIdPtr->GetVersion()));
383 return NULL;
384 }
385 result = anIdPtr;
386 }
387
388 }
389
390 if (!result) return NULL;
391
392 return dynamic_cast<AliCDBId*> (result->Clone());
393}
394
395//_____________________________________________________________________________
396AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
397// get AliCDBId from the database
398// User must delete returned object
399
400 AliCDBId* dataId=0;
401
402 AliCDBId selectedId(queryId);
403 if (!selectedId.HasVersion()) {
404 // if version is not specified, first check the selection criteria list
405 GetSelection(&selectedId);
406 }
407
408 TObjArray validFileIds;
409 validFileIds.SetOwner(1);
410
411 // look for file matching query requests (path, runRange, version)
412 if(selectedId.GetFirstRun() == fRun &&
413 fPathFilter.Comprises(selectedId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
414 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
415 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
416 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
417 dataId = GetId(fValidFileIds, selectedId);
418
419 } else {
420 // List of files valid for reqested run was not loaded. Looking directly into CDB
421 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
422 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
423
424 TString filter;
425 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
426
427 TString pattern = Form("%s/Run*", selectedId.GetPath().Data());
428 if(selectedId.GetVersion() >= 0) pattern += Form("_v%d*",selectedId.GetVersion());
429 pattern += ".root";
430 AliDebug(2,Form("pattern: %s", pattern.Data()));
431
432 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
433 if (res) {
434 AliCDBId validFileId;
435 for(int i=0; i<res->GetEntries(); i++){
436 TString filename = res->GetKey(i, "lfn");
437 if(filename == "") continue;
438 if(FilenameToId(filename, validFileId))
439 validFileIds.AddLast(validFileId.Clone());
440 }
441 delete res;
442 }
443 dataId = GetId(validFileIds, selectedId);
444 }
445
446 return dataId;
447}
448
449//_____________________________________________________________________________
450AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
451// get AliCDBEntry from the database
452
453 AliCDBId* dataId = GetEntryId(queryId);
454
455 if (!dataId) return NULL;
456
457 TString filename;
458 if (!IdToFilename(*dataId, filename)) {
459 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
460 delete dataId;
461 return NULL;
462 }
463
464 AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
465
466 delete dataId;
467 return anEntry;
468}
469
470//_____________________________________________________________________________
471AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
472// Get AliCBEntry object from file "filename"
473
474 AliDebug(2,Form("Opening file: %s",filename.Data()));
475
476 filename.Prepend("/alien");
477
478 // if option="CACHEREAD" TFile will use the local caching facility!
479 TString option="READ";
480 if(fCacheFolder != ""){
481
482 // Check if local cache folder was changed in the meanwhile
483 TString origCache(TFile::GetCacheFileDir());
484 if(fCacheFolder != origCache) {
485 AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s",
486 fCacheFolder.Data(), origCache.Data()));
487 TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected);
488 TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval);
489 }
490
491 option.Prepend("CACHE");
492 }
493
494 AliDebug(2, Form("Option: %s", option.Data()));
495
496 TFile *file = TFile::Open(filename, option);
497 if (!file) {
498 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
499 return NULL;
500 }
501
502 // get the only AliCDBEntry object from the file
503 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
504
505 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
506
507 if (!anEntry) {
508 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
509 file->Close();
510 return NULL;
511 }
512
513 // The object's Id is not reset during storage
514 // If object's Id runRange or version do not match with filename,
515 // it means that someone renamed file by hand. In this case a warning msg is issued.
516
517 if(anEntry){
518 AliCDBId entryId = anEntry->GetId();
519 Int_t tmpSubVersion = dataId->GetSubVersion();
520 dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
521 if(!entryId.IsEqual(dataId)){
522 AliWarning(Form("Mismatch between file name and object's Id!"));
523 AliWarning(Form("File name: %s", dataId->ToString().Data()));
524 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
525 }
526 dataId->SetSubVersion(tmpSubVersion);
527 }
528
529 anEntry->SetLastStorage("grid");
530
531 // Check whether entry contains a TTree. In case load the tree in memory!
532 LoadTreeFromFile(anEntry);
533
534 // close file, return retieved entry
535 file->Close(); delete file; file=0;
536
537 return anEntry;
538}
539
540//_____________________________________________________________________________
541TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
542// multiple request (AliCDBStorage::GetAll)
543
544 TList* result = new TList();
545 result->SetOwner();
546
547 TObjArray validFileIds;
548 validFileIds.SetOwner(1);
549
550 Bool_t alreadyLoaded = kFALSE;
551
552 // look for file matching query requests (path, runRange)
553 if(queryId.GetFirstRun() == fRun &&
554 fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
555 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
556 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
557 queryId.GetFirstRun(), queryId.GetPath().Data()));
558
559 alreadyLoaded = kTRUE;
560
561 } else {
562 // List of files valid for reqested run was not loaded. Looking directly into CDB
563 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
564 queryId.GetFirstRun(), queryId.GetPath().Data()));
565
566 TString filter;
567 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
568
569 TString pattern = Form("%s/Run*.root", queryId.GetPath().Data());
570 AliDebug(2,Form("pattern: %s", pattern.Data()));
571
572 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
573
574 AliCDBId validFileId;
575 for(int i=0; i<res->GetEntries(); i++){
576 TString filename = res->GetKey(i, "lfn");
577 if(filename == "") continue;
578 if(FilenameToId(filename, validFileId))
579 validFileIds.AddLast(validFileId.Clone());
580 }
581 delete res;
582 }
583
584 TIter *iter=0;
585 if(alreadyLoaded){
586 iter = new TIter(&fValidFileIds);
587 } else {
588 iter = new TIter(&validFileIds);
589 }
590
591 TObjArray selectedIds;
592 selectedIds.SetOwner(1);
593
594 // loop on list of valid Ids to select the right version to get.
595 // According to query and to the selection criteria list, version can be the highest or exact
596 AliCDBPath pathCopy;
597 AliCDBId* anIdPtr=0;
598 AliCDBId* dataId=0;
599 AliCDBPath queryPath = queryId.GetAliCDBPath();
600 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
601 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
602 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
603 pathCopy = thisCDBPath;
604
605 // check the selection criteria list for this query
606 AliCDBId thisId(*anIdPtr);
607 thisId.SetVersion(queryId.GetVersion());
608 if(!thisId.HasVersion()) GetSelection(&thisId);
609
610 if(alreadyLoaded){
611 dataId = GetId(fValidFileIds, thisId);
612 } else {
613 dataId = GetId(validFileIds, thisId);
614 }
615 if(dataId) selectedIds.Add(dataId);
616 }
617
618 delete iter; iter=0;
619
620 // selectedIds contains the Ids of the files matching all requests of query!
621 // All the objects are now ready to be retrieved
622 iter = new TIter(&selectedIds);
623 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
624 TString filename;
625 if (!IdToFilename(*anIdPtr, filename)) {
626 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
627 continue;
628 }
629
630 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
631
632 if(anEntry) result->Add(anEntry);
633
634 }
635 delete iter; iter=0;
636
637 return result;
638}
639
640//_____________________________________________________________________________
641Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
642// put an AliCDBEntry object into the database
643
644 AliCDBId& id = entry->GetId();
645
646 // set version for the entry to be stored
647 if (!PrepareId(id)) return kFALSE;
648
649 // build filename from entry's id
650 TString filename;
651 if (!IdToFilename(id, filename)) {
652 AliError("Bad ID encountered! Subnormal error!");
653 return kFALSE;
654 }
655
656 TString folderToTag = Form("%s%s",
657 fDBFolder.Data(),
658 id.GetPath().Data());
659
660 TDirectory* saveDir = gDirectory;
661
662 TString fullFilename = Form("/alien%s", filename.Data());
663 // specify SE to filename
664 if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
665
666 // open file
667 TFile *file = TFile::Open(fullFilename,"CREATE");
668 if(!file || !file->IsWritable()){
669 AliError(Form("Can't open file <%s>!", filename.Data()));
670 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
671 return kFALSE;
672 }
673
674 file->cd();
675
676 //SetTreeToFile(entry, file);
677
678 entry->SetVersion(id.GetVersion());
679
680 // write object (key name: "AliCDBEntry")
681 Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
682 if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
683
684
685 if (saveDir) saveDir->cd(); else gROOT->cd();
686 file->Close(); delete file; file=0;
687
688 if(result) {
689
690 if(!TagFileId(filename, &id)){
691 AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
692 if(!gGrid->Rm(filename.Data()))
693 AliError("Can't delete file!");
694 return kFALSE;
695 }
696
697 TagFileMetaData(filename, entry->GetMetaData());
698 }
699
700 AliInfo(Form("CDB object stored into file %s", filename.Data()));
701 AliInfo(Form("Storage Element: %s", fSE.Data()));
702 return result;
703}
704//_____________________________________________________________________________
705Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
706// add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
707
708 Bool_t result = kTRUE;
709 AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
710 TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
711 TGridResult *gridres = gGrid->Command(addTag.Data());
712 const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
713 if(resCode[0] != '1') {
714 AliError(Form("Couldn't add %s tags to folder %s !",
715 tagname, folderToTag.Data()));
716 result = kFALSE;
717 }
718 delete gridres;
719 return result;
720}
721
722//_____________________________________________________________________________
723Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
724// tag stored object in CDB table using object Id's parameters
725
726 TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
727 TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
728 id->GetFirstRun(),
729 id->GetLastRun(),
730 id->GetVersion());
731 TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\"",
732 id->GetPathLevel(0).Data(),
733 id->GetPathLevel(1).Data(),
734 id->GetPathLevel(2).Data());
735 TString addTagValue = Form("%s%s%s",
736 addTagValue1.Data(),
737 addTagValue2.Data(),
738 addTagValue3.Data());
739
740 Bool_t result = kFALSE;
741 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
742 TGridResult* res = gGrid->Command(addTagValue.Data());
743 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
744 if(resCode[0] != '1') {
745 AliError(Form("Couldn't add CDB tag value to file %s !",
746 filename.Data()));
747 result = kFALSE;
748 } else {
749 AliDebug(2, "Object successfully tagged.");
750 result = kTRUE;
751 }
752 delete res;
753 return result;
754
755}
756
757//_____________________________________________________________________________
758Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
759// tag folder with ShortLived tag
760
761 TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
762
763 Bool_t result = kFALSE;
764 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
765 TGridResult* res = gGrid->Command(addTagValue.Data());
766 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
767 if(resCode[0] != '1') {
768 AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
769 result = kFALSE;
770 } else {
771 AliDebug(2,"Object successfully tagged.");
772 result = kTRUE;
773 }
774 delete res;
775 return result;
776
777}
778
779//_____________________________________________________________________________
780Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
781// tag stored object in CDB table using object Id's parameters
782
783 TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
784 TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
785 md->GetObjectClassName(),
786 md->GetResponsible(),
787 md->GetBeamPeriod());
788 TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
789 md->GetAliRootVersion(),
790 md->GetComment());
791 TString addTagValue = Form("%s%s%s",
792 addTagValue1.Data(),
793 addTagValue2.Data(),
794 addTagValue3.Data());
795
796 Bool_t result = kFALSE;
797 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
798 TGridResult* res = gGrid->Command(addTagValue.Data());
799 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
800 if(resCode[0] != '1') {
801 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
802 filename.Data()));
803 result = kFALSE;
804 } else {
805 AliDebug(2,"Object successfully tagged.");
806 result = kTRUE;
807 }
808 return result;
809}
810
811//_____________________________________________________________________________
812TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
813
814 TString turl(fileName);
815 turl.Prepend("/alien" + fDBFolder);
816 turl += "?se="; turl += fSE.Data();
817 TFile *file = TFile::Open(turl);
818 if (!file) {
819 AliError(Form("Can't open selection file <%s>!", turl.Data()));
820 return NULL;
821 }
822
823 TList *list = new TList();
824 list->SetOwner();
825 int i=0;
826 TString keycycle;
827
828 AliCDBId *id;
829 while(1){
830 i++;
831 keycycle = "AliCDBId;";
832 keycycle+=i;
833
834 id = (AliCDBId*) file->Get(keycycle);
835 if(!id) break;
836 list->AddFirst(id);
837 }
838 file->Close(); delete file; file=0;
839
840 return list;
841
842
843}
844
845//_____________________________________________________________________________
846Bool_t AliCDBGrid::Contains(const char* path) const{
847// check for path in storage's DBFolder
848
849 TString initDir(gGrid->Pwd(0));
850 TString dirName(fDBFolder);
851 dirName += path; // dirName = fDBFolder/path
852 Bool_t result=kFALSE;
853 if (gGrid->Cd(dirName,0)) result=kTRUE;
854 gGrid->Cd(initDir.Data(),0);
855 return result;
856}
857
858//_____________________________________________________________________________
859void AliCDBGrid::QueryValidFiles()
860{
861// Query the CDB for files valid for AliCDBStorage::fRun
862// fills list fValidFileIds with AliCDBId objects created from file name
863
864 TString filter;
865 MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
866
867 TString pattern = Form("%s/Run*", fPathFilter.GetPath().Data());
868 if(fVersion >= 0) pattern += Form("_v%d*", fVersion);
869 pattern += ".root";
870 AliDebug(2,Form("pattern: %s", pattern.Data()));
871
872 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
873
874 if (!res) {
875 AliError("Grid query failed");
876 return;
877 }
878
879 AliCDBId validFileId;
880 for(int i=0; i<res->GetEntries(); i++){
881 TString filename = res->GetKey(i, "lfn");
882 if(filename == "") continue;
883 AliDebug(2,Form("Found valid file: %s", filename.Data()));
884 Bool_t result = FilenameToId(filename, validFileId);
885 if(result) {
886 fValidFileIds.AddLast(validFileId.Clone());
887 }
888 }
889 delete res;
890
891}
892
893//_____________________________________________________________________________
894void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
895 const AliCDBMetaData* md, TString& result) const
896{
897// create filter for file query
898
899 result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
900
901// if(version >= 0) {
902// result += Form(" and CDB:version=%d", version);
903// }
904// if(pathFilter.GetLevel0() != "*") {
905// result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
906// }
907// if(pathFilter.GetLevel1() != "*") {
908// result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
909// }
910// if(pathFilter.GetLevel2() != "*") {
911// result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
912// }
913
914 if(md){
915 if(md->GetObjectClassName()[0] != '\0') {
916 result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
917 }
918 if(md->GetResponsible()[0] != '\0') {
919 result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
920 }
921 if(md->GetBeamPeriod() != 0) {
922 result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
923 }
924 if(md->GetAliRootVersion()[0] != '\0') {
925 result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
926 }
927 if(md->GetComment()[0] != '\0') {
928 result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
929 }
930 }
931 AliDebug(2, Form("filter: %s",result.Data()));
932
933}
934
935//_____________________________________________________________________________
936Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
937// get last version found in the database valid for run and path
938
939 AliCDBPath aCDBPath(path);
940 if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
941 AliError(Form("Invalid path in request: %s", path));
942 return -1;
943 }
944 AliCDBId query(path, run, run, -1, -1);
945 AliCDBId* dataId = 0;
946
947 // look for file matching query requests (path, runRange, version)
948 if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
949 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
950 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
951 run, path));
952 dataId = GetId(fValidFileIds, query);
953 if (!dataId) return -1;
954 Int_t version = dataId->GetVersion();
955 delete dataId;
956 return version;
957
958 }
959 // List of files valid for reqested run was not loaded. Looking directly into CDB
960 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
961 run, path));
962
963 TObjArray validFileIds;
964 validFileIds.SetOwner(1);
965
966 TString filter;
967 MakeQueryFilter(run, run, 0, filter);
968
969 TString pattern = Form("%s/Run*.root", path);
970 AliDebug(2,Form("pattern: %s", pattern.Data()));
971
972 TGridResult *res = gGrid->Query(fDBFolder, pattern, filter, "");
973 AliCDBId validFileId;
974 for(int i=0; i<res->GetEntries(); i++){
975 TString filename = res->GetKey(i, "lfn");
976 if(filename == "") continue;
977 if(FilenameToId(filename, validFileId))
978 validFileIds.AddLast(validFileId.Clone());
979 }
980 delete res;
981
982 dataId = GetId(validFileIds, query);
983 if (!dataId) return -1;
984
985 Int_t version = dataId->GetVersion();
986 delete dataId;
987 return version;
988
989}
990
991//_____________________________________________________________________________
992Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
993// get last subversion found in the database valid for run and path
994 AliError("Objects in GRID storage have no sub version!");
995 return -1;
996}
997
998
999/////////////////////////////////////////////////////////////////////////////////////////////////
1000// //
1001// AliCDBGrid factory //
1002// //
1003/////////////////////////////////////////////////////////////////////////////////////////////////
1004
1005ClassImp(AliCDBGridFactory)
1006
1007//_____________________________________________________________________________
1008Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1009// check if the string is valid Grid URI
1010
1011 TRegexp gridPattern("^alien://.+$");
1012 TRegexp gridRawPattern("^raw://.+$");
1013
1014 return (TString(gridString).Contains(gridPattern)||TString(gridString).Contains(gridRawPattern));
1015}
1016
1017//_____________________________________________________________________________
1018AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
1019// create AliCDBGridParam class from the URI string
1020
1021 if (!Validate(gridString)) {
1022 return NULL;
1023 }
1024
1025 TString buffer(gridString);
1026 Bool_t rawFlag = kFALSE; // flag to say whether we are in the "raw://" case
1027
1028 TString gridUrl = "alien://";
1029 TString user = "";
1030 TString dbFolder = "";
1031 TString se = "default";
1032 TString cacheFolder = "";
1033 Bool_t operateDisconnected = kTRUE;
1034 Long64_t cacheSize = (UInt_t) 1024*1024*1024; // 1GB
1035 Long_t cleanupInterval = 0;
1036
1037 TObjArray *arr = buffer.Tokenize('?');
1038 TIter iter(arr);
1039 TObjString *str = 0;
1040
1041 while((str = (TObjString*) iter.Next())){
1042 TString entry(str->String());
1043 Int_t indeq = entry.Index('=');
1044 if(indeq == -1) {
1045 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
1046 gridUrl = entry;
1047 continue;
1048 } else if(entry.BeginsWith("raw://")) { // maybe it's a gridRawUrl!
1049 gridUrl = entry;
1050 rawFlag = kTRUE;
1051 continue;
1052 } else {
1053 AliError(Form("Invalid entry! %s",entry.Data()));
1054 continue;
1055 }
1056 }
1057
1058 TString key = entry(0,indeq);
1059 TString value = entry(indeq+1,entry.Length()-indeq);
1060
1061 if(key.Contains("grid",TString::kIgnoreCase)) {
1062 gridUrl += value;
1063 }
1064 else if (key.Contains("user",TString::kIgnoreCase)){
1065 user = value;
1066 }
1067 else if (key.Contains("se",TString::kIgnoreCase)){
1068 se = value;
1069 }
1070 else if (key.Contains("cacheF",TString::kIgnoreCase)){
1071 cacheFolder = value;
1072 if (!cacheFolder.EndsWith("/"))
1073 cacheFolder += "/";
1074 }
1075 else if (key.Contains("folder",TString::kIgnoreCase)){
1076 dbFolder = value;
1077 }
1078 else if (key.Contains("operateDisc",TString::kIgnoreCase)){
1079 if(value == "kTRUE") {
1080 operateDisconnected = kTRUE;
1081 } else if (value == "kFALSE") {
1082 operateDisconnected = kFALSE;
1083 } else if (value == "0" || value == "1") {
1084 operateDisconnected = (Bool_t) value.Atoi();
1085 } else {
1086 AliError(Form("Invalid entry! %s",entry.Data()));
1087 return NULL;
1088 }
1089 }
1090 else if (key.Contains("cacheS",TString::kIgnoreCase)){
1091 if(value.IsDigit()) {
1092 cacheSize = value.Atoi();
1093 } else {
1094 AliError(Form("Invalid entry! %s",entry.Data()));
1095 return NULL;
1096 }
1097 }
1098 else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
1099 if(value.IsDigit()) {
1100 cleanupInterval = value.Atoi();
1101 } else {
1102 AliError(Form("Invalid entry! %s",entry.Data()));
1103 return NULL;
1104 }
1105 }
1106 else{
1107 AliError(Form("Invalid entry! %s",entry.Data()));
1108 return NULL;
1109 }
1110 }
1111 delete arr; arr=0;
1112
1113 AliDebug(2, Form("gridUrl: %s", gridUrl.Data()));
1114 AliDebug(2, Form("user: %s", user.Data()));
1115 AliDebug(2, Form("dbFolder: %s", dbFolder.Data()));
1116 AliDebug(2, Form("s.e.: %s", se.Data()));
1117 AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
1118 AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
1119 AliDebug(2, Form("local cache size: %d", cacheSize));
1120 AliDebug(2, Form("local cache cleanup interval: %d", cleanupInterval));
1121
1122 if(dbFolder == "" && !rawFlag){
1123 AliError("Base folder must be specified!");
1124 return NULL;
1125 }
1126
1127 return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1128 dbFolder.Data(), se.Data(), cacheFolder.Data(),
1129 operateDisconnected, cacheSize, cleanupInterval);
1130}
1131
1132//_____________________________________________________________________________
1133AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1134// create AliCDBGrid storage instance from parameters
1135
1136 AliCDBGrid *grid = 0;
1137 if (AliCDBGridParam::Class() == param->IsA()) {
1138
1139 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
1140 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1141 gridParam->GetUser().Data(),
1142 gridParam->GetDBFolder().Data(),
1143 gridParam->GetSE().Data(),
1144 gridParam->GetCacheFolder().Data(),
1145 gridParam->GetOperateDisconnected(),
1146 gridParam->GetCacheSize(),
1147 gridParam->GetCleanupInterval());
1148
1149 }
1150
1151 if(!gGrid && grid) {
1152 delete grid; grid=0;
1153 }
1154
1155 return grid;
1156}
1157
1158/////////////////////////////////////////////////////////////////////////////////////////////////
1159// //
1160// AliCDBGrid Parameter class // //
1161// //
1162/////////////////////////////////////////////////////////////////////////////////////////////////
1163
1164ClassImp(AliCDBGridParam)
1165
1166//_____________________________________________________________________________
1167AliCDBGridParam::AliCDBGridParam():
1168 AliCDBParam(),
1169 fGridUrl(),
1170 fUser(),
1171 fDBFolder(),
1172 fSE(),
1173 fCacheFolder(),
1174 fOperateDisconnected(),
1175 fCacheSize(),
1176 fCleanupInterval()
1177
1178 {
1179// default constructor
1180
1181}
1182
1183//_____________________________________________________________________________
1184AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
1185 const char* se, const char* cacheFolder, Bool_t operateDisconnected,
1186 Long64_t cacheSize, Long_t cleanupInterval):
1187 AliCDBParam(),
1188 fGridUrl(gridUrl),
1189 fUser(user),
1190 fDBFolder(dbFolder),
1191 fSE(se),
1192 fCacheFolder(cacheFolder),
1193 fOperateDisconnected(operateDisconnected),
1194 fCacheSize(cacheSize),
1195 fCleanupInterval(cleanupInterval)
1196{
1197// constructor
1198
1199 SetType("alien");
1200
1201 TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
1202 "?OperateDisconnected=%d?CacheSize=%d?CleanupInterval=%d",
1203 fGridUrl.Data(), fUser.Data(),
1204 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1205 fOperateDisconnected, fCacheSize, fCleanupInterval);
1206
1207 AliDebug(2,Form("uri = %s", uri.Data()));
1208 SetURI(uri.Data());
1209}
1210
1211//_____________________________________________________________________________
1212AliCDBGridParam::~AliCDBGridParam() {
1213// destructor
1214
1215}
1216
1217//_____________________________________________________________________________
1218AliCDBParam* AliCDBGridParam::CloneParam() const {
1219// clone parameter
1220
1221 return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
1222 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1223 fOperateDisconnected, fCacheSize, fCleanupInterval);
1224}
1225
1226//_____________________________________________________________________________
1227ULong_t AliCDBGridParam::Hash() const {
1228// return Hash function
1229
1230 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
1231}
1232
1233//_____________________________________________________________________________
1234Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
1235// check if this object is equal to AliCDBParam obj
1236
1237 if (this == obj) {
1238 return kTRUE;
1239 }
1240
1241 if (AliCDBGridParam::Class() != obj->IsA()) {
1242 return kFALSE;
1243 }
1244
1245 AliCDBGridParam* other = (AliCDBGridParam*) obj;
1246
1247 if(fGridUrl != other->fGridUrl) return kFALSE;
1248 if(fUser != other->fUser) return kFALSE;
1249 if(fDBFolder != other->fDBFolder) return kFALSE;
1250 if(fSE != other->fSE) return kFALSE;
1251 if(fCacheFolder != other->fCacheFolder) return kFALSE;
1252 if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
1253 if(fCacheSize != other->fCacheSize) return kFALSE;
1254 if(fCleanupInterval != other->fCleanupInterval) return kFALSE;
1255 return kTRUE;
1256}
1257