ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.5
Committed: Thu Nov 20 17:49:15 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.4: +34 -16 lines
Log Message:
Add Print plus Cleaned up.

File Contents

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