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
 
bool hasAbilityToProduceInBeginLumis () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndRuns () const final
 

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
 
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
 
bool m_use_txt_files
 
bool m_useDeterministicSeed
 

Static Private Attributes

static const double MIN_JET_ENERGY = 1e-2
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer<>
typedef CacheContexts< T... > CacheTypes
 
typedef CacheTypes::GlobalCache GlobalCache
 
typedef AbilityChecker< T... > HasAbility
 
typedef CacheTypes::LuminosityBlockCache LuminosityBlockCache
 
typedef LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCacheLuminosityBlockContext
 
typedef CacheTypes::LuminosityBlockSummaryCache LuminosityBlockSummaryCache
 
typedef CacheTypes::RunCache RunCache
 
typedef RunContextT< RunCache, GlobalCacheRunContext
 
typedef CacheTypes::RunSummaryCache 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

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

Definition at line 98 of file SmearedJetProducerT.h.

Constructor & Destructor Documentation

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, Exception, edm::ParameterSet::exists(), contentValuesFiles::fullPath, edm::ParameterSet::getParameter(), NOMINAL, SurveyInfoScenario_cff::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.reset(new JME::JetResolution(resolutionFile));
117  m_scale_factor_from_file.reset(new JME::JetResolutionScaleFactor(scaleFactorFile));
118  } else {
119  m_jets_algo = cfg.getParameter<std::string>("algo");
120  m_jets_algo_pt = cfg.getParameter<std::string>("algopt");
121  }
122 
123  std::uint32_t seed = cfg.getParameter<std::uint32_t>("seed");
124  m_random_generator = std::mt19937(seed);
125 
126  bool skipGenMatching = cfg.getParameter<bool>("skipGenMatching");
127  if (!skipGenMatching)
128  m_genJetMatcher = std::make_shared<pat::GenJetMatcher>(cfg, consumesCollector());
129 
130  std::int32_t variation = cfg.getParameter<std::int32_t>("variation");
131  m_nomVar = 1;
132  if (variation == 0)
134  else if (variation == 1)
136  else if (variation == -1)
138  else if (variation == 101) {
140  m_nomVar = 1;
141  } else if (variation == -101) {
143  m_nomVar = -1;
144  } else
146  "Invalid value for 'variation' parameter. Only -1, 0, 1 or 101, -101 are supported.");
147  }
148 
149  produces<JetCollection>();
150  }
edm::EDGetTokenT< JetCollection > m_jets_token
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
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

Member Function Documentation

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

Definition at line 152 of file SmearedJetProducerT.h.

References edm::ParameterSetDescription::add(), edm::ConfigurationDescriptions::addDefault(), edm::ParameterSetDescription::addNode(), edm::ParameterSetDescription::addUntracked(), pat::GenJetMatcher::fillDescriptions(), eostools::move(), and source.

152  {
154 
155  desc.add<edm::InputTag>("src");
156  desc.add<bool>("enabled");
157  desc.add<edm::InputTag>("rho");
158  desc.add<std::int32_t>("variation", 0);
159  desc.add<std::uint32_t>("seed", 37428479);
160  desc.add<bool>("skipGenMatching", false);
161  desc.add<bool>("useDeterministicSeed", true);
162  desc.addUntracked<bool>("debug", false);
163 
164  auto source = (edm::ParameterDescription<std::string>("algo", true) and
165  edm::ParameterDescription<std::string>("algopt", true)) xor
166  (edm::ParameterDescription<edm::FileInPath>("resolutionFile", true) and
167  edm::ParameterDescription<edm::FileInPath>("scaleFactorFile", true));
168  desc.addNode(std::move(source));
169 
171 
172  descriptions.addDefault(desc);
173  }
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
ParameterDescriptionNode * addNode(ParameterDescriptionNode const &node)
static void fillDescriptions(edm::ParameterSetDescription &desc)
void addDefault(ParameterSetDescription const &psetDescription)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static std::string const source
Definition: EdmProvDump.cc:47
def move(src, dest)
Definition: eostools.py:511
template<typename T >
void SmearedJetProducerT< T >::produce ( edm::Event event,
const edm::EventSetup setup 
)
inlineoverride

Definition at line 175 of file SmearedJetProducerT.h.

References gather_cfg::cout, ztail::d, reco::LeafCandidate::energy(), reco::LeafCandidate::eta(), JME::JetResolution::get(), JME::JetResolutionScaleFactor::get(), JME::JetResolution::getResolution(), JME::JetResolutionScaleFactor::getScaleFactor(), edm::EventBase::isRealData(), metsig::jet, JME::JetEta, JME::JetPt, singleTopDQM_cfi::jets, eostools::move(), reco::LeafCandidate::phi(), reco::LeafCandidate::pt(), L1TObjectsTimingClient_cff::resolution, JME::Rho, SurveyInfoScenario_cff::seed, and mathSSE::sqrt().

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

Member Data Documentation

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

Definition at line 321 of file SmearedJetProducerT.h.

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

Definition at line 312 of file SmearedJetProducerT.h.

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

Definition at line 307 of file SmearedJetProducerT.h.

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

Definition at line 313 of file SmearedJetProducerT.h.

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

Definition at line 309 of file SmearedJetProducerT.h.

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

Definition at line 308 of file SmearedJetProducerT.h.

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

Definition at line 305 of file SmearedJetProducerT.h.

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

Definition at line 323 of file SmearedJetProducerT.h.

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

Definition at line 319 of file SmearedJetProducerT.h.

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

Definition at line 316 of file SmearedJetProducerT.h.

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

Definition at line 306 of file SmearedJetProducerT.h.

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

Definition at line 317 of file SmearedJetProducerT.h.

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

Definition at line 310 of file SmearedJetProducerT.h.

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

Definition at line 315 of file SmearedJetProducerT.h.

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

Definition at line 311 of file SmearedJetProducerT.h.

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

Definition at line 303 of file SmearedJetProducerT.h.