Plot histograms from different cpp

Dear all,
I have made two identical programs that creates vector of histograms. Now I have to plot two of those histograms(one from the first and one from the second set of histograms) on the same canvas superimposed. Do you know how to do it?

I thought as something like this:

TFile * inputfile = new TFile("TryPush.root","READ");
//[code]
TGraph* g = (TGraph*) inputfile -> Get("histos[0]");
g->Draw("sames");

But the canvas shows only the first histograms and not histos[0]

thank you,

Svante

inputfile->ls();

It’s not working, I have putted TryPush.root in the same file and I have done:

inputfile->ls();
TGraph* g = (TGraph*) inputfile -> Get("histos[0]");
g->Draw("sames");

But I have this error:

 warning: null passed to a callee that requires a non-null argument [-Wnonnull]
        inputfile->ls();
        ^~~~~~~~~

I really don’t know the problem.

The error says that you did not initialize the “inputfile” pointer.

Can you post a complete example showing the problem ?

Double_t PeakFitErf(Double_t *x, Double_t *p){
		return TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
	}


	
	Double_t PeakFit(Double_t *x, Double_t *p){
		return (p[0]* exp( -0.5 * ((x[0]-p[1])/p[2]) * ((x[0]-p[1])/p[2])))+ p[3]*TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
	}


void TryPush()
{
	//TFile * inputfile = new TFile("TryPush1.root","READ");
	TFile* inputfile = TFile::Open("TryPush.root");
//---list of vector that I use for the program
	vector<Float_t>SBdx;
	vector<Float_t>SBsx;
	vector<Float_t>Count;
	vector<Float_t>Err;
	vector<TH1F*> histos;
	vector<Int_t> Time;//da riempire
	//-----list of histogram
	TH1F * hh = new TH1F("hh", "hh", 16384, 0,16383);
	TH1F * h1;
	TH1F * hc;//istogramma che poi clono per plottare
	TH1F *hccal;
	//------list of fitting function
	TF1 * fitfondo;
	TF1 *fit;
	//----convertion factors for calibration
	Double_t p=0.2318;
	Double_t q=-0.04483;
	
	int i=0;
	double num1=0;
	ifstream infile;
	char filename[15];
	char histoname[15];
	int Ngroup = 10;

	int Nhours = 5;
	int Nhistos = Nhours*2;
	
	
		
	for(int j=0; j<Nhistos; j++) {
	
		for(int l=0; l<Ngroup; l++) {
			sprintf(histoname, "h%d", l+j*Ngroup);
			h1 = new TH1F(histoname,"Quartz",16384,0,16383);
			if(l+j*Ngroup<10) {
				sprintf(filename, "LoAx00%d.txt", l+j*Ngroup);
			} else if(l+j*Ngroup<100){
				sprintf(filename, "LoAx0%d.txt", l+j*Ngroup);
			}else if(l+j*Ngroup<1000){
				sprintf(filename, "LoAx%d.txt", l+j*Ngroup);
			}
			
			cout << filename << endl;
			infile.open(filename);
			
			if(infile.is_open()) {		
				i=0;
				while (1) {
					i+=1;
					infile >> num1;
					h1-> Fill(i,num1);
					if (infile.eof()) break;
				}
				infile.close();
			} else {
				cout << "Failed to open file!" << endl;
			}
			
			hh->Add(h1, +1);
			h1->Reset();
		}		
		hc = (TH1F*)hh->Clone();
		hccal= new TH1F(histoname, "Quartz", 16384,0,(16383*p)+q);
		for(int i=0; i<hc->GetNbinsX(); i++){
				hccal->SetBinContent(i, hc->GetBinContent(i));		
			}
	
		//-------fitting the peak 
		fitfondo = new TF1("fitfondo", PeakFitErf,310.5,313.4,3);
		fit= new TF1("fit",  PeakFit ,310.5,313.4,5);
			fitfondo-> SetParName(1,"offsetshelf");
			fitfondo-> SetParameter(1, 80);
			fitfondo-> SetParName(2,"shiftshelf");
			fitfondo-> SetParameter(2, 388);
	 		fit-> SetParName(0,"normgaus");
			fit-> SetParameter(0,800);
			fit-> SetParName(1,"mean");
			fit-> SetParameter(1,312.);
			fit-> SetParName(2,"sigma");
			fit-> SetParameter(2, 3);
			fit-> SetParName(3,"offsetshelf");
			fit-> SetParameter(3, 80);
			fit-> SetParName(4,"shiftshelf");
			fit-> SetParameter(4, 388);(1, 80);
			hccal-> Fit(fit, "R", "L");
			hccal-> Fit(fitfondo, "R", "L");
			fitfondo-> SetLineColor(kGreen);
			fit-> SetLineColor(kRed);
			fit-> Draw("same");	
			fitfondo-> Draw("same");
		
		Double_t sigma =fit-> GetParameter(2);
		Double_t media=fit-> GetParameter(1);
		Double_t MediaMeno= media-(2*sigma);
		Double_t MediaPiu= media+(2*sigma);
		Double_t conteggi= fit-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
		Double_t error= sqrt(conteggi);
		Double_t conteggifondo= fitfondo-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
		Double_t conteggieffettivi=conteggi-conteggifondo;
		Double_t erroredef= sqrt(conteggieffettivi);
	
		Double_t sideband_sx= hccal-> Integral(hccal->GetBin(282),hccal->GetBin(293));
		Double_t sideband_dx= hccal-> Integral(hccal-> GetBin(347), hccal->GetBin(358));
		//------cout useful 
		cout<< "area fit="<<conteggi << endl;
		cout<<"errore area fit="<<error<<endl;
		cout<<"energia picco fit="<<media<<endl;
		cout<<"conteggi del fondo="<<conteggifondo<<endl;
		cout<<"conteggi con fondo sottratto="<<conteggieffettivi<<endl;
		cout<<"errore conteggi finale="<<erroredef<<endl;
		cout<<"sbsx="<<sideband_sx<<endl;
		cout<<"sbdx="<<sideband_dx<<endl;
		//------push back values filling the vectors
		SBdx.push_back(sideband_dx);
		SBsx.push_back(sideband_sx);
		Count.push_back(conteggieffettivi);
		Err.push_back(erroredef);
		histos.push_back(hccal);
		hh->Reset();
		
	}
	
	TCanvas *c1 = new TCanvas("c1","histogram1",1000,700);
	TH1F * ht = (TH1F*)inputfile->Get(histos[7]);
	ht->SetLineColor(1);
	ht->Draw();
	histos[0]->SetTitle("blabla");
	histos[0]->GetXaxis()->SetTitle("Energy[KeV]");
	histos[0]->GetYaxis()->SetTitle("Count[a.u]");
	histos[0]->SetLineColor(6);
	histos[0]->Draw("histosame");


	return 0;
}

