CMS 3D CMS Logo

NPUTablesProducer.cc
Go to the documentation of this file.
10 
11 #include <vector>
12 #include <iostream>
13 
15 public:
17  : npuTag_(consumes<std::vector<PileupSummaryInfo>>(params.getParameter<edm::InputTag>("src"))),
18  pvTag_(consumes<std::vector<reco::Vertex>>(params.getParameter<edm::InputTag>("pvsrc"))),
19  vz_(params.getParameter<std::vector<double>>("zbins")) {
20  produces<nanoaod::FlatTable>();
21  }
22 
23  ~NPUTablesProducer() override {}
24 
25  void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
26  auto npuTab = std::make_unique<nanoaod::FlatTable>(1, "Pileup", true);
27 
29  iEvent.getByToken(pvTag_, pvsIn);
30  const double refpvz = (*pvsIn)[0].position().z();
31 
33  if (iEvent.getByToken(npuTag_, npuInfo)) {
34  fillNPUObjectTable(*npuInfo, *npuTab, refpvz);
35  }
36 
37  iEvent.put(std::move(npuTab));
38  }
39 
40  void fillNPUObjectTable(const std::vector<PileupSummaryInfo>& npuProd, nanoaod::FlatTable& out, double refpvz) const {
41  // Get BX 0
42  unsigned int bx0 = 0;
43  unsigned int nt = 0;
44  unsigned int npu = 0;
45 
46  auto zbin = std::lower_bound(vz_.begin(), vz_.end() - 1, std::abs(refpvz));
47  float pudensity = 0;
48  float gpudensity = 0;
49 
50  for (unsigned int ibx = 0; ibx < npuProd.size(); ibx++) {
51  if (npuProd[ibx].getBunchCrossing() == 0) {
52  bx0 = ibx;
53  nt = npuProd[ibx].getTrueNumInteractions();
54  npu = npuProd[ibx].getPU_NumInteractions();
55 
56  std::vector<float> zpositions;
57  unsigned int nzpositions = npuProd[ibx].getPU_zpositions().size();
58  for (unsigned int j = 0; j < nzpositions; ++j) {
59  zpositions.push_back(npuProd[ibx].getPU_zpositions()[j]);
60  if (std::abs(zpositions.back() - refpvz) < 0.1)
61  pudensity++; //N_PU/mm
62  auto bin = std::lower_bound(vz_.begin(), vz_.end() - 1, std::abs(zpositions.back()));
63  if (bin != vz_.end() && bin == zbin)
64  gpudensity++;
65  }
66  gpudensity /= (20.0 * (*(zbin) - *(zbin - 1)));
67  }
68  }
69  unsigned int eoot = 0;
70  for (unsigned int ipu = 0; ipu < bx0; ipu++) {
71  eoot += npuProd[ipu].getPU_NumInteractions();
72  }
73  unsigned int loot = 0;
74  for (unsigned int ipu = npuProd.size() - 1; ipu > bx0; ipu--) {
75  loot += npuProd[ipu].getPU_NumInteractions();
76  }
77  out.addColumnValue<float>("nTrueInt",
78  nt,
79  "the true mean number of the poisson distribution for this event from which the number "
80  "of interactions each bunch crossing has been sampled",
82  out.addColumnValue<int>(
83  "nPU",
84  npu,
85  "the number of pileup interactions that have been added to the event in the current bunch crossing",
87  out.addColumnValue<int>("sumEOOT", eoot, "number of early out of time pileup", nanoaod::FlatTable::IntColumn);
88  out.addColumnValue<int>("sumLOOT", loot, "number of late out of time pileup", nanoaod::FlatTable::IntColumn);
89  out.addColumnValue<float>("pudensity", pudensity, "PU vertices / mm", nanoaod::FlatTable::FloatColumn);
90  out.addColumnValue<float>(
91  "gpudensity", gpudensity, "Generator-level PU vertices / mm", nanoaod::FlatTable::FloatColumn);
92  }
93 
94  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
96  desc.add<edm::InputTag>("src", edm::InputTag("slimmedAddPileupInfo"))
97  ->setComment("tag for the PU information (vector<PileupSummaryInfo>)");
98  desc.add<edm::InputTag>("pvsrc", edm::InputTag("offlineSlimmedPrimaryVertices"))->setComment("tag for the PVs");
99  desc.add<std::vector<double>>("zbins", {})
100  ->setComment("Z bins to compute the generator-level number of PU vertices per mm");
101  descriptions.add("puTable", desc);
102  }
103 
104 protected:
107 
108  const std::vector<double> vz_;
109 };
110 
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
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)
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:124
int nt
Definition: AMPTWrapper.h:42
const edm::EDGetTokenT< std::vector< PileupSummaryInfo > > npuTag_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
fixed size matrix
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