Fix for bug#78633
[u/mrichter/AliRoot.git] / OADB / AliOADBContainer.cxx
CommitLineData
cc336771 1/**************************************************************************
2 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
16/* $Id$ */
17
18//-------------------------------------------------------------------------
19// Offline Analysis Database Container and Service Class
20// Author: Andreas Morsch, CERN
21//-------------------------------------------------------------------------
22
23
24
25
26#include "AliOADBContainer.h"
27#include "AliLog.h"
28#include <TObjArray.h>
29#include <TArrayI.h>
30#include <TFile.h>
bad1b400 31#include <TList.h>
cc336771 32
33ClassImp(AliOADBContainer);
34
35//______________________________________________________________________________
36AliOADBContainer::AliOADBContainer() :
37 TNamed(),
38 fArray(new TObjArray(100)),
bad1b400 39 fDefaultList(new TList()),
cc336771 40 fLowerLimits(),
41 fUpperLimits(),
42 fEntries(0)
43{
58a28442 44 // Default constructor
cc336771 45}
46
47AliOADBContainer::AliOADBContainer(char* name) :
48 TNamed(name, "OADBContainer"),
49 fArray(new TObjArray(100)),
bad1b400 50 fDefaultList(new TList()),
cc336771 51 fLowerLimits(),
52 fUpperLimits(),
53 fEntries(0)
54{
58a28442 55 // Constructor
cc336771 56}
57
58
59//______________________________________________________________________________
60AliOADBContainer::~AliOADBContainer()
61{
62 // destructor
63}
64
65//______________________________________________________________________________
66AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
67 TNamed(cont),
68 fArray(cont.fArray),
bad1b400 69 fDefaultList(cont.fDefaultList),
cc336771 70 fLowerLimits(cont.fLowerLimits),
71 fUpperLimits(cont.fUpperLimits),
72 fEntries(cont.fEntries)
73{
74 // Copy constructor.
75}
76
77//______________________________________________________________________________
78AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
79{
58a28442 80 //
cc336771 81 // Assignment operator
58a28442 82 // Copy objects related to run ranges
cc336771 83 if(this!=&cont) {
84 TNamed::operator=(cont);
35f37af6 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);
92 }
cc336771 93 }
58a28442 94 //
95 // Copy default objects
96 TList* list = cont.GetDefaultList();
97 TIter next(list);
98 TObject* obj;
99 while((obj = next())) fDefaultList->Add(obj);
100 //
cc336771 101 return *this;
102}
103
104void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
105{
58a28442 106 //
cc336771 107 // Append a new object to the list
58a28442 108 //
109 // Check that there is no overlap with existing run ranges
110 Int_t index = HasOverlap(lower, upper);
111
112 if (index != -1) {
113 AliFatal(Form("Ambiguos validity range (%5d) !\n", index));
114 return;
115 }
116 //
117 // Adjust arrays
cc336771 118 fEntries++;
119 fLowerLimits.Set(fEntries);
120 fUpperLimits.Set(fEntries);
121
58a28442 122 // Add the object
cc336771 123 fLowerLimits[fEntries - 1] = lower;
124 fUpperLimits[fEntries - 1] = upper;
cc336771 125 fArray->Add(obj);
126}
127
128void AliOADBContainer::RemoveObject(Int_t idx)
129{
58a28442 130 //
cc336771 131 // Remove object from the list
58a28442 132
133 //
134 // Check that index is inside range
cc336771 135 if (idx < 0 || idx >= fEntries)
136 {
137 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
138 return;
139 }
58a28442 140 //
141 // Remove the object
cc336771 142 TObject* obj = fArray->RemoveAt(idx);
143 delete obj;
58a28442 144 //
145 // Adjust the run ranges and shrink the array
cc336771 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);
150 }
151 fArray->RemoveAt(fEntries - 1);
152 fEntries--;
153}
154
155void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
156{
58a28442 157 //
cc336771 158 // Append a new object to the list
58a28442 159
160 // Check that index is inside range
cc336771 161 if (idx < 0 || idx >= fEntries)
162 {
163 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
164 return;
165 }
58a28442 166 //
167 // Check that there is no overlap with existing run ranges
168 Int_t index = HasOverlap(lower, upper);
169 if (index != -1) {
170 AliFatal(Form("Ambiguos validity range (%5d) !\n", index));
171 return;
172 }
173 //
174 // Add object
cc336771 175 fLowerLimits[idx] = lower;
176 fUpperLimits[idx] = upper;
177 fArray->AddAt(obj, idx);
178}
58a28442 179
180
bad1b400 181void AliOADBContainer::AddDefaultObject(TNamed* obj)
182{
183 // Add a default object
184 fDefaultList->Add(obj);
185}
186
187void AliOADBContainer::CleanDefaultList()
188{
189 // Clean default list
190 fDefaultList->Delete();
191}
192
35f37af6 193Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
cc336771 194{
bad1b400 195 //
cc336771 196 // Find the index for a given run
197 Int_t found = 0;
198 Int_t index = -1;
199 for (Int_t i = 0; i < fEntries; i++)
200 {
201 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
202 {
203 found++;
204 index = i;
205 }
206 }
bad1b400 207
208 if (found > 1) {
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));
212 }
213
cc336771 214 return index;
215}
216
bad1b400 217TObject* AliOADBContainer::GetObject(Int_t run, char* def) const
218{
219 // Return object for given run or default if not found
220 TObject* obj = 0;
221 Int_t idx = GetIndexForRun(run);
222 if (idx == -1) {
223 // no object found, try default
224 obj = fDefaultList->FindObject(def);
225 if (!obj) {
226 AliError("Default Object not found !\n");
227 return (0);
228 } else {
229 return (obj);
230 }
231 } else {
232 return (fArray->At(idx));
233 }
234}
235
236TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
237{
238 // Return object for given index
239 return (fArray->At(run));
240}
241
35f37af6 242void AliOADBContainer::WriteToFile(char* fname) const
cc336771 243{
bad1b400 244 //
cc336771 245 // Write object to file
246 TFile* f = new TFile(fname, "recreate");
247 Write();
248 f->Close();
249}
bad1b400 250
35f37af6 251Int_t AliOADBContainer::InitFromFile(char* fname, char* key)
252{
bad1b400 253 //
35f37af6 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);
259 if (!cont)
260 {
261 AliError("Object not found in file \n");
262 return 1;
263 }
264
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);
bad1b400 271 fArray->AddAt(cont->GetObjectByIndex(i), i);
35f37af6 272 }
58a28442 273
274 TIter next(cont->GetDefaultList());
275 TObject* obj;
276 while((obj = next())) fDefaultList->Add(obj);
277
35f37af6 278 return 0;
279
280}
281
cc336771 282
283void AliOADBContainer::List()
284{
bad1b400 285 //
cc336771 286 // List Objects
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();
290 }
58a28442 291 TIter next(fDefaultList);
292 TObject* obj;
293 while((obj = next())) obj->Dump();
294
295}
296
297Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper)
298{
299 //
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]))
304 {
305 return (i);
306 }
307 }
308 return (-1);
cc336771 309}