Hi,
I am trying to read a TGraphErrors in from a file and extract the points X and Y which i then want to store and use later.
I have managed to obtain the graph and redraw it, however when i try to extract the points and print the values to the screen have a problem. The code compiles, but i get a bizzare output.
The code segment is:
//get the graph
ostringstream myname;
myname<<“CorrectedElectron_”<<ID<<"_"<<RunNo;
cout<<"Getting Graph "<<myname.str().c_str()<<endl;
TGraphErrors * h1 = (TGraphErrors*)f1->Get(myname.str().c_str());
h1->Draw();
//works till here
Double_t myX , myY;
// Int_t Num = h1->GetN();
h1->GetX();
h1->GetY();
for(int j = 0; j< 200; j++){
Double_t mynum1 = h1->GetPoint(j,myX,myY); ///Why doesn't this work how i want it to??
cout<<j<<" "<<myX<<" "<<myY<<" "<<mynum1<<endl;
}
The output i get is:
Getting Graph CorrectedElectron_1431_143
j myX myY mynum1
0 0 6.95335e-310 8.41928e+07
1 0 6.95335e-310 8.41928e+07
2 0 6.95335e-310 8.41928e+07
3 0 6.95335e-310 8.41928e+07
4 0 6.95335e-310 8.41928e+07
5 0 6.95335e-310 8.41928e+07
6 0 6.95335e-310 8.41928e+07
7 0 6.95335e-310 8.41928e+07
8 0 6.95335e-310 8.41928e+07
9 0 6.95335e-310 8.41928e+07
etc. for each of the 200 points.
Can anyone see where my mistake is?
Thanks
couet
May 4, 2011, 12:41pm
2
It works for me:
{
Int_t i;
Double_t xp, yp;
Double_t x[10], y[10];
for (i=0;i<10;i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
}
gr = new TGraph(10,x,y);
for (i=0;i<10;i++) {
gr->GetPoint(i,xp,yp);
printf("xp[%d] = %g - yp[%d] = %g\n",i,xp,i,yp);
}
}
It gives:
xp[0] = 0 - yp[0] = 1.98669
xp[1] = 0.1 - yp[1] = 2.9552
xp[2] = 0.2 - yp[2] = 3.89418
xp[3] = 0.3 - yp[3] = 4.79426
xp[4] = 0.4 - yp[4] = 5.64642
xp[5] = 0.5 - yp[5] = 6.44218
xp[6] = 0.6 - yp[6] = 7.17356
xp[7] = 0.7 - yp[7] = 7.83327
xp[8] = 0.8 - yp[8] = 8.41471
xp[9] = 0.9 - yp[9] = 8.91207
Hi,
Thanks for your quick reply. I have tried saving your graph to a file and reading it back in before extracting the points:
void func() {
cout<<"Hello World"<<endl;
TFile * f2 = new TFile("testpoints.root","RECREATE");
Int_t i;
Double_t xp, yp;
Double_t x[10], y[10];
for (i=0;i<10;i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
}
TCanvas * c1 = new TCanvas("c1","c1");
TGraph * gr = new TGraph(10,x,y);
gr->SetTitle("mygraph");
gr->Draw("AP");
c1->Write();
f2->Write();
f2->Close();
delete gr;
delete c1;
cout<<"Now get the points"<<endl;
TFile * f3 = new TFile("testpoints.root","READ");
TGraph * g1 = (TGraph*)f3->Get("c1");
if(!g1){
cout<<"Graph not found"<<endl;
return;
}
g1->Draw();
for (i=0;i<10;i++) {
g1->GetPoint(i,xp,yp);
printf("xp[%d] = %g - yp[%d] = %g\n",i,xp,i,yp);
}
cout<<"finished "<<endl;
}
I now get:
xp[0] = 0 - yp[0] = 0
xp[1] = 0 - yp[1] = 0
xp[2] = 0 - yp[2] = 0
xp[3] = 0 - yp[3] = 0
xp[4] = 0 - yp[4] = 0
xp[5] = 0 - yp[5] = 0
xp[6] = 0 - yp[6] = 0
xp[7] = 0 - yp[7] = 0
xp[8] = 0 - yp[8] = 0
xp[9] = 0 - yp[9] = 0
So is it something in the reading in procedure? When i draw the graph it looks the same as the original though. I am very confused.
couet
May 4, 2011, 2:10pm
4
Try:
{
cout<<"Hello World"<<endl;
TFile * f2 = new TFile("testpoints.root","RECREATE");
Int_t i;
Double_t xp, yp;
Double_t x[10], y[10];
for (i=0;i<10;i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
}
TCanvas * c1 = new TCanvas("c1","c1");
TGraph * gr = new TGraph(10,x,y);
gr->SetName("mygraph");
f2->cd();
gr->Write();
f2->Close();
delete gr;
delete c1;
cout<<"Now get the points"<<endl;
TFile * f3 = new TFile("testpoints.root","READ");
TGraph * g1 = (TGraph*)f3->Get("mygraph");
if(!g1){
cout<<"Graph not found"<<endl;
return;
}
for (i=0;i<10;i++) {
g1->GetPoint(i,xp,yp);
printf("xp[%d] = %g - yp[%d] = %g\n",i,xp,i,yp);
}
cout<<"finished "<<endl;
}
hi, pls how did you resolve the issue?
couet
July 25, 2017, 3:13pm
6
Have you tried my last suggestion ?
I created another function inplace of the GetPoint. I was unable to use the ypoints that i derived from the getpoint.
thanks
couet
July 31, 2017, 7:34am
8
Ok. So you solved your problem ?
1 Like