Introduced tree caching and async reading for data (ESD and AOD) and MC. An read...
authoragheata <agheata@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 4 Oct 2012 08:58:55 +0000 (08:58 +0000)
committeragheata <agheata@f7af4fe6-9843-0410-8265-dc069ae4e863>
Thu, 4 Oct 2012 08:58:55 +0000 (08:58 +0000)
ANALYSIS/AliAnalysisManager.cxx
ANALYSIS/AliAnalysisManager.h
STEER/STEERBase/AliMCEventHandler.cxx
STEER/STEERBase/AliMCEventHandler.h
STEER/STEERBase/AliVEventHandler.h

index 620a873..36fb766 100644 (file)
@@ -33,6 +33,8 @@
 #include <TMap.h>
 #include <TClass.h>
 #include <TFile.h>
+#include <TTreeCache.h>
+#include <TEnv.h>
 #include <TMath.h>
 #include <TH1.h>
 #include <TMethodCall.h>
@@ -97,10 +99,12 @@ AliAnalysisManager::AliAnalysisManager(const char *name, const char *title)
                     fExtraFiles(),
                     fFileInfoLog(),
                     fAutoBranchHandling(kTRUE), 
+                    fAsyncReading(kTRUE), // default prefetching on
                     fTable(),
                     fRunFromPath(0),
                     fNcalls(0),
                     fMaxEntries(0),
+                    fCacheSize(100000000), // default 100 MB
                     fStatisticsMsg(),
                     fRequestedBranches(),
                     fStatistics(0),
@@ -165,10 +169,12 @@ AliAnalysisManager::AliAnalysisManager(const AliAnalysisManager& other)
                     fExtraFiles(other.fExtraFiles),
                     fFileInfoLog(other.fFileInfoLog),
                     fAutoBranchHandling(other.fAutoBranchHandling), 
+                    fAsyncReading(other.fAsyncReading),
                     fTable(),
                     fRunFromPath(0),
                     fNcalls(other.fNcalls),
                     fMaxEntries(other.fMaxEntries),
+                    fCacheSize(other.fCacheSize),
                     fStatisticsMsg(other.fStatisticsMsg),
                     fRequestedBranches(other.fRequestedBranches),
                     fStatistics(other.fStatistics),
@@ -229,10 +235,12 @@ AliAnalysisManager& AliAnalysisManager::operator=(const AliAnalysisManager& othe
       fgCommonFileName = "AnalysisResults.root";
       fgAnalysisManager = this;
       fAutoBranchHandling = other.fAutoBranchHandling;
+      fAsyncReading = other.fAsyncReading;
       fTable.Clear("nodelete");
       fRunFromPath = other.fRunFromPath;
       fNcalls     = other. fNcalls;
       fMaxEntries = other.fMaxEntries;
+      fCacheSize = other.fCacheSize;
       fStatisticsMsg = other.fStatisticsMsg;
       fRequestedBranches = other.fRequestedBranches;
       fStatistics = other.fStatistics;
@@ -273,6 +281,30 @@ AliAnalysisManager::~AliAnalysisManager()
 }
 
 //______________________________________________________________________________
