1 |
loizides |
1.1 |
//--------------------------------------------------------------------------------------------------
|
2 |
loizides |
1.4 |
// $Id: Array.h,v 1.3 2008/10/23 18:22:27 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 |
|
|
~Array() {}
|
25 |
|
|
|
26 |
|
|
ArrayElement *AddNew();
|
27 |
|
|
ArrayElement *Allocate();
|
28 |
|
|
const TClonesArray &Arr() const { return fArray; }
|
29 |
|
|
TClonesArray &Arr() { return fArray; }
|
30 |
|
|
ArrayElement *At(UInt_t idx);
|
31 |
|
|
const ArrayElement *At(UInt_t idx) const;
|
32 |
|
|
void Clear(Option_t */*opt*/="") { fArray.~TClonesArray(); }
|
33 |
|
|
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 |
loizides |
1.1 |
Bool_t IsOwner() const { return kTRUE; }
|
38 |
|
|
TIterator *Iterator(Bool_t dir = kIterForward) const;
|
39 |
|
|
Bool_t MustClear() const { return this->TestBit(14); }
|
40 |
|
|
Bool_t MustDelete() const { return this->TestBit(15); }
|
41 |
loizides |
1.4 |
void Print(Option_t *opt="") const;
|
42 |
loizides |
1.1 |
void Reset();
|
43 |
|
|
void SetMustClearBit() { this->SetBit(14); }
|
44 |
|
|
void SetMustDeleteBit() { this->SetBit(15); }
|
45 |
|
|
void SetName(const char *name) { fArray.SetName(name); }
|
46 |
loizides |
1.4 |
void Trim() { fArray.Compress(); }
|
47 |
loizides |
1.1 |
ArrayElement *UncheckedAt(UInt_t idx);
|
48 |
|
|
const ArrayElement *UncheckedAt(UInt_t idx) const;
|
49 |
|
|
ArrayElement *operator[](UInt_t idx);
|
50 |
|
|
const ArrayElement *operator[](UInt_t idx) const;
|
51 |
|
|
|
52 |
|
|
protected:
|
53 |
|
|
TClonesArray fArray; //array for storage
|
54 |
|
|
UInt_t fNumEntries; //number of entries in the array
|
55 |
|
|
|
56 |
|
|
private:
|
57 |
|
|
Array(const Array &a);
|
58 |
|
|
|
59 |
|
|
ClassDefT(Array,1) // Wrapper around TClonesArray class
|
60 |
|
|
};
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
//--------------------------------------------------------------------------------------------------
|
64 |
|
|
template<class ArrayElement>
|
65 |
|
|
inline mithep::Array<ArrayElement>::Array(UInt_t size, const char *name) :
|
66 |
|
|
fArray(ArrayElement::Class_Name(),size),
|
67 |
|
|
fNumEntries(0)
|
68 |
|
|
{
|
69 |
|
|
// Default constructor.
|
70 |
|
|
|
71 |
|
|
fArray.BypassStreamer(kTRUE);
|
72 |
|
|
|
73 |
|
|
if (name)
|
74 |
|
|
fArray.SetName(name);
|
75 |
|
|
|
76 |
|
|
ArrayElement test;
|
77 |
|
|
if (test.MustClear())
|
78 |
|
|
SetMustClearBit();
|
79 |
|
|
if (test.MustDelete())
|
80 |
|
|
SetMustDeleteBit();
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
//--------------------------------------------------------------------------------------------------
|
84 |
|
|
template<class ArrayElement>
|
85 |
|
|
inline ArrayElement *mithep::Array<ArrayElement>::AddNew()
|
86 |
|
|
{
|
87 |
|
|
// Add new entry and return pointer to it.
|
88 |
|
|
|
89 |
|
|
return new(Allocate()) ArrayElement();
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
//--------------------------------------------------------------------------------------------------
|
93 |
|
|
template<class ArrayElement>
|
94 |
|
|
inline ArrayElement *mithep::Array<ArrayElement>::Allocate()
|
95 |
|
|
{
|
96 |
|
|
// Allocate a slot in the array, *only* to be used in placement new operator.
|
97 |
|
|
|
98 |
|
|
return static_cast<ArrayElement*>(fArray[fNumEntries++]);
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
//--------------------------------------------------------------------------------------------------
|
102 |
|
|
template<class ArrayElement>
|
103 |
|
|
inline ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx)
|
104 |
|
|
{
|
105 |
|
|
// Return entry at given index.
|
106 |
|
|
|
107 |
|
|
if (idx<fNumEntries)
|
108 |
|
|
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
109 |
|
|
|
110 |
|
|
ArrayElement tmp;
|
111 |
loizides |
1.3 |
TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
|
112 |
loizides |
1.4 |
idx, fNumEntries, GetName(), tmp.GetName());
|
113 |
loizides |
1.1 |
return 0;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
//--------------------------------------------------------------------------------------------------
|
117 |
|
|
template<class ArrayElement>
|
118 |
|
|
inline const ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx) const
|
119 |
|
|
{
|
120 |
|
|
// Return entry at given index.
|
121 |
|
|
|
122 |
|
|
if (idx<fNumEntries)
|
123 |
|
|
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
124 |
|
|
|
125 |
|
|
ArrayElement tmp;
|
126 |
loizides |
1.3 |
TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
|
127 |
loizides |
1.4 |
idx, fNumEntries, GetName(), tmp.GetName());
|
128 |
loizides |
1.1 |
return 0;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
//--------------------------------------------------------------------------------------------------
|
132 |
|
|
template<class ArrayElement>
|
133 |
|
|
inline TIterator *mithep::Array<ArrayElement>::Iterator(Bool_t dir) const
|
134 |
|
|
{
|
135 |
|
|
// Return ROOT collection iterator.
|
136 |
|
|
|
137 |
|
|
return fArray.MakeIterator(dir);
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
//--------------------------------------------------------------------------------------------------
|
141 |
|
|
template<class ArrayElement>
|
142 |
loizides |
1.4 |
void mithep::Array<ArrayElement>::Print(Option_t *opt) const
|
143 |
loizides |
1.1 |
{
|
144 |
loizides |
1.4 |
// Print out elements of array.
|
145 |
loizides |
1.1 |
|
146 |
loizides |
1.4 |
printf("%s: Contains %d (out of %d) objs of name %s\n",
|
147 |
|
|
GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
|
148 |
loizides |
1.1 |
|
149 |
loizides |
1.4 |
const UInt_t N = GetEntries();
|
150 |
|
|
for (UInt_t i=0; i<N; ++i) {
|
151 |
|
|
printf("%4d: ",i);
|
152 |
|
|
At(i)->Print(opt);
|
153 |
|
|
}
|
154 |
loizides |
1.1 |
}
|
155 |
|
|
|
156 |
|
|
//--------------------------------------------------------------------------------------------------
|
157 |
|
|
template<class ArrayElement>
|
158 |
|
|
inline void mithep::Array<ArrayElement>::Reset()
|
159 |
|
|
{
|
160 |
|
|
// ROOT implementation for clearing the array.
|
161 |
|
|
|
162 |
loizides |
1.4 |
if (MustDelete())
|
163 |
loizides |
1.1 |
fArray.Delete(); //will call destructor for every element
|
164 |
loizides |
1.4 |
else if (MustClear())
|
165 |
loizides |
1.1 |
fArray.Clear("C"); //will call clear for every element
|
166 |
|
|
else
|
167 |
|
|
fArray.Clear();
|
168 |
|
|
|
169 |
|
|
fNumEntries = 0;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
//--------------------------------------------------------------------------------------------------
|
173 |
|
|
template<class ArrayElement>
|
174 |
|
|
inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
|
175 |
|
|
{
|
176 |
|
|
// Return entry at given index.
|
177 |
|
|
|
178 |
|
|
return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
//--------------------------------------------------------------------------------------------------
|
182 |
|
|
template<class ArrayElement>
|
183 |
|
|
inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
|
184 |
|
|
{
|
185 |
|
|
// Return entry at given index.
|
186 |
|
|
|
187 |
|
|
return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
|
188 |
|
|
}
|
189 |
loizides |
1.4 |
|
190 |
|
|
//--------------------------------------------------------------------------------------------------
|
191 |
|
|
template<class ArrayElement>
|
192 |
|
|
inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
|
193 |
|
|
{
|
194 |
|
|
// Return entry at given index.
|
195 |
|
|
|
196 |
|
|
return At(idx);
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
//--------------------------------------------------------------------------------------------------
|
200 |
|
|
template<class ArrayElement>
|
201 |
|
|
inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
|
202 |
|
|
{
|
203 |
|
|
// Return entry at given index.
|
204 |
|
|
|
205 |
|
|
return At(idx);
|
206 |
|
|
}
|
207 |
loizides |
1.1 |
#endif
|