ATLAS Offline Software
JetBTaggingAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
11 
12 #include <string>
13 #include <optional>
14 
15 namespace Analysis {
16 
17 
18  JetBTaggingAlg::JetBTaggingAlg(const std::string& n, ISvcLocator *p) :
20  m_JetName(""),
21  m_bTagTool("Analysis::BTagTool",this),
22  m_bTagSecVtxTool("Analysis::BTagSecVertexing",this)
23  {
24  declareProperty("JetCalibrationName", m_JetName);
25  declareProperty("BTagTool", m_bTagTool);
26  declareProperty("BTagSecVertexing", m_bTagSecVtxTool);
27  }
28 
30 
33 
34  // This will check that the properties were initialized properly
35  // by job configuration.
36  ATH_CHECK( m_JetCollectionName.initialize() );
37  ATH_CHECK( m_IncomingTracks.initialize() );
38  ATH_CHECK( m_OutgoingTracks.initialize() );
39  ATH_CHECK( m_BTaggingCollectionName.initialize() );
40  ATH_CHECK( m_jetBTaggingLinkName.initialize() );
41  ATH_CHECK( m_bTagJetDecorLinkName.initialize() );
42 
43  // this is a terrible, awful hack
44  // but right now there aren't any muons for b-tagging in the trigger
45  // so if an empty muon container is passed, DON'T DECLARE A DEPENDENCY
46  // we'll make an empty container on the b-tagging object later...
47  m_DoMuons = !m_IncomingMuons.key().empty();
48 
49  if (m_DoMuons) {
50  ATH_MSG_DEBUG("#BTAG# muons requested for: " << m_JetCollectionName.key());
52  ATH_CHECK( m_IncomingMuons.initialize(true) );
53  } else {
54  ATH_MSG_DEBUG("#BTAG# no muons requested for: " << m_JetCollectionName.key());
55  ATH_CHECK( m_IncomingMuons.initialize(false) );
56  }
57 
59  ATH_CHECK( m_OutgoingMuons.initialize() );
60 
61  ATH_MSG_DEBUG("#BTAG# Jet container name: " << m_JetCollectionName.key());
62  ATH_MSG_DEBUG("#BTAG# BTagging container name: " << m_BTaggingCollectionName.key());
63  ATH_MSG_DEBUG("#BTAG# EL from Jet to BTagging: " << m_jetBTaggingLinkName.key());
64  ATH_MSG_DEBUG("#BTAG# EL from BTagging to Jet: " << m_bTagJetDecorLinkName.key());
65 
67  if ( m_bTagTool.retrieve().isFailure() ) {
68  ATH_MSG_FATAL("#BTAG# Failed to retrieve tool " << m_bTagTool);
69  return StatusCode::FAILURE;
70  } else {
71  ATH_MSG_DEBUG("#BTAG# Retrieved tool " << m_bTagTool);
72  }
73 
75  if ( m_bTagSecVtxTool.retrieve().isFailure() ) {
76  ATH_MSG_FATAL("#BTAGVTX# Failed to retrieve tool " << m_bTagSecVtxTool);
77  return StatusCode::FAILURE;
78  } else {
79  ATH_MSG_DEBUG("#BTAGVTX# Retrieved tool " << m_bTagSecVtxTool);
80  }
81 
84 
85  return StatusCode::SUCCESS;
86  }
87 
88 
89  StatusCode JetBTaggingAlg::execute(const EventContext& ctx) const {
90  //retrieve the Jet container
92  if (!h_JetCollectionName.isValid()) {
93  ATH_MSG_ERROR( " cannot retrieve jet container with key " << m_JetCollectionName.key() );
94  return StatusCode::FAILURE;
95  }
96 
97  if (h_JetCollectionName->empty()) {
98  ATH_MSG_DEBUG("#BTAG# Empty JetContainer !!");
99  }
100  else {
101  ATH_MSG_DEBUG("#BTAG# Nb jets in JetContainer: "<< h_JetCollectionName->size());
102  }
103 
105  h_IncomingTracks(m_IncomingTracks, ctx);
106 
107  std::optional<SG::ReadDecorHandle<xAOD::JetContainer, std::vector<ElementLink< xAOD::IParticleContainer> > > >
108  h_IncomingMuons;
109 
110  if (m_DoMuons) {
112  tmp(m_IncomingMuons, ctx);
113  h_IncomingMuons = tmp;
114  }
115 
116 
118  h_OutgoingTracks(m_OutgoingTracks, ctx);
119 
121  h_OutgoingMuons(m_OutgoingMuons, ctx);
122 
123 
124  //Decor Jet with element link to the BTagging
126  //Decor BTagging with element link to the Jet
128 
129  //Create a xAOD::BTaggingContainer in any case (must be done)
130  std::string bTaggingContName = m_BTaggingCollectionName.key();
131  ATH_MSG_DEBUG("#BTAG# Container name: "<< bTaggingContName);
132 
133  /* Record the BTagging output container */
135  ATH_CHECK( h_BTaggingCollectionName.record(std::make_unique<xAOD::BTaggingContainer>(),
136  std::make_unique<xAOD::BTaggingAuxContainer>()) );
137 
138  MagField::AtlasFieldCache fieldCache;
139  // Get field cache object
141  const AtlasFieldCacheCondObj* fieldCondObj{*readHandle};
142 
143  if (fieldCondObj == nullptr) {
144  ATH_MSG_ERROR("Failed to retrieve AtlasFieldCacheCondObj with key " << m_fieldCacheCondObjInputKey.key());
145  return StatusCode::FAILURE;
146  }
147  fieldCondObj->getInitializedCache (fieldCache);
148 
149  if (!fieldCache.solenoidOn()) {
150  for (const auto *jet : *h_JetCollectionName) {
152  h_jetBTaggingLinkName(*jet) = linkBTagger;
153  }
154  return StatusCode::SUCCESS;
155  } else { //Solenoid ON
156  for (const auto *jet : *h_JetCollectionName.ptr()) {
157  xAOD::BTagging *newBTagMT = new xAOD::BTagging();
158  h_BTaggingCollectionName->push_back(newBTagMT);
159 
160  // Track association
161  const std::vector<ElementLink<xAOD::IParticleContainer> >& trackLinks = h_IncomingTracks(*jet);
162 
163  std::vector<ElementLink<xAOD::TrackParticleContainer> > tmpTracks;
164 
165  tmpTracks.reserve(trackLinks.size());
166  for (const ElementLink<xAOD::IParticleContainer>& elpart : trackLinks)
167  tmpTracks.emplace_back(elpart.key(), elpart.index());
168 
169  h_OutgoingTracks(*newBTagMT) = tmpTracks;
170 
171  // Muon association
172  // awful hack part deux
173  // if a non-empty incoming muon key was requested
174  // then we actually copy them over
175  // otherwise just create an empty container
176  std::vector<ElementLink<xAOD::MuonContainer> > tmpMuons;
177  if (m_DoMuons) {
178  const std::vector<ElementLink<xAOD::IParticleContainer> >& muonLinks = (*h_IncomingMuons)(*jet);
179 
180  for (const ElementLink<xAOD::IParticleContainer>& elpart : muonLinks)
181  tmpMuons.emplace_back(elpart.key(), elpart.index());
182  }
183 
184  h_OutgoingMuons(*newBTagMT) = tmpMuons;
185 
186  }
187 
188  }
189 
190  // Secondary vertex reconstruction.
191  StatusCode SV = m_bTagSecVtxTool->BTagSecVertexing_exec(h_JetCollectionName.ptr(), h_BTaggingCollectionName.ptr());
192  if (SV.isFailure()) {
193  ATH_MSG_WARNING("#BTAG# Failed to reconstruct sec vtx");
194  }
195 
196 
197  //Tag the jets
198  SV = m_bTagTool->tagJet( h_JetCollectionName.ptr(), h_BTaggingCollectionName.ptr(), m_JetName);
199  if (SV.isFailure()) {
200  ATH_MSG_WARNING("#BTAG# Failed in taggers call");
201  }
202 
203  //Create the element link from the jet to the btagging and reverse link
204  for (size_t jetIndex=0; jetIndex < h_JetCollectionName->size() ; ++jetIndex) {
205  const xAOD::Jet * jetToTag = h_JetCollectionName->at(jetIndex);
206  xAOD::BTagging * itBTag = h_BTaggingCollectionName->at(jetIndex);
208  linkBTagger.toContainedElement(*h_BTaggingCollectionName.ptr(), itBTag);
209  h_jetBTaggingLinkName(*jetToTag) = linkBTagger;
211  linkJet.toContainedElement(*h_JetCollectionName.ptr(), jetToTag);
212  h_bTagJetLinkName(*itBTag) = linkJet;
213  }
214 
215  return StatusCode::SUCCESS;
216 
217  }
218 
219 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
AtlasFieldCacheCondObj
Definition: AtlasFieldCacheCondObj.h:19
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
Analysis::JetBTaggingAlg::m_BTaggingCollectionName
SG::WriteHandleKey< xAOD::BTaggingContainer > m_BTaggingCollectionName
Definition: JetBTaggingAlg.h:56
Analysis::JetBTaggingAlg::JetBTaggingAlg
JetBTaggingAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructors and destructors.
Definition: JetBTaggingAlg.cxx:18
Analysis::JetBTaggingAlg::m_jetBTaggingLinkName
SG::WriteDecorHandleKey< xAOD::JetContainer > m_jetBTaggingLinkName
Definition: JetBTaggingAlg.h:55
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
Analysis::JetBTaggingAlg::m_IncomingMuons
SG::ReadDecorHandleKey< xAOD::JetContainer > m_IncomingMuons
Definition: JetBTaggingAlg.h:50
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
BTaggingAuxContainer.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
Analysis::JetBTaggingAlg::m_JetCollectionName
SG::ReadHandleKey< xAOD::JetContainer > m_JetCollectionName
Definition: JetBTaggingAlg.h:46
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
WriteDecorHandle.h
Handle class for adding a decoration to an object.
Analysis::JetBTaggingAlg::initialize
virtual StatusCode initialize() override
Main routines specific to an ATHENA algorithm.
Definition: JetBTaggingAlg.cxx:29
Analysis::JetBTaggingAlg::m_bTagJetDecorLinkName
SG::WriteDecorHandleKey< xAOD::BTaggingContainer > m_bTagJetDecorLinkName
Definition: JetBTaggingAlg.h:57
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
xAOD::BTagging_v1
Definition: BTagging_v1.h:39
Analysis::JetBTaggingAlg::m_bTagTool
ToolHandle< IBTagTool > m_bTagTool
Definition: JetBTaggingAlg.h:63
MagField::AtlasFieldCache::solenoidOn
bool solenoidOn() const
status of the magnets
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Analysis::JetBTaggingAlg::m_OutgoingTracks
SG::WriteDecorHandleKey< xAOD::BTaggingContainer > m_OutgoingTracks
Definition: JetBTaggingAlg.h:49
Analysis
The namespace of all packages in PhysicsAnalysis/JetTagging.
Definition: BTaggingCnvAlg.h:20
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
MuonContainer.h
SG::ReadHandle::ptr
const_pointer_type ptr()
Dereference the pointer.
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.
Analysis::JetBTaggingAlg::m_OutgoingMuons
SG::WriteDecorHandleKey< xAOD::BTaggingContainer > m_OutgoingMuons
Definition: JetBTaggingAlg.h:51
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::BTagging
BTagging_v1 BTagging
Definition of the current "BTagging version".
Definition: BTagging.h:17
MagField::AtlasFieldCache
Local cache for magnetic field (based on MagFieldServices/AtlasFieldSvcTLS.h)
Definition: AtlasFieldCache.h:43
Analysis::JetBTaggingAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: JetBTaggingAlg.cxx:89
Analysis::JetBTaggingAlg::m_DoMuons
bool m_DoMuons
Definition: JetBTaggingAlg.h:61
ReadDecorHandle.h
Handle class for reading a decoration on an object.
Analysis::JetBTaggingAlg::m_JetName
std::string m_JetName
Definition: JetBTaggingAlg.h:59
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
Analysis::JetBTaggingAlg::m_IncomingTracks
SG::ReadDecorHandleKey< xAOD::JetContainer > m_IncomingTracks
Definition: JetBTaggingAlg.h:48
Analysis::JetBTaggingAlg::m_fieldCacheCondObjInputKey
SG::ReadCondHandleKey< AtlasFieldCacheCondObj > m_fieldCacheCondObjInputKey
Definition: JetBTaggingAlg.h:54
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
Analysis::JetBTaggingAlg::m_bTagSecVtxTool
ToolHandle< IBTagLightSecVertexing > m_bTagSecVtxTool
Definition: JetBTaggingAlg.h:64
JetBTaggingAlg.h