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 "AliRunDataFile.h"
33 ClassImp(AliRunDataFile)
36 //_____________________________________________________________________________
37 AliRunDataFile::AliRunDataFile(const char* fileName, Bool_t readOnly) :
44 AliError("no file name given");
47 TDirectory* saveDir = gDirectory;
48 fFile = TFile::Open(fileName, ((readOnly) ? "READ" : "UPDATE"));
49 if (saveDir) saveDir->cd(); else gROOT->cd();
50 if (!fFile || !fFile->IsOpen()) {
51 AliError(Form("could not open file %s", fileName));
56 //_____________________________________________________________________________
57 AliRunDataFile::~AliRunDataFile()
67 //_____________________________________________________________________________
68 AliRunDataFile::AliRunDataFile(const AliRunDataFile& /*db*/) :
74 AliFatal("not implemented");
77 //_____________________________________________________________________________
78 AliRunDataFile& AliRunDataFile::operator = (const AliRunDataFile& /*db*/)
80 // assignment operator
82 AliFatal("not implemented");
87 //_____________________________________________________________________________
88 AliRunData* AliRunDataFile::GetEntry(AliMetaData& metaData, Int_t runNumber)
90 // get an object from the data base
92 // go to the directory
93 TDirectory* saveDir = gDirectory;
94 TString name(metaData.GetName());
95 Int_t last = name.Last('/');
99 TString dirName(name(0, last));
100 if (!fFile->cd(dirName)) {
101 AliDebug(1, Form("no directory %s found", dirName.Data()));
102 if (saveDir) saveDir->cd(); else gROOT->cd();
105 name.Remove(0, last+1);
108 TKey* key = fFile->GetKey(name);
110 AliDebug(1, Form("no object with name %s found", metaData.GetName()));
111 if (saveDir) saveDir->cd(); else gROOT->cd();
114 Int_t nCycles = key->GetCycle();
116 // find the closest entry
117 AliRunData* closestEntry = NULL;
118 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
119 key = fFile->GetKey(name, iCycle);
121 AliRunData* entry = (AliRunData*) key->ReadObj();
122 if (!entry) continue;
123 if (!entry->InheritsFrom(AliRunData::Class())) {
124 AliMetaData metaData;
125 entry = new AliRunData(entry, metaData);
127 if (!entry->GetMetaData().IsValid(runNumber, &metaData) ||
128 (entry->Compare(closestEntry) <= 0)) {
133 closestEntry = entry;
135 if (saveDir) saveDir->cd(); else gROOT->cd();
136 if (!closestEntry) return NULL;
140 //_____________________________________________________________________________
141 Bool_t AliRunDataFile::PutEntry(AliRunData* entry)
143 // put an object into the data base
145 if (!entry || !fFile) return kFALSE;
146 if (!fFile->IsWritable()) {
147 AliError(Form("The data base file was opened in read only mode. "
148 "The object %s was not inserted", entry->GetName()));
151 TDirectory* saveDir = gDirectory;
153 // go to or create the directory
154 TString name(entry->GetName());
155 while (name.BeginsWith("/")) name.Remove(0);
156 TDirectory* dir = fFile;
158 while ((index = name.Index("/")) >= 0) {
159 TString dirName(name(0, index));
160 if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
163 name.Remove(0, index+1);
166 // determine the version number
168 TKey* key = fFile->GetKey(name);
170 Int_t nCycles = key->GetCycle();
171 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
172 key = fFile->GetKey(entry->GetName(), iCycle);
174 AliRunData* oldEntry = (AliRunData*) key->ReadObj();
175 if (!oldEntry) continue;
176 if (oldEntry->InheritsFrom(AliRunData::Class())) {
177 if (version <= oldEntry->GetMetaData().GetVersion()) {
178 version = oldEntry->GetMetaData().GetVersion()+1;
184 entry->SetVersion(version);
186 Bool_t result = (entry->Write(name) != 0);
187 if (saveDir) saveDir->cd(); else gROOT->cd();