From 3f05f734e5c8c09b65c71f6eabaaec81ef9652dd Mon Sep 17 00:00:00 2001
From: Philippe Canal <pcanal@fnal.gov>
Date: Fri, 20 Jan 2012 20:13:07 +0000
Subject: [PATCH] From Peter van Gemmeren:

Allow multiple read caches per file by adding a fCacheReadMap member and owner arguments to GetCacheRead() and SetCacheRead()
to TFile.

Index: io/io/inc/TFileCacheRead.h
Index: io/io/src/TFileCacheRead.cxx
* Adding new argument for constructor to specify TTree owner
* whitespace indents

Index: tree/tree/inc/TBasket.h
Index: tree/tree/src/TBasket.cxx
* Add optional tree owner argument to LoadBasketBuffers() to retrieve cache.
* In LoadBasketBuffers() and ReadBuffers() check the cache first than call TFile::ReadBuffer only if needed.

Index: tree/tree/inc/TChain.h
Index: tree/tree/src/TChain.cxx
* Override SetCacheSize() function to update TTree owner on the current cache.
* whitespace indents

Index: tree/tree/src/TTree.cxx
* Get TTreeCache for current TTree, by specifying owner in GetCacheRead() calls
* Always delete owned cache

Index: tree/tree/src/TTreeCache.cxx
* Adding new argument for constructor for TFileCacheRead to specify TTree owner

Index: tree/treeplayer/src/TTreePlayer.cxx
* Get TTreeCache for current TTree, by specifying owner in GetCacheRead() calls

Index: tree/tree/src/TTreeCloner.cxx
* Call TBasket::LoadBasketBuffers() with current TTree as owner of cache

Index: proof/proofplayer/src/TEventIter.cxx
* Get TTreeCache for current TTree, by specifying owner in GetCacheRead() and SetCacheRead() calls


git-svn-id: http://root.cern.ch/svn/root/trunk@42754 27541ba8-7e3a-0410-8455-c3a389f83636
---
 io/io/inc/TFile.h                    |  8 ++++--
 io/io/inc/TFileCacheRead.h           |  2 +-
 io/io/src/TFile.cxx                  | 25 ++++++++++++++---
 io/io/src/TFileCacheRead.cxx         | 26 ++++++++----------
 io/io/src/TMemFile.cxx               |  2 ++
 proof/proofplayer/src/TEventIter.cxx | 10 +++++--
 tree/tree/inc/TBasket.h              |  2 +-
 tree/tree/inc/TChain.h               |  1 +
 tree/tree/src/TBasket.cxx            | 41 +++++++++++++++++++++++-----
 tree/tree/src/TBranch.cxx            |  2 +-
 tree/tree/src/TChain.cxx             | 38 ++++++++++++++++++--------
 tree/tree/src/TTree.cxx              | 33 ++++++++++------------
 tree/tree/src/TTreeCache.cxx         |  2 +-
 tree/tree/src/TTreeCloner.cxx        |  2 +-
 tree/treeplayer/src/TTreePlayer.cxx  |  6 ++--
 15 files changed, 131 insertions(+), 69 deletions(-)

diff --git a/io/io/inc/TFile.h b/io/io/inc/TFile.h
index 16110b3b128..c4d2c6b7527 100644
--- a/io/io/inc/TFile.h
+++ b/io/io/inc/TFile.h
@@ -24,6 +24,9 @@
 #ifndef ROOT_TDirectoryFile
 #include "TDirectoryFile.h"
 #endif
+#ifndef ROOT_TMap
+#include "TMap.h"
+#endif
 #ifndef ROOT_TUrl
 #include "TUrl.h"
 #endif
@@ -76,6 +79,7 @@ protected:
    Long64_t         fOffset;         //!Seek offset cache
    TArchiveFile    *fArchive;        //!Archive file from which we read this file
    TFileCacheRead  *fCacheRead;      //!Pointer to the read cache (if any)
+   TMap            *fCacheReadMap;   //!Pointer to the read cache (if any)
    TFileCacheWrite *fCacheWrite;     //!Pointer to the write cache (if any)
    Long64_t         fArchiveOffset;  //!Offset at which file starts in archive
    Bool_t           fIsArchive;      //!True if this is a pure archive file
@@ -172,7 +176,7 @@ public:
    Long64_t            GetArchiveOffset() const { return fArchiveOffset; }
    Int_t               GetBestBuffer() const;
    virtual Int_t       GetBytesToPrefetch() const;
