How to create a ROOT Histogram from a large file containing two columns of data? I only want to create a histogram from one column of data

This is my code. Please let me know if there is a way to make the histogram without changing the code significantly. Thank you.

{
TFile *f = new TFile("Data.root", "RECREATE");
TNtuple *t = new TNtuple("current_data", "Data from HV", "Unix:Current");
t->ReadFile("NP02_HVCurrent_10-09-2019_11-09-2019");
t->Write();

TH1F *h = new TH1F("Current_Hist", "Current Vs. Events", 100, -5, 5);
h->Fill("Current");
h->Draw();
}

Hi @TheScientist,
and welcome to the ROOT forum. Assuming that ReadFile call works, how about just:

{
  t->ReadFile("NP02_HVCurrent_10-09-2019_11-09-2019");
  TH1F *h = new TH1F("Current_Hist", "Current Vs. Events", 100, -5, 5);
  t->Draw("Current>>Current_Hist"); // draw column "Current" in histogram called "Current_Hist"
}

Cheers,
Enrico

1 Like

It worked. Thank you very much for your quick reply.

I am also trying to do the same thing using TTree instead of TNtuple. Here is my code.

{
	TFile *f = new TFile("data.root", "RECREATE");
	TTree *t = new TTree("current_data", "Data from HV");
	t->ReadFile("current.txt");

	float unix_time, current;
	
	t->Branch("unix_time", &unix_time, "time/F");
	t->Branch("current", &current, "current/F");
  	
	TH1F *h = new TH1F("Current_Hist", "Current Vs. Events", 15, -5, 15);
  	t->Draw("current>>Current_Hist");
}

This was the result. I am messing it up somewhere.

These lines do nothing for you:

TFile *f = new TFile("data.root", "RECREATE");

and

float unix_time, current;
	
t->Branch("unix_time", &unix_time, "time/F");
t->Branch("current", &current, "current/F");

The problem might be that you are creating a new Branch(“current”) or that ReadFile is not reading current.txt correctly. You can check the contents of t with t->Scan(), see the docs.

@eguiraud If I remove these lines, my code will look like this

{
TTree t;
t.ReadFile("NP02_HVCurrent_10-09-2019_11-09-2019.txt");

TH1F *h = new TH1F("h1", "Events Vs Current", 100, -5 , 15);
t.Draw("Current>>h1");

}

I have a file with two columns. One column has time and other column has current data. I am trying to create the histogram of current data. Please let me know the correct way to do that. Thank you.

Yes, what’s missing from that script? It reads the contents of the txt and plots the “Current” variable in histogram h1.

Have you checked the ReadFile call reads the data you expect, e.g. with t.Print() and t.Scan()?

1 Like

@eguiraud I am an absolute beginner so I am having a hard time learning this stuff. This is what I figured out.

This code only reads the first column of the data. So, I changed it to this one.

I was able to create the histogram, however, the title, axis labels and number of bins. None of these parameters are changing with the code.

I don’t know what I am doing wrong here.

Hi,
t.Draw("variable>>histoname") should draw variable in the histogram named histoname. In your case histoname is h1.

Can you post the exact code you are using and possibly also a few lines of the .txt so I can try to reproduce the problem?

Cheers,
Enrico

@eguiraud Here is my code and data. The first column of the data is unix time and the second column is current (microamps). I am trying to create a histogram out of current data. Thank you.

code.C (171 Bytes) NP02_HVCurrent_10-09-2019_11-09-2019.txt (2.3 MB)

Line

TH1F("h1", "Events Vs Current; Current; Events", 5, -5 , 25);

should be

TH1F h1("h1", "Events Vs Current; Current; Events", 5, -5 , 25);

In the first case, the histogram was created and immediately destroyed (there was no corresponding variable), so the TTree::Draw call was filling a new histogram and deciding the binning on its own.

Cheers,
Enrico

1 Like

Thank you very much, Enrico.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.