{ TH1 *h1 = new TH1D("h1", "1D Histo", 10,0,10); h1->Fill(4); h1->Fill(1); h1->Fill(1); h1->Fill(5); h1->Fill(8); h1->Draw(); TFile *save = new TFile("savehisto.root","recreate"); //creation of a new .root file to save the histo save->ls(); h1->Write(); save->Close(); TFile *save = new TFile("savehisto.root"); //opens the root file save->ls(); TH1D *h11 = (TH1D*)save->Get("h1"); float bincenter, value; ofstream myfile; myfile.open ("histogramtxt.txt"); myfile << "bincenter value\n"; for (int idx=1; idx<=h11->GetNbinsX(); ++idx) { bincenter = h11->GetBinCenter(idx); value = h11->GetBinContent(idx); cout << bincenter << " " << value << endl; //prints on the screen myfile << bincenter << " " << value << "\n"; //writes to the input file } myfile.close(); }