CMS 3D CMS Logo

PomwigHadronizer.cc
Go to the documentation of this file.
2 
3 #include <cstring>
4 #include <sstream>
5 #include <string>
6 #include <vector>
7 #include <memory>
8 #include <map>
9 #include <set>
10 
11 #include <boost/shared_ptr.hpp>
12 #include <boost/algorithm/string/classification.hpp>
13 #include <boost/algorithm/string/split.hpp>
14 
15 #include <HepMC/GenEvent.h>
16 #include <HepMC/GenParticle.h>
17 #include <HepMC/GenVertex.h>
18 #include <HepMC/PdfInfo.h>
19 #include <HepMC/HerwigWrapper.h>
20 #include <HepMC/HEPEVT_Wrapper.h>
21 #include <HepMC/IO_HERWIG.h>
22 #include "HepPID/ParticleIDTranslations.hh"
23 
26 
29 
32 
36 
38 
41 
42 namespace gen
43 {
44 extern "C" {
45  void hwuidt_(int *iopt, int *ipdg, int *iwig, char nwig[8]);
46 }
47 
48 // helpers
49 namespace {
50  inline bool call_hwmsct()
51  {
52  int result;
53  hwmsct(&result);
54  return result;
55  }
56 
57  int pdgToHerwig(int ipdg, char *nwig)
58  {
59  int iopt = 1;
60  int iwig = 0;
61  hwuidt_(&iopt, &ipdg, &iwig, nwig);
62  return ipdg ? iwig : 0;
63  }
64 
65  bool markStable(int pdgId)
66  {
67  char nwig[9] = " ";
68  if (!pdgToHerwig(pdgId, nwig))
69  return false;
70  hwusta(nwig, 1);
71  return true;
72  }
73 }
74 
75 #define qcd_1994 qcd_1994_
76 extern "C" {
77  void qcd_1994(double&,double&,double*,int&);
78 }
79 // For H1 2006 fits
80 #define qcd_2006 qcd_2006_
81 extern "C" {
82  void qcd_2006(double&,double&,int&,double*,double*,double*,double*,double*);
83 }
84 
85 extern "C" {
86  void hwwarn_(const char *method, int *id);
87  void setherwpdf_(void);
88  void mysetpdfpath_(const char *path);
89 }
90 
93 
95  BaseHadronizer(params),
96  needClear(false),
97  parameters(params.getParameter<edm::ParameterSet>("HerwigParameters")),
98  herwigVerbosity(params.getUntrackedParameter<int>("herwigVerbosity", 0)),
99  hepmcVerbosity(params.getUntrackedParameter<int>("hepmcVerbosity", 0)),
100  maxEventsToPrint(params.getUntrackedParameter<int>("maxEventsToPrint", 0)),
101  printCards(params.getUntrackedParameter<bool>("printCards", false)),
102  comEnergy(params.getParameter<double>("comEnergy")),
103  survivalProbability(params.getParameter<double>("survivalProbability")),
104  diffTopology(params.getParameter<int>("diffTopology")),
105  h1fit(params.getParameter<int>("h1fit")),
106  useJimmy(params.getParameter<bool>("useJimmy")),
107  doMPInteraction(params.getParameter<bool>("doMPInteraction")),
108  numTrials(params.getUntrackedParameter<int>("numTrialsMPI", 100)),
109  doPDGConvert(false)
110 {
111  if ( params.exists( "doPDGConvert" ) )
112  doPDGConvert = params.getParameter<bool>("doPDGConvert");
113 }
114 
116 {
117  clear();
118 }
119 
120 void PomwigHadronizer::doSetRandomEngine(CLHEP::HepRandomEngine* v)
121 {
123 }
124 
126 {
127  if (!needClear)
128  return;
129 
130  // teminate elementary process
131  call(hwefin);
132  if (useJimmy)
133  call(jmefin);
134 
135  needClear = false;
136 }
137 
139 {
140  return false;
141 }
142 
144 {
145 
146  clear();
147 
148  edm::LogVerbatim("") << "----------------------------------------------\n"
149  << "Initializing PomwigHadronizer\n"
150  << "----------------------------------------------\n";
151 
152  // Call hwudat to set up HERWIG block data
153  hwudat();
154 
155  // Setting basic parameters ...
156  hwproc.PBEAM1 = comEnergy/2.;
157  hwproc.PBEAM2 = comEnergy/2.;
158  // Choose beam particles for POMWIG depending on topology
159  switch (diffTopology){
160  case 0: //DPE
161  hwbmch.PART1[0] = 'E';
162  hwbmch.PART1[1] = '-';
163  hwbmch.PART2[0] = 'E';
164  hwbmch.PART2[1] = '-';
165  break;
166  case 1: //SD survive PART1
167  hwbmch.PART1[0] = 'E';
168  hwbmch.PART1[1] = '-';
169  hwbmch.PART2[0] = 'P';
170  hwbmch.PART2[1] = ' ';
171  break;
172  case 2: //SD survive PART2
173  hwbmch.PART1[0] = 'P';
174  hwbmch.PART1[1] = ' ';
175  hwbmch.PART2[0] = 'E';
176  hwbmch.PART2[1] = '-';
177  break;
178  case 3: //Non diffractive
179  hwbmch.PART1[0] = 'P';
180  hwbmch.PART1[1] = ' ';
181  hwbmch.PART2[0] = 'P';
182  hwbmch.PART2[1] = ' ';
183  break;
184  default:
185  throw edm::Exception(edm::errors::Configuration,"PomwigError")
186  <<" Invalid Diff. Topology. Must be DPE(diffTopology = 0), SD particle 1 (diffTopology = 1), SD particle 2 (diffTopology = 2) and Non diffractive (diffTopology = 3)";
187  break;
188  }
189  for(int i=2;i<8;++i){
190  hwbmch.PART1[i] = ' ';
191  hwbmch.PART2[i] = ' ';}
192 
193  // initialize other common blocks ...
194  call(hwigin);
195 
196  hwevnt.MAXER = 100000000; // O(inf)
197  hwpram.LWSUD = 0; // don't write Sudakov form factors
198  hwdspn.LWDEC = 0; // don't write three/four body decays
199  // (no fort.77 and fort.88 ...)a
200 
201  std::memset(hwprch.AUTPDF, ' ', sizeof hwprch.AUTPDF);
202  for(unsigned int i = 0; i < 2; i++) {
203  hwpram.MODPDF[i] = -111;
204  std::memcpy(hwprch.AUTPDF[i], "HWLHAPDF", 8);
205  }
206 
207  hwevnt.MAXPR = maxEventsToPrint;
208  hwpram.IPRINT = herwigVerbosity;
209 
210  edm::LogVerbatim("") << "------------------------------------\n"
211  << "Reading HERWIG parameters\n"
212  << "------------------------------------\n";
213 
215  line != parameters.end(); ++line) {
216  edm::LogVerbatim("") << " " << *line;
217  if (!give(*line))
219  << "Herwig 6 did not accept the following: \""
220  << *line << "\"." << std::endl;
221  }
222 
223  needClear = true;
224 
225  return true;
226 
227 }
228 
230 {
231 
232  call(hwuinc);
233 
234  hwusta("PI0 ",1);
235 
236  if(!initializeDPDF()) return false;
237 
238  call(hweini);
239 
240  return true;
241 }
242 
244 {
245  // Initialize H1 pomeron/reggeon
246 
247  if(diffTopology == 3) return true;
248 
249  if((diffTopology != 0)&&(diffTopology != 1)&&(diffTopology != 2)) return false;
250 
251  int nstru = hwpram.NSTRU;
252  int ifit = h1fit;
253  if((nstru == 9)||(nstru == 10)){
254  if((ifit <= 0)||(ifit >= 7)){
255  throw edm::Exception(edm::errors::Configuration,"PomwigError")
256  <<" Attempted to set non existant H1 1997 fit index. Has to be 1...6";
257  }
258  std::string aux((nstru == 9)?"Pomeron":"Reggeon");
259  edm::LogVerbatim("") << " H1 1997 pdf's: " << aux << "\n"
260  << " IFIT = " << ifit;
261  double xp = 0.1;
262  double Q2 = 75.0;
263  double xpq[13];
264  qcd_1994(xp,Q2,xpq,ifit);
265  } else if((nstru >= 12)&&(nstru <= 15)){
266  bool isPom = (nstru == 12)||(nstru == 14);
267  bool isFitA = (nstru == 12)||(nstru == 13);
268  ifit = (isFitA)?1:2;
269  std::string aux_0((isFitA)?"A":"B");
270  std::string aux_1((isPom)?"Pomeron":"Reggeon");
271  edm::LogVerbatim("") << " H1 2006 Fit " << aux_0 << " " << aux_1 << "\n"
272  << " IFIT = "<< ifit;
273  double xp = 0.1;
274  double Q2 = 75.0;
275  double xpq[13];
276  double f2[2];
277  double fl[2];
278  double c2[2];
279  double cl[2];
280  qcd_2006(xp,Q2,ifit,xpq,f2,fl,c2,cl);
281  } else{
282  throw edm::Exception(edm::errors::Configuration,"PomwigError")
283  <<" Only running Pomeron H1 1997 (NSTRU=9), H1 2006 fit A (NSTRU=12) and H1 2006 fit B (NSTRU=14) or Reggeon H1 1997 (NSTRU=10), H1 2006 fit A (NSTRU=13) and H1 2006 fit B (NSTRU=15)";
284  }
285 
286  return true;
287 }
288 
289 bool PomwigHadronizer::declareStableParticles(const std::vector<int> &pdgIds)
290 {
291  for(std::vector<int>::const_iterator iter = pdgIds.begin();
292  iter != pdgIds.end(); ++iter)
293  if (!markStable(*iter))
294  return false;
295  return true;
296 }
297 
299 {
300  double RNWGT = 1. / hwevnt.NWGTS;
301  double AVWGT = hwevnt.WGTSUM * RNWGT;
302 
303  double xsec = 1.0e3 * AVWGT;
304  xsec = survivalProbability*xsec;
305 
306  runInfo().setInternalXSec(xsec);
307 }
308 
310 {
311  return false;
312 }
313 
315 {
316  // hard process generation, parton shower, hadron formation
317 
318  InstanceWrapper wrapper(this); // safe guard
319 
320  event().reset();
321 
322  // call herwig routines to create HEPEVT
323 
324  hwuine(); // initialize event
325 
326  if (callWithTimeout(10, hwepro)) { // process event and PS
327  // We hung for more than 10 seconds
328  int error = 199;
329  hwwarn_("HWHGUP", &error);
330  }
331 
332  hwbgen(); // parton cascades
333 
334  // call jimmy ... only if event is not killed yet by HERWIG
335  if (useJimmy && doMPInteraction && !hwevnt.IERROR && call_hwmsct())
336  return false;
337 
338  hwdhob(); // heavy quark decays
339  hwcfor(); // cluster formation
340  hwcdec(); // cluster decays
341 
342  // if event *not* killed by HERWIG, return true
343  if (!hwevnt.IERROR) return true;
344 
345  hwufne(); // finalize event
346  return false;
347 }
348 
350 {
352 
353  event()->set_signal_process_id(hwproc.IPROC);
354 
355  event()->weights().push_back(hwevnt.EVWGT);
356 }
357 
359 {
360  // hadron decays
361 
362  InstanceWrapper wrapper(this); // safe guard
363 
364  hwdhad(); // unstable particle decays
365  hwdhvy(); // heavy flavour decays
366  hwmevt(); // soft underlying event
367 
368  hwufne(); // finalize event
369 
370  if (hwevnt.IERROR)
371  return false;
372 
373  event().reset(new HepMC::GenEvent);
374  if (!conv.fill_next_event(event().get()))
375  throw cms::Exception("PomwigError")
376  << "HepMC Conversion problems in event." << std::endl;
377 
378  // do particle ID conversion Herwig->PDG, if requested
379  if(doPDGConvert){
380  for ( HepMC::GenEvent::particle_iterator part = event()->particles_begin(); part != event()->particles_end(); ++part) {
381  if ((*part)->pdg_id() != HepPID::translateHerwigtoPDT((*part)->pdg_id()))
382  (*part)->set_pdg_id(HepPID::translateHerwigtoPDT((*part)->pdg_id()));
383  }
384  }
385 
386  return true;
387 }
388 
390 {
391  return true;
392 }
393 
394 }//namespace gen
bool declareStableParticles(const std::vector< int > &pdgIds)
T getParameter(std::string const &) const
void call(void(&fn)())
void mysetpdfpath_(const char *path)
bool exists(std::string const &parameterName) const
checks if a parameter exists
static void fixHepMCEventTimeOrdering(HepMC::GenEvent *event)
Definition: LHEEvent.cc:513
gen::ParameterCollector parameters
void setInternalXSec(const XSec &xsec)
double v[5][pyjets_maxn]
std::auto_ptr< HepMC::GenEvent > & event()
GenRunInfoProduct & runInfo()
static const std::vector< std::string > theSharedResources
#define hwprch
Definition: herwig.h:63
bool callWithTimeout(unsigned int secs, void(*fn)())
void hwwarn_(const char *method, int *id)
void setherwpdf_(void)
#define qcd_2006
#define jmefin
Definition: herwig.h:322
HepMC::IO_HERWIG conv
bool give(const std::string &line)
static const std::string kFortranInstance
#define qcd_1994
void hwuidt_(int *iopt, int *ipdg, int *iwig, char nwig[8])
part
Definition: HCALResponse.h:20
#define hwdspn
Definition: herwig.h:223
virtual void doSetRandomEngine(CLHEP::HepRandomEngine *v) override
#define hwmsct
Definition: herwig.h:321
HLT enums.
const_iterator end() const
void setHerwigRandomEngine(CLHEP::HepRandomEngine *v)
const_iterator begin() const
PomwigHadronizer(const edm::ParameterSet &params)
static const std::string kHerwig6
Definition: event.py:1
static HepMC::HEPEVT_Wrapper wrapper