I have a .dat file, it is separated in 2 columns and about 20 000 rows.
I want to open this file somehow and plot a histogram for one of the columns.
I have looked at these two threads:
and tried this code but it does not work for me.
I understand how to draw a histogram based off of things that I have calculated (example: write a code to calculate the energy then make a histogram of this energy)
I just don’t know what command to use to read my .dat file and make the histogram
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)
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:
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();
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();
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");
// ...
these lines have been tested on my machine and I am sure they work. I think we are missing something fundamental Can you share in a post the code which leads to the errors you are reporting? I am sure we can solve those quickly.
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();