Filling Histograms with a text file

Hi, I am new to ROOT and I am trying to create a macro for a 1-D histogram which is filled with a .txt file, but I don’t really know how to do it.
I’ve read some recommendations on other forums on how to do this, but whenever I try I get an empty canvas, meaning that it is not reading the text file nor filling it.

Here is the macro (which is most likely very wrong since I am new to ROOT… pls help)
testalphacount.C (268 Bytes)

Here’s the text file in question (it has about 4000+ entries):
Am241.txt (8.2 KB)

now, I also have a couple questions:
Does it matter where I save the text file for ROOT to read it into the histogram? if so, where should I save it? How do I read the text file on the macro to fill the histogram?

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
  1. In your file you have 4095 lines.
  2. You create your histogram with 100 bins
  3. You do the loop over SetBinContent from 1 to 6000

This means that all the bin numbers above 100 will be outside the histogram range and all the bins between 4095 and 6000 will be the same but in addition the value x will be undefined.

You should first clarify what you want to do exactly. Does one line in the .txt file correspond to one bin of your histogram ?

Hi,

Add a name to the column in your Am241.txt file (e.g. Col0) and try:

root [0] auto tdf = ROOT::RDF::MakeCsvDataFrame("Am241.txt");
root [1] auto h = tdf.Histo1D("Col0");
root [2] h->Draw()

Cheers, Bertrand.

Or even better, without touching your Am241.txt file (thanks @Danilo):

root [0] auto tdf = ROOT::RDF::MakeCsvDataFrame("Am241.txt", false);
root [1] auto h = tdf.Histo1D("Col0");
root [2] h->Draw()

Hi!
Sorry for any bad wording.
Overall I wanted big bins in my histogram (hence why I chose it to have just 100 bins), and now I see what I did wrong on the SetBinContent loop.
Anyhow, if I wanted each line in the .txt to correspond to one bin, what should I do then?
Thank you for your response!

Hi!
I keep getting a blank canvas and an error saying “Error in TGraphPainter::PaintGraph: illegal number of points (0)”

Hi!
I added a name to the column in the .txt file and put your code into a macro, but It gives me this error:

Error in <TRint::HandleTermInput()>: std::runtime_error caught: Unknown column: Col0

OK, so remove the name and try the second method:

auto tdf = ROOT::RDF::MakeCsvDataFrame("Am241.txt", false);
auto h = tdf.Histo1D("Col0");
h->Draw();

your macro should be:

{
  auto am241=new TH1F("am241",
           "Count Rate;N_{counts};# occurencies",
           4095,
           -0.5,
           6000);
  ifstream inp; double x;
  inp.open("Am241.txt");
  for (int i=1; i<=4095; i++) {
    inp >> x;
    am241->SetBinContent(i,x);
  }
  am241->Draw();
}

And you will get:

1 Like

I left 6000 as maximum value for the X axis. It was intentional to show that the number of bins and the axis values are decoupled. In that particular case it might be more logical to replace 6000 by 4095… that’s up to you to decide.

1 Like

Also, a log scale along the Y axis might be something to consider:

1 Like

Hi Couet!
Thank you for your input!
However, I’m still having issues with reading the text file.
Here’s the code to my macro:

{
  auto am241=new TH1F("am241",
		     "am241;N_{counts};# occurencies",
		      4095, //number of bins
		      -0.5, //x-axis min
		      6000); //x-axis max
  ifstream inp; double x;
  inp.open("Am241.txt");
  for (int i=1; i<4095; i++) {
    inp >> x;
    am241->SetBinContent(i,x);
  }
  c1 = new TCanvas("c1","Bin Size", 1200,400);
  c1->cd(1);
  am241->Draw();
  
  
}

and the histogram gets drawn, however it is still empty:

My text file is located in the same directory as my macros, but its not reading it :frowning_face:


Is there a way to call the file or something of sorts?

Thank you for everything else!

When I run your macro I get this plot:


How do you run the macro ?
Do you have some messages when you it ?
I have the text file Am241.txt and the macro testalphacount.C in the same folder. I start root at the shell prompt from that folder and I do:

$ root
   ------------------------------------------------------------
  | Welcome to ROOT 6.17/01                  https://root.cern |
  |                               (c) 1995-2019, The ROOT Team |
  | Built for macosx64 on May 22 2019, 06:34:58                |
  | From heads/master@v6-16-00-rc1-2164-g789b8e6cd5            |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] .x testalphacount.C
root [1] 

I usually just open the terminal and open root from the predetermined prompt shell, then I execute the file just how you do it, but I don’t get any messages:

I’ve also just checked from where I’m opening root and I get this:

Now, when I try to open root from the macros folder, it just boots root from the same directory as before:

Screenshot%20from%202019-05-23%2004-33-30

Could it be the way I built root?
should I uninstall and then rebuild it?

The “Am241.txt” file must be in the same subdirectory in which you start “root”.

1 Like

In /home/jorge (where you are starting root) if you do:

$ ls testalphacount.C Am241.txt 

you should get:

Am241.txt		testalphacount.C
1 Like

I moved the file to the home directory and it works now!
However, is there a better way to do this? since I don’t want to flood my home directory with text files for root to read it.

@couet @Wile_E_Coyote

Thank you for your responses!