#include #include #include #include void fill_TH2D() { // Set style options gStyle->SetLineScalePS(1); gStyle->SetOptStat(0); // Disable statistics box gStyle->SetPalette(kBird); // Set color palette gStyle->SetNumberContours(999); // Increase number of color contours gStyle->SetPadTopMargin(0.07); gStyle->SetPadBottomMargin(0.09); gStyle->SetPadLeftMargin(0.07); gStyle->SetPadRightMargin(0.13); // Create a 3x3 canvas with 2000x1200 resolution TCanvas *c1 = new TCanvas("c1", "3x3 Canvas with Histograms", 2000, 1200); c1->Divide(3, 3); // Random number generator TRandom rand; // Function to fill a single histogram auto fill_histogram = [&](TH2D *h2) { // Fill all bins along Y-axis for X in [-20, 20] for (int i = 0; i < 50000; i++) { double x = rand.Uniform(-20, 20); // Uniform distribution for X in [-20, 20] double y = rand.Gaus(50, 15); // Gaussian for Y h2->Fill(x, y); } // Fill odd bins along Y-axis for X outside [-20, 20] for (int i = 0; i < 50000; i++) { // Fill the left X range [-40, -20) double x1 = -40 + rand.Landau(0, 5); if (x1 >= -40 && x1 < -20) { double y1 = rand.Gaus(50, 15); int ybin = h2->GetYaxis()->FindBin(y1); if (ybin % 2 != 0) { // Check if Y bin is odd h2->Fill(x1, y1); } } // Fill the right X range (20, 40] double x2 = 40 - rand.Landau(0, 5); if (x2 > 20 && x2 <= 40) { double y2 = rand.Gaus(50, 15); int ybin = h2->GetYaxis()->FindBin(y2); if (ybin % 2 != 0) { // Check if Y bin is odd h2->Fill(x2, y2); } } } }; // Loop through the subpads and create histograms for (int i = 1; i <= 9; i++) { c1->cd(i); // Create a histogram for the subpad TH2D *h2 = new TH2D( Form("h2_%d", i), Form("Subpad %d Histogram;X-axis;Y-axis;Z-axis", i), 80, -40, 40, 72, 0, 72); // Fill the histogram fill_histogram(h2); h2->GetYaxis()->SetTitleOffset(-1.0); h2->GetXaxis()->SetTitleOffset(-1.0); h2->GetXaxis()->SetLabelOffset(-0.05); h2->GetYaxis()->SetLabelOffset(-0.01); h2->GetXaxis()->SetTickSize(0.018); h2->GetYaxis()->SetTickSize(0.010); h2->GetXaxis()->SetTicks("-"); h2->GetYaxis()->SetTicks("+"); h2->Draw("COLZ"); } // Save the canvas as PDF and PNG c1->SaveAs("th2d_histogram.pdf"); c1->SaveAs("th2d_histogram.png"); // Clean up delete c1; }