ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Array.h
Revision: 1.1
Committed: Tue Jul 29 10:36:20 2008 UTC (16 years, 9 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: MITHEP_2_0_x
Log Message:
Added submodule for containers only

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2     // $Id: Array.h,v 1.13 2008/07/14 20:55:19 loizides Exp $
3     //
4     // Array
5     //
6     // Implementation of Collection interface using TClonesArray class.
7     //
8     // Authors: C.Loizides
9     //--------------------------------------------------------------------------------------------------
10    
11     #ifndef DATATREE_ARRAY_H
12     #define DATATREE_ARRAY_H
13    
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     Bool_t IsOwner() const { return kTRUE; }
37     TIterator *Iterator(Bool_t dir = kIterForward) const;
38     Bool_t MustClear() const { return this->TestBit(14); }
39     Bool_t MustDelete() const { return this->TestBit(15); }
40     void Reset();
41     void Trim() { fArray.Compress(); }
42     void SetMustClearBit() { this->SetBit(14); }
43     void SetMustDeleteBit() { this->SetBit(15); }
44     void SetName(const char *name) { fArray.SetName(name); }
45     ArrayElement *UncheckedAt(UInt_t idx);
46     const ArrayElement *UncheckedAt(UInt_t idx) const;
47    
48     ArrayElement *operator[](UInt_t idx);
49     const ArrayElement *operator[](UInt_t idx) const;
50    
51     protected:
52     TClonesArray fArray; //array for storage
53     UInt_t fNumEntries; //number of entries in the array
54    
55     private:
56     Array(const Array &a);
57    
58     ClassDefT(Array,1) // Wrapper around TClonesArray class
59     };
60     }
61    
62     //--------------------------------------------------------------------------------------------------
63     template<class ArrayElement>
64     inline mithep::Array<ArrayElement>::Array(UInt_t size, const char *name) :
65     fArray(ArrayElement::Class_Name(),size),
66     fNumEntries(0)
67     {
68     // Default constructor.
69    
70     fArray.BypassStreamer(kTRUE);
71    
72     if (name)
73     fArray.SetName(name);
74    
75     ArrayElement test;
76     if (test.MustClear())
77     SetMustClearBit();
78     if (test.MustDelete())
79     SetMustDeleteBit();
80     }
81    
82     //--------------------------------------------------------------------------------------------------
83     template<class ArrayElement>
84     inline ArrayElement *mithep::Array<ArrayElement>::AddNew()
85     {
86     // Add new entry and return pointer to it.
87    
88     return new(Allocate()) ArrayElement();
89     }
90    
91     //--------------------------------------------------------------------------------------------------
92     template<class ArrayElement>
93     inline ArrayElement *mithep::Array<ArrayElement>::Allocate()
94     {
95     // Allocate a slot in the array, *only* to be used in placement new operator.
96    
97     return static_cast<ArrayElement*>(fArray[fNumEntries++]);
98     }
99    
100     //--------------------------------------------------------------------------------------------------
101     template<class ArrayElement>
102     inline ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx)
103     {
104     // Return entry at given index.
105    
106     if (idx<fNumEntries)
107     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
108    
109     ArrayElement tmp;
110     Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
111     idx, fNumEntries, this->GetName(), tmp.GetName());
112     return 0;
113     }
114    
115     //--------------------------------------------------------------------------------------------------
116     template<class ArrayElement>
117     inline const ArrayElement *mithep::Array<ArrayElement>::At(UInt_t idx) const
118     {
119     // Return entry at given index.
120    
121     if (idx<fNumEntries)
122     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
123    
124     ArrayElement tmp;
125     Fatal("At","Index too large: (%ud < %ud violated) for %s containing %s",
126     idx, fNumEntries, this->GetName(), tmp.GetName());
127     return 0;
128     }
129    
130     //--------------------------------------------------------------------------------------------------
131     template<class ArrayElement>
132     inline TIterator *mithep::Array<ArrayElement>::Iterator(Bool_t dir) const
133     {
134     // Return ROOT collection iterator.
135    
136     return fArray.MakeIterator(dir);
137     }
138    
139     //--------------------------------------------------------------------------------------------------
140     template<class ArrayElement>
141     inline const ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx) const
142     {
143     // Return entry at given index.
144    
145     return At(idx);
146     }
147    
148     //--------------------------------------------------------------------------------------------------
149     template<class ArrayElement>
150     inline ArrayElement *mithep::Array<ArrayElement>::operator[](UInt_t idx)
151     {
152     // Return entry at given index.
153    
154     return At(idx);
155     }
156    
157     //--------------------------------------------------------------------------------------------------
158     template<class ArrayElement>
159     inline void mithep::Array<ArrayElement>::Reset()
160     {
161     // ROOT implementation for clearing the array.
162    
163     if (this->MustDelete())
164     fArray.Delete(); //will call destructor for every element
165     else if (this->MustClear())
166     fArray.Clear("C"); //will call clear for every element
167     else
168     fArray.Clear();
169    
170     fNumEntries = 0;
171     }
172    
173     //--------------------------------------------------------------------------------------------------
174     template<class ArrayElement>
175     inline ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx)
176     {
177     // Return entry at given index.
178    
179     return static_cast<ArrayElement*>(fArray.UncheckedAt(idx));
180     }
181    
182     //--------------------------------------------------------------------------------------------------
183     template<class ArrayElement>
184     inline const ArrayElement *mithep::Array<ArrayElement>::UncheckedAt(UInt_t idx) const
185     {
186     // Return entry at given index.
187    
188     return static_cast<const ArrayElement*>(fArray.UncheckedAt(idx));
189     }
190     #endif