1 /**************************************************************************
2 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 //-------------------------------------------------------------------------
19 // Offline Analysis Database Container and Service Class
20 // Author: Andreas Morsch, CERN
21 //-------------------------------------------------------------------------
23 #include "AliOADBContainer.h"
25 #include <TObjArray.h>
34 ClassImp(AliOADBContainer);
36 //______________________________________________________________________________
37 AliOADBContainer::AliOADBContainer() :
45 // Default constructor
48 AliOADBContainer::AliOADBContainer(const char* name) :
49 TNamed(name, "OADBContainer"),
50 fArray(new TObjArray(100)),
51 fDefaultList(new TList()),
60 //______________________________________________________________________________
61 AliOADBContainer::~AliOADBContainer()
64 if (fArray) delete fArray;
65 if (fDefaultList) delete fDefaultList;
69 //______________________________________________________________________________
70 AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
73 fDefaultList(cont.fDefaultList),
74 fLowerLimits(cont.fLowerLimits),
75 fUpperLimits(cont.fUpperLimits),
76 fEntries(cont.fEntries)
81 //______________________________________________________________________________
82 AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
85 // Assignment operator
86 // Copy objects related to run ranges
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);
99 // Copy default objects
100 TList* list = cont.GetDefaultList();
103 while((obj = next())) fDefaultList->Add(obj);
108 void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
111 // Append a new object to the list
113 // Check that there is no overlap with existing run ranges
114 Int_t index = HasOverlap(lower, upper);
117 AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
123 fLowerLimits.Set(fEntries);
124 fUpperLimits.Set(fEntries);
127 fLowerLimits[fEntries - 1] = lower;
128 fUpperLimits[fEntries - 1] = upper;
132 void AliOADBContainer::RemoveObject(Int_t idx)
135 // Remove object from the list
138 // Check that index is inside range
139 if (idx < 0 || idx >= fEntries)
141 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
146 TObject* obj = fArray->RemoveAt(idx);
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);
155 fArray->RemoveAt(fEntries - 1);
159 void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
162 // Update an existing object, at a given position
164 // Check that index is inside range
165 if (idx < 0 || idx >= fEntries)
167 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
171 // Remove the old object and reset the range
173 fArray->RemoveAt(idx);
174 // don't delete it: if you are updating it may be pointing to the same location of obj...
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);
181 AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
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);
193 void AliOADBContainer::AddDefaultObject(TObject* obj)
195 // Add a default object
196 fDefaultList->Add(obj);
199 void AliOADBContainer::CleanDefaultList()
201 // Clean default list
202 fDefaultList->Delete();
205 Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
208 // Find the index for a given run
212 for (Int_t i = 0; i < fEntries; i++)
214 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
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));
230 TObject* AliOADBContainer::GetObject(Int_t run, const char* def) const
232 // Return object for given run or default if not found
234 Int_t idx = GetIndexForRun(run);
236 // no object found, try default
237 obj = fDefaultList->FindObject(def);
239 AliError(Form("Default Object (%s) not found !\n", GetName()));
245 return (fArray->At(idx));
249 TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
251 // Return object for given index
252 return (fArray->At(run));
255 void AliOADBContainer::WriteToFile(const char* fname) const
258 // Write object to file
259 TFile* f = new TFile(fname, "update");
265 Int_t AliOADBContainer::InitFromFile(const char* fname, const char* key)
268 // Hans: See whether the file is already open
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());
276 TObject *obj = nextFile();
278 printf("%s\n",obj->GetName());
280 printf("-----------------------------------------------\n");
284 // Try to get the file from the list of already open files
285 const TSeqCollection *listOfFiles(gROOT->GetListOfFiles());
287 file =dynamic_cast<TFile*> (listOfFiles->FindObject(fname));
290 printf("Success! File was already open!\n");
293 printf("Couldn't find file, opening it\n");
294 file = TFile::Open(fname);
298 // Initialize object from file
299 if (!file) return (1);
300 AliOADBContainer* cont = 0;
301 file->GetObject(key, cont);
304 AliError(Form("Object (%s) not found in file \n", GetName()));
308 SetName(cont->GetName());
309 SetTitle(cont->GetTitle());
311 fEntries = cont->GetNumberOfEntries();
312 fLowerLimits.Set(fEntries);
313 fUpperLimits.Set(fEntries);
314 if(fEntries > fArray->GetSize()) fArray->Expand(fEntries);
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);
321 if (!fDefaultList) fDefaultList = new TList();
322 TIter next(cont->GetDefaultList());
324 while((obj = next())) fDefaultList->Add(obj);
331 void AliOADBContainer::List()
335 printf("Entries %d\n", fEntries);
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();
341 TIter next(fDefaultList);
343 while((obj = next())) obj->Dump();
347 Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const
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]))
361 void AliOADBContainer::Browse(TBrowser *b)
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
369 for (Int_t i = 0; i < fEntries; i++) {
370 b->Add(fArray->At(i),Form("%9.9d - %9.9d", fLowerLimits[i], fUpperLimits[i]));
372 TIter next(fDefaultList);
374 while((obj = next())) b->Add(obj);
381 //______________________________________________________________________________
382 const char* AliOADBContainer::GetOADBPath()
384 // returns the path of the OADB
385 // this static function just depends on environment variables
387 static TString oadbPath;
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"));
394 ::Fatal("AliAnalysisManager::GetOADBPath", "Cannot figure out AODB path. Define ALICE_ROOT or OADB_PATH!");