HepMC3 event record library
HepMC3Glue.cc
2 #include "HepMC3/GenEvent.h"
3 #include "HepMC3/Writer.h"
4 #include "HepMC3/WriterHEPEVT.h"
5 #include "HepMC3/WriterAscii.h"
7 #include "HepMC3/Print.h"
8 #include "HepMC3/Attribute.h"
9 #include "HepMC3/GenEvent.h"
10 #include "HepMC3/GenRunInfo.h"
11 #ifdef HEPMC3_ROOTIO
12 #include "HepMC3/WriterRoot.h"
13 #include "HepMC3/WriterRootTree.h"
14 #endif
15 using namespace HepMC3;
16 std::map<int,std::pair<Writer*,GenEvent*> > gWriters;
17 extern "C" {
18 
19  int delete_writer_(const int & position)
20  {
21  if (gWriters.find(position)==gWriters.end()) { printf("Warning in %s: Writer at position %i does not exist\n",__FUNCTION__,position); return 1;}
22  gWriters[position].first->close();
23  gWriters.erase(gWriters.find(position));
24  return 0;
25 
26  }
27  int convert_event_(const int & position)
28  {
29  if (gWriters.find(position)==gWriters.end()) { printf("Warning in %s: Writer at position %i does not exist\n",__FUNCTION__,position); return 1;}
30  if (!hepevtptr)
31  {
32  printf("Error in %s: HEPEVT block does not exist\n",__FUNCTION__);
33  return 1;
34  }
35  gWriters[position].second=new GenEvent(Units::GEV,Units::MM);
36  for( int i=1; i<=HEPEVT_Wrapper::number_entries(); i++ )
37  if (hepevtptr->jmohep[i-1][1]<hepevtptr->jmohep[i-1][0]) hepevtptr->jmohep[i-1][1]=hepevtptr->jmohep[i-1][0];
38 
39  HEPEVT_Wrapper::HEPEVT_to_GenEvent(gWriters[position].second);
40  std::shared_ptr<GenRunInfo> run=std::make_shared<GenRunInfo>();
41  std::vector<std::string> names;
42  names.push_back("Default");
43  run->set_weight_names(names);
44  gWriters[position].second->set_run_info(run);
45  return 0;
46  }
47  int write_event_(const int & position)
48  {
49  if (gWriters.find(position)==gWriters.end()) { printf("Warning in %s: Writer at position %i does not exist\n",__FUNCTION__,position); return 1;}
50  gWriters[position].first->write_event(*(gWriters[position].second));
51  return 0;
52  }
53  int clear_event_(const int & position)
54  {
55  if (gWriters.find(position)==gWriters.end()) { printf("Warning in %s: Writer at position %i does not exist\n",__FUNCTION__,position); return 1;}
56  gWriters[position].second->clear();
57  return 0;
58  }
59  int set_cross_section_(const int & position, const double& x,const double& xe, const int& n1,const int& n2)
60  {
61  if (gWriters.find(position)==gWriters.end()) { printf("Warning in %s: Writer at position %i does not exist\n",__FUNCTION__,position); return 1;}
62  GenCrossSectionPtr cs=std::make_shared< GenCrossSection>();
63  cs->set_cross_section(x,xe,n1,n2);
64  gWriters[position].second->set_cross_section(cs);
65  return 0;
66  }
67  int set_hepevt_address_(int* a)
68  {
69  if (!hepevtptr)
70  {
71  printf("Info in %s: setting /hepevt/ block adress\n",__FUNCTION__);
73  return 0;
74  }
75  else
76  {
77  printf("Info in %s: /hepevt/ block adress is already set\n",__FUNCTION__);
78  return 1;
79  }
80  }
81  int set_attribute_int_(const int & position,const int & attval,const char* attname, size_t len)
82  {
83  if (gWriters.find(position)==gWriters.end()) { printf("Warning in %s: Writer at position %i does not exist\n",__FUNCTION__,position); return 1;}
84  gWriters[position].second->add_attribute(attname,std::make_shared<IntAttribute>(attval));
85  return 0;
86  }
87  int set_attribute_double_(const int & position,const double & attval,const char* attname, size_t len)
88  {
89  if (gWriters.find(position)==gWriters.end()) { printf("Warning in %s: Writer at position %i does not exist\n",__FUNCTION__,position); return 1;}
90  gWriters[position].second->add_attribute(attname,std::make_shared<DoubleAttribute>(attval));
91  return 0;
92  }
93 
94  int new_writer_(const int & position,const int & mode,const char* ffilename, size_t len)
95  {
96  std::string filename=std::string(ffilename,len);
97  int r_position=position;
98  if (r_position==0)
99  {
100  if (gWriters.size()==0) r_position=1;
101  if (gWriters.size()!=0) r_position=gWriters.rend()->first+1;
102  }
103  if (gWriters.find(r_position)!=gWriters.end()) { printf("Error in %s: Writer at position %i already exists\n",__FUNCTION__,r_position); exit(1);}
104  switch (mode)
105  {
106  case 1:
107  gWriters[r_position]=std::pair<Writer*,GenEvent*>(new WriterAscii(filename.c_str()),new GenEvent(Units::GEV,Units::MM));
108  break;
109  case 2:
110  gWriters[r_position]=std::pair<Writer*,GenEvent*>(new WriterAsciiHepMC2(filename.c_str()),new GenEvent(Units::GEV,Units::MM));
111  break;
112  case 3:
113  gWriters[r_position]=std::pair<Writer*,GenEvent*>(new WriterHEPEVT(filename.c_str()),new GenEvent(Units::GEV,Units::MM));
114  break;
115 #ifdef HEPMC3_ROOTIO
116  case 4:
117  gWriters[r_position]=std::pair<Writer*,GenEvent*>(new WriterRoot(filename.c_str()),new GenEvent(Units::GEV,Units::MM));
118  break;
119  case 5:
120  gWriters[r_position]=std::pair<Writer*,GenEvent*>(new WriterRootTree(filename.c_str()),new GenEvent(Units::GEV,Units::MM));
121  break;
122 #endif
123  default:
124  printf("Error in %s:Output format %d is unknown or not supported.\n",__FUNCTION__,mode);
125  exit(2);
126  break;
127  }
128  return r_position;
129  }
130 }
GenEvent I/O serialization for structured text files.
Definition of class GenRunInfo.
Definition of class WriterHEPEVT.
Definition of class WriterRootTree.
Definition of class WriterAscii.
static void set_hepevt_address(char *c)
Set Fortran block address.
GenEvent I/O serialization for HEPEVT files.
Definition: WriterHEPEVT.h:27
Stores event-related information.
Definition: GenEvent.h:42
Definition of interface Writer.
GenEvent I/O serialization for root files.
Definition: WriterRoot.h:35
GenEvent I/O serialization for root files based on root TTree.
static bool HEPEVT_to_GenEvent(GenEvent *evt)
Convert HEPEVT to GenEvent.
Definition of class WriterAsciiHepMC2.
Definition of class WriterRoot.
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.
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
Definition of class HEPEVT_Wrapper.
static int number_entries()
Get number of entries.