0

I am trying to draw a COLZ plot i.e. a 2D histogram with a colour bar, from a Tree, and be able to define the number of bins myself. My Tree is called event:

I have tried:

event->Draw("x:y>>hist1(1000,100,500,1000,0,500)", "x>100");
TH2F * hist1 = (TH2F*)gDirectory->Get("hist1");
hist1->Draw("COLZ");

and:

event->Draw("x:y>>hist1(1000,100,500,1000,0,500)", "x>100", "COLZ");
TH2F * hist1 = (TH2F*)gDirectory->Get("hist1");
hist1->Draw();

But neither will draw the histogram.

This will draw a scatter plot:

event->Draw("x:y>>hist1(1000,100,500,1000,0,500)", "x>100");
TH2F * hist1 = (TH2F*)gDirectory->Get("hist1");
hist1->Draw();

This will draw a COLZ plot but using this method I'm unable to define bin sizes myself:

 event->Draw("x:y", "x>100", "COLZ");
Oceanescence
  • 1,967
  • 3
  • 18
  • 28

2 Answers2

3

I can not reproduce the issue, your first try works for me:

event->Draw("x:y>>hist1(1000,100,500,1000,0,500)", "x>100");
TH2F * hist1 = (TH2F*)gDirectory->Get("hist1");
hist1->Draw("COLZ");

It can also work in a single line:

event->Draw("x:y>>hist1(1000,100,500,1000,0,500)", "x>100", "COLZ");

In your 3rd case, if hist1->Draw(); draws a scatter plot, then hist1->Draw("COLZ"); should work too. Did you run exactly the same way? If so, can you provide a Minimal, Complete, and Verifiable example?

Note: the result of hist1->Draw(); is not a scatter plot (ROOT is misleading here), it is a histogram where bin contents are represented by dots. See e.g. this plot where you can guess the underlying bins.

Keldorn
  • 1,980
  • 15
  • 25
  • Unfortunately, I still get the results as in my original post. I can't see how we are doing anything differently but mine simply doesn't work. I agree in the 3rd case it should work as I get a plot without "COLZ" in my call to ->Draw(), but it doesn't plot anything. – Oceanescence Mar 10 '19 at 22:06
0

Thank you for your answer Keldorn but the problem lay within part of my code that I had not posted.

I was accessing my root file using:

 TFile f("file.root"); 
 TTree* event = (TTree*)f.Get("EventTree");

Changing this to:

TFile *f = new TFile("file.root");
TTree* event = (TTree*)f->Get("EventTree");

fixed all of my histogram problems!

Oceanescence
  • 1,967
  • 3
  • 18
  • 28
  • 1
    Glad you found something that works for you. There's something fishy though, both should be equally valid. So you probably still have a problem somewhere, which might reveal itself with another symptom at some point. – Keldorn Mar 14 '19 at 03:39
  • That is probably because it is not. Like a memory leak earlier with random-ish side effects. That's why a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) would help. – Keldorn Mar 14 '19 at 21:16