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 // base class for data base access classes //
22 ///////////////////////////////////////////////////////////////////////////////
30 #include "AliMetaData.h"
31 #include "AliRunData.h"
32 #include "AliRunDataStorage.h"
35 ClassImp(AliRunDataStorage)
38 AliRunDataStorage* AliRunDataStorage::fgInstance = NULL;
41 //_____________________________________________________________________________
42 AliRunDataStorage::AliRunDataStorage() :
48 // default constructor
50 if (fgInstance) delete fgInstance;
54 //_____________________________________________________________________________
55 AliRunDataStorage::~AliRunDataStorage()
68 //_____________________________________________________________________________
69 AliRunDataStorage::AliRunDataStorage(const AliRunDataStorage& db) :
77 AliFatal("not implemented");
80 //_____________________________________________________________________________
81 AliRunDataStorage& AliRunDataStorage::operator = (const AliRunDataStorage& /*db*/)
83 // assignment operator
85 AliFatal("not implemented");
90 //_____________________________________________________________________________
91 const TObject* AliRunDataStorage::Get(const char* name, Int_t runNumber)
93 // get an object from the data base
94 // (AliRunDataStorage is the owner of the returned object)
96 AliMetaData defaultMetaData;
97 AliMetaData* selectedMetaData = &defaultMetaData;
99 // look for a meta data selection
100 for (Int_t i = 0; i < fSelection.GetEntriesFast(); i++) {
101 AliMetaData* selection = (AliMetaData*) fSelection[i];
102 if (!selection) continue;
103 if (selection->Matches(name, runNumber)) {
104 selectedMetaData = selection;
109 AliMetaData metaData(*selectedMetaData);
110 metaData.SetName(name);
111 AliRunData* entry = GetEntry(metaData, runNumber);
113 AliDebug(2, "got the entry:");
114 ToAliDebug(2, entry->Dump());
116 AliDebug(2, Form("got no entry for %s", name));
119 // update array of current entries
120 if (!entry) return NULL;
121 TObject* oldEntry = fEntries.FindObject(entry->GetName());
123 delete fEntries.Remove(oldEntry);
127 // record entry to a file
129 Bool_t isAlreadyRecorded = kFALSE;
130 TDirectory* dir = gDirectory;
132 TKey* key = fRecordFile->GetKey(entry->GetName());
134 Int_t nCycles = key->GetCycle();
135 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
136 key = fRecordFile->GetKey(entry->GetName(), iCycle);
138 AliRunData* recEntry = (AliRunData*) key->ReadObj();
139 if (!recEntry) continue;
140 if (recEntry->InheritsFrom(AliRunData::Class()) &&
141 (recEntry->GetMetaData() == entry->GetMetaData())) {
142 isAlreadyRecorded = kTRUE;
145 if (isAlreadyRecorded) break;
148 if (!isAlreadyRecorded) {
149 if (entry->Write() == 0) {
150 AliError(Form("could not record entry %s", entry->GetName()));
153 if (dir) dir->cd(); else gROOT->cd();
156 return entry->GetObject();
160 //_____________________________________________________________________________
161 Bool_t AliRunDataStorage::Put(const TObject* object,
162 const AliMetaData& metaData)
164 // put an object into the data base
165 // (AliRunDataStorage does not adopt the object)
167 if (!object) return kFALSE;
168 AliRunData entry(object->Clone(), metaData);
169 return PutEntry(&entry);
172 //_____________________________________________________________________________
173 Bool_t AliRunDataStorage::PutEntry(AliRunData* entry)
175 // put an object into the data base
177 if (!entry) return kFALSE;
178 AliError(Form("This is a read only data base. "
179 "The object %s was not inserted", entry->GetName()));
184 //_____________________________________________________________________________
185 void AliRunDataStorage::Select(const AliMetaData& metaData)
187 // add some meta data selection criteria
189 fSelection.Add(new AliMetaData(metaData));
193 //_____________________________________________________________________________
194 Bool_t AliRunDataStorage::RecordToFile(const char* fileName)
196 // record entries retrieved from the data base to a file with the given name
199 fRecordFile->Close();
203 TDirectory* dir = gDirectory;
204 fRecordFile = TFile::Open(fileName, "UPDATE");
205 if (dir) dir->cd(); else gROOT->cd();
206 if (!fRecordFile || !fRecordFile->IsOpen()) {
207 AliError(Form("could not open file %s", fileName));
216 //_____________________________________________________________________________
217 AliRunDataStorage* AliRunDataStorage::Instance()
219 // return the current instance of the DB