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

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