I cannot draw multiple histogram

I’m trying to read a file with many columns, where each column corresponds to an element for which I need to draw a histogram. The columns I’m interested in are the fourth, the fifth, the seventh and so on. I tried writing a macro, but it takes wrong values and after a while, it gives me this error:
Error in : fatal X11 error (connection to server lost?!)
Here there is my code:

//vedere le modifiche fatte sul file di ubuntu
void dose_sf2()
	{
	std::fstream instream; //stream per leggere il file
	std::string valoriString; //stringa in cui volta per volta inserisco una riga intera del file
	std::vector<std::vector<double>> valori; //matrice di vettori
	std::vector<TH1F *> Istogrammi; //vettore di istogrammi
	std::vector<TCanvas *> Canva; //creo diverse canvas così da avere gli istogrammi singoli
	string nomi[102]={i, j, k, Dose, anti_nu_e, anti_nu_e_f, e-, e-_f,	gamma, gamma_f, neutron_1, neutron_1_f, proton,	proton_f, deuteron, deuteron_f, alpha, alpha_f,	Li7, Li7_f,	C12, C12_f, C13, C13_f, C14, C14_f, N14, N14_f, N15, N15_f, O16, O16_f, O17, O17_f, Na23, Na23_f,	Na24, Na24_f, Mg24, Mg24_f, Mg25, Mg25_f,	Mg26,	Mg26_f, Si29, Si29_f, Si30, Si30_f,	P31, P31_f,	P32, P32_f, S32,	S32_f, S33,	S33_f, S35,	S35_f, S36,	S36_f, Cl35, Cl35_f, Cl36, Cl36_f,	Cl37,	Cl37_f, Cl38, Cl38_f, Ar36, Ar36_f,	Ar38,	Ar38_f, Ar40, Ar40_f, K39, K39_f, K40, K40_f, K41, K41_f, K42, K42_f,	Ca40,	Ca40_f, Ca42, Ca42_f, Mn55, Mn55_f,	Fe55,	Fe55_f, Fe57, Fe57_f, Cu65, Cu65_f,	Zn65,	Zn65_f, Zn68, Zn68_f, Zn69, Zn69_f,	Ga69,	Ga69_f};
	int i=0;
	instream.open("/home/davide/tesi/BNCT-build/dose_sf2.out");
	while (std::getline(instream, valoriString))
		{ //prendo una rifa intera e la salvo come stringa
		std::vector<double> valoriRiga; //conterrà i valori che mi interessano della riga
		std::stringstream stream(valoriString); //stream per leggere la stringa come se fosse un file e poter usare >>
		double tempVal; //salvo il valore temporaneo volta per volta
		while (stream >> tempVal)
			{ //estraggo il singolo valore e lo metto in tempval
			i++;
			if ((i>2) && (i % 2 != 0)){
				valoriRiga.push_back(tempVal); //in questo modo salvo solo le dosi che mi interessano
				std::string nome = "h" + i;
				Istogrammi.push_back(new TH1F(nome, nomi[i],80,0,20));
				}	
			}
		valori.push_back(valoriRiga); //aggiungo il vettore con i valori alla matrice
		i=0;
		}
	
	int k=1;
	for (auto riga : valori)
		{
		int l=0;
		for (double elemento : riga){
			std::cout << elemento << " ";
			Istogrammi[l]->SetBinContent(k;elemento); //qui lascio fisso il bin da riempire e scorro sugli istogrammi
			l++;
			}
		std::cout << "\n";
		k++;
		}
	
	int j = 1;
	for (auto isto : Istogrammi)
		{ //qui mi occupo del settaggio degli istogrammi
		isto->GetXaxis()->SetTitle("Depth(cm)");
		isto->GetYaxis()->SetTitle("Dose");
		Canva.push_back(new TCanvas());
		if (j<10)
			{
			isto->SetFillColor(j);
			isto->SetLineColor(j);
			}
		else
			{
			isto->SetFillColor(j+1);
			isto->SetLineColor(j+1);
			}
		isto->Draw();
		j++;
		}
	instream.close()
}

In the beginning, add:

#include "TH1.h"
#include "TCanvas.h"
#include "TAxis.h"

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>

Then, fix all errors and warnings reported by:

$(root-config --cxx --cflags) -O2 -Wall -Wextra -c dose_sf2.cxx

thank you for you help, but when I try to initialize the histograms with push_back, there are warningsabout TROOT::Append and potential memory leaks

At least: std::string nome = "h" + std::to_string(i);

Yeah, at the moment my macro is:

