ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/RefArray.h
Revision: 1.2
Committed: Wed Sep 10 03:33:26 2008 UTC (16 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_004
Changes since 1.1: +3 -3 lines
Log Message:
Cleanup

File Contents

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