Search code examples
histogramroot-frameworkhistogram2d

How to Draw a COLZ TH2F from a TTree?


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");

Solution

  • 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!