CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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<> edm::stream::EDProducerBase edm::ProducerBase edm::EDConsumerBase edm::ProductRegistryHelper

Public Member Functions

virtual 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
 
- Public Member Functions inherited from edm::stream::EDProducerBase
 EDProducerBase ()
 
ModuleDescription const & moduleDescription () const
 
virtual ~EDProducerBase ()
 
- Public Member Functions inherited from edm::ProducerBase
void callWhenNewProductsRegistered (std::function< void(BranchDescription const &)> const &func)
 
 ProducerBase ()
 
void registerProducts (ProducerBase *, ProductRegistry *, ModuleDescription const &)
 
std::function< void(BranchDescription
const &)> 
registrationCallback () const
 used by the fwk to register list of products More...
 
virtual ~ProducerBase ()
 
- Public Member Functions inherited from edm::EDConsumerBase
std::vector< ConsumesInfoconsumesInfo () const
 
 EDConsumerBase ()
 
ProductHolderIndexAndSkipBit indexFrom (EDGetToken, BranchType, TypeID const &) const
 
void itemsMayGet (BranchType, std::vector< ProductHolderIndexAndSkipBit > &) const
 
void itemsToGet (BranchType, std::vector< ProductHolderIndexAndSkipBit > &) const
 
std::vector
< ProductHolderIndexAndSkipBit >
const & 
itemsToGetFromEvent () const
 
void labelsForToken (EDGetToken iToken, Labels &oLabels) const
 
void modulesDependentUpon (std::string const &iProcessName, std::string const &iModuleLabel, bool iPrint, std::vector< char const * > &oModuleLabels) const
 
void modulesWhoseProductsAreConsumed (std::vector< ModuleDescription const * > &modules, ProductRegistry const &preg, std::map< std::string, ModuleDescription const * > const &labelsToDesc, std::string const &processName) const
 
bool registeredToConsume (ProductHolderIndex, bool, BranchType) const
 
bool registeredToConsumeMany (TypeID const &, BranchType) const
 
void updateLookup (BranchType iBranchType, ProductHolderIndexHelper const &)
 
virtual ~EDConsumerBase ()
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 
- Static Public Member Functions inherited from edm::stream::EDProducerBase
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
static void prevalidate (ConfigurationDescriptions &descriptions)
 

Private Types

using JetCollection = std::vector< T >
 

Private Attributes

GreaterByPt< TjetPtComparator
 
bool m_debug
 
bool m_enabled
 
std::shared_ptr
< pat::GenJetMatcher
m_genJetMatcher
 
std::string m_jets_algo
 
std::string m_jets_algo_pt
 
edm::EDGetTokenT< JetCollectionm_jets_token
 
std::mt19937 m_random_generator
 
std::unique_ptr
< JME::JetResolution
m_resolution_from_file
 
edm::EDGetTokenT< double > m_rho_token
 
std::unique_ptr
< JME::JetResolutionScaleFactor
m_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, GlobalCache
LuminosityBlockContext
 
typedef
CacheTypes::LuminosityBlockSummaryCache 
LuminosityBlockSummaryCache
 
typedef CacheTypes::RunCache RunCache
 
typedef RunContextT< RunCache,
GlobalCache
RunContext
 
typedef CacheTypes::RunSummaryCache RunSummaryCache
 
- Public Types inherited from edm::stream::EDProducerBase
typedef EDProducerAdaptorBase ModuleType
 
- Public Types inherited from edm::ProducerBase
typedef
ProductRegistryHelper::TypeLabelList 
TypeLabelList
 
- Public Types inherited from edm::EDConsumerBase
typedef ProductLabels Labels
 
- Protected Member Functions inherited from edm::EDConsumerBase
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > consumes (edm::InputTag const &tag)
 
