Writing a LorentzVector to an existing Branch in PyROOT


ROOT Version: ROOT 6.14/06
Platform: Linux
Compiler: linuxx8664gcc


Dear experts,

I’m fairly new to this so if this should be posted in the newbie section then let me know.

I have root file that I would like to open, access some variables (px,py,pz, E) and then put them in a LorentzVector. Then I want to boost the vectors, and save them back to that root file. I potentially want to deconstruct each LorentzVector back into px,py,pz, and E variables and then save those variables to the TTree instead of the LorentzVector itself as I’m not sure how easy it is to access LorentzVector quantities.

So far my script doesn’t work and I get the error:

Error in TBranchElement::Fill: attempt to fill branch TVecLep while addresss is not set
Error in TBranchElement::Fill: attempt to fill branch TVecTauDaug while addresss is not set

My questions are:

  1. How do I properly save the branches I want
  2. Is it simple to access LorentzVectors in branches? Or should I deconstruct the LorentzVector back to the individual variables again?
  3. How do you deconstruct the LorentzVector back into variables?

Here is my code:

import ROOT
from ROOT import Math, TLorentzVector, TFile

myfile = TFile(‘v12_Bktl_mc11_eplus.root’, ‘update’)
mytree = myfile.variables

VecLep = Math.LorentzVector(‘ROOT::Math::PxPyPzE4D’)(mytree.Lep_CMS_px, mytree.Lep_CMS_py, mytree.Lep_CMS_pz, mytree.Lep_CMS_E)

VecTau = Math.LorentzVector(‘ROOT::Math::PxPyPzE4D’)(mytree.Tau_CMS_px, mytree.Tau_CMS_py, mytree.Tau_CMS_pz, mytree.Tau_CMS_E)

VecTauDaug = Math.LorentzVector(‘ROOT::Math::PxPyPzE4D’)(mytree.Tau_CMS_px, mytree.Tau_CMS_py, mytree.Tau_CMS_pz, mytree.Tau_CMS_E)

cmTau = VecTau.BoostToCM()

TVecLep = ROOT.Math.VectorUtil.boost(VecLep, cmTau)
TVecTauDaug = ROOT.Math.VectorUtil.boost(VecTauDaug, cmTau)

mytree.Branch(‘TVecLep’, TVecLep)
mytree.Branch(‘TVecTauDaug’, TVecTauDaug)

mytree.Fill()
myfile.Write()
myfile.Close()

Thank you in advance

Hi everyone,

I have fixed this and wanted to share my update/solution. One thing I needed to do was to fill the branches event by event in a loop. I also needed to define the branch variables as arrays and define the branches themselves with those arrays. I also deconstructed the LorentzVectors to just get the variables I wanted. Lastly, I added the TFile.kOverwrite line to the Write() line so that it did not create new trees.

Here is my new code:

import ROOT
from ROOT import Math, TLorentzVector, TFile
from array import array

myfile = TFile(‘v12b_Bktl_mc11_eplus.root’, ‘update’)
mytree = myfile.variables

Lep_tRF_px = array(‘d’,[0])
Lep_tRF_py = array(‘d’,[0])
Lep_tRF_pz = array(‘d’,[0])
Lep_tRF_E = array(‘d’,[0])
Lep_tRF_p = array(‘d’,[0])

TauDaug_tRF_px = array(‘d’,[0])
TauDaug_tRF_py = array(‘d’,[0])
TauDaug_tRF_pz = array(‘d’,[0])
TauDaug_tRF_E = array(‘d’,[0])
TauDaug_tRF_p = array(‘d’,[0])

branch_Lep_tRF_px = mytree.Branch(‘Lep_tRF_px’, Lep_tRF_px, ‘Lep_tRF_px/D’)
branch_Lep_tRF_py = mytree.Branch(‘Lep_tRF_py’, Lep_tRF_py, ‘Lep_tRF_py/D’)
branch_Lep_tRF_pz = mytree.Branch(‘Lep_tRF_pz’, Lep_tRF_pz, ‘Lep_tRF_pz/D’)
branch_Lep_tRF_E = mytree.Branch(‘Lep_tRF_E’, Lep_tRF_E, ‘Lep_tRF_E/D’)
branch_Lep_tRF_p = mytree.Branch(‘Lep_tRF_p’, Lep_tRF_p, ‘Lep_tRF_p/D’)

