I want to shift TGraph to zero on the X-Axis. I am looking for a solution, where all the graphs should start from same point. Please find the attachment for more clarification of the problem.
My code is as follows:
TFile *f1 = TFile::Open("graph.root");
TCanvas *c = new TCanvas("S0", "S0", 0, 0, 1000, 1000);
TTree *T1 = (TTree*)f1->Get("abc");
TCut cut = "Time>3";
T1->Draw("a0:t", cut, "same");
TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
gr1->SetLineColor(kRed);
T1->Draw("a1:t", cut, "same");
TGraph *gr2 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
gr2->SetLineColor(kBlue);
T1->Draw("a2:t", cut, "same");
TGraph *gr3 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
gr3->SetLineColor(kMagenta);
T1->Draw("a3:t", cut, "same");
TGraph *gr4 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
gr4->SetLineColor(kBlack);
TMultiGraph *mg = new TMultiGraph();
mg -> Add(gr1);
mg -> Add(gr2);
mg -> Add(gr3);
mg -> Add(gr4);
mg -> Draw("AC");
[color=#FF0040]mg->GetXaxis()->SetLimits(0, 1500);[/color] //Here I am trying to set the starting point of all the Graph to zero but its not working.
OR we have some function for shifiting the TGraph? Guide me.
You want to shift all the graphs at (0.,0.). I guess you want to do that keeping the graphs shapes. Therefore you need to shift all the points of each graph. The pseudo C code would be:
dx = x[0];
for ( int i = 0; i<number_of_points_in_graph; i++) x[i] = x[i]-dx;
@Wile E. Coyote : I used the tips gave by you in the code. But its not working.
T1->Draw("a0:t", cut, "same");
TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
Double_t xmin = TMath::MinElement(gr1 ->GetN(), gr1->GetX());
for (Int_t i = 0; i < gr1 ->GetN(); i++)
(gr1 ->GetX())[i] = (gr1 ->GetX())[i] - xmin;
gr1->SetLineColor(kRed);
@ couet : Yes, I want to shift all the graphs to a Starting point (0,0). While keeping the shape of the graph will be the same as previous.
What you mean by x[0]? Is it array of all x-points in the graph. Means, x[0] = gr1->GetX();
T1->Draw("a0:t", cut, "same");
TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
Int_t n = gr1->GetN();
Double_t x = gr1->GetX();
Double_t y = gr1->GetY();
for (Int_t i = 0; i < n; i++)
{
int dx,dy;
dx = x[0];
dy = y[0];
x[i] = x[i]-dx;
y[i] = y[i]-dy;
gr1->SetPoint(i,x,y); //Is it correct? I have it because,in the code we are not setting the updated values
}
gr1->SetLineColor(kRed);
But its not working. I am still looking for the solution.
But you do realise that you need to “shift” each of your graphs (i.e. one by one) and that for each of them you need to calculate its own “oldXmin”, don’t you?
I’m actually having the same issue, but wanting to re-scale the X axis of a TGraph. I am using PyROOT and I am wondering why the obvious solution is not working:
x = g.GetX()
for i in range(N):
x *= scaling
g.Draw("AL")
It looks like the x values have changed, but now it does not draw properly. Is there anything else I need to do manually to make it draw properly? I tried g.GetXaxis().SetRangeUser(), but it makes no difference. The axis that shows up is always the original one, so the new graph is off-scale.
Thanks, that works. I guess it’s because the TGraph is somehow converted to a histogram when drawn with the “A” option, but then that histogram sticks around?
The “A” drawing option creates a “dummy” histogram which is used to draw axes. Once it exists, successive calls to “Draw” will not “recreate” / “renew” it.
See also: TGraph::GetHistogram