minexam.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: minexam.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Rene Brun   22/08/95
00003 
00004 //______________________________________________________________________________
00005 //*-*-*-*-*-*-*-*-*-*-*-*The Minuit standard test program-*-*-*-*-*-*-*-*-*
00006 //*-*                    ========================                         *
00007 //*-*                                                                     *
00008 //*-*    This program is the translation to C++ of the minexam program    *
00009 //*-*    distributed with the Minuit/Fortran source file.                 *
00010 //*-*         original author Fred James                                  *
00011 //*-*                                                                     *
00012 //*-*       Fit randomly-generated leptonic K0 decays to the              *
00013 //*-*       time distribution expected for interfering K1 and K2,         *
00014 //*-*       with free parameters Re(X), Im(X), DeltaM, and GammaS.        *
00015 //*-*                                                                     *
00016 //*-*   This program can be run in batch mode with the makefile           *
00017 //*-*   or executed interactively with the command:                       *
00018 //*-*         Root > .x minexam.cxx                                       *
00019 //*-*                                                                     *
00020 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00021 
00022 
00023 void fcnk0(int &npar, double *gin, double &f, double *x, int iflag);
00024 int minexam();
00025 
00026 #ifndef __CINT__
00027 #include "TVirtualFitter.h"
00028 #include "TMath.h"
00029 #include "TStopwatch.h"
00030 
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 
00034 //______________________________________________________________________________
00035 int main()
00036 {
00037    return minexam();
00038 }
00039 #endif
00040 
00041 int minexam()
00042 {
00043    TStopwatch timer;
00044 
00045    // Initialize TMinuit via generic fitter interface with a maximum of 5 params
00046    TVirtualFitter *minuit = TVirtualFitter::Fitter(0, 5);
00047    printf("Starting timer\n");
00048    timer.Start();
00049    minuit->SetFCN(fcnk0);
00050 
00051    minuit->SetParameter(0, "Re(X)",    0,     0.1, 0,0);
00052    minuit->SetParameter(1, "Im(X)",    0,     0.1, 0,0);
00053    minuit->SetParameter(2, "Delta M",  0.535, 0.1, 0,0);
00054    minuit->SetParameter(3, "T Kshort", 0.892, 0  , 0,0);
00055    minuit->SetParameter(4, "T Klong",  518.3, 0  , 0,0);
00056 
00057 //*-*-       Request FCN to read in (or generate random) data (IFLAG=1)
00058    Double_t arglist[100];
00059    arglist[0] = 1;
00060    minuit->ExecuteCommand("CALL FCN", arglist, 1);
00061    minuit->FixParameter(2);
00062    arglist[0] = 0;
00063    minuit->ExecuteCommand("SET PRINT", arglist, 1);
00064    minuit->ExecuteCommand("MIGRAD", arglist, 0);
00065    minuit->ExecuteCommand("MINOS", arglist, 0);
00066    minuit->ReleaseParameter(2);
00067    minuit->ExecuteCommand("MIGRAD", arglist, 0);
00068    minuit->ExecuteCommand("MINOS", arglist, 0);
00069    arglist[0] = 3;
00070    minuit->ExecuteCommand("CALL FCN", arglist, 1);
00071 
00072    printf("Time at the end of job = %f seconds\n",timer.CpuTime());
00073    return 0;
00074 }
00075 
00076 //______________________________________________________________________________
00077 void fcnk0(Int_t &, Double_t *, Double_t &f, Double_t *x, Int_t iflag)
00078 {
00079    static Double_t thplu[50],thmin[50],t[50];
00080 
00081    static Double_t evtp[30] = {
00082               11.,  9., 13., 13., 17.,  9.,  1.,  7.,  8.,  9.,
00083                6.,  4.,  6.,  3.,  7.,  4.,  7.,  3.,  8.,  4.,
00084                6.,  5.,  7.,  2.,  7.,  1.,  4.,  1.,  4.,  5.};
00085    static Double_t evtm[30] = {
00086                0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  1.,
00087                0.,  2.,  1.,  4.,  4.,  2.,  4.,  2.,  2.,  0.,
00088                2.,  3.,  7.,  2.,  3.,  6.,  2.,  4.,  1.,  5.};
00089    static Double_t sevtp, sevtm;
00090 
00091    const Int_t nbins = 30;
00092    const Int_t nevtot = 250;
00093 
00094    Int_t i, nevplu, nevmin;
00095 
00096    Double_t xre, xim, dm, gams, gaml,gamls,xr1,xr2,em,ep;
00097    Double_t chisq, ti, thp, thm, evp, evm, chi1, chi2;
00098    Double_t sthplu, sthmin, ehalf, th, sterm;
00099 
00100    xre   = x[0];
00101    xim   = x[1];
00102    dm    = x[2];
00103    gams  = 1/x[3];
00104    gaml  = 1/x[4];
00105    gamls = 0.5*(gaml+gams);
00106       //  generate random data
00107    if (iflag == 1) {
00108       sthplu = sthmin = 0;
00109       for (i=0;i<nbins; i++) {
00110          t[i]     = 0.1*(i+1);
00111          ti       = t[i];         ehalf    = TMath::Exp(-ti*gamls);
00112          xr1      = 1-xre;
00113          xr2      = 1+xre;
00114          th       = (xr1*xr1 + xim*xim)*TMath::Exp(-ti*gaml);
00115          th      += (xr2*xr2 + xim*xim)*TMath::Exp(-ti*gams);
00116          th      -= 4*ehalf*xim*TMath::Sin(dm*ti);
00117          sterm    = 2*ehalf*(1-xre*xre-xim*xim)*TMath::Cos(dm*ti);
00118          thplu[i] = th + sterm;
00119          thmin[i] = th - sterm;
00120          sthplu  += thplu[i];
00121          sthmin  += thmin[i];
00122       }
00123       nevplu = Int_t(nevtot*(sthplu/(sthplu+sthmin)));
00124       nevmin = Int_t(nevtot*(sthmin/(sthplu+sthmin)));
00125       printf("  LEPTONIC K ZERO DECAYS\n");
00126       printf(" PLUS, MINUS, TOTAL=%5d %5d %5d\n",nevplu,nevmin,nevtot);
00127       printf("0      TIME       THEOR+      EXPTL+      THEOR-      EXPTL-\n");
00128       sevtp = sevtm = 0;
00129       for (i=0;i<nbins; i++) {
00130          thplu[i] = thplu[i]*nevplu/ sthplu;
00131          thmin[i] = thmin[i]*nevmin/ sthmin;
00132          sevtp   += evtp[i];
00133          sevtm   += evtm[i];
00134          printf("%12.4f%12.4f%12.4f%12.4f%12.4f\n",t[i],thplu[i],evtp[i],thmin[i],evtm[i]);
00135       }
00136       printf(" DATA EVTS PLUS, MINUS= %10.2f%10.2f\n", sevtp,sevtm);
00137    }
00138 //                      calculate chisquared
00139    chisq = sthplu = sthmin = 0;
00140    for (i=0;i<nbins; i++) {
00141       ti    = t[i];
00142       ehalf = TMath::Exp(-ti*gamls);
00143       xr1   = 1-xre;
00144       xr2   = 1+xre;
00145       th    = (xr1*xr1 + xim*xim)*TMath::Exp(-ti*gaml);
00146       th   += (xr2*xr2 + xim*xim)*TMath::Exp(-ti*gams);
00147       th   -= 4*ehalf*xim*TMath::Sin(dm*ti);
00148       sterm = 2*ehalf*(1-xre*xre-xim*xim)*TMath::Cos(dm*ti);
00149       thplu[i] = th + sterm;
00150       thmin[i] = th - sterm;
00151       sthplu  += thplu[i];
00152       sthmin  += thmin[i];
00153   }
00154    thp = thm = evp = evm = 0;
00155    if (iflag != 4) {
00156       printf("          POSITIVE LEPTONS                    ,NEGATIVE LEPTONS\n");
00157       printf("      TIME    THEOR    EXPTL    chisq         TIME    THEOR    EXPTL    chisq\n");
00158    }
00159    for (i=0;i<nbins; i++) {
00160       thplu[i] = thplu[i]*sevtp / sthplu;
00161       thmin[i] = thmin[i]*sevtm / sthmin;
00162       thp += thplu[i];
00163       thm += thmin[i];
00164       evp += evtp[i];
00165       evm += evtm[i];
00166          //  Sum over bins until at least four events found
00167       if (evp > 3)  {
00168          ep     = evp-thp;
00169          chi1   = (ep*ep)/evp;
00170          chisq += chi1;
00171          if (iflag != 4) printf(" %9.3f%9.3f%9.3f%9.3f\n",t[i],thp,evp,chi1);
00172          thp = evp = 0;
00173       }
00174       if (evm > 3) {
00175          em     = evm-thm;
00176          chi2   = (em*em)/evm;
00177          chisq += chi2;
00178          if (iflag != 4) {
00179             printf("                                          %9.3f%9.3f%9.3f%9.3f\n"
00180                              ,t[i],thm,evm,chi2);
00181          }
00182          thm = evm = 0;
00183       }
00184   }
00185   f = chisq;
00186 }

Generated on Tue Jul 5 15:15:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1