6#include "ReaderuprootTree.h" 
   13template <
class T> std::vector<T>
 
   17    PyObject * pArgs = PyTuple_New(4);
 
   18    PyTuple_SetItem(pArgs, 0, file_name);
 
   19    PyTuple_SetItem(pArgs, 1, Py_BuildValue(
"s#", array_name.c_str(), array_name.length()));
 
   20    PyTuple_SetItem(pArgs, 2, Py_BuildValue(
"i", i));
 
   21    PyTuple_SetItem(pArgs, 3, Py_BuildValue(
"s#", desired_type.c_str(), desired_type.length()));
 
   22    PyObject *pReturn = PyObject_CallObject(pFunc, pArgs);
 
   23    auto *np_ret = 
reinterpret_cast<PyArrayObject*
>(pReturn);
 
   26    if (np_ret) len0 = PyArray_SHAPE(np_ret)[0];
 
   28        int len = PyArray_SHAPE(np_ret)[1];
 
   29        T*  c_out = 
reinterpret_cast<T*
>(PyArray_DATA(np_ret));
 
   30        for (
int i = 0; i < len; i++) out.push_back(c_out[i]);
 
   33    if (np_ret) Py_DECREF(np_ret);
 
 
   39std::vector<std::string>
 
   41    if (desired_type.length() == 0) desired_type = 
"U500";
 
   42    int i = m_events_count;
 
   43    PyObject *pFunc = m_access_function;
 
   44    PyObject * pArgs = PyTuple_New(4);
 
   45    PyTuple_SetItem(pArgs, 0, file_name);
 
   46    PyTuple_SetItem(pArgs, 1, Py_BuildValue(
"s#", array_name.c_str(), array_name.length()));
 
   47    PyTuple_SetItem(pArgs, 2, Py_BuildValue(
"i", i));
 
   48    PyTuple_SetItem(pArgs, 3, Py_BuildValue(
"s#", desired_type.c_str(), desired_type.length()));
 
   50    PyObject *pReturn = PyObject_CallObject(pFunc, pArgs);
 
   51    auto *np_ret = 
reinterpret_cast<PyArrayObject*
>(pReturn);
 
   52    std::vector<std::string> out;
 
   54    if (np_ret) len0 = PyArray_SHAPE(np_ret)[0];
 
   56        int len = PyArray_SHAPE(np_ret)[1];
 
   57        using  wc500 = 
wchar_t[500];
 
   58        auto* c_out = 
