Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // @(#)root/tree:$Id$
0002 // Author: Brian Bockelman, 2017-06-13
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2017, 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_TTreeReaderFast
0013 #define ROOT_TTreeReaderFast
0014 
0015 
0016 ////////////////////////////////////////////////////////////////////////////
0017 //                                                                        //
0018 // TTreeReader                                                            //
0019 //                                                                        //
0020 // A simple interface for reading trees or chains.                        //
0021 //                                                                        //
0022 //                                                                        //
0023 ////////////////////////////////////////////////////////////////////////////
0024 
0025 #include "TTree.h"
0026 #include "TTreeReader.h"
0027 
0028 #include <deque>
0029 
0030 // Forward decl's
0031 namespace ROOT {
0032 namespace Experimental {
0033 namespace Internal {
0034 class TTreeReaderValueFast;
0035 class TTreeReaderValueFastBase;
0036 }  // Internal
0037 
0038 class TTreeReaderFast: public TObject {
0039 public:
0040 
0041    // A simple iterator based on TTreeReader::Iterator_t; allows use of the
0042    // TTreeReaderFast.
0043    //
0044    // NOTE that an increment may invalidate previous copies of the iterator.
0045    class Iterator_t {
0046 
0047       Int_t   *fIdx{nullptr}; ///< Current offset inside this cluster.
0048       Int_t    fCount{0};     ///< Number of entries inside this cluster.
0049       Long64_t fEntry{-1};    ///< Entry number of the tree referenced by this iterator; -1 is invalid.
0050       TTreeReaderFast *fReader{nullptr}; ///< The reader we select the entries on.
0051 
0052       /// Whether the iterator points to a valid entry.
0053       bool IsValid() const { return fEntry >= 0; }
0054 
0055    public:
0056       using iterator_category = std::input_iterator_tag;
0057       using value_type = const Long64_t;
0058       using difference_type = Long_t;
0059       using pointer = const Long64_t *;
0060       using const_pointer = const Long64_t *;
0061       using reference = const Long64_t &;
0062 
0063       /// Default-initialize the iterator as "past the end".
0064       Iterator_t() {}
0065 
0066       /// Initialize the iterator with the reader it steers and a
0067       /// tree entry number; -1 is invalid.
0068       Iterator_t(TTreeReaderFast& reader, Long64_t first):
0069          fIdx(&(reader.fEvtIndex)), fEntry(first), fReader(&reader)
0070       {
0071          //printf("Initializing new iterator; start of %lld, proceed for %lld events.\n", first, count);
0072          fCount = fReader->GetNextRange(fEntry);
0073          *fIdx = 0;
0074       }
0075 
0076       /// Compare two iterators for equality.
0077       bool operator==(const Iterator_t& lhs) const {
0078          // From C++14: value initialized (past-end) it compare equal.
0079          if (R__unlikely(!IsValid() && !lhs.IsValid())) {return true;}
0080          return R__unlikely(fEntry == lhs.fEntry && fReader == lhs.fReader);
0081       }
0082 
0083       /// Compare two iterators for inequality.
0084       bool operator!=(const Iterator_t& lhs) const {
0085          return !(*this == lhs);
0086       }
0087 
0088       /// Increment the iterator (postfix i++).
0089       Iterator_t operator++(int) {
0090          Iterator_t ret = *this;
0091          this->operator++();
0092          return ret;
0093       }
0094 
0095       /// Increment the iterator (prefix ++i).
0096       Iterator_t& operator++() {
0097          (*fIdx)++;
0098          if (R__unlikely(*fIdx == fCount)) {
0099              //printf("Hit end-of-basket of %d events.  Get next entry.\n", fCount);
0100              fEntry += fCount;
0101              *fIdx = 0;
0102              fCount = fReader->GetNextRange(fEntry);
0103              //printf("This chunk has %d events.\n", fCount);
0104              if (R__unlikely(!fCount || (fCount < 0))) {
0105                  fEntry = -1;
0106                  fReader->fEntryStatus = TTreeReader::kEntryBadReader;
0107              }
0108          }
0109          return *this;
0110       }
0111 
0112       /// Set the entry number in the reader and return it.
0113       Long64_t operator*() {
0114          return fEntry + *fIdx;
0115       }
0116 
0117       Long64_t operator*() const {
0118          return **const_cast<Iterator_t*>(this);
0119       }
0120    };
0121 
0122    typedef Iterator_t iterator;
0123 
0124    TTreeReaderFast():
0125       fTree(nullptr),
0126       fEntryStatus(TTreeReader::kEntryNoTree),
0127       fLastEntry(-1)
0128    {}
0129 
0130    TTreeReaderFast(TTree* tree);
0131    TTreeReaderFast(const char* keyname, TDirectory* dir = nullptr );
0132 
0133    ~TTreeReaderFast() override;
0134 
0135    TTreeReader::EEntryStatus SetEntriesRange(Long64_t first, Long64_t last);
0136 
0137    TTreeReader::EEntryStatus GetEntryStatus() const { return fEntryStatus; }
0138 
0139    TTree* GetTree() const { return fTree; }
0140 
0141    TTreeReader::EEntryStatus SetEntry(Long64_t);
0142 
0143    /// Return an iterator to the 0th TTree entry.
0144    Iterator_t begin() {
0145       return Iterator_t(*this, 0);
0146    }
0147    Iterator_t end() const { return Iterator_t(); }
0148 
0149 protected:
0150 
0151    // Returns a reference to the current event index in the various value buffers.
0152    Int_t &GetIndexRef() {return fEvtIndex;}
0153 
0154    void RegisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase* reader);
0155    void DeregisterValueReader(ROOT::Experimental::Internal::TTreeReaderValueFastBase* reader);
0156 
0157 private:
0158 
0159    Int_t GetNextRange(Int_t);
0160    void Initialize();
0161 
0162    TTree* fTree{nullptr}; ///< tree that's read
0163    TDirectory* fDirectory{nullptr}; ///< directory (or current file for chains)
0164    ROOT::Internal::TBranchProxyDirector* fDirector{nullptr}; ///< proxying director, owned
0165    TTreeReader::EEntryStatus fEntryStatus{TTreeReader::kEntryNotLoaded}; ///< status of most recent read request
0166    std::deque<ROOT::Experimental::Internal::TTreeReaderValueFastBase*> fValues; ///< readers that use our director
0167 
0168    Int_t    fEvtIndex{-1};
0169    Long64_t fBaseEvent{-1};
0170    Long64_t fLastEntry{-1};
0171 
0172    friend class ROOT::Experimental::Internal::TTreeReaderValueFastBase;
0173 
0174    ClassDefOverride(TTreeReaderFast, 0); // A simple interface to read trees via bulk I/O
0175 };
0176 
0177 }  // Experimental
0178 }  // ROOT
0179 #endif // defined TTreeReaderFast