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()
65 //______________________________________________________________________________
66 AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
69 fDefaultList(cont.fDefaultList),
70 fLowerLimits(cont.fLowerLimits),
71 fUpperLimits(cont.fUpperLimits),
72 fEntries(cont.fEntries)
77 //______________________________________________________________________________
78 AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
81 // Assignment operator
82 // Copy objects related to run ranges
84 TNamed::operator=(cont);
85 fEntries = cont.fEntries;
86 fLowerLimits.Set(fEntries);
87 fUpperLimits.Set(fEntries);
88 for (Int_t i = 0; i < fEntries; i++) {
89 fLowerLimits[i] = cont.fLowerLimits[i];
90 fUpperLimits[i] = cont.fUpperLimits[i];
91 fArray->AddAt(cont.fArray->At(i), i);
95 // Copy default objects
96 TList* list = cont.GetDefaultList();
99 while((obj = next())) fDefaultList->Add(obj);
104 void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
107 // Append a new object to the list
109 // Check that there is no overlap with existing run ranges
110 Int_t index = HasOverlap(lower, upper);
113 AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
119 fLowerLimits.Set(fEntries);
120 fUpperLimits.Set(fEntries);
123 fLowerLimits[fEntries - 1] = lower;
124 fUpperLimits[fEntries - 1] = upper;
128 void AliOADBContainer::RemoveObject(Int_t idx)
131 // Remove object from the list
134 // Check that index is inside range
135 if (idx < 0 || idx >= fEntries)
137 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
142 TObject* obj = fArray->RemoveAt(idx);
145 // Adjust the run ranges and shrink the array
146 for (Int_t i = idx; i < (fEntries-1); i++) {
147 fLowerLimits[i] = fLowerLimits[i + 1];
148 fUpperLimits[i] = fUpperLimits[i + 1];
149 fArray->AddAt(fArray->At(i+1), i);
151 fArray->RemoveAt(fEntries - 1);
155 void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
158 // Update an existing object, at a given position
160 // Check that index is inside range
161 if (idx < 0 || idx >= fEntries)
163 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
167 // Remove the old object and reset the range
169 fArray->RemoveAt(idx);
170 // don't delete it: if you are updating it may be pointing to the same location of obj...
172 fLowerLimits[idx] = -1;
173 fUpperLimits[idx] = -1;
174 // Check that there is no overlap with existing run ranges
175 Int_t index = HasOverlap(lower, upper);
177 AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
181 // Add object at the same position
182 //printf("idx %d obj %llx\n", idx, obj);
183 fLowerLimits[idx] = lower;
184 fUpperLimits[idx] = upper;
185 fArray->AddAt(obj, idx);
189 void AliOADBContainer::AddDefaultObject(TObject* obj)
191 // Add a default object
192 fDefaultList->Add(obj);
195 void AliOADBContainer::CleanDefaultList()
197 // Clean default list
198 fDefaultList->Delete();
201 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
204 // Find the index for a given run
208 for (Int_t i = 0; i < fEntries; i++)
210 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
218 AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
219 } else if (index == -1) {
220 AliWarning(Form("No object (%s) found for run %5d !\n", GetName(), run));
226 TObject* AliOADBContainer::GetObject(Int_t run, const char* def) const
228 // Return object for given run or default if not found
230 Int_t idx = GetIndexForRun(run);
232 // no object found, try default
233 obj = fDefaultList->FindObject(def);
235 AliError(Form("Default Object (%s) not found !\n", GetName()));
241 return (fArray->At(idx));
245 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
247 // Return object for given index
248 return (fArray->At(run));
251 void AliOADBContainer::WriteToFile(const char* fname) const
254 // Write object to file
255 TFile* f = new TFile(fname, "update");
261 Int_t AliOADBContainer::InitFromFile(const char* fname, const char* key)
264 // Initialize object from file
265 TFile* file = TFile::Open(fname);
266 if (!file) return (1);
267 AliOADBContainer* cont = 0;
268 file->GetObject(key, cont);
271 AliError(Form("Object (%s) not found in file \n", GetName()));
275 SetName(cont->GetName());
276 SetTitle(cont->GetTitle());
278 fEntries = cont->GetNumberOfEntries();
279 fLowerLimits.Set(fEntries);
280 fUpperLimits.Set(fEntries);
281 if(fEntries > fArray->GetSize()) fArray->Expand(fEntries);
283 for (Int_t i = 0; i < fEntries; i++) {
284 fLowerLimits[i] = cont->LowerLimit(i);
285 fUpperLimits[i] = cont->UpperLimit(i);
286 fArray->AddAt(cont->GetObjectByIndex(i), i);
288 if (!fDefaultList) fDefaultList = new TList();
289 TIter next(cont->GetDefaultList());
291 while((obj = next())) fDefaultList->Add(obj);
298 void AliOADBContainer::List()
302 printf("Entries %d\n", fEntries);
304 for (Int_t i = 0; i < fEntries; i++) {
305 printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
306 (fArray->At(i))->Dump();
308 TIter next(fDefaultList);
310 while((obj = next())) obj->Dump();
314 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const
317 // Checks for overlpapping validity regions
318 for (Int_t i = 0; i < fEntries; i++) {
319 if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
320 (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))
328 void AliOADBContainer::Browse(TBrowser *b)
330 // Browse this object.
331 // If b=0, there is no Browse call TObject::Browse(0) instead.
332 // This means TObject::Inspect() will be invoked indirectly
336 for (Int_t i = 0; i < fEntries; i++) {
337 b->Add(fArray->At(i),Form("%9.9d - %9.9d", fLowerLimits[i], fUpperLimits[i]));
339 TIter next(fDefaultList);
341 while((obj = next())) b->Add(obj);
348 //______________________________________________________________________________
349 const char* AliOADBContainer::GetOADBPath()
351 // returns the path of the OADB
352 // this static function just depends on environment variables
354 static TString oadbPath;
356 if (gSystem->Getenv("OADB_PATH"))
357 oadbPath = gSystem->Getenv("OADB_PATH");
358 else if (gSystem->Getenv("ALICE_ROOT"))
359 oadbPath.Form("%s/OADB", gSystem->Getenv("ALICE_ROOT"));
361 ::Fatal("AliAnalysisManager::GetOADBPath", "Cannot figure out AODB path. Define ALICE_ROOT or OADB_PATH!");