ATLAS Offline Software
Loading...
Searching...
No Matches
ShallowCopy.h
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7#ifndef XAODCORE_SHALLOWCOPY_H
8#define XAODCORE_SHALLOWCOPY_H
9
10// System include(s):
11#include <map>
12#include <iostream>
13
14// EDM include(s):
15#include "AthLinks/DataLink.h"
20
21// Local include(s):
24
25
26
27namespace xAOD {
28
38
39 template< class T >
40 std::unique_ptr<T> prepareElementForShallowCopy(const T* /*elem*/) {
41 return std::make_unique<T>();
42 }
43
58 template <SG::IsAuxDataVector T>
59 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
60 shallowCopy(const T& cont, const EventContext& ctx);
61
73 template <SG::IsAuxDataVector T>
74 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
75 shallowCopy(const T& cont);
76
94 template <SG::IsConstAuxElement T>
95 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo>> shallowCopy(
96 const T& obj, const EventContext& ctx);
97
112 template <SG::IsConstAuxElement T>
113 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo>> shallowCopy(
114 const T& obj);
115
116 namespace detail{
121 template<class T>
122 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
125
126 if (!link.cptr()) {
127 // This can happen when we read the input file in standalone
128 // mode in branch access mode. That's unfortunately not
129 // compatible with using a shallow copy auxiliary store.
130 std::cout << "xAOD::shallowCopyContainer ERROR Couldn't set up "
131 << "DataLink to the original container's auxiliary store"
132 << std::endl;
133 std::cout << "xAOD::shallowCopyContainer ERROR Are you using the "
134 << "xAOD::TEvent::kBranchAccess access mode?" << std::endl;
135 return {};
136 }
137
138 // Create the new DV container:
139 auto newCont = std::make_unique<T>();
140
141 // Add the required number of elements to it:
142 newCont->reserve(cont.size());
143 for (size_t i = 0; i < cont.size(); ++i) {
144 newCont->push_back(prepareElementForShallowCopy(cont[i]));
145 }
146
147 // Create a new shallow auxiliary container:
148 auto aux = std::make_unique<ShallowAuxContainer>(link);
149
150 // Connect it to the interface container:
151 newCont->setStore(aux.get());
152
153 // Return the new objects:
154 return std::make_pair(std::move(newCont), std::move(aux));
155 }
156 }//end namespace detail
157
176
177 template<class T>
178 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
179 shallowCopyContainer(const T& cont,
180 const EventContext& ctx )
181 {
182 DataLink<SG::IConstAuxStore> link (cont.getConstStore(), ctx);
183 return detail::shallowCopyContainerImpl(cont,link);
184 }
185
187 //
201 template< class T >
202 std::pair< T*, ShallowAuxContainer* > shallowCopyContainer( const T& cont ) {
203 DataLink<SG::IConstAuxStore> link (cont.getConstStore());
204 auto tmp = detail::shallowCopyContainerImpl(cont, link);
205 return std::make_pair(tmp.first.release(), tmp.second.release());
206 }
207
228 template< class T >
229 std::pair< std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo> >
230 shallowCopyObject( const T& obj, const EventContext& ctx ) {
231
232 // Create a DataLink, to check if we'll be successful:
233 DataLink< SG::IConstAuxStore > link( obj.getConstStore(), ctx );
234 if( ! link.cptr() ) {
235 // This can happen when we read the input file in standalone
236 // mode in branch access mode. That's unfortunately not
237 // compatible with using a shallow copy auxiliary store.
238 std::cout << "xAOD::shallowCopyObject ERROR Couldn't set up "
239 << "DataLink to the original object's auxiliary store"
240 << std::endl;
241 std::cout << "xAOD::shallowCopyObject ERROR Are you using the "
242 << "xAOD::TEvent::kBranchAccess access mode?"
243 << std::endl;
244 std::pair< std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo> > dummy;
245 return dummy;
246 }
247
248 // Create a new object using its default constructor:
249 auto newObj = std::make_unique<T>();
250
251 // Create a new shallow auxiliary store:
252 auto aux = std::make_unique<ShallowAuxInfo>( link );
253
254 // Connect it to the interface object:
255 newObj->setStore( aux.get() );
256
257 // Return the new objects:
258 return std::make_pair( std::move(newObj), std::move(aux) );
259 }
260
261 // Backwards compatibility
262 template< class T >
263 std::pair< T*, ShallowAuxInfo* >
264 shallowCopyObject( const T& obj ) {
265 auto ptrs = shallowCopyObject (obj, Gaudi::Hive::currentContext());
266 return std::make_pair (ptrs.first.release(), ptrs.second.release());
267 }
268
269} // namespace xAOD
270
271// Include the implementation.
273
274#endif // XAODCORE_SHALLOWCOPY_H
Interface for const operations on an auxiliary store.
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainerImpl(const T &cont, DataLink< SG::IConstAuxStore > &link)
Impl function for shallow copy container.
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
std::unique_ptr< CaloCluster > prepareElementForShallowCopy(const CaloCluster *orgCluster)
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopy(const T &cont, const EventContext &ctx)
Create a shallow copy of an existing container.
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, const EventContext &ctx)
Function making a shallow copy of a constant container.
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxInfo > > shallowCopyObject(const T &obj, const EventContext &ctx)
Function making a shallow copy of a constant standalone object.