EDGetToken consumes (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken consumes (TypeToGet const &id, edm::InputTag const &tag)
 
ConsumesCollector consumesCollector ()
 Use a ConsumesCollector to gather consumes information from helper functions. More...
 
template<typename ProductType , BranchType B = InEvent>
void consumesMany ()
 
void consumesMany (const TypeToGet &id)
 
template<BranchType B>
void consumesMany (const TypeToGet &id)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > mayConsume (edm::InputTag const &tag)
 
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 

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 99 of file SmearedJetProducerT.h.

Member Typedef Documentation

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

Definition at line 101 of file SmearedJetProducerT.h.

Constructor & Destructor Documentation

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

Definition at line 104 of file SmearedJetProducerT.h.

References looper::cfg, edm::errors::ConfigFileReadError, edm::EDConsumerBase::consumesCollector(), DOWN, Exception, edm::ParameterSet::exists(), contentValuesFiles::fullPath, edm::ParameterSet::getParameter(), SmearedJetProducerT< T >::m_enabled, SmearedJetProducerT< T >::m_genJetMatcher, SmearedJetProducerT< T >::m_jets_algo, SmearedJetProducerT< T >::m_jets_algo_pt, SmearedJetProducerT< T >::m_jets_token, 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_use_txt_files, NOMINAL, fileCollector::seed, AlCaHLTBitMon_QueryRunRegistry::string, and UP.

104  :
105  m_enabled(cfg.getParameter<bool>("enabled")),
106  m_useDeterministicSeed(cfg.getParameter<bool>("useDeterministicSeed")),
107  m_debug(cfg.getUntrackedParameter<bool>("debug", false)) {
108 
109  m_jets_token = consumes<JetCollection>(cfg.getParameter<edm::InputTag>("src"));
110 
111  if (m_enabled) {
112  m_rho_token = consumes<double>(cfg.getParameter<edm::InputTag>("rho"));
113 
114  m_use_txt_files = cfg.exists("resolutionFile") && cfg.exists("scaleFactorFile");
115 
116  if (m_use_txt_files) {
117  std::string resolutionFile = cfg.getParameter<edm::FileInPath>("resolutionFile").fullPath();
118  std::string scaleFactorFile = cfg.getParameter<edm::FileInPath>("scaleFactorFile").fullPath();
119 
120  m_resolution_from_file.reset(new JME::JetResolution(resolutionFile));
121  m_scale_factor_from_file.reset(new JME::JetResolutionScaleFactor(scaleFactorFile));
122  } else {
123  m_jets_algo = cfg.getParameter<std::string>("algo");
124  m_jets_algo_pt = cfg.getParameter<std::string>("algopt");
125  }
126 
127  std::uint32_t seed = cfg.getParameter<std::uint32_t>("seed");
128  m_random_generator = std::mt19937(seed);
129 
130  bool skipGenMatching = cfg.getParameter<bool>("skipGenMatching");
131  if (! skipGenMatching)
132  m_genJetMatcher = std::make_shared<pat::GenJetMatcher>(cfg, consumesCollector());
133 
134  std::int32_t variation = cfg.getParameter<std::int32_t>("variation");
135  if (variation == 0)
137  else if (variation == 1)
139  else if (variation == -1)
141  else
142  throw edm::Exception(edm::errors::ConfigFileReadError, "Invalid value for 'variation' parameter. Only -1, 0 or 1 are supported.");
143  }
144 
145  produces<JetCollection>();
146  }
edm::EDGetTokenT< JetCollection > m_jets_token
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
tuple cfg
Definition: looper.py:293
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::unique_ptr< JME::JetResolution > m_resolution_from_file
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
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 148 of file SmearedJetProducerT.h.

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

148  {
150 
151  desc.add<edm::InputTag>("src");
152  desc.add<bool>("enabled");
153  desc.add<edm::InputTag>("rho");
154  desc.add<std::int32_t>("variation", 0);
155  desc.add<std::uint32_t>("seed", 37428479);
156  desc.add<bool>("skipGenMatching", false);
157  desc.add<bool>("useDeterministicSeed", true);
158  desc.addUntracked<bool>("debug", false);
159 
160  auto source =
162  (edm::ParameterDescription<edm::FileInPath>("resolutionFile", true) and edm::ParameterDescription<edm::FileInPath>("scaleFactorFile", true));
163  desc.addNode(source);
164 
166 
167  descriptions.addDefault(desc);
168  }
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:43
template<typename T >
virtual void SmearedJetProducerT< T >::produce ( edm::Event event,
const edm::EventSetup setup 
)
inlineoverridevirtual

Implements edm::stream::EDProducerBase.

Definition at line 170 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, SmearedJetProducerT< T >::jetPtComparator, fwrapper::jets, SmearedJetProducerT< T >::m_debug, SmearedJetProducerT< T >::m_enabled, SmearedJetProducerT< T >::m_genJetMatcher, SmearedJetProducerT< T >::m_jets_algo, SmearedJetProducerT< T >::m_jets_algo_pt, SmearedJetProducerT< T >::m_jets_token, 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_use_txt_files, SmearedJetProducerT< T >::m_useDeterministicSeed, SmearedJetProducerT< T >::MIN_JET_ENERGY, eostools::move(), reco::LeafCandidate::phi(), reco::LeafCandidate::pt(), dtDQMClient_cfg::resolution, rho, JME::Rho, fileCollector::seed, and mathSSE::sqrt().

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

Member Data Documentation

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

Definition at line 312 of file SmearedJetProducerT.h.

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

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

Definition at line 303 of file SmearedJetProducerT.h.

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

template<typename T >
bool SmearedJetProducerT< T >::m_enabled
private
template<typename T >
std::shared_ptr<pat::GenJetMatcher> SmearedJetProducerT< T >::m_genJetMatcher
private
template<typename T >
std::string SmearedJetProducerT< T >::m_jets_algo
private
template<typename T >
std::string SmearedJetProducerT< T >::m_jets_algo_pt
private
template<typename T >
edm::EDGetTokenT<JetCollection> SmearedJetProducerT< T >::m_jets_token
private
template<typename T >
std::mt19937 SmearedJetProducerT< T >::m_random_generator
private
template<typename T >
std::unique_ptr<JME::JetResolution> SmearedJetProducerT< T >::m_resolution_from_file
private
template<typename T >
edm::EDGetTokenT<double> SmearedJetProducerT< T >::m_rho_token
private
template<typename T >
std::unique_ptr<JME::JetResolutionScaleFactor> SmearedJetProducerT< T >::m_scale_factor_from_file
private
template<typename T >
Variation SmearedJetProducerT< T >::m_systematic_variation
private
template<typename T >
bool SmearedJetProducerT< T >::m_use_txt_files
private
template<typename T >
bool SmearedJetProducerT< T >::m_useDeterministicSeed
private

Definition at line 302 of file SmearedJetProducerT.h.

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

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

Definition at line 294 of file SmearedJetProducerT.h.

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