ATLAS Offline Software
JetCopier.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 #include "JetRec/JetCopier.h"
6 
8 #include "xAODJet/JetContainer.h"
11 #include "xAODCore/ShallowCopy.h"
12 
13 using xAOD::JetContainer;
14 
16 
17  ATH_MSG_DEBUG("Initializing...");
18 
19  if(m_inputJets.empty()){
20  ATH_MSG_ERROR("Jet finding requested with no inputs");
21 
22  return StatusCode::FAILURE;
23  }
24  else{
25  if(m_shallowIO && !m_shallowCopy){
26  ATH_MSG_ERROR("ShallowIO set to True while ShallowCopy is False");
27 
28  return StatusCode::FAILURE;
29  }
30  else{
31  ATH_CHECK(m_inputJets.initialize());
32  }
33  }
34 
35  return StatusCode::SUCCESS;
36 }
37 
38 
39 #ifndef XAOD_STANDALONE
40 // Setup helper to propagate decorations from original to copy
42  return m_decorDeps.initialize(m_inputJets, outputJets) ;
43 }
44 #endif
45 
46 
48  std::unique_ptr<xAOD::JetContainer> jets(nullptr);
49  std::unique_ptr<SG::IAuxStore> auxCont(nullptr);
50  std::tie(jets, auxCont) = getJets();
51 
52  if(jets.get() == nullptr || auxCont.get() == nullptr) {return StatusCode::FAILURE;}
53 
54  if(m_shallowCopy){
55  std::unique_ptr<xAOD::ShallowAuxContainer> auxCont_derived(static_cast<xAOD::ShallowAuxContainer*>(auxCont.release()));
56  ATH_CHECK( jetHandle.record(std::move(jets), std::move(auxCont_derived)) );
57 #ifndef XAOD_STANDALONE
58  ATH_CHECK( m_decorDeps.linkDecors (m_inputJets) );
59 #endif
60  }
61  else{
62  std::unique_ptr<xAOD::JetAuxContainer> auxCont_derived(static_cast<xAOD::JetAuxContainer*>(auxCont.release()));
63  ATH_CHECK( jetHandle.record(std::move(jets), std::move(auxCont_derived)) );
64 #ifndef XAOD_STANDALONE
65  ATH_CHECK( m_decorDeps.linkDecors (m_inputJets) );
66 #endif
67  }
68  return StatusCode::SUCCESS;
69 }
70 
71 std::pair<std::unique_ptr<xAOD::JetContainer>,std::unique_ptr<SG::IAuxStore> > JetCopier::getJets() const {
72  if(m_shallowCopy){
73  return ShallowCopyJets();
74  }
75  else{
76  return DeepCopyJets();
77  }
78 }
79 
80 std::pair<std::unique_ptr<xAOD::JetContainer>,std::unique_ptr<SG::IAuxStore> > JetCopier::ShallowCopyJets() const {
81 
82  // retrieve input
84 
85  if(inputJetsHandle.isValid()) {
86  ATH_MSG_DEBUG("Retrieval of JetContainer was OK");
87  } else {
88  ATH_MSG_ERROR("Retrieval of JetContainer failed");
89  return std::make_pair(std::unique_ptr<xAOD::JetContainer>(nullptr),std::unique_ptr<SG::IAuxStore>(nullptr));
90  }
91 
92  ATH_MSG_DEBUG("Shallow-copying jets");
93 
94  std::pair<xAOD::JetContainer*,xAOD::ShallowAuxContainer*> shallowcopy = xAOD::shallowCopyContainer(*inputJetsHandle);
95 
96  shallowcopy.second->setShallowIO(m_shallowIO);
97 
98  // Make sure that memory is managed safely
99  std::unique_ptr<xAOD::JetContainer> outjets(shallowcopy.first);
100  std::unique_ptr<xAOD::ShallowAuxContainer> shallowaux(shallowcopy.second);
101 
102  // Connect the copied jets to their originals
103  xAOD::setOriginalObjectLink(*inputJetsHandle, *outjets);
104 
105  return std::make_pair(std::move(outjets),std::move(shallowaux));
106 }
107 
108 std::pair<std::unique_ptr<xAOD::JetContainer>,std::unique_ptr<SG::IAuxStore> > JetCopier::DeepCopyJets() const {
109 
110  // retrieve input
111  SG::ReadHandle<JetContainer> inputJetsHandle(m_inputJets);
112 
113  if(inputJetsHandle.isValid()) {
114  ATH_MSG_DEBUG("Retrieval of JetContainer was OK");
115  } else {
116  ATH_MSG_ERROR("Retrieval of JetContainer failed");
117  return std::make_pair(std::unique_ptr<xAOD::JetContainer>(nullptr),std::unique_ptr<SG::IAuxStore>(nullptr));
118  }
119 
120  ATH_MSG_DEBUG("Deep-copying jets");
121 
122  std::unique_ptr<xAOD::JetContainer> goodJets = std::make_unique<xAOD::JetContainer>();
123  std::unique_ptr<xAOD::JetAuxContainer> goodJetsAux = std::make_unique<xAOD::JetAuxContainer>();
124 
125  goodJets->setStore (goodJetsAux.get());
126 
127  for(const xAOD::Jet* jet : *inputJetsHandle){
128  xAOD::Jet* goodJet = new xAOD::Jet();
129  goodJets->push_back (goodJet);
130  *goodJet = *jet;
131  }
132 
133  return std::make_pair(std::move(goodJets),std::move(goodJetsAux));
134 }
ShallowCopy.h
JetCopier::m_decorDeps
SG::ShallowCopyDecorDeps< xAOD::JetContainer > m_decorDeps
Definition: JetCopier.h:68
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
JetCopier::DeepCopyJets
virtual std::pair< std::unique_ptr< xAOD::JetContainer >, std::unique_ptr< SG::IAuxStore > > DeepCopyJets() const
Definition: JetCopier.cxx:108
JetCopier.h
xAOD::ShallowAuxContainer
Class creating a shallow copy of an existing auxiliary container.
Definition: ShallowAuxContainer.h:54
JetCopier::initWithOutput
virtual StatusCode initWithOutput(const SG::WriteHandleKey< xAOD::JetContainer > &outputJets) override
Method to allow the client to pass in a WriteHandle during initialisation, in case this is needed for...
Definition: JetCopier.cxx:41
xAOD::JetAuxContainer_v1
Temporary container used until we have I/O for AuxStoreInternal.
Definition: JetAuxContainer_v1.h:37
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::WriteHandleKey
Property holding a SG store/key/clid from which a WriteHandle is made.
Definition: StoreGate/StoreGate/WriteHandleKey.h:40
JetCopier::getJets
virtual std::pair< std::unique_ptr< xAOD::JetContainer >, std::unique_ptr< SG::IAuxStore > > getJets() const override
Method to build the collection and return it to the caller.
Definition: JetCopier.cxx:71
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
ReadHandle.h
Handle class for reading from StoreGate.
JetCopier::getAndRecordJets
virtual StatusCode getAndRecordJets(SG::WriteHandle< xAOD::JetContainer > &jetHandle) const override
Method to allow the client to pass in a WriteHandle for the container and aux container to be recorde...
Definition: JetCopier.cxx:47
JetCopier::m_shallowCopy
Gaudi::Property< bool > m_shallowCopy
Definition: JetCopier.h:64
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
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
JetCopier::m_inputJets
SG::ReadHandleKey< xAOD::JetContainer > m_inputJets
Definition: JetCopier.h:62
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
JetContainer.h
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
JetAuxContainer.h
JetCopier::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: JetCopier.cxx:15
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
IParticleHelpers.h
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
JetCopier::m_shallowIO
Gaudi::Property< bool > m_shallowIO
Definition: JetCopier.h:65
xAOD::Jet
Jet_v1 Jet
Definition of the current "jet version".
Definition: Event/xAOD/xAODJet/xAODJet/Jet.h:17
JetCopier::ShallowCopyJets
virtual std::pair< std::unique_ptr< xAOD::JetContainer >, std::unique_ptr< SG::IAuxStore > > ShallowCopyJets() const
Definition: JetCopier.cxx:80