Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 09:11:50

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                                               const std::set<std::string> &suppressErrorsForMissingBranches);
0076    void Reset();
0077 };
0078 } // End of namespace Internal
0079 
0080 class TTreeProcessorMT {
0081 private:
0082    const std::vector<std::string> fFileNames; ///< Names of the files
0083    const std::vector<std::string> fTreeNames; ///< TTree names (always same size and ordering as fFileNames)
0084    /// User-defined selection of entry numbers to be processed, empty if none was provided
0085    TEntryList fEntryList;
0086    ROOT::TreeUtils::RFriendInfo fFriendInfo;
0087    ROOT::TThreadExecutor fPool; ///<! Thread pool for processing.
0088 
0089    /// Thread-local TreeViews
0090    // Must be declared after fPool, for IMT to be initialized first!
0091    ROOT::TThreadedObject<ROOT::Internal::TTreeView> fTreeView{TNumSlots{ROOT::GetThreadPoolSize()}};
0092 
0093    std::vector<std::string> FindTreeNames();
0094    static unsigned int fgTasksPerWorkerHint;
0095 
0096    std::pair<Long64_t, Long64_t> fGlobalRange{0, std::numeric_limits<Long64_t>::max()};
0097 
0098    // List of branches for which we want to suppress the printed error about
0099    // missing branch when switching to a new tree
0100    std::set<std::string> fSuppressErrorsForMissingBranches{};
0101 
0102 public:
0103    TTreeProcessorMT(std::string_view filename, 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(const std::vector<std::string_view> &filenames, std::string_view treename = "",
0106                     UInt_t nThreads = 0u,
0107                     const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()});
0108    TTreeProcessorMT(std::initializer_list<std::string_view> filenames, std::string_view treename = "", UInt_t nThreads = 0u,
0109                     const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()}):
0110                     TTreeProcessorMT(std::vector<std::string_view>(filenames), treename, nThreads, globalRange) {}
0111    TTreeProcessorMT(TTree &tree, const TEntryList &entries, UInt_t nThreads = 0u,
0112                     const std::set<std::string> &suppressErrorsForMissingBranches = {});
0113    TTreeProcessorMT(TTree &tree, UInt_t nThreads = 0u,
0114                     const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()},
0115                     const std::set<std::string> &suppressErrorsForMissingBranches = {});
0116 
0117    void Process(std::function<void(TTreeReader &)> func);
0118 
0119    static void SetTasksPerWorkerHint(unsigned int m);
0120    static unsigned int GetTasksPerWorkerHint();
0121 };
0122 
0123 } // End of namespace ROOT
0124 
0125 #endif // defined TTreeProcessorMT