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 // access classes for a data base in a LOCAL file //
22 ///////////////////////////////////////////////////////////////////////////////
29 #include "AliCDBEntry.h"
30 #include "AliCDBMetaData.h"
31 #include "AliCDBDump.h"
37 //_____________________________________________________________________________
38 AliCDBDump::AliCDBDump(const char* fileName, Bool_t readOnly) :
45 AliError("no file name given");
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));
57 //_____________________________________________________________________________
58 AliCDBDump::~AliCDBDump()
68 //_____________________________________________________________________________
69 AliCDBDump::AliCDBDump(const AliCDBDump& /*db*/) :
75 AliFatal("not implemented");
78 //_____________________________________________________________________________
79 AliCDBDump& AliCDBDump::operator = (const AliCDBDump& /*db*/)
81 // assignment operator
83 AliFatal("not implemented");
88 //_____________________________________________________________________________
89 AliCDBEntry* AliCDBDump::GetEntry(AliCDBMetaDataSelect& selMetaData, Int_t runNumber)
91 // get an object from the data base
93 // go to the directory
94 TDirectory* saveDir = gDirectory;
95 TDirectory *dir = fFile;
96 TString name(selMetaData.GetName());
97 Int_t last = name.Last('/');
101 TString dirName(name(0, last));
102 if (!dir->cd(dirName)) {
103 AliError(Form("no directory %s found", dirName.Data()));
104 if (saveDir) saveDir->cd(); else gROOT->cd();
107 name.Remove(0, last+1);
111 TKey* key = dir->GetKey(name);
113 AliError(Form("no object with name %s found", selMetaData.GetName()));
114 if (saveDir) saveDir->cd(); else gROOT->cd();
117 Int_t nCycles = key->GetCycle();
119 // find the closest entry
120 AliCDBEntry* closestEntry = NULL;
121 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
122 key = dir->GetKey(name, iCycle);
125 AliCDBEntry* entry = (AliCDBEntry*) key->ReadObj();
126 if (!entry) continue;
127 if (!entry->InheritsFrom(AliCDBEntry::Class())) {
128 AliCDBMetaData metaData;
129 entry = new AliCDBEntry(entry, metaData);
131 if (!entry->GetCDBMetaData().IsStrictlyValid(runNumber, &selMetaData) ||
132 (entry->Compare(closestEntry) <= 0)) {
137 closestEntry = entry;
139 if (saveDir) saveDir->cd(); else gROOT->cd();
140 if(!closestEntry) AliError(Form("No valid entry found for: name %s, version %d, run %d!!!",
141 selMetaData.GetName(),selMetaData.GetVersion(),runNumber));
142 if (!closestEntry) return NULL;
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()));
151 //_____________________________________________________________________________
152 Bool_t AliCDBDump::PutEntry(AliCDBEntry* entry)
154 // puts an object into the database
156 // AliCDBEntry entry is composed by the object and its MetaData
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"
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()));
172 TDirectory* saveDir = gDirectory;
174 // go to or create the directory
175 TString name(entry->GetName());
176 while (name.BeginsWith("/")) name.Remove(0);
177 TDirectory* dir = fFile;
179 while ((index = name.Index("/")) >= 0) {
180 TString dirName(name(0, index));
181 if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
184 name.Remove(0, index+1);
187 // determine the version number
189 TKey* key = dir->GetKey(name);
191 Int_t nCycles = key->GetCycle();
192 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
193 key = dir->GetKey(name, iCycle);
195 AliCDBEntry* oldEntry = (AliCDBEntry*) key->ReadObj();
196 if (!oldEntry) continue;
197 if (oldEntry->InheritsFrom(AliCDBEntry::Class())) {
198 if (version <= oldEntry->GetCDBMetaData().GetVersion()) {
199 version = oldEntry->GetCDBMetaData().GetVersion()+1;
205 entry->SetVersion(version);
207 Bool_t result = (entry->Write(name) != 0);
208 if (saveDir) saveDir->cd(); else gROOT->cd();
211 AliInfo(Form("Run object %s",entry->GetName()));
212 AliInfo(Form("was successfully written into file %s",fFile->GetName()));
218 //_____________________________________________________________________________
219 void AliCDBDump::TagForProduction
220 (const AliCDBMetaDataSelect& /* selMetaData */, UInt_t /* prodVers */){
222 AliError(Form("Not implemented in this case"));