Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RecoEgamma/EgammaTools] Updates to EnergyScaleCorrection class for scales and smearings in UL #29526

Merged
merged 7 commits into from May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions RecoEgamma/EgammaTools/interface/EnergyScaleCorrection.h
Expand Up @@ -92,6 +92,16 @@ class EnergyScaleCorrection {
etMax_(et),
gain_(gainSeed) {}

CorrectionCategory(unsigned int runMin,
unsigned int runMax,
float etaMin,
float etaMax,
float r9Min,
float r9Max,
float etMin,
float etMax,
unsigned int gainSeed);

bool operator<(const CorrectionCategory& b) const;
bool inCategory(
const unsigned int runnr, const float et, const float eta, const float r9, const unsigned int gainSeed) const;
Expand Down Expand Up @@ -149,6 +159,21 @@ class EnergyScaleCorrection {
double errDeltaP,
double errSystDeltaP,
double errDeltaPGain);

void addScale(int runMin,
int runMax,
double etaMin,
double etaMax,
double r9Min,
double r9Max,
double etMin,
double etMax,
unsigned int gain,
double energyScale,
double energyScaleErrStat,
double energyScaleErrSyst,
double energyScaleErrGain);

void addSmearing(const std::string& category,
int runMin,
int runMax,
Expand Down
10 changes: 7 additions & 3 deletions RecoEgamma/EgammaTools/python/calibratedEgammas_cff.py
Expand Up @@ -2,17 +2,21 @@

_correctionFile2016Legacy = "EgammaAnalysis/ElectronTools/data/ScalesSmearings/Legacy2016_07Aug2017_FineEtaR9_v3_ele_unc"
_correctionFile2017Nov17 = "EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2017_17Nov2017_v1_ele_unc"
_correctionFile2017UL = "EgammaAnalysis/ElectronTools/data/ScalesSmearings/Run2017_24Feb2020_runEtaR9Gain_v2"

calibratedEgammaSettings = cms.PSet(minEtToCalibrate = cms.double(5.0),
semiDeterministic = cms.bool(True),
correctionFile = cms.string(_correctionFile2017Nov17),
correctionFile = cms.string(_correctionFile2017UL),
recHitCollectionEB = cms.InputTag('reducedEcalRecHitsEB'),
recHitCollectionEE = cms.InputTag('reducedEcalRecHitsEE'),
produceCalibratedObjs = cms.bool(True)
)
)
from Configuration.Eras.Modifier_run2_miniAOD_80XLegacy_cff import run2_miniAOD_80XLegacy
run2_miniAOD_80XLegacy.toModify(calibratedEgammaSettings,correctionFile = _correctionFile2016Legacy)

from Configuration.Eras.Modifier_run2_miniAOD_94XFall17_cff import run2_miniAOD_94XFall17
run2_miniAOD_94XFall17.toModify(calibratedEgammaSettings,correctionFile = _correctionFile2017Nov17)

calibratedEgammaPatSettings = calibratedEgammaSettings.clone(
recHitCollectionEB = cms.InputTag('reducedEgamma','reducedEBRecHits'),
recHitCollectionEE = cms.InputTag('reducedEgamma','reducedEERecHits')
Expand Down Expand Up @@ -60,7 +64,7 @@
calibratedEgammaPatSettings,
epCombConfig = ecalTrkCombinationRegression,
src = cms.InputTag('slimmedElectrons'),
)
)

