# Draw() function not working for Histograms

**URL:** <https://root-forum.cern.ch/t/draw-function-not-working-for-histograms/37260>\
**Category:** Newbie\
**Created:** [December 19, 2019, 4:04am UTC](https://root-forum.cern.ch/t/draw-function-not-working-for-histograms/37260 "2019-12-19T04:04:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfarrington](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@mfarrington](https://root-forum.cern.ch/u/mfarrington)\
**Post date:** [December 19, 2019, 4:04am UTC](https://root-forum.cern.ch/t/draw-function-not-working-for-histograms/37260/1 "2019-12-19T04:04:41Z")

</div>

I am trying to extract data from a tree and put it into a histogram using a c++ program, however I am having the issue that the program creates the TCanvas for the histogram when I use the Draw() function, but I never see a canvas pop up on my screen. For reference, I am using a Windows machine, but am running the code on a linux machine I have connected to using bitvise ssh and am using Xming. I am able to see histograms pop up when I create a TBrowser and click on a branch from the tree. I also tried not even filling the histogram with data from the tree and just trying to get the system to draw a histogram I filled with integers generated by a for loop but that did not work either. I have posted my code below for reference.

```auto
int main()
{
        TFile* F = new TFile("Output.root", "READ", "File");

        TTree* T = (TTree*) F->Get("RecTree");

        TH1* Hist = new TH1I("Hist", "Energy", 100, 0, 1.3);

        int i;
        float Energy;

        T->SetBranchAddress("VetoEnergy", &Energy);

        for(i = 0; i < T->GetEntries(); i++)
        {
              T->GetEntry(i);
              Hist->Fill(Energy);
        }
  
        F->Close();

        Hist->Draw();

        return 0;
}

```

\_ROOT Version: 6.18/04  
\_Platform: Linux and Windows

* * *

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [December 19, 2019, 6:35am UTC](https://root-forum.cern.ch/t/draw-function-not-working-for-histograms/37260/2 "2019-12-19T06:35:56Z")

</div>

> [@mfarrington](#):
>
> **TH1** \* Hist = new **TH1I** (“Hist”, “Energy”, 100, 0, 1.3);

If Energy is a float in the tree, try instead:

```auto
TH1F* Hist = new TH1F("Hist", "Energy", 100, 0, 1.3);

```

(if it’s double, use TH1D).

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [December 19, 2019, 3:35pm UTC](https://root-forum.cern.ch/t/draw-function-not-working-for-histograms/37260/3 "2019-12-19T15:35:13Z")

</div>

Do

```auto
TH1* Hist = new TH1I("Hist", "Energy", 100, 0, 1.3);
Hist->SetDirectory(nullptr);

```

to avoid the histogram from being deleted when the file is Closed.

---

<div class="post-metadata">

**Author:** ![mfarrington](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@mfarrington](https://root-forum.cern.ch/u/mfarrington)\
**Post date:** [December 21, 2019, 3:27am UTC](https://root-forum.cern.ch/t/draw-function-not-working-for-histograms/37260/4 "2019-12-21T03:27:23Z")

</div>

I tried both approaches and neither worked. I also tried simplifying the code down to

```auto
        TH1* Hist = new TH1I("Hist", "Energy", 100, 0, 20);

        int i;
      

        for(i = 0; i < 11; i++)
        {
              Hist->Fill(i);
        }

        Hist->Draw()

```

and that did not work either. Any suggestions? Thanks.

---

<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:** [December 21, 2019, 8:17am UTC](https://root-forum.cern.ch/t/draw-function-not-working-for-histograms/37260/5 "2019-12-21T08:17:07Z")

</div>

Try to connect to your Linux machine using `ssh -X -Y ...` and, if you get no warnings nor errors, as a first test try to start there an `xterm` (and if you get a new terminal window displayed then it should also work for ROOT).
