ATLAS Offline Software
MV2Tag.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 #include "JetTagTools/MV2Tag.h"
6 
8 
9 #include <fstream>
10 #include <sstream>
11 #include <algorithm>
12 #include <iostream>
13 #include <limits>
14 #include <cmath>
15 #include <utility>
16 #include <vector>
17 #include <map>
18 #include <list>
19 #include <math.h> /* hypot */
20 
21 #include "AthenaKernel/Units.h"
22 
23 using Athena::Units::GeV;
24 
25 namespace Analysis {
26 
34  MV2Tag::MV2Tag(const std::string& name, const std::string& n, const IInterface* p):
35  base_class(name, n,p),
36  m_runModus("analysis") {
37 
38  // force MV2 to always use a calibration derived from MV2CalibAlias jet collection
39  declareProperty("forceMV2CalibrationAlias", m_forceMV2CalibrationAlias = true);
40  declareProperty("MV2CalibAlias", m_MV2CalibAlias = "AntiKt4EMTopo");
41 
42  // global configuration:
43  declareProperty("Runmodus", m_runModus);
44 
45  declareProperty("xAODBaseName", m_xAODBaseName);//"MV2c20" or etc.
46 
47  // which calibration folder to use
48  declareProperty("taggerNameBase", m_taggerNameBase = "MV2");
49 
50  declareProperty("defaultvals", m_defaultvals );
51  declareProperty("MVTMvariableNames", m_MVTM_name_translations );
52 
53 
54  }
55 
56 
58 
59  m_disableAlgo=false;
60  m_warnCounter=0;
61 
62  m_varStrName = "variables";
63 
64  // prepare readKey for calibration data:
65  ATH_CHECK(m_readKey.initialize());
66 
67  for (const auto& p : m_MVTM_name_translations) {
68  m_MVTM_name_backtrans[p.second] = p.first;
69  }
70 
71  return StatusCode::SUCCESS;
72  }
73 
74 
76  const std::map<std::string, double> &inputs,
77  const std::string& assigned_jet_author) const
78  {
79  /*
80  * #index for this function
81  * #1: Preparation of MVA instance using MVAUtils BDT
82  * #2: set input variables from MultivariateTagManager inputs map
83  * #3: Calcuation of MVA output variable(s)
84  * #4: Fill MVA output variable(s) into xAOD
85  */
86 
87  // #0: Check if disabled or not
88  if ( m_disableAlgo ) {
89  if (m_warnCounter<20) {
90  ATH_MSG_WARNING("#BTAG# .... algorithm was disabled due to mis-configuration problems: no output will be written ... PLEASE WATCH OUT" );
91  m_warnCounter++;
92  }
93  }
94 
95  // #1: Preparation of MVA instance using egammaBDT
96  /* jet author: */
97  std::string author(assigned_jet_author);
98  ATH_MSG_DEBUG("#BTAG# Jet author for MV2: " << author);
99 
102  }
103 
104  //Retrieval of Calibration Condition Data objects
106 
107  std::string alias = readCdo->getChannelAlias(author);
108 
109  //Retrieve BDT from cond object
110  MVAUtils::BDT *bdt(nullptr);
111  ATH_MSG_DEBUG("#BTAG# Getting MVAUtils::BDT for "<<m_taggerNameBase);
112  bdt = readCdo->retrieveBdt(m_taggerNameBase,author);
113  if (!bdt) {
114  ATH_MSG_WARNING("#BTAG# No BDT for " << m_taggerNameBase<<" exists in the condition object.. Disabling algorithm.");
115  m_disableAlgo=true;
116  return;
117  }
118 
119  //Retrieve input variables of BDT from cond object
120  std::vector<std::string> inputVars = readCdo->retrieveInputVars(m_taggerNameBase,author, m_taggerNameBase+"Calib/"+m_varStrName);
121  std::vector<float> vars = CreateVariables (inputs, inputVars);
122  ATH_MSG_DEBUG("#BTAG# nConfigVar"<<vars.size());
123 
124  if ( inputVars.size()!=vars.size() ) {
125  ATH_MSG_WARNING("#BTAG# Number of expected variables for MVA: "<< vars.size() << " does not match the number of variables found in the calibration file: " << inputVars.size() << " ... the algorithm will be 'disabled' "<<alias<<" "<<author);
126  m_disableAlgo=true;
127  return;
128  }
129 
130 
131  // #3: Calculation of MVA output variable(s)
132  /* compute MV2: */
133  double mv2 = -10.; double mv2m_pb=-10., mv2m_pu=-10., mv2m_pc=-10.;
134 
135  if (m_taggerNameBase.find("MV2c")!=std::string::npos) mv2= GetClassResponse(vars, bdt);//this gives back double
136  else { //if it is MV2m
137  std::vector<float> outputs= GetMulticlassResponse(vars, bdt);//this gives back float
138  //vector size is checked in the function above
139  mv2m_pb=outputs[0]; mv2m_pu=outputs[1]; mv2m_pc=outputs[2] ;
140  }
141 
142  if (m_taggerNameBase.find("MV2c")!=std::string::npos) ATH_MSG_DEBUG("#BTAG# MV2 weight: " << mv2<<", "<<alias<<", "<<author);
143  else ATH_MSG_DEBUG("#BTAG# MV2 pb, pu, pc= " << mv2m_pb<<"\t"<<mv2m_pu<<"\t"<<mv2m_pc<<", "<<alias<<", "<<author);
144 
145  // #4: Fill MVA output variable(s) into xAOD
147  if(m_runModus=="analysis") {
148 
149  if (m_taggerNameBase.find("MV2c")!=std::string::npos) {
150  //MV2cXX
151  BTag->setVariable<float>(m_xAODBaseName, "discriminant", mv2);
152  }else {
153  //MV2m
154  BTag->setVariable<float>(m_xAODBaseName, "pb", mv2m_pb);
155  BTag->setVariable<float>(m_xAODBaseName, "pu", mv2m_pu);
156  BTag->setVariable<float>(m_xAODBaseName, "pc", mv2m_pc);
157  }
158  }
159 
160  }//end assign probability
161 
162  float MV2Tag::d0sgn_wrtJet(const TLorentzVector& jet, const TLorentzVector& trk, float d0sig) const {
163  const double dPhi = jet.DeltaPhi(trk);
164  const float d0_sign = sin(dPhi) * d0sig;
165 
166  if (std::fabs(d0_sign) < 1e-4)
167  return 1.0;
168 
169  const float res = d0_sign / std::fabs(d0_sign);
170  return res;
171  }
172 
173  float MV2Tag::z0sgn_wrtJet(float trackTheta, float trackZ0, float jetEta) const {
174  const float trackEta = -std::log(std::tan(trackTheta/2.));
175  const float zs = (jetEta - trackEta)*trackZ0;
176  return (zs>=0. ? 1. : -1.);
177  }
178 
179 
180  std::vector<float>
181  MV2Tag::CreateVariables (const std::map<std::string, double> &inputs,
182  const std::vector<std::string>& inputVars) const
183  {
184  std::vector<float> vals;
185  vals.reserve (inputVars.size());
186 
187  for (const std::string& varName : inputVars) {
188  std::string inputName = varName;
189  auto it = m_MVTM_name_backtrans.find (varName);
190  if (it != m_MVTM_name_backtrans.end()) inputName = it->second;
191  auto it2 = inputs.find (inputName);
192  if (it2 == inputs.end()) {
193  }
194  else {
195  double val = it2->second;
196  if (std::isnan (val)) {
197  val = m_defaultvals.at (varName);
198  }
199  vals.push_back (val);
200  }
201  }
202  return vals;
203  }
204 
205 
206 
207 }//end namespace Analysis
Analysis::MV2Tag::m_taggerNameBase
std::string m_taggerNameBase
Definition: MV2Tag.h:57
Analysis::MV2Tag::initialize
virtual StatusCode initialize() override
Definition: MV2Tag.cxx:57
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
BTagging.h
Analysis::MV2Tag::GetClassResponse
double GetClassResponse(const std::vector< float > &vars, const MVAUtils::BDT *bdt) const
Definition: MV2Tag.h:97
Analysis::MV2Tag::CreateVariables
std::vector< float > CreateVariables(const std::map< std::string, double > &inputs, const std::vector< std::string > &inputVars) const
Definition: MV2Tag.cxx:181
Analysis::MV2Tag::GetMulticlassResponse
std::vector< float > GetMulticlassResponse(const std::vector< float > &vars, const MVAUtils::BDT *bdt) const
Definition: MV2Tag.h:91
skel.it
it
Definition: skel.GENtoEVGEN.py:423
Analysis::MV2Tag::m_MVTM_name_backtrans
std::map< std::string, std::string > m_MVTM_name_backtrans
Map from names in calibration file to names in tool input.
Definition: MV2Tag.h:70
MVAUtils::BDT
Simplified Boosted Regression Tree, support TMVA, lgbm, and xgboost.
Definition: BDT.h:34
Analysis::MV2Tag::m_warnCounter
std::atomic< int > m_warnCounter
Definition: MV2Tag.h:76
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
Analysis::MV2Tag::assignProbability
virtual void assignProbability(xAOD::BTagging *BTag, const std::map< std::string, double > &inputs, const std::string &jetauthor) const override
Definition: MV2Tag.cxx:75
Analysis::MV2Tag::d0sgn_wrtJet
float d0sgn_wrtJet(const TLorentzVector &jet, const TLorentzVector &trk, float d0sig) const
Definition: MV2Tag.cxx:162
Analysis::MV2Tag::m_defaultvals
std::map< std::string, double > m_defaultvals
Definition: MV2Tag.h:66
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
Analysis::MV2Tag::m_varStrName
std::string m_varStrName
Definition: MV2Tag.h:58
beamspotman.n
n
Definition: beamspotman.py:731
PixelAthClusterMonAlgCfg.varName
string varName
end cluster ToT and charge
Definition: PixelAthClusterMonAlgCfg.py:117
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
MV2Tag.h
TauGNNUtils::Variables::Track::dPhi
bool dPhi(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:530
COOLRates.alias
alias
Definition: COOLRates.py:1172
Analysis::MV2Tag::m_xAODBaseName
std::string m_xAODBaseName
Definition: MV2Tag.h:64
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
drawFromPickle.tan
tan
Definition: drawFromPickle.py:36
xAOD::BTagging_v1
Definition: BTagging_v1.h:39
python.CreateTierZeroArgdict.outputs
outputs
Definition: CreateTierZeroArgdict.py:189
Analysis::MV2Tag::z0sgn_wrtJet
float z0sgn_wrtJet(float trackTheta, float trackZ0, float jetEta) const
Definition: MV2Tag.cxx:173
Analysis::MV2Tag::MV2Tag
MV2Tag(const std::string &name, const std::string &n, const IInterface *)
Definition: MV2Tag.cxx:34
Analysis
The namespace of all packages in PhysicsAnalysis/JetTagging.
Definition: BTaggingCnvAlg.h:20
Analysis::MV2Tag::m_readKey
SG::ReadCondHandleKey< JetTagCalibCondData > m_readKey
Key of calibration data:
Definition: MV2Tag.h:61
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
Analysis::MV2Tag::m_disableAlgo
std::atomic< bool > m_disableAlgo
Definition: MV2Tag.h:75
Analysis::MV2Tag::m_MV2CalibAlias
std::string m_MV2CalibAlias
Definition: MV2Tag.h:63
Units.h
Wrapper to avoid constant divisions when using units.
Analysis::MV2Tag::m_runModus
std::string m_runModus
This switch is needed to indicate what to do.
Definition: MV2Tag.h:81
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
CaloCondBlobAlgs_fillNoiseFromASCII.author
string author
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:26
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
xAODType::BTag
@ BTag
The object is a b-tagging object.
Definition: ObjectType.h:60
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
Analysis::MV2Tag::m_forceMV2CalibrationAlias
bool m_forceMV2CalibrationAlias
Definition: MV2Tag.h:62
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
Analysis::MV2Tag::m_MVTM_name_translations
std::map< std::string, std::string > m_MVTM_name_translations
Map from names in tool input to names in calibration file.
Definition: MV2Tag.h:68
TauGNNUtils::Variables::Track::trackEta
bool trackEta(const xAOD::TauJet &, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:475
PlotCalibFromCool.vals
vals
Definition: PlotCalibFromCool.py:474