HepMC3 event record library
ReaderuprootTree.cc
1 #include "ReaderuprootTree.h"
2 namespace HepMC3
3 {
4 
5 HEPMC3_DECLARE_READER_FILE(ReaderuprootTree)
6 
7 /// @brief obtain vector of objects using name and type
8 template <class T> std::vector<T>
9 ReaderuprootTree::get_vector(PyObject * file_name, const std::string& array_name, std::string desired_type) {
10  int i = m_events_count;
11  PyObject *pFunc = m_access_function;
12  PyObject * pArgs = PyTuple_New(4);
13  PyTuple_SetItem(pArgs, 0, file_name);
14  PyTuple_SetItem(pArgs, 1, Py_BuildValue("s#", array_name.c_str(), array_name.length()));
15  PyTuple_SetItem(pArgs, 2, Py_BuildValue("i", i));
16  PyTuple_SetItem(pArgs, 3, Py_BuildValue("s#", desired_type.c_str(), desired_type.length()));
17  PyObject *pReturn = PyObject_CallObject(pFunc, pArgs);
18  PyArrayObject *np_ret = reinterpret_cast<PyArrayObject*>(pReturn);
19  std::vector<T> out;
20  int len0 = 0;
21  if (np_ret) len0 = PyArray_SHAPE(np_ret)[0];
22  if (len0 > 0) {
23  int len = PyArray_SHAPE(np_ret)[1];
24  T* c_out = reinterpret_cast<T*>(PyArray_DATA(np_ret));
25  for (int i = 0; i < len; i++) out.push_back(c_out[i]);
26  }
27  Py_DECREF(pArgs);
28  if (np_ret) Py_DECREF(np_ret);
29  return out;
30 }
31 
32 /// @brief obtain vector of objects using name and type, specified for std::string
33 template <>
34 std::vector<std::string>
35 ReaderuprootTree::get_vector<std::string>(PyObject * file_name, const std::string& array_name, std::string desired_type) {
36  if (desired_type.length() == 0) desired_type = "U500";
37  int i = m_events_count;
38  PyObject *pFunc = m_access_function;
39  PyObject * pArgs = PyTuple_New(4);
40  PyTuple_SetItem(pArgs, 0, file_name);
41  PyTuple_SetItem(pArgs, 1, Py_BuildValue("s#", array_name.c_str(), array_name.length()));
42  PyTuple_SetItem(pArgs, 2, Py_BuildValue("i", i));
43  PyTuple_SetItem(pArgs, 3, Py_BuildValue("s#", desired_type.c_str(), desired_type.length()));
44 
45  PyObject *pReturn = PyObject_CallObject(pFunc, pArgs);
46  PyArrayObject *np_ret = reinterpret_cast<PyArrayObject*>(pReturn);
47  std::vector<std::string> out;
48  int len0 = 0;
49  if (np_ret) len0 = PyArray_SHAPE(np_ret)[0];
50  if (len0>0) {
51  int len = PyArray_SHAPE(np_ret)[1];
52  typedef wchar_t wc500[500];
53  wc500* c_out = reinterpret_cast<wc500*>(PyArray_DATA(np_ret));
54 
55  for (int i = 0; i < len; i++) {
56  std::wstring wa((c_out[i]));
57  std::string ret(wa.begin(), wa.end() );
58  out.push_back(ret);
59  }
60  }
61  Py_DECREF(pArgs);
62  if (np_ret) Py_DECREF(np_ret);
63  return out;
64 }
65 
66 PyObject* ReaderuprootTree::get_function(PyObject* m_python_module, const std::string& name)
67 {
68  if (!m_python_module) return nullptr;
69  PyObject* pFuncInitFile = PyObject_GetAttrString(m_python_module, name.c_str());
70  if (!pFuncInitFile || !PyCallable_Check(pFuncInitFile)) {
71  Py_XDECREF(pFuncInitFile);
72  std::cout << name<<"is null or not callable" << std::endl;
73  return nullptr;
74  }
75  return pFuncInitFile;
76 }
77 
78 
79 PyObject* ReaderuprootTree::init_python_module(const std::string& code)
80 {
81  const char *SomeModuleName = "uproot4forhepmc3";
82  const char *SomeModuleCode = code.c_str();
83  PyObject *m_python_module = PyModule_New(SomeModuleName);
84  PyModule_AddStringConstant(m_python_module, "__file__", "");
85  PyObject *localDict = PyModule_GetDict(m_python_module);
86  PyObject *builtins = PyEval_GetBuiltins();
87  PyDict_SetItemString(localDict, "__builtins__", builtins);
88 
89  PyObject *pyValue = PyRun_String(SomeModuleCode, Py_file_input, localDict, localDict);
90  if (pyValue == nullptr) {
91  return nullptr;
92  }
93  else
94  {
95  Py_DECREF(pyValue);
96  }
97  return m_python_module;
98 }
99 
100 ReaderuprootTree::ReaderuprootTree(const std::string &filename,const std::string &treename,const std::string &branchname):
101  m_events_count(0),m_tree_name(treename.c_str()), m_branch_name(branchname.c_str()),m_tree(nullptr)
102 {
103  if (!init(filename)) return;
104 }
105 
106 bool ReaderuprootTree::init(const std::string &filename)
107 {
108 
109  m_event_data = new GenEventData();
110 
112 
113  set_run_info(std::make_shared<GenRunInfo>());
114 
115  Py_Initialize();
116  import_array()
117 
119 
120  R"EOT(
121 import uproot
122 import numpy
123 def init_file(filename):
124  rootfile=uproot.open(str(filename))
125 # print(rootfile.keys())
126  return rootfile
127 
128 def close_file(filename):
129  return filename.close()
130 
131 def init_tree(rootfile,treename,branchname):
132  tree=rootfile[str(treename)+"/"+str(branchname)]
133 # print(tree.keys())
134  return tree
135 
136 def init_genruninfo(rootfile,treename,branchname):
137  tree=rootfile[str(treename)+"/"+str(branchname)]
138 # print(tree.keys())
139  return tree
140 
141 def get_number_of_entries_in_tree(tree):
142  return tree.num_entries
143 
144 def get_array_from_tree(tree,branch,i,destype):
145  result=tree[str(branch)].array(library="np")[i]
146  if len(destype.strip()) == 0:
147  output=numpy.array([result])
148  else:
149  output=numpy.array([result], dtype=destype)
150 # print("a.shape={}, a.dtype={}".format(output.shape, output.dtype))
151 # print(branch,output)
152  return output
153 )EOT"
154 );
155  bool result =false;
156  if (!m_python_module) {
157  HEPMC3_ERROR( "ReaderuprootTree: cannot initialize python modulr. Please check your uproot and/or numpy instalation.")
158  return result;
159  }
160  PyObject *pFuncInitFile = nullptr;
161  PyObject *pFuncInitTree = nullptr;
162  PyObject* pFuncEntries = nullptr;
163 
164 
165  PyObject * pArgsFile = nullptr;
166  PyObject * pArgsTree = nullptr;
167  PyObject * pArgsEntries = nullptr;
168  PyObject * pArgsGenRunInfo = nullptr;
169 
170  m_access_function = get_function(m_python_module, "get_array_from_tree");
171  pFuncInitFile = get_function(m_python_module, "init_file");
172  pFuncInitTree = get_function(m_python_module, "init_tree");
173  pFuncEntries = get_function(m_python_module, "get_number_of_entries_in_tree");
174 
175 
176 
177 if (m_access_function && pFuncInitFile && pFuncInitTree && pFuncEntries) {
178  pArgsFile = PyTuple_New(1);
179  PyTuple_SetItem(pArgsFile, 0, Py_BuildValue("s#", filename.c_str(), filename.length()));
180  m_file=PyObject_CallObject(pFuncInitFile,pArgsFile);
181 
182 
183  if (m_file){
184  pArgsTree = PyTuple_New(3);
185  PyTuple_SetItem(pArgsTree, 0, m_file);
186  PyTuple_SetItem(pArgsTree, 1, Py_BuildValue("s#", m_tree_name.c_str(), m_tree_name.length()));
187  PyTuple_SetItem(pArgsTree, 2, Py_BuildValue("s#", m_branch_name.c_str(), m_branch_name.length()));
188  m_tree = PyObject_CallObject(pFuncInitTree, pArgsTree);
189 
190  pArgsGenRunInfo = PyTuple_New(3);
191  PyTuple_SetItem(pArgsGenRunInfo, 0, m_file);
192  PyTuple_SetItem(pArgsGenRunInfo, 1, Py_BuildValue("s#", m_tree_name.c_str(), m_tree_name.length()));
193  PyTuple_SetItem(pArgsGenRunInfo, 2, Py_BuildValue("s#", "GenRunInfo", 10));
194  m_genruninfo = PyObject_CallObject(pFuncInitTree, pArgsGenRunInfo);
195  }
196 
197  if (m_tree){
198  m_tree_getEntries = 0;
199  pArgsEntries = PyTuple_New(1);
200  PyTuple_SetItem(pArgsEntries, 0, m_tree);
201  PyObject * ret = PyObject_CallObject(pFuncEntries,pArgsEntries);
202  m_tree_getEntries = PyLong_AsLong(ret);
203  Py_DECREF(ret);
204  }
205  if (m_tree && m_file && m_genruninfo && m_tree_getEntries) result=true;
206 }
207 
208  Py_DECREF(pFuncInitFile);
209  Py_DECREF(pFuncInitTree);
210  Py_DECREF(pArgsEntries);
211 
212  Py_DECREF(pFuncEntries);
213  Py_DECREF(pArgsFile);
214  return result;
215 }
216 
217 bool ReaderuprootTree::skip(const int n)
218 {
219  m_events_count+=n;
220  if (m_events_count>m_tree_getEntries) return false;
221  return true;
222 }
223 
224 
225 
227 {
228  if (!m_python_module) return false;
229  if (m_events_count >= m_tree_getEntries) { m_events_count++; return false;}
230  m_event_data->particles.clear();
231  m_event_data->vertices.clear();
232  m_event_data->links1.clear();
233  m_event_data->links2.clear();
234  m_event_data->attribute_id.clear();
235  m_event_data->attribute_name.clear();
237 
238  auto event_number_v = get_vector<int>(m_tree, "event_number");
239  if (event_number_v.size() == 0) { m_events_count++; return false;}
240  auto weights = get_vector<double>(m_tree, "weights");
241  auto event_pos_1_v = get_vector<double>(m_tree, "event_pos/event_pos.m_v1");
242  if (event_pos_1_v.size() == 0) { m_events_count++; return false;}
243  auto event_pos_2_v = get_vector<double>(m_tree,"event_pos/event_pos.m_v2");
244  if (event_pos_2_v.size() == 0) { m_events_count++; return false;}
245  auto event_pos_3_v = get_vector<double>(m_tree, "event_pos/event_pos.m_v3");
246  if (event_pos_3_v.size() == 0) { m_events_count++; return false;}
247  auto event_pos_4_v = get_vector<double>(m_tree, "event_pos/event_pos.m_v4");
248  if (event_pos_4_v.size() == 0) { m_events_count++; return false;}
249  auto momentum_unit_v = get_vector<int>(m_tree, "momentum_unit");
250  if (momentum_unit_v.size() == 0) { m_events_count++; return false;}
251  auto length_unit_v = get_vector<int>(m_tree, "length_unit");
252  if (length_unit_v.size() == 0) { m_events_count++; return false;}
253 
254  auto event_number = event_number_v.at(0);
255  auto event_pos_1 = event_pos_1_v.at(0);
256  auto event_pos_2 = event_pos_2_v.at(0);
257  auto event_pos_3 = event_pos_3_v.at(0);
258  auto event_pos_4 = event_pos_4_v.at(0);
259  auto momentum_unit = momentum_unit_v.at(0);
260  auto length_unit = length_unit_v.at(0);
261 
262  auto links1 = get_vector<int>(m_tree, "links1");
263  auto links2 = get_vector<int>(m_tree, "links2");
264  auto attribute_id = get_vector<int>(m_tree, "attribute_id");
265  auto attribute_name = get_vector<std::string>(m_tree, "attribute_name");
266  auto attribute_string = get_vector<std::string>(m_tree, "attribute_string");
267  auto particlesmomentumm_v1 = get_vector<double>(m_tree, "particles/particles.momentum.m_v1");
268  auto particlesmomentumm_v2 = get_vector<double>(m_tree, "particles/particles.momentum.m_v2");
269  auto particlesmomentumm_v3 = get_vector<double>(m_tree, "particles/particles.momentum.m_v3");
270  auto particlesmomentumm_v4 = get_vector<double>(m_tree, "particles/particles.momentum.m_v4");
271  auto particlesmass = get_vector<double>(m_tree, "particles/particles.mass");
272  auto particlesis_mass_set = get_vector<bool>(m_tree, "particles/particles.is_mass_set");
273  auto particlesparticlespid = get_vector<int>(m_tree, "particles/particles.pid");
274  auto particlesparticlesstatus = get_vector<int>(m_tree, "particles/particles.status");
275 
276  auto verticespositionm_v1 = get_vector<double>(m_tree, "vertices/vertices.position.m_v1");
277  auto verticespositionm_v2 = get_vector<double>(m_tree, "vertices/vertices.position.m_v2");
278  auto verticespositionm_v3 = get_vector<double>(m_tree, "vertices/vertices.position.m_v3");
279  auto verticespositionm_v4 = get_vector<double>(m_tree, "vertices/vertices.position.m_v4");
280  auto verticesverticesstatus = get_vector<int>(m_tree, "vertices/vertices.status");
281 
282  m_event_data->event_number = event_number;
283  m_event_data->momentum_unit = momentum_unit == 0?HepMC3::Units::MEV:HepMC3::Units::GEV;
284  m_event_data->length_unit = length_unit == 0?HepMC3::Units::MM:HepMC3::Units::CM;
285  m_event_data->event_pos = HepMC3::FourVector(event_pos_1, event_pos_2, event_pos_3, event_pos_4) ;
286  m_event_data->links1 = links1;
287  m_event_data->links2 = links2;
288 
289  for (size_t k=0; k < particlesparticlespid.size(); k++)
290  {
291  HepMC3::GenParticleData p = { particlesparticlespid[k], particlesparticlesstatus[k], particlesis_mass_set[k], particlesmass[k],
292  HepMC3::FourVector(particlesmomentumm_v1[k], particlesmomentumm_v1[k], particlesmomentumm_v1[k], particlesmomentumm_v1[k])
293  };
294  m_event_data->particles.push_back(p);
295  }
296 
297  for (size_t k=0; k < verticesverticesstatus.size(); k++)
298  {
299  HepMC3::GenVertexData v = { verticesverticesstatus[k], HepMC3::FourVector(verticespositionm_v1[k], verticespositionm_v2[k], verticespositionm_v3[k], verticespositionm_v4[k])};
300  m_event_data->vertices.push_back(v);
301  }
302  m_event_data->weights=weights;
303  m_event_data->attribute_id=attribute_id;
304  m_event_data->attribute_name=attribute_name;
305  m_event_data->attribute_string=attribute_string;
306  evt.read_data(*m_event_data);
307 
308  m_run_info_data->weight_names.clear();
309  m_run_info_data->tool_name.clear();
310  m_run_info_data->tool_version.clear();
314 
315  m_run_info_data->weight_names = get_vector<std::string>(m_genruninfo, "weight_names");
316  m_run_info_data->tool_name = get_vector<std::string>(m_genruninfo, "tool_name");
317  m_run_info_data->tool_version = get_vector<std::string>(m_genruninfo, "tool_version");
318  m_run_info_data->tool_description = get_vector<std::string>(m_genruninfo, "tool_description");
319  m_run_info_data->attribute_name = get_vector<std::string>(m_genruninfo, "attribute_name");
320  m_run_info_data->attribute_string = get_vector<std::string>(m_genruninfo, "attribute_string");
321 
322  run_info()->read_data(*m_run_info_data);
323  evt.set_run_info(run_info());
324  m_events_count++;
325  return true;
326 }
327 
329 {
330  Py_DECREF(m_genruninfo);
331  Py_DECREF(m_tree);
332  Py_DECREF(m_file); //This should close the file, right?
333  Py_DECREF(m_access_function);
334  Py_DECREF(m_python_module);
335 
336  m_file = nullptr;
337  m_tree = nullptr;
338  m_genruninfo = nullptr;
339  m_access_function = nullptr;
340  m_python_module = nullptr;
341  Py_DECREF( PyImport_ImportModule("threading")); //If someone, at some point would document it in CPython...
342  Py_Finalize();
343 }
344 
346 {
347  if (!m_python_module) return true;
348  if (m_events_count >= m_tree_getEntries) return true;
349  return false;
350 }
351 ReaderuprootTree::~ReaderuprootTree()
352 {
353  if (m_event_data) {delete m_event_data; m_event_data=nullptr;}
354  if (m_run_info_data) {delete m_run_info_data; m_run_info_data=nullptr;}
355 }
356 
357 } // namespace HepMC3
358 
359 
HepMC3 main namespace.
long int m_tree_getEntries
number of processed events
std::vector< T > get_vector(PyObject *file_name, const std::string &array_name, std::string desired_type="")
Get arrays.
std::vector< int > attribute_id
Attribute owner id.
Definition: GenEventData.h:54
PyObject * init_python_module(const std::string &)
Init python module.
std::vector< std::string > attribute_name
Attribute name.
std::vector< int > links2
Second id of the vertex links.
Definition: GenEventData.h:52
std::string m_tree_name
Name of TTree.
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
bool failed() override
Get file error state.
int event_number
Event number.
Definition: GenEventData.h:27
PyObject * get_function(PyObject *, const std::string &)
Get python functions.
PyObject * m_access_function
Python access function for arrays.
Units::MomentumUnit momentum_unit
Momentum unit.
Definition: GenEventData.h:28
int m_events_count
Events count. Needed to read the tree.
GenEventData * m_event_data
Pointer to structure that holds event data.
std::vector< int > links1
First id of the vertex links.
Definition: GenEventData.h:51
FourVector event_pos
Event position.
Definition: GenEventData.h:35
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition: GenEventData.h:56
std::string m_branch_name
Name of TBranch in TTree.
Stores event-related information.
Definition: GenEvent.h:41
Stores serializable event information.
Definition: GenEventData.h:26
Generic 4-vector.
Definition: FourVector.h:36
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< std::string > tool_name
Tool names.
std::vector< std::string > attribute_name
Attribute name.
Definition: GenEventData.h:55
bool read_event(GenEvent &evt) override
Read event from file.
Stores serializable vertex information.
Definition: GenVertexData.h:22
Stores serializable run information.
Stores serializable particle information.
bool skip(const int) override
skip events
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition: GenEvent.cc:690
std::vector< GenParticleData > particles
Particles.
Definition: GenEventData.h:31
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Reader.h:44
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:141
PyObject * m_file
Python file handler.
void close() override
Close file.
PyObject * m_genruninfo
Python runInfo handler.
std::vector< double > weights
Weights.
Definition: GenEventData.h:33
std::vector< GenVertexData > vertices
Vertices.
Definition: GenEventData.h:32
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Reader.h:64
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:24
std::vector< std::string > tool_version
Tool versions.
bool init(const std::string &filename)
init routine
std::vector< std::string > weight_names
Weight names.
PyObject * m_python_module
Python module.
std::vector< std::string > tool_description
Tool descriptions.
ReaderuprootTree(const std::string &filename, const std::string &treename="hepmc3_tree", const std::string &branchname="hepmc3_event")
Constructor with tree and branch names.
Units::LengthUnit length_unit
Length unit.
Definition: GenEventData.h:29
PyObject * m_tree
Python tree handler.