Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:51

0001 // @(#)root/thread:$Id$
0002 // Authors: Enric Tejedor, Enrico Guiraud CERN 05/06/2018
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TTreeProcessorMT
0013 #define ROOT_TTreeProcessorMT
0014 
0015 #include "TKey.h"
0016 #include "TTree.h"
0017 #include "TFile.h"
0018 #include "TChain.h"
0019 #include "TEntryList.h"
0020 #include "TTreeReader.h"
0021 #include "TError.h"
0022 #include "TEntryList.h"
0023 #include "ROOT/TThreadedObject.hxx"
0024 #include "ROOT/TThreadExecutor.hxx"
0025 #include "ROOT/InternalTreeUtils.hxx" // RNoCleanupNotifier
0026 #include "ROOT/RFriendInfo.hxx"
0027 
0028 #include <functional>
0029 #include <memory>
0030 #include <vector>
0031 #include <limits>
0032 #include <RtypesCore.h> // Long64_t
0033 
0034 /** \class TTreeView
0035     \brief A helper class that encapsulates a file and a tree.
0036 
0037 A helper class that encapsulates a TFile and a TTree, along with their names.
0038 It is used together with TTProcessor and ROOT::TThreadedObject, so that
0039 in the TTProcessor::Process method each thread can work on its own
0040 <TFile,TTree> pair.
0041 
0042 This class can also be used with a collection of file names or a TChain, in case
0043 the tree is stored in more than one file. A view will always contain only the
0044 current (active) tree and file objects.
0045 
0046 A copy constructor is defined for TTreeView to work with ROOT::TThreadedObject.
0047 The latter makes a copy of a model object every time a new thread accesses
0048 the threaded object.
0049 */
0050 
0051 namespace ROOT {
0052 
0053 namespace Internal {
0054 
0055 class TTreeView {
0056    ROOT::Internal::TreeUtils::RNoCleanupNotifier fNoCleanupNotifier;
0057 
0058    std::vector<std::unique_ptr<TChain>> fFriends; ///< Friends of the tree/chain, if present
0059    std::unique_ptr<TEntryList> fEntryList;        ///< TEntryList for fChain, if present
0060    // NOTE: fFriends and fEntryList MUST come before fChain to be deleted after it, because neither friend trees nor
0061    // entrylists are deregistered from the main tree at destruction (ROOT-9283 tracks the issue for friends).
0062    std::unique_ptr<TChain> fChain; ///< Chain on which to operate
0063 
0064    void MakeChain(const std::vector<std::string> &treeName, const std::vector<std::string> &fileNames,
0065                   const ROOT::TreeUtils::RFriendInfo &friendInfo, const std::vector<Long64_t> &nEntries);
0066 
0067 public:
0068    TTreeView() = default;
0069    // no-op, we don't want to copy the local TChains
0070    TTreeView(const TTreeView &) {}
0071    std::unique_ptr<TTreeReader> GetTreeReader(Long64_t start, Long64_t end, const std::vector<std::string> &treeName,
0072                                               const std::vector<std::string> &fileNames,
0073                                               const ROOT::TreeUtils::RFriendInfo &friendInfo,
0074                                               const TEntryList &entryList, const std::vector<Long64_t> &nEntries);
0075    void Reset();
0076 };
0077 } // End of namespace Internal
0078 
0079 class TTreeProcessorMT {
0080 private:
0081    const std::vector<std::string> fFileNames; ///< Names of the files
0082    const std::vector<std::string> fTreeNames; ///< TTree names (always same size and ordering as fFileNames)
0083    /// User-defined selection of entry numbers to be processed, empty if none was provided
0084    TEntryList fEntryList;
0085    ROOT::TreeUtils::RFriendInfo fFriendInfo;
0086    ROOT::TThreadExecutor fPool; ///<! Thread pool for processing.
0087 
0088    /// Thread-local TreeViews
0089    // Must be declared after fPool, for IMT to be initialized first!
0090    ROOT::TThreadedObject<ROOT::Internal::TTreeView> fTreeView{TNumSlots{ROOT::GetThreadPoolSize()}};
0091 
0092    std::vector<std::string> FindTreeNames();
0093    static unsigned int fgTasksPerWorkerHint;
0094 
0095    std::pair<Long64_t, Long64_t> fGlobalRange{0, std::numeric_limits<Long64_t>::max()};
0096 
0097 public:
0098    TTreeProcessorMT(std::string_view filename, std::string_view treename = "", UInt_t nThreads = 0u,
0099                     const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()});
0100    TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename = "",
0101                     UInt_t nThreads = 0u,
0102                     const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()});
0103    TTreeProcessorMT(std::initializer_list<std::string_view> filenames, std::string_view treename = "", UInt_t nThreads = 0u,
0104                     const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()}):
0105                     TTreeProcessorMT(std::vector<std::string_view>(filenames), treename, nThreads, globalRange) {}
0106    TTreeProcessorMT(TTree &tree, const TEntryList &entries, UInt_t nThreads = 0u);
0107    TTreeProcessorMT(TTree &tree, UInt_t nThreads = 0u,
0108                     const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()});
0109 
0110    void Process(std::function<void(TTreeReader &)> func);
0111 
0112    static void SetTasksPerWorkerHint(unsigned int m);
0113    static unsigned int GetTasksPerWorkerHint();
0114 };
0115 
0116 } // End of namespace ROOT
0117 
0118 #endif // defined TTreeProcessorMT