Read vectors as variables for a TGraph

I’m quite unexperienced with ROOT…
I store data (x,y and error of y values) in vectors and want to draw “y : x” with TGraph. Somehow it seems that TGraph doesn’t recognize my vectors.

As a second step I want to add a second graph in my plot with TGraphAsymmErrors; that’s why I use TGraph and not simply a histogram.

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <TGraph.h>
#include <TGraphAsymmErrors.h>
#include <TFile.h>
#include <TCanvas.h>
#include <TH1.h>

using namespace std;
using std::ifstream;

vector<double> xp;
vector<double> F2;
vector<double> errF2;

void plotting(){
//make filenames
	string name,file_mod,file_dat,file_root;
		name = "Q1100b394";
		file_mod = "model/" + name + ".dat";

//read model
		int i = 0;
		double mtest;

		ifstream modfile(file_mod.c_str());
		while (modfile >> mtest){
			if (i%3 == 2){
			else if (i%3 == 0){

	 	gROOT ->SetStyle("Plain");
		TCanvas *c1 = new TCanvas("c1",name.c_str(),200,10,700,500);
// draw a frame to define the range
		TH1F *h = c1->DrawFrame(0.0001,0,0.01,1);

		TGraph *g_mod = new TGraph(xp.size(),xp,F2);

		g_mod ->SetLineColor(1);
	 	g_mod ->SetLineWidth(2);
	 	g_mod ->SetMarkerColor(1);
	 	g_mod ->SetMarkerStyle(1);
	 	g_mod ->GetXaxis()->SetTitle("x_{P}");
	 	g_mod ->GetYaxis()->SetTitle("F2");
	 	g_mod ->GetXaxis()->CenterTitle();
	 	g_mod ->GetYaxis()->CenterTitle();
	 	g_mod ->Draw("CP");

		delete xp;
		delete F2;
		delete errF2;

This is my error message:

Error: Can't call TGraph::TGraph(xp.size(),xp,F2) in current scope plotting.cpp:49:
Possible candidates are...
(in TGraph)
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(void);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(Int_t n);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(Int_t n,const Int_t* x,const Int_t* y);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(Int_t n,const Float_t* x,const Float_t* y);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(Int_t n,const Double_t* x,const Double_t* y);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(const TGraph& gr);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(const TVectorF& vx,const TVectorF& vy);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(const TVectorD& vx,const TVectorD& vy);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(const TH1* h);
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(const TF1* f,Option_t* option="");
/Users/***/Programs/root/lib/  -1:-1   0 public: TGraph TGraph::TGraph(const char* filename,const char* format="%lg %lg",Option_t* option="");

Thanks a lot for help!


There is no TGraph constructor taking vectors as arguments. Try this syntax:

Cheers, Bertrand.

That works perfectly fine Bertrand, thank you!
Can you explain me why?


Hi Boginja,

The STL vector is designed to give direct access to its internal array of type (double in this case) by using &vec[0], which is similar to a double *

Cheers, Bertrand.