CMS 3D CMS Logo

SmearedJetProducerT.h
Go to the documentation of this file.
1 #ifndef PAT_SMEAREDJETPRODUCERT_H
2 #define PAT_SMEAREDJETPRODUCERT_H
3 
16 
25 
29 
31 
32 #include <TFile.h>
33 #include <TH1.h>
34 #include <TH1F.h>
35 
36 #include <memory>
37 #include <random>
38 
39 namespace pat {
40  class GenJetMatcher {
41  public:
43  : m_genJetsToken(collector.consumes<reco::GenJetCollection>(cfg.getParameter<edm::InputTag>("genJets"))),
44  m_dR_max(cfg.getParameter<double>("dRMax")),
45  m_dPt_max_factor(cfg.getParameter<double>("dPtMaxFactor")) {
46  // Empty
47  }
48 
50  desc.add<edm::InputTag>("genJets");
51  desc.add<double>("dRMax");
52  desc.add<double>("dPtMaxFactor");
53  }
54 
55  void getTokens(const edm::Event& event) { event.getByToken(m_genJetsToken, m_genJets); }
56 
57  template <class T>
58  const reco::GenJet* match(const T& jet, double resolution) {
60 
61  // Try to find a gen jet matching
62  // dR < m_dR_max
63  // dPt < m_dPt_max_factor * resolution
64 
65  double min_dR = std::numeric_limits<double>::infinity();
66  const reco::GenJet* matched_genJet = nullptr;
67 
68  for (const auto& genJet : genJets) {
69  double dR = deltaR(genJet, jet);
70 
71  if (dR > min_dR)
72  continue;
73 
74  if (dR < m_dR_max) {
75  double dPt = std::abs(genJet.pt() - jet.pt());
76  if (dPt > m_dPt_max_factor * resolution)
77  continue;
78 
79  min_dR = dR;
80  matched_genJet = &genJet;
81  }
82  }
83 
84  return matched_genJet;
85  }
86 
87  private:
90 
91  double m_dR_max;
93  };
94 }; // namespace pat
95 
96 template <typename T>
98  using JetCollection = std::vector<T>;
99 
100 public:
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_uncertaintySource = cfg.getParameter<std::string>("uncertaintySource");
132  m_nomVar = 1;
133  if (variation == 0)
134  m_systematic_variation = Variation::NOMINAL;
135  else if (variation == 1)
136  m_systematic_variation = Variation::UP;
137  else if (variation == -1)
138  m_systematic_variation = Variation::DOWN;
139  else if (variation == 101) {
140  m_systematic_variation = Variation::NOMINAL;
141  m_nomVar = 1;
142  } else if (variation == -101) {
143  m_systematic_variation = Variation::NOMINAL;
144  m_nomVar = -1;
145  } else
147  "Invalid value for 'variation' parameter. Only -1, 0, 1 or 101, -101 are supported.");
148  }
149 
150  produces<JetCollection>();
151  }
152 
155 
156  desc.add<edm::InputTag>("src");
157  desc.add<bool>("enabled");
158  desc.add<edm::InputTag>("rho");
159  desc.add<std::int32_t>("variation", 0);
160  desc.add<std::string>("uncertaintySource", "");
161  desc.add<std::uint32_t>("seed", 37428479);
162  desc.add<bool>("skipGenMatching", false);
163  desc.add<bool>("useDeterministicSeed", true);
164  desc.addUntracked<bool>("debug", false);
165 
166  auto source = (edm::ParameterDescription<std::string>("algo", true) and
167  edm::ParameterDescription<std::string>("algopt", true)) xor
168  (edm::ParameterDescription<edm::FileInPath>("resolutionFile", true) and
169  edm::ParameterDescription<edm::FileInPath>("scaleFactorFile", true));
170  desc.addNode(std::move(source));
171 
173 
174  descriptions.addDefault(desc);
175  }
176 
177  void produce(edm::Event& event, const edm::EventSetup& setup) override {
178  edm::Handle<JetCollection> jets_collection;
179  event.getByToken(m_jets_token, jets_collection);
180 
181  // Disable the module when running on real data
182  if (m_enabled && event.isRealData()) {
183  m_enabled = false;
184  m_genJetMatcher.reset();
185  }
186 
188  if (m_enabled)
189  event.getByToken(m_rho_token, rho);
190 
192  JME::JetResolutionScaleFactor resolution_sf;
193 
194  const JetCollection& jets = *jets_collection;
195 
196  if (m_enabled) {
197  if (m_use_txt_files) {
198  resolution = *m_resolution_from_file;
199  resolution_sf = *m_scale_factor_from_file;
200  } else {
201  resolution = JME::JetResolution::get(setup, m_jets_algo_pt);
202  resolution_sf = JME::JetResolutionScaleFactor::get(setup, m_jets_algo);
203  }
204 
205  if (m_useDeterministicSeed) {
206  unsigned int runNum_uint = static_cast<unsigned int>(event.id().run());
207  unsigned int lumiNum_uint = static_cast<unsigned int>(event.id().luminosityBlock());
208  unsigned int evNum_uint = static_cast<unsigned int>(event.id().event());
209  unsigned int jet0eta = uint32_t(jets.empty() ? 0 : jets[0].eta() / 0.01);
210  std::uint32_t seed = jet0eta + m_nomVar + (lumiNum_uint << 10) + (runNum_uint << 20) + evNum_uint;
211  m_random_generator.seed(seed);
212  }
213  }
214 
215  if (m_genJetMatcher)
216  m_genJetMatcher->getTokens(event);
217 
218  auto smearedJets = std::make_unique<JetCollection>();
219 
220  for (const auto& jet : jets) {
221  if ((!m_enabled) || (jet.pt() == 0)) {
222  // Module disabled or invalid p4. Simply copy the input jet.
223  smearedJets->push_back(jet);
224 
225  continue;
226  }
227 
228  double jet_resolution = resolution.getResolution(
229  {{JME::Binning::JetPt, jet.pt()}, {JME::Binning::JetEta, jet.eta()}, {JME::Binning::Rho, *rho}});
230  double jer_sf = resolution_sf.getScaleFactor({{JME::Binning::JetPt, jet.pt()}, {JME::Binning::JetEta, jet.eta()}},
231  m_systematic_variation,
232  m_uncertaintySource);
233  if (m_debug) {
234  std::cout << "jet: pt: " << jet.pt() << " eta: " << jet.eta() << " phi: " << jet.phi()
235  << " e: " << jet.energy() << std::endl;
236  std::cout << "resolution: " << jet_resolution << std::endl;
237  std::cout << "resolution scale factor: " << jer_sf << std::endl;
238  }
239 
240  const reco::GenJet* genJet = nullptr;
241  if (m_genJetMatcher)
242  genJet = m_genJetMatcher->match(jet, jet.pt() * jet_resolution);
243 
244  double smearFactor = 1.;
245 
246  if (genJet) {
247  /*
248  * Case 1: we have a "good" gen jet matched to the reco jet
249  */
250 
251  if (m_debug) {
252  std::cout << "gen jet: pt: " << genJet->pt() << " eta: " << genJet->eta() << " phi: " << genJet->phi()
253  << " e: " << genJet->energy() << std::endl;
254  }
255 
256  double dPt = jet.pt() - genJet->pt();
257  smearFactor = 1 + m_nomVar * (jer_sf - 1.) * dPt / jet.pt();
258 
259  } else if (jer_sf > 1) {
260  /*
261  * Case 2: we don't have a gen jet. Smear jet pt using a random gaussian variation
262  */
263 
264  double sigma = jet_resolution * std::sqrt(jer_sf * jer_sf - 1);
265  if (m_debug) {
266  std::cout << "gaussian width: " << sigma << std::endl;
267  }
268 
269  std::normal_distribution<> d(0, sigma);
270  smearFactor = 1. + m_nomVar * d(m_random_generator);
271  } else if (m_debug) {
272  std::cout << "Impossible to smear this jet" << std::endl;
273  }
274 
275  if (jet.energy() * smearFactor < MIN_JET_ENERGY) {
276  // Negative or too small smearFactor. We would change direction of the jet
277  // and this is not what we want.
278  // Recompute the smearing factor in order to have jet.energy() == MIN_JET_ENERGY
279  double newSmearFactor = MIN_JET_ENERGY / jet.energy();
280  if (m_debug) {
281  std::cout << "The smearing factor (" << smearFactor << ") is either negative or too small. Fixing it to "
282  << newSmearFactor << " to avoid change of direction." << std::endl;
283  }
284  smearFactor = newSmearFactor;
285  }
286 
287  T smearedJet = jet;
288  smearedJet.scaleEnergy(smearFactor);
289 
290  if (m_debug) {
291  std::cout << "smeared jet (" << smearFactor << "): pt: " << smearedJet.pt() << " eta: " << smearedJet.eta()
292  << " phi: " << smearedJet.phi() << " e: " << smearedJet.energy() << std::endl;
293  }
294 
295  smearedJets->push_back(smearedJet);
296  }
297 
298  // Sort jets by pt
299  std::sort(smearedJets->begin(), smearedJets->end(), jetPtComparator);
300 
301  event.put(std::move(smearedJets));
302  }
303 
304 private:
305  static constexpr const double MIN_JET_ENERGY = 1e-2;
306 
309  bool m_enabled;
315  bool m_debug;
316  std::shared_ptr<pat::GenJetMatcher> m_genJetMatcher;
317 
319  std::unique_ptr<JME::JetResolution> m_resolution_from_file;
320  std::unique_ptr<JME::JetResolutionScaleFactor> m_scale_factor_from_file;
321 
322  std::mt19937 m_random_generator;
323 
325 
326  int m_nomVar;
327 };
328 #endif
edm::EDGetTokenT< JetCollection > m_jets_token
T getParameter(std::string const &) const
float getResolution(const JetParameters &parameters) const
double eta() const final
momentum pseudorapidity
void getTokens(const edm::Event &event)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static const JetResolution get(const edm::EventSetup &, const std::string &)
edm::Handle< reco::GenJetCollection > m_genJets
std::vector< GenJet > GenJetCollection
collection of GenJet objects
GenJetMatcher(const edm::ParameterSet &cfg, edm::ConsumesCollector &&collector)
bool exists(std::string const &parameterName) const
checks if a parameter exists
SmearedJetProducerT(const edm::ParameterSet &cfg)
ParameterDescriptionNode * addNode(ParameterDescriptionNode const &node)
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
double pt() const final
transverse momentum
bool isRealData() const
Definition: EventBase.h:62
Definition: HeavyIon.h:7
GreaterByPt< T > jetPtComparator
const reco::GenJet * match(const T &jet, double resolution)
std::unique_ptr< JME::JetResolution > m_resolution_from_file
void produce(edm::Event &event, const edm::EventSetup &setup) override
static void fillDescriptions(edm::ParameterSetDescription &desc)
void addDefault(ParameterSetDescription const &psetDescription)
std::vector< T > JetCollection
T sqrt(T t)
Definition: SSEVec.h:18
vector< PseudoJet > jets
double energy() const final
energy
const double infinity
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Jets made from MC generator particles.
Definition: GenJet.h:25
float getScaleFactor(const JetParameters &parameters, Variation variation=Variation::NOMINAL, std::string uncertaintySource="") const
ParameterDescriptionBase * add(U const &iLabel, T const &value)
edm::EDGetTokenT< double > m_rho_token
std::mt19937 m_random_generator
static const JetResolutionScaleFactor get(const edm::EventSetup &, const std::string &)
std::shared_ptr< pat::GenJetMatcher > m_genJetMatcher
fixed size matrix
HLT enums.
std::unique_ptr< JME::JetResolutionScaleFactor > m_scale_factor_from_file
edm::EDGetTokenT< reco::GenJetCollection > m_genJetsToken
long double T
double phi() const final
momentum azimuthal angle
static std::string const source
Definition: EdmProvDump.cc:47
def move(src, dest)
Definition: eostools.py:511
#define constexpr
Definition: event.py:1