ATLAS Offline Software
BTagTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /***************************************************************************
6  BTagTool.cxx - Description
7  -------------------
8  begin : 10.03.04
9  authors : Andreas Wildauer (CERN PH-ATC), Fredrik Akesson (CERN PH-ATC)
10  email : andreas.wildauer@cern.ch, fredrik.akesson@cern.ch
11 
12  changes : 13.01.05 (FD) introduce soft lepton case
13 
14 ***************************************************************************/
15 
16 #include "BTagging/BTagTool.h"
17 
18 #include "xAODTracking/Vertex.h"
20 
21 namespace Analysis {
22 
23  BTagTool::BTagTool(const std::string& t, const std::string& n, const IInterface* p) :
24  base_class(t,n,p),
25  m_bTagToolHandleArray(this),
26  m_bTagTool(std::map<std::string, ITagTool*>())
27  {
28  // List of the tagging tools to be used, HAS TO BE given by jobOptions
29  declareProperty("TagToolList", m_bTagToolHandleArray);
30  }
31 
33 
34  // This will check that the properties were initialized properly
35  // by job configuration.
36  ATH_CHECK( m_VertexCollectionName.initialize() );
37 
38  /* ----------------------------------------------------------------------------------- */
39  /* RETRIEVE SERVICES FROM STOREGATE */
40  /* ----------------------------------------------------------------------------------- */
41  if ( m_bTagToolHandleArray.empty() ) {
42  ATH_MSG_DEBUG("#BTAG# No tagging tools defined. Please revisit configuration.");
43  }
44  else {
45  if ( m_bTagToolHandleArray.retrieve().isFailure() ) {
46  ATH_MSG_ERROR("#BTAG# Failed to retrieve " << m_bTagToolHandleArray);
47  return StatusCode::FAILURE;
48  } else {
49  ATH_MSG_DEBUG("#BTAG# Retrieved " << m_bTagToolHandleArray);
50  }
51  }
52 
53  // get BJetTagTools
54  ToolHandleArray< ITagTool >::iterator itTagTools = m_bTagToolHandleArray.begin();
55  ToolHandleArray< ITagTool >::iterator itTagToolsEnd = m_bTagToolHandleArray.end();
56  for ( ; itTagTools != itTagToolsEnd; ++itTagTools ) {
57  ATH_MSG_DEBUG("#BTAG# "<< name() << " inserting: " << (*itTagTools).typeAndName() << " to tools list.");
58  m_bTagTool.insert(std::pair<std::string, ITagTool*>((*itTagTools).name(), &(*(*itTagTools))));
59  }
60 
61  // JBdV for debugging
62  m_nBeamSpotPvx = 0;
63  m_nAllJets = 0;
64 
65  return StatusCode::SUCCESS;
66  }
67 
68 
69  StatusCode BTagTool::tagJet(const xAOD::Jet* jetToTag, xAOD::BTagging* BTag, const std::string &jetName, const xAOD::Vertex* vtx) const {
70 
71  ATH_MSG_VERBOSE("#BTAG# (p, E) of original Jet: (" << jetToTag->px() << ", " << jetToTag->py() << ", "
72  << jetToTag->pz() << "; " << jetToTag->e() << ") MeV");
73 
74  m_nAllJets++;
75 
76  /* ----------------------------------------------------------------------------------- */
77  /* RETRIEVE PRIMARY VERTEX CONTAINER FROM STOREGATE */
78  /* ----------------------------------------------------------------------------------- */
79  const xAOD::Vertex* primaryVertex(nullptr);
80  SG::ReadHandle<xAOD::VertexContainer> h_VertexCollectionName (m_VertexCollectionName);
81 
82  if (vtx) {
83  primaryVertex = vtx;
84  } else {
85  if (!h_VertexCollectionName.isValid()) {
86  ATH_MSG_ERROR( " cannot retrieve primary vertex container with key " << m_VertexCollectionName.key() );
87  return StatusCode::FAILURE;
88  }
89  unsigned int nVertexes = h_VertexCollectionName->size();
90  if (nVertexes == 0) {
91  ATH_MSG_DEBUG("#BTAG# Vertex container is empty");
92  return StatusCode::SUCCESS;
93  }
94 
95  for (const auto *fz : *h_VertexCollectionName) {
96  if (fz->vertexType() == xAOD::VxType::PriVtx) {
97  primaryVertex = fz;
98  break;
99  }
100  }
101 
102  if (! primaryVertex) {
103  ATH_MSG_DEBUG("#BTAG# No vertex labeled as VxType::PriVtx!");
104  xAOD::VertexContainer::const_iterator fz = h_VertexCollectionName->begin();
105  primaryVertex = *fz;
106  if (primaryVertex->nTrackParticles() == 0) {
107  ATH_MSG_DEBUG("#BTAG# PV==BeamSpot: probably poor tagging");
108  m_nBeamSpotPvx++;
109  }
110  }
111  }
112 
113  /* ----------------------------------------------------------------------------------- */
114  /* Call all the tag tools specified in m_bTagToolHandleArray */
115  /* ----------------------------------------------------------------------------------- */
116 
117  for (const ToolHandle<ITagTool>& tool : m_bTagToolHandleArray) {
118  StatusCode sc = tool->tagJet(*primaryVertex, *jetToTag, *BTag, jetName);
119  if (sc.isFailure()) {
120  ATH_MSG_WARNING("#BTAG# failed tagger: " << tool.typeAndName() );
121  }
122 
123  }
124 
125  // ----------------------------------------------------------------------------------
126 
127  ATH_MSG_VERBOSE("#BTAG# tagJet(...) end.");
128  return StatusCode::SUCCESS;
129  }
130 
131  StatusCode BTagTool::tagJet(const xAOD::JetContainer * jetContainer, xAOD::BTaggingContainer * btaggingContainer, const std::string &jetName) const {
132 
133  /* ----------------------------------------------------------------------------------- */
134  /* RETRIEVE PRIMARY VERTEX CONTAINER FROM STOREGATE */
135  /* ----------------------------------------------------------------------------------- */
136  SG::ReadHandle<xAOD::VertexContainer> h_VertexCollectionName (m_VertexCollectionName);
137  if (!h_VertexCollectionName.isValid()) {
138  ATH_MSG_ERROR( " cannot retrieve primary vertex container with key " << m_VertexCollectionName.key() );
139  return StatusCode::FAILURE;
140  }
141  unsigned int nVertexes = h_VertexCollectionName->size();
142  if (nVertexes == 0) {
143  ATH_MSG_DEBUG("#BTAG# Vertex container is empty");
144  return StatusCode::SUCCESS;
145  }
146 
147  const xAOD::Vertex* primaryVertex(nullptr);
148  for (const auto *fz : *h_VertexCollectionName) {
149  if (fz->vertexType() == xAOD::VxType::PriVtx) {
150  primaryVertex = fz;
151  break;
152  }
153  }
154 
155 
156  if (! primaryVertex) {
157  ATH_MSG_DEBUG("#BTAG# No vertex labeled as VxType::PriVtx!");
158  xAOD::VertexContainer::const_iterator fz = h_VertexCollectionName->begin();
159  primaryVertex = *fz;
160  if (primaryVertex->nTrackParticles() == 0) {
161  ATH_MSG_DEBUG("#BTAG# PV==BeamSpot: probably poor tagging");
162  m_nBeamSpotPvx++;
163  }
164  }
165 
166  xAOD::BTaggingContainer::iterator btagIter=btaggingContainer->begin();
167  for (xAOD::JetContainer::const_iterator jetIter = jetContainer->begin(); jetIter != jetContainer->end(); ++jetIter, ++btagIter) {
168  ATH_MSG_VERBOSE("#BTAG# (p, E) of original Jet: (" << (*jetIter)->px() << ", " << (*jetIter)->py() << ", "
169  << (*jetIter)->pz() << "; " << (*jetIter)->e() << ") MeV");
170  m_nAllJets++;
171 
172  /* ----------------------------------------------------------------------------------- */
173  /* Call all the tag tools specified in m_bTagToolHandleArray */
174  /* ----------------------------------------------------------------------------------- */
175 
176  for (const ToolHandle<ITagTool>& tool : m_bTagToolHandleArray) {
177  StatusCode sc = tool->tagJet(*primaryVertex, **jetIter, **btagIter, jetName);
178  if (sc.isFailure()) {
179  ATH_MSG_WARNING("#BTAG# failed tagger: " << tool.typeAndName() );
180  }
181 
182  }
183 
184  // ----------------------------------------------------------------------------------
185 
186  ATH_MSG_VERBOSE("#BTAG# tagJet(...) end.");
187  }
188 
189  return StatusCode::SUCCESS;
190  }
191 
193  ATH_MSG_INFO("#BTAG# number of jets with Primary Vertex set to BeamSpot " << m_nBeamSpotPvx << " Total number of jets seen " << m_nAllJets);
194  return StatusCode::SUCCESS;
195  }
196 
197 
199  void BTagTool::finalizeHistos() {
200  // ITagTool * myTagTool(0);
201  // /// finalize calculates the integratedIP histos
202  // myTagTool = m_bTagTool["LifetimeTag1D"];
203  // if (myTagTool!=0) myTagTool->finalizeHistos();
204  // myTagTool = m_bTagTool["LifetimeTag2D"];
205  // if (myTagTool!=0) myTagTool->finalizeHistos();
206  // myTagTool = m_bTagTool["SecVtxTagBU"];
207  // if (myTagTool!=0) myTagTool->finalizeHistos();
208  }
209 
210 }
211 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:53
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
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
initialize
void initialize()
Definition: run_EoverP.cxx:894
BTagTool.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Analysis::BTagTool::BTagTool
BTagTool(const std::string &, const std::string &, const IInterface *)
Constructors and destructors.
Definition: BTagTool.cxx:32
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
xAOD::Jet_v1::pz
float pz() const
The z-component of the jet's momentum.
Definition: Jet_v1.cxx:99
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
beamspotman.n
n
Definition: beamspotman.py:731
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
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::Jet_v1::py
float py() const
The y-component of the jet's momentum.
Definition: Jet_v1.cxx:94
xAOD::BTagging_v1
Definition: BTagging_v1.h:39
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
Vertex.h
JetAnalysisAlgorithmsTest_EMTopo_eljob.jetContainer
string jetContainer
Definition: JetAnalysisAlgorithmsTest_EMTopo_eljob.py:36
Analysis
The namespace of all packages in PhysicsAnalysis/JetTagging.
Definition: BTaggingCnvAlg.h:20
xAOD::Jet_v1::px
float px() const
The x-component of the jet's momentum.
Definition: Jet_v1.cxx:90
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
VertexContainer.h
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
xAOD::Jet_v1::e
virtual double e() const
The total energy of the particle.
Definition: Jet_v1.cxx:63
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAODType::BTag
@ BTag
The object is a b-tagging object.
Definition: ObjectType.h:60
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.