+void AliAnalysisManager::CreateReadCache()
+{
+// Create cache for reading according fCacheSize and fAsyncReading.
+   if (!fTree || !fTree->GetCurrentFile()) {
+      Error("CreateReadCache","Current tree or tree file not yet defined");
+      return;
+   }   
+   if (!fCacheSize) {
+      if (fDebug) Info("CreateReadCache","=== Read caching disabled ===");
+      return;
+   }
+//   gEnv->SetValue("TFile.AsyncPrefetching",(Int_t)fAsyncReading);
+//   if (fAsyncReading) gEnv->SetValue("Cache.Directory",Form("file://%s/cache", gSystem->WorkingDirectory()));
+   if (fAsyncReading) gEnv->SetValue("TFile.AsyncReading",1);
+   fTree->SetCacheSize(fCacheSize);
+   TTreeCache::SetLearnEntries(1);  //<<< we can take the decision after 1 entry
+   fTree->AddBranchToCache("*",kTRUE);    //<<< add all branches to the cache
+   if (fDebug) {
+      Info("CreateReadCache","Read cache enabled %lld bytes with async reading=%d",fCacheSize, (Int_t)fAsyncReading);
+   }
+   return;
+}   
+      
+//______________________________________________________________________________
 Int_t AliAnalysisManager::GetEntry(Long64_t entry, Int_t getall)
 {
 // Read one entry of the tree or a whole branch.
@@ -383,6 +415,7 @@ Bool_t AliAnalysisManager::Init(TTree *tree)
    if (!fInitOK) InitAnalysis();
    if (!fInitOK) return kFALSE;
    fTree = tree;
+   CreateReadCache();
    fTable.Rehash(100);
    AliAnalysisDataContainer *top = fCommonInput;
    if (!top) top = (AliAnalysisDataContainer*)fInputs->At(0);
@@ -408,7 +441,7 @@ void AliAnalysisManager::SlaveBegin(TTree *tree)
    // Init timer should be already started
    // Apply debug options
    ApplyDebugOptions();
-   
+   if (fCacheSize && fMCtruthEventHandler) fMCtruthEventHandler->SetCacheSize(fCacheSize);
    if (!CheckTasks()) Fatal("SlaveBegin", "Not all needed libraries were loaded");
    static Bool_t isCalled = kFALSE;
    Bool_t init = kFALSE;
index 6dfe3fd..b1f441d 100644 (file)
@@ -98,6 +98,7 @@ enum EAliAnalysisFlags {
                        GetAnalysisType() const    {return fMode;}
    void                GetAnalysisTypeString(TString &type) const;                    
    Bool_t              GetAutoBranchLoading() const {return fAutoBranchHandling;} 
+   Long64_t            GetCacheSize() const       {return fCacheSize;}
    static const char  *GetCommonFileName()        {return fgCommonFileName.Data();}
    AliAnalysisDataContainer *
                        GetCommonInputContainer() const  {return fCommonInput;}
@@ -106,6 +107,7 @@ enum EAliAnalysisFlags {
    TObjArray          *GetContainers() const      {return fContainers;}
    Long64_t            GetCurrentEntry() const    {return fCurrentEntry;}
    UInt_t              GetDebugLevel() const      {return fDebug;}
+   Bool_t              GetAsyncReading() const {return fAsyncReading;}
    TString             GetExtraFiles() const      {return fExtraFiles;}
    AliVEventPool*      GetEventPool()  const      {return fEventPool;}
    Bool_t              GetFileFromWrapper(const char *filename, const TList *source);
@@ -139,11 +141,13 @@ enum EAliAnalysisFlags {
    void                SetAnalysisType(EAliAnalysisExecMode mode) {fMode = mode;}
    void                SetAutoBranchLoading(Bool_t b) { fAutoBranchHandling = b; }
    void                SetCurrentEntry(Long64_t entry)            {fCurrentEntry = entry;}
+   void                SetCacheSize(Long64_t size)                {fCacheSize = size;}
    void                SetCollectSysInfoEach(Int_t nevents=0)     {fNSysInfo = nevents;}
    void                SetCollectThroughput(Bool_t flag)          {Changed(); TObject::SetBit(kCollectThroughput,flag);}
    static void         SetCommonFileName(const char *name)        {fgCommonFileName = name;}
    void                SetDebugLevel(UInt_t level);
    void                SetDisableBranches(Bool_t disable=kTRUE)   {Changed(); TObject::SetBit(kDisableBranches,disable);}
+   void                SetAsyncReading(Bool_t flag=kTRUE)    {fAsyncReading = flag;}
    void                SetExternalLoop(Bool_t flag)               {Changed(); TObject::SetBit(kExternalLoop,flag);}
    void                SetEventPool(AliVEventPool* const epool)   {Changed(); fEventPool = epool;}
    void                SetFileInfoLog(const char *name) {TObject::SetBit(kCollectThroughput,kTRUE); fFileInfoLog = name;}
@@ -219,6 +223,7 @@ enum EAliAnalysisFlags {
    void                 UnLock();
    void                 Changed();
 protected:
+   void                 CreateReadCache();
    void                 ImportWrappers(TList *source);
    void                 SetEventLoop(Bool_t flag=kTRUE) {TObject::SetBit(kEventLoop,flag);}
    void                 DoLoadBranch(const char *name);
@@ -255,10 +260,12 @@ private:
    TString                 fExtraFiles;          // List of extra files to be merged
    TString                 fFileInfoLog;         // File name for fileinfo logs
    Bool_t                  fAutoBranchHandling;  // def=kTRUE, turn off if you use LoadBranch
+   Bool_t                  fAsyncReading;        // Enable async reading
    THashTable              fTable;               // keep branch ptrs in case of manual branch loading
    Int_t                   fRunFromPath;         // Run number retrieved from path to input data
    Int_t                   fNcalls;              // Total number of calls (events) of ExecAnalysis
    Long64_t                fMaxEntries;          // Maximum number of entries
+   Long64_t                fCacheSize;           // Cache size in bytes
    static Int_t            fPBUpdateFreq;        // Progress bar update freq.
    TString                 fStatisticsMsg;       // Statistics user message
    TString                 fRequestedBranches;   // Requested branch names
@@ -272,6 +279,6 @@ private:
    Double_t                fInitTime;            //! Cumulated time in initialization
    static TString          fgCommonFileName;     //! Common output file name (not streamed)
    static AliAnalysisManager *fgAnalysisManager; //! static pointer to object instance
-   ClassDef(AliAnalysisManager,17)  // Analysis manager class
+   ClassDef(AliAnalysisManager,18)  // Analysis manager class
 };   
 #endif
index 47fcaf2..8bbce32 100644 (file)
@@ -35,6 +35,7 @@
 #include "AliLog.h"
 
 #include <TTree.h>
+#include <TTreeCache.h>
 #include <TFile.h>
 #include <TList.h>
 #include <TParticle.h>
@@ -67,7 +68,10 @@ AliMCEventHandler::AliMCEventHandler() :
     fInitOk(kFALSE),
     fSubsidiaryHandlers(0),
     fEventsInContainer(0),
-    fPreReadMode(kNoPreRead)
+    fPreReadMode(kNoPreRead),
+    fCacheSize(0),
+    fCacheTK(0),
+    fCacheTR(0)
 {
   //
   // Default constructor
@@ -99,7 +103,10 @@ AliMCEventHandler::AliMCEventHandler(const char* name, const char* title) :
     fInitOk(kFALSE),
     fSubsidiaryHandlers(0),
     fEventsInContainer(0),
-    fPreReadMode(kNoPreRead)
+    fPreReadMode(kNoPreRead),
+    fCacheSize(0),
+    fCacheTK(0),
+    fCacheTR(0)
 {
   //
   // Constructor
@@ -115,6 +122,8 @@ AliMCEventHandler::~AliMCEventHandler()
     delete fFileE;
     delete fFileK;
     delete fFileTR;
+    delete fCacheTK;
+    delete fCacheTR;
 }
 
 Bool_t AliMCEventHandler::Init(Option_t* opt)
@@ -184,7 +193,10 @@ Bool_t AliMCEventHandler::LoadEvent(Int_t iev)
   if (!fInitOk) return kFALSE;
     
   Int_t inew  = iev / fEventsPerFile;
+  Bool_t firsttree = (fTreeK==0) ? kTRUE : kFALSE;
+  Bool_t newtree = firsttree;
   if (inew != fFileNumber) {
+    newtree = kTRUE;
     fFileNumber = inew;
     if (!OpenFile(fFileNumber)){
       return kFALSE;
@@ -227,8 +239,35 @@ Bool_t AliMCEventHandler::LoadEvent(Int_t iev)
     // Connect TR to MCEvent
     fMCEvent->ConnectTreeTR(fTreeTR);
   }
-
-  //
+  // Now setup the caches if not yet done
+  if (fCacheSize) {
+    if (firsttree) {
+      fTreeK->SetCacheSize(fCacheSize);
+      fCacheTK = (TTreeCache*) fFileK->GetCacheRead(fTreeK);
+      TTreeCache::SetLearnEntries(1);
+      fTreeK->AddBranchToCache("*",kTRUE);
+      Info("LoadEvent","Read cache enabled %lld bytes for TreeK",fCacheSize);
+      if (fTreeTR) {
+        fTreeTR->SetCacheSize(fCacheSize);
+        fCacheTR = (TTreeCache*) fFileTR->GetCacheRead(fTreeTR);
+        TTreeCache::SetLearnEntries(1);
+        fTreeTR->AddBranchToCache("*",kTRUE);
+        Info("LoadEvent","Read cache enabled %lld bytes for TreeTR",fCacheSize);
+      } 
+    } else {
+      // We need to reuse the previous caches and every new event is a new tree
+      if (fCacheTK) {
+         fCacheTK->ResetCache();
+         if (fFileK) fFileK->SetCacheRead(fCacheTK, fTreeK);
+         fCacheTK->UpdateBranches(fTreeK);
+      }
+      if (fCacheTR) {
+         fCacheTR->ResetCache();
+         if (fFileTR) fFileTR->SetCacheRead(fCacheTR, fTreeTR);
+         fCacheTR->UpdateBranches(fTreeTR);
+      }   
+    }
+  }  
   return kTRUE;
 }
 
@@ -241,7 +280,7 @@ Bool_t AliMCEventHandler::OpenFile(Int_t i)
        fkExtension = "";
     }
     
-    
+    if (fFileK && fCacheTK) fFileK->SetCacheRead(0, fTreeK);
     delete fFileK;
     fFileK = TFile::Open(Form("%sKinematics%s.root", fPathName->Data(), fkExtension));
     if (!fFileK) {
@@ -251,13 +290,14 @@ Bool_t AliMCEventHandler::OpenFile(Int_t i)
     }
     
     if (fReadTR) {
-       delete fFileTR;
-       fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fkExtension));
-       if (!fFileTR) {
-           AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fkExtension, fPathName->Data()));
-           fInitOk = kFALSE;
-           return kFALSE;
-       }
+      if (fFileTR && fCacheTR) fFileTR->SetCacheRead(0, fTreeTR);
+      delete fFileTR;
+      fFileTR = TFile::Open(Form("%sTrackRefs%s.root", fPathName->Data(), fkExtension));
+      if (!fFileTR) {
+        AliWarning(Form("AliMCEventHandler:TrackRefs%s.root not found in directory %s ! \n", fkExtension, fPathName->Data()));
+        fInitOk = kFALSE;
+        return kFALSE;
+      }
     }
     
     fInitOk = kTRUE;
