ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/ObjArray.h
Revision: 1.17
Committed: Mon May 18 06:27:48 2009 UTC (15 years, 11 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_025c_branch2, Mit_025c_branch1, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_025c_branch0, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e, Mit_014d, Mit_014c, Mit_014b, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012h, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, HEAD
Branch point for: Mit_025c_branch
Changes since 1.16: +28 -12 lines
Log Message:
Implemented trim to take care of removed slots.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.17 // $Id: ObjArray.h,v 1.16 2009/03/23 22:15:09 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 loizides 1.17 void Clear(Option_t */*opt*/="") { fArray.~TObjArray(); }
36     UInt_t Entries() const { return fNumEntries; }
37     UInt_t GetEntries() const { return fNumEntries; }
38 loizides 1.1 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 loizides 1.17 void Remove(const ArrayElement *ae);
49 loizides 1.1 Bool_t IsOwner() const { return fArray.IsOwner(); }
50     TIterator *Iterator(Bool_t dir = kIterForward) const;
51     void Reset();
52 loizides 1.17 void SetName(const char *name) { fArray.SetName(name); }
53 bendavid 1.8 void SetOwner(Bool_t o);
54 loizides 1.17 void Sort() { fArray.Sort(); }
55     void Trim();
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.17 ClassDef(ObjArray, 1) // Wrapper around TObjArray 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.16 TObject::Error("Add", "Cannot 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 loizides 1.16 TObject::Error("Add", "Cannot add collection since IsOwner() returns kTRUE.");
122 loizides 1.11 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.16 TObject::Error("Add", "Cannot 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 loizides 1.15 BaseCollection::Clear();
156 loizides 1.1 }
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 loizides 1.16 TObject::Error("AddOwned","Cannot add object since IsOwner() returns kFALSE.");
166 bendavid 1.8 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 loizides 1.17 inline void mithep::ObjArray<ArrayElement>::Remove(const ArrayElement *ae)
282 loizides 1.1 {
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.17 fArray.Remove(const_cast<ArrayElement*>(ae));
289 loizides 1.1 }
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 loizides 1.14 BaseCollection::Clear();
301 loizides 1.1 }
302    
303     //--------------------------------------------------------------------------------------------------
304     template<class ArrayElement>
305 bendavid 1.8 inline void mithep::ObjArray<ArrayElement>::SetOwner(Bool_t o)
306     {
307     // Set ownership of array. Must do this before adding any objects
308    
309     if (fNumEntries) {
310     TObject::Error("SetOwner","Cannot safely change ownership of non-empty array.");
311     return;
312     }
313    
314     fArray.SetOwner(o);
315     }
316    
317     //--------------------------------------------------------------------------------------------------
318     template<class ArrayElement>
319 loizides 1.1 inline TIterator *mithep::ObjArray<ArrayElement>::Iterator(Bool_t dir) const
320     {
321     // Return ROOT collection iterator.
322    
323     return fArray.MakeIterator(dir);
324     }
325    
326     //--------------------------------------------------------------------------------------------------
327     template<class ArrayElement>
328 loizides 1.5 void mithep::ObjArray<ArrayElement>::Print(Option_t *opt) const
329     {
330     // Print out elements of array.
331    
332     printf("%s: Contains %d (out of %d) objs of name %s\n",
333     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
334    
335 loizides 1.9 if (opt && opt[0]=='l') {
336     const UInt_t N = GetEntries();
337     for (UInt_t i=0; i<N; ++i) {
338     printf("%4d: ",i);
339 loizides 1.17 if (At(i))
340     At(i)->Print(opt);
341     else
342     printf("Removed\n");
343 loizides 1.9 }
344 loizides 1.5 }
345     }
346    
347     //--------------------------------------------------------------------------------------------------
348     template<class ArrayElement>
349 loizides 1.17 inline void mithep::ObjArray<ArrayElement>::Trim()
350     {
351     // Trim array.
352    
353     if (static_cast<Int_t>(fNumEntries) != fArray.GetEntries()) {
354     fArray.Compress();
355     fNumEntries = fArray.GetEntriesFast();
356     BaseCollection::Clear();
357     }
358     }
359    
360     //--------------------------------------------------------------------------------------------------
361     template<class ArrayElement>
362 loizides 1.5 inline ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx)
363 loizides 1.1 {
364     // Return entry at given index.
365    
366 loizides 1.5 return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
367 loizides 1.1 }
368    
369     //--------------------------------------------------------------------------------------------------
370     template<class ArrayElement>
371 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::UncheckedAt(UInt_t idx) const
372 loizides 1.1 {
373     // Return entry at given index.
374    
375 loizides 1.5 return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
376 loizides 1.1 }
377    
378     //--------------------------------------------------------------------------------------------------
379     template<class ArrayElement>
380 loizides 1.5 inline const ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx) const
381 loizides 1.1 {
382     // Return entry at given index.
383    
384 loizides 1.5 return At(idx);
385 loizides 1.1 }
386    
387     //--------------------------------------------------------------------------------------------------
388     template<class ArrayElement>
389 loizides 1.5 inline ArrayElement *mithep::ObjArray<ArrayElement>::operator[](UInt_t idx)
390 loizides 1.1 {
391     // Return entry at given index.
392    
393 loizides 1.5 return At(idx);
394 loizides 1.1 }
395     #endif