]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCDBGrid.cxx
Creation of tags after the reconstruction (P.Christakoglou)
[u/mrichter/AliRoot.git] / STEER / AliCDBGrid.cxx
CommitLineData
9e1ceb13 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 "AliCDBGrid.h"
36
37
38ClassImp(AliCDBGrid)
39
40//_____________________________________________________________________________
41AliCDBGrid::AliCDBGrid(const char *host, const Int_t port,
42 const char *user, const char *dbPath, const char *se) :
43AliCDBStorage(),
44fHost(host),
45fPort(port),
46fUser(user),
47fDBPath(dbPath),
48fSE(se)
49{
50// constructor //
51
52 TString grid="alien://";
53 grid+=host; grid+=":"; grid+=port;
54
55 // if the same Grid is alreay active, skip connection
56 if (!gGrid || TString(host) != gGrid->GetHost() ||
57 port != gGrid->GetPort() || TString(user) != gGrid->GetUser()) {
58 // connection to the Grid
59 TGrid::Connect(grid.Data(),fUser.Data());
60 }
61
62 if(!gGrid) {
63 AliError("Connection failed!");
64 return;
65 }
66
67 TString initDir(gGrid->Pwd(0));
68 if (fDBPath[0] != '/') {
69 fDBPath.Prepend(initDir);
70 }
71
72 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
73 if(!gGrid->Cd(fDBPath.Data(),0)){
74 AliDebug(2,Form("Creating new folder <%s> ...",fDBPath.Data()));
75 if(!gGrid->Mkdir(fDBPath.Data(),"",0)){
76 AliError(Form("Cannot create folder <%s> !",fDBPath.Data()));
77 }
78 } else {
79 AliDebug(2,Form("Folder <%s> found",fDBPath.Data()));
80 }
81
82 // removes any '/' at the end of path, then append one '/'
83 while(fDBPath.EndsWith("/")) fDBPath.Remove(fDBPath.Last('/'));
84 fDBPath+="/";
85
86 // return to the initial directory
87 gGrid->Cd(initDir.Data(),0);
88}
89
90//_____________________________________________________________________________
91AliCDBGrid::~AliCDBGrid()
92{
93// destructor
94
95}
96
97//_____________________________________________________________________________
98Bool_t AliCDBGrid::FilenameToId(const char* filename, AliCDBRunRange& runRange,
99 Int_t& gridVersion) {
100// build AliCDBId from filename numbers
101
102 Ssiz_t mSize;
103
104 // valid filename: Run#firstRun_#lastRun_v#version.root
105 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
106 keyPattern.Index(filename, &mSize);
107 if (!mSize) {
108 AliDebug(2,Form("Bad filename <%s>.", filename));
109 return kFALSE;
110 }
111
112 TString idString(filename);
113 idString.Resize(idString.Length() - sizeof(".root") + 1);
114
115 TObjArray* strArray = (TObjArray*) idString.Tokenize("_");
116
117 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
118 runRange.SetFirstRun(atoi(firstRunString.Data() + 3));
119 runRange.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
120
121 TString verString(((TObjString*) strArray->At(2))->GetString());
122 gridVersion = atoi(verString.Data() + 1);
123
124 delete strArray;
125
126 return kTRUE;
127}
128
129//_____________________________________________________________________________
130Bool_t AliCDBGrid::IdToFilename(const AliCDBRunRange& runRange, Int_t gridVersion,
131 TString& filename) {
132// build file name from AliCDBId data (run range, version)
133
134 if (!runRange.IsValid()) {
135 AliWarning(Form("Invalid run range <%d, %d>.",
136 runRange.GetFirstRun(), runRange.GetLastRun()));
137 return kFALSE;
138 }
139
140 if (gridVersion < 0) {
141 AliWarning(Form("Invalid version <%d>.", gridVersion));
142 return kFALSE;
143 }
144
145 filename += "Run";
146 filename += runRange.GetFirstRun();
147 filename += "_";
148 filename += runRange.GetLastRun();
149 filename += "_v";
150 filename += gridVersion;
151 filename += ".root";
152
153 return kTRUE;
154}
155
156//_____________________________________________________________________________
157Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
158// prepare id (version) of the object that will be stored (called by PutEntry)
159
160 TString initDir(gGrid->Pwd(0));
161 TString pathName= id.GetPath();
162
163 TString dirName(fDBPath);
164
165 Bool_t dirExist=kFALSE;
166
167 // go to the path; if directory does not exist, create it
168 TObjArray *arrName=pathName.Tokenize("/");
169 for(int i=0;i<arrName->GetEntries();i++){
170 TString buffer((arrName->At(i))->GetName());
171 dirName+=buffer; dirName+="/";
172 dirExist=gGrid->Cd(dirName,0);
173 if (!dirExist) {
174 AliInfo(Form("Creating new folder <%s> ...",dirName.Data()));
175 if(!gGrid->Mkdir(dirName,"",0)){
176 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
177 gGrid->Cd(initDir.Data());
178 return kFALSE;
179 }
180 }
181 }
182 delete arrName;
183 gGrid->Cd(initDir,0);
184
185 const char* filename;
186 AliCDBRunRange aRunRange; // the runRange got from filename
187 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
188 Int_t aVersion; // the version got from filename
189 Int_t lastVersion=0; // highest version found
190
191 TGridResult *res = gGrid->Ls(dirName);
192
193 //loop on the files in the directory, look for highest version
194 for(int i=0; i < res->GetEntries(); i++){
195 filename=res->GetFileName(i);
196 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
197 if (aRunRange.Overlaps(id.GetAliCDBRunRange()) && aVersion > lastVersion) {
198 lastVersion = aVersion;
199 lastRunRange = aRunRange;
200 }
201
202 }
203 delete res;
204
205 id.SetVersion(lastVersion + 1);
206
207 TString lastStorage = id.GetLastStorage();
208 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
7541e2ae 209 AliWarning(Form("*** WARNING! a NEW object is being stored with version %d",
9e1ceb13 210 id.GetVersion()));
7541e2ae 211 AliWarning(Form("and it will hide previously stored object with version %d!",
9e1ceb13 212 id.GetVersion()-1));
213 }
214
215 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
216 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
217 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
218
219 return kTRUE;
220}
221
222//_____________________________________________________________________________
223AliCDBId AliCDBGrid::GetId(const AliCDBId& query) {
224// look for filename matching query (called by GetEntry)
225
226 TString initDir(gGrid->Pwd(0));
227
228 AliCDBId result(query.GetAliCDBPath(), -1, -1, -1, -1);
229
230 TString dirName(fDBPath);
231 dirName += query.GetPath(); // dirName = fDBPath/idPath
232
233 if (!gGrid->Cd(dirName,0)) {
234 AliError(Form("Directory <%s> not found", (query.GetPath()).Data()));
235 AliError(Form("in DB folder %s", fDBPath.Data()));
236 return result;
237 }
238
239 TGridResult *res = gGrid->Ls(dirName);
240
241 const char* filename;
242 AliCDBRunRange aRunRange; // the runRange got from filename
243 Int_t aVersion; // the version got from filename
244
245 for(int i=0; i < res->GetEntries(); i++){
246 filename=res->GetFileName(i);
247 if (!FilenameToId(filename, aRunRange, aVersion)) continue;
248 // aRunRange and aVersion filled from filename
249
250 if (!aRunRange.Comprises(query.GetAliCDBRunRange())) continue;
251 // aRunRange contains requested run!
252
253 if (!query.HasVersion()){ // look for highest version
254 if(result.GetVersion() > aVersion) continue;
255 if(result.GetVersion() == aVersion) {
256 AliError(Form("More than one object valid for run %d, version %d!",
257 query.GetFirstRun(), aVersion));
258 result.SetRunRange(-1,-1); result.SetVersion(-1);
259 return result;
260 }
261 result.SetVersion(aVersion);
262 result.SetFirstRun(aRunRange.GetFirstRun());
263 result.SetLastRun(aRunRange.GetLastRun());
264
265 } else { // look for specified version
266 if(query.GetVersion() != aVersion) continue;
267 if(result.GetVersion() == aVersion){
268 AliError(Form("More than one object valid for run %d, version %d!",
269 query.GetFirstRun(), aVersion));
270 result.SetRunRange(-1,-1); result.SetVersion(-1);
271 return result;
272 }
273 result.SetVersion(aVersion);
274 result.SetFirstRun(aRunRange.GetFirstRun());
275 result.SetLastRun(aRunRange.GetLastRun());
276 }
277 } // end loop on filenames
278 delete res;
279
280 gGrid->Cd(initDir.Data(),0);
281
282 return result;
283}
284
285//_____________________________________________________________________________
286AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
287// get AliCDBEntry from the database
288
289 AliCDBId dataId;
290
291 // look for a filename matching query requests (path, runRange, version, subVersion)
292 if (!queryId.HasVersion()) {
293 // if version is not specified, first check the selection criteria list
294 dataId = GetId(GetSelection(queryId));
295 } else {
296 dataId = GetId(queryId);
297 }
298
299 if (!dataId.IsSpecified()) return NULL;
300
301 TString filename;
302 if (!IdToFilename(dataId.GetAliCDBRunRange(), dataId.GetVersion(),filename)) {
303 AliError("Bad data ID encountered! Subnormal error!");
304 return NULL;
305 }
306
307 filename.Prepend("/alien" + fDBPath + queryId.GetPath() + '/');
308 filename += "?se="; filename += fSE.Data();
309
310 AliInfo(Form("Opening file: %s",filename.Data()));
311 TFile *file = TFile::Open(filename);
312 if (!file) {
313 AliError(Form("Can't open file <%s>!", filename.Data()));
314 return NULL;
315 }
316
317 // get the only AliCDBEntry object from the file
318 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
319
320 TObject* anObject = file->Get("AliCDBEntry");
321
322 if (!anObject) {
323 AliError("Bad storage data: NULL entry object!");
324 return NULL;
325 }
326
327 if (AliCDBEntry::Class() != anObject->IsA()) {
328 AliError("Bad storage data: Invalid entry object!");
329 return NULL;
330 }
331
332 AliCDBId entryId = ((AliCDBEntry* ) anObject)->GetId();
333
334 // The object's Id is not reset during storage
335 // If object's Id runRange or version do not match with filename,
336 // it means that someone renamed file by hand. In this case a warning msg is issued.
337
338 ((AliCDBEntry*) anObject)->SetLastStorage("grid");
339
340 if(!((entryId.GetAliCDBRunRange()).IsEqual(&dataId.GetAliCDBRunRange())) ||
341 entryId.GetVersion() != dataId.GetVersion()){
342 AliWarning(Form("Either RunRange or gridVersion in the object's metadata do noth match with fileName numbers:"));
343 AliWarning(Form("someone renamed file by hand!"));
344 }
345
346 // close file, return retieved entry
347 file->Close(); delete file; file=0;
348 return (AliCDBEntry*) anObject;
349}
350
351//_____________________________________________________________________________
352void AliCDBGrid::GetEntriesForLevel0(const char* level0,
353 const AliCDBId& queryId, TList* result) {
354// multiple request (AliCDBStorage::GetAll)
355
356 TString level0Dir=fDBPath;
357 level0Dir += level0;
358
359 if (!gGrid->Cd(level0Dir,0)) {
360 AliError(Form("Level0 directory <%s> not found", level0Dir.Data()));
361 return;
362 }
363
364 TGridResult *res = gGrid->Ls(level0Dir);
365 TString level1;
366 for(int i=0; i < res->GetEntries(); i++){
367 level1=res->GetFileName(i);
368 if (queryId.GetAliCDBPath().Level1Comprises(level1))
369 GetEntriesForLevel1(level0, level1, queryId, result);
370 }
371 delete res;
372}
373
374//_____________________________________________________________________________
375void AliCDBGrid::GetEntriesForLevel1(const char* level0, const char* level1,
376 const AliCDBId& queryId, TList* result) {
377// multiple request (AliCDBStorage::GetAll)
378
379 TString level1Dir=fDBPath;
380 level1Dir += level0;
381 level1Dir += '/';
382 level1Dir += level1;
383
384 if (!gGrid->Cd(level1Dir,0)) {
385 AliError(Form("Level1 directory <%s> not found", level1Dir.Data()));
386 return;
387 }
388
389 TGridResult *res = gGrid->Ls(level1Dir);
390 TString level2;
391 for(int i=0; i < res->GetEntries(); i++){
392 level2=res->GetFileName(i);
393 if (queryId.GetAliCDBPath().Level2Comprises(level2)){
394 AliCDBPath entryPath(level0, level1, level2);
395 AliCDBId entryId(entryPath,
396 queryId.GetAliCDBRunRange(),
397 queryId.GetVersion(),
398 queryId.GetSubVersion());
399
400 AliCDBEntry* anEntry = GetEntry(entryId);
401 if (anEntry) result->Add(anEntry);
402
403 }
404 }
405 delete res;
406}
407
408//_____________________________________________________________________________
409TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
410// multiple request (AliCDBStorage::GetAll)
411
412 TList* result = new TList();
413 result->SetOwner();
414
415 TString initDir(gGrid->Pwd(0));
416
417 TGridResult *res = gGrid->Ls(fDBPath);
418 TString level0;
419
420 for(int i=0; i < res->GetEntries(); i++){
421 level0=res->GetFileName(i);
422 if (queryId.GetAliCDBPath().Level0Comprises(level0))
423 GetEntriesForLevel0(level0, queryId, result);
424 }
425 delete res;
426
427 gGrid->Cd(initDir.Data(),0);
428 return result;
429}
430
431//_____________________________________________________________________________
432Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry) {
433// put an AliCDBEntry object into the database
434
435 AliCDBId& id = entry->GetId();
436
437 // set version for the entry to be stored
438 if (!PrepareId(id)) return kFALSE;
439
440 // build filename from entry's id
441 TString filename;
442 if (!IdToFilename(id.GetAliCDBRunRange(), id.GetVersion(), filename)) {
443 AliError("Bad ID encountered! Subnormal error!");
444 return kFALSE;
445 }
446
447 filename.Prepend("/alien" + fDBPath + id.GetPath() + '/');
448 TString filenameCopy(filename);
449 filename += "?se="; filename += fSE.Data();
450
451 TDirectory* saveDir = gDirectory;
452
453 // open file
454 TFile *file = TFile::Open(filename,"CREATE");
455 if(!file || !file->IsWritable()){
456 AliError(Form("Can't open file <%s>!", filename.Data()));
457 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
458 return kFALSE;
459 }
460
461 file->cd();
462
463 entry->SetVersion(id.GetVersion());
464
465 // write object (key name: "AliCDBEntry")
466 Bool_t result = (entry->Write("AliCDBEntry") != 0);
467 if (!result) AliError(Form("Can't write entry to file <%s>!",filename.Data()));
468
469
470 if (saveDir) saveDir->cd(); else gROOT->cd();
471 file->Close(); delete file; file=0;
472 if(result) {
473 AliInfo(Form("AliCDBEntry stored into file %s",filenameCopy.Data()));
474 AliInfo(Form("using S.E. %s", fSE.Data()));
475 }
476
477 return result;
478}
479
480/////////////////////////////////////////////////////////////////////////////////////////////////
481// //
482// AliCDBGrid factory //
483// //
484/////////////////////////////////////////////////////////////////////////////////////////////////
485
486ClassImp(AliCDBGridFactory)
487
488//_____________________________________________________________________________
489Bool_t AliCDBGridFactory::Validate(const char* gridString) {
490// check if the string is valid Grid URI
491
492 // pattern: alien://hostName:Port;user;dbPath;SE
493 // example of a valid pattern:
494 // "alien://aliendb4.cern.ch:9000;colla;DBTest;ALICE::CERN::Server"
495 TRegexp gridPattern("^alien://.+:[0-9]+;[a-zA-Z0-9_-.]+;.+;.+$");
496
497 return TString(gridString).Contains(gridPattern);
498}
499
500//_____________________________________________________________________________
501AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
502// create AliCDBGridParam class from the URI string
503
504 if (!Validate(gridString)) {
505 return NULL;
506 }
507 TString buffer(gridString + sizeof("alien://") - 1);
508 TString host = buffer(0,buffer.First(':')); // host (ex. aliendb4.cern.ch)
509 buffer = buffer(host.Sizeof(),buffer.Sizeof());
510 TString strPort = buffer(0, buffer.First(';'));
511 Int_t port = atoi(strPort.Data()); // port number (ex. 9000)
512 buffer = buffer(strPort.Sizeof(),buffer.Sizeof());
513 TString user = buffer(0,buffer.First(';')); // user (ex. colla)
514 buffer = buffer(user.Sizeof(),buffer.Sizeof());
515 TString dbPath = buffer(0,buffer.First(';')); // DB path (ex. /alice/cern.ch/user/c/colla/DB)
516 TString se = buffer(dbPath.Sizeof(),buffer.Sizeof()); // storage element (ex. ALICE::CERN::Server)
517
518 AliInfo(Form("host: %s",host.Data()));
519 AliInfo(Form("port: %d",port));
520 AliInfo(Form("user: %s",user.Data()));
521 AliInfo(Form("dbPath: %s",dbPath.Data()));
522 AliInfo(Form("s.e.: %s",se.Data()));
523
524 return new AliCDBGridParam(host, port, user, dbPath, se);
525}
526
527//_____________________________________________________________________________
528AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
529// create AliCDBGrid storage instance from parameters
530
531 if (AliCDBGridParam::Class() == param->IsA()) {
532
533 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
534 AliCDBGrid *grid = new AliCDBGrid(gridParam->GetHost(), gridParam->GetPort(),
535 gridParam->GetUser(), gridParam->GetPath(),
536 gridParam->GetSE());
537
538 if(gGrid) return grid;
539 }
540
541 return NULL;
542}
543
544/////////////////////////////////////////////////////////////////////////////////////////////////
545// //
546// AliCDBGrid Parameter class // //
547// //
548/////////////////////////////////////////////////////////////////////////////////////////////////
549
550ClassImp(AliCDBGridParam)
551
552//_____________________________________________________________________________
553AliCDBGridParam::AliCDBGridParam() {
554// default constructor
555
556}
557
558//_____________________________________________________________________________
559AliCDBGridParam::AliCDBGridParam(const char* host,
560 const Int_t port,
561 const char* user,
562 const char* dbPath,
563 const char* se):
564 fHost(host),
565 fPort(port),
566 fUser(user),
567 fDBPath(dbPath),
568 fSE(se)
569{
570// constructor
571
572 SetType("alien");
573
574 TString uri=("alien://");
575 uri+=host; uri+=":"; uri+=port; uri+=";";
576 uri+=user; uri+=";"; uri+=dbPath; uri+=";";
577 uri+=se;
578
579 SetURI(uri);
580}
581
582//_____________________________________________________________________________
583AliCDBGridParam::~AliCDBGridParam() {
584// destructor
585
586}
587
588//_____________________________________________________________________________
589AliCDBParam* AliCDBGridParam::CloneParam() const {
590// clone parameter
591
592 return new AliCDBGridParam(fHost, fPort, fUser, fDBPath, fSE);
593}
594
595//_____________________________________________________________________________
596ULong_t AliCDBGridParam::Hash() const {
597// return Hash function
598
599 return fHost.Hash()+fPort+fUser.Hash()+fDBPath.Hash()+fSE.Hash();
600}
601
602//_____________________________________________________________________________
603Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
604// check if this object is equal to AliCDBParam obj
605
606 if (this == obj) {
607 return kTRUE;
608 }
609
610 if (AliCDBGridParam::Class() != obj->IsA()) {
611 return kFALSE;
612 }
613
614 AliCDBGridParam* other = (AliCDBGridParam*) obj;
615
616 if(fHost != other->fHost) return kFALSE;
617 if(fPort != other->fPort) return kFALSE;
618 if(fUser != other->fUser) return kFALSE;
619 if(fDBPath != other->fDBPath) return kFALSE;
620 if(fSE != other->fSE) return kFALSE;
621 return kTRUE;
622}
623