ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/RefArray.h
Revision: 1.3
Committed: Wed Oct 1 18:37:48 2008 UTC (16 years, 7 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -4 lines
Log Message:
Enabled default copy constructor

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.3 // $Id: RefArray.h,v 1.2 2008/09/10 03:33:26 loizides Exp $
3 loizides 1.1 //
4     // RefArray
5     //
6     // Implementation of a TRefArray using stack (and not heap) memory.
7     // For various reasons, the array can not be written in split mode.
8     // Maximum size of references is set to 1024 (but this could be
9     // changed if there is need for it).
10     //
11     // Authors: C.Loizides, J.Bendavid
12     //--------------------------------------------------------------------------------------------------
13    
14 loizides 1.2 #ifndef MITANA_DATACONT_REFARRAY
15     #define MITANA_DATACONT_REFARRAY
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     #include "MitAna/DataCont/interface/Collection.h"
23    
24     namespace mithep
25     {
26     template<class ArrayElement>
27     class RefArray : public Collection<ArrayElement>
28     {
29     public:
30     RefArray();
31     ~RefArray() { fProcID = 0; }
32    
33     void Add(ArrayElement *ae);
34     ArrayElement *At(UInt_t idx);
35     const ArrayElement *At(UInt_t idx) const;
36     void Clear(Option_t */*opt*/="") { fProcID = 0;}
37     UInt_t Entries() const { return GetEntries(); }
38     UInt_t GetEntries() const { return fSize; }
39     Bool_t IsOwner() const { return kTRUE; }
40     void Reset() { fSize = 0; }
41     void Trim() {}
42     ArrayElement *UncheckedAt(UInt_t idx);
43     const ArrayElement *UncheckedAt(UInt_t idx) const;
44    
45     ArrayElement *operator[](UInt_t idx);
46     const ArrayElement *operator[](UInt_t idx) const;
47    
48     protected:
49     TObject *GetObject(UInt_t idx) const;
50     UInt_t GetUID(UInt_t idx) const;
51    
52     TProcessID *fProcID; //!ptr to Process Unique Identifier
53     UShort_t fSize; //size of array
54     UInt_t fUIDs[1024]; //storage of uids of referenced objects
55    
56     ClassDefT(RefArray,1) // Implementation of our own TRefArray
57     };
58     }
59    
60     //--------------------------------------------------------------------------------------------------
61     template<class ArrayElement>
62     inline mithep::RefArray<ArrayElement>::RefArray() :
63     fProcID(TProcessID::GetSessionProcessID()),
64     fSize(0)
65     {
66     // Default constructor.
67     }
68    
69     //--------------------------------------------------------------------------------------------------
70     template<class ArrayElement>
71     void mithep::RefArray<ArrayElement>::Add(ArrayElement *ae)
72     {
73     // Add new reference to object.
74    
75     if(fSize>=1024) {
76     Fatal("Add", "Maximum number of references reached: To support more requires change in code!");
77     return;
78     }
79    
80     // check if the object can belong here and assign or get its uid
81     UInt_t uid = 0;
82     if (ae->TestBit(kHasUUID)) {
83     Error("Add", "Object can not be added as it has not UUID!");
84     return;
85     }
86    
87     if (ae->TestBit(kIsReferenced)) {
88     uid = ae->GetUniqueID();
89     if (fProcID != TProcessID::GetProcessWithUID(uid,ae)) {
90     Error("Add", "Object can not be added as it has a different process id!");
91     return;
92     }
93     } else {
94     if (fProcID != TProcessID::GetSessionProcessID()) {
95     Error("Add", "Object can not be added as it has a different process id!");
96     return;
97     }
98     uid = TProcessID::AssignID(ae);
99     }
100    
101     fUIDs[fSize] = uid;
102     ++fSize;
103     }
104    
105     //--------------------------------------------------------------------------------------------------
106     template<class ArrayElement>
107     inline ArrayElement *mithep::RefArray<ArrayElement>::At(UInt_t idx)
108     {
109     // Return entry at given index.
110    
111     if(idx<fSize)
112     return static_cast<ArrayElement*>(GetObject(idx));
113    
114     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
115     return 0;
116     }
117    
118     //--------------------------------------------------------------------------------------------------
119     template<class ArrayElement>
120     inline const ArrayElement *mithep::RefArray<ArrayElement>::At(UInt_t idx) const
121     {
122     // Return entry at given index.
123    
124     if(idx<fSize)
125     return static_cast<const ArrayElement*>(GetObject(idx));
126    
127     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
128     return 0;
129     }
130    
131     //--------------------------------------------------------------------------------------------------
132     template<class ArrayElement>
133     TObject *mithep::RefArray<ArrayElement>::GetObject(UInt_t idx) const
134     {
135     // Return entry at given index. Code adapted from TRef::GetObject().
136    
137     if (!fProcID) {
138     Error("GetObject","Process id pointer is null!");
139     return 0;
140     }
141    
142     if (!TProcessID::IsValid(fProcID)) {
143     Error("GetObject","Process id is invalid!");
144     return 0;
145     }
146     UInt_t uid = GetUID(idx);
147    
148     //the reference may be in the TRefTable
149     TRefTable *table = TRefTable::GetRefTable();
150     if (table) {
151     table->SetUID(uid, fProcID);
152     table->Notify();
153     }
154    
155     //try to find the object from the table of the corresponding PID
156     TObject *obj = fProcID->GetObjectWithID(uid);
157     return obj;
158     }
159    
160     //--------------------------------------------------------------------------------------------------
161     template<class ArrayElement>
162     inline UInt_t mithep::RefArray<ArrayElement>::GetUID(UInt_t idx) const
163     {
164     // Return uid corresponding to idx.
165    
166     return fUIDs[idx];
167     }
168    
169     //--------------------------------------------------------------------------------------------------
170     template<class ArrayElement>
171     inline const ArrayElement *mithep::RefArray<ArrayElement>::operator[](UInt_t idx) const
172     {
173     // Return entry at given index.
174    
175     return At(idx);
176     }
177    
178     //--------------------------------------------------------------------------------------------------
179     template<class ArrayElement>
180     inline ArrayElement *mithep::RefArray<ArrayElement>::operator[](UInt_t idx)
181     {
182     // Return entry at given index.
183    
184     return At(idx);
185     }
186    
187     //-------------------------------------------------------------------------------------------------
188     template<class ArrayElement>
189     void mithep::RefArray<ArrayElement>::Streamer(TBuffer &b)
190     {
191     // Stream all objects in the array to or from the I/O buffer.
192    
193     if (b.IsReading()) {
194     //UInt_t sv, cv;
195     //b.ReadVersion(&sv, &cv);
196     //TObject::Streamer(b);
197     b >> fSize;
198     if (fSize) {
199     UShort_t pidf;
200     b >> pidf;
201     pidf += b.GetPidOffset();
202     fProcID = b.ReadProcessID(pidf);
203     b.ReadFastArray(fUIDs,fSize);
204     }
205     //b.CheckByteCount(sv, cv, RefArray::IsA());
206     } else { /*writing*/
207     //UInt_t cv;
208     //cv = b.WriteVersion(RefArray::IsA(), kTRUE);
209     //TObject::Streamer(b);
210     b << fSize;
211     if (fSize) {
212     UShort_t pidf;
213     pidf = b.WriteProcessID(fProcID);
214     b << pidf;
215     b.WriteFastArray(fUIDs,fSize);
216     }
217     //b.SetByteCount(cv, kTRUE);
218     }
219     }
220    
221     //--------------------------------------------------------------------------------------------------
222     template<class ArrayElement>
223     inline ArrayElement *mithep::RefArray<ArrayElement>::UncheckedAt(UInt_t idx)
224     {
225     // Return entry at given index.
226    
227     return static_cast<ArrayElement*>(GetObject(idx));
228     }
229    
230     //--------------------------------------------------------------------------------------------------
231     template<class ArrayElement>
232     inline const ArrayElement *mithep::RefArray<ArrayElement>::UncheckedAt(UInt_t idx) const
233     {
234     // Return entry at given index.
235    
236     return static_cast<const ArrayElement*>(GetObject(idx));
237     }
238     #endif