Line data Source code
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 : #include "TObjString.h"
34 :
35 176 : ClassImp(AliOADBContainer);
36 :
37 : //______________________________________________________________________________
38 : AliOADBContainer::AliOADBContainer() :
39 0 : TNamed(),
40 0 : fArray(0),
41 0 : fDefaultList(0),
42 0 : fPassNames(0),
43 0 : fLowerLimits(),
44 0 : fUpperLimits(),
45 0 : fEntries(0)
46 0 : {
47 : // Default constructor
48 0 : }
49 :
50 : AliOADBContainer::AliOADBContainer(const char* name) :
51 0 : TNamed(name, "OADBContainer"),
52 0 : fArray(new TObjArray(100)),
53 0 : fDefaultList(new TList()),
54 0 : fPassNames(new TObjArray(100)),
55 0 : fLowerLimits(),
56 0 : fUpperLimits(),
57 0 : fEntries(0)
58 0 : {
59 : // Constructor
60 0 : }
61 :
62 :
63 : //______________________________________________________________________________
64 : AliOADBContainer::~AliOADBContainer()
65 0 : {
66 : // destructor
67 0 : if (fArray) delete fArray;
68 0 : if (fDefaultList) delete fDefaultList;
69 0 : if (fPassNames) delete fPassNames;
70 :
71 0 : }
72 :
73 : //______________________________________________________________________________
74 : AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
75 0 : TNamed(cont),
76 0 : fArray(cont.fArray),
77 0 : fDefaultList(cont.fDefaultList),
78 0 : fPassNames(cont.fPassNames),
79 0 : fLowerLimits(cont.fLowerLimits),
80 0 : fUpperLimits(cont.fUpperLimits),
81 0 : fEntries(cont.fEntries)
82 0 : {
83 : // Copy constructor.
84 0 : }
85 :
86 : //______________________________________________________________________________
87 : AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
88 : {
89 : //
90 : // Assignment operator
91 : // Copy objects related to run ranges
92 0 : if(this!=&cont) {
93 0 : TNamed::operator=(cont);
94 0 : fEntries = cont.fEntries;
95 0 : fLowerLimits.Set(fEntries);
96 0 : fUpperLimits.Set(fEntries);
97 0 : for (Int_t i = 0; i < fEntries; i++) {
98 0 : fLowerLimits[i] = cont.fLowerLimits[i];
99 0 : fUpperLimits[i] = cont.fUpperLimits[i];
100 0 : fArray->AddAt(cont.fArray->At(i), i);
101 0 : if (cont.fPassNames) if (cont.fPassNames->At(i)) fPassNames->AddAt(cont.fPassNames->At(i), i);
102 : }
103 0 : }
104 : //
105 : // Copy default objects
106 0 : TList* list = cont.GetDefaultList();
107 0 : TIter next(list);
108 : TObject* obj;
109 0 : while((obj = next())) fDefaultList->Add(obj);
110 : //
111 : return *this;
112 0 : }
113 :
114 : void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper, TString passName)
115 : {
116 0 : if (!fPassNames) { // create array of pass names for compatibility with old format
117 0 : fPassNames = new TObjArray(100);
118 0 : for (Int_t i=0;i<fArray->GetEntriesFast();i++) fPassNames->Add(new TObjString(""));
119 0 : }
120 : //
121 : // Append a new object to the list
122 : //
123 : // Check that there is no overlap with existing run ranges
124 0 : Int_t index = HasOverlap(lower, upper, passName);
125 :
126 0 : if (index != -1) {
127 0 : AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
128 0 : return;
129 : }
130 : //
131 : // Adjust arrays
132 0 : fEntries++;
133 0 : fLowerLimits.Set(fEntries);
134 0 : fUpperLimits.Set(fEntries);
135 : // Add the object
136 0 : fLowerLimits[fEntries - 1] = lower;
137 0 : fUpperLimits[fEntries - 1] = upper;
138 0 : fArray->Add(obj);
139 0 : fPassNames->Add(new TObjString(passName.Data()));
140 0 : }
141 :
142 : void AliOADBContainer::RemoveObject(Int_t idx)
143 : {
144 0 : if (!fPassNames) { // create array of pass names for compatibility with old format
145 0 : fPassNames = new TObjArray(100);
146 0 : for (Int_t i=0;i<fArray->GetEntriesFast();i++) fPassNames->Add(new TObjString(""));
147 0 : }
148 :
149 : //
150 : // Remove object from the list
151 :
152 : //
153 : // Check that index is inside range
154 0 : if (idx < 0 || idx >= fEntries)
155 : {
156 0 : AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
157 0 : return;
158 : }
159 : //
160 : // Remove the object
161 0 : TObject* obj = fArray->RemoveAt(idx);
162 0 : delete obj;
163 :
164 0 : TObject* pass = fPassNames->RemoveAt(idx);
165 0 : delete pass;
166 : //
167 : // Adjust the run ranges and shrink the array
168 0 : for (Int_t i = idx; i < (fEntries-1); i++) {
169 0 : fLowerLimits[i] = fLowerLimits[i + 1];
170 0 : fUpperLimits[i] = fUpperLimits[i + 1];
171 0 : fArray->AddAt(fArray->At(i+1), i);
172 0 : fPassNames->AddAt(fPassNames->At(i+1),i);
173 : }
174 0 : fArray->RemoveAt(fEntries - 1);
175 0 : fPassNames->RemoveAt(fEntries - 1);
176 0 : fEntries--;
177 0 : }
178 :
179 : void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper, TString passName)
180 : {
181 : //
182 : // Update an existing object, at a given position
183 :
184 : // Check that index is inside range
185 0 : if (idx < 0 || idx >= fEntries)
186 : {
187 0 : AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
188 0 : return;
189 : }
190 : //
191 : // Remove the old object and reset the range
192 : // TObject* obj2 =
193 0 : fArray->RemoveAt(idx);
194 : // don't delete it: if you are updating it may be pointing to the same location of obj...
195 : // delete obj2;
196 0 : fLowerLimits[idx] = -1;
197 0 : fUpperLimits[idx] = -1;
198 : // Check that there is no overlap with existing run ranges
199 0 : Int_t index = HasOverlap(lower, upper,passName);
200 0 : if (index != -1) {
201 0 : AliFatal(Form("Ambiguos validity range (%5d, %5.5d-%5.5d) !\n", index,lower,upper));
202 0 : return;
203 : }
204 : //
205 : // Add object at the same position
206 : //printf("idx %d obj %llx\n", idx, obj);
207 0 : fLowerLimits[idx] = lower;
208 0 : fUpperLimits[idx] = upper;
209 0 : TObjString* pass = (TObjString*) fPassNames->At(idx);
210 0 : pass->SetString(passName.Data());
211 0 : fArray->AddAt(obj, idx);
212 :
213 0 : }
214 :
215 : void AliOADBContainer::AddDefaultObject(TObject* obj)
216 : {
217 : // Add a default object
218 0 : fDefaultList->Add(obj);
219 0 : }
220 :
221 : void AliOADBContainer::CleanDefaultList()
222 : {
223 : // Clean default list
224 0 : fDefaultList->Delete();
225 0 : }
226 :
227 : Int_t AliOADBContainer::GetIndexForRun(Int_t run, TString passName) const
228 : {
229 : //
230 : // Find the index for a given run
231 :
232 : Int_t found = 0;
233 : Int_t index = -1;
234 0 : for (Int_t i = 0; i < fEntries; i++)
235 : {
236 0 : if (fPassNames) if (fPassNames->At(i)) if (passName.CompareTo(fPassNames->At(i)->GetName())) continue;
237 0 : if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
238 : {
239 0 : found++;
240 : index = i;
241 0 : }
242 : }
243 :
244 0 : if (found > 1) {
245 0 : AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
246 0 : } else if (index == -1) {
247 0 : AliWarning(Form("No object (%s) found for run %5d !\n", GetName(), run));
248 0 : }
249 :
250 0 : return index;
251 : }
252 :
253 : TObject* AliOADBContainer::GetObject(Int_t run, const char* def, TString passName) const
254 : {
255 : // Return object for given run or default if not found
256 : TObject* obj = 0;
257 0 : Int_t idx = GetIndexForRun(run,passName);
258 0 : if (idx == -1) idx = GetIndexForRun(run); // try default pass for this run range
259 0 : if (idx == -1) {
260 : // no object found, try default
261 0 : obj = fDefaultList->FindObject(def);
262 0 : if (!obj) {
263 0 : AliError(Form("Default Object (%s) not found !\n", GetName()));
264 0 : return (0);
265 : } else {
266 0 : return (obj);
267 : }
268 : } else {
269 0 : return (fArray->At(idx));
270 : }
271 0 : }
272 :
273 : TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
274 : {
275 : // Return object for given index
276 0 : return (fArray->At(run));
277 : }
278 :
279 : TObject* AliOADBContainer::GetPassNameByIndex(Int_t idx) const
280 : {
281 : // Return object for given index
282 0 : if (!fPassNames) return NULL;
283 0 : return (fPassNames->At(idx));
284 0 : }
285 :
286 :
287 : void AliOADBContainer::WriteToFile(const char* fname) const
288 : {
289 : //
290 : // Write object to file
291 0 : TFile* f = new TFile(fname, "update");
292 0 : Write();
293 0 : f->Purge();
294 0 : f->Close();
295 0 : }
296 :
297 : Int_t AliOADBContainer::InitFromFile(const char* fname, const char* key)
298 : {
299 : // August 2015, Hans: We expand the filename such that
300 : // /cvms/blabla matches the variable $ALICE_ROOT
301 : // We have to delete the returned char*
302 0 : AliInfo(Form("File: %s and key %s\n",fname,key));
303 0 : fname = gSystem->ExpandPathName(fname);
304 0 : if(!fname){AliError("Can not expand path name");return 1;}
305 0 : AliInfo(Form("File name expanded to %s",fname));
306 : //
307 : // Hans: See whether the file is already open
308 : //
309 : // For now print info
310 0 : AliInfo("-----------------------------------------------");
311 0 : AliInfo("List of already open files:\n");
312 0 : TIter nextFile(gROOT->GetListOfFiles());
313 0 : while (1) {
314 0 : TObject *obj = nextFile();
315 0 : if(!obj)break;
316 0 : AliInfo(Form("%s",obj->GetName()));
317 0 : }
318 0 : AliInfo("-----------------------------------------------");
319 :
320 : // Declare the file
321 : TFile* file(0);
322 : // Try to get the file from the list of already open files
323 0 : const TSeqCollection *listOfFiles(gROOT->GetListOfFiles());
324 0 : if(listOfFiles){
325 0 : file =dynamic_cast<TFile*> (listOfFiles->FindObject(fname));
326 0 : }
327 0 : if(file){
328 0 : AliInfo("Success! File was already open!\n");
329 : }
330 : else{
331 0 : AliInfo("Couldn't find file, opening it\n");
332 0 : file = TFile::Open(fname);
333 : }
334 : // Delete pointer from ExpandPathName()
335 0 : delete fname;
336 : fname=0;
337 :
338 : // Initialize object from file
339 0 : if (!file) return (1);
340 0 : AliOADBContainer* cont = 0;
341 0 : file->GetObject(key, cont);
342 0 : if (!cont)
343 : {
344 0 : AliError(Form("Object (%s) not found in file \n", GetName()));
345 0 : return 1;
346 : }
347 :
348 0 : SetName(cont->GetName());
349 0 : SetTitle(cont->GetTitle());
350 :
351 0 : fEntries = cont->GetNumberOfEntries();
352 0 : fLowerLimits.Set(fEntries);
353 0 : fUpperLimits.Set(fEntries);
354 0 : if(fEntries > fArray->GetSize()) fArray->Expand(fEntries);
355 0 : if (!fPassNames) fPassNames = new TObjArray(100);
356 0 : if(fEntries > fPassNames->GetSize()) fPassNames->Expand(fEntries);
357 :
358 0 : for (Int_t i = 0; i < fEntries; i++) {
359 0 : fLowerLimits[i] = cont->LowerLimit(i);
360 0 : fUpperLimits[i] = cont->UpperLimit(i);
361 0 : fArray->AddAt(cont->GetObjectByIndex(i), i);
362 0 : TObject* passName = cont->GetPassNameByIndex(i);
363 0 : fPassNames->AddAt(passName ? passName : new TObjString(""), i);
364 : }
365 0 : if (!fDefaultList) fDefaultList = new TList();
366 0 : TIter next(cont->GetDefaultList());
367 : TObject* obj;
368 0 : while((obj = next())) fDefaultList->Add(obj);
369 :
370 : return 0;
371 :
372 0 : }
373 :
374 :
375 : void AliOADBContainer::List()
376 : {
377 : //
378 : // List Objects
379 0 : printf("Entries %d\n", fEntries);
380 :
381 0 : for (Int_t i = 0; i < fEntries; i++) {
382 0 : printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
383 0 : (fArray->At(i))->Dump();
384 : }
385 0 : TIter next(fDefaultList);
386 : TObject* obj;
387 0 : while((obj = next())) obj->Dump();
388 :
389 0 : }
390 :
391 : Int_t AliOADBContainer::HasOverlap(Int_t lower, Int_t upper, TString passName) const
392 : {
393 : //
394 : // Checks for overlpapping validity regions
395 0 : for (Int_t i = 0; i < fEntries; i++) {
396 0 : if (fPassNames) if (fPassNames->At(i)) if (passName.CompareTo(fPassNames->At(i)->GetName())) continue;
397 0 : if ((lower >= fLowerLimits[i] && lower <= fUpperLimits[i]) ||
398 0 : (upper >= fLowerLimits[i] && upper <= fUpperLimits[i]))
399 : {
400 0 : return (i);
401 : }
402 : }
403 0 : return (-1);
404 0 : }
405 :
406 : void AliOADBContainer::Browse(TBrowser *b)
407 : {
408 : // Browse this object.
409 : // If b=0, there is no Browse call TObject::Browse(0) instead.
410 : // This means TObject::Inspect() will be invoked indirectly
411 :
412 :
413 0 : if (b) {
414 0 : for (Int_t i = 0; i < fEntries; i++) {
415 0 : TString pass = !fPassNames ? " - " : (fPassNames->At(i) ? Form(" - %s",fPassNames->At(i)->GetName()) : " - ");
416 0 : b->Add(fArray->At(i),Form("%9.9d - %9.9d%s", fLowerLimits[i], fUpperLimits[i],pass.CompareTo(" - ")? pass.Data() :""));
417 0 : }
418 0 : TIter next(fDefaultList);
419 : TObject* obj;
420 0 : while((obj = next())) b->Add(obj);
421 :
422 0 : }
423 : else
424 0 : TObject::Browse(b);
425 0 : }
426 :
427 : //______________________________________________________________________________
428 : const char* AliOADBContainer::GetOADBPath()
429 : {
430 : // returns the path of the OADB
431 : // this static function just depends on environment variables
432 :
433 0 : static TString oadbPath;
434 :
435 0 : if (gSystem->Getenv("OADB_PATH"))
436 0 : oadbPath = gSystem->Getenv("OADB_PATH");
437 0 : else if (gSystem->Getenv("ALICE_ROOT"))
438 0 : oadbPath.Form("%s/OADB", gSystem->Getenv("ALICE_ROOT"));
439 : else
440 0 : ::Fatal("AliAnalysisManager::GetOADBPath", "Cannot figure out AODB path. Define ALICE_ROOT or OADB_PATH!");
441 0 : return oadbPath;
442 0 : }
|