JSROOT Canvas Rendered is Empty when drawing hisogram with weights

Hello All,

I am drawing a histogram with weights and everything looks ok on my local root (without using JSROOT)

ntuples = (TTree *)gDirectory->Get("ntuples");
m = ntuples->Draw("x:y:z:w>>hist","","col");
TH3F *hist = (TH3F *)gDirectory->Get("hist");
hist->SetTitle("Position;Z;X;Y");
hist->SetTitleSize(0.2, "X;Y;Z");
hist->SetStats(0);
TCanvas *c = new TCanvas("c","A Simple Graph Example with Text",700,500);
hist->Draw();
c->SaveAs("Canvas.root");

Then I try to open and display it using JSROOT and the plot is empty. All I see are the axes, but none of the points.

The code I have for JSROOT html is:

   <script type='module'>
      import { openFile, draw } from '/jsroot/modules/main.mjs';
      let file = await openFile('./Canvas.root');
      let obj = await file.readObject('c;1');
      draw('drawing', obj,"","colz");
      // Also tried draw('drawing', obj);
   </script>

Why am I getting an empty canvas?
Thanks in advance.


ROOT Version: ROOT 6.28/06
Platform: Not Provided
Compiler: Not Provided


Hi,

Which JSROOT version you are using?
Can you check output of browser console? You can activate it with Ctrl + Shift + F11.
And as last - can you provide your ROOT file?

Regards,
Sergey

This is my version of jsroot

$ git log -n1
commit 7fe50757a27cb863bc07a94e47685f2346b2c061 (HEAD -> master, origin/master, origin/dev, origin/HEAD)
Author: Sergey Linev <S.Linev@gsi.de>
Date:   Fri Oct 6 10:52:13 2023 +0200

Also I found what was causing the problem (but don’t know how to fix it)

TFile f("test.root");
ntuples->Draw("x:y:z:w>>PlotHist");
c1->SaveAs("c1.root");  // This works with JSROOT (I see axes and data)

// SETTING TITLES MAKES THE CANVAS EMPTY
auto c2 = new TCanvas("c2", "Detection Position", 600, 600);
PlotHist->SetTitle("Position; Z;Y;X");
PlotHist->SetStats(0);
c2->cd();
PlotHist->Draw();
c2->SaveAs("c2.root"); // This does not render completely. Just the axes is visible

I need either test.root or c2.root file.

Hello Sergey,

Creation of c1 and c2 files:

TTree *t = new TTree("t", "testdata.csv");
t->ReadFile("testdata.csv", "x:y:z:w");
t->Draw("x : y : z : w>>hist");
c1->SaveAs("c1.root")
auto c2 = new TCanvas("c2", "Detection Position", 600, 600);
hist->SetTitle("Position; Z;Y;X");
hist->SetStats(0);
c2->cd();
hist->Draw();
c2->SaveAs("c2.root");

plotting c1: this works

<script type='module'>
   import { openFile, draw } from '/html/jsroot/modules/main.mjs';
   let file = await openFile('./c1.root');
   let obj = await file.readObject('c1;1');
   draw('drawing', obj,"","colz");
</script>

plotting c2: this does not work. ie shows only axes

<script type='module'>
   import { openFile, draw } from '/html/jsroot/modules/main.mjs';
   let file = await openFile('./c2.root');
   let obj = await file.readObject('c2;1');
   draw('drawing', obj,"","colz");
</script>

c2.root (14.3 KB)
c1.root (15.7 KB)
test.root (15.1 KB)

Thanks.

Hi,

Drawings in test.root and c1.root are not empty - see test.root and c1.root links.
Maybe marker size is too small.

Canvas in c2.root is empty while it only contains empty histogram - nothing else.
You can expand canvas content in this link

Be aware that as result of your TTree::Draw not a histogram but TPolyMarker3D objects are created. Histogram used only for axes drawings.

Regards,
Sergey

Sergey,
Thanks for your reply.
I am a little confused by the answer however. This (that I can see test.root and c1.root, but not c2.root) is my problem. In your links, c2.root is still not visible. So phrased differently, how do you make c2 visible, either by changing markersize or otherwise? c2 is visible when using root from command line.
Thanks

In c2.root you just storing histogram from TTree::Draw. But this histogram has no content and used only for axis drawings. Therefore you see it empty.

When you store canvas - you store all drawn objects.

My apologies for being dense, but when I say:

auto c2 = new TCanvas("c2", "Detection Position", 600, 600);
...
c2->cd();  hist->Draw();
...
c2->SaveAs("c2.root");

am I not saving the canvas with the included histogram?

You saved histogram, but histogram has no entries.
Data of TTree::Draw stored in other objects - TPolyMarker3D

The inevitable follow up question is then how do you save the points/entries from the histogram to the canvas?

From my point of view, I am drawing two histograms, saving them to two different root files. One renders and the other one doesn’t. The only difference with the other being that I set the title.
With regular root (ie run on command line, hist->Draw() produces the right visual.

Thank you for your patience.

Look for list of objects in the canvas in test.root file.

Most of the objects there are not histograms. And htemp is just used for axis drawing.
It is because of 4-dim tree drawing with expression x:y:z:w.

It is different n case of 2-dim draw expression. In that case data directly stored in histogram.

1 Like

Sergey,

Thanks for the explanation.
One more question: This works inspite of being a 4-D hist. The only difference I see is that I am setting a title in the case where it fails.

TTree *t = new TTree("t", "testdata.csv");
t->ReadFile("testdata.csv", "x:y:z:w");
t->Draw("x : y : z : w>>hist");
c1->SaveAs("c1.root")

The histogram drawn to c1 is also 4d, and yet it works.

Thanks.

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