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
 
bool hasAbilityToProduceInLumis () const final
 
bool hasAbilityToProduceInRuns () const final
 

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_
 

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

Definition at line 22 of file GenParticlePruner.cc.

Constructor & Destructor Documentation

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

Definition at line 95 of file GenParticlePruner.cc.

95  :
96  firstEvent_(true),
97  srcToken_(consumes<GenParticleCollection>(cfg.getParameter<InputTag>("src"))), keepOrDropAll_(drop),
98  selection_(cfg.getParameter<vector<string> >("select")) {
99  using namespace ::helper;
100  produces<GenParticleCollection>();
101  produces<edm::Association<reco::GenParticleCollection> >();
102 }
T getParameter(std::string const &) const
Definition: helper.py:1
const int drop
edm::EDGetTokenT< reco::GenParticleCollection > srcToken_
std::vector< std::string > selection_

Member Function Documentation

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

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

104  {
106  for(GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i)
107  flags_[i->key()] = keepOrDrop;
108 }
std::vector< int > flags_
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 GenParticlePruner::flagMothers ( const reco::GenParticle gen,
int  keepOrDrop 
)
private

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

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

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

Referenced by produce().

276  {
277  for(GenParticleRefVector::const_iterator j = daughters.begin();
278  j != daughters.end(); ++j) {
279  GenParticleRef dau = *j;
280  if (find(daIndxs.begin(), daIndxs.end(), dau.key()) == daIndxs.end()) {
281  daIndxs.push_back( dau.key() );
282  int idx = flags_[dau.key()];
283  if (idx > 0 ) {
284  daNewIndxs.push_back( idx );
285  } else {
286  const GenParticleRefVector & daus = dau->daughterRefVector();
287  if(!daus.empty())
288  getDaughterKeys(daIndxs, daNewIndxs, daus);
289  }
290  }
291  }
292 }
std::vector< int > flags_
key_type key() const
Accessor for product key.
Definition: Ref.h:265
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 GenParticlePruner::getMotherKeys ( std::vector< size_t > &  moIndxs,
std::vector< size_t > &  moNewIndxs,
const reco::GenParticleRefVector mothers 
) const
private

Definition at line 296 of file GenParticlePruner.cc.

References edm::RefVector< C, T, F >::begin(), DEFINE_FWK_MODULE, 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().

Referenced by produce().

297  {
299  j != mothers.end(); ++j) {
300  GenParticleRef mom = *j;
301  if (find(moIndxs.begin(), moIndxs.end(), mom.key()) == moIndxs.end()) {
302  moIndxs.push_back( mom.key() );
303  int idx = flags_[mom.key()];
304  if (idx >= 0 ) {
305  moNewIndxs.push_back( idx );
306  } else {
307  const GenParticleRefVector & moms = mom->motherRefVector();
308  if(!moms.empty())
309  getMotherKeys(moIndxs, moNewIndxs, moms);
310  }
311  }
312  }
313 }
std::vector< int > flags_
key_type key() const
Accessor for product key.
Definition: Ref.h:265
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 getMotherKeys(std::vector< size_t > &, std::vector< size_t > &, const reco::GenParticleRefVector &) const
void GenParticlePruner::parse ( const std::string &  selection,
helper::SelectCode code,
std::string &  cut 
) const
private

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

49  {
50  using namespace ::helper;
51  size_t f = selection.find_first_not_of(' ');
52  size_t n = selection.size();
53  string command;
54  char c;
55  for(; (c = selection[f]) != ' ' && f < n; ++f) {
56  command.push_back(c);
57  }
58  if(command[0] == '+') {
59  command.erase(0, 1);
60  if(command[0] == '+') {
61  command.erase(0, 1);
62  code.mothersDepth_ = SelectCode::kAll;
63  } else {
64  code.mothersDepth_ = SelectCode::kFirst;
65  }
66  } else
68 
69  if(command[command.size() - 1] == '+') {
70  command.erase(command.size() - 1);
71  if(command[command.size()-1] == '+') {
72  command.erase(command.size() - 1);
73  code.daughtersDepth_ = SelectCode::kAll;
74  } else {
75  code.daughtersDepth_ = SelectCode::kFirst;
76  }
77  } else
79 
80  if(command == "keep") code.keepOrDrop_ = SelectCode::kKeep;
81  else if(command == "drop") code.keepOrDrop_ = SelectCode::kDrop;
82  else {
84  << "invalid selection command: " << command << "\n" << endl;
85  }
86  for(; f < n; ++f) {
87  if(selection[f] != ' ') break;
88  }
89  cut = string(selection, f);
90  if(cut[0] == '*')
91  cut = string(cut, 0, cut.find_first_of(' '));
92  code.all_ = cut == "*";
93 }
Definition: helper.py:1
selection
main part
Definition: corrVsCorr.py:98
double f[11][100]
list command
Definition: mps_check.py:23
void GenParticlePruner::produce ( edm::Event evt,
const edm::EventSetup es 
)
overrideprivate

