CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Herwig6Hadronizer.cc
Go to the documentation of this file.
1 #include <cstring>
2 #include <sstream>
3 #include <string>
4 #include <vector>
5 #include <memory>
6 #include <map>
7 #include <set>
8 #include <boost/shared_ptr.hpp>
9 #include <boost/algorithm/string/classification.hpp>
10 #include <boost/algorithm/string/split.hpp>
11 #include <HepMC/GenEvent.h>
12 #include <HepMC/GenParticle.h>
13 #include <HepMC/GenVertex.h>
14 #include <HepMC/PdfInfo.h>
15 #include <HepMC/HerwigWrapper.h>
16 #include <HepMC/HEPEVT_Wrapper.h>
17 #include <HepMC/IO_HERWIG.h>
18 #include "HepPID/ParticleIDTranslations.hh"
19 
21 
24 
27 
32 
34 
37 
39 
40 extern "C" {
41  void hwuidt_(int *iopt, int *ipdg, int *iwig, char nwig[8]);
42  double hwualf_(int *mode, double* scale);
43  double hwuaem_(double* scale);
44 }
45 
46 // helpers
47 namespace {
48  static inline bool call_hwmatch()
49  {
50  int result;
51  hwmatch(&result);
52  return result;
53  }
54  static inline bool call_hwmsct()
55  {
56  int result;
57  hwmsct(&result);
58  return result;
59  }
60 
61  static int pdgToHerwig(int ipdg, char *nwig)
62  {
63  int iopt = 1;
64  int iwig = 0;
65  hwuidt_(&iopt, &ipdg, &iwig, nwig);
66  return ipdg ? iwig : 0;
67  }
68 
69  static bool markStable(int pdgId)
70  {
71  char nwig[9] = " ";
72  if (!pdgToHerwig(pdgId, nwig))
73  return false;
74  hwusta(nwig, 1);
75  return true;
76  }
77 }
78 
80  public gen::Herwig6Instance {
81  public:
82  Herwig6Hadronizer(const edm::ParameterSet &params);
84 
85  void setSLHAFromHeader(const std::vector<std::string> &lines);
86  bool initialize(const lhef::HEPRUP *heprup);
87 
88  bool readSettings( int );
89 
90  // bool initializeForInternalPartons() { return initialize(0); }
91  // bool initializeForExternalPartons() { return initialize(lheRunInfo()->getHEPRUP()); }
92 
95 
96  bool declareStableParticles( const std::vector<int> &pdgIds);
97  bool declareSpecialSettings( const std::vector<std::string> );
98 
99  void statistics();
100 
101 
103  bool hadronize();
104  bool decay();
105  bool residualDecay();
106  void finalizeEvent();
107 
108  const char *classname() const { return "Herwig6Hadronizer"; }
109 
110  private:
111  void clear();
112 
113  int pythiaStatusCode(const HepMC::GenParticle *p) const;
114  void pythiaStatusCodes();
115 
116  virtual void upInit();
117  virtual void upEvnt();
118 
119  HepMC::IO_HERWIG conv;
120  bool needClear;
122 
129  double comEnergy;
130  bool useJimmy;
133  bool fConvertToPDG; // convert PIDs
136  int nMatch;
138 
140 
141  // -------------------------------------------------------------------------------
142  std::string particleSpecFileName; //Lars 20/Jul/2011
144  // -------------------------------------------------------------------------------
145 
146 };
147 
148 extern "C" {
149  void hwwarn_(const char *method, int *id);
150  void setherwpdf_(void);
151  void mysetpdfpath_(const char *path);
152 } // extern "C"
153 
155  BaseHadronizer(params),
156  needClear(false),
157  parameters(params.getParameter<edm::ParameterSet>("HerwigParameters")),
158  herwigVerbosity(params.getUntrackedParameter<int>("herwigVerbosity", 0)),
159  hepmcVerbosity(params.getUntrackedParameter<int>("hepmcVerbosity", 0)),
160  maxEventsToPrint(params.getUntrackedParameter<int>("maxEventsToPrint", 0)),
161  printCards(params.getUntrackedParameter<bool>("printCards", false)),
162  emulatePythiaStatusCodes(params.getUntrackedParameter<bool>("emulatePythiaStatusCodes", false)),
163  comEnergy(params.getParameter<double>("comEnergy")),
164  useJimmy(params.getParameter<bool>("useJimmy")),
165  doMPInteraction(params.getParameter<bool>("doMPInteraction")),
166  numTrials(params.getUntrackedParameter<int>("numTrialsMPI", 100)),
167  doMatching(params.getUntrackedParameter<bool>("doMatching", false)),
168  inclusiveMatching(params.getUntrackedParameter<bool>("inclusiveMatching", true)),
169  nMatch(params.getUntrackedParameter<int>("nMatch", 0)),
170  matchingScale(params.getUntrackedParameter<double>("matchingScale", 0.0)),
171  readMCatNLOfile(false),
172 
173  // added to be able to read external particle spectrum file
174  particleSpecFileName(params.getUntrackedParameter<std::string>("ParticleSpectrumFileName","")),
175  readParticleSpecFile(params.getUntrackedParameter<bool>("readParticleSpecFile", false))
176 
177 {
178 
179  fConvertToPDG = false;
180  if ( params.exists( "doPDGConvert" ) )
181  fConvertToPDG = params.getParameter<bool>("doPDGConvert");
182 }
183 
185 {
186  clear();
187 }
188 
190 {
191  if (!needClear)
192  return;
193 
194  // teminate elementary process
195  call(hwefin);
196  if (useJimmy)
197  call(jmefin);
198 
199  needClear = false;
200 }
201 
203  const std::vector<std::string> &lines)
204 {
205  std::set<std::string> blocks;
206  std::string block;
207  for(std::vector<std::string>::const_iterator iter = lines.begin();
208  iter != lines.end(); ++iter) {
209  std::string line = *iter;
210  std::transform(line.begin(), line.end(),
211  line.begin(), (int(*)(int))std::toupper);
212  std::string::size_type pos = line.find('#');
213  if (pos != std::string::npos)
214  line.resize(pos);
215 
216  if (line.empty())
217  continue;
218 
219  if (!boost::algorithm::is_space()(line[0])) {
220  std::vector<std::string> tokens;
221  boost::split(tokens, line,
222  boost::algorithm::is_space(),
223  boost::token_compress_on);
224  if (!tokens.size())
225  continue;
226  block.clear();
227  if (tokens.size() < 2)
228  continue;
229  if (tokens[0] == "BLOCK")
230  block = tokens[1];
231  else if (tokens[0] == "DECAY")
232  block = "DECAY";
233 
234  if (block.empty())
235  continue;
236 
237  if (!blocks.count(block)) {
238  blocks.insert(block);
239  edm::LogWarning("Generator|Herwig6Hadronzier")
240  << "Unsupported SLHA block \"" << block
241  << "\". It will be ignored."
242  << std::endl;
243  }
244  }
245  }
246 }
247 
249 {
250 
251  clear();
252  const lhef::HEPRUP* heprup = lheRunInfo()->getHEPRUP();
253  externalPartons = ( heprup != 0 );
254 
255  if ( key == 0 && externalPartons ) return false;
256  if ( key > 0 && !externalPartons ) return false;
257 
258  std::ostringstream info;
259  info << "---------------------------------------------------\n";
260  info << "Taking in settinsg for Herwig6Hadronizer for "
261  << (externalPartons ? "external" : "internal") << " partons\n";
262  info << "---------------------------------------------------\n";
263 
264  info << " Herwig verbosity level = " << herwigVerbosity << "\n";
265  info << " HepMC verbosity = " << hepmcVerbosity << "\n";
266  info << " Number of events to be printed = " << maxEventsToPrint << "\n";
267 
268  // Call hwudat to set up HERWIG block data
269  hwudat();
270 
271  // Setting basic parameters
272  if (externalPartons) {
273  hwproc.PBEAM1 = heprup->EBMUP.first;
274  hwproc.PBEAM2 = heprup->EBMUP.second;
275  pdgToHerwig(heprup->IDBMUP.first, hwbmch.PART1);
276  pdgToHerwig(heprup->IDBMUP.second, hwbmch.PART2);
277  }
278  else
279  {
280  hwproc.PBEAM1 = 0.5 * comEnergy;
281  hwproc.PBEAM2 = 0.5 * comEnergy;
282  pdgToHerwig(2212, hwbmch.PART1);
283  pdgToHerwig(2212, hwbmch.PART2);
284  }
285 
286  if (doMatching) {
287  hwmatchpram.n_match = nMatch;
288  hwmatchpram.matching_scale = matchingScale;
289 
290  if (inclusiveMatching)
291  hwmatchpram.max_multiplicity_flag = 1;
292  else
293  hwmatchpram.max_multiplicity_flag = 0;
294 
295  }
296 
297  if (useJimmy)
298  {
299  info << " HERWIG will be using JIMMY for UE/MI.\n";
300  jmparm.MSFLAG = 1;
301  if (doMPInteraction)
302  info << " JIMMY trying to generate multiple interactions.\n";
303  }
304 
305 
306  // set the IPROC already here... needed for VB pairs
307 
308  bool iprocFound=false;
309 
311  line != parameters.end(); ++line)
312  {
313  if(!strcmp((line->substr(0,5)).c_str(),"IPROC")) {
314  if (!give(*line))
316  << "Herwig 6 did not accept the following: \""
317  << *line << "\"." << std::endl;
318  else iprocFound=true;
319  }
320  }
321 
322  if (!iprocFound && !externalPartons)
324  << "You have to define the process with IPROC." << std::endl;
325 
326  // initialize other common blocks ...
327  call(hwigin); // default init
328 
329  hwevnt.MAXER = 100000000; // O(inf)
330  hwpram.LWSUD = 0; // don't write Sudakov form factors
331  hwdspn.LWDEC = 0; // don't write three/four body decays
332  // (no fort.77 and fort.88 ...)
333  // init LHAPDF glue
334  std::memset(hwprch.AUTPDF, ' ', sizeof hwprch.AUTPDF);
335  for(unsigned int i = 0; i < 2; i++) {
336  hwpram.MODPDF[i] = -111;
337  std::memcpy(hwprch.AUTPDF[i], "HWLHAPDF", 8);
338  }
339 
340  hwevnt.MAXPR = maxEventsToPrint;
341  hwpram.IPRINT = herwigVerbosity;
342 // hwprop.RMASS[6] = 175.0; //FIXME
343 
344  if (printCards) {
345  info << "\n";
346  info << "------------------------------------\n";
347  info << "Reading HERWIG parameters\n";
348  info << "------------------------------------\n";
349 
350  }
352  line != parameters.end(); ++line) {
353  if (printCards)
354  info << " " << *line << "\n";
355  if (!give(*line))
357  << "Herwig 6 did not accept the following: \""
358  << *line << "\"." << std::endl;
359  }
360 
361  if (printCards)
362  info << "\n";
363 
364  if (externalPartons) {
365  std::vector<std::string> slha =
366  lheRunInfo()->findHeader("slha");
367  if (!slha.empty())
368  setSLHAFromHeader(slha);
369  }
370 
371  needClear = true;
372 
373  std::pair<int, int> pdfs(-1, -1);
374  if (externalPartons)
375  pdfs = lheRunInfo()->pdfSetTranslation();
376 
377  if (hwpram.MODPDF[0] != -111 || hwpram.MODPDF[1] != -111) {
378  for(unsigned int i = 0; i < 2; i++)
379  if (hwpram.MODPDF[i] == -111)
380  hwpram.MODPDF[i] = -1;
381 
382  if (pdfs.first != -1 || pdfs.second != -1)
383  edm::LogError("Generator|Herwig6Hadronzier")
384  << "Both external Les Houches event and "
385  "config file specify a PDF set. "
386  "User PDF will override external one."
387  << std::endl;
388 
389  pdfs.first = hwpram.MODPDF[0] != -111 ? hwpram.MODPDF[0] : -1;
390  pdfs.second = hwpram.MODPDF[1] != -111 ? hwpram.MODPDF[1] : -1;
391  }
392 
393  printf("pdfs.first = %i, pdfs.second = %i\n",pdfs.first,pdfs.second);
394 
395  hwpram.MODPDF[0] = pdfs.first;
396  hwpram.MODPDF[1] = pdfs.second;
397 
398  if (externalPartons && hwproc.IPROC>=0)
399  hwproc.IPROC = -1;
400 
401 
402  //Lars: lower EFFMIN threshold, to continue execution of IPROC=4000, lambda'_211=0.01 at LM7,10
403  if( readParticleSpecFile ) {
405  hwpram.EFFMIN = 1e-5;
406  }
407 
408 
409  edm::LogInfo(info.str());
410 
411  return true;
412 
413 }
414 
416 {
417 
418  if (useJimmy) call(jimmin);
419 
420  call(hwuinc);
421 
422  // initialize HERWIG event generation
423  call(hweini);
424 
425  if (useJimmy) {
426  call(jminit);
427  }
428 
429  return true;
430 
431 }
432 
433 
435 {
436  clear();
437 
438  externalPartons = (heprup != 0);
439 
440  std::ostringstream info;
441  info << "---------------------------------------------------\n";
442  info << "Initializing Herwig6Hadronizer for "
443  << (externalPartons ? "external" : "internal") << " partons\n";
444  info << "---------------------------------------------------\n";
445 
446  info << " Herwig verbosity level = " << herwigVerbosity << "\n";
447  info << " HepMC verbosity = " << hepmcVerbosity << "\n";
448  info << " Number of events to be printed = " << maxEventsToPrint << "\n";
449 
450  // Call hwudat to set up HERWIG block data
451  hwudat();
452 
453  // Setting basic parameters
454  if (externalPartons) {
455  hwproc.PBEAM1 = heprup->EBMUP.first;
456  hwproc.PBEAM2 = heprup->EBMUP.second;
457  pdgToHerwig(heprup->IDBMUP.first, hwbmch.PART1);
458  pdgToHerwig(heprup->IDBMUP.second, hwbmch.PART2);
459  } else {
460  hwproc.PBEAM1 = 0.5 * comEnergy;
461  hwproc.PBEAM2 = 0.5 * comEnergy;
462  pdgToHerwig(2212, hwbmch.PART1);
463  pdgToHerwig(2212, hwbmch.PART2);
464  }
465 
466  if (useJimmy) {
467  info << " HERWIG will be using JIMMY for UE/MI.\n";
468  jmparm.MSFLAG = 1;
469  if (doMPInteraction)
470  info << " JIMMY trying to generate multiple interactions.\n";
471  }
472 
473  // set the IPROC already here... needed for VB pairs
474 
475  bool iprocFound=false;
476 
478  line != parameters.end(); ++line) {
479  if(!strcmp((line->substr(0,5)).c_str(),"IPROC")) {
480  if (!give(*line))
482  << "Herwig 6 did not accept the following: \""
483  << *line << "\"." << std::endl;
484  else iprocFound=true;
485  }
486  }
487 
488  if (!iprocFound && !externalPartons)
490  << "You have to define the process with IPROC." << std::endl;
491 
492  // initialize other common blocks ...
493  call(hwigin);
494  hwevnt.MAXER = 100000000; // O(inf)
495  hwpram.LWSUD = 0; // don't write Sudakov form factors
496  hwdspn.LWDEC = 0; // don't write three/four body decays
497  // (no fort.77 and fort.88 ...)
498 
499  // init LHAPDF glue
500 
501  std::memset(hwprch.AUTPDF, ' ', sizeof hwprch.AUTPDF);
502  for(unsigned int i = 0; i < 2; i++) {
503  hwpram.MODPDF[i] = -111;
504  std::memcpy(hwprch.AUTPDF[i], "HWLHAPDF", 8);
505  }
506 
507  if (useJimmy)
508  call(jimmin);
509 
510  hwevnt.MAXPR = maxEventsToPrint;
511  hwpram.IPRINT = herwigVerbosity;
512 // hwprop.RMASS[6] = 175.0; //FIXME
513 
514  if (printCards) {
515  info << "\n";
516  info << "------------------------------------\n";
517  info << "Reading HERWIG parameters\n";
518  info << "------------------------------------\n";
519 
520  }
522  line != parameters.end(); ++line) {
523  if (printCards)
524  info << " " << *line << "\n";
525  if (!give(*line))
527  << "Herwig 6 did not accept the following: \""
528  << *line << "\"." << std::endl;
529  }
530 
531  if (printCards)
532  info << "\n";
533 
534  if (externalPartons) {
535  std::vector<std::string> slha =
536  lheRunInfo()->findHeader("slha");
537  if (!slha.empty())
538  setSLHAFromHeader(slha);
539  }
540 
541  needClear = true;
542 
543  std::pair<int, int> pdfs(-1, -1);
544  if (externalPartons)
545  pdfs = lheRunInfo()->pdfSetTranslation();
546 
547  if (hwpram.MODPDF[0] != -111 || hwpram.MODPDF[1] != -111) {
548  for(unsigned int i = 0; i < 2; i++)
549  if (hwpram.MODPDF[i] == -111)
550  hwpram.MODPDF[i] = -1;
551 
552  if (pdfs.first != -1 || pdfs.second != -1)
553  edm::LogError("Generator|Herwig6Hadronzier")
554  << "Both external Les Houches event and "
555  "config file specify a PDF set. "
556  "User PDF will override external one."
557  << std::endl;
558 
559  pdfs.first = hwpram.MODPDF[0] != -111 ? hwpram.MODPDF[0] : -1;
560  pdfs.second = hwpram.MODPDF[1] != -111 ? hwpram.MODPDF[1] : -1;
561  }
562 
563  printf("pdfs.first = %i, pdfs.second = %i\n",pdfs.first,pdfs.second);
564 
565  hwpram.MODPDF[0] = pdfs.first;
566  hwpram.MODPDF[1] = pdfs.second;
567 
568  if (externalPartons)
569  hwproc.IPROC = -1;
570 
571  //Lars: lower EFFMIN threshold, to continue execution of IPROC=4000, lambda'_211=0.01 at LM7,10
572  if( readParticleSpecFile ) {
574  hwpram.EFFMIN = 1e-5;
575  }
576 
577  // HERWIG preparations ...
578  call(hwuinc);
579  markStable(13); // MU+
580  markStable(-13); // MU-
581  markStable(3112); // SIGMA+
582  markStable(-3112); // SIGMABAR+
583  markStable(3222); // SIGMA-
584  markStable(-3222); // SIGMABAR-
585  markStable(3122); // LAMBDA0
586  markStable(-3122); // LAMBDABAR0
587  markStable(3312); // XI-
588  markStable(-3312); // XIBAR+
589  markStable(3322); // XI0
590  markStable(-3322); // XI0BAR
591  markStable(3334); // OMEGA-
592  markStable(-3334); // OMEGABAR+
593  markStable(211); // PI+
594  markStable(-211); // PI-
595  markStable(321); // K+
596  markStable(-321); // K-
597  markStable(310); // K_S0
598  markStable(130); // K_L0
599 
600  // better: merge with declareStableParticles
601  // and get the list from configuration / Geant4 / Core somewhere
602 
603  // initialize HERWIG event generation
604  call(hweini);
605 
606  if (useJimmy)
607  call(jminit);
608 
609  edm::LogInfo(info.str());
610 
611  return true;
612 }
613 
614 bool Herwig6Hadronizer::declareStableParticles(const std::vector<int> &pdgIds)
615 {
616  markStable(13); // MU+
617  markStable(-13); // MU-
618  markStable(3112); // SIGMA+
619  markStable(-3112); // SIGMABAR+
620  markStable(3222); // SIGMA-
621  markStable(-3222); // SIGMABAR-
622  markStable(3122); // LAMBDA0
623  markStable(-3122); // LAMBDABAR0
624  markStable(3312); // XI-
625  markStable(-3312); // XIBAR+
626  markStable(3322); // XI0
627  markStable(-3322); // XI0BAR
628  markStable(3334); // OMEGA-
629  markStable(-3334); // OMEGABAR+
630  markStable(211); // PI+
631  markStable(-211); // PI-
632  markStable(321); // K+
633  markStable(-321); // K-
634  markStable(310); // K_S0
635  markStable(130); // K_L0
636 
637  for(std::vector<int>::const_iterator iter = pdgIds.begin();
638  iter != pdgIds.end(); ++iter)
639  if (!markStable(*iter))
640  return false;
641  return true;
642 }
643 
644 bool Herwig6Hadronizer::declareSpecialSettings( const std::vector<std::string> )
645 {
646  return true;
647 }
648 
650 {
651  if (!runInfo().internalXSec()) {
652  // not set via LHE, so get it from HERWIG
653  // the reason is that HERWIG doesn't compute the xsec
654  // in all LHE modes
655 
656  double RNWGT = 1. / hwevnt.NWGTS;
657  double AVWGT = hwevnt.WGTSUM * RNWGT;
658 
659  double xsec = 1.0e3 * AVWGT;
660 
661  runInfo().setInternalXSec(xsec);
662  }
663 }
664 
666 {
667  // hard process generation, parton shower, hadron formation
668 
669  InstanceWrapper wrapper(this); // safe guard
670 
671  event().reset();
672 
673  // call herwig routines to create HEPEVT
674 
675  hwuine(); // initialize event
676 
677  if (callWithTimeout(10, hwepro)) { // process event and PS
678  // We hung for more than 10 seconds
679  int error = 199;
680  hwwarn_("HWHGUP", &error);
681  }
682 
683  hwbgen(); // parton cascades
684 
685  // call jimmy ... only if event is not killed yet by HERWIG
686  if (useJimmy && doMPInteraction && !hwevnt.IERROR && call_hwmsct()) {
688  return false;
689  }
690 
691  hwdhob(); // heavy quark decays
692  hwcfor(); // cluster formation
693  hwcdec(); // cluster decays
694 
695 // // if event *not* killed by HERWIG, return true
696 // if (hwevnt.IERROR) {
697 // hwufne(); // finalize event, to keep system clean
698 // if (lheEvent()) lheEvent()->count(lhef::LHERunInfo::kKilled);
699 // return false;
700 // }
701 
702  //if (lheEvent()) lheEvent()->count(lhef::LHERunInfo::kAccepted);
703 
704  hwdhad(); // unstable particle decays
705  hwdhvy(); // heavy flavour decays
706  hwmevt(); // soft underlying event
707 
708 
709  hwufne(); // finalize event
710 
711 
712  // if event *not* killed by HERWIG, return true
713  if (hwevnt.IERROR) {
715  return false;
716  }
717 
718  if (doMatching) {
719  bool pass = call_hwmatch();
720  if (!pass) {
721  printf("Event failed MLM matching\n");
723  return false;
724  }
725  }
726 
728 
729  event().reset(new HepMC::GenEvent);
730  if (!conv.fill_next_event(event().get()))
731  throw cms::Exception("Herwig6Error")
732  << "HepMC Conversion problems in event." << std::endl;
733 
734  // do particle ID conversion Herwig->PDG, if requested
735  if ( fConvertToPDG ) {
736  for ( HepMC::GenEvent::particle_iterator part = event()->particles_begin(); part != event()->particles_end(); ++part) {
737  if ((*part)->pdg_id() != HepPID::translateHerwigtoPDT((*part)->pdg_id()))
738  (*part)->set_pdg_id(HepPID::translateHerwigtoPDT((*part)->pdg_id()));
739  }
740  }
741 
742  return true;
743 }
744 
746 {
748 
749  HepMC::PdfInfo pdfInfo;
750  if(externalPartons) {
751  lheEvent()->fillEventInfo( event().get() );
752  lheEvent()->fillPdfInfo( &pdfInfo );
753 
754  // for MC@NLO: IDWRUP is not filled...
755  if(event()->signal_process_id()==0)
756  event()->set_signal_process_id( abs(hwproc.IPROC) );
757 
758  }
759 
760  HepMC::GenParticle* incomingParton = NULL;
761  HepMC::GenParticle* targetParton = NULL;
762 
763  HepMC::GenParticle* incomingProton = NULL;
764  HepMC::GenParticle* targetProton = NULL;
765 
766  // find incoming parton (first entry with IST=121)
767  for(HepMC::GenEvent::particle_const_iterator it = event()->particles_begin();
768  (it != event()->particles_end() && incomingParton==NULL); it++)
769  if((*it)->status()==121) incomingParton = (*it);
770 
771  // find target parton (first entry with IST=122)
772  for(HepMC::GenEvent::particle_const_iterator it = event()->particles_begin();
773  (it != event()->particles_end() && targetParton==NULL); it++)
774  if((*it)->status()==122) targetParton = (*it);
775 
776  // find incoming Proton (first entry ID=2212, IST=101)
777  for(HepMC::GenEvent::particle_const_iterator it = event()->particles_begin();
778  (it != event()->particles_end() && incomingProton==NULL); it++)
779  if((*it)->status()==101 && (*it)->pdg_id()==2212) incomingProton = (*it);
780 
781  // find target Proton (first entry ID=2212, IST=102)
782  for(HepMC::GenEvent::particle_const_iterator it = event()->particles_begin();
783  (it != event()->particles_end() && targetProton==NULL); it++)
784  if((*it)->status()==102 && (*it)->pdg_id()==2212) targetProton = (*it);
785 
786  // find hard scale Q (computed from colliding partons)
787  if( incomingParton && targetParton ) {
788  math::XYZTLorentzVector totMomentum(0,0,0,0);
789  totMomentum+=incomingParton->momentum();
790  totMomentum+=targetParton->momentum();
791  double evScale = totMomentum.mass();
792  double evScale2 = evScale*evScale;
793 
794  // find alpha_QED & alpha_QCD
795  int one=1;
796  double alphaQCD=hwualf_(&one,&evScale);
797  double alphaQED=hwuaem_(&evScale2);
798 
799  if(!externalPartons || event()->event_scale() < 0) event()->set_event_scale(evScale);
800  if(!externalPartons || event()->alphaQCD() < 0) event()->set_alphaQCD(alphaQCD);
801  if(!externalPartons || event()->alphaQED() < 0) event()->set_alphaQED(alphaQED);
802 
803  if(!externalPartons || pdfInfo.x1() < 0) {
804  // get the PDF information
805  pdfInfo.set_id1( incomingParton->pdg_id()==21 ? 0 : incomingParton->pdg_id());
806  pdfInfo.set_id2( targetParton->pdg_id()==21 ? 0 : targetParton->pdg_id());
807  if( incomingProton && targetProton ) {
808  double x1 = incomingParton->momentum().pz()/incomingProton->momentum().pz();
809  double x2 = targetParton->momentum().pz()/targetProton->momentum().pz();
810  pdfInfo.set_x1(x1);
811  pdfInfo.set_x2(x2);
812  }
813  // we do not fill pdf1 & pdf2, since they are not easily available (what are they needed for anyways???)
814  pdfInfo.set_scalePDF(evScale); // the same as Q above... does this make sense?
815  }
816 
817  if(!externalPartons || event()->signal_process_id() < 0) event()->set_signal_process_id( abs(hwproc.IPROC) );
818  event()->set_pdf_info( pdfInfo );
819  }
820 
821  // add event weight & PDF information
822  if (lheRunInfo() != 0 && std::abs(lheRunInfo()->getHEPRUP()->IDWTUP) == 4)
823  // in LHE weighting mode 4 the weight is an xsec, so convert form HERWIG
824  // to standard CMS unit "picobarn"
825  event()->weights().push_back( 1.0e3 * hwevnt.EVWGT );
826  else
827  event()->weights().push_back( hwevnt.EVWGT );
828 
829 
830  // find final parton (first entry with IST=123)
831  HepMC::GenParticle* finalParton = NULL;
832  for(HepMC::GenEvent::particle_const_iterator it = event()->particles_begin();
833  (it != event()->particles_end() && finalParton==NULL); it++)
834  if((*it)->status()==123) finalParton = (*it);
835 
836 
837  // add GenEventInfo & binning Values
838  eventInfo().reset(new GenEventInfoProduct(event().get()));
839  if(finalParton) {
840  double thisPt=finalParton->momentum().perp();
841  eventInfo()->setBinningValues(std::vector<double>(1, thisPt));
842  }
843 
844  // emulate PY6 status codes, if switched on...
847 
848 }
849 
850 
852 {
853  // hadron decays
854 
855 // InstanceWrapper wrapper(this); // safe guard
856 //
857 // //int iproc = hwproc.IPROC;
858 // //hwproc.IPROC = 312;
859 // hwdhad(); // unstable particle decays
860 // //hwproc.IPROC = iproc;
861 //
862 // hwdhvy(); // heavy flavour decays
863 // hwmevt(); // soft underlying event
864 //
865 // if (doMatching) {
866 // bool pass = call_hwmatch();
867 // if (!pass) {
868 // printf("Event failed MLM matching\n");
869 // hwufne();
870 // if (lheEvent()) lheEvent()->count(lhef::LHERunInfo::kKilled);
871 // return false;
872 // }
873 // }
874 //
875 // hwufne(); // finalize event
876 //
877 // if (hwevnt.IERROR)
878 // return false;
879 //
880 // if (lheEvent()) lheEvent()->count(lhef::LHERunInfo::kAccepted);
881 //
882 // event().reset(new HepMC::GenEvent);
883 // if (!conv.fill_next_event(event().get()))
884 // throw cms::Exception("Herwig6Error")
885 // << "HepMC Conversion problems in event." << std::endl;
886 //
887 // // do particle ID conversion Herwig->PDG, if requested
888 // if ( fConvertToPDG ) {
889 // for ( HepMC::GenEvent::particle_iterator part = event()->particles_begin(); part != event()->particles_end(); ++part) {
890 // if ((*part)->pdg_id() != HepPID::translateHerwigtoPDT((*part)->pdg_id()))
891 // (*part)->set_pdg_id(HepPID::translateHerwigtoPDT((*part)->pdg_id()));
892 // }
893 // }
894 
895  return true;
896 }
897 
899 {
900  return true;
901 }
902 
904 {
906  heprup_.pdfgup[0] = heprup_.pdfgup[1] = -1;
907  heprup_.pdfsup[0] = heprup_.pdfsup[1] = -1;
908  // we set up the PDFs ourselves
909 
910  // pass HERWIG paramaters fomr header (if present)
911  std::string mcnloHeader="herwig6header";
912  std::vector<lhef::LHERunInfo::Header> headers=lheRunInfo()->getHeaders();
913  for(std::vector<lhef::LHERunInfo::Header>::const_iterator hIter=headers.begin();hIter!=headers.end(); ++hIter) {
914  if(hIter->tag()==mcnloHeader){
915  readMCatNLOfile=true;
916  for(lhef::LHERunInfo::Header::const_iterator lIter=hIter->begin(); lIter != hIter->end(); ++lIter) {
917  if((lIter->c_str())[0]!='#' && (lIter->c_str())[0]!='\n') { // it's not a comment)
918  if (!give(*lIter))
920  << "Herwig 6 did not accept the following: \""
921  << *lIter << "\"." << std::endl;
922  }
923  }
924  }
925  }
926 }
927 
929 {
931 
932  // if MCatNLO external file is read, read comment & pass IHPRO to HERWIG
933  if(readMCatNLOfile) {
934  for(std::vector<std::string>::const_iterator iter=lheEvent()->getComments().begin();
935  iter!=lheEvent()->getComments().end(); ++iter) {
936  std::string toParse(iter->substr(1));
937  if (!give(toParse))
939  << "Herwig 6 did not accept the following: \""
940  << toParse << "\"." << std::endl;
941  }
942  }
943 
944 }
945 
947 {
948  int status = p->status();
949 
950  // weird 9922212 particles...
951  if (status == 3 && !p->end_vertex())
952  return 2;
953 
954  if (status >= 1 && status <= 3)
955  return status;
956 
957  if (!p->end_vertex())
958  return 1;
959 
960  // let's prevent particles having status 3, if the identical
961  // particle downstream is a better status 3 candidate
962  int currentId = p->pdg_id();
963  int orig = status;
964  if (status == 123 || status == 124 ||
965  status == 155 || status == 156 || status == 160 ||
966  (status >= 195 && status <= 197)) {
967  for(const HepMC::GenParticle *q = p;;) {
968  const HepMC::GenVertex *vtx = q->end_vertex();
969  if (!vtx)
970  break;
971 
972  HepMC::GenVertex::particles_out_const_iterator iter;
973  for(iter = vtx->particles_out_const_begin();
974  iter != vtx->particles_out_const_end(); ++iter)
975  if ((*iter)->pdg_id() == currentId)
976  break;
977 
978  if (iter == vtx->particles_out_const_end())
979  break;
980 
981  q = *iter;
982  if (q->status() == 3 ||
983  ((status == 120 || status == 123 ||
984  status == 124) && orig > 124))
985  return 4;
986  }
987  }
988 
989  int nesting = 0;
990  for(;;) {
991  if ((status >= 120 && status <= 122) || status == 3) {
992  // avoid flagging status 3 if there is a
993  // better status 3 candidate upstream
994  if (externalPartons)
995  return ((orig >= 121 && orig <= 124) ||
996  orig == 3) ? 3 : 4;
997  else
998  return (nesting ||
999  (status != 3 && orig <= 124)) ? 3 : 4;
1000  }
1001 
1002  // check whether we are leaving the hard process
1003  // including heavy resonance decays
1004  if (!(status == 4 || status == 123 || status == 124 ||
1005  status == 155 || status == 156 || status == 160 ||
1006  (status >= 195 && status <= 197)))
1007  break;
1008 
1009  const HepMC::GenVertex *vtx = p->production_vertex();
1010  if (!vtx || !vtx->particles_in_size())
1011  break;
1012 
1013  p = *vtx->particles_in_const_begin();
1014  status = p->status();
1015 
1016  int newId = p->pdg_id();
1017 
1018  if (!newId)
1019  break;
1020 
1021  // nesting increases if we move to the next-best mother
1022  if (newId != currentId) {
1023  if (++nesting > 1 && externalPartons)
1024  break;
1025  currentId = newId;
1026  }
1027  }
1028 
1029  return 2;
1030 }
1031 
1033 {
1034  for(HepMC::GenEvent::particle_iterator iter =
1035  event()->particles_begin();
1036  iter != event()->particles_end(); iter++)
1037  (*iter)->set_status(pythiaStatusCode(*iter));
1038 
1039  for(HepMC::GenEvent::particle_iterator iter =
1040  event()->particles_begin();
1041  iter != event()->particles_end(); iter++)
1042  if ((*iter)->status() == 4)
1043  (*iter)->set_status(2);
1044 }
1045 
1047 
1049 DEFINE_FWK_MODULE(Herwig6GeneratorFilter);
1050 
1052 DEFINE_FWK_MODULE(Herwig6HadronizerFilter);
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
void call(void(&fn)())
void setSLHAFromHeader(const std::vector< std::string > &lines)
bool declareStableParticles(const std::vector< int > &pdgIds)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void fillEventInfo(HepMC::GenEvent *hepmc) const
Definition: LHEEvent.cc:245
std::pair< double, double > EBMUP
Definition: LesHouches.h:78
const char * classname() const
#define hwdspn
Definition: herwig.h:223
#define jminit
Definition: herwig.h:320
int pdfgup[2]
edm::HadronizerFilter< Herwig6Hadronizer, gen::ExternalDecayDriver > Herwig6HadronizerFilter
bool exists(std::string const &parameterName) const
checks if a parameter exists
HepMC::IO_HERWIG conv
#define abs(x)
Definition: mlp_lapack.h:159
static void fixHepMCEventTimeOrdering(HepMC::GenEvent *event)
Definition: LHEEvent.cc:508
void openParticleSpecFile(const std::string fileName)
#define NULL
Definition: scimark2.h:8
void count(LHERunInfo::CountMode count, double weight=1.0, double matchWeight=1.0)
Definition: LHEEvent.cc:203
std::pair< int, int > IDBMUP
Definition: LesHouches.h:73
struct HEPRUP_ heprup_
std::vector< std::string >::const_iterator const_iterator
edm::GeneratorFilter< Herwig6Hadronizer, gen::ExternalDecayDriver > Herwig6GeneratorFilter
virtual void upInit()
bool initialize(const lhef::HEPRUP *heprup)
void setInternalXSec(const XSec &xsec)
#define hwprch
Definition: herwig.h:63
uint16_t size_type
std::auto_ptr< HepMC::GenEvent > & event()
int pythiaStatusCode(const HepMC::GenParticle *p) const
#define hwmsct
Definition: herwig.h:321
double hwualf_(int *mode, double *scale)
double hwuaem_(double *scale)
#define hwmatch
Definition: herwig.h:340
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:30
GenRunInfoProduct & runInfo()
list path
Definition: scaleCards.py:51
#define jmparm
Definition: herwig.h:301
void hwuidt_(int *iopt, int *ipdg, int *iwig, char nwig[8])
const std::vector< std::string > & getComments() const
Definition: LHEEvent.h:46
void mysetpdfpath_(const char *path)
lhef::LHEEvent * lheEvent()
bool callWithTimeout(unsigned int secs, void(*fn)())
void hwwarn_(const char *method, int *id)
tuple result
Definition: query.py:137
static void fillHEPEUP(const HEPEUP *hepeup)
void fillPdfInfo(HepMC::PdfInfo *info) const
Definition: LHEEvent.cc:217
const std::vector< Header > & getHeaders() const
Definition: LHERunInfo.h:58
block
Formating index page&#39;s pieces.
Definition: Association.py:232
std::auto_ptr< GenEventInfoProduct > & eventInfo()
bool give(const std::string &line)
lhef::LHERunInfo * lheRunInfo()
#define jmefin
Definition: herwig.h:322
virtual void upEvnt()
#define hwmatchpram
Definition: herwig.h:334
const HEPRUP * getHEPRUP() const
Definition: LHERunInfo.h:52
part
Definition: HCALResponse.h:21
list blocks
Definition: gather_cfg.py:90
std::string particleSpecFileName
std::pair< int, int > pdfSetTranslation() const
Definition: LHERunInfo.cc:502
bool declareSpecialSettings(const std::vector< std::string >)
#define begin
Definition: vmac.h:31
list key
Definition: combine.py:13
const_iterator end() const
Herwig6Hadronizer(const edm::ParameterSet &params)
const_iterator begin() const
void setherwpdf_(void)
std::vector< std::string > findHeader(const std::string &tag) const
Definition: LHERunInfo.cc:390
#define jimmin
Definition: herwig.h:319
int pdfsup[2]
tuple status
Definition: ntuplemaker.py:245
gen::ParameterCollector parameters
double split
Definition: MVATrainer.cc:139
bool initializeForExternalPartons()
static void fillHEPRUP(const HEPRUP *heprup)
static HepMC::HEPEVT_Wrapper wrapper