Fixed include file (Johen)
[u/mrichter/AliRoot.git] / OADB / 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
23
24
25
26#include "AliOADBContainer.h"
27#include "AliLog.h"
28#include <TObjArray.h>
29#include <TArrayI.h>
30#include <TFile.h>
31
32ClassImp(AliOADBContainer);
33
34//______________________________________________________________________________
35AliOADBContainer::AliOADBContainer() :
36 TNamed(),
37 fArray(new TObjArray(100)),
38 fLowerLimits(),
39 fUpperLimits(),
40 fEntries(0)
41{
42}
43
44AliOADBContainer::AliOADBContainer(char* name) :
45 TNamed(name, "OADBContainer"),
46 fArray(new TObjArray(100)),
47 fLowerLimits(),
48 fUpperLimits(),
49 fEntries(0)
50{
51}
52
53
54//______________________________________________________________________________
55AliOADBContainer::~AliOADBContainer()
56{
57 // destructor
58}
59
60//______________________________________________________________________________
61AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
62 TNamed(cont),
63 fArray(cont.fArray),
64 fLowerLimits(cont.fLowerLimits),
65 fUpperLimits(cont.fUpperLimits),
66 fEntries(cont.fEntries)
67{
68 // Copy constructor.
69}
70
71//______________________________________________________________________________
72AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
73{
74 // Assignment operator
75 if(this!=&cont) {
76 TNamed::operator=(cont);
35f37af6 77 fEntries = cont.fEntries;
78 fLowerLimits.Set(fEntries);
79 fUpperLimits.Set(fEntries);
80 for (Int_t i = 0; i < fEntries; i++) {
81 fLowerLimits[i] = cont.fLowerLimits[i];
82 fUpperLimits[i] = cont.fUpperLimits[i];
83 fArray->AddAt(cont.fArray->At(i), i);
84 }
cc336771 85 }
86 return *this;
87}
88
89void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
90{
91 // Append a new object to the list
92 fEntries++;
93 fLowerLimits.Set(fEntries);
94 fUpperLimits.Set(fEntries);
95
96 fLowerLimits[fEntries - 1] = lower;
97 fUpperLimits[fEntries - 1] = upper;
98
99 fArray->Add(obj);
100}
101
102void AliOADBContainer::RemoveObject(Int_t idx)
103{
104 // Remove object from the list
105 if (idx < 0 || idx >= fEntries)
106 {
107 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
108 return;
109 }
110
111 TObject* obj = fArray->RemoveAt(idx);
112 delete obj;
113
114 for (Int_t i = idx; i < (fEntries-1); i++) {
115 fLowerLimits[i] = fLowerLimits[i + 1];
116 fUpperLimits[i] = fUpperLimits[i + 1];
117 fArray->AddAt(fArray->At(i+1), i);
118 }
119 fArray->RemoveAt(fEntries - 1);
120 fEntries--;
121}
122
123void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
124{
125 // Append a new object to the list
126 if (idx < 0 || idx >= fEntries)
127 {
128 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
129 return;
130 }
131
132 fLowerLimits[idx] = lower;
133 fUpperLimits[idx] = upper;
134 fArray->AddAt(obj, idx);
135}
136
35f37af6 137Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
cc336771 138{
139 // Find the index for a given run
140 Int_t found = 0;
141 Int_t index = -1;
142 for (Int_t i = 0; i < fEntries; i++)
143 {
144 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
145 {
146 found++;
147 index = i;
148 }
149 }
150 return index;
151}
152
35f37af6 153void AliOADBContainer::WriteToFile(char* fname) const
cc336771 154{
155 // Write object to file
156 TFile* f = new TFile(fname, "recreate");
157 Write();
158 f->Close();
159}
35f37af6 160Int_t AliOADBContainer::InitFromFile(char* fname, char* key)
161{
162 // Initialize object from file
163 TFile* file = TFile::Open(fname);
164 if (!file) return (1);
165 AliOADBContainer* cont = 0;
166 file->GetObject(key, cont);
167 if (!cont)
168 {
169 AliError("Object not found in file \n");
170 return 1;
171 }
172
173 fEntries = cont->GetNumberOfEntries();
174 fLowerLimits.Set(fEntries);
175 fUpperLimits.Set(fEntries);
176 for (Int_t i = 0; i < fEntries; i++) {
177 fLowerLimits[i] = cont->LowerLimit(i);
178 fUpperLimits[i] = cont->UpperLimit(i);
179 fArray->AddAt(cont->GetObject(i), i);
180 }
181
182 return 0;
183
184}
185
cc336771 186
187void AliOADBContainer::List()
188{
189 // List Objects
190 for (Int_t i = 0; i < fEntries; i++) {
191 printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
192 (fArray->At(i))->Dump();
193 }
194}