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
 
 EDProducer (const EDProducer &)=delete
 
bool hasAbilityToProduceInBeginLumis () const final
 
bool hasAbilityToProduceInBeginProcessBlocks () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndProcessBlocks () const final
 
bool hasAbilityToProduceInEndRuns () const final
 
const EDProduceroperator= (const EDProducer &)=delete
 

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::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecordtableToken_
 
edm::EDGetTokenT< TrackingParticleCollectiontpToken_
 

Additional Inherited Members

- Public Types inherited from edm::stream::EDProducer<>
using CacheTypes = CacheContexts< T... >
 
using GlobalCache = typename CacheTypes::GlobalCache
 
using HasAbility = AbilityChecker< T... >
 
using InputProcessBlockCache = typename CacheTypes::InputProcessBlockCache
 
using LuminosityBlockCache = typename CacheTypes::LuminosityBlockCache
 
using LuminosityBlockContext = LuminosityBlockContextT< LuminosityBlockCache, RunCache, GlobalCache >
 
using LuminosityBlockSummaryCache = typename CacheTypes::LuminosityBlockSummaryCache
 
using RunCache = typename CacheTypes::RunCache
 
using RunContext = RunContextT< RunCache, GlobalCache >
 
using RunSummaryCache = typename CacheTypes::RunSummaryCache
 

Detailed Description

Description: [one line class summary]

Implementation: [Notes on implementation]

Definition at line 49 of file TrackingParticleSelectorByGen.cc.

Constructor & Destructor Documentation

◆ TrackingParticleSelectorByGen()

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

Definition at line 134 of file TrackingParticleSelectorByGen.cc.

135  : tpToken_(consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("trackingParticles"))),
136  gpToken_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("genParticles"))),
138  firstEvent_(true),
140  selection_(iConfig.getParameter<vector<string>>("select")) {
141  produces<TrackingParticleCollection>();
142 }
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::EDGetTokenT< TrackingParticleCollection > tpToken_
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord > tableToken_
const int drop
edm::EDGetTokenT< reco::GenParticleCollection > gpToken_

Member Function Documentation

◆ fillDescriptions()

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

Definition at line 333 of file TrackingParticleSelectorByGen.cc.

References edm::ConfigurationDescriptions::add(), submitPVResolutionJobs::desc, and HLT_2022v12_cff::InputTag.

333  {
335  desc.add<edm::InputTag>("trackingParticles", edm::InputTag("mix", "MergedTrackTruth"));
336  desc.add<edm::InputTag>("genParticles", edm::InputTag("genParticles"));
337  desc.add<vector<string>>("select");
338 
339  descriptions.add("tpSelectorByGenDefault", desc);
340 }
void add(std::string const &label, ParameterSetDescription const &psetDescription)

◆ flagDaughters()

void TrackingParticleSelectorByGen::flagDaughters ( const reco::GenParticle gen,
int  keepOrDrop 
)
private

◆ flagMothers()

void TrackingParticleSelectorByGen::flagMothers ( const reco::GenParticle gen,
int  keepOrDrop 
)
private

Definition at line 150 of file TrackingParticleSelectorByGen.cc.

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

Referenced by produce().

150  {
151  const GenParticleRefVector &mothers = gen.motherRefVector();
152  for (GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i)
153  flags_[i->key()] = keepOrDrop;
154 }
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223

◆ getDaughterKeys()

void TrackingParticleSelectorByGen::getDaughterKeys ( std::vector< size_t > &  daIndxs,
std::vector< size_t > &  daNewIndxs,
const reco::GenParticleRefVector daughters 
) const
private

Definition at line 198 of file TrackingParticleSelectorByGen.cc.

References BPhysicsValidation_cfi::daughters, edm::RefVector< C, T, F >::empty(), spr::find(), flags_, heavyIonCSV_trainingSettings::idx, dqmiolumiharvest::j, and edm::Ref< C, T, F >::key().

200  {
201  for (GenParticleRefVector::const_iterator j = daughters.begin(); j != daughters.end(); ++j) {
202  GenParticleRef dau = *j;
203  if (find(daIndxs.begin(), daIndxs.end(), dau.key()) == daIndxs.end()) {
204  daIndxs.push_back(dau.key());
205  int idx = flags_[dau.key()];
206  if (idx > 0) {
207  daNewIndxs.push_back(idx);
208  } else {
209  const GenParticleRefVector &daus = dau->daughterRefVector();
210  if (!daus.empty())
211  getDaughterKeys(daIndxs, daNewIndxs, daus);
212  }
213  }
214  }
215 }
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:99
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
key_type key() const
Accessor for product key.
Definition: Ref.h:250
void getDaughterKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const

