CMS 3D CMS Logo

Public Member Functions | Public Attributes | Private Attributes

GenParticleProducer Class Reference

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

List of all members.

Public Member Functions

int chargeTimesThree (int) const
bool convertParticle (reco::GenParticle &cand, const HepMC::GenParticle *part)
bool fillDaughters (reco::GenParticleCollection &cand, const HepMC::GenParticle *part, size_t index)
bool fillIndices (const HepMC::GenEvent *mc, std::vector< const HepMC::GenParticle * > &particles, std::vector< int > &barCodeVector, int offset)
 GenParticleProducer (const edm::ParameterSet &)
 constructor
virtual void produce (edm::Event &e, const edm::EventSetup &)
 process one event
 ~GenParticleProducer ()
 destructor

Public Attributes

std::map< int, size_t > barcodes_
reco::GenParticleRefProd ref_

Private Attributes

bool abortOnUnknownPDGCode_
 unknown code treatment flag
std::vector< int > chargeM_
std::map< int, int > chargeMap_
std::vector< int > chargeP_
 charge indices
bool doSubEvent_
 input & output modes
bool firstEvent_
 whether the first event was looked at
bool saveBarCodes_
 save bar-codes
edm::InputTag src_
 source collection name
bool useCF_
std::vector< std::string > vectorSrc_

Detailed Description

Definition at line 25 of file GenParticleProducer.cc.


Constructor & Destructor Documentation

GenParticleProducer::GenParticleProducer ( const edm::ParameterSet cfg)

constructor

Definition at line 85 of file GenParticleProducer.cc.

References ExpressReco_HICollisions_FallBack::alias, doSubEvent_, edm::ParameterSet::getParameter(), edm::ParameterSet::getUntrackedParameter(), saveBarCodes_, src_, useCF_, and vectorSrc_.

                                                                   :
  firstEvent_(true), 
  abortOnUnknownPDGCode_( cfg.getUntrackedParameter<bool>( "abortOnUnknownPDGCode", true ) ),
  saveBarCodes_( cfg.getUntrackedParameter<bool>( "saveBarCodes", false ) ),
  chargeP_( PDGCacheMax, 0 ), chargeM_( PDGCacheMax, 0 ),
  doSubEvent_(cfg.getUntrackedParameter<bool>( "doSubEvent", false )),
  useCF_(cfg.getUntrackedParameter<bool>( "useCrossingFrame", false ))
{
  produces<GenParticleCollection>();
  if( saveBarCodes_ ) {
    std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
    produces<vector<int> >().setBranchAlias( alias + "BarCodes" );
  }                               

  if(doSubEvent_){
     vectorSrc_ = cfg.getParameter<std::vector<std::string> >( "srcVector" );
     //     produces<SubEventMap>();
  }else if(useCF_) src_ = cfg.getUntrackedParameter<InputTag>( "src" , InputTag("mix","generator"));
  else src_ = cfg.getParameter<InputTag>( "src" );
  
}
GenParticleProducer::~GenParticleProducer ( )

destructor

Definition at line 107 of file GenParticleProducer.cc.

                                          { 
}

Member Function Documentation

int GenParticleProducer::chargeTimesThree ( int  id) const

Definition at line 110 of file GenParticleProducer.cc.

References abortOnUnknownPDGCode_, abs, chargeM_, chargeMap_, chargeP_, Exception, f, ExpressReco_HICollisions_FallBack::id, edm::errors::LogicError, RecoTau_DiTaus_pt_20-420_cfg::ParticleID, and PDGCacheMax.

Referenced by convertParticle().

                                                        {
  if( std::abs( id ) < PDGCacheMax ) 
    return id > 0 ? chargeP_[ id ] : chargeM_[ - id ];
  map<int, int>::const_iterator f = chargeMap_.find( id );
  if ( f == chargeMap_.end() )  {
    if ( abortOnUnknownPDGCode_ )
      throw edm::Exception( edm::errors::LogicError ) 
        << "invalid PDG id: " << id << endl;
    else
      return HepPDT::ParticleID(id).threeCharge();
  }
  return f->second;
}
bool GenParticleProducer::convertParticle ( reco::GenParticle cand,
const HepMC::GenParticle *  part 
)

