CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Types | Private Attributes | Static Private Attributes
SmearedJetProducerT< T > Class Template Reference

#include <SmearedJetProducerT.h>

Inheritance diagram for SmearedJetProducerT< T >:
edm::stream::EDProducer<>

Public Member Functions

void produce (edm::Event &event, const edm::EventSetup &setup) override
 
 SmearedJetProducerT (const edm::ParameterSet &cfg)
 
- Public Member Functions inherited from edm::stream::EDProducer<>
 EDProducer ()=default
 
 EDProducer (const EDProducer &)=delete
 
bool hasAbilityToProduceInBeginLumis () const final
 
bool hasAbilityToProduceInBeginProcessBlocks () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndProcessBlocks () const final
 
bool hasAbilityToProduceInEndRuns () const final
 
const EDProduceroperator= (const EDProducer &)=delete
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 

Private Types

using JetCollection = std::vector< T >
 

Private Attributes

GreaterByPt< TjetPtComparator
 
bool m_debug
 
bool m_enabled
 
std::shared_ptr< pat::GenJetMatcherm_genJetMatcher
 
std::string m_jets_algo
 
std::string m_jets_algo_pt
 
JME::JetResolution::Token m_jets_algo_pt_token
 
JME::JetResolutionScaleFactor::Token m_jets_algo_token
 
edm::EDGetTokenT< JetCollectionm_jets_token
 
int m_nomVar
 
std::mt19937 m_random_generator
 
std::unique_ptr< JME::JetResolutionm_resolution_from_file
 
edm::EDGetTokenT< double > m_rho_token
 
std::unique_ptr< JME::JetResolutionScaleFactorm_scale_factor_from_file
 
Variation m_systematic_variation
 
std::string m_uncertaintySource
 
bool m_use_txt_files
 
bool m_useDeterministicSeed
 

Static Private Attributes

static constexpr const double MIN_JET_ENERGY = 1e-2
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer<>
using CacheTypes = CacheContexts< T... >
 
using GlobalCache = typename CacheTypes::GlobalCache
 
using HasAbility = AbilityChecker< T... >
 
using InputProcessBlockCache = typename CacheTypes::InputProcessBlockCache
 
using LuminosityBlockCache = typename CacheTypes::LuminosityBlockCache
 
using LuminosityBlockContext = LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCache >
 
using LuminosityBlockSummaryCache = typename CacheTypes::LuminosityBlockSummaryCache
 
using RunCache = typename CacheTypes::RunCache
 
using RunContext = RunContextT< RunCache, GlobalCache >
 
using RunSummaryCache = typename CacheTypes::RunSummaryCache
 

Detailed Description

template<typename T>
class SmearedJetProducerT< T >

Produce collection of "smeared" jets.

The aim of this correction is to account for the difference in jet energy resolution between Monte Carlo simulation and Data.

Author
Sébastien Brochet

Definition at line 97 of file SmearedJetProducerT.h.

Member Typedef Documentation

◆ JetCollection

template<typename T >
using SmearedJetProducerT< T >::JetCollection = std::vector<T>
private

Definition at line 98 of file SmearedJetProducerT.h.

Constructor & Destructor Documentation

◆ SmearedJetProducerT()

template<typename T >
SmearedJetProducerT< T >::SmearedJetProducerT ( const edm::ParameterSet cfg)
inlineexplicit

Definition at line 101 of file SmearedJetProducerT.h.

References looper::cfg, edm::errors::ConfigFileReadError, DOWN, deDxTools::esConsumes(), Exception, contentValuesFiles::fullPath, SmearedJetProducerT< T >::m_enabled, SmearedJetProducerT< T >::m_genJetMatcher, SmearedJetProducerT< T >::m_jets_algo, SmearedJetProducerT< T >::m_jets_algo_pt, SmearedJetProducerT< T >::m_jets_algo_pt_token, SmearedJetProducerT< T >::m_jets_algo_token, SmearedJetProducerT< T >::m_jets_token, SmearedJetProducerT< T >::m_nomVar, SmearedJetProducerT< T >::m_random_generator, SmearedJetProducerT< T >::m_resolution_from_file, SmearedJetProducerT< T >::m_rho_token, SmearedJetProducerT< T >::m_scale_factor_from_file, SmearedJetProducerT< T >::m_systematic_variation, SmearedJetProducerT< T >::m_uncertaintySource, SmearedJetProducerT< T >::m_use_txt_files, NOMINAL, fileCollector::seed, patPFMETCorrections_cff::skipGenMatching, AlCaHLTBitMon_QueryRunRegistry::string, UP, and patPFMETCorrections_cff::variation.

