Projecting Tree to Histogram

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:

  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

Try:

  // 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);
1 Like

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.