When I ask to draw a Histogram, the Canvas is blank

Goodmorning everyone,
I’m trying to use the RDataFrame function and one of the first exercises it does is to draw a Histogram.
Here is the code used:

void B()
{
   ROOT::EnableImplicitMT();
   ROOT::RDataFrame d("h1","Data.root",{"nt"});
 
   auto entries1 = d.Filter("nkg==1&&nt==3").Count();
   std::cout << *entries1 << " entries passed all filters" << std::endl;

   auto hist = d.Filter("nkg==1&&nt==3").Histo1D();
   std::cout << "Filled h " << hist->GetEntries() << " times, mean: " << hist->GetMean() << std::endl;
   hist->Draw();
}

when I run it, what it returns me is an empty Canvas.
I guess the error is silly, as if I type the same commands from the terminal, the histogram appears.

I am using ROOT version 6.22.02.

Thanks in advance.

Hi,
see e.g. I draw a canvas with no picture, classic ROOT lifetimes problems.

Cheers,
Enrico

1 Like

Thanks @eguiraud.

My difficulty, however, is to go and redefine the TH1D using the command:

TH1D* hist = new TH1D("hist","histogram",bin,min,max); 

I’ll show you the simplest example of the construct.

ROOT::EnableImplicitMT(); // Tell ROOT you want to go parallel
ROOT::RDataFrame d("myTree", "file_*.root"); // Interface to TTree and TChain
auto myHisto = d.Histo1D("Branch_A"); // This happens in parallel!
myHisto->Draw();

The histogram is defined on the third line.
I tried to redefine it and then put it equal to “d.Histo1D (” Branch_A “);” but it does not work.
How would you do it?

Sounds like a separate issue?

About the blank canvas, the problem is that the histogram goes out of scope and “dies” before the canvas is displayed, you can use DrawClone() instead of Draw() to draw a copy of the histogram that ROOT will own, or keep myHisto around in some other way.

About specifying name, title and binning for the histogram, take a look at the RDF tutorials, you can do it so: Histo1D({"hist", "histogram", bin, min, max}, "Branch_A").

Cheers,
Enrico

2 Likes

Thanks Enrico.
I thought the problem was in the definition of the histogram.
Replacing with “DrawClone ()” instead of “Draw ()” solved the problem.

You were very fast!
Good day!

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