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>
33 ClassImp(AliOADBContainer);
35 //______________________________________________________________________________
36 AliOADBContainer::AliOADBContainer() :
38 fArray(new TObjArray(100)),
39 fDefaultList(new TList()),
44 // Default constructor
47 AliOADBContainer::AliOADBContainer(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) !\n", index));
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 // Append a new object to the list
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 // Check that there is no overlap with existing run ranges
168 Int_t index = HasOverlap(lower, upper);
170 AliFatal(Form("Ambiguos validity range (%5d) !\n", index));
175 fLowerLimits[idx] = lower;
176 fUpperLimits[idx] = upper;
177 fArray->AddAt(obj, idx);
181 void AliOADBContainer::AddDefaultObject(TNamed* obj)
183 // Add a default object
184 fDefaultList->Add(obj);
187 void AliOADBContainer::CleanDefaultList()
189 // Clean default list
190 fDefaultList->Delete();
193 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
196 // Find the index for a given run
199 for (Int_t i = 0; i < fEntries; i++)
201 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
209 AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
210 } else if (index == -1) {
211 AliWarning(Form("No object found for run %5d !\n", run));
217 TObject* AliOADBContainer::GetObject(Int_t run, char* def) const
219 // Return object for given run or default if not found
221 Int_t idx = GetIndexForRun(run);
223 // no object found, try default
224 obj = fDefaultList->FindObject(def);
226 AliError("Default Object not found !\n");
232 return (fArray->At(idx));
236 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
238 // Return object for given index
239 return (fArray->At(run));
242 void AliOADBContainer::WriteToFile(char* fname) const
245 // Write object to file
246 TFile* f = new TFile(fname, "recreate");
251 Int_t AliOADBContainer::InitFromFile(char* fname, char* key)
254 // Initialize object from file
255 TFile* file = TFile::Open(fname);
256 if (!file) return (1);
257 AliOADBContainer* cont = 0;
258 file->GetObject(key, cont);
261 AliError("Object not found in file \n");
265 fEntries = cont->GetNumberOfEntries();
266 fLowerLimits.Set(fEntries);
267 fUpperLimits.Set(fEntries);
268 for (Int_t i = 0; i < fEntries; i++) {
269 fLowerLimits[i] = cont->LowerLimit(i);
270 fUpperLimits[i] = cont->UpperLimit(i);
271 fArray->AddAt(cont->GetObjectByIndex(i), i);
274 TIter next(cont->GetDefaultList());
276 while((obj = next())) fDefaultList->Add(obj);
283 void AliOADBContainer::List()
287 for (Int_t i = 0; i < fEntries; i++) {
288 printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
289 (fArray->At(i))->Dump();
291 TIter next(fDefaultList);
293 while((obj = next())) obj->Dump();
297 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper)
300 // Checks for overlpapping validity regions
301 for (Int_t i = 0; i < fEntries; i++) {
302 if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
303 (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))