27namespace ADD_CMODE(CP) {
31ElectronChargeEfficiencyCorrectionTool::
32 ElectronChargeEfficiencyCorrectionTool(
const std::string& name)
34 , m_dataTypeOverwrite(-1)
35 , m_eventInfoCollectionName(
"EventInfo")
39 , m_useRandomRunNumber(
true)
40 , m_defaultRandomRunNumber(999999)
48 , m_filtered_sys_sets()
51 , m_appliedSystematics(
nullptr)
52 , m_sf_decoration_name(
"chargeIDEffiSF")
56 declareProperty(
"CorrectionFileName",
58 "Name of the file with charge flipping rates");
60 "WorkingPoint", m_workingPoint,
"Name of working point folder in the file");
61 declareProperty(
"ScaleFactorDecorationName", m_sf_decoration_name);
62 declareProperty(
"ForceDataType",
64 "Force the DataType of the electron to specified value (to "
65 "circumvent problem of incorrect DataType for forward "
66 "electrons in some old releases)");
67 declareProperty(
"EventInfoCollectionName",
68 m_eventInfoCollectionName,
69 "The EventInfo Collection Name");
70 declareProperty(
"UseRandomRunNumber", m_useRandomRunNumber);
71 declareProperty(
"DefaultRandomRunNumber", m_defaultRandomRunNumber);
77ElectronChargeEfficiencyCorrectionTool::
78 ~ElectronChargeEfficiencyCorrectionTool()
88ElectronChargeEfficiencyCorrectionTool::initialize()
102 if (m_filename.empty()) {
103 ATH_MSG_ERROR(
" PathResolver was not able to find the file ... aborting");
104 return StatusCode::FAILURE;
108 TFile* rootFile = TFile::Open(rootfilename.c_str());
111 if (rootFile ==
nullptr) {
113 <<
" ...... aborting");
114 return StatusCode::FAILURE;
147 TList* keyListfolder = rootFile->GetListOfKeys();
148 std::vector<std::string> names;
149 std::set<std::string> set_systematics;
151 names.reserve(keyListfolder->GetEntries());
152 for (
int j = 0; j < keyListfolder->GetEntries(); j++) {
153 names.emplace_back((keyListfolder->At(j)->GetName()));
157 for (
const std::string & name : names) {
160 if (name.contains(
"SFCentral_")) {
164 if (name.contains(
"_OS")) {
169 std::string histid = (name);
171 histid.erase(histid.size() - 3, 3);
174 if (histid.contains(
"RunNumber")) {
176 std::string runlow = histid;
177 runlow.erase(histid.find(
"RunNumber"), 9);
178 runlow.erase(runlow.find(
'_'), runlow.size());
179 m_RunNumbers.push_back(
180 static_cast<unsigned int>(atoi(runlow.c_str())));
181 std::string runhigh = histid;
182 runhigh.erase(histid.find(
"RunNumber"), 9);
183 runhigh.erase(0, runhigh.find(
'_') + 1);
184 m_RunNumbers.push_back(
185 static_cast<unsigned int>(atoi(runhigh.c_str())));
188 m_SF_OS[histid].push_back(
static_cast<TH2*
>(rootFile->Get(name.c_str())));
190 std::string histid = (name);
192 histid.erase(histid.size() - 3, 3);
194 m_SF_SS[histid].push_back(
static_cast<TH2*
>(rootFile->Get(name.c_str())));
199 if (name.contains(
"STAT_")) {
202 if (name.contains(
"_OS")) {
207 std::string histid = (name);
209 histid.erase(histid.size() - 3, 3);
212 m_SF_OS[histid].push_back(
static_cast<TH2*
>(rootFile->Get(name.c_str())));
214 std::string histid = (name);
217 histid.erase(histid.size() - 3, 3);
219 m_SF_SS[histid].push_back(
static_cast<TH2*
>(rootFile->Get(name.c_str())));
225 if (name.contains(
"SYST")) {
228 if (name.contains(
"_OS")) {
233 std::string histid = (name);
235 histid.erase(histid.size() - 3, 3);
237 std::string sysname = histid;
238 sysname.erase(sysname.find(
'_'), sysname.size());
239 set_systematics.insert(std::move(sysname));
241 histid.erase(0, histid.find(
'_') + 1);
244 m_SF_OS[histid].push_back(
static_cast<TH2*
>(rootFile->Get(name.c_str())));
246 std::string histid = (name);
248 histid.erase(histid.size() - 3, 3);
249 histid.erase(0, histid.find(
'_') + 1);
251 m_SF_SS[histid].push_back(
static_cast<TH2*
>(rootFile->Get(name.c_str())));
259 if (m_SF_OS.empty() || m_SF_SS.empty() || m_SF_SS.size() != m_SF_OS.size()) {
261 "OS/SS SF vectors not filled or of different size. -- Problem with "
262 "files. -- Report to <hn-atlas-EGammaWG@cern.ch>");
263 return StatusCode::FAILURE;
266 m_systematics.insert(m_systematics.end(), set_systematics.begin(), set_systematics.end());
268 std::sort(m_RunNumbers.begin(), m_RunNumbers.end());
274 std::map<std::string, std::vector<TH2*>>
::iterator it = m_SF_OS.begin();
277 m_eta_lowlimit = (*it).second.at(0)->GetYaxis()->GetXmin();
278 m_eta_uplimit = (*it).second.at(0)->GetYaxis()->GetXmax();
279 ATH_MSG_VERBOSE(
"|eta| limits " << m_eta_lowlimit <<
", " << m_eta_uplimit);
281 m_pt_lowlimit = (*it).second.at(0)->GetXaxis()->GetXmin();
282 m_pt_uplimit = (*it).second.at(0)->GetXaxis()->GetXmax();
283 ATH_MSG_VERBOSE(
"pt limits " << m_pt_lowlimit <<
", " << m_pt_uplimit);
289 m_affectingSys = affectingSystematics();
292 if (registerSystematics() != StatusCode::SUCCESS) {
293 ATH_MSG_ERROR(
"(registerSystematics() != CP::SystematicCode::Ok)");
294 return StatusCode::FAILURE;
297 return StatusCode::SUCCESS;
308ElectronChargeEfficiencyCorrectionTool::getEfficiencyScaleFactor(
318 bool goodEle =
false;
323 ATH_MSG_DEBUG(
"This is the check of goodeleCC in getscalefactor. Scale "
324 "factor set to -999");
325 return goodEle_result;
335 int reco_ele_charge = ele.
charge();
336 const double ele_pt = ele.
pt() * m_gevmev;
340 int truth_ele_charge = 9999;
345 ATH_MSG_VERBOSE(
"This is check of geteletruthchargeCC in getscalefactor. "
346 "Scale factor set to -9999");
347 return charge_result;
350 if (truth_ele_charge == 0) {
356 <<
"; Truth charge = " << truth_ele_charge);
364 std::string cutRunNumber =
"all";
366 if (!m_RunNumbers.empty()) {
367 unsigned int runnumber = m_defaultRandomRunNumber;
369 << m_useRandomRunNumber);
370 if (m_useRandomRunNumber) {
372 evtStore()->retrieve<
const xAOD::EventInfo>(m_eventInfoCollectionName);
383 "Pileup tool not run before using ElectronEfficiencyTool! SFs do not "
384 "reflect PU distribution in data");
387 runnumber = randomrunnumber(*(eventInfo));
389 ATH_MSG_DEBUG(
"Number of RunNumbers in file: " << m_RunNumbers.size());
390 for (std::size_t
r = 0;
r < m_RunNumbers.size();
r++) {
395 bool isInRunNumberRange =
false;
396 for ( std::size_t
r=0;
r<m_RunNumbers.size()-1;
r+=2 ){
399 if (
runnumber >= (
unsigned int)m_RunNumbers.at(
r) &&
400 runnumber <= (
unsigned int)m_RunNumbers.at(
r+1) ) {
401 cutRunNumber.clear();
403 Form(
"RunNumber%d_%d", m_RunNumbers.at(
r), m_RunNumbers.at(
r + 1));
404 ATH_MSG_DEBUG(
"Random run number lies in range " << m_RunNumbers.at(
r) <<
" " << m_RunNumbers.at(
r+1));
405 isInRunNumberRange =
true;
410 (
runnumber > m_RunNumbers.at(m_RunNumbers.size() - 1))) {
416 if ( !isInRunNumberRange ) {
422 if ( ele_eta < m_eta_lowlimit || ele_eta > m_eta_uplimit ) {
424 ATH_MSG_DEBUG(
"Got an electron outside of the range of eta validity " << ele_eta);
428 if ( ele_pt < m_pt_lowlimit ) {
430 ATH_MSG_DEBUG(
"Got an electron outside of the range of pt validity: pt lower than lower limit");
435 const std::vector<TH2*>& SShistograms = m_SF_SS.at(cutRunNumber.c_str());
436 const std::vector<TH2*>& OShistograms = m_SF_OS.at(cutRunNumber.c_str());
441 if (truth_ele_charge * reco_ele_charge > 0)
445 retVal = this->getChargeFlipRate(ele_eta, ele_pt, OShistograms.at(0), sf);
452 retVal = this->getChargeFlipRate(ele_eta, ele_pt, SShistograms.at(0), sf);
469 this->getChargeFlipRate(ele_eta, ele_pt, OShistograms.at(1), val_stat);
477 this->getChargeFlipRate(ele_eta, ele_pt, SShistograms.at(1), val_stat);
484 std::vector<float> systs;
485 double val_sys{ 0.0 };
487 for (
unsigned int s = 2; s < OShistograms.size(); s++) {
490 this->getChargeFlipRate(ele_eta, ele_pt, OShistograms.at(s), val_sys);
498 this->getChargeFlipRate(ele_eta, ele_pt, SShistograms.at(s), val_sys);
504 systs.push_back(
static_cast<float>(val_sys));
509 if (m_mySysConf.empty()) {
511 }
else if (*(m_mySysConf.begin()) ==
513 sf = (sf + (val_stat));
515 }
else if (*(m_mySysConf.begin()) ==
517 sf = (sf - (val_stat));
521 for (
unsigned int i = 0; i < m_systematics.size(); i++) {
522 if (*(m_mySysConf.begin()) ==
524 Form(
"EL_CHARGEID_SYS%s", m_systematics.at(i).c_str()), 1)) {
525 sf = (sf + (val_sys));
529 if (*(m_mySysConf.begin()) ==
531 Form(
"EL_CHARGEID_SYS%s", m_systematics.at(i).c_str()), -1)) {
532 sf = (sf - (val_sys));
547ElectronChargeEfficiencyCorrectionTool::applyEfficiencyScaleFactor(
552 "ElectronChargeEfficiencyCorrectionTool::applyEfficiencyScaleFactor("
553 "const xAOD::IParticle& part) const");
557 (*m_sfDec)(part) =
static_cast<float>(sf);
563ElectronChargeEfficiencyCorrectionTool::getChargeFlipRate(
567 double& flipRate)
const
570 <<
" TH2, double&)");
572 if (pt > m_pt_uplimit)
573 pt = m_pt_uplimit * 0.999;
575 int bin2D = hrates->FindBin(pt,
eta);
576 flipRate = hrates->GetBinContent(bin2D);
579 << hrates->GetName());
587ElectronChargeEfficiencyCorrectionTool::isAffectedBySystematic(
592 return sys.find(systematic) != sys.end();
599ElectronChargeEfficiencyCorrectionTool::affectingSystematics()
const
605 for (
unsigned int i = 0; i < m_systematics.size(); i++) {
607 Form(
"EL_CHARGEID_SYS%s", m_systematics.at(i).c_str()), 1));
609 Form(
"EL_CHARGEID_SYS%s", m_systematics.at(i).c_str()), -1));
617ElectronChargeEfficiencyCorrectionTool::recommendedSystematics()
const
620 return affectingSystematics();
626ElectronChargeEfficiencyCorrectionTool::applySystematicVariation(
631 systConfig, m_affectingSys, m_mySysConf)) {
633 "Unsupported combination of systematics passed to the tool! ");
634 return StatusCode::FAILURE;
637 return StatusCode::SUCCESS;
641StatusCode ElectronChargeEfficiencyCorrectionTool::registerSystematics() {
644 if (registry.registerSystematics(*
this) != StatusCode::SUCCESS) {
645 ATH_MSG_ERROR(
"Failed to add systematic to list of recommended systematics.");
646 return StatusCode::FAILURE;
649 return StatusCode::SUCCESS;