Filling a histogram in Rint from a TFile Tree


I would like to create a histogram in root’s memory, open a TFile, fill the histogram from a TTree in that file, close the file and still have the filled histogram in memory. Below is a snippet of code that does not do what I want, as myHist ends up in myFile.

TH1D *myHist = new TH1D("myHist", "myHist", 100, 2, 7);

int myMacro() {

  TTree *myTree;
  TFile *myTFile = new TFile("myRootFile.root", "r");
  myTFile->GetObject("Tree", myTree);
  myTree->Project("myHist", "mass"); // this apparently fills a copy of myHist
                                     // in the myRootFile.root: directory
  myHist->Draw(); // this apparently draws the (empty) myHist from Rint:

  // what I would like is to have a _filled_ version in myHist that exists in memory even after
  // myTFile->Close();
  // myTFile->~TFile;
  return 1;

This clearly has to do with a misunderstanding that I have about the directory structure within root, so I would appreciate being pointed in the right direction.



ROOT Version: 6.24/06
Platform: OS-X 11.6.2
Compiler: Not Provided

Right after opening the ROOT file, add: gROOT->cd();

Worked perfectly–I knew it was a simple problem, just didn’t know the answer.