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 "AliRunData.h"
30 #include "AliSelectionMetaData.h"
31 #include "AliObjectMetaData.h"
32 #include "AliRunDataFile.h"
35 ClassImp(AliRunDataFile)
38 //_____________________________________________________________________________
39 AliRunDataFile::AliRunDataFile(const char* fileName, Bool_t readOnly) :
46 AliError("no file name given");
49 TDirectory* saveDir = gDirectory;
50 fFile = TFile::Open(fileName, ((readOnly) ? "READ" : "UPDATE"));
51 if (saveDir) saveDir->cd(); else gROOT->cd();
52 if (!fFile || !fFile->IsOpen()) {
53 AliError(Form("could not open file %s", fileName));
58 //_____________________________________________________________________________
59 AliRunDataFile::~AliRunDataFile()
69 //_____________________________________________________________________________
70 AliRunDataFile::AliRunDataFile(const AliRunDataFile& /*db*/) :
76 AliFatal("not implemented");
79 //_____________________________________________________________________________
80 AliRunDataFile& AliRunDataFile::operator = (const AliRunDataFile& /*db*/)
82 // assignment operator
84 AliFatal("not implemented");
89 //_____________________________________________________________________________
90 AliRunData* AliRunDataFile::GetEntry(AliSelectionMetaData& selMetaData, Int_t runNumber)
92 // get an object from the data base
94 // go to the directory
95 TDirectory* saveDir = gDirectory;
96 TDirectory *dir = fFile;
97 TString name(selMetaData.GetName());
98 Int_t last = name.Last('/');
102 TString dirName(name(0, last));
103 if (!dir->cd(dirName)) {
104 AliError(Form("no directory %s found", dirName.Data()));
105 if (saveDir) saveDir->cd(); else gROOT->cd();
108 name.Remove(0, last+1);
112 TKey* key = dir->GetKey(name);
114 AliError(Form("no object with name %s found", selMetaData.GetName()));
115 if (saveDir) saveDir->cd(); else gROOT->cd();
118 Int_t nCycles = key->GetCycle();
120 // find the closest entry
121 AliRunData* closestEntry = NULL;
122 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
123 key = dir->GetKey(name, iCycle);
126 AliRunData* entry = (AliRunData*) key->ReadObj();
127 if (!entry) continue;
128 if (!entry->InheritsFrom(AliRunData::Class())) {
129 AliObjectMetaData objMetaData;
130 entry = new AliRunData(entry, objMetaData);
132 if (!entry->GetObjectMetaData().IsValid(runNumber, &selMetaData) ||
133 (entry->Compare(closestEntry) <= 0)) {
138 closestEntry = entry;
140 if (saveDir) saveDir->cd(); else gROOT->cd();
141 if(!closestEntry) AliError(Form("No valid entry found for: name %s, version %d, run %d!!!",
142 selMetaData.GetName(),selMetaData.GetVersion(),runNumber));
143 if (!closestEntry) return NULL;
144 if(selMetaData.GetVersion() > -1 && (closestEntry->GetObjectMetaData()).GetVersion() != selMetaData.GetVersion())
145 AliWarning(Form("Warning: selected version (%d) not found, got version %d instead",
146 selMetaData.GetVersion(),(closestEntry->GetObjectMetaData()).GetVersion()));
150 //_____________________________________________________________________________
151 Bool_t AliRunDataFile::PutEntry(AliRunData* entry)
153 // puts an object into the database
155 // AliRunData entry is composed by the object and its MetaData
156 // this method takes the metaData, reads the name, runRange and Version
157 // creates the TDirectory structure into the file
158 // looks for runs with same name, if exist increment version
159 // (therefore version should not be put in the metadata)
160 // Note: the key name of the entry is "DetSpecType"
163 if (!entry || !fFile) return kFALSE;
164 if (!fFile->IsWritable()) {
165 AliError(Form("The data base file was opened in read only mode. "
166 "The object %s was not inserted", entry->GetName()));
171 TDirectory* saveDir = gDirectory;
173 // go to or create the directory
174 TString name(entry->GetName());
175 while (name.BeginsWith("/")) name.Remove(0);
176 TDirectory* dir = fFile;
178 while ((index = name.Index("/")) >= 0) {
179 TString dirName(name(0, index));
180 if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
183 name.Remove(0, index+1);
186 // determine the version number
188 TKey* key = dir->GetKey(name);
190 Int_t nCycles = key->GetCycle();
191 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
192 key = dir->GetKey(name, iCycle);
194 AliRunData* oldEntry = (AliRunData*) key->ReadObj();
195 if (!oldEntry) continue;
196 if (oldEntry->InheritsFrom(AliRunData::Class())) {
197 if (version <= oldEntry->GetObjectMetaData().GetVersion()) {
198 version = oldEntry->GetObjectMetaData().GetVersion()+1;
204 entry->SetVersion(version);
206 Bool_t result = (entry->Write(name) != 0);
207 if (saveDir) saveDir->cd(); else gROOT->cd();