Skip to content

Commit

Permalink
Merge pull request cms-sw#13 from lgray/VID3_validation_74X
Browse files Browse the repository at this point in the history
Vid3 validation 74 x
  • Loading branch information
ikrav committed Jun 18, 2015
2 parents 34be80f + 13a6cdd commit 9310ac9
Show file tree
Hide file tree
Showing 46 changed files with 774 additions and 5 deletions.
60 changes: 60 additions & 0 deletions DataFormats/PatCandidates/interface/VIDCutFlowResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef __DataFormats_PatCandidates_VIDResult_H__
#define __DataFormats_PatCandidates_VIDResult_H__

#include <unordered_map>
#include <vector>
#include <string>

namespace vid {
class CutFlowResult {
public:
template<class T> friend class VersionedSelector;

CutFlowResult() : bitmap_(0) {}
CutFlowResult(const std::string& name,
const std::unordered_map<std::string,unsigned>& n2idx,
unsigned bitmap,
const std::vector<double>& values) :
name_(name),
bitmap_(bitmap),
values_(values),
name_to_index_(n2idx) {}

const std::string& cutFlowName() const { return name_; }
bool cutFlowPassed() const {
const unsigned all_pass = name_to_index_.size()-1;
return (all_pass&bitmap_) == all_pass;
}
size_t cutFlowSize() const { return name_to_index_.size(); }

const std::string& getNameAtIndex(const unsigned idx) const;

bool getCutResultByIndex(const unsigned idx) const;
bool getCutResultByName(const std::string& name) const;

double getValueCutUpon(const unsigned idx) const;
double getValueCutUpon(const std::string& name) const;

CutFlowResult getCutFlowResultMasking(const unsigned idx) const;
CutFlowResult getCutFlowResultMasking(const std::string& name) const;

CutFlowResult getCutFlowResultMasking(const std::vector<unsigned>& idxs) const;
CutFlowResult getCutFlowResultMasking(const std::vector<std::string>& names) const;

private:
std::string name_;
unsigned bitmap_;
std::vector<double> values_;
std::unordered_map<std::string,unsigned> name_to_index_;

bool getCutBit(const unsigned idx) const {
return (bool)(0x1&(bitmap_>>idx));
}

bool getCutValue(const unsigned idx) const {
return values_[idx];
}
};
}

#endif
105 changes: 105 additions & 0 deletions DataFormats/PatCandidates/src/VIDCutFlowResult.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "DataFormats/PatCandidates/interface/VIDCutFlowResult.h"
#include "FWCore/Utilities/interface/Exception.h"

namespace {
static const std::string empty_str("");
}

