Problem with changing axis label

Hello everyone,

I am trying to plot a graph with certain y values with respect to their run numbers along the x-axis. Here is my code :

        const Int_t n = 11;
        Double_t run[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //run index that needs to be changed on the x-axis

/* Some code to compute my y values */

       TCanvas *c1 = new TCanvas("c1", "c1", 700, 500);
	c1->SetGrid();
	
	TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
	
	gr->GetXaxis()->ChangeLabel(1, -1, -1, -1, -1, -1, "0");
	gr->GetXaxis()->ChangeLabel(2, -1, -1, -1, -1, -1, "1");
	gr->GetXaxis()->ChangeLabel(3, -1, -1, -1, -1, -1, "2");
	gr->GetXaxis()->ChangeLabel(4, -1, -1, -1, -1, -1, "139");
	gr->GetXaxis()->ChangeLabel(5, -1, -1, -1, -1, -1, "140");
	gr->GetXaxis()->ChangeLabel(6, -1, -1, -1, -1, -1, "141");
	gr->GetXaxis()->ChangeLabel(7, -1, -1, -1, -1, -1, "142");
	gr->GetXaxis()->ChangeLabel(8, -1, -1, -1, -1, -1, "144");
	gr->GetXaxis()->ChangeLabel(9, -1, -1, -1, -1, -1, "147");
	gr->GetXaxis()->ChangeLabel(10, -1, -1, -1, -1, -1, "149");
	gr->GetXaxis()->ChangeLabel(11, -1, -1, -1, -1, -1, "150");
	
	gr->SetTitle("#phi_{477}/#phi_{980}");
	gr->GetXaxis()->SetLimits(0.65, 11.45);
	gr->SetMinimum(0.1);
	gr->SetMaximum(2.5);
	gr->GetXaxis()->SetTitle("Run index");
	gr->GetYaxis()->SetTitle("#phi_{477}/#phi_{980}");
	
	
	
	gr->SetMarkerStyle(20);
	gr->SetMarkerColor(kBlue+1);
	gr->SetMarkerSize(0.9);
	gr->Fit("fun_fit");
	gr->Draw("AP");

As you can see I have different labels for my 11 runs going from 0 to 150. Using this type of code worked for me before but now it just won’t make it as shown in the next picture.
c1.pdf (14.4 KB)

Do you have any idea what’s wrong ?

can you provide a running example ?

root [0] 
Processing mz.C...
/Users/couet/roottest/./mz.C:11:48: error: use of class template 'ratio' requires template arguments
   TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
                                               ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ratio:248:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS ratio
                           ^
/Users/couet/roottest/./mz.C:11:58: error: use of undeclared identifier 'sig_ratio'
   TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
                                                         ^

//Calcul de l'incertitude sur le rapport d'embranchement du gamma à 477keV

#include "TH1.h"
#include "TMath.h"
#include "TF1.h"
#include "TLegend.h"
#include "TCanvas.h"


{

	const Int_t n = 11;
	
	Double_t run[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
	//RUN 0, 1, 2, 139, 140, 141, 142, 144, 147, 149, 150
	
	//ANCIENNE ANALYSE AVEC LES VIEILLES EFFICACITES
	//Double_t phi_477[n] = {212574, 254687, 259500, 720549, 701025, 658536, 589420, 536485, 522301, 587538, 577487};
	//Double_t sig_phi_477[n] = {24142, 34929, 36492, 80856, 82734, 75177, 68634, 82093, 66930, 70391, 67724};
	
	
	//NOUVELLES EFFICACITES MAIS SIGNAL A MU +- 3SIG
	//Double_t phi_477[n] = {216175, 259001, 263895, 732620, 712769, 669568, 599294, 545473, 531051, 597381, 587162};
	//Double_t sig_phi_477[n] = {5442, 20953, 22871, 22335, 33928, 24775, 26098, 59134, 36649, 30988, 26844};
	
	//SIGNAL ENTRE BORNES DEFINIES AVEC LES CHAINES
	Double_t phi_477[n] = {218332, 264278, 267550, 708438, 691411, 657000, 585347, 522908, 515426, 576696, 571899};
	Double_t sig_phi_477[n] = {5314, 19598, 20671, 20553, 29995, 22583, 23156, 49936, 32215, 27198, 23759};
	
	
	//Double_t phi_980[n] = {140049, 181945, 174288, 459274, 462182, 455030, 391348, 337135, 341731, 386646, 384988};
	//Double_t sig_phi_980[n] = {19583, 25457, 24388, 62772, 63122, 62124, 53429, 46046, 46658, 52792, 52564};
	
	Double_t phi_980[n] = {162792, 195363, 186831, 497068, 494028, 486971, 407969, 348544, 357062, 408052, 406690};
	Double_t sig_phi_980[n] = {1298, 1810, 1775, 4248, 4454, 4063, 3399, 3180, 3012, 3469, 3438};

	Double_t ratio[n];
	Double_t sig_ratio[n];
	
	for(Int_t i = 0; i < n; i++){
		ratio[i] = phi_477[i]/phi_980[i];
		sig_ratio[i] = pow(pow(sig_phi_477[i]/phi_980[i], 2) + pow(phi_477[i]*sig_phi_980[i]/pow(phi_980[i], 2), 2), 0.5);
		cout << "rat = " << ratio[i] << endl;
		cout << "sig_rat = " << sig_ratio[i] << endl;
		}
		
	TF1 *fun_fit = new TF1("fun_fit", "[0]", 1, 11);
	
	TCanvas *c1 = new TCanvas("c1", "c1", 700, 500);
	c1->SetGrid();
	
	TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);
	
	gr->GetXaxis()->ChangeLabel(1, -1, -1, -1, -1, -1, "0");
	gr->GetXaxis()->ChangeLabel(2, -1, -1, -1, -1, -1, "1");
	gr->GetXaxis()->ChangeLabel(3, -1, -1, -1, -1, -1, "2");
	gr->GetXaxis()->ChangeLabel(4, -1, -1, -1, -1, -1, "139");
	gr->GetXaxis()->ChangeLabel(5, -1, -1, -1, -1, -1, "140");
	gr->GetXaxis()->ChangeLabel(6, -1, -1, -1, -1, -1, "141");
	gr->GetXaxis()->ChangeLabel(7, -1, -1, -1, -1, -1, "142");
	gr->GetXaxis()->ChangeLabel(8, -1, -1, -1, -1, -1, "144");
	gr->GetXaxis()->ChangeLabel(9, -1, -1, -1, -1, -1, "147");
	gr->GetXaxis()->ChangeLabel(10, -1, -1, -1, -1, -1, "149");
	gr->GetXaxis()->ChangeLabel(11, -1, -1, -1, -1, -1, "150");
	
	gr->SetTitle("#phi_{477}/#phi_{980}");
	gr->GetXaxis()->SetLimits(0.65, 11.45);
	gr->SetMinimum(0.1);
	gr->SetMaximum(2.5);
	gr->GetXaxis()->SetTitle("Run index");
	gr->GetYaxis()->SetTitle("#phi_{477}/#phi_{980}");
	
	
	
	gr->SetMarkerStyle(20);
	gr->SetMarkerColor(kBlue+1);
	gr->SetMarkerSize(0.9);
	gr->Fit("fun_fit");
	gr->Draw("AP");
	
	


}
//Calcul de l'incertitude sur le rapport d'embranchement du gamma à 477keV

#include "TH1.h"
#include "TMath.h"
#include "TF1.h"
#include "TLegend.h"
#include "TCanvas.h"


{

   const Int_t n = 11;

   Double_t run[n] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
   //RUN 0, 1, 2, 139, 140, 141, 142, 144, 147, 149, 150

   //ANCIENNE ANALYSE AVEC LES VIEILLES EFFICACITES
   //Double_t phi_477[n] = {212574, 254687, 259500, 720549, 701025, 658536, 589420, 536485, 522301, 587538, 577487};
   //Double_t sig_phi_477[n] = {24142, 34929, 36492, 80856, 82734, 75177, 68634, 82093, 66930, 70391, 67724};


   //NOUVELLES EFFICACITES MAIS SIGNAL A MU +- 3SIG
   //Double_t phi_477[n] = {216175, 259001, 263895, 732620, 712769, 669568, 599294, 545473, 531051, 597381, 587162};
   //Double_t sig_phi_477[n] = {5442, 20953, 22871, 22335, 33928, 24775, 26098, 59134, 36649, 30988, 26844};

   //SIGNAL ENTRE BORNES DEFINIES AVEC LES CHAINES
   Double_t phi_477[n] = {218332, 264278, 267550, 708438, 691411, 657000, 585347, 522908, 515426, 576696, 571899};
   Double_t sig_phi_477[n] = {5314, 19598, 20671, 20553, 29995, 22583, 23156, 49936, 32215, 27198, 23759};


   //Double_t phi_980[n] = {140049, 181945, 174288, 459274, 462182, 455030, 391348, 337135, 341731, 386646, 384988};
   //Double_t sig_phi_980[n] = {19583, 25457, 24388, 62772, 63122, 62124, 53429, 46046, 46658, 52792, 52564};

   Double_t phi_980[n] = {162792, 195363, 186831, 497068, 494028, 486971, 407969, 348544, 357062, 408052, 406690};
   Double_t sig_phi_980[n] = {1298, 1810, 1775, 4248, 4454, 4063, 3399, 3180, 3012, 3469, 3438};

   Double_t ratio[n];
   Double_t sig_ratio[n];

   for(Int_t i = 0; i < n; i++){
      ratio[i] = phi_477[i]/phi_980[i];
      sig_ratio[i] = pow(pow(sig_phi_477[i]/phi_980[i], 2) + pow(phi_477[i]*sig_phi_980[i]/pow(phi_980[i], 2), 2), 0.5);
      cout << "rat = " << ratio[i] << endl;
      cout << "sig_rat = " << sig_ratio[i] << endl;
      }

   TF1 *fun_fit = new TF1("fun_fit", "[0]", 1, 11);

   TCanvas *c1 = new TCanvas("c1", "c1", 700, 500);
   c1->SetGrid();

   TGraphErrors *gr = new TGraphErrors(n, run, ratio, 0, sig_ratio);

   gr->SetTitle("#phi_{477}/#phi_{980}");
   gr->GetXaxis()->SetLimits(0.65, 11.45);
   gr->SetMinimum(0.1);
   gr->SetMaximum(2.5);
   gr->GetXaxis()->SetTitle("Run index");
   gr->GetYaxis()->SetTitle("#phi_{477}/#phi_{980}");
   gr->GetXaxis()->SetNdivisions(515);

   gr->GetXaxis()->ChangeLabel(1, -1, -1, -1, -1, -1, "0");
   gr->GetXaxis()->ChangeLabel(2, -1, -1, -1, -1, -1, "1");
   gr->GetXaxis()->ChangeLabel(3, -1, -1, -1, -1, -1, "2");
   gr->GetXaxis()->ChangeLabel(4, -1, -1, -1, -1, -1, "139");
   gr->GetXaxis()->ChangeLabel(5, -1, -1, -1, -1, -1, "140");
   gr->GetXaxis()->ChangeLabel(6, -1, -1, -1, -1, -1, "141");
   gr->GetXaxis()->ChangeLabel(7, -1, -1, -1, -1, -1, "142");
   gr->GetXaxis()->ChangeLabel(8, -1, -1, -1, -1, -1, "144");
   gr->GetXaxis()->ChangeLabel(9, -1, -1, -1, -1, -1, "147");
   gr->GetXaxis()->ChangeLabel(10, -1, -1, -1, -1, -1, "149");
   gr->GetXaxis()->ChangeLabel(11, -1, -1, -1, -1, -1, "150");

   gr->SetMarkerStyle(20);
   gr->SetMarkerColor(kBlue+1);
   gr->SetMarkerSize(0.9);
   gr->Fit("fun_fit");
   gr->Draw("AP");
}

Okay it works but I have to check why

Thanks for the answer though !

Best

ChangeLabel change existing labels. In you case the label optimization displayed to low number of labels.

1 Like

Oh I see, for my other code where it works I actually have 9 and not 11 points to plot so the default value of n=510 works there but not here !

Thanks for your reply !
Best

1 Like