#include#include #include TFile *file = new TFile("myHistograms.root", "READ"); TH1D *hist = (TH1D*)file->Get("myHistogram"); TCanvas *canvas = new TCanvas(); hist->Draw(); for (int i = 1; i <= hist->GetNbinsX(); i++) { double binContent = hist->GetBinContent(i); cout << "Bin " << i << " content: " << binContent << endl; }
#includeThis code opens a ROOT file containing a one-dimensional histogram named "myHistogram", loops over all bins to calculate the sum of all bin contents, and then calculates the mean value of the histogram. Package/library that these examples use is ROOT.#include TFile *file = new TFile("myHistograms.root", "READ"); TH1D *hist = (TH1D*)file->Get("myHistogram"); double sum = 0; for (int i = 1; i <= hist->GetNbinsX(); i++) { double binContent = hist->GetBinContent(i); sum += binContent; } double mean = sum / hist->GetNbinsX(); cout << "Mean value of histogram: " << mean << endl;