newly added. Not for productive used yetsvn diff | grep Index:
This commit is contained in:
84
src/tests/minuit2example/PGlobalChiSquare.cpp
Normal file
84
src/tests/minuit2example/PGlobalChiSquare.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/***************************************************************************
|
||||
|
||||
PGlobalChiSquare.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "PGlobalChiSquare.h"
|
||||
|
||||
namespace ROOT {
|
||||
namespace Minuit2 {
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
PGlobalChiSquare::PGlobalChiSquare(PRunList &runList)
|
||||
{
|
||||
fRunList = runList;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*/
|
||||
PGlobalChiSquare::~PGlobalChiSquare()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* \param par
|
||||
*/
|
||||
double PGlobalChiSquare::operator()(const std::vector<double>& par) const
|
||||
{
|
||||
double chi2 = 0.0;
|
||||
double funcValue;
|
||||
double diff;
|
||||
|
||||
// calculate chi2 for the given model
|
||||
for (unsigned int i=0; i<fRunList.size(); i++) { // run loop
|
||||
for (unsigned int j=0; j<fRunList[i].fTime.size(); j++) { // data loop
|
||||
funcValue = par[i]*exp(-par[2]*fRunList[i].fTime[j]); // par[i] since par[0] = ampl of run 0, etc.
|
||||
diff = funcValue - fRunList[i].fValue[j];
|
||||
chi2 += diff*diff/(fRunList[i].fError[j]*fRunList[i].fError[j]);
|
||||
}
|
||||
}
|
||||
|
||||
return chi2;
|
||||
}
|
||||
|
||||
} // namespace Minuit2
|
||||
} // namespace ROOT
|
||||
|
||||
62
src/tests/minuit2example/PGlobalChiSquare.h
Normal file
62
src/tests/minuit2example/PGlobalChiSquare.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/***************************************************************************
|
||||
|
||||
PGlobalChiSquare.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _PGLOBALCHISQUARE_H_
|
||||
#define _PGLOBALCHISQUARE_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Minuit2/FCNBase.h"
|
||||
|
||||
#include "mn2test.h"
|
||||
|
||||
namespace ROOT {
|
||||
namespace Minuit2 {
|
||||
|
||||
class PGlobalChiSquare : public FCNBase
|
||||
{
|
||||
|
||||
public:
|
||||
PGlobalChiSquare(PRunList &runList);
|
||||
~PGlobalChiSquare();
|
||||
|
||||
double Up() const { return 1.0; }
|
||||
double operator()(const std::vector<double>&) const;
|
||||
|
||||
private:
|
||||
PRunList fRunList;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Minuit2
|
||||
} // namespace ROOT
|
||||
|
||||
#endif // _PGLOBALCHISQUARE_H_
|
||||
157
src/tests/minuit2example/mn2test.cpp
Normal file
157
src/tests/minuit2example/mn2test.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/***************************************************************************
|
||||
|
||||
mn2test.cpp
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "Minuit2/FunctionMinimum.h"
|
||||
#include "Minuit2/MnUserParameters.h"
|
||||
#include "Minuit2/MnMinimize.h"
|
||||
#include "Minuit2/MnMinos.h"
|
||||
|
||||
#include "PGlobalChiSquare.h"
|
||||
|
||||
#include "mn2test.h"
|
||||
|
||||
using namespace ROOT::Minuit2;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/**
|
||||
* <p>generates data for testing
|
||||
* model = ampl * exp(-lambda * t), where ampl is depending on the run, but
|
||||
* not lambda
|
||||
*/
|
||||
void generateData(PRunList &data)
|
||||
{
|
||||
PRun run;
|
||||
const unsigned int length = 100;
|
||||
double value;
|
||||
|
||||
for (unsigned int i=0; i<2; i++) { // run loop
|
||||
for (unsigned int j=0; j<length; j++) { // data loop
|
||||
run.fTime.push_back((double)j/(double)length);
|
||||
value = (2*i+10)*exp(-1.7*(double)j/(double)length);
|
||||
value += 2.0*sqrt(value)*((double)random()/(double)RAND_MAX-0.5);
|
||||
run.fValue.push_back(value);
|
||||
run.fError.push_back(sqrt(value/4.0));
|
||||
}
|
||||
data.push_back(run);
|
||||
|
||||
// clean up
|
||||
run.fTime.clear();
|
||||
run.fValue.clear();
|
||||
run.fError.clear();
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc || argv);
|
||||
|
||||
int success = 1;
|
||||
|
||||
PRunList runs;
|
||||
generateData(runs);
|
||||
|
||||
unsigned int max = 25;
|
||||
if (runs[0].fTime.size() < max)
|
||||
max = runs[0].fTime.size();
|
||||
for (unsigned int i=0; i<runs.size(); i++) { // run loop
|
||||
std::cout << std::endl << "run " << i;
|
||||
for (unsigned int j=0; j<max; j++) { // data loop
|
||||
std::cout << std::endl << runs[i].fTime[j] << ", " << runs[i].fValue[j] << ", " << runs[i].fError[j];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
PGlobalChiSquare chi2(runs); // chi2 object derived from ROOT::Minuit2::FCNBase
|
||||
|
||||
// create parameters ---------------------------------
|
||||
MnUserParameters upar;
|
||||
|
||||
// model = ampl_i * exp(-lambda * t), i = run no.
|
||||
upar.Add("ampl1", 10.0, 1.0); // ampl1
|
||||
upar.Add("ampl2", 12.0, 1.0); // ampl2
|
||||
upar.Add("lambda", 0.1, 0.01); // lambda
|
||||
|
||||
// create minimizer ---------------------------------
|
||||
MnMinimize minimize(chi2, upar);
|
||||
|
||||
// minimize
|
||||
FunctionMinimum min = minimize();
|
||||
|
||||
if (min.IsValid()) {
|
||||
std::cout << std::endl << "minimize (MIGRAD/SIMPLEX) found minimum ...";
|
||||
} else {
|
||||
std::cout << std::endl << "minimize (MIGRAD/SIMPLEX) failed to find minimum ...";
|
||||
}
|
||||
std::cout << std::endl << "Chi2 = " << min.Fval()
|
||||
<< ", NDF = " << 2*runs[0].fTime.size()-3
|
||||
<< ", Chi2r = " << min.Fval()/((double)(2*runs[0].fTime.size()-3)) << std::endl;
|
||||
|
||||
// make minos analysis
|
||||
MnMinos minos(chi2, min);
|
||||
|
||||
// 1-sigma MINOS errors
|
||||
std::pair<double, double> e0 = minos(0);
|
||||
std::pair<double, double> e1 = minos(1);
|
||||
std::pair<double, double> e2 = minos(2);
|
||||
|
||||
// print out parameters
|
||||
unsigned int i=0;
|
||||
std::cout << std::endl << "1-sigma minos errors:" << std::endl;
|
||||
i=0;
|
||||
std::cout << std::endl << upar.Name(i) << ": "
|
||||
<< min.UserState().Value(i) << " "
|
||||
<< e0.first << " "
|
||||
<< e0.second;
|
||||
i++;
|
||||
std::cout << std::endl << upar.Name(i) << ": "
|
||||
<< min.UserState().Value(i) << " "
|
||||
<< e1.first << " "
|
||||
<< e1.second;
|
||||
i++;
|
||||
std::cout << std::endl << upar.Name(i) << ": "
|
||||
<< min.UserState().Value(i) << " "
|
||||
<< e2.first << " "
|
||||
<< e2.second;
|
||||
|
||||
std::cout << std::endl << std::endl;
|
||||
|
||||
// clean up
|
||||
runs.clear();
|
||||
|
||||
return success;
|
||||
}
|
||||
45
src/tests/minuit2example/mn2test.h
Normal file
45
src/tests/minuit2example/mn2test.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/***************************************************************************
|
||||
|
||||
mn2test.h
|
||||
|
||||
Author: Andreas Suter
|
||||
e-mail: andreas.suter@psi.ch
|
||||
|
||||
$Id$
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Andreas Suter *
|
||||
* andreas.suter@psi.ch *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef _MN2TEST_H_
|
||||
#define _MN2TEST_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
typedef struct {
|
||||
std::vector<double> fTime;
|
||||
std::vector<double> fValue;
|
||||
std::vector<double> fError;
|
||||
} PRun;
|
||||
|
||||
typedef std::vector<PRun> PRunList;
|
||||
|
||||
#endif // _MN2TEST_H_
|
||||
26
src/tests/minuit2example/mn2test.pro
Normal file
26
src/tests/minuit2example/mn2test.pro
Normal file
@@ -0,0 +1,26 @@
|
||||
#------------------------------------------------------
|
||||
# mn2test.pro
|
||||
# qmake file for mn2test
|
||||
#
|
||||
# Andreas Suter, 2007/10/23
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
#------------------------------------------------------
|
||||
|
||||
MAKEFILE = Makefile.mn2test
|
||||
|
||||
CONFIG += warn_on debug
|
||||
|
||||
HEADERS = PGlobalChiSquare.h
|
||||
|
||||
SOURCES = mn2test.cpp \
|
||||
PGlobalChiSquare.cpp
|
||||
|
||||
INCLUDEPATH += /usr/local/include
|
||||
INCLUDEPATH += ./
|
||||
|
||||
unix:LIBS += -L/usr/local/lib -lMinuit2Base -lbsd -lm -ldl -lutil
|
||||
|
||||
TARGET=mn2test
|
||||
|
||||
Reference in New Issue
Block a user