ATLAS Offline Software
ShallowCopy.h
Go to the documentation of this file.
1 // Dear emacs, this is -*- c++ -*-
2 
3 /*
4  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // $Id: ShallowCopy.h 766390 2016-08-04 11:18:59Z wlampl $
8 #ifndef XAODCORE_SHALLOWCOPY_H
9 #define XAODCORE_SHALLOWCOPY_H
10 
11 // System include(s):
12 #include <map>
13 #include <iostream>
14 
15 // EDM include(s):
16 #include "AthLinks/DataLink.h"
18 
19 // Local include(s):
22 
23 #ifndef XAOD_STANDALONE
24 #include <GaudiKernel/ThreadLocalContext.h>
25 #else
26 class EventContext;
27 #endif
28 
29 
30 namespace xAOD {
31 
41 
42  template< class T >
43  std::unique_ptr<T> prepareElementForShallowCopy(const T* /*elem*/) {
44  return std::make_unique<T>();
45  }
46 
47  namespace detail{
52  template<class T>
53  std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
54  shallowCopyContainerImpl(const T& cont,
56 
57  if (!link.cptr()) {
58  // This can happen when we read the input file in standalone
59  // mode in branch access mode. That's unfortunately not
60  // compatible with using a shallow copy auxiliary store.
61  std::cout << "xAOD::shallowCopyContainer ERROR Couldn't set up "
62  << "DataLink to the original container's auxiliary store"
63  << std::endl;
64  std::cout << "xAOD::shallowCopyContainer ERROR Are you using the "
65  << "xAOD::TEvent::kBranchAccess access mode?" << std::endl;
66  return {};
67  }
68 
69  // Create the new DV container:
70  auto newCont = std::make_unique<T>();
71 
72  // Add the required number of elements to it:
73  newCont->reserve(cont.size());
74  for (size_t i = 0; i < cont.size(); ++i) {
75  newCont->push_back(prepareElementForShallowCopy(cont[i]));
76  }
77 
78  // Create a new shallow auxiliary container:
79  auto aux = std::make_unique<ShallowAuxContainer>(link);
80 
81  // Connect it to the interface container:
82  newCont->setStore(aux.get());
83 
84  // Return the new objects:
85  return std::make_pair(std::move(newCont), std::move(aux));
86  }
87  }//end namespace detail
88 
107 
108  template<class T>
109  std::pair<std::unique_ptr<T>, std::unique_ptr<ShallowAuxContainer>>
110  shallowCopyContainer(const T& cont,
111  [[maybe_unused]] const EventContext& ctx
112  ){
113 #ifndef XAOD_STANDALONE
114  DataLink<SG::IConstAuxStore> link (cont.getConstStore(), ctx);
115 #else
116  //Note that in AnalysisBase
117  //- EventContext, is not a complete type, we have no class.
118  //- We do not have a DataLink ctor with EventContext
119  //- The return value of currentContext() can not be used.
120  DataLink<SG::IConstAuxStore> link(cont.getConstStore());
121 #endif
122  return detail::shallowCopyContainerImpl(cont,link);
123  }
124 
126  //
140  template< class T >
141  std::pair< T*, ShallowAuxContainer* > shallowCopyContainer( const T& cont ) {
142  DataLink<SG::IConstAuxStore> link (cont.getConstStore());
143  auto tmp = detail::shallowCopyContainerImpl(cont, link);
144  return std::make_pair(tmp.first.release(), tmp.second.release());
145  }
146 
162  template< class T >
163  std::pair< T*, ShallowAuxInfo* > shallowCopyObject( const T& obj ) {
164 
165  // Create a DataLink, to check if we'll be successful:
166  DataLink< SG::IConstAuxStore > link( obj.getConstStore() );
167  if( ! link.cptr() ) {
168  // This can happen when we read the input file in standalone
169  // mode in branch access mode. That's unfortunately not
170  // compatible with using a shallow copy auxiliary store.
171  std::cout << "xAOD::shallowCopyObject ERROR Couldn't set up "
172  << "DataLink to the original object's auxiliary store"
173  << std::endl;
174  std::cout << "xAOD::shallowCopyObject ERROR Are you using the "
175  << "xAOD::TEvent::kBranchAccess access mode?"
176  << std::endl;
177  std::pair< T*, ShallowAuxInfo* > dummy;
178  return dummy;
179  }
180 
181  // Create a new object using its default constructor:
182  T* newObj = new T();
183 
184  // Create a new shallow auxiliary store:
185  ShallowAuxInfo* aux = new ShallowAuxInfo( link );
186 
187  // Connect it to the interface object:
188  newObj->setStore( aux );
189 
190  // Return the new objects:
191  return std::make_pair( newObj, aux );
192  }
193 
194 } // namespace xAOD
195 
196 #endif // XAODCORE_SHALLOWCOPY_H
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
detail
Definition: extract_histogram_tag.cxx:14
ShallowAuxContainer.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
xAOD::ShallowAuxInfo
Shallow copy for the auxiliary store of standalone objects.
Definition: ShallowAuxInfo.h:29
xAOD::shallowCopyObject
std::pair< T *, ShallowAuxInfo * > shallowCopyObject(const T &obj)
Function making a shallow copy of a constant standalone object.
Definition: ShallowCopy.h:163
python.xAODType.dummy
dummy
Definition: xAODType.py:4
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
ShallowAuxInfo.h
IConstAuxStore.h
Interface for const operations on an auxiliary store.
python.PyAthena.obj
obj
Definition: PyAthena.py:135
xAOD::detail::shallowCopyContainerImpl
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainerImpl(const T &cont, DataLink< SG::IConstAuxStore > &link)
Impl function for shallow copy container.
Definition: ShallowCopy.h:54
xAOD::prepareElementForShallowCopy
std::unique_ptr< CaloCluster > prepareElementForShallowCopy(const CaloCluster *orgCluster)
Definition: CaloClusterShallowCopy.cxx:7