102  : m_enabled(cfg.getParameter<bool>("enabled")),
103  m_useDeterministicSeed(cfg.getParameter<bool>("useDeterministicSeed")),
104  m_debug(cfg.getUntrackedParameter<bool>("debug", false)) {
105  m_jets_token = consumes<JetCollection>(cfg.getParameter<edm::InputTag>("src"));
106 
107  if (m_enabled) {
108  m_rho_token = consumes<double>(cfg.getParameter<edm::InputTag>("rho"));
109 
110  m_use_txt_files = cfg.exists("resolutionFile") && cfg.exists("scaleFactorFile");
111 
112  if (m_use_txt_files) {
113  std::string resolutionFile = cfg.getParameter<edm::FileInPath>("resolutionFile").fullPath();
114  std::string scaleFactorFile = cfg.getParameter<edm::FileInPath>("scaleFactorFile").fullPath();
115 
116  m_resolution_from_file = std::make_unique<JME::JetResolution>(resolutionFile);
117  m_scale_factor_from_file = std::make_unique<JME::JetResolutionScaleFactor>(scaleFactorFile);
118  } else {
119  m_jets_algo = cfg.getParameter<std::string>("algo");
120  m_jets_algo_pt = cfg.getParameter<std::string>("algopt");
123  }
124 
125  std::uint32_t seed = cfg.getParameter<std::uint32_t>("seed");
126  m_random_generator = std::mt19937(seed);
127 
128  bool skipGenMatching = cfg.getParameter<bool>("skipGenMatching");
129  if (!skipGenMatching)
130  m_genJetMatcher = std::make_shared<pat::GenJetMatcher>(cfg, consumesCollector());
131 
132  std::int32_t variation = cfg.getParameter<std::int32_t>("variation");
133  m_uncertaintySource = cfg.getParameter<std::string>("uncertaintySource");
134  m_nomVar = 1;
135  if (variation == 0)
137  else if (variation == 1)
139  else if (variation == -1)
141  else if (variation == 101) {
143  m_nomVar = 1;
144  } else if (variation == -101) {
146  m_nomVar = -1;
147  } else
149  "Invalid value for 'variation' parameter. Only -1, 0, 1 or 101, -101 are supported.");
150  }
151 
152  produces<JetCollection>();
153  }
edm::EDGetTokenT< JetCollection > m_jets_token
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
JME::JetResolutionScaleFactor::Token m_jets_algo_token
std::unique_ptr< JME::JetResolution > m_resolution_from_file
edm::EDGetTokenT< double > m_rho_token
std::mt19937 m_random_generator
std::shared_ptr< pat::GenJetMatcher > m_genJetMatcher
std::unique_ptr< JME::JetResolutionScaleFactor > m_scale_factor_from_file
JME::JetResolution::Token m_jets_algo_pt_token

Member Function Documentation

◆ fillDescriptions()

template<typename T >
static void SmearedJetProducerT< T >::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
inlinestatic

Definition at line 155 of file SmearedJetProducerT.h.

References edm::ConfigurationDescriptions::addDefault(), submitPVResolutionJobs::desc, pat::GenJetMatcher::fillDescriptions(), eostools::move(), source, and AlCaHLTBitMon_QueryRunRegistry::string.

