Superimposing histograms

I have this code but its not giving me ABCMC histogram in yellow can anyone help me,
basically i need to pick one histogram from one file and other histogram from other file and want to draw the second one with yellow colour filled bins and on the same canvas want to draw the first histogram .

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;

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

            if (histogramABCMC && histogramABCMC->InheritsFrom(TH1::Class())) {
                std::cout << "Histogram found: " << histName << std::endl;

                // Set fill color and style for histogramABCMC
                histogramABCMC->SetFillStyle(3004); // Pattern style for filling

                // Create and draw the canvas
                TCanvas *canvas = new TCanvas("canvas", "Overlay Histograms", 800, 600);
                canvas->SetFillColor(kWhite); // Set canvas background color


                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


Can you provide us a reproducer? With a simple and standalone example we’ll be able to suggest a solution.


it could be histogramABC has a different range than histogramABCMC. In that case you might not see histogramABC. Plot the two histograms separately 1st to check that. Then if that’s the case (different ranges) use THStack.

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