Skip to content

Commit

Permalink
Merge pull request #8354 from arizzi/metuncertinfloat16-from-CMSSW_7_…
Browse files Browse the repository at this point in the history
…5_X_2015-03-17-0200

MINIAOD: reducing packedMETunceratinty to float16 on top of #8219
  • Loading branch information
cmsbuild committed Mar 18, 2015
2 parents 09941db + 78ddc2d commit 885b2fa
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 190 deletions.
79 changes: 29 additions & 50 deletions DataFormats/PatCandidates/interface/MET.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef DataFormats_PatCandidates_MET_h
#define DataFormats_PatCandidates_MET_h


/**
\class pat::MET MET.h "DataFormats/PatCandidates/interface/MET.h"
\brief Analysis-level MET class
Expand Down Expand Up @@ -67,28 +68,10 @@ namespace pat {
/// set the associated GenMET
void setGenMET(const reco::GenMET & gm);

// ---- methods for MET corrections ----
//! uses internal info from mEtCorr
//! except for full uncorrection, how do you know which is which?
//! you don't,
//! present ordering:
//! 1: jet escale Type1 correction
//! 2: muon Type1 (?) correction
//! 3: tau Type1 (?) correction
unsigned int nCorrections() const;
enum UncorrectionType {
uncorrNONE = -1, //! do nothing
uncorrALL = 0, //! uncorrect to bare bones
uncorrJES, //! uncorrect for JES only
uncorrMUON, //! uncorrect for MUON only
uncorrTAU, //! uncorrect for TAU only
uncorrMAXN
};
float corEx(UncorrectionType ix = uncorrALL) const;
float corEy(UncorrectionType ix = uncorrALL) const;
float corSumEt(UncorrectionType ix = uncorrALL) const;
float uncorrectedPt(UncorrectionType ix = uncorrALL) const;
float uncorrectedPhi(UncorrectionType ix = uncorrALL) const;
// ---- methods for uncorrected MET ----
float uncorrectedPt() const;
float uncorrectedPhi() const;
float uncorrectedSumEt() const;

// ---- methods to know what the pat::MET was constructed from ----
/// True if this pat::MET was made from a reco::CaloMET
Expand Down Expand Up @@ -156,22 +139,13 @@ namespace pat {
}

// ---- members for MET corrections ----
struct UncorInfo {
UncorInfo(): corEx(0), corEy(0), corSumEt(0), pt(0), phi(0) {}
float corEx;
float corEy;
float corSumEt;
float pt;
float phi;
};

enum METUncertainty {
JetEnUp=0, JetEnDown=1, JetResUp=2, JetResDown=3,
MuonEnUp=4, MuonEnDown=5, ElectronEnUp=6, ElectronEnDown=7, TauEnUp=8,TauEnDown=9,
UnclusteredEnUp=10,UnclusteredEnDown=11, NoShift=12, METUncertaintySize=13
};
enum METUncertaintyLevel {
Raw=0, Type1=1, Type1p2=2
Raw=0, Type1=1, Type1p2=2, Calo=3
};
struct Vector2 {
double px, py;
Expand All @@ -189,19 +163,33 @@ namespace pat {

void setShift(double px, double py, double sumEt, METUncertainty shift, METUncertaintyLevel level=Type1) ;

// specific method to fill and retrieve the caloMET quickly from miniAODs,
//should be used by JetMET experts only for the beginning
//of the runII, will be discarded later once we are sure
//everything is fine
Vector2 caloMETP2() const;
double caloMETPt() const;
double caloMETPhi() const;
double caloMETSumEt() const;

/// 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() : dpx_(0), dpy_(0), dsumEt_(0) {}
PackedMETUncertainty(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; }
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_; }
void set(float dpx, float dpy, float dsumEt) { dpx_ = dpx; dpy_ = dpy; dsumEt_ = dsumEt; pack(); unpack();}
void unpack() const ;
void pack();

protected:
float dpx_, dpy_, dsumEt_;
mutable float dpx_, dpy_, dsumEt_;
mutable bool unpacked_;
uint16_t packedDpx_,packedDpy_,packedDSumEt_;
};
private:

Expand All @@ -212,22 +200,13 @@ namespace pat {
// ---- holder for pfMET specific info ---
std::vector<SpecificPFMETData> pfMET_;

// uncorrection transients
#if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
mutable std::atomic<std::vector<UncorInfo>*> uncorInfo_;
#else
mutable std::vector<UncorInfo>* uncorInfo_;
#endif
mutable unsigned int nCorrections_; //thread-safe protected by uncorInfo_

protected:

// ---- non-public correction utilities ----
void checkUncor_() const;
void setPtPhi_(UncorInfo& uci) const;

std::vector<PackedMETUncertainty> uncertaintiesRaw_, uncertaintiesType1_, uncertaintiesType1p2_;

PackedMETUncertainty caloPackedMet_;

};


Expand Down
171 changes: 58 additions & 113 deletions DataFormats/PatCandidates/src/MET.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ using namespace pat;


/// default constructor
MET::MET(): uncorInfo_(nullptr) {
MET::MET() {
}


/// constructor from reco::MET
MET::MET(const reco::MET & aMET) : PATObject<reco::MET>(aMET), uncorInfo_(nullptr) {
MET::MET(const reco::MET & aMET) : PATObject<reco::MET>(aMET) {
const reco::CaloMET * calo = dynamic_cast<const reco::CaloMET *>(&aMET);
if (calo != 0) caloMET_.push_back(calo->getSpecific());
const reco::PFMET * pf = dynamic_cast<const reco::PFMET *>(&aMET);
Expand All @@ -22,15 +22,15 @@ MET::MET(const reco::MET & aMET) : PATObject<reco::MET>(aMET), uncorInfo_(nullpt


/// constructor from ref to reco::MET
MET::MET(const edm::RefToBase<reco::MET> & aMETRef) : PATObject<reco::MET>(aMETRef), uncorInfo_(nullptr) {
MET::MET(const edm::RefToBase<reco::MET> & aMETRef) : PATObject<reco::MET>(aMETRef) {
const reco::CaloMET * calo = dynamic_cast<const reco::CaloMET *>(aMETRef.get());
if (calo != 0) caloMET_.push_back(calo->getSpecific());
const reco::PFMET * pf = dynamic_cast<const reco::PFMET *>(aMETRef.get());
if (pf != 0) pfMET_.push_back(pf->getSpecific());
}

/// constructor from ref to reco::MET
MET::MET(const edm::Ptr<reco::MET> & aMETRef) : PATObject<reco::MET>(aMETRef), uncorInfo_(nullptr) {
MET::MET(const edm::Ptr<reco::MET> & aMETRef) : PATObject<reco::MET>(aMETRef) {
const reco::CaloMET * calo = dynamic_cast<const reco::CaloMET *>(aMETRef.get());
if (calo != 0) caloMET_.push_back(calo->getSpecific());
const reco::PFMET * pf = dynamic_cast<const reco::PFMET *>(aMETRef.get());
Expand All @@ -43,35 +43,27 @@ PATObject<reco::MET>(iOther),
genMET_(iOther.genMET_),
caloMET_(iOther.caloMET_),
pfMET_(iOther.pfMET_),
uncorInfo_(nullptr)
{
auto tmp = iOther.uncorInfo_.load(std::memory_order_acquire);
if(tmp != nullptr) {
//Only thread-safe to read iOther.nCorrections_ if iOther.uncorInfo_ != nullptr
nCorrections_ = iOther.nCorrections_;
uncorInfo_.store( new std::vector<UncorInfo>{*tmp},std::memory_order_release);
}
uncertaintiesRaw_(iOther.uncertaintiesRaw_),
uncertaintiesType1_(iOther.uncertaintiesType1_),
uncertaintiesType1p2_(iOther.uncertaintiesType1p2_),
caloPackedMet_(iOther.caloPackedMet_) {
}

/// destructor
MET::~MET() {
delete uncorInfo_.load(std::memory_order_acquire);

}

MET& MET::operator=(MET const& iOther) {
PATObject<reco::MET>::operator=(iOther);
genMET_ = iOther.genMET_;
caloMET_ =iOther.caloMET_;
pfMET_ =iOther.pfMET_;
auto tmp = iOther.uncorInfo_.load(std::memory_order_acquire);
if(tmp != nullptr) {
//Only thread-safe to read iOther.nCorrections_ if iOther.uncorInfo_ != nullptr
nCorrections_ = iOther.nCorrections_;
delete uncorInfo_.exchange( new std::vector<UncorInfo>{*tmp},std::memory_order_acq_rel);
} else {
nCorrections_ = 0;
delete uncorInfo_.exchange(nullptr, std::memory_order_acq_rel);
}
uncertaintiesRaw_ = iOther.uncertaintiesRaw_;
uncertaintiesType1_ = iOther.uncertaintiesType1_;
uncertaintiesType1p2_ = iOther.uncertaintiesType1p2_;
caloPackedMet_ = iOther.caloPackedMet_;

return *this;
}

Expand All @@ -87,102 +79,18 @@ void MET::setGenMET(const reco::GenMET & gm) {
}

//! return uncorrrection related stuff
unsigned int MET::nCorrections() const { checkUncor_(); return nCorrections_; }
//unsigned int MET::nCorrections() const { checkUncor_(); return nCorrections_; }

float MET::corEx(UncorrectionType ix) const {
if (ix == uncorrNONE) return 0;
checkUncor_(); return (*uncorInfo_.load(std::memory_order_acquire))[ix].corEx;
}
float MET::corEy(UncorrectionType ix) const {
if (ix == uncorrNONE) return 0;
checkUncor_(); return (*uncorInfo_.load(std::memory_order_acquire))[ix].corEy;
}
float MET::corSumEt(UncorrectionType ix) const {
if (ix == uncorrNONE) return 0;
checkUncor_(); return (*uncorInfo_.load(std::memory_order_acquire))[ix].corSumEt;
float MET::uncorrectedPt() const {
return shiftedPt(MET::METUncertainty::NoShift, MET::METUncertaintyLevel::Raw);
}
float MET::uncorrectedPt(UncorrectionType ix) const {
if (ix == uncorrNONE) return pt();
checkUncor_(); return (*uncorInfo_.load(std::memory_order_acquire))[ix].pt;
float MET::uncorrectedPhi() const {
return shiftedPt(MET::METUncertainty::NoShift, MET::METUncertaintyLevel::Raw);
}
float MET::uncorrectedPhi(UncorrectionType ix) const {
if (ix == uncorrNONE) return phi();
checkUncor_(); return (*uncorInfo_.load(std::memory_order_acquire))[ix].phi;
}


//! check and set transients
void MET::checkUncor_() const {
if (uncorInfo_.load(std::memory_order_acquire)!=nullptr ) return;

const std::vector<CorrMETData>& corrs(mEtCorr());
const auto nCorrectionsTmp = corrs.size();

std::unique_ptr<std::vector<UncorInfo>> uncorInfoTmpPtr{ new std::vector<UncorInfo>{uncorrMAXN} };
auto& uncorInfoTmp = *uncorInfoTmpPtr;

UncorrectionType ix;

//! ugly
//! ALL
ix = uncorrALL;
uncorInfoTmp[ix] = UncorInfo();
for (unsigned int iC=0; iC < nCorrectionsTmp; ++iC){
uncorInfoTmp[ix].corEx += corrs[iC].mex;
uncorInfoTmp[ix].corEy += corrs[iC].mey;
uncorInfoTmp[ix].corSumEt += corrs[iC].sumet;
}
setPtPhi_(uncorInfoTmp[ix]);

//! JES
ix = uncorrJES;
uncorInfoTmp[ix] = UncorInfo();
if (nCorrectionsTmp >=1 ){
unsigned int iC = 0;
uncorInfoTmp[ix].corEx += corrs[iC].mex;
uncorInfoTmp[ix].corEy += corrs[iC].mey;
uncorInfoTmp[ix].corSumEt += corrs[iC].sumet;
}
setPtPhi_(uncorInfoTmp[ix]);

//! MUON
ix = uncorrMUON;
uncorInfoTmp[ix] = UncorInfo();
if (nCorrectionsTmp >=2 ){
unsigned int iC = 1;
uncorInfoTmp[ix].corEx += corrs[iC].mex;
uncorInfoTmp[ix].corEy += corrs[iC].mey;
uncorInfoTmp[ix].corSumEt += corrs[iC].sumet;
}
setPtPhi_(uncorInfoTmp[ix]);

//! TAU
ix = uncorrTAU;
uncorInfoTmp[ix] = UncorInfo();
if (nCorrectionsTmp >=3 ){
unsigned int iC = 2;
uncorInfoTmp[ix].corEx += corrs[iC].mex;
uncorInfoTmp[ix].corEy += corrs[iC].mey;
uncorInfoTmp[ix].corSumEt += corrs[iC].sumet;
}
setPtPhi_(uncorInfoTmp[ix]);

//The compare_exchange_strong guarantees that the new value of nCorrections_ will be seen by other
// threads
nCorrections_ = nCorrectionsTmp;

std::vector<UncorInfo>* expected=nullptr;
if(uncorInfo_.compare_exchange_strong(expected,uncorInfoTmpPtr.get(),std::memory_order_acq_rel)) {
uncorInfoTmpPtr.release();
}
float MET::uncorrectedSumEt() const {
return shiftedSumEt(MET::METUncertainty::NoShift, MET::METUncertaintyLevel::Raw);
}

void MET::setPtPhi_(UncorInfo& uci) const {
float lpx = px() - uci.corEx;
float lpy = py() - uci.corEy;
uci.pt = sqrt(lpx*lpx + lpy*lpy);
uci.phi = atan2(lpy, lpx);
}

MET::Vector2 MET::shiftedP2(MET::METUncertainty shift, MET::METUncertaintyLevel level) const {
const std::vector<PackedMETUncertainty> &v = (level == Type1 ? uncertaintiesType1_ : (level == Type1p2 ? uncertaintiesType1p2_ : uncertaintiesRaw_));
Expand All @@ -207,7 +115,44 @@ double MET::shiftedSumEt(MET::METUncertainty shift, MET::METUncertaintyLevel lev
return sumEt() + v[shift].dsumEt();
}
void MET::setShift(double px, double py, double sumEt, MET::METUncertainty shift, MET::METUncertaintyLevel level) {
if(level != Calo ) {
std::vector<PackedMETUncertainty> &v = (level == Type1 ? uncertaintiesType1_ : (level == Type1p2 ? uncertaintiesType1p2_ : uncertaintiesRaw_));
if (v.empty()) v.resize(METUncertaintySize);
v[shift].set(px - this->px(), py - this->py(), sumEt - this->sumEt());
} else {
caloPackedMet_.set(px, py, sumEt);
}
}

MET::Vector2 MET::caloMETP2() const {
Vector2 ret{ caloPackedMet_.dpx(), caloPackedMet_.dpy() };
return ret;
}

double MET::caloMETPt() const {
return caloMETP2().pt();
}

double MET::caloMETPhi() const {
return caloMETP2().phi();
}

double MET::caloMETSumEt() const {
return caloPackedMet_.dsumEt();
}

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

void MET::PackedMETUncertainty::pack() {
packedDpx_ = MiniFloatConverter::float32to16(dpx_);
packedDpy_ = MiniFloatConverter::float32to16(dpy_);
packedDSumEt_ = MiniFloatConverter::float32to16(dsumEt_);
}
void MET::PackedMETUncertainty::unpack() const {
unpacked_=true;
dpx_=MiniFloatConverter::float16to32(packedDpx_);
dpy_=MiniFloatConverter::float16to32(packedDpy_);
dsumEt_=MiniFloatConverter::float16to32(packedDSumEt_);

}

18 changes: 14 additions & 4 deletions DataFormats/PatCandidates/src/classes_def_objects.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,27 @@
<ioread sourceClass = "pat::Jet" version="[1-11]" targetClass="pat::Jet" source="int partonFlavour_" target="jetFlavourInfo_">
<![CDATA[jetFlavourInfo_ = reco::JetFlavourInfo(0,onfile.partonFlavour_);]]>
</ioread>
<class name="pat::MET" ClassVersion="13">
<class name="pat::MET" ClassVersion="14">
<version ClassVersion="14" checksum="1795935545"/>
<version ClassVersion="13" checksum="2368359386"/>
<version ClassVersion="12" checksum="1474251442"/>
<version ClassVersion="11" checksum="1829185007"/>
<version ClassVersion="10" checksum="1136648776"/>
<field name="uncorInfo_" transient="true"/>
<field name="nCorrections_" transient="true"/>
</class>
<class name="pat::MET::PackedMETUncertainty" ClassVersion="10">
<class name="pat::MET::PackedMETUncertainty" ClassVersion="11">
<version ClassVersion="11" checksum="3523936012"/>
<version ClassVersion="10" checksum="1984780659"/>
<field name="dpx_" transient="true" />
<field name="dpy_" transient="true" />
<field name="dsumEt_" transient="true" />
<field name="unpacked_" transient="true" />

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

<class name="std::vector<pat::MET::PackedMETUncertainty>" />
<class name="pat::MHT" ClassVersion="12">
<version ClassVersion="12" checksum="965016657"/>
Expand Down

0 comments on commit 885b2fa

Please sign in to comment.