ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.11
Committed: Wed Dec 10 11:26:52 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.10: +80 -16 lines
Log Message:
Implemented ObjAt function.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.11 // $Id: ObjArray.h,v 1.10 2008/12/09 17:42:20 loizides Exp $
3 loizides 1.1 //
4     // ObjArray
5     //
6 loizides 1.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 loizides 1.1 //
10     // Authors: C.Loizides
11     //--------------------------------------------------------------------------------------------------
12    
13 loizides 1.2 #ifndef MITANA_DATACONT_OBJARRAY_H
14     #define MITANA_DATACONT_OBJARRAY_H
15 loizides 1.1
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     ~ObjArray() {}
27    
28 loizides 1.6 void Add(const ArrayElement *ae);
29 loizides 1.11 void Add(const BaseCollection *col);
30 loizides 1.4 void Add(const TCollection *col);
31 loizides 1.6 void AddOwned(ArrayElement *ae);
32 loizides 1.1 const TObjArray &Arr() const { return fArray; }
33     TObjArray &Arr() { return fArray; }
34     ArrayElement *At(UInt_t idx);
35     const ArrayElement *At(UInt_t idx) const;
36     void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
37     UInt_t Entries() const { return fNumEntries; }
38     UInt_t GetEntries() const { return fNumEntries; }
39     const char* GetName() const { return fArray.GetName(); }
40 loizides 1.6 UInt_t GetSize() const { return fArray.GetSize(); }
41 bendavid 1.7 Bool_t HasObject(const ArrayElement *obj) const;
42 loizides 1.1 const ArrayElement *Find(const char *name) const;
43     ArrayElement *Find(const char *name);
44 bendavid 1.7 void Print(Option_t *opt="") const;
45 loizides 1.11 TObject *ObjAt(UInt_t idx);
46     const TObject *ObjAt(UInt_t idx) const;
47 loizides 1.1 void Remove(UInt_t idx);
48     void Remove(const char *name);
49     void Remove(ArrayElement *ae);
50     Bool_t IsOwner() const { return fArray.IsOwner(); }
51     TIterator *Iterator(Bool_t dir = kIterForward) const;
52     void Reset();
53 loizides 1.6 void SetName(const char *name) { fArray.SetName(name); }
54 bendavid 1.8 void SetOwner(Bool_t o);
55 loizides 1.1 void Sort() { fArray.Sort(); }
56 loizides 1.5 void Trim() { fArray.Compress();}
57 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
58     const ArrayElement *UncheckedAt(UInt_t idx) const;
59     ArrayElement *operator[](UInt_t idx);
60     const ArrayElement *operator[](UInt_t idx) const;
61    
62     protected:
63 loizides 1.6 void AddLast(const ArrayElement *e);
64 loizides 1.1
65     TObjArray fArray; //array for storage
66     UInt_t fNumEntries; //number of entries in the array
67    
68     private:
69     ObjArray(const ObjArray &a);
70    
71 loizides 1.11 ClassDefT(ObjArray, 1) // Wrapper around TClonesArray class
72 loizides 1.1 };
73     }
74    
75     //--------------------------------------------------------------------------------------------------
76     template<class ArrayElement>
77     inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
78     fArray(size),
79     fNumEntries(0)
80     {
81     // Default constructor.
82    
83     if (name)
84     fArray.SetName(name);
85     }
86    
87     //--------------------------------------------------------------------------------------------------
88     template<class ArrayElement>
89 loizides 1.4 inline void mithep::ObjArray<ArrayElement>::Add(const TCollection *col)
90     {
91 loizides 1.5 // Add objects from collection to array.
92    
93 loizides 1.4 if (!col)
94     return;
95    
96     if (IsOwner()) {
97 loizides 1.11 TObject::Error("Add", "Can not add collection since IsOwner() returns kTRUE.");
98 loizides 1.4 return;
99     }
100    
101 loizides 1.11 TIter iter(col->MakeIterator());
102     while (1) {
103     TObject *obj = iter.Next();
104     if (!obj)
105     break;
106     const ArrayElement *to = dynamic_cast<const ArrayElement*>(obj);
107     if (to)
108     AddLast(to);
109     }
110     }
111    
112     //--------------------------------------------------------------------------------------------------
113     template<class ArrayElement>
114     inline void mithep::ObjArray<ArrayElement>::Add(const BaseCollection *col)
115     {
116     // Add objects from collection to array.
117    
118     if (!col)
119     return;
120    
121     if (IsOwner()) {
122     TObject::Error("Add", "Can not add collection since IsOwner() returns kTRUE.");
123     return;
124     }
125    
126     UInt_t n = col->GetEntries();
127     for (UInt_t i=0; i<n; ++i) {
128     const ArrayElement *to = dynamic_cast<const ArrayElement*>(col->ObjAt(i));
129     if (to)
130 loizides 1.4 AddLast(to);
131     }
132     }
133    
134     //--------------------------------------------------------------------------------------------------
135     template<class ArrayElement>
136 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::Add(const ArrayElement *ae)
137     {
138     // Add object to array. This function should be used in cases the array does not own the objects.
139    
140     if (IsOwner()) {
141 loizides 1.11 TObject::Error("Add", "Can not add object since IsOwner() returns kTRUE.");
142 loizides 1.6 return;
143     }
144    
145     AddLast(ae);
146     }
147    
148     //--------------------------------------------------------------------------------------------------
149     template<class ArrayElement>
150     inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *ae)
151 loizides 1.1 {
152     // Add new entry at the end of array.
153    
154 loizides 1.6 fArray.AddLast(const_cast<ArrayElement*>(ae));
155 loizides 1.1 fNumEntries++;
156     }
157    
158     //--------------------------------------------------------------------------------------------------
159     template<class ArrayElement>
160 loizides 1.6 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 bendavid 1.8 if (!IsOwner()) {
165     TObject::Error("AddOwned","Can not add object since IsOwner() returns kFALSE.");
166     return;
167     }
168    
169 loizides 1.6 AddLast(ae);
170     }
171    
172     //--------------------------------------------------------------------------------------------------
173     template<class ArrayElement>
174 loizides 1.1 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 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
182     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
183 loizides 1.1 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 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
196     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
197 loizides 1.1 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 bendavid 1.7 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 loizides 1.11 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: (%ud < %ud 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: (%ud < %ud violated) for %s containing %s",
251     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
252     return 0;
253     }
254    
255     //--------------------------------------------------------------------------------------------------
256     template<class ArrayElement>
257 loizides 1.1 inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
258     {
259 loizides 1.11 // Remove object at given index from array. You probably want to call Trim() as well.
260    
261     if (idx>=fNumEntries)
262     return;
263 loizides 1.1
264     fArray.RemoveAt(idx);
265 loizides 1.11 --fNumEntries;
266 loizides 1.1 }
267    
268     //--------------------------------------------------------------------------------------------------
269     template<class ArrayElement>
270     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
271     {
272 loizides 1.11 // Remove object with given name from array. You probably want to call Trim() as well.
273 loizides 1.1
274     TObject *obj = fArray.FindObject(name);
275 loizides 1.11 if (!obj)
276     return;
277     fArray.Remove(obj);
278     --fNumEntries;
279 loizides 1.1 }
280    
281     //--------------------------------------------------------------------------------------------------
282     template<class ArrayElement>
283     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
284     {
285 loizides 1.11 // Remove object from array. You probably want to call Trim() as well.
286    
287     if (!ae)
288     return;
289    
290 loizides 1.1 fArray.Remove(ae);
291 loizides 1.11 --fNumEntries;
292 loizides 1.1 }
293    
294     //--------------------------------------------------------------------------------------------------
295     template<class ArrayElement>
296     inline void mithep::ObjArray<ArrayElement>::Reset()
297     {
298     // Default implementation for clearing the array.
299    
300     fArray.Clear(); //will delete objects if owner
301    
302     fNumEntries = 0;
303     }
304    
305     //--------------------------------------------------------------------------------------------------
306     template<class ArrayElement>
307 bendavid 1.8 inline void mithep::ObjArray<ArrayElement>::SetOwner(Bool_t o)
308     {
309     // Set ownership of array. Must do this before adding any objects
310    
311     if (fNumEntries) {
312     TObject::Error("SetOwner","Cannot safely change ownership of non-empty array.");
313     return;
314     }
315    
316     fArray.SetOwner(o);
317     }
318    
319     //--------------------------------------------------------------------------------------------------
320     template<class ArrayElement>
321 loizides 1.1 inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
322     {
323     // Return ROOT collection iterator.
324    
325     return fArray.MakeIterator(dir);
326     }
327    
328     //--------------------------------------------------------------------------------------------------
329     template<class ArrayElement>
330 loizides 1.5 void mithep::ObjArray<ArrayElement>::Print(Option_t *opt) const
331     {
332     // Print out elements of array.
333    
334     printf("%s: Contains %d (out of %d) objs of name %s\n",
335     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
336    
337 loizides 1.9 if (opt && opt[0]=='l') {
338     const UInt_t N = GetEntries();
339     for (UInt_t i=0; i<N; ++i) {
340     printf("%4d: ",i);
341     At(i)->Print(opt);
342     }
343 loizides 1.5 }
344     }
345    
346     //--------------------------------------------------------------------------------------------------
347     template<class ArrayElement>
348     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
349 loizides 1.1 {
350     // Return entry at given index.
351    
352 loizides 1.5 return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
353 loizides 1.1 }
354    
355     //--------------------------------------------------------------------------------------------------
356     template<class ArrayElement>
357 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
358 loizides 1.1 {
359     // Return entry at given index.
360    
361 loizides 1.5 return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
362 loizides 1.1 }
363    
364     //--------------------------------------------------------------------------------------------------
365     template<class ArrayElement>
366 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
367 loizides 1.1 {
368     // Return entry at given index.
369    
370 loizides 1.5 return At(idx);
371 loizides 1.1 }
372    
373     //--------------------------------------------------------------------------------------------------
374     template<class ArrayElement>
375 loizides 1.5 inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
376 loizides 1.1 {
377     // Return entry at given index.
378    
379 loizides 1.5 return At(idx);
380 loizides 1.1 }
381     #endif