ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/RefArray.h
Revision: 1.5
Committed: Thu Oct 2 14:00:02 2008 UTC (16 years, 7 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_005
Changes since 1.4: +2 -2 lines
Log Message:
Ensure default ProcID is always set

File Contents

# User Rev Content
1 loizides 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.5 // $Id: RefArray.h,v 1.4 2008/10/02 12:48:08 bendavid 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 bendavid 1.5 fProcID(TProcessID::GetSessionProcessID()),
64 loizides 1.1 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 bendavid 1.4 if (fSize==0)
90     fProcID = TProcessID::GetProcessWithUID(uid,ae);
91 loizides 1.1 if (fProcID != TProcessID::GetProcessWithUID(uid,ae)) {
92     Error("Add", "Object can not be added as it has a different process id!");
93     return;
94     }
95     } else {
96 bendavid 1.4 if (fSize==0)
97     fProcID = TProcessID::GetSessionProcessID();
98 loizides 1.1 if (fProcID != TProcessID::GetSessionProcessID()) {
99     Error("Add", "Object can not be added as it has a different process id!");
100     return;
101     }
102     uid = TProcessID::AssignID(ae);
103     }
104    
105     fUIDs[fSize] = uid;
106     ++fSize;
107     }
108    
109     //--------------------------------------------------------------------------------------------------
110     template<class ArrayElement>
111     inline ArrayElement *mithep::RefArray<ArrayElement>::At(UInt_t idx)
112     {
113     // Return entry at given index.
114    
115     if(idx<fSize)
116     return static_cast<ArrayElement*>(GetObject(idx));
117    
118     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
119     return 0;
120     }
121    
122     //--------------------------------------------------------------------------------------------------
123     template<class ArrayElement>
124     inline const ArrayElement *mithep::RefArray<ArrayElement>::At(UInt_t idx) const
125     {
126     // Return entry at given index.
127    
128     if(idx<fSize)
129     return static_cast<const ArrayElement*>(GetObject(idx));
130    
131     Error("At", "Given index (%ud) is larger than array size (%ud)", idx, fSize);
132     return 0;
133     }
134    
135     //--------------------------------------------------------------------------------------------------
136     template<class ArrayElement>
137     TObject *mithep::RefArray<ArrayElement>::GetObject(UInt_t idx) const
138     {
139     // Return entry at given index. Code adapted from TRef::GetObject().
140    
141     if (!fProcID) {
142     Error("GetObject","Process id pointer is null!");
143     return 0;
144     }
145    
146     if (!TProcessID::IsValid(fProcID)) {
147     Error("GetObject","Process id is invalid!");
148     return 0;
149     }
150     UInt_t uid = GetUID(idx);
151    
152     //the reference may be in the TRefTable
153     TRefTable *table = TRefTable::GetRefTable();
154     if (table) {
155     table->SetUID(uid, fProcID);
156     table->Notify();
157     }
158    
159     //try to find the object from the table of the corresponding PID
160     TObject *obj = fProcID->GetObjectWithID(uid);
161     return obj;
162     }
163    
164     //--------------------------------------------------------------------------------------------------
165     template<class ArrayElement>
166     inline UInt_t mithep::RefArray<ArrayElement>::GetUID(UInt_t idx) const
167     {
168     // Return uid corresponding to idx.
169    
170     return fUIDs[idx];
171     }
172    
173     //--------------------------------------------------------------------------------------------------
174     template<class ArrayElement>
175     inline const ArrayElement *mithep::RefArray<ArrayElement>::operator[](UInt_t idx) const
176     {
177     // Return entry at given index.
178    
179     return At(idx);
180     }
181    
182     //--------------------------------------------------------------------------------------------------
183     template<class ArrayElement>
184     inline ArrayElement *mithep::RefArray<ArrayElement>::operator[](UInt_t idx)
185     {
186     // Return entry at given index.
187    
188     return At(idx);
189     }
190    
191     //-------------------------------------------------------------------------------------------------
192     template<class ArrayElement>
193     void mithep::RefArray<ArrayElement>::Streamer(TBuffer &b)
194     {
195     // Stream all objects in the array to or from the I/O buffer.
196    
197     if (b.IsReading()) {
198     //UInt_t sv, cv;
199     //b.ReadVersion(&sv, &cv);
200     //TObject::Streamer(b);
201     b >> fSize;
202     if (fSize) {
203     UShort_t pidf;
204     b >> pidf;
205     pidf += b.GetPidOffset();
206     fProcID = b.ReadProcessID(pidf);
207     b.ReadFastArray(fUIDs,fSize);
208     }
209     //b.CheckByteCount(sv, cv, RefArray::IsA());
210     } else { /*writing*/
211     //UInt_t cv;
212     //cv = b.WriteVersion(RefArray::IsA(), kTRUE);
213     //TObject::Streamer(b);
214     b << fSize;
215     if (fSize) {
216     UShort_t pidf;
217     pidf = b.WriteProcessID(fProcID);
218     b << pidf;
219     b.WriteFastArray(fUIDs,fSize);
220     }
221     //b.SetByteCount(cv, kTRUE);
222     }
223     }
224    
225     //--------------------------------------------------------------------------------------------------
226     template<class ArrayElement>
227     inline ArrayElement *mithep::RefArray<ArrayElement>::UncheckedAt(UInt_t idx)
228     {
229     // Return entry at given index.
230    
231     return static_cast<ArrayElement*>(GetObject(idx));
232     }
233    
234     //--------------------------------------------------------------------------------------------------
235     template<class ArrayElement>
236     inline const ArrayElement *mithep::RefArray<ArrayElement>::UncheckedAt(UInt_t idx) const
237     {
238     // Return entry at given index.
239    
240     return static_cast<const ArrayElement*>(GetObject(idx));
241     }
242     #endif