]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEERBase/AliOADBContainer.cxx
Merge branch 'TPCdev' of https://git.cern.ch/reps/AliRoot into TPCdev
[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 #include <TROOT.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(const 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   if (fArray)       delete fArray;
65   if (fDefaultList) delete fDefaultList;
66
67 }
68
69 //______________________________________________________________________________
70 AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
71   TNamed(cont),
72   fArray(cont.fArray),
73   fDefaultList(cont.fDefaultList),
74   fLowerLimits(cont.fLowerLimits),
75   fUpperLimits(cont.fUpperLimits),
76   fEntries(cont.fEntries)
77 {
78   // Copy constructor.
79 }
80
81 //______________________________________________________________________________
82 AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
83 {
84   //
85   // Assignment operator
86   // Copy objects related to run ranges
87   if(this!=&cont) {
88     TNamed::operator=(cont);
89     fEntries = cont.fEntries;
90     fLowerLimits.Set(fEntries);
91     fUpperLimits.Set(fEntries);
92     for (Int_t i = 0; i < fEntries; i++) {
93         fLowerLimits[i] = cont.fLowerLimits[i]; 
94         fUpperLimits[i] = cont.fUpperLimits[i];
95         fArray->AddAt(cont.fArray->At(i), i);
96     }
97   }
98   //
99   // Copy default objects
100   TList* list = cont.GetDefaultList();
101   TIter next(list);
102   TObject* obj;
103   while((obj = next())) fDefaultList->Add(obj);
104   //
105   return *this;
106 }
107
108 void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
109 {
110   //
111   // Append a new object to the list 
112   //
113   // Check that there is no overlap with existing run ranges
114   Int_t index = HasOverlap(lower, upper);
115   
116   if (index != -1) {
117     AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
118     return;
119   }
120   //
121   // Adjust arrays
122   fEntries++;
123   fLowerLimits.Set(fEntries);
124   fUpperLimits.Set(fEntries);
125
126   // Add the object
127   fLowerLimits[fEntries - 1] = lower;
128   fUpperLimits[fEntries - 1] = upper;
129   fArray->Add(obj);
130 }
131
132 void AliOADBContainer::RemoveObject(Int_t idx)
133 {
134   //
135   // Remove object from the list 
136
137   //
138   // Check that index is inside range 
139   if (idx < 0 || idx >= fEntries) 
140     {
141       AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
142       return;
143     }
144   //
145   // Remove the object
146   TObject* obj = fArray->RemoveAt(idx);
147   delete obj;
148   //
149   // Adjust the run ranges and shrink the array
150   for (Int_t i = idx; i < (fEntries-1); i++) {
151     fLowerLimits[i] = fLowerLimits[i + 1]; 
152     fUpperLimits[i] = fUpperLimits[i + 1];
153     fArray->AddAt(fArray->At(i+1), i);
154   }
155   fArray->RemoveAt(fEntries - 1);
156   fEntries--;
157 }
158
159 void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
160 {
161   //
162   // Update an existing object, at a given position 
163
164   // Check that index is inside range
165   if (idx < 0 || idx >= fEntries) 
166     {
167       AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
168       return;
169     }
170   //
171   // Remove the old object and reset the range
172   //  TObject* obj2 = 
173   fArray->RemoveAt(idx);
174   // don't delete it: if you are updating it may be pointing to the same location of obj...
175   //  delete obj2;
176   fLowerLimits[idx] = -1;
177   fUpperLimits[idx] = -1;
178   // Check that there is no overlap with existing run ranges  
179   Int_t index = HasOverlap(lower, upper);
180   if (index != -1) {
181     AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
182     return;
183   }
184   //
185   // Add object at the same position
186   //printf("idx %d obj %llx\n", idx, obj);
187   fLowerLimits[idx] = lower;
188   fUpperLimits[idx] = upper;
189   fArray->AddAt(obj, idx);
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 (%s) found for run %5d !\n", GetName(), 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(Form("Default Object (%s) not found !\n", GetName()));
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   // Hans: See whether the file is already open
269   //
270   // For now print info
271   printf("-----------------------------------------------\n");
272   printf("D-InitFromFile for file: %s and key %s\n",fname,key);
273   printf("D-List of already open files:\n");
274   TIter nextFile(gROOT->GetListOfFiles());
275   while (1) {
276     TObject *obj = nextFile();
277     if(!obj)break;
278     printf("%s\n",obj->GetName());
279   }
280   printf("-----------------------------------------------\n");
281
282   // Declare the file
283   TFile* file(0);
284   // Try to get the file from the list of already open files
285   const TSeqCollection *listOfFiles(gROOT->GetListOfFiles());
286   if(listOfFiles){
287     file =dynamic_cast<TFile*> (listOfFiles->FindObject(fname));
288   }
289   if(file){
290     printf("Success! File was already open!\n");
291   }
292   else{
293     printf("Couldn't find file, opening it\n");
294     file = TFile::Open(fname);
295   }
296
297     // 
298     // Initialize object from file
299     if (!file) return (1);
300     AliOADBContainer* cont  = 0;
301     file->GetObject(key, cont);
302     if (!cont)
303     {
304       AliError(Form("Object (%s) not found in file \n", GetName()));    
305         return 1;
306     }
307
308     SetName(cont->GetName());
309     SetTitle(cont->GetTitle());
310
311     fEntries = cont->GetNumberOfEntries();
312     fLowerLimits.Set(fEntries);
313     fUpperLimits.Set(fEntries);
314     if(fEntries > fArray->GetSize()) fArray->Expand(fEntries);
315
316     for (Int_t i = 0; i < fEntries; i++) {
317         fLowerLimits[i] = cont->LowerLimit(i); 
318         fUpperLimits[i] = cont->UpperLimit(i);
319         fArray->AddAt(cont->GetObjectByIndex(i), i);
320     }
321     if (!fDefaultList) fDefaultList = new TList(); 
322     TIter next(cont->GetDefaultList());
323     TObject* obj;
324     while((obj = next())) fDefaultList->Add(obj);
325
326     return 0;
327     
328 }
329
330
331 void AliOADBContainer::List()
332 {
333   //
334   // List Objects
335   printf("Entries %d\n", fEntries);
336   
337   for (Int_t i = 0; i < fEntries; i++) {
338     printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
339     (fArray->At(i))->Dump();
340   }
341   TIter next(fDefaultList);
342   TObject* obj;
343   while((obj = next())) obj->Dump();
344
345 }
346
347 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const
348 {
349   //
350   // Checks for overlpapping validity regions
351   for (Int_t i = 0; i < fEntries; i++) {
352     if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
353         (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))
354       {
355         return (i);
356       }
357   }
358   return (-1);
359 }
360
361 void AliOADBContainer::Browse(TBrowser *b)
362 {
363    // Browse this object.
364    // If b=0, there is no Browse call TObject::Browse(0) instead.
365    //         This means TObject::Inspect() will be invoked indirectly
366
367
368   if (b) {
369     for (Int_t i = 0; i < fEntries; i++) {
370       b->Add(fArray->At(i),Form("%9.9d - %9.9d", fLowerLimits[i], fUpperLimits[i]));
371     }
372     TIter next(fDefaultList);
373     TObject* obj;
374     while((obj = next())) b->Add(obj);
375         
376   }     
377    else
378       TObject::Browse(b);
379 }
380
381 //______________________________________________________________________________
382 const char* AliOADBContainer::GetOADBPath()
383 {
384 // returns the path of the OADB
385 // this static function just depends on environment variables
386
387    static TString oadbPath;
388
389    if (gSystem->Getenv("OADB_PATH"))
390       oadbPath = gSystem->Getenv("OADB_PATH");
391    else if (gSystem->Getenv("ALICE_ROOT"))
392       oadbPath.Form("%s/OADB", gSystem->Getenv("ALICE_ROOT"));
393    else
394    ::Fatal("AliAnalysisManager::GetOADBPath", "Cannot figure out AODB path. Define ALICE_ROOT or OADB_PATH!");
395    return oadbPath;
396 }