This my code. TryPush1.root is the file root identical to this.

where is the root file ?

TryPush1.root (411 Bytes)
TryPush1.root

it is located in the same folder on my laptop

can you fix the following errors ?

root [0] 
Processing TryPush.C...
In file included from input_line_9:1:
/Users/couet/Downloads/TryPush.C:75:38: error: use of undeclared identifier 'PeakFitErf'
      fitfondo = new TF1("fitfondo", PeakFitErf,310.5,313.4,3);
                                     ^
/Users/couet/Downloads/TryPush.C:76:28: error: use of undeclared identifier 'PeakFit'
      fit= new TF1("fit",  PeakFit ,310.5,313.4,5);
                           ^
/Users/couet/Downloads/TryPush.C:130:34: error: no matching member function for call to 'Get'
   TH1F * ht = (TH1F*)inputfile->Get(histos[7]);
                      ~~~~~~~~~~~^~~
/Users/couet/git/roottrunk-bin/include/TDirectoryFile.h:77:24: note: candidate function not viable: no known conversion from 'std::__1::__vector_base<TH1F *, std::__1::allocator<TH1F *> >::value_type' (aka 'TH1F *') to 'const char *' for
      1st argument
   virtual TObject    *Get(const char *namecycle);
                       ^
/Users/couet/git/roottrunk-bin/include/TDirectoryFile.h:79:33: note: candidate template ignored: couldn't infer template argument 'T'
   template <class T> inline T* Get(const char* namecycle)
                                ^
root [1] 

Double_t PeakFitErf(Double_t *x, Double_t *p){
		return TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
	}


	
	Double_t PeakFit(Double_t *x, Double_t *p){
		return (p[0]* exp( -0.5 * ((x[0]-p[1])/p[2]) * ((x[0]-p[1])/p[2])))+ p[3]*TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
	}


