/********************************************************************** * * * Copyright (c) 2003 INFN - Sezione di Napoli * * * * For more information (including a list of authors) see * * the README file * * * * This library 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 library 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 library (see file COPYING); if not, write to the * * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * * Boston, MA 02111-1307 USA, or contact the authors. * * * **********************************************************************/ #include "StatTools/Pdf/Ext/Defaults.h" #include "StatTools/Fit/Ext/Defaults.h" #include "StatTools/Fit/Chi2.h" #include "StatTools/Fit/Chi2Fitter.h" #include "StatTools/ToyMC/Experiment.h" #include "StatTools/Funct/Function.h" #include "StatTools/Funct/Parameter.h" #include "StatTools/Pdf/RandomGeneratorConstant.h" #include "StatTools/Pdf/Pdf.h" #include "StatTools/Pdf/RandomGeneratorSample.h" #include "TFile.h" #include "TH1.h" using namespace std; using namespace Fit; using namespace Funct; using namespace ToyMC; template struct functor { static double f( double * x, double * ) { return (*_t)( *x ); } static void set( const T& t ) { _t = & t; } private: static T const * _t; }; template T const * functor::_t = 0; struct SinPdf : public Pdf::PdfNoNormalization< X > { template< typename F > SinPdf ( const F& f ) : Pdf::PdfNoNormalization< X, Expression >( f ) { } }; RANDOM_GENERATOR_SAMPLE( SinPdf, 1024, 0.001, 15 ) int main() { X x; SinPdf pdf( sqr( sin(x) / x ) ); UniformPartition partition( 100, 0.001, 15 ); const int bins = partition.bins(); // const double binw = partition.binWidth(); TFile file( "testfunctfit2.root", "RECREATE" ); TH1F histo( "signal", "(sin(x)/x=^2", bins, 0.001, 15 ); Pdf::Constant n( 10000 ); Experiment< Pdf::Constant, SinPdf > experiment( n, pdf ); SampleErr sample( bins ); experiment.generate( sample, partition ); int bin = 1; for( SampleErr::const_iterator i = sample.begin(); i != sample.end(); i++, bin++ ) { double n = i->content, dn = i->error; histo.SetBinContent( bin, n ); histo.SetBinError( bin, dn ); } histo.Write(); Parameter fit_constant( 1000 ); Parameter fit_omega( 1 ); Function< X > f( fit_constant * sqr( sin( fit_omega * x ) / ( fit_omega * x ) ) ); Chi2< Function< X > > chi2( f, partition ); Chi2Fitter< Chi2< Function< X > > > fit( chi2 ); fit.addParameter( "const", fit_constant.ptr(), 0, 100000 ); fit.addParameter( "omega", fit_omega.ptr(), 0, 10 ); cout << "intial function : " << endl << f << endl; for ( double x = 0.001; x <= 15; x += 1 ) cout << "pdf("< >::set( f ); TF1 rootfit( "fit", functor< Function< X > >::f, 0.001, 15 ); rootfit.Write(); return 0; }