CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EvtGenInterface.cc
Go to the documentation of this file.
1 
3 
5 
8 
10 
17 #include "CLHEP/Random/Random.h"
18 
21 
22 #include "HepMC/GenEvent.h"
23 // #include "HepMC/PythiaWrapper6_2.h"
24 
25 //#define PYGIVE pygive_
26 //extern "C" {
27 // void PYGIVE(const char*,int length);
28 //}
29 
30 using namespace gen;
31 using namespace edm;
32 
34 {
35 
36  ntotal = 0;
37  nevent = 0;
38  std::cout << " EvtGenProducer starting ... " << std::endl;
39 
40  // create random engine and initialize seed using Random Number
41  // Generator Service
42  // as configured in the configuration file
43 
45 
46  if ( ! rngen.isAvailable()) {
47  throw cms::Exception("Configuration")
48  << "The EvtGenProducer module requires the RandomNumberGeneratorService\n"
49  "which is not present in the configuration file. You must add the service\n"
50  "in the configuration file if you want to run EvtGenProducer";
51  }
52 
53  CLHEP::HepRandomEngine& m_engine = rngen->getEngine();
54  m_flat = new CLHEP::RandFlat(m_engine, 0., 1.);
55  myEvtRandomEngine* the_engine = new myEvtRandomEngine(&m_engine);
56 
57  // Get data from parameter set
58  edm::FileInPath decay_table = pset.getParameter<edm::FileInPath>("decay_table");
59  edm::FileInPath pdt = pset.getParameter<edm::FileInPath>("particle_property_file");
60  bool useDefault = pset.getUntrackedParameter<bool>("use_default_decay",true);
61  usePythia = pset.getUntrackedParameter<bool>("use_internal_pythia",true);
62 
63  edm::FileInPath user_decay = pset.getParameter<edm::FileInPath>("user_decay_file");
64  std::string decay_table_s = decay_table.fullPath();
65  std::string pdt_s = pdt.fullPath();
66  std::string user_decay_s = user_decay.fullPath();
67 
68  //-->pythia_params = pset.getParameter< std::vector<std::string> >("processParameters");
69 
70 
71  // any number of alias names for forced decays can be specified using dynamic std vector of strings
72  std::vector<std::string> forced_names = pset.getParameter< std::vector<std::string> >("list_forced_decays");
73 
74  m_EvtGen = new EvtGen (decay_table_s.c_str(),pdt_s.c_str(),the_engine);
75  // 4th parameter should be rad cor - set to PHOTOS (default)
76 
77  if (!useDefault) m_EvtGen->readUDecay( user_decay_s.c_str() );
78 
79  std::vector<std::string>::const_iterator i;
80  nforced=0;
81 
82  for (i=forced_names.begin(); i!=forced_names.end(); ++i)
83  // i will point to strings containing
84  // names of particles with forced decay
85  {
86  nforced++;
87  EvtId found = EvtPDL::getId(*i);
88  if (found.getId()==-1)
89  {
90  throw cms::Exception("Configuration")
91  << "name in part list for forced decays not found: " << *i;
92  }
93  if (found.getId()==found.getAlias())
94  {
95  throw cms::Exception("Configuration")
96  << "name in part list for forced decays is not an alias: " << *i;
97  }
98  forced_Evt.push_back(found); // forced_Evt is the list of EvtId's
99  forced_Hep.push_back(EvtPDL::getStdHep(found)); // forced_Hep is the list of stdhep codes
100  }
101 
102  // fill up default list of particles to be declared stable in the "master generator"
103  // these are assumed to be PDG ID's
104  // in case of combo with Pythia6, translation is done in Pythia6Hadronizer
105  //
106  // Note: Pythia6's kc=43, 44, and 84 commented out because they're obsolete (per S.Mrenna)
107  //
108  m_PDGs.push_back( 300553 ) ;
109  m_PDGs.push_back( 511 ) ;
110  m_PDGs.push_back( 521 ) ;
111  m_PDGs.push_back( 523 ) ;
112  m_PDGs.push_back( 513 ) ;
113  m_PDGs.push_back( 533 ) ;
114  m_PDGs.push_back( 531 ) ;
115 
116  m_PDGs.push_back( 15 ) ;
117 
118  m_PDGs.push_back( 413 ) ;
119  m_PDGs.push_back( 423 ) ;
120  m_PDGs.push_back( 433 ) ;
121  m_PDGs.push_back( 411 ) ;
122  m_PDGs.push_back( 421 ) ;
123  m_PDGs.push_back( 431 ) ;
124  m_PDGs.push_back( 10411 );
125  m_PDGs.push_back( 10421 );
126  m_PDGs.push_back( 10413 );
127  m_PDGs.push_back( 10423 );
128  m_PDGs.push_back( 20413 );
129  m_PDGs.push_back( 20423 );
130 
131  m_PDGs.push_back( 415 );
132  m_PDGs.push_back( 425 );
133  m_PDGs.push_back( 10431 );
134  m_PDGs.push_back( 20433 );
135  m_PDGs.push_back( 10433 );
136  m_PDGs.push_back( 435 );
137 
138  m_PDGs.push_back( 310 );
139  m_PDGs.push_back( 311 );
140  m_PDGs.push_back( 313 );
141  m_PDGs.push_back( 323 );
142  m_PDGs.push_back( 10321 );
143  m_PDGs.push_back( 10311 );
144  m_PDGs.push_back( 10313 );
145  m_PDGs.push_back( 10323 );
146  m_PDGs.push_back( 20323 );
147  m_PDGs.push_back( 20313 );
148  m_PDGs.push_back( 325 );
149  m_PDGs.push_back( 315 );
150 
151  m_PDGs.push_back( 100313 );
152  m_PDGs.push_back( 100323 );
153  m_PDGs.push_back( 30313 );
154  m_PDGs.push_back( 30323 );
155  m_PDGs.push_back( 30343 );
156  m_PDGs.push_back( 30353 );
157  m_PDGs.push_back( 30363 );
158 
159  m_PDGs.push_back( 111 );
160  m_PDGs.push_back( 221 );
161  m_PDGs.push_back( 113 );
162  m_PDGs.push_back( 213 );
163  m_PDGs.push_back( 223 );
164  m_PDGs.push_back( 331 );
165  m_PDGs.push_back( 333 );
166  m_PDGs.push_back( 20213 );
167  m_PDGs.push_back( 20113 );
168  m_PDGs.push_back( 215 );
169  m_PDGs.push_back( 115 );
170  m_PDGs.push_back( 10213 );
171  m_PDGs.push_back( 10113 );
172  m_PDGs.push_back( 9000111 ); // PDG ID = 9000111, Pythia6 ID = 10111
173  m_PDGs.push_back( 9000211 ); // PDG ID = 9000211, Pythia6 ID = 10211
174  m_PDGs.push_back( 9010221 ); // PDG ID = 9010211, Pythia6 ID = ???
175  m_PDGs.push_back( 10221 );
176  m_PDGs.push_back( 20223 );
177  m_PDGs.push_back( 20333 );
178  m_PDGs.push_back( 225 );
179  m_PDGs.push_back( 9020221 ); // PDG ID = 9020211, Pythia6 ID = ???
180  m_PDGs.push_back( 335 );
181  m_PDGs.push_back( 10223 );
182  m_PDGs.push_back( 10333 );
183  m_PDGs.push_back( 100213 );
184  m_PDGs.push_back( 100113 );
185 
186  m_PDGs.push_back( 441 );
187  m_PDGs.push_back( 100441 );
188  m_PDGs.push_back( 443 );
189  m_PDGs.push_back( 100443 );
190  m_PDGs.push_back( 9000443 );
191  m_PDGs.push_back( 9010443 );
192  m_PDGs.push_back( 9020443 );
193  m_PDGs.push_back( 10441 );
194  m_PDGs.push_back( 20443 );
195  m_PDGs.push_back( 445 );
196 
197  m_PDGs.push_back( 30443 );
198  m_PDGs.push_back( 551 );
199  m_PDGs.push_back( 553 );
200  m_PDGs.push_back( 100553 );
201  m_PDGs.push_back( 200553 );
202  m_PDGs.push_back( 10551 );
203  m_PDGs.push_back( 20553 );
204  m_PDGs.push_back( 555 );
205  m_PDGs.push_back( 10553 );
206 
207  m_PDGs.push_back( 110551 );
208  m_PDGs.push_back( 120553 );
209  m_PDGs.push_back( 100555 );
210  m_PDGs.push_back( 210551 );
211  m_PDGs.push_back( 220553 );
212  m_PDGs.push_back( 200555 );
213  m_PDGs.push_back( 30553 );
214  m_PDGs.push_back( 20555 );
215 
216  m_PDGs.push_back( 557 );
217  m_PDGs.push_back( 130553 );
218  m_PDGs.push_back( 120555 );
219  m_PDGs.push_back( 100557 );
220  m_PDGs.push_back( 110553 );
221  m_PDGs.push_back( 210553 );
222  m_PDGs.push_back( 10555 );
223  m_PDGs.push_back( 110555 );
224 
225  m_PDGs.push_back( 4122 );
226  m_PDGs.push_back( 4132 );
227  // m_PDGs.push_back( 84 ); // obsolete
228  m_PDGs.push_back( 4112 );
229  m_PDGs.push_back( 4212 );
230  m_PDGs.push_back( 4232 );
231  m_PDGs.push_back( 4222 );
232  m_PDGs.push_back( 4322 );
233  m_PDGs.push_back( 4312 );
234 
235  m_PDGs.push_back( 13122 );
236  m_PDGs.push_back( 13124 );
237  m_PDGs.push_back( 23122 );
238  m_PDGs.push_back( 33122 );
239  m_PDGs.push_back( 43122 );
240  m_PDGs.push_back( 53122 );
241  m_PDGs.push_back( 13126 );
242  m_PDGs.push_back( 13212 );
243  m_PDGs.push_back( 13241 );
244 
245  m_PDGs.push_back( 3126 );
246  m_PDGs.push_back( 3124 );
247  m_PDGs.push_back( 3122 );
248  m_PDGs.push_back( 3222 );
249  m_PDGs.push_back( 2214 );
250  m_PDGs.push_back( 2224 );
251  m_PDGs.push_back( 3324 );
252  m_PDGs.push_back( 2114 );
253  m_PDGs.push_back( 1114 );
254  m_PDGs.push_back( 3112 );
255  m_PDGs.push_back( 3212 );
256  m_PDGs.push_back( 3114 );
257  m_PDGs.push_back( 3224 );
258  m_PDGs.push_back( 3214 );
259  m_PDGs.push_back( 3216 );
260  m_PDGs.push_back( 3322 );
261  m_PDGs.push_back( 3312 );
262  m_PDGs.push_back( 3314 );
263  m_PDGs.push_back( 3334 );
264 
265  m_PDGs.push_back( 4114 );
266  m_PDGs.push_back( 4214 );
267  m_PDGs.push_back( 4224 );
268  m_PDGs.push_back( 4314 );
269  m_PDGs.push_back( 4324 );
270  m_PDGs.push_back( 4332 );
271  m_PDGs.push_back( 4334 );
272  //m_PDGs.push_back( 43 ); // obsolete (?)
273  //m_PDGs.push_back( 44 ); // obsolete (?)
274  m_PDGs.push_back( 10443 );
275 
276  m_PDGs.push_back( 5122 );
277  m_PDGs.push_back( 5132 );
278  m_PDGs.push_back( 5232 );
279  m_PDGs.push_back( 5332 );
280  m_PDGs.push_back( 5222 );
281  m_PDGs.push_back( 5112 );
282  m_PDGs.push_back( 5212 );
283  m_PDGs.push_back( 541 );
284  m_PDGs.push_back( 14122 );
285  m_PDGs.push_back( 14124 );
286  m_PDGs.push_back( 5312 );
287  m_PDGs.push_back( 5322 );
288  m_PDGs.push_back( 10521 );
289  m_PDGs.push_back( 20523 );
290  m_PDGs.push_back( 10523 );
291 
292  m_PDGs.push_back( 525 );
293  m_PDGs.push_back( 10511 );
294  m_PDGs.push_back( 20513 );
295  m_PDGs.push_back( 10513 );
296  m_PDGs.push_back( 515 );
297  m_PDGs.push_back( 10531 );
298  m_PDGs.push_back( 20533 );
299  m_PDGs.push_back( 10533 );
300  m_PDGs.push_back( 535 );
301  m_PDGs.push_back( 543 );
302  m_PDGs.push_back( 545 );
303  m_PDGs.push_back( 5114 );
304  m_PDGs.push_back( 5224 );
305  m_PDGs.push_back( 5214 );
306  m_PDGs.push_back( 5314 );
307  m_PDGs.push_back( 5324 );
308  m_PDGs.push_back( 5334 );
309  m_PDGs.push_back( 10541 );
310  m_PDGs.push_back( 10543 );
311  m_PDGs.push_back( 20543 );
312 
313  m_PDGs.push_back( 4424 );
314  m_PDGs.push_back( 4422 );
315  m_PDGs.push_back( 4414 );
316  m_PDGs.push_back( 4412 );
317  m_PDGs.push_back( 4432 );
318  m_PDGs.push_back( 4434 );
319 
320  m_PDGs.push_back( 130 );
321 
322  // now check if we need to override default list of particles/IDs
323  if ( pset.exists("operates_on_particles") )
324  {
325  std::vector<int> tmpPIDs = pset.getParameter< std::vector<int> >("operates_on_particles");
326  if ( tmpPIDs.size() > 0 )
327  {
328  if ( tmpPIDs[0] > 0 ) // 0 means default !!!
329  {
330  m_PDGs.clear();
331  m_PDGs = tmpPIDs;
332  }
333  }
334  }
335 
336  m_Py6Service = new Pythia6Service;
337 }
338 
340 {
341  std::cout << " EvtGenProducer terminating ... " << std::endl;
342  delete m_Py6Service;
343 }
344 
346 {
347 
348  Pythia6Service::InstanceWrapper guard(m_Py6Service); // grab Py6 instance
349 
350  // Do here initialization of EvtPythia then restore original settings
351  if (usePythia) EvtPythia::pythiaInit(0);
352 
353 // no need - will be done via Pythia6Hadronizer::declareStableParticles
354 //
355 // for( std::vector<std::string>::const_iterator itPar = pythia_params.begin(); itPar != pythia_params.end(); ++itPar ) {
356 // call_pygive(*itPar);
357 // }
358 
359  return ;
360 
361 }
362 
363 
364 HepMC::GenEvent* EvtGenInterface::decay( HepMC::GenEvent* evt )
365 {
366  Pythia6Service::InstanceWrapper guard(m_Py6Service); // grab Py6 instance
367 
368  nevent++;
369  npartial = 0;
370  // std::cout << "nevent = " << nevent << std::endl ;
371 
372  int idHep,ipart,status;
373  EvtId idEvt;
374 
375  nPythia = evt->particles_size();
376  // FIX A MEMORY LEAK (RC)
377  // HepMC::GenEvent* newEvt = new HepMC::GenEvent( *evt );
378 
379  // First pass through undecayed Pythia particles to decay particles known to EvtGen left stable by Pythia
380  // except candidates to be forced which will be searched later to include EvtGen decay products
381  nlist = 0;
382 
383  // Notice dynamical use of evt
384  for (HepMC::GenEvent::particle_const_iterator p= evt->particles_begin(); p != evt->particles_end(); ++p)
385  {
386  status = (*p)->status();
387 
388  if(status==1) { // only not decayed (status = 1) particles
389 
390 
391  idHep = (*p)->pdg_id();
392  int do_force=0;
393  for(int i=0;i<nforced;i++) // First check if part with forced decay
394  { // In that case do not decay immediately
395  if(idHep == forced_Hep[i]) // (only 1 per event will be forced)
396  { // Fill list
397  update_candlist(i,*p);
398  do_force=1;
399  }
400  }
401  if(do_force==0) // particles with decays not forced are decayed immediately
402  {
403  idEvt = EvtPDL::evtIdFromStdHep(idHep);
404  ipart = idEvt.getId();
405  if (ipart==-1) continue; // particle not known to EvtGen
406  if (EvtDecayTable::getNMode(ipart)==0) continue; // particles stable for EvtGen
407  addToHepMC(*p,idEvt,evt,true); // generate decay
408  }
409  }
410  }
411 
412  if(nlist!=0)
413  {
414  // decide randomly which one to decay as alias
415  int which = (int)(nlist*m_flat->fire());
416  if (which == nlist) which = nlist-1;
417 
418  for(int k=0;k < nlist; k++)
419  {
420  if(k == which || !usePythia) {
421  addToHepMC(listp[k],forced_Evt[index[k]],evt,false); // decay as alias
422  }
423  else
424  {
425  int id_non_alias = forced_Evt[index[k]].getId();
426  EvtId non_alias(id_non_alias,id_non_alias); // create new EvtId with id = alias
427  addToHepMC(listp[k],non_alias,evt,false); // decay as standard (non alias)
428  }
429  }
430  }
431 
432  return evt;
433 
434 }
435 
436 void EvtGenInterface::addToHepMC(HepMC::GenParticle* partHep, EvtId idEvt, HepMC::GenEvent* theEvent, bool del_daug )
437 {
438  // Set spin type
439  EvtSpinType::spintype stype = EvtPDL::getSpinType(idEvt);
440  EvtParticle* partEvt;
441  switch (stype){
442  case EvtSpinType::SCALAR:
443  partEvt = new EvtScalarParticle();
444  break;
445  case EvtSpinType::STRING:
446  partEvt = new EvtStringParticle();
447  break;
448  case EvtSpinType::DIRAC:
449  partEvt = new EvtDiracParticle();
450  break;
451  case EvtSpinType::VECTOR:
452  partEvt = new EvtVectorParticle();
453  break;
454  case EvtSpinType::RARITASCHWINGER:
455  partEvt = new EvtRaritaSchwingerParticle();
456  break;
457  case EvtSpinType::TENSOR:
458  partEvt = new EvtTensorParticle();
459  break;
460  case EvtSpinType::SPIN5HALF: case EvtSpinType::SPIN3: case EvtSpinType::SPIN7HALF: case EvtSpinType::SPIN4:
461  partEvt = new EvtHighSpinParticle();
462  break;
463  default:
464  std::cout << "Unknown spintype in EvtSpinType!" << std::endl;
465  return;
466  }
467 
468  // Generate decay
469  EvtVector4R momEvt;
470  HepMC::FourVector momHep = partHep->momentum();
471  momEvt.set(momHep.t(),momHep.x(),momHep.y(),momHep.z());
472  EvtVector4R posEvt;
473  HepMC::GenVertex* initVert = partHep->production_vertex();
474  HepMC::FourVector posHep = initVert->position();
475  posEvt.set(posHep.t(),posHep.x(),posHep.y(),posHep.z());
476  partEvt->init(idEvt,momEvt);
477  partEvt->setDiagonalSpinDensity();
478  partEvt->decay();
479 
480  // extend the search of candidates to be forced to EvtGen decay products and delete their daughters **
481  // otherwise they wouldn't get their chance to take part in the forced decay lottery **
482  if (del_daug) go_through_daughters(partEvt); // recursive function go_through_daughters will do **
483 
484  // Change particle in stdHEP format
485  static EvtStdHep evtstdhep;
486 
487  evtstdhep.init();
488  partEvt->makeStdHep(evtstdhep);
489 
490  /* if (ntotal < 1000 && ntotal%10 == 0) { // DEBUG
491  // if (evtstdhep.getStdHepID(0) == 521 || evtstdhep.getStdHepID(0) == 523) { // DEBUG
492 
493  partEvt->printParticle();
494  partEvt->printTree();
495  std::cout << evtstdhep << "\n" <<
496  "--------------------------------------------" << std::endl;
497  } */
498 
499  ntotal++;
500  partEvt->deleteTree();
501 
502  // ********* Now add to the HepMC Event **********
503 
504  // Then loop on evtstdhep to add vertexes...
505  HepMC::GenVertex* theVerts[200];
506  for (int ivert = 0; ivert < 200; ivert++) {
507  theVerts[ivert] = 0;
508  }
509 
510  for (int ipart = 0; ipart < evtstdhep.getNPart(); ipart++) {
511  int theMum = evtstdhep.getFirstMother(ipart);
512  if (theMum != -1 && !theVerts[theMum]) {
513  EvtVector4R theVpos = evtstdhep.getX4(ipart) + posEvt;
514  theVerts[theMum] =
515  new HepMC::GenVertex(HepMC::FourVector(theVpos.get(1),
516  theVpos.get(2),
517  theVpos.get(3),
518  theVpos.get(0)),0);
519  }
520  }
521 
522  // ...then particles
523  partHep->set_status(2);
524  if (theVerts[0]) theVerts[0]->add_particle_in( partHep );
525 
526  for (int ipart2 = 1; ipart2 < evtstdhep.getNPart(); ipart2++) {
527  int idHep = evtstdhep.getStdHepID(ipart2);
528  HepMC::GenParticle* thePart =
529  new HepMC::GenParticle( HepMC::FourVector(evtstdhep.getP4(ipart2).get(1),
530  evtstdhep.getP4(ipart2).get(2),
531  evtstdhep.getP4(ipart2).get(3),
532  evtstdhep.getP4(ipart2).get(0)),
533  idHep,
534  evtstdhep.getIStat(ipart2));
535  npartial++;
536  thePart->suggest_barcode(npartial + nPythia);
537  int theMum2 = evtstdhep.getFirstMother(ipart2);
538  if (theMum2 != -1 && theVerts[theMum2]) theVerts[theMum2]->add_particle_out( thePart );
539  if (theVerts[ipart2]) theVerts[ipart2]->add_particle_in( thePart );
540 
541  }
542 
543  for (int ipart3 = 0; ipart3 < evtstdhep.getNPart(); ipart3++) {
544  if (theVerts[ipart3]) theEvent->add_vertex( theVerts[ipart3] );
545  }
546 
547 }
548 
549 /*
550 void
551 EvtGenInterface::call_pygive(const std::string& iParm ) {
552 
553  //call the fortran routine pygive with a fortran string
554  PYGIVE( iParm.c_str(), iParm.length() );
555 
556 }
557 */
558 
559 void
561 
562  int NDaug=part->getNDaug();
563  if(NDaug)
564  {
565  EvtParticle* Daughter;
566  for (int i=0;i<NDaug;i++)
567  {
568  Daughter=part->getDaug(i);
569  int idHep = EvtPDL::getStdHep(Daughter->getId());
570  int found=0;
571  for(int k=0;k<nforced;k++)
572  {
573  if(idHep == forced_Hep[k])
574  {
575  found = 1;
576  Daughter->deleteDaughters();
577  }
578  }
579  if (!found) go_through_daughters(Daughter);
580  }
581  }
582 }
583 
584 void
586 {
587  if(nlist<10) // not nice ... but is 10 a reasonable maximum?
588  {
589  bool isThere = false;
590  if (nlist) {
591  for (int check=0; check < nlist; check++) {
592  if (listp[check]->barcode() == thePart->barcode()) isThere = true;
593  }
594  }
595  if (!isThere) {
596  listp[nlist] = thePart;
597  index[nlist++] = theIndex;
598  }
599  }
600  else
601  {
602  throw cms::Exception("runtime")
603  << "more than 10 candidates to be forced ";
604  return;
605  }
606  return;
607 }
608 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
HepMC::GenEvent * decay(HepMC::GenEvent *)
void addToHepMC(HepMC::GenParticle *partHep, EvtId idEvt, HepMC::GenEvent *theEvent, bool del_daug)
void update_candlist(int theIndex, HepMC::GenParticle *thePart)
static unsigned int getId(void)
bool exists(std::string const &parameterName) const
checks if a parameter exists
void go_through_daughters(EvtParticle *part)
return((rh^lh)&mask)
int nevent
Definition: AMPTWrapper.h:74
double p[5][pyjets_maxn]
bool check(const DataFrame &df, bool capcheck, bool dvercheck)
bool isAvailable() const
Definition: Service.h:47
virtual CLHEP::HepRandomEngine & getEngine() const =0
Use this to get the random number engine, this is the only function most users should call...
int k[5][pyjets_maxn]
EvtGenInterface(const edm::ParameterSet &)
part
Definition: HCALResponse.h:21
tuple cout
Definition: gather_cfg.py:41
std::string fullPath() const
Definition: FileInPath.cc:170
tuple status
Definition: ntuplemaker.py:245