HepMC3 event record library
Pythia6ToHepMC3.cc
1// -*- C++ -*-
2//
3// This file is part of HepMC3
4// Copyright (C) 2014-2021 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef Pythia6_Pythia6ToHepMC3_H
7#define Pythia6_Pythia6ToHepMC3_H
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/GenEvent.h"
96#include "HepMC3/GenRunInfo.h"
97using namespace HepMC3;
98#ifndef PYTHIA6HEPEVTSIZE
99#define PYTHIA6HEPEVTSIZE 10000
100#endif
101/** The conversion interface, templated version */
103/** Storage for the output objects (Writers)*/
104std::map<int, std::pair<std::shared_ptr<Writer>, GenEvent*> > hepmc3_gWriters;
105/** Storage for the GenRunInfo objects associated with the outputs */
106std::map<int, std::shared_ptr<GenRunInfo> > hepmc3_gGenRunInfos;
107/** Interface to acces the enets from C++, e.g. Rivet */
108GenEvent* hepmc3_gWriters_get_event(const int & position)
109{
110 if (hepmc3_gWriters.count(position) == 0) {
111 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
112 return NULL;
113 }
114 return hepmc3_gWriters[position].second;
115}
116/** Interfaces for C/Fortran */
117extern "C" {
118
119 int hepmc3_delete_writer_(const int & position)
120 {
121 if (hepmc3_gWriters.count(position) == 0) {
122 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
123 return 1;
124 }
125 hepmc3_gWriters[position].first->close();
126 hepmc3_gWriters.erase(hepmc3_gWriters.find(position));
127 return 0;
128
129 }
130 int hepmc3_convert_event_(const int & position)
131 {
132 if (hepmc3_gWriters.count(position) == 0) {
133 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
134 return 1;
135 }
136 if (!hepmc3_gInterface.m_hepevtptr)
137 {
138 printf("Error in %s: HEPEVT block does not exist\n", __FUNCTION__);
139 return 1;
140 }
141 hepmc3_gWriters[position].second = new GenEvent(Units::GEV, Units::MM);
142 for( int i = 1; i <= hepmc3_gInterface.number_entries(); i++ )
143 if (hepmc3_gInterface.m_hepevtptr->jmohep[i-1][1]<hepmc3_gInterface.m_hepevtptr->jmohep[i-1][0]) hepmc3_gInterface.m_hepevtptr->jmohep[i-1][1] = hepmc3_gInterface.m_hepevtptr->jmohep[i-1][0];
144 hepmc3_gInterface.HEPEVT_to_GenEvent(hepmc3_gWriters[position].second);
145 if (hepmc3_gGenRunInfos.count(position) == 0) hepmc3_gGenRunInfos[position] = std::make_shared<GenRunInfo>();
146 hepmc3_gWriters[position].second->set_run_info(hepmc3_gGenRunInfos[position]);
147 hepmc3_gWriters[position].second->weights() = std::vector<double>(hepmc3_gGenRunInfos[position]->weight_names().size(), 1.0);
148 return 0;
149 }
150 int hepmc3_write_event_(const int & position)
151 {
152 if (hepmc3_gWriters.count(position) == 0) {
153 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
154 return 1;
155 }
156 hepmc3_gWriters[position].first->write_event(*(hepmc3_gWriters[position].second));
157 return 0;
158 }
159 int hepmc3_clear_event_(const int & position)
160 {
161 if (hepmc3_gWriters.count(position) == 0) {
162 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
163 return 1;
164 }
165 hepmc3_gWriters[position].second->clear();
166 return 0;
167 }
168 int hepmc3_set_cross_section_(const int & position, const double& x, const double& xe, const int& n1, const int& n2)
169 {
170 if (hepmc3_gWriters.count(position) == 0) {
171 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
172 return 1;
173 }
174 GenCrossSectionPtr cs = std::make_shared< GenCrossSection>();
175 cs->set_cross_section(x, xe, n1, n2);
176 hepmc3_gWriters[position].second->set_cross_section(cs);
177 return 0;
178 }
179
180 int hepmc3_set_pdf_info_(const int & position, const int& parton_id1, const int& parton_id2, const double& x1, const double& x2,
181 const double& scale_in, const double& xf1, const double& xf2,
182 const int& pdf_id1, const int& pdf_id2)
183 {
184 if (hepmc3_gWriters.count(position) == 0) {
185 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
186 return 1;
187 }
188 GenPdfInfoPtr pdf=std::make_shared< GenPdfInfo>();
189 pdf->set(parton_id1, parton_id2, x1, x2, scale_in, xf1, xf2, pdf_id1, pdf_id2);
190 hepmc3_gWriters[position].second->set_pdf_info(pdf);
191 return 0;
192 }
193 int hepmc3_set_hepevt_address_(int* a)
194 {
195 printf("Info in %s: setting /hepevt/ block adress\n", __FUNCTION__);
196 hepmc3_gInterface.set_hepevt_address((char*)a);
197 return 0;
198 }
199 int hepmc3_set_attribute_int_(const int & position, const int & attval, const char* attname)
200 {
201 if (hepmc3_gWriters.count(position) == 0) {
202 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
203 return 1;
204 }
205 hepmc3_gWriters[position].second->add_attribute(attname, std::make_shared<IntAttribute>(attval));
206 return 0;
207 }
208 int hepmc3_set_attribute_double_(const int & position, const double & attval, const char* attname)
209 {
210 if (hepmc3_gWriters.count(position) == 0) {
211 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
212 return 1;
213 }
214 hepmc3_gWriters[position].second->add_attribute(attname, std::make_shared<DoubleAttribute>(attval));
215 return 0;
216 }
217
218 int hepmc3_new_writer_(const int & position, const int & mode, const char* ffilename)
219 {
220 std::string libHepMC3rootIO="libHepMC3rootIO.so";
221#ifdef __darwin__
222 libHepMC3rootIO="libHepMC3rootIO.dylib";
223#endif
224#ifdef WIN32
225 libHepMC3rootIO="HepMC3rootIO.dll";
226#endif
227 std::string filename=std::string(ffilename);
228 int r_position=position;
229 if (r_position == 0)
230 {
231 if (hepmc3_gWriters.size() == 0) r_position = 1;
232 if (hepmc3_gWriters.size() != 0) r_position = hepmc3_gWriters.rend()->first+1;
233 }
234 if (hepmc3_gWriters.count(r_position) != 0) {
235 printf("Error in %s: Writer at position %i already exists\n", __FUNCTION__, r_position);
236 exit(1);
237 }
238 if (hepmc3_gGenRunInfos.count(r_position) != 0) {
239 printf("Warning in %s: RunInfo at position %i already exists\n", __FUNCTION__, r_position);
240 }
241 else
242 {
243 hepmc3_gGenRunInfos[r_position]=std::make_shared<GenRunInfo>();
244 }
245
246 switch (mode)
247 {
248 case 1:
249 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));
250 break;
251 case 2:
252 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));
253 break;
254 case 3:
255 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));
256 break;
257 case 4:
258 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));
259 break;
260 case 5:
261 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));
262 break;
263 default:
264 printf("Error in %s:Output format %d is unknown or not supported.\n", __FUNCTION__, mode);
265 exit(2);
266 break;
267 }
268 return r_position;
269 }
270 int hepmc3_new_weight_(const int & position, const char* name)
271 {
272 if (hepmc3_gGenRunInfos.count(position) == 0) {
273 printf("Warning in %s: RunInfo at position %i does not exist\n", __FUNCTION__, position);
274 return 1;
275 }
276 if (hepmc3_gGenRunInfos[position]->weight_index(std::string(name)) >= 0) return 0;
277 std::vector<std::string> weight_names = hepmc3_gGenRunInfos[position]->weight_names();
278 weight_names.push_back(std::string(name));
279 hepmc3_gWriters[position].second->weights().push_back(1.0);
280 hepmc3_gGenRunInfos[position]->set_weight_names(weight_names);
281 return 0;
282 }
283 int hepmc3_set_weight_by_index_(const int & position, const double& val, const int & index)
284 {
285 if (hepmc3_gWriters.count(position) == 0) {
286 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
287 return 1;
288 }
289 if (hepmc3_gWriters[position].second->weights().size() < index) {
290 printf("Warning in %s: Event has no weight with index %i\n", __FUNCTION__, index);
291 return 2;
292 }
293 hepmc3_gWriters[position].second->weights()[index] = val;
294 return 0;
295 }
296 int hepmc3_set_weight_by_name_(const int & position, const double& val, const char* name)
297 {
298 if (hepmc3_gWriters.count(position) == 0) {
299 printf("Warning in %s: Writer at position %i does not exist\n", __FUNCTION__, position);
300 return 1;
301 }
302 hepmc3_new_weight_(position, name);
303 hepmc3_gWriters[position].second->weight(std::string(name)) = val;
304 return 0;
305 }
306}
307#endif
308#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.