ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Ref.h
Revision: 1.6
Committed: Tue Mar 24 05:44:14 2009 UTC (16 years, 1 month ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_014d, Mit_014c, Mit_014b, Mit_014a, Mit_014, Mit_014pre3, Mit_014pre2, Mit_014pre1, Mit_013d, Mit_013c, Mit_013b, Mit_013a, Mit_013, Mit_013pre1, Mit_012i, Mit_012h, Mit_012g, Mit_012f, Mit_012e, Mit_012d, Mit_012c, Mit_012b, Mit_012a, Mit_012, Mit_011a, Mit_011, Mit_010a, Mit_010, Mit_009c, Mit_009b, Mit_009a, Mit_009, Mit_008
Changes since 1.5: +2 -2 lines
Log Message:
Typo

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 loizides 1.6 // $Id: Ref.h,v 1.5 2009/03/23 22:15:10 loizides Exp $
3 bendavid 1.1 //
4     // Ref
5     //
6 loizides 1.6 // Templated implementation of our own TRef.
7 bendavid 1.1 //
8     // Authors: J.Bendavid
9     //--------------------------------------------------------------------------------------------------
10    
11     #ifndef MITANA_DATACONT_REF_H
12     #define MITANA_DATACONT_REF_H
13    
14     #include <TObject.h>
15     #include <TRefTable.h>
16     #include <TProcessID.h>
17     #include <TError.h>
18 loizides 1.3 #include "MitAna/DataCont/interface/RefResolver.h"
19 bendavid 1.1 #include "MitAna/DataCont/interface/ProcIDRef.h"
20    
21     namespace mithep
22     {
23     template<class ArrayElement>
24     class Ref
25     {
26     public:
27 loizides 1.3 Ref() : fPID(0), fUID(0) {}
28 bendavid 1.1 Ref(const ArrayElement *ae) { SetObject(ae); }
29 loizides 1.3 virtual ~Ref() {}
30 bendavid 1.1
31     Bool_t IsNull() const { return fUID==0 ? kTRUE : kFALSE; }
32 loizides 1.4 Bool_t IsValid() const { return !IsNull(); }
33 bendavid 1.1 const ArrayElement *Obj() const;
34     ArrayElement *Obj();
35     Bool_t RefsObject(const ArrayElement *ae) const;
36     void SetObject(const ArrayElement *ae);
37    
38 loizides 1.4 void operator= (const ArrayElement *ae) { SetObject(ae); }
39 bendavid 1.1 Bool_t operator==(const ArrayElement *ae) { return RefsObject(ae); }
40    
41     protected:
42     TObject *GetObject() const;
43    
44 loizides 1.3 ProcIDRef fPID; //||process id corresponding to referenced object
45     UInt_t fUID; //unique id of the referenced object
46    
47     ClassDef(Ref, 1) // Templated implementation of our own TRef
48 bendavid 1.1 };
49     }
50    
51     //--------------------------------------------------------------------------------------------------
52     template<class ArrayElement>
53     TObject *mithep::Ref<ArrayElement>::GetObject() const
54     {
55 loizides 1.3 // Return pointer to object. Code adapted from TRef::GetObject().
56 bendavid 1.1
57     if (IsNull())
58     return 0;
59    
60 loizides 1.3 TProcessID *pid = fPID.Pid();
61 bendavid 1.1 if (!pid) {
62     Fatal("GetObject","Process id pointer is null!");
63     return 0;
64     }
65    
66     if (!TProcessID::IsValid(pid)) {
67     Fatal("GetObject","Process id is invalid!");
68     return 0;
69     }
70    
71 loizides 1.3 return RefResolver::GetObjectWithID(fUID,pid);
72 bendavid 1.1 }
73    
74     //--------------------------------------------------------------------------------------------------
75     template<class ArrayElement>
76     const ArrayElement *mithep::Ref<ArrayElement>::Obj() const
77     {
78     // Return entry at given index. Code adapted from TRef::GetObject().
79    
80     return static_cast<const ArrayElement*>(GetObject());
81     }
82    
83     //--------------------------------------------------------------------------------------------------
84     template<class ArrayElement>
85     ArrayElement *mithep::Ref<ArrayElement>::Obj()
86     {
87     // Return entry at given index. Code adapted from TRef::GetObject().
88    
89     return static_cast<ArrayElement*>(GetObject());
90     }
91    
92     //--------------------------------------------------------------------------------------------------
93     template<class ArrayElement>
94     Bool_t mithep::Ref<ArrayElement>::RefsObject(const ArrayElement *ae) const
95     {
96 loizides 1.4 // Check whether Ref points to given object.
97 bendavid 1.1
98     if (IsNull())
99     return kFALSE;
100    
101     if (!ae->TestBit(kIsReferenced))
102     return kFALSE;
103    
104     UInt_t oUid = ae->GetUniqueID();
105     TProcessID *oPid = TProcessID::GetProcessWithUID(oUid, const_cast<ArrayElement*>(ae));
106 loizides 1.4 if (!oPid)
107     return kFALSE;
108 bendavid 1.1
109 loizides 1.4 if ( (fUID&0xffffff)!=(oUid&0xffffff) || fPID.Pid()->GetUniqueID()!=oPid->GetUniqueID())
110 bendavid 1.1 return kFALSE;
111 loizides 1.4 return kTRUE;
112 bendavid 1.1 }
113    
114     //--------------------------------------------------------------------------------------------------
115     template<class ArrayElement>
116     void mithep::Ref<ArrayElement>::SetObject(const ArrayElement *ae)
117     {
118 loizides 1.4 // Set reference to object.
119    
120     if (!ae)
121     return;
122 bendavid 1.1
123     // check if the object can belong here and assign or get its uid
124     if (ae->TestBit(kHasUUID)) {
125 loizides 1.5 Fatal("Add", "Object cannot be added as it has not UUID!");
126 bendavid 1.1 return;
127     }
128    
129     if (ae->TestBit(kIsReferenced)) {
130     fUID = ae->GetUniqueID();
131     fPID.SetPid(TProcessID::GetProcessWithUID(fUID, const_cast<ArrayElement*>(ae)));
132     } else {
133     fPID.SetPid(TProcessID::GetSessionProcessID());
134     fUID = TProcessID::AssignID(const_cast<ArrayElement*>(ae));
135     }
136     }
137     #endif