Definition at line 275 of file GenParticleProducer.cc.

References chargeTimesThree(), mmToCm, p4, benchmark_cfg::pdgId, reco::GenParticle::setCollisionId(), reco::LeafCandidate::setP4(), reco::LeafCandidate::setPdgId(), reco::LeafCandidate::setStatus(), reco::LeafCandidate::setThreeCharge(), reco::LeafCandidate::setVertex(), and v.

Referenced by produce().

                                                                                             {
   Candidate::LorentzVector p4( part->momentum() );
   int pdgId = part->pdg_id();
   cand.setThreeCharge( chargeTimesThree( pdgId ) );
   cand.setPdgId( pdgId );
   cand.setStatus( part->status() );
   cand.setP4( p4 );
   cand.setCollisionId(0);
   const GenVertex * v = part->production_vertex();
   if ( v != 0 ) {
      ThreeVector vtx = v->point3d();
      Candidate::Point vertex( vtx.x() * mmToCm, vtx.y() * mmToCm, vtx.z() * mmToCm );
      cand.setVertex( vertex );
   } else {
      cand.setVertex( Candidate::Point( 0, 0, 0 ) );
   }
   return true;
}
bool GenParticleProducer::fillDaughters ( reco::GenParticleCollection cand,
const HepMC::GenParticle *  part,
size_t  index 
)

Definition at line 294 of file GenParticleProducer.cc.

References barcodes_, configurableAnalysis::GenParticle, getHLTprescales::index, m, ref_, and edm::second().

Referenced by produce().

                                                                                                                    {

   const GenVertex * productionVertex = part->production_vertex();
   size_t numberOfMothers = productionVertex->particles_in_size();
   if ( numberOfMothers > 0 ) {
      GenVertex::particles_in_const_iterator motherIt = productionVertex->particles_in_const_begin();
      for( ; motherIt != productionVertex->particles_in_const_end(); motherIt++) {
         const HepMC::GenParticle * mother = * motherIt;
         size_t m = barcodes_.find( mother->barcode() )->second;
         cands[ m ].addDaughter( GenParticleRef( ref_, index ) );
         cands[ index ].addMother( GenParticleRef( ref_, m ) );
      }
   }

   return true;
}
bool GenParticleProducer::fillIndices ( const HepMC::GenEvent *  mc,
std::vector< const HepMC::GenParticle * > &  particles,
std::vector< int > &  barCodeVector,
int  offset 
)

Definition at line 311 of file GenParticleProducer.cc.

References barcodes_, begin, end, configurableAnalysis::GenParticle, evf::evtn::offset(), and L1TEmulatorMonitor_cff::p.

Referenced by produce().

                                                                                                                                                 {
   size_t idx = offset;
   HepMC::GenEvent::particle_const_iterator begin = mc->particles_begin(), end = mc->particles_end();
   for( HepMC::GenEvent::particle_const_iterator p = begin; p != end; ++ p ) {
      const HepMC::GenParticle * particle = * p;
      size_t barCode_this_event = particle->barcode();
      size_t barCode = barCode_this_event + offset;
      if( barcodes_.find(barCode) != barcodes_.end() )
         throw cms::Exception( "WrongReference" )
            << "barcodes are duplicated! " << endl;
      particles[idx] = particle;
      barCodeVector[idx] = barCode;
      barcodes_.insert( make_pair(barCode_this_event, idx ++) );
   }
      return true;
}
void GenParticleProducer::produce ( edm::Event e,
const edm::EventSetup es 
) [virtual]

process one event

fill indices

Implements edm::EDProducer.

Definition at line 124 of file GenParticleProducer.cc.

