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 //-------------------------------------------------------------------------
26 #include "AliOADBContainer.h"
28 #include <TObjArray.h>
34 ClassImp(AliOADBContainer);
36 //______________________________________________________________________________
37 AliOADBContainer::AliOADBContainer() :
45 // Default constructor
48 AliOADBContainer::AliOADBContainer(char* name) :
49 TNamed(name, "OADBContainer"),
50 fArray(new TObjArray(100)),
51 fDefaultList(new TList()),
60 //______________________________________________________________________________
61 AliOADBContainer::~AliOADBContainer()
66 //______________________________________________________________________________
67 AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
70 fDefaultList(cont.fDefaultList),
71 fLowerLimits(cont.fLowerLimits),
72 fUpperLimits(cont.fUpperLimits),
73 fEntries(cont.fEntries)
78 //______________________________________________________________________________
79 AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
82 // Assignment operator
83 // Copy objects related to run ranges
85 TNamed::operator=(cont);
86 fEntries = cont.fEntries;
87 fLowerLimits.Set(fEntries);
88 fUpperLimits.Set(fEntries);
89 for (Int_t i = 0; i < fEntries; i++) {
90 fLowerLimits[i] = cont.fLowerLimits[i];
91 fUpperLimits[i] = cont.fUpperLimits[i];
92 fArray->AddAt(cont.fArray->At(i), i);
96 // Copy default objects
97 TList* list = cont.GetDefaultList();
100 while((obj = next())) fDefaultList->Add(obj);
105 void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
108 // Append a new object to the list
110 // Check that there is no overlap with existing run ranges
111 Int_t index = HasOverlap(lower, upper);
114 AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
120 fLowerLimits.Set(fEntries);
121 fUpperLimits.Set(fEntries);
124 fLowerLimits[fEntries - 1] = lower;
125 fUpperLimits[fEntries - 1] = upper;
129 void AliOADBContainer::RemoveObject(Int_t idx)
132 // Remove object from the list
135 // Check that index is inside range
136 if (idx < 0 || idx >= fEntries)
138 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
143 TObject* obj = fArray->RemoveAt(idx);
146 // Adjust the run ranges and shrink the array
147 for (Int_t i = idx; i < (fEntries-1); i++) {
148 fLowerLimits[i] = fLowerLimits[i + 1];
149 fUpperLimits[i] = fUpperLimits[i + 1];
150 fArray->AddAt(fArray->At(i+1), i);
152 fArray->RemoveAt(fEntries - 1);
156 void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
159 // Append a new object to the list
161 // Check that index is inside range
162 if (idx < 0 || idx >= fEntries)
164 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
168 // Check that there is no overlap with existing run ranges
169 Int_t index = HasOverlap(lower, upper);
171 AliFatal(Form("Ambiguos validity range (%5d) !\n", index));
176 fLowerLimits[idx] = lower;
177 fUpperLimits[idx] = upper;
178 fArray->AddAt(obj, idx);
182 void AliOADBContainer::AddDefaultObject(TObject* obj)
184 // Add a default object
185 fDefaultList->Add(obj);
188 void AliOADBContainer::CleanDefaultList()
190 // Clean default list
191 fDefaultList->Delete();
194 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
197 // Find the index for a given run
200 for (Int_t i = 0; i < fEntries; i++)
202 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
210 AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
211 } else if (index == -1) {
212 AliWarning(Form("No object found for run %5d !\n", run));
218 TObject* AliOADBContainer::GetObject(Int_t run, char* def) const
220 // Return object for given run or default if not found
222 Int_t idx = GetIndexForRun(run);
224 // no object found, try default
225 obj = fDefaultList->FindObject(def);
227 AliError("Default Object not found !\n");
233 return (fArray->At(idx));
237 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
239 // Return object for given index
240 return (fArray->At(run));
243 void AliOADBContainer::WriteToFile(char* fname) const
246 // Write object to file
247 TFile* f = new TFile(fname, "update");
253 Int_t AliOADBContainer::InitFromFile(char* fname, char* key)
256 // Initialize object from file
257 TFile* file = TFile::Open(fname);
258 if (!file) return (1);
259 AliOADBContainer* cont = 0;
260 file->GetObject(key, cont);
263 AliError("Object not found in file \n");
267 fEntries = cont->GetNumberOfEntries();
268 fLowerLimits.Set(fEntries);
269 fUpperLimits.Set(fEntries);
270 for (Int_t i = 0; i < fEntries; i++) {
271 fLowerLimits[i] = cont->LowerLimit(i);
272 fUpperLimits[i] = cont->UpperLimit(i);
273 fArray->AddAt(cont->GetObjectByIndex(i), i);
276 TIter next(cont->GetDefaultList());
278 while((obj = next())) fDefaultList->Add(obj);
285 void AliOADBContainer::List()
289 for (Int_t i = 0; i < fEntries; i++) {
290 printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
291 (fArray->At(i))->Dump();
293 TIter next(fDefaultList);
295 while((obj = next())) obj->Dump();
299 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const
302 // Checks for overlpapping validity regions
303 for (Int_t i = 0; i < fEntries; i++) {
304 if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
305 (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))
313 void AliOADBContainer::Browse(TBrowser *b)
315 // Browse this object.
316 // If b=0, there is no Browse call TObject::Browse(0) instead.
317 // This means TObject::Inspect() will be invoked indirectly
321 for (Int_t i = 0; i < fEntries; i++) {
322 b->Add(fArray->At(i),Form("%9.9d - %9.9d", fLowerLimits[i], fUpperLimits[i]));
324 TIter next(fDefaultList);
326 while((obj = next())) b->Add(obj);