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