Home | News | Documentation | Download

TBranch & TH1D: writing & matching issues

Dear ROOT community,
I have a Python code which works fine. It does analysis and fills histograms. I wish to add TTrees which would contain the same information filled in some of the histograms.
For example, defining the TTree and TH1D:

t_mu = TTree('MuTree', 'MuTree')
pt_leadmu = ROOT.std.vector(float)()
t_mu.Branch('pt_leadmu', pt_leadmu)
h_pt_leadmu = TH1D("h_pt_leadmu", "h_pt_leadmu; p_{T, #mu1} [GeV]; Arb. u.", nbins, xbins)

filling the TH1D and vector intended for the TTree (inside event loop):

h_pt_leadmu.Fill(Lead_mu.Pt()/1000, weight)
pt_leadmu.push_back(Lead_mu.Pt()/1000)

finally filling the TTree and writing everything up (outside of loop/s):

t_mu.Fill()
newfile = TFile(case+"_Zprime_mumu.root","RECREATE")
t_mu.Write()
h_pt_leadmu.Write()
newfile.Close()

I am facing two problems:

  1. At the end of the run I get an error message:
    TBranchElement::TBranc... ERROR basket's WriteBuffer failed.
  2. The TH1D and TTree do not match, although they should be containing exactly the same information (never mind binning etc., they simply do not match).

What am I missing/doing wrong?
Should I have used TChain and if so, how?

Your help would be deeply appreciated, as always.


_ROOT Version: 6.16
_Platform: Linux (lxplus)
_Compiler: Python 2.7.15


Hello,

There’s a few things you can try:

  • Create the file before you create the tree, both before the loop.
  • You set pt_leadmu as an std::vector branch of the tree, which is fine. However, inside the loop, you need to push_back as many elements you want for the vector, run t_mu.Fill(), and then clear the vector for the next iteration (every iteration you should start with pt_leadmu empty). Note that I said clearing the vector, not reassigning pt_leadmu to a new vector.
  • As said above, t_mu.Fill() needs to be called inside the loop, for every iteration, not just once after the loop. Every time you call it, a new entry in the tree will be filled.

Thanks!