Skip to content

Commit

Permalink
Merge pull request #12890 from cms-btv-pog/UpdatedJetTools_from-CMSSW…
Browse files Browse the repository at this point in the history
…_7_6_3

Added updateJetCollection() function to jetTools.py and related changes to the PATJetUpdater and the pat::Jet class (76X)
  • Loading branch information
davidlange6 committed Jan 24, 2016
2 parents fda3b3d + 5f36e28 commit 61e2189
Show file tree
Hide file tree
Showing 11 changed files with 799 additions and 391 deletions.
17 changes: 9 additions & 8 deletions DataFormats/PatCandidates/src/Jet.cc
Expand Up @@ -352,23 +352,24 @@ const std::vector<std::pair<std::string, float> > & Jet::getPairDiscri() const {
/// get b discriminant from label name
float Jet::bDiscriminator(const std::string & aLabel) const {
float discriminator = -1000.;
const std::string & theLabel = ((aLabel == "" || aLabel == "default")) ? "trackCountingHighEffBJetTags" : aLabel;
for(unsigned int i=0; i!=pairDiscriVector_.size(); i++){
if(pairDiscriVector_[i].first == theLabel){
for(int i=(int(pairDiscriVector_.size())-1); i>=0; i--){
if(pairDiscriVector_[i].first == aLabel){
discriminator = pairDiscriVector_[i].second;
break;
}
}
return discriminator;
}

const reco::BaseTagInfo * Jet::tagInfo(const std::string &label) const {
std::vector<std::string>::const_iterator it = std::find(tagInfoLabels_.begin(), tagInfoLabels_.end(), label);
if (it != tagInfoLabels_.end()) {
if ( tagInfosFwdPtr_.size() > 0 ) return tagInfosFwdPtr_[it - tagInfoLabels_.begin()].get();
else if ( tagInfos_.size() > 0 ) return & tagInfos_[it - tagInfoLabels_.begin()];
for(int i=(int(tagInfoLabels_.size())-1); i>=0; i--){
if (tagInfoLabels_[i] == label) {
if ( tagInfosFwdPtr_.size() > 0 ) return tagInfosFwdPtr_[i].get();
else if ( tagInfos_.size() > 0 ) return & tagInfos_[i];
return 0;
}
return 0;
}
return 0;
}


Expand Down
12 changes: 6 additions & 6 deletions PhysicsTools/PatAlgos/plugins/PATJetProducer.cc
Expand Up @@ -41,7 +41,8 @@ using namespace pat;


PATJetProducer::PATJetProducer(const edm::ParameterSet& iConfig) :
useUserData_(iConfig.exists("userData"))
useUserData_(iConfig.exists("userData")),
printWarning_(true)
{
// initialize configurables
jetsToken_ = consumes<edm::View<reco::Jet> >(iConfig.getParameter<edm::InputTag>( "jetSource" ));
Expand Down Expand Up @@ -220,7 +221,6 @@ void PATJetProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup
edm::RefProd<reco::PFCandidateCollection > h_pfCandidatesOut = iEvent.getRefBeforePut<reco::PFCandidateCollection > ( "pfCandidates" );
edm::RefProd<edm::OwnVector<reco::BaseTagInfo> > h_tagInfosOut = iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo> > ( "tagInfos" );

bool first=true; // this is introduced to issue warnings only for the first jet
for (edm::View<reco::Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {

// construct the Jet from the ref -> save ref to original object
Expand Down Expand Up @@ -292,10 +292,10 @@ void PATJetProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup
}
else{
ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"));
if(first){
edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the correction applied jetCorrFactors \n"
<< "of module " << jetCorrs[0][jetRef].jecSet() << " jets will remain"
<< " uncorrected."; first=false;
if(printWarning_){
edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the jetCorrFactors\n"
<< "of module " << jetCorrs[0][jetRef].jecSet() << ". Jets will remain"
<< " uncorrected."; printWarning_=false;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions PhysicsTools/PatAlgos/plugins/PATJetProducer.h
Expand Up @@ -104,6 +104,8 @@ namespace pat {

bool useUserData_;
pat::PATUserDataHelper<pat::Jet> userDataHelper_;
//
bool printWarning_; // this is introduced to issue warnings only once per job



Expand Down
130 changes: 116 additions & 14 deletions PhysicsTools/PatAlgos/plugins/PATJetUpdater.cc
Expand Up @@ -26,18 +26,54 @@ using namespace pat;


PATJetUpdater::PATJetUpdater(const edm::ParameterSet& iConfig) :
useUserData_(iConfig.exists("userData"))
useUserData_(iConfig.exists("userData")),
printWarning_(true)
{
// initialize configurables
jetsToken_ = consumes<edm::View<Jet> >(iConfig.getParameter<edm::InputTag>( "jetSource" ));
jetsToken_ = consumes<edm::View<reco::Jet> >(iConfig.getParameter<edm::InputTag>( "jetSource" ));
addJetCorrFactors_ = iConfig.getParameter<bool>( "addJetCorrFactors" );
jetCorrFactorsTokens_ = edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >( "jetCorrFactorsSource" ), [this](edm::InputTag const & tag){return mayConsume<edm::ValueMap<JetCorrFactors> >(tag);});
if( addJetCorrFactors_ ) {
jetCorrFactorsTokens_ = edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >( "jetCorrFactorsSource" ), [this](edm::InputTag const & tag){return mayConsume<edm::ValueMap<JetCorrFactors> >(tag);});
}
addBTagInfo_ = iConfig.getParameter<bool>( "addBTagInfo" );
addDiscriminators_ = iConfig.getParameter<bool>( "addDiscriminators" );
discriminatorTags_ = iConfig.getParameter<std::vector<edm::InputTag> >( "discriminatorSources" );
discriminatorTokens_ = edm::vector_transform(discriminatorTags_, [this](edm::InputTag const & tag){return mayConsume<reco::JetFloatAssociation::Container>(tag);});
addTagInfos_ = iConfig.getParameter<bool>( "addTagInfos" );
tagInfoTags_ = iConfig.getParameter<std::vector<edm::InputTag> >( "tagInfoSources" );
tagInfoTokens_ =edm::vector_transform(tagInfoTags_, [this](edm::InputTag const & tag){return mayConsume<edm::View<reco::BaseTagInfo> >(tag);});
if (discriminatorTags_.empty()) {
addDiscriminators_ = false;
} else {
for (std::vector<edm::InputTag>::const_iterator it = discriminatorTags_.begin(), ed = discriminatorTags_.end(); it != ed; ++it) {
std::string label = it->label();
std::string::size_type pos = label.find("JetTags");
if ((pos != std::string::npos) && (pos != label.length() - 7)) {
label.erase(pos+7); // trim a tail after "JetTags"
}
discriminatorLabels_.push_back(label);
}
}
if (tagInfoTags_.empty()) {
addTagInfos_ = false;
} else {
for (std::vector<edm::InputTag>::const_iterator it = tagInfoTags_.begin(), ed = tagInfoTags_.end(); it != ed; ++it) {
std::string label = it->label();
std::string::size_type pos = label.find("TagInfos");
if ((pos != std::string::npos) && (pos != label.length() - 8)) {
label.erase(pos+8); // trim a tail after "TagInfos"
}
tagInfoLabels_.push_back(label);
}
}
if (!addBTagInfo_) { addDiscriminators_ = false; addTagInfos_ = false; }
// Check to see if the user wants to add user data
if ( useUserData_ ) {
userDataHelper_ = PATUserDataHelper<Jet>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
}
// produces vector of jets
produces<std::vector<Jet> >();
produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
}


Expand All @@ -49,7 +85,7 @@ PATJetUpdater::~PATJetUpdater() {
void PATJetUpdater::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
{
// Get the vector of jets
edm::Handle<edm::View<Jet> > jets;
edm::Handle<edm::View<reco::Jet> > jets;
iEvent.getByToken(jetsToken_, jets);

// read in the jet correction factors ValueMap
Expand All @@ -62,17 +98,36 @@ void PATJetUpdater::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
}
}

// Get the vector of jet tags with b-tagging info
std::vector<edm::Handle<reco::JetFloatAssociation::Container> > jetDiscriminators;
if (addBTagInfo_ && addDiscriminators_) {
jetDiscriminators.resize(discriminatorTokens_.size());
for (size_t i = 0; i < discriminatorTokens_.size(); ++i) {
iEvent.getByToken(discriminatorTokens_[i], jetDiscriminators[i]);
}
}
std::vector<edm::Handle<edm::View<reco::BaseTagInfo> > > jetTagInfos;
if (addBTagInfo_ && addTagInfos_) {
jetTagInfos.resize(tagInfoTokens_.size());
for (size_t i = 0; i < tagInfoTokens_.size(); ++i) {
iEvent.getByToken(tagInfoTokens_[i], jetTagInfos[i]);
}
}

// loop over jets
std::auto_ptr< std::vector<Jet> > patJets ( new std::vector<Jet>() );

bool first=true; // this is introduced to issue warnings only for the first jet
for (edm::View<Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {
std::auto_ptr<edm::OwnVector<reco::BaseTagInfo> > tagInfosOut ( new edm::OwnVector<reco::BaseTagInfo>() );

edm::RefProd<edm::OwnVector<reco::BaseTagInfo> > h_tagInfosOut = iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo> > ( "tagInfos" );

for (edm::View<reco::Jet>::const_iterator itJet = jets->begin(); itJet != jets->end(); itJet++) {

// construct the Jet from the ref -> save ref to original object
unsigned int idx = itJet - jets->begin();
edm::RefToBase<Jet> jetRef = jets->refAt(idx);
edm::Ptr<Jet> jetPtr = jets->ptrAt(idx);
Jet ajet(jetPtr);
edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
edm::Ptr<reco::Jet> jetPtr = jets->ptrAt(idx);
Jet ajet( edm::RefToBase<Jet>(jetRef.castTo<JetRef>()) );

if (addJetCorrFactors_) {
unsigned int setindex = ajet.availableJECSets().size();
Expand All @@ -94,14 +149,50 @@ void PATJetUpdater::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
}
else{
ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("Uncorrected"), JetCorrFactors::NONE, setindex);
if(first){
edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the correction applied jetCorrFactors \n"
<< "of module " << jetCorrs[0][jetRef].jecSet() << " jets will remain"
<< " uncorrected."; first=false;
if(printWarning_){
edm::LogWarning("L3Absolute not found") << "L2L3Residual and L3Absolute are not part of the jetCorrFactors\n"
<< "of module " << jetCorrs[0][jetRef].jecSet() << ". Jets will remain"
<< " uncorrected."; printWarning_=false;
}
}
}

// add b-tag info if available & required
if (addBTagInfo_) {
if (addDiscriminators_) {
for (size_t k=0; k<jetDiscriminators.size(); ++k) {
float value = (*jetDiscriminators[k])[jetRef];
ajet.addBDiscriminatorPair(std::make_pair(discriminatorLabels_[k], value));
}
}
if (addTagInfos_) {
for (size_t k=0; k<jetTagInfos.size(); ++k) {
const edm::View<reco::BaseTagInfo> & taginfos = *jetTagInfos[k];
// This is not associative, so we have to search the jet
edm::Ptr<reco::BaseTagInfo> match;
// Try first by 'same index'
if ((idx < taginfos.size()) && (taginfos[idx].jet() == jetRef)) {
match = taginfos.ptrAt(idx);
} else {
// otherwise fail back to a simple search
for (edm::View<reco::BaseTagInfo>::const_iterator itTI = taginfos.begin(), edTI = taginfos.end(); itTI != edTI; ++itTI) {
if (itTI->jet() == jetRef) { match = taginfos.ptrAt( itTI - taginfos.begin() ); break; }
}
}
if (match.isNonnull()) {
tagInfosOut->push_back( match->clone() );
// set the "forward" ptr to the thinned collection
edm::Ptr<reco::BaseTagInfo> tagInfoForwardPtr ( h_tagInfosOut.id(), &tagInfosOut->back(), tagInfosOut->size() - 1 );
// set the "backward" ptr to the original collection for association
edm::Ptr<reco::BaseTagInfo> tagInfoBackPtr ( match );
// make FwdPtr
TagInfoFwdPtrCollection::value_type tagInfoFwdPtr( tagInfoForwardPtr, tagInfoBackPtr ) ;
ajet.addTagInfo(tagInfoLabels_[k], tagInfoFwdPtr );
}
}
}
}

if ( useUserData_ ) {
userDataHelper_.add( ajet, iEvent, iSetup );
}
Expand All @@ -115,6 +206,8 @@ void PATJetUpdater::produce(edm::Event & iEvent, const edm::EventSetup & iSetup)
// put genEvt in Event
iEvent.put(patJets);

iEvent.put( tagInfosOut, "tagInfos" );

}

// ParameterSet description for module
Expand All @@ -126,11 +219,20 @@ void PATJetUpdater::fillDescriptions(edm::ConfigurationDescriptions & descriptio
// input source
iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection");

// tag info
iDesc.add<bool>("addTagInfos", true);
std::vector<edm::InputTag> emptyVInputTags;
iDesc.add<std::vector<edm::InputTag> >("tagInfoSources", emptyVInputTags);

// jet energy corrections
iDesc.add<bool>("addJetCorrFactors", true);
std::vector<edm::InputTag> emptyVInputTags;
iDesc.add<std::vector<edm::InputTag> >("jetCorrFactorsSource", emptyVInputTags);

// btag discriminator tags
iDesc.add<bool>("addBTagInfo",true);
iDesc.add<bool>("addDiscriminators", true);
iDesc.add<std::vector<edm::InputTag> >("discriminatorSources", emptyVInputTags);

// Check to see if the user wants to add user data
edm::ParameterSetDescription userDataPSet;
PATUserDataHelper<Jet>::fillDescription(userDataPSet);
Expand Down
14 changes: 13 additions & 1 deletion PhysicsTools/PatAlgos/plugins/PATJetUpdater.h
Expand Up @@ -45,14 +45,26 @@ namespace pat {
private:

// configurables
edm::EDGetTokenT<edm::View<Jet> > jetsToken_;
edm::EDGetTokenT<edm::View<reco::Jet> > jetsToken_;
bool addJetCorrFactors_;
std::vector<edm::EDGetTokenT<edm::ValueMap<JetCorrFactors> > > jetCorrFactorsTokens_;

bool addBTagInfo_;
bool addDiscriminators_;
std::vector<edm::InputTag> discriminatorTags_;
std::vector<edm::EDGetTokenT<reco::JetFloatAssociation::Container> > discriminatorTokens_;
std::vector<std::string> discriminatorLabels_;
bool addTagInfos_;
std::vector<edm::InputTag> tagInfoTags_;
std::vector<edm::EDGetTokenT<edm::View<reco::BaseTagInfo> > > tagInfoTokens_;
std::vector<std::string> tagInfoLabels_;

GreaterByPt<Jet> pTComparator_;

bool useUserData_;
pat::PATUserDataHelper<pat::Jet> userDataHelper_;
//
bool printWarning_; // this is introduced to issue warnings only once per job

};

Expand Down
Expand Up @@ -3,13 +3,13 @@
from PhysicsTools.PatAlgos.recoLayer0.jetCorrections_cff import *
from PhysicsTools.PatAlgos.producersLayer1.jetUpdater_cfi import *

patJetCorrFactorsUpdated = patJetCorrFactors.clone(
updatedPatJetCorrFactors = patJetCorrFactors.clone(
src = cms.InputTag("slimmedJets"),
primaryVertices = cms.InputTag("offlineSlimmedPrimaryVertices")
)

## for scheduled mode
makePatJetsUpdated = cms.Sequence(
patJetCorrFactorsUpdated *
patJetsUpdated
makeUpdatedPatJets = cms.Sequence(
updatedPatJetCorrFactors *
updatedPatJets
)
13 changes: 11 additions & 2 deletions PhysicsTools/PatAlgos/python/producersLayer1/jetUpdater_cfi.py
@@ -1,6 +1,6 @@
import FWCore.ParameterSet.Config as cms

patJetsUpdated = cms.EDProducer("PATJetUpdater",
updatedPatJets = cms.EDProducer("PATJetUpdater",
# input
jetSource = cms.InputTag("slimmedJets"),
# add user data
Expand All @@ -27,7 +27,16 @@
),
# jet energy corrections
addJetCorrFactors = cms.bool(True),
jetCorrFactorsSource = cms.VInputTag(cms.InputTag("patJetCorrFactorsUpdated") ),
jetCorrFactorsSource = cms.VInputTag(cms.InputTag("updatedPatJetCorrFactors") ),
# btag information
addBTagInfo = cms.bool(True), ## master switch
addDiscriminators = cms.bool(True), ## addition of btag discriminators
discriminatorSources = cms.VInputTag(),
# clone tag infos ATTENTION: these take lots of space!
# usually the discriminators from the default algos
# are sufficient
addTagInfos = cms.bool(False),
tagInfoSources = cms.VInputTag()
)


0 comments on commit 61e2189

Please sign in to comment.