#include #include #include "TFile.h" #include "TH1F.h" void convert_to_text() { // Open the ROOT file TFile* file = TFile::Open("test.root"); // Check if the file is open if (!file) { std::cerr << "Error: failed to open the ROOT file." << std::endl; return 1; } // Get the histogram from the ROOT file TH1F* hist = (TH1F*)file->Get("hprt"); // Check if the histogram exists if (!hist) { std::cerr << "Error: failed to get the histogram from the ROOT file." << std::endl; file->Close(); return 1; } // Open the output text file std::ofstream outFile("test.dat"); // Check if the output file is open if (!outFile) { std::cerr << "Error: failed to open the output file." << std::endl; file->Close(); return 1; } // Write the bin edges and bin contents to the output file for (int i = 1; i <= hist->GetNbinsX(); i++) { // outFile << hist->GetBinCenter(i) << "\t" << hist->GetBinContent(i) << std::endl; outFile << hist->GetBinContent(i) << std::endl; } // Close the files outFile.close(); file->Close(); return 0; }