Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
PyROOTHelpers.hxx
Go to the documentation of this file.
1 // Author: Danilo Piparo, Enrico Guiraud, Stefan Wunsch CERN 04/2018
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #ifndef ROOT_PyROOTHelpers
12 #define ROOT_PyROOTHelpers
13 
14 #include "ROOT/RDataFrame.hxx"
15 
16 #include <vector>
17 #include <string>
18 #include <utility>
19 
20 namespace ROOT {
21 namespace Internal {
22 namespace RDF {
23 
24 template <typename dtype>
25 ULong64_t GetVectorAddress(std::vector<dtype> &p)
26 {
27  return reinterpret_cast<ULong64_t>(&p);
28 }
29 
30 inline ULong64_t GetAddress(std::vector<std::string> &p)
31 {
32  return reinterpret_cast<ULong64_t>(&p);
33 }
34 inline ULong64_t GetAddress(TTree &p)
35 {
36  return reinterpret_cast<ULong64_t>(&p);
37 }
38 
39 template <typename BufType, typename... ColTypes, std::size_t... Idx>
40 void TTreeAsFlatMatrix(std::index_sequence<Idx...>, TTree &tree, std::vector<BufType> &matrix,
41  std::vector<std::string> &columns)
42 {
43  auto buffer = matrix.data();
44 
45  auto fillMatrix = [buffer](ColTypes... cols, ULong64_t entry) {
46  int expander[] = {(buffer[entry * sizeof...(Idx) + Idx] = cols, 0)...};
47  (void)expander;
48  };
49 
50  auto columnsWithEntry = columns;
51  columnsWithEntry.emplace_back("tdfentry_");
52 
53  ROOT::RDataFrame dataframe(tree, columns);
54  dataframe.Foreach(fillMatrix, columnsWithEntry);
55 }
56 
57 template <typename BufType, typename... ColTypes>
58 void TTreeAsFlatMatrixHelper(TTree &tree, std::vector<BufType> &matrix, std::vector<std::string> &columns)
59 {
60  TTreeAsFlatMatrix<BufType, ColTypes...>(std::index_sequence_for<ColTypes...>(), tree, matrix, columns);
61 }
62 
63 // RDataFrame.AsNumpy helpers
64 
65 // NOTE: This is a workaround for the missing Take action in the PyROOT interface
66 template <typename T>
67 ROOT::RDF::RResultPtr<std::vector<T>> RDataFrameTake(ROOT::RDF::RNode df, std::string_view column)
68 {
69  return df.Take<T>(column);
70 }
71 
72 } // namespace RDF
73 } // namespace Internal
74 } // namespace ROOT
75 
76 #endif