ATLAS Offline Software
BTagScaleFactorCalculator.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 // $Id: BTagScaleFactorCalculator.cxx 809570 2017-08-18 13:15:17Z iconnell $
9 #include "TopEvent/EventTools.h"
10 
11 #include "xAODJet/JetContainer.h"
12 #include <cmath>
13 #include <algorithm>
14 #include <functional>
15 
16 // For debug function
22 
23 namespace top {
25  asg::AsgTool(name),
26  m_config(nullptr),
27  m_nominal(CP::SystematicSet()) {
28  declareProperty("config", m_config);
29  }
30 
32  ATH_MSG_INFO(" top::BTagScaleFactorCalculator initialize");
33 
34  // for calo jets
35  for (const std::string& WP : m_config->bTagWP_calib()) {
36  m_btagSelTools[WP] = "BTaggingSelectionTool_" + WP + "_" + m_config->sgKeyJets();
37  top::check(m_btagSelTools[WP].retrieve(), "Failed to retrieve b-tagging Selection tool");
38  m_btagEffTools[WP] = "BTaggingEfficiencyTool_" + WP + "_" + m_config->sgKeyJets();
39  top::check(m_btagEffTools[WP].retrieve(), "Failed to retrieve b-tagging Efficiency tool");
40  m_systs[WP] = m_btagEffTools[WP]->affectingSystematics();
41  std::set<std::string> base_names = m_systs[WP].getBaseNames();
42  m_config->setBTaggingSFSysts(WP, base_names);
43  }
44  if (m_config->useTrackJets()) {
45  // for track jets
46  for (const std::string& WP : m_config->bTagWP_calib_trkJet()) {
47  m_trkjet_btagSelTools[WP] = "BTaggingSelectionTool_" + WP + "_" + m_config->sgKeyTrackJets();
48  top::check(m_trkjet_btagSelTools[WP].retrieve(), "Failed to retrieve b-tagging Selection tool");
49  m_trkjet_btagEffTools[WP] = "BTaggingEfficiencyTool_" + WP + "_" + m_config->sgKeyTrackJets();
50  top::check(m_trkjet_btagEffTools[WP].retrieve(), "Failed to retrieve b-tagging Efficiency tool");
51  m_trkjet_systs[WP] = m_trkjet_btagEffTools[WP]->affectingSystematics();
52  std::set<std::string> base_names = m_trkjet_systs[WP].getBaseNames();
53  m_config->setBTaggingSFSysts(WP, base_names, true);
54  }
55  }
56 
57  return StatusCode::SUCCESS;
58  }
59 
61  top::check(apply(m_config->systSgKeyMapJets(false)),
62  "Failed to apply btagging SFs");
63  if (m_config->useTrackJets()) top::check(apply(m_config->systSgKeyMapTrackJets(), true),
64  "Failed to apply track jet btagging SFs");
65 
66  return StatusCode::SUCCESS;
67  }
68 
69  StatusCode BTagScaleFactorCalculator::apply(const std::shared_ptr<std::unordered_map<std::size_t,
70  std::string> >& jet_syst_collections,
71  bool use_trackjets) {
74  for (auto currentSystematic : *jet_syst_collections) {
75  const xAOD::JetContainer* jets(nullptr);
76  top::check(evtStore()->retrieve(jets, currentSystematic.second), "failed to retrieve jets");
77 
80  for (auto jetPtr : *jets) {
81  bool passSelection(false);
82  if (jetPtr->isAvailable<char>("passPreORSelection")) {
83  if (jetPtr->auxdataConst<char>("passPreORSelection") == 1) {
84  passSelection = true;
85  }
86  }
87  if (jetPtr->isAvailable<char>("passPreORSelectionLoose")) {
88  if (jetPtr->auxdataConst<char>("passPreORSelectionLoose") == 1) {
89  passSelection = true;
90  }
91  }
92 
93  if (passSelection) {
94  // now loop over all available WPs
95 
96  for (const std::string& tagWP : (use_trackjets ? m_config->bTagWP_calib_trkJet() : m_config->bTagWP_calib())) {
97  ToolHandle<IBTaggingEfficiencyTool>& btageff =
98  use_trackjets ? m_trkjet_btagEffTools[tagWP] : m_btagEffTools[tagWP];
99  ToolHandle<IBTaggingSelectionTool>& btagsel =
100  use_trackjets ? m_trkjet_btagSelTools[tagWP] : m_btagSelTools[tagWP];
101  CP::SystematicSet& sysSet = use_trackjets ? m_trkjet_systs[tagWP] : m_systs[tagWP];
102 
103  // need now the DSID to find out which shower was used in the sample
104  unsigned int MapIndex = m_config->getMapIndex();
105 
106  btageff->setMapIndex("Light", MapIndex);
107  btageff->setMapIndex("C", MapIndex);
108  btageff->setMapIndex("B", MapIndex);
109  btageff->setMapIndex("T", MapIndex);
110 
111  // Check if this jet collection systematic matches with one removed from the EV decomposition
112  // (TopCorrectionsTools)
113  std::string bTagSystName = top::bTagNamedSystCheck(m_config, currentSystematic.second, tagWP, use_trackjets, false);
114  // If this string is not empty, we need to search and find the appropriate systematic set to apply
115  if (bTagSystName != "") {
116  CP::SystematicSet bTagSyst;
117  bTagSyst.insert(sysSet.getSystematicByBaseName(bTagSystName));
118  top::check(btageff->applySystematicVariation(bTagSyst),
119  "Failed to set new b-tagging SF to a shifted systematic set : " + bTagSystName);
120  } else {
121  top::check(btageff->applySystematicVariation(m_nominal),
122  "Failed to set new b-tagging SF to nominal");
123  }
124 
125  float btag_SF(1.0);
126  bool isTagged = false;//unused in case of Continuous
127  if (std::fabs(jetPtr->eta()) <= 2.5) {
128  if (tagWP.find("Continuous") == std::string::npos) {
129  isTagged = static_cast<bool>(btagsel->accept(*jetPtr));
130  if (isTagged) top::check(btageff->getScaleFactor(*jetPtr, btag_SF),
131  "Failed to get nominal b-tagging SF");
132  else top::check(btageff->getInefficiencyScaleFactor(*jetPtr, btag_SF),
133  "Failed to get nominal b-tagging SF");
134  } else {
135  top::check(btageff->getScaleFactor(*jetPtr, btag_SF),
136  "Failed to get nominal Continuous b-tagging SF");
137  }
138  }
139  jetPtr->auxdecor<float>("btag_SF_" + tagWP + "_nom") = btag_SF;
140 
142  if (currentSystematic.first == m_config->nominalHashValue()) {
143  for (const auto& variation : sysSet) {
144  btag_SF = 1.;
145  CP::SystematicSet syst_set;
146  syst_set.insert(variation);
147  top::check(btageff->applySystematicVariation(syst_set),
148  "Failed to set new b-tagging systematic variation " + syst_set.name());
149  if (std::fabs(jetPtr->eta()) <= 2.5) {
150  if (tagWP.find("Continuous") == std::string::npos) {
151  if (isTagged) top::check(btageff->getScaleFactor(*jetPtr, btag_SF),
152  "Failed to get b-tagging SF for variation " + syst_set.name());
153  else top::check(btageff->getInefficiencyScaleFactor(*jetPtr, btag_SF),
154  "Failed to get b-tagging SF for variation " + syst_set.name());
155  } else {
156  top::check(btageff->getScaleFactor(*jetPtr, btag_SF),
157  "Failed to get Continuous b-tagging SF for variation " + syst_set.name());
158  }
159  }
160  jetPtr->auxdecor<float>("btag_SF_" + tagWP + "_" + variation.name()) = btag_SF;
161  } // loop through b-tagging systematic variations
162  } // Calibration systematic is nominal, so calculate SF systematics
163  }
164  }
165  }
166  }
167 
168 
169  return StatusCode::SUCCESS;
170  }
171 
173  ATH_MSG_INFO("BTagScaleFactorCalculator::debug function");
174  // Use after package is initialised otherwise vectors will be empty
175  for (const std::string& tagWP : m_config->bTagWP()) {
176  ATH_MSG_INFO("Tagger working point : " << tagWP);
177  ToolHandle<IBTaggingEfficiencyTool>& btageff = m_btagEffTools[tagWP];
178  // Retrieve tool
179  top::check(btageff.retrieve(), "Failed to retrieve tool");
180  // Retrieve list of systematics included
181  CP::SystematicSet systs = btageff->affectingSystematics();
182  CP::SystematicSet recsysts = btageff->recommendedSystematics();
183 
184  ATH_MSG_INFO("-----------------------------------------------------------------------");
185 
186  const std::map<CP::SystematicVariation,
187  std::vector<std::string> > allowed_variations = btageff->listSystematics();
188 
189  ATH_MSG_INFO("Allowed systematics variations for tool " << btageff->name());
190  for (auto var : allowed_variations) {
191  std::string flvs = "";
192  for (auto flv : var.second) flvs += flv;
193  ATH_MSG_INFO(" (" << flvs << ") - " << var.first);
194  }
195 
196  // Now use the new functions added into xAODBTaggingEfficiency-00-00-39
197  // std::map<std::string, std::vector<std::string> >
198  ATH_MSG_INFO("-----------------------------------------------------------------------");
199  ATH_MSG_INFO("List of b-tagging scale factor systematics");
200  std::map<std::string,
201  std::vector<std::string> > listOfScaleFactorSystematics = btageff->listScaleFactorSystematics(false);
202  for (auto var : listOfScaleFactorSystematics) {
203  ATH_MSG_INFO("Jet flavour : " << var.first);
204  std::vector<std::string> systs = var.second;
205  std::sort(systs.begin(), systs.end());
206  for (const auto& sys : systs) {
207  ATH_MSG_INFO(" (" << var.first << ") - " << sys);
208  }
209  ATH_MSG_INFO(" ");
210  }
211 
212  ATH_MSG_INFO("List of (named) b-tagging scale factor systematics");
213  listOfScaleFactorSystematics = btageff->listScaleFactorSystematics(true);
214  for (auto var : listOfScaleFactorSystematics) {
215  ATH_MSG_INFO("Jet flavour : " << var.first);
216  std::vector<std::string> systs = var.second;
217  std::sort(systs.begin(), systs.end());
218  for (const auto& sys : systs) {
219  ATH_MSG_INFO(" (" << var.first << ") - " << sys);
220  }
221  ATH_MSG_INFO(" ");
222  }
223  ATH_MSG_INFO("-----------------------------------------------------------------------");
224  }
225  return StatusCode::SUCCESS;
226  }
227 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
top::BTagScaleFactorCalculator::m_systs
std::unordered_map< std::string, CP::SystematicSet > m_systs
Definition: BTagScaleFactorCalculator.h:60
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
top::BTagScaleFactorCalculator::m_trkjet_btagSelTools
std::unordered_map< std::string, ToolHandle< IBTaggingSelectionTool > > m_trkjet_btagSelTools
Definition: BTagScaleFactorCalculator.h:68
top::BTagScaleFactorCalculator::apply
StatusCode apply(const std::shared_ptr< std::unordered_map< std::size_t, std::string > > &jet_syst_collections, bool use_trackjets=false)
Definition: BTagScaleFactorCalculator.cxx:69
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
IsoCloseByCorrectionTest.WP
WP
Definition: IsoCloseByCorrectionTest.py:56
SystematicSet.h
asg
Definition: DataHandleTestTool.h:28
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
CP::SystematicSet::name
std::string name() const
returns: the systematics joined into a single string.
Definition: SystematicSet.cxx:278
CP::SystematicVariation
Definition: SystematicVariation.h:47
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
EventTools.h
A few functions for doing operations on particles / events. Currently holds code for dR,...
top::BTagScaleFactorCalculator::m_trkjet_btagEffTools
std::unordered_map< std::string, ToolHandle< IBTaggingEfficiencyTool > > m_trkjet_btagEffTools
Definition: BTagScaleFactorCalculator.h:65
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
CP::SystematicSet::getSystematicByBaseName
SystematicVariation getSystematicByBaseName(const std::string &basename) const
description: get the first systematic matching basename
Definition: SystematicSet.cxx:171
CalibrationDataInterfaceROOT.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
top::check
void check(bool thingToCheck, const std::string &usefulFailureMessage)
Print an error message and terminate if thingToCheck is false.
Definition: EventTools.cxx:15
top::bTagNamedSystCheck
std::string bTagNamedSystCheck(std::shared_ptr< top::TopConfig > config, const std::string &systCollection, const std::string &tagWP, const bool isTrackJet, const bool verbose=false)
Definition: TopCorrectionsTools.cxx:13
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
StatusCode.h
PathResolver.h
CP::SystematicSet::insert
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
Definition: SystematicSet.cxx:88
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
top::BTagScaleFactorCalculator::debug
StatusCode debug()
Definition: BTagScaleFactorCalculator.cxx:172
top::BTagScaleFactorCalculator::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: BTagScaleFactorCalculator.cxx:31
TopConfig.h
top::BTagScaleFactorCalculator::m_trkjet_systs
std::unordered_map< std::string, CP::SystematicSet > m_trkjet_systs
Definition: BTagScaleFactorCalculator.h:61
TopCorrectionsTools.h
JetContainer.h
top::BTagScaleFactorCalculator::BTagScaleFactorCalculator
BTagScaleFactorCalculator(const std::string &name)
Definition: BTagScaleFactorCalculator.cxx:24
top::BTagScaleFactorCalculator::execute
StatusCode execute()
Definition: BTagScaleFactorCalculator.cxx:60
top::BTagScaleFactorCalculator::m_btagEffTools
std::unordered_map< std::string, ToolHandle< IBTaggingEfficiencyTool > > m_btagEffTools
B-tagging efficiency tools.
Definition: BTagScaleFactorCalculator.h:64
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
top::BTagScaleFactorCalculator::m_btagSelTools
std::unordered_map< std::string, ToolHandle< IBTaggingSelectionTool > > m_btagSelTools
B-tagging selection tools.
Definition: BTagScaleFactorCalculator.h:67
top::BTagScaleFactorCalculator::m_config
std::shared_ptr< top::TopConfig > m_config
Definition: BTagScaleFactorCalculator.h:57
top::BTagScaleFactorCalculator::m_nominal
CP::SystematicSet m_nominal
Definition: BTagScaleFactorCalculator.h:59
SystematicVariation.h
BTagScaleFactorCalculator.h