155  {
157 
158  desc.add<edm::InputTag>("src");
159  desc.add<bool>("enabled");
160  desc.add<edm::InputTag>("rho");
161  desc.add<std::int32_t>("variation", 0);
162  desc.add<std::string>("uncertaintySource", "");
163  desc.add<std::uint32_t>("seed", 37428479);
164  desc.add<bool>("skipGenMatching", false);
165  desc.add<bool>("useDeterministicSeed", true);
166  desc.addUntracked<bool>("debug", false);
167 
168  auto source = (edm::ParameterDescription<std::string>("algo", true) and
169  edm::ParameterDescription<std::string>("algopt", true)) xor
170  (edm::ParameterDescription<edm::FileInPath>("resolutionFile", true) and
171  edm::ParameterDescription<edm::FileInPath>("scaleFactorFile", true));
172  desc.addNode(std::move(source));
173 
175 
176  descriptions.addDefault(desc);
177  }
static void fillDescriptions(edm::ParameterSetDescription &desc)
void addDefault(ParameterSetDescription const &psetDescription)
static std::string const source
Definition: EdmProvDump.cc:46
def move(src, dest)
Definition: eostools.py:511

◆ produce()

template<typename T >
void SmearedJetProducerT< T >::produce ( edm::Event event,
const edm::EventSetup setup 
)
inlineoverride

Definition at line 179 of file SmearedJetProducerT.h.

References gather_cfg::cout, ztail::d, reco::LeafCandidate::energy(), reco::LeafCandidate::eta(), JME::JetResolution::get(), JME::JetResolutionScaleFactor::get(), JME::JetResolutionScaleFactor::getScaleFactor(), metsig::jet, JME::JetEta, JME::JetPt, SmearedJetProducerT< T >::jetPtComparator, PDWG_EXODelayedJetMET_cff::jets, SmearedJetProducerT< T >::m_debug, SmearedJetProducerT< T >::m_enabled, SmearedJetProducerT< T >::m_genJetMatcher, SmearedJetProducerT< T >::m_jets_algo_pt_token, SmearedJetProducerT< T >::m_jets_algo_token, SmearedJetProducerT< T >::m_jets_token, SmearedJetProducerT< T >::m_nomVar, SmearedJetProducerT< T >::m_random_generator, SmearedJetProducerT< T >::m_resolution_from_file, SmearedJetProducerT< T >::m_rho_token, SmearedJetProducerT< T >::m_scale_factor_from_file, SmearedJetProducerT< T >::m_systematic_variation, SmearedJetProducerT< T >::m_uncertaintySource, SmearedJetProducerT< T >::m_use_txt_files, SmearedJetProducerT< T >::m_useDeterministicSeed, SmearedJetProducerT< T >::MIN_JET_ENERGY, eostools::move(), reco::LeafCandidate::phi(), reco::LeafCandidate::pt(), L1TObjectsTimingClient_cff::resolution, rho, JME::Rho, fileCollector::seed, singleTopDQM_cfi::setup, jetUpdater_cfi::sort, and mathSSE::sqrt().