void TryPush()
{
	//TFile * inputfile = new TFile("TryPush1.root","READ");
	TFile* inputfile = TFile::Open("TryPush.root");
//---list of vector that I use for the program
	vector<Float_t>SBdx;
	vector<Float_t>SBsx;
	vector<Float_t>Count;
	vector<Float_t>Err;
	vector<TH1F*> histos;
	vector<Int_t> Time;//da riempire
	//-----list of histogram
	TH1F * hh = new TH1F("hh", "hh", 16384, 0,16383);
	TH1F * h1;
	TH1F * hc;//istogramma che poi clono per plottare
	TH1F *hccal;
	//------list of fitting function
	TF1 * fitfondo;
	TF1 *fit;
	//----convertion factors for calibration
	Double_t p=0.2318;
	Double_t q=-0.04483;
	
	int i=0;
	double num1=0;
	ifstream infile;
	char filename[15];
	char histoname[15];
	int Ngroup = 10;

	int Nhours = 5;
	int Nhistos = Nhours*2;
	
	
		
	for(int j=0; j<Nhistos; j++) {
	
		for(int l=0; l<Ngroup; l++) {
			sprintf(histoname, "h%d", l+j*Ngroup);
			h1 = new TH1F(histoname,"Quartz",16384,0,16383);
			if(l+j*Ngroup<10) {
				sprintf(filename, "LoAx00%d.txt", l+j*Ngroup);
			} else if(l+j*Ngroup<100){
				sprintf(filename, "LoAx0%d.txt", l+j*Ngroup);
			}else if(l+j*Ngroup<1000){
				sprintf(filename, "LoAx%d.txt", l+j*Ngroup);
			}
			
			cout << filename << endl;
			infile.open(filename);
			
			if(infile.is_open()) {		
				i=0;
				while (1) {
					i+=1;
					infile >> num1;
					h1-> Fill(i,num1);
					if (infile.eof()) break;
				}
				infile.close();
			} else {
				cout << "Failed to open file!" << endl;
			}
			
			hh->Add(h1, +1);
			h1->Reset();
		}		
		hc = (TH1F*)hh->Clone();
		hccal= new TH1F(histoname, "Quartz", 16384,0,(16383*p)+q);
		for(int i=0; i<hc->GetNbinsX(); i++){
				hccal->SetBinContent(i, hc->GetBinContent(i));		
			}
	
		//-------fitting the peak 
		fitfondo = new TF1("fitfondo", PeakFitErf,310.5,313.4,3);
		fit= new TF1("fit",  PeakFit ,310.5,313.4,5);
			fitfondo-> SetParName(1,"offsetshelf");
			fitfondo-> SetParameter(1, 80);
			fitfondo-> SetParName(2,"shiftshelf");
			fitfondo-> SetParameter(2, 388);
	 		fit-> SetParName(0,"normgaus");
			fit-> SetParameter(0,800);
			fit-> SetParName(1,"mean");
			fit-> SetParameter(1,312.);
			fit-> SetParName(2,"sigma");
			fit-> SetParameter(2, 3);
			fit-> SetParName(3,"offsetshelf");
			fit-> SetParameter(3, 80);
			fit-> SetParName(4,"shiftshelf");
			fit-> SetParameter(4, 388);(1, 80);
			hccal-> Fit(fit, "R", "L");
			hccal-> Fit(fitfondo, "R", "L");
			fitfondo-> SetLineColor(kGreen);
			fit-> SetLineColor(kRed);
			fit-> Draw("same");	
			fitfondo-> Draw("same");
		
		Double_t sigma =fit-> GetParameter(2);
		Double_t media=fit-> GetParameter(1);
		Double_t MediaMeno= media-(2*sigma);
		Double_t MediaPiu= media+(2*sigma);
		Double_t conteggi= fit-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
		Double_t error= sqrt(conteggi);
		Double_t conteggifondo= fitfondo-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
		Double_t conteggieffettivi=conteggi-conteggifondo;
		Double_t erroredef= sqrt(conteggieffettivi);
	
		Double_t sideband_sx= hccal-> Integral(hccal->GetBin(282),hccal->GetBin(293));
		Double_t sideband_dx= hccal-> Integral(hccal-> GetBin(347), hccal->GetBin(358));
		//------cout useful 
		cout<< "area fit="<<conteggi << endl;
		cout<<"errore area fit="<<error<<endl;
		cout<<"energia picco fit="<<media<<endl;
		cout<<"conteggi del fondo="<<conteggifondo<<endl;
		cout<<"conteggi con fondo sottratto="<<conteggieffettivi<<endl;
		cout<<"errore conteggi finale="<<erroredef<<endl;
		cout<<"sbsx="<<sideband_sx<<endl;
		cout<<"sbdx="<<sideband_dx<<endl;
		//------push back values filling the vectors
		SBdx.push_back(sideband_dx);
		SBsx.push_back(sideband_sx);
		Count.push_back(conteggieffettivi);
		Err.push_back(erroredef);
		histos.push_back(hccal);
		hh->Reset();
		
	}
	
	TCanvas *c1 = new TCanvas("c1","histogram1",1000,700);
	TH1F * ht = (TH1F*)inputfile->Get(histos[7]);
	ht->SetLineColor(1);
	ht->Draw();
	histos[0]->SetTitle("blabla");
	histos[0]->GetXaxis()->SetTitle("Energy[KeV]");
	histos[0]->GetYaxis()->SetTitle("Count[a.u]");
	histos[0]->SetLineColor(6);
	histos[0]->Draw("histosame");


	return 0;
}

