TopFlavorTaggingCPTools.cxx 26.5 KB
Newer Older
1
/*
Tomas Dado's avatar
Tomas Dado committed
2
   Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
Tomas Dado's avatar
Tomas Dado committed
3
 */
4
5
6
7
8

#include "TopCPTools/TopFlavorTaggingCPTools.h"

#include <map>
#include <string>
9
10
#include <algorithm>
#include <iterator>
11
12
13

// Top includes
#include "TopConfiguration/TopConfig.h"
14
#include "TopConfiguration/Tokenize.h"
15
16
17
18
19
20
21
22
23
#include "TopEvent/EventTools.h"

// PathResolver include(s):
#include "PathResolver/PathResolver.h"

#include "xAODBTaggingEfficiency/BTaggingEfficiencyTool.h"
#include "xAODBTaggingEfficiency/BTaggingSelectionTool.h"

namespace top {
Tomas Dado's avatar
Tomas Dado committed
24
  FlavorTaggingCPTools::FlavorTaggingCPTools(const std::string& name) :
25
    asg::AsgTool(name) {
Tomas Dado's avatar
Tomas Dado committed
26
    declareProperty("config", m_config);
27
28
  }

Tomas Dado's avatar
Tomas Dado committed
29
30
  StatusCode FlavorTaggingCPTools::initialize() {
    ATH_MSG_INFO("top::FlavorTaggingCPTools initialize...");
31

Tomas Dado's avatar
Tomas Dado committed
32
33
34
35
    if (m_config->isTruthDxAOD()) {
      ATH_MSG_INFO("top::FlavorTaggingCPTools: no need to initialise anything on truth DxAOD");
      return StatusCode::SUCCESS;
    }
36

Tomas Dado's avatar
Tomas Dado committed
37
38
39
    if (!m_config->useJets()) {
      ATH_MSG_INFO("top::FlavorTaggingCPTools: no need to initialise anything since not using jets");
      return StatusCode::SUCCESS;
40
    }
41

Tomas Dado's avatar
Tomas Dado committed
42
    static const std::string cdi_file_default =
43
      "xAODBTaggingEfficiency/13TeV/2020-21-13TeV-MC16-CDI-2021-04-16_v1.root";
44

Tomas Dado's avatar
Tomas Dado committed
45
46
47
48
49
50
51
52
    m_tagger = ""; // Extract in the loop
    if (m_config->bTaggingCDIPath() != "Default") {
      if (m_config->bTaggingCDIPath() != cdi_file_default) {
        m_config->setPrintCDIpathWarning(true);
      }
      m_cdi_file = m_config->bTaggingCDIPath();
    } else {
      m_cdi_file = cdi_file_default;
53
    }
Tomas Dado's avatar
Tomas Dado committed
54
55
    // This ordering needs to match the indexing in TDP (for missing cases, we use default which gives a MC/MC of 1 as
    // its the same as the eff used in the calibration
56
    // Pythia6;Herwigpp;Pythia8;Sherpa(2.2);Sherpa(2.1);aMC@NLO+Pythia8;Herwig7.1.3;Sherpa228;Sherpa2210;Herwigpp721
Tomas Dado's avatar
Tomas Dado committed
57
    // Default changed from 410501 to 410470 in the CDI release of October 2018
58
    m_efficiency_maps = "default;410558;410470;410250;default;410464;411233;421152;700122;600666";
Tomas Dado's avatar
Tomas Dado committed
59
60
61
62
63

    // Configure all tagger/WP/calibration with helper function touching member variables
    // Calibrated and uncalibrated working points for EMTopo jets for all algorithms
    top::check(setTaggerWorkingPoints("AntiKt4EMTopoJets", true, "MV2c10",
                                      {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85",
64
65
                                       "Continuous"}),
                                       "Error setting AntiKt4EMTopoJets WP");
Tomas Dado's avatar
Tomas Dado committed
66
67
    top::check(setTaggerWorkingPoints("AntiKt4EMTopoJets", true, "DL1",
                                      {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85",
68
                                       "Continuous"}), "Error setting AntiKt4EMTopoJets WP");
69
    top::check(setTaggerWorkingPoints("AntiKt4EMTopoJets", false, "DL1r", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt4EMTopoJets WP");
70
    top::check(setTaggerWorkingPoints("AntiKt4EMTopoJets", false, "DL1rmu", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "CTag_Loose", "CTag_Tight", "Continuous"}), "Error setting AntiKt4EMTopoJets WP");
Tomas Dado's avatar
Tomas Dado committed
71
72
73
    top::check(setTaggerWorkingPoints("AntiKt4EMTopoJets", false, "DL1", {"CTag_Loose", "CTag_Tight"}), "Error setting AntiKt4EMTopoJets WP");

    // Calibrated and uncalibrated working points for EMPflow jets for all algorithms
74
    top::check(setTaggerWorkingPoints("AntiKt4EMPFlowJets", false, "MV2c10", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt4EMPFlowJets WP");
75
    top::check(setTaggerWorkingPoints("AntiKt4EMPFlowJets", true, "DL1", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt4EMPFlowJets WP");
yili's avatar
yili committed
76
    top::check(setTaggerWorkingPoints("AntiKt4EMPFlowJets", true, "DL1r", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt4EMPFlowJets WP");
77
    top::check(setTaggerWorkingPoints("AntiKt4EMPFlowJets", false, "DL1rmu", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt4EMPFlowJets WP");
Tomas Dado's avatar
Tomas Dado committed
78
79

    // Calibrated and uncalibrated working points for R=0.2 track jets for all algorithms
80
81
82
83
    top::check(setTaggerWorkingPoints("AntiKt2PV0TrackJets", true, "MV2c10", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt2PV0TrackJets WP");
    top::check(setTaggerWorkingPoints("AntiKt2PV0TrackJets", true, "DL1", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt2PV0TrackJets WP");
    top::check(setTaggerWorkingPoints("AntiKt2PV0TrackJets", false, "DL1r", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt2PV0TrackJets WP");
    top::check(setTaggerWorkingPoints("AntiKt2PV0TrackJets", false, "DL1rmu", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKt2PV0TrackJets WP");
Tomas Dado's avatar
Tomas Dado committed
84
85

    // Calibrated and uncalibrated working points for VR track jets for all algorithms
86
87
    top::check(setTaggerWorkingPoints("AntiKtVR30Rmax4Rmin02TrackJets", true, "MV2c10", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKtVR30Rmax4Rmin02TrackJets WP");
    top::check(setTaggerWorkingPoints("AntiKtVR30Rmax4Rmin02TrackJets", true, "DL1", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKtVR30Rmax4Rmin02TrackJets WP");
88
    top::check(setTaggerWorkingPoints("AntiKtVR30Rmax4Rmin02TrackJets", true, "DL1r", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKtVR30Rmax4Rmin02TrackJets WP");
89
    top::check(setTaggerWorkingPoints("AntiKtVR30Rmax4Rmin02TrackJets", false, "DL1rmu", {"FixedCutBEff_60", "FixedCutBEff_70", "FixedCutBEff_77", "FixedCutBEff_85", "Continuous"}), "Error setting AntiKtVR30Rmax4Rmin02TrackJets WP");
Tomas Dado's avatar
Tomas Dado committed
90
91
92
93
94


    std::string caloJets_type = m_config->sgKeyJetsType();
    std::string caloJets_collection = m_config->sgKeyJets();

95
96
97
    std::string trackJets_type = m_config->sgKeyTrackJetsType();
    std::string trackJets_collection = m_config->sgKeyTrackJets();

Tomas Dado's avatar
Tomas Dado committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    const std::string calib_file_path = PathResolverFindCalibFile(m_cdi_file);
    const std::string excludedSysts = m_config->bTagSystsExcludedFromEV() == "none" ? "" : m_config->bTagSystsExcludedFromEV();

    //------------------------------------------------------------
    // Loop through all the different working points we have and create a
    // BTaggingSelectionTool and corresponding BTaggingEfficiencyTool if the working point is calibrated.
    //------------------------------------------------------------

    // check if the WP requested by the user are available, and if yes, initialize the tools
    // loop through all btagging WPs requested
    for (auto TaggerBtagWP : m_config->bTagWP()) {
      // Overwrite m_tagger anyway (default has to be mv2c10 for R20.7
      m_tagger = TaggerBtagWP.first;
      std::string btagWP = TaggerBtagWP.second;
      std::string bTagWPName = m_tagger + "_" + btagWP;
      if ((caloJets_type == "AntiKt4EMTopoJets" && std::find(m_calo_WPs.begin(), m_calo_WPs.end(), bTagWPName) == m_calo_WPs.end()) ||
          (caloJets_type == "AntiKt4EMPFlowJets" && std::find(m_pflow_WPs.begin(), m_pflow_WPs.end(), bTagWPName) == m_pflow_WPs.end())) {
        ATH_MSG_WARNING("top::FlavorTaggingCPTools::initialize");
116
        ATH_MSG_WARNING("     b-tagging WP: " + bTagWPName + " not supported for jet collection " + caloJets_collection + " with algorithm " + m_tagger);
Tomas Dado's avatar
Tomas Dado committed
117
118
        ATH_MSG_WARNING("     it will therefore be ignored");
      } else {
119
120
121
        //------------------------------------------------------------
        // Setup BTaggingSelectionTool
        //------------------------------------------------------------
Tomas Dado's avatar
Tomas Dado committed
122
123
        // Updated name to use m_tagger
        std::string btagsel_tool_name = "BTaggingSelectionTool_" + bTagWPName + "_" + caloJets_collection;
124
125
        BTaggingSelectionTool* btagsel = new BTaggingSelectionTool(btagsel_tool_name);
        top::check(btagsel->setProperty("TaggerName", m_tagger),
Tomas Dado's avatar
Tomas Dado committed
126
127
128
                   "Failed to set b-tagging selecton tool TaggerName");
        top::check(btagsel->setProperty("JetAuthor", caloJets_collection),
                   "Failed to set b-tagging selection JetAuthor");
129
130
        top::check(btagsel->setProperty("FlvTagCutDefinitionsFileName",
                                        m_cdi_file),
Tomas Dado's avatar
Tomas Dado committed
131
                   "Failed to set b-tagging selection tool CDI file");
132
        top::check(btagsel->setProperty("OperatingPoint", btagWP),
Tomas Dado's avatar
Tomas Dado committed
133
                   "Failed to set b-tagging selection tool OperatingPoint");
134
        top::check(btagsel->setProperty("MinPt",
Tomas Dado's avatar
Tomas Dado committed
135
136
                                        static_cast<double>(m_config->jetPtcut())),
                   "Failed to set b-tagging selection tool MinPt");
137
        top::check(btagsel->setProperty("MaxEta",
Tomas Dado's avatar
Tomas Dado committed
138
139
                                        static_cast<double>(m_config->jetEtacut())),
                   "Failed to set b-tagging selection tool MaxEta");
140
        top::check(btagsel->initialize(),
Tomas Dado's avatar
Tomas Dado committed
141
                   "Failed to initialize b-tagging selection tool");
142
        m_btagging_selection_tools.push_back(btagsel);
143
        m_config->setBTagAlgo_available(m_tagger, btagsel_tool_name);
144

Tomas Dado's avatar
Tomas Dado committed
145
146
147
        if ((caloJets_type == "AntiKt4EMTopoJets" && std::find(m_calo_WPs_calib.begin(), m_calo_WPs_calib.end(), bTagWPName) == m_calo_WPs_calib.end()) ||
            (caloJets_type == "AntiKt4EMPFlowJets" && std::find(m_pflow_WPs_calib.begin(), m_pflow_WPs_calib.end(), bTagWPName) == m_pflow_WPs_calib.end())) {
          ATH_MSG_WARNING("top::FlavorTaggingCPTools::initialize");
148
          ATH_MSG_WARNING("     b-tagging WP: " + bTagWPName + " is not calibrated for jet collection " + caloJets_collection);
Tomas Dado's avatar
Tomas Dado committed
149
150
          ATH_MSG_WARNING("     it will therefore be ignored for the scale-factors, although the tagging decisions will be saved");
        } else {
151
152
153
          //------------------------------------------------------------
          // Setup BTaggingEfficiencyTool
          //------------------------------------------------------------
Tomas Dado's avatar
Tomas Dado committed
154
          std::string btageff_tool_name = "BTaggingEfficiencyTool_" + bTagWPName + "_" + caloJets_collection;
155
156
          BTaggingEfficiencyTool* btageff = new BTaggingEfficiencyTool(btageff_tool_name);
          top::check(btageff->setProperty("TaggerName", m_tagger),
Tomas Dado's avatar
Tomas Dado committed
157
                     "Failed to set b-tagging TaggerName");
158
          top::check(btageff->setProperty("OperatingPoint", btagWP),
Tomas Dado's avatar
Tomas Dado committed
159
160
161
                     "Failed to set b-tagging OperatingPoint");
          top::check(btageff->setProperty("JetAuthor", caloJets_collection),
                     "Failed to set b-tagging JetAuthor");
162
163
164
	  top::check(btageff->setProperty("MinPt",
                                      static_cast<double>(m_config->jetPtcut())),
		     "Failed to set b-tagging selection tool MinPt");
165
          top::check(btageff->setProperty("EfficiencyFileName", calib_file_path),
Tomas Dado's avatar
Tomas Dado committed
166
                     "Failed to set path to b-tagging CDI file");
167
          top::check(btageff->setProperty("ScaleFactorFileName", calib_file_path),
Tomas Dado's avatar
Tomas Dado committed
168
                     "Failed to set path to b-tagging CDI file");
169
          top::check(btageff->setProperty("ScaleFactorBCalibration", m_config->bTaggingCalibration_B()),
Tomas Dado's avatar
Tomas Dado committed
170
                     "Failed to set b-tagging calibration (B): " + m_config->bTaggingCalibration_B());
171
          top::check(btageff->setProperty("ScaleFactorCCalibration", m_config->bTaggingCalibration_C()),
Tomas Dado's avatar
Tomas Dado committed
172
                     "Failed to set b-tagging calibration (C): " + m_config->bTaggingCalibration_C());
173
174
          // using same calibration for T as for C
          top::check(btageff->setProperty("ScaleFactorTCalibration", m_config->bTaggingCalibration_C()),
Tomas Dado's avatar
Tomas Dado committed
175
                     "Failed to set b-tagging calibration (T): " + m_config->bTaggingCalibration_C());
176
          top::check(btageff->setProperty("ScaleFactorLightCalibration", m_config->bTaggingCalibration_Light()),
Tomas Dado's avatar
Tomas Dado committed
177
                     "Failed to set b-tagging calibration (Light): " + m_config->bTaggingCalibration_Light());
178
          for (auto jet_flav : m_jet_flavors) {
Tomas Dado's avatar
Tomas Dado committed
179
180
181
182
183
184
185
            // 09/02/18 IC: The pseudo-continuous does not have MC/MC SF so we need to only apply default for this case
            // 08/05/18 Francesco La Ruffa: The pseudo-continuous has now its own MC/MC SFs, no needed to set default
            top::check(btageff->setProperty("Efficiency" + jet_flav + "Calibrations", m_efficiency_maps),
                       "Failed to set " + jet_flav + "-calibrations efficiency maps");
          }
          top::check(btageff->setProperty("ExcludeFromEigenVectorTreatment", excludedSysts),
                     "Failed to set b-tagging systematics to exclude from EV treatment");
186
          top::check(btageff->initialize(), "Failed to initialize " + bTagWPName);
Tomas Dado's avatar
Tomas Dado committed
187
188
189
          // Check the excludedSysts - Cannot check before the tool is initialised
          top::check(this->checkExcludedSysts(btageff, excludedSysts),
                     "Incorrect excluded systematics have been provided.");
190
          m_btagging_efficiency_tools.push_back(btageff);
Tomas Dado's avatar
Tomas Dado committed
191
          m_config->setBTagWP_calibrated(bTagWPName);
192
        }
Tomas Dado's avatar
Tomas Dado committed
193
        m_config->setBTagWP_available(bTagWPName);
194
      }
195
196
197
198
199
200
    }
    if (m_config->useTrackJets()) {
      for (auto TaggerBtagWP : m_config->bTagWP_trkJet()) {
        m_tagger = TaggerBtagWP.first;
        std::string btagWP = TaggerBtagWP.second;
        std::string bTagWPName = m_tagger + "_" + btagWP;
Tomas Dado's avatar
Tomas Dado committed
201
202
        std::vector<std::string> track_WPs = {};
        std::vector<std::string> track_WPs_calib = {};
203
        if (trackJets_type == "AntiKtVR30Rmax4Rmin02TrackJets") {
Tomas Dado's avatar
Tomas Dado committed
204
205
          track_WPs = m_trackAntiKtVR_WPs;
          track_WPs_calib = m_trackAntiKtVR_WPs_calib;
206
        } else if (trackJets_type == "AntiKt2PV0TrackJets") {
Tomas Dado's avatar
Tomas Dado committed
207
208
          track_WPs = m_trackAntiKt2_WPs;
          track_WPs_calib = m_trackAntiKt2_WPs_calib;
209
        } else if (trackJets_type == "AntiKt4PV0TrackJets") {
Tomas Dado's avatar
Tomas Dado committed
210
211
212
          track_WPs = m_trackAntiKt4_WPs;
          track_WPs_calib = m_trackAntiKt4_WPs_calib;
        }
213

Tomas Dado's avatar
Tomas Dado committed
214
215
        if (std::find(track_WPs.begin(), track_WPs.end(), bTagWPName) == track_WPs.end()) {
          ATH_MSG_WARNING("top::FlavorTaggingCPTools::initialize");
216
          ATH_MSG_WARNING("     b-tagging WP: " + bTagWPName + " not supported for jet collection " + trackJets_collection);
Tomas Dado's avatar
Tomas Dado committed
217
218
219
220
221
          ATH_MSG_WARNING("     it will therefore be ignored");
        } else {
          //------------------------------------------------------------
          // Setup BTaggingSelectionTool
          //------------------------------------------------------------
222
          std::string btagsel_tool_name = "BTaggingSelectionTool_" + bTagWPName + "_" + trackJets_collection;
Tomas Dado's avatar
Tomas Dado committed
223
224
225
          BTaggingSelectionTool* btagsel = new BTaggingSelectionTool(btagsel_tool_name);
          top::check(btagsel->setProperty("TaggerName", m_tagger),
                     "Failed to set b-tagging selecton tool TaggerName");
226
          top::check(btagsel->setProperty("JetAuthor", trackJets_collection),
Tomas Dado's avatar
Tomas Dado committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
                     "Failed to set b-tagging selection JetAuthor");
          top::check(btagsel->setProperty("FlvTagCutDefinitionsFileName",
                                          m_cdi_file),
                     "Failed to set b-tagging selection tool CDI file");
          top::check(btagsel->setProperty("OperatingPoint", btagWP),
                     "Failed to set b-tagging selection tool OperatingPoint");
          top::check(btagsel->setProperty("MinPt",
                                          static_cast<double>(m_config->trackJetPtcut())),
                     "Failed to set b-tagging selection tool MinPt");
          top::check(btagsel->setProperty("MaxEta",
                                          static_cast<double>(m_config->trackJetEtacut())),
                     "Failed to set b-tagging selection tool MaxEta");
          top::check(btagsel->initialize(),
                     "Failed to initialize b-tagging selection tool");
          m_btagging_selection_tools.push_back(btagsel);
242
          m_config->setBTagAlgo_available_trkJet(m_tagger, btagsel_tool_name);
Tomas Dado's avatar
Tomas Dado committed
243
244
245
246

          if (std::find(track_WPs_calib.begin(),
                        track_WPs_calib.end(), bTagWPName) == track_WPs_calib.end()) {
            ATH_MSG_WARNING("top::FlavorTaggingCPTools::initialize");
247
            ATH_MSG_WARNING("     b-tagging WP: " + bTagWPName + " is not calibrated for jet collection " + trackJets_collection);
Tomas Dado's avatar
Tomas Dado committed
248
249
250
251
252
            ATH_MSG_WARNING("     it will therefore be ignored for the scale-factors, although the tagging decisions will be saved");
          } else {
            //------------------------------------------------------------
            // Setup BTaggingEfficiencyTool
            //------------------------------------------------------------
253
            std::string btageff_tool_name = "BTaggingEfficiencyTool_" + bTagWPName + "_" + trackJets_collection;
Tomas Dado's avatar
Tomas Dado committed
254
255
256
257
258
            BTaggingEfficiencyTool* btageff = new BTaggingEfficiencyTool(btageff_tool_name);
            top::check(btageff->setProperty("TaggerName", m_tagger),
                       "Failed to set b-tagging TaggerName");
            top::check(btageff->setProperty("OperatingPoint", btagWP),
                       "Failed to set b-tagging OperatingPoint");
259
            top::check(btageff->setProperty("JetAuthor", trackJets_collection),
Tomas Dado's avatar
Tomas Dado committed
260
                       "Failed to set b-tagging JetAuthor");
261
262
263
            top::check(btageff->setProperty("MinPt",
                                        static_cast<double>(m_config->trackJetPtcut())),
		       "Failed to set b-tagging selection tool MinPt");
Tomas Dado's avatar
Tomas Dado committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
            top::check(btageff->setProperty("EfficiencyFileName", calib_file_path),
                       "Failed to set path to b-tagging CDI file");
            top::check(btageff->setProperty("ScaleFactorFileName", calib_file_path),
                       "Failed to set path to b-tagging CDI file");
            top::check(btageff->setProperty("ScaleFactorBCalibration", m_config->bTaggingCalibration_B()),
                       "Failed to set b-tagging calibration (B): " + m_config->bTaggingCalibration_B());
            top::check(btageff->setProperty("ScaleFactorCCalibration", m_config->bTaggingCalibration_C()),
                       "Failed to set b-tagging calibration (C): " + m_config->bTaggingCalibration_C());
            // using same calibration for T as for C
            top::check(btageff->setProperty("ScaleFactorTCalibration", m_config->bTaggingCalibration_C()),
                       "Failed to set b-tagging calibration (T): " + m_config->bTaggingCalibration_C());
            top::check(btageff->setProperty("ScaleFactorLightCalibration", m_config->bTaggingCalibration_Light()),
                       "Failed to set b-tagging calibration (Light): " + m_config->bTaggingCalibration_Light());
            for (auto jet_flav : m_jet_flavors) {
              top::check(btageff->setProperty("Efficiency" + jet_flav + "Calibrations", m_efficiency_maps),
                         "Failed to set " + jet_flav + "-calibrations efficiency maps");
            }

            top::check(btageff->setProperty("ExcludeFromEigenVectorTreatment", excludedSysts),
                       "Failed to set b-tagging systematics to exclude from EV treatment");
284
            top::check(btageff->initialize(), "Failed to initialize " + bTagWPName);
Tomas Dado's avatar
Tomas Dado committed
285
286
287
288
289
290
291
            // Check the excludedSysts - Cannot check before the tool is initialised
            top::check(this->checkExcludedSysts(btageff, excludedSysts),
                       "Incorrect excluded systematics have been provided.");
            m_btagging_efficiency_tools.push_back(btageff);
            m_config->setBTagWP_calibrated_trkJet(bTagWPName);
          }
          m_config->setBTagWP_available_trkJet(bTagWPName);
292
293
294
        }
      }
    }
Tomas Dado's avatar
Tomas Dado committed
295
    return StatusCode::SUCCESS;
296
  }
Tomas Dado's avatar
Tomas Dado committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

  StatusCode FlavorTaggingCPTools::checkExcludedSysts(BTaggingEfficiencyTool* btageff, std::string excludedSysts) {
    // We pass the pointer to the btagging efficiency tool which is being created and also the excludedSysts string
    // which will be used
    // If the string is empty, then nothing to check
    if (excludedSysts == "") return StatusCode::SUCCESS;

    // Split by a semi-colon delimiter and then check the individual syst strings against the list from the CDI
    std::vector<std::string> listOfExcludedSysts;
    top::tokenize(excludedSysts, listOfExcludedSysts, ";");
    ATH_MSG_INFO(" ------------------------------------------------ ");
    ATH_MSG_INFO(" ------------- EXPERIMENTAL FEATURE ------------- ");
    ATH_MSG_INFO(" ------ Please provide feedback to TopReco ------ ");
    ATH_MSG_INFO(" ------------- EXPERIMENTAL FEATURE ------------- ");
    ATH_MSG_INFO(" ------------------------------------------------ ");
    ATH_MSG_INFO(" AnalysisTop - Checking excludedSysts for flavour tagging EV");
    ATH_MSG_INFO(" This has been split on the semi-colon delimiter to find...");
    for (auto s : listOfExcludedSysts) ATH_MSG_INFO("... " + s);
    // Get the map(string, vector<string>) from the CDI tool
    // Don't care about the flavours (this will be handled in the CDI)
    std::vector<std::string> listOfScaleFactorSystematics;
    for (auto flavour : btageff->listScaleFactorSystematics(false)) {
      for (auto sys : flavour.second) {
        listOfScaleFactorSystematics.push_back(sys);
      }
322
    }
Tomas Dado's avatar
Tomas Dado committed
323
324
325
326
327
328
    // Make this a unique set and then we need to check that all systematics provided by the user are expected by the
    // CDI
    std::set<std::string> setOfExcludedSysts, setOfScaleFactorSystematics;

    for (auto sys : listOfExcludedSysts) {
      if (setOfExcludedSysts.find(sys) == setOfExcludedSysts.end()) setOfExcludedSysts.insert(sys);
329
    }
Tomas Dado's avatar
Tomas Dado committed
330
331
332

    for (auto sys : listOfScaleFactorSystematics) {
      if (setOfScaleFactorSystematics.find(sys) == setOfScaleFactorSystematics.end()) setOfScaleFactorSystematics.insert(sys);
333
    }
Tomas Dado's avatar
Tomas Dado committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360

    //
    std::vector<std::string> unionOfSystematics;
    std::set_intersection(setOfExcludedSysts.begin(), setOfExcludedSysts.end(),
                          setOfScaleFactorSystematics.begin(), setOfScaleFactorSystematics.end(),
                          std::back_inserter(unionOfSystematics));
    // Check we have the same systematics listed in unionOfSystematics
    if (unionOfSystematics.size() != listOfExcludedSysts.size()) {
      ATH_MSG_WARNING("Have not found all systematics listed to be excluded from b-tagging eigenvector method");
      ATH_MSG_INFO("Permitted values are...");
      for (auto sys : setOfScaleFactorSystematics) {
        ATH_MSG_INFO(" ... " + sys);
      }
      return StatusCode::FAILURE;
    } else {
      ATH_MSG_INFO(" Summary of EV impact ");
      for (auto sysRemove : listOfExcludedSysts) {
        std::string flavourAffected = "";
        for (auto flavour : btageff->listScaleFactorSystematics(false)) {
          for (auto sysCDI : flavour.second) {
            if (sysRemove == sysCDI) flavourAffected += flavour.first;
          }
        }
        ATH_MSG_INFO(" ... " + sysRemove + " -> Removed from calibration(s) : [" + flavourAffected + "]");
      }
      ATH_MSG_INFO(" These will be dynamically matched to systematic tree names (if available)");
      ATH_MSG_INFO(" All systematics are accepted by CDI file ");
361
    }
Tomas Dado's avatar
Tomas Dado committed
362
363
364
365
    // We have passed all the tests so now we store the systematics removed from the EV method and use a mapping to
    // ASG/AT naming and return
    ATH_MSG_INFO(" ------------------------------------------------ ");
    return StatusCode::SUCCESS;
366
  }
Tomas Dado's avatar
Tomas Dado committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417

  StatusCode FlavorTaggingCPTools::setTaggerWorkingPoints(std::string jetcollection, bool isCalibrated, std::string tagger, std::vector<std::string> list_of_WP) {
    // To try to reduce errors, make a helper function for setting the lists of tagger_WP which are required
    if (jetcollection == "AntiKt4EMTopoJets" && isCalibrated) {
      // use m_calo_WPs_calib
      for (auto s : list_of_WP) {
        m_calo_WPs_calib.push_back(tagger + "_" + s);
        m_calo_WPs.push_back(tagger + "_" + s);
      }
    } else if (jetcollection == "AntiKt4EMTopoJets" && !isCalibrated) {
      // use m_calo_WPs
      for (auto s : list_of_WP) m_calo_WPs.push_back(tagger + "_" + s);
    } else if (jetcollection == "AntiKt4EMPFlowJets" && isCalibrated) {
      // use m_pflow_WPs_calib
      for (auto s : list_of_WP) {
        m_pflow_WPs_calib.push_back(tagger + "_" + s);
        m_pflow_WPs.push_back(tagger + "_" + s);
      }
    } else if (jetcollection == "AntiKt4EMPFlowJets" && !isCalibrated) {
      // use m_pflow_WPs
      for (auto s : list_of_WP) m_pflow_WPs.push_back(tagger + "_" + s);
    } else if (jetcollection == "AntiKtVR30Rmax4Rmin02TrackJets" && isCalibrated) {
      // use m_trackAntiKt2_WPs_calib
      for (auto s : list_of_WP) {
        m_trackAntiKtVR_WPs_calib.push_back(tagger + "_" + s);
        m_trackAntiKtVR_WPs.push_back(tagger + "_" + s);
      }
    } else if (jetcollection == "AntiKtVR30Rmax4Rmin02TrackJets" && !isCalibrated) {
      // use m_trackAntiKt2_WPs
      for (auto s : list_of_WP) m_trackAntiKtVR_WPs.push_back(tagger + "_" + s);
    } else if (jetcollection == "AntiKt2PV0TrackJets" && isCalibrated) {
      // use m_trackAntiKt2_WPs_calib
      for (auto s : list_of_WP) {
        m_trackAntiKt2_WPs_calib.push_back(tagger + "_" + s);
        m_trackAntiKt2_WPs.push_back(tagger + "_" + s);
      }
    } else if (jetcollection == "AntiKt2PV0TrackJets" && !isCalibrated) {
      // use m_trackAntiKt2_WPs
      for (auto s : list_of_WP) m_trackAntiKt2_WPs.push_back(tagger + "_" + s);
    } else if (jetcollection == "AntiKt4PV0TrackJets" && isCalibrated) {
      // use m_trackAntiKt4_WPs_calib
      for (auto s : list_of_WP) {
        m_trackAntiKt4_WPs_calib.push_back(tagger + "_" + s);
        m_trackAntiKt4_WPs.push_back(tagger + "_" + s);
      }
    } else if (jetcollection == "AntiKt4PV0TrackJets" && !isCalibrated) {
      // use m_trackAntiKt2_WPs_calib
      for (auto s : list_of_WP) m_trackAntiKt4_WPs.push_back(tagger + "_" + s);
    } else {
      ATH_MSG_ERROR("Unknown jet collection and calibration options");
      return StatusCode::FAILURE;
418
    }
Tomas Dado's avatar
Tomas Dado committed
419
    return StatusCode::SUCCESS;
420
421
  }

Tomas Dado's avatar
Tomas Dado committed
422
423
424
425
426
427
  void FlavorTaggingCPTools::printConfigurations() {
    // Debugging function, not used in release
    ATH_MSG_INFO("AntiKt4EMTopoJets - Calibrated WP");
    for (auto s : m_calo_WPs_calib) ATH_MSG_INFO(" -> " << s);
    ATH_MSG_INFO("AntiKt4EMTopoJets - Available selection WP");
    for (auto s : m_calo_WPs) ATH_MSG_INFO(" -> " << s);
428

Tomas Dado's avatar
Tomas Dado committed
429
430
    return;
  }
431
}  // namespace top