#include #include #include "TFile.h" #include "TH2D.h" #include "TCanvas.h" #include "TStyle.h" void CreateAndPlot2DHistogram(const char* file1, const char* file2) { // Open the first data file std::ifstream fileStream1(file1); if (!fileStream1.is_open()) { std::cerr << "Error: Unable to open file " << file1 << std::endl; return; } // Open the second data file std::ifstream fileStream2(file2); if (!fileStream2.is_open()) { std::cerr << "Error: Unable to open file " << file2 << std::endl; return; } // Create vectors to store the bin centers and bin contents for each file std::vector binCenters1, binContents1; std::vector binCenters2, binContents2; // Read data from the first file double center1, content1; while (fileStream1 >> center1 >> content1) { binCenters1.push_back(center1); binContents1.push_back(content1); } // Read data from the second file double center2, content2; while (fileStream2 >> center2 >> content2) { binCenters2.push_back(center2); binContents2.push_back(content2); } // Create a TH2D histogram int nbinsX = binCenters1.size(); int nbinsY = binCenters2.size(); TH2D* hist2D = new TH2D("hist2D", "2D Histogram", 100, 0, 5, 100, 0, 5); // Fill the 2D histogram with data from both files for (int i = 0; i < nbinsX; ++i) { for (int j = 0; j < nbinsY; ++j) { hist2D->Fill(binContents2[j],binContents1[1]); } } // Create a ROOT canvas and draw the 2D histogram TCanvas* canvas = new TCanvas("canvas", "2D Histogram Canvas", 800, 600); canvas->SetRightMargin(0.15); // Adjust the right margin for axis labels hist2D->SetTitle("2D Histogram"); hist2D->GetXaxis()->SetTitle("X-axis"); hist2D->GetYaxis()->SetTitle("Y-axis"); hist2D->Draw("COLZ"); // COLZ option for a color-filled 2D histogram // Save the canvas as an image file (optional) canvas->SaveAs("2D_histogram.png"); // Display the canvas canvas->Update(); canvas->Modified(); canvas->Draw(); } int fill2dhisto() { const char* inputFile1 = "1.dat"; const char* inputFile2 = "2.dat"; gStyle->SetOptStat(0); // Disable statistics box CreateAndPlot2DHistogram(inputFile1, inputFile2); return 0; }