# Projecting Tree to Histogram

**URL:** <https://root-forum.cern.ch/t/projecting-tree-to-histogram/37132>\
**Category:** Newbie\
**Created:** [December 9, 2019, 9:41pm UTC](https://root-forum.cern.ch/t/projecting-tree-to-histogram/37132 "2019-12-09T21:41:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcconn13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/aca169/32.png) [@mcconn13](https://root-forum.cern.ch/u/mcconn13)\
**Post date:** [December 9, 2019, 9:41pm UTC](https://root-forum.cern.ch/t/projecting-tree-to-histogram/37132/1 "2019-12-09T21:41:13Z")

</div>

Hello all,

Being really new to ROOT I thought it best to post this here. I started with something basic (or so I thought) and got stuck.

I have an ASCII text file that contains photon energy spectra for several radionuclides. I bring the data into root using a TTree and then try to TTree::Project a portion of the data to a TH1 using the following code:

```auto
  TTree *data = new TTree("data", "six columns");
  TH1D hCo57("hCo57", "Co57 spectrum", 256, 0.0, 1.0);

  // assuming six columns separated by whitespaces
  data->ReadFile("BasicSources.txt", "Energy:Bkgd:Ba133:Co57:Co60:Cs137");
  TCanvas *c1 = new TCanvas("c1", "c1");
  TCanvas *c2 = new TCanvas("c2", "c2");

  c1->cd();
  c1->SetTitle("Basic Source Spectra in PVT");
  data->Draw("Bkgd:Energy","","L");
  data->Draw("Ba133:Energy","","Lsame");
  data->Draw("Co57:Energy","","Lsame");
  data->Draw("Co60:Energy","","Lsame");
  data->Draw("Cs137:Energy","","Lsame");
  c1->Update();
// data->Scan();

// These commands print the data to a histogram and show the info
// THE HIST DOESN'T PLOT YET
  data->Project("hCo57","Co57");
  c2->cd();
  hCo57.Draw("");
  c2->SetTitle("Co-57 Spectrum");
  hCo57.GetXaxis()->SetTitle("Energy");
  hCo57.GetYaxis()->SetTitle("Counts");
  c2->Update();
  hCo57.Print("all");

```

I used the TTree::Scan method to check the data to ensure that it was reading properly which it does. But then when I try to draw the histogram the plot is empty though plotting the tree works fine. After much tinkering I decided to check the projected data using the TH1::Print(“all”) method to print the data. What I have found is that the projection did not give me the data I thought it would.

What I’m trying to do is plot the data in the TTree which I can do. I’d like to know how to set the y-axis to log format automatically but that’s another matter. Then populate a histogram for other plotting/analysis.

I’m fairly certain that the Project method call is wrong and suspect I actually need to be using a TH2. I have tried to use a TH2 but that fails as well.

thanks all for the help,

Ron

\_ROOT Version:\_6.18.04  
_Platform:_ Ubuntu using Windows subsystem for Linux  
_Compiler:_ Using precompiled binaries

---

<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 10, 2019, 7:10am UTC](https://root-forum.cern.ch/t/projecting-tree-to-histogram/37132/2 "2019-12-10T07:10:53Z")

</div>

Try:

```auto
  // a histogram with automatic bins, created on the heap
  TH1D *hCo57 = new TH1D("hCo57", "Co57 spectrum;Energy;Counts", 256, 0, 0);
  data->Project("hCo57", "Co57");
  hCo57->Draw("");
  gPad->SetLogy(1);

```

---

<div class="post-metadata">

**Author:** ![mcconn13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/aca169/32.png) [@mcconn13](https://root-forum.cern.ch/u/mcconn13)\
**Post date:** [December 10, 2019, 4:55pm UTC](https://root-forum.cern.ch/t/projecting-tree-to-histogram/37132/3 "2019-12-10T16:55:31Z")

</div>

Thanks for the reply. That did the trick. Also, thanks for pointing out the axis command. I found that probably 5 minutes after my post which figures. So much left to learn.
