# Filling Histograms with a text file

**URL:** <https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190>\
**Category:** Newbie\
**Created:** [May 22, 2019, 7:34am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190 "2019-05-22T07:34:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 22, 2019, 8:11am UTC](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190/2 "2019-05-22T08:11:03Z")

</div>

Try:

```auto
/*
  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");
}

```

---

_[View the full topic](https://root-forum.cern.ch/t/filling-histograms-with-a-text-file/34190)._
