ATLAS Offline Software
BTaggingSelectionJsonTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
5 #include <fstream>
6 
8  : asg::AsgTool( name )
9 {
10  m_initialised = false;
11  declareProperty( "MaxEta", m_maxEta = 2.5 );
12  declareProperty( "MinPt", m_minPt = -1 /*MeV*/);
13  declareProperty( "TaggerName", m_taggerName="", "tagging algorithm name");
14  declareProperty( "JetAuthor", m_jetAuthor="", "jet collection");
15  declareProperty( "OperatingPoint", m_OP="", "operating point");
16  declareProperty( "JsonConfigFile", m_json_config_path="", "Path to JSON config file");
17 }
18 
20  m_initialised = true;
21 
22  std::ifstream jsonFile(m_json_config_path);
23  if (!jsonFile.is_open()) {
24  ATH_MSG_ERROR( "JSON file " + m_json_config_path + " do not exist. Please put the correct path of the file." );
25  return StatusCode::FAILURE;
26  }
28  jsonFile.close();
29 
30  if (m_taggerName=="" || !m_json_config.contains(m_taggerName)){
31  ATH_MSG_ERROR( " Tagger " + m_taggerName + " not found in JSON file: " + m_json_config_path );
32  return StatusCode::FAILURE;
33  }
34 
36  ATH_MSG_ERROR( "Tagger: " +m_taggerName+ " and Jet Collection: " +m_jetAuthor+ " not found in JSON file: " +m_json_config_path );
37  return StatusCode::FAILURE;
38  }
39 
41  ATH_MSG_ERROR( "OP " +m_OP+ " not available for " +m_taggerName+ " tagger.");
42  return StatusCode::FAILURE;
43  }
44 
45  m_target = m_json_config[m_taggerName][m_jetAuthor]["meta"]["TaggingTarget"];
46 
47  // pre-load fraction values
48  m_fractionAccessors.clear();
49  for (const auto& outclass : m_json_config[m_taggerName][m_jetAuthor]["meta"]["categories"]) {
50  std::string outclassStr = std::string(outclass);
51  float fraction = m_json_config[m_taggerName][m_jetAuthor]["meta"]["fraction_" + outclassStr].get<float>();
53  bool isTarget = (outclassStr == m_target);
54  m_fractionAccessors.emplace_back(fraction, accessor, isTarget);
55  }
56 
57  // pre-load cut values
58  m_OPCutValues.clear();
59  if ( m_OP == "Continuous" ) {
60  for (const auto& ContinuousOP : m_json_config[m_taggerName][m_jetAuthor]["meta"]["OperatingPoints"]) {
61  float cutvalue = m_json_config[m_taggerName][m_jetAuthor][std::string(ContinuousOP)]["meta"]["cutvalue"];
62  m_OPCutValues.emplace_back(ContinuousOP, cutvalue);
63  }
64  } else {
65  float cutvalue = m_json_config[m_taggerName][m_jetAuthor][m_OP]["meta"]["cutvalue"];
66  m_OPCutValues.emplace_back(m_OP, cutvalue);
67  }
68 
69  return StatusCode::SUCCESS;
70 }
71 
73 
74  float numerator = 0.;
75  float denominator = 0.;
76  for ( const auto& frac : m_fractionAccessors ) {
77  float p_output = frac.accessor( jet );
78  if ( frac.isTarget ) {
79  numerator += frac.fraction * p_output;
80  } else {
81  denominator += frac.fraction * p_output;
82  }
83  }
84 
85  double tagger_discriminant = log(numerator / denominator);
86 
87  return tagger_discriminant;
88 }
89 
92  // Cheatsheet:
93  // For fix cut WP, return 0 for not tagged, 1 for tagged
94  // For Continuous with n WPs (from highest to lowest) A1, A2, A3, ..., An
95  // return 0 if not in b-tagging acceptance
96  // return 1 if between 100% and A1 (untagged)
97  // return 2 if between A1 and A2 (tagged at the A1 WP)
98  // return 3 if between A2 and A3 (tagged at the A2 WP)
99  // ...
100  // return n if between An-1 and An (tagged at the An-1 WP)
101  // return n+1 if between An and 0% (tagged at the An WP)
103 
104  if ( !m_initialised ) {
105  throw std::runtime_error("BTaggingSelectionJsonTool has not been initialised.");
106  }
107 
108  int index = 0;
109 
110  if ( std::abs(jet.eta()) > m_maxEta || jet.pt() < m_minPt ) {
111  return index;
112  }
113 
114  double tagger_discriminant = getTaggerDiscriminant(jet);
115  for (const auto& [opName, cutvalue] : m_OPCutValues) {
116  if (tagger_discriminant > cutvalue) {
117  index += 1;
118  }
119  }
120  return index;
121 }
122 
BTaggingSelectionJsonTool::m_maxEta
double m_maxEta
Definition: BTaggingSelectionJsonTool.h:26
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
BTaggingSelectionJsonTool.h
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:983
BTaggingSelectionJsonTool::m_initialised
bool m_initialised
Definition: BTaggingSelectionJsonTool.h:24
asg
Definition: DataHandleTestTool.h:28
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
BTaggingSelectionJsonTool::m_OP
std::string m_OP
Definition: BTaggingSelectionJsonTool.h:30
BTaggingSelectionJsonTool::m_jetAuthor
std::string m_jetAuthor
Definition: BTaggingSelectionJsonTool.h:31
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
BTaggingSelectionJsonTool::m_minPt
double m_minPt
Definition: BTaggingSelectionJsonTool.h:27
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
checkxAOD.frac
frac
Definition: Tools/PyUtils/bin/checkxAOD.py:256
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: hcg.cxx:111
BTaggingSelectionJsonTool::m_json_config_path
std::string m_json_config_path
Definition: BTaggingSelectionJsonTool.h:32
ReadTripsProbsFromCool.denominator
denominator
Definition: ReadTripsProbsFromCool.py:96
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
BTaggingSelectionJsonTool::initialize
StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: BTaggingSelectionJsonTool.cxx:19
BTaggingSelectionJsonTool::m_json_config
json m_json_config
Definition: BTaggingSelectionJsonTool.h:35
xAOD::JetAttributeAccessor::accessor
const AccessorWrapper< T > * accessor(xAOD::JetAttribute::AttributeID id)
Returns an attribute accessor corresponding to an AttributeID.
Definition: JetAccessorMap.h:26
BTaggingSelectionJsonTool::m_target
std::string m_target
Definition: BTaggingSelectionJsonTool.h:33
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
BTaggingSelectionJsonTool::accept
virtual int accept(const xAOD::Jet &jet) const override
Definition: BTaggingSelectionJsonTool.cxx:90
DeMoScan.index
string index
Definition: DeMoScan.py:362
BTaggingSelectionJsonTool::getTaggerDiscriminant
virtual double getTaggerDiscriminant(const xAOD::Jet &jet) const override
Definition: BTaggingSelectionJsonTool.cxx:72
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
BTaggingSelectionJsonTool::BTaggingSelectionJsonTool
BTaggingSelectionJsonTool(const std::string &name)
Definition: BTaggingSelectionJsonTool.cxx:7
python.handimod.jsonFile
string jsonFile
Definition: handimod.py:652
BTaggingSelectionJsonTool::m_taggerName
std::string m_taggerName
Definition: BTaggingSelectionJsonTool.h:29
BTaggingSelectionJsonTool::m_OPCutValues
std::vector< std::pair< std::string, float > > m_OPCutValues
Definition: BTaggingSelectionJsonTool.h:46
BTaggingSelectionJsonTool::m_fractionAccessors
std::vector< FractionAccessor > m_fractionAccessors
Definition: BTaggingSelectionJsonTool.h:45