ATLAS Offline Software
JetVertexTaggerTool.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // JetVertexTaggerTool.cxx
8 // Implementation file for class JetVertexTaggerTool
9 // Author: James Frost <james.frost@cern.ch>
11 
16 #include <utility>
17 
18 using std::string;
19 using xAOD::JetFourMom_t;
20 
21 //**********************************************************************
22 
24 : asg::AsgTool(name),
25  m_jvthisto(nullptr)
26 {
27 }
28 
29 //**********************************************************************
30 
32  if(m_jvthisto) delete m_jvthisto;
33 }
34 
35 //**********************************************************************
36 
38  ATH_MSG_INFO("Initializing JetVertexTaggerTool " << name());
39  ATH_MSG_INFO("Using origin vertex: " << m_useOriginVertex);
40 
41  if(m_jetContainerName.empty()){
42  ATH_MSG_ERROR("JetVertexTaggerTool needs to have its input jet container configured!");
43  return StatusCode::FAILURE;
44  }
45 
46  // Use the Path Resolver to find the jvt file and retrieve the likelihood histogram
48  ATH_MSG_INFO(" Reading JVT file from:\n " << m_jvtfileName << "\n");
49  ATH_MSG_INFO(" resolved in :\n " << m_fn << "\n\n");
50 
51  std::unique_ptr<TFile> jvtfile {TFile::Open(m_fn)};
52  if(!jvtfile){
53  ATH_MSG_FATAL("Cannot open JVTLikelihoodFile: " << m_fn);
54  return StatusCode::FAILURE;
55  }
56 
57  ATH_MSG_VERBOSE("\n Reading JVT likelihood histogram from:\n " << m_fn << "\n\n");
58 
59  m_jvthisto = (TH2F*)jvtfile->Get(std::string(m_jvtlikelihoodHistName).c_str() );
60  if(!m_jvthisto){
61  ATH_MSG_FATAL( "\n Found JVT file, but JVT histogram missing. Aborting..." );
62  return StatusCode::FAILURE;
63  }
64  m_jvthisto->SetDirectory (nullptr);
65 
67  m_jvfCorrVtxKey = m_jvfCorrKey.key()+"Vec";
69  m_jvtKey = m_jetContainerName + "." + m_jvtKey.key();
70  m_jvtVecKey = m_jvtKey.key()+"Vec";
71  m_rptKey = m_jetContainerName + "." + m_rptKey.key();
72  m_rptVecKey = m_rptKey.key()+"Vec";
73 
74 #ifndef XAOD_STANDALONE
76  // The user has promised that these will be produced by the same alg running JVT.
77  // Tell the scheduler to ignore them to avoid circular dependencies.
80  }
81 #endif
82 
83  ATH_CHECK(m_vertexContainer_key.initialize());
84  ATH_CHECK(m_jvfCorrKey.initialize());
85  ATH_CHECK(m_sumPtTrkKey.initialize());
86  ATH_CHECK(m_jvtKey.initialize());
87  ATH_CHECK(m_rptKey.initialize());
88 
92 
93 
94  return StatusCode::SUCCESS;
95 }
96 
97 //**********************************************************************
98 
100 
101  // Get the vertex to calculate with respect to
102  // Only appropriate if we are using a single vertex interpretation, not if using OriginVertex
103  const xAOD::Vertex* HSvertex = nullptr;
104  if (!m_useOriginVertex)
105  {
106  HSvertex = findHSVertex();
107  if(!HSvertex) return StatusCode::FAILURE;
108  }
109 
110  // Grab vertices for index bookkeeping
112  const xAOD::VertexContainer* vertices = vertexHandle.cptr();
113 
118 
119 
120 
121  for(const xAOD::Jet * jet : jetCont) {
122  // Get origin-vertex-specific information if relevant
123  if (m_useOriginVertex)
124  {
125  HSvertex = jet->getAssociatedObject<xAOD::Vertex>("OriginVertex");
126  if (!HSvertex) // nullptr if the attribute doesn't exist
127  {
128  ATH_MSG_ERROR("OriginVertex was requested, but the jet does not contain an OriginVertex");
129  return StatusCode::FAILURE;
130  }
131  else
132  {
133  ATH_MSG_VERBOSE("JetVertexTaggerTool " << name() << " is using OriginVertex at index: " << HSvertex->index());
134  }
135  }
136 
137  // Calculate RpT and JVFCorr
138  // Default JVFcorr to -1 when no tracks are associated.
139  float jvfcorr = jvfCorrHandle(*jet);
140  std::vector<float> sumpttrk = sumPtTrkHandle(*jet);
141  const float rpt = sumpttrk[HSvertex->index() - (*vertices)[0]->index()]/jet->pt();
142  float jvt = evaluateJvt(rpt, jvfcorr);
143 
144  rptHandle(*jet) = rpt;
145  jvtHandle(*jet) = jvt;
146  ATH_MSG_VERBOSE("JetVertexTaggerTool " << name() << ": JVT=" << jvt << ", RpT=" << rpt << ", JVFCorr=" << jvfcorr);
147  }
148 
149  if (m_useOriginVertex) { // Compute JVT for jets assuming other vertices as origin
150 
154 
155  std::vector<float> jvtVtx;
156  std::vector<float> rptVtx;
157 
158  for(const xAOD::Jet * jet : jetCont) {
159  jvtVtx.clear();
160  rptVtx.clear();
161  std::vector<float> sumpttrk = sumPtTrkHandle(*jet);
162  std::vector<float> jvfcorrVtx = jvfCorrVtxHandle(*jet);
163 
164  // Loop over vertices
165  for(size_t vtxi=0; vtxi<vertices->size(); ++vtxi) {
166 
167  float jvfcorr = jvfcorrVtx.at(vtxi);
168  const float rpt = sumpttrk[vtxi]/jet->pt();
169  float jvt = evaluateJvt(rpt, jvfcorr);
170  jvtVtx.push_back(jvt);
171  rptVtx.push_back(rpt);
172  }
173 
174  jvtVecHandle(*jet) = jvtVtx;
175  rptVecHandle(*jet) = rptVtx;
176  // Done
177 
178  }
179  }
180  return StatusCode::SUCCESS;
181 }
182 //**********************************************************************
183 
184 float JetVertexTaggerTool::evaluateJvt(float rpt, float jvfcorr) const {
185  // Look up JVT value
186  float jvt = -999.;
187  if ( jvfcorr == -1.0 ) {
188  jvt = -0.1;
189  } else {
190  float rpt_inputtojvt = std::min(rpt, (float) 1. );
191  int bin = std::as_const(m_jvthisto)->FindBin(jvfcorr, rpt_inputtojvt);
192  jvt = m_jvthisto->GetBinContent(bin);
193  jvt = m_jvthisto->Interpolate(jvfcorr, rpt_inputtojvt);
194  }
195  return jvt;
196 }
197 
198 //**********************************************************************
199 
203 
204  float jvfcorr = jvfCorrHandle(jet);
205  std::vector<float> sumpttrk = sumPtTrkHandle(jet);
206 
207  // Get the vertex to calculate with respect to
208  const xAOD::Vertex* HSvertex = nullptr;
209  if (!m_useOriginVertex)
210  HSvertex = findHSVertex();
211  else
212  {
213  HSvertex = jet.getAssociatedObject<xAOD::Vertex>("OriginVertex");
214  if (HSvertex) // nullptr if the attribute doesn't exist, nothing to do as behaviour is same as other method, checked below and return -1
215  ATH_MSG_VERBOSE("JetVertexTaggerTool " << name() << " is using OriginVertex at index: " << HSvertex->index());
216  }
217 
218  if(!HSvertex) {
219  ATH_MSG_ERROR("No hard scatter vertex found. Returning JVT=-1");
220  return -1.;
221  }
222 
223  // Grab vertices for index bookkeeping
225  const xAOD::VertexContainer* vertices = vertexHandle.cptr();
226 
227  const float rptnew = sumpttrk[HSvertex->index() - (*vertices)[0]->index()]/jet.pt();
228 
229  return evaluateJvt(rptnew, jvfcorr);
230 }
231 
232 //**********************************************************************
233 
235 {
236  // Get input vertex collection
238  if (!vertexHandle.isValid()){
239  ATH_MSG_ERROR("Invalid VertexContainer datahandle: " << m_vertexContainer_key.key());
240  return nullptr;
241  }
242  const xAOD::VertexContainer* vertices = vertexHandle.cptr();
243  ATH_MSG_DEBUG("Successfully retrieved VertexContainer: " << m_vertexContainer_key.key());
244 
245  if (vertices->empty() ) {
246  ATH_MSG_WARNING("There are no vertices in the container. Exiting");
247  return nullptr;
248  }
249 
250  for ( size_t iVertex = 0; iVertex < vertices->size(); ++iVertex ) {
251  if(vertices->at(iVertex)->vertexType() == xAOD::VxType::PriVtx) {
252 
253  ATH_MSG_VERBOSE("JetVertexTaggerTool " << name() << " Found HS vertex at index: "<< iVertex);
254  return vertices->at(iVertex);
255  }
256  }
257  ATH_MSG_VERBOSE("There is no vertex of type PriVx. Taking default vertex.");
258  return vertices->at(0);
259 }
JetVertexTaggerTool::m_jvfCorrKey
SG::ReadDecorHandleKey< xAOD::JetContainer > m_jvfCorrKey
Definition: JetVertexTaggerTool.h:115
JetVertexTaggerTool::m_jvfCorrVtxKey
SG::ReadDecorHandleKey< xAOD::JetContainer > m_jvfCorrVtxKey
Definition: JetVertexTaggerTool.h:116
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
JetVertexTaggerTool::m_jetContainerName
Gaudi::Property< std::string > m_jetContainerName
Definition: JetVertexTaggerTool.h:109
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
TH2F
Definition: rootspy.cxx:420
JetVertexTaggerTool::m_jvtlikelihoodHistName
Gaudi::Property< std::string > m_jvtlikelihoodHistName
Definition: JetVertexTaggerTool.h:110
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
JetVertexTaggerTool::m_sumPtTrkKey
SG::ReadDecorHandleKey< xAOD::JetContainer > m_sumPtTrkKey
Definition: JetVertexTaggerTool.h:117
AthCommonDataStore< AthCommonMsg< AlgTool > >::renounce
std::enable_if_t< std::is_void_v< std::result_of_t< decltype(&T::renounce)(T)> > &&!std::is_base_of_v< SG::VarHandleKeyArray, T > &&std::is_base_of_v< Gaudi::DataHandle, T >, void > renounce(T &h)
Definition: AthCommonDataStore.h:380
asg
Definition: DataHandleTestTool.h:28
bin
Definition: BinsDiffFromStripMedian.h:43
JetVertexTaggerTool::m_useOriginVertex
Gaudi::Property< bool > m_useOriginVertex
Definition: JetVertexTaggerTool.h:123
JetVertexTaggerTool::m_rptKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_rptKey
Definition: JetVertexTaggerTool.h:120
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
JetVertexTaggerTool::m_rptVecKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_rptVecKey
Definition: JetVertexTaggerTool.h:121
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:269
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
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
JetVertexTaggerTool::~JetVertexTaggerTool
virtual ~JetVertexTaggerTool()
Definition: JetVertexTaggerTool.cxx:31
JetVertexTaggerTool::m_jvtVecKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_jvtVecKey
Definition: JetVertexTaggerTool.h:119
JetVertexTaggerTool::m_jvtKey
SG::WriteDecorHandleKey< xAOD::JetContainer > m_jvtKey
Definition: JetVertexTaggerTool.h:118
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
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:99
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
JetVertexTaggerTool::m_jvtfileName
Gaudi::Property< std::string > m_jvtfileName
Definition: JetVertexTaggerTool.h:111
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
WriteDecorHandle.h
Handle class for adding a decoration to an object.
TH2F::GetBinContent
double GetBinContent(int) const
Definition: rootspy.cxx:425
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
JetVertexTaggerTool::m_fn
TString m_fn
Definition: JetVertexTaggerTool.h:126
JetVertexTaggerTool::evaluateJvt
float evaluateJvt(float rpt, float jvfcorr) const
Definition: JetVertexTaggerTool.cxx:184
min
#define min(a, b)
Definition: cfImp.cxx:40
xAOD::JetFourMom_t
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition: JetTypes.h:17
PathResolver.h
JetVertexTaggerTool::m_jvthisto
TH2F * m_jvthisto
Definition: JetVertexTaggerTool.h:127
JetVertexTaggerTool::decorate
virtual StatusCode decorate(const xAOD::JetContainer &jetCont) const override
Decorate a jet collection without otherwise modifying it.
Definition: JetVertexTaggerTool.cxx:99
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
JetVertexTaggerTool::m_suppressInputDeps
Gaudi::Property< bool > m_suppressInputDeps
Definition: JetVertexTaggerTool.h:112
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
JetVertexTaggerTool.h
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ReadDecorHandle.h
Handle class for reading a decoration on an object.
JetVertexTaggerTool::findHSVertex
const xAOD::Vertex * findHSVertex() const
Definition: JetVertexTaggerTool.cxx:234
JetVertexTaggerTool::m_vertexContainer_key
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexContainer_key
Definition: JetVertexTaggerTool.h:114
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
JetVertexTaggerTool::updateJvt
float updateJvt(const xAOD::Jet &jet) const override
Calculate the updated JVT.
Definition: JetVertexTaggerTool.cxx:200
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
JetVertexTaggerTool::initialize
StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: JetVertexTaggerTool.cxx:37
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
JetVertexTaggerTool::JetVertexTaggerTool
JetVertexTaggerTool(const std::string &name)
Definition: JetVertexTaggerTool.cxx:23