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.16 2010/05/20 12:42:50 srappocc 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& );
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_;
45 
47  bool firstEvent_;
53  std::vector<int> chargeP_, chargeM_;
54  std::map<int, int> chargeMap_;
55 
58  bool useCF_;
59 
60 };
61 
64 
65 //#include "SimDataFormats/HiGenData/interface/SubEventMap.h"
68 
75 #include <fstream>
76 #include <algorithm>
77 using namespace edm;
78 using namespace reco;
79 using namespace std;
80 using namespace HepMC;
81 
82 static const int PDGCacheMax = 32768;
83 static const double mmToCm = 0.1;
84 
86  firstEvent_(true),
87  abortOnUnknownPDGCode_( cfg.getUntrackedParameter<bool>( "abortOnUnknownPDGCode", true ) ),
88  saveBarCodes_( cfg.getUntrackedParameter<bool>( "saveBarCodes", false ) ),
89  chargeP_( PDGCacheMax, 0 ), chargeM_( PDGCacheMax, 0 ),
90  doSubEvent_(cfg.getUntrackedParameter<bool>( "doSubEvent", false )),
91  useCF_(cfg.getUntrackedParameter<bool>( "useCrossingFrame", false ))
92 {
93  produces<GenParticleCollection>();
94  if( saveBarCodes_ ) {
95  std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
96  produces<vector<int> >().setBranchAlias( alias + "BarCodes" );
97  }
98 
99  if(doSubEvent_){
100  vectorSrc_ = cfg.getParameter<std::vector<std::string> >( "srcVector" );
101  // produces<SubEventMap>();
102  }else if(useCF_) src_ = cfg.getUntrackedParameter<InputTag>( "src" , InputTag("mix","generator"));
103  else src_ = cfg.getParameter<InputTag>( "src" );
104 
105 }
106 
108 }
109 
111  if( std::abs( id ) < PDGCacheMax )
112  return id > 0 ? chargeP_[ id ] : chargeM_[ - id ];
113  map<int, int>::const_iterator f = chargeMap_.find( id );
114  if ( f == chargeMap_.end() ) {
117  << "invalid PDG id: " << id << endl;
118  else
119  return HepPDT::ParticleID(id).threeCharge();
120  }
121  return f->second;
122 }
123 
125 
126  if (firstEvent_) {
128  es.getData( pdt );
129  for( HepPDT::ParticleDataTable::const_iterator p = pdt->begin(); p != pdt->end(); ++ p ) {
130  const HepPDT::ParticleID & id = p->first;
131  int pdgId = id.pid(), apdgId = std::abs( pdgId );
132  int q3 = id.threeCharge();
133  if ( apdgId < PDGCacheMax && pdgId > 0 ) {
134  chargeP_[ apdgId ] = q3;
135  chargeM_[ apdgId ] = -q3;
136  } else if ( apdgId < PDGCacheMax ) {
137  chargeP_[ apdgId ] = -q3;
138  chargeM_[ apdgId ] = q3;
139  } else {
140  chargeMap_[ pdgId ] = q3;
141  chargeMap_[ -pdgId ] = -q3;
142  }
143  }
144  firstEvent_ = false;
145  }
146 
147  barcodes_.clear();
148 
149  size_t totalSize = 0;
150  const GenEvent * mc = 0;
151  std::vector<Handle<HepMCProduct> > heps;
152  MixCollection<HepMCProduct>* cfhepmcprod = 0;
153  size_t npiles = vectorSrc_.size();
154 
155  if(useCF_){
157  evt.getByLabel(InputTag("mix","generator"),cf);
158  cfhepmcprod = new MixCollection<HepMCProduct>(cf.product());
159  npiles = cfhepmcprod->size();
160  for(unsigned int icf = 0; icf < npiles; ++icf){
161  totalSize += cfhepmcprod->getObject(icf).GetEvent()->particles_size();
162  }
163  }else if (doSubEvent_){
164  for(size_t i = 0; i < npiles; ++i){
165  // cout<<"Tag "<<vectorSrc_[i]<<endl;
167  heps.push_back(handle);
168  evt.getByLabel( vectorSrc_[i], heps[i] );
169  totalSize += heps[i]->GetEvent()->particles_size();
170  }
171  }else{
173  evt.getByLabel( src_, mcp );
174  mc = mcp->GetEvent();
175  if( mc == 0 )
177  << "HepMC has null pointer to GenEvent" << endl;
178  totalSize = mc->particles_size();
179  }
180 
181  // initialise containers
182  const size_t size = totalSize;
183  vector<const HepMC::GenParticle *> particles( size );
184  auto_ptr<GenParticleCollection> candsPtr( new GenParticleCollection( size ) );
185  // auto_ptr<SubEventMap> subsPtr( new SubEventMap() );
186  auto_ptr<vector<int> > barCodeVector( new vector<int>( size ) );
188  GenParticleCollection & cands = * candsPtr;
189  // SubEventMap & subs = *subsPtr;
190  size_t offset = 0;
191  size_t suboffset = 0;
192 
194  if(doSubEvent_ || useCF_){
195  for(size_t i = 0; i < npiles; ++i){
196  barcodes_.clear();
197  if(useCF_) mc = cfhepmcprod->getObject(i).GetEvent();
198  else mc = heps[i]->GetEvent();
199 
200  //Look whether heavy ion/signal event
201  bool isHI = false;
202  const HepMC::HeavyIon * hi = mc->heavy_ion();
203  if(hi && hi->Ncoll_hard() > 1) isHI = true;
204  size_t num_particles = mc->particles_size();
205  fillIndices(mc, particles, *barCodeVector, offset);
206  // fill output collection and save association
207  for( size_t i = offset; i < offset + num_particles; ++ i ) {
208 
209  const HepMC::GenParticle * part = particles[ i ];
210  reco::GenParticle & cand = cands[ i ];
211  // convert HepMC::GenParticle to new reco::GenParticle
212  convertParticle(cand, part);
213  cand.resetDaughters( ref_.id() );
214  }
215 
216  for( size_t d = offset; d < offset + num_particles; ++ d ) {
217  const HepMC::GenParticle * part = particles[ d ];
218  const GenVertex * productionVertex = part->production_vertex();
219  int sub_id = 0;
220  if ( productionVertex != 0 ) {
221  sub_id = productionVertex->id();
222  if(!isHI) sub_id = 0;
223  // search barcode map and attach daughters
224  fillDaughters(cands,part,d);
225  }else{
226  const GenVertex * endVertex = part->end_vertex();
227  if(endVertex != 0) sub_id = endVertex->id();
228  else throw cms::Exception( "SubEventID" )<<"SubEvent not determined. Particle has no production and no end vertex!"<<endl;
229  }
230  if(sub_id < 0) sub_id = 0;
231  int new_id = sub_id + suboffset;
232  GenParticleRef dref( ref_, d );
233  // subs.insert(dref,new_id); // For SubEventMap
234  cands[d].setCollisionId(new_id); // For new GenParticle
235  LogDebug("VertexId")<<"SubEvent offset 3 : "<<suboffset;
236  }
237  int nsub = -2;
238  if(isHI){
239  nsub = hi->Ncoll_hard()+1;
240  suboffset += nsub;
241  }else{
242  suboffset += 1;
243  }
244  offset += num_particles;
245  }
246  }else{
247  fillIndices(mc, particles, *barCodeVector, 0);
248 
249  // fill output collection and save association
250  for( size_t i = 0; i < particles.size(); ++ i ) {
251  const HepMC::GenParticle * part = particles[ i ];
252  reco::GenParticle & cand = cands[ i ];
253  // convert HepMC::GenParticle to new reco::GenParticle
254  convertParticle(cand, part);
255  cand.resetDaughters( ref_.id() );
256  }
257 
258  // fill references to daughters
259  for( size_t d = 0; d < cands.size(); ++ d ) {
260  const HepMC::GenParticle * part = particles[ d ];
261  const GenVertex * productionVertex = part->production_vertex();
262  // search barcode map and attach daughters
263  if ( productionVertex != 0 ) fillDaughters(cands,part,d);
264  cands[d].setCollisionId(0);
265  }
266  }
267 
268  evt.put( candsPtr );
269  if(saveBarCodes_) evt.put( barCodeVector );
270  // if(doSubEvent_) evt.put(subsPtr); // For SubEventMap
271  if(cfhepmcprod) delete cfhepmcprod;
272 
273 }
274 
276  Candidate::LorentzVector p4( part->momentum() );
277  int pdgId = part->pdg_id();
278  cand.setThreeCharge( chargeTimesThree( pdgId ) );
279  cand.setPdgId( pdgId );
280  cand.setStatus( part->status() );
281  cand.setP4( p4 );
282  cand.setCollisionId(0);
283  const GenVertex * v = part->production_vertex();
284  if ( v != 0 ) {
285  ThreeVector vtx = v->point3d();
286  Candidate::Point vertex( vtx.x() * mmToCm, vtx.y() * mmToCm, vtx.z() * mmToCm );
287  cand.setVertex( vertex );
288  } else {
289  cand.setVertex( Candidate::Point( 0, 0, 0 ) );
290  }
291  return true;
292 }
293 
295 
296  const GenVertex * productionVertex = part->production_vertex();
297  size_t numberOfMothers = productionVertex->particles_in_size();
298  if ( numberOfMothers > 0 ) {
299  GenVertex::particles_in_const_iterator motherIt = productionVertex->particles_in_const_begin();
300  for( ; motherIt != productionVertex->particles_in_const_end(); motherIt++) {
301  const HepMC::GenParticle * mother = * motherIt;
302  size_t m = barcodes_.find( mother->barcode() )->second;
303  cands[ m ].addDaughter( GenParticleRef( ref_, index ) );
304  cands[ index ].addMother( GenParticleRef( ref_, m ) );
305  }
306  }
307 
308  return true;
309 }
310 
311 bool GenParticleProducer::fillIndices(const HepMC::GenEvent * mc, vector<const HepMC::GenParticle*>& particles, vector<int>& barCodeVector, int offset){
312  size_t idx = offset;
313  HepMC::GenEvent::particle_const_iterator begin = mc->particles_begin(), end = mc->particles_end();
314  for( HepMC::GenEvent::particle_const_iterator p = begin; p != end; ++ p ) {
315  const HepMC::GenParticle * particle = * p;
316  size_t barCode_this_event = particle->barcode();
317  size_t barCode = barCode_this_event + offset;
318  if( barcodes_.find(barCode) != barcodes_.end() )
319  throw cms::Exception( "WrongReference" )
320  << "barcodes are duplicated! " << endl;
321  particles[idx] = particle;
322  barCodeVector[idx] = barCode;
323  barcodes_.insert( make_pair(barCode_this_event, idx ++) );
324  }
325  return true;
326 }
327 
328 
330 
332 
#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_
virtual void setStatus(int status)
set status word
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
#define abs(x)
Definition: mlp_lapack.h:159
virtual void setP4(const LorentzVector &p4)
set 4-momentum
~GenParticleProducer()
destructor
int chargeTimesThree(int) const
edm::Ref< edm::HepMCProduct, HepMC::GenParticle > GenParticleRef
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_
virtual void produce(edm::Event &e, const edm::EventSetup &)
process one event
void resetDaughters(const edm::ProductID &id)
set daughters product ID
bool firstEvent_
whether the first event was looked at
virtual void setThreeCharge(Charge qx3)
set electric charge
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
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:359
RefProd< PROD > getRefBeforePut()
Definition: Event.h:96
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:36
static const double mmToCm
part
Definition: HCALResponse.h:21
std::map< int, int > chargeMap_
T const * product() const
Definition: Handle.h:74
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Candidate.h:39
bool doSubEvent_
input &amp; output modes
std::vector< int > chargeM_
#define begin
Definition: vmac.h:31
ProductID id() const
Accessor for product ID.
Definition: RefProd.h:141
virtual void setPdgId(int pdgId)
math::XYZPoint Point
point in the space
Definition: Candidate.h:43
bool saveBarCodes_
save bar-codes
std::vector< std::string > vectorSrc_
tuple size
Write out results.
mathSSE::Vec4< T > v
static const int PDGCacheMax