How to use two given vectors of numbers rather than a formula such as "sin(x)"

I’m following the tutorial and have this, which works (I’m compiling it, and then running it – no interactivity):

	TCanvas c("c", title.c_str(), 0, 0, width, height);
	TF1 f1("f1","sin(x)", -5, 5);
	f1.SetTitle(title.c_str());
	f1.Draw();
	c.Print("plot/all.pdf");

Now, instead of passing an expression like “sin(x)” I want to pass two vectors declared as
vector <double> x, y;. How do I do that?

I’m sorry, I’m new to ROOT and couldn’t find a solution in the tutorial/documentation.

Best regards
Jérôme


ROOT Version: 6.26/06
Platform: Ubuntu 18.04
Compiler: g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0


Hi Jérôme,

Welcome to the forum :slight_smile:

If you want to make a plot using predefined data points, instead of mathematical formula, I would use TGraph instead of TF1.

        TCanvas c("c", title.c_str(), 0, 0, width, height);
        std::vector <double> x {1., 2., 3., 4., 5.};
        std::vector <double> y {1., 4., 9., 16., 25.};
        TGraph gr(x.size(), &x[0], &y[0]);
	    gr.Draw("APL");
        gr.SetMarkerStyle(20);
	    c.Print("plot/all.pdf");

Cheers! That’s exactly what I was looking for