// @(#)root/tree:$Id$
// Author: Rene Brun   04/06/2006

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TTreeCache
#define ROOT_TTreeCache


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TTreeCache                                                           //
//                                                                      //
// Specialization of TFileCacheRead for a TTree                         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TFileCacheRead
#include "TFileCacheRead.h"
#endif
#ifndef ROOT_TObjArray
#include "TObjArray.h"
#endif

class TTree;
class TBranch;

class TTreeCache : public TFileCacheRead {

public:
   enum EPrefillType { kNoPrefill, kAllBranches };

protected:
   Long64_t        fEntryMin;    //! first entry in the cache
   Long64_t        fEntryMax;    //! last entry in the cache
   Long64_t        fEntryCurrent;//! current lowest entry number in the cache
   Long64_t        fEntryNext;   //! next entry number where cache must be filled
   Int_t           fNbranches;   //! Number of branches in the cache
   Int_t           fNReadOk;     //Number of blocks read and found in the cache
   Int_t           fNReadMiss;   //Number of blocks read and not found in the chache
   Int_t           fNReadPref;   //Number of blocks that were prefetched
   TObjArray      *fBranches;    //! List of branches to be stored in the cache
   TList          *fBrNames;     //! list of branch names in the cache
   TTree          *fTree;        //! pointer to the current Tree
   Bool_t          fIsLearning;  //! true if cache is in learning mode
   Bool_t          fIsManual;    //! true if cache is StopLearningPhase was used
   Bool_t          fFirstBuffer; //! true if first buffer is used for prefetching
   Bool_t          fOneTime;     //! used in the learning phase
   Bool_t          fReverseRead; //!  reading in reverse mode
   Int_t           fFillTimes;   //!  how many times we can fill the current buffer
   Bool_t          fFirstTime;   //! save the fact that we processes the first entry
   Long64_t        fFirstEntry;  //! save the value of the first entry
   Bool_t          fReadDirectionSet; //! read direction established
   Bool_t          fEnabled;     //! cache enabled for cached reading
   EPrefillType    fPrefillType; // Whether a prefilling is enabled (and if applicable which type)
   static  Int_t   fgLearnEntries; // number of entries used for learning mode
   Bool_t          fAutoCreated; //! true if cache was automatically created

private:
   TTreeCache(const TTreeCache &);            //this class cannot be copied
   TTreeCache& operator=(const TTreeCache &);

public:

   TTreeCache();
   TTreeCache(TTree *tree, Int_t buffersize=0);
   virtual ~TTreeCache();
   virtual void         AddBranch(TBranch *b, Bool_t subgbranches = kFALSE);
   virtual void         AddBranch(const char *branch, Bool_t subbranches = kFALSE);
   virtual void         DropBranch(TBranch *b, Bool_t subbranches = kFALSE);
   virtual void         DropBranch(const char *branch, Bool_t subbranches = kFALSE);
   virtual void         Disable() {fEnabled = kFALSE;}
   virtual void         Enable() {fEnabled = kTRUE;}
   const TObjArray     *GetCachedBranches() const { return fBranches; }
   EPrefillType         GetConfiguredPrefillType() const;
   Double_t             GetEfficiency() const;
   Double_t             GetEfficiencyRel() const;
   virtual Int_t        GetEntryMin() const {return fEntryMin;}
   virtual Int_t        GetEntryMax() const {return fEntryMax;}
   static Int_t         GetLearnEntries();
   virtual EPrefillType GetLearnPrefill() const {return fPrefillType;}
   TTree               *GetTree() const {return fTree;}
   Bool_t               IsAutoCreated() const {return fAutoCreated;}
   virtual Bool_t       IsEnabled() const {return fEnabled;}
   virtual Bool_t       IsLearning() const {return fIsLearning;}

   virtual Bool_t       FillBuffer();
   virtual void         LearnPrefill();

