Problems by generating a graph

Hi everyone,

I’m working with ROOT 6.22/02 for a university exam using a virtual version of ubuntu on Repl. I’m compiling with g++.

My program should show with TGraph the trend of the intensity of the electric field generated on the axis of two charges, starting from 100 times a given distance to 1000 times that same distance.

It mostly works, but I noticed that some of the points on the graph are set to wrong positions, I really don’t understand where the problem could be.

This is the code:

  TApplication myapp("app",0,0);

  TGraph * field = new TGraph;

	for ( double i = 1; i <=10; i+=0.2) {

		double yy = 100.*i*delta;	
		
		CampoVettoriale CV =  e.CampoElettrico(delta/2,yy,0) + p.CampoElettrico(delta/2,yy,0);

		field->SetPoint(i*5-4,i*5-4,CV.Modulo());
}	

	field->Draw("ALP");
	field->SetMarkerColorAlpha(6,0.30);
  field->SetMarkerStyle(48);
  field->SetMarkerSize(1);

	myapp.Run();

It’s my firts post on here so I hope that it’s appropriate.
Thanks for help.

Nicolas

Hi Nicolas,

There are three points.
First, when you create TGraph, it is better to specify number of points:

  TGraph * field = new TGraph(46);

Second, numbering of points starts from 0.
And third, when you set point via index, index should be integer value:

field->SetPoint(TMath::Nint(i*5-5),i*5-4,CV.Modulo());

Regards,
Sergey

1 Like

Hi Sergey, thanks for helping, this solved my problems.

Nicolas