CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GenParticleProducer.cc
Go to the documentation of this file.
1 /* \class GenParticleProducer
2  *
3  * \author Luca Lista, INFN
4  *
5  * Convert HepMC GenEvent format into a collection of type
6  * CandidateCollection containing objects of type GenParticle
7  *
8  * \version $Id: GenParticleProducer.cc,v 1.20 2013/02/27 23:16:51 wmtan Exp $
9  *
10  */
16 
17 #include <vector>
18 #include <string>
19 #include <map>
20 #include <set>
21 
22 namespace edm { class ParameterSet; }
23 namespace HepMC { class GenParticle; class GenEvent; }
24 
26  public:
31 
33  virtual void produce( edm::Event& e, const edm::EventSetup&) override;
34  int chargeTimesThree( int ) const;
36  bool fillDaughters(reco::GenParticleCollection& cand, const HepMC::GenParticle * part, size_t index);
37  bool fillIndices(const HepMC::GenEvent * mc, std::vector<const HepMC::GenParticle*>& particles, std::vector<int>& barCodeVector, int offset);
38  std::map<int, size_t> barcodes_;
40 
41  private:
44  std::vector<std::string> vectorSrc_;
46 
48  bool firstEvent_;
54  std::vector<int> chargeP_, chargeM_;
55  std::map<int, int> chargeMap_;
56 
59  bool useCF_;
60 
61 };
62 
65 
66 //#include "SimDataFormats/HiGenData/interface/SubEventMap.h"
69 
76 #include <fstream>
77 #include <algorithm>
78 using namespace edm;
79 using namespace reco;
80 using namespace std;
81 using namespace HepMC;
82 
83 static const int PDGCacheMax = 32768;
84 static const double mmToCm = 0.1;
85 
87  firstEvent_(true),
88  abortOnUnknownPDGCode_( cfg.getUntrackedParameter<bool>( "abortOnUnknownPDGCode", true ) ),
89  saveBarCodes_( cfg.getUntrackedParameter<bool>( "saveBarCodes", false ) ),
90  chargeP_( PDGCacheMax, 0 ), chargeM_( PDGCacheMax, 0 ),
91  doSubEvent_(cfg.getUntrackedParameter<bool>( "doSubEvent", false )),
92  useCF_(cfg.getUntrackedParameter<bool>( "useCrossingFrame", false ))
93 {
94  produces<GenParticleCollection>();
95  if( saveBarCodes_ ) {
96  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
97  produces<vector<int> >().setBranchAlias( alias + "BarCodes" );
98  }
99 
100  if(doSubEvent_){
101  vectorSrc_ = cfg.getParameter<std::vector<std::string> >( "srcVector" );
102  // produces<SubEventMap>();
103  }else if(useCF_) {
104  mixLabel_ = cfg.getParameter<std::string>( "mix" );
105  src_ = cfg.getUntrackedParameter<InputTag>( "src" , InputTag(mixLabel_,"generator"));
106  } else src_ = cfg.getParameter<InputTag>( "src" );
107 }
108 
110 }
111 
113  if( std::abs( id ) < PDGCacheMax )
114  return id > 0 ? chargeP_[ id ] : chargeM_[ - id ];
115  map<int, int>::const_iterator f = chargeMap_.find( id );
116  if ( f == chargeMap_.end() ) {
119  << "invalid PDG id: " << id << endl;
120  else
121  return HepPDT::ParticleID(id).threeCharge();
122  }
123  return f->second;
124 }
125 
127 
128  if (firstEvent_) {
130  es.getData( pdt );
131  for( HepPDT::ParticleDataTable::const_iterator p = pdt->begin(); p != pdt->end(); ++ p ) {
132  const HepPDT::ParticleID & id = p->first;
133  int pdgId = id.pid(), apdgId = std::abs( pdgId );
134  int q3 = id.threeCharge();
135  if ( apdgId < PDGCacheMax && pdgId > 0 ) {
136  chargeP_[ apdgId ] = q3;
137  chargeM_[ apdgId ] = -q3;
138  } else if ( apdgId < PDGCacheMax ) {
139  chargeP_[ apdgId ] = -q3;
140  chargeM_[ apdgId ] = q3;
141  } else {
142  chargeMap_[ pdgId ] = q3;
143  chargeMap_[ -pdgId ] = -q3;
144  }
145  }
146  firstEvent_ = false;
147  }
148 
149  barcodes_.clear();
150 
151  size_t totalSize = 0;
152  const GenEvent * mc = 0;
153  std::vector<Handle<HepMCProduct> > heps;
154  MixCollection<HepMCProduct>* cfhepmcprod = 0;
155  size_t npiles = vectorSrc_.size();
156 
157  if(useCF_){
159  evt.getByLabel(InputTag(mixLabel_,"generator"),cf);
160  cfhepmcprod = new MixCollection<HepMCProduct>(cf.product());
161  npiles = cfhepmcprod->size();
162  for(unsigned int icf = 0; icf < npiles; ++icf){
163  totalSize += cfhepmcprod->getObject(icf).GetEvent()->particles_size();
164  }
165  }else if (doSubEvent_){
166  for(size_t i = 0; i < npiles; ++i){
167  // cout<<"Tag "<<vectorSrc_[i]<<endl;
169  heps.push_back(handle);
170  evt.getByLabel( vectorSrc_[i], heps[i] );
171  totalSize += heps[i]->GetEvent()->particles_size();
172  }
173  }else{
175  evt.getByLabel( src_, mcp );
176  mc = mcp->GetEvent();
177  if( mc == 0 )
179  << "HepMC has null pointer to GenEvent" << endl;
180  totalSize = mc->particles_size();
181  }
182 
183  // initialise containers
184  const size_t size = totalSize;
185  vector<const HepMC::GenParticle *> particles( size );
186  auto_ptr<GenParticleCollection> candsPtr( new GenParticleCollection( size ) );
187  // auto_ptr<SubEventMap> subsPtr( new SubEventMap() );
188  auto_ptr<vector<int> > barCodeVector( new vector<int>( size ) );
190  GenParticleCollection & cands = * candsPtr;
191  // SubEventMap & subs = *subsPtr;
192  size_t offset = 0;
193  size_t suboffset = 0;
194 
196  if(doSubEvent_ || useCF_){
197  for(size_t i = 0; i < npiles; ++i){
198  barcodes_.clear();
199  if(useCF_) mc = cfhepmcprod->getObject(i).GetEvent();
200  else mc = heps[i]->GetEvent();
201 
202  //Look whether heavy ion/signal event
203  bool isHI = false;
204  const HepMC::HeavyIon * hi = mc->heavy_ion();
205  if(hi && hi->Ncoll_hard() > 1) isHI = true;
206  size_t num_particles = mc->particles_size();
207  fillIndices(mc, particles, *barCodeVector, offset);
208  // fill output collection and save association
209  for( size_t i = offset; i < offset + num_particles; ++ i ) {
210 
211  const HepMC::GenParticle * part = particles[ i ];
212  reco::GenParticle & cand = cands[ i ];
213  // convert HepMC::GenParticle to new reco::GenParticle
214  convertParticle(cand, part);
215  cand.resetDaughters( ref_.id() );
216  }
217 
218  for( size_t d = offset; d < offset + num_particles; ++ d ) {
219  const HepMC::GenParticle * part = particles[ d ];
220  const GenVertex * productionVertex = part->production_vertex();
221  int sub_id = 0;
222  if ( productionVertex != 0 ) {
223  sub_id = productionVertex->id();
224  if(!isHI) sub_id = 0;
225  // search barcode map and attach daughters
226  fillDaughters(cands,part,d);
227  }else{
228  const GenVertex * endVertex = part->end_vertex();
229  if(endVertex != 0) sub_id = endVertex->id();
230  else throw cms::Exception( "SubEventID" )<<"SubEvent not determined. Particle has no production and no end vertex!"<<endl;
231  }
232  if(sub_id < 0) sub_id = 0;
233  int new_id = sub_id + suboffset;
234  GenParticleRef dref( ref_, d );
235  // subs.insert(dref,new_id); // For SubEventMap
236  cands[d].setCollisionId(new_id); // For new GenParticle
237  LogDebug("VertexId")<<"SubEvent offset 3 : "<<suboffset;
238  }
239  int nsub = -2;
240  if(isHI){
241  nsub = hi->Ncoll_hard()+1;
242  suboffset += nsub;
243  }else{
244  suboffset += 1;
245  }
246  offset += num_particles;
247  }
248  }else{
249  fillIndices(mc, particles, *barCodeVector, 0);
250 
251  // fill output collection and save association
252  for( size_t i = 0; i < particles.size(); ++ i ) {
253  const HepMC::GenParticle * part = particles[ i ];
254  reco::GenParticle & cand = cands[ i ];
255  // convert HepMC::GenParticle to new reco::GenParticle
256  convertParticle(cand, part);
257  cand.resetDaughters( ref_.id() );
258  }
259 
260  // fill references to daughters
261  for( size_t d = 0; d < cands.size(); ++ d ) {
262  const HepMC::GenParticle * part = particles[ d ];
263  const GenVertex * productionVertex = part->production_vertex();
264  // search barcode map and attach daughters
265  if ( productionVertex != 0 ) fillDaughters(cands,part,d);
266  cands[d].setCollisionId(0);
267  }
268  }
269 
270  evt.put( candsPtr );
271  if(saveBarCodes_) evt.put( barCodeVector );
272  // if(doSubEvent_) evt.put(subsPtr); // For SubEventMap
273  if(cfhepmcprod) delete cfhepmcprod;
274 
275 }
276 
278  Candidate::LorentzVector p4( part->momentum() );
279  int pdgId = part->pdg_id();
280  cand.setThreeCharge( chargeTimesThree( pdgId ) );
281  cand.setPdgId( pdgId );
282  cand.setStatus( part->status() );
283  cand.setP4( p4 );
284  cand.setCollisionId(0);
285  const GenVertex * v = part->production_vertex();
286  if ( v != 0 ) {
287  ThreeVector vtx = v->point3d();
288  Candidate::Point vertex( vtx.x() * mmToCm, vtx.y() * mmToCm, vtx.z() * mmToCm );
289  cand.setVertex( vertex );
290  } else {
291  cand.setVertex( Candidate::Point( 0, 0, 0 ) );
292  }
293  return true;
294 }
295 
297 
298  const GenVertex * productionVertex = part->production_vertex();
299  size_t numberOfMothers = productionVertex->particles_in_size();
300  if ( numberOfMothers > 0 ) {
301  GenVertex::particles_in_const_iterator motherIt = productionVertex->particles_in_const_begin();
302  for( ; motherIt != productionVertex->particles_in_const_end(); motherIt++) {
303  const HepMC::GenParticle * mother = * motherIt;
304  size_t m = barcodes_.find( mother->barcode() )->second;
305  cands[ m ].addDaughter( GenParticleRef( ref_, index ) );
306  cands[ index ].addMother( GenParticleRef( ref_, m ) );
307  }
308  }
309 
310  return true;
311 }
312 
313 bool GenParticleProducer::fillIndices(const HepMC::GenEvent * mc, vector<const HepMC::GenParticle*>& particles, vector<int>& barCodeVector, int offset){
314  size_t idx = offset;
315  HepMC::GenEvent::particle_const_iterator begin = mc->particles_begin(), end = mc->particles_end();
316  for( HepMC::GenEvent::particle_const_iterator p = begin; p != end; ++ p ) {
317  const HepMC::GenParticle * particle = * p;
318  size_t barCode_this_event = particle->barcode();
319  size_t barCode = barCode_this_event + offset;
320  if( barcodes_.find(barCode) != barcodes_.end() )
321  throw cms::Exception( "WrongReference" )
322  << "barcodes are duplicated! " << endl;
323  particles[idx] = particle;
324  barCodeVector[idx] = barCode;
325  barcodes_.insert( make_pair(barCode_this_event, idx ++) );
326  }
327  return true;
328 }
329 
330 
332 
334 
#define LogDebug(id)
GenParticleProducer(const edm::ParameterSet &)
constructor
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
T getParameter(std::string const &) const
bool abortOnUnknownPDGCode_
unknown code treatment flag
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
bool fillDaughters(reco::GenParticleCollection &cand, const HepMC::GenParticle *part, size_t index)
edm::InputTag src_
source collection name
void setCollisionId(int s)
Definition: GenParticle.h:38
std::vector< int > chargeP_
charge indices
std::map< int, size_t > barcodes_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::Ref< GenParticleCollection > GenParticleRef
persistent reference to a GenParticle
#define abs(x)
Definition: mlp_lapack.h:159
~GenParticleProducer()
destructor
virtual void produce(edm::Event &e, const edm::EventSetup &) override
process one event
int chargeTimesThree(int) const
int size() const
Definition: MixCollection.h:23
void getData(T &iHolder) const
Definition: EventSetup.h:67
U second(std::pair< T, U > const &p)
reco::GenParticleRefProd ref_
void resetDaughters(const edm::ProductID &id)
set daughters product ID
bool firstEvent_
whether the first event was looked at
virtual void setStatus(int status) GCC11_FINAL
set status word
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
double p4[4]
Definition: TauolaWrapper.h:92
tuple handle
Definition: patZpeak.py:22
double f[11][100]
#define end
Definition: vmac.h:38
unsigned int offset(bool)
virtual void setVertex(const Point &vertex)
set vertex
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
RefProd< PROD > getRefBeforePut()
Definition: Event.h:106
virtual void setThreeCharge(Charge qx3) GCC11_FINAL
set electric charge
bool fillIndices(const HepMC::GenEvent *mc, std::vector< const HepMC::GenParticle * > &particles, std::vector< int > &barCodeVector, int offset)
bool convertParticle(reco::GenParticle &cand, const HepMC::GenParticle *part)
const T & getObject(unsigned int ip) const
Definition: MixCollection.h:30
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:35
static const double mmToCm
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
part
Definition: HCALResponse.h:20
std::map< int, int > chargeMap_
T const * product() const
Definition: Handle.h:74
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:41
bool doSubEvent_
input &amp; output modes
std::vector< int > chargeM_
#define begin
Definition: vmac.h:31
virtual void setP4(const LorentzVector &p4) GCC11_FINAL
set 4-momentum
ProductID id() const
Accessor for product ID.
Definition: RefProd.h:140
math::XYZPoint Point
point in the space
Definition: Candidate.h:45
bool saveBarCodes_
save bar-codes
std::vector< std::string > vectorSrc_
virtual void setPdgId(int pdgId) GCC11_FINAL
tuple size
Write out results.
static const int PDGCacheMax