// rangauss2d.C : generate correlated random gaussian numbers // #include #include #include #include #include #include #include #include int rangauss2d(const int Nrndm=500, const int Nfill=100) { // * 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 Nbinsx=50; const double xmin=-3.9; const double xmax=3.9; const int Nbinsy=50; const double ymin=-3.9; const double ymax=3.9; const double mux=0.; const double muy=0.; const double sigx=1.; const double sigy=1.; const double rho=0.75; double u1,u2,x,y; // initialze Random Generator TRandom3 myrnd; // ---------------------------------------------------------------- // create main canvas // at xtop ytop sizex sizey TCanvas *theCanvas=new TCanvas("theCanvas","TestCanvas", 100, 100, 540, 600); TPad *txtPad = new TPad("txtPad","TextPad",0.,0.9,1.,1.); TPad *hstPad = new TPad("hstPad","HistPad",0.,0. ,1.,0.9); txtPad->Draw(); hstPad->Draw(); // set active pad to text pad and print some text txtPad->cd(); gPad->SetFillColor(42); gStyle->SetTextColor(9); char txt[50]; sprintf(txt,"mux=%1.2f, sigx=%1.2f, muy=%1.2f, sigy=%1.2f, rho=%1.2f" ,mux,sigx,muy,sigy,rho); TText* txtHead= new TText(0.1,0.7, const_cast(txt)); txtHead->SetTextSize(0.3); txtHead->Draw(); // text for output gStyle->SetTextColor(1); // text color black TText* txtResult= new TText(); txtResult->SetTextSize(0.3); // 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"); gStyle->SetOptStat(1111); // set active pad to graphics pad and define some colors hstPad->cd(); gPad->SetFillColor(0); gPad->SetGrid(); // book a histogram, w. Nbins bins, range [xmin,xmax[ TH2F* hxy =new TH2F("hxy","correlated random numbers" ,Nbinsx,xmin,xmax,Nbinsy,ymin,ymax); // fill random numbers in a loop , Nfill times, Nrndm at a time for(int j =0; jGaus(0.,1.); u2=gRandom->Gaus(0.,1.); x=mux+sigx*u1; y=muy+sigy*(rho*u1 + sqrt(1-rho*rho)*u2); hxy->Fill(x,y); } txtPad->cd(); sprintf(txt,"measured correlation %1.3g",hxy->GetCorrelationFactor()); txtResult->SetText(0.5,0.3,txt); txtResult->Draw(); hstPad->cd(); hxy->Draw("surf3"); hstPad->Update(); } // cout<< "weiter? "; cin >> i; // delete theCanvas; return 0; }