# I cannot draw multiple histogram

**URL:** <https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202>\
**Category:** Newbie\
**Created:** [March 24, 2023, 1:52pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202 "2023-03-24T13:52:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![DAVIDE\_GATTI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/davide_gatti/32/32846_2.png) [@DAVIDE\_GATTI](https://root-forum.cern.ch/u/DAVIDE_GATTI)\
**Post date:** [March 24, 2023, 1:52pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/1 "2023-03-24T13:52:15Z")

</div>

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:

```auto
//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()
}

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 24, 2023, 2:10pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/2 "2023-03-24T14:10:33Z")

</div>

In the beginning, add:

```cpp
#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`

---

<div class="post-metadata">

**Author:** ![DAVIDE\_GATTI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/davide_gatti/32/32846_2.png) [@DAVIDE\_GATTI](https://root-forum.cern.ch/u/DAVIDE_GATTI)\
**Post date:** [March 24, 2023, 7:39pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/3 "2023-03-24T19:39:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 24, 2023, 8:49pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/4 "2023-03-24T20:49:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DAVIDE\_GATTI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/davide_gatti/32/32846_2.png) [@DAVIDE\_GATTI](https://root-forum.cern.ch/u/DAVIDE_GATTI)\
**Post date:** [March 24, 2023, 9:09pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/5 "2023-03-24T21:09:26Z")

</div>

Yeah, at the moment my macro is:

```cpp
#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();
}

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 24, 2023, 9:40pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/6 "2023-03-24T21:40:50Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![DAVIDE\_GATTI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/davide_gatti/32/32846_2.png) [@DAVIDE\_GATTI](https://root-forum.cern.ch/u/DAVIDE_GATTI)\
**Post date:** [March 24, 2023, 10:00pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/7 "2023-03-24T22:00:58Z")

</div>

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?!)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 24, 2023, 10:13pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/8 "2023-03-24T22:13:03Z")

</div>

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 ...`

---

<div class="post-metadata">

**Author:** ![DAVIDE\_GATTI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/davide_gatti/32/32846_2.png) [@DAVIDE\_GATTI](https://root-forum.cern.ch/u/DAVIDE_GATTI)\
**Post date:** [March 25, 2023, 5:21pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/9 "2023-03-25T17:21:03Z")

</div>

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();

```

}

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 25, 2023, 5:42pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/10 "2023-03-25T17:42:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DAVIDE\_GATTI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/davide_gatti/32/32846_2.png) [@DAVIDE\_GATTI](https://root-forum.cern.ch/u/DAVIDE_GATTI)\
**Post date:** [March 25, 2023, 6:20pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/11 "2023-03-25T18:20:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 25, 2023, 6:33pm UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/12 "2023-03-25T18:33:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DAVIDE\_GATTI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/davide_gatti/32/32846_2.png) [@DAVIDE\_GATTI](https://root-forum.cern.ch/u/DAVIDE_GATTI)\
**Post date:** [March 26, 2023, 8:27am UTC](https://root-forum.cern.ch/t/i-cannot-draw-multiple-histogram/54202/13 "2023-03-26T08:27:40Z")

</div>

Thank you very much
