Draw 2D histogram of variables of a function with random numbers generated

Hello all,

I have function such like y= C*cos2x. Here C is constant not important indeed. I need to draw the distribution of x vs y. I have no data, not at all. I will fill the histogram with random numbers. What is the best way to produce it? FillRandom do the work? I think I need something like that but could not complete it.

TH2F h3 (“h3”, “histogram title”, 100, -5.0 , 5.0, 200, 0.0, 1.0);
auto form1 = new TFormula(“form1”,“C*cos2x”);
auto sqroot = new TF1(“sqroot”,“form1”,0,10);
h3->FillRandom(“sqroot”, 1000);
h3->ProjectionX();

I may get the “x” distribution here but I need histogram of x vs y. Any idea how to do? Thanks.

Hi,

FillRandom is fine. After FillRandom your TH2 histogram will contain the joint(x,y) distribution. Is this what you want ? Or do you want its projection in X or Y ? Or do you want to plot the average of Y vs X ? In this case you should then project to a profile and use TH2::ProfileX

Cheers

Lorenzo

Hi,

thanks for the reply. I just want to know how y changes while generating random numbers over x. So I need to plot y vs x variables not its projection on X or Y.

TF1 *sqroot = new TF1("sqroot", "[0] * cos(2.0 * x)", 0, 10); sqroot->SetParName(0, "C"); sqroot->SetParameter(0, 1.0); sqroot->Draw(); sqroot->Print();

Hi Pepe,

thanks it’s working.

I wonder if it would be possible to draw cosx vs y ? I tried to loop over bin number to get the contents but I do not think TF1::GetNbins() exist. Is there any other way ? Thanks.