]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBStorage.cxx
Additional protection
[u/mrichter/AliRoot.git] / STEER / AliCDBStorage.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 /* $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"
30 #include "AliCDBEntry.h"
31 #include "AliCDBStorage.h"
32
33
34 ClassImp(AliCDBStorage)
35
36
37 AliCDBStorage* AliCDBStorage::fgInstance = NULL;
38
39
40 //_____________________________________________________________________________
41 AliCDBStorage::AliCDBStorage() :
42   TObject(),
43   fSelection(),
44   fEntries(),
45   fDumpFile(NULL)
46 {
47 // default constructor
48   if (fgInstance) delete fgInstance;
49   fgInstance = this;
50   fStorageMode=kDevelopment;
51 }
52
53 //_____________________________________________________________________________
54 AliCDBStorage::~AliCDBStorage()
55 {
56 // destructor
57   fSelection.Delete();
58   fEntries.Delete();
59   if (fDumpFile) {
60     fDumpFile->Close();
61     delete fDumpFile;
62   }
63   fgInstance = NULL;
64 }
65
66 //_____________________________________________________________________________
67 AliCDBStorage::AliCDBStorage(const AliCDBStorage& db) :
68   TObject(db),
69   fSelection(),
70   fEntries(),
71   fDumpFile(NULL)
72 {
73 // copy constructor
74
75   AliFatal("not implemented");
76 }
77
78 //_____________________________________________________________________________
79 AliCDBStorage& AliCDBStorage::operator = (const AliCDBStorage& /*db*/)
80 {
81 // assignment operator
82
83   AliFatal("not implemented");
84   return *this;
85 }
86
87
88 //_____________________________________________________________________________
89 const TObject* AliCDBStorage::Get(const char* name, Int_t runNumber)
90 {
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"
95
96   AliCDBMetaDataSelect defaultMetaData;
97   AliCDBMetaDataSelect* selectedMetaData = &defaultMetaData;
98
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;
105     }
106   }
107
108   // get the entry
109   AliCDBMetaDataSelect selMetaData(*selectedMetaData);
110   selMetaData.SetName(name);
111   AliCDBEntry* entry = GetEntry(selMetaData, runNumber);
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
120   if (!entry) return NULL;  
121   TObject* oldEntry = fEntries.FindObject(entry->GetName());
122   if (oldEntry) {
123     delete fEntries.Remove(oldEntry);
124   }
125   fEntries.Add(entry);
126
127   // Dump entry to a file (in the same way as AliCDBDump::PutEntry, 
128   // so that the file can be opened as a AliCDBDump!)
129
130   if (fDumpFile) {
131     fDumpFile->cd();
132     TDirectory* saveDir = gDirectory;
133
134     // go to or create the directory
135     TString strname(name);
136     while (strname.BeginsWith("/")) strname.Remove(0);
137     TDirectory* dir = fDumpFile;
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   
150   }
151   
152   return (entry->GetObject())->Clone();
153
154 }
155
156 //_____________________________________________________________________________
157 Bool_t AliCDBStorage::Put(const TObject* object, 
158                               const AliCDBMetaData& metaData)
159 {
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. 
166
167   if (!object) return kFALSE;
168
169   AliCDBEntry *entry= new AliCDBEntry(object, metaData);
170
171   Bool_t result = PutEntry(entry);
172     
173   delete entry;
174
175   return result;
176 }
177
178 //_____________________________________________________________________________
179 Bool_t AliCDBStorage::PutEntry(AliCDBEntry* entry)
180 {
181 // put an object into the data base
182 // Refer to the specific method of the current AliCDBStorage instance
183 // (AliCDBDump, AliCDBLocalFile, AliCDBGrid)
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 //_____________________________________________________________________________
193 void AliCDBStorage::Select(const AliCDBMetaDataSelect& selMetaData)
194 {
195 // add some meta data selection criteria
196
197   fSelection.Add(new AliCDBMetaDataSelect(selMetaData));
198 }
199
200
201 //_____________________________________________________________________________
202 Bool_t AliCDBStorage::DumpToFile(const char* fileName)
203 {
204 // Dump entries retrieved from the data base to a file with the given name
205
206   if (fDumpFile) {
207     fDumpFile->Close();
208     delete fDumpFile;
209   }
210
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));
216     delete fDumpFile;
217     fDumpFile = NULL;
218     return kFALSE;
219   }
220   return kTRUE;
221 }
222
223
224 //_____________________________________________________________________________
225 AliCDBStorage* AliCDBStorage::Instance()
226 {
227 // return the current instance of the DB (AliCDBDump, AliCDBLocalFile...)
228 // Example of usage: after creating an istance of AliCDBStorage:
229 // AliCDBStorage::Instance()->Get(...)
230
231   return fgInstance;
232 }
233
234
235 //_____________________________________________________________________________
236 const AliCDBMetaData& AliCDBStorage::GetCDBMetaData(const char* name)
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
242 AliCDBEntry *entry = (AliCDBEntry*) fEntries.FindObject(name);
243  if(!entry){
244     AliError(Form("Entry %s not found! You make me crash!",name));
245  }
246  return entry->GetCDBMetaData(); 
247
248 }
249