/*************************************************************************** * -------------------------------------------------------------------------- * Template to exercise Random Numbers and CLT * -> Book and fill a histogram with 10000 uniform random numbers * -------------------------------------------------------------------------- * 13.06.2005, Klaus Rabbertz * * modification log: * kr, 04.07.2006: Removed explicit ex10 naming * as, 06.06.2008: Some fixes for running as macro * gq, 22.12.2010: extracted from ranclt_2 ***************************************************************************/ /* Note that this file can be either used as a compiled program * or as a ROOT macro. * If it is used as a compiled program, additional include statements * and the definition of the main program have to be made. This is * not needed if the code is used as a macro. */ #ifndef __CINT__ // These include-statements are needed if the program is // run as a "stand-alone application", i.e. if it is not // called from an interactive ROOT session #include #include "TFile.h" #include "TH1.h" #include "TH2.h" #include "TProfile.h" #include "TRandom.h" #include "TMath.h" #include "TCanvas.h" #include "TPad.h" #include "TF1.h" #include "TStyle.h" #include "TText.h" #include // Here we declare the functions we are going to use later. // Each function corresponds to one subexercise void ex11(); void rancor(); void erprop(); void ranexp(); //______________________________________________________________________________ int main() { ex11(); return 0; } #endif void ex11() { // Here we call the functions solving the subexercises. // If you are just working on one, comment out the others. rancor(); erprop(); ranexp(); } //______________________________________________________________________________ // from here on, the code can also be used as a macro void rancor() { // Create a new ROOT file. TFile hfile("rancor.root", "RECREATE", "rancor"); // Book a 1D histogram "hran" (or any other name) holding 100 random numbers. // Create vector of pointers to 1D histograms. // Each pointer points to a histogram which holds the content of a // specific bin. TH1F *hlist[5]; // Book 1D histograms for content of each bin of the above histogram hlist[0] = new TH1F("hran01","bin 1",50,0.0,50.0); /* book a 2D histogram for the scatter plot * TH2F is a ROOT class for 2D histograms hscat in an instance of that class * Arguments: TH2F("identifier", "some title", NumberBinsX, XMin, XMax, * NumberBinsY, YMin, YMax); */ // Book a profile histogram // TProfile is a ROOT class for profile histograms, hprof is an instance // of that class. // Arguments: // TProfile("identifier", "some title", NumberBins, XMin, XMax, YMin, YMax, Option); // Note that YMin and YMax can be left blank/ // The Option sets the treatment of errors (refer to the ROOT manual) // "Option" can be left blank, too. // Now all histograms have been booked and we can start the real work. for (int j=0; j<10000; j++) { // Repeat the experiment 10000 times. // Clear histogram "hran", i.e. set the content of each bin to zero. // Generate 100 random numbers for each experiment. // Unpack histogram into array content. for (int k=0;k<5;k++) { // loop over all bins // Fill content of each bin into histogram } // Fill the scatter plot with the content of bin 2 and bin 4. // Fill the profile plot with the content of bin 2 and bin 4. } // Save all objects in the ROOT file and close it. hfile.Write(); hfile.Close(); } void erprop() { // Teste Fehlerfortpflanzung G. Quast, Dez. 2010 // Fülle Quotienten und Differenz Gauss-verteilter Zufallszahlen // in Histogram und vergleiche mit Gauss-Verteilungen // some variables holding the input const double mut=1.0; const double muy=0.5; const double sigt=0.3; const double sigy=0.1; const int Nrndm=10000; // eventually, create a new ROOT file. // TFile hfile("erprop.root","RECREATE","erprop"); double t,y; // ---------------------------------------------------------------- // create a canvas, eventually with sub-pads for the three parts // TCanvas *c1 ... // book a histograms, w. Nbins bins, range [xmin,xmax[ // TH1F* htovery = ... // TH1F* hyovert = ... // TH1F* htminusy = ... // generate random t, y and fill into histograms in a loop // normalised Gauss function // TF1 *Ngauss=new ... // draw histograms with Gauss functions and eventually some text into pads // eventually, save all objects in the ROOT file and close it. /* c1->Write(); hfile.Write(); hfile.Close(); */ } void ranexp() { Int_t NumRand=100000; // Number of random numbers to be filled into // a histogram. // Create a new ROOT file. TFile hfile("ranexp.root","RECREATE","pdfs"); // Book histogram TH1F *h100 = new TH1F("h100","exponential decay", 100,0.0,10.0); //------------------------------------------------------------------------ // Exponential distribution // Fill histogram h100 with exponential decay using transformation method //------------------------------------------------------------------------ // Save all histograms in the ROOT file and close it. hfile.Write(); hfile.Close(); }