# Filling a histogram in Rint from a TFile Tree

**URL:** <https://root-forum.cern.ch/t/filling-a-histogram-in-rint-from-a-tfile-tree/48745>\
**Category:** Newbie\
**Tags:** tree, hist\
**Created:** [February 10, 2022, 1:43pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-in-rint-from-a-tfile-tree/48745 "2022-02-10T13:43:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![preimer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@preimer](https://root-forum.cern.ch/u/preimer)\
**Post date:** [February 10, 2022, 1:43pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-in-rint-from-a-tfile-tree/48745/1 "2022-02-10T13:43:53Z")

</div>

Hello,

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.

```auto
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.

Thanks,

Paul

_ROOT Version:_ 6.24/06  
_Platform:_ OS-X 11.6.2  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 10, 2022, 1:55pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-in-rint-from-a-tfile-tree/48745/2 "2022-02-10T13:55:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![preimer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@preimer](https://root-forum.cern.ch/u/preimer)\
**Post date:** [February 10, 2022, 1:58pm UTC](https://root-forum.cern.ch/t/filling-a-histogram-in-rint-from-a-tfile-tree/48745/3 "2022-02-10T13:58:00Z")

</div>

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

Thanks,

Paul
