User Tools


//Generates data points according to a Gaussian distribution,
//fills a histogram with these points, and performs a Gaussian fit
//to the histogram.

//To invoke, start ROOT and...:
//.L TRandomExample
//TRandomExample(0.0,1.0)  //Or change the mean and standard dev. as you like

void TRandomExample(float mean, float std, int npoints = 100000) {
	TRandom3 *t_rand = new TRandom3();
	t_rand->SetSeed(0); //Sets a random seed

	//Set the boundaries and number of bins of the histogram.
	float x_min = mean-10.0*std;
	float x_max = mean+10.0*std;
	int nbins = 200;

	//Create the histogram
	TH1F *histogram = new TH1F("histogram","Random Numbers",nbins,x_min,x_max);

	//Fill the histogram	
	for (int i = 0; i < npoints; ++i) {
		float value = t_rand->Gaus(mean,std);
		histogram->Fill(value);
	}

	histogram->Fit("gaus"); //Fitting automatically draws the histogram
//	histogram->Draw();
}

Top