179  {
180  edm::Handle<JetCollection> jets_collection;
181  event.getByToken(m_jets_token, jets_collection);
182 
183  // Disable the module when running on real data
184  if (m_enabled && event.isRealData()) {
185  m_enabled = false;
186  m_genJetMatcher.reset();
187  }
188 
190  if (m_enabled)
191  event.getByToken(m_rho_token, rho);
192 
194  JME::JetResolutionScaleFactor resolution_sf;
195 
196  const JetCollection& jets = *jets_collection;
197 
198  if (m_enabled) {
199  if (m_use_txt_files) {
201  resolution_sf = *m_scale_factor_from_file;
202  } else {
205  }
206 
208  unsigned int runNum_uint = static_cast<unsigned int>(event.id().run());
209  unsigned int lumiNum_uint = static_cast<unsigned int>(event.id().luminosityBlock());
210  unsigned int evNum_uint = static_cast<unsigned int>(event.id().event());
211  unsigned int jet0eta = uint32_t(jets.empty() ? 0 : jets[0].eta() / 0.01);
212  std::uint32_t seed = jet0eta + m_nomVar + (lumiNum_uint << 10) + (runNum_uint << 20) + evNum_uint;
213  m_random_generator.seed(seed);
214  }
215  }
216 
217  if (m_genJetMatcher)
218  m_genJetMatcher->getTokens(event);
219 
220  auto smearedJets = std::make_unique<JetCollection>();
221 
222  for (const auto& jet : jets) {
223  if ((!m_enabled) || (jet.pt() == 0)) {
224  // Module disabled or invalid p4. Simply copy the input jet.
225  smearedJets->push_back(jet);
226 
227  continue;
228  }
229 
230  double jet_resolution = resolution.getResolution(
232  double jer_sf = resolution_sf.getScaleFactor({{JME::Binning::JetPt, jet.pt()}, {JME::Binning::JetEta, jet.eta()}},
235 
236  if (m_debug) {
237  std::cout << "jet: pt: " << jet.pt() << " eta: " << jet.eta() << " phi: " << jet.phi()
238  << " e: " << jet.energy() << std::endl;
239  std::cout << "resolution: " << jet_resolution << std::endl;
240  std::cout << "resolution scale factor: " << jer_sf << std::endl;
241  }
242 
243  const reco::GenJet* genJet = nullptr;
244  if (m_genJetMatcher)
245  genJet = m_genJetMatcher->match(jet, jet.pt() * jet_resolution);
246 
247  double smearFactor = 1.;
248 
249  if (genJet) {
250  /*
251  * Case 1: we have a "good" gen jet matched to the reco jet
252  */
253 
254  if (m_debug) {
255  std::cout << "gen jet: pt: " << genJet->pt() << " eta: " << genJet->eta() << " phi: " << genJet->phi()
256  << " e: " << genJet->energy() << std::endl;
257  }
258 
259  double dPt = jet.pt() - genJet->pt();
260  smearFactor = 1 + m_nomVar * (jer_sf - 1.) * dPt / jet.pt();
261 
262  } else if (jer_sf > 1) {
263  /*
264  * Case 2: we don't have a gen jet. Smear jet pt using a random gaussian variation
265  */
266 
267  double sigma = jet_resolution * std::sqrt(jer_sf * jer_sf - 1);
268  if (m_debug) {
269  std::cout << "gaussian width: " << sigma << std::endl;
270  }
271 
272  std::normal_distribution<> d(0, sigma);
273  smearFactor = 1. + m_nomVar * d(m_random_generator);
274  } else if (m_debug) {
275  std::cout << "Impossible to smear this jet" << std::endl;
276  }
277 
278  if (jet.energy() * smearFactor < MIN_JET_ENERGY) {
279  // Negative or too small smearFactor. We would change direction of the jet
280  // and this is not what we want.
281  // Recompute the smearing factor in order to have jet.energy() == MIN_JET_ENERGY
282  double newSmearFactor = MIN_JET_ENERGY / jet.energy();
283  if (m_debug) {
284  std::cout << "The smearing factor (" << smearFactor << ") is either negative or too small. Fixing it to "
285  << newSmearFactor << " to avoid change of direction." << std::endl;
286  }
287  smearFactor = newSmearFactor;
288  }
289 
290  T smearedJet = jet;
291  smearedJet.scaleEnergy(smearFactor);
292 
293  if (m_debug) {
294  std::cout << "smeared jet (" << smearFactor << "): pt: " << smearedJet.pt() << " eta: " << smearedJet.eta()
295  << " phi: " << smearedJet.phi() << " e: " << smearedJet.energy() << std::endl;
296  }
297 
298  smearedJets->push_back(smearedJet);
299  }
300 
301  // Sort jets by pt
302  std::sort(smearedJets->begin(), smearedJets->end(), jetPtComparator);
303 
304  event.put(std::move(smearedJets));
305  }
edm::EDGetTokenT< JetCollection > m_jets_token
static constexpr const double MIN_JET_ENERGY
static const JetResolutionScaleFactor get(const edm::EventSetup &, const Token &)
double pt() const final
transverse momentum
GreaterByPt< T > jetPtComparator
JME::JetResolutionScaleFactor::Token m_jets_algo_token
std::unique_ptr< JME::JetResolution > m_resolution_from_file
std::vector< T > JetCollection
float getScaleFactor(const JetParameters &parameters, Variation variation=Variation::NOMINAL, std::string uncertaintySource="") const
T sqrt(T t)
Definition: SSEVec.h:19
Jets made from MC generator particles.
Definition: GenJet.h:23
edm::EDGetTokenT< double > m_rho_token
d
Definition: ztail.py:151
std::mt19937 m_random_generator
std::shared_ptr< pat::GenJetMatcher > m_genJetMatcher
std::unique_ptr< JME::JetResolutionScaleFactor > m_scale_factor_from_file
long double T
double phi() const final
momentum azimuthal angle
def move(src, dest)
Definition: eostools.py:511
JME::JetResolution::Token m_jets_algo_pt_token
Definition: event.py:1
static const JetResolution get(const edm::EventSetup &, const Token &)
double energy() const final
energy
double eta() const final
momentum pseudorapidity

Member Data Documentation

◆ jetPtComparator

template<typename T >
GreaterByPt<T> SmearedJetProducerT< T >::jetPtComparator
private

Definition at line 329 of file SmearedJetProducerT.h.

Referenced by SmearedJetProducerT< T >::produce().

◆ m_debug

template<typename T >
bool SmearedJetProducerT< T >::m_debug
private

Definition at line 320 of file SmearedJetProducerT.h.

Referenced by SmearedJetProducerT< T >::produce().

◆ m_enabled

template<typename T >
bool SmearedJetProducerT< T >::m_enabled
private

◆ m_genJetMatcher

template<typename T >
std::shared_ptr<pat::GenJetMatcher> SmearedJetProducerT< T >::m_genJetMatcher
private

◆ m_jets_algo

template<typename T >
std::string SmearedJetProducerT< T >::m_jets_algo
private

◆ m_jets_algo_pt

template<typename T >
std::string SmearedJetProducerT< T >::m_jets_algo_pt
private

◆ m_jets_algo_pt_token

template<typename T >
JME::JetResolution::Token SmearedJetProducerT< T >::m_jets_algo_pt_token
private

◆ m_jets_algo_token

template<typename T >
JME::JetResolutionScaleFactor::Token SmearedJetProducerT< T >::m_jets_algo_token
private

◆ m_jets_token

template<typename T >
edm::EDGetTokenT<JetCollection> SmearedJetProducerT< T >::m_jets_token
private

◆ m_nomVar

template<typename T >
int SmearedJetProducerT< T >::m_nomVar
private

◆ m_random_generator

template<typename T >
std::mt19937 SmearedJetProducerT< T >::m_random_generator
private

◆ m_resolution_from_file

template<typename T >
std::unique_ptr<JME::JetResolution> SmearedJetProducerT< T >::m_resolution_from_file
private

◆ m_rho_token

template<typename T >
edm::EDGetTokenT<double> SmearedJetProducerT< T >::m_rho_token
private

◆ m_scale_factor_from_file

template<typename T >
std::unique_ptr<JME::JetResolutionScaleFactor> SmearedJetProducerT< T >::m_scale_factor_from_file
private

◆ m_systematic_variation

template<typename T >
Variation SmearedJetProducerT< T >::m_systematic_variation
private

◆ m_uncertaintySource

template<typename T >
std::string SmearedJetProducerT< T >::m_uncertaintySource
private

◆ m_use_txt_files

template<typename T >
bool SmearedJetProducerT< T >::m_use_txt_files
private

◆ m_useDeterministicSeed

template<typename T >
bool SmearedJetProducerT< T >::m_useDeterministicSeed
private

Definition at line 319 of file SmearedJetProducerT.h.

Referenced by SmearedJetProducerT< T >::produce().

◆ MIN_JET_ENERGY

template<typename T >
constexpr const double SmearedJetProducerT< T >::MIN_JET_ENERGY = 1e-2
staticprivate

Definition at line 308 of file SmearedJetProducerT.h.

Referenced by SmearedJetProducerT< T >::produce().