/* In this ROOT function we generate the sum of a flat distribution nEvents times and fir the output witha straight line. To run do: root .L foldflat.C // or .L foldflat.C+ to compile or .L foldflat.C++ to force compile foldflat(100000) */ // include C++ STL headers #include using namespace std; // ROOT library obejcts #include // 1d histogram classes #include // random generators #include // style object void foldflat(Int_t nEvents) // nEvents is an argument for the function { cout << "Generating " << nEvents << " events" << endl << endl; // create histogram object TH1F* hist = new TH1F("hist", "My first histogram", 100, 0, 1); // note that you can declare variable where they are needed!!! // create random generator object TRandom* randGen = new TRandom(); // make a loop for the number of events for(Int_t n = 0; n < nEvents; n++) { if((n+1)%100==0) cout << "event " << n+1 << endl; hist->Fill(randGen->Rndm()); // fill histogram with flat values } // Note that n only has local scope within the lopp so if you do // cout << n << endl; // the program won't compile! // set some options for how to display histogram info when you draw // Note that gStyle is a global object in ROOT of the clas TStyle gStyle->SetOptStat(1111); gStyle->SetOptFit(1111); hist->SetMinimum(0); // lower y range hist->Fit("pol0"); } // Now you have to work: // in the command line do: // .ls // to see list of objects in current directory // from the commandline do: // hist->Fit("pol0", "L") // L for maximum likelihood // Think about this // try to click on the objects in the graphical window and see what options you get! // change some options in this program and see the effects // 1) Fill the hist with a Gauss and fit with a Gauss // 2) Change back to flat distributions but change the argument so you now can fold it nTimes times and fit with Gauss - when does the distribution get Gaussian // 3) Why does it get Gaussian?