-   TFileCacheRead     *GetCacheRead() const;
+   TFileCacheRead     *GetCacheRead(TObject* tree = 0) const;
    TFileCacheWrite    *GetCacheWrite() const;
    TArrayC            *GetClassIndex() const { return fClassIndex; }
    Int_t               GetCompressionAlgorithm() const;
@@ -228,7 +232,7 @@ public:
    virtual Int_t       Recover();
    virtual Int_t       ReOpen(Option_t *mode);
    virtual void        Seek(Long64_t offset, ERelativeTo pos = kBeg);
-   virtual void        SetCacheRead(TFileCacheRead *cache);
+   virtual void        SetCacheRead(TFileCacheRead *cache, TObject* tree = 0);
    virtual void        SetCacheWrite(TFileCacheWrite *cache);
    virtual void        SetCompressionAlgorithm(Int_t algorithm=0);
    virtual void        SetCompressionLevel(Int_t level=1);
diff --git a/io/io/inc/TFileCacheRead.h b/io/io/inc/TFileCacheRead.h
index 29ee96ca64a..344180c8826 100644
--- a/io/io/inc/TFileCacheRead.h
+++ b/io/io/inc/TFileCacheRead.h
@@ -80,7 +80,7 @@ private:
 
 public:
    TFileCacheRead();
-   TFileCacheRead(TFile *file, Int_t buffersize);
+   TFileCacheRead(TFile *file, Int_t buffersize, TObject *tree = 0);
    virtual ~TFileCacheRead();
    virtual void        AddBranch(TBranch * /*b*/, Bool_t /*subbranches*/ = kFALSE) {}
    virtual void        AddBranch(const char * /*branch*/, Bool_t /*subbranches*/ = kFALSE) {}
diff --git a/io/io/src/TFile.cxx b/io/io/src/TFile.cxx
index 15081426e96..20002a135a7 100644
--- a/io/io/src/TFile.cxx
+++ b/io/io/src/TFile.cxx
@@ -154,6 +154,7 @@ TFile::TFile() : TDirectoryFile(), fInfoCache(0)
    fOffset          = 0;
    fArchive         = 0;
    fCacheRead       = 0;
+   fCacheReadMap    = new TMap();
    fCacheWrite      = 0;
    fArchiveOffset   = 0;
    fReadCalls       = 0;
@@ -340,6 +341,7 @@ TFile::TFile(const char *fname1, Option_t *option, const char *ftitle, Int_t com
    fNProcessIDs  = 0;
    fOffset       = 0;
    fCacheRead    = 0;
+   fCacheReadMap = new TMap();
    fCacheWrite   = 0;
    fReadCalls    = 0;
    SetBit(kBinaryFile, kTRUE);
@@ -503,6 +505,7 @@ TFile::~TFile()
    SafeDelete(fOpenPhases);
    SafeDelete(fAsyncHandle);
    SafeDelete(fCacheRead);
+   SafeDelete(fCacheReadMap);
    SafeDelete(fCacheWrite);
 
    R__LOCKGUARD2(gROOTMutex);
@@ -1080,11 +1083,20 @@ void TFile::ResetErrno() const
 }
 
 //______________________________________________________________________________
-TFileCacheRead *TFile::GetCacheRead() const
+TFileCacheRead *TFile::GetCacheRead(TObject* tree) const
 {
    // Return a pointer to the current read cache.
 
-   return fCacheRead;
+   if (!tree) {
+      if (!fCacheRead && fCacheReadMap->GetSize() == 1) {
+         TIter next(fCacheReadMap);
+         return (TFileCacheRead *)fCacheReadMap->GetValue(next());
+      }
+      return fCacheRead;
+   }
+   TFileCacheRead *cache = (TFileCacheRead *)fCacheReadMap->GetValue(tree);
+   if (!cache) return fCacheRead;
+   return cache;
 }
 
 //______________________________________________________________________________
@@ -1993,7 +2005,7 @@ void TFile::SetCompressionSettings(Int_t settings)
 }
 
 //______________________________________________________________________________
