CMS 3D CMS Logo

FFTJetPileupAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FFTJetPileupAnalyzer
4 // Class: FFTJetPileupAnalyzer
5 //
13 //
14 // Original Author: Igor Volobouev
15 // Created: Thu Apr 21 15:52:11 CDT 2011
16 //
17 //
18 
19 #include <cassert>
20 #include <sstream>
21 #include <numeric>
22 
23 #include "TNtuple.h"
24 
25 // user include files
32 
34 #include <TH2D.h>
38 
40 
42 
44 
46 
47 #define init_param(type, varname) varname(ps.getParameter<type>(#varname))
48 
49 //
50 // class declaration
51 //
53 public:
54  explicit FFTJetPileupAnalyzer(const edm::ParameterSet&);
55  ~FFTJetPileupAnalyzer() override;
56 
57 private:
58  FFTJetPileupAnalyzer() = delete;
61 
62  // The following method should take all necessary info from
63  // PileupSummaryInfo and fill out the ntuple
64  void analyzePileup(const std::vector<PileupSummaryInfo>& pInfo);
65 
66  void beginJob() override;
67  void analyze(const edm::Event&, const edm::EventSetup&) override;
68  void endJob() override;
69 
77 
86 
98 
99  double vertexNdofCut;
101 
102  std::vector<float> ntupleData;
103  TNtuple* nt;
104  int totalNpu;
105  int totalNPV;
106  unsigned long counter;
107 };
108 
109 //
110 // constructors and destructor
111 //
131  init_param(double, vertexNdofCut),
132  init_param(double, crazyEnergyCut),
133  nt(nullptr),
134  totalNpu(-1),
135  totalNPV(-1),
136  counter(0) {
138  pileupToken = consumes<std::vector<PileupSummaryInfo> >(pileupLabel);
139 
140  if (collectHistos)
141  histoToken = consumes<TH2D>(histoLabel);
142 
143  if (collectSummaries)
144  summaryToken = consumes<reco::FFTJetPileupSummary>(summaryLabel);
145 
146  if (collectFastJetRho) {
147  fastJetRhoToken = consumes<double>(fastJetRhoLabel);
148  fastJetSigmaToken = consumes<double>(fastJetSigmaLabel);
149  }
150 
151  if (collectGrids)
152  gridToken = consumes<reco::DiscretizedEnergyFlow>(gridLabel);
153 
154  if (collectGridDensity)
155  etSumToken = consumes<std::pair<double, double> >(histoLabel);
156 
157  if (collectVertexInfo)
158  srcPVsToken = consumes<reco::VertexCollection>(srcPVs);
159 }
160 
162 
163 //
164 // member functions
165 //
166 void FFTJetPileupAnalyzer::analyzePileup(const std::vector<PileupSummaryInfo>& info) {
167  const unsigned nBx = info.size();
168  if (collectPileup)
169  ntupleData.push_back(static_cast<float>(nBx));
170 
171  double sumpt_Lo = 0.0, sumpt_Hi = 0.0;
172  totalNpu = 0;
173 
174  int npu_by_Bx[3] = {
175  0,
176  };
177  double sumpt_Lo_by_Bx[3] =
178  {
179  0.0,
180  },
181  sumpt_Hi_by_Bx[3] = {
182  0.0,
183  };
184 
185  if (verbosePileupInfo)
186  std::cout << "\n**** Pileup info begin" << std::endl;
187 
188  bool isCrazy = false;
189  for (unsigned ibx = 0; ibx < nBx; ++ibx) {
190  const PileupSummaryInfo& puInfo(info[ibx]);
191 
192  const int bx = puInfo.getBunchCrossing();
193  const int npu = puInfo.getPU_NumInteractions();
194  const std::vector<float>& lopt(puInfo.getPU_sumpT_lowpT());
195  const std::vector<float>& hipt(puInfo.getPU_sumpT_highpT());
196  const double losum = std::accumulate(lopt.begin(), lopt.end(), 0.0);
197  const double hisum = std::accumulate(hipt.begin(), hipt.end(), 0.0);
198 
199  if (losum >= crazyEnergyCut)
200  isCrazy = true;
201  if (hisum >= crazyEnergyCut)
202  isCrazy = true;
203 
204  totalNpu += npu;
205  sumpt_Lo += losum;
206  sumpt_Hi += hisum;
207 
208  const unsigned idx = bx < 0 ? 0U : (bx == 0 ? 1U : 2U);
209  npu_by_Bx[idx] += npu;
210  sumpt_Lo_by_Bx[idx] += losum;
211  sumpt_Hi_by_Bx[idx] += hisum;
212 
213  if (verbosePileupInfo)
214  std::cout << "ibx " << ibx << " bx " << bx << " npu " << npu << " losum " << losum << " hisum " << hisum
215  << std::endl;
216  }
217 
218  if (verbosePileupInfo)
219  std::cout << "**** Pileup info end\n" << std::endl;
220 
221  if (isCrazy) {
222  totalNpu = -1;
223  sumpt_Lo = 0.0;
224  sumpt_Hi = 0.0;
225  for (unsigned ibx = 0; ibx < 3; ++ibx) {
226  npu_by_Bx[ibx] = -1;
227  sumpt_Lo_by_Bx[ibx] = 0.0;
228  sumpt_Hi_by_Bx[ibx] = 0.0;
229  }
230  }
231 
232  if (collectPileup) {
233  ntupleData.push_back(totalNpu);
234  ntupleData.push_back(sumpt_Lo);
235  ntupleData.push_back(sumpt_Hi);
236  }
237 
238  if (collectOOTPileup)
239  for (unsigned ibx = 0; ibx < 3; ++ibx) {
240  ntupleData.push_back(npu_by_Bx[ibx]);
241  ntupleData.push_back(sumpt_Lo_by_Bx[ibx]);
242  ntupleData.push_back(sumpt_Hi_by_Bx[ibx]);
243  }
244 }
245 
246 // ------------ method called once each job just before starting event loop
248  // Come up with the list of variables
249  std::string vars = "cnt:run:event";
250  if (collectPileup)
251  vars += ":nbx:npu:sumptLowCut:sumptHiCut";
252  if (collectOOTPileup) {
253  vars += ":npu_negbx:sumptLowCut_negbx:sumptHiCut_negbx";
254  vars += ":npu_0bx:sumptLowCut_0bx:sumptHiCut_0bx";
255  vars += ":npu_posbx:sumptLowCut_posbx:sumptHiCut_posbx";
256  }
257  if (collectSummaries)
258  vars += ":estimate:pileup:uncert:uncertCode";
259  if (collectFastJetRho)
260  vars += ":fjrho:fjsigma";
261  if (collectGridDensity)
262  vars += ":gridEtDensity:gridEtDensityMixed";
263  if (collectVertexInfo)
264  vars += ":nPV";
265 
266  // Book the ntuple
268  nt = fs->make<TNtuple>(ntupleName.c_str(), ntupleTitle.c_str(), vars.c_str());
269  ntupleData.reserve(nt->GetNvar());
270 }
271 
272 // ------------ method called to for each event ------------
274  ntupleData.clear();
275  ntupleData.push_back(counter);
276  totalNpu = -1;
277  totalNPV = -1;
278 
279  edm::RunNumber_t const runnumber = iEvent.id().run();
280  edm::EventNumber_t const eventnumber = iEvent.id().event();
281  ntupleData.push_back(runnumber);
282  ntupleData.push_back(eventnumber);
283 
284  // Get pileup information from the pile-up information module
287  if (iEvent.getByToken(pileupToken, puInfo))
288  analyzePileup(*puInfo);
289  else {
290  if (collectPileup) {
291  ntupleData.push_back(-1);
292  ntupleData.push_back(-1);
293  ntupleData.push_back(0.f);
294  ntupleData.push_back(0.f);
295  }
296  if (collectOOTPileup)
297  for (unsigned ibx = 0; ibx < 3; ++ibx) {
298  ntupleData.push_back(-1);
299  ntupleData.push_back(0.f);
300  ntupleData.push_back(0.f);
301  }
302  }
303  }
304 
305  if (collectHistos) {
307  iEvent.getByToken(histoToken, input);
308 
310  TH2D* copy = new TH2D(*input);
311 
312  std::ostringstream os;
313  os << copy->GetName() << '_' << counter << '_' << totalNpu << '_' << runnumber << '_' << eventnumber;
314  const std::string& newname(os.str());
315  copy->SetNameTitle(newname.c_str(), newname.c_str());
316 
317  copy->SetDirectory(fs->getBareDirectory());
318  }
319 
320  if (collectSummaries) {
322  iEvent.getByToken(summaryToken, summary);
323 
324  ntupleData.push_back(summary->uncalibratedQuantile());
325  ntupleData.push_back(summary->pileupRho());
326  ntupleData.push_back(summary->pileupRhoUncertainty());
327  ntupleData.push_back(summary->uncertaintyCode());
328  }
329 
330  if (collectFastJetRho) {
331  edm::Handle<double> fjrho, fjsigma;
332  iEvent.getByToken(fastJetRhoToken, fjrho);
333  iEvent.getByToken(fastJetSigmaToken, fjsigma);
334 
335  ntupleData.push_back(*fjrho);
336  ntupleData.push_back(*fjsigma);
337  }
338 
339  if (collectGrids) {
341  iEvent.getByToken(gridToken, input);
342 
343  // Make sure the input grid is reasonable
344  const double* data = input->data();
345  assert(data);
346  assert(input->phiBin0Edge() == 0.0);
347  const unsigned nEta = input->nEtaBins();
348  const unsigned nPhi = input->nPhiBins();
349 
350  // Generate a name for the output histogram
351  std::ostringstream os;
352  os << "FFTJetGrid_" << counter << '_' << totalNpu << '_' << runnumber << '_' << eventnumber;
353  const std::string& newname(os.str());
354 
355  // Make a histogram and copy the grid data into it
357  TH2F* h =
358  fs->make<TH2F>(newname.c_str(), newname.c_str(), nEta, input->etaMin(), input->etaMax(), nPhi, 0.0, 2.0 * M_PI);
359  h->GetXaxis()->SetTitle("Eta");
360  h->GetYaxis()->SetTitle("Phi");
361  h->GetZaxis()->SetTitle("Transverse Energy");
362 
363  for (unsigned ieta = 0; ieta < nEta; ++ieta)
364  for (unsigned iphi = 0; iphi < nPhi; ++iphi)
365  h->SetBinContent(ieta + 1U, iphi + 1U, data[ieta * nPhi + iphi]);
366  }
367 
368  if (collectGridDensity) {
370  iEvent.getByToken(etSumToken, etSum);
371 
372  ntupleData.push_back(etSum->first);
373  ntupleData.push_back(etSum->second);
374  }
375 
376  if (collectVertexInfo) {
378  iEvent.getByToken(srcPVsToken, pvCollection);
379  totalNPV = 0;
380  if (!pvCollection->empty())
381  for (reco::VertexCollection::const_iterator pv = pvCollection->begin(); pv != pvCollection->end(); ++pv) {
382  const double ndof = pv->ndof();
383  if (!pv->isFake() && ndof > vertexNdofCut)
384  ++totalNPV;
385  }
386  ntupleData.push_back(totalNPV);
387  }
388 
389  assert(ntupleData.size() == static_cast<unsigned>(nt->GetNvar()));
390  nt->Fill(&ntupleData[0]);
391 
392  ++counter;
393 }
394 
395 // ------------ method called once each job just after ending the event loop
397 
398 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:38
EventNumber_t event() const
Definition: EventID.h:40
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
static const TGPicture * info(bool iBackgroundIsBlack)
#define init_param(type, varname)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
edm::EDGetTokenT< reco::VertexCollection > srcPVsToken
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileupToken
#define nullptr
unsigned long long EventNumber_t
FFTJetPileupAnalyzer()=delete
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
const int getBunchCrossing() const
void analyzePileup(const std::vector< PileupSummaryInfo > &pInfo)
static std::string const input
Definition: EdmProvDump.cc:48
FFTJetPileupAnalyzer & operator=(const FFTJetPileupAnalyzer &)=delete
const double * data() const
edm::EDGetTokenT< TH2D > histoToken
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const std::vector< float > & getPU_sumpT_highpT() const
TDirectory * getBareDirectory(const std::string &subdir="") const
Definition: TFileService.h:52
edm::EDGetTokenT< reco::FFTJetPileupSummary > summaryToken
def pv(vc)
Definition: MetAnalyzer.py:7
double f[11][100]
edm::EDGetTokenT< double > fastJetSigmaToken
#define M_PI
const int getPU_NumInteractions() const
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
edm::EDGetTokenT< double > fastJetRhoToken
unsigned int RunNumber_t
vars
Definition: DeepTauId.cc:158
edm::EDGetTokenT< reco::DiscretizedEnergyFlow > gridToken
std::vector< float > ntupleData
edm::EDGetTokenT< std::pair< double, double > > etSumToken
void analyze(const edm::Event &, const edm::EventSetup &) override
const std::vector< float > & getPU_sumpT_lowpT() const