CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
TrackingParticleSelectorByGen Class Reference

#include <SimGeneral/TrackingParticleSelectorByGen/plugins/TrackingParticleSelectorByGen.cc>

Inheritance diagram for TrackingParticleSelectorByGen:
edm::stream::EDProducer<>

Public Member Functions

 TrackingParticleSelectorByGen (const edm::ParameterSet &)
 
- Public Member Functions inherited from edm::stream::EDProducer<>
 EDProducer ()=default
 
bool hasAbilityToProduceInLumis () const final
 
bool hasAbilityToProduceInRuns () const final
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 

Private Member Functions

void flagDaughters (const reco::GenParticle &, int)
 
void flagMothers (const reco::GenParticle &, int)
 
void getDaughterKeys (std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
 
void getMotherKeys (std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
 
void parse (const std::string &selection, helper::SelectCode &code, std::string &cut) const
 
void produce (edm::Event &, const edm::EventSetup &) override
 
void recursiveFlagDaughters (size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
 
void recursiveFlagMothers (size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
 

Private Attributes

bool firstEvent_
 
std::vector< int > flags_
 
edm::EDGetTokenT< reco::GenParticleCollectiongpToken_
 
int keepOrDropAll_
 
std::vector< std::pair< StringCutObjectSelector< reco::GenParticle >, helper::SelectCode > > select_
 
std::vector< std::string > selection_
 
edm::EDGetTokenT< TrackingParticleCollectiontpToken_
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer<>
typedef CacheContexts< T... > CacheTypes
 
typedef CacheTypes::GlobalCache GlobalCache
 
typedef AbilityChecker< T... > HasAbility
 
typedef CacheTypes::LuminosityBlockCache LuminosityBlockCache
 
typedef LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCacheLuminosityBlockContext
 
typedef CacheTypes::LuminosityBlockSummaryCache LuminosityBlockSummaryCache
 
typedef CacheTypes::RunCache RunCache
 
typedef RunContextT< RunCache, GlobalCacheRunContext
 
typedef CacheTypes::RunSummaryCache RunSummaryCache
 

Detailed Description

Description: [one line class summary]

Implementation: [Notes on implementation]

Definition at line 47 of file TrackingParticleSelectorByGen.cc.

Constructor & Destructor Documentation

TrackingParticleSelectorByGen::TrackingParticleSelectorByGen ( const edm::ParameterSet iConfig)
explicit

Definition at line 131 of file TrackingParticleSelectorByGen.cc.

132  : tpToken_(consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("trackingParticles"))),
133  gpToken_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("genParticles"))),
134  firstEvent_(true),
136  selection_(iConfig.getParameter<vector<string>>("select")) {
137  produces<TrackingParticleCollection>();
138 }
T getParameter(std::string const &) const
edm::EDGetTokenT< TrackingParticleCollection > tpToken_
const int drop
edm::EDGetTokenT< reco::GenParticleCollection > gpToken_

Member Function Documentation

void TrackingParticleSelectorByGen::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 329 of file TrackingParticleSelectorByGen.cc.

References edm::ConfigurationDescriptions::add(), edm::ParameterSetDescription::add(), and DEFINE_FWK_MODULE.

329  {
331  desc.add<edm::InputTag>("trackingParticles", edm::InputTag("mix", "MergedTrackTruth"));
332  desc.add<edm::InputTag>("genParticles", edm::InputTag("genParticles"));
333  desc.add<vector<string>>("select");
334 
335  descriptions.add("tpSelectorByGenDefault", desc);
336 }
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void TrackingParticleSelectorByGen::flagDaughters ( const reco::GenParticle gen,
int  keepOrDrop 
)
private

Definition at line 140 of file TrackingParticleSelectorByGen.cc.

References edm::RefVector< C, T, F >::begin(), reco::CompositeRefCandidateT< D >::daughterRefVector(), BPhysicsValidation_cfi::daughters, edm::RefVector< C, T, F >::end(), flags_, and mps_fire::i.

Referenced by produce().

140  {
142  for (GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i)
143  flags_[i->key()] = keepOrDrop;
144 }
const daughters & daughterRefVector() const
references to daughtes
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:253
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248
void TrackingParticleSelectorByGen::flagMothers ( const reco::GenParticle gen,
int  keepOrDrop 
)
private

Definition at line 146 of file TrackingParticleSelectorByGen.cc.

References edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::end(), flags_, mps_fire::i, and reco::CompositeRefCandidateT< D >::motherRefVector().

Referenced by produce().

146  {
147  const GenParticleRefVector &mothers = gen.motherRefVector();
148  for (GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i)
149  flags_[i->key()] = keepOrDrop;
150 }
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:253
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248
const mothers & motherRefVector() const
references to mothers
void TrackingParticleSelectorByGen::getDaughterKeys ( std::vector< size_t > &  daIndxs,
std::vector< size_t > &  daNewIndxs,
const reco::GenParticleRefVector daughters 
) const
private

Definition at line 194 of file TrackingParticleSelectorByGen.cc.

References edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::empty(), edm::RefVector< C, T, F >::end(), spr::find(), flags_, training_settings::idx, and edm::Ref< C, T, F >::key().

196  {
197  for (GenParticleRefVector::const_iterator j = daughters.begin(); j != daughters.end(); ++j) {
198  GenParticleRef dau = *j;
199  if (find(daIndxs.begin(), daIndxs.end(), dau.key()) == daIndxs.end()) {
200  daIndxs.push_back(dau.key());
201  int idx = flags_[dau.key()];
202  if (idx > 0) {
203  daNewIndxs.push_back(idx);
204  } else {
205  const GenParticleRefVector &daus = dau->daughterRefVector();
206  if (!daus.empty())
207  getDaughterKeys(daIndxs, daNewIndxs, daus);
208  }
209  }
210  }
211 }
key_type key() const
Accessor for product key.
Definition: Ref.h:263
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:253
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:104
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248
void getDaughterKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
void TrackingParticleSelectorByGen::getMotherKeys ( std::vector< size_t > &  moIndxs,
std::vector< size_t > &  moNewIndxs,
const reco::GenParticleRefVector mothers 
) const
private

Definition at line 213 of file TrackingParticleSelectorByGen.cc.

References edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::empty(), edm::RefVector< C, T, F >::end(), spr::find(), flags_, training_settings::idx, and edm::Ref< C, T, F >::key().

215  {
216  for (GenParticleRefVector::const_iterator j = mothers.begin(); j != mothers.end(); ++j) {
217  GenParticleRef mom = *j;
218  if (find(moIndxs.begin(), moIndxs.end(), mom.key()) == moIndxs.end()) {
219  moIndxs.push_back(mom.key());
220  int idx = flags_[mom.key()];
221  if (idx >= 0) {
222  moNewIndxs.push_back(idx);
223  } else {
224  const GenParticleRefVector &moms = mom->motherRefVector();
225  if (!moms.empty())
226  getMotherKeys(moIndxs, moNewIndxs, moms);
227  }
228  }
229  }
230 }
void getMotherKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
key_type key() const
Accessor for product key.
Definition: Ref.h:263
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:253
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:104
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248
void TrackingParticleSelectorByGen::parse ( const std::string &  selection,
helper::SelectCode code,
std::string &  cut 
) const
private

Definition at line 81 of file TrackingParticleSelectorByGen.cc.

References helper::SelectCode::all_, EnergyCorrector::c, mps_check::command, edm::errors::Configuration, helper::SelectCode::daughtersDepth_, Exception, f, helper::SelectCode::kAll, helper::SelectCode::kDrop, helper::SelectCode::keepOrDrop_, helper::SelectCode::kFirst, helper::SelectCode::kKeep, helper::SelectCode::kNone, helper::SelectCode::mothersDepth_, gen::n, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by produce().

83  {
84  using namespace ::helper;
85  size_t f = selection.find_first_not_of(' ');
86  size_t n = selection.size();
87  string command;
88  char c;
89  for (; (c = selection[f]) != ' ' && f < n; ++f) {
90  command.push_back(c);
91  }
92  if (command[0] == '+') {
93  command.erase(0, 1);
94  if (command[0] == '+') {
95  command.erase(0, 1);
96  code.mothersDepth_ = SelectCode::kAll;
97  } else {
98  code.mothersDepth_ = SelectCode::kFirst;
99  }
100  } else
102 
103  if (command[command.size() - 1] == '+') {
104  command.erase(command.size() - 1);
105  if (command[command.size() - 1] == '+') {
106  command.erase(command.size() - 1);
107  code.daughtersDepth_ = SelectCode::kAll;
108  } else {
109  code.daughtersDepth_ = SelectCode::kFirst;
110  }
111  } else
113 
114  if (command == "keep")
115  code.keepOrDrop_ = SelectCode::kKeep;
116  else if (command == "drop")
117  code.keepOrDrop_ = SelectCode::kDrop;
118  else {
119  throw Exception(errors::Configuration) << "invalid selection command: " << command << "\n" << endl;
120  }
121  for (; f < n; ++f) {
122  if (selection[f] != ' ')
123  break;
124  }
125  cut = string(selection, f);
126  if (cut[0] == '*')
127  cut = string(cut, 0, cut.find_first_of(' '));
128  code.all_ = cut == "*";
129 }
Definition: helper.py:1
selection
main part
Definition: corrVsCorr.py:100
double f[11][100]
list command
Definition: mps_check.py:25
void TrackingParticleSelectorByGen::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivate

Definition at line 232 of file TrackingParticleSelectorByGen.cc.

References helper::SelectCode::all_, edm::errors::Configuration, TkAlMuonSelectors_cfi::cut, drop, Exception, firstEvent_, flagDaughters(), flagMothers(), flags_, edm::Event::get(), gpToken_, mps_fire::i, keep, helper::SelectCode::keepOrDrop_, keepOrDropAll_, WDecay::kNone, eostools::move(), gen::n, MillePedeFileConverter_cfg::out, AlCaHLTBitMon_ParallelJobs::p, parse(), edm::Event::put(), recursiveFlagDaughters(), recursiveFlagMothers(), cuy::rep, PdgEntryReplacer::replace(), triggerObjects_cff::sel, select_, selection_, and tpToken_.

232  {
233  using namespace edm;
234 
235  if (firstEvent_) {
236  PdgEntryReplacer rep(iSetup);
237  for (vector<string>::const_iterator i = selection_.begin(); i != selection_.end(); ++i) {
238  string cut;
240  parse(*i, code, cut);
241  if (code.all_) {
242  if (i != selection_.begin())
244  << "selections \"keep *\" and \"drop *\" can be used only as first options. Here used in position # "
245  << (i - selection_.begin()) + 1 << "\n"
246  << endl;
247  switch (code.keepOrDrop_) {
248  case ::helper::SelectCode::kDrop:
250  break;
251  case ::helper::SelectCode::kKeep:
253  };
254  } else {
255  cut = rep.replace(cut);
256  select_.push_back(make_pair(StringCutObjectSelector<GenParticle>(cut), code));
257  }
258  }
259  firstEvent_ = false;
260  }
261 
262  const auto &tps = iEvent.get(tpToken_);
263 
264  const auto &gps = iEvent.get(gpToken_);
265 
266  using namespace ::helper;
267  const size_t n = gps.size();
268  flags_.clear();
269  flags_.resize(n, keepOrDropAll_);
270  for (size_t j = 0; j < select_.size(); ++j) {
271  const pair<StringCutObjectSelector<GenParticle>, SelectCode> &sel = select_[j];
272  SelectCode code = sel.second;
273  const StringCutObjectSelector<GenParticle> &cut = sel.first;
274  for (size_t i = 0; i < n; ++i) {
275  const GenParticle &p = gps[i];
276  if (cut(p)) {
277  int keepOrDrop = keep;
278  switch (code.keepOrDrop_) {
279  case SelectCode::kKeep:
280  keepOrDrop = keep;
281  break;
282  case SelectCode::kDrop:
283  keepOrDrop = drop;
284  };
285  flags_[i] = keepOrDrop;
286  std::vector<size_t> allIndicesDa;
287  std::vector<size_t> allIndicesMo;
288  switch (code.daughtersDepth_) {
289  case SelectCode::kAll:
290  recursiveFlagDaughters(i, gps, keepOrDrop, allIndicesDa);
291  break;
292  case SelectCode::kFirst:
293  flagDaughters(p, keepOrDrop);
294  break;
295  case SelectCode::kNone:;
296  };
297  switch (code.mothersDepth_) {
298  case SelectCode::kAll:
299  recursiveFlagMothers(i, gps, keepOrDrop, allIndicesMo);
300  break;
301  case SelectCode::kFirst:
302  flagMothers(p, keepOrDrop);
303  break;
304  case SelectCode::kNone:;
305  };
306  }
307  }
308  }
309 
310  auto out = std::make_unique<TrackingParticleCollection>();
311  out->reserve(n);
312 
313  for (auto &&tp : tps) {
314  auto &associatedGenParticles = tp.genParticles();
315 
316  bool isSelected = false;
317  for (auto &&assocGen : associatedGenParticles) {
318  if (flags_[assocGen.index()] == keep)
319  isSelected = true;
320  }
321 
322  if (isSelected) {
323  out->emplace_back(tp);
324  }
325  }
326  iEvent.put(std::move(out));
327 }
const int keep
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
Definition: helper.py:1
void flagDaughters(const reco::GenParticle &, int)
edm::EDGetTokenT< TrackingParticleCollection > tpToken_
void parse(const std::string &selection, helper::SelectCode &code, std::string &cut) const
void flagMothers(const reco::GenParticle &, int)
void recursiveFlagMothers(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
void recursiveFlagDaughters(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:326
std::vector< std::pair< StringCutObjectSelector< reco::GenParticle >, helper::SelectCode > > select_
rep
Definition: cuy.py:1190
const int drop
HLT enums.
edm::EDGetTokenT< reco::GenParticleCollection > gpToken_
def move(src, dest)
Definition: eostools.py:511
void TrackingParticleSelectorByGen::recursiveFlagDaughters ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
)
private

Definition at line 152 of file TrackingParticleSelectorByGen.cc.

References edm::RefVector< C, T, F >::begin(), BPhysicsValidation_cfi::daughters, edm::RefVector< C, T, F >::end(), spr::find(), flags_, and mps_fire::i.

Referenced by produce().

155  {
156  GenParticleRefVector daughters = src[index].daughterRefVector();
157  // avoid infinite recursion if the daughters are set to "this" particle.
158  size_t cachedIndex = index;
159  for (GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i) {
160  index = i->key();
161  // To also avoid infinite recursion if a "loop" is found in the daughter list,
162  // check to make sure the index hasn't already been added.
163  if (find(allIndices.begin(), allIndices.end(), index) == allIndices.end()) {
164  allIndices.push_back(index);
165  if (cachedIndex != index) {
166  flags_[index] = keepOrDrop;
167  recursiveFlagDaughters(index, src, keepOrDrop, allIndices);
168  }
169  }
170  }
171 }
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:253
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248
void recursiveFlagDaughters(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
void TrackingParticleSelectorByGen::recursiveFlagMothers ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
)
private

Definition at line 173 of file TrackingParticleSelectorByGen.cc.

References edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::end(), spr::find(), flags_, and mps_fire::i.

Referenced by produce().

176  {
177  GenParticleRefVector mothers = src[index].motherRefVector();
178  // avoid infinite recursion if the mothers are set to "this" particle.
179  size_t cachedIndex = index;
180  for (GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i) {
181  index = i->key();
182  // To also avoid infinite recursion if a "loop" is found in the daughter list,
183  // check to make sure the index hasn't already been added.
184  if (find(allIndices.begin(), allIndices.end(), index) == allIndices.end()) {
185  allIndices.push_back(index);
186  if (cachedIndex != index) {
187  flags_[index] = keepOrDrop;
188  recursiveFlagMothers(index, src, keepOrDrop, allIndices);
189  }
190  }
191  }
192 }
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:253
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
void recursiveFlagMothers(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248

Member Data Documentation

bool TrackingParticleSelectorByGen::firstEvent_
private

Definition at line 68 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

std::vector<int> TrackingParticleSelectorByGen::flags_
private
edm::EDGetTokenT<reco::GenParticleCollection> TrackingParticleSelectorByGen::gpToken_
private

Definition at line 67 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

int TrackingParticleSelectorByGen::keepOrDropAll_
private

Definition at line 69 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

std::vector<std::pair<StringCutObjectSelector<reco::GenParticle>, helper::SelectCode> > TrackingParticleSelectorByGen::select_
private

Definition at line 72 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

std::vector<std::string> TrackingParticleSelectorByGen::selection_
private

Definition at line 71 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

edm::EDGetTokenT<TrackingParticleCollection> TrackingParticleSelectorByGen::tpToken_
private

Definition at line 66 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().