calibratedPhotons = cms.EDProducer("CalibratedPhotonProducer",
calibratedEgammaSettings,
Expand Down
112 changes: 107 additions & 5 deletions RecoEgamma/EgammaTools/src/EnergyScaleCorrection.cc
Expand Up @@ -7,6 +7,8 @@
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <sstream>
#include <iterator>

EnergyScaleCorrection::EnergyScaleCorrection(const std::string& correctionFileName, unsigned int genSeed)
: smearingType_(ECALELF) {
Expand Down Expand Up @@ -148,6 +150,31 @@ void EnergyScaleCorrection::addScale(const std::string& category,
std::sort(scales_.begin(), scales_.end(), Sorter<CorrectionCategory, ScaleCorrection>());
}

void EnergyScaleCorrection::addScale(int runMin,
int runMax,
double etaMin,
double etaMax,
double r9Min,
double r9Max,
double etMin,
double etMax,
unsigned int gain,
double energyScale,
double energyScaleErrStat,
double energyScaleErrSyst,
double energyScaleErrGain) {
CorrectionCategory cat(runMin, runMax, etaMin, etaMax, r9Min, r9Max, etMin, etMax, gain);

auto result = std::equal_range(scales_.begin(), scales_.end(), cat, Sorter<CorrectionCategory, ScaleCorrection>());
if (result.first != result.second) {
throw cms::Exception("ConfigError") << "Category already defined! " << cat;
}

ScaleCorrection corr(energyScale, energyScaleErrStat, energyScaleErrSyst, energyScaleErrGain);
scales_.push_back({cat, corr});
std::sort(scales_.begin(), scales_.end(), Sorter<CorrectionCategory, ScaleCorrection>());
}

void EnergyScaleCorrection::addSmearing(const std::string& category,
int runMin,
int runMax,
Expand Down Expand Up @@ -189,10 +216,56 @@ void EnergyScaleCorrection::readScalesFromFile(const std::string& filename) {
std::string category, region2;
double energyScale, energyScaleErr, energyScaleErrStat, energyScaleErrSyst, energyScaleErrGain;

for (file >> category; file.good(); file >> category) {
file >> region2 >> runMin >> runMax >> energyScale >> energyScaleErr >> energyScaleErrStat >> energyScaleErrSyst >>
energyScaleErrGain;
addScale(category, runMin, runMax, energyScale, energyScaleErrStat, energyScaleErrSyst, energyScaleErrGain);
double etaMin; ///< Min eta value for the bin
double etaMax; ///< Max eta value for the bin
double r9Min; ///< Min R9 vaule for the bin
double r9Max; ///< Max R9 value for the bin
double etMin; ///< Min Et value for the bin
double etMax; ///< Max Et value for the bin
unsigned int gain; ///< 12, 6, 1, 61 (double gain switch)

// TO count the #columns in that txt file and decide based on that the version to read
std::string line;
std::stringstream stream;
getline(file, line);
stream.clear();
stream << line;

int ncols = std::distance(std::istream_iterator<std::string>(stream), std::istream_iterator<std::string>());

file.seekg(0, std::ios::beg);

if (ncols == 9) {
for (file >> category; file.good(); file >> category) {
file >> region2 >> runMin >> runMax >> energyScale >> energyScaleErr >> energyScaleErrStat >>
energyScaleErrSyst >> energyScaleErrGain;
addScale(category, runMin, runMax, energyScale, energyScaleErrStat, energyScaleErrSyst, energyScaleErrGain);
}
} else {
if (file.peek() == 'r')
file.ignore(1000, 10);

for (file >> runMin; file.good(); file >> runMin) {
file >> runMax >> etaMin >> etaMax >> r9Min >> r9Max >> etMin >> etMax >> gain >> energyScale >> energyScaleErr;
file.ignore(1000, 10);
energyScaleErrStat = energyScaleErr;
energyScaleErrSyst = 0;
energyScaleErrGain = 0;

addScale(runMin,
runMax,
etaMin,
etaMax,
r9Min,
r9Max,
etMin,
etMax,
gain,
energyScale,
energyScaleErrStat,
energyScaleErrSyst,
energyScaleErrGain);
}
}

file.close();
Expand Down Expand Up @@ -383,7 +456,7 @@ EnergyScaleCorrection::CorrectionCategory::CorrectionCategory(const std::string&
p2 = category.find("-", p1); // Position of - or end of string
gain_ = std::stoul(category.substr(p1, p2 - p1), nullptr);
}
//so turns out the code does an includes X<=Y<=Z search for bins
//so turns out the code does an inclusive X<=Y<=Z search for bins
//which is what we want for run numbers
//however then the problem is when we get a value exactly at the bin boundary
//for the et/eta/r9 which then gives multiple bins
Expand All @@ -393,6 +466,35 @@ EnergyScaleCorrection::CorrectionCategory::CorrectionCategory(const std::string&
etaMax_ = std::nextafterf(etaMax_, std::numeric_limits<float>::min());
r9Max_ = std::nextafterf(r9Max_, std::numeric_limits<float>::min());
}

///for the new file format
EnergyScaleCorrection::CorrectionCategory::CorrectionCategory(unsigned int runMin,
unsigned int runMax,
float etaMin,
float etaMax,
float r9Min,
float r9Max,
float etMin,
float etMax,
unsigned int gainSeed)
: runMin_(runMin),
runMax_(runMax),
etaMin_(etaMin),
etaMax_(etaMax),
r9Min_(r9Min),
r9Max_(r9Max),
etMin_(etMin),
etMax_(etMax),
gain_(gainSeed) {
///Same logic as the above constructor to avoid problems at the bin
///boundary of et/eta/R9 - just decrement the maxValues
///ever so slightly to ensure that they are different
///from the next bins min value
etMax_ = std::nextafterf(etMax_, std::numeric_limits<float>::min());
etaMax_ = std::nextafterf(etaMax_, std::numeric_limits<float>::min());
r9Max_ = std::nextafterf(r9Max_, std::numeric_limits<float>::min());
};

bool EnergyScaleCorrection::CorrectionCategory::inCategory(
const unsigned int runnr, const float et, const float eta, const float r9, const unsigned int gainSeed) const {
return runnr >= runMin_ && runnr <= runMax_ && et >= etMin_ && et <= etMax_ && eta >= etaMin_ && eta <= etaMax_ &&
Expand Down