Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:30

0001 // @(#)root/tree:$Id$
0002 // Author: Rene Brun   04/06/2006
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2000, 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_TTreeCache
0013 #define ROOT_TTreeCache
0014 
0015 
0016 //////////////////////////////////////////////////////////////////////////
0017 //                                                                      //
0018 // TTreeCache                                                           //
0019 //                                                                      //
0020 // Specialization of TFileCacheRead for a TTree                         //
0021 //                                                                      //
0022 //////////////////////////////////////////////////////////////////////////
0023 
0024 #include "TFileCacheRead.h"
0025 
0026 #include <vector>
0027 
0028 class TTree;
0029 class TBranch;
0030 class TObjArray;
0031 
0032 class TTreeCache : public TFileCacheRead {
0033 
0034 public:
0035    enum EPrefillType { kNoPrefill, kAllBranches };
0036 
0037 protected:
0038    Long64_t     fEntryMin{0};         ///<! first entry in the cache
0039    Long64_t     fEntryMax{1};         ///<! last entry in the cache
0040    Long64_t     fEntryCurrent{-1};    ///<! current lowest entry number in the cache
0041    Long64_t     fEntryNext{-1};       ///<! next entry number where cache must be filled
0042    Long64_t     fCurrentClusterStart{-1}; ///<! Start of the cluster(s) where the current content was picked out
0043    Long64_t     fNextClusterStart{-1};    ///<! End+1 of the cluster(s) where the current content was picked out
0044    Int_t        fNbranches{0};        ///<! Number of branches in the cache
0045    Int_t        fNReadOk{0};          ///<  Number of blocks read and found in the cache
0046    Int_t        fNMissReadOk{0};      ///<  Number of blocks read, not found in the primary cache, and found in the secondary cache.
0047    Int_t        fNReadMiss{0};        ///<  Number of blocks read and not found in the cache
0048    Int_t        fNMissReadMiss{0};    ///<  Number of blocks read and not found in either cache.
0049    Int_t        fNReadPref{0};        ///<  Number of blocks that were prefetched
0050    Int_t        fNMissReadPref{0};    ///<  Number of blocks read into the secondary ("miss") cache.
0051    TObjArray   *fBranches{nullptr};   ///<! List of branches to be stored in the cache
0052    TList       *fBrNames{nullptr};    ///<! list of branch names in the cache
0053    TTree       *fTree{nullptr};       ///<! pointer to the current Tree
0054    bool         fIsLearning{true};   ///<! true if cache is in learning mode
0055    bool         fIsManual{false};    ///<! true if cache is StopLearningPhase was used
0056    bool         fFirstBuffer{true};  ///<! true if first buffer is used for prefetching
0057    bool         fOneTime{false};     ///<! used in the learning phase
0058    bool         fReverseRead{false}; ///<! reading in reverse mode
0059    Int_t        fFillTimes{0};        ///<! how many times we can fill the current buffer
0060    bool         fFirstTime{true};    ///<! save the fact that we processes the first entry
0061    Long64_t     fFirstEntry{-1};      ///<! save the value of the first entry
0062    bool         fReadDirectionSet{false}; ///<! read direction established
0063    bool         fEnabled{true};      ///<! cache enabled for cached reading
0064    EPrefillType fPrefillType;         ///<  Whether a pre-filling is enabled (and if applicable which type)
0065    static Int_t fgLearnEntries;       ///<  number of entries used for learning mode
0066    bool         fAutoCreated{false}; ///<! true if cache was automatically created
0067 
0068    bool         fLearnPrefilling{false}; ///<! true if we are in the process of executing LearnPrefill
0069 
0070    // These members hold cached data for missed branches when miss optimization
0071    // is enabled.  Pointers are only initialized if the miss cache is enabled.
0072    bool     fOptimizeMisses{false}; ///<! true if we should optimize cache misses.
0073    Long64_t fFirstMiss{-1};          ///<! set to the event # of the first miss.
0074    Long64_t fLastMiss{-1};           ///<! set to the event # of the last miss.
0075 
0076    // Representation of a positioned buffer IO.
0077    // {0,0} designates the uninitialized - or invalid - buffer.
0078    struct IOPos {
0079       IOPos(Long64_t pos, Int_t len) : fPos(pos), fLen(len) {}
0080 
0081       Long64_t fPos{0}; //! Position in file of cache entry.
0082       Int_t fLen{0};    //! Length of cache entry.
0083    };
0084 
0085    struct MissCache {
0086       struct Entry {
0087          Entry(IOPos io) : fIO(io) {}
0088 
0089          IOPos fIO;
0090          ULong64_t fIndex{0}; ///<! Location in fData corresponding to this entry.
0091          friend bool operator<(const Entry &a, const Entry &b) { return a.fIO.fPos < b.fIO.fPos; }
0092       };
0093       std::vector<Entry> fEntries;      ///<! Description of buffers in the miss cache.
0094       std::vector<TBranch *> fBranches; ///<! list of branches that we read on misses.
0095       std::vector<char> fData;          ///<! Actual data in the cache.
0096 
0097       void clear()
0098       {
0099          fEntries.clear();
0100          fBranches.clear();
0101          fData.clear();
0102       }
0103    };
0104 
0105    std::unique_ptr<MissCache> fMissCache; ///<! Cache contents for misses
0106 
0107 private:
0108    TTreeCache(const TTreeCache &) = delete; ///< this class cannot be copied
0109    TTreeCache &operator=(const TTreeCache &) = delete;
0110 
0111    // These are all functions related to the "miss cache": this attempts to
0112    // optimize ROOT's behavior when the TTreeCache has a cache miss.  In this
0113    // case, we try to read several branches for the event with the miss.
0114    //
0115    // The miss cache is more CPU-intensive than the rest of the TTreeCache code;
0116    // for local work (i.e., laptop with SSD), this CPU cost may outweight the
0117    // benefit.
0118    bool CheckMissCache(char *buf, Long64_t pos,
0119                          int len); ///< Check the miss cache for a particular buffer, fetching if deemed necessary.
0120    bool FillMissCache();         ///< Fill the miss cache from the current set of active branches.
0121    bool CalculateMissCache();    ///< Calculate the appropriate miss cache to fetch; helper function for FillMissCache
0122    IOPos  FindBranchBasketPos(TBranch &, Long64_t entry); ///< Given a branch and an entry, determine the file location
0123                                                           ///< (offset / size) of the corresponding basket.
0124    TBranch *CalculateMissEntries(Long64_t, int, bool);    ///< Given an file read, try to determine the corresponding branch.
0125    bool     ProcessMiss(Long64_t pos, int len); ///<! Given a file read not in the miss cache, handle (possibly) loading the data.
0126 
0127 public:
0128 
0129    TTreeCache();
0130    TTreeCache(TTree *tree, Int_t buffersize=0);
0131    ~TTreeCache() override;
0132    Int_t                AddBranch(TBranch *b, bool subgbranches = false) override;
0133    Int_t                AddBranch(const char *branch, bool subbranches = false) override;
0134    virtual Int_t        DropBranch(TBranch *b, bool subbranches = false);
0135    virtual Int_t        DropBranch(const char *branch, bool subbranches = false);
0136    virtual void         Disable() {fEnabled = false;}
0137    virtual void         Enable() {fEnabled = true;}
0138    bool                 GetOptimizeMisses() const { return fOptimizeMisses; }
0139    const TObjArray     *GetCachedBranches() const { return fBranches; }
0140    EPrefillType         GetConfiguredPrefillType() const;
0141    Double_t             GetEfficiency() const;
0142    Double_t             GetEfficiencyRel() const;
0143    virtual Int_t        GetEntryMin() const {return fEntryMin;}
0144    virtual Int_t        GetEntryMax() const {return fEntryMax;}
0145    static Int_t         GetLearnEntries();
0146    virtual EPrefillType GetLearnPrefill() const {return fPrefillType;}
0147    Double_t             GetMissEfficiency() const;
0148    Double_t             GetMissEfficiencyRel() const;
0149    TTree               *GetTree() const {return fTree;}
0150    bool                 IsAutoCreated() const {return fAutoCreated;}
0151    virtual bool         IsEnabled() const {return fEnabled;}
0152    bool                 IsLearning() const override {return fIsLearning;}
0153 
0154    virtual bool         FillBuffer();
0155    Int_t                LearnBranch(TBranch *b, bool subgbranches = false) override;
0156    virtual void         LearnPrefill();
0157 
0158    void                 Print(Option_t *option="") const override;
0159    Int_t                ReadBuffer(char *buf, Long64_t pos, Int_t len) override;
0160    virtual Int_t        ReadBufferNormal(char *buf, Long64_t pos, Int_t len);
0161    virtual Int_t        ReadBufferPrefetch(char *buf, Long64_t pos, Int_t len);
0162    virtual void         ResetCache();
0163    void                 ResetMissCache(); // Reset the miss cache.
0164    void                 SetAutoCreated(bool val) {fAutoCreated = val;}
0165    Int_t                SetBufferSize(Int_t buffersize) override;
0166    virtual void         SetEntryRange(Long64_t emin,   Long64_t emax);
0167    void                 SetFile(TFile *file, TFile::ECacheAction action=TFile::kDisconnect) override;
0168    virtual void         SetLearnPrefill(EPrefillType type = kNoPrefill);
0169    static void          SetLearnEntries(Int_t n = 10);
0170    void                 SetOptimizeMisses(bool opt);
0171    void                 StartLearningPhase();
0172    virtual void         StopLearningPhase();
0173    virtual void         UpdateBranches(TTree *tree);
0174 
0175    ClassDefOverride(TTreeCache,3)  //Specialization of TFileCacheRead for a TTree
0176 };
0177 
0178 #endif