]>
Commit | Line | Data |
---|---|---|
2c8628dd | 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 | /* $Id$ */ | |
17 | ||
18 | /////////////////////////////////////////////////////////////////////////////// | |
19 | // // | |
20 | // base class for data base access classes // | |
21 | // // | |
22 | /////////////////////////////////////////////////////////////////////////////// | |
23 | ||
24 | ||
25 | #include <TFile.h> | |
26 | #include <TKey.h> | |
27 | #include <TROOT.h> | |
28 | ||
29 | #include "AliLog.h" | |
fe913d8f | 30 | #include "AliCDBEntry.h" |
31 | #include "AliCDBStorage.h" | |
2c8628dd | 32 | |
33 | ||
fe913d8f | 34 | ClassImp(AliCDBStorage) |
2c8628dd | 35 | |
36 | ||
fe913d8f | 37 | AliCDBStorage* AliCDBStorage::fgInstance = NULL; |
2c8628dd | 38 | |
39 | ||
40 | //_____________________________________________________________________________ | |
fe913d8f | 41 | AliCDBStorage::AliCDBStorage() : |
2c8628dd | 42 | TObject(), |
43 | fSelection(), | |
44 | fEntries(), | |
fe913d8f | 45 | fDumpFile(NULL) |
2c8628dd | 46 | { |
47 | // default constructor | |
2c8628dd | 48 | if (fgInstance) delete fgInstance; |
49 | fgInstance = this; | |
fe913d8f | 50 | fStorageMode=kDevelopment; |
2c8628dd | 51 | } |
52 | ||
53 | //_____________________________________________________________________________ | |
fe913d8f | 54 | AliCDBStorage::~AliCDBStorage() |
2c8628dd | 55 | { |
56 | // destructor | |
2c8628dd | 57 | fSelection.Delete(); |
58 | fEntries.Delete(); | |
fe913d8f | 59 | if (fDumpFile) { |
60 | fDumpFile->Close(); | |
61 | delete fDumpFile; | |
2c8628dd | 62 | } |
63 | fgInstance = NULL; | |
64 | } | |
65 | ||
66 | //_____________________________________________________________________________ | |
fe913d8f | 67 | AliCDBStorage::AliCDBStorage(const AliCDBStorage& db) : |
2c8628dd | 68 | TObject(db), |
69 | fSelection(), | |
70 | fEntries(), | |
fe913d8f | 71 | fDumpFile(NULL) |
2c8628dd | 72 | { |
73 | // copy constructor | |
74 | ||
75 | AliFatal("not implemented"); | |
76 | } | |
77 | ||
78 | //_____________________________________________________________________________ | |
fe913d8f | 79 | AliCDBStorage& AliCDBStorage::operator = (const AliCDBStorage& /*db*/) |
2c8628dd | 80 | { |
81 | // assignment operator | |
82 | ||
83 | AliFatal("not implemented"); | |
84 | return *this; | |
85 | } | |
86 | ||
87 | ||
88 | //_____________________________________________________________________________ | |
fe913d8f | 89 | const TObject* AliCDBStorage::Get(const char* name, Int_t runNumber) |
2c8628dd | 90 | { |
91 | // get an object from the data base | |
fe913d8f | 92 | // (AliCDBStorage is NOT the owner of the returned object) |
f05209ee | 93 | // name must be in the form "Detector/DBType/DetSpecType" |
94 | // es: "ZDC/Calib/Pedestals" | |
2c8628dd | 95 | |
fe913d8f | 96 | AliCDBMetaDataSelect defaultMetaData; |
97 | AliCDBMetaDataSelect* selectedMetaData = &defaultMetaData; | |
2c8628dd | 98 | |
99 | // look for a meta data selection | |
100 | for (Int_t i = 0; i < fSelection.GetEntriesFast(); i++) { | |
fe913d8f | 101 | AliCDBMetaDataSelect* selection = (AliCDBMetaDataSelect*) fSelection[i]; |
2c8628dd | 102 | if (!selection) continue; |
103 | if (selection->Matches(name, runNumber)) { | |
104 | selectedMetaData = selection; | |
105 | } | |
106 | } | |
107 | ||
108 | // get the entry | |
fe913d8f | 109 | AliCDBMetaDataSelect selMetaData(*selectedMetaData); |
f05209ee | 110 | selMetaData.SetName(name); |
fe913d8f | 111 | AliCDBEntry* entry = GetEntry(selMetaData, runNumber); |
2c8628dd | 112 | if (entry) { |
113 | AliDebug(2, "got the entry:"); | |
114 | ToAliDebug(2, entry->Dump()); | |
115 | } else { | |
116 | AliDebug(2, Form("got no entry for %s", name)); | |
117 | } | |
118 | ||
119 | // update array of current entries | |
f05209ee | 120 | if (!entry) return NULL; |
2c8628dd | 121 | TObject* oldEntry = fEntries.FindObject(entry->GetName()); |
122 | if (oldEntry) { | |
123 | delete fEntries.Remove(oldEntry); | |
124 | } | |
125 | fEntries.Add(entry); | |
126 | ||
fe913d8f | 127 | // Dump entry to a file (in the same way as AliCDBDump::PutEntry, |
128 | // so that the file can be opened as a AliCDBDump!) | |
f05209ee | 129 | |
fe913d8f | 130 | if (fDumpFile) { |
131 | fDumpFile->cd(); | |
f05209ee | 132 | TDirectory* saveDir = gDirectory; |
133 | ||
134 | // go to or create the directory | |
135 | TString strname(name); | |
136 | while (strname.BeginsWith("/")) strname.Remove(0); | |
fe913d8f | 137 | TDirectory* dir = fDumpFile; |
f05209ee | 138 | Int_t index = -1; |
139 | while ((index = strname.Index("/")) >= 0) { | |
140 | TString dirName(strname(0, index)); | |
141 | if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName); | |
142 | dir->cd(dirName); | |
143 | dir = gDirectory; | |
144 | strname.Remove(0, index+1); | |
145 | } | |
146 | ||
147 | entry->Write(strname); | |
148 | if (saveDir) saveDir->cd(); else gROOT->cd(); | |
149 | ||
2c8628dd | 150 | } |
f05209ee | 151 | |
152 | return (entry->GetObject())->Clone(); | |
2c8628dd | 153 | |
2c8628dd | 154 | } |
155 | ||
2c8628dd | 156 | //_____________________________________________________________________________ |
fe913d8f | 157 | Bool_t AliCDBStorage::Put(const TObject* object, |
158 | const AliCDBMetaData& metaData) | |
2c8628dd | 159 | { |
160 | // put an object into the data base | |
fe913d8f | 161 | // (AliCDBStorage does not adopt the object) |
f05209ee | 162 | // location of where the object is stored is defined by |
fe913d8f | 163 | // the AliCDBMetaData's name ("Detector/DBType/DetSpecType") |
f05209ee | 164 | // and run Range. Storage is handled by the PutEntry method |
fe913d8f | 165 | // of the current AliCDBStorage instance. |
2c8628dd | 166 | |
167 | if (!object) return kFALSE; | |
fe913d8f | 168 | |
169 | AliCDBEntry *entry= new AliCDBEntry(object, metaData); | |
170 | ||
171 | Bool_t result = PutEntry(entry); | |
172 | ||
173 | delete entry; | |
174 | ||
175 | return result; | |
2c8628dd | 176 | } |
177 | ||
178 | //_____________________________________________________________________________ | |
fe913d8f | 179 | Bool_t AliCDBStorage::PutEntry(AliCDBEntry* entry) |
2c8628dd | 180 | { |
181 | // put an object into the data base | |
fe913d8f | 182 | // Refer to the specific method of the current AliCDBStorage instance |
183 | // (AliCDBDump, AliCDBLocalFile, AliCDBGrid) | |
2c8628dd | 184 | |
185 | if (!entry) return kFALSE; | |
186 | AliError(Form("This is a read only data base. " | |
187 | "The object %s was not inserted", entry->GetName())); | |
188 | return kFALSE; | |
189 | } | |
190 | ||
191 | ||
192 | //_____________________________________________________________________________ | |
fe913d8f | 193 | void AliCDBStorage::Select(const AliCDBMetaDataSelect& selMetaData) |
2c8628dd | 194 | { |
195 | // add some meta data selection criteria | |
196 | ||
fe913d8f | 197 | fSelection.Add(new AliCDBMetaDataSelect(selMetaData)); |
2c8628dd | 198 | } |
199 | ||
200 | ||
201 | //_____________________________________________________________________________ | |
fe913d8f | 202 | Bool_t AliCDBStorage::DumpToFile(const char* fileName) |
2c8628dd | 203 | { |
fe913d8f | 204 | // Dump entries retrieved from the data base to a file with the given name |
2c8628dd | 205 | |
fe913d8f | 206 | if (fDumpFile) { |
207 | fDumpFile->Close(); | |
208 | delete fDumpFile; | |
2c8628dd | 209 | } |
210 | ||
211 | TDirectory* dir = gDirectory; | |
fe913d8f | 212 | fDumpFile = TFile::Open(fileName, "UPDATE"); |
2c8628dd | 213 | if (dir) dir->cd(); else gROOT->cd(); |
fe913d8f | 214 | if (!fDumpFile || !fDumpFile->IsOpen()) { |
2c8628dd | 215 | AliError(Form("could not open file %s", fileName)); |
fe913d8f | 216 | delete fDumpFile; |
217 | fDumpFile = NULL; | |
2c8628dd | 218 | return kFALSE; |
219 | } | |
220 | return kTRUE; | |
221 | } | |
222 | ||
223 | ||
224 | //_____________________________________________________________________________ | |
fe913d8f | 225 | AliCDBStorage* AliCDBStorage::Instance() |
2c8628dd | 226 | { |
fe913d8f | 227 | // return the current instance of the DB (AliCDBDump, AliCDBLocalFile...) |
228 | // Example of usage: after creating an istance of AliCDBStorage: | |
229 | // AliCDBStorage::Instance()->Get(...) | |
2c8628dd | 230 | |
231 | return fgInstance; | |
232 | } | |
f05209ee | 233 | |
234 | ||
235 | //_____________________________________________________________________________ | |
fe913d8f | 236 | const AliCDBMetaData& AliCDBStorage::GetCDBMetaData(const char* name) |
f05209ee | 237 | { |
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 | |
240 | // retrieved) | |
241 | ||
fe913d8f | 242 | AliCDBEntry *entry = (AliCDBEntry*) fEntries.FindObject(name); |
f05209ee | 243 | if(!entry){ |
244 | AliError(Form("Entry %s not found! You make me crash!",name)); | |
245 | } | |
fe913d8f | 246 | return entry->GetCDBMetaData(); |
f05209ee | 247 | |
248 | } | |
fe913d8f | 249 |