   virtual void         Print(Option_t *option="") const;
   virtual Int_t        ReadBuffer(char *buf, Long64_t pos, Int_t len);
   virtual Int_t        ReadBufferNormal(char *buf, Long64_t pos, Int_t len);
   virtual Int_t        ReadBufferPrefetch(char *buf, Long64_t pos, Int_t len);
   virtual void         ResetCache();
   void                 SetAutoCreated(Bool_t val) {fAutoCreated = val;}
   virtual void         SetEntryRange(Long64_t emin,   Long64_t emax);
   virtual void         SetFile(TFile *file, TFile::ECacheAction action=TFile::kDisconnect);
   virtual void         SetLearnPrefill(EPrefillType type = kNoPrefill);
   static void          SetLearnEntries(Int_t n = 10);
   void                 StartLearningPhase();
   virtual void         StopLearningPhase();
   virtual void         UpdateBranches(TTree *tree);

   ClassDef(TTreeCache,2)  //Specialization of TFileCacheRead for a TTree
};

#endif
 TTreeCache.h:1
 TTreeCache.h:2
 TTreeCache.h:3
 TTreeCache.h:4
 TTreeCache.h:5
 TTreeCache.h:6
 TTreeCache.h:7
 TTreeCache.h:8
 TTreeCache.h:9
 TTreeCache.h:10
 TTreeCache.h:11
 TTreeCache.h:12
 TTreeCache.h:13
 TTreeCache.h:14
 TTreeCache.h:15
 TTreeCache.h:16
 TTreeCache.h:17
 TTreeCache.h:18
 TTreeCache.h:19
 TTreeCache.h:20
 TTreeCache.h:21
 TTreeCache.h:22
 TTreeCache.h:23
 TTreeCache.h:24
 TTreeCache.h:25
 TTreeCache.h:26
 TTreeCache.h:27
 TTreeCache.h:28
 TTreeCache.h:29
 TTreeCache.h:30
 TTreeCache.h:31
 TTreeCache.h:32
 TTreeCache.h:33
 TTreeCache.h:34
 TTreeCache.h:35
 TTreeCache.h:36
 TTreeCache.h:37
 TTreeCache.h:38
 TTreeCache.h:39
 TTreeCache.h:40
 TTreeCache.h:41
 TTreeCache.h:42
 TTreeCache.h:43
 TTreeCache.h:44
 TTreeCache.h:45
 TTreeCache.h:46
 TTreeCache.h:47
 TTreeCache.h:48
 TTreeCache.h:49
 TTreeCache.h:50
 TTreeCache.h:51
 TTreeCache.h:52
 TTreeCache.h:53
 TTreeCache.h:54
 TTreeCache.h:55
 TTreeCache.h:56
 TTreeCache.h:57
 TTreeCache.h:58
 TTreeCache.h:59
 TTreeCache.h:60
 TTreeCache.h:61
 TTreeCache.h:62
 TTreeCache.h:63
 TTreeCache.h:64
 TTreeCache.h:65
 TTreeCache.h:66
 TTreeCache.h:67
 TTreeCache.h:68
 TTreeCache.h:69
 TTreeCache.h:70
 TTreeCache.h:71
 TTreeCache.h:72
 TTreeCache.h:73
 TTreeCache.h:74
 TTreeCache.h:75
 TTreeCache.h:76
 TTreeCache.h:77
 TTreeCache.h:78
 TTreeCache.h:79
 TTreeCache.h:80
 TTreeCache.h:81
 TTreeCache.h:82
 TTreeCache.h:83
 TTreeCache.h:84
 TTreeCache.h:85
 TTreeCache.h:86
 TTreeCache.h:87
 TTreeCache.h:88
 TTreeCache.h:89
 TTreeCache.h:90
 TTreeCache.h:91
 TTreeCache.h:92
 TTreeCache.h:93
 TTreeCache.h:94
 TTreeCache.h:95
 TTreeCache.h:96
 TTreeCache.h:97
 TTreeCache.h:98
 TTreeCache.h:99
 TTreeCache.h:100
 TTreeCache.h:101
 TTreeCache.h:102
 TTreeCache.h:103
 TTreeCache.h:104
 TTreeCache.h:105
 TTreeCache.h:106
 TTreeCache.h:107
 TTreeCache.h:108
 TTreeCache.h:109
 TTreeCache.h:110
 TTreeCache.h:111
 TTreeCache.h:112
 TTreeCache.h:113