ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.14
Committed: Thu Mar 12 18:19:48 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_008pre2
Changes since 1.13: +2 -1 lines
Log Message:
Proper resetting of the cached entry in BaseCollection.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.14 // $Id: ObjArray.h,v 1.13 2009/03/02 12:34:00 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    
27 loizides 1.6 void Add(const ArrayElement *ae);
28 loizides 1.11 void Add(const BaseCollection *col);
29 loizides 1.4 void Add(const TCollection *col);
30 loizides 1.6 void AddOwned(ArrayElement *ae);
31 loizides 1.1 const TObjArray &Arr() const { return fArray; }
32     TObjArray &Arr() { return fArray; }
33     ArrayElement *At(UInt_t idx);
34     const ArrayElement *At(UInt_t idx) const;
35     void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
36     UInt_t Entries() const { return fNumEntries; }
37     UInt_t GetEntries() const { return fNumEntries; }
38     const char* GetName() const { return fArray.GetName(); }
39 loizides 1.6 UInt_t GetSize() const { return fArray.GetSize(); }
40 bendavid 1.7 Bool_t HasObject(const ArrayElement *obj) const;
41 loizides 1.1 const ArrayElement *Find(const char *name) const;
42     ArrayElement *Find(const char *name);
43 bendavid 1.7 void Print(Option_t *opt="") const;
44 loizides 1.11 TObject *ObjAt(UInt_t idx);
45     const TObject *ObjAt(UInt_t idx) const;
46 loizides 1.1 void Remove(UInt_t idx);
47     void Remove(const char *name);
48     void Remove(ArrayElement *ae);
49     Bool_t IsOwner() const { return fArray.IsOwner(); }
50     TIterator *Iterator(Bool_t dir = kIterForward) const;
51     void Reset();
52 loizides 1.6 void SetName(const char *name) { fArray.SetName(name); }
53 bendavid 1.8 void SetOwner(Bool_t o);
54 loizides 1.1 void Sort() { fArray.Sort(); }
55 loizides 1.5 void Trim() { fArray.Compress();}
56 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
57     const ArrayElement *UncheckedAt(UInt_t idx) const;
58     ArrayElement *operator[](UInt_t idx);
59     const ArrayElement *operator[](UInt_t idx) const;
60    
61     protected:
62 loizides 1.6 void AddLast(const ArrayElement *e);
63 loizides 1.1
64     TObjArray fArray; //array for storage
65     UInt_t fNumEntries; //number of entries in the array
66    
67     private:
68     ObjArray(const ObjArray &a);
69    
70 loizides 1.13 ClassDef(ObjArray, 1) // Wrapper around TClonesArray class
71 loizides 1.1 };
72     }
73    
74     //--------------------------------------------------------------------------------------------------
75     template<class ArrayElement>
76     inline mithep::ObjArray<ArrayElement>::ObjArray(UInt_t size, const char *name) :
77     fArray(size),
78     fNumEntries(0)
79     {
80     // Default constructor.
81    
82     if (name)
83     fArray.SetName(name);
84     }
85    
86     //--------------------------------------------------------------------------------------------------
87     template<class ArrayElement>
88 loizides 1.4 inline void mithep::ObjArray<ArrayElement>::Add(const TCollection *col)
89     {
90 loizides 1.5 // Add objects from collection to array.
91    
92 loizides 1.4 if (!col)
93     return;
94    
95     if (IsOwner()) {
96 loizides 1.11 TObject::Error("Add", "Can not add collection since IsOwner() returns kTRUE.");
97 loizides 1.4 return;
98     }
99    
100 loizides 1.11 TIter iter(col->MakeIterator());
101     while (1) {
102     TObject *obj = iter.Next();
103     if (!obj)
104     break;
105     const ArrayElement *to = dynamic_cast<const ArrayElement*>(obj);
106     if (to)
107     AddLast(to);
108     }
109     }
110    
111     //--------------------------------------------------------------------------------------------------
112     template<class ArrayElement>
113     inline void mithep::ObjArray<ArrayElement>::Add(const BaseCollection *col)
114     {
115     // Add objects from collection to array.
116    
117     if (!col)
118     return;
119    
120     if (IsOwner()) {
121     TObject::Error("Add", "Can not add collection since IsOwner() returns kTRUE.");
122     return;
123     }
124    
125     UInt_t n = col->GetEntries();
126     for (UInt_t i=0; i<n; ++i) {
127     const ArrayElement *to = dynamic_cast<const ArrayElement*>(col->ObjAt(i));
128     if (to)
129 loizides 1.4 AddLast(to);
130     }
131     }
132    
133     //--------------------------------------------------------------------------------------------------
134     template<class ArrayElement>
135 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::Add(const ArrayElement *ae)
136     {
137     // Add object to array. This function should be used in cases the array does not own the objects.
138    
139     if (IsOwner()) {
140 loizides 1.11 TObject::Error("Add", "Can not add object since IsOwner() returns kTRUE.");
141 loizides 1.6 return;
142     }
143    
144     AddLast(ae);
145     }
146    
147     //--------------------------------------------------------------------------------------------------
148     template<class ArrayElement>
149     inline void mithep::ObjArray<ArrayElement>::AddLast(const ArrayElement *ae)
150 loizides 1.1 {
151     // Add new entry at the end of array.
152    
153 loizides 1.6 fArray.AddLast(const_cast<ArrayElement*>(ae));
154 loizides 1.1 fNumEntries++;
155     }
156    
157     //--------------------------------------------------------------------------------------------------
158     template<class ArrayElement>
159 loizides 1.6 inline void mithep::ObjArray<ArrayElement>::AddOwned(ArrayElement *ae)
160     {
161     // Add object to array. This function should be used in cases the array owns the objects.
162    
163 bendavid 1.8 if (!IsOwner()) {
164     TObject::Error("AddOwned","Can not add object since IsOwner() returns kFALSE.");
165     return;
166     }
167    
168 loizides 1.6 AddLast(ae);
169     }
170    
171     //--------------------------------------------------------------------------------------------------
172     template<class ArrayElement>
173 loizides 1.1 inline ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx)
174     {
175     // Return entry at given index.
176    
177     if (idx<fNumEntries)
178     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
179    
180 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
181 loizides 1.5 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
182 loizides 1.1 return 0;
183     }
184    
185     //--------------------------------------------------------------------------------------------------
186     template<class ArrayElement>
187     inline const ArrayElement* mithep::ObjArray<ArrayElement>::At(UInt_t idx) const
188     {
189     // Return entry at given index.
190    
191     if (idx<fNumEntries)
192     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
193    
194 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
195 loizides 1.5 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
196 loizides 1.1 return 0;
197     }
198    
199     //--------------------------------------------------------------------------------------------------
200     template<class ArrayElement>
201     inline const ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name) const
202     {
203     // Find object by name.
204    
205     return static_cast<const ArrayElement*>(fArray.FindObject(name));
206     }
207    
208     //--------------------------------------------------------------------------------------------------
209     template<class ArrayElement>
210     inline ArrayElement *mithep::ObjArray<ArrayElement>::Find(const char *name)
211     {
212     // Find object by name.
213    
214     return static_cast<ArrayElement*>(fArray.FindObject(name));
215     }
216    
217     //--------------------------------------------------------------------------------------------------
218     template<class ArrayElement>
219 bendavid 1.7 inline Bool_t mithep::ObjArray<ArrayElement>::HasObject(const ArrayElement *obj) const
220     {
221     // Check whether object is in array.
222    
223     return fArray.FindObject(obj);
224     }
225    
226     //--------------------------------------------------------------------------------------------------
227     template<class ArrayElement>
228 loizides 1.11 TObject *mithep::ObjArray<ArrayElement>::ObjAt(UInt_t idx)
229     {
230     // Return object at given index.
231    
232     if (idx<fNumEntries)
233     return fArray.UncheckedAt(idx);
234    
235 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
236 loizides 1.11 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
237     return 0;
238     }
239    
240     //--------------------------------------------------------------------------------------------------
241     template<class ArrayElement>
242     const TObject *mithep::ObjArray<ArrayElement>::ObjAt(UInt_t idx) const
243     {
244     // Return object at given index.
245    
246     if (idx<fNumEntries)
247     return static_cast<const TObject*>(fArray.UncheckedAt(idx));
248    
249 loizides 1.12 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
250 loizides 1.11 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
251     return 0;
252     }
253    
254     //--------------------------------------------------------------------------------------------------
255     template<class ArrayElement>
256 loizides 1.1 inline void mithep::ObjArray<ArrayElement>::Remove(UInt_t idx)
257     {
258 loizides 1.11 // Remove object at given index from array. You probably want to call Trim() as well.
259    
260     if (idx>=fNumEntries)
261     return;
262 loizides 1.1
263     fArray.RemoveAt(idx);
264     }
265    
266     //--------------------------------------------------------------------------------------------------
267     template<class ArrayElement>
268     inline void mithep::ObjArray<ArrayElement>::Remove(const char *name)
269     {
270 loizides 1.11 // Remove object with given name from array. You probably want to call Trim() as well.
271 loizides 1.1
272     TObject *obj = fArray.FindObject(name);
273 loizides 1.11 if (!obj)
274     return;
275     fArray.Remove(obj);
276 loizides 1.1 }
277    
278     //--------------------------------------------------------------------------------------------------
279     template<class ArrayElement>
280     inline void mithep::ObjArray<ArrayElement>::Remove(ArrayElement *ae)
281     {
282 loizides 1.11 // Remove object from array. You probably want to call Trim() as well.
283    
284     if (!ae)
285     return;
286    
287 loizides 1.1 fArray.Remove(ae);
288     }
289    
290     //--------------------------------------------------------------------------------------------------
291     template<class ArrayElement>
292     inline void mithep::ObjArray<ArrayElement>::Reset()
293     {
294     // Default implementation for clearing the array.
295    
296     fArray.Clear(); //will delete objects if owner
297    
298     fNumEntries = 0;
299 loizides 1.14 BaseCollection::Clear();
300 loizides 1.1 }
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