CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

GenParticlePruner Class Reference

Inheritance diagram for GenParticlePruner:
edm::EDProducer edm::ProducerBase edm::ProductRegistryHelper

List of all members.

Public Member Functions

 GenParticlePruner (const edm::ParameterSet &)

Private Member Functions

void addDaughterRefs (std::vector< size_t > &, reco::GenParticle &, reco::GenParticleRefProd, const reco::GenParticleRefVector &) const
void addMotherRefs (std::vector< size_t > &, reco::GenParticle &, reco::GenParticleRefProd, const reco::GenParticleRefVector &) const
void flagDaughters (const reco::GenParticle &, int)
void flagMothers (const reco::GenParticle &, int)
void parse (const std::string &selection, helper::SelectCode &code, std::string &cut) const
void produce (edm::Event &, const edm::EventSetup &)
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::InputTag src_

Detailed Description

Definition at line 21 of file GenParticlePruner.cc.


Constructor & Destructor Documentation

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

Definition at line 94 of file GenParticlePruner.cc.

                                                            :
  firstEvent_(true),
  src_(cfg.getParameter<InputTag>("src")), keepOrDropAll_(drop),
  selection_(cfg.getParameter<vector<string> >("select")) {
  using namespace ::helper;
  produces<GenParticleCollection>();
}

Member Function Documentation

void GenParticlePruner::addDaughterRefs ( std::vector< size_t > &  ,
reco::GenParticle ,
reco::GenParticleRefProd  ,
const reco::GenParticleRefVector  
) const [private]

Definition at line 252 of file GenParticlePruner.cc.

References reco::CompositeRefCandidateT< D >::addDaughter(), edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::end(), spr::find(), flags_, customizeTrackingMonitorSeedNumber::idx, j, edm::Ref< C, T, F >::key(), and edm::RefVector< C, T, F >::size().

Referenced by produce().

                                                                                     {
  for(GenParticleRefVector::const_iterator j = daughters.begin();
      j != daughters.end(); ++j) {
    GenParticleRef dau = *j;
    if ( find(daIndxs.begin(), daIndxs.end(), dau.key()) == daIndxs.end() ) {
      int idx = flags_[dau.key()];
      daIndxs.push_back( dau.key() );
      if(idx > 0) {
        GenParticleRef newDau(outRef, static_cast<size_t>(idx));
        newGen.addDaughter(newDau);
      } else {
        const GenParticleRefVector daus = dau->daughterRefVector();
        if(daus.size()>0) {
          addDaughterRefs(daIndxs, newGen, outRef, daus);
        }
      }
    }
  }
}
void GenParticlePruner::addMotherRefs ( std::vector< size_t > &  ,
reco::GenParticle ,
reco::GenParticleRefProd  ,
const reco::GenParticleRefVector  
) const [private]

Definition at line 276 of file GenParticlePruner.cc.

References reco::CompositeRefCandidateT< D >::addMother(), edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::end(), spr::find(), flags_, customizeTrackingMonitorSeedNumber::idx, j, edm::Ref< C, T, F >::key(), and edm::RefVector< C, T, F >::size().

Referenced by produce().

                                                                                 {
  for(GenParticleRefVector::const_iterator j = mothers.begin();
      j != mothers.end(); ++j) {
    GenParticleRef mom = *j;
    if ( find(moIndxs.begin(), moIndxs.end(), mom.key()) == moIndxs.end() ) {
      int idx = flags_[mom.key()];
      moIndxs.push_back( mom.key() );
      if(idx > 0) {
        GenParticleRef newMom(outRef, static_cast<size_t>(idx));
        newGen.addMother(newMom);
      } else {
        const GenParticleRefVector moms = mom->motherRefVector();
        if(moms.size()>0)
          addMotherRefs(moIndxs, newGen, outRef, moms);
      }
    }
  }
}
void GenParticlePruner::flagDaughters ( const reco::GenParticle gen,
int  keepOrDrop 
) [private]
void GenParticlePruner::flagMothers ( const reco::GenParticle gen,
int  keepOrDrop 
) [private]
void GenParticlePruner::parse ( const std::string &  selection,
helper::SelectCode code,
std::string &  cut 
) const [private]

Referenced by produce().

void GenParticlePruner::produce ( edm::Event evt,
const edm::EventSetup es 
) [private, virtual]

Implements edm::EDProducer.

Definition at line 152 of file GenParticlePruner.cc.

