#include "TH1.h" #include "TF1.h" #include "Riostream.h" void hist() { ifstream in; in.open("eu_gu_1.txt"); Int_t i, npeaks = 5; auto f = TFile::Open("hist.root","RECREATE"); TCanvas *c1 = new TCanvas("c1","c1"); TGraph *g = new TGraph("eu_gu_1.txt"); // g->Sort(); // just a precaution Int_t n = g->GetN();//get the no. of points on the TGraph *g Double_t source[n]; Double_t *x = new Double_t[(n + 1)]; x[0] = (3.0 * g->GetX()[0] - g->GetX()[1]) / 2.0; x[n] = (3.0 * g->GetX()[(n - 1)] - g->GetX()[(n - 2)]) / 2.0; for (Int_t i = 1; i < n; i++) { x[i] = (g->GetX()[(i - 1)] + g->GetX()[i]) / 2.0; } TH1D *h1 = new TH1D("h1", "spectrum;x-value(Channels);#counts", n, x); h1->FillN(n, g->GetX(), g->GetY()); h1->Sumw2(kFALSE); // restore proper errors delete [] x; // no longer needed delete g; // no longer needed h1->Draw(); in.close(); f->Write(); }