ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.6
Committed: Fri Nov 21 20:13:35 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.5: +34 -8 lines
Log Message:
Change add to accept const pointer, while AddOwned accepts a pointer.

File Contents

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