ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.7
Committed: Mon Dec 1 17:17:20 2008 UTC (16 years, 5 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.6: +12 -2 lines
Log Message:
Add HasObject to Collection interface

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.7 // $Id: ObjArray.h,v 1.6 2008/11/21 20:13:35 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.4 void Add(const TCollection *col);
30 loizides 1.6 void AddOwned(ArrayElement *ae);
31 loizides 1.1 const TObjArray &Arr() const { return fArray; }
32     TObjArray &Arr() { return fArray; }
33     ArrayElement *At(UInt_t idx);
34     const ArrayElement *At(UInt_t idx) const;
35     void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
36     UInt_t Entries() const { return fNumEntries; }
37     UInt_t GetEntries() const { return fNumEntries; }
38     const char* GetName() const { return fArray.GetName(); }
39 loizides 1.6 UInt_t GetSize() const { return fArray.GetSize(); }
40 bendavid 1.7 Bool_t HasObject(const ArrayElement *obj) const;
41 loizides 1.1 const ArrayElement *Find(const char *name) const;
42     ArrayElement *Find(const char *name);
43 bendavid 1.7 void Print(Option_t *opt="") const;
44 loizides 1.1 void Remove(UInt_t idx);
45     void Remove(const char *name);
46     void Remove(ArrayElement *ae);
47     Bool_t IsOwner() const { return fArray.IsOwner(); }
48     TIterator *Iterator(Bool_t dir = kIterForward) const;
49     void Reset();
50 loizides 1.6 void SetName(const char *name) { fArray.SetName(name); }
51 loizides 1.1 void SetOwner(Bool_t o) { fArray.SetOwner(o); }
52     void Sort() { fArray.Sort(); }
53 loizides 1.5 void Trim() { fArray.Compress();}
54 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
55     const ArrayElement *UncheckedAt(UInt_t idx) const;
56     ArrayElement *operator[](UInt_t idx);
57     const ArrayElement *operator[](UInt_t idx) const;
58    
59     protected:
60 loizides 1.6 void AddLast(const ArrayElement *e);
61 loizides 1.1
62     TObjArray fArray; //array for storage
63     UInt_t fNumEntries; //number of entries in the array
64    
65     private:
66     ObjArray(const ObjArray &a);
67    
68     ClassDefT(ObjArray,1) // Wrapper around TClonesArray class
69     };
70     }
71    
72     //--------------------------------------------------------------------------------------------------
73     template<class ArrayElement>
74     inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
75     fArray(size),
76     fNumEntries(0)
77     {
78     // Default constructor.
79    
80     if (name)
81     fArray.SetName(name);
82     }
83    
84     //--------------------------------------------------------------------------------------------------
85     template<class ArrayElement>
86 loizides 1.4 inline void mithep::ObjArray<ArrayElement>::Add(const TCollection *col)
87     {
88 loizides 1.5 // Add objects from collection to array.
89    
90 loizides 1.4 if (!col)
91     return;
92    
93     if (IsOwner()) {
94     TObject::Error("Add","Can not add collection since IsOwner() returns kTRUE.");
95     return;
96     }
97    
98     TIterator *iter = col->MakeIterator();
99     if (iter) {
100     ArrayElement *to = dynamic_cast<ArrayElement *>(iter->Next());
101     while (to) {
102     AddLast(to);
103     to = dynamic_cast<ArrayElement *>(iter->Next());
104     }
105     }
106     }
107    
108     //--------------------------------------------------------------------------------------------------
109     template<class ArrayElement>
110 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::Add(const ArrayElement *ae)
111     {
112     // Add object to array. This function should be used in cases the array does not own the objects.
113    
114     if (IsOwner()) {
115     TObject::Error("Add","Can not add object since IsOwner() returns kTRUE.");
116     return;
117     }
118    
119     AddLast(ae);
120     }
121    
122     //--------------------------------------------------------------------------------------------------
123     template<class ArrayElement>
124     inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *ae)
125 loizides 1.1 {
126     // Add new entry at the end of array.
127    
128 loizides 1.6 fArray.AddLast(const_cast<ArrayElement*>(ae));
129 loizides 1.1 fNumEntries++;
130     }
131    
132     //--------------------------------------------------------------------------------------------------
133     template<class ArrayElement>
134 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::AddOwned(ArrayElement *ae)
135     {
136     // Add object to array. This function should be used in cases the array owns the objects.
137    
138     AddLast(ae);
139     }
140    
141     //--------------------------------------------------------------------------------------------------
142     template<class ArrayElement>
143 loizides 1.1 inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
144     {
145     // Return entry at given index.
146    
147     if (idx<fNumEntries)
148     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
149    
150 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
151     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
152 loizides 1.1 return 0;
153     }
154    
155     //--------------------------------------------------------------------------------------------------
156     template<class ArrayElement>
157     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
158     {
159     // Return entry at given index.
160    
161     if (idx<fNumEntries)
162     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
163    
164 loizides 1.5 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
165     idx, fNumEntries, GetName(), ArrayElement::Class_Name());
166 loizides 1.1 return 0;
167     }
168    
169     //--------------------------------------------------------------------------------------------------
170     template<class ArrayElement>
171     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
172     {
173     // Find object by name.
174    
175     return static_cast<const ArrayElement*>(fArray.FindObject(name));
176     }
177    
178     //--------------------------------------------------------------------------------------------------
179     template<class ArrayElement>
180     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
181     {
182     // Find object by name.
183    
184     return static_cast<ArrayElement*>(fArray.FindObject(name));
185     }
186    
187     //--------------------------------------------------------------------------------------------------
188     template<class ArrayElement>
189 bendavid 1.7 inline Bool_t mithep::ObjArray<ArrayElement>::HasObject(const ArrayElement *obj) const
190     {
191     // Check whether object is in array.
192    
193     return fArray.FindObject(obj);
194     }
195    
196     //--------------------------------------------------------------------------------------------------
197     template<class ArrayElement>
198 loizides 1.1 inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
199     {
200     // Remove object at given index from array. You probably want to call Trim as well.
201    
202     fArray.RemoveAt(idx);
203     }
204    
205     //--------------------------------------------------------------------------------------------------
206     template<class ArrayElement>
207     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
208     {
209     // Remove object from array. You probably want to call Trim as well.
210    
211     TObject *obj = fArray.FindObject(name);
212     if (obj) fArray.Remove(obj);
213     }
214    
215     //--------------------------------------------------------------------------------------------------
216     template<class ArrayElement>
217     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
218     {
219     fArray.Remove(ae);
220     }
221    
222     //--------------------------------------------------------------------------------------------------
223     template<class ArrayElement>
224     inline void mithep::ObjArray<ArrayElement>::Reset()
225     {
226     // Default implementation for clearing the array.
227    
228     fArray.Clear(); //will delete objects if owner
229    
230     fNumEntries = 0;
231     }
232    
233     //--------------------------------------------------------------------------------------------------
234     template<class ArrayElement>
235     inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
236     {
237     // Return ROOT collection iterator.
238    
239     return fArray.MakeIterator(dir);
240     }
241    
242     //--------------------------------------------------------------------------------------------------
243     template<class ArrayElement>
244 loizides 1.5 void mithep::ObjArray<ArrayElement>::Print(Option_t *opt) const
245     {
246     // Print out elements of array.
247    
248     printf("%s: Contains %d (out of %d) objs of name %s\n",
249     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
250    
251     const UInt_t N = GetEntries();
252     for (UInt_t i=0; i<N; ++i) {
253     printf("%4d: ",i);
254     At(i)->Print(opt);
255     }
256     }
257    
258     //--------------------------------------------------------------------------------------------------
259     template<class ArrayElement>
260     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
261 loizides 1.1 {
262     // Return entry at given index.
263    
264 loizides 1.5 return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
265 loizides 1.1 }
266    
267     //--------------------------------------------------------------------------------------------------
268     template<class ArrayElement>
269 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
270 loizides 1.1 {
271     // Return entry at given index.
272    
273 loizides 1.5 return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
274 loizides 1.1 }
275    
276     //--------------------------------------------------------------------------------------------------
277     template<class ArrayElement>
278 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
279 loizides 1.1 {
280     // Return entry at given index.
281    
282 loizides 1.5 return At(idx);
283 loizides 1.1 }
284    
285     //--------------------------------------------------------------------------------------------------
286     template<class ArrayElement>
287 loizides 1.5 inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
288 loizides 1.1 {
289     // Return entry at given index.
290    
291 loizides 1.5 return At(idx);
292 loizides 1.1 }
293     #endif