CMS 3D CMS Logo

PFJetAnalyzerDQM.cc
Go to the documentation of this file.
13 
14 #include <algorithm>
15 #include <numeric>
16 #include <regex>
17 #include <sstream>
18 #include <vector>
19 #include <memory>
20 
22 public:
24  void analyze(const edm::Event&, const edm::EventSetup&) override;
25 
26 protected:
27  //Book histograms
28  void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
29  void dqmBeginRun(const edm::Run&, const edm::EventSetup&) override {}
30  void dqmEndRun(const edm::Run&, const edm::EventSetup&) override {}
31 
32 private:
33  class Plot1DInBin {
34  public:
36  const uint32_t nbins;
37  const float min, max;
40 
41  Plot1DInBin(const std::string _name,
42  const std::string _title,
43  const uint32_t _nbins,
44  const float _min,
45  const float _max,
46  float _ptbin_low,
47  float _ptbin_high,
48  float _etabin_low,
49  float _etabin_high)
50  : name(_name),
51  title(_title),
52  nbins(_nbins),
53  min(_min),
54  max(_max),
55  ptbin_low(_ptbin_low),
56  ptbin_high(_ptbin_high),
57  etabin_low(_etabin_low),
58  etabin_high(_etabin_high) {}
59 
60  void book(DQMStore::IBooker& booker) { plot_ = booker.book1D(name, title, nbins, min, max); }
61 
62  void fill(float value) {
63  assert(plot_ != nullptr);
64  plot_->Fill(value);
65  }
66 
67  //Check if a jet with a value v would be in the bin that applies to this plot
68  bool isInBin(float v, float low, float high) { return v >= low && v < high; }
69 
70  bool isInPtBin(float pt) { return isInBin(pt, ptbin_low, ptbin_high); }
71 
72  bool isInEtaBin(float eta) { return isInBin(eta, etabin_low, etabin_high); }
73 
74  bool isInPtEtaBin(float pt, float eta) { return isInPtBin(pt) && isInEtaBin(eta); }
75  };
76 
78  public:
80  std::unique_ptr<TH1F> base_hist;
83 
85  const std::string _title,
86  std::unique_ptr<TH1F> _base_hist,
87  float _ptbin_low,
88  float _ptbin_high,
89  float _etabin_low,
90  float _etabin_high)
91  : name(_name),
92  title(_title),
93  base_hist(std::move(_base_hist)),
94  ptbin_low(_ptbin_low),
95  ptbin_high(_ptbin_high),
96  etabin_low(_etabin_low),
97  etabin_high(_etabin_high) {}
98 
99  void book(DQMStore::IBooker& booker) { plot_ = booker.book1D(name.c_str(), base_hist.get()); }
100 
101  void fill(float value) {
102  assert(plot_ != nullptr);
103  plot_->Fill(value);
104  }
105 
106  //Check if a jet with a value v would be in the bin that applies to this plot
107  bool isInBin(float v, float low, float high) { return v >= low && v < high; }
108 
109  bool isInPtBin(float pt) { return isInBin(pt, ptbin_low, ptbin_high); }
110 
111  bool isInEtaBin(float eta) { return isInBin(eta, etabin_low, etabin_high); }
112 
113  bool isInPtEtaBin(float pt, float eta) { return isInPtBin(pt) && isInEtaBin(eta); }
114  };
115 
116  std::vector<Plot1DInBin> jetResponsePlots;
117  std::vector<Plot1DInBinVariable> genJetPlots;
118 
119  // Is this data or MC?
120  bool isMC;
121 
122  float jetDeltaR;
123 
129 
131  void prepareJetResponsePlots(const std::vector<edm::ParameterSet>& genjet_plots_pset);
132  void prepareGenJetPlots(const std::vector<edm::ParameterSet>& genjet_plots_pset);
133 };
134 
135 void PFJetAnalyzerDQM::prepareJetResponsePlots(const std::vector<edm::ParameterSet>& response_plots) {
136  for (auto& pset : response_plots) {
137  //Low and high edges of the pt and eta bins for jets to pass to be filled into this histogram
138  const auto ptbin_low = pset.getParameter<double>("ptBinLow");
139  const auto ptbin_high = pset.getParameter<double>("ptBinHigh");
140  const auto etabin_low = pset.getParameter<double>("etaBinLow");
141  const auto etabin_high = pset.getParameter<double>("etaBinHigh");
142 
143  const auto response_nbins = pset.getParameter<uint32_t>("responseNbins");
144  const auto response_low = pset.getParameter<double>("responseLow");
145  const auto response_high = pset.getParameter<double>("responseHigh");
146 
147  const auto name = pset.getParameter<std::string>("name");
148  const auto title = pset.getParameter<std::string>("title");
149 
150  jetResponsePlots.push_back(Plot1DInBin(
151  name, title, response_nbins, response_low, response_high, ptbin_low, ptbin_high, etabin_low, etabin_high));
152  }
153  if (jetResponsePlots.size() > 200) {
154  throw std::runtime_error("Requested too many jet response plots, aborting as this seems unusual.");
155  }
156 }
157 
158 void PFJetAnalyzerDQM::prepareGenJetPlots(const std::vector<edm::ParameterSet>& genjet_plots_pset) {
159  for (auto& pset : genjet_plots_pset) {
160  const auto name = pset.getParameter<std::string>("name");
161  const auto title = pset.getParameter<std::string>("title");
162 
163  //Low and high edges of the eta bins for jets to pass to be filled into this histogram
164  const auto ptbins_d = pset.getParameter<std::vector<double>>("ptBins");
165  std::vector<float> ptbins(ptbins_d.begin(), ptbins_d.end());
166 
167  const auto etabin_low = pset.getParameter<double>("etaBinLow");
168  const auto etabin_high = pset.getParameter<double>("etaBinHigh");
169 
170  /*
171  for (auto v : ptbins) {
172  std::cout << " " << v;
173  }
174  std::cout << std::endl;
175  */
176 
178  name,
179  title,
180  std::make_unique<TH1F>(name.c_str(), title.c_str(), static_cast<int>(ptbins.size()) - 1, ptbins.data()),
181  0.0,
182  0.0,
183  etabin_low,
184  etabin_high));
185  }
186 }
187 
189  recoJetsLabel = iConfig.getParameter<edm::InputTag>("recoJetCollection");
190  genJetsLabel = iConfig.getParameter<edm::InputTag>("genJetCollection");
191 
192  //DeltaR for reco to gen jet matching
193  jetDeltaR = iConfig.getParameter<double>("jetDeltaR");
194 
195  //Create all jet response plots in bins of genjet pt and eta
196  const auto& response_plots = iConfig.getParameter<std::vector<edm::ParameterSet>>("responsePlots");
197  prepareJetResponsePlots(response_plots);
198 
199  const auto& genjet_plots = iConfig.getParameter<std::vector<edm::ParameterSet>>("genJetPlots");
200  prepareGenJetPlots(genjet_plots);
201 
202  recoJetsToken = consumes<edm::View<pat::Jet>>(recoJetsLabel);
203  genJetsToken = consumes<edm::View<reco::Jet>>(genJetsLabel);
204 }
205 
207  bool use_rawpt = false;
208 
209  //match gen jets to reco jets, require minimum jetDeltaR, choose closest, do not try to match charge
210  std::vector<int> matchIndices;
211  PFB::match(genJetCollection, recoJetCollection, matchIndices, false, jetDeltaR);
212 
213  for (unsigned int i = 0; i < genJetCollection.size(); i++) {
214  const auto& genJet = genJetCollection.at(i);
215  const auto pt_gen = genJet.pt();
216  const auto eta_gen = abs(genJet.eta());
217  const int iMatch = matchIndices[i];
218 
219  //Fill genjet pt
220  for (auto& plot : genJetPlots) {
221  if (plot.isInEtaBin(eta_gen)) {
222  plot.fill(pt_gen);
223  }
224  }
225 
226  //If gen jet had a matched reco jet
227  if (iMatch != -1) {
228  const auto& recoJet = recoJetCollection[iMatch];
229  auto pt_reco = recoJet.pt();
230  if (use_rawpt)
231  pt_reco *= recoJet.jecFactor("Uncorrected");
232  const auto response = pt_reco / pt_gen;
233 
234  //Loop linearly through all plots and check if they match the pt and eta bin
235  //this is not algorithmically optimal but we don't expect to more than a few hundred plots
236  //If this turns out to be a problem, can easily make a 2D-map for indices
237  for (auto& plot : jetResponsePlots) {
238  if (plot.isInPtEtaBin(pt_gen, eta_gen)) {
239  plot.fill(response);
240  }
241  }
242  }
243  }
244 }
245 
247  //std::cout << "PFJetAnalyzerDQM booking response histograms" << std::endl;
248  booker.setCurrentFolder("ParticleFlow/JetResponse/");
249  for (auto& plot : jetResponsePlots) {
250  plot.book(booker);
251  }
252 
253  //Book plots for gen-jet pt spectra
254  booker.setCurrentFolder("ParticleFlow/GenJets/");
255  for (auto& plot : genJetPlots) {
256  plot.book(booker);
257  }
258 }
259 
261  edm::Handle<edm::View<pat::Jet>> recoJetCollectionHandle;
262  iEvent.getByToken(recoJetsToken, recoJetCollectionHandle);
263  auto recoJetCollection = *recoJetCollectionHandle;
264 
265  isMC = !iEvent.isRealData();
266 
267  if (isMC) {
268  edm::Handle<edm::View<reco::Jet>> genJetCollectionHandle;
269  iEvent.getByToken(genJetsToken, genJetCollectionHandle);
270  auto genJetCollection = *genJetCollectionHandle;
271 
273  }
274 }
275 
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX)
Definition: DQMStore.cc:239
T getParameter(std::string const &) const
void dqmEndRun(const edm::Run &, const edm::EventSetup &) override
edm::EDGetTokenT< edm::View< pat::Jet > > recoJetsToken
Plot1DInBin(const std::string _name, const std::string _title, const uint32_t _nbins, const float _min, const float _max, float _ptbin_low, float _ptbin_high, float _etabin_low, float _etabin_high)
void book(DQMStore::IBooker &booker)
void prepareJetResponsePlots(const std::vector< edm::ParameterSet > &genjet_plots_pset)
void match(const C &candCollection, const M &matchedCandCollection, std::vector< int > &matchIndices, bool matchCharge=false, float dRMax=-1)
Definition: Matchers.h:17
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:418
std::vector< Plot1DInBin > jetResponsePlots
bool isInBin(float v, float low, float high)
size_type size() const
void fillJetResponse(edm::View< pat::Jet > &recoJetCollection, edm::View< reco::Jet > &genJetCollection)
void dqmBeginRun(const edm::Run &, const edm::EventSetup &) override
edm::EDGetTokenT< edm::View< reco::Jet > > genJetsToken
bool isRealData() const
Definition: EventBase.h:62
PFJetAnalyzerDQM(const edm::ParameterSet &)
bool isInBin(float v, float low, float high)
bool isInPtEtaBin(float pt, float eta)
edm::InputTag genJetsLabel
void Fill(long long x)
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void book(DQMStore::IBooker &booker)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Definition: value.py:1
edm::InputTag recoJetsLabel
edm::EDGetTokenT< reco::CandViewMatchMap > srcRefToJetMap
std::vector< Plot1DInBinVariable > genJetPlots
void analyze(const edm::Event &, const edm::EventSetup &) override
void prepareGenJetPlots(const std::vector< edm::ParameterSet > &genjet_plots_pset)
Plot1DInBinVariable(const std::string _name, const std::string _title, std::unique_ptr< TH1F > _base_hist, float _ptbin_low, float _ptbin_high, float _etabin_low, float _etabin_high)
bool isInPtEtaBin(float pt, float eta)
const_reference at(size_type pos) const
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45