Creating histograms from simulations


I am trying to create a histogram using data from a simulation. I have written some code to create, fill and draw the histogram. However, on loading my code in root, it produces an empty canvas and then will not draw the histogram when running the code. I also know that the histograms are being filled with non-zero values because I can output them to the terminal. How do I rectify the problem of the empty canvas appearing when the code is loaded? I’ve attached the code below.


TH2F* Kstar_mass_against_B0_ThetaK = new TH2F (“Kstar_mass_against_B0_ThetaK”,“Kstar mass against B0 cos(ThetaK)”,100, 500, 2000, 100, -1., 1.);

if (fChain == 0) return;

Long64_t nentries = fChain->GetEntriesFast();

Long64_t nbytes = 0, nb = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
// if (Cut(ientry) < 0) continue;

// Filling histograms

double costheta = cos(B0_ThetaK);

Kstar_mass_against_B0_ThetaK -> Fill(Kstar_M, cos(B0_ThetaK));
// Drawing histograms

TCanvas* c_Kstar_mass_against_B0_ThetaK = new TCanvas();
Kstar_mass_against_B0_ThetaK -> Draw(“COLZ”);

I can’t see any problems in the source code that you show.
There’s one thing you can try … right after you create your histogram …
Kstar_mass_against_B0_ThetaK->SetDirectory(gROOT); // just a precaution

BTW: set the “upper limit” for your “cos” histogram axis (“Y”) to be 1.00001 (slightly higher than 1).

Thanks for your advice but the problem still persists. I just get a blank canvas when I load and then the code does not produce anything else…

Maybe you could provide a small script which reproduces the problem (plus a small data file to try).