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=="TRUEELECTRON") m_eCheckTruth = TauAnalysisTools::TruthElectron;
203  if (sTruthType=="TRUEMUON") m_eCheckTruth = TauAnalysisTools::TruthMuon;
204  if (sTruthType=="TRUEJET") m_eCheckTruth = TauAnalysisTools::TruthJet;
205  if (sTruthType=="TRUEHADDITAU") m_eCheckTruth = TauAnalysisTools::TruthHadronicDiTau;
206 
207  for (auto mSF : m_mDTSF)
208  {
209  // parse for nuisance parameter in histogram name
210  std::vector<std::string> vSplitNP = {};
211  split(mSF.first,'_',vSplitNP);
212  std::string sNP = vSplitNP.at(0);
213  std::string sNPUppercase = vSplitNP.at(0);
214 
215  // skip nominal scale factors
216  if (sNP == "sf") continue;
217 
218  // test if NP starts with a capital letter indicating that this should be recommended
219  bool bIsRecommended = false;
220  if (isupper(sNP.at(0)))
221  bIsRecommended = true;
222 
223  // make sNP uppercase and build final NP entry name
224  std::transform(sNPUppercase.begin(), sNPUppercase.end(), sNPUppercase.begin(), toupper);
225  std::string sSystematicString = sSystematicBaseString+sNPUppercase;
226 
227  // add all found systematics to the AffectingSystematics
229  m_sAffectingSystematics.insert(CP::SystematicVariation (sSystematicString, -1));
230  // only add found uppercase systematics to the RecommendedSystematics
231  if (bIsRecommended)
232  {
235  }
236 
237  ATH_MSG_DEBUG("connected histogram base name " << sNP << " with systematic " <<sSystematicString);
238  m_mSystematicsHistNames.insert({sSystematicString,sNP});
239  }
240 }
241 
242 //______________________________________________________________________________
244  const xAOD::DiTauJet& xDiTau,
245  double& dEfficiencyScaleFactor) const
246 {
247  TH3* hHist = m_mDTSF.at(sHistName);
248  if (!hHist)
249  {
250  ATH_MSG_ERROR("Histogram with name "<<sHistName<<" was not found in input file.");
252  }
253 
254  double dX = m_fX(xDiTau);
255  double dY = m_fY(xDiTau);
256  double dZ = m_fZ(xDiTau);
257 
258  // protect values from underflow bins
259  dX = std::max(dX,hHist->GetXaxis()->GetXmin());
260  dY = std::max(dY,hHist->GetYaxis()->GetXmin());
261  dZ = std::max(dZ,hHist->GetZaxis()->GetXmin());
262  // protect values from overflow bins (times .999 to keep it inside last bin)
263  dX = std::min(dX,hHist->GetXaxis()->GetXmax() * .999);
264  dY = std::min(dY,hHist->GetYaxis()->GetXmax() * .999);
265  dZ = std::min(dZ,hHist->GetZaxis()->GetXmax() * .999);
266 
267  int iBin = hHist->FindFixBin(dX,dY,dZ);
268  dEfficiencyScaleFactor = hHist->GetBinContent(iBin);
269 
270  return CP::CorrectionCode::Ok;
271 }
272 
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.
TauAnalysisTools::TruthElectron
@ TruthElectron
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:100
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TauAnalysisTools::CommonDiTauSmearingTool::m_fX
double(* m_fX)(const xAOD::DiTauJet &xDiTau)
Definition: CommonDiTauSmearingTool.h:59
max
#define max(a, b)
Definition: cfImp.cxx:41
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:334
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
xAOD::DiTauJet
DiTauJet_v1 DiTauJet
Definition of the current version.
Definition: DiTauJet.h:17
TruthParticleContainer.h
TauAnalysisTools::CommonSmearingTool::m_sSystematicSet
const CP::SystematicSet * m_sSystematicSet
Definition: CommonSmearingTool.h:84
TauAnalysisTools::CommonDiTauSmearingTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: CommonDiTauSmearingTool.cxx:35
skel.it
it
Definition: skel.GENtoEVGEN.py:423
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:113
TauAnalysisTools::CommonSmearingTool
Definition: CommonSmearingTool.h:43
xAOD::DiTauJet_v1::eta
virtual double eta() const
The pseudorapidity ( ) of the particle.
TauAnalysisTools::TruthJet
@ TruthJet
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:101
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:40
TauAnalysisTools::TruthLeadPt
double TruthLeadPt(const xAOD::DiTauJet &xDiTau)
return the truth vis pT of the leading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:326
TauAnalysisTools::CommonSmearingTool::m_sInputFilePath
std::string m_sInputFilePath
Definition: CommonSmearingTool.h:101
TauAnalysisTools::CommonDiTauSmearingTool::m_fZ
double(* m_fZ)(const xAOD::DiTauJet &xDiTau)
Definition: CommonDiTauSmearingTool.h:61
TauAnalysisTools::CommonDiTauSmearingTool::getValue
virtual CP::CorrectionCode getValue(const std::string &sHistName, const xAOD::DiTauJet &xDiTau, double &dCorrectionFactor) const
Definition: CommonDiTauSmearingTool.cxx:243
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:111
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:312
TH3
Definition: rootspy.cxx:440
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:112
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
min
#define min(a, b)
Definition: cfImp.cxx:40
TauAnalysisTools::TruthMuon
@ TruthMuon
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:99
TauAnalysisTools::TruthDeltaR
double TruthDeltaR(const xAOD::DiTauJet &xDiTau)
return the dR of between the leading and subleading pT matched particle.
Definition: CommonDiTauEfficiencyTool.cxx:342
TauAnalysisTools::CommonSmearingTool::m_bIsData
bool m_bIsData
Definition: CommonSmearingTool.h:102
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:172
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
CP::CorrectionCode::Ok
@ Ok
The correction was done successfully.
Definition: CorrectionCode.h:38
TauAnalysisTools::CommonSmearingTool::m_bSkipTruthMatchCheck
bool m_bSkipTruthMatchCheck
Definition: CommonSmearingTool.h:104
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:572
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:86
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:102
TauAnalysisTools::split
void split(const std::string &sInput, const char cDelim, std::vector< std::string > &vOut)
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:23