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