References abs, barcodes_, chargeM_, chargeMap_, chargeP_, convertParticle(), doSubEvent_, Exception, fillDaughters(), fillIndices(), firstEvent_, configurableAnalysis::GenParticle, edm::Event::getByLabel(), edm::EventSetup::getData(), edm::HepMCProduct::GetEvent(), MixCollection< T >::getObject(), edm::Event::getRefBeforePut(), patZpeak::handle, i, edm::RefProd< C >::id(), edm::errors::InvalidReference, LogDebug, evf::evtn::offset(), L1TEmulatorMonitor_cff::p, RecoTau_DiTaus_pt_20-420_cfg::ParticleID, PDGCacheMax, benchmark_cfg::pdgId, edm::Handle< T >::product(), edm::Event::put(), ref_, reco::CompositeRefCandidateT< D >::resetDaughters(), saveBarCodes_, MixCollection< T >::size(), findQualityFiles::size, src_, useCF_, and vectorSrc_.

                                                                    {

  if (firstEvent_) {
     ESHandle<HepPDT::ParticleDataTable> pdt;
     es.getData( pdt );
     for( HepPDT::ParticleDataTable::const_iterator p = pdt->begin(); p != pdt->end(); ++ p ) {
       const HepPDT::ParticleID & id = p->first;
       int pdgId = id.pid(), apdgId = std::abs( pdgId );
       int q3 = id.threeCharge();
       if ( apdgId < PDGCacheMax && pdgId > 0 ) {
         chargeP_[ apdgId ] = q3;
         chargeM_[ apdgId ] = -q3;
       } else if ( apdgId < PDGCacheMax ) {
         chargeP_[ apdgId ] = -q3;
         chargeM_[ apdgId ] = q3;
       } else {
         chargeMap_[ pdgId ] = q3;
         chargeMap_[ -pdgId ] = -q3;
       }
     }
     firstEvent_ = false; 
   }
      
   barcodes_.clear();
   
   size_t totalSize = 0;
   const GenEvent * mc = 0;   
   std::vector<Handle<HepMCProduct> > heps;
   MixCollection<HepMCProduct>* cfhepmcprod = 0;
   size_t npiles = vectorSrc_.size();

   if(useCF_){
      Handle<CrossingFrame<HepMCProduct> > cf;
      evt.getByLabel(InputTag("mix","generator"),cf);
      cfhepmcprod = new MixCollection<HepMCProduct>(cf.product());
      npiles = cfhepmcprod->size();
      for(unsigned int icf = 0; icf < npiles; ++icf){
         totalSize += cfhepmcprod->getObject(icf).GetEvent()->particles_size();
      }
   }else if (doSubEvent_){
      for(size_t i = 0; i < npiles; ++i){
        //       cout<<"Tag "<<vectorSrc_[i]<<endl;
         Handle<HepMCProduct> handle;
         heps.push_back(handle);
         evt.getByLabel( vectorSrc_[i], heps[i] );
         totalSize += heps[i]->GetEvent()->particles_size();
      }
   }else{
      Handle<HepMCProduct> mcp;
      evt.getByLabel( src_, mcp );
      mc = mcp->GetEvent();
      if( mc == 0 ) 
         throw edm::Exception( edm::errors::InvalidReference ) 
            << "HepMC has null pointer to GenEvent" << endl;
      totalSize  = mc->particles_size();
   }
      
   // initialise containers 
   const size_t size = totalSize;
  vector<const HepMC::GenParticle *> particles( size );
  auto_ptr<GenParticleCollection> candsPtr( new GenParticleCollection( size ) );
  //  auto_ptr<SubEventMap> subsPtr( new SubEventMap() );
  auto_ptr<vector<int> > barCodeVector( new vector<int>( size ) );
  ref_ = evt.getRefBeforePut<GenParticleCollection>();
  GenParticleCollection & cands = * candsPtr;
  //  SubEventMap & subs = *subsPtr;
  size_t offset = 0;
  size_t suboffset = 0;

  if(doSubEvent_ || useCF_){
     for(size_t i = 0; i < npiles; ++i){
        barcodes_.clear();
        if(useCF_) mc = cfhepmcprod->getObject(i).GetEvent();
        else mc = heps[i]->GetEvent();

        //Look whether heavy ion/signal event
        bool isHI = false;
        const HepMC::HeavyIon * hi = mc->heavy_ion();
        if(hi && hi->Ncoll_hard() > 1) isHI = true;
        size_t num_particles = mc->particles_size();
        fillIndices(mc, particles, *barCodeVector, offset);
        // fill output collection and save association 
        for( size_t i = offset; i < offset + num_particles; ++ i ) {

           const HepMC::GenParticle * part = particles[ i ];
           reco::GenParticle & cand = cands[ i ];
           // convert HepMC::GenParticle to new reco::GenParticle
           convertParticle(cand, part);
           cand.resetDaughters( ref_.id() );
        }

        for( size_t d = offset; d < offset + num_particles; ++ d ) {
           const HepMC::GenParticle * part = particles[ d ];
           const GenVertex * productionVertex = part->production_vertex();
           int sub_id = 0;
           if ( productionVertex != 0 ) {
              sub_id = productionVertex->id();
              if(!isHI) sub_id = 0;
              // search barcode map and attach daughters 
              fillDaughters(cands,part,d);
           }else{
              const GenVertex * endVertex = part->end_vertex();
              if(endVertex != 0) sub_id = endVertex->id();
              else throw cms::Exception( "SubEventID" )<<"SubEvent not determined. Particle has no production and no end vertex!"<<endl;
           }
           if(sub_id < 0) sub_id = 0;
           int new_id = sub_id + suboffset;
           GenParticleRef dref( ref_, d );
           //      subs.insert(dref,new_id);   // For SubEventMap
           cands[d].setCollisionId(new_id); // For new GenParticle
           LogDebug("VertexId")<<"SubEvent offset 3 : "<<suboffset;
        }
        int nsub = -2;
        if(isHI){
           nsub = hi->Ncoll_hard()+1;
           suboffset += nsub;
        }else{
           suboffset += 1;
        }
        offset += num_particles;
     }
  }else{
     fillIndices(mc, particles, *barCodeVector, 0);
     
     // fill output collection and save association
     for( size_t i = 0; i < particles.size(); ++ i ) {
        const HepMC::GenParticle * part = particles[ i ];
        reco::GenParticle & cand = cands[ i ];
        // convert HepMC::GenParticle to new reco::GenParticle
        convertParticle(cand, part);
        cand.resetDaughters( ref_.id() );
     }
     
     // fill references to daughters
     for( size_t d = 0; d < cands.size(); ++ d ) {
        const HepMC::GenParticle * part = particles[ d ];
        const GenVertex * productionVertex = part->production_vertex();
        // search barcode map and attach daughters
        if ( productionVertex != 0 ) fillDaughters(cands,part,d);
        cands[d].setCollisionId(0);
     }
  }
  
  evt.put( candsPtr );
  if(saveBarCodes_) evt.put( barCodeVector );
  //  if(doSubEvent_) evt.put(subsPtr); // For SubEventMap
  if(cfhepmcprod) delete cfhepmcprod;

}

