From TTree to TGraph

Dear all,

I would like to read some tree branches and ‘convert’ them to graphs so I can manipulate it.
Can you help me with this?

For example, this is code:

f1 = TFile("file.root","r")
c1 = TCanvas('c1','The Ntuple canvas',200,10,700,780)
ntuple = gROOT.FindObject('arcBPM_Right')

Now, how can I get this plot S1X:ScanLB and add it to TGraph so I can easily manipulate it?

Thanks in advance,
Veljko M.

Hi Velijko,
possibly simpler, if your ROOT version is new enough (and it returns the TGraph object): the lines above can be substituted with

graph = ROOT.RDataFrame("arcBPM_Right", "file.root")\
            .Graph("S1X", "ScanLB")
c1 = ROOT.TCanvas(‘c1’,‘The Ntuple canvas’,200,10,700,780)


Thank you Enrico for quick response. I tried with your suggestion but an error occurs:

SyntaxError: Non-ASCII character ‘\xe2’ in file on line 23, but no encoding declared;

It could be I don`t have ROOT verions new enough. I use
ROOT 6.10/04
From tag v6-10-04, 28 July 2017

All the best,
Veljko M.

That’s a Python error that does not have to do with ROOT, I think it’s because the TCanvas arguments use backticks ` instead of double quotes ". I fixed my original snippet.

In any case RDataFrame is not available in v6.10 I’m afraid.

I guess you can retrieve the TGraph from the canvas or gROOT instead.

I agree with you, I can use TGraph from canvas, but there`s a problem since I draw multiple plots with the ‘same’ option and some data points are lost. This happens because sometimes first drawn graph has y-axis ranges smaller then the next one. Maybe you can help me with this?
For example:

ntuple = gROOT.FindObject('arcBPM_Left')

ntuple1 = gROOT.FindObject('arcBPM_Right')
ntuple1.Draw('%s:ScanLB'%beam,draw, 'same')

All the best,
Veljko M.

@couet can probably help with axis range issues :smile:
It would probably be helpful if you could provide a self-contained reproducer (something we can run ourselves, or the data needed to run the snippet above).

You can find attached *py code and *root file which are standalone to run the macro.
OrbitDriftData_2018_900GeV_S1.root (120.4 KB) (639 Bytes)

After running the you can find some points lost ‘in translation’

Thanks in advance,
Veljko M.

Group the graph in a TMultiGraph which will compute for you the global range.

On a side note, the “same” option is not a TGraph::Draw option. By default a graph is drawn on the current plot (unlike the histograms which need “same” to do that). On the opposite the option “A” for TGraph allows to start a new plot. We have been obliged to handle the case of “SAME” for TGraph, as many people (like you) did that mistake. So “SAME” on TGraph is simply ignored.

1 Like

Thanks for replay,

Actually I`m familiar with the TMultiGraph class, but there is a problem how can I ‘convert’ TTree content to TGraph and than everything will be easier? Enrico came out with an idea but it was not working for my ROOT version.

root [0] ntuple->Draw("px:py")
root [1] auto graph = (TGraph*)gPad->GetPrimitive("Graph");
root [2] graph->Draw("L")

Thanks, guys, you helped me a lot.

All the best,
Veljko M.

1 Like

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