#include #include #include #include #include #include #include // Use: // Compile -> g++ -fopenmp -o tryMacro tryMacro.cpp `root-config --cflags --libs` // Run -> ./tryMacro const Int_t kMax = 1000; const Int_t numThreads = 4; void savePlot(TH1D *hist); int main(int argc, char *argv[]){ // Enable thread safety for ROOT ROOT::EnableThreadSafety(); // Print std::cout << "Starting!" << std::endl; // Create a Model histogram TH1D *model = new TH1D("model", "", 100, 0, 10); TRandom3 *rand = new TRandom3(); for(int i=0; i<1000; i++){ model->Fill(rand->Gaus(5,2)); } // Create a new histogram to fill TH1D *result = new TH1D("result", "", 100, 0, 10); // Extract some data from the histogram model and get the average. Then fill omp_set_num_threads(numThreads); #pragma omp parallel for for(Int_t k=0; kFillRandom(model, 20000); randHist2->FillRandom(model, 20000); #pragma omp critical { std::cout << k << "\t"; result->Fill(randHist->GetMean()); } randHist->Delete(); randHist2->Delete(); } std::cout << "\nEnding!" << std::endl; savePlot(model); savePlot(result); // Close and return model->Delete(); result->Delete(); rand->Delete(); return 0; } void savePlot(TH1D *hist){ TCanvas *c1 = new TCanvas("c1", "", 700,500); hist->Draw(); std::string name = std::string(hist->GetName())+".png"; c1->Print(name.c_str()); delete c1; }