]> git.uio.no Git - u/mrichter/AliRoot.git/blob - OADB/AliOADBContainer.cxx
Coverity fixes.
[u/mrichter/AliRoot.git] / OADB / AliOADBContainer.cxx
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>
31 #include <TList.h>
32
33 ClassImp(AliOADBContainer);
34
35 //______________________________________________________________________________
36 AliOADBContainer::AliOADBContainer() : 
37   TNamed(),
38   fArray(new TObjArray(100)),
39   fDefaultList(new TList()),
40   fLowerLimits(),
41   fUpperLimits(),
42   fEntries(0)
43 {
44   // Default constructor
45 }
46
47 AliOADBContainer::AliOADBContainer(char* name) : 
48   TNamed(name, "OADBContainer"),
49   fArray(new TObjArray(100)),
50   fDefaultList(new TList()),
51   fLowerLimits(),
52   fUpperLimits(),
53   fEntries(0)
54 {
55   // Constructor
56 }
57
58
59 //______________________________________________________________________________
60 AliOADBContainer::~AliOADBContainer() 
61 {
62   // destructor
63 }
64
65 //______________________________________________________________________________
66 AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
67   TNamed(cont),
68   fArray(cont.fArray),
69   fDefaultList(cont.fDefaultList),
70   fLowerLimits(cont.fLowerLimits),
71   fUpperLimits(cont.fUpperLimits),
72   fEntries(cont.fEntries)
73 {
74   // Copy constructor.
75 }
76
77 //______________________________________________________________________________
78 AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
79 {
80   //
81   // Assignment operator
82   // Copy objects related to run ranges
83   if(this!=&cont) {
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);
92     }
93   }
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   //
101   return *this;
102 }
103
104 void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
105 {
106   //
107   // Append a new object to the list 
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
118   fEntries++;
119   fLowerLimits.Set(fEntries);
120   fUpperLimits.Set(fEntries);
121
122   // Add the object
123   fLowerLimits[fEntries - 1] = lower;
124   fUpperLimits[fEntries - 1] = upper;
125   fArray->Add(obj);
126 }
127
128 void AliOADBContainer::RemoveObject(Int_t idx)
129 {
130   //
131   // Remove object from the list 
132
133   //
134   // Check that index is inside range 
135   if (idx < 0 || idx >= fEntries) 
136     {
137       AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
138       return;
139     }
140   //
141   // Remove the object
142   TObject* obj = fArray->RemoveAt(idx);
143   delete obj;
144   //
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);
150   }
151   fArray->RemoveAt(fEntries - 1);
152   fEntries--;
153 }
154
155 void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
156 {
157   //
158   // Append a new object to the list 
159   
160   // Check that index is inside range
161   if (idx < 0 || idx >= fEntries) 
162     {
163       AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
164       return;
165     }
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
175   fLowerLimits[idx] = lower;
176   fUpperLimits[idx] = upper;
177   fArray->AddAt(obj, idx);
178 }
179     
180  
181 void  AliOADBContainer::AddDefaultObject(TNamed* obj)
182 {
183   // Add a default object
184   fDefaultList->Add(obj);
185 }
186
187 void  AliOADBContainer::CleanDefaultList()
188 {
189   // Clean default list
190   fDefaultList->Delete();
191 }
192
193 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
194 {
195   //
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     }
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   
214   return index;
215 }
216
217 TObject* 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
236 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
237 {
238   // Return object for given index
239   return (fArray->At(run));
240 }
241
242 void AliOADBContainer::WriteToFile(char* fname) const
243 {
244   //
245   // Write object to file
246   TFile* f = new TFile(fname, "recreate");
247   Write();
248   f->Close();
249 }
250
251 Int_t AliOADBContainer::InitFromFile(char* fname, char* key)
252 {
253     // 
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);
271         fArray->AddAt(cont->GetObjectByIndex(i), i);
272     }
273
274     TIter next(cont->GetDefaultList());
275     TObject* obj;
276     while((obj = next())) fDefaultList->Add(obj);
277
278     return 0;
279     
280 }
281
282
283 void AliOADBContainer::List()
284 {
285   //
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   }
291   TIter next(fDefaultList);
292   TObject* obj;
293   while((obj = next())) obj->Dump();
294
295 }
296
297 Int_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);
309 }