/* In this ROOT function we generate a distribution according to sin(x) between 0 and pi using the ROOT function method To run do: root .L boxgenerate.C+ boxgenerate(10000) */ // include C++ STL headers #include using namespace std; // ROOT library obejcts #include // 1d function class #include // 1d histogram classes #include // style object #include // math functions #include // canvas object void rootfuncgenerate(Int_t nEvents) { cout << "Generating " << nEvents << " events" << endl << endl; // create histogram objects TH1F* hSin = new TH1F("hSin", "ROOT func generated sin(x) distribution", 100, 0, TMath::Pi()); // make a loop for the number of events TF1* sinFunc = new TF1("sinFunc", "sin(x)", 0, TMath::Pi()); for(Int_t n = 0; n < nEvents; n++) { if((n+1)%1000==0) cout << "event " << n+1 << endl; hSin->Fill(sinFunc->GetRandom()); // fill our sin dist histogram } gStyle->SetOptStat(1111); gStyle->SetOptFit(1111); // create canvas for hSin TCanvas* c1 = new TCanvas("c1", "sin canvas", 900, 600); hSin->SetMinimum(0); hSin->Draw(); // creat 1d function TF1* func = new TF1("func", "[0]*sin(x)", 0, TMath::Pi()); func->SetParameter(0, 10); func->SetLineColor(kRed); hSin->Fit(func); c1->SaveAs("sinx_rootfunc.gif"); } // Questions: // What is the efficiency of the generation? // does this agree with theory?