CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // $Id: FFTJetPileupAnalyzer.cc,v 1.12 2011/07/18 17:40:54 igv Exp $
17 //
18 //
19 
20 #include <cassert>
21 #include <sstream>
22 #include <numeric>
23 
24 #include "TNtuple.h"
25 
26 // user include files
33 
39 
41 
43 
45 
46 #define init_param(type, varname) varname (ps.getParameter< type >( #varname ))
47 
48 //
49 // class declaration
50 //
52 {
53 public:
54  explicit FFTJetPileupAnalyzer(const edm::ParameterSet&);
56 
57 private:
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  virtual void beginJob() ;
67  virtual void analyze(const edm::Event&, const edm::EventSetup&);
68  virtual void endJob() ;
69 
76  std::string pileupLabel;
77  std::string ntupleName;
78  std::string ntupleTitle;
88 
89  double vertexNdofCut;
91 
92  std::vector<float> ntupleData;
93  TNtuple* nt;
94  int totalNpu;
95  int totalNPV;
96  unsigned long counter;
97 };
98 
99 //
100 // constructors and destructor
101 //
103  : init_param(edm::InputTag, histoLabel),
104  init_param(edm::InputTag, summaryLabel),
105  init_param(edm::InputTag, fastJetRhoLabel),
106  init_param(edm::InputTag, fastJetSigmaLabel),
107  init_param(edm::InputTag, gridLabel),
108  init_param(edm::InputTag, srcPVs),
109  init_param(std::string, pileupLabel),
110  init_param(std::string, ntupleName),
111  init_param(std::string, ntupleTitle),
112  init_param(bool, collectHistos),
113  init_param(bool, collectSummaries),
114  init_param(bool, collectFastJetRho),
115  init_param(bool, collectPileup),
116  init_param(bool, collectOOTPileup),
117  init_param(bool, collectGrids),
118  init_param(bool, collectGridDensity),
119  init_param(bool, collectVertexInfo),
120  init_param(bool, verbosePileupInfo),
121  init_param(double, vertexNdofCut),
122  init_param(double, crazyEnergyCut),
123  nt(0),
124  totalNpu(-1),
125  totalNPV(-1),
126  counter(0)
127 {
128 }
129 
130 
132 {
133 }
134 
135 
136 //
137 // member functions
138 //
140  const std::vector<PileupSummaryInfo>& info)
141 {
142  const unsigned nBx = info.size();
143  if (collectPileup)
144  ntupleData.push_back(static_cast<float>(nBx));
145 
146  double sumpt_Lo = 0.0, sumpt_Hi = 0.0;
147  totalNpu = 0;
148 
149  int npu_by_Bx[3] = {0,};
150  double sumpt_Lo_by_Bx[3] = {0.0,}, sumpt_Hi_by_Bx[3] = {0.0,};
151 
152  if (verbosePileupInfo)
153  std::cout << "\n**** Pileup info begin" << std::endl;
154 
155  bool isCrazy = false;
156  for (unsigned ibx = 0; ibx < nBx; ++ibx)
157  {
158  const PileupSummaryInfo& puInfo(info[ibx]);
159 
160  const int bx = puInfo.getBunchCrossing();
161  const int npu = puInfo.getPU_NumInteractions();
162  const std::vector<float>& lopt(puInfo.getPU_sumpT_lowpT());
163  const std::vector<float>& hipt(puInfo.getPU_sumpT_highpT());
164  const double losum = std::accumulate(lopt.begin(), lopt.end(), 0.0);
165  const double hisum = std::accumulate(hipt.begin(), hipt.end(), 0.0);
166 
167  if (losum >= crazyEnergyCut)
168  isCrazy = true;
169  if (hisum >= crazyEnergyCut)
170  isCrazy = true;
171 
172  totalNpu += npu;
173  sumpt_Lo += losum;
174  sumpt_Hi += hisum;
175 
176  const unsigned idx = bx < 0 ? 0U : (bx == 0 ? 1U : 2U);
177  npu_by_Bx[idx] += npu;
178  sumpt_Lo_by_Bx[idx] += losum;
179  sumpt_Hi_by_Bx[idx] += hisum;
180 
181  if (verbosePileupInfo)
182  std::cout << "ibx " << ibx << " bx " << bx
183  << " npu " << npu << " losum " << losum
184  << " hisum " << hisum
185  << std::endl;
186  }
187 
188  if (verbosePileupInfo)
189  std::cout << "**** Pileup info end\n" << std::endl;
190 
191  if (isCrazy)
192  {
193  totalNpu = -1;
194  sumpt_Lo = 0.0;
195  sumpt_Hi = 0.0;
196  for (unsigned ibx = 0; ibx < 3; ++ibx)
197  {
198  npu_by_Bx[ibx] = -1;
199  sumpt_Lo_by_Bx[ibx] = 0.0;
200  sumpt_Hi_by_Bx[ibx] = 0.0;
201  }
202  }
203 
204  if (collectPileup)
205  {
206  ntupleData.push_back(totalNpu);
207  ntupleData.push_back(sumpt_Lo);
208  ntupleData.push_back(sumpt_Hi);
209  }
210 
211  if (collectOOTPileup)
212  for (unsigned ibx = 0; ibx < 3; ++ibx)
213  {
214  ntupleData.push_back(npu_by_Bx[ibx]);
215  ntupleData.push_back(sumpt_Lo_by_Bx[ibx]);
216  ntupleData.push_back(sumpt_Hi_by_Bx[ibx]);
217  }
218 }
219 
220 
221 // ------------ method called once each job just before starting event loop
223 {
224  // Come up with the list of variables
225  std::string vars = "cnt:run:event";
226  if (collectPileup)
227  vars += ":nbx:npu:sumptLowCut:sumptHiCut";
228  if (collectOOTPileup)
229  {
230  vars += ":npu_negbx:sumptLowCut_negbx:sumptHiCut_negbx";
231  vars += ":npu_0bx:sumptLowCut_0bx:sumptHiCut_0bx";
232  vars += ":npu_posbx:sumptLowCut_posbx:sumptHiCut_posbx";
233  }
234  if (collectSummaries)
235  vars += ":estimate:pileup:uncert:uncertCode";
236  if (collectFastJetRho)
237  vars += ":fjrho:fjsigma";
238  if (collectGridDensity)
239  vars += ":gridEtDensity:gridEtDensityMixed";
240  if (collectVertexInfo)
241  vars += ":nPV";
242 
243  // Book the ntuple
245  nt = fs->make<TNtuple>(ntupleName.c_str(), ntupleTitle.c_str(),
246  vars.c_str());
247  ntupleData.reserve(nt->GetNvar());
248 }
249 
250 
251 // ------------ method called to for each event ------------
253  const edm::EventSetup& iSetup)
254 {
255  ntupleData.clear();
256  ntupleData.push_back(counter);
257  totalNpu = -1;
258  totalNPV = -1;
259 
260  const long runnumber = iEvent.id().run();
261  const long eventnumber = iEvent.id().event();
262  ntupleData.push_back(runnumber);
263  ntupleData.push_back(eventnumber);
264 
265  // Get pileup information from the pile-up information module
267  {
269  if (iEvent.getByLabel(pileupLabel, puInfo))
270  analyzePileup(*puInfo);
271  else
272  {
273  if (collectPileup)
274  {
275  ntupleData.push_back(-1);
276  ntupleData.push_back(-1);
277  ntupleData.push_back(0.f);
278  ntupleData.push_back(0.f);
279  }
280  if (collectOOTPileup)
281  for (unsigned ibx = 0; ibx < 3; ++ibx)
282  {
283  ntupleData.push_back(-1);
284  ntupleData.push_back(0.f);
285  ntupleData.push_back(0.f);
286  }
287  }
288  }
289 
290  if (collectHistos)
291  {
293  iEvent.getByLabel(histoLabel, input);
294 
296  TH2D* copy = new TH2D(*input);
297 
298  std::ostringstream os;
299  os << copy->GetName() << '_' << counter << '_'
300  << totalNpu << '_' << runnumber << '_' << eventnumber;
301  const std::string& newname(os.str());
302  copy->SetNameTitle(newname.c_str(), newname.c_str());
303 
304  copy->SetDirectory(fs->getBareDirectory());
305  }
306 
307  if (collectSummaries)
308  {
310  iEvent.getByLabel(summaryLabel, summary);
311 
312  ntupleData.push_back(summary->uncalibratedQuantile());
313  ntupleData.push_back(summary->pileupRho());
314  ntupleData.push_back(summary->pileupRhoUncertainty());
315  ntupleData.push_back(summary->uncertaintyCode());
316  }
317 
318  if (collectFastJetRho)
319  {
320  edm::Handle<double> fjrho, fjsigma;
321  iEvent.getByLabel(fastJetRhoLabel, fjrho);
322  iEvent.getByLabel(fastJetSigmaLabel, fjsigma);
323 
324  ntupleData.push_back(*fjrho);
325  ntupleData.push_back(*fjsigma);
326  }
327 
328  if (collectGrids)
329  {
331  iEvent.getByLabel(gridLabel, input);
332 
333  // Make sure the input grid is reasonable
334  const double* data = input->data();
335  assert(data);
336  assert(input->phiBin0Edge() == 0.0);
337  const unsigned nEta = input->nEtaBins();
338  const unsigned nPhi = input->nPhiBins();
339 
340  // Generate a name for the output histogram
341  std::ostringstream os;
342  os << "FFTJetGrid_" << counter << '_'
343  << totalNpu << '_' << runnumber << '_' << eventnumber;
344  const std::string& newname(os.str());
345 
346  // Make a histogram and copy the grid data into it
348  TH2F* h = fs->make<TH2F>(newname.c_str(), newname.c_str(),
349  nEta, input->etaMin(), input->etaMax(),
350  nPhi, 0.0, 2.0*M_PI);
351  h->GetXaxis()->SetTitle("Eta");
352  h->GetYaxis()->SetTitle("Phi");
353  h->GetZaxis()->SetTitle("Transverse Energy");
354 
355  for (unsigned ieta=0; ieta<nEta; ++ieta)
356  for (unsigned iphi=0; iphi<nPhi; ++iphi)
357  h->SetBinContent(ieta+1U, iphi+1U, data[ieta*nPhi + iphi]);
358  }
359 
360  if (collectGridDensity)
361  {
363  iEvent.getByLabel(histoLabel, etSum);
364 
365  ntupleData.push_back(etSum->first);
366  ntupleData.push_back(etSum->second);
367  }
368 
369  if (collectVertexInfo)
370  {
372  iEvent.getByLabel(srcPVs, pvCollection);
373  totalNPV = 0;
374  if (!pvCollection->empty())
375  for (reco::VertexCollection::const_iterator pv = pvCollection->begin();
376  pv != pvCollection->end(); ++pv)
377  {
378  const double ndof = pv->ndof();
379  if (!pv->isFake() && ndof > vertexNdofCut)
380  ++totalNPV;
381  }
382  ntupleData.push_back(totalNPV);
383  }
384 
385  assert(ntupleData.size() == static_cast<unsigned>(nt->GetNvar()));
386  nt->Fill(&ntupleData[0]);
387 
388  ++counter;
389 }
390 
391 
392 // ------------ method called once each job just after ending the event loop
394 {
395 }
396 
397 
398 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:42
EventNumber_t event() const
Definition: EventID.h:44
TDirectory * getBareDirectory(const std::string &subdir="") const
#define init_param(type, varname)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const int getBunchCrossing() const
void analyzePileup(const std::vector< PileupSummaryInfo > &pInfo)
int iEvent
Definition: GenABIO.cc:243
const std::vector< float > & getPU_sumpT_highpT() const
double f[11][100]
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
int nt
Definition: AMPTWrapper.h:32
#define M_PI
Definition: BFit3D.cc:3
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
virtual void analyze(const edm::Event &, const edm::EventSetup &)
const int getPU_NumInteractions() const
FFTJetPileupAnalyzer & operator=(const FFTJetPileupAnalyzer &)
edm::EventID id() const
Definition: EventBase.h:56
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
T * make() const
make new ROOT object
tuple cout
Definition: gather_cfg.py:121
std::vector< float > ntupleData
const std::vector< float > & getPU_sumpT_lowpT() const