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