Skip to content

Commit

Permalink
Merge pull request #39963 from shdutta16/CutBasedPhotonID_RunIII_Wint…
Browse files Browse the repository at this point in the history
…er22

Cut based photon id run iii winter22
  • Loading branch information
cmsbuild authored Nov 17, 2022
2 parents 8c5adee + dacec30 commit 5a51d53
Show file tree
Hide file tree
Showing 18 changed files with 1,082 additions and 98 deletions.
9 changes: 8 additions & 1 deletion CommonTools/Egamma/interface/EffectiveAreas.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
class EffectiveAreas {
public:
// Constructor, destructor
EffectiveAreas(const std::string& filename);
EffectiveAreas(const std::string& filename, const bool quadraticEAflag = false);

// Accessors
const float getEffectiveArea(float eta) const;
const float getLinearEA(float eta) const;
const float getQuadraticEA(float eta) const;

// Utility functions
void printEffectiveAreas() const;
Expand All @@ -22,6 +24,11 @@ class EffectiveAreas {
std::vector<float> absEtaMin_; // low limit of the eta range
std::vector<float> absEtaMax_; // upper limit of the eta range
std::vector<float> effectiveAreaValues_; // effective area for this eta range

// Following members are for quadratic PU-correction (introduced for cutBasedPhotonID in Run3_122X)
const bool quadraticEAflag_;
std::vector<float> linearEffectiveAreaValues_;
std::vector<float> quadraticEffectiveAreaValues_;
};

#endif
189 changes: 150 additions & 39 deletions CommonTools/Egamma/src/EffectiveAreas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <string>
#include <sstream>

EffectiveAreas::EffectiveAreas(const std::string& filename) : filename_(filename) {
EffectiveAreas::EffectiveAreas(const std::string& filename, const bool quadraticEAflag)
: filename_(filename), quadraticEAflag_(quadraticEAflag) {
// Open the file with the effective area constants
std::ifstream inputFile;
inputFile.open(filename_.c_str());
Expand All @@ -16,23 +17,54 @@ EffectiveAreas::EffectiveAreas(const std::string& filename) : filename_(filename
// Read file line by line
std::string line;
const float undef = -999;
while (getline(inputFile, line)) {
if (line[0] == '#')
continue; // skip the comments lines
float etaMin = undef, etaMax = undef, effArea = undef;
std::stringstream ss(line);
ss >> etaMin >> etaMax >> effArea;
// In case if the format is messed up, there are letters
// instead of numbers, or not exactly three numbers in the line,
// it is likely that one or more of these vars never changed
// the original "undef" value:
if (etaMin == undef || etaMax == undef || effArea == undef)
throw cms::Exception("EffectiveAreas config failure")
<< "wrong file format, file name " << filename_ << std::endl;

absEtaMin_.push_back(etaMin);
absEtaMax_.push_back(etaMax);
effectiveAreaValues_.push_back(effArea);
if (!quadraticEAflag_) {
while (getline(inputFile, line)) {
if (line[0] == '#')
continue; // skip the comments lines
float etaMin = undef, etaMax = undef, effArea = undef;
std::stringstream ss(line);
ss >> etaMin >> etaMax >> effArea;
// In case if the format is messed up, there are letters
// instead of numbers, or not exactly three numbers in the line,
// it is likely that one or more of these vars never changed
// the original "undef" value:
if (etaMin == undef || etaMax == undef || effArea == undef)
throw cms::Exception("EffectiveAreas config failure")
<< "wrong file format, file name " << filename_ << std::endl;

absEtaMin_.push_back(etaMin);
absEtaMax_.push_back(etaMax);
effectiveAreaValues_.push_back(effArea);
}

}

else {
while (getline(inputFile, line)) {
if (line[0] == '#')
continue; // skip the comments lines

float etaMin = undef, etaMax = undef;
float linEffArea = undef, quadEffArea = undef;

std::stringstream ss(line);

ss >> etaMin >> etaMax >> linEffArea >> quadEffArea;

// In case if the format is messed up, there are letters
// instead of numbers, or not exactly three numbers in the line,
// it is likely that one or more of these vars never changed
// the original "undef" value:
if (etaMin == undef || etaMax == undef || linEffArea == undef || quadEffArea == undef)
throw cms::Exception("EffectiveAreas config failure")
<< "wrong file format, file name " << filename_ << std::endl;

absEtaMin_.push_back(etaMin);
absEtaMax_.push_back(etaMax);
linearEffectiveAreaValues_.push_back(linEffArea);
quadraticEffectiveAreaValues_.push_back(quadEffArea);
}
}

// Extra consistency checks are in the function below.
Expand All @@ -54,41 +86,120 @@ const float EffectiveAreas::getEffectiveArea(float eta) const {
return effArea;
}

void EffectiveAreas::printEffectiveAreas() const {
printf("EffectiveAreas: source file %s\n", filename_.c_str());
printf(" eta_min eta_max effective area\n");
// Return linear term of EA for given eta
const float EffectiveAreas::getLinearEA(float eta) const {
float linEffArea = 0;
uint nEtaBins = absEtaMin_.size();

for (uint iEta = 0; iEta < nEtaBins; iEta++) {
printf(" %8.4f %8.4f %8.5f\n", absEtaMin_[iEta], absEtaMax_[iEta], effectiveAreaValues_[iEta]);
if (std::abs(eta) >= absEtaMin_[iEta] && std::abs(eta) < absEtaMax_[iEta]) {
linEffArea = linearEffectiveAreaValues_[iEta];
break;
}
}

return linEffArea;
}

// Return quadratic term of EA for given eta
const float EffectiveAreas::getQuadraticEA(float eta) const {
float quadEffArea = 0;
uint nEtaBins = absEtaMin_.size();

for (uint iEta = 0; iEta < nEtaBins; iEta++) {
if (std::abs(eta) >= absEtaMin_[iEta] && std::abs(eta) < absEtaMax_[iEta]) {
quadEffArea = quadraticEffectiveAreaValues_[iEta];
break;
}
}

return quadEffArea;
}

void EffectiveAreas::printEffectiveAreas() const {
printf("EffectiveAreas: source file %s\n", filename_.c_str());

if (!quadraticEAflag_) {
printf(" eta_min eta_max effective area\n");
uint nEtaBins = absEtaMin_.size();

for (uint iEta = 0; iEta < nEtaBins; iEta++) {
printf(" %8.4f %8.4f %8.5f\n", absEtaMin_[iEta], absEtaMax_[iEta], effectiveAreaValues_[iEta]);
}
}

else {
printf(" eta_min eta_max EA linear term EA quadratic term\n");
uint nEtaBins = absEtaMin_.size();
for (uint iEta = 0; iEta < nEtaBins; iEta++) {
printf(" %8.4f %8.4f %8.5f %8.5f\n",
absEtaMin_[iEta],
absEtaMax_[iEta],
linearEffectiveAreaValues_[iEta],
quadraticEffectiveAreaValues_[iEta]);
}
}
}

// Basic common sense checks
void EffectiveAreas::checkConsistency() const {
// There should be at least one eta range with one constant
if (effectiveAreaValues_.empty())
throw cms::Exception("EffectiveAreas config failure")
<< "found no effective area constans in the file " << filename_ << std::endl;

uint nEtaBins = absEtaMin_.size();
for (uint iEta = 0; iEta < nEtaBins; iEta++) {
// The low limit should be lower than the upper limit
if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
if (!quadraticEAflag_) {
if (effectiveAreaValues_.empty())
throw cms::Exception("EffectiveAreas config failure")
<< "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;
<< "found no effective area constants in the file " << filename_ << std::endl;

uint nEtaBins = absEtaMin_.size();

// The low limit of the next range should be (near) equal to the
// upper limit of the previous range
if (iEta != nEtaBins - 1) // don't do the check for the last bin
if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
for (uint iEta = 0; iEta < nEtaBins; iEta++) {
// The low limit should be lower than the upper limit
if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
throw cms::Exception("EffectiveAreas config failure")
<< "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;
<< "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;

// The low limit of the next range should be (near) equal to the
// upper limit of the previous range
if (iEta != nEtaBins - 1) // don't do the check for the last bin
if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
throw cms::Exception("EffectiveAreas config failure")
<< "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;

// The effective area should be non-negative number,
// and should be less than the whole calorimeter area
// eta range -2.5 to 2.5, phi 0 to 2pi => Amax = 5*2*pi ~= 31.4
if (!(effectiveAreaValues_[iEta] >= 0 && effectiveAreaValues_[iEta] < 31.4))
throw cms::Exception("EffectiveAreas config failure")
<< "effective area values are too large or negative in the file" << filename_ << std::endl;
}
}

// The effective area should be non-negative number,
// and should be less than the whole calorimeter area
// eta range -2.5 to 2.5, phi 0 to 2pi => Amax = 5*2*pi ~= 31.4
if (!(effectiveAreaValues_[iEta] >= 0 && effectiveAreaValues_[iEta] < 31.4))
else {
if (linearEffectiveAreaValues_.empty() || quadraticEffectiveAreaValues_.empty())
throw cms::Exception("EffectiveAreas config failure")
<< "effective area values are too large or negative in the file" << filename_ << std::endl;
<< "found no effective area constants (linear and/or quadratic) in the file " << filename_ << std::endl;

uint nEtaBins = absEtaMin_.size();

for (uint iEta = 0; iEta < nEtaBins; iEta++) {
// The low limit should be lower than the upper limit
if (!(absEtaMin_[iEta] < absEtaMax_[iEta]))
throw cms::Exception("EffectiveAreas config failure")
<< "eta ranges improperly defined (min>max) in the file" << filename_ << std::endl;

// The low limit of the next range should be (near) equal to the
// upper limit of the previous range
if (iEta != nEtaBins - 1) // don't do the check for the last bin
if (!(absEtaMin_[iEta + 1] - absEtaMax_[iEta] < 0.0001))
throw cms::Exception("EffectiveAreas config failure")
<< "eta ranges improperly defined (disjointed) in the file " << filename_ << std::endl;

// The linear effective area should be non-negative number,
// and should be less than the whole calorimeter area
// eta range -2.5 to 2.5, phi 0 to 2pi => Amax = 5*2*pi ~= 31.4
if (!(linearEffectiveAreaValues_[iEta] >= 0 && linearEffectiveAreaValues_[iEta] < 31.4))
throw cms::Exception("EffectiveAreas config failure")
<< "effective area values are too large or negative in the file" << filename_ << std::endl;
}
}
}
Loading

0 comments on commit 5a51d53

Please sign in to comment.