1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // base class for data base access classes //
22 ///////////////////////////////////////////////////////////////////////////////
30 #include "AliCDBEntry.h"
31 #include "AliCDBStorage.h"
34 ClassImp(AliCDBStorage)
37 AliCDBStorage* AliCDBStorage::fgInstance = NULL;
40 //_____________________________________________________________________________
41 AliCDBStorage::AliCDBStorage() :
47 // default constructor
48 if (fgInstance) delete fgInstance;
50 fStorageMode=kDevelopment;
53 //_____________________________________________________________________________
54 AliCDBStorage::~AliCDBStorage()
66 //_____________________________________________________________________________
67 AliCDBStorage::AliCDBStorage(const AliCDBStorage& db) :
75 AliFatal("not implemented");
78 //_____________________________________________________________________________
79 AliCDBStorage& AliCDBStorage::operator = (const AliCDBStorage& /*db*/)
81 // assignment operator
83 AliFatal("not implemented");
88 //_____________________________________________________________________________
89 const TObject* AliCDBStorage::Get(const char* name, Int_t runNumber)
91 // get an object from the data base
92 // (AliCDBStorage is NOT the owner of the returned object)
93 // name must be in the form "Detector/DBType/DetSpecType"
94 // es: "ZDC/Calib/Pedestals"
96 AliCDBMetaDataSelect defaultMetaData;
97 AliCDBMetaDataSelect* selectedMetaData = &defaultMetaData;
99 // look for a meta data selection
100 for (Int_t i = 0; i < fSelection.GetEntriesFast(); i++) {
101 AliCDBMetaDataSelect* selection = (AliCDBMetaDataSelect*) fSelection[i];
102 if (!selection) continue;
103 if (selection->Matches(name, runNumber)) {
104 selectedMetaData = selection;
109 AliCDBMetaDataSelect selMetaData(*selectedMetaData);
110 selMetaData.SetName(name);
111 AliCDBEntry* entry = GetEntry(selMetaData, runNumber);
113 AliDebug(2, "got the entry:");
114 ToAliDebug(2, entry->Dump());
116 AliDebug(2, Form("got no entry for %s", name));
119 // update array of current entries
120 if (!entry) return NULL;
121 TObject* oldEntry = fEntries.FindObject(entry->GetName());
123 delete fEntries.Remove(oldEntry);
127 // Dump entry to a file (in the same way as AliCDBDump::PutEntry,
128 // so that the file can be opened as a AliCDBDump!)
132 TDirectory* saveDir = gDirectory;
134 // go to or create the directory
135 TString strname(name);
136 while (strname.BeginsWith("/")) strname.Remove(0);
137 TDirectory* dir = fDumpFile;
139 while ((index = strname.Index("/")) >= 0) {
140 TString dirName(strname(0, index));
141 if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
144 strname.Remove(0, index+1);
147 entry->Write(strname);
148 if (saveDir) saveDir->cd(); else gROOT->cd();
152 return (entry->GetObject())->Clone();
156 //_____________________________________________________________________________
157 Bool_t AliCDBStorage::Put(const TObject* object,
158 const AliCDBMetaData& metaData)
160 // put an object into the data base
161 // (AliCDBStorage does not adopt the object)
162 // location of where the object is stored is defined by
163 // the AliCDBMetaData's name ("Detector/DBType/DetSpecType")
164 // and run Range. Storage is handled by the PutEntry method
165 // of the current AliCDBStorage instance.
167 if (!object) return kFALSE;
169 AliCDBEntry *entry= new AliCDBEntry(object, metaData);
171 Bool_t result = PutEntry(entry);
178 //_____________________________________________________________________________
179 Bool_t AliCDBStorage::PutEntry(AliCDBEntry* entry)
181 // put an object into the data base
182 // Refer to the specific method of the current AliCDBStorage instance
183 // (AliCDBDump, AliCDBLocalFile, AliCDBGrid)
185 if (!entry) return kFALSE;
186 AliError(Form("This is a read only data base. "
187 "The object %s was not inserted", entry->GetName()));
192 //_____________________________________________________________________________
193 void AliCDBStorage::Select(const AliCDBMetaDataSelect& selMetaData)
195 // add some meta data selection criteria
197 fSelection.Add(new AliCDBMetaDataSelect(selMetaData));
201 //_____________________________________________________________________________
202 Bool_t AliCDBStorage::DumpToFile(const char* fileName)
204 // Dump entries retrieved from the data base to a file with the given name
211 TDirectory* dir = gDirectory;
212 fDumpFile = TFile::Open(fileName, "UPDATE");
213 if (dir) dir->cd(); else gROOT->cd();
214 if (!fDumpFile || !fDumpFile->IsOpen()) {
215 AliError(Form("could not open file %s", fileName));
224 //_____________________________________________________________________________
225 AliCDBStorage* AliCDBStorage::Instance()
227 // return the current instance of the DB (AliCDBDump, AliCDBLocalFile...)
228 // Example of usage: after creating an istance of AliCDBStorage:
229 // AliCDBStorage::Instance()->Get(...)
235 //_____________________________________________________________________________
236 const AliCDBMetaData& AliCDBStorage::GetCDBMetaData(const char* name)
238 // Returns the object's metadata of the already retrieved object
239 // (useful, for example, if you want to know the format of the object you have
242 AliCDBEntry *entry = (AliCDBEntry*) fEntries.FindObject(name);
244 AliError(Form("Entry %s not found! You make me crash!",name));
246 return entry->GetCDBMetaData();