1 |
loizides |
1.1 |
//--------------------------------------------------------------------------------------------------
|
2 |
loizides |
1.12 |
// $Id: Array.h,v 1.11 2009/03/02 12:34:00 loizides Exp $
|
3 |
loizides |
1.1 |
//
|
4 |
|
|
// Array
|
5 |
|
|
//
|
6 |
|
|
// Implementation of Collection interface using TClonesArray class.
|
7 |
|
|
//
|
8 |
|
|
// Authors: C.Loizides
|
9 |
|
|
//--------------------------------------------------------------------------------------------------
|
10 |
|
|
|
11 |
loizides |
1.2 |
#ifndef MITANA_DATACONT_ARRAY_H
|
12 |
|
|
#define MITANA_DATACONT_ARRAY_H
|
13 |
loizides |
1.1 |
|
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 |
bendavid |
1.10 |
void Delete();
|
33 |
loizides |
1.1 |
UInt_t Entries() const { return fNumEntries; }
|
34 |
|
|
UInt_t GetEntries() const { return fNumEntries; }
|
35 |
|
|
const char *GetName() const { return fArray.GetName(); }
|
36 |
loizides |
1.4 |
UInt_t GetSize() const { return fArray.GetSize(); }
|
37 |
bendavid |
1.5 |
Bool_t HasObject(const ArrayElement *obj) const;
|
38 |
loizides |
1.1 |
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 |
loizides |
1.8 |
TObject *ObjAt(UInt_t idx);
|
43 |
|
|
const TObject *ObjAt(UInt_t idx) const;
|
44 |
loizides |
1.4 |
void Print(Option_t *opt="") const;
|
45 |
loizides |
1.1 |
void Reset();
|
46 |
|
|
void SetMustClearBit() { this->SetBit(14); }
|
47 |
|
|
void SetMustDeleteBit() { this->SetBit(15); }
|
48 |
|
|
void SetName(const char *name) { fArray.SetName(name); }
|
49 |
loizides |
1.7 |
void Sort() { fArray.Sort(); }
|
50 |
loizides |
1.4 |
void Trim() { fArray.Compress(); }
|
51 |
loizides |
1.1 |
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 |
loizides |
1.11 |
ClassDef(Array, 1) // Wrapper around TClonesArray class
|
64 |
loizides |
1.1 |
};
|
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 |
|
|
return static_cast<ArrayElement*>(fArray[fNumEntries++]);
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
//--------------------------------------------------------------------------------------------------
|
106 |
|
|
template<class ArrayElement>
|
107 |
|
|
inline ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx)
|
108 |
|
|
{
|
109 |
|
|
// Return entry at given index.
|
110 |
|
|
|
111 |
|
|
if (idx<fNumEntries)
|
112 |
|
|
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
113 |
|
|
|
114 |
loizides |
1.9 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
115 |
loizides |
1.8 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
116 |
loizides |
1.1 |
return 0;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
//--------------------------------------------------------------------------------------------------
|
120 |
|
|
template<class ArrayElement>
|
121 |
|
|
inline const ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx) const
|
122 |
|
|
{
|
123 |
|
|
// Return entry at given index.
|
124 |
|
|
|
125 |
|
|
if (idx<fNumEntries)
|
126 |
|
|
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
127 |
|
|
|
128 |
loizides |
1.9 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
129 |
loizides |
1.8 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
130 |
loizides |
1.1 |
return 0;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
//--------------------------------------------------------------------------------------------------
|
134 |
|
|
template<class ArrayElement>
|
135 |
|
|
inline TIterator *mithep::Array<ArrayElement>::Iterator(Bool_t dir) const
|
136 |
|
|
{
|
137 |
|
|
// Return ROOT collection iterator.
|
138 |
|
|
|
139 |
|
|
return fArray.MakeIterator(dir);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
//--------------------------------------------------------------------------------------------------
|
143 |
|
|
template<class ArrayElement>
|
144 |
bendavid |
1.5 |
inline Bool_t mithep::Array<ArrayElement>::HasObject(const ArrayElement *obj) const
|
145 |
|
|
{
|
146 |
|
|
// Check whether object is in array.
|
147 |
|
|
|
148 |
|
|
return fArray.FindObject(obj);
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
//--------------------------------------------------------------------------------------------------
|
152 |
|
|
template<class ArrayElement>
|
153 |
loizides |
1.4 |
void mithep::Array<ArrayElement>::Print(Option_t *opt) const
|
154 |
loizides |
1.1 |
{
|
155 |
loizides |
1.4 |
// Print out elements of array.
|
156 |
loizides |
1.1 |
|
157 |
loizides |
1.4 |
printf("%s: Contains %d (out of %d) objs of name %s\n",
|
158 |
loizides |
1.12 |
GetName(), Entries(), GetSize(), ArrayElement::Class_Name());
|
159 |
loizides |
1.1 |
|
160 |
loizides |
1.6 |
if (opt && opt[0]=='l') {
|
161 |
loizides |
1.12 |
const UInt_t N = Entries();
|
162 |
loizides |
1.6 |
for (UInt_t i=0; i<N; ++i) {
|
163 |
|
|
printf("%4d: ",i);
|
164 |
|
|
At(i)->Print(opt);
|
165 |
|
|
}
|
166 |
loizides |
1.4 |
}
|
167 |
loizides |
1.1 |
}
|
168 |
|
|
|
169 |
|
|
//--------------------------------------------------------------------------------------------------
|
170 |
|
|
template<class ArrayElement>
|
171 |
loizides |
1.8 |
inline TObject *mithep::Array<ArrayElement>::ObjAt(UInt_t idx)
|
172 |
|
|
{
|
173 |
|
|
// Return object at given index.
|
174 |
|
|
|
175 |
|
|
if (idx<fNumEntries)
|
176 |
|
|
return fArray.UncheckedAt(idx);
|
177 |
|
|
|
178 |
loizides |
1.9 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
179 |
loizides |
1.8 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
180 |
|
|
return 0;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
//--------------------------------------------------------------------------------------------------
|
184 |
|
|
template<class ArrayElement>
|
185 |
|
|
const TObject *mithep::Array<ArrayElement>::ObjAt(UInt_t idx) const
|
186 |
|
|
{
|
187 |
|
|
// Return object at given index.
|
188 |
|
|
|
189 |
|
|
if (idx<fNumEntries)
|
190 |
|
|
return static_cast<const TObject*>(fArray.UncheckedAt(idx));
|
191 |
|
|
|
192 |
loizides |
1.9 |
TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
|
193 |
loizides |
1.8 |
idx, fNumEntries, GetName(), ArrayElement::Class_Name());
|
194 |
|
|
return 0;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
//--------------------------------------------------------------------------------------------------
|
198 |
|
|
template<class ArrayElement>
|
199 |
loizides |
1.1 |
inline void mithep::Array<ArrayElement>::Reset()
|
200 |
|
|
{
|
201 |
|
|
// ROOT implementation for clearing the array.
|
202 |
|
|
|
203 |
loizides |
1.4 |
if (MustDelete())
|
204 |
loizides |
1.1 |
fArray.Delete(); //will call destructor for every element
|
205 |
loizides |
1.4 |
else if (MustClear())
|
206 |
loizides |
1.1 |
fArray.Clear("C"); //will call clear for every element
|
207 |
|
|
else
|
208 |
|
|
fArray.Clear();
|
209 |
|
|
|
210 |
|
|
fNumEntries = 0;
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
//--------------------------------------------------------------------------------------------------
|
214 |
|
|
template<class ArrayElement>
|
215 |
bendavid |
1.10 |
inline void mithep::Array<ArrayElement>::Delete()
|
216 |
|
|
{
|
217 |
|
|
// ROOT implementation for clearing the array, deleting all objects inside
|
218 |
|
|
|
219 |
|
|
fArray.Delete(); //will call destructor for every element
|
220 |
|
|
|
221 |
|
|
fNumEntries = 0;
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
//--------------------------------------------------------------------------------------------------
|
225 |
|
|
template<class ArrayElement>
|
226 |
loizides |
1.1 |
inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
|
227 |
|
|
{
|
228 |
|
|
// Return entry at given index.
|
229 |
|
|
|
230 |
|
|
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
//--------------------------------------------------------------------------------------------------
|
234 |
|
|
template<class ArrayElement>
|
235 |
|
|
inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
|
236 |
|
|
{
|
237 |
|
|
// Return entry at given index.
|
238 |
|
|
|
239 |
|
|
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
240 |
|
|
}
|
241 |
loizides |
1.4 |
|
242 |
|
|
//--------------------------------------------------------------------------------------------------
|
243 |
|
|
template<class ArrayElement>
|
244 |
|
|
inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
|
245 |
|
|
{
|
246 |
|
|
// Return entry at given index.
|
247 |
|
|
|
248 |
|
|
return At(idx);
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
//--------------------------------------------------------------------------------------------------
|
252 |
|
|
template<class ArrayElement>
|
253 |
|
|
inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
|
254 |
|
|
{
|
255 |
|
|
// Return entry at given index.
|
256 |
|
|
|
257 |
|
|
return At(idx);
|
258 |
|
|
}
|
259 |
loizides |
1.1 |
#endif
|