Creating a histogram from a TTree

Gabriel Teixeira picture Gabriel Teixeira · Jan 22, 2015 · Viewed 7.5k times · Source

I'm trying to create a 1D histogram from a TTree that contains only one variable.

TTree *tree = new TTree("tree", "");
tree->ReadFile("occupancyPerDataset.txt", "size");

TH1F *occupancy = new TH1F("occupancy", "Occupancy per Dataset", 100, 0,      0.063)

Above you can see I created the tree and filled it with the data from a .txt file. I then created the histogram with the number of bins, x_low, and x_high that I need. When I try to do

occupancy->Fill(tree);

or

occupancy->Fill(size);

I get an error. Any suggestions?

Answer

Asen Christov picture Asen Christov · Apr 29, 2015

The the Fill method of TH1 (TH1F is daughter class) doesn't take the tree as parameter.

You have two options:

  1. loop over the tree entries and fill them in the histogram one by one.

    e.g. by calling tree->MakeClass("MYTREE") and then manipulating the Loop() function in MYTREE.C by adding

    occupancy->Fill(VARIABLENAME);
    

    into the loop and execute the resulting code.

  2. use the Draw method of TTree:

    tree->Draw("VARIABLENAME>>HISTOGRAMNAME");