Hi,
What is the equivalent function for GetBinContentent in TGraph? I want to get the y-value of an ith point in a TGraph, given x.
Thanks.
Frances
Hi,
What is the equivalent function for GetBinContentent in TGraph? I want to get the y-value of an ith point in a TGraph, given x.
Thanks.
Frances
Get the Y
vector using root.cern.ch/root/html/TGraph.html#TGraph:GetY
and use Y[i+1]
.
Or if X
is not a point of the graph use.
root.cern.ch/root/html/TGraph.html#TGraph:Eval
Thanks coet,
With the use of GetY(), i get the following:
i 70 counts 159643488.000000
i 71 counts 159643488.000000
i 72 counts 159643488.000000
i 73 counts 159643488.000000
i 74 counts 159643488.000000
i 75 counts 159643488.000000
i 76 counts 159643488.000000
i 77 counts 159643488.000000
i 78 counts 159643488.000000
i 79 counts 159643488.000000
i 80 counts 159643488.000000
i 81 counts 159643488.000000
i 82 counts 159643488.000000
i 83 counts 159643488.000000
i 84 counts 159643488.000000
i 85 counts 159643488.000000
i 86 counts 159643488.000000
i 87 counts 159643488.000000
i 88 counts 159643488.000000
i 89 counts 159643488.000000
i 90 counts 159643488.000000
i 91 counts 159643488.000000
i 92 counts 159643488.000000
i 93 counts 159643488.000000
i 94 counts 159643488.000000
i 95 counts 159643488.000000
i 96 counts 159643488.000000
i 97 counts 159643488.000000
i 98 counts 159643488.000000
i 99 counts 159643488.000000
i 100 counts 159643488.000000
i 101 counts 159643488.000000
i 102 counts 159643488.000000
i 103 counts 159643488.000000
i 104 counts 159643488.000000
i 105 counts 159643488.000000
i 106 counts 159643488.000000
i 107 counts 159643488.000000
i 108 counts 159643488.000000
That is, I am getting a contstant Y, while i am expecting a non-constant value.
The code below:
TGraph *g_offs( TGraph *g, Double_t offs1, Double_t offs2) {
Int_t i;
Double_t shift;
Double_t counts=0;
Double_t min = g->GetXaxis()->GetXmin();
Double_t max = g->GetXaxis()->GetXmax();
TGraph *g_offs = new TGraph();
for(i=min; i<max; i++) {
counts = g->GetY();
printf("i %d counts %f\n", i,counts);
if (i%2 == 0) {
shift = (Double_t) i + offs1;
} else {
shift = (Double_t) i + offs2;
}
g_offs->SetPoint(i,shift,counts);
g_offs->GetXaxis()->SetTitle("Position x 10microns");
g_offs->GetYaxis()->SetTitle("Counts");
g_offs->SetLineColor(2);
g_offs->SetMarkerStyle(24);
g_offs->SetMarkerColor(2);
counts = 0;
}
return g_offs;
}
TGraph g here is my source graph.
What did I probably miss?
Thanks.
Frances
it works for me:
{
Double_t x[3] = {1,2,3};
Double_t y[3] = {10,20,30};
TGraph *g = new TGraph(3,x,y);
Double_t *yg = g->GetY();
for (int i = 0; i<3; i++) printf("yg[%d] = %g\n", i,yg[i]);
}
[code]#include
#include “TGraph.h”
#include “TAxis.h”
#include “TMath.h”
#include “TH1.h”
TGraph *g_offs( TGraph *g, Double_t offs1, Double_t offs2)
{
Int_t i;
TGraph *gg = new TGraph(*g);
Double_t *X = gg->GetX();
Double_t *Y = gg->GetY();
for(i = 0; i < g->GetN(); i++) {
printf(“X[%d]=%f Y[%d]=%f\n”, i, X[i], i, Y[i]);
if (TMath::Nint(X[i]) % 2 == 0) {
X[i] += offs1;
} else {
X[i] += offs2;
}
}
gg->GetXaxis()->SetTitle(“Position x 10microns”);
gg->GetYaxis()->SetTitle(“Counts”);
gg->SetLineColor(2);
gg->SetMarkerStyle(24);
gg->SetMarkerColor(2);
return gg;
}[/code]
thanks.