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