◆ getMotherKeys()

void TrackingParticleSelectorByGen::getMotherKeys ( std::vector< size_t > &  moIndxs,
std::vector< size_t > &  moNewIndxs,
const reco::GenParticleRefVector mothers 
) const
private

Definition at line 217 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_, heavyIonCSV_trainingSettings::idx, dqmiolumiharvest::j, and edm::Ref< C, T, F >::key().

219  {
220  for (GenParticleRefVector::const_iterator j = mothers.begin(); j != mothers.end(); ++j) {
221  GenParticleRef mom = *j;
222  if (find(moIndxs.begin(), moIndxs.end(), mom.key()) == moIndxs.end()) {
223  moIndxs.push_back(mom.key());
224  int idx = flags_[mom.key()];
225  if (idx >= 0) {
226  moNewIndxs.push_back(idx);
227  } else {
228  const GenParticleRefVector &moms = mom->motherRefVector();
229  if (!moms.empty())
230  getMotherKeys(moIndxs, moNewIndxs, moms);
231  }
232  }
233  }
234 }
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:99
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
key_type key() const
Accessor for product key.
Definition: Ref.h:250
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223
void getMotherKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const

◆ parse()

void TrackingParticleSelectorByGen::parse ( const std::string &  selection,
helper::SelectCode code,
std::string &  cut 
) const
private

Definition at line 84 of file TrackingParticleSelectorByGen.cc.

References helper::SelectCode::all_, c, mps_check::command, edm::errors::Configuration, PA_MinBiasSkim_cff::cut, helper::SelectCode::daughtersDepth_, Exception, f, helper::SelectCode::keepOrDrop_, WDecay::kNone, helper::SelectCode::mothersDepth_, dqmiodumpmetadata::n, corrVsCorr::selection, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by produce().

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

◆ produce()

void TrackingParticleSelectorByGen::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivate

Definition at line 236 of file TrackingParticleSelectorByGen.cc.

References edm::errors::Configuration, PA_MinBiasSkim_cff::cut, drop, Exception, firstEvent_, flagDaughters(), flagMothers(), flags_, edm::EventSetup::getData(), gpToken_, mps_fire::i, iEvent, isSelected(), dqmiolumiharvest::j, keep, keepOrDropAll_, WDecay::kNone, eostools::move(), dqmiodumpmetadata::n, MillePedeFileConverter_cfg::out, AlCaHLTBitMon_ParallelJobs::p, parse(), pdgEntryReplace(), recursiveFlagDaughters(), recursiveFlagMothers(), EgammaValidation_Wenu_cff::sel, select_, selection_, tableToken_, cmsswSequenceInfo::tp, and tpToken_.

