ATLAS Offline Software
CommonDiTauEfficiencyTool.cxx
Go to the documentation of this file.
1 
5 // Framework include(s):
7 
8 // local include(s)
13 
14 // ROOT include(s)
15 #include "TH2F.h"
16 #include <utility>
17 
18 using namespace TauAnalysisTools;
19 
20 //______________________________________________________________________________
22  : CommonEfficiencyTool( sName )
23  , m_fXDiTau(&TruthLeadPt)
24  , m_fYDiTau(&TruthSubleadPt)
25  , m_fZDiTau(&TruthDeltaR)
26  , m_bSFIsAvailableDiTau(false)
27  , m_bSFIsAvailableCheckedDiTau(false)
28 {
29 }
30 
32 {
33 }
34 
35 
36 /*
37  - Find the root files with scale factor inputs on cvmfs using PathResolver
38  (more info here:
39  https://twiki.cern.ch/twiki/bin/viewauth/AtlasComputing/PathResolver)
40  - Call further functions to process and define NP strings and so on
41  - Configure to provide nominal scale factors by default
42 */
44 {
45  ATH_MSG_INFO( "Initializing CommonDiTauEfficiencyTool" );
46  // only read in histograms once
47  if (m_mSF==nullptr)
48  {
49  std::string sInputFilePath = PathResolverFindCalibFile(m_sInputFilePath);
50 
51  m_mSF = std::make_unique< tSFMAP >();
52  std::unique_ptr< TFile > fSF( TFile::Open( sInputFilePath.c_str(), "READ" ) );
53  if(!fSF)
54  {
55  ATH_MSG_FATAL("Could not open file " << sInputFilePath.c_str());
56  return StatusCode::FAILURE;
57  }
58  ReadInputs(fSF);
59  fSF->Close();
60  }
61 
62  // needed later on in generateSystematicSets(), maybe move it there
63  std::vector<std::string> vInputFilePath;
64  split(m_sInputFilePath,'/',vInputFilePath);
65  m_sInputFileName = vInputFilePath.back();
66 
68 
69  if (m_sWP.length()>0)
70  m_sSFHistName = "sf_"+m_sWP;
71 
72  // load empty systematic variation by default
73  if (applySystematicVariation(CP::SystematicSet()) != StatusCode::SUCCESS )
74  return StatusCode::FAILURE;
75 
76  return StatusCode::SUCCESS;
77 }
78 
79 
80 
81 /*
82  Retrieve the scale factors and if requested the values for the NP's and add
83  this stuff in quadrature. Finally return sf_nom +/- n*uncertainty
84 */
85 //______________________________________________________________________________
87  double& dEfficiencyScaleFactor, unsigned int /*iRunNumber*/, unsigned int /*iMu*/)
88 {
89  // check which true state is requestet
90  if (getTruthParticleType(xDiTau) != m_eCheckTruth)
91  {
92  dEfficiencyScaleFactor = 1.;
94  }
95 
96  CP::CorrectionCode tmpCorrectionCode = getValue(m_sSFHistName,
97  xDiTau,
98  dEfficiencyScaleFactor);
99  // return correction code if histogram is not available
100  if (tmpCorrectionCode != CP::CorrectionCode::Ok)
101  return tmpCorrectionCode;
102 
103  // skip further process if systematic set is empty
104  if (m_sSystematicSet->size() == 0)
105  return CP::CorrectionCode::Ok;
106 
107  // get uncertainties summed in quadrature
108  double dTotalSystematic2 = 0.;
109  double dDirection = 0.;
110  for (auto syst : *m_sSystematicSet)
111  {
112 
113  // check if systematic is available
114  auto it = m_mSystematicsHistNames.find(syst.basename());
115 
116  // get uncertainty value
117  double dUncertaintySyst = 0.;
118 
119  // needed for up/down decision
120  dDirection = syst.parameter();
121 
122  // build up histogram name
123  std::string sHistName = it->second;
124  if (dDirection>0) sHistName+="_up";
125  else sHistName+="_down";
126  if (!m_sWP.empty()) sHistName+="_"+m_sWP;
127 
128  // get the uncertainty from the histogram
129  tmpCorrectionCode = getValue(sHistName,
130  xDiTau,
131  dUncertaintySyst);
132 
133  // return correction code if histogram is not available
134  if (tmpCorrectionCode != CP::CorrectionCode::Ok)
135  return tmpCorrectionCode;
136 
137  // scale uncertainty with direction, i.e. +/- n*sigma
138  dUncertaintySyst *= dDirection;
139 
140  // square uncertainty and add to total uncertainty
141  dTotalSystematic2 += dUncertaintySyst * dUncertaintySyst;
142  }
143 
144  // now use dDirection to use up/down uncertainty
145  dDirection = (dDirection > 0.) ? 1. : -1.;
146 
147  // finally apply uncertainty (eff * ( 1 +/- \sum )
148  dEfficiencyScaleFactor *= 1. + dDirection * std::sqrt(dTotalSystematic2);
149 
150  return CP::CorrectionCode::Ok;
151 }
152 
153 /*
154  Get scale factor from getEfficiencyScaleFactor and decorate it to the
155  tau. Note that this can only be done if the variable name is not already used,
156  e.g. if the variable was already decorated on a previous step (enured by the
157  m_bSFIsAvailableCheckedDiTau check).
158 
159  Technical note: cannot use `static SG::Decorator` as we will have
160  multiple instances of this tool with different decoration names.
161 */
162 //______________________________________________________________________________
164  unsigned int iRunNumber, unsigned int iMu)
165 {
166  double dSf = 0.;
167 
170  {
171  m_bSFIsAvailableDiTau = decor.isAvailable(xDiTau);
174  {
175  ATH_MSG_DEBUG(m_sVarName << " decoration is available on first ditau processed, switched of applyEfficiencyScaleFactor for further ditaus.");
176  ATH_MSG_DEBUG("If an application of efficiency scale factors needs to be redone, please pass a shallow copy of the original ditau.");
177  }
178  }
180  return CP::CorrectionCode::Ok;
181 
182  // retrieve scale factor
183  CP::CorrectionCode tmpCorrectionCode = getEfficiencyScaleFactor(xDiTau, dSf, iRunNumber, iMu);
184  // adding scale factor to tau as decoration
185  decor(xDiTau) = dSf;
186 
187  return tmpCorrectionCode;
188 }
189 
190 
191 // //______________________________________________________________________________
192 void CommonDiTauEfficiencyTool::ReadInputs(std::unique_ptr<TFile> &fFile)
193 {
194  m_mSF->clear();
195 
196  // initialize function pointer
200 
201  TKey *kKey;
202  TIter itNext(fFile->GetListOfKeys());
203  while ((kKey = (TKey*)itNext()))
204  {
205  // parse file content for objects of type TNamed, check their title for
206  // known strings and reset funtion pointer
207  std::string sKeyName = kKey->GetName();
208 
209  std::vector<std::string> vSplitName = {};
210  split(sKeyName,'_',vSplitName);
211  if (vSplitName[0] == "sf")
212  {
213  addHistogramToSFMap(kKey, sKeyName);
214  }
215  else
216  {
217  if (sKeyName.find("_up_") != std::string::npos or sKeyName.find("_down_") != std::string::npos)
218  addHistogramToSFMap(kKey, sKeyName);
219  else
220  {
221  size_t iPos = sKeyName.find('_');
222  addHistogramToSFMap(kKey, sKeyName.substr(0,iPos)+"_up"+sKeyName.substr(iPos));
223  addHistogramToSFMap(kKey, sKeyName.substr(0,iPos)+"_down"+sKeyName.substr(iPos));
224  }
225  }
226  }
227  ATH_MSG_INFO("data loaded from " << fFile->GetName());
228 }
229 
230 
231 /*
232  This function parses the names of the objects from the input file and
233  generates the systematic sets and defines which ones are recommended or only
234  available. It also checks, based on the root file name, on which tau it needs
235  to be applied, e.g. only on reco taus coming from true taus or on those faked
236  by true electrons...
237  Examples:
238  filename: JetID_TrueHadDiTau_2017-fall.root -> apply only to true ditaus
239  histname: sf_* -> nominal scale factor
240  histname: TOTAL_* -> "total" NP, recommended
241  histname: afii_* -> "total" NP, not recommended, but available
242 */
243 //______________________________________________________________________________
245 {
246  // creation of basic string for all NPs, e.g. "TAUS_TRUEHADTAU_EFF_RECO_"
247  std::vector<std::string> vSplitInputFilePath = {};
248  split(m_sInputFileName,'_',vSplitInputFilePath);
249  std::string sEfficiencyType = vSplitInputFilePath.at(0);
250  std::string sTruthType = vSplitInputFilePath.at(1);
251  std::transform(sEfficiencyType.begin(), sEfficiencyType.end(), sEfficiencyType.begin(), toupper);
252  std::transform(sTruthType.begin(), sTruthType.end(), sTruthType.begin(), toupper);
253  std::string sSystematicBaseString = "TAUS_"+sTruthType+"_EFF_"+sEfficiencyType+"_";
254  // set truth type to check for in truth matching
255  if (sTruthType=="TRUEHADTAU") m_eCheckTruth = TauAnalysisTools::TruthHadronicTau;
256  if (sTruthType=="TRUEELECTRON") m_eCheckTruth = TauAnalysisTools::TruthElectron;
257  if (sTruthType=="TRUEMUON") m_eCheckTruth = TauAnalysisTools::TruthMuon;
258  if (sTruthType=="TRUEJET") m_eCheckTruth = TauAnalysisTools::TruthJet;
259  if (sTruthType=="TRUEHADDITAU") m_eCheckTruth = TauAnalysisTools::TruthHadronicDiTau;
260  if (sEfficiencyType=="ELEOLR") m_bNoMultiprong = true;
261  for (auto mSF : *m_mSF)
262  {
263  // parse for nuisance parameter in histogram name
264  std::vector<std::string> vSplitNP = {};
265  split(mSF.first,'_',vSplitNP);
266  std::string sNP = vSplitNP.at(0);
267  std::string sNPUppercase = vSplitNP.at(0);
268  // skip nominal scale factors
269  if (sNP == "sf") continue;
270  // test if NP starts with a capital letter indicating that this should be recommended
271  bool bIsRecommended = false;
272  if (isupper(sNP.at(0)))
273  bIsRecommended = true;
274  // make sNP uppercase and build final NP entry name
275  std::transform(sNPUppercase.begin(), sNPUppercase.end(), sNPUppercase.begin(), toupper);
276  std::string sSystematicString = sSystematicBaseString+sNPUppercase;
277  // add all found systematics to the AffectingSystematics
279  m_sAffectingSystematics.insert(CP::SystematicVariation (sSystematicString, -1));
280  // only add found uppercase systematics to the RecommendedSystematics
281  if (bIsRecommended)
282  {
285  }
286  ATH_MSG_DEBUG("connected base name " << sNP << " with systematic " <<sSystematicString);
287  m_mSystematicsHistNames.insert({sSystematicString,sNP});
288  }
289 }
290 
291 /*
292  return value from the tuple map object based on the pt/eta values (or the
293  corresponding value in case of configuration)
294 */
295 //______________________________________________________________________________
297  const xAOD::DiTauJet& xDiTau,
298  double& dEfficiencyScaleFactor) const
299 {
300  const tSFMAP& mSF = *m_mSF;
301  auto it = mSF.find (sHistName);
302  if (it == mSF.end())
303  {
304  ATH_MSG_ERROR("Object with name "<<sHistName<<" was not found in input file.");
305  ATH_MSG_DEBUG("Content of input file");
306  for (auto eEntry : mSF)
307  ATH_MSG_DEBUG(" Entry: "<<eEntry.first);
309  }
310 
311  // get a tuple (TObject*,functionPointer) from the scale factor map
312  tTupleObjectFunc tTuple = it->second;
313 
314  // get pt and eta (for x and y axis respectively)
315  double dX = m_fXDiTau(xDiTau);
316  double dY = m_fYDiTau(xDiTau);
317  double dZ = m_fZDiTau(xDiTau);
318 
319  double dVars[3] = {dX, dY, dZ};
320  // finally obtain efficiency scale factor from TH1F/TH1D/TF1, by calling the
321  // function pointer stored in the tuple from the scale factor map
322  return (std::get<1>(tTuple))(std::get<0>(tTuple), dEfficiencyScaleFactor, dVars);
323 }
324 
325 //______________________________________________________________________________
327 {
328  // return leading truth tau pt in GeV
329  static const SG::ConstAccessor< float > acc( "TruthVisLeadPt" );
330  return acc( xDiTau ) * 0.001;
331 }
332 
333 //______________________________________________________________________________
335 {
336  // return subleading truth tau pt in GeV
337  static const SG::ConstAccessor< float > acc( "TruthVisSubleadPt" );
338  return acc( xDiTau ) * 0.001;
339 }
340 
341 //______________________________________________________________________________
343 {
344  // return truth taus distance delta R
345  static const SG::ConstAccessor< float > acc( "TruthVisDeltaR" );
346  return acc( xDiTau );
347 }
TauAnalysisTools
Definition: TruthCollectionMakerTau.h:16
TauAnalysisTools::CommonDiTauEfficiencyTool::getEfficiencyScaleFactor
virtual CP::CorrectionCode getEfficiencyScaleFactor(const xAOD::DiTauJet &xDiTau, double &dEfficiencyScaleFactor, unsigned int iRunNumber=0, unsigned int iMu=0) override
Get the Efficiency Scale Factor of ditau jet.
Definition: CommonDiTauEfficiencyTool.cxx:86
TauAnalysisTools::TruthElectron
@ TruthElectron
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:100
TauAnalysisTools::CommonDiTauEfficiencyTool::~CommonDiTauEfficiencyTool
~CommonDiTauEfficiencyTool()
Definition: CommonDiTauEfficiencyTool.cxx:31
TauAnalysisTools::CommonEfficiencyTool::m_eCheckTruth
TruthMatchedParticleType m_eCheckTruth
Definition: CommonEfficiencyTool.h:148
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TauAnalysisTools::CommonDiTauEfficiencyTool::m_fXDiTau
double(* m_fXDiTau)(const xAOD::DiTauJet &xDiTau)
scale factor bin x (e.g.
Definition: CommonDiTauEfficiencyTool.h:82
TauAnalysisTools::TruthSubleadPt
double TruthSubleadPt(const xAOD::DiTauJet &xDiTau)
return the truth vis pT of the subleading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:334
TauAnalysisTools::CommonDiTauEfficiencyTool::generateSystematicSets
void generateSystematicSets()
generate a set of relevant systematic variations to be applied
Definition: CommonDiTauEfficiencyTool.cxx:244
TauAnalysisTools::TruthHadronicTau
@ TruthHadronicTau
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:97
CP::SystematicSet::size
size_t size() const
returns: size of the set
Definition: SystematicSet.h:71
TauAnalysisTools::CommonDiTauEfficiencyTool::applyEfficiencyScaleFactor
virtual CP::CorrectionCode applyEfficiencyScaleFactor(const xAOD::DiTauJet &xDiTau, unsigned int iRunNumber=0, unsigned int iMu=0) override
Get the Efficiency Scale Factor of ditau jet.
Definition: CommonDiTauEfficiencyTool.cxx:163
TauAnalysisTools::CommonEfficiencyTool::m_sSystematicSet
const CP::SystematicSet * m_sSystematicSet
Definition: CommonEfficiencyTool.h:99
TruthParticleContainer.h
skel.it
it
Definition: skel.GENtoEVGEN.py:396
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
TauAnalysisTools::CommonEfficiencyTool::tSFMAP
std::map< std::string, tTupleObjectFunc > tSFMAP
Definition: CommonEfficiencyTool.h:88
TauAnalysisTools::CommonEfficiencyTool::applySystematicVariation
virtual StatusCode applySystematicVariation(const CP::SystematicSet &sSystematicSet)
configure this tool for the given list of systematic variations.
Definition: CommonEfficiencyTool.cxx:342
TauAnalysisTools::CommonDiTauEfficiencyTool::m_fYDiTau
double(* m_fYDiTau)(const xAOD::DiTauJet &xDiTau)
scale factor bin y (e.g.
Definition: CommonDiTauEfficiencyTool.h:84
CommonDiTauEfficiencyTool.h
TauAnalysisTools::CommonEfficiencyTool::m_sVarName
std::string m_sVarName
Definition: CommonEfficiencyTool.h:140
SG::ConstAccessor< float >
TauAnalysisTools::TruthJet
@ TruthJet
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:101
CP::SystematicVariation
Definition: SystematicVariation.h:47
TauAnalysisTools::CommonEfficiencyTool::tTupleObjectFunc
std::tuple< TObject *, CP::CorrectionCode(*)(const TObject *oObject, double &dEfficiencyScaleFactor, double dVars[]) > tTupleObjectFunc
Definition: CommonEfficiencyTool.h:87
TauAnalysisTools::TruthLeadPt
double TruthLeadPt(const xAOD::DiTauJet &xDiTau)
return the truth vis pT of the leading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:326
CP::CorrectionCode::Error
@ Error
Some error happened during the object correction.
Definition: CorrectionCode.h:36
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:59
TauEfficiencyCorrectionsTool.h
TauAnalysisTools::CommonEfficiencyTool::m_sWP
std::string m_sWP
Definition: CommonEfficiencyTool.h:139
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
TauAnalysisTools::CommonEfficiencyTool::m_mSF
std::unique_ptr< tSFMAP > m_mSF
Definition: CommonEfficiencyTool.h:95
TauAnalysisTools::CommonDiTauEfficiencyTool::CommonDiTauEfficiencyTool
CommonDiTauEfficiencyTool(const std::string &sName)
Create a proper constructor for Athena.
Definition: CommonDiTauEfficiencyTool.cxx:21
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
TauAnalysisTools::CommonEfficiencyTool
Definition: CommonEfficiencyTool.h:39
TauAnalysisTools::CommonDiTauEfficiencyTool::ReadInputs
void ReadInputs(std::unique_ptr< TFile > &fFile)
Definition: CommonDiTauEfficiencyTool.cxx:192
TauAnalysisTools::CommonDiTauEfficiencyTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: CommonDiTauEfficiencyTool.cxx:43
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
TauAnalysisTools::CommonEfficiencyTool::m_sSFHistName
std::string m_sSFHistName
Definition: CommonEfficiencyTool.h:141
TauAnalysisTools::TruthMuon
@ TruthMuon
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:99
TauAnalysisTools::CommonEfficiencyTool::m_sInputFilePath
std::string m_sInputFilePath
Definition: CommonEfficiencyTool.h:137
TauAnalysisTools::TruthDeltaR
double TruthDeltaR(const xAOD::DiTauJet &xDiTau)
return the dR of between the leading and subleading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:342
PathResolver.h
TauAnalysisTools::CommonEfficiencyTool::m_sAffectingSystematics
CP::SystematicSet m_sAffectingSystematics
Definition: CommonEfficiencyTool.h:134
CP::SystematicSet::insert
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
Definition: SystematicSet.cxx:88
TauAnalysisTools::CommonDiTauEfficiencyTool::m_bSFIsAvailableDiTau
bool m_bSFIsAvailableDiTau
true if scale factor name is already decorated
Definition: CommonDiTauEfficiencyTool.h:106
TauAnalysisTools::CommonEfficiencyTool::addHistogramToSFMap
void addHistogramToSFMap(TKey *kKey, const std::string &sKeyName)
Definition: CommonEfficiencyTool.cxx:555
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
TauAnalysisTools::CommonDiTauEfficiencyTool::m_fZDiTau
double(* m_fZDiTau)(const xAOD::DiTauJet &xDiTau)
scale factor bin z (e.g.
Definition: CommonDiTauEfficiencyTool.h:86
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
SG::Decorator::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
TauAnalysisTools::getTruthParticleType
TruthMatchedParticleType getTruthParticleType(const xAOD::TauJet &xTau)
return TauJet match type
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:572
CP::CorrectionCode
Return value from object correction CP tools.
Definition: CorrectionCode.h:31
xAOD::DiTauJet_v1
Definition: DiTauJet_v1.h:31
TauAnalysisTools::CommonDiTauEfficiencyTool::m_bSFIsAvailableCheckedDiTau
bool m_bSFIsAvailableCheckedDiTau
true if cale factor name is already decorated has already been checked
Definition: CommonDiTauEfficiencyTool.h:108
TauAnalysisTools::CommonEfficiencyTool::m_bNoMultiprong
bool m_bNoMultiprong
Definition: CommonEfficiencyTool.h:143
TauAnalysisTools::CommonEfficiencyTool::m_sInputFileName
std::string m_sInputFileName
Definition: CommonEfficiencyTool.h:138
Decorator.h
Helper class to provide type-safe access to aux data.
TauAnalysisTools::CommonEfficiencyTool::m_mSystematicsHistNames
std::map< std::string, std::string > m_mSystematicsHistNames
Definition: CommonEfficiencyTool.h:101
TauAnalysisTools::TruthHadronicDiTau
@ TruthHadronicDiTau
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:102
TauAnalysisTools::CommonEfficiencyTool::m_sRecommendedSystematics
CP::SystematicSet m_sRecommendedSystematics
Definition: CommonEfficiencyTool.h:135
TauAnalysisTools::CommonDiTauEfficiencyTool::getValue
CP::CorrectionCode getValue(const std::string &sHistName, const xAOD::DiTauJet &xDiTau, double &dEfficiencyScaleFactor) const
Get the scale factor from a particular recommendations histogram.
Definition: CommonDiTauEfficiencyTool.cxx:296
TauAnalysisTools::split
void split(const std::string &sInput, const char cDelim, std::vector< std::string > &vOut)
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:23