ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Array.h
Revision: 1.14
Committed: Mon Mar 23 13:07:17 2009 UTC (16 years, 1 month 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, Mit_009, Mit_008, HEAD
Branch point for: Mit_025c_branch
Changes since 1.13: +3 -2 lines
Log Message:
Reset entries cache when objects are added

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.14 // $Id: Array.h,v 1.13 2009/03/12 18:19:47 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    
25     ArrayElement *AddNew();
26     ArrayElement *Allocate();
27     const TClonesArray &Arr() const { return fArray; }
28     TClonesArray &Arr() { return fArray; }
29     ArrayElement *At(UInt_t idx);
30     const ArrayElement *At(UInt_t idx) const;
31     void Clear(Option_t */*opt*/="") { fArray.~TClonesArray(); }
32 bendavid 1.10 void Delete();
33 loizides 1.1 UInt_t Entries() const { return fNumEntries; }
34     UInt_t GetEntries() const { return fNumEntries; }
35     const char *GetName() const { return fArray.GetName(); }
36 loizides 1.4 UInt_t GetSize() const { return fArray.GetSize(); }
37 bendavid 1.5 Bool_t HasObject(const ArrayElement *obj) const;
38 loizides 1.1 Bool_t IsOwner() const { return kTRUE; }
39     TIterator *Iterator(Bool_t dir = kIterForward) const;
40     Bool_t MustClear() const { return this->TestBit(14); }
41     Bool_t MustDelete() const { return this->TestBit(15); }
42 loizides 1.8 TObject *ObjAt(UInt_t idx);
43     const TObject *ObjAt(UInt_t idx) const;
44 loizides 1.4 void Print(Option_t *opt="") const;
45 loizides 1.1 void Reset();
46     void SetMustClearBit() { this->SetBit(14); }
47     void SetMustDeleteBit() { this->SetBit(15); }
48     void SetName(const char *name) { fArray.SetName(name); }
49 loizides 1.7 void Sort() { fArray.Sort(); }
50 loizides 1.4 void Trim() { fArray.Compress(); }
51 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
52     const ArrayElement *UncheckedAt(UInt_t idx) const;
53     ArrayElement *operator[](UInt_t idx);
54     const ArrayElement *operator[](UInt_t idx) const;
55    
56     protected:
57     TClonesArray fArray; //array for storage
58     UInt_t fNumEntries; //number of entries in the array
59    
60     private:
61     Array(const Array &a);
62    
63 loizides 1.11 ClassDef(Array, 1) // Wrapper around TClonesArray class
64 loizides 1.1 };
65     }
66    
67     //--------------------------------------------------------------------------------------------------
68     template<class ArrayElement>
69     inline mithep::Array<ArrayElement>::Array(UInt_t size, const char *name) :
70     fArray(ArrayElement::Class_Name(),size),
71     fNumEntries(0)
72     {
73     // Default constructor.
74    
75     fArray.BypassStreamer(kTRUE);
76    
77     if (name)
78     fArray.SetName(name);
79    
80     ArrayElement test;
81     if (test.MustClear())
82     SetMustClearBit();
83     if (test.MustDelete())
84     SetMustDeleteBit();
85     }
86    
87     //--------------------------------------------------------------------------------------------------
88     template<class ArrayElement>
89     inline ArrayElement *mithep::Array<ArrayElement>::AddNew()
90     {
91     // Add new entry and return pointer to it.
92    
93     return new(Allocate()) ArrayElement();
94     }
95    
96     //--------------------------------------------------------------------------------------------------
97     template<class ArrayElement>
98     inline ArrayElement *mithep::Array<ArrayElement>::Allocate()
99     {
100     // Allocate a slot in the array, *only* to be used in placement new operator.
101    
102 loizides 1.14 BaseCollection::Clear();
103     return static_cast<ArrayElement*>(fArray[fNumEntries++]);
104 loizides 1.1 }
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 loizides 1.12 GetName(), Entries(), GetSize(), ArrayElement::Class_Name());
160 loizides 1.1
161 loizides 1.6 if (opt && opt[0]=='l') {
162 loizides 1.12 const UInt_t N = Entries();
163 loizides 1.6 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 loizides 1.13 // ROOT-like implementation for clearing the array.
203 loizides 1.1
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 loizides 1.13 BaseCollection::Clear();
213 loizides 1.1 }
214    
215     //--------------------------------------------------------------------------------------------------
216     template<class ArrayElement>
217 bendavid 1.10 inline void mithep::Array<ArrayElement>::Delete()
218     {
219     // ROOT implementation for clearing the array, deleting all objects inside
220    
221     fArray.Delete(); //will call destructor for every element
222     fNumEntries = 0;
223 loizides 1.13 BaseCollection::Clear();
224 bendavid 1.10 }
225    
226     //--------------------------------------------------------------------------------------------------
227     template<class ArrayElement>
228 loizides 1.1 inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
229     {
230     // Return entry at given index.
231    
232     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
233     }
234    
235     //--------------------------------------------------------------------------------------------------
236     template<class ArrayElement>
237     inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
238     {
239     // Return entry at given index.
240    
241     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
242     }
243 loizides 1.4
244     //--------------------------------------------------------------------------------------------------
245     template<class ArrayElement>
246     inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
247     {
248     // Return entry at given index.
249    
250     return At(idx);
251     }
252    
253     //--------------------------------------------------------------------------------------------------
254     template<class ArrayElement>
255     inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
256     {
257     // Return entry at given index.
258    
259     return At(idx);
260     }
261 loizides 1.1 #endif