1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: Array.h,v 1.13 2009/03/12 18:19:47 loizides Exp $
|
3 |
//
|
4 |
// Array
|
5 |
//
|
6 |
// Implementation of Collection interface using TClonesArray class.
|
7 |
//
|
8 |
// Authors: C.Loizides
|
9 |
//--------------------------------------------------------------------------------------------------
|
10 |
|
11 |
#ifndef MITANA_DATACONT_ARRAY_H
|
12 |
#define MITANA_DATACONT_ARRAY_H
|
13 |
|
14 |
#include <TClonesArray.h>
|
15 |
#include "MitAna/DataCont/interface/Collection.h"
|
16 |
|
17 |
namespace mithep
|
18 |
{
|
19 |
template<class ArrayElement>
|
20 |
class Array : public Collection<ArrayElement>
|
21 |
{
|
22 |
public:
|
23 |
Array(UInt_t size=0, const char *name=0);
|
24 |
|
25 |
ArrayElement *AddNew();
|
26 |
ArrayElement *Allocate();
|
27 |
const TClonesArray &Arr() const { return fArray; }
|
28 |
TClonesArray &Arr() { return fArray; }
|
29 |
ArrayElement *At(UInt_t idx);
|
30 |
const ArrayElement *At(UInt_t idx) const;
|
31 |
void Clear(Option_t */*opt*/="") { fArray.~TClonesArray(); }
|
32 |
void Delete();
|
33 |
UInt_t Entries() const { return fNumEntries; }
|
34 |
UInt_t GetEntries() const { return fNumEntries; }
|
35 |
const char *GetName() const { return fArray.GetName(); }
|
36 |
UInt_t GetSize() const { return fArray.GetSize(); }
|
37 |
Bool_t HasObject(const ArrayElement *obj) const;
|
38 |
Bool_t IsOwner() const { return kTRUE; }
|
39 |
TIterator *Iterator(Bool_t dir = kIterForward) const;
|
40 |
Bool_t MustClear() const { return this->TestBit(14); }
|
41 |
Bool_t MustDelete() const { return this->TestBit(15); }
|
42 |
TObject *ObjAt(UInt_t idx);
|
43 |
const TObject *ObjAt(UInt_t idx) const;
|
44 |
void Print(Option_t *opt="") const;
|
45 |
void Reset();
|
46 |
void SetMustClearBit() { this->SetBit(14); }
|
47 |
void SetMustDeleteBit() { this->SetBit(15); }
|
48 |
void SetName(const char *name) { fArray.SetName(name); }
|
49 |
void Sort() { fArray.Sort(); }
|
50 |
void Trim() { fArray.Compress(); }
|
51 |
ArrayElement *UncheckedAt(UInt_t idx);
|
52 |
const ArrayElement *UncheckedAt(UInt_t idx) const;
|
53 |
ArrayElement *operator[](UInt_t idx);
|
54 |
const ArrayElement *operator[](UInt_t idx) const;
|
55 |
|
56 |
protected:
|
57 |
TClonesArray fArray; //array for storage
|
58 |
UInt_t fNumEntries; //number of entries in the array
|
59 |
|
60 |
private:
|
61 |
Array(const Array &a);
|
62 |
|
63 |
ClassDef(Array, 1) // Wrapper around TClonesArray class
|
64 |
};
|
65 |
}
|
66 |
|
67 |
//--------------------------------------------------------------------------------------------------
|
68 |
template<class ArrayElement>
|
69 |
inline mithep::Array<ArrayElement>::Array(UInt_t size, const char *name) :
|
70 |
fArray(ArrayElement::Class_Name(),size),
|
71 |
fNumEntries(0)
|
72 |
{
|
73 |
// Default constructor.
|
74 |
|
75 |
fArray.BypassStreamer(kTRUE);
|
76 |
|
77 |
if (name)
|
78 |
fArray.SetName(name);
|
79 |
|
80 |
ArrayElement test;
|
81 |
if (test.MustClear())
|
82 |
SetMustClearBit();
|
83 |
if (test.MustDelete())
|
84 |
SetMustDeleteBit();
|
85 |
}
|
86 |
|
87 |
//--------------------------------------------------------------------------------------------------
|
88 |
template<class ArrayElement>
|
89 |
inline ArrayElement *mithep::Array<ArrayElement>::AddNew()
|
90 |
{
|
91 |
// Add new entry and return pointer to it.
|
92 |
|
93 |
return new(Allocate()) ArrayElement();
|
94 |
}
|
95 |
|
96 |
//--------------------------------------------------------------------------------------------------
|
97 |
template<class ArrayElement>
|
98 |
inline ArrayElement *mithep::Array<ArrayElement>::Allocate()
|
99 |
{
|
100 |
// Allocate a slot in the array, *only* to be used in placement new operator.
|
101 |
|
102 |
BaseCollection::Clear();
|
103 |
return static_cast<ArrayElement*>(fArray[fNumEntries++]);
|
104 |
}
|
105 |
|
106 |
//--------------------------------------------------------------------------------------------------
|
107 |
template<class ArrayElement>
|
108 |
inline ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx)
|
109 |
{
|
110 |
// Return entry at given index.
|
111 |
|
112 |
if (idx<fNumEntries)
|
113 |
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
114 |
|
115 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
116 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
117 |
return 0;
|
118 |
}
|
119 |
|
120 |
//--------------------------------------------------------------------------------------------------
|
121 |
template<class ArrayElement>
|
122 |
inline const ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx) const
|
123 |
{
|
124 |
// Return entry at given index.
|
125 |
|
126 |
if (idx<fNumEntries)
|
127 |
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
128 |
|
129 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
130 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
131 |
return 0;
|
132 |
}
|
133 |
|
134 |
//--------------------------------------------------------------------------------------------------
|
135 |
template<class ArrayElement>
|
136 |
inline TIterator *mithep::Array<ArrayElement>::Iterator(Bool_t dir) const
|
137 |
{
|
138 |
// Return ROOT collection iterator.
|
139 |
|
140 |
return fArray.MakeIterator(dir);
|
141 |
}
|
142 |
|
143 |
//--------------------------------------------------------------------------------------------------
|
144 |
template<class ArrayElement>
|
145 |
inline Bool_t mithep::Array<ArrayElement>::HasObject(const ArrayElement *obj) const
|
146 |
{
|
147 |
// Check whether object is in array.
|
148 |
|
149 |
return fArray.FindObject(obj);
|
150 |
}
|
151 |
|
152 |
//--------------------------------------------------------------------------------------------------
|
153 |
template<class ArrayElement>
|
154 |
void mithep::Array<ArrayElement>::Print(Option_t *opt) const
|
155 |
{
|
156 |
// Print out elements of array.
|
157 |
|
158 |
printf("%s: Contains %d (out of %d) objs of name %s\n",
|
159 |
GetName(), Entries(), GetSize(), ArrayElement::Class_Name());
|
160 |
|
161 |
if (opt && opt[0]=='l') {
|
162 |
const UInt_t N = Entries();
|
163 |
for (UInt_t i=0; i<N; ++i) {
|
164 |
printf("%4d: ",i);
|
165 |
At(i)->Print(opt);
|
166 |
}
|
167 |
}
|
168 |
}
|
169 |
|
170 |
//--------------------------------------------------------------------------------------------------
|
171 |
template<class ArrayElement>
|
172 |
inline TObject *mithep::Array<ArrayElement>::ObjAt(UInt_t idx)
|
173 |
{
|
174 |
// Return object at given index.
|
175 |
|
176 |
if (idx<fNumEntries)
|
177 |
return fArray.UncheckedAt(idx);
|
178 |
|
179 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
180 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
181 |
return 0;
|
182 |
}
|
183 |
|
184 |
//--------------------------------------------------------------------------------------------------
|
185 |
template<class ArrayElement>
|
186 |
const TObject *mithep::Array<ArrayElement>::ObjAt(UInt_t idx) const
|
187 |
{
|
188 |
// Return object at given index.
|
189 |
|
190 |
if (idx<fNumEntries)
|
191 |
return static_cast<const TObject*>(fArray.UncheckedAt(idx));
|
192 |
|
193 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
194 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
195 |
return 0;
|
196 |
}
|
197 |
|
198 |
//--------------------------------------------------------------------------------------------------
|
199 |
template<class ArrayElement>
|
200 |
inline void mithep::Array<ArrayElement>::Reset()
|
201 |
{
|
202 |
// ROOT-like implementation for clearing the array.
|
203 |
|
204 |
if (MustDelete())
|
205 |
fArray.Delete(); //will call destructor for every element
|
206 |
else if (MustClear())
|
207 |
fArray.Clear("C"); //will call clear for every element
|
208 |
else
|
209 |
fArray.Clear();
|
210 |
|
211 |
fNumEntries = 0;
|
212 |
BaseCollection::Clear();
|
213 |
}
|
214 |
|
215 |
//--------------------------------------------------------------------------------------------------
|
216 |
template<class ArrayElement>
|
217 |
inline void mithep::Array<ArrayElement>::Delete()
|
218 |
{
|
219 |
// ROOT implementation for clearing the array, deleting all objects inside
|
220 |
|
221 |
fArray.Delete(); //will call destructor for every element
|
222 |
fNumEntries = 0;
|
223 |
BaseCollection::Clear();
|
224 |
}
|
225 |
|
226 |
//--------------------------------------------------------------------------------------------------
|
227 |
template<class ArrayElement>
|
228 |
inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
|
229 |
{
|
230 |
// Return entry at given index.
|
231 |
|
232 |
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
233 |
}
|
234 |
|
235 |
//--------------------------------------------------------------------------------------------------
|
236 |
template<class ArrayElement>
|
237 |
inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
|
238 |
{
|
239 |
// Return entry at given index.
|
240 |
|
241 |
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
242 |
}
|
243 |
|
244 |
//--------------------------------------------------------------------------------------------------
|
245 |
template<class ArrayElement>
|
246 |
inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
|
247 |
{
|
248 |
// Return entry at given index.
|
249 |
|
250 |
return At(idx);
|
251 |
}
|
252 |
|
253 |
//--------------------------------------------------------------------------------------------------
|
254 |
template<class ArrayElement>
|
255 |
inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
|
256 |
{
|
257 |
// Return entry at given index.
|
258 |
|
259 |
return At(idx);
|
260 |
}
|
261 |
#endif
|