Home | News | Documentation | Download

Fitting a multigraph

Hello everyone,

I want to fit a Graph, in which I use ‘star’ markers. The thing is, when I apply the fit, the markers disappear and I only see the line of the fit and the curves of the graphs in the multigraph.
Do you know any command to cope with this ? Or is it just not possible to keep the markers with the fit curve ?

Thanks in advance
Adèle


  	TMultiGraph *mg = new TMultiGraph();
  	mg->SetTitle("Calibration Ligne Faisceau");
  	mg->GetXaxis()->SetTitle("Energy [keV]");
	mg->GetYaxis()->SetTitle("Channels");

	
	// Drawing the Graph with the TGraph class.
	TGraph *f1 = new TGraph(n, X, Y);
	f1->SetMarkerColor(4);
	f1->SetMarkerSize(2);
	f1->SetLineColor(4);
	//f1->Draw("AC*");
	

	TGraph *alpha = new TGraph(3, X_a, Y_a);
	alpha->SetMarkerColor(3);
	alpha->SetMarkerSize(2);
	alpha->SetLineColor(3);
	//alpha->Draw("CP");


	// adding all the Graphs into the total graph for the plot.
	mg->Add(f1);
	mg->Add(alpha);
	mg->Draw("A*");





//________________________
// Linear regression = linear fit with a straight line (type y = ax +b)


	TF1 *reglin = new TF1("Regression lineaire","[0]*x+[1]", 3000, 11000);
	reglin->SetParameters(0.2,2);
	reglin->SetParNames("Gain", "E0"); //parametrisation : a = gain ; b = offset energy (E0)
	//reglin->SetLineColor(3);

	reglin->Draw();
	mg->Fit(reglin);
	mg->Draw();
	gStyle->SetOptFit(1); // Normalises the axis.

	float chi2 = reglin->GetChisquare();
	cout << "chi2 = " << chi2 << endl; 

Can you provide a macro we can run ? with the vectors building the TGraphs defined ?

Sure, here is the total macro :

using namespace std;

void Calibration(){

    TCanvas *fenetre = new TCanvas("Regression_lineaire", "Calibration", 700 , 500) ;
    fenetre->Draw();

	gStyle->SetOptStat(1); // Normalises the axis

	const Int_t n = 8 ; // Total number of data in file.

	// number of data in Si1 files : 11
	// number of data in Si2 files : 10
	// number of data in Si3 files : 8
	// number of data in Si4 files : 6
	

//____________________
	// Experimental datas.

	// Storing values of ENERGIES in a 1D array.
	Double_t E[n+3] ;
	Double_t C[n+3] ; // Uncertainty on the energy.

	Double_t X_a[3];
	Double_t Y_a[3];

	Double_t X[n];
	Double_t Y[n];


	// part for Si calibration
	ifstream data("E_loss_24_86_Si_3.txt"); // file storing channel datas.
	for(Int_t i = 0 ; i < n+3 ; i++){
       	data >> C[i] >> E[i] ; // filling the arrays with the values of the file. 
	}
    data.close();

	for (Int_t i = 0 ; i < n ; i++){
		Y[i] = C[i];
		X[i] = E[i];
	}

	for (Int_t i = 0 ; i < 3 ; i++){
		Y_a[i] = C[n+i];
		X_a[i] = E[n+i];
	}


  	TMultiGraph *mg = new TMultiGraph();
  	mg->SetTitle("Calibration Ligne Faisceau");
  	mg->GetXaxis()->SetTitle("Energy [keV]");
	mg->GetYaxis()->SetTitle("Channels");

	
	// Drawing the Graph with the TGraph class.
	TGraph *f1 = new TGraph(n, X, Y);
	f1->SetMarkerColor(4);
	f1->SetMarkerSize(2);
	f1->SetLineColor(4);
	//f1->Draw("AC*");
	

	TGraph *alpha = new TGraph(3, X_a, Y_a);
	alpha->SetMarkerColor(3);
	alpha->SetMarkerSize(2);
	alpha->SetLineColor(3);
	//alpha->Draw("CP");


	// adding all the Graphs into the total graph for the plot.
	mg->Add(f1);
	mg->Add(alpha);
	mg->Draw("A*");





//________________________
// Linear regression = linear fit with a straight line (type y = ax +b)


	TF1 *reglin = new TF1("Regression lineaire","[0]*x+[1]", 3000, 11000);
	reglin->SetParameters(0.2,2);
	reglin->SetParNames("Gain", "E0"); //parametrisation : a = gain ; b = offset energy (E0)
	//reglin->SetLineColor(3);

	reglin->Draw();
	mg->Fit(reglin);
	mg->Draw();
	gStyle->SetOptFit(1); // Normalises the axis.

	float chi2 = reglin->GetChisquare();
	cout << "chi2 = " << chi2 << endl; 
}

E_loss_24_86_Si_3.txt (266 Bytes)

Thanks I will look.

1 Like

Et voilà:

Calibration.C (2.2 KB)

Note: the points in the blue graph are not ordered. It visible by looking at the line drawn. It makes a loop backward.

Thanks a lot.
Actually I do not need the blue line to be plot, thus I thought it was not important if the point were not ordered.
I tried to run the code with your correction at my place and it does not let the point appear neither. Do you think that the fact that the points are not ordered is the reason the fit takes the whole screen ?

What about if you run exactly the macro I send you ? do you see the plot ?

I actually do !
Thanks a lot again :slight_smile:

Ok so very likely there was some mistake when you transcribed the changes in the main code. Can you send me what you did ? I will look at it asap (I need to update my Mac now)

Is it ok ? did you managed to see the correct plot when you import the code in your real program ?