]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEERBase/AliOADBContainer.cxx
Patch that prints the name of the OADB object
[u/mrichter/AliRoot.git] / STEER / STEERBase / 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 #include "AliOADBContainer.h"
24 #include "AliLog.h"
25 #include <TObjArray.h>
26 #include <TArrayI.h>
27 #include <TFile.h>
28 #include <TList.h>
29 #include <TBrowser.h>
30 #include <TSystem.h>
31 #include <TError.h>
32
33 ClassImp(AliOADBContainer);
34
35 //______________________________________________________________________________
36 AliOADBContainer::AliOADBContainer() : 
37   TNamed(),
38   fArray(0),
39   fDefaultList(0),
40   fLowerLimits(),
41   fUpperLimits(),
42   fEntries(0)
43 {
44   // Default constructor
45 }
46
47 AliOADBContainer::AliOADBContainer(const 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, %5.5d-%5.5d) !\n", index,lower,upper));
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   // Update an existing object, at a given position 
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   // Remove the old object and reset the range
168   //  TObject* obj2 = 
169   fArray->RemoveAt(idx);
170   // don't delete it: if you are updating it may be pointing to the same location of obj...
171   //  delete obj2;
172   fLowerLimits[idx] = -1;
173   fUpperLimits[idx] = -1;
174   // Check that there is no overlap with existing run ranges  
175   Int_t index = HasOverlap(lower, upper);
176   if (index != -1) {
177     AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
178     return;
179   }
180   //
181   // Add object at the same position
182   //printf("idx %d obj %llx\n", idx, obj);
183   fLowerLimits[idx] = lower;
184   fUpperLimits[idx] = upper;
185   fArray->AddAt(obj, idx);
186
187 }
188  
189 void  AliOADBContainer::AddDefaultObject(TObject* obj)
190 {
191   // Add a default object
192   fDefaultList->Add(obj);
193 }
194
195 void  AliOADBContainer::CleanDefaultList()
196 {
197   // Clean default list
198   fDefaultList->Delete();
199 }
200
201 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
202 {
203   //
204   // Find the index for a given run 
205   
206   Int_t found = 0;
207   Int_t index = -1;
208   for (Int_t i = 0; i < fEntries; i++) 
209     {
210       if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
211         {
212           found++;
213           index = i;
214         }
215     }
216
217   if (found > 1) {
218     AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
219   } else if (index == -1) {
220     AliWarning(Form("No object (%s) found for run %5d !\n", GetName(), run));
221   }
222   
223   return index;
224 }
225
226 TObject* AliOADBContainer::GetObject(Int_t run, const char* def) const
227 {
228   // Return object for given run or default if not found
229   TObject* obj = 0;
230   Int_t idx = GetIndexForRun(run);
231   if (idx == -1) {
232     // no object found, try default
233     obj = fDefaultList->FindObject(def);
234     if (!obj) {
235       AliError(Form("Default Object (%s) not found !\n", GetName()));
236       return (0);
237     } else {
238       return (obj);
239     }
240   } else {
241     return (fArray->At(idx));
242   }
243 }
244
245 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
246 {
247   // Return object for given index
248   return (fArray->At(run));
249 }
250
251 void AliOADBContainer::WriteToFile(const char* fname) const
252 {
253   //
254   // Write object to file
255   TFile* f = new TFile(fname, "update");
256   Write();
257   f->Purge();
258   f->Close();
259 }
260
261 Int_t AliOADBContainer::InitFromFile(const char* fname, const char* key)
262 {
263     // 
264     // Initialize object from file
265     TFile* file = TFile::Open(fname);
266     if (!file) return (1);
267     AliOADBContainer* cont  = 0;
268     file->GetObject(key, cont);
269     if (!cont)
270     {
271       AliError(Form("Object (%s) not found in file \n", GetName()));    
272         return 1;
273     }
274
275     SetName(cont->GetName());
276     SetTitle(cont->GetTitle());
277
278     fEntries = cont->GetNumberOfEntries();
279     fLowerLimits.Set(fEntries);
280     fUpperLimits.Set(fEntries);
281     if(fEntries > fArray->GetSize()) fArray->Expand(fEntries);
282
283     for (Int_t i = 0; i < fEntries; i++) {
284         fLowerLimits[i] = cont->LowerLimit(i); 
285         fUpperLimits[i] = cont->UpperLimit(i);
286         fArray->AddAt(cont->GetObjectByIndex(i), i);
287     }
288     if (!fDefaultList) fDefaultList = new TList(); 
289     TIter next(cont->GetDefaultList());
290     TObject* obj;
291     while((obj = next())) fDefaultList->Add(obj);
292
293     return 0;
294     
295 }
296
297
298 void AliOADBContainer::List()
299 {
300   //
301   // List Objects
302   printf("Entries %d\n", fEntries);
303   
304   for (Int_t i = 0; i < fEntries; i++) {
305     printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
306     (fArray->At(i))->Dump();
307   }
308   TIter next(fDefaultList);
309   TObject* obj;
310   while((obj = next())) obj->Dump();
311
312 }
313
314 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const
315 {
316   //
317   // Checks for overlpapping validity regions
318   for (Int_t i = 0; i < fEntries; i++) {
319     if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
320         (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))
321       {
322         return (i);
323       }
324   }
325   return (-1);
326 }
327
328 void AliOADBContainer::Browse(TBrowser *b)
329 {
330    // Browse this object.
331    // If b=0, there is no Browse call TObject::Browse(0) instead.
332    //         This means TObject::Inspect() will be invoked indirectly
333
334
335   if (b) {
336     for (Int_t i = 0; i < fEntries; i++) {
337       b->Add(fArray->At(i),Form("%9.9d - %9.9d", fLowerLimits[i], fUpperLimits[i]));
338     }
339     TIter next(fDefaultList);
340     TObject* obj;
341     while((obj = next())) b->Add(obj);
342         
343   }     
344    else
345       TObject::Browse(b);
346 }
347
348 //______________________________________________________________________________
349 const char* AliOADBContainer::GetOADBPath()
350 {
351 // returns the path of the OADB
352 // this static function just depends on environment variables
353
354    static TString oadbPath;
355
356    if (gSystem->Getenv("OADB_PATH"))
357       oadbPath = gSystem->Getenv("OADB_PATH");
358    else if (gSystem->Getenv("ALICE_ROOT"))
359       oadbPath.Form("%s/OADB", gSystem->Getenv("ALICE_ROOT"));
360    else
361    ::Fatal("AliAnalysisManager::GetOADBPath", "Cannot figure out AODB path. Define ALICE_ROOT or OADB_PATH!");
362    return oadbPath;
363 }