1 /**************************************************************************
2 * Copyright(c) 1998-2007, 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 //-------------------------------------------------------------------------
19 // Offline Analysis Database Container and Service Class
20 // Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
23 #include "AliOADBContainer.h"
25 #include <TObjArray.h>
33 ClassImp(AliOADBContainer);
35 //______________________________________________________________________________
36 AliOADBContainer::AliOADBContainer() :
44 // Default constructor
47 AliOADBContainer::AliOADBContainer(const char* name) :
48 TNamed(name, "OADBContainer"),
49 fArray(new TObjArray(100)),
50 fDefaultList(new TList()),
59 //______________________________________________________________________________
60 AliOADBContainer::~AliOADBContainer()
63 if (fArray) delete fArray;
64 if (fDefaultList) delete fDefaultList;
68 //______________________________________________________________________________
69 AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
72 fDefaultList(cont.fDefaultList),
73 fLowerLimits(cont.fLowerLimits),
74 fUpperLimits(cont.fUpperLimits),
75 fEntries(cont.fEntries)
80 //______________________________________________________________________________
81 AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
84 // Assignment operator
85 // Copy objects related to run ranges
87 TNamed::operator=(cont);
88 fEntries = cont.fEntries;
89 fLowerLimits.Set(fEntries);
90 fUpperLimits.Set(fEntries);
91 for (Int_t i = 0; i < fEntries; i++) {
92 fLowerLimits[i] = cont.fLowerLimits[i];
93 fUpperLimits[i] = cont.fUpperLimits[i];
94 fArray->AddAt(cont.fArray->At(i), i);
98 // Copy default objects
99 TList* list = cont.GetDefaultList();
102 while((obj = next())) fDefaultList->Add(obj);
107 void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
110 // Append a new object to the list
112 // Check that there is no overlap with existing run ranges
113 Int_t index = HasOverlap(lower, upper);
116 AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
122 fLowerLimits.Set(fEntries);
123 fUpperLimits.Set(fEntries);
126 fLowerLimits[fEntries - 1] = lower;
127 fUpperLimits[fEntries - 1] = upper;
131 void AliOADBContainer::RemoveObject(Int_t idx)
134 // Remove object from the list
137 // Check that index is inside range
138 if (idx < 0 || idx >= fEntries)
140 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
145 TObject* obj = fArray->RemoveAt(idx);
148 // Adjust the run ranges and shrink the array
149 for (Int_t i = idx; i < (fEntries-1); i++) {
150 fLowerLimits[i] = fLowerLimits[i + 1];
151 fUpperLimits[i] = fUpperLimits[i + 1];
152 fArray->AddAt(fArray->At(i+1), i);
154 fArray->RemoveAt(fEntries - 1);
158 void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
161 // Update an existing object, at a given position
163 // Check that index is inside range
164 if (idx < 0 || idx >= fEntries)
166 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
170 // Remove the old object and reset the range
172 fArray->RemoveAt(idx);
173 // don't delete it: if you are updating it may be pointing to the same location of obj...
175 fLowerLimits[idx] = -1;
176 fUpperLimits[idx] = -1;
177 // Check that there is no overlap with existing run ranges
178 Int_t index = HasOverlap(lower, upper);
180 AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
184 // Add object at the same position
185 //printf("idx %d obj %llx\n", idx, obj);
186 fLowerLimits[idx] = lower;
187 fUpperLimits[idx] = upper;
188 fArray->AddAt(obj, idx);
192 void AliOADBContainer::AddDefaultObject(TObject* obj)
194 // Add a default object
195 fDefaultList->Add(obj);
198 void AliOADBContainer::CleanDefaultList()
200 // Clean default list
201 fDefaultList->Delete();
204 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
207 // Find the index for a given run
211 for (Int_t i = 0; i < fEntries; i++)
213 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
221 AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
222 } else if (index == -1) {
223 AliWarning(Form("No object (%s) found for run %5d !\n", GetName(), run));
229 TObject* AliOADBContainer::GetObject(Int_t run, const char* def) const
231 // Return object for given run or default if not found
233 Int_t idx = GetIndexForRun(run);
235 // no object found, try default
236 obj = fDefaultList->FindObject(def);
238 AliError(Form("Default Object (%s) not found !\n", GetName()));
244 return (fArray->At(idx));
248 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
250 // Return object for given index
251 return (fArray->At(run));
254 void AliOADBContainer::WriteToFile(const char* fname) const
257 // Write object to file
258 TFile* f = new TFile(fname, "update");
264 Int_t AliOADBContainer::InitFromFile(const char* fname, const char* key)
267 // Initialize object from file
268 TFile* file = TFile::Open(fname);
269 if (!file) return (1);
270 AliOADBContainer* cont = 0;
271 file->GetObject(key, cont);
274 AliError(Form("Object (%s) not found in file \n", GetName()));
278 SetName(cont->GetName());
279 SetTitle(cont->GetTitle());
281 fEntries = cont->GetNumberOfEntries();
282 fLowerLimits.Set(fEntries);
283 fUpperLimits.Set(fEntries);
284 if(fEntries > fArray->GetSize()) fArray->Expand(fEntries);
286 for (Int_t i = 0; i < fEntries; i++) {
287 fLowerLimits[i] = cont->LowerLimit(i);
288 fUpperLimits[i] = cont->UpperLimit(i);
289 fArray->AddAt(cont->GetObjectByIndex(i), i);
291 if (!fDefaultList) fDefaultList = new TList();
292 TIter next(cont->GetDefaultList());
294 while((obj = next())) fDefaultList->Add(obj);
301 void AliOADBContainer::List()
305 printf("Entries %d\n", fEntries);
307 for (Int_t i = 0; i < fEntries; i++) {
308 printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
309 (fArray->At(i))->Dump();
311 TIter next(fDefaultList);
313 while((obj = next())) obj->Dump();
317 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const
320 // Checks for overlpapping validity regions
321 for (Int_t i = 0; i < fEntries; i++) {
322 if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
323 (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))
331 void AliOADBContainer::Browse(TBrowser *b)
333 // Browse this object.
334 // If b=0, there is no Browse call TObject::Browse(0) instead.
335 // This means TObject::Inspect() will be invoked indirectly
339 for (Int_t i = 0; i < fEntries; i++) {
340 b->Add(fArray->At(i),Form("%9.9d - %9.9d", fLowerLimits[i], fUpperLimits[i]));
342 TIter next(fDefaultList);
344 while((obj = next())) b->Add(obj);
351 //______________________________________________________________________________
352 const char* AliOADBContainer::GetOADBPath()
354 // returns the path of the OADB
355 // this static function just depends on environment variables
357 static TString oadbPath;
359 if (gSystem->Getenv("OADB_PATH"))
360 oadbPath = gSystem->Getenv("OADB_PATH");
361 else if (gSystem->Getenv("ALICE_ROOT"))
362 oadbPath.Form("%s/OADB", gSystem->Getenv("ALICE_ROOT"));
364 ::Fatal("AliAnalysisManager::GetOADBPath", "Cannot figure out AODB path. Define ALICE_ROOT or OADB_PATH!");