22CommonDiTauEfficiencyTool::CommonDiTauEfficiencyTool(
const std::string& sName)
23 :
asg::AsgTool( sName )
24 , m_fXDiTau(&TruthLeadPt)
25 , m_fYDiTau(&TruthSubleadPt)
26 , m_fZDiTau(&TruthDeltaR)
27 , m_sSystematicSet(nullptr)
28 , m_bSFIsAvailableDiTau(false)
29 , m_bSFIsAvailableCheckedDiTau(false)
34CommonDiTauEfficiencyTool::~CommonDiTauEfficiencyTool()
37 for (
auto mEntry : *m_mSF)
38 delete std::get<0>(mEntry.second);
49StatusCode CommonDiTauEfficiencyTool::initialize()
51 ATH_MSG_INFO(
"Initializing CommonDiTauEfficiencyTool" );
57 m_mSF = std::make_unique< tSFMAP >();
58 std::unique_ptr< TFile > fSF( TFile::Open( sInputFilePath.c_str(),
"READ" ) );
61 ATH_MSG_FATAL(
"Could not open file " << sInputFilePath.c_str());
62 return StatusCode::FAILURE;
69 std::vector<std::string> vInputFilePath;
70 split(m_sInputFilePath,
'/',vInputFilePath);
71 m_sInputFileName = vInputFilePath.back();
73 generateSystematicSets();
76 m_sSFHistName =
"sf_"+m_sWP;
80 return StatusCode::FAILURE;
82 return StatusCode::SUCCESS;
93 double& dEfficiencyScaleFactor)
96 if (!m_bSkipTruthMatchCheck and getTruthParticleType(xDiTau) != m_eCheckTruth)
98 dEfficiencyScaleFactor = 1.;
104 dEfficiencyScaleFactor);
107 return tmpCorrectionCode;
110 if (m_sSystematicSet->size() == 0)
114 double dTotalSystematic2 = 0.;
115 double dDirection = 0.;
116 for (
const auto & syst : *m_sSystematicSet)
120 auto it = m_mSystematicsHistNames.find(syst.basename());
121 if (it == m_mSystematicsHistNames.end())[[
unlikely]]
continue;
123 double dUncertaintySyst = 0.;
126 dDirection = syst.parameter();
129 std::string sHistName =
it->second;
130 if (dDirection>0) sHistName+=
"_up";
131 else sHistName+=
"_down";
132 if (!m_sWP.empty()) sHistName+=
"_"+m_sWP;
135 tmpCorrectionCode = getValue(sHistName,
141 return tmpCorrectionCode;
144 dUncertaintySyst *= dDirection;
147 dTotalSystematic2 += dUncertaintySyst * dUncertaintySyst;
151 dDirection = (dDirection > 0.) ? 1. : -1.;
154 dEfficiencyScaleFactor *= 1. + dDirection * std::sqrt(dTotalSystematic2);
174 if (!m_bSFIsAvailableCheckedDiTau)
176 m_bSFIsAvailableDiTau = decor.isAvailable(xDiTau);
177 m_bSFIsAvailableCheckedDiTau =
true;
178 if (m_bSFIsAvailableDiTau)
180 ATH_MSG_DEBUG(m_sVarName <<
" decoration is available on first ditau processed, switched of applyEfficiencyScaleFactor for further ditaus.");
181 ATH_MSG_DEBUG(
"If an application of efficiency scale factors needs to be redone, please pass a shallow copy of the original ditau.");
184 if (m_bSFIsAvailableDiTau)
192 return tmpCorrectionCode;
202 return sys.find(systematic) !=
sys.end();
212 return m_sAffectingSystematics;
221 return m_sRecommendedSystematics;
235StatusCode CommonDiTauEfficiencyTool::applySystematicVariation (
const CP::SystematicSet& sSystematicSet)
239 auto itSystematicSet = m_mSystematicSets.
find(sSystematicSet);
240 if (itSystematicSet != m_mSystematicSets.end())
242 m_sSystematicSet = &itSystematicSet->first;
243 return StatusCode::SUCCESS;
247 double dDirection = 0.;
249 for (
const auto & sSyst : sSystematicSet)
252 auto it = m_mSystematicsHistNames.find(sSyst.basename());
253 if (it == m_mSystematicsHistNames.end())
255 ATH_MSG_VERBOSE(
"unsupported systematic variation: "<< sSyst.basename()<<
"; skipping this one");
260 if (sSyst.parameter() * dDirection < 0)
262 ATH_MSG_ERROR(
"unsupported set of systematic variations, you should either use only \"UP\" or only \"DOWN\" systematics in one set!");
264 return StatusCode::FAILURE;
266 dDirection = sSyst.parameter();
268 if ((m_sRecommendedSystematics.find(sSyst.basename()) != m_sRecommendedSystematics.end()) and sSystematicSet.size() > 1)
270 ATH_MSG_ERROR(
"unsupported set of systematic variations, you should not combine \"TAUS_{TRUE|FAKE}_EFF_*_TOTAL\" with other systematic variations!");
272 return StatusCode::FAILURE;
276 sSystematicSetAvailable.
insert(sSyst);
280 m_sSystematicSet = &m_mSystematicSets.
insert(std::pair<CP::SystematicSet,std::string>(sSystematicSetAvailable, sSystematicSet.name())).first->first;
282 return StatusCode::SUCCESS;
286void CommonDiTauEfficiencyTool::ReadInputs(std::unique_ptr<TFile> &fFile)
291 m_fXDiTau = &TruthLeadPt;
292 m_fYDiTau = &TruthSubleadPt;
293 m_fZDiTau = &TruthDeltaR;
296 TIter itNext(fFile->GetListOfKeys());
297 while ((kKey = (TKey*)itNext()))
301 std::string sKeyName = kKey->GetName();
303 std::vector<std::string> vSplitName = {};
304 split(sKeyName,
'_',vSplitName);
305 if (vSplitName[0] ==
"sf")
307 addHistogramToSFMap(kKey, sKeyName);
311 if (sKeyName.find(
"_up_") != std::string::npos or sKeyName.find(
"_down_") != std::string::npos)
312 addHistogramToSFMap(kKey, sKeyName);
315 size_t iPos = sKeyName.find(
'_');
316 addHistogramToSFMap(kKey, sKeyName.substr(0,iPos)+
"_up"+sKeyName.substr(iPos));
317 addHistogramToSFMap(kKey, sKeyName.substr(0,iPos)+
"_down"+sKeyName.substr(iPos));
328void CommonDiTauEfficiencyTool::addHistogramToSFMap(TKey* kKey,
const std::string& sKeyName)
332 TClass *cClass = gROOT->GetClass(kKey->GetClassName());
333 if (cClass->InheritsFrom(
"TH2"))
335 TH2* oObject =
static_cast<TH2*
>(kKey->ReadObj());
336 oObject->SetDirectory(0);
337 (*m_mSF)[sKeyName] = tTupleObjectFunc(oObject,&getValueTH2);
340 else if (cClass->InheritsFrom(
"TH1"))
342 TH1* oObject =
static_cast<TH1*
>(kKey->ReadObj());
343 oObject->SetDirectory(0);
344 (*m_mSF)[sKeyName] = tTupleObjectFunc(oObject,&getValueTH1);
367void CommonDiTauEfficiencyTool::generateSystematicSets()
370 std::vector<std::string> vSplitInputFilePath = {};
371 split(m_sInputFileName,
'_',vSplitInputFilePath);
372 std::string sEfficiencyType = vSplitInputFilePath.at(0);
373 std::string sTruthType = vSplitInputFilePath.at(1);
374 std::transform(sEfficiencyType.begin(), sEfficiencyType.end(), sEfficiencyType.begin(), toupper);
375 std::transform(sTruthType.begin(), sTruthType.end(), sTruthType.begin(), toupper);
376 std::string sSystematicBaseString =
"TAUS_"+sTruthType+
"_EFF_"+sEfficiencyType+
"_";
381 for (
const auto & mSF : *m_mSF)
384 std::vector<std::string> vSplitNP = {};
385 split(mSF.first,
'_',vSplitNP);
386 std::string sNP = vSplitNP.at(0);
387 std::string sNPUppercase = vSplitNP.at(0);
389 if (sNP ==
"sf")
continue;
391 bool bIsRecommended =
false;
392 if (isupper(sNP.at(0)))
393 bIsRecommended =
true;
395 std::transform(sNPUppercase.begin(), sNPUppercase.end(), sNPUppercase.begin(), toupper);
396 std::string sSystematicString = sSystematicBaseString+sNPUppercase;
406 ATH_MSG_DEBUG(
"connected base name " << sNP <<
" with systematic " <<sSystematicString);
407 m_mSystematicsHistNames.insert({sSystematicString,sNP});
418 double& dEfficiencyScaleFactor)
const
420 const tSFMAP& mSF = *m_mSF;
421 auto it = mSF.find (sHistName);
424 ATH_MSG_ERROR(
"Object with name "<<sHistName<<
" was not found in input file.");
426 for (
const auto & eEntry : mSF){
433 tTupleObjectFunc tTuple =
it->second;
436 double dX = m_fXDiTau(xDiTau);
437 double dY = m_fYDiTau(xDiTau);
438 double dZ = m_fZDiTau(xDiTau);
440 double dVars[3] = {dX, dY, dZ};
443 return (std::get<1>(tTuple))(std::get<0>(tTuple), dEfficiencyScaleFactor, dVars);
451 return acc( xDiTau ) * 0.001;
459 return acc( xDiTau ) * 0.001;
467 return acc( xDiTau );
477 double& dEfficiencyScaleFactor,
double dVars[])
479 double dPt = dVars[0];
481 const TH1* hHist =
dynamic_cast<const TH1*
>(oObject);
490 dPt = std::max(dPt,hHist->GetXaxis()->GetXmin());
492 dPt = std::min(dPt,hHist->GetXaxis()->GetXmax() * .999);
495 int iBin = hHist->FindFixBin(dPt);
496 dEfficiencyScaleFactor = hHist->GetBinContent(iBin);
507 double& dEfficiencyScaleFactor,
double dVars[])
509 double dPt = dVars[0];
510 double dEta = dVars[1];
512 const TH2* hHist =
dynamic_cast<const TH2*
>(oObject);
521 dPt = std::max(dPt,hHist->GetXaxis()->GetXmin());
522 dEta = std::max(dEta,hHist->GetYaxis()->GetXmin());
524 dPt = std::min(dPt,hHist->GetXaxis()->GetXmax() * .999);
525 dEta = std::min(dEta,hHist->GetYaxis()->GetXmax() * .999);
528 int iBin = hHist->FindFixBin(dPt,dEta);
529 dEfficiencyScaleFactor = hHist->GetBinContent(iBin);
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(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
std::vector< std::string > split(const std::string &s, const std::string &t=":")
SG::Decorator< T, ALLOC > Decorator
Helper class to provide type-safe access to aux data, specialized for JaggedVecElt.
bool dEta(const xAOD::TauJet &tau, const xAOD::CaloVertexedTopoCluster &cluster, float &out)
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
DiTauJet_v1 DiTauJet
Definition of the current version.