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 <print>
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
45 template <class T>
48
50 template <SG::IsAuxDataVector T>
54 using type =
55 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>;
56 };
57
59 template <SG::IsConstAuxElement T>
60 struct ShallowCopyResult<T> {
63 using type = std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo>>;
64 };
65
67 template <class T>
69
84 template <SG::IsAuxDataVector T>
85 ShallowCopyResult_t<T> shallowCopy(const T& cont, const EventContext& ctx);
86
98 template <SG::IsAuxDataVector T>
100
118 template <SG::IsConstAuxElement T>
119 ShallowCopyResult_t<T> shallowCopy(const T& obj, const EventContext& ctx);
120
135 template <SG::IsConstAuxElement T>
137
138 namespace detail{
143 template<class T>
144 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
147
148 if (!link.cptr()) {
149 // This can happen when we read the input file in standalone
150 // mode in branch access mode. That's unfortunately not
151 // compatible with using a shallow copy auxiliary store.
152 std::println( "xAOD::shallowCopyContainer ERROR Couldn't set up "
153 "DataLink to the original container's auxiliary store" );
154 std::println( "xAOD::shallowCopyContainer ERROR Are you using the "
155 "xAOD::TEvent::kBranchAccess access mode?" );
156 return {};
157 }
158
159 // Create the new DV container:
160 auto newCont = std::make_unique<T>();
161
162 // Add the required number of elements to it:
163 newCont->reserve(cont.size());
164 for (size_t i = 0; i < cont.size(); ++i) {
165 newCont->push_back(prepareElementForShallowCopy(cont[i]));
166 }
167
168 // Create a new shallow auxiliary container:
169 auto aux = std::make_unique<ShallowAuxContainer>(link);
170
171 // Connect it to the interface container:
172 newCont->setStore(aux.get());
173
174 // Return the new objects:
175 return std::make_pair(std::move(newCont), std::move(aux));
176 }
177 }//end namespace detail
178
197
198 template<class T>
199 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
200 shallowCopyContainer(const T& cont,
201 const EventContext& ctx )
202 {
203 DataLink<SG::IConstAuxStore> link (cont.getConstStore(), ctx);
204 return detail::shallowCopyContainerImpl(cont,link);
205 }
206
208 //
222 template< class T >
223 [[deprecated("Please switch to xAOD::shallowCopy(...), which returns unique_ptr")]]
224 std::pair< T*, ShallowAuxContainer* > shallowCopyContainer( const T& cont ) {
225 DataLink<SG::IConstAuxStore> link (cont.getConstStore());
226 auto tmp = detail::shallowCopyContainerImpl(cont, link);
227 return std::make_pair(tmp.first.release(), tmp.second.release());
228 }
229
250 template< class T >
251 std::pair< std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo> >
252 shallowCopyObject( const T& obj, const EventContext& ctx ) {
253
254 // Create a DataLink, to check if we'll be successful:
255 DataLink< SG::IConstAuxStore > link( obj.getConstStore(), ctx );
256 if( ! link.cptr() ) {
257 // This can happen when we read the input file in standalone
258 // mode in branch access mode. That's unfortunately not
259 // compatible with using a shallow copy auxiliary store.
260 std::println( "xAOD::shallowCopyObject ERROR Couldn't set up "
261 "DataLink to the original object's auxiliary store" );
262 std::println ( "xAOD::shallowCopyObject ERROR Are you using the "
263 "xAOD::TEvent::kBranchAccess access mode?" );
264 std::pair< std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo> > dummy;
265 return dummy;
266 }
267
268 // Create a new object using its default constructor:
269 auto newObj = std::make_unique<T>();
270
271 // Create a new shallow auxiliary store:
272 auto aux = std::make_unique<ShallowAuxInfo>( link );
273
274 // Connect it to the interface object:
275 newObj->setStore( aux.get() );
276
277 // Return the new objects:
278 return std::make_pair( std::move(newObj), std::move(aux) );
279 }
280
281 // Backwards compatibility
282 template< class T >
283 [[deprecated("Please switch to xAOD::shallowCopy(...), which returns unique_ptr")]]
284 std::pair< T*, ShallowAuxInfo* >
285 shallowCopyObject( const T& obj ) {
286 auto ptrs = shallowCopyObject (obj, Gaudi::Hive::currentContext());
287 return std::make_pair (ptrs.first.release(), ptrs.second.release());
288 }
289
290} // namespace xAOD
291
292// Include the implementation.
294
295#endif // XAODCORE_SHALLOWCOPY_H
Interface for const operations on an auxiliary store.
Concept for "xAOD style" DataVector interface containers.
Test if T is compatible with ConstAuxElement.
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.
typename ShallowCopyResult< T >::type ShallowCopyResult_t
Return type of xAOD::shallowCopy.
Definition ShallowCopy.h:68
std::unique_ptr< CaloCluster > prepareElementForShallowCopy(const CaloCluster *orgCluster)
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.
ShallowCopyResult_t< T > shallowCopy(const T &cont, const EventContext &ctx)
Create a shallow copy of an existing 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.
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > type
The type returned by xAOD::shallowCopy when called with a container of type T.
Definition ShallowCopy.h:54
Base trait for the return type of xAOD::shallowCopy.
Definition ShallowCopy.h:47