# Variable range 2D histograms

Hello–

I think this is a simple question, but I can’t seem to figure it out.

How can I create a 2D histogram with a variable range? With 1D I’ve been setting the min and max to 0, but I get errors if I do that for 2D. I don’t know what the final range will be, so I want it to change as the histogram fills.

Thank you!

try this example

Rene

```void var2d() { TH2F *h = new TH2F("h","test",40,0,0,30,0,0); h->SetBuffer(2000); TRandom r; for (Int_t i=0;i<10000;i++) { h->Fill(0.1*r.Rndm(),50*r.Rndm()); } h->Draw(); }```

Hmm… that did work. I can’t figure out what’s wrong with mine. It seems to be working partially (the axes it draws are correct), but it gives me an error:

Error in TCanvas::Range: illegal world coordinates range: x1=0.000000, y1=0.000000, x2=0.000000, y2=0.000000
Error in TCanvas::RangeAxis: illegal axis coordinates range: xmin=0.000000, ymin=0.000000, xmax=0.000000, ymax=0.000000
root  Error in TCanvas::Range: illegal world coordinates range: x1=0.000000, y1=0.000000, x2=0.000000, y2=0.000000
Error in TCanvas::RangeAxis: illegal axis coordinates range: xmin=0.000000, ymin=0.000000, xmax=0.000000, ymax=0.000000

Here’s some of my code:

// Constructor
TH2F* delayVsPrompt = new TH2F(“dE(pE)”,
“Delay energy as a function of prompt energy”,
75, 0, 0, 75, 0, 0);
//Fill (in a loop)…
//Plot
TCanvas* c5=new TCanvas(“dE(pE)”,
“Delay energy as a function of prompt energy”, 700, 500);
c5->Clear();
delayVsPrompt->SetXTitle(“Prompt energy (MeV)”);
delayVsPrompt->SetYTitle(“Delay energy (MeV)”);
delayVsPrompt->Draw(“colz”);
gStyle->SetPalette(1,0); I’m using version 4.00/08 (14 July 2004).

Thanks!

Use a version >= 4.04

Rene

Ok, working now, thanks!

The only problem is that the number of entries is wrong. For small sets of data (<150 or so “real” entries), the number of entries is double the real number of entries. For large data sets, the number of entries is the real number + size of buffer + 1.

I suppose this is just a cosmetic problem (the data displayed, mean, RMS, etc is unaffected), but is there a way to keep this from happening?