#include#include #include int main() { const int n_bins = 20; TH1D hist("my_hist", "Example TH1D", n_bins, 0, 100); for (int i = 0; i < 1000; ++i) { double x = std::sqrt(i); hist.Fill(x); } std::cout << "Number of entries: " << hist.GetEntries() << std::endl; return 0; }
#includeIn this example, we create a TH1D object like example 1 and fill it with data. Then, we create a TFile object named "output.root" in write mode ("RECREATE"). We save the histogram object to this file using the Write() method and then close the file. Package library - The ROOT package library#include int main() { TH1D hist("my_hist", "Example TH1D", 20, 0, 100); // Fill histogram here TFile file("output.root", "RECREATE"); hist.Write(); // Save the histogram to file file.Close(); return 0; }