Definition at line 154 of file GenParticlePruner.cc.

References reco::CompositeRefCandidateT< D >::addDaughter(), reco::CompositeRefCandidateT< D >::addMother(), helper::SelectCode::all_, edm::errors::Configuration, TkAlMuonSelectors_cfi::cut, reco::CompositeRefCandidateT< D >::daughterRefVector(), drop, Exception, firstEvent_, flagDaughters(), flagMothers(), flags_, relval_steps::gen(), GenParticle::GenParticle, edm::Event::getByToken(), getDaughterKeys(), getMotherKeys(), edm::Event::getRefBeforePut(), mps_fire::i, indices_, keep, helper::SelectCode::keepOrDrop_, keepOrDropAll_, WDecay::kNone, reco::CompositeRefCandidateT< D >::motherRefVector(), 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_, TrackRefitter_38T_cff::src, srcToken_, and reco::GenParticle::statusFlags().

154  {
155  if (firstEvent_) {
156  PdgEntryReplacer rep(es);
157  for(vector<string>::const_iterator i = selection_.begin(); i != selection_.end(); ++i) {
158  string cut;
160  parse(*i, code, cut);
161  if(code.all_) {
162  if(i != selection_.begin())
164  << "selections \"keep *\" and \"drop *\" can be used only as first options. Here used in position # "
165  << (i - selection_.begin()) + 1 << "\n" << endl;
166  switch(code.keepOrDrop_) {
167  case ::helper::SelectCode::kDrop :
168  keepOrDropAll_ = drop; break;
169  case ::helper::SelectCode::kKeep :
171  };
172  } else {
173  cut = rep.replace(cut);
174  select_.push_back(make_pair(StringCutObjectSelector<GenParticle>(cut), code));
175  }
176  }
177  firstEvent_ = false;
178  }
179 
180  using namespace ::helper;
182  evt.getByToken(srcToken_, src);
183  const size_t n = src->size();
184  flags_.clear();
185  flags_.resize(n, keepOrDropAll_);
186  for(size_t j = 0; j < select_.size(); ++j) {
187  const pair<StringCutObjectSelector<GenParticle>, SelectCode> & sel = select_[j];
188  SelectCode code = sel.second;
189  const StringCutObjectSelector<GenParticle> & cut = sel.first;
190  for(size_t i = 0; i < n; ++i) {
191  const GenParticle & p = (*src)[i];
192  if(cut(p)) {
193  int keepOrDrop = keep;
194  switch(code.keepOrDrop_) {
195  case SelectCode::kKeep:
196  keepOrDrop = keep; break;
197  case SelectCode::kDrop:
198  keepOrDrop = drop;
199  };
200  flags_[i] = keepOrDrop;
201  std::vector<size_t> allIndicesDa;
202  std::vector<size_t> allIndicesMo;
203  switch(code.daughtersDepth_) {
204  case SelectCode::kAll :
205  recursiveFlagDaughters(i, *src, keepOrDrop, allIndicesDa); break;
206  case SelectCode::kFirst :
207  flagDaughters(p, keepOrDrop); break;
208  case SelectCode::kNone:
209  ;
210  };
211  switch(code.mothersDepth_) {
212  case SelectCode::kAll :
213  recursiveFlagMothers(i, *src, keepOrDrop, allIndicesMo); break;
214  case SelectCode::kFirst :
215  flagMothers(p, keepOrDrop); break;
216  case SelectCode::kNone:
217  ;
218  };
219  }
220  }
221  }
222  indices_.clear();
223  int counter = 0;
224  for(size_t i = 0; i < n; ++i) {
225  if(flags_[i] == keep) {
226  indices_.push_back(i);
227  flags_[i] = counter++;
228  } else
229  {
230  flags_[i]=-1; //set to invalid ref
231  }
232  }
233 
234  auto out = std::make_unique<GenParticleCollection>();
236  out->reserve(counter);
237 
238  for(vector<size_t>::const_iterator i = indices_.begin(); i != indices_.end(); ++i) {
239  size_t index = *i;
240  const GenParticle & gen = (*src)[index];
241  const LeafCandidate & part = gen;
242  out->push_back(GenParticle(part));
243  GenParticle & newGen = out->back();
244  //fill status flags
245  newGen.statusFlags() = gen.statusFlags();
246  // The "daIndxs" and "moIndxs" keep a list of the keys for the mother/daughter
247  // parentage/descendency. In some cases, a circular referencing is encountered,
248  // which would result in an infinite loop. The list is checked to
249  // avoid this.
250  vector<size_t> daIndxs, daNewIndxs;
251  getDaughterKeys(daIndxs, daNewIndxs, gen.daughterRefVector());
252  std::sort(daNewIndxs.begin(),daNewIndxs.end());
253  for(size_t i=0; i<daNewIndxs.size(); ++i)
254  newGen.addDaughter( GenParticleRef(outRef, daNewIndxs[i]) );
255 
256  vector<size_t> moIndxs, moNewIndxs;
257  getMotherKeys(moIndxs, moNewIndxs, gen.motherRefVector());
258  std::sort(moNewIndxs.begin(),moNewIndxs.end());
259  for(size_t i=0; i<moNewIndxs.size(); ++i)
260  newGen.addMother( GenParticleRef(outRef, moNewIndxs[i]) );
261  }
262 
263 
265  auto orig2new = std::make_unique<edm::Association<reco::GenParticleCollection>>(oh);
267  orig2newFiller.insert(src, flags_.begin(), flags_.end());
268  orig2newFiller.fill();
269  evt.put(std::move(orig2new));
270 
271 
272 }
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:137
Definition: helper.py:1
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
void flagMothers(const reco::GenParticle &, int)
void recursiveFlagMothers(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
const daughters & daughterRefVector() const
references to daughtes
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
const mothers & motherRefVector() const
references to mothers
rep
Definition: cuy.py:1189
def gen(fragment, howMuch)
Production test section ####.
void parse(const std::string &selection, helper::SelectCode &code, std::string &cut) const
void addMother(const typename mothers::value_type &)
add a daughter via a reference
RefProd< PROD > getRefBeforePut()
Definition: Event.h:167
const int drop
const GenStatusFlags & statusFlags() const
Definition: GenParticle.h:41
part
Definition: HCALResponse.h:20
edm::EDGetTokenT< reco::GenParticleCollection > srcToken_
std::vector< std::string > selection_
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
def move(src, dest)
Definition: eostools.py:510
void GenParticlePruner::recursiveFlagDaughters ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
)
private

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

