ATLAS Offline Software
SystematicStrategyComparison.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifdef XAOD_STANDALONE
7 #else
9 #endif
10 
15 #include "xAODJet/JetContainer.h"
17 #include "xAODBTagging/BTagging.h"
19 
20 #include <string>
21 #include <iomanip>
22 #include "TFile.h"
23 
24 
25 using CP::CorrectionCode;
26 
35 
36 // Further information on usage:
37 // This tool can be configured to test also the response, given a CDI file and a DAOD file.
38 // In this case, the CDI file must contain information for taggers which can operate on the objects in the DAOD file,
39 // outdated CDI files cannot be used on newer DAODs and vice versa! This code will fail otherwise - to avoid this, you
40 // can filter down what combinations to study but adding some simple statements of the form "if(...) continue;", below
41 
42 #ifdef XAOD_STANDALONE
44 #else
46 #endif
47 
48 
49 int main() {
50  bool retval = true;
51  bool perform_jet_validation = false; // flag for turning on the
52  // systematic strategies to compare
53  std::vector<std::string> strats;
54  strats.push_back("SFEigen");
55  strats.push_back("SFGlobalEigen");
56  std::cout << "Starting up the SystematicStrategyComparison . . ." << std::endl;
57  // set your CDI file path here
58  std::string CDIfile = "/cvmfs/atlas.cern.ch/repo/sw/database/GroupData/xAODBTaggingEfficiency/13TeV/2022-22-13TeV-MC20-CDI-2022-07-28_v1.root";
59 
60  if(perform_jet_validation){
61  // set your own DAOD file (ideally a sample with many jets, like ttH), and select a suitable event below
62  std::string DAODpath = "/DAODs/Example.pool.root.1"; // path to an example DAOD file, from which we can retrieve jets to test the BTagging tools
63  //load some jets to show how to use the tool
64  TFile* m_file = TFile::Open(DAODpath.c_str(),"read");
65  if(!event.readFrom(m_file).isSuccess()){
66  std::cout << "failed to load file" << std::endl;
67  return -1;
68  }
69  event.getEntry(42);
70  }
71 
72  Analysis::CDIReader Reader(CDIfile);
73  for(const std::string& tag : Reader.getTaggers()){
74  for(const std::string& jeta : Reader.getJetCollections(tag)){
75  for(const std::string& wp : Reader.getWorkingPoints(tag, jeta)){
76  for(const std::string& strat : strats ){
77  asg::StandaloneToolHandle<IBTaggingEfficiencyTool> tool("BTaggingEfficiencyTool/SysStratTest");
78  StatusCode code1 = tool.setProperty("ScaleFactorFileName", CDIfile); // set your CDI file here
79  StatusCode code2 = tool.setProperty("TaggerName", tag );
80  StatusCode code3 = tool.setProperty("OperatingPoint", wp);
81  StatusCode code4 = tool.setProperty("JetAuthor", jeta);
82  StatusCode code5 = tool.setProperty("MinPt", 1);
83  StatusCode code6 = tool.setProperty("SystematicsStrategy", strat);//either "SFEigen" or "SFGlobalEigen"
84  StatusCode code7 = tool.setProperty("useFlexibleConfig", true);
85 
86  StatusCode code_init = tool.initialize();
87  if (code_init != StatusCode::SUCCESS || code1 != StatusCode::SUCCESS || code2 != StatusCode::SUCCESS
88  || code3 != StatusCode::SUCCESS || code4 != StatusCode::SUCCESS || code5 != StatusCode::SUCCESS
89  || code6 != StatusCode::SUCCESS || code7 != StatusCode::SUCCESS)
90  {
91  std::cout << "Initialization of tool " << tool->name() << " failed! " << std::endl;
92  return -1;
93  }
94  else {
95  std::cout << "Initialization of tool " << tool->name() << " finished." << std::endl;
96  }
97 
98  // select your efficiency map based on the DSID of your sample:
99  unsigned int sample_dsid = 410464;
100 
101  tool->setMapIndex(sample_dsid);
102 
103 
104  std::cout << "-----------------------------------------------------" << std::endl;
105  const std::map<CP::SystematicVariation, std::vector<std::string> > allowed_variations = tool->listSystematics();
106  std::cout << "Allowed systematics variations for tool " << tool->name() << ":" << std::endl;
107  for (const auto& var : allowed_variations) {
108  std::cout << std::setw(40) << std::left << var.first.name() << ":";
109  for (const auto& flv : var.second) std::cout << " " << flv;
110  std::cout << std::endl;
111  }
112  std::cout << "-----------------------------------------------------" << std::endl;
113 
114  if(perform_jet_validation){
115  // retrieve the "real jets" of the jet collection in question
116  const xAOD::JetContainer* jets = nullptr;
117  if (!event.retrieve(jets, jeta).isSuccess()){ std::cout << " error retrieving jets " << std::endl; return -1;}
118 
119  // test with the jet!
120  int jet_index = 0;
121  for(const xAOD::Jet* jet : *jets){
122  if(jet->pt() < 20000 or std::abs(jet->eta()) > 2.4) break; // any lower than this and you start seeing failed SF/Eff retrieval
123  int truthlabel = -999;
124  jet->getAttribute("HadronConeExclTruthLabelID",truthlabel);
125  std::cout << "\n- - - - - - - - - - - - - - - Jet " << jet_index << " - - - - - - - - - - - - - - - -" << std::endl;
126  std::cout << " |- Jet index " << jet_index << " px = " << jet->px() << " py = " << jet->py() << " pz = " << jet->pz() << " e " << jet->e() << std::endl;
127  std::cout << " \\_ pt = " << jet->pt() << " eta = " << jet->eta() << " phi = " << jet->phi() << " m " << jet->m() << std::endl;
128 
129  // Storage for sf/eff values
130  float sf=0;
131  float eff=0;
133  std::cout << "Testing function calls without systematics..." << std::endl;
134  result = tool->getEfficiency(*jet,eff);
135  if( result!=CorrectionCode::Ok) { std::cout << "b jet get efficiency failed"<<std::endl; retval=false;}
136  else {
137  std::cout << "b jet get efficiency succeeded: " << eff << std::endl;
138  }
139  result = tool->getScaleFactor(*jet,sf);
140  if( result!=CorrectionCode::Ok) { std::cout << "b jet get scale factor failed"<<std::endl; retval=false;}
141  else {
142  std::cout << "b jet get scale factor succeeded: " << sf << std::endl;
143  }
144 
145  std::cout << "Testing function calls with systematics..." << std::endl;
146  const CP::SystematicSet& systs = tool->affectingSystematics();
147  for(const auto& var : systs){
149  set.insert(var);
150  StatusCode sresult = tool->applySystematicVariation(set);
151  if( sresult !=StatusCode::SUCCESS) {
152  std::cout << var.name() << " apply systematic variation FAILED " << std::endl;
153  }
154  result = tool->getScaleFactor(*jet,sf);
155  if( result!=CorrectionCode::Ok) {
156  std::cout << var.name() << " getScaleFactor FAILED" << std::endl;
157  } else {
158  std::cout << var.name() << ": scale-factor = " << sf << std::endl;
159  }
160  }
161  // don't forget to switch back off the systematics...
162  CP::SystematicSet defaultSet;
163  StatusCode dummyResult = tool->applySystematicVariation(defaultSet);
164  if (dummyResult != StatusCode::SUCCESS) std::cout << "problem disabling systematics setting!" << std::endl;
165 
166  jet_index += 1;
167  }
168  }
169  } // end strats loop
170  } // end wps loop
171  } // end jets loop
172  } // end taggers loop
173  std::cout << " Great, now the SystematicStrategyComparison is finished! " << std::endl;
174 
175  return retval;
176 }
BTaggingUtilities.h
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
Analysis::dummyResult
const CalibResult dummyResult(dummyValue, dummyValue)
get_generator_info.result
result
Definition: get_generator_info.py:21
BTagging.h
CDIReader.h
main
int main()
Definition: SystematicStrategyComparison.cxx:49
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:100
m_file
std::unique_ptr< TFile > m_file
description: this is a custom writer for the old-school drivers that don't use an actual writer
Definition: OutputStreamData.cxx:52
POOL::TEvent::kClassAccess
@ kClassAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:46
LArCellBinning_test.retval
def retval
Definition: LArCellBinning_test.py:112
IBTaggingEfficiencyTool.h
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:133
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
BTaggingAuxContainer.h
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
asg::StandaloneToolHandle
an "initializing" ToolHandle for stand-alone applications
Definition: StandaloneToolHandle.h:44
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TEvent.h
LHEF::Reader
Pythia8::Reader Reader
Definition: Prophecy4fMerger.cxx:11
PlotSFuncertainty.wp
wp
Definition: PlotSFuncertainty.py:112
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
POOL::TEvent
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:40
TEvent.h
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
JetContainer.h
mapkey::sf
@ sf
Definition: TElectronEfficiencyCorrectionTool.cxx:38
POOL::TEvent::retrieve
StatusCode retrieve(const T *&obj)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:74
CP::CorrectionCode
Return value from object correction CP tools.
Definition: CorrectionCode.h:31
JetAuxContainer.h
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
dqt_zlumi_alleff_HIST.eff
int eff
Definition: dqt_zlumi_alleff_HIST.py:113
StandaloneToolHandle.h
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
Analysis::CDIReader
Definition: CDIReader.h:39
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84