Read Float saved in a TBranch pyROOT

Hello,
I read many similar topics but I didn’t reach a solution.

I have a C+ code where I save some Branching Ratio values in a TTree in this way:

  TTree *Branching_ratios = new TTree ("Branching_ratios", "Branching ratios");
  Branching_ratios->Branch("Electron_channel", &BRe);
  Branching_ratios->Fill();
  Branching_ratios->Branch("Muon_channel", &BRm);
  Branching_ratios->Fill();
  Branching_ratios->Branch("Hadronic_channel", &BRq);
  Branching_ratios->Fill();
  Branching_ratios->Branch("Tau_channel", &BRtau);
  Branching_ratios->Fill();

And it’s ok, the values are correctly saved. If I check in interactive ROOT and open the Leaf the value is saved in the “Mean” voice.
Here an example:

Now I’m makin a Python macro where I want save that value in an array, but I couldn’t find a solution.

        rootfile = ROOT.TFile.Open(filename)

        files.append(rootfile)


        h1=rootfile.Branching_ratios.GetBranch("Electron_channel").GetValue()
        h2=rootfile.Branching_ratios.GetBranch("Muon_channel").GetValue()
        h3=rootfile.Branching_ratios.GetBranch("Hadronic_channel").GetValue()
        h4=rootfile.Branching_ratios.GetBranch("Tau_channel").GetValue()

        print(h1)
        print(h2)
        print(h3)
        print(h4)

filename is correctly taken, rootfile idem.
I tried also GetLeaf, GetEntry, GetMean (GetMean and GetValue are not attributes of TBranch i know, but I tried everything).

How can I resolve this?

ROOT Version: Not Provided
Platform: lxplus
Compiler: Not Provided

Your C++ code is wrong. You must first define all branches and only then fill the tree.

Ok thanks, I’ve fixed it in this way:

TTree *Branching_ratios = new TTree ("Branching_ratios", "Branching ratios");
  Branching_ratios->Branch("Electron_channel", &BRe);
  Branching_ratios->Branch("Muon_channel", &BRm);
  Branching_ratios->Branch("Hadronic_channel", &BRq);
  Branching_ratios->Branch("Tau_channel", &BRtau);
  Branching_ratios->Fill();

But in my Python code how I take the value? The value is always saved in Mean so my problem remains.

Try:

rootfile = ROOT.TFile.Open(filename)
tree = rootfile.Get("Branching_ratios")
tree.GetEntry(0)
h1 = tree.GetLeaf("Electron_channel").GetValue()
print(h1)

It works! Thank you so much.

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