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.size()>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)
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 {
165  double dSf = 0.;
166 
169  {
170  m_bSFIsAvailableDiTau = decor.isAvailable(xDiTau);
173  {
174  ATH_MSG_DEBUG(m_sVarName << " decoration is available on first ditau processed, switched of applyEfficiencyScaleFactor for further ditaus.");
175  ATH_MSG_DEBUG("If an application of efficiency scale factors needs to be redone, please pass a shallow copy of the original ditau.");
176  }
177  }
179  return CP::CorrectionCode::Ok;
180 
181  // retrieve scale factor
182  CP::CorrectionCode tmpCorrectionCode = getEfficiencyScaleFactor(xDiTau, dSf);
183  // adding scale factor to tau as decoration
184  decor(xDiTau) = dSf;
185 
186  return tmpCorrectionCode;
187 }
188 
189 
190 // //______________________________________________________________________________
191 void CommonDiTauEfficiencyTool::ReadInputs(std::unique_ptr<TFile> &fFile)
192 {
193  m_mSF->clear();
194 
195  // initialize function pointer
199 
200  TKey *kKey;
201  TIter itNext(fFile->GetListOfKeys());
202  while ((kKey = (TKey*)itNext()))
203  {
204  // parse file content for objects of type TNamed, check their title for
205  // known strings and reset funtion pointer
206  std::string sKeyName = kKey->GetName();
207 
208  std::vector<std::string> vSplitName = {};
209  split(sKeyName,'_',vSplitName);
210  if (vSplitName[0] == "sf")
211  {
212  addHistogramToSFMap(kKey, sKeyName);
213  }
214  else
215  {
216  if (sKeyName.find("_up_") != std::string::npos or sKeyName.find("_down_") != std::string::npos)
217  addHistogramToSFMap(kKey, sKeyName);
218  else
219  {
220  size_t iPos = sKeyName.find('_');
221  addHistogramToSFMap(kKey, sKeyName.substr(0,iPos)+"_up"+sKeyName.substr(iPos));
222  addHistogramToSFMap(kKey, sKeyName.substr(0,iPos)+"_down"+sKeyName.substr(iPos));
223  }
224  }
225  }
226  ATH_MSG_INFO("data loaded from " << fFile->GetName());
227 }
228 
229 
230 /*
231  This function parses the names of the objects from the input file and
232  generates the systematic sets and defines which ones are recommended or only
233  available. It also checks, based on the root file name, on which tau it needs
234  to be applied, e.g. only on reco taus coming from true taus or on those faked
235  by true electrons...
236  Examples:
237  filename: JetID_TrueHadDiTau_2017-fall.root -> apply only to true ditaus
238  histname: sf_* -> nominal scale factor
239  histname: TOTAL_* -> "total" NP, recommended
240  histname: afii_* -> "total" NP, not recommended, but available
241 */
242 //______________________________________________________________________________
244 {
245  // creation of basic string for all NPs, e.g. "TAUS_TRUEHADTAU_EFF_RECO_"
246  std::vector<std::string> vSplitInputFilePath = {};
247  split(m_sInputFileName,'_',vSplitInputFilePath);
248  std::string sEfficiencyType = vSplitInputFilePath.at(0);
249  std::string sTruthType = vSplitInputFilePath.at(1);
250  std::transform(sEfficiencyType.begin(), sEfficiencyType.end(), sEfficiencyType.begin(), toupper);
251  std::transform(sTruthType.begin(), sTruthType.end(), sTruthType.begin(), toupper);
252  std::string sSystematicBaseString = "TAUS_"+sTruthType+"_EFF_"+sEfficiencyType+"_";
253  // set truth type to check for in truth matching
254  if (sTruthType=="TRUEHADTAU") m_eCheckTruth = TauAnalysisTools::TruthHadronicTau;
255  if (sTruthType=="TRUEHADDITAU") m_eCheckTruth = TauAnalysisTools::TruthHadronicDiTau;
256 
257  for (auto mSF : *m_mSF)
258  {
259  // parse for nuisance parameter in histogram name
260  std::vector<std::string> vSplitNP = {};
261  split(mSF.first,'_',vSplitNP);
262  std::string sNP = vSplitNP.at(0);
263  std::string sNPUppercase = vSplitNP.at(0);
264  // skip nominal scale factors
265  if (sNP == "sf") continue;
266  // test if NP starts with a capital letter indicating that this should be recommended
267  bool bIsRecommended = false;
268  if (isupper(sNP.at(0)))
269  bIsRecommended = true;
270  // make sNP uppercase and build final NP entry name
271  std::transform(sNPUppercase.begin(), sNPUppercase.end(), sNPUppercase.begin(), toupper);
272  std::string sSystematicString = sSystematicBaseString+sNPUppercase;
273  // add all found systematics to the AffectingSystematics
275  m_sAffectingSystematics.insert(CP::SystematicVariation (sSystematicString, -1));
276  // only add found uppercase systematics to the RecommendedSystematics
277  if (bIsRecommended)
278  {
281  }
282  ATH_MSG_DEBUG("connected base name " << sNP << " with systematic " <<sSystematicString);
283  m_mSystematicsHistNames.insert({sSystematicString,sNP});
284  }
285 }
286 
287 /*
288  return value from the tuple map object based on the pt/eta values (or the
289  corresponding value in case of configuration)
290 */
291 //______________________________________________________________________________
293  const xAOD::DiTauJet& xDiTau,
294  double& dEfficiencyScaleFactor) const
295 {
296  const tSFMAP& mSF = *m_mSF;
297  auto it = mSF.find (sHistName);
298  if (it == mSF.end())
299  {
300  ATH_MSG_ERROR("Object with name "<<sHistName<<" was not found in input file.");
301  ATH_MSG_DEBUG("Content of input file");
302  for (auto eEntry : mSF)
303  ATH_MSG_DEBUG(" Entry: "<<eEntry.first);
305  }
306 
307  // get a tuple (TObject*,functionPointer) from the scale factor map
308  tTupleObjectFunc tTuple = it->second;
309 
310  // get pt and eta (for x and y axis respectively)
311  double dX = m_fXDiTau(xDiTau);
312  double dY = m_fYDiTau(xDiTau);
313  double dZ = m_fZDiTau(xDiTau);
314 
315  double dVars[3] = {dX, dY, dZ};
316  // finally obtain efficiency scale factor from TH1F/TH1D/TF1, by calling the
317  // function pointer stored in the tuple from the scale factor map
318  return (std::get<1>(tTuple))(std::get<0>(tTuple), dEfficiencyScaleFactor, dVars);
319 }
320 
321 //______________________________________________________________________________
323 {
324  // return leading truth tau pt in GeV
325  static const SG::ConstAccessor< float > acc( "TruthVisLeadPt" );
326  return acc( xDiTau ) * 0.001;
327 }
328 
329 //______________________________________________________________________________
331 {
332  // return subleading truth tau pt in GeV
333  static const SG::ConstAccessor< float > acc( "TruthVisSubleadPt" );
334  return acc( xDiTau ) * 0.001;
335 }
336 
337 //______________________________________________________________________________
339 {
340  // return truth taus distance delta R
341  static const SG::ConstAccessor< float > acc( "TruthVisDeltaR" );
342  return acc( xDiTau );
343 }
TauAnalysisTools
Definition: TruthCollectionMakerTau.h:16
TauAnalysisTools::CommonDiTauEfficiencyTool::~CommonDiTauEfficiencyTool
~CommonDiTauEfficiencyTool()
Definition: CommonDiTauEfficiencyTool.cxx:31
TauAnalysisTools::CommonEfficiencyTool::m_eCheckTruth
TruthMatchedParticleType m_eCheckTruth
Definition: CommonEfficiencyTool.h:146
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TauAnalysisTools::CommonDiTauEfficiencyTool::applyEfficiencyScaleFactor
virtual CP::CorrectionCode applyEfficiencyScaleFactor(const xAOD::DiTauJet &xDiTau) override
Get the Efficiency Scale Factor of ditau jet.
Definition: CommonDiTauEfficiencyTool.cxx:163
TauAnalysisTools::CommonEfficiencyTool::m_sInputFilePath
Gaudi::Property< std::string > m_sInputFilePath
Definition: CommonEfficiencyTool.h:133
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:75
TauAnalysisTools::TruthSubleadPt
double TruthSubleadPt(const xAOD::DiTauJet &xDiTau)
return the truth vis pT of the subleading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:330
TauAnalysisTools::CommonDiTauEfficiencyTool::generateSystematicSets
void generateSystematicSets()
generate a set of relevant systematic variations to be applied
Definition: CommonDiTauEfficiencyTool.cxx:243
TauAnalysisTools::TruthHadronicTau
@ TruthHadronicTau
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:99
CP::SystematicSet::size
size_t size() const
returns: size of the set
Definition: SystematicSet.h:71
TauAnalysisTools::CommonEfficiencyTool::m_sSystematicSet
const CP::SystematicSet * m_sSystematicSet
Definition: CommonEfficiencyTool.h:99
TruthParticleContainer.h
TauAnalysisTools::CommonDiTauEfficiencyTool::getEfficiencyScaleFactor
virtual CP::CorrectionCode getEfficiencyScaleFactor(const xAOD::DiTauJet &xDiTau, double &dEfficiencyScaleFactor) override
Get the Efficiency Scale Factor of ditau jet.
Definition: CommonDiTauEfficiencyTool.cxx:86
skel.it
it
Definition: skel.GENtoEVGEN.py:407
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:340
TauAnalysisTools::CommonDiTauEfficiencyTool::m_fYDiTau
double(* m_fYDiTau)(const xAOD::DiTauJet &xDiTau)
scale factor bin y (e.g.
Definition: CommonDiTauEfficiencyTool.h:77
CommonDiTauEfficiencyTool.h
SG::ConstAccessor< float >
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:322
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
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:40
TauAnalysisTools::CommonDiTauEfficiencyTool::ReadInputs
void ReadInputs(std::unique_ptr< TFile > &fFile)
Definition: CommonDiTauEfficiencyTool.cxx:191
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_sVarName
Gaudi::Property< std::string > m_sVarName
Definition: CommonEfficiencyTool.h:134
TauAnalysisTools::CommonEfficiencyTool::m_sSFHistName
std::string m_sSFHistName
Definition: CommonEfficiencyTool.h:143
TauAnalysisTools::TruthDeltaR
double TruthDeltaR(const xAOD::DiTauJet &xDiTau)
return the dR of between the leading and subleading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:338
PathResolver.h
TauAnalysisTools::CommonEfficiencyTool::m_sAffectingSystematics
CP::SystematicSet m_sAffectingSystematics
Definition: CommonEfficiencyTool.h:130
CP::SystematicSet::insert
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
Definition: SystematicSet.cxx:87
TauAnalysisTools::CommonDiTauEfficiencyTool::m_bSFIsAvailableDiTau
bool m_bSFIsAvailableDiTau
true if scale factor name is already decorated
Definition: CommonDiTauEfficiencyTool.h:99
TauAnalysisTools::CommonEfficiencyTool::addHistogramToSFMap
void addHistogramToSFMap(TKey *kKey, const std::string &sKeyName)
Definition: CommonEfficiencyTool.cxx:540
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:283
TauAnalysisTools::CommonDiTauEfficiencyTool::m_fZDiTau
double(* m_fZDiTau)(const xAOD::DiTauJet &xDiTau)
scale factor bin z (e.g.
Definition: CommonDiTauEfficiencyTool.h:79
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
TauAnalysisTools::CommonEfficiencyTool::m_sWP
Gaudi::Property< std::string > m_sWP
Definition: CommonEfficiencyTool.h:135
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:494
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:101
TauAnalysisTools::CommonEfficiencyTool::m_sInputFileName
std::string m_sInputFileName
Definition: CommonEfficiencyTool.h:142
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:104
TauAnalysisTools::CommonEfficiencyTool::m_sRecommendedSystematics
CP::SystematicSet m_sRecommendedSystematics
Definition: CommonEfficiencyTool.h:131
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:292
TauAnalysisTools::split
void split(const std::string &sInput, const char cDelim, std::vector< std::string > &vOut)
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:24