File indexing completed on 2024-04-06 12:13:40
0001
0002
0003
0004 #include <memory>
0005 #include <string>
0006 #include <cmath>
0007 #include <vector>
0008 #include <algorithm>
0009
0010
0011 #include "FWCore/Framework/interface/Frameworkfwd.h"
0012 #include "FWCore/Framework/interface/global/EDFilter.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Framework/interface/MakerMacros.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
0017
0018 class MCParticleModuloFilter : public edm::global::EDFilter<> {
0019 public:
0020 explicit MCParticleModuloFilter(const edm::ParameterSet&);
0021 ~MCParticleModuloFilter() override {}
0022
0023 bool filter(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0024
0025 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0026
0027 private:
0028
0029 const edm::EDGetTokenT<edm::HepMCProduct> token_;
0030 std::vector<int> particleIDs_;
0031 unsigned multipleOf_;
0032 bool absID_;
0033 unsigned min_;
0034 int status_;
0035 };
0036
0037 MCParticleModuloFilter::MCParticleModuloFilter(const edm::ParameterSet& iConfig)
0038 : token_(consumes<edm::HepMCProduct>(iConfig.getParameter<edm::InputTag>("moduleLabel"))),
0039 particleIDs_(iConfig.getParameter<std::vector<int>>("particleIDs")),
0040 multipleOf_(iConfig.getParameter<unsigned>("multipleOf")),
0041 absID_(iConfig.getParameter<bool>("absID")),
0042 min_(iConfig.getParameter<unsigned>("min")),
0043 status_(iConfig.getParameter<int>("status")) {
0044
0045 std::sort(particleIDs_.begin(), particleIDs_.end());
0046 }
0047
0048 bool MCParticleModuloFilter::filter(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0049
0050 edm::Handle<edm::HepMCProduct> h_evt;
0051 iEvent.getByToken(token_, h_evt);
0052 const HepMC::GenEvent* myGenEvent = h_evt->GetEvent();
0053
0054 unsigned sum_part = 0;
0055 for (auto i_part = myGenEvent->particles_begin(); i_part != myGenEvent->particles_end(); ++i_part) {
0056 int id_part = (*i_part)->pdg_id();
0057 if (absID_)
0058 id_part = std::abs(id_part);
0059 if (std::binary_search(particleIDs_.begin(), particleIDs_.end(), id_part) and
0060 (status_ == 0 or (*i_part)->status() == status_))
0061 ++sum_part;
0062 }
0063
0064 return (sum_part % multipleOf_ == 0) and (sum_part >= min_);
0065 }
0066
0067 void MCParticleModuloFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0068 edm::ParameterSetDescription desc;
0069 desc.add<edm::InputTag>("moduleLabel", edm::InputTag("generator", "unsmeared"));
0070 desc.add<std::vector<int>>("particleIDs", {});
0071 desc.add<unsigned>("multipleOf", 1);
0072 desc.add<bool>("absID", false);
0073 desc.add<unsigned>("min", 0);
0074 desc.add<int>("status", 0);
0075
0076 descriptions.add("MCParticleModuloFilter", desc);
0077 }
0078
0079
0080 DEFINE_FWK_MODULE(MCParticleModuloFilter);