]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliRunDataStorage.cxx
3fd1166b891828f05204c5fdab4648f098a835c5
[u/mrichter/AliRoot.git] / STEER / AliRunDataStorage.cxx
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 "AliSelectionMetaData.h"
31 #include "AliObjectMetaData.h"
32 #include "AliRunData.h"
33 #include "AliRunDataStorage.h"
34
35
36 ClassImp(AliRunDataStorage)
37
38
39 AliRunDataStorage* AliRunDataStorage::fgInstance = NULL;
40
41
42 //_____________________________________________________________________________
43 AliRunDataStorage::AliRunDataStorage() :
44   TObject(),
45   fSelection(),
46   fEntries(),
47   fRecordFile(NULL)
48 {
49 // default constructor
50   if (fgInstance) delete fgInstance;
51   fgInstance = this;
52 }
53
54 //_____________________________________________________________________________
55 AliRunDataStorage::~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 //_____________________________________________________________________________
69 AliRunDataStorage::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 //_____________________________________________________________________________
81 AliRunDataStorage& AliRunDataStorage::operator = (const AliRunDataStorage& /*db*/)
82 {
83 // assignment operator
84
85   AliFatal("not implemented");
86   return *this;
87 }
88
89
90 //_____________________________________________________________________________
91 const TObject* AliRunDataStorage::Get(const char* name, Int_t runNumber)
92 {
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"
97
98   AliSelectionMetaData defaultMetaData;
99   AliSelectionMetaData* selectedMetaData = &defaultMetaData;
100
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;
107     }
108   }
109
110   // get the entry
111   AliSelectionMetaData selMetaData(*selectedMetaData);
112   selMetaData.SetName(name);
113   AliRunData* entry = GetEntry(selMetaData, runNumber);
114   if (entry) {
115     AliDebug(2, "got the entry:");
116     ToAliDebug(2, entry->Dump());
117   } else {
118     AliDebug(2, Form("got no entry for %s", name));
119   }
120
121   // update array of current entries
122   if (!entry) return NULL;  
123   TObject* oldEntry = fEntries.FindObject(entry->GetName());
124   if (oldEntry) {
125     delete fEntries.Remove(oldEntry);
126   }
127   fEntries.Add(entry);
128
129   // record entry to a file (in the same way as AliRunDataFile::PutEntry, 
130   // so that the file can be opened as a AliRunDataFile!)
131
132   if (fRecordFile) {
133     fRecordFile->cd();
134     TDirectory* saveDir = gDirectory;
135
136     // go to or create the directory
137     TString strname(name);
138     while (strname.BeginsWith("/")) strname.Remove(0);
139     TDirectory* dir = fRecordFile;
140     Int_t index = -1;
141     while ((index = strname.Index("/")) >= 0) {
142       TString dirName(strname(0, index));
143       if ((index > 0) && !dir->Get(dirName)) dir->mkdir(dirName);
144       dir->cd(dirName);
145       dir = gDirectory;
146       strname.Remove(0, index+1);
147     } 
148
149     entry->Write(strname);
150     if (saveDir) saveDir->cd(); else gROOT->cd();
151   
152   }
153   
154   return (entry->GetObject())->Clone();
155
156 }
157
158
159 //_____________________________________________________________________________
160 Bool_t AliRunDataStorage::Put(const TObject* object, 
161                               const AliObjectMetaData& objMetaData)
162 {
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. 
169
170   if (!object) return kFALSE;
171   AliRunData entry(object->Clone(), objMetaData);
172   return PutEntry(&entry);
173 }
174
175 //_____________________________________________________________________________
176 Bool_t AliRunDataStorage::PutEntry(AliRunData* entry)
177 {
178 // put an object into the data base
179 // Refer to the specific method of the current AliRunDataStorage instance
180 // (AliRunDataFile, AliRunDataOrganizedFile, AliRunDataAlien)
181
182   if (!entry) return kFALSE;
183   AliError(Form("This is a read only data base. "
184                 "The object %s was not inserted", entry->GetName()));
185   return kFALSE;
186 }
187
188
189 //_____________________________________________________________________________
190 void AliRunDataStorage::Select(const AliSelectionMetaData& selMetaData)
191 {
192 // add some meta data selection criteria
193
194   fSelection.Add(new AliSelectionMetaData(selMetaData));
195 }
196
197
198 //_____________________________________________________________________________
199 Bool_t AliRunDataStorage::RecordToFile(const char* fileName)
200 {
201 // record entries retrieved from the data base to a file with the given name
202
203   if (fRecordFile) {
204     fRecordFile->Close();
205     delete fRecordFile;
206   }
207
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));
213     delete fRecordFile;
214     fRecordFile = NULL;
215     return kFALSE;
216   }
217   return kTRUE;
218 }
219
220
221 //_____________________________________________________________________________
222 AliRunDataStorage* AliRunDataStorage::Instance()
223 {
224 // return the current instance of the DB (AliRunDataFile, AliRunOrganizedDataFile...)
225 // Example of usage: after creating an istance of AliRunDataStorage:
226 // AliRunDataStorage::Instance()->Get(...)
227
228   return fgInstance;
229 }
230
231
232 //_____________________________________________________________________________
233 const AliObjectMetaData& AliRunDataStorage::GetObjectMetaData(const char* name)
234 {
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 
237 // retrieved)
238
239 AliRunData *entry = (AliRunData*) fEntries.FindObject(name);
240  if(!entry){
241     AliError(Form("Entry %s not found! You make me crash!",name));
242  }
243  return entry->GetObjectMetaData(); 
244
245 }