Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TTreeProcessorMT.hxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Authors: Enric Tejedor, Enrico Guiraud CERN 05/06/2018
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_TTreeProcessorMT
13 #define ROOT_TTreeProcessorMT
14 
15 #include "TKey.h"
16 #include "TTree.h"
17 #include "TFile.h"
18 #include "TChain.h"
19 #include "TTreeReader.h"
20 #include "TError.h"
21 #include "TEntryList.h"
22 #include "TFriendElement.h"
23 #include "ROOT/RMakeUnique.hxx"
24 #include "ROOT/TThreadedObject.hxx"
25 
26 #include <string.h>
27 #include <functional>
28 #include <vector>
29 
30 /** \class TTreeView
31  \brief A helper class that encapsulates a file and a tree.
32 
33 A helper class that encapsulates a TFile and a TTree, along with their names.
34 It is used together with TTProcessor and ROOT::TThreadedObject, so that
35 in the TTProcessor::Process method each thread can work on its own
36 <TFile,TTree> pair.
37 
38 This class can also be used with a collection of file names or a TChain, in case
39 the tree is stored in more than one file. A view will always contain only the
40 current (active) tree and file objects.
41 
42 A copy constructor is defined for TTreeView to work with ROOT::TThreadedObject.
43 The latter makes a copy of a model object every time a new thread accesses
44 the threaded object.
45 */
46 
47 namespace ROOT {
48 namespace Internal {
49 /// Names, aliases, and file names of a TTree's or TChain's friends
50 using NameAlias = std::pair<std::string, std::string>;
51 struct FriendInfo {
52  /// Pairs of names and aliases of friend trees/chains
53  std::vector<Internal::NameAlias> fFriendNames;
54  /// Names of the files where each friend is stored. fFriendFileNames[i] is the list of files for friend with
55  /// name fFriendNames[i]
56  std::vector<std::vector<std::string>> fFriendFileNames;
57 };
58 
59 class TTreeView {
60 public:
61  using TreeReaderEntryListPair = std::pair<std::unique_ptr<TTreeReader>, std::unique_ptr<TEntryList>>;
62 
63 private:
64  // NOTE: fFriends must come before fChain to be deleted after it, see ROOT-9281 for more details
65  std::vector<std::unique_ptr<TChain>> fFriends; ///< Friends of the tree/chain
66  std::unique_ptr<TChain> fChain; ///< Chain on which to operate
67 
68  void MakeChain(const std::string &treeName, const std::vector<std::string> &fileNames, const FriendInfo &friendInfo,
69  const std::vector<Long64_t> &nEntries, const std::vector<std::vector<Long64_t>> &friendEntries);
70  TreeReaderEntryListPair MakeReaderWithEntryList(TEntryList &globalList, Long64_t start, Long64_t end);
71  std::unique_ptr<TTreeReader> MakeReader(Long64_t start, Long64_t end);
72 
73 public:
74  TTreeView() = default;
75  // no-op, we don't want to copy the local TChains
76  TTreeView(const TTreeView &) {}
77  TreeReaderEntryListPair GetTreeReader(Long64_t start, Long64_t end, const std::string &treeName,
78  const std::vector<std::string> &fileNames, const FriendInfo &friendInfo,
79  TEntryList entryList, const std::vector<Long64_t> &nEntries,
80  const std::vector<std::vector<Long64_t>> &friendEntries);
81 };
82 } // End of namespace Internal
83 
84 class TTreeProcessorMT {
85 private:
86  const std::vector<std::string> fFileNames; ///< Names of the files
87  const std::string fTreeName; ///< Name of the tree
88  /// User-defined selection of entry numbers to be processed, empty if none was provided
89  const TEntryList fEntryList; // const to be sure to avoid race conditions among TTreeViews
90  const Internal::FriendInfo fFriendInfo;
91 
92  ROOT::TThreadedObject<ROOT::Internal::TTreeView> fTreeView; ///<! Thread-local TreeViews
93 
94  Internal::FriendInfo GetFriendInfo(TTree &tree);
95  std::string FindTreeName();
96  static unsigned int fgMaxTasksPerFilePerWorker;
97 
98 public:
99  TTreeProcessorMT(std::string_view filename, std::string_view treename = "");
100  TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename = "");
101  TTreeProcessorMT(TTree &tree, const TEntryList &entries);
102  TTreeProcessorMT(TTree &tree);
103 
104  void Process(std::function<void(TTreeReader &)> func);
105  static void SetMaxTasksPerFilePerWorker(unsigned int m);
106  static unsigned int GetMaxTasksPerFilePerWorker();
107 };
108 
109 } // End of namespace ROOT
110 
111 #endif // defined TTreeProcessorMT