ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Array.h
Revision: 1.13
Committed: Thu Mar 12 18:19:47 2009 UTC (16 years, 2 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre2
Changes since 1.12: +4 -3 lines
Log Message:
Proper resetting of the cached entry in BaseCollection.

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: Array.h,v 1.12 2009/03/06 13:52:54 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 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 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
115 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
116 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 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
129 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
130 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 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 void mithep::Array<ArrayElement>::Print(Option_t *opt) const
154 {
155 // Print out elements of array.
156
157 printf("%s: Contains %d (out of %d) objs of name %s\n",
158 GetName(), Entries(), GetSize(), ArrayElement::Class_Name());
159
160 if (opt && opt[0]=='l') {
161 const UInt_t N = Entries();
162 for (UInt_t i=0; i<N; ++i) {
163 printf("%4d: ",i);
164 At(i)->Print(opt);
165 }
166 }
167 }
168
169 //--------------------------------------------------------------------------------------------------
170 template<class ArrayElement>
171 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 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
179 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 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
193 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
194 return 0;
195 }
196
197 //--------------------------------------------------------------------------------------------------
198 template<class ArrayElement>
199 inline void mithep::Array<ArrayElement>::Reset()
200 {
201 // ROOT-like implementation for clearing the array.
202
203 if (MustDelete())
204 fArray.Delete(); //will call destructor for every element
205 else if (MustClear())
206 fArray.Clear("C"); //will call clear for every element
207 else
208 fArray.Clear();
209
210 fNumEntries = 0;
211 BaseCollection::Clear();
212 }
213
214 //--------------------------------------------------------------------------------------------------
215 template<class ArrayElement>
216 inline void mithep::Array<ArrayElement>::Delete()
217 {
218 // ROOT implementation for clearing the array, deleting all objects inside
219
220 fArray.Delete(); //will call destructor for every element
221 fNumEntries = 0;
222 BaseCollection::Clear();
223 }
224
225 //--------------------------------------------------------------------------------------------------
226 template<class ArrayElement>
227 inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
228 {
229 // Return entry at given index.
230
231 return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
232 }
233
234 //--------------------------------------------------------------------------------------------------
235 template<class ArrayElement>
236 inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
237 {
238 // Return entry at given index.
239
240 return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
241 }
242
243 //--------------------------------------------------------------------------------------------------
244 template<class ArrayElement>
245 inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
246 {
247 // Return entry at given index.
248
249 return At(idx);
250 }
251
252 //--------------------------------------------------------------------------------------------------
253 template<class ArrayElement>
254 inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
255 {
256 // Return entry at given index.
257
258 return At(idx);
259 }
260 #endif