References addDaughterRefs(), addMotherRefs(), edm::errors::Configuration, GOODCOLL_filter_cfg::cut, reco::CompositeRefCandidateT< D >::daughterRefVector(), drop, Exception, firstEvent_, flagDaughters(), flagMothers(), flags_, cmsDownloadME::gen, configurableAnalysis::GenParticle, edm::Event::getByLabel(), edm::Event::getRefBeforePut(), i, getHLTprescales::index, indices_, j, keep, keepOrDropAll_, WDecay::kNone, reco::CompositeRefCandidateT< D >::motherRefVector(), n, dbtoconf::out, AlCaHLTBitMon_ParallelJobs::p, parse(), edm::Event::put(), recursiveFlagDaughters(), recursiveFlagMothers(), PdgEntryReplacer::replace(), EgammaValidation_Wenu_cff::sel, select_, selection_, alcazmumu_cfi::src, and src_.

                                                                {
  if (firstEvent_) {
    PdgEntryReplacer rep(es);
    for(vector<string>::const_iterator i = selection_.begin(); i != selection_.end(); ++i) {
      string cut;
      ::helper::SelectCode code;
      parse(*i, code, cut);
      if(code.all_) {
        if(i != selection_.begin())
          throw Exception(errors::Configuration)
            << "selections \"keep *\" and \"drop *\" can be used only as first options. Here used in position # "
            << (i - selection_.begin()) + 1 << "\n" << endl;
        switch(code.keepOrDrop_) {
        case ::helper::SelectCode::kDrop :
          keepOrDropAll_ = drop; break;
        case ::helper::SelectCode::kKeep :
          keepOrDropAll_ = keep;
        };
      } else {
        cut = rep.replace(cut);
        select_.push_back(make_pair(StringCutObjectSelector<GenParticle>(cut), code));
      }
    }
    firstEvent_ = false;
  }

  using namespace ::helper;
  Handle<GenParticleCollection> src;
  evt.getByLabel(src_, src);
  const size_t n = src->size();
  flags_.clear();
  flags_.resize(n, keepOrDropAll_);
  for(size_t j = 0; j < select_.size(); ++j) { 
    const pair<StringCutObjectSelector<GenParticle>, SelectCode> & sel = select_[j];
    SelectCode code = sel.second;
    const StringCutObjectSelector<GenParticle> & cut = sel.first;
    for(size_t i = 0; i < n; ++i) {
      const GenParticle & p = (*src)[i];
      if(cut(p)) {
        int keepOrDrop = keep;
        switch(code.keepOrDrop_) {
        case SelectCode::kKeep:
          keepOrDrop = keep; break;
        case SelectCode::kDrop:
          keepOrDrop = drop; 
        };
        flags_[i] = keepOrDrop;
        std::vector<size_t> allIndicesDa;
        std::vector<size_t> allIndicesMo;
        switch(code.daughtersDepth_) {
        case SelectCode::kAll : 
          recursiveFlagDaughters(i, *src, keepOrDrop, allIndicesDa); break;
        case SelectCode::kFirst :
          flagDaughters(p, keepOrDrop); break;
        case SelectCode::kNone:
          ;
        };
        switch(code.mothersDepth_) {
        case SelectCode::kAll :
          recursiveFlagMothers(i, *src, keepOrDrop, allIndicesMo); break;
        case SelectCode::kFirst :
          flagMothers(p, keepOrDrop); break;
        case SelectCode::kNone:
          ;
        };
      }
    }
  }
  indices_.clear();
  int counter = 0;
  for(size_t i = 0; i < n; ++i) {
    if(flags_[i] == keep) {
      indices_.push_back(i);
      flags_[i] = counter++;
    }
  }

  auto_ptr<GenParticleCollection> out(new GenParticleCollection);
  GenParticleRefProd outRef = evt.getRefBeforePut<GenParticleCollection>();
  out->reserve(counter);
  for(vector<size_t>::const_iterator i = indices_.begin(); i != indices_.end(); ++i) {
    size_t index = *i;
    const GenParticle & gen = (*src)[index];
    const LeafCandidate & part = gen;
    out->push_back(GenParticle(part));
    GenParticle & newGen = out->back();
    // The "daIndxs" and "moIndxs" keep a list of the keys for the mother/daughter
    // parentage/descendency. In some cases, a circular referencing is encountered,
    // which would result in an infinite loop. The list is checked to
    // avoid this. 
    vector<size_t> daIndxs;
    addDaughterRefs(daIndxs, newGen, outRef, gen.daughterRefVector());
    vector<size_t> moIndxs;
    addMotherRefs(moIndxs, newGen, outRef, gen.motherRefVector());
  }

  evt.put(out);
}
void GenParticlePruner::recursiveFlagDaughters ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
) [private]

Definition at line 114 of file GenParticlePruner.cc.

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

Referenced by produce().

                                                                                {
  GenParticleRefVector daughters = src[index].daughterRefVector();
  // avoid infinite recursion if the daughters are set to "this" particle.
  size_t cachedIndex = index;
  for(GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i) {
    index = i->key();
    // To also avoid infinite recursion if a "loop" is found in the daughter list,
    // check to make sure the index hasn't already been added. 
    if ( find( allIndices.begin(), allIndices.end(), index ) == allIndices.end() ) {
      allIndices.push_back( index );
      if ( cachedIndex != index ) {
        flags_[index] = keepOrDrop;
        recursiveFlagDaughters(index, src, keepOrDrop, allIndices);
      }
    }
  }
}
void GenParticlePruner::recursiveFlagMothers ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop,
std::vector< size_t > &  allIndices 
) [private]

Definition at line 133 of file GenParticlePruner.cc.

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

Referenced by produce().

                                                                              {
  GenParticleRefVector mothers = src[index].motherRefVector();
  // avoid infinite recursion if the mothers are set to "this" particle.
  size_t cachedIndex = index;
  for(GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i) {
    index = i->key();
    // To also avoid infinite recursion if a "loop" is found in the daughter list,
    // check to make sure the index hasn't already been added. 
    if ( find( allIndices.begin(), allIndices.end(), index ) == allIndices.end() ) {
      allIndices.push_back( index );
      if ( cachedIndex != index ) {
        flags_[index] = keepOrDrop;
        recursiveFlagMothers(index, src, keepOrDrop, allIndices);
      }
    }
  }
}

Member Data Documentation

Definition at line 26 of file GenParticlePruner.cc.

Referenced by produce().

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

Definition at line 32 of file GenParticlePruner.cc.

Referenced by produce().

Definition at line 28 of file GenParticlePruner.cc.

Referenced by produce().

Definition at line 30 of file GenParticlePruner.cc.

Referenced by produce().

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

Definition at line 29 of file GenParticlePruner.cc.

Referenced by produce().

Definition at line 27 of file GenParticlePruner.cc.

Referenced by produce().