]>
Commit | Line | Data |
---|---|---|
cc336771 | 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 | ||
cc336771 | 23 | #include "AliOADBContainer.h" |
24 | #include "AliLog.h" | |
25 | #include <TObjArray.h> | |
26 | #include <TArrayI.h> | |
27 | #include <TFile.h> | |
bad1b400 | 28 | #include <TList.h> |
60cfe266 | 29 | #include <TBrowser.h> |
30 | #include <TSystem.h> | |
31 | #include <TError.h> | |
e7d0c332 | 32 | #include <TROOT.h> |
cc336771 | 33 | |
34 | ClassImp(AliOADBContainer); | |
35 | ||
36 | //______________________________________________________________________________ | |
37 | AliOADBContainer::AliOADBContainer() : | |
38 | TNamed(), | |
63671ce7 | 39 | fArray(0), |
40 | fDefaultList(0), | |
cc336771 | 41 | fLowerLimits(), |
42 | fUpperLimits(), | |
43 | fEntries(0) | |
44 | { | |
58a28442 | 45 | // Default constructor |
cc336771 | 46 | } |
47 | ||
0add5ddf | 48 | AliOADBContainer::AliOADBContainer(const char* name) : |
cc336771 | 49 | TNamed(name, "OADBContainer"), |
50 | fArray(new TObjArray(100)), | |
bad1b400 | 51 | fDefaultList(new TList()), |
cc336771 | 52 | fLowerLimits(), |
53 | fUpperLimits(), | |
54 | fEntries(0) | |
55 | { | |
58a28442 | 56 | // Constructor |
cc336771 | 57 | } |
58 | ||
59 | ||
60 | //______________________________________________________________________________ | |
61 | AliOADBContainer::~AliOADBContainer() | |
62 | { | |
63 | // destructor | |
2a210779 | 64 | if (fArray) delete fArray; |
65 | if (fDefaultList) delete fDefaultList; | |
66 | ||
cc336771 | 67 | } |
68 | ||
69 | //______________________________________________________________________________ | |
70 | AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) : | |
71 | TNamed(cont), | |
72 | fArray(cont.fArray), | |
bad1b400 | 73 | fDefaultList(cont.fDefaultList), |
cc336771 | 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 | { | |
58a28442 | 84 | // |
cc336771 | 85 | // Assignment operator |
58a28442 | 86 | // Copy objects related to run ranges |
cc336771 | 87 | if(this!=&cont) { |
88 | TNamed::operator=(cont); | |
35f37af6 | 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 | } | |
cc336771 | 97 | } |
58a28442 | 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 | // | |
cc336771 | 105 | return *this; |
106 | } | |
107 | ||
108 | void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper) | |
109 | { | |
58a28442 | 110 | // |
cc336771 | 111 | // Append a new object to the list |
58a28442 | 112 | // |
113 | // Check that there is no overlap with existing run ranges | |
114 | Int_t index = HasOverlap(lower, upper); | |
115 | ||
116 | if (index != -1) { | |
dcfbfe28 | 117 | AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper)); |
58a28442 | 118 | return; |
119 | } | |
120 | // | |
121 | // Adjust arrays | |
cc336771 | 122 | fEntries++; |
123 | fLowerLimits.Set(fEntries); | |
124 | fUpperLimits.Set(fEntries); | |
125 | ||
58a28442 | 126 | // Add the object |
cc336771 | 127 | fLowerLimits[fEntries - 1] = lower; |
128 | fUpperLimits[fEntries - 1] = upper; | |
cc336771 | 129 | fArray->Add(obj); |
130 | } | |
131 | ||
132 | void AliOADBContainer::RemoveObject(Int_t idx) | |
133 | { | |
58a28442 | 134 | // |
cc336771 | 135 | // Remove object from the list |
58a28442 | 136 | |
137 | // | |
138 | // Check that index is inside range | |
cc336771 | 139 | if (idx < 0 || idx >= fEntries) |
140 | { | |
141 | AliError(Form("Index out of Range %5d >= %5d", idx, fEntries)); | |
142 | return; | |
143 | } | |
58a28442 | 144 | // |
145 | // Remove the object | |
cc336771 | 146 | TObject* obj = fArray->RemoveAt(idx); |
147 | delete obj; | |
58a28442 | 148 | // |
149 | // Adjust the run ranges and shrink the array | |
cc336771 | 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 | { | |
58a28442 | 161 | // |
0add5ddf | 162 | // Update an existing object, at a given position |
163 | ||
58a28442 | 164 | // Check that index is inside range |
cc336771 | 165 | if (idx < 0 || idx >= fEntries) |
166 | { | |
167 | AliError(Form("Index out of Range %5d >= %5d", idx, fEntries)); | |
168 | return; | |
169 | } | |
58a28442 | 170 | // |
0add5ddf | 171 | // Remove the old object and reset the range |
d0f63026 | 172 | // TObject* obj2 = |
173 | fArray->RemoveAt(idx); | |
e3c81f72 | 174 | // don't delete it: if you are updating it may be pointing to the same location of obj... |
0add5ddf | 175 | // delete obj2; |
176 | fLowerLimits[idx] = -1; | |
177 | fUpperLimits[idx] = -1; | |
58a28442 | 178 | // Check that there is no overlap with existing run ranges |
179 | Int_t index = HasOverlap(lower, upper); | |
180 | if (index != -1) { | |
9939b6b6 | 181 | AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper)); |
58a28442 | 182 | return; |
183 | } | |
184 | // | |
0add5ddf | 185 | // Add object at the same position |
e3c81f72 | 186 | //printf("idx %d obj %llx\n", idx, obj); |
cc336771 | 187 | fLowerLimits[idx] = lower; |
188 | fUpperLimits[idx] = upper; | |
189 | fArray->AddAt(obj, idx); | |
0add5ddf | 190 | |
cc336771 | 191 | } |
58a28442 | 192 | |
08b752d1 | 193 | void AliOADBContainer::AddDefaultObject(TObject* obj) |
bad1b400 | 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 | ||
35f37af6 | 205 | Int_t AliOADBContainer::GetIndexForRun(Int_t run) const |
cc336771 | 206 | { |
bad1b400 | 207 | // |
cc336771 | 208 | // Find the index for a given run |
0add5ddf | 209 | |
cc336771 | 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 | } | |
bad1b400 | 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) { | |
4dcd83a4 | 224 | AliWarning(Form("No object (%s) found for run %5d !\n", GetName(), run)); |
bad1b400 | 225 | } |
226 | ||
cc336771 | 227 | return index; |
228 | } | |
229 | ||
9939b6b6 | 230 | TObject* AliOADBContainer::GetObject(Int_t run, const char* def) const |
bad1b400 | 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) { | |
4dcd83a4 | 239 | AliError(Form("Default Object (%s) not found !\n", GetName())); |
bad1b400 | 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 | ||
0add5ddf | 255 | void AliOADBContainer::WriteToFile(const char* fname) const |
cc336771 | 256 | { |
bad1b400 | 257 | // |
cc336771 | 258 | // Write object to file |
a18817ac | 259 | TFile* f = new TFile(fname, "update"); |
cc336771 | 260 | Write(); |
a18817ac | 261 | f->Purge(); |
cc336771 | 262 | f->Close(); |
263 | } | |
bad1b400 | 264 | |
0add5ddf | 265 | Int_t AliOADBContainer::InitFromFile(const char* fname, const char* key) |
35f37af6 | 266 | { |
e7d0c332 | 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 | ||
bad1b400 | 297 | // |
35f37af6 | 298 | // Initialize object from file |
35f37af6 | 299 | if (!file) return (1); |
300 | AliOADBContainer* cont = 0; | |
301 | file->GetObject(key, cont); | |
302 | if (!cont) | |
303 | { | |
4dcd83a4 | 304 | AliError(Form("Object (%s) not found in file \n", GetName())); |
35f37af6 | 305 | return 1; |
306 | } | |
d40bb25d | 307 | |
308 | SetName(cont->GetName()); | |
309 | SetTitle(cont->GetTitle()); | |
310 | ||
35f37af6 | 311 | fEntries = cont->GetNumberOfEntries(); |
312 | fLowerLimits.Set(fEntries); | |
313 | fUpperLimits.Set(fEntries); | |
685113d4 | 314 | if(fEntries > fArray->GetSize()) fArray->Expand(fEntries); |
315 | ||
35f37af6 | 316 | for (Int_t i = 0; i < fEntries; i++) { |
317 | fLowerLimits[i] = cont->LowerLimit(i); | |
318 | fUpperLimits[i] = cont->UpperLimit(i); | |
bad1b400 | 319 | fArray->AddAt(cont->GetObjectByIndex(i), i); |
35f37af6 | 320 | } |
d40bb25d | 321 | if (!fDefaultList) fDefaultList = new TList(); |
58a28442 | 322 | TIter next(cont->GetDefaultList()); |
323 | TObject* obj; | |
324 | while((obj = next())) fDefaultList->Add(obj); | |
325 | ||
35f37af6 | 326 | return 0; |
327 | ||
328 | } | |
329 | ||
cc336771 | 330 | |
331 | void AliOADBContainer::List() | |
332 | { | |
bad1b400 | 333 | // |
cc336771 | 334 | // List Objects |
0add5ddf | 335 | printf("Entries %d\n", fEntries); |
336 | ||
cc336771 | 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 | } | |
58a28442 | 341 | TIter next(fDefaultList); |
342 | TObject* obj; | |
343 | while((obj = next())) obj->Dump(); | |
344 | ||
345 | } | |
346 | ||
63671ce7 | 347 | Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper) const |
58a28442 | 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); | |
cc336771 | 359 | } |
08b752d1 | 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 | ||
60cfe266 | 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 | } |