ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.3
Committed: Sat Sep 27 06:04:49 2008 UTC (16 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_005, Mit_004
Changes since 1.2: +4 -23 lines
Log Message:
Have one Add and AddLast pair.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.3 // $Id: ObjArray.h,v 1.2 2008/09/10 03:33:26 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 loizides 1.3 void Add(ArrayElement *ae) { AddLast(ae); }
27 loizides 1.1 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    
56     TObjArray fArray; //array for storage
57     UInt_t fNumEntries; //number of entries in the array
58    
59     private:
60     ObjArray(const ObjArray &a);
61    
62     ClassDefT(ObjArray,1) // Wrapper around TClonesArray class
63     };
64     }
65    
66     //--------------------------------------------------------------------------------------------------
67     template<class ArrayElement>
68     inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
69     fArray(size),
70     fNumEntries(0)
71     {
72     // Default constructor.
73    
74     if (name)
75     fArray.SetName(name);
76     }
77    
78     //--------------------------------------------------------------------------------------------------
79     template<class ArrayElement>
80 loizides 1.3 inline void mithep::ObjArray<ArrayElement>::AddLast(ArrayElement *ae)
81 loizides 1.1 {
82     // Add new entry at the end of array.
83    
84 loizides 1.3 fArray.AddLast(ae);
85 loizides 1.1 fNumEntries++;
86     }
87    
88     //--------------------------------------------------------------------------------------------------
89     template<class ArrayElement>
90     inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
91     {
92     // Return entry at given index.
93    
94     if (idx<fNumEntries)
95     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
96    
97     Fatal("At","Index too large: (%ud < %ud violated) for %s",
98     idx, fNumEntries, GetName());
99     return 0;
100     }
101    
102     //--------------------------------------------------------------------------------------------------
103     template<class ArrayElement>
104     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
105     {
106     // Return entry at given index.
107    
108     if (idx<fNumEntries)
109     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
110    
111     Fatal("At","Index too large: (%ud < %ud violated) for %s",
112     idx, fNumEntries, GetName());
113     return 0;
114     }
115    
116     //--------------------------------------------------------------------------------------------------
117     template<class ArrayElement>
118     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
119     {
120     // Find object by name.
121    
122     return static_cast<const ArrayElement*>(fArray.FindObject(name));
123     }
124    
125     //--------------------------------------------------------------------------------------------------
126     template<class ArrayElement>
127     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
128     {
129     // Find object by name.
130    
131     return static_cast<ArrayElement*>(fArray.FindObject(name));
132     }
133    
134     //--------------------------------------------------------------------------------------------------
135     template<class ArrayElement>
136     inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
137     {
138     // Remove object at given index from array. You probably want to call Trim as well.
139    
140     fArray.RemoveAt(idx);
141     }
142    
143     //--------------------------------------------------------------------------------------------------
144     template<class ArrayElement>
145     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
146     {
147     // Remove object from array. You probably want to call Trim as well.
148    
149     TObject *obj = fArray.FindObject(name);
150     if (obj) fArray.Remove(obj);
151     }
152    
153     //--------------------------------------------------------------------------------------------------
154     template<class ArrayElement>
155     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
156     {
157     fArray.Remove(ae);
158     }
159    
160     //--------------------------------------------------------------------------------------------------
161     template<class ArrayElement>
162     inline void mithep::ObjArray<ArrayElement>::Reset()
163     {
164     // Default implementation for clearing the array.
165    
166     fArray.Clear(); //will delete objects if owner
167    
168     fNumEntries = 0;
169     }
170    
171     //--------------------------------------------------------------------------------------------------
172     template<class ArrayElement>
173     inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
174     {
175     // Return ROOT collection iterator.
176    
177     return fArray.MakeIterator(dir);
178     }
179    
180     //--------------------------------------------------------------------------------------------------
181     template<class ArrayElement>
182     inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
183     {
184     // Return entry at given index.
185    
186     return At(idx);
187     }
188    
189     //--------------------------------------------------------------------------------------------------
190     template<class ArrayElement>
191     inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
192     {
193     // Return entry at given index.
194    
195     return At(idx);
196     }
197    
198     //--------------------------------------------------------------------------------------------------
199     template<class ArrayElement>
200     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
201     {
202     // Return entry at given index.
203    
204     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
205     }
206    
207     //--------------------------------------------------------------------------------------------------
208     template<class ArrayElement>
209     inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
210     {
211     // Return entry at given index.
212    
213     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
214     }
215     #endif