namespace vid {
const std::string& CutFlowResult::getNameAtIndex(const unsigned idx) const {
for( const auto& value : name_to_index_ ) {
if( value.second == idx ) return value.first;
}
throw cms::Exception("IndexNotFound")
<< "index = " << idx << " has no corresponding cut name!";
return empty_str;
}

bool CutFlowResult::getCutResultByIndex(const unsigned idx) const {
if( idx >= name_to_index_.size() ) {
throw cms::Exception("OutOfBounds")
<< idx << " is out of bounds for this cut flow!";
}
return getCutBit(idx);
}

bool CutFlowResult::getCutResultByName(const std::string& name) const {
auto idx = name_to_index_.find(name);
if( idx == name_to_index_.end() ) {
throw cms::Exception("UnknownName")
<< "Cut name: " << name
<< " is not known for this cutflow!";
}
return getCutBit(idx->second);
}

double CutFlowResult::getValueCutUpon(const unsigned idx) const {
if( idx >= name_to_index_.size() ) {
throw cms::Exception("OutOfBounds")
<< idx << " is out of bounds for this cut flow!";
}
return getCutValue(idx);
}

double CutFlowResult::getValueCutUpon(const std::string& name) const {
auto idx = name_to_index_.find(name);
if( idx == name_to_index_.end() ) {
throw cms::Exception("UnknownName")
<< "Cut name: " << name
<< " is not known for this cutflow!";
}
return getCutValue(idx->second);
}

CutFlowResult CutFlowResult::
getCutFlowResultMasking(const std::vector<unsigned>& idxs) const {
unsigned bitmap = bitmap_;
for( const unsigned idx : idxs ) {
if( idx >= name_to_index_.size() ) {
throw cms::Exception("OutOfBounds")
<< idx << " is out of bounds for this cut flow!";
}
bitmap = bitmap | 1 << idx;
}
return CutFlowResult(name_,name_to_index_,bitmap,values_);
}

CutFlowResult CutFlowResult::
getCutFlowResultMasking(const std::vector<std::string>& names) const {
unsigned bitmap = bitmap_;
for( const std::string& name : names ) {
auto idx = name_to_index_.find(name);
if( idx == name_to_index_.end() ) {
throw cms::Exception("UnknownName")
<< "Cut name: " << name
<< " is not known for this cutflow!";
}
bitmap = bitmap | 1 << idx->second;
}
return CutFlowResult(name_,name_to_index_,bitmap,values_);
}

CutFlowResult CutFlowResult::
getCutFlowResultMasking(const unsigned idx) const {
unsigned bitmap = bitmap_;
if( idx >= name_to_index_.size() ) {
throw cms::Exception("OutOfBounds")
<< idx << " is out of bounds for this cut flow!";
}
bitmap = bitmap | 1 << idx;
return CutFlowResult(name_,name_to_index_,bitmap,values_);
}

CutFlowResult CutFlowResult::
getCutFlowResultMasking(const std::string& name) const {
unsigned bitmap = bitmap_;
auto idx = name_to_index_.find(name);
if( idx == name_to_index_.end() ) {
throw cms::Exception("UnknownName")
<< "Cut name: " << name
<< " is not known for this cutflow!";
}
bitmap = bitmap | 1 << idx->second;
return CutFlowResult(name_,name_to_index_,bitmap,values_);
}
}
7 changes: 7 additions & 0 deletions DataFormats/PatCandidates/src/classes_def_other.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
<class name="pat::CandKinResolutionValueMap" />
<class name="edm::Wrapper<pat::CandKinResolutionValueMap>" />

<class name="vid::CutFlowResult" ClassVersion="10">
<version ClassVersion="10" checksum="2936434398"/>
</class>
<class name="edm::ValueMap<vid::CutFlowResult>"/>
<class name="edm::Wrapper<vid::CutFlowResult>"/>
<class name="edm::Wrapper<edm::ValueMap<vid::CutFlowResult> >"/>

</selection>
<exclusion>
</exclusion>
Expand Down
7 changes: 7 additions & 0 deletions DataFormats/PatCandidates/src/classes_other.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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

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

namespace DataFormats_PatCandidates {
struct dictionaryother {

Expand Down Expand Up @@ -51,6 +53,11 @@ namespace DataFormats_PatCandidates {
pat::CandKinResolutionValueMap vm_ckr;
edm::Wrapper<pat::CandKinResolutionValueMap> w_vm_ckr;

vid::CutFlowResult vcfr;
edm::ValueMap<vid::CutFlowResult> vm_vcfr;
edm::Wrapper<vid::CutFlowResult> w_vcfr;
edm::Wrapper<edm::ValueMap<vid::CutFlowResult> > w_vm_vcfr;

};

}
4 changes: 4 additions & 0 deletions PhysicsTools/SelectorUtils/interface/CandidateCut.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace candidate_functions {
public:
virtual result_type operator()(const argument_type&) const = 0;
virtual ~CandidateCut() {}

virtual double value(const reco::CandidatePtr&) const = 0;

virtual const std::string& name() const = 0;
};
}

Expand Down
14 changes: 13 additions & 1 deletion PhysicsTools/SelectorUtils/interface/VersionedIdProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "DataFormats/PatCandidates/interface/VIDCutFlowResult.h"
#include "DataFormats/Common/interface/View.h"

