13 #ifdef ROOT_FIT_PARALLEL 39 #define NUMBER_OF_THREADS 2 52 namespace FitUtilParallel {
72 void Set(
unsigned int nrej,
double sum) {
77 const BinData &
Data()
const {
return *fData; }
81 double Sum()
const {
return fSum; }
83 unsigned int NRej()
const {
return fNRej; }
85 unsigned int Begin()
const {
return fBegin; }
86 unsigned int End()
const {
return fEnd; }
90 const unsigned int fBegin;
91 const unsigned int fEnd;
92 const BinData * fData;
100 void *EvaluateResidual(
void * ptr) {
102 ThreadData * t = (ThreadData *) ptr;
104 unsigned int istart = t->Begin();
105 unsigned int iend = t->End();
107 unsigned int nRejected = 0;
108 const int nthreads = NUMBER_OF_THREADS;
110 const BinData &
data = t->Data();
112 for (
unsigned int i = istart; i < iend; i+=nthreads) {
113 const double *
x = data.Coords(i);
114 double y = data.Value(i);
115 double invError = data.InvError(i);
128 if (fval > - std::numeric_limits<double>::max() && fval < std::numeric_limits<double>::max() ) {
130 double tmp = ( y -fval )* invError;
139 std::cout <<
"end loop " << istart <<
" " << iend <<
" chi2 = " << chi2 <<
" nrej " << nRejected << std::endl;
141 t->Set(nRejected,chi2);
149 const int nthreads = NUMBER_OF_THREADS;
151 unsigned int n = data.Size();
154 std::cout <<
"\n\nFit data size = " << n << std::endl;
157 func.SetParameters(p);
160 pthread_t thread[nthreads];
161 ThreadData * td[nthreads];
162 unsigned int istart = 0;
163 for (
int ithread = 0; ithread < nthreads; ++ithread) {
169 td[ithread] =
new ThreadData(istart,iend,data,func);
170 pthread_create(&thread[ithread],
NULL, EvaluateResidual, td[ithread]);
174 for (
int ithread = 0; ithread < nthreads; ++ithread)
175 pthread_join(thread[ithread],
NULL);
181 for (
int ithread = 0; ithread < nthreads; ++ithread) {
182 nRejected += td[ithread]->NRej();
183 chi2 += td[ithread]->Sum();
189 std::cout <<
"chi2 = " << chi2 <<
" n = " << nRejected << std::endl;
192 nPoints = n - nRejected;
208 inline double EvalLogF(
double fval) {
212 const static double epsilon = 2.*std::numeric_limits<double>::min();
214 return fval/epsilon +
std::log(epsilon) - 1;
223 unsigned int n = data.Size();
226 std::cout <<
"\n\nFit data size = " << n << std::endl;
228 std::cout <<
"\tpar = [ " << func.NPar() <<
" ] = ";
229 for (
unsigned int ipar = 0; ipar < func.NPar(); ++ipar)
230 std::cout << p[ipar] <<
", ";
231 std::cout <<
"---------------------------\n";
242 #pragma omp for reduction (+:logl,nRejected) 246 for (
unsigned int i = 0; i <
n; ++ i) {
251 const double * x = data.Coords(i);
252 double fval =
func ( x, p );
259 logl += EvalLogF( fval);
263 { std::cout <<
" ==== i = " << i <<
" thread " << omp_get_thread_num()
264 <<
"fval = " << fval <<
" logl = " << logl << std::endl;}
272 if (nRejected != 0) nPoints = n - nRejected;
276 int pr = std::cout.precision(15);
277 std::cout <<
"ncalls " << ncalls <<
" Logl = " << logl <<
" np = " << nPoints << std::endl;
278 std::cout.precision(pr);
static long int sum(long int i)
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
ROOT::Math::IParamMultiFunction Func
std::vector< std::vector< double > > Data
double EvaluateLogL(const IModelFunction &func, const UnBinData &data, const double *x, int iWeight, bool extended, unsigned int &nPoints)
evaluate the LogL given a model function and the data at the point x.
ROOT::Math::IParamMultiFunction IModelFunction
double EvaluateChi2(const IModelFunction &func, const BinData &data, const double *x, unsigned int &nPoints)
Chi2 Functions.
Double_t Sum(const double *x, const double *p)
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
double func(double *x, double *p)