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 (reco::GenParticle &, reco::GenParticleRefProd, const reco::GenParticleRefVector &) const
void addMotherRefs (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)
void recursiveFlagMothers (size_t, const reco::GenParticleCollection &, int)

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 ( reco::GenParticle newGen,
reco::GenParticleRefProd  outRef,
const reco::GenParticleRefVector daughters 
) const [private]

Definition at line 224 of file GenParticlePruner.cc.

References reco::CompositeRefCandidateT< D >::addDaughter(), edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::end(), flags_, 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;
    int idx = flags_[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(newGen, outRef, daus);
    }
  }
}
void GenParticlePruner::addMotherRefs ( reco::GenParticle newGen,
reco::GenParticleRefProd  outRef,
const reco::GenParticleRefVector mothers 
) const [private]

Definition at line 241 of file GenParticlePruner.cc.

References reco::CompositeRefCandidateT< D >::addMother(), edm::RefVector< C, T, F >::begin(), edm::RefVector< C, T, F >::end(), flags_, 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;
    int idx = flags_[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(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 132 of file GenParticlePruner.cc.

References addDaughterRefs(), addMotherRefs(), edm::errors::Configuration, align_tpl::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(), runTheMatrix::sel, select_, selection_, align_tpl::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;
        switch(code.daughtersDepth_) {
        case SelectCode::kAll : 
          recursiveFlagDaughters(i, *src, keepOrDrop); break;
        case SelectCode::kFirst :
          flagDaughters(p, keepOrDrop); break;
        case SelectCode::kNone:
          ;
        };
        switch(code.mothersDepth_) {
        case SelectCode::kAll :
          recursiveFlagMothers(i, *src, keepOrDrop); 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();
    addDaughterRefs(newGen, outRef, gen.daughterRefVector());
    addMotherRefs(newGen, outRef, gen.motherRefVector());
  }

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

Definition at line 114 of file GenParticlePruner.cc.

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

Referenced by produce().

                                                                                                                  {
  GenParticleRefVector daughters = src[index].daughterRefVector();
  for(GenParticleRefVector::const_iterator i = daughters.begin(); i != daughters.end(); ++i) {
    index = i->key();
    flags_[index] = keepOrDrop;
    recursiveFlagDaughters(index, src, keepOrDrop);
  }
}
void GenParticlePruner::recursiveFlagMothers ( size_t  index,
const reco::GenParticleCollection src,
int  keepOrDrop 
) [private]

Definition at line 123 of file GenParticlePruner.cc.

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

Referenced by produce().

                                                                                                                {
  GenParticleRefVector mothers = src[index].motherRefVector();
  for(GenParticleRefVector::const_iterator i = mothers.begin(); i != mothers.end(); ++i) {
    index = i->key();
    flags_[index] = keepOrDrop;
    recursiveFlagMothers(index, src, keepOrDrop);
  }
}

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().