#include "PhysicsTools/SelectorUtils/interface/VersionedSelector.h"
Expand Down Expand Up @@ -116,6 +117,7 @@ VersionedIdProducer(const edm::ParameterSet& iConfig) {
produces<edm::ValueMap<float> >(idname); // for PAT
produces<edm::ValueMap<unsigned> >(idname);
produces<edm::ValueMap<unsigned> >(idname+std::string(bitmap_label));
produces<edm::ValueMap<vid::CutFlowResult> >(idname);
}
}

Expand All @@ -134,16 +136,21 @@ produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
std::auto_ptr<edm::ValueMap<float> > outPassf(new edm::ValueMap<float>() );
std::auto_ptr<edm::ValueMap<unsigned> > outHowFar(new edm::ValueMap<unsigned>() );
std::auto_ptr<edm::ValueMap<unsigned> > outBitmap(new edm::ValueMap<unsigned>() );
std::auto_ptr<edm::ValueMap<vid::CutFlowResult> > out_cfrs(new edm::ValueMap<vid::CutFlowResult>() );

std::vector<bool> passfail;
std::vector<float> passfailf;
std::vector<unsigned> howfar;
std::vector<unsigned> bitmap;
std::vector<vid::CutFlowResult> cfrs;

for(size_t i = 0; i < physicsobjects.size(); ++i) {
auto po = physicsobjects.ptrAt(i);
passfail.push_back((*id)(po,iEvent));
passfailf.push_back(passfail.back());
howfar.push_back(id->howFarInCutFlow());
bitmap.push_back(id->bitMap());
cfrs.push_back(id->cutFlowResult());
}

edm::ValueMap<bool>::Filler fillerpassfail(*outPass);
Expand All @@ -162,13 +169,18 @@ produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
fillerbitmap.insert(physicsObjectsHandle, bitmap.begin(), bitmap.end() );
fillerbitmap.fill();

edm::ValueMap<vid::CutFlowResult>::Filler fillercfr(*out_cfrs);
fillercfr.insert(physicsObjectsHandle, cfrs.begin(), cfrs.end() );
fillercfr.fill();

iEvent.put(outPass,id->name());
iEvent.put(outPassf,id->name());
iEvent.put(outHowFar,id->name());
iEvent.put(outBitmap,id->name()+std::string(bitmap_label));
iEvent.put(out_cfrs,id->name());
iEvent.put(std::auto_ptr<std::string>(new std::string(id->md5String())),
id->name());

}
}

27 changes: 27 additions & 0 deletions PhysicsTools/SelectorUtils/interface/VersionedSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

namespace candf = candidate_functions;

namespace vid {
class CutFlowResult;
}

