New version of the CDB classes. Run number added (A.Colla).
[u/mrichter/AliRoot.git] / STEER / AliCDBStorage.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 #include <TKey.h>
17 #include <TH1.h>
18 #include "AliCDBManager.h"
19 #include "AliCDBStorage.h"
20
21 #include "AliCDBEntry.h"
22 #include "AliLog.h"
23
24 ClassImp(AliCDBStorage)
25
26 //_____________________________________________________________________________
27 AliCDBStorage::AliCDBStorage() {
28 // constructor
29
30         fSelections.SetOwner(1);
31 }
32
33 //_____________________________________________________________________________
34 AliCDBStorage::~AliCDBStorage() {
35 // destructor
36
37 }
38
39 //_____________________________________________________________________________
40 void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
41 // return required version and subversion from the list of selection criteria 
42         
43         TIter iter(&fSelections);
44         AliCDBId* aSelection;
45         
46         // loop on the list of selection criteria
47         while ((aSelection = (AliCDBId*) iter.Next())) {
48                 // check if selection element contains id's path and run (range) 
49                 if (aSelection->Comprises(*id)) {
50                         AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
51                         // return required version and subversion
52                         
53                         id->SetVersion(aSelection->GetVersion());
54                         id->SetSubVersion(aSelection->GetSubVersion());
55                         return;  
56                 }
57         }
58         
59         // no valid element is found in the list of selection criteria -> return
60         AliDebug(2,"Looking for objects with most recent version");
61         return;
62 }
63
64 //_____________________________________________________________________________
65 void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
66 // read selection criteria list from file
67         
68         RemoveAllSelections();
69         
70         TList *list = GetIdListFromFile(fileName);
71         if(!list) return;
72         
73         list->SetOwner();       
74         Int_t nId = list->GetEntries();
75         AliCDBId *id;
76         TKey *key;
77         
78         for(int i=nId-1;i>=0;i--){
79                 key = (TKey*) list->At(i);
80                 id = (AliCDBId*) key->ReadObj();
81                 if(id) AddSelection(*id);
82         }
83         delete list;
84         AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
85         PrintSelectionList();
86         
87 }
88
89 //_____________________________________________________________________________
90 void AliCDBStorage::AddSelection(const AliCDBId& selection) {
91 // add a selection criterion
92
93         AliCDBPath path = selection.GetPath();
94         if(!path.IsValid()) return;
95         
96         TIter iter(&fSelections);
97         const AliCDBId *anId;
98         while((anId = (AliCDBId*) iter.Next())){
99                 if(selection.Comprises(*anId)){
100                         AliWarning("This selection is more general than a previous one and will hide it!");
101                         AliWarning(Form("%s", (anId->ToString()).Data()));
102                         fSelections.AddBefore(anId, new AliCDBId(selection));
103                         return;
104                 }
105         
106         }
107         fSelections.AddFirst(new AliCDBId(selection));
108 }
109
110 //_____________________________________________________________________________
111 void AliCDBStorage::AddSelection(const AliCDBPath& path, 
112         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
113 // add a selection criterion
114
115         AddSelection(AliCDBId(path, runRange, version, subVersion));
116 }
117
118 //_____________________________________________________________________________
119 void AliCDBStorage::AddSelection(const AliCDBPath& path,
120         Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
121 // add a selection criterion
122         
123         AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
124 }
125
126 //_____________________________________________________________________________
127 void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
128 // remove a selection criterion
129
130         TIter iter(&fSelections);
131         AliCDBId* aSelection;
132
133         while ((aSelection = (AliCDBId*) iter.Next())) {
134                 if (selection.Comprises(*aSelection)) {
135                         fSelections.Remove(aSelection);
136                 }
137         }
138 }
139
140 //_____________________________________________________________________________
141 void AliCDBStorage::RemoveSelection(const AliCDBPath& path, 
142         const AliCDBRunRange& runRange){
143 // remove a selection criterion
144
145         RemoveSelection(AliCDBId(path, runRange, -1, -1));
146 }
147
148 //_____________________________________________________________________________
149 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
150         Int_t firstRun, Int_t lastRun){
151 // remove a selection criterion
152
153         RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
154 }
155
156 //_____________________________________________________________________________
157 void AliCDBStorage::RemoveSelection(int position){
158 // remove a selection criterion from its position in the list
159
160         fSelections.RemoveAt(position);
161 }
162
163 //_____________________________________________________________________________
164 void AliCDBStorage::RemoveAllSelections(){
165 // remove all selection criteria
166
167         fSelections.RemoveAll();
168 }
169
170 //_____________________________________________________________________________
171 void AliCDBStorage::PrintSelectionList(){
172 // prints the list of selection criteria
173
174         TIter iter(&fSelections);
175         AliCDBId* aSelection;
176         
177         // loop on the list of selection criteria
178         int index=0;
179         while ((aSelection = (AliCDBId*) iter.Next())) {
180                 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
181         }
182
183 }
184
185 //_____________________________________________________________________________
186 AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {        
187 // get an AliCDBEntry object from the database
188         
189         // check if query's path and runRange are valid
190         // query is invalid also if version is not specified and subversion is!
191         if (!query.IsValid()) {
192                 AliError(Form("Invalid query: %s", query.ToString().Data()));
193                 return NULL;
194         }
195
196         // query is not specified if path contains wildcard or runrange = [-1,-1] 
197         if (!query.IsSpecified()) {
198                 AliError(Form("Unspecified query: %s", 
199                                 query.ToString().Data()));
200                 return NULL;
201         }
202         
203         // This is needed otherwise TH1  objects (histos, TTree's) are lost when file is closed!
204         Bool_t oldStatus = TH1::AddDirectoryStatus();
205         TH1::AddDirectory(kFALSE);
206
207         AliCDBEntry* entry = GetEntry(query);
208
209         if (oldStatus != kFALSE)
210                 TH1::AddDirectory(kTRUE);
211                 
212         if (entry) {
213                 AliInfo(Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
214         } else {
215                 AliInfo(Form("No valid CDB object found! request was: name = <%s>, run = %d", 
216                         (query.GetPath()).Data(), query.GetFirstRun()));
217         }
218         
219         // if drain storage is set, drain entry into drain storage
220         if(entry && (AliCDBManager::Instance())->IsDrainSet())
221                 AliCDBManager::Instance()->Drain(entry);
222         
223         return entry;
224 }
225
226 //_____________________________________________________________________________
227 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber, 
228         Int_t version, Int_t subVersion) {
229 // get an AliCDBEntry object from the database
230
231         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
232 }
233
234 //_____________________________________________________________________________
235 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, 
236         const AliCDBRunRange& runRange, Int_t version,
237         Int_t subVersion) {
238 // get an AliCDBEntry object from the database
239
240         return Get(AliCDBId(path, runRange, version, subVersion));
241 }
242
243 //_____________________________________________________________________________
244 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
245 // get multiple AliCDBEntry objects from the database
246
247
248         if (!query.IsValid()) {
249                 AliError(Form("Invalid query: %s", query.ToString().Data()));
250                 return NULL;
251         }
252
253         if (query.IsAnyRange()) {
254                 AliError(Form("Unspecified run or runrange: %s",
255                                 query.ToString().Data()));      
256                 return NULL;
257         }       
258         
259         // This is needed otherwise TH1  objects (histos, TTree's) are lost when file is closed!
260         Bool_t oldStatus = TH1::AddDirectoryStatus();
261         TH1::AddDirectory(kFALSE);
262
263         TList *result = GetEntries(query);
264
265         if (oldStatus != kFALSE)
266                 TH1::AddDirectory(kTRUE);
267
268         Int_t nEntries = result->GetEntries();
269         if (nEntries) {
270                  AliInfo(Form("%d AliCDBEntry objects retrieved.",nEntries));
271                  for(int i=0; i<nEntries;i++){
272                         AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
273                         AliInfo(Form("%s",entry->GetId().ToString().Data()));
274                  
275                  }
276         } else {
277                  AliInfo(Form("No valid CDB object found! request was: name = <%s>, run = %d, version = %d", 
278                         (query.GetPath()).Data(), query.GetFirstRun(), query.GetVersion()));
279         }
280
281         // if drain storage is set, drain entries into drain storage
282         if((AliCDBManager::Instance())->IsDrainSet()){
283                 for(int i = 0; i<result->GetEntries(); i++){
284                         AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
285                         AliCDBManager::Instance()->Drain(entry);
286                 }
287         }
288         
289
290         return result;
291 }
292
293 //_____________________________________________________________________________
294 TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber, 
295         Int_t version, Int_t subVersion) {
296 // get multiple AliCDBEntry objects from the database
297
298         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
299                         subVersion));
300 }
301
302 //_____________________________________________________________________________
303 TList* AliCDBStorage::GetAll(const AliCDBPath& path, 
304         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
305 // get multiple AliCDBEntry objects from the database
306
307         return GetAll(AliCDBId(path, runRange, version, subVersion));
308 }
309
310
311 //_____________________________________________________________________________
312 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData) {
313 // store an AliCDBEntry object into the database
314         
315         AliCDBEntry anEntry(object, id, metaData);
316
317         return Put(&anEntry);
318
319
320 //_____________________________________________________________________________
321 Bool_t AliCDBStorage::Put(AliCDBEntry* entry) {
322 // store an AliCDBEntry object into the database
323
324         if (!entry){
325                 AliError("No entry!");
326                 return kFALSE;
327         }
328         
329         if (!entry->GetId().IsValid()) {
330                 AliError(Form("Invalid entry ID: %s", 
331                         entry->GetId().ToString().Data()));
332                 return kFALSE;
333         }       
334
335         if (!entry->GetId().IsSpecified()) {
336                 AliError(Form("Unspecified entry ID: %s", 
337                         entry->GetId().ToString().Data()));
338                 return kFALSE;
339         }
340
341         // set object's class name into metaData!
342         entry->GetMetaData()->SetObjectClassName(entry->GetObject()->ClassName());
343
344         return PutEntry(entry);
345 }
346