first prototype of interface to storage of run dependent objects, implementation...
[u/mrichter/AliRoot.git] / STEER / AliRunDataStorage.cxx
CommitLineData
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// //
20// base class for data base access classes //
21// //
22///////////////////////////////////////////////////////////////////////////////
23
24
25#include <TFile.h>
26#include <TKey.h>
27#include <TROOT.h>
28
29#include "AliLog.h"
30#include "AliMetaData.h"
31#include "AliRunData.h"
32#include "AliRunDataStorage.h"
33
34
35ClassImp(AliRunDataStorage)
36
37
38AliRunDataStorage* AliRunDataStorage::fgInstance = NULL;
39
40
41//_____________________________________________________________________________
42AliRunDataStorage::AliRunDataStorage() :
43 TObject(),
44 fSelection(),
45 fEntries(),
46 fRecordFile(NULL)
47{
48// default constructor
49
50 if (fgInstance) delete fgInstance;
51 fgInstance = this;
52}
53
54//_____________________________________________________________________________
55AliRunDataStorage::~AliRunDataStorage()
56{
57// destructor
58
59 fSelection.Delete();
60 fEntries.Delete();
61 if (fRecordFile) {
62 fRecordFile->Close();
63 delete fRecordFile;
64 }
65 fgInstance = NULL;
66}
67
68//_____________________________________________________________________________
69AliRunDataStorage::AliRunDataStorage(const AliRunDataStorage& db) :
70 TObject(db),
71 fSelection(),
72 fEntries(),
73 fRecordFile(NULL)
74{
75// copy constructor
76
77 AliFatal("not implemented");
78}
79
80//_____________________________________________________________________________
81AliRunDataStorage& AliRunDataStorage::operator = (const AliRunDataStorage& /*db*/)
82{
83// assignment operator
84
85 AliFatal("not implemented");
86 return *this;
87}
88
89
90//_____________________________________________________________________________
91const TObject* AliRunDataStorage::Get(const char* name, Int_t runNumber)
92{
93// get an object from the data base
94// (AliRunDataStorage is the owner of the returned object)
95
96 AliMetaData defaultMetaData;
97 AliMetaData* selectedMetaData = &defaultMetaData;
98
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;
105 }
106 }
107
108 // get the entry
109 AliMetaData metaData(*selectedMetaData);
110 metaData.SetName(name);
111 AliRunData* entry = GetEntry(metaData, runNumber);
112 if (entry) {
113 AliDebug(2, "got the entry:");
114 ToAliDebug(2, entry->Dump());
115 } else {
116 AliDebug(2, Form("got no entry for %s", name));
117 }
118
119 // update array of current entries
120 if (!entry) return NULL;
121 TObject* oldEntry = fEntries.FindObject(entry->GetName());
122 if (oldEntry) {
123 delete fEntries.Remove(oldEntry);
124 }
125 fEntries.Add(entry);
126
127 // record entry to a file
128 if (fRecordFile) {
129 Bool_t isAlreadyRecorded = kFALSE;
130 TDirectory* dir = gDirectory;
131 fRecordFile->cd();
132 TKey* key = fRecordFile->GetKey(entry->GetName());
133 if (key) {
134 Int_t nCycles = key->GetCycle();
135 for (Int_t iCycle = nCycles; iCycle > 0; iCycle--) {
136 key = fRecordFile->GetKey(entry->GetName(), iCycle);
137 if (!key) continue;
138 AliRunData* recEntry = (AliRunData*) key->ReadObj();
139 if (!recEntry) continue;
140 if (recEntry->InheritsFrom(AliRunData::Class()) &&
141 (recEntry->GetMetaData() == entry->GetMetaData())) {
142 isAlreadyRecorded = kTRUE;
143 }
144 delete recEntry;
145 if (isAlreadyRecorded) break;
146 }
147 }
148 if (!isAlreadyRecorded) {
149 if (entry->Write() == 0) {
150 AliError(Form("could not record entry %s", entry->GetName()));
151 }
152 }
153 if (dir) dir->cd(); else gROOT->cd();
154 }
155
156 return entry->GetObject();
157}
158
159
160//_____________________________________________________________________________
161Bool_t AliRunDataStorage::Put(const TObject* object,
162 const AliMetaData& metaData)
163{
164// put an object into the data base
165// (AliRunDataStorage does not adopt the object)
166
167 if (!object) return kFALSE;
168 AliRunData entry(object->Clone(), metaData);
169 return PutEntry(&entry);
170}
171
172//_____________________________________________________________________________
173Bool_t AliRunDataStorage::PutEntry(AliRunData* entry)
174{
175// put an object into the data base
176
177 if (!entry) return kFALSE;
178 AliError(Form("This is a read only data base. "
179 "The object %s was not inserted", entry->GetName()));
180 return kFALSE;
181}
182
183
184//_____________________________________________________________________________
185void AliRunDataStorage::Select(const AliMetaData& metaData)
186{
187// add some meta data selection criteria
188
189 fSelection.Add(new AliMetaData(metaData));
190}
191
192
193//_____________________________________________________________________________
194Bool_t AliRunDataStorage::RecordToFile(const char* fileName)
195{
196// record entries retrieved from the data base to a file with the given name
197
198 if (fRecordFile) {
199 fRecordFile->Close();
200 delete fRecordFile;
201 }
202
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));
208 delete fRecordFile;
209 fRecordFile = NULL;
210 return kFALSE;
211 }
212 return kTRUE;
213}
214
215
216//_____________________________________________________________________________
217AliRunDataStorage* AliRunDataStorage::Instance()
218{
219// return the current instance of the DB
220
221 return fgInstance;
222}