ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.2
Committed: Wed Sep 10 03:33:26 2008 UTC (16 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.1: +3 -3 lines
Log Message:
Cleanup

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.2 // $Id: ObjArray.h,v 1.1 2008/07/29 10:36:20 loizides Exp $
3 loizides 1.1 //
4     // ObjArray
5     //
6     // Implementation of Collection interface using TObjArray class.
7     //
8     // Authors: C.Loizides
9     //--------------------------------------------------------------------------------------------------
10    
11 loizides 1.2 #ifndef MITANA_DATACONT_OBJARRAY_H
12     #define MITANA_DATACONT_OBJARRAY_H
13 loizides 1.1
14     #include <TObjArray.h>
15     #include "MitAna/DataCont/interface/Collection.h"
16    
17     namespace mithep
18     {
19     template<class ArrayElement>
20     class ObjArray : public Collection<ArrayElement>
21     {
22     public:
23     ObjArray(UInt_t size=0, const char *name=0);
24     ~ObjArray() {}
25    
26     void Add(ArrayElement *ae);
27     const TObjArray &Arr() const { return fArray; }
28     TObjArray &Arr() { return fArray; }
29     ArrayElement *At(UInt_t idx);
30     const ArrayElement *At(UInt_t idx) const;
31     void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
32     UInt_t Entries() const { return fNumEntries; }
33     UInt_t GetEntries() const { return fNumEntries; }
34     const char* GetName() const { return fArray.GetName(); }
35     const ArrayElement *Find(const char *name) const;
36     ArrayElement *Find(const char *name);
37     void Remove(UInt_t idx);
38     void Remove(const char *name);
39     void Remove(ArrayElement *ae);
40     Bool_t IsOwner() const { return fArray.IsOwner(); }
41     TIterator *Iterator(Bool_t dir = kIterForward) const;
42     void Reset();
43     void Trim() { fArray.Compress();}
44     void SetName(const char* name) { fArray.SetName(name); }
45     void SetOwner(Bool_t o) { fArray.SetOwner(o); }
46     void Sort() { fArray.Sort(); }
47     ArrayElement *UncheckedAt(UInt_t idx);
48     const ArrayElement *UncheckedAt(UInt_t idx) const;
49    
50     ArrayElement *operator[](UInt_t idx);
51     const ArrayElement *operator[](UInt_t idx) const;
52    
53     protected:
54     void AddLast(ArrayElement *e);
55     void AddLast(const ArrayElement *e);
56    
57     TObjArray fArray; //array for storage
58     UInt_t fNumEntries; //number of entries in the array
59    
60     private:
61     ObjArray(const ObjArray &a);
62    
63     ClassDefT(ObjArray,1) // Wrapper around TClonesArray class
64     };
65     }
66    
67     //--------------------------------------------------------------------------------------------------
68     template<class ArrayElement>
69     inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
70     fArray(size),
71     fNumEntries(0)
72     {
73     // Default constructor.
74    
75     if (name)
76     fArray.SetName(name);
77     }
78    
79     //--------------------------------------------------------------------------------------------------
80     template<class ArrayElement>
81     inline void mithep::ObjArray<ArrayElement>::Add(ArrayElement *ae)
82     {
83     // Add new entry to array.
84    
85     AddLast(ae);
86     }
87    
88     //--------------------------------------------------------------------------------------------------
89     template<class ArrayElement>
90     inline void mithep::ObjArray<ArrayElement>::AddLast(ArrayElement *e)
91     {
92     // Add new entry at the end of array.
93    
94     fArray.AddLast(e);
95     fNumEntries++;
96     }
97    
98     //--------------------------------------------------------------------------------------------------
99     template<class ArrayElement>
100     inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *e)
101     {
102     // Add new entry at the end of array.
103    
104     AddLast(const_cast<ArrayElement*>(e));
105     }
106    
107     //--------------------------------------------------------------------------------------------------
108     template<class ArrayElement>
109     inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
110     {
111     // Return entry at given index.
112    
113     if (idx<fNumEntries)
114     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
115    
116     Fatal("At","Index too large: (%ud < %ud violated) for %s",
117     idx, fNumEntries, GetName());
118     return 0;
119     }
120    
121     //--------------------------------------------------------------------------------------------------
122     template<class ArrayElement>
123     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
124     {
125     // Return entry at given index.
126    
127     if (idx<fNumEntries)
128     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
129    
130     Fatal("At","Index too large: (%ud < %ud violated) for %s",
131     idx, fNumEntries, GetName());
132     return 0;
133     }
134    
135     //--------------------------------------------------------------------------------------------------
136     template<class ArrayElement>
137     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
138     {
139     // Find object by name.
140    
141     return static_cast<const ArrayElement*>(fArray.FindObject(name));
142     }
143    
144     //--------------------------------------------------------------------------------------------------
145     template<class ArrayElement>
146     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
147     {
148     // Find object by name.
149    
150     return static_cast<ArrayElement*>(fArray.FindObject(name));
151     }
152    
153     //--------------------------------------------------------------------------------------------------
154     template<class ArrayElement>
155     inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
156     {
157     // Remove object at given index from array. You probably want to call Trim as well.
158    
159     fArray.RemoveAt(idx);
160     }
161    
162     //--------------------------------------------------------------------------------------------------
163     template<class ArrayElement>
164     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
165     {
166     // Remove object from array. You probably want to call Trim as well.
167    
168     TObject *obj = fArray.FindObject(name);
169     if (obj) fArray.Remove(obj);
170     }
171    
172     //--------------------------------------------------------------------------------------------------
173     template<class ArrayElement>
174     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
175     {
176     fArray.Remove(ae);
177     }
178    
179     //--------------------------------------------------------------------------------------------------
180     template<class ArrayElement>
181     inline void mithep::ObjArray<ArrayElement>::Reset()
182     {
183     // Default implementation for clearing the array.
184    
185     fArray.Clear(); //will delete objects if owner
186    
187     fNumEntries = 0;
188     }
189    
190     //--------------------------------------------------------------------------------------------------
191     template<class ArrayElement>
192     inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
193     {
194     // Return ROOT collection iterator.
195    
196     return fArray.MakeIterator(dir);
197     }
198    
199     //--------------------------------------------------------------------------------------------------
200     template<class ArrayElement>
201     inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
202     {
203     // Return entry at given index.
204    
205     return At(idx);
206     }
207    
208     //--------------------------------------------------------------------------------------------------
209     template<class ArrayElement>
210     inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
211     {
212     // Return entry at given index.
213    
214     return At(idx);
215     }
216    
217     //--------------------------------------------------------------------------------------------------
218     template<class ArrayElement>
219     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
220     {
221     // Return entry at given index.
222    
223     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
224     }
225    
226     //--------------------------------------------------------------------------------------------------
227     template<class ArrayElement>
228     inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
229     {
230     // Return entry at given index.
231    
232     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
233     }
234     #endif