ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.12
Committed: Thu Dec 18 13:34:16 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006b, Mit_006a
Changes since 1.11: +5 -8 lines
Log Message:
Fix printout.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.12 // $Id: ObjArray.h,v 1.11 2008/12/10 11:26:52 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.11 void Add(const BaseCollection *col);
30 loizides 1.4 void Add(const TCollection *col);
31 loizides 1.6 void AddOwned(ArrayElement *ae);
32 loizides 1.1 const TObjArray &Arr() const { return fArray; }
33     TObjArray &Arr() { return fArray; }
34     ArrayElement *At(UInt_t idx);
35     const ArrayElement *At(UInt_t idx) const;
36     void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
37     UInt_t Entries() const { return fNumEntries; }
38     UInt_t GetEntries() const { return fNumEntries; }
39     const char* GetName() const { return fArray.GetName(); }
40 loizides 1.6 UInt_t GetSize() const { return fArray.GetSize(); }
41 bendavid 1.7 Bool_t HasObject(const ArrayElement *obj) const;
42 loizides 1.1 const ArrayElement *Find(const char *name) const;
43     ArrayElement *Find(const char *name);
44 bendavid 1.7 void Print(Option_t *opt="") const;
45 loizides 1.11 TObject *ObjAt(UInt_t idx);
46     const TObject *ObjAt(UInt_t idx) const;
47 loizides 1.1 void Remove(UInt_t idx);
48     void Remove(const char *name);
49     void Remove(ArrayElement *ae);
50     Bool_t IsOwner() const { return fArray.IsOwner(); }
51     TIterator *Iterator(Bool_t dir = kIterForward) const;
52     void Reset();
53 loizides 1.6 void SetName(const char *name) { fArray.SetName(name); }
54 bendavid 1.8 void SetOwner(Bool_t o);
55 loizides 1.1 void Sort() { fArray.Sort(); }
56 loizides 1.5 void Trim() { fArray.Compress();}
57 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
58     const ArrayElement *UncheckedAt(UInt_t idx) const;
59     ArrayElement *operator[](UInt_t idx);
60     const ArrayElement *operator[](UInt_t idx) const;
61    
62     protected:
63 loizides 1.6 void AddLast(const ArrayElement *e);
64 loizides 1.1
65     TObjArray fArray; //array for storage
66     UInt_t fNumEntries; //number of entries in the array
67    
68     private:
69     ObjArray(const ObjArray &a);
70    
71 loizides 1.11 ClassDefT(ObjArray, 1) // Wrapper around TClonesArray class
72 loizides 1.1 };
73     }
74    
75     //--------------------------------------------------------------------------------------------------
76     template<class ArrayElement>
77     inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
78     fArray(size),
79     fNumEntries(0)
80     {
81     // Default constructor.
82    
83     if (name)
84     fArray.SetName(name);
85     }
86    
87     //--------------------------------------------------------------------------------------------------
88     template<class ArrayElement>
89 loizides 1.4 inline void mithep::ObjArray<ArrayElement>::Add(const TCollection *col)
90     {
91 loizides 1.5 // Add objects from collection to array.
92    
93 loizides 1.4 if (!col)
94     return;
95    
96     if (IsOwner()) {
97 loizides 1.11 TObject::Error("Add", "Can not add collection since IsOwner() returns kTRUE.");
98 loizides 1.4 return;
99     }
100    
101 loizides 1.11 TIter iter(col->MakeIterator());
102     while (1) {
103     TObject *obj = iter.Next();
104     if (!obj)
105     break;
106     const ArrayElement *to = dynamic_cast<const ArrayElement*>(obj);
107     if (to)
108     AddLast(to);
109     }
110     }
111    
112     //--------------------------------------------------------------------------------------------------
113     template<class ArrayElement>
114     inline void mithep::ObjArray<ArrayElement>::Add(const BaseCollection *col)
115     {
116     // Add objects from collection to array.
117    
118     if (!col)
119     return;
120    
121     if (IsOwner()) {
122     TObject::Error("Add", "Can not add collection since IsOwner() returns kTRUE.");
123     return;
124     }
125    
126     UInt_t n = col->GetEntries();
127     for (UInt_t i=0; i<n; ++i) {
128     const ArrayElement *to = dynamic_cast<const ArrayElement*>(col->ObjAt(i));
129     if (to)
130 loizides 1.4 AddLast(to);
131     }
132     }
133    
134     //--------------------------------------------------------------------------------------------------
135     template<class ArrayElement>
136 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::Add(const ArrayElement *ae)
137     {
138     // Add object to array. This function should be used in cases the array does not own the objects.
139    
140     if (IsOwner()) {
141 loizides 1.11 TObject::Error("Add", "Can not add object since IsOwner() returns kTRUE.");
142 loizides 1.6 return;
143     }
144    
145     AddLast(ae);
146     }
147    
148     //--------------------------------------------------------------------------------------------------
149     template<class ArrayElement>
150     inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *ae)
151 loizides 1.1 {
152     // Add new entry at the end of array.
153    
154 loizides 1.6 fArray.AddLast(const_cast<ArrayElement*>(ae));
155 loizides 1.1 fNumEntries++;
156     }
157    
158     //--------------------------------------------------------------------------------------------------
159     template<class ArrayElement>
160 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::AddOwned(ArrayElement *ae)
161     {
162     // Add object to array. This function should be used in cases the array owns the objects.
163    
164 bendavid 1.8 if (!IsOwner()) {
165     TObject::Error("AddOwned","Can not add object since IsOwner() returns kFALSE.");
166     return;
167     }
168    
169 loizides 1.6 AddLast(ae);
170     }
171    
172     //--------------------------------------------------------------------------------------------------
173     template<class ArrayElement>
174 loizides 1.1 inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
175     {
176     // Return entry at given index.
177    
178     if (idx<fNumEntries)
179     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
180    
181 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
182 loizides 1.5 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
183 loizides 1.1 return 0;
184     }
185    
186     //--------------------------------------------------------------------------------------------------
187     template<class ArrayElement>
188     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
189     {
190     // Return entry at given index.
191    
192     if (idx<fNumEntries)
193     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
194    
195 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
196 loizides 1.5 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
197 loizides 1.1 return 0;
198     }
199    
200     //--------------------------------------------------------------------------------------------------
201     template<class ArrayElement>
202     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
203     {
204     // Find object by name.
205    
206     return static_cast<const ArrayElement*>(fArray.FindObject(name));
207     }
208    
209     //--------------------------------------------------------------------------------------------------
210     template<class ArrayElement>
211     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
212     {
213     // Find object by name.
214    
215     return static_cast<ArrayElement*>(fArray.FindObject(name));
216     }
217    
218     //--------------------------------------------------------------------------------------------------
219     template<class ArrayElement>
220 bendavid 1.7 inline Bool_t mithep::ObjArray<ArrayElement>::HasObject(const ArrayElement *obj) const
221     {
222     // Check whether object is in array.
223    
224     return fArray.FindObject(obj);
225     }
226    
227     //--------------------------------------------------------------------------------------------------
228     template<class ArrayElement>
229 loizides 1.11 TObject *mithep::ObjArray<ArrayElement>::ObjAt(UInt_t idx)
230     {
231     // Return object at given index.
232    
233     if (idx<fNumEntries)
234     return fArray.UncheckedAt(idx);
235    
236 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
237 loizides 1.11 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
238     return 0;
239     }
240    
241     //--------------------------------------------------------------------------------------------------
242     template<class ArrayElement>
243     const TObject *mithep::ObjArray<ArrayElement>::ObjAt(UInt_t idx) const
244     {
245     // Return object at given index.
246    
247     if (idx<fNumEntries)
248     return static_cast<const TObject*>(fArray.UncheckedAt(idx));
249    
250 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
251 loizides 1.11 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
252     return 0;
253     }
254    
255     //--------------------------------------------------------------------------------------------------
256     template<class ArrayElement>
257 loizides 1.1 inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
258     {
259 loizides 1.11 // Remove object at given index from array. You probably want to call Trim() as well.
260    
261     if (idx>=fNumEntries)
262     return;
263 loizides 1.1
264     fArray.RemoveAt(idx);
265     }
266    
267     //--------------------------------------------------------------------------------------------------
268     template<class ArrayElement>
269     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
270     {
271 loizides 1.11 // Remove object with given name from array. You probably want to call Trim() as well.
272 loizides 1.1
273     TObject *obj = fArray.FindObject(name);
274 loizides 1.11 if (!obj)
275     return;
276     fArray.Remove(obj);
277 loizides 1.1 }
278    
279     //--------------------------------------------------------------------------------------------------
280     template<class ArrayElement>
281     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
282     {
283 loizides 1.11 // Remove object from array. You probably want to call Trim() as well.
284    
285     if (!ae)
286     return;
287    
288 loizides 1.1 fArray.Remove(ae);
289     }
290    
291     //--------------------------------------------------------------------------------------------------
292     template<class ArrayElement>
293     inline void mithep::ObjArray<ArrayElement>::Reset()
294     {
295     // Default implementation for clearing the array.
296    
297     fArray.Clear(); //will delete objects if owner
298    
299     fNumEntries = 0;
300     }
301    
302     //--------------------------------------------------------------------------------------------------
303     template<class ArrayElement>
304 bendavid 1.8 inline void mithep::ObjArray<ArrayElement>::SetOwner(Bool_t o)
305     {
306     // Set ownership of array. Must do this before adding any objects
307    
308     if (fNumEntries) {
309     TObject::Error("SetOwner","Cannot safely change ownership of non-empty array.");
310     return;
311     }
312    
313     fArray.SetOwner(o);
314     }
315    
316     //--------------------------------------------------------------------------------------------------
317     template<class ArrayElement>
318 loizides 1.1 inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
319     {
320     // Return ROOT collection iterator.
321    
322     return fArray.MakeIterator(dir);
323     }
324    
325     //--------------------------------------------------------------------------------------------------
326     template<class ArrayElement>
327 loizides 1.5 void mithep::ObjArray<ArrayElement>::Print(Option_t *opt) const
328     {
329     // Print out elements of array.
330    
331     printf("%s: Contains %d (out of %d) objs of name %s\n",
332     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
333    
334 loizides 1.9 if (opt && opt[0]=='l') {
335     const UInt_t N = GetEntries();
336     for (UInt_t i=0; i<N; ++i) {
337     printf("%4d: ",i);
338     At(i)->Print(opt);
339     }
340 loizides 1.5 }
341     }
342    
343     //--------------------------------------------------------------------------------------------------
344     template<class ArrayElement>
345     inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
346 loizides 1.1 {
347     // Return entry at given index.
348    
349 loizides 1.5 return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
350 loizides 1.1 }
351    
352     //--------------------------------------------------------------------------------------------------
353     template<class ArrayElement>
354 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
355 loizides 1.1 {
356     // Return entry at given index.
357    
358 loizides 1.5 return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
359 loizides 1.1 }
360    
361     //--------------------------------------------------------------------------------------------------
362     template<class ArrayElement>
363 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
364 loizides 1.1 {
365     // Return entry at given index.
366    
367 loizides 1.5 return At(idx);
368 loizides 1.1 }
369    
370     //--------------------------------------------------------------------------------------------------
371     template<class ArrayElement>
372 loizides 1.5 inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
373 loizides 1.1 {
374     // Return entry at given index.
375    
376 loizides 1.5 return At(idx);
377 loizides 1.1 }
378     #endif