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
Thank you!
Hi @ftheodor ,
and welcome to the ROOT forum!
I guess you can extract the TGraphErrors object from the file with (something like):
import ROOT
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);
Cheers,
Enrico
Hi, thank you for your reply!
Unfortunately there is no GetPointX/Y attribute on the TCanvas class ( AttributeError: âTCanvasâ object has no attribute âGetPointXâ ).
Hi,
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!
What does 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:
import ROOT
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?
ftheodor:
TGraphErrors
Sorry for not using the forum uploader but for some reason it was stucking at 15%.
Hi,
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.
Cheers,
Enrico
Hi,
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!
couet
October 8, 2020, 12:55pm
14
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).
couet
October 8, 2020, 1:32pm
16
To retrieve the TGraphErrors
do:
% 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-1200.0.32.2) |
| Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------------
root [0]
root [1] 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 [2] TGraphErrors *gre = (TGraphErrors*)c->FindObject("Graph");
root [3] TCanvas C
root [4] gre->Draw("AL*")
1 Like
Hi and thanks for your reply!
I try to get x & y dimensions of the points but I always get 0.00000
root [29] gre->GetErrorX(1)
(double) 0.0000000
root [30] gre->GetErrorY(1)
(double) 0.0000000
root [31] gre->GetEY()[1]
(double) 0.0000000`
I tried also on pyRoot and I am still getting 0 values.
I think you want GetPointX
and GetPointY
rather than GetErrorX
and GetErrorY
couet
October 9, 2020, 7:21am
19
To get the points values you should use the methods @eguiraud suggested.
And, for info, the errors are really 0 in your TGraphErrors
:
root [3] gre->Print();
x[0]=40, y[0]=12.9644, ex[0]=0, ey[0]=0
x[1]=80, y[1]=22.4258, ex[1]=0, ey[1]=0
x[2]=120, y[2]=31.0251, ex[2]=0, ey[2]=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!