1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: ObjArray.h,v 1.16 2009/03/23 22:15:09 loizides Exp $
|
3 |
//
|
4 |
// ObjArray
|
5 |
//
|
6 |
// Implementation of Collection interface using TObjArray class. By default, the
|
7 |
// array does not own its elements, and hence will not delete them. Use SetOwner(1)
|
8 |
// to turn on ownership.
|
9 |
//
|
10 |
// Authors: C.Loizides
|
11 |
//--------------------------------------------------------------------------------------------------
|
12 |
|
13 |
#ifndef MITANA_DATACONT_OBJARRAY_H
|
14 |
#define MITANA_DATACONT_OBJARRAY_H
|
15 |
|
16 |
#include <TObjArray.h>
|
17 |
#include "MitAna/DataCont/interface/Collection.h"
|
18 |
|
19 |
namespace mithep
|
20 |
{
|
21 |
template<class ArrayElement>
|
22 |
class ObjArray : public Collection<ArrayElement>
|
23 |
{
|
24 |
public:
|
25 |
ObjArray(UInt_t size=0, const char *name=0);
|
26 |
|
27 |
void Add(const ArrayElement *ae);
|
28 |
void Add(const BaseCollection *col);
|
29 |
void Add(const TCollection *col);
|
30 |
void AddOwned(ArrayElement *ae);
|
31 |
const TObjArray &Arr() const { return fArray; }
|
32 |
TObjArray &Arr() { return fArray; }
|
33 |
ArrayElement *At(UInt_t idx);
|
34 |
const ArrayElement *At(UInt_t idx) const;
|
35 |
void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
|
36 |
UInt_t Entries() const { return fNumEntries; }
|
37 |
UInt_t GetEntries() const { return fNumEntries; }
|
38 |
const char* GetName() const { return fArray.GetName(); }
|
39 |
UInt_t GetSize() const { return fArray.GetSize(); }
|
40 |
Bool_t HasObject(const ArrayElement *obj) const;
|
41 |
const ArrayElement *Find(const char *name) const;
|
42 |
ArrayElement *Find(const char *name);
|
43 |
void Print(Option_t *opt="") const;
|
44 |
TObject *ObjAt(UInt_t idx);
|
45 |
const TObject *ObjAt(UInt_t idx) const;
|
46 |
void Remove(UInt_t idx);
|
47 |
void Remove(const char *name);
|
48 |
void Remove(const ArrayElement *ae);
|
49 |
Bool_t IsOwner() const { return fArray.IsOwner(); }
|
50 |
TIterator *Iterator(Bool_t dir = kIterForward) const;
|
51 |
void Reset();
|
52 |
void SetName(const char *name) { fArray.SetName(name); }
|
53 |
void SetOwner(Bool_t o);
|
54 |
void Sort() { fArray.Sort(); }
|
55 |
void Trim();
|
56 |
ArrayElement *UncheckedAt(UInt_t idx);
|
57 |
const ArrayElement *UncheckedAt(UInt_t idx) const;
|
58 |
ArrayElement *operator[](UInt_t idx);
|
59 |
const ArrayElement *operator[](UInt_t idx) const;
|
60 |
|
61 |
protected:
|
62 |
void AddLast(const ArrayElement *e);
|
63 |
|
64 |
TObjArray fArray; //array for storage
|
65 |
UInt_t fNumEntries; //number of entries in the array
|
66 |
|
67 |
private:
|
68 |
ObjArray(const ObjArray &a);
|
69 |
|
70 |
ClassDef(ObjArray, 1) // Wrapper around TObjArray class
|
71 |
};
|
72 |
}
|
73 |
|
74 |
//--------------------------------------------------------------------------------------------------
|
75 |
template<class ArrayElement>
|
76 |
inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
|
77 |
fArray(size),
|
78 |
fNumEntries(0)
|
79 |
{
|
80 |
// Default constructor.
|
81 |
|
82 |
if (name)
|
83 |
fArray.SetName(name);
|
84 |
}
|
85 |
|
86 |
//--------------------------------------------------------------------------------------------------
|
87 |
template<class ArrayElement>
|
88 |
inline void mithep::ObjArray<ArrayElement>::Add(const TCollection *col)
|
89 |
{
|
90 |
// Add objects from collection to array.
|
91 |
|
92 |
if (!col)
|
93 |
return;
|
94 |
|
95 |
if (IsOwner()) {
|
96 |
TObject::Error("Add", "Cannot add collection since IsOwner() returns kTRUE.");
|
97 |
return;
|
98 |
}
|
99 |
|
100 |
TIter iter(col->MakeIterator());
|
101 |
while (1) {
|
102 |
TObject *obj = iter.Next();
|
103 |
if (!obj)
|
104 |
break;
|
105 |
const ArrayElement *to = dynamic_cast<const ArrayElement*>(obj);
|
106 |
if (to)
|
107 |
AddLast(to);
|
108 |
}
|
109 |
}
|
110 |
|
111 |
//--------------------------------------------------------------------------------------------------
|
112 |
template<class ArrayElement>
|
113 |
inline void mithep::ObjArray<ArrayElement>::Add(const BaseCollection *col)
|
114 |
{
|
115 |
// Add objects from collection to array.
|
116 |
|
117 |
if (!col)
|
118 |
return;
|
119 |
|
120 |
if (IsOwner()) {
|
121 |
TObject::Error("Add", "Cannot add collection since IsOwner() returns kTRUE.");
|
122 |
return;
|
123 |
}
|
124 |
|
125 |
UInt_t n = col->GetEntries();
|
126 |
for (UInt_t i=0; i<n; ++i) {
|
127 |
const ArrayElement *to = dynamic_cast<const ArrayElement*>(col->ObjAt(i));
|
128 |
if (to)
|
129 |
AddLast(to);
|
130 |
}
|
131 |
}
|
132 |
|
133 |
//--------------------------------------------------------------------------------------------------
|
134 |
template<class ArrayElement>
|
135 |
inline void mithep::ObjArray<ArrayElement>::Add(const ArrayElement *ae)
|
136 |
{
|
137 |
// Add object to array. This function should be used in cases the array does not own the objects.
|
138 |
|
139 |
if (IsOwner()) {
|
140 |
TObject::Error("Add", "Cannot add object since IsOwner() returns kTRUE.");
|
141 |
return;
|
142 |
}
|
143 |
|
144 |
AddLast(ae);
|
145 |
}
|
146 |
|
147 |
//--------------------------------------------------------------------------------------------------
|
148 |
template<class ArrayElement>
|
149 |
inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *ae)
|
150 |
{
|
151 |
// Add new entry at the end of array.
|
152 |
|
153 |
fArray.AddLast(const_cast<ArrayElement*>(ae));
|
154 |
fNumEntries++;
|
155 |
BaseCollection::Clear();
|
156 |
}
|
157 |
|
158 |
//--------------------------------------------------------------------------------------------------
|
159 |
template<class ArrayElement>
|
160 |
inline void mithep::ObjArray<ArrayElement>::AddOwned(ArrayElement *ae)
|
161 |
{
|
162 |
// Add object to array. This function should be used in cases the array owns the objects.
|
163 |
|
164 |
if (!IsOwner()) {
|
165 |
TObject::Error("AddOwned","Cannot add object since IsOwner() returns kFALSE.");
|
166 |
return;
|
167 |
}
|
168 |
|
169 |
AddLast(ae);
|
170 |
}
|
171 |
|
172 |
//--------------------------------------------------------------------------------------------------
|
173 |
template<class ArrayElement>
|
174 |
inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
|
175 |
{
|
176 |
// Return entry at given index.
|
177 |
|
178 |
if (idx<fNumEntries)
|
179 |
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
180 |
|
181 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
182 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
183 |
return 0;
|
184 |
}
|
185 |
|
186 |
//--------------------------------------------------------------------------------------------------
|
187 |
template<class ArrayElement>
|
188 |
inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
|
189 |
{
|
190 |
// Return entry at given index.
|
191 |
|
192 |
if (idx<fNumEntries)
|
193 |
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
194 |
|
195 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
196 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
197 |
return 0;
|
198 |
}
|
199 |
|
200 |
//--------------------------------------------------------------------------------------------------
|
201 |
template<class ArrayElement>
|
202 |
inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
|
203 |
{
|
204 |
// Find object by name.
|
205 |
|
206 |
return static_cast<const ArrayElement*>(fArray.FindObject(name));
|
207 |
}
|
208 |
|
209 |
//--------------------------------------------------------------------------------------------------
|
210 |
template<class ArrayElement>
|
211 |
inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
|
212 |
{
|
213 |
// Find object by name.
|
214 |
|
215 |
return static_cast<ArrayElement*>(fArray.FindObject(name));
|
216 |
}
|
217 |
|
218 |
//--------------------------------------------------------------------------------------------------
|
219 |
template<class ArrayElement>
|
220 |
inline Bool_t mithep::ObjArray<ArrayElement>::HasObject(const ArrayElement *obj) const
|
221 |
{
|
222 |
// Check whether object is in array.
|
223 |
|
224 |
return fArray.FindObject(obj);
|
225 |
}
|
226 |
|
227 |
//--------------------------------------------------------------------------------------------------
|
228 |
template<class ArrayElement>
|
229 |
TObject *mithep::ObjArray<ArrayElement>::ObjAt(UInt_t idx)
|
230 |
{
|
231 |
// Return object at given index.
|
232 |
|
233 |
if (idx<fNumEntries)
|
234 |
return fArray.UncheckedAt(idx);
|
235 |
|
236 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
237 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
238 |
return 0;
|
239 |
}
|
240 |
|
241 |
//--------------------------------------------------------------------------------------------------
|
242 |
template<class ArrayElement>
|
243 |
const TObject *mithep::ObjArray<ArrayElement>::ObjAt(UInt_t idx) const
|
244 |
{
|
245 |
// Return object at given index.
|
246 |
|
247 |
if (idx<fNumEntries)
|
248 |
return static_cast<const TObject*>(fArray.UncheckedAt(idx));
|
249 |
|
250 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
251 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
252 |
return 0;
|
253 |
}
|
254 |
|
255 |
//--------------------------------------------------------------------------------------------------
|
256 |
template<class ArrayElement>
|
257 |
inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
|
258 |
{
|
259 |
// Remove object at given index from array. You probably want to call Trim() as well.
|
260 |
|
261 |
if (idx>=fNumEntries)
|
262 |
return;
|
263 |
|
264 |
fArray.RemoveAt(idx);
|
265 |
}
|
266 |
|
267 |
//--------------------------------------------------------------------------------------------------
|
268 |
template<class ArrayElement>
|
269 |
inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
|
270 |
{
|
271 |
// Remove object with given name from array. You probably want to call Trim() as well.
|
272 |
|
273 |
TObject *obj = fArray.FindObject(name);
|
274 |
if (!obj)
|
275 |
return;
|
276 |
fArray.Remove(obj);
|
277 |
}
|
278 |
|
279 |
//--------------------------------------------------------------------------------------------------
|
280 |
template<class ArrayElement>
|
281 |
inline void mithep::ObjArray<ArrayElement>::Remove(const ArrayElement *ae)
|
282 |
{
|
283 |
// Remove object from array. You probably want to call Trim() as well.
|
284 |
|
285 |
if (!ae)
|
286 |
return;
|
287 |
|
288 |
fArray.Remove(const_cast<ArrayElement*>(ae));
|
289 |
}
|
290 |
|
291 |
//--------------------------------------------------------------------------------------------------
|
292 |
template<class ArrayElement>
|
293 |
inline void mithep::ObjArray<ArrayElement>::Reset()
|
294 |
{
|
295 |
// Default implementation for clearing the array.
|
296 |
|
297 |
fArray.Clear(); //will delete objects if owner
|
298 |
|
299 |
fNumEntries = 0;
|
300 |
BaseCollection::Clear();
|
301 |
}
|
302 |
|
303 |
//--------------------------------------------------------------------------------------------------
|
304 |
template<class ArrayElement>
|
305 |
inline void mithep::ObjArray<ArrayElement>::SetOwner(Bool_t o)
|
306 |
{
|
307 |
// Set ownership of array. Must do this before adding any objects
|
308 |
|
309 |
if (fNumEntries) {
|
310 |
TObject::Error("SetOwner","Cannot safely change ownership of non-empty array.");
|
311 |
return;
|
312 |
}
|
313 |
|
314 |
fArray.SetOwner(o);
|
315 |
}
|
316 |
|
317 |
//--------------------------------------------------------------------------------------------------
|
318 |
template<class ArrayElement>
|
319 |
inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
|
320 |
{
|
321 |
// Return ROOT collection iterator.
|
322 |
|
323 |
return fArray.MakeIterator(dir);
|
324 |
}
|
325 |
|
326 |
//--------------------------------------------------------------------------------------------------
|
327 |
template<class ArrayElement>
|
328 |
void mithep::ObjArray<ArrayElement>::Print(Option_t *opt) const
|
329 |
{
|
330 |
// Print out elements of array.
|
331 |
|
332 |
printf("%s: Contains %d (out of %d) objs of name %s\n",
|
333 |
GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
|
334 |
|
335 |
if (opt && opt[0]=='l') {
|
336 |
const UInt_t N = GetEntries();
|
337 |
for (UInt_t i=0; i<N; ++i) {
|
338 |
printf("%4d: ",i);
|
339 |
if (At(i))
|
340 |
At(i)->Print(opt);
|
341 |
else
|
342 |
printf("Removed\n");
|
343 |
}
|
344 |
}
|
345 |
}
|
346 |
|
347 |
//--------------------------------------------------------------------------------------------------
|
348 |
template<class ArrayElement>
|
349 |
inline void mithep::ObjArray<ArrayElement>::Trim()
|
350 |
{
|
351 |
// Trim array.
|
352 |
|
353 |
if (static_cast<Int_t>(fNumEntries) != fArray.GetEntries()) {
|
354 |
fArray.Compress();
|
355 |
fNumEntries = fArray.GetEntriesFast();
|
356 |
BaseCollection::Clear();
|
357 |
}
|
358 |
}
|
359 |
|
360 |
//--------------------------------------------------------------------------------------------------
|
361 |
template<class ArrayElement>
|
362 |
inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
|
363 |
{
|
364 |
// Return entry at given index.
|
365 |
|
366 |
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
367 |
}
|
368 |
|
369 |
//--------------------------------------------------------------------------------------------------
|
370 |
template<class ArrayElement>
|
371 |
inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
|
372 |
{
|
373 |
// Return entry at given index.
|
374 |
|
375 |
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
376 |
}
|
377 |
|
378 |
//--------------------------------------------------------------------------------------------------
|
379 |
template<class ArrayElement>
|
380 |
inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
|
381 |
{
|
382 |
// Return entry at given index.
|
383 |
|
384 |
return At(idx);
|
385 |
}
|
386 |
|
387 |
//--------------------------------------------------------------------------------------------------
|
388 |
template<class ArrayElement>
|
389 |
inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
|
390 |
{
|
391 |
// Return entry at given index.
|
392 |
|
393 |
return At(idx);
|
394 |
}
|
395 |
#endif
|