#include "Riostream.h" void Ascii2root(const char *fname = "eff_6.dat") { if (!(fname && fname[0])) return; // just a precaution auto a = TFile::Open("eff_6.root","RECREATE"); TGraph *g = new TGraph(fname); Int_t n = g->GetN(); // get the no. of points if (n < 2) { delete g; return; } // just a precaution // g->Sort(); // just a precaution 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 *h = new TH1D("h", "spectrum;x-value(Channels);#counts", n, x); delete [] x; // no longer needed h->FillN(n, g->GetX(), g->GetY()); delete g; // no longer needed h->Sumw2(kFALSE); // make sure "bin_error = sqrt(bin_content)" //h->ResetStats(); // reset the statistics including the number of entries //h->Print("All"); //h->Draw(); h->Write(); a->Close(); delete a; }