]> git.uio.no Git - u/mrichter/AliRoot.git/blob - OADB/AliOADBContainer.cxx
new reconstruction with pass0 calibration
[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 #include "TBrowser.h"
33
34 ClassImp(AliOADBContainer);
35
36 //______________________________________________________________________________
37 AliOADBContainer::AliOADBContainer() : 
38   TNamed(),
39   fArray(0),
40   fDefaultList(0),
41   fLowerLimits(),
42   fUpperLimits(),
43   fEntries(0)
44 {
45   // Default constructor
46 }
47
48 AliOADBContainer::AliOADBContainer(char* name) : 
49   TNamed(name, "OADBContainer"),
50   fArray(new TObjArray(100)),
51   fDefaultList(new TList()),
52   fLowerLimits(),
53   fUpperLimits(),
54   fEntries(0)
55 {
56   // Constructor
57 }
58
59
60 //______________________________________________________________________________
61 AliOADBContainer::~AliOADBContainer() 
62 {
63   // destructor
64 }
65
66 //______________________________________________________________________________
67 AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
68   TNamed(cont),
69   fArray(cont.fArray),
70   fDefaultList(cont.fDefaultList),
71   fLowerLimits(cont.fLowerLimits),
72   fUpperLimits(cont.fUpperLimits),
73   fEntries(cont.fEntries)
74 {
75   // Copy constructor.
76 }
77
78 //______________________________________________________________________________
79 AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
80 {
81   //
82   // Assignment operator
83   // Copy objects related to run ranges
84   if(this!=&cont) {
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);
93     }
94   }
95   //
96   // Copy default objects
97   TList* list = cont.GetDefaultList();
98   TIter next(list);
99   TObject* obj;
100   while((obj = next())) fDefaultList->Add(obj);
101   //
102   return *this;
103 }
104
105 void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
106 {
107   //
108   // Append a new object to the list 
109   //
110   // Check that there is no overlap with existing run ranges
111   Int_t index = HasOverlap(lower, upper);
112   
113   if (index != -1) {
114     AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
115     return;
116   }
117   //
118   // Adjust arrays
119   fEntries++;
120   fLowerLimits.Set(fEntries);
121   fUpperLimits.Set(fEntries);
122
123   // Add the object
124   fLowerLimits[fEntries - 1] = lower;
125   fUpperLimits[fEntries - 1] = upper;
126   fArray->Add(obj);
127 }
128
129 void AliOADBContainer::RemoveObject(Int_t idx)
130 {
131   //
132   // Remove object from the list 
133
134   //
135   // Check that index is inside range 
136   if (idx < 0 || idx >= fEntries) 
137     {
138       AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
139       return;
140     }
141   //
142   // Remove the object
143   TObject* obj = fArray->RemoveAt(idx);
144   delete obj;
145   //
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);
151   }
152   fArray->RemoveAt(fEntries - 1);
153   fEntries--;
154 }
155
156 void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
157 {
158   //
159   // Append a new object to the list 
160   
161   // Check that index is inside range
162   if (idx < 0 || idx >= fEntries) 
163     {
164       AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
165       return;
166     }
167   //
168   // Remove the old object
169   RemoveObject(idx);
170
171   // Check that there is no overlap with existing run ranges  
172   Int_t index = HasOverlap(lower, upper);
173   if (index != -1) {
174     AliFatal(Form("Ambiguos validity range (%5d) !\n", index));
175     return;
176   }
177   //
178   // Add object
179   fLowerLimits[idx] = lower;
180   fUpperLimits[idx] = upper;
181   fArray->AddAt(obj, idx);
182 }
183     
184  
185 void  AliOADBContainer::AddDefaultObject(TObject* obj)
186 {
187   // Add a default object
188   fDefaultList->Add(obj);
189 }
190
191 void  AliOADBContainer::CleanDefaultList()
192 {
193   // Clean default list
194   fDefaultList->Delete();
195 }
196
197 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
198 {
199   //
200   // Find the index for a given run 
201   Int_t found = 0;
202   Int_t index = -1;
203   for (Int_t i = 0; i < fEntries; i++) 
204     {
205       if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
206         {
207           found++;
208           index = i;
209         }
210     }
211
212   if (found > 1) {
213     AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
214   } else if (index == -1) {
215     AliWarning(Form("No object found for run %5d !\n", run));
216   }
217   
218   return index;
219 }
220
221 TObject* AliOADBContainer::GetObject(Int_t run, char* def) const
222 {
223   // Return object for given run or default if not found
224   TObject* obj = 0;
225   Int_t idx = GetIndexForRun(run);
226   if (idx == -1) {
227     // no object found, try default
228     obj = fDefaultList->FindObject(def);
229     if (!obj) {
230       AliError("Default Object not found !\n");
231       return (0);
232     } else {
233       return (obj);
234     }
235   } else {
236     return (fArray->At(idx));
237   }
238 }
239
240 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
241 {
242   // Return object for given index
243   return (fArray->At(run));
244 }
245
246 void AliOADBContainer::WriteToFile(char* fname) const
247 {
248   //
249   // Write object to file
250   TFile* f = new TFile(fname, "update");
251   Write();
252   f->Purge();
253   f->Close();
254 }
255
256 Int_t AliOADBContainer::InitFromFile(char* fname, char* key)
257 {
258     // 
259     // Initialize object from file
260     TFile* file = TFile::Open(fname);
261     if (!file) return (1);
262     AliOADBContainer* cont  = 0;
263     file->GetObject(key, cont);
264     if (!cont)
265     {
266         AliError("Object not found in file \n");        
267         return 1;
268     }
269
270     SetName(cont->GetName());
271     SetTitle(cont->GetTitle());
272
273     fEntries = cont->GetNumberOfEntries();
274     fLowerLimits.Set(fEntries);
275     fUpperLimits.Set(fEntries);
276     for (Int_t i = 0; i < fEntries; i++) {
277         fLowerLimits[i] = cont->LowerLimit(i); 
278         fUpperLimits[i] = cont->UpperLimit(i);
279         fArray->AddAt(cont->GetObjectByIndex(i), i);
280     }
281     if (!fDefaultList) fDefaultList = new TList(); 
282     TIter next(cont->GetDefaultList());
283     TObject* obj;
284     while((obj = next())) fDefaultList->Add(obj);
285
286     return 0;
287     
288 }
289
290
291 void AliOADBContainer::List()
292 {
293   //
294   // List Objects
295   for (Int_t i = 0; i < fEntries; i++) {
296     printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
297     (fArray->At(i))->Dump();
298   }
299   TIter next(fDefaultList);
300   TObject* obj;
301   while((obj = next())) obj->Dump();
302
303 }
304
305 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const
306 {
307   //
308   // Checks for overlpapping validity regions
309   for (Int_t i = 0; i < fEntries; i++) {
310     if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
311         (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))
312       {
313         return (i);
314       }
315   }
316   return (-1);
317 }
318
319 void AliOADBContainer::Browse(TBrowser *b)
320 {
321    // Browse this object.
322    // If b=0, there is no Browse call TObject::Browse(0) instead.
323    //         This means TObject::Inspect() will be invoked indirectly
324
325
326   if (b) {
327     for (Int_t i = 0; i < fEntries; i++) {
328       b->Add(fArray->At(i),Form("%9.9d - %9.9d", fLowerLimits[i], fUpperLimits[i]));
329     }
330     TIter next(fDefaultList);
331     TObject* obj;
332     while((obj = next())) b->Add(obj);
333         
334   }     
335    else
336       TObject::Browse(b);
337 }
338