TChain and generating histograms

Hi,
I have a .root file with four TTrees and each of them has a branch with the name ‘InvaraintMass’. I want to make a single histogram with all the four InvariantMass overlaying on each other. I do not know how to make a single histogram from many TTrees.

Kindly help me with this.

Thank you very much!

What do you mean when you say “overlaying”? Do you need to distinguish which came from which file?

If not, you can just use a TChain:

auto chain = new TChain("tree_name");
chain->Add("file_name1.root");
...
chain->Add("file_nameN.root");

chain->Draw("InvariantMass");

If you do want to be able to distinguish them, you probably want to put them into a THStack:

std::vector<std::string> file_names = {"file_name1.root", ..., "file_nameN.root"};
TFile * f;
TTree * tree;
auto histStack = new THStack();
for (auto file_name : file_names) {
    f = TFile::Open(file_name.c_str());
    tree = (TTree*) f->Get("tree_name");
    tree->Draw("InvariantMass>>h(100, 0, 120)");  // Add correct binning here
    histStack->Add((TH1*) gROOT->FindObject("h"));
}
histStack->Draw("PLC");

Hi,
probably something like this would work:

TChain c;
c.Add("file.root/tree1");
c.Add("file.root/tree2");
c.Add("file.root/tree2");
TTreeReader r(&c);
TTreeReaderValue<double> v(r, "InvariantMass");
while (r.Next())
  std::cout << *v << std::endl;

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