Adding events to an existing tree

Hi,

I probably have a very similar problem to the one described in:
Appending branch of user class to existing tree.
However, even after reading it I can not solve my problem.

I am creating a tree:

[code]gROOT.ProcessLine(
“struct MyStruct {
Int_t idd;
Int_t ipeaks;
Int_t ip;
Float_t xpos;
Float_t ypos;
Float_t integral;
Float_t area;
Float_t max_value;
Float_t covar_sigx2;
Float_t covar_sigy2;
};” );

def creat_file(name):
print ("— root file is created —")

from ROOT import MyStruct
mystruct = MyStruct()

f = TFile(name,"update");

tree = TTree( 'tree1', 'Hydrogen data MCP' )
tree.Branch( 'data', mystruct, 'idd/I:ipeaks:ip:xpos/F:ypos:integral:area:max_value:covar_sigx2:covar_sigy2' )

"""
for i in range(10):
	mystruct.idd   = i
	mystruct.ipeaks   = i*i
	mystruct.ip   = i*i*i
	mystruct.xpos   = i
	mystruct.ypos   = i
	mystruct.integral   = i
	mystruct.area   = i
	mystruct.max_value   = i
	mystruct.covar_sigx2   = i
	mystruct.covar_sigy2   = i

	tree.Fill()
"""

f.Write()

return f, tree;[/code]

I am returning tree and file because I thought it may help (but it didn’t).
I want to open that tree and simply add new events. My problem is, that my code does not add properly new events. It correctly adds two events, however most of values are “0”, while they should be numbers. It correctly open old tree and I have access to old events.

[code] from ROOT import gDirectory
mystruct = MyStruct()
#tree = f.Get(‘tree1’)
tree.Print()

	mystruct.idd = 10
	tree.SetBranchAddress('data', mystruct)

	#for event in tree:
	#	print event.idd
	
	#bylo num
	for i in range (0, 2):
		mystruct.idd = 12
		print(mystruct.idd) # it prints 12
		mystruct.ipeaks = i
		mystruct.ip = events_numbers[i]
		mystruct.xpos = xs[i]
		print(mystruct.xpos)
		mystruct.ypos = ys[i]
		mystruct.integral = peak_flux[i]
		mystruct.area = peak_area[i]
		print(mystruct.area)
		mystruct.max_value = max_value_2[i]
		mystruct.covar_sigx2 = covar_sigx2_2[i]
		mystruct.covar_sigy2 = covar_sigy2_2[i]

		tree.Fill()

	#tree.Write()
	from ROOT import TObject
	#f.WriteTObject(tree)

	f.Write("",TObject.kWriteDelete)
	#f.Close()[/code]

Thank you for any ideas.
Best,
Basia

Hi,

I solved my problem, however I do not understand fully why it works.

When I open the tree and declare branch address instead of:
tree.SetBranchAddress(‘data’, AddressOf(mystruct))
or
tree.SetBranchAddress(‘data’, mystruct)
I wrote:
tree.SetBranchAddress(‘data’, AddressOf(mystruct, ‘idd’))
and it works.

I post it for others in future.:slight_smile:

If you understand why there is a difference, I will be thankful. I always thought that AddressOf(mystruct, ‘idd’) is an address to an address for only one element of MyStruct.

Best,
Basia

SetBranchAddress does require the parameter to be the address of a pointer to the (begin of the) data …

Cheers,
Philippe.