Filling Histograms with a text file

Try:

/*
  Usage examples:
  root [0] .L Am241.cxx
  root [1] Am241(); // expects "Am241.txt" in the current directory
  root [1] Am241("/full/path/to/some/Am241.txt");
  or simply:
  root [0] .x Am241.cxx
  root [0] .x Am241.cxx("/full/path/to/some/Am241.txt")
*/
#include "TGraph.h"
#include <fstream>
void Am241(const char *file = "Am241.txt") {
  if ((!file) || (!file[0])) return; // just a precaution
  TGraph *g = new TGraph();
  g->SetTitle("Count Rate;N_{counts};# occurencies");
  double y;
  ifstream inp(file);
  while (inp >> y) g->SetPoint(g->GetN(), g->GetN(), y); // note: x = g->GetN()
  g->Draw("AL");
}
1 Like