CMS 3D CMS Logo

TrackingParticleSelector.h
Go to the documentation of this file.
1 #ifndef SimTracker_Common_TrackingParticleSelector_h
2 #define SimTracker_Common_TrackingParticleSelector_h
3 /* \class TrackingParticleSelector
4  *
5  * \author Giuseppe Cerati, INFN
6  *
7  * $Date: 2013/05/14 15:46:46 $
8  * $Revision: 1.5.4.2 $
9  *
10  */
15 
17 public:
20  double ptMax,
21  double minRapidity,
22  double maxRapidity,
23  double tip,
24  double lip,
25  int minHit,
26  bool signalOnly,
27  bool intimeOnly,
28  bool chargedOnly,
29  bool stableOnly,
30  const std::vector<int> &pdgId = std::vector<int>(),
31  bool invertRapidityCut = false,
32  double minPhi = -3.2,
33  double maxPhi = 3.2)
34  : ptMin2_(ptMin * ptMin),
35  ptMax2_(ptMax * ptMax),
38  meanPhi_((minPhi + maxPhi) / 2.),
39  rangePhi_((maxPhi - minPhi) / 2.),
40  tip2_(tip * tip),
41  lip_(lip),
42  minHit_(minHit),
47  pdgId_(pdgId),
49  if (minPhi >= maxPhi) {
50  throw cms::Exception("Configuration")
51  << "TrackingParticleSelector: minPhi (" << minPhi << ") must be smaller than maxPhi (" << maxPhi
52  << "). The range is constructed from minPhi to maxPhi around their "
53  "average.";
54  }
55  if (minPhi >= M_PI) {
56  throw cms::Exception("Configuration") << "TrackingParticleSelector: minPhi (" << minPhi
57  << ") must be smaller than PI. The range is constructed from minPhi "
58  "to maxPhi around their average.";
59  }
60  if (maxPhi <= -M_PI) {
61  throw cms::Exception("Configuration") << "TrackingParticleSelector: maxPhi (" << maxPhi
62  << ") must be larger than -PI. The range is constructed from minPhi "
63  "to maxPhi around their average.";
64  }
65  }
66 
67  bool isCharged(const TrackingParticle *tp) const { return (tp->charge() == 0 ? false : true); }
68 
69  bool isInTime(const TrackingParticle *tp) const { return (tp->eventId().bunchCrossing() == 0); }
70 
71  bool isSignal(const TrackingParticle *tp) const {
72  return (tp->eventId().bunchCrossing() == 0 && tp->eventId().event() == 0);
73  }
74 
75  bool isStable(const TrackingParticle *tp) const {
76  for (TrackingParticle::genp_iterator j = tp->genParticle_begin(); j != tp->genParticle_end(); ++j) {
77  if (j->get() == nullptr || j->get()->status() != 1) {
78  return false;
79  }
80  }
81  // test for remaining unstabled due to lack of genparticle pointer
82  int pdgid = std::abs(tp->pdgId());
83  if (tp->status() == -99 && (pdgid != 11 && pdgid != 13 && pdgid != 211 && pdgid != 321 && pdgid != 2212 &&
84  pdgid != 3112 && pdgid != 3222 && pdgid != 3312 && pdgid != 3334)) {
85  return false;
86  }
87  return true;
88  }
89 
92  bool operator()(const TrackingParticle &tp) const { return select(&tp); }
93  bool operator()(const TrackingParticle *tp) const { return select(tp); }
94 
95  bool select(const TrackingParticle *tp) const {
96  // signal only means no PU particles
97  if (signalOnly_ && !isSignal(tp))
98  return false;
99  // intime only means no OOT PU particles
100  if (intimeOnly_ && !isInTime(tp))
101  return false;
102 
103  // select only if charge!=0
104  if (chargedOnly_ && !isCharged(tp))
105  return false;
106 
107  // select for particle type
108  if (!selectParticleType(tp)) {
109  return false;
110  }
111 
112  // select only stable particles
113  if (stableOnly_ && !isStable(tp)) {
114  return false;
115  }
116 
117  return selectKinematics(tp);
118  }
119 
120  bool selectKinematics(const TrackingParticle *tp) const {
121  auto etaOk = [&](const TrackingParticle *p) -> bool {
122  float eta = etaFromXYZ(p->px(), p->py(), p->pz());
123  if (!invertRapidityCut_)
124  return (eta >= minRapidity_) && (eta <= maxRapidity_);
125  else
126  return (eta < minRapidity_ || eta > maxRapidity_);
127  };
128  auto phiOk = [&](const TrackingParticle *p) {
129  float dphi = deltaPhi(atan2f(p->py(), p->px()), meanPhi_);
130  return dphi >= -rangePhi_ && dphi <= rangePhi_;
131  };
132  auto ptOk = [&](const TrackingParticle *p) {
133  double pt2 = tp->p4().perp2();
134  return pt2 >= ptMin2_ && pt2 <= ptMax2_;
135  };
136  return (tp->numberOfTrackerLayers() >= minHit_ && ptOk(tp) && etaOk(tp) && phiOk(tp) &&
137  std::abs(tp->vertex().z()) <= lip_ && // vertex last to avoid to load it if not striclty
138  // necessary...
139  tp->vertex().perp2() <= tip2_);
140  }
141 
143  auto pdgid = tp->pdgId();
144  if (!pdgId_.empty()) {
145  for (auto id : pdgId_) {
146  if (id == pdgid) {
147  return true;
148  }
149  }
150  } else {
151  return true;
152  }
153  return false;
154  }
155 
156 private:
157  double ptMin2_;
158  double ptMax2_;
161  float meanPhi_;
162  float rangePhi_;
163  double tip2_;
164  double lip_;
165  int minHit_;
170  std::vector<int> pdgId_;
172 };
173 
176 
177 namespace reco {
178  namespace modules {
179 
180  template <>
183  return make(cfg);
184  }
185 
187  return TrackingParticleSelector(cfg.getParameter<double>("ptMin"),
188  cfg.getParameter<double>("ptMax"),
189  cfg.getParameter<double>("minRapidity"),
190  cfg.getParameter<double>("maxRapidity"),
191  cfg.getParameter<double>("tip"),
192  cfg.getParameter<double>("lip"),
193  cfg.getParameter<int>("minHit"),
194  cfg.getParameter<bool>("signalOnly"),
195  cfg.getParameter<bool>("intimeOnly"),
196  cfg.getParameter<bool>("chargedOnly"),
197  cfg.getParameter<bool>("stableOnly"),
198  cfg.getParameter<std::vector<int>>("pdgId"),
199  cfg.getParameter<bool>("invertRapidityCut"),
200  cfg.getParameter<double>("minPhi"),
201  cfg.getParameter<double>("maxPhi"));
202  }
203  };
204 
205  } // namespace modules
206 } // namespace reco
207 
208 #endif
bool operator()(const TrackingParticle *tp) const
bool select(const TrackingParticle *tp) const
TrackingParticleSelector(double ptMin, double ptMax, double minRapidity, double maxRapidity, double tip, double lip, int minHit, bool signalOnly, bool intimeOnly, bool chargedOnly, bool stableOnly, const std::vector< int > &pdgId=std::vector< int >(), bool invertRapidityCut=false, double minPhi=-3.2, double maxPhi=3.2)
constexpr float ptMin
bool isInTime(const TrackingParticle *tp) const
bool selectKinematics(const TrackingParticle *tp) const
bool isSignal(const TrackingParticle *tp) const
bool isStable(const TrackingParticle *tp) const
bool operator()(const TrackingParticle &tp) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define M_PI
bool isCharged(const TrackingParticle *tp) const
fixed size matrix
static S make(const edm::ParameterSet &cfg)
Monte Carlo truth information used for tracking validation.
bool selectParticleType(const TrackingParticle *tp) const
static TrackingParticleSelector make(const edm::ParameterSet &cfg)
SingleObjectSelector< TrackingParticleCollection, ::TrackingParticleSelector > TrackingParticleSelector
static TrackingParticleSelector make(const edm::ParameterSet &cfg, edm::ConsumesCollector &iC)