117  {
118  GenParticleRefVector daughters = src[index].daughterRefVector();
119  // avoid infinite recursion if the daughters are set to "this" particle.
120  size_t cachedIndex = index;
121  for(GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i) {
122  index = i->key();
123  // To also avoid infinite recursion if a "loop" is found in the daughter list,
124  // check to make sure the index hasn't already been added.
125  if ( find( allIndices.begin(), allIndices.end(), index ) == allIndices.end() ) {
126  allIndices.push_back( index );
127  if ( cachedIndex != index ) {
128  flags_[index] = keepOrDrop;
129  recursiveFlagDaughters(index, src, keepOrDrop, allIndices);
130  }
131  }
132  }
133 }
std::vector< int > flags_
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 recursiveFlagDaughters(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:248
void GenParticlePruner::recursiveFlagMothers ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
)
private

Definition at line 135 of file GenParticlePruner.cc.

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

Referenced by produce().

136  {
137  GenParticleRefVector mothers = src[index].motherRefVector();
138  // avoid infinite recursion if the mothers are set to "this" particle.
139  size_t cachedIndex = index;
140  for(GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i) {
141  index = i->key();
142  // To also avoid infinite recursion if a "loop" is found in the daughter list,
143  // check to make sure the index hasn't already been added.
144  if ( find( allIndices.begin(), allIndices.end(), index ) == allIndices.end() ) {
145  allIndices.push_back( index );
146  if ( cachedIndex != index ) {
147  flags_[index] = keepOrDrop;
148  recursiveFlagMothers(index, src, keepOrDrop, allIndices);
149  }
150  }
151  }
152 }
std::vector< int > flags_
void recursiveFlagMothers(size_t, const reco::GenParticleCollection &, int, std::vector< size_t > &)
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

Member Data Documentation

bool GenParticlePruner::firstEvent_
private

Definition at line 27 of file GenParticlePruner.cc.

Referenced by produce().

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

Definition at line 33 of file GenParticlePruner.cc.

Referenced by produce().

int GenParticlePruner::keepOrDropAll_
private

Definition at line 29 of file GenParticlePruner.cc.

Referenced by produce().

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

Definition at line 31 of file GenParticlePruner.cc.

Referenced by produce().

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

Definition at line 30 of file GenParticlePruner.cc.

Referenced by produce().

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

Definition at line 28 of file GenParticlePruner.cc.

Referenced by produce().