]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCDBStorage.cxx
i) Modifying the GetInputChain function - removing the AliXMLCollection::OverlapColle...
[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 "AliCDBStorage.h"
19 #include "AliCDBGrid.h"
20
21 #include "AliCDBEntry.h"
22 #include "AliLog.h"
23
24 ClassImp(AliCDBStorage)
25
26 //_____________________________________________________________________________
27 AliCDBStorage::AliCDBStorage():
28 fValidFileIds(),
29 fRun(-1),
30 fPathFilter(),
31 fVersion(-1),
32 fMetaDataFilter(0),
33 fSelections(),
34 fURI(),
35 fType(),
36 fBaseFolder()
37 {
38 // constructor
39
40         fValidFileIds.SetOwner(1);
41         fSelections.SetOwner(1);
42 }
43
44 //_____________________________________________________________________________
45 AliCDBStorage::~AliCDBStorage() {
46 // destructor
47
48         RemoveAllSelections();
49         fValidFileIds.Clear();
50         delete fMetaDataFilter;
51
52 }
53
54 //_____________________________________________________________________________
55 void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
56 // return required version and subversion from the list of selection criteria
57         
58         TIter iter(&fSelections);
59         AliCDBId* aSelection;
60         
61         // loop on the list of selection criteria
62         while ((aSelection = (AliCDBId*) iter.Next())) {
63                 // check if selection element contains id's path and run (range) 
64                 if (aSelection->Comprises(*id)) {
65                         AliDebug(2,Form("Using selection criterion: %s ", aSelection->ToString().Data()));
66                         // return required version and subversion
67                         
68                         id->SetVersion(aSelection->GetVersion());
69                         id->SetSubVersion(aSelection->GetSubVersion());
70                         return;  
71                 }
72         }
73         
74         // no valid element is found in the list of selection criteria -> return
75         AliDebug(2,"Looking for objects with most recent version");
76         return;
77 }
78
79 //_____________________________________________________________________________
80 void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
81 // read selection criteria list from file
82         
83         RemoveAllSelections();
84         
85         TList *list = GetIdListFromFile(fileName);
86         if(!list) return;
87         
88         list->SetOwner();       
89         Int_t nId = list->GetEntries();
90         AliCDBId *id;
91         TKey *key;
92         
93         for(int i=nId-1;i>=0;i--){
94                 key = (TKey*) list->At(i);
95                 id = (AliCDBId*) key->ReadObj();
96                 if(id) AddSelection(*id);
97         }
98         delete list;
99         AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
100         PrintSelectionList();
101         
102 }
103
104 //_____________________________________________________________________________
105 void AliCDBStorage::AddSelection(const AliCDBId& selection) {
106 // add a selection criterion
107
108         AliCDBPath path = selection.GetPath();
109         if(!path.IsValid()) return;
110         
111         TIter iter(&fSelections);
112         const AliCDBId *anId;
113         while((anId = (AliCDBId*) iter.Next())){
114                 if(selection.Comprises(*anId)){
115                         AliWarning("This selection is more general than a previous one and will hide it!");
116                         AliWarning(Form("%s", (anId->ToString()).Data()));
117                         fSelections.AddBefore(anId, new AliCDBId(selection));
118                         return;
119                 }
120         
121         }
122         fSelections.AddFirst(new AliCDBId(selection));
123 }
124
125 //_____________________________________________________________________________
126 void AliCDBStorage::AddSelection(const AliCDBPath& path,
127         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
128 // add a selection criterion
129
130         AddSelection(AliCDBId(path, runRange, version, subVersion));
131 }
132
133 //_____________________________________________________________________________
134 void AliCDBStorage::AddSelection(const AliCDBPath& path,
135         Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion){
136 // add a selection criterion
137         
138         AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
139 }
140
141 //_____________________________________________________________________________
142 void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
143 // remove a selection criterion
144
145         TIter iter(&fSelections);
146         AliCDBId* aSelection;
147
148         while ((aSelection = (AliCDBId*) iter.Next())) {
149                 if (selection.Comprises(*aSelection)) {
150                         fSelections.Remove(aSelection);
151                 }
152         }
153 }
154
155 //_____________________________________________________________________________
156 void AliCDBStorage::RemoveSelection(const AliCDBPath& path, 
157         const AliCDBRunRange& runRange){
158 // remove a selection criterion
159
160         RemoveSelection(AliCDBId(path, runRange, -1, -1));
161 }
162
163 //_____________________________________________________________________________
164 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
165         Int_t firstRun, Int_t lastRun){
166 // remove a selection criterion
167
168         RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
169 }
170
171 //_____________________________________________________________________________
172 void AliCDBStorage::RemoveSelection(int position){
173 // remove a selection criterion from its position in the list
174
175         delete fSelections.RemoveAt(position);
176 }
177
178 //_____________________________________________________________________________
179 void AliCDBStorage::RemoveAllSelections(){
180 // remove all selection criteria
181
182         fSelections.Clear();
183 }
184
185 //_____________________________________________________________________________
186 void AliCDBStorage::PrintSelectionList(){
187 // prints the list of selection criteria
188
189         TIter iter(&fSelections);
190         AliCDBId* aSelection;
191
192         // loop on the list of selection criteria
193         int index=0;
194         while ((aSelection = (AliCDBId*) iter.Next())) {
195                 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
196         }
197
198 }
199
200 //_____________________________________________________________________________
201 AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
202 // get an AliCDBEntry object from the database
203         
204         // check if query's path and runRange are valid
205         // query is invalid also if version is not specified and subversion is!
206         if (!query.IsValid()) {
207                 AliError(Form("Invalid query: %s", query.ToString().Data()));
208                 return NULL;
209         }
210
211         // query is not specified if path contains wildcard or runrange = [-1,-1] 
212         if (!query.IsSpecified()) {
213                 AliError(Form("Unspecified query: %s", 
214                                 query.ToString().Data()));
215                 return NULL;
216         }
217
218         // This is needed otherwise TH1  objects (histos, TTree's) are lost when file is closed!
219         Bool_t oldStatus = TH1::AddDirectoryStatus();
220         TH1::AddDirectory(kFALSE);
221
222         AliCDBEntry* entry = GetEntry(query);
223
224         if (oldStatus != kFALSE)
225                 TH1::AddDirectory(kTRUE);
226
227         if (entry) {
228                 // this is to make the SHUTTLE output lighter
229                 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
230                         AliDebug(2, Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
231         } else {
232                 // this is to make the SHUTTLE output lighter
233                 if(!(query.GetPath().Contains("SHUTTLE/STATUS")))
234                         AliInfo(Form("No valid CDB object found! request was: %s", query.ToString().Data()));
235         }
236
237         // if drain storage is set, drain entry into drain storage
238         if(entry && (AliCDBManager::Instance())->IsDrainSet())
239                 AliCDBManager::Instance()->Drain(entry);
240
241         return entry;
242 }
243
244 //_____________________________________________________________________________
245 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, Int_t runNumber, 
246         Int_t version, Int_t subVersion) {
247 // get an AliCDBEntry object from the database
248
249         return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
250 }
251
252 //_____________________________________________________________________________
253 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path, 
254         const AliCDBRunRange& runRange, Int_t version,
255         Int_t subVersion) {
256 // get an AliCDBEntry object from the database
257
258         return Get(AliCDBId(path, runRange, version, subVersion));
259 }
260
261 //_____________________________________________________________________________
262 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
263 // get multiple AliCDBEntry objects from the database
264
265
266         if (!query.IsValid()) {
267                 AliError(Form("Invalid query: %s", query.ToString().Data()));
268                 return NULL;
269         }
270
271         if (query.IsAnyRange()) {
272                 AliError(Form("Unspecified run or runrange: %s",
273                                 query.ToString().Data()));      
274                 return NULL;
275         }       
276         
277         // This is needed otherwise TH1  objects (histos, TTree's) are lost when file is closed!
278         Bool_t oldStatus = TH1::AddDirectoryStatus();
279         TH1::AddDirectory(kFALSE);
280
281         TList *result = GetEntries(query);
282
283         if (oldStatus != kFALSE)
284                 TH1::AddDirectory(kTRUE);
285
286         Int_t nEntries = result->GetEntries();
287
288         AliInfo(Form("%d objects retrieved. Request was: %s",
289                         nEntries, query.ToString().Data()));
290         for(int i=0; i<nEntries;i++){
291                 AliCDBEntry *entry = (AliCDBEntry*) result->At(i);
292                 AliInfo(Form("%s",entry->GetId().ToString().Data()));
293         }
294
295
296         // if drain storage is set, drain entries into drain storage
297         if((AliCDBManager::Instance())->IsDrainSet()){
298                 for(int i = 0; i<result->GetEntries(); i++){
299                         AliCDBEntry* entry = (AliCDBEntry*) result->At(i);
300                         AliCDBManager::Instance()->Drain(entry);
301                 }
302         }
303         
304
305         return result;
306 }
307
308 //_____________________________________________________________________________
309 TList* AliCDBStorage::GetAll(const AliCDBPath& path, Int_t runNumber, 
310         Int_t version, Int_t subVersion) {
311 // get multiple AliCDBEntry objects from the database
312
313         return GetAll(AliCDBId(path, runNumber, runNumber, version,     
314                         subVersion));
315 }
316
317 //_____________________________________________________________________________
318 TList* AliCDBStorage::GetAll(const AliCDBPath& path, 
319         const AliCDBRunRange& runRange, Int_t version, Int_t subVersion) {
320 // get multiple AliCDBEntry objects from the database
321
322         return GetAll(AliCDBId(path, runRange, version, subVersion));
323 }
324
325
326 //_____________________________________________________________________________
327 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData, AliCDBManager::DataType type) {
328 // store an AliCDBEntry object into the database
329         
330         AliCDBEntry anEntry(object, id, metaData);
331
332         return Put(&anEntry, type);
333 }
334
335 //_____________________________________________________________________________
336 Bool_t AliCDBStorage::Put(AliCDBEntry* entry, AliCDBManager::DataType type) {
337 // store an AliCDBEntry object into the database
338
339         if (!entry){
340                 AliError("No entry!");
341                 return kFALSE;
342         }
343         
344         if (!entry->GetId().IsValid()) {
345                 AliError(Form("Invalid entry ID: %s",
346                         entry->GetId().ToString().Data()));
347                 return kFALSE;
348         }       
349
350         if (!entry->GetId().IsSpecified()) {
351                 AliError(Form("Unspecified entry ID: %s",
352                         entry->GetId().ToString().Data()));
353                 return kFALSE;
354         }
355
356         AliCDBManager::DataType expectedType = GetDataType();
357
358         if(expectedType != AliCDBManager::kPrivate && type != expectedType) {
359                 AliError(Form("It is forbidden to store %s data into a folder of type %s!",
360                         AliCDBManager::GetDataTypeName(type),
361                         AliCDBManager::GetDataTypeName(expectedType)));
362                         return 0;
363         }
364
365         // set object's class name into metaData!
366         entry->GetMetaData()->SetObjectClassName(entry->GetObject()->ClassName());
367
368         return PutEntry(entry);
369 }
370
371 //_____________________________________________________________________________
372 void AliCDBStorage::QueryCDB(Int_t run, const char* pathFilter,
373                                 Int_t version, AliCDBMetaData* md){
374 // query CDB for files valid for given run, and fill list fValidFileIds
375 // Actual query is done in virtual function QueryValidFiles()
376
377         fRun = run;
378
379         fPathFilter = pathFilter;
380         if(!fPathFilter.IsValid()) {
381                 AliError(Form("Filter not valid: %s", pathFilter));
382                 fPathFilter = "*";
383                 return;
384         }
385
386         fVersion = version;
387
388         AliInfo(Form("Querying files valid for run %d and path \"%s\" into CDB storage  \"%s://%s\"",
389                                 fRun, pathFilter, fType.Data(), fBaseFolder.Data()));
390
391         // Clear fValidFileIds list (it cannot be filled twice!
392         AliDebug(2, "Clearing list of CDB Id's previously loaded");
393         fValidFileIds.Clear();
394
395         if(fMetaDataFilter) {delete fMetaDataFilter; fMetaDataFilter=0;}
396         if(md) fMetaDataFilter = dynamic_cast<AliCDBMetaData*> (md->Clone());
397
398         QueryValidFiles();
399         AliCDBId queryId(pathFilter,run,run,version);
400
401         AliInfo(Form("%d valid files found!", fValidFileIds.GetEntriesFast()));
402
403 }
404
405 //_____________________________________________________________________________
406 void AliCDBStorage::PrintQueryCDB(){
407 // print parameters used to load list of CDB Id's (fRun, fPathFilter, fVersion)
408
409         AliCDBId paramId(fPathFilter, fRun, fRun, fVersion);
410         AliInfo(Form("**** QueryCDB Parameters **** \n\t<%s>\n",
411                                 paramId.ToString().Data()));
412
413         if(fMetaDataFilter) fMetaDataFilter->PrintMetaData();
414
415
416         TString message = "**** Id's of valid objects found *****\n";
417         TIter iter(&fValidFileIds);
418         AliCDBId* anId=0;
419
420         // loop on the list of selection criteria
421         while ((anId = dynamic_cast<AliCDBId*>(iter.Next()))) {
422                 message += Form("\t%s\n", anId->ToString().Data());
423         }
424         message += Form("\n\tTotal: %d objects found\n", fValidFileIds.GetEntriesFast());
425         AliInfo(Form("%s", message.Data()));
426 }
427
428 //_____________________________________________________________________________
429 AliCDBManager::DataType AliCDBStorage::GetDataType() const {
430 // returns the type of the data that should be stored into this storage:
431 // kConditions: conditions data; kReference: reference data; kPrivate: private (user-defined) data type
432
433         if(GetType() != "alien") return AliCDBManager::kPrivate;
434
435         TString condFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetCondParam())->GetDBFolder();
436         TString refFolder = ((AliCDBGridParam*) AliCDBManager::Instance()->GetRefParam())->GetDBFolder();
437
438         if(GetBaseFolder().Contains(condFolder)) return AliCDBManager::kCondition;
439         if(GetBaseFolder().Contains(refFolder)) return AliCDBManager::kReference;
440
441         return AliCDBManager::kPrivate;
442 }