Random distribution from a TF1 in a range

Hi,

I think there are two points that could be improved in TH1::FillRandom :

1/ Arguments specifying the range would be welcome.

2/ TH1::FillRandom should take care of the TF1 range.

For instance, say that I want to fill a histogram defined from 0 to 10 in the range 3 to 6 with 1000 events randomly chosen from a distribution y=x. I would do :

TF1* f = new TF1("f","x",3,6); TH1* h = new TH1("h","h",10,0,10); h -> FillRandom("h",1000,3,6);

Or :

TF1* f = new TF1("f","x",3,6); TH1* h = new TH1("h","h",10,0,10); h -> FillRandom("h",1000);

Currently the first function is not implemented (it would be welcome), and the second example does not behave as expected, but fills the histogram from 0 to 10 while the TF1 is defined only from 3 to 6.

Any reason for the second behaviour ? And comment on the first suggestion ?

The main workaround is to use GetRandom in a loop :

TF1* f = new TF1("f","x",3,6); TH1* h = new TH1("h","h",10,0,10); for (int i=0; i<1000; i++) h -> Fill(f->GetRandom(3,6));

Cheers.

Edit : Would probably have been better placed in ROOT Discussion…

An option could be implemented in TH1::FillRandom. However this option has not been implemented when writing the original code to avoid the case where the function range starts/ends in the middle of histogram bins. Since the workaround is trivial (note it is not your workaround) we will not implement this option in TH1::FillRandom.

Rene

TF1* f = new TF1("f","x",3,6); TH1* h = new TH1("h","h",10,0,10); for (int i=0; i<1000; i++) h -> Fill(f->GetRandom());

[quote=“brun”]this option has not been implemented when writing the original code to avoid the case where the function range starts/ends in the middle of histogram bins.[/quote]That makes sense, indeed. Giving the bin range would be more appropriate.

[quote=“brun”]note it is not your workaround […]

f->GetRandom()[/quote]My bad, I thought I had tried and it didn’t work… :blush: