CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
GenParticlePruner Class Reference
Inheritance diagram for GenParticlePruner:
edm::stream::EDProducer<>

Public Member Functions

 GenParticlePruner (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
 

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_
 
std::vector< size_t > indices_
 
int keepOrDropAll_
 
std::vector< std::pair< StringCutObjectSelector< reco::GenParticle >, helper::SelectCode > > select_
 
std::vector< std::string > selection_
 
edm::EDGetTokenT< reco::GenParticleCollectionsrcToken_
 
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecordtableToken_
 

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

Definition at line 23 of file GenParticlePruner.cc.

Constructor & Destructor Documentation

◆ GenParticlePruner()

GenParticlePruner::GenParticlePruner ( const edm::ParameterSet cfg)

Definition at line 100 of file GenParticlePruner.cc.

101  : firstEvent_(true),
102  srcToken_(consumes<GenParticleCollection>(cfg.getParameter<InputTag>("src"))),
105  selection_(cfg.getParameter<vector<string>>("select")) {
106  using namespace ::helper;
107  produces<GenParticleCollection>();
108  produces<edm::Association<reco::GenParticleCollection>>();
109 }
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const int drop
TracksUtilities< TrackerTraits > helper
edm::EDGetTokenT< reco::GenParticleCollection > srcToken_
std::vector< std::string > selection_
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord > tableToken_

Member Function Documentation

◆ flagDaughters()

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

Definition at line 111 of file GenParticlePruner.cc.

References BPhysicsValidation_cfi::daughters, flags_, and mps_fire::i.

Referenced by produce().

111  {
112  const GenParticleRefVector &daughters = gen.daughterRefVector();
113  for (GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i)
114  flags_[i->key()] = keepOrDrop;
115 }
std::vector< int > flags_

◆ flagMothers()

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

Definition at line 117 of file GenParticlePruner.cc.

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

Referenced by produce().

117  {
118  const GenParticleRefVector &mothers = gen.motherRefVector();
119  for (GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i)
120  flags_[i->key()] = keepOrDrop;
121 }
std::vector< int > flags_
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 GenParticlePruner::getDaughterKeys ( std::vector< size_t > &  daIndxs,
std::vector< size_t > &  daNewIndxs,
const reco::GenParticleRefVector daughters 
) const
private

Definition at line 288 of file GenParticlePruner.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().

Referenced by produce().

290  {
291  for (GenParticleRefVector::const_iterator j = daughters.begin(); j != daughters.end(); ++j) {
292  GenParticleRef dau = *j;
293  if (find(daIndxs.begin(), daIndxs.end(), dau.key()) == daIndxs.end()) {
294  daIndxs.push_back(dau.key());
295  int idx = flags_[dau.key()];
296  if (idx > 0) {
297  daNewIndxs.push_back(idx);
298  } else {
299  const GenParticleRefVector &daus = dau->daughterRefVector();
300  if (!daus.empty())
301  getDaughterKeys(daIndxs, daNewIndxs, daus);
302  }
303  }
304  }
305 }
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:99
std::vector< int > flags_
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 GenParticlePruner::getMotherKeys ( std::vector< size_t > &  moIndxs,
std::vector< size_t > &  moNewIndxs,
const reco::GenParticleRefVector mothers 
) const
private

Definition at line 307 of file GenParticlePruner.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().

Referenced by produce().

309  {
310  for (GenParticleRefVector::const_iterator j = mothers.begin(); j != mothers.end(); ++j) {
311  GenParticleRef mom = *j;
312  if (find(moIndxs.begin(), moIndxs.end(), mom.key()) == moIndxs.end()) {
313  moIndxs.push_back(mom.key());
314  int idx = flags_[mom.key()];
315  if (idx >= 0) {
316  moNewIndxs.push_back(idx);
317  } else {
318  const GenParticleRefVector &moms = mom->motherRefVector();
319  if (!moms.empty())
320  getMotherKeys(moIndxs, moNewIndxs, moms);
321  }
322  }
323  }
324 }
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:99
std::vector< int > flags_
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 getMotherKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
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

◆ parse()

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

Definition at line 52 of file GenParticlePruner.cc.

References helper::SelectCode::all_, HltBtagPostValidation_cff::c, mps_check::command, edm::errors::Configuration, DMR_cfg::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().

52  {
53  using namespace ::helper;
54  size_t f = selection.find_first_not_of(' ');
55  size_t n = selection.size();
56  string command;
57  char c;
58  for (; (c = selection[f]) != ' ' && f < n; ++f) {
59  command.push_back(c);
60  }
61  if (command[0] == '+') {
62  command.erase(0, 1);
63  if (command[0] == '+') {
64  command.erase(0, 1);
65  code.mothersDepth_ = SelectCode::kAll;
66  } else {
67  code.mothersDepth_ = SelectCode::kFirst;
68  }
69  } else
71 
72  if (command[command.size() - 1] == '+') {
73  command.erase(command.size() - 1);
74  if (command[command.size() - 1] == '+') {
75  command.erase(command.size() - 1);
76  code.daughtersDepth_ = SelectCode::kAll;
77  } else {
78  code.daughtersDepth_ = SelectCode::kFirst;
79  }
80  } else
82 
83  if (command == "keep")
84  code.keepOrDrop_ = SelectCode::kKeep;
85  else if (command == "drop")
86  code.keepOrDrop_ = SelectCode::kDrop;
87  else {
88  throw Exception(errors::Configuration) << "invalid selection command: " << command << "\n" << endl;
89  }
90  for (; f < n; ++f) {
91  if (selection[f] != ' ')
92  break;
93  }
94  cut = string(selection, f);
95  if (cut[0] == '*')
96  cut = string(cut, 0, cut.find_first_of(' '));
97  code.all_ = cut == "*";
98 }
selection
main part
Definition: corrVsCorr.py:100
double f[11][100]
TracksUtilities< TrackerTraits > helper
list command
Definition: mps_check.py:25

◆ produce()

void GenParticlePruner::produce ( edm::Event evt,
const edm::EventSetup es 
)
overrideprivate

Definition at line 165 of file GenParticlePruner.cc.

References reco::CompositeRefCandidateT< D >::addDaughter(), reco::CompositeRefCandidateT< D >::addMother(), edm::errors::Configuration, DMR_cfg::cut, drop, Exception, firstEvent_, flagDaughters(), flagMothers(), flags_, relval_steps::gen(), GenParticle::GenParticle, edm::Event::getByToken(), edm::EventSetup::getData(), getDaughterKeys(), getMotherKeys(), edm::Event::getRefBeforePut(), mps_fire::i, indices_, dqmiolumiharvest::j, keep, keepOrDropAll_, WDecay::kNone, eostools::move(), dqmiodumpmetadata::n, MillePedeFileConverter_cfg::out, AlCaHLTBitMon_ParallelJobs::p, parse(), pdgEntryReplace(), edm::Event::put(), recursiveFlagDaughters(), recursiveFlagMothers(), EgammaValidation_Wenu_cff::sel, select_, selection_, reco::GenParticle::setCollisionId(), jetUpdater_cfi::sort, TrackRefitter_38T_cff::src, srcToken_, reco::GenParticle::statusFlags(), and tableToken_.

165  {
166  if (firstEvent_) {
167  auto const &pdt = es.getData(tableToken_);
168 
169  for (vector<string>::const_iterator i = selection_.begin(); i != selection_.end(); ++i) {
170  string cut;
171  ::helper::SelectCode code;
172  parse(*i, code, cut);
173  if (code.all_) {
174  if (i != selection_.begin())
176  << "selections \"keep *\" and \"drop *\" can be used only as first options. Here used in position # "
177  << (i - selection_.begin()) + 1 << "\n"
178  << endl;
179  switch (code.keepOrDrop_) {
180  case ::helper::SelectCode::kDrop:
182  break;
183  case ::helper::SelectCode::kKeep:
185  };
186  } else {
187  cut = pdgEntryReplace(cut, pdt);
188  select_.push_back(make_pair(StringCutObjectSelector<GenParticle>(cut), code));
189  }
190  }
191  firstEvent_ = false;
192  }
193 
194  using namespace ::helper;
196  evt.getByToken(srcToken_, src);
197  const size_t n = src->size();
198  flags_.clear();
199  flags_.resize(n, keepOrDropAll_);
200  for (size_t j = 0; j < select_.size(); ++j) {
201  const pair<StringCutObjectSelector<GenParticle>, SelectCode> &sel = select_[j];
202  SelectCode code = sel.second;
204  for (size_t i = 0; i < n; ++i) {
205  const GenParticle &p = (*src)[i];
206  if (cut(p)) {
207  int keepOrDrop = keep;
208  switch (code.keepOrDrop_) {
209  case SelectCode::kKeep:
210  keepOrDrop = keep;
211  break;
212  case SelectCode::kDrop:
213  keepOrDrop = drop;
214  };
215  flags_[i] = keepOrDrop;
216  std::vector<size_t> allIndicesDa;
217  std::vector<size_t> allIndicesMo;
218  switch (code.daughtersDepth_) {
219  case SelectCode::kAll:
220  recursiveFlagDaughters(i, *src, keepOrDrop, allIndicesDa);
221  break;
222  case SelectCode::kFirst:
223  flagDaughters(p, keepOrDrop);
224  break;
225  case SelectCode::kNone:;
226  };
227  switch (code.mothersDepth_) {
228  case SelectCode::kAll:
229  recursiveFlagMothers(i, *src, keepOrDrop, allIndicesMo);
230  break;
231  case SelectCode::kFirst:
232  flagMothers(p, keepOrDrop);
233  break;
234  case SelectCode::kNone:;
235  };
236  }
237  }
238  }
239  indices_.clear();
240  int counter = 0;
241  for (size_t i = 0; i < n; ++i) {
242  if (flags_[i] == keep) {
243  indices_.push_back(i);
244  flags_[i] = counter++;
245  } else {
246  flags_[i] = -1; //set to invalid ref
247  }
248  }
249 
250  auto out = std::make_unique<GenParticleCollection>();
252  out->reserve(counter);
253 
254  for (vector<size_t>::const_iterator i = indices_.begin(); i != indices_.end(); ++i) {
255  size_t index = *i;
256  const GenParticle &gen = (*src)[index];
257  const LeafCandidate &part = gen;
258  out->push_back(GenParticle(part));
259  GenParticle &newGen = out->back();
260  //fill status flags
261  newGen.statusFlags() = gen.statusFlags();
262  newGen.setCollisionId(gen.collisionId());
263  // The "daIndxs" and "moIndxs" keep a list of the keys for the mother/daughter
264  // parentage/descendency. In some cases, a circular referencing is encountered,
265  // which would result in an infinite loop. The list is checked to
266  // avoid this.
267  vector<size_t> daIndxs, daNewIndxs;
268  getDaughterKeys(daIndxs, daNewIndxs, gen.daughterRefVector());
269  std::sort(daNewIndxs.begin(), daNewIndxs.end());
270  for (size_t i = 0; i < daNewIndxs.size(); ++i)
271  newGen.addDaughter(GenParticleRef(outRef, daNewIndxs[i]));
272 
273  vector<size_t> moIndxs, moNewIndxs;
274  getMotherKeys(moIndxs, moNewIndxs, gen.motherRefVector());
275  std::sort(moNewIndxs.begin(), moNewIndxs.end());
276  for (size_t i = 0; i < moNewIndxs.size(); ++i)
277  newGen.addMother(GenParticleRef(outRef, moNewIndxs[i]));
278  }
279 
281  auto orig2new = std::make_unique<edm::Association<reco::GenParticleCollection>>(oh);
283  orig2newFiller.insert(src, flags_.begin(), flags_.end());
284  orig2newFiller.fill();
285  evt.put(std::move(orig2new));
286 }
std::vector< std::pair< StringCutObjectSelector< reco::GenParticle >, helper::SelectCode > > select_
void flagDaughters(const reco::GenParticle &, int)
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
std::vector< int > flags_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
void setCollisionId(int s)
Definition: GenParticle.h:35
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void flagMothers(const reco::GenParticle &, int)
void recursiveFlagMothers(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
std::string pdgEntryReplace(const std::string &, HepPDT::ParticleDataTable const &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:536
void parse(const std::string &selection, helper::SelectCode &code, std::string &cut) const
void recursiveFlagDaughters(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
void addDaughter(const typename daughters::value_type &)
add a daughter via a reference
std::vector< size_t > indices_
const int keep
def gen(fragment, howMuch)
Production test section ####.
void addMother(const typename mothers::value_type &)
add a daughter via a reference
RefProd< PROD > getRefBeforePut()
Definition: Event.h:158
const int drop
void getMotherKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
TracksUtilities< TrackerTraits > helper
const GenStatusFlags & statusFlags() const
Definition: GenParticle.h:38
part
Definition: HCALResponse.h:20
edm::EDGetTokenT< reco::GenParticleCollection > srcToken_
void getDaughterKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
std::vector< std::string > selection_
def move(src, dest)
Definition: eostools.py:511
edm::ESGetToken< HepPDT::ParticleDataTable, edm::DefaultRecord > tableToken_

◆ recursiveFlagDaughters()

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

Definition at line 123 of file GenParticlePruner.cc.

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

Referenced by produce().

126  {
127  GenParticleRefVector daughters = src[index].daughterRefVector();
128  // avoid infinite recursion if the daughters are set to "this" particle.
129  size_t cachedIndex = index;
130  for (GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i) {
131  index = i->key();
132  // To also avoid infinite recursion if a "loop" is found in the daughter list,
133  // check to make sure the index hasn't already been added.
134  if (find(allIndices.begin(), allIndices.end(), index) == allIndices.end()) {
135  allIndices.push_back(index);
136  if (cachedIndex != index) {
137  flags_[index] = keepOrDrop;
138  recursiveFlagDaughters(index, src, keepOrDrop, allIndices);
139  }
140  }
141  }
142 }
std::vector< int > flags_
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 GenParticlePruner::recursiveFlagMothers ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
)
private

Definition at line 144 of file GenParticlePruner.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().

147  {
148  GenParticleRefVector mothers = src[index].motherRefVector();
149  // avoid infinite recursion if the mothers are set to "this" particle.
150  size_t cachedIndex = index;
151  for (GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i) {
152  index = i->key();
153  // To also avoid infinite recursion if a "loop" is found in the daughter list,
154  // check to make sure the index hasn't already been added.
155  if (find(allIndices.begin(), allIndices.end(), index) == allIndices.end()) {
156  allIndices.push_back(index);
157  if (cachedIndex != index) {
158  flags_[index] = keepOrDrop;
159  recursiveFlagMothers(index, src, keepOrDrop, allIndices);
160  }
161  }
162  }
163 }
std::vector< int > flags_
void recursiveFlagMothers(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
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 GenParticlePruner::firstEvent_
private

Definition at line 29 of file GenParticlePruner.cc.

Referenced by produce().

◆ flags_

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

◆ indices_

std::vector<size_t> GenParticlePruner::indices_
private

Definition at line 36 of file GenParticlePruner.cc.

Referenced by produce().

◆ keepOrDropAll_

int GenParticlePruner::keepOrDropAll_
private

Definition at line 32 of file GenParticlePruner.cc.

Referenced by produce().

◆ select_

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

Definition at line 34 of file GenParticlePruner.cc.

Referenced by produce().

◆ selection_

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

Definition at line 33 of file GenParticlePruner.cc.

Referenced by produce().

◆ srcToken_

edm::EDGetTokenT<reco::GenParticleCollection> GenParticlePruner::srcToken_
private

Definition at line 30 of file GenParticlePruner.cc.

Referenced by produce().

◆ tableToken_

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

Definition at line 31 of file GenParticlePruner.cc.

Referenced by produce().