Add method to trace back the first common ancestor of 2 generated particles
[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>
bad1b400 31#include <TList.h>
cc336771 32
33ClassImp(AliOADBContainer);
34
35//______________________________________________________________________________
36AliOADBContainer::AliOADBContainer() :
37 TNamed(),
38 fArray(new TObjArray(100)),
bad1b400 39 fDefaultList(new TList()),
cc336771 40 fLowerLimits(),
41 fUpperLimits(),
42 fEntries(0)
43{
44}
45
46AliOADBContainer::AliOADBContainer(char* name) :
47 TNamed(name, "OADBContainer"),
48 fArray(new TObjArray(100)),
bad1b400 49 fDefaultList(new TList()),
cc336771 50 fLowerLimits(),
51 fUpperLimits(),
52 fEntries(0)
53{
54}
55
56
57//______________________________________________________________________________
58AliOADBContainer::~AliOADBContainer()
59{
60 // destructor
61}
62
63//______________________________________________________________________________
64AliOADBContainer::AliOADBContainer(const AliOADBContainer& cont) :
65 TNamed(cont),
66 fArray(cont.fArray),
bad1b400 67 fDefaultList(cont.fDefaultList),
cc336771 68 fLowerLimits(cont.fLowerLimits),
69 fUpperLimits(cont.fUpperLimits),
70 fEntries(cont.fEntries)
71{
72 // Copy constructor.
73}
74
75//______________________________________________________________________________
76AliOADBContainer& AliOADBContainer::operator=(const AliOADBContainer& cont)
77{
78 // Assignment operator
79 if(this!=&cont) {
80 TNamed::operator=(cont);
35f37af6 81 fEntries = cont.fEntries;
82 fLowerLimits.Set(fEntries);
83 fUpperLimits.Set(fEntries);
84 for (Int_t i = 0; i < fEntries; i++) {
85 fLowerLimits[i] = cont.fLowerLimits[i];
86 fUpperLimits[i] = cont.fUpperLimits[i];
87 fArray->AddAt(cont.fArray->At(i), i);
88 }
cc336771 89 }
90 return *this;
91}
92
93void AliOADBContainer::AppendObject(TObject* obj, Int_t lower, Int_t upper)
94{
95 // Append a new object to the list
96 fEntries++;
97 fLowerLimits.Set(fEntries);
98 fUpperLimits.Set(fEntries);
99
100 fLowerLimits[fEntries - 1] = lower;
101 fUpperLimits[fEntries - 1] = upper;
102
103 fArray->Add(obj);
104}
105
106void AliOADBContainer::RemoveObject(Int_t idx)
107{
108 // Remove object from the list
109 if (idx < 0 || idx >= fEntries)
110 {
111 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
112 return;
113 }
114
115 TObject* obj = fArray->RemoveAt(idx);
116 delete obj;
117
118 for (Int_t i = idx; i < (fEntries-1); i++) {
119 fLowerLimits[i] = fLowerLimits[i + 1];
120 fUpperLimits[i] = fUpperLimits[i + 1];
121 fArray->AddAt(fArray->At(i+1), i);
122 }
123 fArray->RemoveAt(fEntries - 1);
124 fEntries--;
125}
126
127void AliOADBContainer::UpdateObject(Int_t idx, TObject* obj, Int_t lower, Int_t upper)
128{
129 // Append a new object to the list
130 if (idx < 0 || idx >= fEntries)
131 {
132 AliError(Form("Index out of Range %5d >= %5d", idx, fEntries));
133 return;
134 }
135
136 fLowerLimits[idx] = lower;
137 fUpperLimits[idx] = upper;
138 fArray->AddAt(obj, idx);
139}
140
bad1b400 141void AliOADBContainer::AddDefaultObject(TNamed* obj)
142{
143 // Add a default object
144 fDefaultList->Add(obj);
145}
146
147void AliOADBContainer::CleanDefaultList()
148{
149 // Clean default list
150 fDefaultList->Delete();
151}
152
35f37af6 153Int_t AliOADBContainer::GetIndexForRun(Int_t run) const
cc336771 154{
bad1b400 155 //
cc336771 156 // Find the index for a given run
157 Int_t found = 0;
158 Int_t index = -1;
159 for (Int_t i = 0; i < fEntries; i++)
160 {
161 if (run >= fLowerLimits[i] && run <= fUpperLimits[i])
162 {
163 found++;
164 index = i;
165 }
166 }
bad1b400 167
168 if (found > 1) {
169 AliError(Form("More than one (%5d) object found; return last (%5d) !\n", found, index));
170 } else if (index == -1) {
171 AliWarning(Form("No object found for run %5d !\n", run));
172 }
173
cc336771 174 return index;
175}
176
bad1b400 177TObject* AliOADBContainer::GetObject(Int_t run, char* def) const
178{
179 // Return object for given run or default if not found
180 TObject* obj = 0;
181 Int_t idx = GetIndexForRun(run);
182 if (idx == -1) {
183 // no object found, try default
184 obj = fDefaultList->FindObject(def);
185 if (!obj) {
186 AliError("Default Object not found !\n");
187 return (0);
188 } else {
189 return (obj);
190 }
191 } else {
192 return (fArray->At(idx));
193 }
194}
195
196TObject* AliOADBContainer::GetObjectByIndex(Int_t run) const
197{
198 // Return object for given index
199 return (fArray->At(run));
200}
201
35f37af6 202void AliOADBContainer::WriteToFile(char* fname) const
cc336771 203{
bad1b400 204 //
cc336771 205 // Write object to file
206 TFile* f = new TFile(fname, "recreate");
207 Write();
208 f->Close();
209}
bad1b400 210
35f37af6 211Int_t AliOADBContainer::InitFromFile(char* fname, char* key)
212{
bad1b400 213 //
35f37af6 214 // Initialize object from file
215 TFile* file = TFile::Open(fname);
216 if (!file) return (1);
217 AliOADBContainer* cont = 0;
218 file->GetObject(key, cont);
219 if (!cont)
220 {
221 AliError("Object not found in file \n");
222 return 1;
223 }
224
225 fEntries = cont->GetNumberOfEntries();
226 fLowerLimits.Set(fEntries);
227 fUpperLimits.Set(fEntries);
228 for (Int_t i = 0; i < fEntries; i++) {
229 fLowerLimits[i] = cont->LowerLimit(i);
230 fUpperLimits[i] = cont->UpperLimit(i);
bad1b400 231 fArray->AddAt(cont->GetObjectByIndex(i), i);
35f37af6 232 }
233
234 return 0;
235
236}
237
cc336771 238
239void AliOADBContainer::List()
240{
bad1b400 241 //
cc336771 242 // List Objects
243 for (Int_t i = 0; i < fEntries; i++) {
244 printf("Lower %5d Upper %5d \n", fLowerLimits[i], fUpperLimits[i]);
245 (fArray->At(i))->Dump();
246 }
247}