#include "TH1.h"
#include "TCanvas.h"
#include "TAxis.h"

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
void dose_sf2()
	{
	std::fstream instream; //stream per leggere il file
	std::string valoriString; //stringa in cui volta per volta inserisco una riga intera del file
	std::vector<std::vector<double>> valori; //matrice di vettori
	std::vector<TH1F *> Istogrammi; //vettore di istogrammi
	std::vector<TCanvas *> Canva; //creo diverse canvas così da avere gli istogrammi singoli
	std::vector<string> nomi={"i","j","k","Dose(Gy)","e-","e-_f","gamma","gamma_f","anti_nu_e","anti_nu_e_f","neutron_1","neutron_1_f","proton","proton_f","deuteron","deuteron_f","alpha","alpha_f","Li7","Li7_f","B11","B11_f","C12","C12_f","C13","C13_f","C14","C14_f","N14","N14_f","N15","N15_f","O16","O16_f","O17","O17_f","Na24","Na24_f","Mg24","Mg24_f","Mg25","Mg25_f","Mg26","Mg26_f","Si29","Si29_f","P31","P31_f","P32","P32_f","S32","S32_f","S33","S33_f","S34","S34_f","S35","S35_f","S36","S36_f","Cl35","Cl35_f","Cl36","Cl36_f","Cl38","Cl38_f","Ar36","Ar36_f","Ar38","Ar38_f","Ar40","Ar40_f","K39","K39_f","K40","K40_f","K41","K41_f","K42","K42_f","Ca40","Ca40_f","Ca42","Ca42_f","Mn55","Mn55_f","Fe55","Fe55_f","Fe57","Fe57_f"};
	int a=0;
	//std::cout << nomi.size() << endl;
	instream.open("/home/davide/tesi/BNCT-build/dose_sf2.out");
	while (std::getline(instream, valoriString))
		{ //prendo una rifa intera e la salvo come stringa
		std::vector<double> valoriRiga; //conterrà i valori che mi interessano della riga
		std::stringstream stream(valoriString); //stream per leggere la stringa come se fosse un file e poter usare >>
		double tempVal; //salvo il valore temporaneo volta per volta
		while (stream >> tempVal)
			{ 
			//std::cout << tempVal << " ";//estraggo il singolo valore e lo metto in tempval
			if (a>2) 
				{
				if ((a==3) || (a % 2 == 0))
					{
					//std::cout << tempVal << " ";
					valoriRiga.push_back(tempVal); //in questo modo salvo solo le dosi che mi interessano
					std::string nome = "h" + std::to_string(i);				
					Istogrammi.push_back(new TH1F(nome.c_str(), nomi[a].c_str(), 80, 0., 20.));
					}
				}
			a=a+1;
			}
		std::cout << "\n";
		a=0;
		valori.push_back(valoriRiga); //aggiungo il vettore con i valori alla matrice
		}
	
	//int l=1;
	//for (auto riga : valori)
	//	{
	//	int m=0;
	//	for (double elemento : riga){
	//		std::cout << elemento << " ";
	//		Istogrammi[m]->SetBinContent(l,elemento); //qui lascio fisso il bin da riempire e scorro sugli istogrammi
	//		m++;
	//		}
	//	std::cout << "\n";
	//	l++;
	//	}
	
	//int n = 1;
	//for (auto isto : Istogrammi)
	//	{ //qui mi occupo del settaggio degli istogrammi
	//	isto->GetXaxis()->SetTitle("Depth(cm)");
	//	isto->GetYaxis()->SetTitle("Dose");
	//	Canva.push_back(new TCanvas());
	//	if (n<10)
	//		{
	//		isto->SetFillColor(n);
	//		isto->SetLineColor(n);
	//		}
	//	else
	//		{
	//		isto->SetFillColor(n+1);
	//		isto->SetLineColor(n+1);
	//		}
	//	isto->Draw();
	//	n++;
	//	}
	instream.close();
}

std::vector<std::string> nomi={...};

std::string nome = "h" + std::to_string(a);

Yeahs, the warnings are still there and if I try to uncomment the second part of the macro it gives me Error in : fatal X11 error (connection to server lost?!)

The “potential memory leak” warnings come from the fact that, in the “while (std::getline(instream, valoriString))” loop, you create histograms with the same “nome” for every “valoriString”.

What concerns the “X11 error” … if you connect to a remote machine, make sure you use: ssh -X -Y ...

I understand, I want only to inizialize an histogram per column, I correct that part but now I want to create a Canva for each histogram varying the name of the Canva in order to have the histograms on different Canvas, but I don’t know how to do that.
int l=1;
for (auto riga : valori)
{
int m=0;
for (double elemento : riga){
std::cout << elemento << " ";
Istogrammi[m]->SetBinContent(l,elemento); //qui lascio fisso il bin da riempire e scorro sugli istogrammi
m++;
}
std::cout << “\n”;
l++;
}

int n = 1;
for (auto isto : Istogrammi)
	{ //qui mi occupo del settaggio degli istogrammi
	isto->GetXaxis()->SetTitle("Depth(cm)");
	isto->GetYaxis()->SetTitle("Dose");
	
	//Canva.push_back(new TCanvas());
	if (n<10)
		{
		isto->SetFillColor(n);
		isto->SetLineColor(n);
		}
	else
		{
		isto->SetFillColor(n+1);
		isto->SetLineColor(n+1);
		}
	isto->Draw();
	n++;
	}
instream.close();

}

Each “new TCanvas()” should automatically generate a new name.

Bit of I use Canvas.push_back(new TCanvas()) before isto->Draw() It gives fatale error

Try: isto->Draw(); gPad->Modified(); gPad->Update();

Thank you very much