ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/RefArray.h
Revision: 1.10
Committed: Wed Dec 10 11:26:52 2008 UTC (16 years, 4 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.9: +34 -9 lines
Log Message:
Implemented ObjAt function.

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.10 // $Id: RefArray.h,v 1.9 2008/12/03 11:36:02 loizides Exp $
3 loizides 1.1 //
4     // RefArray
5     //
6     // Implementation of a TRefArray using stack (and not heap) memory.
7 bendavid 1.7 // The maximum number of references is left as a template parameter.
8     // Since all of the stack allocation is handled by the StackArrays
9     // the RefArray itself can now actually be split.
10     //
11     // RefArray now supports objects from multiple PIDs, but only a single PID will be stored as long
12     // as only objects from a single PID are added.
13 loizides 1.1 //
14     // Authors: C.Loizides, J.Bendavid
15     //--------------------------------------------------------------------------------------------------
16    
17 loizides 1.2 #ifndef MITANA_DATACONT_REFARRAY
18     #define MITANA_DATACONT_REFARRAY
19 loizides 1.1
20     #include <TObject.h>
21     #include <TRefArray.h>
22     #include <TRefTable.h>
23     #include <TProcessID.h>
24     #include <TError.h>
25     #include "MitAna/DataCont/interface/Collection.h"
26 bendavid 1.7 #include "MitAna/DataCont/interface/StackArray.h"
27     #include "MitAna/DataCont/interface/StackArrayBasic.h"
28     #include "MitAna/DataCont/interface/ProcIDRef.h"
29 loizides 1.1
30     namespace mithep
31     {
32 bendavid 1.7 template<class ArrayElement, UInt_t N>
33 loizides 1.10 class RefArray /*: public Collection<ArrayElement> TODO to be enabled for Mit_008*/
34 loizides 1.1 {
35     public:
36     RefArray();
37 bendavid 1.7 virtual ~RefArray() {}
38 loizides 1.1
39 loizides 1.9 void Add(const ArrayElement *ae);
40 loizides 1.1 ArrayElement *At(UInt_t idx);
41 loizides 1.8 const ArrayElement *At(UInt_t idx) const;
42     void Clear(Option_t */*opt*/="") {}
43     UInt_t Entries() const { return GetEntries(); }
44     UInt_t GetEntries() const { return fUIDs.GetEntries(); }
45     UInt_t GetSize() const { return N; }
46     Bool_t IsOwner() const { return kTRUE; }
47 loizides 1.10 TObject *ObjAt(UInt_t idx);
48     const TObject *ObjAt(UInt_t idx) const;
49 bendavid 1.7 void Reset();
50 loizides 1.8 void Trim() {}
51 loizides 1.1 ArrayElement *UncheckedAt(UInt_t idx);
52 loizides 1.8 const ArrayElement *UncheckedAt(UInt_t idx) const;
53 loizides 1.1 ArrayElement *operator[](UInt_t idx);
54 loizides 1.8 const ArrayElement *operator[](UInt_t idx) const;
55 loizides 1.1
56     protected:
57 loizides 1.8 TObject *GetObject(UInt_t idx) const;
58     UInt_t GetUID(UInt_t idx) const;
59 loizides 1.1
60 bendavid 1.7 StackArray<ProcIDRef,N> fPIDs;//|| process ids of referenced objects
61     StackArrayBasic<UInt_t,N> fUIDs;//|| unique ids of referenced objects
62 loizides 1.1
63 loizides 1.10 ClassDef(RefArray, 2) // Implementation of our own TRefArray
64 loizides 1.1 };
65     }
66    
67     //--------------------------------------------------------------------------------------------------
68 bendavid 1.7 template<class ArrayElement, UInt_t N>
69     inline mithep::RefArray<ArrayElement,N>::RefArray()
70 loizides 1.1 {
71 loizides 1.8 // Default constructor.
72 loizides 1.1 }
73    
74     //--------------------------------------------------------------------------------------------------
75 bendavid 1.7 template<class ArrayElement, UInt_t N>
76 loizides 1.9 void mithep::RefArray<ArrayElement,N>::Add(const ArrayElement *ae)
77 loizides 1.1 {
78     // Add new reference to object.
79    
80 loizides 1.10 if (GetEntries()>=N) {
81 loizides 1.8 Fatal("Add", "Maximum number of slots reached (%d>=%d): "
82     "To support more requires a different template!", GetEntries(), N);
83 loizides 1.1 return;
84     }
85    
86     // check if the object can belong here and assign or get its uid
87     if (ae->TestBit(kHasUUID)) {
88 loizides 1.8 Fatal("Add", "Object can not be added as it has not UUID!");
89 loizides 1.1 return;
90     }
91    
92 bendavid 1.7 UInt_t uid = 0;
93     TProcessID *pid = 0;
94 loizides 1.1 if (ae->TestBit(kIsReferenced)) {
95     uid = ae->GetUniqueID();
96 loizides 1.9 pid = TProcessID::GetProcessWithUID(uid, const_cast<ArrayElement*>(ae));
97 loizides 1.1 } else {
98 bendavid 1.7 pid = TProcessID::GetSessionProcessID();
99 loizides 1.9 uid = TProcessID::AssignID(const_cast<ArrayElement*>(ae));
100 loizides 1.1 }
101    
102 loizides 1.8 // If RefArray contains one and only one PID reference, then only add another if the added object
103     // has a different PID. When this occurs all of the extra spaces which had been left empty get
104     // filled in with the original PID
105 bendavid 1.7 if (fPIDs.GetEntries()==1) {
106     if (pid != fPIDs.At(0)->Pid() ) {
107     while (fPIDs.GetEntries()<GetEntries())
108     fPIDs.Allocate()->SetPid(fPIDs.At(0)->Pid());
109    
110     fPIDs.Allocate()->SetPid(pid);
111     }
112     }
113     else
114     fPIDs.Allocate()->SetPid(pid);
115    
116     fUIDs.Add(uid);
117 loizides 1.1 }
118    
119     //--------------------------------------------------------------------------------------------------
120 bendavid 1.7 template<class ArrayElement, UInt_t N>
121     inline ArrayElement *mithep::RefArray<ArrayElement,N>::At(UInt_t idx)
122 loizides 1.1 {
123     // Return entry at given index.
124    
125 loizides 1.10 if (idx<GetEntries())
126 loizides 1.1 return static_cast<ArrayElement*>(GetObject(idx));
127    
128 loizides 1.8 Fatal("At", "Given index (%ud) is larger than array size (%ud)", idx, GetEntries());
129 loizides 1.1 return 0;
130     }
131    
132     //--------------------------------------------------------------------------------------------------
133 bendavid 1.7 template<class ArrayElement, UInt_t N>
134     inline const ArrayElement *mithep::RefArray<ArrayElement,N>::At(UInt_t idx) const
135 loizides 1.1 {
136     // Return entry at given index.
137    
138 loizides 1.10 if (idx<GetEntries())
139 loizides 1.1 return static_cast<const ArrayElement*>(GetObject(idx));
140    
141 loizides 1.8 Fatal("At", "Given index (%ud) is larger than array size (%ud)", idx, GetEntries());
142 loizides 1.1 return 0;
143     }
144    
145     //--------------------------------------------------------------------------------------------------
146 bendavid 1.7 template<class ArrayElement, UInt_t N>
147     TObject *mithep::RefArray<ArrayElement,N>::GetObject(UInt_t idx) const
148 loizides 1.1 {
149     // Return entry at given index. Code adapted from TRef::GetObject().
150 loizides 1.8
151 bendavid 1.7 TProcessID *pid=0;
152     if (fPIDs.GetEntries()>1)
153     pid = fPIDs.At(idx)->Pid();
154     else
155     pid = fPIDs.At(0)->Pid();
156 loizides 1.1
157 bendavid 1.7 if (!pid) {
158 loizides 1.8 Fatal("GetObject","Process id pointer is null!");
159 loizides 1.1 return 0;
160     }
161    
162 bendavid 1.7 if (!TProcessID::IsValid(pid)) {
163 loizides 1.8 Fatal("GetObject","Process id is invalid!");
164 loizides 1.1 return 0;
165     }
166 loizides 1.8
167 loizides 1.1 UInt_t uid = GetUID(idx);
168    
169     //the reference may be in the TRefTable
170     TRefTable *table = TRefTable::GetRefTable();
171     if (table) {
172 bendavid 1.7 table->SetUID(uid, pid);
173 loizides 1.1 table->Notify();
174     }
175    
176     //try to find the object from the table of the corresponding PID
177 bendavid 1.7 TObject *obj = pid->GetObjectWithID(uid);
178 loizides 1.1 return obj;
179     }
180    
181     //--------------------------------------------------------------------------------------------------
182 bendavid 1.7 template<class ArrayElement, UInt_t N>
183     inline UInt_t mithep::RefArray<ArrayElement,N>::GetUID(UInt_t idx) const
184 loizides 1.1 {
185     // Return uid corresponding to idx.
186    
187 bendavid 1.7 return fUIDs.At(idx);
188 loizides 1.1 }
189    
190     //--------------------------------------------------------------------------------------------------
191 bendavid 1.7 template<class ArrayElement, UInt_t N>
192 loizides 1.10 TObject *mithep::RefArray<ArrayElement,N>::ObjAt(UInt_t idx)
193     {
194     // Return object at given index.
195    
196     if (idx<GetEntries())
197     return GetObject(idx);
198    
199     Fatal("ObjAt", "Given index (%ud) is larger than array size (%ud)", idx, GetEntries());
200     return 0;
201     }
202    
203     //--------------------------------------------------------------------------------------------------
204     template<class ArrayElement,UInt_t N>
205     const TObject *mithep::RefArray<ArrayElement,N>::ObjAt(UInt_t idx) const
206     {
207     // Return object at given index.
208    
209     if (idx<GetEntries())
210     return static_cast<const TObject*>(GetObject(idx));
211    
212     Fatal("ObjAt", "Given index (%ud) is larger than array size (%ud)", idx, GetEntries());
213     return 0;
214     }
215    
216     //--------------------------------------------------------------------------------------------------
217     template<class ArrayElement, UInt_t N>
218 loizides 1.8 inline void mithep::RefArray<ArrayElement,N>::Reset()
219 loizides 1.1 {
220 loizides 1.8 fUIDs.Reset();
221     fPIDs.Reset();
222 loizides 1.1 }
223    
224     //--------------------------------------------------------------------------------------------------
225 bendavid 1.7 template<class ArrayElement, UInt_t N>
226 loizides 1.8 inline ArrayElement *mithep::RefArray<ArrayElement,N>::UncheckedAt(UInt_t idx)
227 loizides 1.1 {
228     // Return entry at given index.
229    
230 loizides 1.8 return static_cast<ArrayElement*>(GetObject(idx));
231 loizides 1.1 }
232    
233 bendavid 1.7 //--------------------------------------------------------------------------------------------------
234     template<class ArrayElement, UInt_t N>
235 loizides 1.8 inline const ArrayElement *mithep::RefArray<ArrayElement,N>::UncheckedAt(UInt_t idx) const
236 bendavid 1.7 {
237 loizides 1.8 // Return entry at given index.
238    
239     return static_cast<const ArrayElement*>(GetObject(idx));
240 loizides 1.1 }
241    
242     //--------------------------------------------------------------------------------------------------
243 bendavid 1.7 template<class ArrayElement, UInt_t N>
244 loizides 1.8 inline const ArrayElement *mithep::RefArray<ArrayElement,N>::operator[](UInt_t idx) const
245 loizides 1.1 {
246     // Return entry at given index.
247    
248 loizides 1.8 return At(idx);
249 loizides 1.1 }
250    
251     //--------------------------------------------------------------------------------------------------
252 bendavid 1.7 template<class ArrayElement, UInt_t N>
253 loizides 1.8 inline ArrayElement *mithep::RefArray<ArrayElement,N>::operator[](UInt_t idx)
254 loizides 1.1 {
255     // Return entry at given index.
256    
257 loizides 1.8 return At(idx);
258 loizides 1.1 }
259     #endif