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