Histogram from a tree

Hi,

So I have a tree from a data file that I want to create a histogram from this data. The following is my code:

  TTree *T = new TTree("ntuple","csv file data");
  Long64_t nlines = T->ReadFile(TString::Format("%sdata%d.csv",dir.Data(), i),"a:b:c:d:e");
  printf(" found %lld points\n",nlines);

  
  TH1F *H = new TH1F("H", "signal distribution", 100000, -1, 1);

  T->Draw("a","b");

I have this inside a for loop, looping over several data files in the directory.
I have no trouble drawing the tree, it works perfectly. But I want to fill the histogram with this data and plot it so that I can use its drawing and fitting options.

I tried
H->Fill(nlines);
and then drawing it, but it didn’t work.

I’m sure it’s a relatively simple step that I am missing, but it’s tripping me up.

Thank you

Either:
T->Project(“H”, “a”, “b”);
or:
T->Draw(“a >> H”, “b”, “goff”);

1 Like

Cool, the Draw option worked. It shows a histogram of my data, but it is filled in with a color. I wish to only have the data points shown. Is there an easy manner of doing so? I’ve looked at https://root.cern/doc/master/classTHistPainter.html
and
https://root.cern.ch/doc/v608/classTAttFill.html#a70a111d74324b6a6deaee4a23eb65ff4
but I haven’t been able to find anything.

Again, thanks much

Instead of “goff”, you can use any drawing option suitable for 1D histograms.

Another quick question:

When you use
T->Draw(“a >> H”, “b”, “goff”);
Is it merely giving H the information of column “a”?

Search for “>>” in the TTree::Draw method description.

Hi,
if you have access to ROOT 6.10, this is also the perfect use case for TDataFrame:

TTree t("ntuple","csv file data");
t.ReadFile(TString::Format("%sdata%d.csv",dir.Data(), i),"a:b:c:d:e");
ROOT::Experimental::TDataFrame d(t);
auto h = d.Histo1D("a", "b"); // create histogram of column a using b as weights
h->Draw();
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.