PyRoot TLine Struggles

Hello everyone! This is my first forum post and I wanted to ask about the TLine() function in regards to drawing a line on an existing plot like a histogram plot for example but even before that I’m struggling with just drawing a line in general.
Here is my attempt to use it along with a histogram:
for xstart in (num1, num2): I need multiple lines on the same plot
xend = xstart
l = ROOT.TLine(xstart, 0, xend, 10000)
l.Draw(“SAME”) forcing lines to be drawn on the same canvas as the histogram
The histogram comes out nicely but the lines don’t show up at their needed spots.

I decided to test TLine individually using this code (I included .Paint() out of desperation but I don’t think it does anything):
while user_input != “N”:
testL = ROOT.TLine(5+i, 0, 5+i, 10000)
i = i+5
user_input = input(“Do you wish to continue viewing lines? Y/N”)
None of these lines show up but a blank canvas does appear, any tips would be appreciated!

Platform: Linux
Compiler: NA

Try to add c.Update() (c being your canvas), or ROOT.gPad.Update() after l.Draw()

1 Like


If you create a new line in every iteration and bind it to the same variable in Python, the line of the previous iteration will get garbage collected (only the line created at the last iteration will survive). Try creating a list and adding all the lines to that list at the end of each iteration, that will make all the lines avoid garbage collection.

1 Like

Thanks for the response, I tried this and unfortunately my lines are still not showing up.
I’ve also now added a .SetLineColor(1) to make sure the lines are black in color and they still aren’t showing up.
EDIT: Now that I look closer the lines appear for a short amount of time then disappear as the loop continues. I might have to include @etejedor 's tip of putting the lines in a list to stop them from getting removed.

Thank you to everyone that replied! I combined all your answers along with some adjustments of my own (my numbers were a bit off so some lines were out of frame) and now the TLines are showing up perfectly.
Code snippet:

	for det in ["LAr","GAr"]: # drawing Tlines for current histograms ( both LAr and GAr)
		for xstart in (detectorDict[det][particle][0], detectorDict[det][particle][1]):
				print("loop entry")
				xend = xstart
				l = ROOT.TLine(xstart, 0, xend, 100000)
				l.Draw("SAME") #forcing lines to be drawn on the same canvas as the histogram
				linestorage.append(l) #storing lines

Not sure exactly what fixed it but I’ll definitely take it!