ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/RefArray.h
Revision: 1.24
Committed: Thu Mar 29 23:41:54 2012 UTC (13 years, 1 month ago) by paus
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, Mit_029, Mit_029_pre1, Mit_028a, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, HEAD
Changes since 1.23: +13 -1 lines
Log Message:
Version with working skimming and last 4.4 tag.

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: RefArray.h,v 1.23 2010/09/19 23:46:08 bendavid Exp $
3 //
4 // RefArray
5 //
6 // Implementation of an efficient TRefArray using the FastArray.
7 //
8 // RefArray now supports objects from multiple PIDs, but only a single
9 // PID will be stored as long as only objects from a single PID are added.
10 //
11 // Authors: C.Loizides, J.Bendavid
12 //--------------------------------------------------------------------------------------------------
13
14 #ifndef MITANA_DATACONT_REFARRAY_H
15 #define MITANA_DATACONT_REFARRAY_H
16
17 #include <TObject.h>
18 #include <TRefArray.h>
19 #include <TRefTable.h>
20 #include <TProcessID.h>
21 #include <TError.h>
22 #include "MitAna/DataCont/interface/RefResolver.h"
23 #include "MitAna/DataCont/interface/Collection.h"
24 #include "MitAna/DataCont/interface/FastArray.h"
25 #include "MitAna/DataCont/interface/FastArrayBasic.h"
26 #include "MitAna/DataCont/interface/ProcIDRef.h"
27
28 namespace mithep
29 {
30 template<class ArrayElement>
31 class RefArray : public Collection<ArrayElement>
32 {
33 public:
34 RefArray();
35
36 void Add(const ArrayElement *ae);
37 ArrayElement *At(UInt_t idx);
38 const ArrayElement *At(UInt_t idx) const;
39 void Clear(Option_t */*opt*/="") { fPIDs.Clear(); fUIDs.Clear(); }
40 UInt_t Entries() const { return fUIDs.Entries(); }
41 UInt_t GetEntries() const { return fUIDs.GetEntries(); }
42 UInt_t GetSize() const { return fUIDs.GetSize(); }
43 Bool_t HasObject(const ArrayElement *obj) const;
44 Bool_t IsOwner() const { return kTRUE; }
45 TObject *ObjAt(UInt_t idx);
46 const TObject *ObjAt(UInt_t idx) const;
47 void Reset();
48 void Trim() { fPIDs.Trim(); fUIDs.Trim(); }
49 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 void Mark(UInt_t i=1) const;
55
56 protected:
57 TObject *GetObject(UInt_t idx) const;
58 TProcessID *GetPID(UInt_t idx) const;
59 UInt_t GetUID(UInt_t idx) const;
60
61 FastArray<ProcIDRef> fPIDs; //||process ids of referenced objects
62 FastArrayBasic<UInt_t> fUIDs; //||unique ids of referenced objects
63
64 ClassDef(RefArray, 1) // Implementation of our own TRefArray
65 };
66 }
67
68 //--------------------------------------------------------------------------------------------------
69 template<class ArrayElement>
70 inline mithep::RefArray<ArrayElement>::RefArray()
71 {
72 // Default constructor.
73 }
74
75 //--------------------------------------------------------------------------------------------------
76 template<class ArrayElement>
77 void mithep::RefArray<ArrayElement>::Add(const ArrayElement *ae)
78 {
79 // Add reference to object.
80
81 if (!ae)
82 return;
83
84 // check if the object can belong here and assign or get its uid
85 if (ae->TestBit(kHasUUID)) {
86 Fatal("Add", "Object cannot be added as it has not UUID!");
87 return;
88 }
89
90 UInt_t uid = 0;
91 TProcessID *pid = 0;
92 if (ae->TestBit(kIsReferenced)) {
93 uid = ae->GetUniqueID();
94 pid = TProcessID::GetProcessWithUID(uid, const_cast<ArrayElement*>(ae));
95 } else {
96 pid = TProcessID::GetSessionProcessID();
97 uid = TProcessID::AssignID(const_cast<ArrayElement*>(ae));
98 }
99
100 // If RefArray contains one and only one PID reference, then only add another if the added object
101 // has a different PID. When this occurs all of the extra spaces which had been left empty get
102 // filled in with the original PID
103 if (fPIDs.Entries()==1) {
104 if (pid != fPIDs.At(0)->Pid() ) {
105 while (fPIDs.Entries()<Entries())
106 fPIDs.GetNew()->SetPid(fPIDs.At(0)->Pid());
107
108 fPIDs.GetNew()->SetPid(pid);
109 }
110 }
111 else
112 fPIDs.GetNew()->SetPid(pid);
113
114 fUIDs.Add(uid);
115 BaseCollection::Clear();
116 }
117
118 //--------------------------------------------------------------------------------------------------
119 template<class ArrayElement>
120 inline ArrayElement *mithep::RefArray<ArrayElement>::At(UInt_t idx)
121 {
122 // Return entry at given index.
123
124 if (idx<Entries())
125 return static_cast<ArrayElement*>(GetObject(idx));
126
127 Fatal("At", "Given index (%u) is larger than array size (%u)", idx, Entries());
128 return 0;
129 }
130
131 //--------------------------------------------------------------------------------------------------
132 template<class ArrayElement>
133 inline const ArrayElement *mithep::RefArray<ArrayElement>::At(UInt_t idx) const
134 {
135 // Return entry at given index.
136
137 if (idx<Entries())
138 return static_cast<const ArrayElement*>(GetObject(idx));
139
140 Fatal("At", "Given index (%u) is larger than array size (%u)", idx, Entries());
141 return 0;
142 }
143
144 //--------------------------------------------------------------------------------------------------
145 template<class ArrayElement>
146 void mithep::RefArray<ArrayElement>::Mark(UInt_t ib) const
147 {
148 // Mark the entire array
149
150 for (UInt_t i=0; i<Entries(); ++i)
151 At(i)->Mark(ib);
152 }
153
154 //--------------------------------------------------------------------------------------------------
155 template<class ArrayElement>
156 TObject *mithep::RefArray<ArrayElement>::GetObject(UInt_t idx) const
157 {
158 // Return entry at given index. Code adapted from TRef::GetObject().
159
160 TProcessID *pid = GetPID(idx);
161
162 if (!pid) {
163 Fatal("GetObject","Process id pointer is null!");
164 return 0;
165 }
166
167 if (!TProcessID::IsValid(pid)) {
168 Fatal("GetObject","Process id is invalid!");
169 return 0;
170 }
171
172 UInt_t uid = GetUID(idx);
173 TObject *obj = RefResolver::GetObjectWithID(uid,pid);
174 if (!obj) {
175 Fatal("RefArray::GetObject","Null pointer for filled element!");
176 }
177 return obj;
178 }
179
180 //--------------------------------------------------------------------------------------------------
181 template<class ArrayElement>
182 inline TProcessID *mithep::RefArray<ArrayElement>::GetPID(UInt_t idx) const
183 {
184 // Return pid corresponding to given idx.
185
186 TProcessID *pid = 0;
187 if (fPIDs.Entries()>1)
188 pid = fPIDs.UncheckedAt(idx)->Pid();
189 else
190 pid = fPIDs.UncheckedAt(0)->Pid();
191
192 return pid;
193 }
194
195 //--------------------------------------------------------------------------------------------------
196 template<class ArrayElement>
197 inline UInt_t mithep::RefArray<ArrayElement>::GetUID(UInt_t idx) const
198 {
199 // Return uid corresponding to given idx.
200
201 return fUIDs.UncheckedAt(idx);
202 }
203
204 //--------------------------------------------------------------------------------------------------
205 template<class ArrayElement>
206 Bool_t mithep::RefArray<ArrayElement>::HasObject(const ArrayElement *obj) const
207 {
208 // Check whether array contains a given object.
209
210 if (!obj->TestBit(kIsReferenced))
211 return kFALSE;
212
213 UInt_t oUid = obj->GetUniqueID();
214 TProcessID *oPid = TProcessID::GetProcessWithUID(oUid, const_cast<ArrayElement*>(obj));
215
216 for (UInt_t i=0; i<Entries(); ++i) {
217 if ( (GetUID(i)&0xffffff)==(oUid&0xffffff) && GetPID(i)->GetUniqueID()==oPid->GetUniqueID())
218 return kTRUE;
219 }
220
221 return kFALSE;
222 }
223
224 //--------------------------------------------------------------------------------------------------
225 template<class ArrayElement>
226 TObject *mithep::RefArray<ArrayElement>::ObjAt(UInt_t idx)
227 {
228 // Return object at given index.
229
230 if (idx<Entries())
231 return GetObject(idx);
232
233 Fatal("ObjAt", "Given index (%u) is larger than array size (%u)", idx, Entries());
234 return 0;
235 }
236
237 //--------------------------------------------------------------------------------------------------
238 template<class ArrayElement>
239 const TObject *mithep::RefArray<ArrayElement>::ObjAt(UInt_t idx) const
240 {
241 // Return object at given index.
242
243 if (idx<Entries())
244 return static_cast<const TObject*>(GetObject(idx));
245
246 Fatal("ObjAt", "Given index (%u) is larger than array size (%u)", idx, Entries());
247 return 0;
248 }
249
250 //--------------------------------------------------------------------------------------------------
251 template<class ArrayElement>
252 inline void mithep::RefArray<ArrayElement>::Reset()
253 {
254 // Reset the array.
255
256 fUIDs.Reset();
257 fPIDs.Reset();
258 BaseCollection::Clear();
259 }
260
261 //--------------------------------------------------------------------------------------------------
262 template<class ArrayElement>
263 inline ArrayElement *mithep::RefArray<ArrayElement>::UncheckedAt(UInt_t idx)
264 {
265 // Return entry at given index.
266
267 return static_cast<ArrayElement*>(GetObject(idx));
268 }
269
270 //--------------------------------------------------------------------------------------------------
271 template<class ArrayElement>
272 inline const ArrayElement *mithep::RefArray<ArrayElement>::UncheckedAt(UInt_t idx) const
273 {
274 // Return entry at given index.
275
276 return static_cast<const ArrayElement*>(GetObject(idx));
277 }
278
279 //--------------------------------------------------------------------------------------------------
280 template<class ArrayElement>
281 inline const ArrayElement *mithep::RefArray<ArrayElement>::operator[](UInt_t idx) const
282 {
283 // Return entry at given index.
284
285 return At(idx);
286 }
287
288 //--------------------------------------------------------------------------------------------------
289 template<class ArrayElement>
290 inline ArrayElement *mithep::RefArray<ArrayElement>::operator[](UInt_t idx)
291 {
292 // Return entry at given index.
293
294 return At(idx);
295 }
296 #endif