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