Superimposing two histograms on the same graph with the same scale only drawing one histogram

I have the following code, designed to superimpose a histogram generated with calculated data from a text file (hist1) over a histogram created with data taken from a .root file with a TTree (hist2).

/* 
Program in C to analye and compare the gbterr and good gbt entries for a given run
Written by Joseph Farah on July 5, 2017
Last updated by [Joseph Farah] on: [July 6, 2017]

Notes
- 
*/
// header imports
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <typeinfo>
#include <algorithm>
#include <chrono>
#include <string>
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include "TSystem.h"
#include "TMath.h"
#include "TH1D.h"
#include "TRandom3.h"
#include "TRandom.h"
#include "TCanvas.h"
#include "TApplication.h"



std::vector<std::string> explode(const std::string& str, const char& ch) {
	std::string next;
	std::vector<std::string> result;

	// For each character in the string
	for (std::string::const_iterator it = str.begin(); it != str.end(); it++) {
		// If we've hit the terminal character
		if (*it == ch) {
			// If we have some characters accumulated
			if (!next.empty()) {
				// Add them to the result vector
				result.push_back(next);
				next.clear();
			}
		} else {
			// Accumulate the next character into the sequence
			next += *it;
		}
	}
	if (!next.empty())
		result.push_back(next);
	return result;
}

int convert_epoch_to_date(double given_date)
{
	std::cout << "Test" << std::endl;
}

void progress(double time_diff, int nprocessed, int ntotal){
	double rate = (double)(nprocessed+1)/time_diff;
	std::cout.precision(1);
	std::cout << "\r > " << nprocessed << " / " << ntotal 
	<< " | "   << std::fixed << 100*(double)(nprocessed)/(double)(ntotal) << "%"
	<< " | "   << std::fixed << rate << "Hz"
	<< " | "   << std::fixed << time_diff/60 << "m elapsed"
	<< " | "   << std::fixed << (double)(ntotal-nprocessed)/(rate*60) << "m remaining"
	<< std::flush;
	std::cout.precision(6);
}