template<class T>
class VersionedSelector : public Selector<T> {
public:
Expand Down Expand Up @@ -70,6 +74,7 @@ class VersionedSelector : public Selector<T> {
virtual bool operator()( const T& ref, pat::strbitset& ret ) CINT_GUARD(override final) {
howfar_ = 0;
bitmap_ = 0;
values_.clear();
bool failed = false;
if( !initialized_ ) {
throw cms::Exception("CutNotInitialized")
Expand All @@ -78,6 +83,7 @@ class VersionedSelector : public Selector<T> {
for( unsigned i = 0; i < cuts_.size(); ++i ) {
reco::CandidatePtr temp(ref);
const bool result = (*cuts_[i])(temp);
values_.push_back(cuts_[i]->value(temp));
if( result || this->ignoreCut(cut_indices_[i]) ) {
this->passCut(ret,cut_indices_[i]);
bitmap_ |= 1<<i;
Expand Down Expand Up @@ -134,6 +140,8 @@ class VersionedSelector : public Selector<T> {

const size_t cutFlowSize() const { return cuts_.size(); }

vid::CutFlowResult cutFlowResult() const;

void initialize(const edm::ParameterSet&);

CINT_GUARD(void setConsumes(edm::ConsumesCollector));
Expand All @@ -144,6 +152,7 @@ class VersionedSelector : public Selector<T> {
std::vector<bool> needs_event_content_;
std::vector<typename Selector<T>::index_type> cut_indices_;
unsigned howfar_, bitmap_;
std::vector<double> values_;

private:
unsigned char id_md5_[MD5_DIGEST_LENGTH];
Expand Down Expand Up @@ -210,7 +219,25 @@ initialize( const edm::ParameterSet& conf ) {
initialized_ = true;
}



#ifdef REGULAR_CPLUSPLUS
#include "DataFormats/PatCandidates/interface/VIDCutFlowResult.h"
template<class T>
vid::CutFlowResult VersionedSelector<T>::cutFlowResult() const {
std::unordered_map<std::string,unsigned> names_to_index;
std::map<std::string,unsigned> cut_counter;
for( unsigned idx = 0; idx < cuts_.size(); ++idx ) {
const std::string& name = cuts_[idx]->name();
if( !cut_counter.count(name) ) cut_counter[name] = 0;
std::stringstream realname;
realname << name << "_" << cut_counter[name];
names_to_index.emplace(realname.str(),idx);
cut_counter[name]++;
}
return vid::CutFlowResult(name_,names_to_index,bitmap_,values_);
}

#include "PhysicsTools/SelectorUtils/interface/CutApplicatorWithEventContentBase.h"
template<class T>
void VersionedSelector<T>::setConsumes(edm::ConsumesCollector cc) {
Expand Down
6 changes: 5 additions & 1 deletion PhysicsTools/SelectorUtils/plugins/EtaMultiRangeCut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class EtaMultiRangeCut : public CutApplicatorBase {
}
}

double value(const reco::CandidatePtr& cand) const override final {
return ( _absEta ? std::abs(cand->eta()) : cand->eta() );
}

result_type asCandidate(const argument_type&) const override final;

private:
Expand All @@ -25,7 +29,7 @@ DEFINE_EDM_PLUGIN(CutApplicatorFactory,EtaMultiRangeCut,"EtaMultiRangeCut");

CutApplicatorBase::result_type
EtaMultiRangeCut::
asCandidate(const argument_type& cand) const{
asCandidate(const argument_type& cand) const {
const double the_eta = ( _absEta ? std::abs(cand->eta()) : cand->eta() );
bool result = false;
for(const auto& range : _ranges ) {
Expand Down
4 changes: 4 additions & 0 deletions PhysicsTools/SelectorUtils/plugins/MaxAbsEtaCut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class MaxAbsEtaCut : public CutApplicatorBase {
CutApplicatorBase(c),
_maxEta(c.getParameter<double>("maxEta")) { }

double value(const reco::CandidatePtr& cand) const override final {
return std::abs(cand->eta());
}

result_type asCandidate(const argument_type& cand) const override final {
return std::abs(cand->eta()) < _maxEta;
}
Expand Down
4 changes: 4 additions & 0 deletions PhysicsTools/SelectorUtils/plugins/MinPtCut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class MinPtCut : public CutApplicatorBase {
CutApplicatorBase(c),
_minPt(c.getParameter<double>("minPt")) { }

double value(const reco::CandidatePtr& cand) const override final {
return cand->pt();
}

result_type asCandidate(const argument_type& cand) const override final {
return cand->pt() > _minPt;
}
Expand Down
6 changes: 5 additions & 1 deletion PhysicsTools/SelectorUtils/plugins/MinPtCutInEtaRanges.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class MinPtCutInEtaRanges : public CutApplicatorBase {
_minPt.push_back(minPt);
}
}


double value(const reco::CandidatePtr& cand) const override final {
return cand->pt();
}

result_type asCandidate(const argument_type&) const override final;

private:
Expand Down
Loading

0 comments on commit 9310ac9

Please sign in to comment.