ATLAS Offline Software
AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // DFlowAlg3.cxx
8 // Implementation file for class DFlowAlg3
9 // Author: S.Binet<binet@cern.ch>
11 #undef NDEBUG
12 
13 // AthExStoreGateExample includes
14 #include "DFlowAlg3.h"
15 #include "StoreGate/ReadHandle.h"
16 #include "StoreGate/UpdateHandle.h"
17 
18 // STL includes
19 
20 // FrameWork includes
21 #include "Gaudi/Property.h"
22 
23 namespace SG {
24 template<>
25 struct IsThreadSafeForUH<int, std::true_type>
26 {
27  typedef std::true_type type;
28 };
29 }
30 
31 namespace AthEx {
32 
34 // Public methods:
36 
37 // Constructors
39 DFlowAlg3::DFlowAlg3( const std::string& name,
40  ISvcLocator* pSvcLocator ) :
41  ::AthReentrantAlgorithm( name, pSvcLocator )
42 {
43 }
44 
45 // Destructor
48 {}
49 
50 // Athena Algorithm's Hooks
53 {
54  ATH_MSG_INFO ("Initializing " << name() << "...");
58 
59  return StatusCode::SUCCESS;
60 }
61 
63 {
64  ATH_MSG_INFO ("Finalizing " << name() << "...");
65 
66  return StatusCode::SUCCESS;
67 }
68 
69 StatusCode DFlowAlg3::execute (const EventContext& ctx) const
70 {
71  ATH_MSG_DEBUG ("Executing " << name() << "...");
72 
73  SG::ReadHandle<int> r_int (m_r_int, ctx);
74  ATH_MSG_INFO("================================");
75  ATH_MSG_INFO("myint r-handle...");
76  ATH_MSG_INFO("name: [" << r_int.name() << "]");
77  ATH_MSG_INFO("store [" << r_int.store() << "]");
78  ATH_MSG_INFO("clid: [" << r_int.clid() << "]");
79 
80  ATH_MSG_INFO("ptr: " << r_int.cptr());
81  if (r_int.isValid()) {
82  ATH_MSG_INFO("val: " << *(r_int.cptr()));
83  }
84 
86  ATH_MSG_INFO("ints r-handle...");
87  ATH_MSG_INFO("name: [" << r_ints.name() << "]");
88  ATH_MSG_INFO("store [" << r_ints.store() << "]");
89  ATH_MSG_INFO("clid: [" << r_ints.clid() << "]");
90 
91  ATH_MSG_INFO("ptr: " << r_ints.ptr());
92 
93  ATH_MSG_INFO("ints w-handle...");
94 
96  ATH_CHECK( w_ints.record (std::make_unique<std::vector<int> > (*r_ints)) );
97  w_ints->push_back(10);
98  if (r_int.isValid()) {
99  w_ints->push_back(*r_int);
100  }
101  ATH_MSG_INFO("size:" << w_ints->size());
102  for (int i = 0, imax = w_ints->size();
103  i!=imax;
104  ++i) {
105  ATH_MSG_INFO("val[" << i << "]= " << w_ints->at(i));
106  }
107 
108  // try to modify 'ints' via ReadHandle<>
109  // shouldn't compile
110 #ifdef TRY_COMPILATION_ERRORS
111  r_ints->push_back(666);
112 #endif
113 
114  // create a temporary r-handle
115  SG::ReadHandle<std::vector<int> > ints(r_ints.name());
116  ATH_MSG_INFO("temporary r-handle[ints] - size: " << ints->size());
117  ATH_MSG_INFO("compare pointers: ok=" << (ints.ptr() == r_ints.ptr()));
118  ATH_MSG_INFO("compare pointers: ok=" << (ints.ptr() == w_ints.ptr()));
119 
120  // test that modification thru one handle is seen thru the other one
121  std::vector<int> save = *w_ints;
122  *w_ints = std::vector<int>();
123  ATH_MSG_INFO("temporary r-handle[ints] - size: " << ints->size());
124  if (r_int.isValid()) {
125  ATH_MSG_INFO("data mbr r-handle[ints] - size: " << r_ints->size());
126  }
127  ATH_MSG_INFO("data mbr w-handle[ints] - size: " << w_ints->size());
128 
129  ATH_MSG_INFO("--restore--");
130  *w_ints = save;
131  ATH_MSG_INFO("temporary r-handle[ints] - size: " << ints->size());
132  if (r_int.isValid()) {
133  ATH_MSG_INFO("data mbr r-handle[ints] - size: " << r_ints->size());
134  }
135  ATH_MSG_INFO("data mbr w-handle[ints] - size: " << w_ints->size());
136 
137 
138  // test that inexistant proxies are correctly detected
139  ATH_MSG_INFO("-- testing inexistant proxies --");
140  {
141  SG::ReadHandle<int> o("--r-not-there--");
142  ATH_MSG_INFO("name: " << o.name());
143  ATH_MSG_INFO("valid:" << o.isValid());
144  if (o.isValid()) {
145  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
146  return StatusCode::FAILURE;
147  }
148  }
149  {
150  SG::WriteHandle<int> o("--w-not-there--");
151  ATH_MSG_INFO("name: " << o.name());
152  ATH_MSG_INFO("valid:" << o.isValid());
153  if (o.isValid()) {
154  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
155  return StatusCode::FAILURE;
156  }
157  }
158  {
159  SG::UpdateHandle<int> o("--rw-not-there--");
160  ATH_MSG_INFO("name: " << o.name());
161  ATH_MSG_INFO("valid:" << o.isValid());
162  if (o.isValid()) {
163  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
164  return StatusCode::FAILURE;
165  }
166  }
167 
168  //
169  if (r_int.isValid()) {
170  SG::UpdateHandle<int> rw_int(r_int.name());
171  if (rw_int.isValid()) {
172  ATH_MSG_INFO("temporary r/w-int: " << *rw_int);
173  }
174  ATH_CHECK( r_int.setConst() );
175  if (!r_int.isConst()) {
176  ATH_MSG_ERROR("ReadHandle<int>@[" << r_int.name() << "] should be CONST !");
177  return StatusCode::FAILURE;
178  }
179  //now we can't put it into an update handle anymore
180  SG::UpdateHandle<int> rw_const_int(r_int.name());
181  if (rw_const_int.isValid()) {
182  ATH_MSG_ERROR("UpdateHandle<int>@[" << r_int.name() << "] should not be allowed to refer to a const value !");
183  return StatusCode::FAILURE;
184  }
185 
186  }
187 
188  // test WVar<T> semantics
189  ATH_MSG_INFO("-- testing WVar<T> semantics...");
190  {
191  SG::WriteHandle<int> o(r_int.name());
192  ATH_MSG_INFO("name: " << o.name());
193  ATH_MSG_INFO("valid:" << o.isValid());
194  if (o.isValid()) {
195  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
196  return StatusCode::FAILURE;
197  }
198  try {
199  *o = 42;
200  if (o.isValid()) {
201  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
202  return StatusCode::FAILURE;
203  }
204  } catch (std::exception &err) {
205  ATH_MSG_INFO("good, caught: [" << err.what() << "]");
206  }
207  ATH_MSG_INFO("valid:" << o.isValid());
208  if (o.isValid()) {
209  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
210  return StatusCode::FAILURE;
211  }
212  }
213 
214  // test RWVar<T> semantics
215  ATH_MSG_INFO("-- testing RWVar<T> semantics...");
216  {
217  SG::UpdateHandle<int> o(r_int.name());
218  ATH_MSG_INFO("name: " << o.name());
219  ATH_MSG_INFO("valid:" << o.isValid());
220  if (o.isValid()) {
221  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
222  return StatusCode::FAILURE;
223  }
224  try {
225  *o = 42;
226  if (o.isValid()) {
227  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
228  return StatusCode::FAILURE;
229  }
230  } catch (std::exception &err) {
231  ATH_MSG_INFO("good, caught: [" << err.what() << "]");
232  }
233  ATH_MSG_INFO("valid:" << o.isValid());
234  if (o.isValid()) {
235  ATH_MSG_ERROR("should NOT be valid ! [line " << __LINE__ << "]" );
236  return StatusCode::FAILURE;
237  }
238  }
239  return StatusCode::SUCCESS;
240 }
241 
242 
243 } //> end namespace AthEx
SG::IsThreadSafeForUH
Definition: UpdateHandle.h:36
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
SG::VarHandleBase::clid
CLID clid() const
Return the class ID for the referenced object.
SG::UpdateHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
SG::VarHandleBase::setConst
StatusCode setConst()
Set the 'const' bit for the bound proxy in the store.
Definition: StoreGate/src/VarHandleBase.cxx:599
SG::IsThreadSafeForUH< int, std::true_type >::type
std::true_type type
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.cxx:27
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
checkTP.save
def save(self, fileName="./columbo.out")
Definition: checkTP.py:178
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:193
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
AthEx
Definition: Hist.h:25
SG::VarHandleBase::store
std::string store() const
Return the name of the store holding the object we are proxying.
Definition: StoreGate/src/VarHandleBase.cxx:379
calibdata.exception
exception
Definition: calibdata.py:496
AthEx::DFlowAlg3::initialize
virtual StatusCode initialize() override
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.cxx:52
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
AthEx::DFlowAlg3::finalize
virtual StatusCode finalize() override
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.cxx:62
imax
int imax(int i, int j)
Definition: TileLaserTimingTool.cxx:33
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
AthEx::DFlowAlg3::~DFlowAlg3
virtual ~DFlowAlg3()
Destructor:
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.cxx:47
AthEx::DFlowAlg3::DFlowAlg3
DFlowAlg3()=delete
SG::WriteHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
AthEx::DFlowAlg3::m_r_int
SG::ReadHandleKey< int > m_r_int
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.h:43
SG::UpdateHandle
Definition: UpdateHandle.h:94
DFlowAlg3.h
SG::ReadHandle::ptr
const_pointer_type ptr()
Dereference the pointer.
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
UpdateHandle.h
Handle class for modifying an existing object in StoreGate.
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
SG::VarHandleBase::isConst
bool isConst() const
True if this handle has a proxy, and the proxy is const.
Definition: StoreGate/src/VarHandleBase.cxx:437
ReadHandle.h
Handle class for reading from StoreGate.
AthEx::DFlowAlg3::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.cxx:69
AthEx::DFlowAlg3::m_r_ints
SG::ReadHandleKey< std::vector< int > > m_r_ints
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.h:45
AthEx::DFlowAlg3::m_w_ints
SG::WriteHandleKey< std::vector< int > > m_w_ints
Definition: AthenaExamples/AthExStoreGateExample/src_dflow/DFlowAlg3.h:47