How to simply make a histogram from .dat file

Which code then? For me, root just kept “crashing” and nothing would run.

I also want to know how to choose certain elements to plot.

Let’s say I would want to plot only the numbers that are with 1, how would I do it ?

If you’re talking about this
auto tree = TTree::ReadFile(events.dat,“x:y:z”);
tree->Draw(“x”);
I guess I don’t know how to write the file name in a way that works, since it doesn’t accept it like this. Also don’t know what “draw(x)” will be drawing (first column, etc)

or if I need to change x,y,z

TTree *t = new TTree("t", "my events");
t->ReadFile("events.dat", "first/I:second/D");
t->Print();
t->Draw("first");

ah, I see. Thank you.

I have another question, if I wanted to select to plot only the energies corresponding to 1, or to 2 etc how would I arrange this ?

t->Draw("second", "first == 1");

Hi,

this is the modern way to achieve this:

auto df = ROOT::Experimental::TDF::MakeCsvDataFrame("data.txt", false, ' ');
auto h = df.Histo1D("Col1")
h->Draw();

It has plenty of advantages, besides being the suggested way to code:

  1. You can easily convert your dataset to ROOT:
auto df = ROOT::Experimental::TDF::MakeCsvDataFrame("data.txt", false, ' ');
df.Snapshot("data", "data.root"); // <- one line is enough
auto h = df.Histo1D("Col1")
h->Draw();
  1. You can run using all the cores of your machine (not mutually exclusive with 1.)
ROOT::EnableImplicitMT(); // <- just adding this line is enough
auto df = ROOT::Experimental::TDF::MakeCsvDataFrame("data.txt", false, ' ');
df.Snapshot("data", "data.root");
auto h = df.Histo1D("Col1")
h->Draw();
  1. You can create several histograms in the same event loop, therewith limiting the disk access:
ROOT::EnableImplicitMT(); // <- just adding this line is enough
auto df = ROOT::Experimental::TDF::MakeCsvDataFrame("data.txt", false, ' ');
df.Snapshot("data", "data.root");
auto h = df.Histo1D("Col1");
auto h1 = df.Histo1D("Col0");
auto hWeighted = df.Histo1D("Col1", "Col0");
auto h2 = df.Histo2D("Col0", "Col1");
// ...

See all different options in these examples.

Best,
D

These are both very helpful.
One last thing, if I wanted to make a 2D TH2F distribution, how would I proceed ?

Hi,

it’s in my point 2):

auto h2 = df.Histo2D("Col0", "Col1");

Cheers,
D

When I run it for the 1D, I simply get a blank canvas. When I run with this line for the 2D, I get many errors

error: no matching member function for call to ‘Histo2D’

among other things.

Sorry if these questions are basic, I am very new to this

Hi,

these lines have been tested on my machine and I am sure they work. I think we are missing something fundamental :slight_smile: Can you share in a post the code which leads to the errors you are reporting? I am sure we can solve those quickly.

Cheers,
D

void two_d()
{
	auto df = ROOT::Experimental::TDF::MakeCsvDataFrame("events.dat", false, ' ');
	auto h2 = df.Histo2D("Col0", "Col1");
	h2->Draw();
    
}

I really don’t know what to add or to write

Hi,

can you try:

      auto df = ROOT::Experimental::TDF::MakeCsvDataFrame( "data.txt", false, ' ');
      auto h = df.Histo2D({"myHisto", "My Title", 5, 0.5, 5.5, 256, -400, 400}, "Col0", "Col1");
      h->Draw();

Cheers,
Danilo

This is what I get

Processing Histo2D.C…
terminate called after throwing an instance of ‘std::runtime_error’
what(): Unknown columns: Col0,Col1

Now it runs, but it only produces a blank canvas c1

Ok.
This is a well known behaviour independent of data frame. You need to make sure the object survives after the end of the scope. For example:

     auto df = ROOT::Experimental::TDF::MakeCsvDataFrame( "data.txt", false, ' ');
      auto h = df.Histo2D({"myHisto", "My Title", 5, 0.5, 5.5, 256, -400, 400}, "Col0", "Col1");
      auto mycanvas = new TCanvas();
      h->DrawCopy();

This works, but it produces a result not like I had expected. Glad to see it working though. Thanks for your help !

Hi,

what were your expectations?

Best,
D

I expected something that would look more like a histogram, this is just a lot of dots. But it’s okay, I was probably just imagining it wrong

h->DrawCopy("COLZ"); // "COLZ" or "LEGO2Z" or ...

Hi,

here you can find all the options you can use to draw your histograms: https://root.cern.ch/doc/master/classTHistPainter.html

Best,
D