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 | // // |
f05209ee |
20 | // access classes for a data base in a LOCAL file // |
2c8628dd |
21 | // // |
22 | /////////////////////////////////////////////////////////////////////////////// |
23 | |
24 | |
25 | #include <TFile.h> |
26 | #include <TKey.h> |
27 | #include <TROOT.h> |
28 | #include "AliLog.h" |
fe913d8f |
29 | #include "AliCDBEntry.h" |
30 | #include "AliCDBMetaData.h" |
31 | #include "AliCDBDump.h" |
2c8628dd |
32 | |
33 | |
fe913d8f |
34 | ClassImp(AliCDBDump) |
2c8628dd |
35 | |
36 | |
37 | //_____________________________________________________________________________ |
fe913d8f |
38 | AliCDBDump::AliCDBDump(const char* fileName, Bool_t readOnly) : |
39 | AliCDBStorage(), |
2c8628dd |
40 | fFile(NULL) |
41 | { |
42 | // constructor |
43 | |
44 | if (!fileName) { |
45 | AliError("no file name given"); |
46 | return; |
47 | } |
48 | TDirectory* saveDir = gDirectory; |
49 | fFile = TFile::Open(fileName, ((readOnly) ? "READ" : "UPDATE")); |
50 | if (saveDir) saveDir->cd(); else gROOT->cd(); |
51 | if (!fFile || !fFile->IsOpen()) { |
52 | AliError(Form("could not open file %s", fileName)); |
53 | fFile = NULL; |
54 | } |
55 | } |
56 | |
57 | //_____________________________________________________________________________ |
fe913d8f |
58 | AliCDBDump::~AliCDBDump() |
2c8628dd |
59 | { |
60 | // destructor |
61 | |
62 | if (fFile) { |
63 | fFile->Close(); |
64 | delete fFile; |
65 | } |
66 | } |
67 | |
68 | //_____________________________________________________________________________ |
fe913d8f |
69 | AliCDBDump::AliCDBDump(const AliCDBDump& /*db*/) : |
70 | AliCDBStorage(), |
2c8628dd |
71 | fFile(NULL) |
72 | { |
73 | // copy constructor |
74 | |
75 | AliFatal("not implemented"); |
76 | } |
77 | |
78 | //_____________________________________________________________________________ |
fe913d8f |
79 | AliCDBDump& AliCDBDump::operator = (const AliCDBDump& /*db*/) |
2c8628dd |
80 | { |
81 | // assignment operator |
82 | |
83 | AliFatal("not implemented"); |
84 | return *this; |
85 | } |
86 | |
87 | |
88 | //_____________________________________________________________________________ |
fe913d8f |
89 | AliCDBEntry* AliCDBDump::GetEntry(AliCDBMetaDataSelect& selMetaData, Int_t runNumber) |
2c8628dd |
90 | { |
91 | // get an object from the data base |
92 | |
93 | // go to the directory |
94 | TDirectory* saveDir = gDirectory; |
f05209ee |
95 | TDirectory *dir = fFile; |
96 | TString name(selMetaData.GetName()); |
2c8628dd |
97 | Int_t last = name.Last('/'); |
98 | if (last < 0) { |
99 | fFile->cd(); |
100 | } else { |
101 | TString dirName(name(0, last)); |
f05209ee |
102 | if (!dir->cd(dirName)) { |
103 | AliError(Form("no directory %s found", dirName.Data())); |
2c8628dd |
104 | if (saveDir) saveDir->cd(); else gROOT->cd(); |
105 | return NULL; |
106 | } |
107 | name.Remove(0, last+1); |
108 | } |
109 | |
f05209ee |
110 | dir = gDirectory; |
111 | TKey* key = dir->GetKey(name); |
2c8628dd |
112 | if (!key) { |
f05209ee |
113 | AliError(Form("no object with name %s found", selMetaData.GetName())); |
2c8628dd |
114 | if (saveDir) saveDir->cd(); else gROOT->cd(); |
115 | return NULL; |
116 | } |
117 | Int_t nCycles = key->GetCycle(); |
118 | |
119 | // find the closest entry |
fe913d8f |
120 | AliCDBEntry* closestEntry = NULL; |
2c8628dd |
121 | for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) { |
f05209ee |
122 | key = dir->GetKey(name, iCycle); |
123 | |
2c8628dd |
124 | if (!key) continue; |
fe913d8f |
125 | AliCDBEntry* entry = (AliCDBEntry*) key->ReadObj(); |
2c8628dd |
126 | if (!entry) continue; |
fe913d8f |
127 | if (!entry->InheritsFrom(AliCDBEntry::Class())) { |
128 | AliCDBMetaData metaData; |
129 | entry = new AliCDBEntry(entry, metaData); |
2c8628dd |
130 | } |
fe913d8f |
131 | if (!entry->GetCDBMetaData().IsStrictlyValid(runNumber, &selMetaData) || |
2c8628dd |
132 | (entry->Compare(closestEntry) <= 0)) { |
133 | delete entry; |
134 | continue; |
135 | } |
136 | delete closestEntry; |
137 | closestEntry = entry; |
138 | } |
139 | if (saveDir) saveDir->cd(); else gROOT->cd(); |
f05209ee |
140 | if(!closestEntry) AliError(Form("No valid entry found for: name %s, version %d, run %d!!!", |
141 | selMetaData.GetName(),selMetaData.GetVersion(),runNumber)); |
2c8628dd |
142 | if (!closestEntry) return NULL; |
fe913d8f |
143 | |
144 | // if(selMetaData.GetVersion() > -1 && (closestEntry->GetCDBMetaData()).GetVersion() != selMetaData.GetVersion()) |
145 | // AliWarning(Form("Warning: selected version (%d) not found, got version %d instead", |
146 | // selMetaData.GetVersion(),(closestEntry->GetCDBMetaData()).GetVersion())); |
147 | |
2c8628dd |
148 | return closestEntry; |
149 | } |
150 | |
151 | //_____________________________________________________________________________ |
fe913d8f |
152 | Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry) |
2c8628dd |
153 | { |
f05209ee |
154 | // puts an object into the database |
155 | |
fe913d8f |
156 | // AliCDBEntry entry is composed by the object and its MetaData |
f05209ee |
157 | // this method takes the metaData, reads the name, runRange and Version |
158 | // creates the TDirectory structure into the file |
159 | // looks for runs with same name, if exist increment version |
160 | // (therefore version should not be put in the metadata) |
161 | // Note: the key name of the entry is "DetSpecType" |
162 | // return result |
2c8628dd |
163 | |
164 | if (!entry || !fFile) return kFALSE; |
165 | if (!fFile->IsWritable()) { |
166 | AliError(Form("The data base file was opened in read only mode. " |
167 | "The object %s was not inserted", entry->GetName())); |
168 | return kFALSE; |
169 | } |
f05209ee |
170 | |
171 | fFile->cd(); |
2c8628dd |
172 | TDirectory* saveDir = gDirectory; |
173 | |
174 | // go to or create the directory |
175 | TString name(entry->GetName()); |
176 | while (name.BeginsWith("/")) name.Remove(0); |
177 | TDirectory* dir = fFile; |
178 | Int_t index = -1; |
179 | while ((index = name.Index("/")) >= 0) { |
180 | TString dirName(name(0, index)); |
181 | if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName); |
182 | dir->cd(dirName); |
183 | dir = gDirectory; |
184 | name.Remove(0, index+1); |
185 | } |
186 | |
187 | // determine the version number |
188 | Int_t version = 0; |
f05209ee |
189 | TKey* key = dir->GetKey(name); |
2c8628dd |
190 | if (key) { |
191 | Int_t nCycles = key->GetCycle(); |
192 | for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) { |
f05209ee |
193 | key = dir->GetKey(name, iCycle); |
2c8628dd |
194 | if (!key) continue; |
fe913d8f |
195 | AliCDBEntry* oldEntry = (AliCDBEntry*) key->ReadObj(); |
2c8628dd |
196 | if (!oldEntry) continue; |
fe913d8f |
197 | if (oldEntry->InheritsFrom(AliCDBEntry::Class())) { |
198 | if (version <= oldEntry->GetCDBMetaData().GetVersion()) { |
199 | version = oldEntry->GetCDBMetaData().GetVersion()+1; |
2c8628dd |
200 | } |
201 | } |
202 | delete oldEntry; |
203 | } |
204 | } |
205 | entry->SetVersion(version); |
206 | |
207 | Bool_t result = (entry->Write(name) != 0); |
208 | if (saveDir) saveDir->cd(); else gROOT->cd(); |
fe913d8f |
209 | |
210 | if(result) { |
211 | AliInfo(Form("Run object %s",entry->GetName())); |
212 | AliInfo(Form("was successfully written into file %s",fFile->GetName())); |
213 | } |
214 | |
2c8628dd |
215 | return result; |
216 | } |
fe913d8f |
217 | |
218 | //_____________________________________________________________________________ |
219 | void AliCDBDump::TagForProduction |
220 | (const AliCDBMetaDataSelect& /* selMetaData */, UInt_t /* prodVers */){ |
221 | |
222 | AliError(Form("Not implemented in this case")); |
223 | } |