Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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;
34 namespace TDFInternal = ROOT::Internal::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,
53  typename std::enable_if<TDFInternal::TIsContainer<FILENAMESCOLL>::fgValue, int>::type = 0>
54  TDataFrame(std::string_view treeName, const FILENAMESCOLL &filenamescoll,
55  const ColumnNames_t &defaultBranches = {});
56  TDataFrame(std::string_view treeName, ::TDirectory *dirPtr, const ColumnNames_t &defaultBranches = {});
57  TDataFrame(TTree &tree, const ColumnNames_t &defaultBranches = {});
58  TDataFrame(Long64_t numEntries);
59 };
60 
61 template <typename FILENAMESCOLL, typename std::enable_if<TDFInternal::TIsContainer<FILENAMESCOLL>::fgValue, int>::type>
62 TDataFrame::TDataFrame(std::string_view treeName, const FILENAMESCOLL &filenamescoll,
63  const ColumnNames_t &defaultBranches)
64  : TDF::TInterface<TDFDetail::TLoopManager>(std::make_shared<TDFDetail::TLoopManager>(nullptr, defaultBranches))
65 {
66  const std::string treeNameInt(treeName);
67  auto chain = new TChain(treeNameInt.c_str());
68  for (auto &fileName : filenamescoll) chain->Add(TDFInternal::ToConstCharPtr(fileName));
69  fProxiedPtr->SetTree(std::make_shared<TTree>(static_cast<TTree *>(chain)));
70 }
71 
72 } // end NS Experimental
73 } // end NS ROOT
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Print a TDataFrame at the prompt:
77 namespace cling {
79 {
80  auto df = tdf->GetDataFrameChecked();
81  auto *tree = df->GetTree();
82  auto defBranches = df->GetDefaultBranches();
83  auto tmpBranches = df->GetTmpBranches();
84 
85  std::ostringstream ret;
86  if (tree) {
87  ret << "A data frame built on top of the " << tree->GetName() << " dataset.";
88  if (!defBranches.empty()) {
89  if (defBranches.size() == 1)
90  ret << "\nDefault branch: " << defBranches[0];
91  else {
92  ret << "\nDefault branches:\n";
93  for (auto &&branch : defBranches) {
94  ret << " - " << branch << "\n";
95  }
96  }
97  }
98  } else {
99  ret << "A data frame that will create " << df->GetNEmptyEntries() << " entries\n";
100  }
101 
102  return ret.str();
103 }
104 } // namespace cling
105 
106 #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
TDFDetail::ColumnNames_t ColumnNames_t
Definition: TDataFrame.hxx:37
std::string printValue(const TDatime *val)
Print a TDatime at the prompt.
Definition: TDatime.cxx:514
std::shared_ptr< TLoopManager > GetDataFrameChecked()
Get the TLoopManager if reachable. If not, throw.
const char * ToConstCharPtr(const char *s)
Definition: TDFUtils.cxx:115
Describe directory structure in memory.
Definition: TDirectory.h:34
int type
Definition: TGX11.cxx:120
std::shared_ptr< TDFDetail::TLoopManager > fProxiedPtr
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:36
A chain is a collection of files containing TTree objects.
Definition: TChain.h:33
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.
virtual Int_t Add(TChain *chain)
Add all files referenced by the passed chain to this chain.
Definition: TChain.cxx:220