int main()
{   
	std::chrono::time_point<std::chrono::system_clock> time_start;
	std::chrono::duration<double> elapsed_seconds;
	time_start = std::chrono::system_clock::now();
	// Define a new canvas
   TApplication app("app", nullptr, nullptr);
   TCanvas *canvas = new TCanvas("c1", "testing");
   canvas->SetLogy();
	//change canvas height
   canvas->SetWindowSize(900, 600);

   int bin_length = 0.04;
   const int NUM_OF_CRAP = 1979885;
   int i;
   TH1 *hist1 = new TH1D("hist1", "GBTerr Histogram (non combined)", 240, 170,180);
   TH1 *hist2 = new TH1D("hist2", "GBT total histogram", 240, 170,180);
   canvas->Divide(1,1);

   std::vector<std::string> exploded_line;
   std::string line;
   std::ifstream gbterr_file;
   gbterr_file.open("gbterr.txt");
   int line_num = 0;
   double x_val, y_val, hr;
	// creation of the first histogram
   if(gbterr_file.is_open())
   {
   	while(getline(gbterr_file, line))
		{   // get a whole line
			line_num++;
			// if(line_num>=10000) { break; }
			std::stringstream ss(line);
			line.erase(0,1);
			line.erase(line.size()-1);
			exploded_line = explode(line, ',');
			time_t rawtime = stol(exploded_line[2])/(pow(10,9));
			struct tm date; 
			date = *localtime( &rawtime );
			char day[256];
			strftime (day,sizeof(day),"%j",&date);
			char hour[256];
			strftime (hour,sizeof(hour),"%H",&date);
			hr = std::stod(hour)/24.0;
			x_val = std::stod(day)+hr;
			y_val = std::stod(exploded_line[1]);
			hist1->Fill(x_val,y_val);
			if(line_num%1000==0) {    
				elapsed_seconds = (std::chrono::system_clock::now() - time_start);
				progress(elapsed_seconds.count(), line_num, 1979885);
				// std::cout << x_val << std::endl;
			}
		}
	}
	time_start = std::chrono::system_clock::now();
	// create new fileobject for GBT
	TFile *gbt_file_object = new TFile("Run3526_GBT_decode.root");
	// pull the tree from the file
	TTree *gbt_tree = (TTree*)gbt_file_object->Get("GBT_data");
	if (!gbt_tree){
		std::cout << "Error: GBT tree is a null pointer!" << std::endl;
		return 0;
	}

	// get the addresses of all the branches in the GBT and TPFit trees
	// gbt tree address declaration
	int                 gbtTime_sec;
	int                 gbtTime_nsec;
	int                 gEventNum;
	std::vector<int>    *gbtMMFE8 = 0;
	std::vector<int>    *gbt_VMM = 0;
	std::vector<int>    *gbt_CH = 0;
	std::vector<int>    *gbt_BCID = 0;

	// List of branches which may be unnecessary but whatever
	TBranch        *b_EventNum = 0;   
	TBranch        *b_Time_sec = 0; 
	TBranch        *b_Time_nsec = 0;   
	TBranch        *b_gbt_VMM = 0;   
	TBranch        *b_gbt_CH = 0;   
	TBranch        *b_gbt_MMFE8 = 0;   
	TBranch        *b_gbt_BCID = 0;   

	// initialize all the branches using the pointers just created
	gbt_tree->SetBranchAddress("EventNum", &gEventNum, &b_EventNum);
	gbt_tree->SetBranchAddress("Time_sec", &gbtTime_sec, &b_Time_sec);
	gbt_tree->SetBranchAddress("Time_nsec", &gbtTime_nsec, &b_Time_nsec);
	gbt_tree->SetBranchAddress("gbt_VMM", &gbt_VMM, &b_gbt_VMM);
	gbt_tree->SetBranchAddress("gbt_CH", &gbt_CH, &b_gbt_CH);
	gbt_tree->SetBranchAddress("gbt_MMFE8", &gbtMMFE8, &b_gbt_MMFE8);
	gbt_tree->SetBranchAddress("gbt_BCID", &gbt_BCID, &b_gbt_BCID);

	int counter = 0;
	long gbttime;

	while(gbt_tree->GetEntry(counter++))
	{
		gbttime = gbtTime_sec + gbtTime_nsec/pow(10.,9);
		// std::cout << gbttime << std::endl;
		time_t rawtime = gbtTime_sec;
		struct tm date; 
		date = *localtime( &rawtime );
		char day[256];
		strftime (day,sizeof(day),"%j",&date);
		char hour[256];
		strftime (hour,sizeof(hour),"%H",&date);
		hr = std::stod(hour)/24.0;
		x_val = std::stod(day)+hr;
		y_val = gEventNum;
		hist2->Fill(x_val, y_val);
		if(counter%1000==0) {    
			elapsed_seconds = (std::chrono::system_clock::now() - time_start);
			progress(elapsed_seconds.count(), counter, gbt_tree->GetEntries());
				// std::cout << x_val << std::endl;
		}
	}



	hist2->Draw("hist");
	canvas->Update();
	hist1->Draw("same hist");
	hist1->SetFillColor(kRed);
	app.Run();
	return 0;
}

The portion I think is relevant (but I’m not sure) is this part:

hist2->Draw("hist");
canvas->Update();
hist1->Draw("same hist");
hist1->SetFillColor(kRed);
app.Run();

Instead of superimposing two histograms, however, the code only draws hist2.

Any help would be greatly appreciated!

And what do you get when you plot them in two different canvases (or two different pads)?
The obvious reason for your problem might be that the “Y-axis” ranges needed by the two histograms are significantly different (and then “hist1” will not be visible on the “Y-axis” which was created by “hist2”).

THStack might help.
Also to you see hist1 if you turn on log scale along Y ?

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