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 "AliSelectionMetaData.h"
31 #include "AliObjectMetaData.h"
32 #include "AliRunData.h"
33 #include "AliRunDataStorage.h"
36 ClassImp(AliRunDataStorage)
39 AliRunDataStorage* AliRunDataStorage::fgInstance = NULL;
42 //_____________________________________________________________________________
43 AliRunDataStorage::AliRunDataStorage() :
49 // 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 NOT the owner of the returned object)
95 // name must be in the form "Detector/DBType/DetSpecType"
96 // es: "ZDC/Calib/Pedestals"
98 AliSelectionMetaData defaultMetaData;
99 AliSelectionMetaData* selectedMetaData = &defaultMetaData;
101 // look for a meta data selection
102 for (Int_t i = 0; i < fSelection.GetEntriesFast(); i++) {
103 AliSelectionMetaData* selection = (AliSelectionMetaData*) fSelection[i];
104 if (!selection) continue;
105 if (selection->Matches(name, runNumber)) {
106 selectedMetaData = selection;
111 AliSelectionMetaData selMetaData(*selectedMetaData);
112 selMetaData.SetName(name);
113 AliRunData* entry = GetEntry(selMetaData, runNumber);
115 AliDebug(2, "got the entry:");
116 ToAliDebug(2, entry->Dump());
118 AliDebug(2, Form("got no entry for %s", name));
121 // update array of current entries
122 if (!entry) return NULL;
123 TObject* oldEntry = fEntries.FindObject(entry->GetName());
125 delete fEntries.Remove(oldEntry);
129 // record entry to a file (in the same way as AliRunDataFile::PutEntry,
130 // so that the file can be opened as a AliRunDataFile!)
134 TDirectory* saveDir = gDirectory;
136 // go to or create the directory
137 TString strname(name);
138 while (strname.BeginsWith("/")) strname.Remove(0);
139 TDirectory* dir = fRecordFile;
141 while ((index = strname.Index("/")) >= 0) {
142 TString dirName(strname(0, index));
143 if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
146 strname.Remove(0, index+1);
149 entry->Write(strname);
150 if (saveDir) saveDir->cd(); else gROOT->cd();
154 return (entry->GetObject())->Clone();
159 //_____________________________________________________________________________
160 Bool_t AliRunDataStorage::Put(const TObject* object,
161 const AliObjectMetaData& objMetaData)
163 // put an object into the data base
164 // (AliRunDataStorage does not adopt the object)
165 // location of where the object is stored is defined by
166 // the AliObjectMetaData's name ("Detector/DBType/DetSpecType")
167 // and run Range. Storage is handled by the PutEntry method
168 // of the current AliRunDataStorage instance.
170 if (!object) return kFALSE;
171 AliRunData entry(object->Clone(), objMetaData);
172 return PutEntry(&entry);
175 //_____________________________________________________________________________
176 Bool_t AliRunDataStorage::PutEntry(AliRunData* entry)
178 // put an object into the data base
179 // Refer to the specific method of the current AliRunDataStorage instance
180 // (AliRunDataFile, AliRunDataOrganizedFile, AliRunDataAlien)
182 if (!entry) return kFALSE;
183 AliError(Form("This is a read only data base. "
184 "The object %s was not inserted", entry->GetName()));
189 //_____________________________________________________________________________
190 void AliRunDataStorage::Select(const AliSelectionMetaData& selMetaData)
192 // add some meta data selection criteria
194 fSelection.Add(new AliSelectionMetaData(selMetaData));
198 //_____________________________________________________________________________
199 Bool_t AliRunDataStorage::RecordToFile(const char* fileName)
201 // record entries retrieved from the data base to a file with the given name
204 fRecordFile->Close();
208 TDirectory* dir = gDirectory;
209 fRecordFile = TFile::Open(fileName, "UPDATE");
210 if (dir) dir->cd(); else gROOT->cd();
211 if (!fRecordFile || !fRecordFile->IsOpen()) {
212 AliError(Form("could not open file %s", fileName));
221 //_____________________________________________________________________________
222 AliRunDataStorage* AliRunDataStorage::Instance()
224 // return the current instance of the DB (AliRunDataFile, AliRunOrganizedDataFile...)
225 // Example of usage: after creating an istance of AliRunDataStorage:
226 // AliRunDataStorage::Instance()->Get(...)
232 //_____________________________________________________________________________
233 const AliObjectMetaData& AliRunDataStorage::GetObjectMetaData(const char* name)
235 // Returns the object's metadata of the already retrieved object
236 // (useful, for example, if you want to know the format of the object you have
239 AliRunData *entry = (AliRunData*) fEntries.FindObject(name);
241 AliError(Form("Entry %s not found! You make me crash!",name));
243 return entry->GetObjectMetaData();