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
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::cout << "xAOD::shallowCopyContainer ERROR Couldn't set up "
153 << "DataLink to the original container's auxiliary store"
154 << std::endl;
155 std::cout << "xAOD::shallowCopyContainer ERROR Are you using the "
156 << "xAOD::TEvent::kBranchAccess access mode?" << std::endl;
157 return {};
158 }
159
160 // Create the new DV container:
161 auto newCont = std::make_unique<T>();
162
163 // Add the required number of elements to it:
164 newCont->reserve(cont.size());
165 for (size_t i = 0; i < cont.size(); ++i) {
166 newCont->push_back(prepareElementForShallowCopy(cont[i]));
167 }
168
169 // Create a new shallow auxiliary container:
170 auto aux = std::make_unique<ShallowAuxContainer>(link);
171
172 // Connect it to the interface container:
173 newCont->setStore(aux.get());
174
175 // Return the new objects:
176 return std::make_pair(std::move(newCont), std::move(aux));
177 }
178 }//end namespace detail
179
198
199 template<class T>
200 std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
201 shallowCopyContainer(const T& cont,
202 const EventContext& ctx )
203 {
204 DataLink<SG::IConstAuxStore> link (cont.getConstStore(), ctx);
205 return detail::shallowCopyContainerImpl(cont,link);
206 }
207
209 //
223 template< class T >
224 [[deprecated("Please switch to xAOD::shallowCopy(...), which returns unique_ptr")]]
225 std::pair< T*, ShallowAuxContainer* > shallowCopyContainer( const T& cont ) {
226 DataLink<SG::IConstAuxStore> link (cont.getConstStore());
227 auto tmp = detail::shallowCopyContainerImpl(cont, link);
228 return std::make_pair(tmp.first.release(), tmp.second.release());
229 }
230
251 template< class T >
252 std::pair< std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo> >
253 shallowCopyObject( const T& obj, const EventContext& ctx ) {
254
255 // Create a DataLink, to check if we'll be successful:
256 DataLink< SG::IConstAuxStore > link( obj.getConstStore(), ctx );
257 if( ! link.cptr() ) {
258 // This can happen when we read the input file in standalone
259 // mode in branch access mode. That's unfortunately not
260 // compatible with using a shallow copy auxiliary store.
261 std::cout << "xAOD::shallowCopyObject ERROR Couldn't set up "
262 << "DataLink to the original object's auxiliary store"
263 << std::endl;
264 std::cout << "xAOD::shallowCopyObject ERROR Are you using the "
265 << "xAOD::TEvent::kBranchAccess access mode?"
266 << std::endl;
267 std::pair< std::unique_ptr<T>, std::unique_ptr<ShallowAuxInfo> > dummy;
268 return dummy;
269 }
270
271 // Create a new object using its default constructor:
272 auto newObj = std::make_unique<T>();
273
274 // Create a new shallow auxiliary store:
275 auto aux = std::make_unique<ShallowAuxInfo>( link );
276
277 // Connect it to the interface object:
278 newObj->setStore( aux.get() );
279
280 // Return the new objects:
281 return std::make_pair( std::move(newObj), std::move(aux) );
282 }
283
284 // Backwards compatibility
285 template< class T >
286 [[deprecated("Please switch to xAOD::shallowCopy(...), which returns unique_ptr")]]
287 std::pair< T*, ShallowAuxInfo* >
288 shallowCopyObject( const T& obj ) {
289 auto ptrs = shallowCopyObject (obj, Gaudi::Hive::currentContext());
290 return std::make_pair (ptrs.first.release(), ptrs.second.release());
291 }
292
293} // namespace xAOD
294
295// Include the implementation.
297
298#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