# TGraph2D help undergraduate brand new to root

I am an undergarduate researcher new to ROOT. I am trying to use a TGraph2D to help plot some hit data. I would like for my x axis to be the Monte Carlo particle identifier, my y axis to me a number ( 1 to 4) that reflects the level that particle hit, and the z axis to be the number quantity of events that share the same x,y ordered pair. I am at a loss. this is all I have. thank you for your time.

``````
Canvas *c = new TCanvas("c"0,0,700,600);
Double_t x = {-11,-11,11,11,-11,-11,22,11,11,11,11,-11,11,11,11,11,22,22,22,22,11,11,-13,11,11,22,22,11,11,-11,22,22,-11,22,22,11,11,11,11,11,11,-11,11,11,22,-11,-11,11,11,13};
Double_t y = {3,4,3,4,3,3,3,3,4,3,4,3,3,4,3,4,3,3,3,3,3,4,3,3,4,3,3,3,4,3,3,3,3,3,2,3,4,3,4,3,4,3,3,4,3,3,3,3,4,3};
Double_t z = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
Int_t np = 200;
TGraph2D *dt = new TGraph2D();
for (Int_t N=0; N<np; N++)
{
dt->SetPoint(N, x[N], y[N], z[N]);
}

gStyle->SetPalette(55);
dt->Draw("surf1");   `````````

Hi.

With dt->SetPoint(…) you set the value of an individual point, however, you try to set multiple values for a single point. You should be doing instead:

``````Double_t x = {-11,-11,11,11,-11,-11,22,11,11,11,11,-11,11,11,11,11,22,22,22,22,11,11,-13,11,11,22,22,11,11,-11,22,22,-11,22,22,11,11,11,11,11,11,-11,11,11,22,-11,-11,11,11,13};
Double_t y = {3,4,3,4,3,3,3,3,4,3,4,3,3,4,3,4,3,3,3,3,3,4,3,3,4,3,3,3,4,3,3,3,3,3,2,3,4,3,4,3,4,3,3,4,3,3,3,3,4,3};
Double_t z = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
for (Int_t N=0; N<np; N++)
{
dt->SetPoint(N, x[N], y[N], z[N]);
}
``````

Cheers,
heico

i still am getting the error “excess elements in scalar initializer”

Should be

``````Double_t variable[length] = {element1, element2, ....};
``````

when you define your array. Frankly I just copy-pasted from your entry without paying attention.

h

``````{
auto *c = new TCanvas("c","c",0,0,700,600);
const int np = 30;
Double_t x[50] = {-11,-11,11,11,-11,-11,22,11,11,11,11,-11,11,11,11,11,22,22,22,22,11,11,-13,11,11,22,22,11,11,-11,22,22,-11,22,22,11,11,11,11,11,11,-11,11,11,22,-11,-11,11,11,13};
Double_t y[50] = {3,4,3,4,3,3,3,3,4,3,4,3,3,4,3,4,3,3,3,3,3,4,3,3,4,3,3,3,4,3,3,3,3,3,2,3,4,3,4,3,4,3,3,4,3,3,3,3,4,3};
Double_t z[np] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
TGraph2D *dt = new TGraph2D();
for (Int_t N=0; N<np; N++) dt->SetPoint(N, x[N], y[N], z[N]);

gStyle->SetPalette(55);
dt->Draw("surf1");
}
``````

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.