@@ -494,8 +534,10 @@ void AliMCEventHandler::ResetIO()
 Bool_t AliMCEventHandler::FinishEvent()
 {
     // Clean-up after each event
-    delete fDirTR;  fDirTR = 0;
-    delete fDirK;   fDirK  = 0;    
+   if (fFileK && fCacheTK) fFileK->SetCacheRead(0, fTreeK);
+   if (fFileTR && fCacheTR) fFileTR->SetCacheRead(0, fTreeTR);
+   delete fDirTR;  fDirTR = 0;
+   delete fDirK;   fDirK  = 0;    
     if (fInitOk) fMCEvent->FinishEvent();
 
     if (fSubsidiaryHandlers) {
index 8fc8a39..5c1e6df 100644 (file)
@@ -22,6 +22,7 @@
 class TFile;
 class TTree;
 class TList;
+class TTreeCache;
 
 class TParticle;
 class TString;
@@ -62,6 +63,7 @@ public:
     virtual void         AddSubsidiaryHandler(AliMCEventHandler* handler);
     virtual void         SetNumberOfEventsInContainer(Int_t nev) {fEventsInContainer = nev;}
     virtual void         SetPreReadMode(PreReadMode_t mode) {Changed(); fPreReadMode = mode;}
+    virtual void         SetCacheSize(Long64_t size) {if (fCacheSize==0) fCacheSize = size;}
     //
     AliMCEvent* MCEvent() const {return fMCEvent;}
     TTree*      TreeTR()  const {return fTreeTR;}
@@ -104,8 +106,11 @@ private:
     TList                 *fSubsidiaryHandlers; //! List of subsidiary MC handlers (for example for Background)
     Int_t                  fEventsInContainer;  //! Number of events in container class
     PreReadMode_t          fPreReadMode;        // Pre reading mode
+    Long64_t               fCacheSize;          // Cache size for kinematics tree
+    TTreeCache            *fCacheTK;            //! Cache for kinematics tree
+    TTreeCache            *fCacheTR;            //! Cache for track references tree
     
-    ClassDef(AliMCEventHandler,2)  //MC Truth EventHandler class
+    ClassDef(AliMCEventHandler,3)  //MC Truth EventHandler class
 };
 #endif 
 
index cc71225..c70aad7 100644 (file)
@@ -51,6 +51,7 @@ enum EEventHandlerFlags {
     void                 Lock();
     void                 UnLock();
     void                 Changed();
+    virtual void         SetCacheSize(Long64_t) {}
  private :
   ClassDef(AliVEventHandler, 1);
 };