Advertisement
Guest User

TTreeCache ROOT bug

a guest
Dec 19th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "TString.h"
  2. #include <TFile.h>
  3. #include <TTree.h>
  4. #include <TNtuple.h>
  5. #include <TTreeCache.h>
  6.  
  7.  
  8. void test_cache() {
  9.    TString in_fname("/tmp/test_in.root");
  10.  
  11.    // create a tree in file
  12.    {
  13.       TFile * in_file = TFile::Open(in_fname, "RECREATE");
  14.       in_file->cd();
  15.  
  16.       TNtuple * in_tree = new TNtuple("in_tree", "blah","a:b:c");
  17.       for (int i = 0; i < 10; ++i) {
  18.          in_tree->Fill(i,2 * i,3 * i);
  19.       }
  20.  
  21.       in_tree->Write();
  22.       in_file->Close();
  23.    }
  24.  
  25.    TFile * in_file = TFile::Open(in_fname);
  26.  
  27.    TNtuple * in_tree = (TNtuple*) in_file->Get("in_tree");
  28.  
  29.    // set cache on input file (mimic TEventIterTree::GetTrees)
  30.    TTreeCache *fTreeCache = 0;
  31.    int fCacheSize = 100000;
  32.    bool fTreeCacheIsLearning = 1;
  33.  
  34.    TTree * main = (TTree*) in_tree;
  35.    TFile *curfile = main->GetCurrentFile();
  36.    if (!fTreeCache) {
  37.       main->SetCacheSize(fCacheSize);
  38.       fTreeCache = (TTreeCache *)curfile->GetCacheRead(main);
  39.       if (fCacheSize < 0) fCacheSize = main->GetCacheSize();
  40.    } else {
  41.       curfile->SetCacheRead(fTreeCache, main);
  42.       fTreeCache->UpdateBranches(main);
  43.    }
  44.    if (fTreeCache) {
  45.       fTreeCacheIsLearning = fTreeCache->IsLearning();
  46.       //~ if (fTreeCacheIsLearning)
  47.          //~ Info("GetTrees","the tree cache is in learning phase");
  48.    }
  49.  
  50.  
  51.  
  52.    TFile * out_file = TFile::Open("/tmp/test_out.root", "RECREATE");
  53.    in_file->cd(); // Probably somewhere inside PROOF code
  54.  
  55.    TNtuple * out_tree = (TNtuple*) in_tree->CloneTree(0);
  56.    out_tree->SetName("out_tree");
  57.  
  58.  
  59.    // vvv uncomment the following line to work around the bug
  60.    //~ out_tree->SetDirectory(0);
  61.  
  62.  
  63.    out_tree->SetDirectory(out_file);
  64.    out_tree->SetAutoSave();
  65.    out_tree->Fill(-1, -1, -1);
  66.  
  67.  
  68.    in_file->Close();
  69.  
  70.    out_file->cd();
  71.    out_tree->Write();
  72.    out_file->Close();
  73.  
  74. }
  75.  
  76. int main() {
  77.    test_cache();
  78.    return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement