ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Array.h
Revision: 1.7
Committed: Tue Dec 9 17:42:32 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.6: +2 -1 lines
Log Message:
Add sort.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.7 // $Id: Array.h,v 1.6 2008/12/08 15:26:11 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     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.4 void Print(Option_t *opt="") const;
43 loizides 1.1 void Reset();
44     void SetMustClearBit() { this->SetBit(14); }
45     void SetMustDeleteBit() { this->SetBit(15); }
46     void SetName(const char *name) { fArray.SetName(name); }
47 loizides 1.7 void Sort() { fArray.Sort(); }
48 loizides 1.4 void Trim() { fArray.Compress(); }
49 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
50     const ArrayElement *UncheckedAt(UInt_t idx) const;
51     ArrayElement *operator[](UInt_t idx);
52     const ArrayElement *operator[](UInt_t idx) const;
53    
54     protected:
55     TClonesArray fArray; //array for storage
56     UInt_t fNumEntries; //number of entries in the array
57    
58     private:
59     Array(const Array &a);
60    
61     ClassDefT(Array,1) // Wrapper around TClonesArray class
62     };
63     }
64    
65     //--------------------------------------------------------------------------------------------------
66     template<class ArrayElement>
67     inline mithep::Array<ArrayElement>::Array(UInt_t size, const char *name) :
68     fArray(ArrayElement::Class_Name(),size),
69     fNumEntries(0)
70     {
71     // Default constructor.
72    
73     fArray.BypassStreamer(kTRUE);
74    
75     if (name)
76     fArray.SetName(name);
77    
78     ArrayElement test;
79     if (test.MustClear())
80     SetMustClearBit();
81     if (test.MustDelete())
82     SetMustDeleteBit();
83     }
84    
85     //--------------------------------------------------------------------------------------------------
86     template<class ArrayElement>
87     inline ArrayElement *mithep::Array<ArrayElement>::AddNew()
88     {
89     // Add new entry and return pointer to it.
90    
91     return new(Allocate()) ArrayElement();
92     }
93    
94     //--------------------------------------------------------------------------------------------------
95     template<class ArrayElement>
96     inline ArrayElement *mithep::Array<ArrayElement>::Allocate()
97     {
98     // Allocate a slot in the array, *only* to be used in placement new operator.
99    
100     return static_cast<ArrayElement*>(fArray[fNumEntries++]);
101     }
102    
103     //--------------------------------------------------------------------------------------------------
104     template<class ArrayElement>
105     inline ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx)
106     {
107     // Return entry at given index.
108    
109     if (idx<fNumEntries)
110     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
111    
112     ArrayElement tmp;
113 loizides 1.3 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
114 loizides 1.4 idx, fNumEntries, GetName(), tmp.GetName());
115 loizides 1.1 return 0;
116     }
117    
118     //--------------------------------------------------------------------------------------------------
119     template<class ArrayElement>
120     inline const ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx) const
121     {
122     // Return entry at given index.
123    
124     if (idx<fNumEntries)
125     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
126    
127     ArrayElement tmp;
128 loizides 1.3 TObject::Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
129 loizides 1.4 idx, fNumEntries, GetName(), tmp.GetName());
130 loizides 1.1 return 0;
131     }
132    
133     //--------------------------------------------------------------------------------------------------
134     template<class ArrayElement>
135     inline TIterator *mithep::Array<ArrayElement>::Iterator(Bool_t dir) const
136     {
137     // Return ROOT collection iterator.
138    
139     return fArray.MakeIterator(dir);
140     }
141    
142     //--------------------------------------------------------------------------------------------------
143     template<class ArrayElement>
144 bendavid 1.5 inline Bool_t mithep::Array<ArrayElement>::HasObject(const ArrayElement *obj) const
145     {
146     // Check whether object is in array.
147    
148     return fArray.FindObject(obj);
149     }
150    
151     //--------------------------------------------------------------------------------------------------
152     template<class ArrayElement>
153 loizides 1.4 void mithep::Array<ArrayElement>::Print(Option_t *opt) const
154 loizides 1.1 {
155 loizides 1.4 // Print out elements of array.
156 loizides 1.1
157 loizides 1.4 printf("%s: Contains %d (out of %d) objs of name %s\n",
158     GetName(), GetEntries(), GetSize(), ArrayElement::Class_Name());
159 loizides 1.1
160 loizides 1.6 if (opt && opt[0]=='l') {
161     const UInt_t N = GetEntries();
162     for (UInt_t i=0; i<N; ++i) {
163     printf("%4d: ",i);
164     At(i)->Print(opt);
165     }
166 loizides 1.4 }
167 loizides 1.1 }
168    
169     //--------------------------------------------------------------------------------------------------
170     template<class ArrayElement>
171     inline void mithep::Array<ArrayElement>::Reset()
172     {
173     // ROOT implementation for clearing the array.
174    
175 loizides 1.4 if (MustDelete())
176 loizides 1.1 fArray.Delete(); //will call destructor for every element
177 loizides 1.4 else if (MustClear())
178 loizides 1.1 fArray.Clear("C"); //will call clear for every element
179     else
180     fArray.Clear();
181    
182     fNumEntries = 0;
183     }
184    
185     //--------------------------------------------------------------------------------------------------
186     template<class ArrayElement>
187     inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
188     {
189     // Return entry at given index.
190    
191     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
192     }
193    
194     //--------------------------------------------------------------------------------------------------
195     template<class ArrayElement>
196     inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
197     {
198     // Return entry at given index.
199    
200     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
201     }
202 loizides 1.4
203     //--------------------------------------------------------------------------------------------------
204     template<class ArrayElement>
205     inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
206     {
207     // Return entry at given index.
208    
209     return At(idx);
210     }
211    
212     //--------------------------------------------------------------------------------------------------
213     template<class ArrayElement>
214     inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
215     {
216     // Return entry at given index.
217    
218     return At(idx);
219     }
220 loizides 1.1 #endif