// rangauss.cxx : Fill Histogram with Gaussian Random numbers // #include #include #include int rangauss(const int Nrndm=2, const int Nfill=1000) { // * define some constants //const int Nrndm=2; // fill one random number each time //const int Nfill=1000; // number of times to fill histogram const int Nbins=100; const double xmin=-4.; const double xmax=4; const double pi=3.14159265359; // normalised Gauss function TF1 *gauss=new TF1("gauss","1/sqrt(2.*pi)*exp(-x*x/2.)",xmin,xmax); // ---------------------------------------------------------------- // create main canvas // at xtop ytop sizex sizey TCanvas *theCanvas=new TCanvas("theCanvas","TestCanvas", 100, 100, 500, 600); TPad *txtPad = new TPad("txtPad","TextPad",0.,0.8,1.,1.); TPad *hstPad = new TPad("hstPad","HistPad",0.,0. ,1.,0.8.); txtPad->Draw(); hstPad->Draw(); // set active pad to text pad and print some text txtPad->cd(); gPad->SetFillColor(42); gStyle->SetTextColor(9); TText* txtHead= new TText(0.1,0.7, "Gauß Distribution"); txtHead->SetTextSize(0.4); txtHead->Draw(); char* txt[50]; sprintf(txt,"filling %i times with %i numbers",Nfill,Nrndm); gStyle->SetTextColor(1); TText* txtIntro= new TText(0.05,0.2, const_cast(txt)); txtIntro->SetTextSize(0.25); txtIntro->Draw(); // set active pad to graphics pad hstPad->cd(); gPad->SetFillColor(0); gPad->SetGrid(); // some histogram style options gStyle->SetHistLineColor(46); gStyle->SetHistFillColor(3); gStyle->SetHistLineWidth(3); gStyle->SetAxisColor(4,"X"); gStyle->SetAxisColor(4,"Y"); gStyle->SetLabelColor(4,"X"); gStyle->SetLabelColor(4,"Y"); // book a histogram, w. Nbins bins, range [xmin,xmax[ TH1F* hgauss =new TH1F("hgauss","Gauß distribution",Nbins,xmin,xmax); // fill random numbers in a loop , Nfill times, Nrndm at a time for(int j =0; jFillRandom("gaus",Nrndm); hgauss->Draw(); hstPad->Update(); } // draw a Gaussian on top TF1 *gaussN=new TF1("gaussN","[0]*gauss",xmin,xmax); gaussN->SetParameter(0, Nfill*Nrndm * (xmax-xmin)/Nbins); gaussN->Draw("same"); // cout<< "weiter? "; cin >> i; // delete theCanvas; return 0; }