Logo ROOT   6.14/05
Reference Guide
TTreeAsFlatMatrix.h
Go to the documentation of this file.
1 // Author: Danilo Piparo, Enrico Guiraud, Stefan Wunsch CERN 04/2018
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2016, 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_TTreeAsFlatMatrix
12 #define ROOT_TTreeAsFlatMatrix
13 
14 #include <vector>
15 #include <string>
16 #include "TTree.h"
17 #include "ROOT/RDataFrame.hxx"
18 #include "Rtypes.h"
19 #include <utility>
20 
21 namespace PyROOT {
22  template <typename dtype>
23  ULong64_t GetVectorAddress(std::vector<dtype> &p){return reinterpret_cast<ULong64_t>(&p);}
24 
25  inline ULong64_t GetAddress(std::vector<std::string> &p){return reinterpret_cast<ULong64_t>(&p);}
26  inline ULong64_t GetAddress(TTree &p){return reinterpret_cast<ULong64_t>(&p);}
27 
28  template <typename BufType, typename... ColTypes, std::size_t... Idx>
30  std::index_sequence<Idx...>, TTree& tree, std::vector<BufType>& matrix, std::vector<std::string>& columns)
31  {
32  auto buffer = matrix.data();
33 
34  auto fillMatrix = [buffer](ColTypes... cols, ULong64_t entry) {
35  int expander[] = { (buffer[entry * sizeof...(Idx) + Idx] = cols, 0)... };
36  (void)expander;
37  };
38 
39  auto columnsWithEntry = columns;
40  columnsWithEntry.emplace_back("tdfentry_");
41 
42  ROOT::RDataFrame dataframe(tree, columns);
43  dataframe.Foreach(fillMatrix, columnsWithEntry);
44  }
45 
46  template <typename BufType, typename... ColTypes>
47  void TTreeAsFlatMatrixHelper(TTree& tree, std::vector<BufType>& matrix, std::vector<std::string>& columns)
48  {
49  TTreeAsFlatMatrix<BufType, ColTypes...>(std::index_sequence_for<ColTypes...>(), tree, matrix, columns);
50  }
51 }
52 
53 #endif
ULong64_t GetVectorAddress(std::vector< dtype > &p)
void Foreach(F f, const ColumnNames_t &columns={})
Execute a user-defined function on each entry (instant action)
ULong64_t GetAddress(std::vector< std::string > &p)
ROOT&#39;s RDataFrame offers a high level interface for analyses of data stored in TTrees, CSV&#39;s and other data formats.
Definition: RDataFrame.hxx:42
unsigned long long ULong64_t
Definition: RtypesCore.h:70
make_index_sequence< sizeof...(_Tp)> index_sequence_for
typedef void((*Func_t)())
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:70
void TTreeAsFlatMatrix(std::index_sequence< Idx... >, TTree &tree, std::vector< BufType > &matrix, std::vector< std::string > &columns)
void TTreeAsFlatMatrixHelper(TTree &tree, std::vector< BufType > &matrix, std::vector< std::string > &columns)