there doesn’t seem to be anything in that ROOT file:

$> root-dump ./Downloads/TryPush1.root 
>>> file[./Downloads/TryPush1.root]

$> root-ls -t ./Downloads/TryPush1.root 
=== [./Downloads/TryPush1.root] ===
version: 61206

In an identical .cpp I added

//[code]
void TryPush()
{
	TFile *MyFile = new TFile("TryPush.root","NEW");
	//---list of vector that I use for the program
	vector<Float_t>SBdx;
	vector<Float_t>SBsx;
	vector<Float_t>Count;

//[code]

to create the root file. Is it wrong?

In that other macro (where you open the file with “NEW”) have you done some “Write” (of some histograms) in that file at some point ?

This is the macro where I create the root file. It’s identical to the other but here I create the root file that later I pass to the other macro to get one histogram


Double_t PeakFitErf(Double_t *x, Double_t *p){
		return TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
	}


	
	Double_t PeakFit(Double_t *x, Double_t *p){
		return (p[0]* exp( -0.5 * ((x[0]-p[1])/p[2]) * ((x[0]-p[1])/p[2])))+ p[3]*TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
	}


void TryPush()
{
	//TFile * inputfile = new TFile("TryPush.root","READ");
	TFile* inputfile = TFile::Open("TryPush1.root");
	//---list of vector that I use for the program
	vector<Float_t>SBdx;
	vector<Float_t>SBsx;
	vector<Float_t>Count;
	vector<Float_t>Err;
	vector<TH1F*> histos;
	vector<Int_t> Time;
	//-----list of histogram
	TH1F * hh = new TH1F("hh", "hh", 16384, 0,16383);
	TH1F * h1;
	TH1F * hc;//istogramma che poi clono per plottare
	TH1F *hccal;
	//------list of fitting function
	TF1 * fitfondo;
	TF1 *fit;
	//----convertion factors for calibration
	Double_t p=0.2318;
	Double_t q=-0.04483;
	
	int i=0;
	double num1=0;
	ifstream infile;
	char filename[15];
	char histoname[15];
	int Ngroup = 10;//questo è il numero di istogrammi che raggruppa ad  ogni ciclo

	int Nhours = 5;//questo è il numero di ore che voglio
	int Nhistos = Nhours*2;//numero di istogrammi che somma
	
	
		
	for(int j=0; j<Nhistos; j++) {// questo ciclo è quello che prende gli istogrammi da raggruppare
	
		for(int l=0; l<Ngroup; l++) {//questo ciclo con gli if dentro mi apre i file da sommare e li mette tutti insieme a seconda di quanti ne voglio insieme
			sprintf(histoname, "h%d", l+j*Ngroup);
			h1 = new TH1F(histoname,"Quartz",16384,0,16383);
			if(l+j*Ngroup<10) {
				sprintf(filename, "LoAx00%d.txt", l+j*Ngroup);
			} else if(l+j*Ngroup<100){
				sprintf(filename, "LoAx0%d.txt", l+j*Ngroup);
			}else if(l+j*Ngroup<1000){
				sprintf(filename, "LoAx%d.txt", l+j*Ngroup);
			}
			
			cout << filename << endl;
			infile.open(filename);
			//----------questo if riempie l'istogramma
			if(infile.is_open()) {		
				i=0;
				while (1) {
					i+=1;
					infile >> num1;
					h1-> Fill(i,num1);
					if (infile.eof()) break;
				}
				infile.close();
			} else {
				cout << "Failed to open file!" << endl;
			}
			
			hh->Add(h1, +1);
			h1->Reset();
		}		
		hc = (TH1F*)hh->Clone();
		hccal= new TH1F(histoname, "Quartz", 16384,0,(16383*p)+q);
		for(int i=0; i<hc->GetNbinsX(); i++){
				hccal->SetBinContent(i, hc->GetBinContent(i));		
			}
	
		//-------fitting the peak 
		fitfondo = new TF1("fitfondo", PeakFitErf,310.5,313.4,3);
		fit= new TF1("fit",  PeakFit ,310.5,313.4,5);
			fitfondo-> SetParName(1,"offsetshelf");
			fitfondo-> SetParameter(1, 80);
			fitfondo-> SetParName(2,"shiftshelf");
			fitfondo-> SetParameter(2, 388);
	 		fit-> SetParName(0,"normgaus");
			fit-> SetParameter(0,800);
			fit-> SetParName(1,"mean");
			fit-> SetParameter(1,312.);
			fit-> SetParName(2,"sigma");
			fit-> SetParameter(2, 3);
			fit-> SetParName(3,"offsetshelf");
			fit-> SetParameter(3, 80);
			fit-> SetParName(4,"shiftshelf");
			fit-> SetParameter(4, 388);(1, 80);
			hccal-> Fit(fit, "R", "L");
			hccal-> Fit(fitfondo, "R", "L");
			fitfondo-> SetLineColor(kGreen);
			fit-> SetLineColor(kRed);
			fit-> Draw("same");	
			fitfondo-> Draw("same");
		
		Double_t sigma =fit-> GetParameter(2);
		Double_t media=fit-> GetParameter(1);
		Double_t MediaMeno= media-(2*sigma);
		Double_t MediaPiu= media+(2*sigma);
		Double_t conteggi= fit-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
		Double_t error= sqrt(conteggi);
		Double_t conteggifondo= fitfondo-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
		Double_t conteggieffettivi=conteggi-conteggifondo;
		Double_t erroredef= sqrt(conteggieffettivi);
	
		Double_t sideband_sx= hccal-> Integral(hccal->GetBin(282),hccal->GetBin(293));//metti il  umeor del fit che ti interessa
		Double_t sideband_dx= hccal-> Integral(hccal-> GetBin(347), hccal->GetBin(358));
		//------cout useful 
		cout<< "area fit="<<conteggi << endl;
		cout<<"errore area fit="<<error<<endl;
		cout<<"energia picco fit="<<media<<endl;
		cout<<"conteggi del fondo="<<conteggifondo<<endl;
		cout<<"conteggi con fondo sottratto="<<conteggieffettivi<<endl;
		cout<<"errore conteggi finale="<<erroredef<<endl;
		cout<<"sbsx="<<sideband_sx<<endl;
		cout<<"sbdx="<<sideband_dx<<endl;
		//------push back values filling the vectors
		SBdx.push_back(sideband_dx);//sideband destra
		SBsx.push_back(sideband_sx);//side band sinistra
		Count.push_back(conteggieffettivi);//CONTEGGI DEL PICCO
		Err.push_back(erroredef);//errore sul count del picco
		histos.push_back(hccal);
		hh->Reset();
		
	}
	
	TCanvas *c1 = new TCanvas("c1","histogram1",1000,700);
	//TH1F * ht = (TH1F*)inputfile->Get(histos[7]);
	//ht->SetLineColor(1);
	//ht->Draw();
	histos[0]->SetTitle("Quartz etched-measure for 10h");
	histos[0]->GetXaxis()->SetTitle("Energy[KeV]");
	histos[0]->GetYaxis()->SetTitle("Count[a.u]");
	histos[0]->SetLineColor(6);
	histos[0]->Draw("histo");

	
	//------------queste 3 righe mi servono in teoria per.
	//-prendere in considerazione inputfile;
	//-fare il graph e mettere dentro inputfile;
	//-disegnare insieme a histos[0] del file corroso. 
	//inputfile->ls();
	//TGraph* g = (TGraph*) inputfile -> Get("histos[7]");
	//g->Draw("same");
	
	//------cout of values of side bands
	//------ofstream to put in a txt file the results
	
	ofstream outfile("risultati");
	for(i=0; i<10; i++){
	cout<<"Sidebandsx="<<SBsx.at(i)<<endl;
	cout<<"Sidebanddx="<<SBdx.at(i)<<endl;
	cout<<"peak count="<<Count.at(i)<<endl;
	cout<<"errore conteggi="<<Err.at(i)<<endl;
	outfile<< SBsx.at(i)<<"		"<<SBdx.at(i)<<"		"<<Count.at(i)<<endl;
	}
	
	
	
	//-----plotting the data in a TGraph
	TCanvas *c2 =new TCanvas ("C2","SIDEBANDS & COUNT RATE",1000,700);
	
	int n=10;
	int time[n];
	int count[n];
	int sbsx[n];
	int sbdx[n];
	int errorcount[n];
	for(int i=0; i<10; i++){
		time[i]=i+1;
		//cout<<"time="<<time[i]<<endl;
		count[i]=Count.at(i);
		sbsx[i]= SBsx.at(i);
		sbdx[i]= SBdx.at(i);
		errorcount[i]=Err.at(i);
		//-----------tgraph per i sidebands e i count rate
		TGraph * grcount = new TGraph(n, &time[i], &count[i]);
		TGraph * grsbsx = new TGraph(n, &time[i], &sbsx[i]);
		TGraph * grsbdx = new TGraph(n, &time[i], &sbdx[i]);
		grcount->SetTitle("Sidebands and count rate");
		grcount->GetXaxis()->SetTitle("time[h]");
		grcount->GetYaxis()->SetTitle("Count[a.u]");
		grcount->SetLineColor(1);//black
  		grcount->SetLineWidth(2);
  		grcount->SetMarkerColor(4);
   		grcount->SetMarkerSize(1.5);
  		grcount->SetMarkerStyle(21);
  		grsbsx->SetLineColor(4);//blue
   		grsbsx->SetLineWidth(2);
  		grsbsx->SetMarkerColor(5);
  		grsbsx->SetMarkerSize(1.5);
  		grsbsx->SetMarkerStyle(21);
  		grsbdx->SetLineColor(6);//magenta
  		grsbdx->SetLineWidth(2);
   		grsbdx->SetMarkerColor(6);
  		grsbdx->SetMarkerSize(1.5);
  		grsbdx->SetMarkerStyle(21);
		grcount->Draw("AC*");
		grsbsx->Draw("same");
		grsbdx->Draw("same");

	}
	


	return 0;
}

I’m not an expert of root file

You should open the ROOT file with the “NEW” or RECREATE" option to (re)create it. And then do some Write in it. Like in the some example below:

void hsimple()
{
   auto hfile = new TFile("hsimple.root","RECREATE","Demo ROOT file with histograms");

   // Create some histograms, a profile histogram and an ntuple
   auto hpx    = new TH1F("hpx","This is the px distribution",100,-4,4);
   auto hpxpy  = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
   auto hprof  = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
   auto ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");

   // Create a new canvas.
   TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);

   // Fill histograms randomly
   TRandom3 random;
   Float_t px, py, pz;
   const Int_t kUPDATE = 1000;
   for (Int_t i = 0; i < 25000; i++) {
      random.Rannor(px,py);
      pz = px*px + py*py;
      Float_t rnd = random.Rndm(1);
      hpx->Fill(px);
      hpxpy->Fill(px,py);
      hprof->Fill(px,pz);
      ntuple->Fill(px,py,pz,rnd,i);
      if (i && (i%kUPDATE) == 0) {
         if (i == kUPDATE) hpx->Draw();
         c1->Modified();
         c1->Update();
         if (gSystem->ProcessEvents())
            break;
      }
   }

   // Save all objects in this file
   hfile->Write();
}

Then the file is not empty:

$ rootls hsimple.root
hprof  hpx  hpxpy  ntuple

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.