-void TFile::SetCacheRead(TFileCacheRead *cache)
+void TFile::SetCacheRead(TFileCacheRead *cache, TObject* tree)
 {
    // Set a pointer to the read cache.
    // NOTE:  This relinquish ownership of the previous cache, so if you do not
@@ -2001,7 +2013,12 @@ void TFile::SetCacheRead(TFileCacheRead *cache)
    // cache), you ought to retrieve (and delete it if needed) using:
    //    TFileCacheRead *older = myfile->GetCacheRead();
 
-   fCacheRead = cache;
+   if (tree) {
+      if (cache) fCacheReadMap->Add(tree, cache);
+      else fCacheReadMap->RemoveEntry(tree);
+   } else {
+      fCacheRead = cache;
+   }
 }
 
 //______________________________________________________________________________
diff --git a/io/io/src/TFileCacheRead.cxx b/io/io/src/TFileCacheRead.cxx
index 5b442f14be1..37ef3752149 100644
--- a/io/io/src/TFileCacheRead.cxx
+++ b/io/io/src/TFileCacheRead.cxx
@@ -82,7 +82,7 @@ TFileCacheRead::TFileCacheRead() : TObject()
 }
 
 //_____________________________________________________________________________
-TFileCacheRead::TFileCacheRead(TFile *file, Int_t buffersize)
+TFileCacheRead::TFileCacheRead(TFile *file, Int_t buffersize, TObject *tree)
            : TObject()
 {
    // Creates a TFileCacheRead data structure.
@@ -147,16 +147,16 @@ TFileCacheRead::TFileCacheRead(TFile *file, Int_t buffersize)
    }
    else {
       fAsyncReading = gEnv->GetValue("TFile.AsyncReading", 0);
-   if (fAsyncReading) {
-      // Check if asynchronous reading is supported by this TFile specialization
-      fAsyncReading = kFALSE;
-      if (file && !(file->ReadBufferAsync(0, 0)))
-         fAsyncReading = kTRUE;
-   }
-   if (!fAsyncReading) {
-      // we use sync primitives, hence we need the local buffer
-      fBuffer = new char[fBufferSize];
-   }
+      if (fAsyncReading) {
+         // Check if asynchronous reading is supported by this TFile specialization
+         fAsyncReading = kFALSE;
+         if (file && !(file->ReadBufferAsync(0, 0)))
+            fAsyncReading = kTRUE;
+      }
+      if (!fAsyncReading) {
+         // we use sync primitives, hence we need the local buffer
+         fBuffer = new char[fBufferSize];
+      }
    }  
 
    fIsSorted    = kFALSE;
@@ -164,7 +164,7 @@ TFileCacheRead::TFileCacheRead(TFile *file, Int_t buffersize)
    fBIsSorted = kFALSE;
    fBIsTransferred = kFALSE;
 
-   if (file) file->SetCacheRead(this);
+   if (file) file->SetCacheRead(this, tree);
 }
 
 //_____________________________________________________________________________
