Logo ROOT   6.08/07
Reference Guide
TTreeProcessor.hxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Author: Enric Tejedor, CERN 12/09/2016
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TTreeProcessor
13 #define ROOT_TTreeProcessor
14 
15 #ifndef ROOT_TKey
16 #include "TKey.h"
17 #endif
18 
19 #ifndef ROOT_TTree
20 #include "TTree.h"
21 #endif
22 
23 #ifndef ROOT_TFile
24 #include "TFile.h"
25 #endif
26 
27 #ifndef ROOT_TTreeReader
28 #include "TTreeReader.h"
29 #endif
30 
31 #ifndef ROOT_TThreadedObject
32 #include "ROOT/TThreadedObject.hxx"
33 #endif
34 
35 #ifndef ROOT_TError
36 #include "TError.h"
37 #endif
38 
39 #include <string.h>
40 #include <functional>
41 
42 
43 /** \class TTreeView
44  \brief A helper class that encapsulates a file and a tree.
45 
46 A helper class that encapsulates a TFile and a TTree, along with their names.
47 It is used together with TTProcessor and ROOT::TThreadedObject, so that
48 in the TTProcessor::Process method each thread can work on its own
49 <TFile,TTree> pair.
50 
51 A copy constructor is defined for TTreeView to work with ROOT::TThreadedObject.
52 The latter makes a copy of a model object every time a new thread accesses
53 the threaded object.
54 */
55 
56 namespace ROOT {
57  namespace Internal {
58  class TTreeView {
59  private:
60  std::string_view fFileName; ///< Name of the file
61  std::string_view fTreeName; ///< Name of the tree
62  std::unique_ptr<TFile> fFile; ///<! File object of this view.
63  std::unique_ptr<TTree> fTree; ///<! Tree object of this view.
64 
65  ////////////////////////////////////////////////////////////////////////////////
66  /// Initialize the file and the tree for this view, first looking for a tree in
67  /// the file if necessary.
68  void Init()
69  {
70  fFile.reset(TFile::Open(fFileName.data()));
71 
72  // If the tree name is empty, look for a tree in the file
73  if (fTreeName.empty()) {
74  TIter next(fFile->GetListOfKeys());
75  while (TKey *key = (TKey*)next()) {
76  const char *className = key->GetClassName();
77  if (strcmp(className, "TTree") == 0) {
78  fTreeName = key->GetName();
79  break;
80  }
81  }
82  if (fTreeName.empty())
83  ::Error("TreeView constructor", "Cannot find any tree in file %s", fFileName.data());
84  }
85 
86  // We cannot use here the template method (TFile::GetObject) because the header will finish
87  // in the PCH and the specialization will be available. PyROOT will not be able to specialize
88  // the method for types other that TTree.
89  TTree *tp = (TTree*)fFile->Get(fTreeName.data());
90  fTree.reset(tp);
91  }
92 
93  public:
94  //////////////////////////////////////////////////////////////////////////
95  /// Regular constructor.
96  /// \param[in] filename Name of the file containing the tree to process.
97  /// \param[in] treename Name of the tree to process. If not provided,
98  /// the implementation will automatically search for a
99  /// tree in the file.
100  TTreeView(std::string_view fn, std::string_view tn) : fFileName(fn), fTreeName(tn)
101  {
102  Init();
103  }
104 
105  //////////////////////////////////////////////////////////////////////////
106  /// Copy constructor.
107  /// \param[in] Object to copy.
108  TTreeView(const TTreeView& view) : fFileName(view.fFileName), fTreeName(view.fTreeName)
109  {
110  Init();
111  }
112 
113  //////////////////////////////////////////////////////////////////////////
114  /// Get the cluster iterator for the tree of this view, starting from
115  /// entry zero.
117  {
118  return fTree->GetClusterIterator(0);
119  }
120 
121  //////////////////////////////////////////////////////////////////////////
122  /// Get a TTreeReader for the tree of this view.
123  std::unique_ptr<TTreeReader> GetTreeReader() const
124  {
125  return std::unique_ptr<TTreeReader>(new TTreeReader(fTree.get()));
126  }
127 
128  //////////////////////////////////////////////////////////////////////////
129  /// Get the number of entries of the tree of this view.
131  {
132  return fTree->GetEntries();
133  }
134  };
135  } // End of namespace Internal
136 
137 
139  private:
140  ROOT::TThreadedObject<ROOT::Internal::TTreeView> treeView; ///<! Threaded object with <file,tree> per thread
141 
142  public:
143  ////////////////////////////////////////////////////////////////////////
144  /// Regular constructor.
145  /// \param[in] filename Name of the file containing the tree to process.
146  /// \param[in] treename Name of the tree to process. If not provided,
147  /// the implementation will automatically search for a
148  /// tree in the file.
149  TTreeProcessor(std::string_view filename, std::string_view treename = "") : treeView(filename, treename) {}
150 
151  void Process(std::function<void(TTreeReader&)> func);
152 
153  };
154 
155 } // End of namespace ROOT
156 
157 #endif // defined TTreeProcessor
_LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY empty() const _NOEXCEPT
long long Long64_t
Definition: RtypesCore.h:69
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:48
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
std::unique_ptr< TFile > fFile
! File object of this view.
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
std::string_view fTreeName
Name of the tree.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3907
Helper class to iterate over cluster of baskets.
Definition: TTree.h:256
std::unique_ptr< TTree > fTree
! Tree object of this view.
virtual TClusterIterator GetClusterIterator(Long64_t firstentry)
Return an iterator over the cluster of baskets starting at firstentry.
Definition: TTree.cxx:5042
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
void Init()
Initialize the file and the tree for this view, first looking for a tree in the file if necessary...
TTreeView(const TTreeView &view)
Copy constructor.
TTree::TClusterIterator GetClusterIterator()
Get the cluster iterator for the tree of this view, starting from entry zero.
Long64_t GetEntries() const
Get the number of entries of the tree of this view.
double func(double *x, double *p)
Definition: stressTF1.cxx:213
TTreeProcessor(std::string_view filename, std::string_view treename="")
Regular constructor.
virtual Long64_t GetEntries() const
Definition: TTree.h:393
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY const_pointer data() const _NOEXCEPT
virtual TList * GetListOfKeys() const
A TTree object has a header with a name and a title.
Definition: TTree.h:98
ROOT::TThreadedObject< ROOT::Internal::TTreeView > treeView
! Threaded object with <file,tree> per thread
std::string_view fFileName
Name of the file.
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.
TTreeView(std::string_view fn, std::string_view tn)
Regular constructor.
std::unique_ptr< TTreeReader > GetTreeReader() const
Get a TTreeReader for the tree of this view.