Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8288 from ahinzmann/PATJetUpdater
New producer to update JEC and Userdata of pat::Jets
- Loading branch information
Showing
8 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// | ||
|
||
|
||
#include "PhysicsTools/PatAlgos/plugins/PATJetUpdater.h" | ||
|
||
#include "FWCore/MessageLogger/interface/MessageLogger.h" | ||
#include "FWCore/ParameterSet/interface/FileInPath.h" | ||
|
||
#include "DataFormats/Common/interface/ValueMap.h" | ||
#include "DataFormats/Common/interface/Association.h" | ||
#include "DataFormats/Candidate/interface/CandAssociation.h" | ||
|
||
#include "DataFormats/PatCandidates/interface/JetCorrFactors.h" | ||
|
||
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" | ||
|
||
#include "FWCore/Utilities/interface/transform.h" | ||
|
||
#include <vector> | ||
#include <memory> | ||
#include <algorithm> | ||
|
||
|
||
using namespace pat; | ||
|
||
|
||
PATJetUpdater::PATJetUpdater(const edm::ParameterSet& iConfig) : | ||
useUserData_(iConfig.exists("userData")) | ||
{ | ||
// initialize configurables | ||
jetsToken_ = consumes<edm::View<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);}); | ||
// 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> >(); | ||
} | ||
|
||
|
||
PATJetUpdater::~PATJetUpdater() { | ||
|
||
} | ||
|
||
|
||
void PATJetUpdater::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) | ||
{ | ||
// Get the vector of jets | ||
edm::Handle<edm::View<Jet> > jets; | ||
iEvent.getByToken(jetsToken_, jets); | ||
|
||
// read in the jet correction factors ValueMap | ||
std::vector<edm::ValueMap<JetCorrFactors> > jetCorrs; | ||
if (addJetCorrFactors_) { | ||
for ( size_t i = 0; i < jetCorrFactorsTokens_.size(); ++i ) { | ||
edm::Handle<edm::ValueMap<JetCorrFactors> > jetCorr; | ||
iEvent.getByToken(jetCorrFactorsTokens_[i], jetCorr); | ||
jetCorrs.push_back( *jetCorr ); | ||
} | ||
} | ||
|
||
// 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++) { | ||
|
||
// 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); | ||
|
||
if (addJetCorrFactors_) { | ||
unsigned int setindex = ajet.availableJECSets().size(); | ||
// Undo previous jet energy corrections | ||
ajet.setP4(ajet.correctedP4(0)); | ||
// add additional JetCorrs to the jet | ||
for ( unsigned int i=0; i<jetCorrFactorsTokens_.size(); ++i ) { | ||
const JetCorrFactors& jcf = jetCorrs[i][jetRef]; | ||
// uncomment for debugging | ||
// jcf.print(); | ||
ajet.addJECFactors(jcf); | ||
} | ||
std::vector<std::string> levels = jetCorrs[0][jetRef].correctionLabels(); | ||
if(std::find(levels.begin(), levels.end(), "L2L3Residual")!=levels.end()){ | ||
ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L2L3Residual"), JetCorrFactors::NONE, setindex); | ||
} | ||
else if(std::find(levels.begin(), levels.end(), "L3Absolute")!=levels.end()){ | ||
ajet.initializeJEC(jetCorrs[0][jetRef].jecLevel("L3Absolute"), JetCorrFactors::NONE, setindex); | ||
} | ||
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 ( useUserData_ ) { | ||
userDataHelper_.add( ajet, iEvent, iSetup ); | ||
} | ||
|
||
patJets->push_back(ajet); | ||
} | ||
|
||
// sort jets in pt | ||
std::sort(patJets->begin(), patJets->end(), pTComparator_); | ||
|
||
// put genEvt in Event | ||
iEvent.put(patJets); | ||
|
||
} | ||
|
||
// ParameterSet description for module | ||
void PATJetUpdater::fillDescriptions(edm::ConfigurationDescriptions & descriptions) | ||
{ | ||
edm::ParameterSetDescription iDesc; | ||
iDesc.setComment("PAT jet producer module"); | ||
|
||
// input source | ||
iDesc.add<edm::InputTag>("jetSource", edm::InputTag("no default"))->setComment("input collection"); | ||
|
||
// jet energy corrections | ||
iDesc.add<bool>("addJetCorrFactors", true); | ||
std::vector<edm::InputTag> emptyVInputTags; | ||
iDesc.add<std::vector<edm::InputTag> >("jetCorrFactorsSource", emptyVInputTags); | ||
|
||
// Check to see if the user wants to add user data | ||
edm::ParameterSetDescription userDataPSet; | ||
PATUserDataHelper<Jet>::fillDescription(userDataPSet); | ||
iDesc.addOptional("userData", userDataPSet); | ||
|
||
descriptions.add("PATJetUpdater", iDesc); | ||
} | ||
|
||
#include "FWCore/Framework/interface/MakerMacros.h" | ||
|
||
DEFINE_FWK_MODULE(PATJetUpdater); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// | ||
|
||
#ifndef PhysicsTools_PatAlgos_PATJetUpdater_h | ||
#define PhysicsTools_PatAlgos_PATJetUpdater_h | ||
|
||
/** | ||
\class pat::PATJetUpdater PATJetUpdater.h "PhysicsTools/PatAlgos/interface/PATJetUpdater.h" | ||
\brief Produces pat::Jet's | ||
The PATJetUpdater produces analysis-level pat::Jet's starting from | ||
a collection of pat::Jet's and updates information. | ||
\author Andreas Hinzmann | ||
\version $Id: PATJetUpdater.h,v 1.00 2014/03/11 18:13:54 srappocc Exp $ | ||
*/ | ||
|
||
|
||
#include "FWCore/Framework/interface/EDProducer.h" | ||
#include "FWCore/Framework/interface/Event.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSet.h" | ||
#include "FWCore/Utilities/interface/InputTag.h" | ||
#include "DataFormats/Common/interface/View.h" | ||
|
||
#include "CommonTools/Utils/interface/PtComparator.h" | ||
|
||
#include "DataFormats/PatCandidates/interface/Jet.h" | ||
|
||
#include "DataFormats/PatCandidates/interface/UserData.h" | ||
#include "PhysicsTools/PatAlgos/interface/PATUserDataHelper.h" | ||
|
||
namespace pat { | ||
|
||
class PATJetUpdater : public edm::EDProducer { | ||
|
||
public: | ||
|
||
explicit PATJetUpdater(const edm::ParameterSet & iConfig); | ||
~PATJetUpdater(); | ||
|
||
virtual void produce(edm::Event & iEvent, const edm::EventSetup& iSetup) override; | ||
|
||
static void fillDescriptions(edm::ConfigurationDescriptions & descriptions); | ||
|
||
private: | ||
|
||
// configurables | ||
edm::EDGetTokenT<edm::View<Jet> > jetsToken_; | ||
bool addJetCorrFactors_; | ||
std::vector<edm::EDGetTokenT<edm::ValueMap<JetCorrFactors> > > jetCorrFactorsTokens_; | ||
|
||
GreaterByPt<Jet> pTComparator_; | ||
|
||
bool useUserData_; | ||
pat::PATUserDataHelper<pat::Jet> userDataHelper_; | ||
|
||
}; | ||
|
||
|
||
} | ||
|
||
#endif |
15 changes: 15 additions & 0 deletions
15
PhysicsTools/PatAlgos/python/producersLayer1/jetUpdater_cff.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import FWCore.ParameterSet.Config as cms | ||
|
||
from PhysicsTools.PatAlgos.recoLayer0.jetCorrections_cff import * | ||
from PhysicsTools.PatAlgos.producersLayer1.jetUpdater_cfi import * | ||
|
||
patJetCorrFactorsUpdated = patJetCorrFactors.clone( | ||
src = cms.InputTag("slimmedJets"), | ||
primaryVertices = cms.InputTag("offlineSlimmedPrimaryVertices") | ||
) | ||
|
||
## for scheduled mode | ||
makePatJetsUpdated = cms.Sequence( | ||
patJetCorrFactorsUpdated * | ||
patJetsUpdated | ||
) |
33 changes: 33 additions & 0 deletions
33
PhysicsTools/PatAlgos/python/producersLayer1/jetUpdater_cfi.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import FWCore.ParameterSet.Config as cms | ||
|
||
patJetsUpdated = cms.EDProducer("PATJetUpdater", | ||
# input | ||
jetSource = cms.InputTag("slimmedJets"), | ||
# add user data | ||
userData = cms.PSet( | ||
# add custom classes here | ||
userClasses = cms.PSet( | ||
src = cms.VInputTag('') | ||
), | ||
# add doubles here | ||
userFloats = cms.PSet( | ||
src = cms.VInputTag('') | ||
), | ||
# add ints here | ||
userInts = cms.PSet( | ||
src = cms.VInputTag('') | ||
), | ||
# add candidate ptrs here | ||
userCands = cms.PSet( | ||
src = cms.VInputTag('') | ||
), | ||
# add "inline" functions here | ||
userFunctions = cms.vstring(), | ||
userFunctionLabels = cms.vstring() | ||
), | ||
# jet energy corrections | ||
addJetCorrFactors = cms.bool(True), | ||
jetCorrFactorsSource = cms.VInputTag(cms.InputTag("patJetCorrFactorsUpdated") ), | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# This configuration is an example that recalibrates the slimmedJets from MiniAOD | ||
# and adds a new userfloat "oldJetMass" to it | ||
|
||
import FWCore.ParameterSet.Config as cms | ||
|
||
process = cms.Process("PATUPDATE") | ||
process.load("FWCore.MessageLogger.MessageLogger_cfi") | ||
|
||
from PhysicsTools.PatAlgos.patInputFiles_cff import filesRelValTTbarPileUpMINIAODSIM | ||
process.source = cms.Source("PoolSource", | ||
fileNames = filesRelValTTbarPileUpMINIAODSIM | ||
) | ||
|
||
process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(100) ) | ||
|
||
process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff") | ||
from Configuration.AlCa.GlobalTag import GlobalTag | ||
process.GlobalTag = GlobalTag(process.GlobalTag, 'auto:run2_mc') | ||
|
||
process.load("PhysicsTools.PatAlgos.producersLayer1.jetUpdater_cff") | ||
|
||
# An example where the jet energy correction are updated to the GlobalTag given above | ||
# and a usedfloat containing the previous mass of the jet is added | ||
from RecoJets.Configuration.RecoPFJets_cff import ak8PFJetsCHSSoftDropMass | ||
process.oldJetMass = ak8PFJetsCHSSoftDropMass.clone( | ||
src = cms.InputTag("slimmedJets"), | ||
matched = cms.InputTag("slimmedJets") ) | ||
process.patJetsUpdated.userData.userFloats.src += ['oldJetMass'] | ||
process.p = cms.Path( process.oldJetMass + process.makePatJetsUpdated ) | ||
|
||
# An example where the jet correction is undone | ||
process.patJetCorrFactorsUndoJEC = process.patJetCorrFactorsUpdated.clone( | ||
src = cms.InputTag("patJetsUpdated"), | ||
levels = [] ) | ||
process.patJetsUndoJEC = process.patJetsUpdated.clone( | ||
jetSource = cms.InputTag("patJetsUpdated"), | ||
jetCorrFactorsSource = cms.VInputTag(cms.InputTag("patJetCorrFactorsUndoJEC")) | ||
) | ||
process.patJetsUndoJEC.userData.userFloats.src = [] | ||
process.p += cms.Sequence( process.patJetCorrFactorsUndoJEC + process. patJetsUndoJEC ) | ||
|
||
# An example where the jet correction are reapplied | ||
process.patJetCorrFactorsReapplyJEC = process.patJetCorrFactorsUpdated.clone( | ||
src = cms.InputTag("patJetsUndoJEC"), | ||
levels = ['L1FastJet', | ||
'L2Relative', | ||
'L3Absolute'] ) | ||
process.patJetsReapplyJEC = process.patJetsUpdated.clone( | ||
jetSource = cms.InputTag("patJetsUndoJEC"), | ||
jetCorrFactorsSource = cms.VInputTag(cms.InputTag("patJetCorrFactorsReapplyJEC")) | ||
) | ||
process.patJetsReapplyJEC.userData.userFloats.src = [] | ||
process.p += cms.Sequence( process.patJetCorrFactorsReapplyJEC + process. patJetsReapplyJEC ) | ||
|
||
process.out = cms.OutputModule("PoolOutputModule", | ||
fileName = cms.untracked.string("patTupleUpdated.root"), | ||
outputCommands = cms.untracked.vstring('keep *') | ||
) | ||
|
||
process.endpath = cms.EndPath(process.out) | ||
|