ATLAS Offline Software
ElementLinkResetAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // System include(s):
6 #include <map>
7 
8 // Framework include(s):
10 
11 // EDM include(s):
16 #include "AthLinks/ElementLinkBase.h"
17 
18 // Local include(s):
19 #include "ElementLinkResetAlg.h"
20 
21 namespace xAODMaker {
22 
24  ISvcLocator* svcLoc )
25  : AthAlgorithm( name, svcLoc ) {
26 
27  declareProperty( "SGKeys", m_keys );
28  }
29 
31 
32  // Tell the user what's happening:
33  ATH_MSG_INFO( "Initialising" );
34  ATH_MSG_DEBUG( "SGKeys = " << m_keys );
35 
36  // Return gracefully:
37  return StatusCode::SUCCESS;
38  }
39 
41 
42  // Collect all the container(s):
43  std::vector< std::pair< const SG::IConstAuxStore*, std::string > > stores;
44  if( !m_keys.empty() ) {
45  for( const std::string& key : m_keys ) {
46  const SG::IConstAuxStore* store = nullptr;
48  stores.emplace_back( store, key );
49  }
50  } else {
53  for( auto itr = begin; itr != end; ++itr ) {
54  const SG::IConstAuxStore* store = nullptr;
55  ATH_CHECK( evtStore()->retrieve( store, itr.key() ) );
56  stores.emplace_back( store, itr.key() );
57  }
58  }
59  ATH_MSG_DEBUG( "Number of IConstAuxStore objects retrieved: "
60  << stores.size() );
61 
62  // Reset the ElementLinks in all of them:
63  for( const auto& storeKey : stores ) {
64  ATH_MSG_VERBOSE( "Reseting element links in store: "
65  << storeKey.second );
66  ATH_CHECK( reset( *( storeKey.first ), storeKey.second ) );
67  }
68 
69  // Return gracefully:
70  return StatusCode::SUCCESS;
71  }
72 
74  const std::string& key ) {
75 
76  // If the container is empty, return right away:
77  if( ! store.size() ) {
78  return StatusCode::SUCCESS;
79  }
80 
81  // Get all the IDs stored in this object:
82  const SG::auxid_set_t& auxids = store.getAuxIDs();
83 
84  // The auxiliary type registry:
86 
87  // Loop over them:
88  for( SG::auxid_t auxid : auxids ) {
89 
90  // Check/cache its type:
91  if( m_typeCache.size() <= auxid ) {
92  m_typeCache.resize( auxid + 1 );
93  }
94  if( ! m_typeCache[ auxid ].isSet ) {
95  const std::string tname =
96  SG::normalizedTypeinfoName( *( reg.getType( auxid ) ) );
97  static const std::string pat1 = "ElementLink<";
98  static const std::string pat2 = "std::vector<ElementLink<";
99  if( tname.substr( 0, pat1.size() ) == pat1 ) {
100  m_typeCache[ auxid ].isEL = true;
101  } else if( tname.substr( 0, pat2.size() ) == pat2 ) {
102  m_typeCache[ auxid ].isELVec = true;
103  }
104  m_typeCache[ auxid ].isSet = true;
105  ATH_MSG_VERBOSE( "Type for \"" << tname << "\": isEL = "
106  << m_typeCache[ auxid ].isEL << ", isELVec = "
107  << m_typeCache[ auxid ].isELVec );
108  }
109 
110  // If it's not an EL type, then don't bother:
111  if( ! ( m_typeCache[ auxid ].isEL || m_typeCache[ auxid ].isELVec ) ) {
112  continue;
113  }
114 
115  // Get a pointer to the vector variable. We need to cast away
116  // its constness in this ugly way, we can't afford to only ask for
117  // non-const pointers from the store. Since the ElementLink to be
118  // reset may very well be a const variable that was accessed already,
119  // and now needs its cache wiped.
120  void* ptr = const_cast< void* >( store.getData( auxid ) );
121 
122  // If the pointer is null, then something dodgy happened with this
123  // (dynamic) variable.
124  if( ! ptr ) {
125  // Check if this is a static variable. If it is, it's not an error
126  // to get a null pointer for it. Since such a case can only happen
127  // when a new static variable was introduced into the EDM, and we're
128  // reading an old input file that doesn't have this variable in it
129  // yet. Which is an okay scenario.
130  const SG::IAuxStoreIO* storeIO =
131  dynamic_cast< const SG::IAuxStoreIO* >( &store );
132  if( ( ! storeIO ) || ( storeIO->getDynamicAuxIDs().find( auxid ) !=
133  storeIO->getDynamicAuxIDs().end() ) ) {
134  REPORT_MESSAGE( MSG::ERROR )
135  << "Invalid pointer received for variable: " << key
136  << reg.getName( auxid );
137  } else {
138  ATH_MSG_DEBUG( "Static variable " << key << reg.getName( auxid )
139  << " is empty" );
140  }
141  continue;
142  }
143 
144  // Get the variable's element size:
145  const size_t eltSize = reg.getEltSize( auxid );
146 
147  // Loop over its elements:
148  const size_t sz_i = store.size();
149  for( size_t i = 0; i < sz_i; ++i ) {
150 
151  // Raw pointer to the object:
152  void* eltPtr = reinterpret_cast< char* >( ptr ) + i * eltSize;
153 
154  // Do different things based on the variable type:
155  if( m_typeCache[ auxid ].isEL ) {
156  // Cast to an ElementLinkBase object:
157  ElementLinkBase* elb =
158  reinterpret_cast< ElementLinkBase* >( eltPtr );
159  // Reset the link only if it has its persistent variable(s) set:
160  if( elb->persKey() ) {
161  elb->toTransient();
162  }
163  } else if( m_typeCache[ auxid ].isELVec ) {
164  // For a vector of links we heavily rely on knowing how GCC/Clang
165  // lays out the memory for vectors.
166  std::vector< ElementLinkBase >& v =
167  *( reinterpret_cast< std::vector< ElementLinkBase >* >( eltPtr ) );
168  const size_t sz_j = v.size();
169  for( size_t j = 0; j < sz_j; ++j ) {
170  // Access the link:
171  ElementLinkBase& elb = v[ j ];
172  // Reset the link only if it has its persistent variable(s)
173  // set:
174  if( elb.persKey() ) {
175  elb.toTransient();
176  }
177  }
178  } else {
179  ATH_MSG_FATAL( "There is a logic error in the code" );
180  return StatusCode::FAILURE;
181  }
182  }
183  }
184 
185  // Return gracefully:
186  return StatusCode::SUCCESS;
187  }
188 
190  : isSet( false ), isEL( false ), isELVec( false ) {
191 
192  }
193 
194 } // namespace xAODMaker
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
store
StoreGateSvc * store
Definition: fbtTestBasics.cxx:69
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAODMaker::ElementLinkResetAlg::m_typeCache
std::vector< AuxIDType > m_typeCache
Cached types of the auxiliary IDs.
Definition: ElementLinkResetAlg.h:68
SG::AuxTypeRegistry::instance
static AuxTypeRegistry & instance()
Return the singleton registry instance.
Definition: AuxTypeRegistry.cxx:49
SG::normalizedTypeinfoName
std::string normalizedTypeinfoName(const std::type_info &info)
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
Definition: normalizedTypeinfoName.cxx:120
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
CxxUtils::ConcurrentBitset::end
const_iterator end() const
Return an end iterator.
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
xAODMaker::ElementLinkResetAlg::reset
StatusCode reset(const SG::IConstAuxStore &store, const std::string &key)
Function reseting all the ElementLinks in one specific container.
Definition: ElementLinkResetAlg.cxx:73
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAODMaker::ElementLinkResetAlg::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: ElementLinkResetAlg.cxx:30
xAODMaker
Definition: StoreGateSvc.h:72
python.DomainsRegistry.reg
reg
globals -----------------------------------------------------------------—
Definition: DomainsRegistry.py:343
SG::IAuxStoreIO
Interface providing I/O for a generic auxiliary store.
Definition: IAuxStoreIO.h:44
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
ElementLinkBase
Base class for ElementLinks to vectors of pointers.
Definition: AthLinks/ElementLinkBase.h:59
ElementLinkBase::persKey
sgkey_t persKey() const
Return the SG key that we reference, as a hash.
Definition: ElementLinkBase.cxx:13
SG::AuxTypeRegistry
Handle mappings between names and auxid_t.
Definition: AuxTypeRegistry.h:62
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
SG::auxid_t
size_t auxid_t
Identifier for a particular aux data item.
Definition: AuxTypes.h:27
ElementLinkBase::toTransient
bool toTransient(IProxyDict *sg=0)
Finish initialization after link has been read.
lumiFormat.i
int i
Definition: lumiFormat.py:92
xAODMaker::ElementLinkResetAlg::execute
virtual StatusCode execute()
Function executing the algorithm.
Definition: ElementLinkResetAlg.cxx:40
IAuxStoreIO.h
Interface providing I/O for a generic auxiliary store.
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::IAuxStoreIO::getDynamicAuxIDs
virtual const SG::auxid_set_t & getDynamicAuxIDs() const =0
Get the list of all dynamically created variables.
AthAlgorithm
Definition: AthAlgorithm.h:47
normalizedTypeinfoName.h
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
CxxUtils::ConcurrentBitset::find
const_iterator find(bit_t bit) const
If bit bit is set, return an iterator pointing to it.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
errorcheck.h
Helpers for checking error return status codes and reporting errors.
REPORT_MESSAGE
#define REPORT_MESSAGE(LVL)
Report a message.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:365
python.PyAthena.v
v
Definition: PyAthena.py:157
AuxTypeRegistry.h
Handle mappings between names and auxid_t.
SG::auxid_set_t
A set of aux data identifiers.
Definition: AuxTypes.h:47
xAODMaker::ElementLinkResetAlg::ElementLinkResetAlg
ElementLinkResetAlg(const std::string &name, ISvcLocator *svcLoc)
Regular Algorithm constructor.
Definition: ElementLinkResetAlg.cxx:23
ElementLinkResetAlg.h
IConstAuxStore.h
Interface for const operations on an auxiliary store.
SG::IConstAuxStore
Interface for const operations on an auxiliary store.
Definition: IConstAuxStore.h:64
SG::ConstIterator
Definition: SGIterator.h:163
xAODMaker::ElementLinkResetAlg::AuxIDType::AuxIDType
AuxIDType()
Definition: ElementLinkResetAlg.cxx:189
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
xAODMaker::ElementLinkResetAlg::m_keys
std::vector< std::string > m_keys
StoreGate keys of the auxiliary objects to be processed.
Definition: ElementLinkResetAlg.h:56