ATLAS Offline Software
CommonDiTauSmearingTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Framework include(s):
7 
8 // local include(s)
11 
12 // ROOT include(s)
13 #include "TROOT.h"
14 #include "TClass.h"
15 #include "TKey.h"
16 #include "TH3.h"
17 
18 
19 using namespace TauAnalysisTools;
20 
21 //______________________________________________________________________________
23  : CommonSmearingTool( sName )
24  , m_fX(&TruthLeadPt)
25  , m_fY(&TruthSubleadPt)
26  , m_fZ(&TruthDeltaR)
27 {
28 }
29 
30 /*
31  - Find the root files with smearing inputs on eos/cvmfs using PathResolver
32  - Call further functions to process and define NP strings and so on
33  - Configure to provide nominal smearings by default
34 */
36 {
37  ATH_MSG_INFO( "Initializing CommonDiTauSmearingTool" );
38 
39  // only read in histograms once
40  if (m_mDTSF.empty())
41  {
42  std::string sInputFilePath = PathResolverFindCalibFile(m_sInputFilePath);
43  std::unique_ptr< TFile > fSF( TFile::Open(sInputFilePath.c_str(), "READ") );
44  if(fSF == nullptr)
45  {
46  ATH_MSG_FATAL("Could not open file " << sInputFilePath.c_str());
47  return StatusCode::FAILURE;
48  }
49  ReadInputs(fSF.get(), m_mDTSF);
50  fSF->Close();
51  }
52 
54 
55  // load empty systematic variation by default
56  if (applySystematicVariation(CP::SystematicSet()) != StatusCode::SUCCESS )
57  return StatusCode::FAILURE;
58 
59  return StatusCode::SUCCESS;
60 }
61 
62 /*
63  Retrieve the smearing value and if requested the values for the NP's and add
64  this stuff in quadrature. Finally apply the correction to the tau pt of the
65  non-const tau.
66 */
67 //______________________________________________________________________________
69 {
70  // step out here if we run on data
71  if (m_bIsData)
73 
74  // check which true state is requested
76  {
78  }
79 
80  double dCorrection = 1.;
81  // get standard scale factor
82  CP::CorrectionCode tmpCorrectionCode = getValue("sf",
83  xDiTau,
84  dCorrection);
85  // return correction code if histogram is not available
86  if (tmpCorrectionCode != CP::CorrectionCode::Ok)
87  return tmpCorrectionCode;
88 
89  // skip further process if systematic set is empty
90  if (m_sSystematicSet->size() > 0)
91  {
92  // get uncertainties summed in quadrature
93  double dTotalSystematic2 = 0;
94  double dDirection = 0;
95  for (auto syst : *m_sSystematicSet)
96  {
97  // check if systematic is available
98  auto it = m_mSystematicsHistNames.find(syst.basename());
99 
100  // get uncertainty value
101  double dUncertaintySyst = 0;
102  tmpCorrectionCode = getValue(it->second,
103  xDiTau,
104  dUncertaintySyst);
105 
106  // return correction code if histogram is not available
107  if (tmpCorrectionCode != CP::CorrectionCode::Ok)
108  return tmpCorrectionCode;
109 
110  // needed for up/down decision
111  dDirection = syst.parameter();
112 
113  // scale uncertainty with direction, i.e. +/- n*sigma
114  dUncertaintySyst *= dDirection;
115 
116  // square uncertainty and add to total uncertainty
117  dTotalSystematic2 += dUncertaintySyst * dUncertaintySyst;
118  }
119 
120  // now use dDirection to use up/down uncertainty
121  dDirection = (dDirection > 0) ? +1 : -1;
122 
123  // finally apply uncertainty (eff * ( 1 +/- \sum )
124  dCorrection *= 1 + dDirection * std::sqrt(dTotalSystematic2);
125  }
126 
127  // finally apply correction
128  xDiTau.setP4( xDiTau.pt() * dCorrection,
129  xDiTau.eta(), xDiTau.phi(), xDiTau.m());
130  return CP::CorrectionCode::Ok;
131 }
132 
133 /*
134  Create a non-const copy of the passed const xDiTau object and apply the
135  correction to the non-const copy.
136  */
137 //______________________________________________________________________________
139  xAOD::DiTauJet*& xDiTauCopy )
140 {
141 
142  // A sanity check:
143  if( xDiTauCopy )
144  {
145  ATH_MSG_WARNING( "Non-null pointer received. "
146  "There's a possible memory leak!" );
147  }
148 
149  // Create a new object:
150  xDiTauCopy = new xAOD::DiTauJet();
151  xDiTauCopy->makePrivateStore( xDiTau );
152 
153  // Use the other function to modify this object:
154  return applyCorrection( *xDiTauCopy );
155 }
156 
157 //=================================PRIVATE-PART=================================
158 
159 template<class T>
160 void CommonDiTauSmearingTool::ReadInputs(TFile* fFile, std::map<std::string, T>& mMap)
161 {
162  // initialize function pointer
163  m_fX = &TruthLeadPt;
164  m_fY = &TruthSubleadPt;
165  m_fZ = &TruthDeltaR;
166 
167  TKey *kKey;
168  TIter itNext(fFile->GetListOfKeys());
169  while ((kKey = (TKey*)itNext()))
170  {
171  TClass *cClass = gROOT->GetClass(kKey->GetClassName());
172  std::string sKeyName = kKey->GetName();
173 
174  if (!cClass->InheritsFrom("TH3"))
175  continue;
176 
177  T tObj = (T)kKey->ReadObj();
178  tObj->SetDirectory(0);
179  mMap[sKeyName] = tObj;
180  }
181  ATH_MSG_INFO("data loaded from " << fFile->GetName());
182 }
183 
184 //______________________________________________________________________________
186 {
187  std::vector<std::string> vInputFilePath;
188  split(m_sInputFilePath,'/',vInputFilePath);
189  std::string sInputFileName = vInputFilePath.back();
190 
191  // creation of basic string for all NPs, e.g. "TAUS_TRUEHADTAU_SME_TES_"
192  std::vector<std::string> vSplitInputFilePath = {};
193  split(sInputFileName,'_',vSplitInputFilePath);
194  std::string sEfficiencyType = vSplitInputFilePath.at(0);
195  std::string sTruthType = vSplitInputFilePath.at(1);
196  std::transform(sEfficiencyType.begin(), sEfficiencyType.end(), sEfficiencyType.begin(), toupper);
197  std::transform(sTruthType.begin(), sTruthType.end(), sTruthType.begin(), toupper);
198  std::string sSystematicBaseString = "TAUS_"+sTruthType+"_SME_"+sEfficiencyType+"_";
199 
200  // set truth type to check for in truth matching
201  if (sTruthType=="TRUEHADTAU") m_eCheckTruth = TauAnalysisTools::TruthHadronicTau;
202  if (sTruthType=="TRUEHADDITAU") m_eCheckTruth = TauAnalysisTools::TruthHadronicDiTau;
203 
204  for (auto mSF : m_mDTSF)
205  {
206  // parse for nuisance parameter in histogram name
207  std::vector<std::string> vSplitNP = {};
208  split(mSF.first,'_',vSplitNP);
209  std::string sNP = vSplitNP.at(0);
210  std::string sNPUppercase = vSplitNP.at(0);
211 
212  // skip nominal scale factors
213  if (sNP == "sf") continue;
214 
215  // test if NP starts with a capital letter indicating that this should be recommended
216  bool bIsRecommended = false;
217  if (isupper(sNP.at(0)))
218  bIsRecommended = true;
219 
220  // make sNP uppercase and build final NP entry name
221  std::transform(sNPUppercase.begin(), sNPUppercase.end(), sNPUppercase.begin(), toupper);
222  std::string sSystematicString = sSystematicBaseString+sNPUppercase;
223 
224  // add all found systematics to the AffectingSystematics
226  m_sAffectingSystematics.insert(CP::SystematicVariation (sSystematicString, -1));
227  // only add found uppercase systematics to the RecommendedSystematics
228  if (bIsRecommended)
229  {
232  }
233 
234  ATH_MSG_DEBUG("connected histogram base name " << sNP << " with systematic " <<sSystematicString);
235  m_mSystematicsHistNames.insert({sSystematicString,sNP});
236  }
237 }
238 
239 //______________________________________________________________________________
241  const xAOD::DiTauJet& xDiTau,
242  double& dEfficiencyScaleFactor) const
243 {
244  TH3* hHist = m_mDTSF.at(sHistName);
245  if (!hHist)
246  {
247  ATH_MSG_ERROR("Histogram with name "<<sHistName<<" was not found in input file.");
249  }
250 
251  double dX = m_fX(xDiTau);
252  double dY = m_fY(xDiTau);
253  double dZ = m_fZ(xDiTau);
254 
255  // protect values from underflow bins
256  dX = std::max(dX,hHist->GetXaxis()->GetXmin());
257  dY = std::max(dY,hHist->GetYaxis()->GetXmin());
258  dZ = std::max(dZ,hHist->GetZaxis()->GetXmin());
259  // protect values from overflow bins (times .999 to keep it inside last bin)
260  dX = std::min(dX,hHist->GetXaxis()->GetXmax() * .999);
261  dY = std::min(dY,hHist->GetYaxis()->GetXmax() * .999);
262  dZ = std::min(dZ,hHist->GetZaxis()->GetXmax() * .999);
263 
264  int iBin = hHist->FindFixBin(dX,dY,dZ);
265  dEfficiencyScaleFactor = hHist->GetBinContent(iBin);
266 
267  return CP::CorrectionCode::Ok;
268 }
269 
xAOD::DiTauJet_v1::m
virtual double m() const
The invariant mass of the particle.
TauAnalysisTools
Definition: TruthCollectionMakerTau.h:16
xAOD::DiTauJet_v1::pt
virtual double pt() const
The transverse momentum ( ) of the particle.
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TauAnalysisTools::CommonSmearingTool::m_bSkipTruthMatchCheck
Gaudi::Property< bool > m_bSkipTruthMatchCheck
Definition: CommonSmearingTool.h:103
TauAnalysisTools::CommonDiTauSmearingTool::m_fX
double(* m_fX)(const xAOD::DiTauJet &xDiTau)
Definition: CommonDiTauSmearingTool.h:59
CommonDiTauSmearingTool.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TauAnalysisTools::TruthSubleadPt
double TruthSubleadPt(const xAOD::DiTauJet &xDiTau)
return the truth vis pT of the subleading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:330
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
xAOD::DiTauJet
DiTauJet_v1 DiTauJet
Definition of the current version.
Definition: DiTauJet.h:17
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
TruthParticleContainer.h
TauAnalysisTools::CommonSmearingTool::m_sSystematicSet
const CP::SystematicSet * m_sSystematicSet
Definition: CommonSmearingTool.h:85
TauAnalysisTools::CommonDiTauSmearingTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: CommonDiTauSmearingTool.cxx:35
skel.it
it
Definition: skel.GENtoEVGEN.py:407
TauAnalysisTools::CommonDiTauSmearingTool::ReadInputs
void ReadInputs(TFile *fFile, std::map< std::string, T > &mMap)
Definition: CommonDiTauSmearingTool.cxx:160
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
TauAnalysisTools::CommonSmearingTool::m_sRecommendedSystematics
CP::SystematicSet m_sRecommendedSystematics
Definition: CommonSmearingTool.h:115
TauAnalysisTools::CommonSmearingTool
Definition: CommonSmearingTool.h:44
xAOD::DiTauJet_v1::eta
virtual double eta() const
The pseudorapidity ( ) of the particle.
CP::SystematicVariation
Definition: SystematicVariation.h:47
xAOD::DiTauJet_v1::setP4
void setP4(double pt, double eta, double phi, double m)
Set methods for IParticle values.
Definition: DiTauJet_v1.cxx:39
TauAnalysisTools::TruthLeadPt
double TruthLeadPt(const xAOD::DiTauJet &xDiTau)
return the truth vis pT of the leading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:322
TauAnalysisTools::CommonDiTauSmearingTool::m_fZ
double(* m_fZ)(const xAOD::DiTauJet &xDiTau)
Definition: CommonDiTauSmearingTool.h:61
TauAnalysisTools::CommonSmearingTool::m_sInputFilePath
Gaudi::Property< std::string > m_sInputFilePath
Definition: CommonSmearingTool.h:102
TauAnalysisTools::CommonDiTauSmearingTool::getValue
virtual CP::CorrectionCode getValue(const std::string &sHistName, const xAOD::DiTauJet &xDiTau, double &dCorrectionFactor) const
Definition: CommonDiTauSmearingTool.cxx:240
CP::CorrectionCode::Error
@ Error
Some error happened during the object correction.
Definition: CorrectionCode.h:36
xAOD::DiTauJet_v1::phi
virtual double phi() const
The azimuthal angle ( ) of the particle.
TauAnalysisTools::CommonSmearingTool::m_eCheckTruth
TruthMatchedParticleType m_eCheckTruth
Definition: CommonSmearingTool.h:113
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TauAnalysisTools::CommonDiTauSmearingTool::m_fY
double(* m_fY)(const xAOD::DiTauJet &xDiTau)
Definition: CommonDiTauSmearingTool.h:60
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::CommonSmearingTool::applySystematicVariation
virtual StatusCode applySystematicVariation(const CP::SystematicSet &sSystematicSet)
configure this tool for the given list of systematic variations.
Definition: CommonSmearingTool.cxx:300
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
TauAnalysisTools::CommonSmearingTool::m_sAffectingSystematics
CP::SystematicSet m_sAffectingSystematics
Definition: CommonSmearingTool.h:114
TauAnalysisTools::CommonDiTauSmearingTool::correctedCopy
virtual CP::CorrectionCode correctedCopy(const xAOD::DiTauJet &xDiTau, xAOD::DiTauJet *&xDiTauCopy) override
Create a corrected copy from a constant ditau.
Definition: CommonDiTauSmearingTool.cxx:138
TauAnalysisTools::CommonDiTauSmearingTool::m_mDTSF
std::map< std::string, TH3 * > m_mDTSF
Definition: CommonDiTauSmearingTool.h:58
TauAnalysisTools::CommonDiTauSmearingTool::generateSystematicSets
void generateSystematicSets()
Definition: CommonDiTauSmearingTool.cxx:185
TauAnalysisTools::TruthDeltaR
double TruthDeltaR(const xAOD::DiTauJet &xDiTau)
return the dR of between the leading and subleading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:338
TauAnalysisTools::CommonSmearingTool::m_bIsData
bool m_bIsData
Definition: CommonSmearingTool.h:108
PathResolver.h
CP::SystematicSet::insert
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
Definition: SystematicSet.cxx:88
SG::AuxElement::makePrivateStore
void makePrivateStore()
Create a new (empty) private store for this object.
Definition: AuxElement.cxx:192
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:283
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
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::CommonSmearingTool::m_mSystematicsHistNames
std::map< std::string, std::string > m_mSystematicsHistNames
Definition: CommonSmearingTool.h:87
TauAnalysisTools::CommonDiTauSmearingTool::applyCorrection
virtual CP::CorrectionCode applyCorrection(xAOD::DiTauJet &xDiTau) override
Declare the interface that the class provides.
Definition: CommonDiTauSmearingTool.cxx:68
TauAnalysisTools::CommonDiTauSmearingTool::CommonDiTauSmearingTool
CommonDiTauSmearingTool(const std::string &sName)
Create a proper constructor for Athena.
Definition: CommonDiTauSmearingTool.cxx:22
TauAnalysisTools::TruthHadronicDiTau
@ TruthHadronicDiTau
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:104
TauAnalysisTools::split
void split(const std::string &sInput, const char cDelim, std::vector< std::string > &vOut)
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:24