Hi, I have some root files containing TCanvas that show TGraphErrors information.
I work on a python script that uses root library and copies the data to the database.
As you can see in the image there are 3 points in the canvas. I want to get the x & y dimensions from the middle one.
Could you help me on how to get the middle point information using python code? I have tried many things but nothing
and welcome to the ROOT forum!
I guess you can extract the TGraphErrors object from the file with (something like):
f = ROOT.TFile("filename.root")
graph = f.Get("nameoftgrapherror")
and then you can query the TGraphErrors for Xs and Ys of its points, e.g. with:
x = graph.GetPointX(1);
y = graph.GetPointY(1);
Hi, thank you for your reply!
Unfortunately there is no GetPointX/Y attribute on the TCanvas class ( AttributeError: ‘TCanvas’ object has no attribute ‘GetPointX’ ).
not the TCanvas, the TGraphErrors: the TCanvas just displays the underlying TGraphErrors, you should be able to retrieve that object (I think).
Hi, I know. I am just dont know how to retrieve the TGraphErrors using python.
Thanks for the help!
file.ls() print? (i.e. what’s inside the file?)
If I use this command in the root program (not pyRoot)
TGraphErrors *gre = (TGraphErrors*) _file0->Get("BPix/BPix_BmO/BPix_BmO_SEC5/BPix_BmO_SEC5_LYR1/BPix_BmO_SEC5_LYR1_LDR4H/BPix_BmO_SEC5_LYR1_LDR4H_MOD2/BPix_BmO_SEC5_LYR1_LDR4H_MOD2_ROC4")
I get the GraphErrors object. I just don’t know how to do this command in python
Perfect, then as mentioned above, from Python it should be:
f = ROOT.TFile("filename.root")
graph = f.Get("BPix/BPix_BmO/BPix_BmO_SEC5/BPix_BmO_SEC5_LYR1/BPix_BmO_SEC5_LYR1_LDR4H/BPix_BmO_SEC5_LYR1_LDR4H_MOD2/BPix_BmO_SEC5_LYR1_LDR4H_MOD2_ROC4")
Yes but this function still returns me a TCanvas object in Python ( <ROOT.TCanvas object (“BPix_BmO_SEC5_LYR1_LDR4H_MOD2_ROC4”) at 0x6ee2650>
Maybe it needs some kind of cast?
Uhm can you share the file with me please?
Sorry for not using the forum uploader but for some reason it was stucking at 15%.
I assumed that you saved the TGraphErrors to the file, but it looks like it really was the TCanvas object.
@couet might know how to recover the user coordinates of a point displayed by a TCanvas.
However, I suggest you save the TGraphErrors to the file rather than the TCanvas, so you have the full information in there.
I know it is a bad way to store information, these are some old measurements and they are stored like this.
Thank you for your help!
The file you posted has a complex directory structure. I scanned it a bit but there is no TGraph nor TCanvas in it.
The TCanvas is
"BPix/BPix_BmO/BPix_BmO_SEC5/BPix_BmO_SEC5_LYR1/BPix_BmO_SEC5_LYR1_LDR4H/BPix_BmO_SEC5_LYR1_LDR4H_MOD2/BPix_BmO_SEC5_LYR1_LDR4H_MOD2_ROC4" (see posts above).
To retrieve the
% root Iana_8.root
| Welcome to ROOT 6.23/01 https://root.cern |
| (c) 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |
| Built for macosx64 on Oct 05 2020, 06:45:25 |
| From heads/master@v6-23-01-1421-ga5d880a434 |
| With Apple clang version 12.0.0 (clang-118.104.22.168) |
| Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
root  TCanvas *c = (TCanvas*) _file0->Get("BPix/BPix_BmO/BPix_BmO_SEC5/BPix_BmO_SEC5_LYR1/BPix_BmO_SEC5_LYR1_LDR4H/BPix_BmO_SEC5_LYR1_LDR4H_MOD2/BPix_BmO_SEC5_LYR1_LDR4H_MOD2_ROC4")
(TCanvas *) 0x7fa0e19081c0
root  TGraphErrors *gre = (TGraphErrors*)c->FindObject("Graph");
root  TCanvas C
root  gre->Draw("AL*")
Hi and thanks for your reply!
I try to get x & y dimensions of the points but I always get 0.00000
root  gre->GetErrorX(1)
root  gre->GetErrorY(1)
root  gre->GetEY()
I tried also on pyRoot and I am still getting 0 values.
I think you want
GetPointY rather than
To get the points values you should use the methods
And, for info, the errors are really 0 in your
root  gre->Print();
x=40, y=12.9644, ex=0, ey=0
x=80, y=22.4258, ex=0, ey=0
x=120, y=31.0251, ex=0, ey=0
It works, many thanks!!
And as for the rest reading:
In python you have to use gre.GetX() or gre.GetY() function to get an array with x/y coordinates.
Thank you again!