ATLAS Offline Software
IParticleHelpers.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // System include(s):
6 #include <iostream>
7 
8 // EDM include(s):
9 #include "AthLinks/ElementLink.h"
10 
11 // Local include(s):
13 
14 namespace xAOD {
15 
18  acc( "originalObjectLink" );
19 
30  bool setOriginalObjectLink( const IParticle& original,
31  IParticle& copy ) {
32 
33  // We mustn't check for the availability of the decoration on the copied
34  // object here. Since if the copy is already part of a container, the
35  // decoration may well exist on it already. But it will not be set to
36  // anything meaningful yet.
37 
38  // Check if the original is part of a container. If not, we can't set
39  // up a link.
40  const IParticleContainer* container =
41  dynamic_cast< const IParticleContainer* >( original.container() );
42  if( ! container ) {
43  std::cerr << "xAOD::setOriginalObjectLink ERROR Original object is "
44  << "not part of a container" << std::endl;
45  return false;
46  }
47 
48  // Construct the ElementLink that points back to the original.
49  // We have to be tricky here. The original itself may already be a
50  // copy. In which case the new object should point back to the same
51  // original that "our original" is pointing to.
53  ( acc.isAvailable( original ) ?
54  acc( original ) :
55  ElementLink< IParticleContainer >( *container, original.index() ) );
56 
57  // Now set this link on the copy:
58  acc( copy ) = link;
59 
60  // We were successful:
61  return true;
62  }
63 
83 
84  // Check that the containers are of the same size:
85  if( original.size() != copy.size() ) {
86  std::cerr << "xAOD::setOriginalObjectLink ERROR Size of original "
87  << "and copy containers differs" << std::endl;
88  return false;
89  }
90 
91  // Make sure that the original is not a view container. As the function
92  // doesn't work correctly for those.
93  if( original.ownPolicy() != SG::OWN_ELEMENTS ) {
94  std::cerr << "xAOD::setOriginalObjectLink ERROR Received a view "
95  << "container" << std::endl;
96  return false;
97  }
98 
99  // If the containers are empty, we're done:
100  if( copy.empty() ) {
101  return true;
102  }
103 
104  // Create an ElementLink to the first element in the original container.
105  // To be able to re-use the hashed key of this object in the loop.
106  const ElementLink< IParticleContainer > refLink( original, 0 );
107 
108  // Loop over the copied container:
109  IParticleContainer::const_iterator orig_itr = original.begin();
110  IParticleContainer::const_iterator orig_end = original.end();
111  IParticleContainer::iterator copy_itr = copy.begin();
112  // To speed up the loop over large containers a bit, make the decision
113  // about how to create the links, just once:
114  if( acc.isAvailable( **orig_itr ) ) {
115  for( ; orig_itr != orig_end; ++orig_itr, ++copy_itr ) {
116  // Copy the variable from the original object:
117  acc( **copy_itr ) = acc( **orig_itr );
118  }
119  } else {
120  for( ; orig_itr != orig_end; ++orig_itr, ++copy_itr ) {
121  // Construct the link from scratch:
122  acc( **copy_itr ) =
124  ( *orig_itr )->index() );
125  }
126  }
127 
128  // We were successful:
129  return true;
130  }
131 
141 
142  // Check if the decoration is available on the object:
143  if( ! acc.isAvailable( copy ) ) {
144  return nullptr;
145  }
146 
147  // Get the link:
148  const ElementLink< IParticleContainer >& link = acc( copy );
149 
150  // Check if the link is valid:
151  if( ! link.isValid() ) {
152  return nullptr;
153  }
154 
155  // Apparently all is fine:
156  return *link;
157  }
158 
169 
170  // Blindly retrieve the link. Rely on the auxiliary store code throwing an
171  // appropriate exception if this cannot be done. ;-)
172  return acc( copy );
173  }
174 
175 } // namespace xAOD
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:66
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
SG::OWN_ELEMENTS
@ OWN_ELEMENTS
this data object owns its elements
Definition: OwnershipPolicy.h:17
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
xAOD::getOriginalObjectLink
const ElementLink< IParticleContainer > & getOriginalObjectLink(const IParticle &copy)
This function should be used instead of xAOD::getOriginalObject when we want to use the "original obj...
Definition: IParticleHelpers.cxx:168
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
xAOD::setOriginalObjectLink
bool setOriginalObjectLink(const IParticle &original, IParticle &copy)
This function should be used by CP tools when they make a deep copy of an object in their correctedCo...
Definition: IParticleHelpers.cxx:30
DataVector::ownPolicy
SG::OwnershipPolicy ownPolicy() const
Return the ownership policy setting for this container.
IParticleHelpers.h
calibdata.copy
bool copy
Definition: calibdata.py:27
SG::AuxElement::container
const SG::AuxVectorData * container() const
Return the container holding this element.
xAOD::getOriginalObject
const IParticle * getOriginalObject(const IParticle &copy)
This function can be used to conveniently get a pointer back to the original object from which a copy...
Definition: IParticleHelpers.cxx:140
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.