About TF2 GetRandom2


What I want to obtain is the random number (u,phi), which obey custom function g2.
My code is :grinning:

#include <TMath.h>
#include <TGraph2D.h>
#include <TRandom.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TF2.h>
#include <TH1.h>
#include "TStopwatch.h"

Double_t g2(Double_t *x, Double_t *par)
  Double_t r1 = par[0]*(1+(1+x[0])*(1+x[0]))-4*x[0]/par[0]*(1+x[0])*(par[0]-x[0])*(1-par[1]*TMath::Cos(2*(x[1]-par[2])));
  Double_t r2 = par[3]*x[0]*(x[0]+2)*(par[0]-2*x[0]);
  Double_t r3 = par[4]*2*x[0]*TMath::Sqrt(x[0]*(par[0]-x[0]))*TMath::Sin(x[1]);
  Double_t r4 = par[0]*par[0]*((1+x[0])*(1+x[0])*(1+x[0]));
  Double_t result = (r1 + r2 + r3)/r4;
  return result;

void originalcode()
     TStopwatch timer;

	 // Define some parameters
     Double_t x, y, u, phi;
	 Double_t E0 = 45.5*1e9;
	 Double_t w0 = 1.24;
	 Double_t me = 0.511*1e6;
	 Double_t gamma = E0/me;
     Double_t kappa = 4*E0*w0/(me*me);
     Double_t L1 = 60;
     Double_t L2 = 40;
	 Double_t theta0 = 3.4467*1e-3;
	 // Create a histogram and fill it randomly with f2
   Double_t umin = 0;
   Double_t umax = kappa;
   Double_t phimin = 0;
   Double_t phimax = 2*TMath::Pi();
   Double_t npar = 5;
   TF2 *f2 = new TF2("f2",g2,umin,umax,phimin,phimax,npar);
   // Fill the 2D histogram
   Int_t nd = 1*20;                  // MC events 
	 for (Int_t N=0; N<nd; N++) 
     cout<<"parameter u is "<< u << endl;
     cout<<"parameter phi is "<< phi << endl;

Quetions: the print u and phi are always the same per run of my code. I don’t know what’ going on. And I don’t know the relationship between the result and setseed~

Thanks for your reply.
Your example are right and I can repeat your simulation too. However I am still confused about my simulation

@dastudillo: If you run in parallel, you would need to provide a different TRandom object to each of the thread running.
For doing this you need to use the new TF1::GetRandom interface available in ROOT 6.24, see


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.