ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/RefArray.h
Revision: 1.7
Committed: Fri Oct 31 18:56:14 2008 UTC (16 years, 6 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_006
Changes since 1.6: +81 -88 lines
Log Message:
Switched to new optimized RefArray now supporting multiple PIDs

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.7 // $Id: RefArray.h,v 1.6 2008/10/23 18:22:27 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     class RefArray
34 loizides 1.1 {
35     public:
36     RefArray();
37 bendavid 1.7 virtual ~RefArray() {}
38 loizides 1.1
39     void Add(ArrayElement *ae);
40     ArrayElement *At(UInt_t idx);
41     const ArrayElement *At(UInt_t idx) const;
42 bendavid 1.7 void Clear(Option_t */*opt*/="") {}
43 loizides 1.1 UInt_t Entries() const { return GetEntries(); }
44 bendavid 1.7 UInt_t GetEntries() const { return fUIDs.GetEntries(); }
45 loizides 1.1 Bool_t IsOwner() const { return kTRUE; }
46 bendavid 1.7 void Reset();
47 loizides 1.1 void Trim() {}
48     ArrayElement *UncheckedAt(UInt_t idx);
49     const ArrayElement *UncheckedAt(UInt_t idx) const;
50    
51     ArrayElement *operator[](UInt_t idx);
52     const ArrayElement *operator[](UInt_t idx) const;
53    
54     protected:
55     TObject *GetObject(UInt_t idx) const;
56     UInt_t GetUID(UInt_t idx) const;
57    
58 bendavid 1.7 StackArray<ProcIDRef,N> fPIDs;//|| process ids of referenced objects
59     StackArrayBasic<UInt_t,N> fUIDs;//|| unique ids of referenced objects
60 loizides 1.1
61 bendavid 1.7 ClassDef(RefArray,2) // Implementation of our own TRefArray
62 loizides 1.1 };
63     }
64    
65     //--------------------------------------------------------------------------------------------------
66 bendavid 1.7 template<class ArrayElement, UInt_t N>
67     inline mithep::RefArray<ArrayElement,N>::RefArray()
68 loizides 1.1 {
69     }
70    
71     //--------------------------------------------------------------------------------------------------
72 bendavid 1.7 template<class ArrayElement, UInt_t N>
73     void mithep::RefArray<ArrayElement,N>::Add(ArrayElement *ae)
74 loizides 1.1 {
75     // Add new reference to object.
76    
77 bendavid 1.7 if(GetEntries()>=N) {
78     Fatal("Add", "Maximum number of references reached: To support more requires change in code!");
79 loizides 1.1 return;
80     }
81    
82     // check if the object can belong here and assign or get its uid
83     if (ae->TestBit(kHasUUID)) {
84     Error("Add", "Object can not be added as it has not UUID!");
85     return;
86     }
87    
88 bendavid 1.7 UInt_t uid = 0;
89     TProcessID *pid = 0;
90 loizides 1.1 if (ae->TestBit(kIsReferenced)) {
91     uid = ae->GetUniqueID();
92 bendavid 1.7 pid = TProcessID::GetProcessWithUID(uid,ae);
93 loizides 1.1 } else {
94 bendavid 1.7 pid = TProcessID::GetSessionProcessID();
95 loizides 1.1 uid = TProcessID::AssignID(ae);
96     }
97    
98 bendavid 1.7 //If RefArray contains one and only one PID reference, then only add another if the added object
99     //has a different PID. When this occurs all of the extra spaces which had been left empty get
100     //filled in with the original PID
101     if (fPIDs.GetEntries()==1) {
102     if (pid != fPIDs.At(0)->Pid() ) {
103     while (fPIDs.GetEntries()<GetEntries())
104     fPIDs.Allocate()->SetPid(fPIDs.At(0)->Pid());
105    
106     fPIDs.Allocate()->SetPid(pid);
107     }
108     }
109     else
110     fPIDs.Allocate()->SetPid(pid);
111    
112     fUIDs.Add(uid);
113    
114 loizides 1.1 }
115    
116     //--------------------------------------------------------------------------------------------------
117 bendavid 1.7 template<class ArrayElement, UInt_t N>
118     inline ArrayElement *mithep::RefArray<ArrayElement,N>::At(UInt_t idx)
119 loizides 1.1 {
120     // Return entry at given index.
121    
122 bendavid 1.7 if(idx<GetEntries())
123 loizides 1.1 return static_cast<ArrayElement*>(GetObject(idx));
124    
125 bendavid 1.7 Error("At", "Given index (%ud) is larger than array size (%ud)", idx, GetEntries());
126 loizides 1.1 return 0;
127     }
128    
129     //--------------------------------------------------------------------------------------------------
130 bendavid 1.7 template<class ArrayElement, UInt_t N>
131     inline const ArrayElement *mithep::RefArray<ArrayElement,N>::At(UInt_t idx) const
132 loizides 1.1 {
133     // Return entry at given index.
134    
135 bendavid 1.7 if(idx<GetEntries())
136 loizides 1.1 return static_cast<const ArrayElement*>(GetObject(idx));
137    
138 bendavid 1.7 Error("At", "Given index (%ud) is larger than array size (%ud)", idx, GetEntries());
139 loizides 1.1 return 0;
140     }
141    
142     //--------------------------------------------------------------------------------------------------
143 bendavid 1.7 template<class ArrayElement, UInt_t N>
144     TObject *mithep::RefArray<ArrayElement,N>::GetObject(UInt_t idx) const
145 loizides 1.1 {
146     // Return entry at given index. Code adapted from TRef::GetObject().
147 bendavid 1.7 TProcessID *pid=0;
148     if (fPIDs.GetEntries()>1)
149     pid = fPIDs.At(idx)->Pid();
150     else
151     pid = fPIDs.At(0)->Pid();
152 loizides 1.1
153 bendavid 1.7 if (!pid) {
154 loizides 1.1 Error("GetObject","Process id pointer is null!");
155     return 0;
156     }
157    
158 bendavid 1.7 if (!TProcessID::IsValid(pid)) {
159 loizides 1.1 Error("GetObject","Process id is invalid!");
160     return 0;
161     }
162     UInt_t uid = GetUID(idx);
163    
164     //the reference may be in the TRefTable
165     TRefTable *table = TRefTable::GetRefTable();
166     if (table) {
167 bendavid 1.7 table->SetUID(uid, pid);
168 loizides 1.1 table->Notify();
169     }
170    
171     //try to find the object from the table of the corresponding PID
172 bendavid 1.7 TObject *obj = pid->GetObjectWithID(uid);
173 loizides 1.1 return obj;
174     }
175    
176     //--------------------------------------------------------------------------------------------------
177 bendavid 1.7 template<class ArrayElement, UInt_t N>
178     inline UInt_t mithep::RefArray<ArrayElement,N>::GetUID(UInt_t idx) const
179 loizides 1.1 {
180     // Return uid corresponding to idx.
181    
182 bendavid 1.7 return fUIDs.At(idx);
183 loizides 1.1 }
184    
185     //--------------------------------------------------------------------------------------------------
186 bendavid 1.7 template<class ArrayElement, UInt_t N>
187     inline const ArrayElement *mithep::RefArray<ArrayElement,N>::operator[](UInt_t idx) const
188 loizides 1.1 {
189     // Return entry at given index.
190    
191     return At(idx);
192     }
193    
194     //--------------------------------------------------------------------------------------------------
195 bendavid 1.7 template<class ArrayElement, UInt_t N>
196     inline ArrayElement *mithep::RefArray<ArrayElement,N>::operator[](UInt_t idx)
197 loizides 1.1 {
198     // Return entry at given index.
199    
200     return At(idx);
201     }
202    
203 bendavid 1.7 //--------------------------------------------------------------------------------------------------
204     template<class ArrayElement, UInt_t N>
205     inline void mithep::RefArray<ArrayElement,N>::Reset()
206     {
207     fUIDs.Reset();
208     fPIDs.Reset();
209 loizides 1.1 }
210    
211     //--------------------------------------------------------------------------------------------------
212 bendavid 1.7 template<class ArrayElement, UInt_t N>
213     inline ArrayElement *mithep::RefArray<ArrayElement,N>::UncheckedAt(UInt_t idx)
214 loizides 1.1 {
215     // Return entry at given index.
216    
217     return static_cast<ArrayElement*>(GetObject(idx));
218     }
219    
220     //--------------------------------------------------------------------------------------------------
221 bendavid 1.7 template<class ArrayElement, UInt_t N>
222     inline const ArrayElement *mithep::RefArray<ArrayElement,N>::UncheckedAt(UInt_t idx) const
223 loizides 1.1 {
224     // Return entry at given index.
225    
226     return static_cast<const ArrayElement*>(GetObject(idx));
227     }
228     #endif