I have a root file which has a tree, a branch and then leaves. The structure is shown in the picture here: . It seems like I cannot read it simply by using tree->SetBranchAddress.
Moreover, the leaves are have dots (.) in their name which is causing problems, eg: "ReconstructedParticles.p.x or mcparticles2.ID. This is the macro that I prepared: macro.C (1.9 KB)
There is an extra trailing space in the string specifying the ROOT file name
The C++ identifies, unlike branch names, must not contain dots (e.g., Float_t ReconstructedParticles.p.x is invalid but Float_t px would be ok)
You’d only need to change the style of the histograms once outside the event loop.
The actual problem, however, is that the file contains a vector of particles in every event and not just one particle. One possibility to fill the histograms would be using a TTreeReader like this
TFile *f = new TFile("FileName.root");
TTreeReader reader("events", f);
TTreeReaderArray<Float_t> pxArray(reader, "ReconstructedParticles.p.x");
TH1F *h1 = new TH1F("px","",100, -20, 20);
h1->GetXaxis()->SetTitle("px");
h1->GetXaxis()->SetTitleSize(0.05);
h1->GetXaxis()->CenterTitle();
h1->SetLineWidth(2);
while (reader.Next()) {
for (auto px: pxArray)
h1->Fill(px);
}
TFile * f1 = new TFile("output.root","RECREATE");
h1->Write();
f1->Close();