ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/RefArray.h
Revision: 1.6
Committed: Thu Oct 23 18:22:27 2008 UTC (16 years, 6 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.5: +3 -10 lines
Log Message:
Cleanup to have TObject::Fatal rather than Fatal to avoid some warnings.

File Contents

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