reinterpret_cast<wc500*
>(PyArray_DATA(np_ret));
 
   60        for (
int i = 0; i < len; i++) {
 
   61            std::wstring wa((c_out[i]));
 
   62            std::string ret(wa.begin(), wa.end() );
 
   67    if (np_ret) Py_DECREF(np_ret);
 
   74    PyObject* pFuncInitFile = PyObject_GetAttrString(
m_python_module, name.c_str());
 
   75    if (!pFuncInitFile || !PyCallable_Check(pFuncInitFile)) {
 
   76        Py_XDECREF(pFuncInitFile);
 
   77        std::cout << name<<
"is null or not callable" << std::endl;
 
 
   86    const char *SomeModuleName = 
"uproot4forhepmc3";
 
   87    const char *SomeModuleCode = code.c_str();
 
   91    PyObject *builtins = PyEval_GetBuiltins();
 
   92    PyDict_SetItemString(localDict, 
"__builtins__", builtins);
 
   94    PyObject *pyValue = PyRun_String(SomeModuleCode, Py_file_input, localDict, localDict);
 
   95    if (pyValue == 
nullptr) {
 
 
  105    if (!
init(filename)) 
return;
 
 
  125def init_file(filename): 
  126    rootfile=uproot.open(str(filename)) 
  127#    print(rootfile.keys()) 
  130def close_file(filename): 
  131    return filename.close() 
  133def init_tree(rootfile,treename,branchname): 
  134    tree=rootfile[str(treename)+"/"+str(branchname)] 
  138def init_genruninfo(rootfile,treename,branchname): 
  139    tree=rootfile[str(treename)+"/"+str(branchname)] 
  143def get_number_of_entries_in_tree(tree): 
  144    return tree.num_entries 
  146def get_array_from_tree(tree,branch,i,destype): 
  147    result=tree[str(branch)].array(library="np")[i] 
  148    if len(destype.strip()) == 0: 
  149     output=numpy.array([result]) 
  151     output=numpy.array([result], dtype=destype) 
  152#    print("a.shape={}, a.dtype={}".format(output.shape, output.dtype)) 
  153#    print(branch,output) 
  159     HEPMC3_ERROR( 
"ReaderuprootTree: cannot initialize python modulr. Please check your uproot and/or numpy instalation.")
 
  162     PyObject *pFuncInitFile = 
nullptr;
 
  163     PyObject *pFuncInitTree = 
nullptr;
 
  164     PyObject* pFuncEntries = 
nullptr;
 
  167     PyObject * pArgsFile = 
nullptr;
 
  168     PyObject * pArgsTree = 
nullptr;
 
  169     PyObject * pArgsEntries = 
nullptr;
 
  170     PyObject * pArgsGenRunInfo = 
nullptr;
 
  180    pArgsFile = PyTuple_New(1);
 
  181    PyTuple_SetItem(pArgsFile, 0, Py_BuildValue(
"s#", filename.c_str(), filename.length()));
 
  182    m_file=PyObject_CallObject(pFuncInitFile,pArgsFile);
 
 
  186    pArgsTree = PyTuple_New(3);
 
  187    PyTuple_SetItem(pArgsTree, 0, 
m_file);
 
  190    m_tree = PyObject_CallObject(pFuncInitTree, pArgsTree);
 
 
  192    pArgsGenRunInfo = PyTuple_New(3);
 
  193    PyTuple_SetItem(pArgsGenRunInfo, 0, 
m_file);
 
  195    PyTuple_SetItem(pArgsGenRunInfo, 2, Py_BuildValue(
"s#", 
"GenRunInfo", 10));
 
  196    m_genruninfo = PyObject_CallObject(pFuncInitTree, pArgsGenRunInfo);
 
  201    pArgsEntries = PyTuple_New(1);
 
  202    PyTuple_SetItem(pArgsEntries, 0, 
m_tree);
 
  203    PyObject * ret = PyObject_CallObject(pFuncEntries,pArgsEntries);
 
  210    Py_DECREF(pFuncInitFile);
 
  211    Py_DECREF(pFuncInitTree);
 
  212    Py_DECREF(pArgsEntries);
 
  214    Py_DECREF(pFuncEntries);
 
  215    Py_DECREF(pArgsFile);
 
  255    auto event_number    = event_number_v.at(0);
 
  256    auto event_pos_1     = event_pos_1_v.at(0);
 
  257    auto event_pos_2     = event_pos_2_v.at(0);
 
  258    auto event_pos_3     = event_pos_3_v.at(0);
 
  259    auto event_pos_4     = event_pos_4_v.at(0);
 
  260    auto momentum_unit   = momentum_unit_v.at(0);
 
  261    auto length_unit     = length_unit_v.at(0);
 
  284    m_event_data->momentum_unit = momentum_unit == 0?HepMC3::Units::MEV:HepMC3::Units::GEV;
 
  285    m_event_data->length_unit = length_unit == 0?HepMC3::Units::MM:HepMC3::Units::CM;
 
  286    m_event_data->event_pos = HepMC3::FourVector(event_pos_1, event_pos_2, event_pos_3, event_pos_4) ;
 
  290    for (
size_t k=0; k < particlesparticlespid.size(); k++)
 
  292        HepMC3::GenParticleData p = { particlesparticlespid[k], particlesparticlesstatus[k], particlesis_mass_set[k], particlesmass[k],
 
  293                                     HepMC3::FourVector(particlesmomentumm_v1[k], particlesmomentumm_v1[k], particlesmomentumm_v1[k], particlesmomentumm_v1[k])
 
 
  298    for (
size_t k=0; k < verticesverticesstatus.size(); k++)
 
 
  342    Py_DECREF( PyImport_ImportModule(
"threading")); 
 
  352ReaderuprootTree::~ReaderuprootTree()
 
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Stores event-related information.
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
ReaderuprootTree(const std::string &filename, const std::string &treename="hepmc3_tree", const std::string &branchname="hepmc3_event")
Constructor with tree and branch names.
PyObject * m_genruninfo
Python runInfo handler.
bool read_event(GenEvent &evt) override
Read event from file.
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.
PyObject * m_access_function
Python access function for arrays.
bool failed() override
Get file error state.
bool skip(const int) override
skip events
PyObject * m_tree
Python tree handler.
bool init(const std::string &filename)
init routine
GenEventData * m_event_data
Pointer to structure that holds event data.
int m_events_count
Events count. Needed to read the tree.
void close() override
Close file.
std::string m_tree_name
Name of TTree.
PyObject * get_function(PyObject *, const std::string &)
Get python functions.
std::string m_branch_name
Name of TBranch in TTree.
PyObject * init_python_module(const std::string &)
Init python module.
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
PyObject * m_python_module
Python module.
PyObject * m_file
Python file handler.
std::vector< std::string > ReaderuprootTree::get_vector< std::string >(PyObject *file_name, const std::string &array_name, std::string desired_type)
obtain vector of objects using name and type, specified for std::string
Stores serializable event information.
std::vector< GenVertexData > vertices
Vertices.
Stores serializable run information.
Stores serializable vertex information.