Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PHHepMCGenHelper.cc
Go to the documentation of this file.
1 // $Id: $
2 
11 #include "PHHepMCGenHelper.h"
12 
13 #include "PHHepMCGenEvent.h"
14 #include "PHHepMCGenEventMap.h"
15 
17 #include <phool/PHCompositeNode.h>
18 #include <phool/PHDataNode.h>
19 #include <phool/PHRandomSeed.h>
20 #include <phool/getClass.h>
21 
22 #include <HepMC/GenEvent.h>
23 #include <HepMC/IO_GenEvent.h>
24 
25 #include <gsl/gsl_const.h>
26 #include <gsl/gsl_randist.h>
27 #include <gsl/gsl_rng.h>
28 
29 #include <cassert>
30 #include <climits>
31 #include <iostream>
32 
33 using namespace std;
34 
36  : _vertex_func_x(Gaus)
37  , _vertex_func_y(Gaus)
38  , _vertex_func_z(Gaus)
39  , _vertex_func_t(Gaus)
40  , _vertex_x(0)
41  , _vertex_y(0)
42  , _vertex_z(0)
43  , _vertex_t(0)
44  , _vertex_width_x(0)
45  , _vertex_width_y(0)
46  , _vertex_width_z(0)
47  , _vertex_width_t(0)
48  , _embedding_id(0)
49  , _reuse_vertex(false)
50  , _reuse_vertex_embedding_id(numeric_limits<int>::min())
51  , _geneventmap(nullptr)
52 {
53 
54  RandomGenerator = gsl_rng_alloc(gsl_rng_mt19937);
55  unsigned int seed = PHRandomSeed(); // fixed seed is handled in this function
56  gsl_rng_set(RandomGenerator, seed );
57 }
58 
60 {
61  gsl_rng_free(RandomGenerator);
62 }
63 
66 {
67  PHNodeIterator iter(topNode);
68  PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
69  if (!dstNode)
70  {
71  cout << PHWHERE << "DST Node missing doing nothing" << endl;
73  }
74 
75  _geneventmap = findNode::getClass<PHHepMCGenEventMap>(dstNode, "PHHepMCGenEventMap");
76  if (!_geneventmap)
77  {
78  _geneventmap = new PHHepMCGenEventMap();
79  PHIODataNode<PHObject> *newmapnode = new PHIODataNode<PHObject>(_geneventmap, "PHHepMCGenEventMap", "PHObject");
80  dstNode->addNode(newmapnode);
81  }
82 
83  assert(_geneventmap);
84 
86 }
87 
90 {
91  assert(evt);
92  assert(_geneventmap);
93 
94  PHHepMCGenEvent *genevent = _geneventmap->insert_event(_embedding_id);
95 
96  genevent->addEvent(evt);
97  move_vertex(genevent);
98 
99  return genevent;
100 }
101 
103 {
104  assert(genevent);
105 
106  assert(_vertex_width_x >= 0);
107 
108  if (_reuse_vertex)
109  {
110  assert(_geneventmap);
111 
112  PHHepMCGenEvent *vtx_evt =
113  _geneventmap->get(_reuse_vertex_embedding_id);
114 
115  if (!vtx_evt)
116  {
117  cout << "PHHepMCGenHelper::move_vertex - Fatal Error - the requested source subevent with embedding ID "
118  << _reuse_vertex_embedding_id << " does not exist. Current HepMCEventMap:";
119  _geneventmap->identify();
120  exit(11);
121  }
122 
123  genevent->moveVertex(
124  vtx_evt->get_collision_vertex().x(),
125  vtx_evt->get_collision_vertex().y(),
126  vtx_evt->get_collision_vertex().z(),
127  vtx_evt->get_collision_vertex().t());
128  }
129 
130  genevent->moveVertex(
131  (smear(_vertex_x, _vertex_width_x, _vertex_func_x)),
132  (smear(_vertex_y, _vertex_width_y, _vertex_func_y)),
133  (smear(_vertex_z, _vertex_width_z, _vertex_func_z)),
134  (smear(_vertex_t, _vertex_width_t, _vertex_func_t)));
135 }
136 
138 {
139  _vertex_func_x = x;
140  _vertex_func_y = y;
141  _vertex_func_z = z;
142  _vertex_func_t = t;
143  return;
144 }
145 
146 void PHHepMCGenHelper::set_vertex_distribution_mean(const double x, const double y, const double z, const double t)
147 {
148  _vertex_x = x;
149  _vertex_y = y;
150  _vertex_z = z;
151  _vertex_t = t;
152  return;
153 }
154 
155 void PHHepMCGenHelper::set_vertex_distribution_width(const double x, const double y, const double z, const double t)
156 {
157  _vertex_width_x = x;
158  _vertex_width_y = y;
159  _vertex_width_z = z;
160  _vertex_width_t = t;
161  return;
162 }
163 
164 double PHHepMCGenHelper::smear(const double position,
165  const double width,
166  VTXFUNC dist) const
167 {
168  assert(width >= 0);
169 
170  double res = position;
171 
172  if (width == 0)
173  return res;
174 
175  if (dist == Uniform)
176  {
177  res = (position - width) + 2 * gsl_rng_uniform_pos(RandomGenerator) * width;
178  }
179  else if (dist == Gaus)
180  {
181  res = position + gsl_ran_gaussian(RandomGenerator, width);
182  }
183  else
184  {
185  cout << "PHHepMCGenHelper::smear - FATAL Error - unknown vertex function " << dist << endl;
186  exit(10);
187  }
188  return res;
189 }
PHHepMCGenEventMap is collection of HEPMC events input into this simulation map of embedding ID -&gt; PH...
PHNode * findFirst(const std::string &, const std::string &)
uniform distribution with half width set via set_vertex_distribution_width()
#define PHWHERE
Definition: phool.h:23
bool addEvent(HepMC::GenEvent *evt)
host an HepMC event
virtual void moveVertex(double x, double y, double z, double t=0)
move the collision vertex position in the Hall coordinate system, use PHENIX units of cm...
PHHepMCGenEvent * insert_event(HepMC::GenEvent *evt)
send HepMC::GenEvent to DST tree. This function takes ownership of evt
const PHHepMCGenEvent * get(int idkey) const
fetch event
gsl_rng * RandomGenerator
const HepMC::FourVector & get_collision_vertex() const
collision vertex position in the Hall coordinate system, use PHENIX units of cm, ns ...
void set_vertex_distribution_mean(const double x, const double y, const double z, const double t)
set the mean value of the vertex distribution, use PHENIX units of cm, ns
void set_vertex_distribution_function(VTXFUNC x, VTXFUNC y, VTXFUNC z, VTXFUNC t)
toss a new vertex according to a Uniform or Gaus distribution
void move_vertex(PHHepMCGenEvent *genevent)
move vertex according to vertex settings
void identify(std::ostream &os=std::cout) const
PHHepMCGenEvent * insert_event(const int embedding_id, const PHHepMCGenEvent *event=nullptr)
insert a event with specific embedding ID
void set_vertex_distribution_width(const double x, const double y, const double z, const double t)
set the width of the vertex distribution function about the mean, use PHENIX units of cm...
VTXFUNC
supported function distributions
normal distribution with sigma width set via set_vertex_distribution_width()
int create_node_tree(PHCompositeNode *topNode)
init interface nodes
double smear(const double position, const double width, VTXFUNC dist) const
virtual ~PHHepMCGenHelper()