73CommonSmearingTool::CommonSmearingTool(
const std::string& sName)
74 :
asg::AsgMetadataTool( sName )
75 , m_sSystematicSet(nullptr)
79 , m_bIsConfigured(false)
80 , m_tauResolutionTool(
"TauResolutionTool", this)
88CommonSmearingTool::~CommonSmearingTool()
90 for (
auto& mEntry : m_mSF)
101StatusCode CommonSmearingTool::initialize()
110 std::unique_ptr<TFile> fSF( TFile::Open(sInputFilePath.c_str()) );
112 ATH_MSG_FATAL(
"Could not open file " << sInputFilePath.c_str());
113 return StatusCode::FAILURE;
115 ReadInputs(fSF.get(), m_mSF);
119 generateSystematicSets();
123 return StatusCode::FAILURE;
126 if (m_bMVATESQualityCheck)
129 ATH_CHECK(m_tauResolutionTool.initialize());
132 return StatusCode::SUCCESS;
148 if(!m_bIsTESCompatibilityCheckAvailable.isValid()) {
150 m_bIsTESCompatibilityCheckAvailable.set (caccTESCompatibility.isAvailable(xTau));
153 if (m_bMVATESQualityCheck && (!(*m_bIsTESCompatibilityCheckAvailable.ptr()))){
155 bool compatibility =
true;
157 if(accPtTauEnergyScale.isAvailable(xTau)) {
158 const auto combinedTEStool =
dynamic_cast<const TauResolutionTool*
>(m_tauResolutionTool.get());
160 throw std::runtime_error(
"CommonSmearingTool::applyCorrection: combinedTEStool is null");
162 compatibility = combinedTEStool->getTESCompatibility(xTau);
165 accTESCompatibility(xTau) =
char(compatibility);
173 if (!m_bSkipTruthMatchCheck and getTruthParticleType(xTau) != m_eCheckTruth) {
183 std::string sProng = ConvertProngToString(xTau.
nTracks());
185 double dCorrection = 1.;
187 if (m_bApplyInsituCorrection)
190 tmpCorrectionCode = getValue(
"sf"+sProng,
195 return tmpCorrectionCode;
199 if (!m_sSystematicSet->empty())
202 double dTotalSystematic2 = 0.;
203 double dDirection = 0.;
204 for (
auto& syst : *m_sSystematicSet)
207 auto it = m_mSystematicsHistNames.find(syst.basename());
208 if (it == m_mSystematicsHistNames.end())[[
unlikely]]{
212 double dUncertaintySyst = 0.;
213 tmpCorrectionCode = getValue(
it->second+sProng,
218 return tmpCorrectionCode;
221 dDirection = syst.parameter();
224 dUncertaintySyst *= dDirection;
227 dTotalSystematic2 += dUncertaintySyst * dUncertaintySyst;
231 dDirection = (dDirection > 0.) ? 1. : -1.;
234 dCorrection *= 1 + dDirection * std::sqrt(dTotalSystematic2);
241 xTau.
eta(), xTau.
phi(), xTau.
m());
258 "There's a possible memory leak!" );
263 xTauCopy->makePrivateStore( xTau );
266 return applyCorrection( *xTauCopy );
276 return sys.find(systematic) !=
sys.end();
286 return m_sAffectingSystematics;
295 return m_sRecommendedSystematics;
309StatusCode CommonSmearingTool::applySystematicVariation (
const CP::SystematicSet& sSystematicSet )
312 auto itSystematicSet = m_mSystematicSets.
find(sSystematicSet);
313 if (itSystematicSet != m_mSystematicSets.end())
315 m_sSystematicSet = &itSystematicSet->first;
316 return StatusCode::SUCCESS;
320 double dDirection = 0.;
322 for (
auto& sSyst : sSystematicSet)
325 auto it = m_mSystematicsHistNames.find(sSyst.basename());
326 if (it == m_mSystematicsHistNames.end())
328 ATH_MSG_VERBOSE(
"unsupported systematic variation: "<< sSyst.basename()<<
"; skipping this one");
332 if (sSyst.parameter() * dDirection < 0)
334 ATH_MSG_ERROR(
"unsupported set of systematic variations, you should either use only \"UP\" or only \"DOWN\" systematics in one set!");
336 return StatusCode::FAILURE;
338 dDirection = sSyst.parameter();
340 if ((m_sRecommendedSystematics.find(sSyst.basename()) != m_sRecommendedSystematics.end()) and sSystematicSet.size() > 1)
342 ATH_MSG_ERROR(
"unsupported set of systematic variations, you should not combine \"TAUS_{TRUE|FAKE}_SME_TOTAL\" with other systematic variations!");
344 return StatusCode::FAILURE;
348 sSystematicSetAvailable.
insert(sSyst);
352 m_sSystematicSet = &m_mSystematicSets.
insert(std::pair<CP::SystematicSet,std::string>(sSystematicSetAvailable, sSystematicSet.name())).first->first;
354 return StatusCode::SUCCESS;
364StatusCode CommonSmearingTool::beginEvent()
367 return StatusCode::SUCCESS;
370 ATH_CHECK(evtStore()->retrieve(xEventInfo,
"EventInfo"));
372 m_bIsConfigured =
true;
374 return StatusCode::SUCCESS;
378std::string CommonSmearingTool::ConvertProngToString(
const int fProngness)
const
380 return fProngness == 1 ?
"_1p" :
"_3p";
385void CommonSmearingTool::ReadInputs(TFile* fFile, std::map<std::string, T>& mMap)
392 TIter itNext(fFile->GetListOfKeys());
393 while ((kKey = (TKey*)itNext()))
395 TClass *cClass = gROOT->GetClass(kKey->GetClassName());
399 std::string sKeyName = kKey->GetName();
400 if (sKeyName ==
"Xaxis")
402 TNamed* tObj = (TNamed*)kKey->ReadObj();
403 std::string sTitle = tObj->GetTitle();
411 if (sKeyName ==
"Yaxis")
413 TNamed* tObj = (TNamed*)kKey->ReadObj();
414 std::string sTitle = tObj->GetTitle();
416 if (sTitle ==
"|eta|")
418 m_fY = &finalTauAbsEta;
422 if (!cClass->InheritsFrom(
"TH1"))
424 T tObj = (T)kKey->ReadObj();
425 tObj->SetDirectory(0);
426 mMap[sKeyName] = tObj;
432void CommonSmearingTool::generateSystematicSets()
434 std::vector<std::string> vInputFilePath;
435 split(m_sInputFilePath,
'/',vInputFilePath);
436 std::string sInputFileName = vInputFilePath.back();
439 std::vector<std::string> vSplitInputFilePath = {};
440 split(sInputFileName,
'_',vSplitInputFilePath);
441 std::string sEfficiencyType = vSplitInputFilePath.at(0);
442 std::string sTruthType = vSplitInputFilePath.at(1);
443 std::transform(sEfficiencyType.begin(), sEfficiencyType.end(), sEfficiencyType.begin(), toupper);
444 std::transform(sTruthType.begin(), sTruthType.end(), sTruthType.begin(), toupper);
445 std::string sSystematicBaseString =
"TAUS_" + sTruthType +
"_SME_" + sEfficiencyType +
"_";
451 for (
auto& mSF : m_mSF)
454 std::vector<std::string> vSplitNP = {};
455 split(mSF.first,
'_',vSplitNP);
457 std::string sNPUppercase;
458 if (vSplitNP.size() > 2)
460 sNP = vSplitNP.at(0)+
'_'+vSplitNP.at(1);
461 sNPUppercase = vSplitNP.at(0) +
'_' + vSplitNP.at(1);
463 sNP = vSplitNP.at(0);
464 sNPUppercase = vSplitNP.at(0);
468 if (sNP ==
"sf")
continue;
470 if (mSF.first.find(
"_1p") == std::string::npos)
continue;
473 bool bIsRecommended =
false;
474 if (isupper(sNP.at(0)))
475 bIsRecommended =
true;
478 std::transform(sNPUppercase.begin(), sNPUppercase.end(), sNPUppercase.begin(), toupper);
479 std::string sSystematicString = sSystematicBaseString + sNPUppercase;
491 ATH_MSG_DEBUG(
"connected histogram base name " << sNP <<
" with systematic " << sSystematicString);
492 m_mSystematicsHistNames.insert({sSystematicString,sNP});
499 double& dEfficiencyScaleFactor)
const
501 TH1* hHist = m_mSF.at(sHistName);
502 if (hHist ==
nullptr)
504 ATH_MSG_ERROR(
"Histogram with name " << sHistName <<
" was not found in input file.");
508 double dPt = m_fX(xTau);
509 double dEta = m_fY(xTau);
512 dPt = std::max(dPt,hHist->GetXaxis()->GetXmin());
513 dEta = std::max(dEta,hHist->GetYaxis()->GetXmin());
515 dPt = std::min(dPt,hHist->GetXaxis()->GetXmax() * .999);
516 dEta = std::min(dEta,hHist->GetYaxis()->GetXmax() * .999);
518 int iBin = hHist->FindFixBin(dPt,dEta);
519 dEfficiencyScaleFactor = hHist->GetBinContent(iBin);
523 std::string sTitle = hHist->GetTitle();
526 TF1
f(
"",sTitle.c_str(), 0, 1000);
527 if (sHistName.find(
"sf_") != std::string::npos)
528 dEfficiencyScaleFactor = (dEfficiencyScaleFactor -1.) *
f.Eval(m_fX(xTau)) + 1.;
530 dEfficiencyScaleFactor *=
f.Eval(m_fX(xTau));
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
struct TBPatternUnitContext Unknown
Return value from object correction CP tools.
@ Error
Some error happened during the object correction.
@ Ok
The correction was done successfully.
Class to wrap a set of SystematicVariations.
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
iterator find(const SystematicVariation &sys) const
description: find an element in the set
Helper class to provide type-safe access to aux data.
Helper class to provide constant type-safe access to aux data.
bool eventType(EventType type) const
Check for one particular bitmask value.
@ IS_SIMULATION
true: simulation, false: data
virtual double phi() const
The azimuthal angle ( ) of the particle.
void setP4(double pt, double eta, double phi, double m)
Set methods for IParticle values.
double ptFinalCalib() const
virtual double m() const
The invariant mass of the particle.
virtual double eta() const
The pseudorapidity ( ) of the particle.
size_t nTracks(TauJetParameters::TauTrackFlag flag=TauJetParameters::TauTrackFlag::classifiedCharged) const
std::vector< std::string > split(const std::string &s, const std::string &t=":")
bool dEta(const xAOD::TauJet &tau, const xAOD::CaloVertexedTopoCluster &cluster, float &out)
EventInfo_v1 EventInfo
Definition of the latest event info version.
TauJet_v3 TauJet
Definition of the current "tau version".