CMS 3D CMS Logo

NPUTablesProducer.cc
Go to the documentation of this file.
10 
11 #include <vector>
12 #include <iostream>
13 #include <algorithm>
14 
16 public:
18  : npuTag_(consumes<std::vector<PileupSummaryInfo>>(params.getParameter<edm::InputTag>("src"))),
19  pvTag_(consumes<std::vector<reco::Vertex>>(params.getParameter<edm::InputTag>("pvsrc"))),
20  vz_(params.getParameter<std::vector<double>>("zbins")),
21  savePtHatMax_(params.getParameter<bool>("savePtHatMax")) {
22  produces<nanoaod::FlatTable>();
23  }
24 
25  ~NPUTablesProducer() override {}
26 
27  void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
28  auto npuTab = std::make_unique<nanoaod::FlatTable>(1, "Pileup", true);
29 
31  iEvent.getByToken(pvTag_, pvsIn);
32  const double refpvz = (*pvsIn)[0].position().z();
33 
35  if (iEvent.getByToken(npuTag_, npuInfo)) {
36  fillNPUObjectTable(*npuInfo, *npuTab, refpvz);
37  }
38 
39  iEvent.put(std::move(npuTab));
40  }
41 
42  void fillNPUObjectTable(const std::vector<PileupSummaryInfo>& npuProd, nanoaod::FlatTable& out, double refpvz) const {
43  // Get BX 0
44  unsigned int bx0 = 0;
45  unsigned int nt = 0;
46  unsigned int npu = 0;
47 
48  auto zbin = std::lower_bound(vz_.begin(), vz_.end() - 1, std::abs(refpvz));
49  float pudensity = 0;
50  float gpudensity = 0;
51  float pthatmax = 0;
52 
53  for (unsigned int ibx = 0; ibx < npuProd.size(); ibx++) {
54  if (npuProd[ibx].getBunchCrossing() == 0) {
55  bx0 = ibx;
56  nt = npuProd[ibx].getTrueNumInteractions();
57  npu = npuProd[ibx].getPU_NumInteractions();
58 
59  std::vector<float> zpositions;
60  unsigned int nzpositions = npuProd[ibx].getPU_zpositions().size();
61  for (unsigned int j = 0; j < nzpositions; ++j) {
62  zpositions.push_back(npuProd[ibx].getPU_zpositions()[j]);
63  if (std::abs(zpositions.back() - refpvz) < 0.1)
64  pudensity++; //N_PU/mm
65  auto bin = std::lower_bound(vz_.begin(), vz_.end() - 1, std::abs(zpositions.back()));
66  if (bin != vz_.end() && bin == zbin)
67  gpudensity++;
68  }
69  gpudensity /= (20.0 * (*(zbin) - *(zbin - 1)));
70  if (savePtHatMax_) {
71  pthatmax = *max_element(npuProd[ibx].getPU_pT_hats().begin(), npuProd[ibx].getPU_pT_hats().end());
72  }
73  }
74  }
75  unsigned int eoot = 0;
76  for (unsigned int ipu = 0; ipu < bx0; ipu++) {
77  eoot += npuProd[ipu].getPU_NumInteractions();
78  }
79  unsigned int loot = 0;
80  for (unsigned int ipu = npuProd.size() - 1; ipu > bx0; ipu--) {
81  loot += npuProd[ipu].getPU_NumInteractions();
82  }
83  out.addColumnValue<float>("nTrueInt",
84  nt,
85  "the true mean number of the poisson distribution for this event from which the number "
86  "of interactions each bunch crossing has been sampled",
88  out.addColumnValue<int>(
89  "nPU",
90  npu,
91  "the number of pileup interactions that have been added to the event in the current bunch crossing",
93  out.addColumnValue<int>("sumEOOT", eoot, "number of early out of time pileup", nanoaod::FlatTable::IntColumn);
94  out.addColumnValue<int>("sumLOOT", loot, "number of late out of time pileup", nanoaod::FlatTable::IntColumn);
95  out.addColumnValue<float>("pudensity", pudensity, "PU vertices / mm", nanoaod::FlatTable::FloatColumn);
96  out.addColumnValue<float>(
97  "gpudensity", gpudensity, "Generator-level PU vertices / mm", nanoaod::FlatTable::FloatColumn);
98  if (savePtHatMax_) {
99  out.addColumnValue<float>("pthatmax", pthatmax, "Maximum pt-hat", nanoaod::FlatTable::FloatColumn);
100  }
101  }
102 
105  desc.add<edm::InputTag>("src", edm::InputTag("slimmedAddPileupInfo"))
106  ->setComment("tag for the PU information (vector<PileupSummaryInfo>)");
107  desc.add<edm::InputTag>("pvsrc", edm::InputTag("offlineSlimmedPrimaryVertices"))->setComment("tag for the PVs");
108  desc.add<std::vector<double>>("zbins", {})
109  ->setComment("Z bins to compute the generator-level number of PU vertices per mm");
110  desc.add<bool>("savePtHatMax", false)->setComment("Store maximum pt-hat of PU");
111  descriptions.add("puTable", desc);
112  }
113 
114 protected:
117 
118  const std::vector<double> vz_;
119 
121 };
122 
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
void produce(edm::StreamID id, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
NPUTablesProducer(edm::ParameterSet const &params)
#define end
Definition: vmac.h:39
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void addColumnValue(const std::string &name, const C &value, const std::string &docString, ColumnType type=defaultColumnType< T >(), int mantissaBits=-1)
Definition: FlatTable.h:101
int nt
Definition: AMPTWrapper.h:32
bin
set the eta bin as selection string.
const edm::EDGetTokenT< std::vector< PileupSummaryInfo > > npuTag_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
fixed size matrix
#define begin
Definition: vmac.h:32
HLT enums.
~NPUTablesProducer() override
void fillNPUObjectTable(const std::vector< PileupSummaryInfo > &npuProd, nanoaod::FlatTable &out, double refpvz) const
const edm::EDGetTokenT< std::vector< reco::Vertex > > pvTag_
const std::vector< double > vz_
def move(src, dest)
Definition: eostools.py:511