Member Data Documentation

unknown code treatment flag

Definition at line 49 of file GenParticleProducer.cc.

Referenced by chargeTimesThree().

std::map<int, size_t> GenParticleProducer::barcodes_

Definition at line 38 of file GenParticleProducer.cc.

Referenced by fillDaughters(), fillIndices(), and produce().

std::vector<int> GenParticleProducer::chargeM_ [private]

Definition at line 53 of file GenParticleProducer.cc.

Referenced by chargeTimesThree(), and produce().

std::map<int, int> GenParticleProducer::chargeMap_ [private]

Definition at line 54 of file GenParticleProducer.cc.

Referenced by chargeTimesThree(), and produce().

std::vector<int> GenParticleProducer::chargeP_ [private]

charge indices

Definition at line 53 of file GenParticleProducer.cc.

Referenced by chargeTimesThree(), and produce().

input & output modes

Definition at line 57 of file GenParticleProducer.cc.

Referenced by GenParticleProducer(), and produce().

whether the first event was looked at

Definition at line 47 of file GenParticleProducer.cc.

Referenced by produce().

Definition at line 39 of file GenParticleProducer.cc.

Referenced by fillDaughters(), and produce().

save bar-codes

Definition at line 51 of file GenParticleProducer.cc.

Referenced by GenParticleProducer(), and produce().

source collection name

Definition at line 43 of file GenParticleProducer.cc.

Referenced by GenParticleProducer(), and produce().

Definition at line 58 of file GenParticleProducer.cc.

Referenced by GenParticleProducer(), and produce().

std::vector<std::string> GenParticleProducer::vectorSrc_ [private]

Definition at line 44 of file GenParticleProducer.cc.

Referenced by GenParticleProducer(), and produce().