File indexing completed on 2024-04-06 12:01:02
0001 #ifndef RecoSelectors_GenParticleCustomSelector_h
0002 #define RecoSelectors_GenParticleCustomSelector_h
0003
0004
0005
0006
0007
0008
0009 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0010
0011 class GenParticleCustomSelector {
0012 public:
0013 GenParticleCustomSelector() {}
0014 GenParticleCustomSelector(double ptMin,
0015 double minRapidity,
0016 double maxRapidity,
0017 double tip,
0018 double lip,
0019 bool chargedOnly,
0020 int status,
0021 const std::vector<int>& pdgId = std::vector<int>(),
0022 bool invertRapidityCut = false,
0023 double minPhi = -3.2,
0024 double maxPhi = 3.2)
0025 : ptMin_(ptMin),
0026 minRapidity_(minRapidity),
0027 maxRapidity_(maxRapidity),
0028 meanPhi_((minPhi + maxPhi) / 2.),
0029 rangePhi_((maxPhi - minPhi) / 2.),
0030 tip_(tip),
0031 lip_(lip),
0032 chargedOnly_(chargedOnly),
0033 status_(status),
0034 pdgId_(pdgId),
0035 invertRapidityCut_(invertRapidityCut) {
0036 if (minPhi >= maxPhi) {
0037 throw cms::Exception("Configuration")
0038 << "GenParticleCustomSelector: minPhi (" << minPhi << ") must be smaller than maxPhi (" << maxPhi
0039 << "). The range is constructed from minPhi to maxPhi around their "
0040 "average.";
0041 }
0042 if (minPhi >= M_PI) {
0043 throw cms::Exception("Configuration") << "GenParticleCustomSelector: minPhi (" << minPhi
0044 << ") must be smaller than PI. The range is constructed from minPhi "
0045 "to maxPhi around their average.";
0046 }
0047 if (maxPhi <= -M_PI) {
0048 throw cms::Exception("Configuration") << "GenParticleCustomSelector: maxPhi (" << maxPhi
0049 << ") must be larger than -PI. The range is constructed from minPhi "
0050 "to maxPhi around their average.";
0051 }
0052 }
0053
0054
0055 bool operator()(const reco::GenParticle& tp) const {
0056 if (chargedOnly_ && tp.charge() == 0)
0057 return false;
0058 bool testId = false;
0059 unsigned int idSize = pdgId_.size();
0060 if (idSize == 0)
0061 testId = true;
0062 else
0063 for (unsigned int it = 0; it != idSize; ++it) {
0064 if (tp.pdgId() == pdgId_[it])
0065 testId = true;
0066 }
0067
0068 auto etaOk = [&](const reco::GenParticle& p) -> bool {
0069 float eta = p.eta();
0070 if (!invertRapidityCut_)
0071 return (eta >= minRapidity_) && (eta <= maxRapidity_);
0072 else
0073 return (eta < minRapidity_ || eta > maxRapidity_);
0074 };
0075 auto phiOk = [&](const reco::GenParticle& p) {
0076 float dphi = deltaPhi(atan2f(p.py(), p.px()), meanPhi_);
0077 return dphi >= -rangePhi_ && dphi <= rangePhi_;
0078 };
0079 auto ptOk = [&](const reco::GenParticle& p) {
0080 double pt = p.pt();
0081 return pt >= ptMin_;
0082 };
0083
0084 return (ptOk(tp) && etaOk(tp) && phiOk(tp) && sqrt(tp.vertex().perp2()) <= tip_ && fabs(tp.vertex().z()) <= lip_ &&
0085 tp.status() == status_ && testId);
0086 }
0087
0088 private:
0089 double ptMin_;
0090 double minRapidity_;
0091 double maxRapidity_;
0092 float meanPhi_;
0093 float rangePhi_;
0094 double tip_;
0095 double lip_;
0096 bool chargedOnly_;
0097 int status_;
0098 std::vector<int> pdgId_;
0099 bool invertRapidityCut_;
0100 };
0101
0102 #include "FWCore/Framework/interface/ConsumesCollector.h"
0103 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0104
0105 namespace reco {
0106 namespace modules {
0107
0108 template <>
0109 struct ParameterAdapter<GenParticleCustomSelector> {
0110 static GenParticleCustomSelector make(const edm::ParameterSet& cfg, edm::ConsumesCollector& iC) {
0111 return make(cfg);
0112 }
0113
0114 static GenParticleCustomSelector make(const edm::ParameterSet& cfg) {
0115 return GenParticleCustomSelector(cfg.getParameter<double>("ptMin"),
0116 cfg.getParameter<double>("minRapidity"),
0117 cfg.getParameter<double>("maxRapidity"),
0118 cfg.getParameter<double>("tip"),
0119 cfg.getParameter<double>("lip"),
0120 cfg.getParameter<bool>("chargedOnly"),
0121 cfg.getParameter<int>("status"),
0122 cfg.getParameter<std::vector<int> >("pdgId"),
0123 cfg.getParameter<bool>("invertRapidityCut"),
0124 cfg.getParameter<double>("minPhi"),
0125 cfg.getParameter<double>("maxPhi"));
0126 }
0127 };
0128
0129 }
0130 }
0131
0132 #endif