HepMC3 event record library
Pythia6ToHepMC3.cc
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC3
4 // Copyright (C) 2014-2020 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef Pythia6_Pythia6ToHepMC3_H
7 #define Pythia6_Pythia6ToHepMC3_H
8 #ifdef DUMMYPYTHIA6TOHEPMC3
9 extern "C" {
10 
11  int hepmc3_delete_writer_(const int & position)
12  {
13  return -1;
14  }
15  int hepmc3_convert_event_(const int & position)
16  {
17  return -1;
18  }
19  int hepmc3_write_event_(const int & position)
20  {
21  return -1;
22  }
23  int hepmc3_clear_event_(const int & position)
24  {
25  return -1;
26  }
27  int hepmc3_set_cross_section_(const int & position, const double& x, const double& xe, const int& n1, const int& n2)
28  {
29  return -1;
30  }
31 
32  int hepmc3_set_pdf_info_(const int & position, const int& parton_id1, const int& parton_id2, const double& x1, const double& x2,
33  const double& scale_in, const double& xf1, const double& xf2,
34  const int& pdf_id1, const int& pdf_id2)
35  {
36  return -1;
37  }
38  int hepmc3_set_hepevt_address_(int* a)
39  {
40  return -1;
41  }
42  int hepmc3_set_attribute_int_(const int & position, const int & attval, const char* attname)
43  {
44  return -1;
45  }
46  int hepmc3_set_attribute_double_(const int & position, const double & attval, const char* attname)
47  {
48  return -1;
49  }
50  int hepmc3_new_writer_(const int & position, const int & mode, const char* ffilename)
51  {
52  return -1;
53  }
54  int hepmc3_new_weight_(const int & position, const char* name)
55  {
56  return -1;
57  }
58  int hepmc3_set_weight_by_index_(const int & position, const double& val, const int & pos)
59  {
60  return -1;
61  }
62  int hepmc3_set_weight_by_name_(const int & position, const double& val, const char* name)
63  {
64  return -1;
65  }
66 }
67 
68 
69 #else
70 #include "HepMC3/HEPEVT_Wrapper.h"
71 #include "HepMC3/GenEvent.h"
72 #include "HepMC3/Writer.h"
73 #include "HepMC3/WriterHEPEVT.h"
74 #include "HepMC3/WriterAscii.h"
76 #include "HepMC3/WriterPlugin.h"
77 #include "HepMC3/Print.h"
78 #include "HepMC3/Attribute.h"
79 #include "HepMC3/GenEvent.h"
80 #include "HepMC3/GenRunInfo.h"
81 using namespace HepMC3;
82 /** Storage for the output objects (Writers)*/
83 std::map<int, std::pair<std::shared_ptr<Writer>, GenEvent*> > hepmc3_gWriters;
84 /** Storage for the GenRunInfo objects associated with the outputs */
85 std::map<int, std::shared_ptr<GenRunInfo> > hepmc3_gGenRunInfos;
86 /** Interface to acces the enets from C++, e.g. Rivet */
87 GenEvent* hepmc3_gWriters_get_event(const int & position)
88 {
89  if (hepmc3_gWriters.count(position) == 0) {
90  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
91  return NULL;
92  }
93  return hepmc3_gWriters[position].second;
94 }
95 /** Interfaces for C/Fortran */
96 extern "C" {
97 
98  int hepmc3_delete_writer_(const int & position)
99  {
100  if (hepmc3_gWriters.count(position) == 0) {
101  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
102  return 1;
103  }
104  hepmc3_gWriters[position].first->close();
105  hepmc3_gWriters.erase(hepmc3_gWriters.find(position));
106  return 0;
107 
108  }
109  int hepmc3_convert_event_(const int & position)
110  {
111  if (hepmc3_gWriters.count(position) == 0) {
112  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
113  return 1;
114  }
115  if (!hepevtptr)
116  {
117  printf("Error in %s: HEPEVT block does not exist\n", __FUNCTION__);
118  return 1;
119  }
120  hepmc3_gWriters[position].second=new GenEvent(Units::GEV, Units::MM);
121  for( int i = 1; i <= HEPEVT_Wrapper::number_entries(); i++ )
122  if (hepevtptr->jmohep[i-1][1]<hepevtptr->jmohep[i-1][0]) hepevtptr->jmohep[i-1][1] = hepevtptr->jmohep[i-1][0];
123  HEPEVT_Wrapper::HEPEVT_to_GenEvent(hepmc3_gWriters[position].second);
124  if (hepmc3_gGenRunInfos.count(position) == 0) hepmc3_gGenRunInfos[position] = std::make_shared<GenRunInfo>();
125  hepmc3_gWriters[position].second->set_run_info(hepmc3_gGenRunInfos[position]);
126  hepmc3_gWriters[position].second->weights() = std::vector<double>(hepmc3_gGenRunInfos[position]->weight_names().size(), 1.0);
127  return 0;
128  }
129  int hepmc3_write_event_(const int & position)
130  {
131  if (hepmc3_gWriters.count(position) == 0) {
132  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
133  return 1;
134  }
135  hepmc3_gWriters[position].first->write_event(*(hepmc3_gWriters[position].second));
136  return 0;
137  }
138  int hepmc3_clear_event_(const int & position)
139  {
140  if (hepmc3_gWriters.count(position) == 0) {
141  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
142  return 1;
143  }
144  hepmc3_gWriters[position].second->clear();
145  return 0;
146  }
147  int hepmc3_set_cross_section_(const int & position, const double& x, const double& xe, const int& n1, const int& n2)
148  {
149  if (hepmc3_gWriters.count(position) == 0) {
150  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
151  return 1;
152  }
153  GenCrossSectionPtr cs=std::make_shared< GenCrossSection>();
154  cs->set_cross_section(x, xe, n1, n2);
155  hepmc3_gWriters[position].second->set_cross_section(cs);
156  return 0;
157  }
158 
159  int hepmc3_set_pdf_info_(const int & position, const int& parton_id1, const int& parton_id2, const double& x1, const double& x2,
160  const double& scale_in, const double& xf1, const double& xf2,
161  const int& pdf_id1, const int& pdf_id2)
162  {
163  if (hepmc3_gWriters.count(position) == 0) {
164  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
165  return 1;
166  }
167  GenPdfInfoPtr pdf=std::make_shared< GenPdfInfo>();
168  pdf->set(parton_id1, parton_id2, x1, x2, scale_in, xf1, xf2, pdf_id1, pdf_id2);
169  hepmc3_gWriters[position].second->set_pdf_info(pdf);
170  return 0;
171  }
172 
173 
174  int hepmc3_set_hepevt_address_(int* a)
175  {
176  if (!hepevtptr)
177  {
178  printf("Info in %s: setting /hepevt/ block adress\n", __FUNCTION__);
180  return 0;
181  }
182  else
183  {
184  printf("Info in %s: /hepevt/ block adress is already set\n", __FUNCTION__);
185  return 1;
186  }
187  }
188  int hepmc3_set_attribute_int_(const int & position, const int & attval, const char* attname)
189  {
190  if (hepmc3_gWriters.count(position) == 0) {
191  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
192  return 1;
193  }
194  hepmc3_gWriters[position].second->add_attribute(attname, std::make_shared<IntAttribute>(attval));
195  return 0;
196  }
197  int hepmc3_set_attribute_double_(const int & position, const double & attval, const char* attname)
198  {
199  if (hepmc3_gWriters.count(position) == 0) {
200  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
201  return 1;
202  }
203  hepmc3_gWriters[position].second->add_attribute(attname, std::make_shared<DoubleAttribute>(attval));
204  return 0;
205  }
206 
207  int hepmc3_new_writer_(const int & position, const int & mode, const char* ffilename)
208  {
209  std::string libHepMC3rootIO="libHepMC3rootIO.so";
210 #ifdef __darwin__
211  libHepMC3rootIO="libHepMC3rootIO.dydl";
212 #endif
213 #ifdef WIN32
214  libHepMC3rootIO="HepMC3rootIO.dll";
215 #endif
216  std::string filename=std::string(ffilename);
217  int r_position=position;
218  if (r_position == 0)
219  {
220  if (hepmc3_gWriters.size() == 0) r_position = 1;
221  if (hepmc3_gWriters.size() != 0) r_position = hepmc3_gWriters.rend()->first+1;
222  }
223  if (hepmc3_gWriters.count(r_position) != 0) {
224  printf("Error in %s: Writer at position %i already exists\n", __FUNCTION__, r_position);
225  exit(1);
226  }
227  if (hepmc3_gGenRunInfos.count(r_position) != 0) {
228  printf("Warning in %s: RunInfo at position %i already exists\n", __FUNCTION__, r_position);
229  }
230  else
231  {
232  hepmc3_gGenRunInfos[r_position]=std::make_shared<GenRunInfo>();
233  }
234 
235  switch (mode)
236  {
237  case 1:
238  hepmc3_gWriters[r_position] = std::pair<std::shared_ptr<Writer>, GenEvent*>(std::make_shared<WriterAscii>(filename.c_str(), hepmc3_gGenRunInfos[position]), new GenEvent(hepmc3_gGenRunInfos[position], Units::GEV, Units::MM));
239  break;
240  case 2:
241  hepmc3_gWriters[r_position] = std::pair<std::shared_ptr<Writer>, GenEvent*>(std::make_shared<WriterAsciiHepMC2>(filename.c_str(), hepmc3_gGenRunInfos[position]), new GenEvent(hepmc3_gGenRunInfos[position], Units::GEV, Units::MM));
242  break;
243  case 3:
244  hepmc3_gWriters[r_position] = std::pair<std::shared_ptr<Writer>, GenEvent*>(std::make_shared<WriterHEPEVT>(filename.c_str()), new GenEvent(hepmc3_gGenRunInfos[position], Units::GEV, Units::MM));
245  break;
246  case 4:
247  hepmc3_gWriters[r_position] = std::pair<std::shared_ptr<Writer>, GenEvent*>(std::make_shared<WriterPlugin>(filename.c_str(), libHepMC3rootIO, std::string("newWriterRootfile"), hepmc3_gGenRunInfos[position]), new GenEvent(hepmc3_gGenRunInfos[position], Units::GEV, Units::MM));
248  break;
249  case 5:
250  hepmc3_gWriters[r_position] = std::pair<std::shared_ptr<Writer>, GenEvent*>(std::make_shared<WriterPlugin>(filename.c_str(), libHepMC3rootIO, std::string("newWriterRootTreefile"), hepmc3_gGenRunInfos[position]), new GenEvent(hepmc3_gGenRunInfos[position], Units::GEV, Units::MM));
251  break;
252  default:
253  printf("Error in %s:Output format %d is unknown or not supported.\n", __FUNCTION__, mode);
254  exit(2);
255  break;
256  }
257  return r_position;
258  }
259  int hepmc3_new_weight_(const int & position, const char* name)
260  {
261  if (hepmc3_gGenRunInfos.count(position) == 0) {
262  printf("Warning in %s: RunInfo at position %i does not exist\n", __FUNCTION__, position);
263  return 1;
264  }
265  if (hepmc3_gGenRunInfos[position]->weight_index(std::string(name)) >= 0) return 0;
266  std::vector<std::string> weight_names = hepmc3_gGenRunInfos[position]->weight_names();
267  weight_names.push_back(std::string(name));
268  hepmc3_gWriters[position].second->weights().push_back(1.0);
269  hepmc3_gGenRunInfos[position]->set_weight_names(weight_names);
270  return 0;
271  }
272  int hepmc3_set_weight_by_index_(const int & position, const double& val, const int & index)
273  {
274  if (hepmc3_gWriters.count(position) == 0) {
275  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
276  return 1;
277  }
278  if (hepmc3_gWriters[position].second->weights().size() < index) {
279  printf("Warning in %s: Event has no weight with index %i\n", __FUNCTION__, index);
280  return 2;
281  }
282  hepmc3_gWriters[position].second->weights()[index] = val;
283  return 0;
284  }
285  int hepmc3_set_weight_by_name_(const int & position, const double& val, const char* name)
286  {
287  if (hepmc3_gWriters.count(position) == 0) {
288  printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
289  return 1;
290  }
291  hepmc3_new_weight_(position, name);
292  hepmc3_gWriters[position].second->weight(std::string(name)) = val;
293  return 0;
294  }
295 }
296 #endif
297 #endif
Definition of class GenRunInfo.
Definition of class WriterHEPEVT.
HepMC3 main namespace.
Definition of class WriterAscii.
struct HEPEVT * hepevtptr
This is a pointer to HEPEVT common block.
static void set_hepevt_address(char *c)
Set Fortran block address.
Stores event-related information.
Definition: GenEvent.h:41
Definition of class WriterPlugin.
Definition of interface Writer.
static bool HEPEVT_to_GenEvent(GenEvent *evt)
Convert HEPEVT to GenEvent.
Definition of class WriterAsciiHepMC2.
int jmohep[NMXHEP][2]
Pointer to position of 1st and 2nd (or last!) mother.
Definition of class GenEvent.
Definition of class Attribute, class IntAttribute and class StringAttribute.
Definition of class HEPEVT_Wrapper.
static int number_entries()
Get number of entries.