Logo ROOT   6.10/09
Reference Guide
TDataFrame.hxx
Go to the documentation of this file.
1 // Author: Enrico Guiraud, Danilo Piparo CERN 12/2016
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 /**
12  \defgroup dataframe Data Frame
13 The ROOT Data Frame allows to analyse data stored in TTrees with a high level interface.
14 */
15 
16 #ifndef ROOT_TDATAFRAME
17 #define ROOT_TDATAFRAME
18 
19 #include "ROOT/TDFInterface.hxx"
20 #include "ROOT/TDFNodes.hxx"
21 #include "ROOT/TDFUtils.hxx"
22 #include "TChain.h"
23 
24 #include <memory>
25 #include <iosfwd> // std::ostringstream
26 #include <stdexcept>
27 #include <string>
28 class TDirectory;
29 class TTree;
30 
31 namespace ROOT {
32 namespace Experimental {
33 namespace TDFDetail = ROOT::Detail::TDF;
35 
36 class TDataFrame : public TDF::TInterface<TDFDetail::TLoopManager> {
37  using ColumnNames_t = TDFDetail::ColumnNames_t;
38 
39 public:
40  TDataFrame(std::string_view treeName, std::string_view filenameglob, const ColumnNames_t &defaultBranches = {});
41  ////////////////////////////////////////////////////////////////////////////
42  /// \brief Build the dataframe
43  /// \tparam FILENAMESCOLL The type of the file collection: only requirement: must have begin and end.
44  /// \param[in] treeName Name of the tree contained in the directory
45  /// \param[in] filenamescoll Collection of file names, for example a list of strings.
46  /// \param[in] defaultBranches Collection of default branches.
47  ///
48  /// The default branches are looked at in case no branch is specified in the
49  /// booking of actions or transformations.
50  /// See TInterface for the documentation of the
51  /// methods available.
52  template <typename FILENAMESCOLL = std::vector<std::string>,
53  typename std::enable_if<TDFInternal::TIsContainer<FILENAMESCOLL>::fgValue &&
54  !std::is_same<std::string, FILENAMESCOLL>::value,
55  int>::type = 0>
56  TDataFrame(std::string_view treeName, const FILENAMESCOLL &filenamescoll, const ColumnNames_t &defaultBranches = {});
57  TDataFrame(std::string_view treeName, ::TDirectory *dirPtr, const ColumnNames_t &defaultBranches = {});
58  TDataFrame(TTree &tree, const ColumnNames_t &defaultBranches = {});
59  TDataFrame(Long64_t numEntries);
60 };
61 
62 template <typename FILENAMESCOLL, typename std::enable_if<TDFInternal::TIsContainer<FILENAMESCOLL>::fgValue &&
63  !std::is_same<std::string, FILENAMESCOLL>::value,
64  int>::type>
65 TDataFrame::TDataFrame(std::string_view treeName, const FILENAMESCOLL &filenamescoll,
66  const ColumnNames_t &defaultBranches)
67  : TDF::TInterface<TDFDetail::TLoopManager>(std::make_shared<TDFDetail::TLoopManager>(nullptr, defaultBranches))
68 {
69  std::string treeNameInt(treeName);
70  auto chain = std::make_shared<TChain>(treeNameInt.c_str());
71  for (auto &fileName : filenamescoll) chain->Add(TDFInternal::ToConstCharPtr(fileName));
72  fProxiedPtr->SetTree(std::static_pointer_cast<TTree>(chain));
73 }
74 
75 } // end NS Experimental
76 } // end NS ROOT
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Print a TDataFrame at the prompt:
80 namespace cling {
82 {
83  auto df = tdf->GetDataFrameChecked();
84  auto *tree = df->GetTree();
85  auto defBranches = df->GetDefaultBranches();
86  auto tmpBranches = df->GetTmpBranches();
87 
88  std::ostringstream ret;
89  if (tree) {
90  ret << "A data frame built on top of the " << tree->GetName() << " dataset.";
91  if (!defBranches.empty()) {
92  if (defBranches.size() == 1)
93  ret << "\nDefault branch: " << defBranches[0];
94  else {
95  ret << "\nDefault branches:\n";
96  for (auto &&branch : defBranches) {
97  ret << " - " << branch << "\n";
98  }
99  }
100  }
101  } else {
102  ret << "A data frame that will create " << df->GetNEmptyEntries() << " entries\n";
103  }
104 
105  return ret.str();
106 }
107 } // namespace cling
108 
109 #endif // ROOT_TDATAFRAME
TDataFrame(std::string_view treeName, std::string_view filenameglob, const ColumnNames_t &defaultBranches={})
Build the dataframe.
Definition: TDataFrame.cxx:531
long long Long64_t
Definition: RtypesCore.h:69
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TDFDetail::ColumnNames_t ColumnNames_t
Definition: TDataFrame.hxx:37
STL namespace.
std::shared_ptr< TLoopManager > GetDataFrameChecked()
Get the TLoopManager if reachable. If not, throw.
const char * ToConstCharPtr(const char *s)
Definition: TDFUtils.cxx:115
std::string printValue(ROOT::Experimental::TDataFrame *tdf)
Definition: TDataFrame.hxx:81
Describe directory structure in memory.
Definition: TDirectory.h:34
int type
Definition: TGX11.cxx:120
Print a TSeq at the prompt:
Definition: TDatime.h:115
std::shared_ptr< TDFDetail::TLoopManager > fProxiedPtr
Key/value store of objects.
Definition: TDirectory.hxx:68
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:36
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:78
The public interface to the TDataFrame federation of classes.