@@ -479,12 +479,10 @@ Int_t TFileCacheRead::ReadBufferExtNormal(char *buf, Long64_t pos, Int_t len, In
          
          if (buf) {
             // disable cache to avoid infinite recursion
-            fFile->SetCacheRead(0);
             if (fFile->ReadBuffer(buf, pos, len)) {
                return -1;
             }
             fFile->SetOffset(pos+len);
-            fFile->SetCacheRead(this);
          }
          
          retval = 1;
diff --git a/io/io/src/TMemFile.cxx b/io/io/src/TMemFile.cxx
index 06a6ff635f4..27357c761ab 100644
--- a/io/io/src/TMemFile.cxx
+++ b/io/io/src/TMemFile.cxx
@@ -317,6 +317,8 @@ void TMemFile::ResetAfterMerge(TFileMergeInfo *info)
    fNProcessIDs  = 0;
    fOffset       = 0;
    fCacheRead    = 0;
+   delete fCacheReadMap;
+   fCacheReadMap = 0;
    fCacheWrite   = 0;
    fReadCalls    = 0;
    if (fFree) {
diff --git a/proof/proofplayer/src/TEventIter.cxx b/proof/proofplayer/src/TEventIter.cxx
index f4fa45f0925..7dede9432e5 100644
--- a/proof/proofplayer/src/TEventIter.cxx
+++ b/proof/proofplayer/src/TEventIter.cxx
@@ -429,7 +429,11 @@ TEventIterTree::TFileTree::~TFileTree()
    // Default dtor.
 
    // Avoid destroying the cache; must be placed before deleting the trees
-   fFile->SetCacheRead(0);
+   TTree *tree = (TTree *)fTrees->First();
+   while (tree) {
+      fFile->SetCacheRead(0, tree);
+      tree = (TTree *)fTrees->After(tree);
+   }
    SafeDelete(fTrees);
    SafeDelete(fFile);
 }
@@ -525,10 +529,10 @@ TTree* TEventIterTree::GetTrees(TDSetElement *elem)
          if (curfile) {
             if (!fTreeCache) {
                main->SetCacheSize(fCacheSize);
-               fTreeCache = (TTreeCache *)curfile->GetCacheRead();
+               fTreeCache = (TTreeCache *)curfile->GetCacheRead(main);
                if (fCacheSize < 0) fCacheSize = main->GetCacheSize();
             } else {
-               curfile->SetCacheRead(fTreeCache);
+               curfile->SetCacheRead(fTreeCache, main);
                fTreeCache->UpdateBranches(main, kTRUE);
             }
             if (fTreeCache) {
diff --git a/tree/tree/inc/TBasket.h b/tree/tree/inc/TBasket.h
index c699b1dcb51..ab6e7ccaa55 100644
--- a/tree/tree/inc/TBasket.h
+++ b/tree/tree/inc/TBasket.h
@@ -85,7 +85,7 @@ public:
            Int_t   ReadBasketBytes(Long64_t pos, TFile *file);
    virtual void    Reset();
 
-           Int_t   LoadBasketBuffers(Long64_t pos, Int_t len, TFile *file);
+           Int_t   LoadBasketBuffers(Long64_t pos, Int_t len, TFile *file, TTree *tree = 0);
    Long64_t        CopyTo(TFile *to);
 
            void    SetBranch(TBranch *branch) { fBranch = branch; }
diff --git a/tree/tree/inc/TChain.h b/tree/tree/inc/TChain.h
index 7a5a15ad9bf..e16cdcda98d 100644
--- a/tree/tree/inc/TChain.h
+++ b/tree/tree/inc/TChain.h
@@ -145,6 +145,7 @@ public:
    virtual Int_t     SetBranchAddress(const char *bname,void *add, TClass *realClass, EDataType datatype, Bool_t isptr);
 
    virtual void      SetBranchStatus(const char *bname, Bool_t status=1, UInt_t *found=0);
+   virtual void      SetCacheSize(Long64_t cacheSize);
    virtual void      SetDirectory(TDirectory *dir);
    virtual void      SetEntryList(TEntryList *elist, Option_t *opt="");
    virtual void      SetEntryListFile(const char *filename="", Option_t *opt="");
diff --git a/tree/tree/src/TBasket.cxx b/tree/tree/src/TBasket.cxx
index b446aa92b98..c4435c164d6 100644
--- a/tree/tree/src/TBasket.cxx
+++ b/tree/tree/src/TBasket.cxx
@@ -221,7 +221,7 @@ Int_t TBasket::GetEntryPointer(Int_t entry)
 }
 
 //_______________________________________________________________________
-Int_t TBasket::LoadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
+Int_t TBasket::LoadBasketBuffers(Long64_t pos, Int_t len, TFile *file, TTree *tree)
 { 
    // Load basket buffers in memory without unziping.
    // This function is called by TTreeCloner.
@@ -244,8 +244,24 @@ Int_t TBasket::LoadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
    fBufferRef->SetParent(file);
    char *buffer = fBufferRef->Buffer();
    file->Seek(pos);
-   if (file->ReadBuffer(buffer,len)) {
-      return 1; //error while reading
+   TFileCacheRead *pf = file->GetCacheRead(tree);
+   if (pf) {
+      Int_t st = pf->ReadBuffer(buffer,pos,len);
+      if (st < 0) {
+         return 1;
+      } else if (st == 0) {
+         // fOffset might have been changed via TFileCacheRead::ReadBuffer(), reset it
+         file->Seek(pos);
+         if (file->ReadBuffer(buffer,len)) {
+            return 1;
+         }
+      }
+      // fOffset might have been changed via TFileCacheRead::ReadBuffer(), reset it
+      file->SetOffset(pos + len);
+   } else {
+      if (file->ReadBuffer(buffer,len)) {
+         return 1; //error while reading
+      }
    }
 
    fBufferRef->SetReadMode();
@@ -412,7 +428,7 @@ Int_t TBasket::ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
    Int_t uncompressedBufferLen;
 
    // See if the cache has already unzipped the buffer for us.
-   TFileCacheRead *pf = file->GetCacheRead();
+   TFileCacheRead *pf = file->GetCacheRead(fBranch->GetTree());
    if (pf) {
       Int_t res = -1;
       Bool_t free = kTRUE;
@@ -443,9 +459,20 @@ Int_t TBasket::ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
        return 1;
    }
 
-   // Read from the file and unstream the header information.
-   if (file->ReadBuffer(readBufferRef->Buffer(),pos,len)) {
-      return 1;
+   if (pf) {
+      Int_t st = pf->ReadBuffer(readBufferRef->Buffer(),pos,len);
+      if (st < 0) {
+         return 1;
+      } else if (st == 0) {
+         if (file->ReadBuffer(readBufferRef->Buffer(),pos,len)) {
+            return 1;
+         }
+      }
+   } else {
+      // Read from the file and unstream the header information.
+      if (file->ReadBuffer(readBufferRef->Buffer(),pos,len)) {
+         return 1;
+      }
    }
    Streamer(*readBufferRef);
    if (IsZombie()) {
diff --git a/tree/tree/src/TBranch.cxx b/tree/tree/src/TBranch.cxx
index af3c0d3f025..d2004211b9e 100644
--- a/tree/tree/src/TBranch.cxx
+++ b/tree/tree/src/TBranch.cxx
@@ -1101,7 +1101,7 @@ TBasket* TBranch::GetBasket(Int_t basketnumber)
       fBasketBytes[basketnumber] = basket->ReadBasketBytes(fBasketSeek[basketnumber],file);
    }
    //add branch to cache (if any)
-   TFileCacheRead *pf = file->GetCacheRead();
+   TFileCacheRead *pf = file->GetCacheRead(fTree);
    if (pf){
       if (pf->IsLearning()) pf->AddBranch(this);
       if (fSkipZip) pf->SetSkipZip();
diff --git a/tree/tree/src/TChain.cxx b/tree/tree/src/TChain.cxx
index 0ad52e01bec..1ba4ac45f46 100644
--- a/tree/tree/src/TChain.cxx
+++ b/tree/tree/src/TChain.cxx
@@ -182,9 +182,9 @@ TChain::~TChain()
    fFiles = 0;
 
    //first delete cache if exists
-   if (fFile && fFile->GetCacheRead()) {
-      delete fFile->GetCacheRead();
-      fFile->SetCacheRead(0);
+   if (fFile && fFile->GetCacheRead(fTree)) {
+      delete fFile->GetCacheRead(fTree);
+      fFile->SetCacheRead(0, fTree);
    }
 
    delete fFile;
@@ -1406,16 +1406,16 @@ Long64_t TChain::LoadTree(Long64_t entry)
    // FIXME: The "unless" case here causes us to leak memory.
    if (fFile) {
       if (!fDirectory->GetList()->FindObject(this)) {
-         tpf = (TTreeCache*) fFile->GetCacheRead();
+         tpf = (TTreeCache*) fFile->GetCacheRead(fTree);
          if (tpf) {
-           tpf->ResetCache();
-           if (tpf->IsEnablePrefetching()){
-              //wait for thread to finish current work
-              tpf->GetPrefetchObj()->GetMutexSynch()->Lock();
-              tpf->GetPrefetchObj()->GetMutexSynch()->UnLock();
-           }
+            tpf->ResetCache();
+            if (tpf->IsEnablePrefetching()){
+               //wait for thread to finish current work
+               tpf->GetPrefetchObj()->GetMutexSynch()->Lock();
+               tpf->GetPrefetchObj()->GetMutexSynch()->UnLock();
+            }
          }
-         fFile->SetCacheRead(0);
+         fFile->SetCacheRead(0, fTree);
          // If the tree has clones, copy them into the chain
          // clone list so we can change their branch addresses
          // when necessary.
@@ -1500,7 +1500,7 @@ Long64_t TChain::LoadTree(Long64_t entry)
    if (tpf) {
       if (fFile) {
          tpf->ResetCache();
-         fFile->SetCacheRead(tpf);
+         fFile->SetCacheRead(tpf, fTree);
          tpf->SetFile(fFile);
          // FIXME: fTree may be zero here.
          tpf->UpdateBranches(fTree);
@@ -2167,6 +2167,20 @@ void TChain::SetAutoDelete(Bool_t autodelete)
    }
 }
 
+void TChain::SetCacheSize(Long64_t cacheSize)
+{
+   TTree::SetCacheSize(cacheSize);
+   TFile* file = GetCurrentFile();
+   if (!file) {
+      return;
+   }
+   TFileCacheRead* pf = file->GetCacheRead(this);
+   if (pf) {
+      file->SetCacheRead(0, this);
+      file->SetCacheRead(pf, fTree);
+   }
+}
+
 //______________________________________________________________________________
 void TChain::ResetBranchAddress(TBranch *branch)
 {
diff --git a/tree/tree/src/TTree.cxx b/tree/tree/src/TTree.cxx
index 114cecb5d56..0faadc3fbab 100644
--- a/tree/tree/src/TTree.cxx
+++ b/tree/tree/src/TTree.cxx
@@ -529,7 +529,7 @@ Long64_t TTree::TClusterIterator::GetEstimatedClusterSize()
          // Humm ... let's double check on the file.
          TFile *file = fTree->GetCurrentFile();
          if (file) {
-            TFileCacheRead *cache = file->GetCacheRead();
+            TFileCacheRead *cache = file->GetCacheRead(fTree);
             if (cache) {
                cacheSize = cache->GetBufferSize();
             }
@@ -784,15 +784,10 @@ TTree::~TTree()
       //delete the file cache if it points to this Tree
       TFile *file = fDirectory->GetFile();
       if (file) {
-         TFileCacheRead *pf = file->GetCacheRead();
-         if (pf && pf->InheritsFrom(TTreeCache::Class())) {
-            TTreeCache *tpf = (TTreeCache*)pf;
-            if (tpf->GetOwner() == this) {
-               delete tpf;
-               tpf = 0;
-               file->SetCacheRead(0);
-            }
-         }
+         TFileCacheRead *pf = file->GetCacheRead(this);
+         delete pf;
+         pf = 0;
+         file->SetCacheRead(0,this);
       }
    }
    // We don't own the leaves in fLeaves, the branches do.
@@ -893,7 +888,7 @@ void TTree::AddBranchToCache(const char*bname, Bool_t subbranches)
    
    TFile *f = GetCurrentFile();
    if (!f) return;
-   TTreeCache *tc = (TTreeCache*)f->GetCacheRead();
+   TTreeCache *tc = (TTreeCache*)f->GetCacheRead(this);
    if (tc) tc->AddBranch(bname,subbranches);
 }
 
@@ -906,7 +901,7 @@ void TTree::AddBranchToCache(TBranch *b, Bool_t subbranches)
    
    TFile *f = GetCurrentFile();
    if (!f) return;
-   TTreeCache *tc = (TTreeCache*)f->GetCacheRead();
+   TTreeCache *tc = (TTreeCache*)f->GetCacheRead(this);
    if (tc) tc->AddBranch(b,subbranches);
 }
 
@@ -920,7 +915,7 @@ void TTree::DropBranchFromCache(const char*bname, Bool_t subbranches)
    
    TFile *f = GetCurrentFile();
    if (!f) return;
-   TTreeCache *tc = (TTreeCache*)f->GetCacheRead();
+   TTreeCache *tc = (TTreeCache*)f->GetCacheRead(this);
    if (tc) tc->DropBranch(bname,subbranches);
 }
 
@@ -933,7 +928,7 @@ void TTree::DropBranchFromCache(TBranch *b, Bool_t subbranches)
    
    TFile *f = GetCurrentFile();
    if (!f) return;
-   TTreeCache *tc = (TTreeCache*)f->GetCacheRead();
+   TTreeCache *tc = (TTreeCache*)f->GetCacheRead(this);
    if (tc) tc->DropBranch(b,subbranches);
 }
 
@@ -6100,7 +6095,7 @@ void TTree::PrintCacheStats(Option_t* option) const
 
    TFile *f = GetCurrentFile();
    if (!f) return;
-   TTreeCache *tc = (TTreeCache*)f->GetCacheRead();
+   TTreeCache *tc = (TTreeCache*)f->GetCacheRead(const_cast<TTree*>(this));
    if (tc) tc->Print(option);
 }
 
@@ -7126,7 +7121,7 @@ void TTree::SetCacheSize(Long64_t cacheSize)
       fCacheSize = cacheSize;
       return;
    }
-   TFileCacheRead* pf = file->GetCacheRead();
+   TFileCacheRead* pf = file->GetCacheRead(this);
    if (pf) {
       if (cacheSize == fCacheSize) {
          return;
@@ -7134,7 +7129,7 @@ void TTree::SetCacheSize(Long64_t cacheSize)
       delete pf;
       pf = 0;
       if (cacheSize == 0) {
-         file->SetCacheRead(0);
+         file->SetCacheRead(0, this);
          fCacheSize=0;
          return;
       }
@@ -7157,7 +7152,7 @@ void TTree::SetCacheEntryRange(Long64_t first, Long64_t last)
 
    TFile *f = GetCurrentFile();
    if (!f) return;
-   TTreeCache *tc = (TTreeCache*)f->GetCacheRead();
+   TTreeCache *tc = (TTreeCache*)f->GetCacheRead(this);
    if (tc) tc->SetEntryRange(first,last);
 }
 
@@ -7654,7 +7649,7 @@ void TTree::StopCacheLearningPhase()
 
    TFile *f = GetCurrentFile();
    if (!f) return;
-   TTreeCache *tc = (TTreeCache*)f->GetCacheRead();
+   TTreeCache *tc = (TTreeCache*)f->GetCacheRead(this);
    if (tc) tc->StopLearningPhase();
 }
 
diff --git a/tree/tree/src/TTreeCache.cxx b/tree/tree/src/TTreeCache.cxx
index db640218a81..c317370a963 100644
--- a/tree/tree/src/TTreeCache.cxx
+++ b/tree/tree/src/TTreeCache.cxx
@@ -272,7 +272,7 @@ TTreeCache::TTreeCache() : TFileCacheRead(),
 }
 
 //______________________________________________________________________________
-TTreeCache::TTreeCache(TTree *tree, Int_t buffersize) : TFileCacheRead(tree->GetCurrentFile(),buffersize),
+TTreeCache::TTreeCache(TTree *tree, Int_t buffersize) : TFileCacheRead(tree->GetCurrentFile(),buffersize,tree),
    fEntryMin(0),
    fEntryMax(tree->GetEntriesFast()),
    fEntryCurrent(-1),
diff --git a/tree/tree/src/TTreeCloner.cxx b/tree/tree/src/TTreeCloner.cxx
index af77028c4fb..3aa096ab7fb 100644
--- a/tree/tree/src/TTreeCloner.cxx
+++ b/tree/tree/src/TTreeCloner.cxx
@@ -637,7 +637,7 @@ void TTreeCloner::WriteBaskets()
          }
          Int_t len = from->GetBasketBytes()[index];
 
-         basket->LoadBasketBuffers(pos,len,fromfile);
+         basket->LoadBasketBuffers(pos,len,fromfile,fFromTree);
          basket->IncrementPidOffset(fPidOffset);
          basket->CopyTo(tofile);
          to->AddBasket(*basket,kTRUE,fToStartEntries + from->GetBasketEntry()[index]);
diff --git a/tree/treeplayer/src/TTreePlayer.cxx b/tree/treeplayer/src/TTreePlayer.cxx
index 4329c7a3ffb..09476ffe91c 100644
--- a/tree/treeplayer/src/TTreePlayer.cxx
+++ b/tree/treeplayer/src/TTreePlayer.cxx
@@ -2129,12 +2129,12 @@ Long64_t TTreePlayer::Process(TSelector *selector,Option_t *option, Long64_t nen
       TTreeCache *tpf = 0;
       TFile *curfile = fTree->GetCurrentFile();
       if (curfile && fTree->GetCacheSize() > 0) {
-         tpf = (TTreeCache*)curfile->GetCacheRead();
+         tpf = (TTreeCache*)curfile->GetCacheRead(fTree);
          if (tpf)
             tpf->SetEntryRange(firstentry,firstentry+nentries);
          else {
             fTree->SetCacheSize(fTree->GetCacheSize());
-            tpf = (TTreeCache*)curfile->GetCacheRead();
+            tpf = (TTreeCache*)curfile->GetCacheRead(fTree);
             if (tpf) tpf->SetEntryRange(firstentry,firstentry+nentries);
          }
       }
@@ -2188,7 +2188,7 @@ Long64_t TTreePlayer::Process(TSelector *selector,Option_t *option, Long64_t nen
       {
          TFile *curfile2 = fTree->GetCurrentFile();
          if (curfile2 && fTree->GetCacheSize() > 0) {
-            tpf = (TTreeCache*)curfile2->GetCacheRead();
+            tpf = (TTreeCache*)curfile2->GetCacheRead(fTree);
             if (tpf) tpf->SetEntryRange(0,0);
          }
       }
-- 
GitLab