source: trunk/src/ExRootCandList.cc@ 9

Last change on this file since 9 was 2, checked in by Pavel Demin, 16 years ago

first commit

File size: 7.2 KB
Line 
1
2/** \class ExRootCandidate
3 *
4 * A list of ExRootCandidates with iterators.
5 *
6 * $Date: 2008-06-04 13:57:53 $
7 * $Revision: 1.1 $
8 *
9 *
10 * \author P. Demin - UCL, Louvain-la-Neuve
11 *
12 */
13
14#include "ExRootAnalysis/ExRootCandList.h"
15#include "ExRootAnalysis/ExRootCandidate.h"
16#include "ExRootAnalysis/ExRootFactory.h"
17
18#include "TObjArray.h"
19#include "TBrowser.h"
20
21ExRootCandList::ExRootCandList() :
22 TNamed("ExRootCandList", ""),
23 fFactory(0),
24 fArray(0)
25{
26}
27
28//------------------------------------------------------------------------------
29
30ExRootCandList::ExRootCandList(const ExRootCandList &object) :
31 TNamed("ExRootCandList", ""),
32 fFactory(0),
33 fArray(0)
34{
35 object.Copy(*this);
36}
37
38//------------------------------------------------------------------------------
39// Geneology functions, no longer in a separate class
40//------------------------------------------------------------------------------
41
42void ExRootCandList::Add(ExRootCandidate *object)
43{
44 if(!fArray) fArray = fFactory->NewArray();
45 fArray->Add(object);
46}
47
48//------------------------------------------------------------------------------
49
50void ExRootCandList::Add(const ExRootCandidate *object)
51{
52 if(!fArray) fArray = fFactory->NewArray();
53 fArray->Add(object->Clone());
54}
55
56//------------------------------------------------------------------------------
57// Access functions
58//------------------------------------------------------------------------------
59
60const ExRootCandidate *ExRootCandList::At(Int_t i) const
61{
62 if(i >= 0 && i < Size())
63 return static_cast<ExRootCandidate *>(fArray->UncheckedAt(i));
64 else
65 return 0;
66}
67
68//------------------------------------------------------------------------------
69
70Int_t ExRootCandList::Size() const
71{
72 if(!fArray) return 0;
73 return fArray->GetEntriesFast();
74}
75
76//------------------------------------------------------------------------------
77
78TObject *ExRootCandList::Clone(const char *newname) const
79{
80 ExRootCandList *object = fFactory->NewCandList();
81 Copy(*object);
82 return object;
83}
84
85//------------------------------------------------------------------------------
86
87void ExRootCandList::Copy(TObject &obj) const
88{
89 ExRootCandList &object = (ExRootCandList &) obj;
90
91 TNamed::Copy(obj);
92
93 object.fFactory = fFactory;
94 object.fArray = 0;
95}
96
97//------------------------------------------------------------------------------
98
99void ExRootCandList::Clear()
100{
101 if(fArray) fArray->Clear();
102}
103
104//------------------------------------------------------------------------------
105
106void ExRootCandList::Sort(ExRootCompare *compare)
107{
108 if(fArray)
109 {
110 ExRootCompare *backup = ExRootCandidate::fgCompare;
111 ExRootCandidate::fgCompare = compare;
112 fArray->Sort();
113 ExRootCandidate::fgCompare = backup;
114 }
115}
116
117//------------------------------------------------------------------------------
118
119void ExRootCandList::Browse(TBrowser *b)
120{
121 if(fArray) fArray->Browse(b);
122}
123
124//------------------------------------------------------------------------------
125//------------------------------------------------------------------------------
126
127ExRootCandIter::ExRootCandIter(TObjArray *array)
128{
129 fArray = array;
130 Reset();
131}
132
133//------------------------------------------------------------------------------
134
135ExRootCandIter::ExRootCandIter(ExRootCandList *object)
136{
137 fArray = object->fArray;
138 Reset();
139}
140
141//------------------------------------------------------------------------------
142
143ExRootCandIter::ExRootCandIter(const ExRootCandIter &iter)
144{
145 fArray = iter.fArray;
146 fCursor = iter.fCursor;
147}
148
149//------------------------------------------------------------------------------
150
151ExRootCandIter &ExRootCandIter::operator=(const ExRootCandIter &rhs)
152{
153 if(this != &rhs)
154 {
155 fArray = rhs.fArray;
156 fCursor = rhs.fCursor;
157 }
158 return *this;
159}
160
161//------------------------------------------------------------------------------
162
163ExRootCandidate *ExRootCandIter::Next(Bool_t direction)
164{
165 // By default the iteration direction is kIterForward.
166 // To go backward use kIterBackward.
167 // Return next object in array. Returns 0 when no more objects in array.
168
169 if(!fArray) return 0;
170
171 if(direction == kIterForward)
172 {
173 Int_t size = fArray->Capacity();
174 while(fCursor < size && fArray->UncheckedAt(fCursor) == 0) { ++fCursor; }
175 if(fCursor < size) return static_cast<ExRootCandidate *>(fArray->UncheckedAt(fCursor++));
176 }
177 else
178 {
179 while(fCursor >= 0 && fArray->UncheckedAt(fCursor) == 0) { --fCursor; }
180 if(fCursor >= 0) return static_cast<ExRootCandidate *>(fArray->UncheckedAt(fCursor--));
181 }
182 return 0;
183}
184
185//------------------------------------------------------------------------------
186
187void ExRootCandIter::Reset(Bool_t direction)
188{
189 if(direction == kIterForward)
190 {
191 fCursor = 0;
192 }
193 else
194 {
195 if(fArray) fCursor = fArray->Capacity() - 1;
196 }
197}
198
199//------------------------------------------------------------------------------
200//------------------------------------------------------------------------------
201
202ExRootCandConstIter::ExRootCandConstIter(const TObjArray *array)
203{
204 fArray = array;
205 Reset();
206}
207
208//------------------------------------------------------------------------------
209
210ExRootCandConstIter::ExRootCandConstIter(const ExRootCandList *object)
211{
212 fArray = object->fArray;
213 Reset();
214}
215
216//------------------------------------------------------------------------------
217
218ExRootCandConstIter::ExRootCandConstIter(const ExRootCandIter &iter)
219{
220 fArray = iter.fArray;
221 fCursor = iter.fCursor;
222}
223
224//------------------------------------------------------------------------------
225
226ExRootCandConstIter::ExRootCandConstIter(const ExRootCandConstIter &iter)
227{
228 fArray = iter.fArray;
229 fCursor = iter.fCursor;
230}
231
232//------------------------------------------------------------------------------
233
234ExRootCandConstIter &ExRootCandConstIter::operator=(const ExRootCandIter &rhs)
235{
236 fArray = rhs.fArray;
237 fCursor = rhs.fCursor;
238 return *this;
239}
240
241//------------------------------------------------------------------------------
242
243ExRootCandConstIter &ExRootCandConstIter::operator=(const ExRootCandConstIter &rhs)
244{
245 if(this != &rhs)
246 {
247 fArray = rhs.fArray;
248 fCursor = rhs.fCursor;
249 }
250 return *this;
251}
252
253//------------------------------------------------------------------------------
254
255const ExRootCandidate *ExRootCandConstIter::Next(Bool_t direction)
256{
257 // By default the iteration direction is kIterForward.
258 // To go backward use kIterBackward.
259 // Return next object in array. Returns 0 when no more objects in array.
260
261 if(!fArray) return 0;
262
263 if(direction == kIterForward)
264 {
265 Int_t size = fArray->Capacity();
266 while(fCursor < size && fArray->UncheckedAt(fCursor) == 0) { ++fCursor; }
267 if(fCursor < size) return static_cast<ExRootCandidate *>(fArray->UncheckedAt(fCursor++));
268 }
269 else
270 {
271 while(fCursor >= 0 && fArray->UncheckedAt(fCursor) == 0) { --fCursor; }
272 if(fCursor >= 0) return static_cast<ExRootCandidate *>(fArray->UncheckedAt(fCursor--));
273 }
274 return 0;
275}
276
277//------------------------------------------------------------------------------
278
279void ExRootCandConstIter::Reset(Bool_t direction)
280{
281 if(direction == kIterForward)
282 {
283 fCursor = 0;
284 }
285 else
286 {
287 if(fArray) fCursor = fArray->Capacity() - 1;
288 }
289}
290
291//------------------------------------------------------------------------------
292
Note: See TracBrowser for help on using the repository browser.