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 #include "AliCDBManager.h"
19 #include "AliCDBStorage.h"
21 #include "AliCDBEntry.h"
24 ClassImp(AliCDBStorage)
26 //_____________________________________________________________________________
27 AliCDBStorage::AliCDBStorage() {
30 fSelections.SetOwner(1);
33 //_____________________________________________________________________________
34 AliCDBStorage::~AliCDBStorage() {
39 //_____________________________________________________________________________
40 void AliCDBStorage::GetSelection(/*const*/ AliCDBId* id) {
41 // return required version and subversion from the list of selection criteria
43 TIter iter(&fSelections);
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
53 id->SetVersion(aSelection->GetVersion());
54 id->SetSubVersion(aSelection->GetSubVersion());
59 // no valid element is found in the list of selection criteria -> return
60 AliDebug(2,"Looking for objects with most recent version");
64 //_____________________________________________________________________________
65 void AliCDBStorage::ReadSelectionFromFile(const char *fileName){
66 // read selection criteria list from file
68 RemoveAllSelections();
70 TList *list = GetIdListFromFile(fileName);
74 Int_t nId = list->GetEntries();
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);
84 AliInfo(Form("Selection criteria list filled with %d entries",fSelections.GetEntries()));
89 //_____________________________________________________________________________
90 void AliCDBStorage::AddSelection(const AliCDBId& selection) {
91 // add a selection criterion
93 AliCDBPath path = selection.GetPath();
94 if(!path.IsValid()) return;
96 TIter iter(&fSelections);
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));
107 fSelections.AddFirst(new AliCDBId(selection));
110 //_____________________________________________________________________________
111 void AliCDBStorage::AddSelection(const AliCDBPath& path,
112 const AliCDBRunRange& runRange, Int_t version, Int_t subVersion){
113 // add a selection criterion
115 AddSelection(AliCDBId(path, runRange, version, subVersion));
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
123 AddSelection(AliCDBId(path, firstRun, lastRun, version, subVersion));
126 //_____________________________________________________________________________
127 void AliCDBStorage::RemoveSelection(const AliCDBId& selection) {
128 // remove a selection criterion
130 TIter iter(&fSelections);
131 AliCDBId* aSelection;
133 while ((aSelection = (AliCDBId*) iter.Next())) {
134 if (selection.Comprises(*aSelection)) {
135 fSelections.Remove(aSelection);
140 //_____________________________________________________________________________
141 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
142 const AliCDBRunRange& runRange){
143 // remove a selection criterion
145 RemoveSelection(AliCDBId(path, runRange, -1, -1));
148 //_____________________________________________________________________________
149 void AliCDBStorage::RemoveSelection(const AliCDBPath& path,
150 Int_t firstRun, Int_t lastRun){
151 // remove a selection criterion
153 RemoveSelection(AliCDBId(path, firstRun, lastRun, -1, -1));
156 //_____________________________________________________________________________
157 void AliCDBStorage::RemoveSelection(int position){
158 // remove a selection criterion from its position in the list
160 fSelections.RemoveAt(position);
163 //_____________________________________________________________________________
164 void AliCDBStorage::RemoveAllSelections(){
165 // remove all selection criteria
167 fSelections.RemoveAll();
170 //_____________________________________________________________________________
171 void AliCDBStorage::PrintSelectionList(){
172 // prints the list of selection criteria
174 TIter iter(&fSelections);
175 AliCDBId* aSelection;
177 // loop on the list of selection criteria
179 while ((aSelection = (AliCDBId*) iter.Next())) {
180 AliInfo(Form("index %d -> selection: %s",index++, aSelection->ToString().Data()));
185 //_____________________________________________________________________________
186 AliCDBEntry* AliCDBStorage::Get(const AliCDBId& query) {
187 // get an AliCDBEntry object from the database
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()));
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()));
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);
207 AliCDBEntry* entry = GetEntry(query);
209 if (oldStatus != kFALSE)
210 TH1::AddDirectory(kTRUE);
213 AliInfo(Form("CDB object retrieved: %s", entry->GetId().ToString().Data()));
215 AliInfo(Form("No valid CDB object found! request was: name = <%s>, run = %d",
216 (query.GetPath()).Data(), query.GetFirstRun()));
219 // if drain storage is set, drain entry into drain storage
220 if(entry && (AliCDBManager::Instance())->IsDrainSet())
221 AliCDBManager::Instance()->Drain(entry);
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
231 return Get(AliCDBId(path, runNumber, runNumber, version, subVersion));
234 //_____________________________________________________________________________
235 AliCDBEntry* AliCDBStorage::Get(const AliCDBPath& path,
236 const AliCDBRunRange& runRange, Int_t version,
238 // get an AliCDBEntry object from the database
240 return Get(AliCDBId(path, runRange, version, subVersion));
243 //_____________________________________________________________________________
244 TList* AliCDBStorage::GetAll(const AliCDBId& query) {
245 // get multiple AliCDBEntry objects from the database
248 if (!query.IsValid()) {
249 AliError(Form("Invalid query: %s", query.ToString().Data()));
253 if (query.IsAnyRange()) {
254 AliError(Form("Unspecified run or runrange: %s",
255 query.ToString().Data()));
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);
263 TList *result = GetEntries(query);
265 if (oldStatus != kFALSE)
266 TH1::AddDirectory(kTRUE);
268 Int_t nEntries = result->GetEntries();
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()));
277 AliInfo(Form("No valid CDB object found! request was: name = <%s>, run = %d, version = %d",
278 (query.GetPath()).Data(), query.GetFirstRun(), query.GetVersion()));
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);
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
298 return GetAll(AliCDBId(path, runNumber, runNumber, version,
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
307 return GetAll(AliCDBId(path, runRange, version, subVersion));
311 //_____________________________________________________________________________
312 Bool_t AliCDBStorage::Put(TObject* object, AliCDBId& id, AliCDBMetaData* metaData) {
313 // store an AliCDBEntry object into the database
315 AliCDBEntry anEntry(object, id, metaData);
317 return Put(&anEntry);
320 //_____________________________________________________________________________
321 Bool_t AliCDBStorage::Put(AliCDBEntry* entry) {
322 // store an AliCDBEntry object into the database
325 AliError("No entry!");
329 if (!entry->GetId().IsValid()) {
330 AliError(Form("Invalid entry ID: %s",
331 entry->GetId().ToString().Data()));
335 if (!entry->GetId().IsSpecified()) {
336 AliError(Form("Unspecified entry ID: %s",
337 entry->GetId().ToString().Data()));
341 // set object's class name into metaData!
342 entry->GetMetaData()->SetObjectClassName(entry->GetObject()->ClassName());
344 return PutEntry(entry);