ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Array.h
Revision: 1.10
Committed: Thu Feb 26 17:05:18 2009 UTC (16 years, 2 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.9: +13 -1 lines
Log Message:
Add FastArray (optimized heap array) and switch RefArray to use FastArray

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.10 // $Id: Array.h,v 1.9 2008/12/18 13:34:16 loizides Exp $
3 loizides 1.1 //
4     // Array
5     //
6     // Implementation of Collection interface using TClonesArray class.
7     //
8     // Authors: C.Loizides
9     //--------------------------------------------------------------------------------------------------
10    
11 loizides 1.2 #ifndef MITANA_DATACONT_ARRAY_H
12     #define MITANA_DATACONT_ARRAY_H
13 loizides 1.1
14     #include <TClonesArray.h>
15     #include "MitAna/DataCont/interface/Collection.h"
16    
17     namespace mithep
18     {
19     template<class ArrayElement>
20     class Array : public Collection<ArrayElement>
21     {
22     public:
23     Array(UInt_t size=0, const char *name=0);
24     ~Array() {}
25    
26     ArrayElement *AddNew();
27     ArrayElement *Allocate();
28     const TClonesArray &Arr() const { return fArray; }
29     TClonesArray &Arr() { return fArray; }
30     ArrayElement *At(UInt_t idx);
31     const ArrayElement *At(UInt_t idx) const;
32     void Clear(Option_t */*opt*/="") { fArray.~TClonesArray(); }
33 bendavid 1.10 void Delete();
34 loizides 1.1 UInt_t Entries() const { return fNumEntries; }
35     UInt_t GetEntries() const { return fNumEntries; }
36     const char *GetName() const { return fArray.GetName(); }
37 loizides 1.4 UInt_t GetSize() const { return fArray.GetSize(); }
38 bendavid 1.5 Bool_t HasObject(const ArrayElement *obj) const;
39 loizides 1.1 Bool_t IsOwner() const { return kTRUE; }
40     TIterator *Iterator(Bool_t dir = kIterForward) const;
41     Bool_t MustClear() const { return this->TestBit(14); }
42     Bool_t MustDelete() const { return this->TestBit(15); }
43 loizides 1.8 TObject *ObjAt(UInt_t idx);
44     const TObject *ObjAt(UInt_t idx) const;
45 loizides 1.4 void Print(Option_t *opt="") const;
46 loizides 1.1 void Reset();
47     void SetMustClearBit() { this->SetBit(14); }
48     void SetMustDeleteBit() { this->SetBit(15); }
49     void SetName(const char *name) { fArray.SetName(name); }
50 loizides 1.7 void Sort() { fArray.Sort(); }
51 loizides 1.4 void Trim() { fArray.Compress(); }
52 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
53     const ArrayElement *UncheckedAt(UInt_t idx) const;
54     ArrayElement *operator[](UInt_t idx);
55     const ArrayElement *operator[](UInt_t idx) const;
56    
57     protected:
58     TClonesArray fArray; //array for storage
59     UInt_t fNumEntries; //number of entries in the array
60    
61     private:
62     Array(const Array &a);
63    
64 loizides 1.8 ClassDefT(Array, 1) // Wrapper around TClonesArray class
65 loizides 1.1 };
66     }
67    
68     //--------------------------------------------------------------------------------------------------
69     template<class ArrayElement>
70     inline mithep::Array<ArrayElement>::Array(UInt_t size, const char *name) :
71     fArray(ArrayElement::Class_Name(),size),
72     fNumEntries(0)
73     {
74     // Default constructor.
75    
76     fArray.BypassStreamer(kTRUE);
77    
78     if (name)
79     fArray.SetName(name);
80    
81     ArrayElement test;
82     if (test.MustClear())
83     SetMustClearBit();
84     if (test.MustDelete())
85     SetMustDeleteBit();
86     }
87    
88     //--------------------------------------------------------------------------------------------------
89     template<class ArrayElement>
90     inline ArrayElement *mithep::Array<ArrayElement>::AddNew()
91     {
92     // Add new entry and return pointer to it.
93    
94     return new(Allocate()) ArrayElement();
95     }
96    
97     //--------------------------------------------------------------------------------------------------
98     template<class ArrayElement>
99     inline ArrayElement *mithep::Array<ArrayElement>::Allocate()
100     {
101     // Allocate a slot in the array, *only* to be used in placement new operator.
102    
103     return static_cast<ArrayElement*>(fArray[fNumEntries++]);
104     }
105    
106     //--------------------------------------------------------------------------------------------------
107     template<class ArrayElement>
108     inline ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx)
109     {
110     // Return entry at given index.
111    
112     if (idx<fNumEntries)
113     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
114    
115 loizides 1.9 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
116 loizides 1.8 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
117 loizides 1.1 return 0;
118     }
119    
120     //--------------------------------------------------------------------------------------------------
121     template<class ArrayElement>
122     inline const ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx) const
123     {
124     // Return entry at given index.
125    
126     if (idx<fNumEntries)
127     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
128    
129 loizides 1.9 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
130 loizides 1.8 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
131 loizides 1.1 return 0;
132     }
133    
134     //--------------------------------------------------------------------------------------------------
135     template<class ArrayElement>
136     inline TIterator *mithep::Array<ArrayElement>::Iterator(Bool_t dir) const
137     {
138     // Return ROOT collection iterator.
139    
140     return fArray.MakeIterator(dir);
141     }
142    
143     //--------------------------------------------------------------------------------------------------
144     template<class ArrayElement>
145 bendavid 1.5 inline Bool_t mithep::Array<ArrayElement>::HasObject(const ArrayElement *obj) const
146     {
147     // Check whether object is in array.
148    
149     return fArray.FindObject(obj);
150     }
151    
152     //--------------------------------------------------------------------------------------------------
153     template<class ArrayElement>
154 loizides 1.4 void mithep::Array<ArrayElement>::Print(Option_t *opt) const
155 loizides 1.1 {
156 loizides 1.4 // Print out elements of array.
157 loizides 1.1
158 loizides 1.4 printf("%s: Contains %d (out of %d) objs of name %s\n",
159     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
160 loizides 1.1
161 loizides 1.6 if (opt && opt[0]=='l') {
162     const UInt_t N = GetEntries();
163     for (UInt_t i=0; i<N; ++i) {
164     printf("%4d: ",i);
165     At(i)->Print(opt);
166     }
167 loizides 1.4 }
168 loizides 1.1 }
169    
170     //--------------------------------------------------------------------------------------------------
171     template<class ArrayElement>
172 loizides 1.8 inline TObject *mithep::Array<ArrayElement>::ObjAt(UInt_t idx)
173     {
174     // Return object at given index.
175    
176     if (idx<fNumEntries)
177     return fArray.UncheckedAt(idx);
178    
179 loizides 1.9 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
180 loizides 1.8 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
181     return 0;
182     }
183    
184     //--------------------------------------------------------------------------------------------------
185     template<class ArrayElement>
186     const TObject *mithep::Array<ArrayElement>::ObjAt(UInt_t idx) const
187     {
188     // Return object at given index.
189    
190     if (idx<fNumEntries)
191     return static_cast<const TObject*>(fArray.UncheckedAt(idx));
192    
193 loizides 1.9 TObject::Fatal("At","Index too large: (%u < %u violated) for %s containing %s",
194 loizides 1.8 idx, fNumEntries, GetName(), ArrayElement::Class_Name());
195     return 0;
196     }
197    
198     //--------------------------------------------------------------------------------------------------
199     template<class ArrayElement>
200 loizides 1.1 inline void mithep::Array<ArrayElement>::Reset()
201     {
202     // ROOT implementation for clearing the array.
203    
204 loizides 1.4 if (MustDelete())
205 loizides 1.1 fArray.Delete(); //will call destructor for every element
206 loizides 1.4 else if (MustClear())
207 loizides 1.1 fArray.Clear("C"); //will call clear for every element
208     else
209     fArray.Clear();
210    
211     fNumEntries = 0;
212     }
213    
214     //--------------------------------------------------------------------------------------------------
215     template<class ArrayElement>
216 bendavid 1.10 inline void mithep::Array<ArrayElement>::Delete()
217     {
218     // ROOT implementation for clearing the array, deleting all objects inside
219    
220     fArray.Delete(); //will call destructor for every element
221    
222     fNumEntries = 0;
223     }
224    
225     //--------------------------------------------------------------------------------------------------
226     template<class ArrayElement>
227 loizides 1.1 inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
228     {
229     // Return entry at given index.
230    
231     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
232     }
233    
234     //--------------------------------------------------------------------------------------------------
235     template<class ArrayElement>
236     inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
237     {
238     // Return entry at given index.
239    
240     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
241     }
242 loizides 1.4
243     //--------------------------------------------------------------------------------------------------
244     template<class ArrayElement>
245     inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
246     {
247     // Return entry at given index.
248    
249     return At(idx);
250     }
251    
252     //--------------------------------------------------------------------------------------------------
253     template<class ArrayElement>
254     inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
255     {
256     // Return entry at given index.
257    
258     return At(idx);
259     }
260 loizides 1.1 #endif