#include "TROOT.h" #include "Math/MinimizerOptions.h" #include "TH1.h" #include "TF1.h" #include "TRandom3.h" #include using std::thread; using std::vector; void a(int type = 0){ ROOT::EnableThreadSafety(); TH1::AddDirectory(kFALSE); if (type) { ROOT::Math::MinimizerOptions::SetDefaultMinimizer("Minuit"); // but also the default minimiser is thread unsafe printf("Using TMinuit \n"); } else { ROOT::Math::MinimizerOptions::SetDefaultMinimizer("Minuit2"); // but also the default minimiser is thread unsafe printf("Using Minuit2 \n"); } vector threads; //gSystem->Load("libMinuit2"); auto gaus = (TF1*) gROOT->GetFunction("gaus"); for (int i=0;i<8;++i){ auto f = [&](){ int ithread = i; TF1* myGaus; { R__LOCKGUARD(gGlobalMutex); //std::cout << "creating TF1 class " << std::endl; myGaus = new TF1(*gaus); //std::cout << "TF1 class created" << std::endl; } //TCanvas c; // W/o this, another problem is detected. TRandom3 rndm(0); TH1F h("h","h",100,-3,3); for (int j=0;j<1000;++j){ h.Fill(rndm.Gaus(0,1)); } auto r = h.Fit(myGaus,"Q0SL"); { R__LOCKGUARD(gGlobalMutex); std::cout << "result for thread " << ithread << std::endl; r->Print(); } delete myGaus; }; threads.emplace_back(f); } for (auto&& t : threads) t.join(); } int main(int argc, char ** argv) { if (argc > 1) a(1); else a(0); }