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

Made pat::MET const thread-safe #38457

Merged
merged 1 commit into from Jun 30, 2022
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
58 changes: 26 additions & 32 deletions DataFormats/PatCandidates/interface/MET.h
Expand Up @@ -266,52 +266,46 @@ namespace pat {
LorentzVector shiftedP4_74x(METUncertainty shift, METCorrectionLevel level) const;
double shiftedSumEt_74x(METUncertainty shift, METCorrectionLevel level) const;

/// this below should be private but Reflex doesn't like it
class PackedMETUncertainty {
class UnpackedMETUncertainty {
// defined as C++ class so that I can change the packing without having to touch the code elsewhere
// the compiler should anyway inline everything whenever possible
public:
PackedMETUncertainty() : dpx_(0), dpy_(0), dsumEt_(0) {
pack();
unpack();
}
PackedMETUncertainty(float dpx, float dpy, float dsumEt) : dpx_(dpx), dpy_(dpy), dsumEt_(dsumEt) {
pack();
unpack();
}
double dpx() const {
if (!unpacked_)
unpack();
return dpx_;
}
double dpy() const {
if (!unpacked_)
unpack();
return dpy_;
}
double dsumEt() const {
if (!unpacked_)
unpack();
return dsumEt_;
}
UnpackedMETUncertainty() : dpx_(0), dpy_(0), dsumEt_(0) {}
UnpackedMETUncertainty(float dpx, float dpy, float dsumEt) : dpx_(dpx), dpy_(dpy), dsumEt_(dsumEt) {}
double dpx() const { return dpx_; }
double dpy() const { return dpy_; }
double dsumEt() const { return dsumEt_; }
void set(float dpx, float dpy, float dsumEt) {
dpx_ = dpx;
dpy_ = dpy;
dsumEt_ = dsumEt;
pack();
unpack();
}
void add(float dpx, float dpy, float dsumEt) {
dpx_ += dpx;
dpy_ += dpy;
dsumEt_ += dsumEt;
}
void unpack() const;
void pack();

private:
float dpx_, dpy_, dsumEt_;
};

/// this below should be private but Reflex doesn't like it
class PackedMETUncertainty {
// defined as C++ class so that I can change the packing without having to touch the code elsewhere
// the compiler should anyway inline everything whenever possible
public:
PackedMETUncertainty() { pack(0, 0, 0); }
PackedMETUncertainty(float dpx, float dpy, float dsumEt) { pack(dpx, dpy, dsumEt); }
void set(float dpx, float dpy, float dsumEt) { pack(dpx, dpy, dsumEt); }
UnpackedMETUncertainty unpack() const;

float unpackDSumEt() const;
float unpackDpx() const;
float unpackDpy() const;

protected:
mutable float dpx_, dpy_, dsumEt_;
mutable bool unpacked_;
void pack(float dpx, float dpy, float dsumEt);
uint16_t packedDpx_, packedDpy_, packedDSumEt_;
};

Expand All @@ -328,7 +322,7 @@ namespace pat {
// MET sumPtUnclustered for MET Significance
double sumPtUnclustered_;

const PackedMETUncertainty findMETTotalShift(MET::METCorrectionLevel cor, MET::METUncertainty shift) const;
UnpackedMETUncertainty findMETTotalShift(MET::METCorrectionLevel cor, MET::METUncertainty shift) const;

std::map<MET::METCorrectionLevel, std::vector<MET::METCorrectionType> > corMap_;
void initCorMap();
Expand Down
80 changes: 47 additions & 33 deletions DataFormats/PatCandidates/src/MET.cc
Expand Up @@ -205,19 +205,18 @@ void MET::initCorMap() {
corMap_[MET::RawDeepResolutionTune] = tmpDeepResolution;
}

const MET::PackedMETUncertainty MET::findMETTotalShift(MET::METCorrectionLevel cor, MET::METUncertainty shift) const {
MET::UnpackedMETUncertainty MET::findMETTotalShift(MET::METCorrectionLevel cor, MET::METUncertainty shift) const {
//find corrections shifts =============================
std::map<MET::METCorrectionLevel, std::vector<MET::METCorrectionType> >::const_iterator itCor_ = corMap_.find(cor);
if (itCor_ == corMap_.end())
throw cms::Exception("Unsupported", "Specified MET correction scheme does not exist");

bool isSmeared = false;
MET::PackedMETUncertainty totShift;
MET::UnpackedMETUncertainty totShift;
unsigned int scor = itCor_->second.size();
for (unsigned int i = 0; i < scor; i++) {
totShift.add(corrections_[itCor_->second[i]].dpx(),
corrections_[itCor_->second[i]].dpy(),
corrections_[itCor_->second[i]].dsumEt());
auto up = corrections_[itCor_->second[i]].unpack();
totShift.add(up.dpx(), up.dpy(), up.dsumEt());

if (itCor_->first >= MET::Type1Smear)
isSmeared = true;
Expand All @@ -233,7 +232,8 @@ const MET::PackedMETUncertainty MET::findMETTotalShift(MET::METCorrectionLevel c
if (isSmeared && shift <= MET::JetResDown)
shift = (MET::METUncertainty)(MET::METUncertaintySize + shift + 1);

totShift.add(uncertainties_[shift].dpx(), uncertainties_[shift].dpy(), uncertainties_[shift].dsumEt());
auto up = uncertainties_[shift].unpack();
totShift.add(up.dpx(), up.dpy(), up.dsumEt());

return totShift;
}
Expand All @@ -248,11 +248,11 @@ MET::Vector2 MET::shiftedP2(MET::METUncertainty shift, MET::METCorrectionLevel c
if (cor != MET::METCorrectionLevel::RawCalo) {
vo = shiftedP2_74x(shift, cor);
} else {
Vector2 ret{caloPackedMet_.dpx(), caloPackedMet_.dpy()};
Vector2 ret{caloPackedMet_.unpackDpx(), caloPackedMet_.unpackDpy()};
vo = ret;
}
} else {
const MET::PackedMETUncertainty &v = findMETTotalShift(cor, shift);
auto v = findMETTotalShift(cor, shift);
Vector2 ret{(px() + v.dpx()), (py() + v.dpy())};
//return ret;
vo = ret;
Expand All @@ -269,11 +269,11 @@ MET::Vector MET::shiftedP3(MET::METUncertainty shift, MET::METCorrectionLevel co
if (cor != MET::METCorrectionLevel::RawCalo) {
vo = shiftedP3_74x(shift, cor);
} else {
Vector tmp(caloPackedMet_.dpx(), caloPackedMet_.dpy(), 0);
Vector tmp(caloPackedMet_.unpackDpx(), caloPackedMet_.unpackDpy(), 0);
vo = tmp;
}
} else {
const MET::PackedMETUncertainty &v = findMETTotalShift(cor, shift);
const MET::UnpackedMETUncertainty &v = findMETTotalShift(cor, shift);
//return Vector(px() + v.dpx(), py() + v.dpy(), 0);
Vector tmp(px() + v.dpx(), py() + v.dpy(), 0);
vo = tmp;
Expand All @@ -290,12 +290,12 @@ MET::LorentzVector MET::shiftedP4(METUncertainty shift, MET::METCorrectionLevel
if (cor != MET::METCorrectionLevel::RawCalo) {
vo = shiftedP4_74x(shift, cor);
} else {
double x = caloPackedMet_.dpx(), y = caloPackedMet_.dpy();
double x = caloPackedMet_.unpackDpx(), y = caloPackedMet_.unpackDpy();
LorentzVector tmp(x, y, 0, std::hypot(x, y));
vo = tmp;
}
} else {
const MET::PackedMETUncertainty &v = findMETTotalShift(cor, shift);
const auto v = findMETTotalShift(cor, shift);
double x = px() + v.dpx(), y = py() + v.dpy();
//return LorentzVector(x, y, 0, std::hypot(x,y));
LorentzVector tmp(x, y, 0, std::hypot(x, y));
Expand All @@ -313,10 +313,10 @@ double MET::shiftedSumEt(MET::METUncertainty shift, MET::METCorrectionLevel cor)
if (cor != MET::METCorrectionLevel::RawCalo) {
sumEto = shiftedSumEt_74x(shift, cor);
} else {
sumEto = caloPackedMet_.dsumEt();
sumEto = caloPackedMet_.unpackDSumEt();
}
} else {
const MET::PackedMETUncertainty &v = findMETTotalShift(cor, shift);
const auto v = findMETTotalShift(cor, shift);
//return sumEt() + v.dsumEt();
sumEto = sumEt() + v.dsumEt();
}
Expand All @@ -343,8 +343,9 @@ void MET::setUncShift(double px, double py, double sumEt, METUncertainty shift,
// which is performed independently
shift = (MET::METUncertainty)(METUncertainty::METUncertaintySize + shift + 1);
const PackedMETUncertainty &ref = corrections_[METCorrectionType::Smear];
uncertainties_[shift].set(
px - ref.dpx() - this->px(), py - ref.dpy() - this->py(), sumEt - ref.dsumEt() - this->sumEt());
uncertainties_[shift].set(px - ref.unpackDpx() - this->px(),
py - ref.unpackDpy() - this->py(),
sumEt - ref.unpackDSumEt() - this->sumEt());
} else
uncertainties_[shift].set(px - this->px(), py - this->py(), sumEt - this->sumEt());
}
Expand Down Expand Up @@ -379,9 +380,11 @@ MET::Vector2 MET::shiftedP2_74x(MET::METUncertainty shift, MET::METCorrectionLev
throw cms::Exception(
"Unsupported",
"MET uncertainties not available for the specified correction type (only central value available)\n");
return Vector2{(px() + v.front().dpx()), (py() + v.front().dpy())};
auto const &p = v.front();
return Vector2{(px() + p.unpackDpx()), (py() + p.unpackDpy())};
}
Vector2 ret{(px() + v[shift].dpx()), (py() + v[shift].dpy())};
auto const &p = v[shift];
Vector2 ret{(px() + p.unpackDpx()), (py() + p.unpackDpy())};
return ret;
}

Expand All @@ -396,9 +399,11 @@ MET::Vector MET::shiftedP3_74x(MET::METUncertainty shift, MET::METCorrectionLeve
throw cms::Exception(
"Unsupported",
"MET uncertainties not available for the specified correction type (only central value available)\n");
return Vector(px() + v.front().dpx(), py() + v.front().dpy(), 0);
auto const &p = v.front();
return Vector(px() + p.unpackDpx(), py() + p.unpackDpy(), 0);
}
return Vector(px() + v[shift].dpx(), py() + v[shift].dpy(), 0);
auto const &p = v[shift];
return Vector(px() + p.unpackDpx(), py() + p.unpackDpy(), 0);
}

MET::LorentzVector MET::shiftedP4_74x(METUncertainty shift, MET::METCorrectionLevel level) const {
Expand All @@ -412,10 +417,12 @@ MET::LorentzVector MET::shiftedP4_74x(METUncertainty shift, MET::METCorrectionLe
throw cms::Exception(
"Unsupported",
"MET uncertainties not available for the specified correction type (only central value available)\n");
double x = px() + v.front().dpx(), y = py() + v.front().dpy();
auto const &p = v.front();
double x = px() + p.unpackDpx(), y = py() + p.unpackDpy();
return LorentzVector(x, y, 0, std::hypot(x, y));
}
double x = px() + v[shift].dpx(), y = py() + v[shift].dpy();
auto const &p = v[shift];
double x = px() + p.unpackDpx(), y = py() + p.unpackDpy();
return LorentzVector(x, y, 0, std::hypot(x, y));
}

Expand All @@ -430,21 +437,28 @@ double MET::shiftedSumEt_74x(MET::METUncertainty shift, MET::METCorrectionLevel
throw cms::Exception(
"Unsupported",
"MET uncertainties not available for the specified correction type (only central value available)\n");
return sumEt() + v.front().dsumEt();
return sumEt() + v.front().unpackDSumEt();
}
return sumEt() + v[shift].dsumEt();
return sumEt() + v[shift].unpackDSumEt();
}

#include "DataFormats/Math/interface/libminifloat.h"

void MET::PackedMETUncertainty::pack() {
packedDpx_ = MiniFloatConverter::float32to16(dpx_);
packedDpy_ = MiniFloatConverter::float32to16(dpy_);
packedDSumEt_ = MiniFloatConverter::float32to16(dsumEt_);
MET::UnpackedMETUncertainty MET::PackedMETUncertainty::unpack() const {
auto dpx = MiniFloatConverter::float16to32(packedDpx_);
auto dpy = MiniFloatConverter::float16to32(packedDpy_);
auto dsumEt = MiniFloatConverter::float16to32(packedDSumEt_);
return UnpackedMETUncertainty(dpx, dpy, dsumEt);
}
void MET::PackedMETUncertainty::unpack() const {
unpacked_ = true;
dpx_ = MiniFloatConverter::float16to32(packedDpx_);
dpy_ = MiniFloatConverter::float16to32(packedDpy_);
dsumEt_ = MiniFloatConverter::float16to32(packedDSumEt_);

float MET::PackedMETUncertainty::unpackDpx() const { return MiniFloatConverter::float16to32(packedDpx_); }

float MET::PackedMETUncertainty::unpackDpy() const { return MiniFloatConverter::float16to32(packedDpy_); }

float MET::PackedMETUncertainty::unpackDSumEt() const { return MiniFloatConverter::float16to32(packedDSumEt_); }

void MET::PackedMETUncertainty::pack(float dpx, float dpy, float dsumEt) {
packedDpx_ = MiniFloatConverter::float32to16(dpx);
packedDpy_ = MiniFloatConverter::float32to16(dpy);
packedDSumEt_ = MiniFloatConverter::float32to16(dsumEt);
}
8 changes: 0 additions & 8 deletions DataFormats/PatCandidates/src/classes_def_objects.xml
Expand Up @@ -289,15 +289,7 @@
<class name="pat::MET::PackedMETUncertainty" ClassVersion="11">
<version ClassVersion="11" checksum="3523936012"/>
<version ClassVersion="10" checksum="1984780659"/>
<field name="dpx_" transient="true" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understanding, removing these fields (up to line 300) doesn't break back compatibility? What does transient="true" supposed to do? Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understanding, removing these fields (up to line 300) doesn't break back compatibility?

I believe that to be correct.

What does transient="true" supposed to do?

It tells ROOT not to store those variables. This is why dropping them from the class doesn't affect read back of old files.

<field name="dpy_" transient="true" />
<field name="dsumEt_" transient="true" />
<field name="unpacked_" transient="true" />

</class>
<ioread sourceClass="pat::MET::PackedMETUncertainty" version="[1-]" targetClass="pat::MET::PackedMETUncertainty" source="" target="unpacked_">
<![CDATA[unpacked_ = false; ]]>
</ioread>
<class name="pat::MET::Vector2" transient="true" />

<class name="std::vector<pat::MET::PackedMETUncertainty>" />
Expand Down