#include "TFile.h"
#include "TH1F.h"
#include "TCanvas.h"
#include "TKey.h"
#include <iostream>
#include "TLegend.h"
#include "TPaveStats.h"
#include "TStyle.h"
void DrawOverlayHistograms(const char* file1, const char* file2) {
// Open the ROOT files containing the histograms
TFile *fileABC = TFile::Open(file1);
TFile *fileABCMC = TFile::Open(file2);
if (!fileABC || !fileABCMC) {
std::cerr << "Error opening input files!" << std::endl;
return;
}
TList *keysABC = fileABC->GetListOfKeys();
TIter nextABC(keysABC);
TKey *keyABC;
// Loop through the histograms in file1
while ((keyABC = (TKey*)nextABC())) {
TObject *objABC = keyABC->ReadObj();
if (objABC->IsA()->InheritsFrom(TH1::Class())) {
TH1F *histogramABC = (TH1F*)objABC;
const char* histName = histogramABC->GetName();
// Get the corresponding histogram from file2
TH1F *histogramABCMC = (TH1F*)fileABCMC->Get(Form("%s_mc", histName));
const char* histName2 = histogramABCMC->GetName();
if (histogramABCMC && histogramABCMC->InheritsFrom(TH1::Class())) {
std::cout << "Histogram found: " << histName << std::endl;
histogramABCMC->Scale(histogramABC->GetEntries() / histogramABCMC->GetEntries());
// Set fill color and style for histogramABCMC
const char* title = Form("%s vs %s", histName,histName2);
histogramABCMC->SetTitle(title);
histogramABC->SetTitle("");
histogramABCMC->SetFillColor(kYellow);
histogramABCMC->SetFillStyle(3001); // Pattern style for filling
histogramABC->SetTitle("");
histogramABC->SetMarkerColor(kRed); // Set marker color to red
histogramABC->SetMarkerStyle(20); // Define the marker style (adjust as needed)
histogramABC->SetMarkerSize(1.2);
const char* xAxisLabel = Form("%s -->", histName);
histogramABCMC->GetXaxis()->SetTitle(xAxisLabel);
histogramABCMC->GetYaxis()->SetTitle("events -->");
histogramABCMC->GetXaxis()->CenterTitle();
histogramABCMC->GetYaxis()->CenterTitle();
TPad* pad;
// Create and draw the canvas
TCanvas *canvas = new TCanvas("canvas", "Overlay Histograms", 800, 600);
TPad *upperPad = new TPad("upperPad", "Upper Pad", 0, 0.3, 1, 1);
upperPad->SetBottomMargin(0.2); // Upper and lower plot are joined
upperPad->SetGridx(); // Vertical grid
upperPad->SetBottomMargin(0); // Set the bottom margin of the upper pad to zero
//upperPad->SetBottomMargin(0); // Set the bottom margin of the upper pad to zero
upperPad->SetTopMargin(0.1); // Adjust the top margin of t
upperPad->SetGridx();
upperPad->Draw();
upperPad->cd();
canvas->SetFillColor(kWhite); // Set canvas background color
gStyle->SetOptStat("nemr");
pad = (TPad*)canvas->cd();
histogramABCMC->Draw("HIST");
gPad->Update();
auto stat = dynamic_cast<TPaveStats*>(histogramABCMC->FindObject("stats"));
if (stat) {
std::cout << " X1NDC: " << stat->GetX1NDC() << " X2NDC: " << stat->GetX2NDC() << std::endl;
stat->SetX1NDC(0.8); stat->SetX2NDC(.95);
stat->SetY1NDC(0.6); stat->SetY2NDC(0.75);
stat->Draw();
}
// histogramABCMC->Draw("HIST");
histogramABC->Draw("SAMES");
auto legend = new TLegend(0.79, 0.5, 0.92, 0.6);
legend->SetHeader("MC vs REAL","C"); // option "C" allows to center the header
legend->AddEntry(histogramABCMC ,"MC_Histogram","f");
legend->AddEntry(histogramABC,"Data_Histogram");
legend->Draw();
canvas->cd();
TPad *lowerPad = new TPad("lowerPad", "Lower Pad", 0, 0, 1, 0.3);
lowerPad->SetTopMargin(0);
lowerPad->SetBottomMargin(0.2);
lowerPad->SetGridx(); // Vertical grid
lowerPad->SetTopMargin(0); // Set the top margin of the lower pad to zero
//lowerPad->SetBottomMargin(0.2);
lowerPad->Draw();
lowerPad->cd();
// Calculate and draw the ratio plot
TH1F *ratioHistogram = (TH1F*)histogramABC->Clone();
ratioHistogram->Divide(histogramABCMC);
ratioHistogram->GetXaxis()->SetLabelSize(0.08); // Adjust the value as needed
// Increase the font size of the Y axis
ratioHistogram->GetYaxis()->SetLabelSize(0.08);
ratioHistogram->SetMarkerStyle(20);
ratioHistogram->Draw("ep");
// Adjust the TPaveStats size and font for the lower pad
gPad->Update();
auto statLower = dynamic_cast<TPaveStats*>(ratioHistogram->FindObject("stats"));
if (statLower) {
statLower->SetX1NDC(0.8); // Adjust the X1NDC to move the stats box to the left
statLower->SetX2NDC(0.9); // Adjust the X2NDC to make the stats box wider
statLower->SetTextSize(0.06); // Increase the text size within the stats box
// statLower->SetTextFont(43); // Set the font (43 is Times New Roman, you can change it)
statLower->SetY2NDC(1);
statLower->SetY1NDC(.5);
statLower->Draw();
}
canvas->SaveAs(Form("Overlay_%s.png", histName)); // Save canvas as an image
delete canvas;
} else {
std::cerr << "Histogram not found: " << histName << std::endl;
}
}
}
// Close the input files
fileABC->Close();
fileABCMC->Close();
}
int main() {
const char* file1 = "Data_Run2022.root"; // Path to the first ROOT file
const char* file2 = "MC_Run2022.root"; // Path to the second ROOT file
DrawOverlayHistograms(file1, file2);
return 0;
}
here my lower pad is actually overlaping the top pad content , could anyone please check where is the mistake