branch_TauDaug_tRF_px = mytree.Branch(‘TauDaug_tRF_px’, TauDaug_tRF_px, ‘TauDaug_tRF_px/D’)
branch_TauDaug_tRF_py = mytree.Branch(‘TauDaug_tRF_py’, TauDaug_tRF_py, ‘TauDaug_tRF_py/D’)
branch_TauDaug_tRF_pz = mytree.Branch(‘TauDaug_tRF_pz’, TauDaug_tRF_pz, ‘TauDaug_tRF_pz/D’)
branch_TauDaug_tRF_E = mytree.Branch(‘TauDaug_tRF_E’, TauDaug_tRF_E, ‘TauDaug_tRF_E/D’)
branch_TauDaug_tRF_p = mytree.Branch(‘TauDaug_tRF_p’, TauDaug_tRF_p, ‘TauDaug_tRF_p/D’)

for entry in mytree:

Lep_CMS_px = entry.Lep_CMS_px
Lep_CMS_py = entry.Lep_CMS_py
Lep_CMS_pz = entry.Lep_CMS_pz
Lep_CMS_E = entry.Lep_CMS_E

Tau_CMS_px = entry.Tau_CMS_px
Tau_CMS_py = entry.Tau_CMS_py
Tau_CMS_pz = entry.Tau_CMS_pz
Tau_CMS_E = entry.Tau_CMS_E

TauDaug_CMS_px = entry.TauDaug_CMS_px
TauDaug_CMS_py = entry.TauDaug_CMS_py
TauDaug_CMS_pz = entry.TauDaug_CMS_pz
TauDaug_CMS_E = entry.TauDaug_CMS_E

VecLep = Math.LorentzVector('ROOT::Math::PxPyPzE4D<double>')(Lep_CMS_px, Lep_CMS_py, Lep_CMS_pz, Lep_CMS_E)
VecTau = Math.LorentzVector('ROOT::Math::PxPyPzE4D<double>')(Tau_CMS_px, Tau_CMS_py, Tau_CMS_pz, Tau_CMS_E)
VecTauDaug = Math.LorentzVector('ROOT::Math::PxPyPzE4D<double>')(TauDaug_CMS_px, TauDaug_CMS_py, TauDaug_CMS_pz, TauDaug_CMS_E)

cmTau = VecTau.BoostToCM()

TVecLep = ROOT.Math.VectorUtil.boost(VecLep, cmTau)
TVecTauDaug = ROOT.Math.VectorUtil.boost(VecTauDaug, cmTau)

  Lep_tRF_px[0] = TVecLep.Px()
branch_Lep_tRF_px.Fill()

Lep_tRF_py[0] = TVecLep.Py()
branch_Lep_tRF_py.Fill()

	  Lep_tRF_pz[0] = TVecLep.Pz()
branch_Lep_tRF_pz.Fill()

Lep_tRF_E[0] = TVecLep.E()
branch_Lep_tRF_E.Fill()

  Lep_tRF_p[0] = TVecLep.P()
  branch_Lep_tRF_p.Fill()

TauDaug_tRF_px[0] = TVecTauDaug.Px()
branch_TauDaug_tRF_px.Fill()

TauDaug_tRF_py[0] = TVecTauDaug.Py()
branch_TauDaug_tRF_py.Fill()

TauDaug_tRF_pz[0] = TVecTauDaug.Pz()
branch_TauDaug_tRF_pz.Fill()

TauDaug_tRF_E[0] = TVecTauDaug.E()
branch_TauDaug_tRF_E.Fill()

TauDaug_tRF_p[0] = TVecTauDaug.P()
branch_TauDaug_tRF_p.Fill()

myfile.Write("",TFile.kOverwrite)
myfile.Close()

Thank you for sharing your solution @tshill!

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