236  {
237  using namespace edm;
238 
239  if (firstEvent_) {
240  auto const &pdt = iSetup.getData(tableToken_);
241  for (vector<string>::const_iterator i = selection_.begin(); i != selection_.end(); ++i) {
242  string cut;
243  ::helper::SelectCode code;
244  parse(*i, code, cut);
245  if (code.all_) {
246  if (i != selection_.begin())
248  << "selections \"keep *\" and \"drop *\" can be used only as first options. Here used in position # "
249  << (i - selection_.begin()) + 1 << "\n"
250  << endl;
251  switch (code.keepOrDrop_) {
252  case ::helper::SelectCode::kDrop:
254  break;
255  case ::helper::SelectCode::kKeep:
257  };
258  } else {
259  cut = pdgEntryReplace(cut, pdt);
260  select_.push_back(make_pair(StringCutObjectSelector<GenParticle>(cut), code));
261  }
262  }
263  firstEvent_ = false;
264  }
265 
266  const auto &tps = iEvent.get(tpToken_);
267 
268  const auto &gps = iEvent.get(gpToken_);
269 
270  using namespace ::helper;
271  const size_t n = gps.size();
272  flags_.clear();
273  flags_.resize(n, keepOrDropAll_);
274  for (size_t j = 0; j < select_.size(); ++j) {
275  const pair<StringCutObjectSelector<GenParticle>, SelectCode> &sel = select_[j];
276  SelectCode code = sel.second;
278  for (size_t i = 0; i < n; ++i) {
279  const GenParticle &p = gps[i];
280  if (cut(p)) {
281  int keepOrDrop = keep;
282  switch (code.keepOrDrop_) {
283  case SelectCode::kKeep:
284  keepOrDrop = keep;
285  break;
286  case SelectCode::kDrop:
287  keepOrDrop = drop;
288  };
289  flags_[i] = keepOrDrop;
290  std::vector<size_t> allIndicesDa;
291  std::vector<size_t> allIndicesMo;
292  switch (code.daughtersDepth_) {
293  case SelectCode::kAll:
294  recursiveFlagDaughters(i, gps, keepOrDrop, allIndicesDa);
295  break;
296  case SelectCode::kFirst:
297  flagDaughters(p, keepOrDrop);
298  break;
299  case SelectCode::kNone:;
300  };
301  switch (code.mothersDepth_) {
302  case SelectCode::kAll:
303  recursiveFlagMothers(i, gps, keepOrDrop, allIndicesMo);
304  break;
305  case SelectCode::kFirst:
306  flagMothers(p, keepOrDrop);
307  break;
308  case SelectCode::kNone:;
309  };
310  }
311  }
312  }
313 
314  auto out = std::make_unique<TrackingParticleCollection>();
315  out->reserve(n);
316 
317  for (auto &&tp : tps) {
318  auto &associatedGenParticles = tp.genParticles();
319 
320  bool isSelected = false;
321  for (auto &&assocGen : associatedGenParticles) {
322  if (flags_[assocGen.index()] == keep)
323  isSelected = true;
324  }
325 
326  if (isSelected) {
327  out->emplace_back(tp);
328  }
329  }
330  iEvent.put(std::move(out));
331 }
const int keep
Definition: helper.py:1
void flagDaughters(const reco::GenParticle &, int)
edm::EDGetTokenT< TrackingParticleCollection > tpToken_
std::string pdgEntryReplace(const std::string &, HepPDT::ParticleDataTable 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 > &)
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord > tableToken_
int iEvent
Definition: GenABIO.cc:224
void parse(const std::string &selection, helper::SelectCode &code, std::string &cut) const
bool getData(T &iHolder) const
Definition: EventSetup.h:122
std::vector< std::pair< StringCutObjectSelector< reco::GenParticle >, helper::SelectCode > > select_
const int drop
bool isSelected(const std::vector< L1HPSPFTauQualityCut > &qualityCuts, const l1t::PFCandidate &pfCand, float_t primaryVertexZ)
HLT enums.
edm::EDGetTokenT< reco::GenParticleCollection > gpToken_
def move(src, dest)
Definition: eostools.py:511

◆ recursiveFlagDaughters()

void TrackingParticleSelectorByGen::recursiveFlagDaughters ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
)
private

Definition at line 156 of file TrackingParticleSelectorByGen.cc.

References BPhysicsValidation_cfi::daughters, spr::find(), flags_, mps_fire::i, and TrackRefitter_38T_cff::src.

Referenced by produce().

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

◆ recursiveFlagMothers()

void TrackingParticleSelectorByGen::recursiveFlagMothers ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
)
private

Definition at line 177 of file TrackingParticleSelectorByGen.cc.

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

Referenced by produce().

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

Member Data Documentation

◆ firstEvent_

bool TrackingParticleSelectorByGen::firstEvent_
private

Definition at line 71 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

◆ flags_

std::vector<int> TrackingParticleSelectorByGen::flags_
private

◆ gpToken_

edm::EDGetTokenT<reco::GenParticleCollection> TrackingParticleSelectorByGen::gpToken_
private

Definition at line 69 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

◆ keepOrDropAll_

int TrackingParticleSelectorByGen::keepOrDropAll_
private

Definition at line 72 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

◆ select_

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

Definition at line 75 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

◆ selection_

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

Definition at line 74 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

◆ tableToken_

edm::ESGetToken<HepPDT::ParticleDataTable, edm::DefaultRecord> TrackingParticleSelectorByGen::tableToken_
private

Definition at line 70 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().

◆ tpToken_

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

Definition at line 68 of file TrackingParticleSelectorByGen.cc.

Referenced by produce().