CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GenProtonTableProducer.cc
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * This is a part of PPS offline software.
4  * Authors:
5  * Laurent Forthomme
6  * Michael Pitt
7  *
8  ****************************************************************************/
9 
10 #include <memory>
11 
18 
20 
23 
25 
27 public:
29  ~GenProtonTableProducer() override = default;
30 
32 
33 private:
34  void produce(edm::Event&, const edm::EventSetup&) override;
35 
40  const double tolerance_;
41  bool use_alt_coll_{false};
42 };
43 
45  : prunedCandsToken_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("srcPruned"))),
46  puCandsToken_(mayConsume<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("srcPUProtons"))),
47  puAltCandsToken_(mayConsume<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("srcAltPUProtons"))),
48  protonsCut_(iConfig.getParameter<std::string>("cut")),
49  table_name_(iConfig.getParameter<std::string>("name")),
50  tolerance_(iConfig.getParameter<double>("tolerance")) {
51  produces<nanoaod::FlatTable>();
52 }
53 
55  // define the variables
56  std::vector<float> pxs, pys, pzs, vzs;
57  std::vector<bool> isPUs;
58  // first loop over signal protons
59  for (const auto& pruned_cand : iEvent.get(prunedCandsToken_)) {
60  if (!protonsCut_(pruned_cand))
61  continue;
62  pxs.emplace_back(pruned_cand.px());
63  pys.emplace_back(pruned_cand.py());
64  pzs.emplace_back(pruned_cand.pz());
65  vzs.emplace_back(pruned_cand.vz());
66  isPUs.emplace_back(false);
67  }
68  // then loop over pruned candidates ; if already in signal protons, discard
70  if (use_alt_coll_ || !iEvent.getByToken(puCandsToken_, hPUCands))
71  use_alt_coll_ = iEvent.getByToken(puAltCandsToken_, hPUCands);
72  for (const auto& pu_cand : *hPUCands) {
73  if (!protonsCut_(pu_cand))
74  continue;
75  bool associated{false};
76  for (size_t i = 0; i < pzs.size(); ++i) {
77  if (fabs(1. - pxs.at(i) / pu_cand.px()) < tolerance_ && fabs(1. - pys.at(i) / pu_cand.py()) < tolerance_ &&
78  fabs(1. - pzs.at(i) / pu_cand.pz()) < tolerance_) {
79  associated = true;
80  break;
81  }
82  }
83  if (associated)
84  continue;
85  pxs.emplace_back(pu_cand.px());
86  pys.emplace_back(pu_cand.py());
87  pzs.emplace_back(pu_cand.pz());
88  vzs.emplace_back(pu_cand.vz());
89  isPUs.emplace_back(true);
90  }
91 
92  auto protons_table = std::make_unique<nanoaod::FlatTable>(isPUs.size(), table_name_, false);
93  protons_table->addColumn<float>("px", pxs, "proton horizontal momentum", 8);
94  protons_table->addColumn<float>("py", pys, "proton vertical momentum", 8);
95  protons_table->addColumn<float>("pz", pzs, "proton longitudinal momentum", 8);
96  protons_table->addColumn<float>("vz", vzs, "proton vertex longitudinal coordinate", 8);
97  protons_table->addColumn<bool>("isPU", isPUs, "pileup proton?");
98  iEvent.put(std::move(protons_table));
99 }
100 
103  desc.add<edm::InputTag>("srcPruned", edm::InputTag("prunedGenParticles"))
104  ->setComment("input source for pruned gen-level particle candidates");
105  desc.add<edm::InputTag>("srcPUProtons", edm::InputTag("genPUProtons"))
106  ->setComment("input source for pileup protons collection");
107  desc.add<edm::InputTag>("srcAltPUProtons", edm::InputTag("genPUProtons", "genPUProtons"))
108  ->setComment("alternative input source for pileup protons collection (for premix-mix backward compatibility)");
109  desc.add<std::string>("cut", "")->setComment("proton kinematic selection");
110  desc.add<std::string>("name", "GenProton")->setComment("flat table name");
111  desc.add<std::string>("doc", "generator level information on (signal+PU) protons")
112  ->setComment("flat table description");
113  desc.add<double>("tolerance", 1.e-3)->setComment("relative difference between the signal and pileup protons momenta");
114  descriptions.add("genProtonTable", desc);
115 }
116 
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
GenProtonTableProducer(const edm::ParameterSet &)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::EDGetTokenT< reco::GenParticleCollection > puAltCandsToken_
int iEvent
Definition: GenABIO.cc:224
static void fillDescriptions(edm::ConfigurationDescriptions &)
def move
Definition: eostools.py:511
const StringCutObjectSelector< reco::Candidate > protonsCut_
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void produce(edm::Event &, const edm::EventSetup &) override
const edm::EDGetTokenT< reco::GenParticleCollection > puCandsToken_
bool use_alt_coll_
Are we using premix/mix collection name for PU protons?
void add(std::string const &label, ParameterSetDescription const &psetDescription)
~GenProtonTableProducer() override=default
const edm::EDGetTokenT< reco::GenParticleCollection > prunedCandsToken_