ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.4
Committed: Thu Oct 23 18:21:57 2008 UTC (16 years, 6 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006
Changes since 1.3: +29 -5 lines
Log Message:
Add function to add pointers to members of a TCollections.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.4 // $Id: ObjArray.h,v 1.3 2008/09/27 06:04:49 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.4 void Add(const TCollection *col);
28 loizides 1.1 const TObjArray &Arr() const { return fArray; }
29     TObjArray &Arr() { return fArray; }
30     ArrayElement *At(UInt_t idx);
31     const ArrayElement *At(UInt_t idx) const;
32     void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
33     UInt_t Entries() const { return fNumEntries; }
34     UInt_t GetEntries() const { return fNumEntries; }
35     const char* GetName() const { return fArray.GetName(); }
36     const ArrayElement *Find(const char *name) const;
37     ArrayElement *Find(const char *name);
38     void Remove(UInt_t idx);
39     void Remove(const char *name);
40     void Remove(ArrayElement *ae);
41     Bool_t IsOwner() const { return fArray.IsOwner(); }
42     TIterator *Iterator(Bool_t dir = kIterForward) const;
43     void Reset();
44     void Trim() { fArray.Compress();}
45     void SetName(const char* name) { fArray.SetName(name); }
46     void SetOwner(Bool_t o) { fArray.SetOwner(o); }
47     void Sort() { fArray.Sort(); }
48     ArrayElement *UncheckedAt(UInt_t idx);
49     const ArrayElement *UncheckedAt(UInt_t idx) const;
50    
51     ArrayElement *operator[](UInt_t idx);
52     const ArrayElement *operator[](UInt_t idx) const;
53    
54     protected:
55     void AddLast(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 loizides 1.4 inline void mithep::ObjArray<ArrayElement>::Add(const TCollection *col)
82     {
83     if (!col)
84     return;
85    
86     if (IsOwner()) {
87     TObject::Error("Add","Can not add collection since IsOwner() returns kTRUE.");
88     return;
89     }
90    
91     TIterator *iter = col->MakeIterator();
92     if (iter) {
93     ArrayElement *to = dynamic_cast<ArrayElement *>(iter->Next());
94     while (to) {
95     AddLast(to);
96     to = dynamic_cast<ArrayElement *>(iter->Next());
97     }
98     }
99     }
100    
101     //--------------------------------------------------------------------------------------------------
102     template<class ArrayElement>
103 loizides 1.3 inline void mithep::ObjArray<ArrayElement>::AddLast(ArrayElement *ae)
104 loizides 1.1 {
105     // Add new entry at the end of array.
106    
107 loizides 1.3 fArray.AddLast(ae);
108 loizides 1.1 fNumEntries++;
109     }
110    
111     //--------------------------------------------------------------------------------------------------
112     template<class ArrayElement>
113     inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
114     {
115     // Return entry at given index.
116    
117     if (idx<fNumEntries)
118     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
119    
120 loizides 1.4 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s",
121     idx, fNumEntries, GetName());
122 loizides 1.1 return 0;
123     }
124    
125     //--------------------------------------------------------------------------------------------------
126     template<class ArrayElement>
127     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
128     {
129     // Return entry at given index.
130    
131     if (idx<fNumEntries)
132     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
133    
134 loizides 1.4 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s",
135     idx, fNumEntries, GetName());
136 loizides 1.1 return 0;
137     }
138    
139     //--------------------------------------------------------------------------------------------------
140     template<class ArrayElement>
141     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
142     {
143     // Find object by name.
144    
145     return static_cast<const ArrayElement*>(fArray.FindObject(name));
146     }
147    
148     //--------------------------------------------------------------------------------------------------
149     template<class ArrayElement>
150     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
151     {
152     // Find object by name.
153    
154     return static_cast<ArrayElement*>(fArray.FindObject(name));
155     }
156    
157     //--------------------------------------------------------------------------------------------------
158     template<class ArrayElement>
159     inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
160     {
161     // Remove object at given index from array. You probably want to call Trim as well.
162    
163     fArray.RemoveAt(idx);
164     }
165    
166     //--------------------------------------------------------------------------------------------------
167     template<class ArrayElement>
168     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
169     {
170     // Remove object from array. You probably want to call Trim as well.
171    
172     TObject *obj = fArray.FindObject(name);
173     if (obj) fArray.Remove(obj);
174     }
175    
176     //--------------------------------------------------------------------------------------------------
177     template<class ArrayElement>
178     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
179     {
180     fArray.Remove(ae);
181     }
182    
183     //--------------------------------------------------------------------------------------------------
184     template<class ArrayElement>
185     inline void mithep::ObjArray<ArrayElement>::Reset()
186     {
187     // Default implementation for clearing the array.
188    
189     fArray.Clear(); //will delete objects if owner
190    
191     fNumEntries = 0;
192     }
193    
194     //--------------------------------------------------------------------------------------------------
195     template<class ArrayElement>
196     inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
197     {
198     // Return ROOT collection iterator.
199    
200     return fArray.MakeIterator(dir);
201     }
202    
203     //--------------------------------------------------------------------------------------------------
204     template<class ArrayElement>
205     inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
206     {
207     // Return entry at given index.
208    
209     return At(idx);
210     }
211    
212     //--------------------------------------------------------------------------------------------------
213     template<class ArrayElement>
214     inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
215     {
216     // Return entry at given index.
217    
218     return At(idx);
219     }
220    
221     //--------------------------------------------------------------------------------------------------
222     template<class ArrayElement>
223     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
224     {
225     // Return entry at given index.
226    
227     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
228     }
229    
230     //--------------------------------------------------------------------------------------------------
231     template<class ArrayElement>
232     inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
233     {
234     // Return entry at given index.
235    
236     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
237     }
238 loizides 1.4
239 loizides 1.1 #endif