#include <pythia6.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

/*********************************************************************
 * Various kinds of calling conventions 
 */
#ifndef WIN32
# define type_of_call 
# define DEFCHARD          const char*
# define DEFCHARL          , const int
# define PASSCHARD(string) string
# define PASSCHARL(string) , strlen(string)
/* Sub-routines and functions */
# define pyinit		pyinit_
# define pyevnt		pyevnt_
# define pyevnw		pyevnw_
# define pystat		pystat_
# define pyfram		pyfram_
# define pyalem		pyalem_
# define pyalps		pyalps_
# define pymrun		pymrun_
# define py2frm		py2frm_
# define py4frm		py4frm_
# define py6frm		py6frm_
# define py4jet		py4jet_
# define pybook		pybook_
# define pyfill		pyfill_
# define pyfact		pyfact_
# define pyoper		pyoper_
# define pyhist		pyhist_
# define pyplot		pyplot_
# define pynull		pynull_
# define pydump		pydump_
# define pytabu		pytabu_
# define pyfowo		pyfowo_
# define pyjmas		pyjmas_
# define pycell		pycell_
# define pyclus		pyclus_
# define pythru		pythru_
# define pysphe		pysphe_
# define pyp		pyp_
# define pyk		pyk_
# define pyr		pyr_
# define pylist		pylist_
# define pyedit		pyedit_
# define pyrobo		pyrobo_
# define pyslha		pyslha_
# define pyupda		pyupda_
# define pylogo		pylogo_
# define pyangl		pyangl_
# define pyerrm		pyerrm_
# define pycomp		pycomp_
# define pychge		pychge_
# define pyname		pyname_
# define pymass		pymass_
# define pydecy		pydecy_
# define pyexec		pyexec_
# define pygive		pygive_
# define py1ent		py1ent_
# define py2ent		py2ent_
# define py3ent		py3ent_
# define py4ent		py4ent_
# define pyshow		pyshow_
# define pyshpe         pysphe_
# define pytest         pytest_
/* Common blocks */
# define pyjets		pyjets_
# define pydat1		pydat1_
# define pydat2		pydat2_
# define pydat3		pydat3_
# define pydat4		pydat4_
# define pydatr		pydatr_
# define pysubs		pysubs_
# define pypars		pypars_
# define pyint1		pyint1_
# define pyint2		pyint2_
# define pyint3		pyint3_
# define pyint4		pyint4_
# define pyint5		pyint5_
# define pyint6		pyint6_
# define pyint7		pyint7_
# define pyint8		pyint8_
# define pyint9		pyint9_
# define pymssm		pymssm_
# define pyssmt		pyssmt_
# define pymsrv		pymsrv_
# define pyrvnv		pyrvnv_
# define pyrvpm		pyrvpm_
# define pyints		pyints_
# define pyg2dx		pyg2dx_
# define pybins		pybins_
# define hepevt		hepevt_
# define heprup		heprup_
# define hepeup		hepeup_
/* User routines */
# define pyevwt         pyevwt_
# define pykcut         pykcut_
# define pytaud         pytaud_
# define pytime         pytime_
# define upveto         upveto_
# define upevnt         upevnt_
# define upinit         upinit_
# define fhhiggscorr    fhhiggscorr_
# define fhsetflags     fhsetflags_
# define fhsetpara      fhsetpara_
# define pdfset         pdfset_
# define ssmssm         ssmssm_
# define sugra          sugra_
# define visaje         visaje_
/* I/O functions */
# define f77open        f77open_
# define f77close       f77close_
# define f77readstring  f77readstring_
# define f77readint     f77readint_
# define f77readfloat   f77readfloat_
# define f77readdouble  f77readdouble_
# define f77writestring f77writestring_
# define f77writeint    f77writeint_
# define f77writefloat  f77writefloat_
# define f77writedouble f77writedouble_
#else 
# define type_of_call      _stdcall
# define DEFCHARD          const char* , const int
# define DEFCHARL
# define PASSCHARD(string) string, strlen(string)
# define PASSCHARL(string)
/* Sub-routines and functions */
# define pyinit		PYINIT
# define pyevnt		PYEVNT
# define pyevnw		PYEVNW
# define pystat		PYSTAT
# define pyfram		PYFRAM
# define pyalem		PYALEM
# define pyalps		PYALPS
# define pymrun		PYMRUN
# define py2frm		PY2FRM
# define py4frm		PY4FRM
# define py6frm		PY6FRM
# define py4jet		PY4JET
# define pybook		PYBOOK
# define pyfill		PYFILL
# define pyfact		PYFACT
# define pyoper		PYOPER
# define pyhist		PYHIST
# define pyplot		PYPLOT
# define pynull		PYNULL
# define pydump		PYDUMP
# define pytabu		PYTABU
# define pyfowo		PYFOWO
# define pyjmas		PYJMAS
# define pycell		PYCELL
# define pyclus		PYCLUS
# define pythru		PYTHRU
# define pysphe		PYSPHE
# define pyp		PYP
# define pyk		PYK
# define pyr		PYR
# define pylist		PYLIST
# define pyedit		PYEDIT
# define pyrobo		PYROBO
# define pyslha		PYSLHA
# define pyupda		PYUPDA
# define pylogo		PYLOGO
# define pyangl		PYANGL
# define pyerrm		PYERRM
# define pycomp		PYCOMP
# define pychge		PYCHGE
# define pyname		PYNAME
# define pymass		PYMASS
# define pydecy		PYDECY
# define pyexec		PYEXEC
# define pygive		PYGIVE
# define py1ent		PY1ENT
# define py2ent		PY2ENT
# define py3ent		PY3ENT
# define py4ent		PY4ENT
# define pyshow		PYSHOW
# define pyshpe         PYSPHE
# define pytest         PYTEST
/* Common blocks */
# define pyjets		PYJETS
# define pydat1		PYDAT1
# define pydat2		PYDAT2
# define pydat3		PYDAT3
# define pydat4		PYDAT4
# define pydatr		PYDATR
# define pysubs		PYSUBS
# define pypars		PYPARS
# define pyint1		PYINT1
# define pyint2		PYINT2
# define pyint3		PYINT3
# define pyint4		PYINT4
# define pyint5		PYINT5
# define pyint6		PYINT6
# define pyint7		PYINT7
# define pyint8		PYINT8
# define pyint9		PYINT9
# define pymssm		PYMSSM
# define pyssmt		PYSSMT
# define pymsrv		PYMSRV
# define pyrvnv		PYRVNV
# define pyrvpm		PYRVPM
# define pyints		PYINTS
# define pyg2dx		PYG2DX
# define pybins		PYBINS
# define hepevt		HEPEVT
# define heprup		HEPRUP
# define hepeup		HEPEUP
/* User defined routines */
# define pyevwt         PYEVWT
# define pykcut         PYKCUT
# define pytaud         PYTAUD
# define pytime         PYTIME
# define upveto         UPVETO
# define upevnt         UPEVNT
# define upinit         UPINIT
# define fhhiggscorr    FHHIGGSCORR
# define fhsetflags     FHSETFLAGS
# define fhsetpara      FHSETPARA
# define pdfset         PDFSET
# define ssmssm         SSMSSM
# define sugra          SUGRA
# define visaje         VISAJE
/* I/O functions */
# define f77open        F77OPEN
# define f77close       F77CLOSE
# define f77readstring  F77READSTRING
# define f77readint     F77READINT
# define f77readfloat   F77READFLOAT
# define f77readdouble  F77READDOUBLE
# define f77writestring F77WRITESTRING
# define f77writeint    F77WRITEINT
# define f77writefloat  F77WRITEFLOAT
# define f77writedouble F77WRITEDOUBLE
#endif
/*********************************************************************
 * Common blocks 
 */
extern pythia_jets_t pyjets;     pythia_jets_t* pythia_jets = &pyjets;
extern pythia_dat1_t pydat1;	 pythia_dat1_t* pythia_dat1 = &pydat1;
extern pythia_dat2_t pydat2;	 pythia_dat2_t* pythia_dat2 = &pydat2;
extern pythia_dat3_t pydat3;	 pythia_dat3_t* pythia_dat3 = &pydat3;
extern pythia_dat4_t pydat4;	 pythia_dat4_t* pythia_dat4 = &pydat4;
extern pythia_datr_t pydatr;	 pythia_datr_t* pythia_datr = &pydatr;
extern pythia_subs_t pysubs;	 pythia_subs_t* pythia_subs = &pysubs;
extern pythia_pars_t pypars;	 pythia_pars_t* pythia_pars = &pypars;
extern pythia_int1_t pyint1;	 pythia_int1_t* pythia_int1 = &pyint1;
extern pythia_int2_t pyint2;	 pythia_int2_t* pythia_int2 = &pyint2;
extern pythia_int3_t pyint3;	 pythia_int3_t* pythia_int3 = &pyint3;
extern pythia_int4_t pyint4;	 pythia_int4_t* pythia_int4 = &pyint4;
extern pythia_int5_t pyint5;	 pythia_int5_t* pythia_int5 = &pyint5;
extern pythia_int6_t pyint6;	 pythia_int6_t* pythia_int6 = &pyint6;
extern pythia_int7_t pyint7;	 pythia_int7_t* pythia_int7 = &pyint7;
extern pythia_int8_t pyint8;	 pythia_int8_t* pythia_int8 = &pyint8;
extern pythia_int9_t pyint9;	 pythia_int9_t* pythia_int9 = &pyint9;
extern pythia_mssm_t pymssm;	 pythia_mssm_t* pythia_mssm = &pymssm;
extern pythia_ssmt_t pyssmt;	 pythia_ssmt_t* pythia_ssmt = &pyssmt;
extern pythia_msrv_t pymsrv;	 pythia_msrv_t* pythia_msrv = &pymsrv;
extern pythia_rvnv_t pyrvnv;	 pythia_rvnv_t* pythia_rvnv = &pyrvnv;
extern pythia_rvpm_t pyrvpm;	 pythia_rvpm_t* pythia_rvpm = &pyrvpm;
extern pythia_ints_t pyints;	 pythia_ints_t* pythia_ints = &pyints;
extern pythia_g2dx_t pyg2dx;	 pythia_g2dx_t* pythia_g2dx = &pyg2dx;
extern pythia_bins_t pybins;	 pythia_bins_t* pythia_bins = &pybins;
extern hep_evt_t     hepevt;	 hep_evt_t*     hep_evt     = &hepevt;
extern hep_rup_t     heprup;	 hep_rup_t*     hep_rup     = &heprup;
extern hep_eup_t     hepeup;     hep_eup_t*     hep_eup     = &hepeup;

/*********************************************************************
 * Subroutines and functions 
 */
extern void   type_of_call pyinit(DEFCHARD, DEFCHARD, DEFCHARD, 
				  double*  win 
				  DEFCHARL DEFCHARL DEFCHARL);
extern void   type_of_call pyevnt();
extern void   type_of_call pyevnw();
extern void   type_of_call pystat(int* mstat);
extern void   type_of_call pyfram(int* iframe);
extern double type_of_call pyalem(double* q2);
extern double type_of_call pyalps(double* q2);
extern double type_of_call pymrun(int* kf, double* q2);
extern void   type_of_call py2frm(int* irad, int* itau, int* icom);
extern void   type_of_call py4frm(double* atotsq, double* a1sq, double* a2sq, 
				  int*    istrat, int*    irad, int*    itau, 
				  int*    icom);
extern void   type_of_call py6frm(double* p12,  double* p13, double* p21, 
				  double* p23,  double* p32, double* ptop, 
				  int*    irad, int* itau, int* icom);
extern void   type_of_call py4jet(double* pmax, int* irad, int* icom);
extern void   type_of_call pybook(int* id, DEFCHARD, int* nx, double* xl, 
				  double* xh DEFCHARL);
extern void   type_of_call pyfill(int* id, double* x, double* w);
extern void   type_of_call pyfact(int* id, double* f);
extern void   type_of_call pyoper(int* id1, char* op, int* id2, int* id3, 
				  double* f1, double* f2);
extern void   type_of_call pyhist();
extern void   type_of_call pyplot(int* id);
extern void   type_of_call pynull(int* id);
extern void   type_of_call pydump(int* mdump, int* lfn, int* nhi, int* ihi);
extern void   type_of_call pytabu(int* mtabu);
extern void   type_of_call pyfowo(double* h10, double* h20, double* h30, 
				  double* h40);
extern void   type_of_call pyjmas(double* pmh, double* pml);
extern void   type_of_call pycell(int* njet);
extern void   type_of_call pyclus(int* njet);
extern void   type_of_call pythru(double* thr, double* obl);
extern void   type_of_call pysphe(double* sph, double* apl);
extern double type_of_call pyp(int* i, int* j);
extern int    type_of_call pyk(int* i, int* j);
extern double type_of_call pyr(int* dummy);
extern void   type_of_call pylist(int* mlist);
extern void   type_of_call pyedit(int* medit);
extern void   type_of_call pyrobo(int*    imi, int*    ima, 
				  double* the, double* phi, 
				  double* bex, double* bey, double* bez);
extern void   type_of_call pyslha(int* mupda, int* kf, int* ifail);
extern void   type_of_call pyupda(int* mupda, int* lfn);
extern void   type_of_call pylogo();
extern double type_of_call pyangl(double* x, double* y);
extern double type_of_call pyerrm(int* merr, DEFCHARD DEFCHARL);
extern int    type_of_call pycomp(int* kf);
extern int    type_of_call pychge(int* kf);
extern void   type_of_call pyname(int* kf, DEFCHARD DEFCHARL);
extern double type_of_call pymass(int* kf);
extern void   type_of_call pydecy(int* ip);
extern void   type_of_call pyexec();
extern void   type_of_call pygive(DEFCHARD DEFCHARL);
extern void   type_of_call py1ent(int* ip, int* kf, double* pe, double* the, 
				  double* phi);
extern void   type_of_call py2ent(int* ip, int* kf1, int* kf2, double* pecm);
extern void   type_of_call py3ent(int* ip, int* kf1, int* kf2, int* kf3,
				  double* pecm, double* x1,  double* x3);
extern void   type_of_call py4ent(int*    ip,   int*    kf1, int*    kf2, 
				  int*    kf3,  int*    kf4, double* pecm, 
				  double* x1,   double* x2,  double* x4, 
				  double* x12,  double* x14);
extern void   type_of_call pyshow(int* ip1, int* ip2, double* qmax);
extern void   type_of_call pytime(int* idat);
extern void   type_of_call pytest(int* what);
extern void   type_of_call pyshpe(double* sph, double* apl);

/* ---------------------------------------------------------------- */
extern void   type_of_call f77open(int* lun, DEFCHARD, int* ierr DEFCHARL);
extern void   type_of_call f77close(int* lun, int* ierr);
extern void   type_of_call f77readstring(int* lun, int* len, 
					 DEFCHARD, int* ierr DEFCHARL);
extern void   type_of_call f77readint(int* lun, int* val, int* ierr);
extern void   type_of_call f77readfloat(int* lun, float* val, int* ierr);
extern void   type_of_call f77readdouble(int* lun, double* val, int* ierr); 
extern void   type_of_call f77writestring(int* lun, DEFCHARD, 
					  int* ierr DEFCHARL);   
extern void   type_of_call f77writeint(int* lun, int* val, int* ierr);   
extern void   type_of_call f77writefloat(int* lun, float* val, int* ierr);
extern void   type_of_call f77writedouble(int* lun, double* val, int* ierr);


/*********************************************************************
 * Our interface 
 */ 
void pythia_init(const char* frame, const char* beam, const char* target, 
	     double      win)
{
  pyinit(PASSCHARD(frame), PASSCHARD(beam), PASSCHARD(target), &win
	 PASSCHARL(frame)  PASSCHARL(beam)  PASSCHARL(target));
}
/* ---------------------------------------------------------------- */
void pythia_evnt()
{
  pyevnt();
}
/* ---------------------------------------------------------------- */
void pythia_evnw()
{
  pyevnw();
}
/* ---------------------------------------------------------------- */
void pythia_stat(int mstat)
{
  pystat(&mstat);
}
/* ---------------------------------------------------------------- */
void pythia_fram(int iframe)
{
  pyfram(&iframe);
}
/* ---------------------------------------------------------------- */
double pythia_alem(double q2)
{
  return pyalem(&q2);
}
/* ---------------------------------------------------------------- */
double pythia_alps(double q2)
{
  return pyalps(&q2);
}
/* ---------------------------------------------------------------- */
double pythia_mrun(int kf, double q2)
{
  return pymrun(&kf, &q2);
}

/* ---------------------------------------------------------------- */
void pythia_2frm(int irad, int itau, int icom)
{
  py2frm(&irad,&itau,&icom);
}
/* ---------------------------------------------------------------- */
void pythia_4frm(double atotsq, double a1sq, double a2sq, 
	       int istrat, int irad, int itau, int icom)
{
  py4frm(&atotsq, &a1sq, &a2sq, &istrat, &irad, &itau, &icom);
}

/* ---------------------------------------------------------------- */
void pythia_6frm(double p12, double p13, double p21, double p23, 
	       double p32, double ptop, int irad, int itau, 
	       int icom)
{
  py6frm(&p12, &p13, &p21, &p23, &p32, &ptop, &irad, &itau, & icom);
}
/* ---------------------------------------------------------------- */
void pythia_4jet(double pmax, int irad, int icom)
{
  py4jet(&pmax,&irad,&icom);
}
/* ---------------------------------------------------------------- */
void pythia_book(int id, const char* title, int nx, double xl, double xh)
{
  pybook(&id, PASSCHARD(title), &nx, &xl, &xh PASSCHARL(title));
}
/* ---------------------------------------------------------------- */
void pythia_fill(int id, double x, double w)
{
  pyfill(&id, &x, &w);
}
/* ---------------------------------------------------------------- */
void pythia_fact(int id, double f)
{
  pyfact(&id, &f);
}
/* ---------------------------------------------------------------- */
void pythia_oper(int id1, char op, int id2, int id3, double f1, double f2)
{
  pyoper(&id1, &op, &id2, &id3, &f1, &f2);
}
/* ---------------------------------------------------------------- */
void pythia_hist()
{
  pyhist();
}
/* ---------------------------------------------------------------- */
void pythia_plot(int id)
{
  pyplot(&id);
}
/* ---------------------------------------------------------------- */
void pythia_null(int id)
{
  pynull(&id);
}
/* ---------------------------------------------------------------- */
void pythia_dump(int mdump, int lfn, int nhi, int ihi) 
{ 
  pydump(&mdump, &lfn, &nhi, &ihi);
}
/* ---------------------------------------------------------------- */
void pythia_tabu(int mtabu)
{
  pytabu(&mtabu);
}
/* ---------------------------------------------------------------- */
void pythia_fowo(double* h10, double* h20, double* h30, double* h40)
{
  pyfowo(h10, h20, h30, h40);
}
/* ---------------------------------------------------------------- */
void pythia_jmas(double* pmh, double* pml)
{
  pyjmas(pmh,pml);
}
/* ---------------------------------------------------------------- */
void pythia_cell(int* njet)
{
  pycell(njet);
}
/* ---------------------------------------------------------------- */
void pythia_clus(int* njet)
{
  pyclus(njet);
}
/* ---------------------------------------------------------------- */
void pythia_thru(double* thr, double* obl)
{
  pythru(thr, obl);
}
/* ---------------------------------------------------------------- */
void pythia_sphe(double* sph, double* apl)
{
  pyshpe(sph, apl);
}
/* ---------------------------------------------------------------- */
double pythia_p(int i, int j)
{
  return pyp(&i, &j);
}
/* ---------------------------------------------------------------- */
int pythia_k(int i, int j)
{
  return pyk(&i, &j);
}
/* ---------------------------------------------------------------- */
double pythia_r(int dummy)
{
  return pyr(&dummy);
}
/* ---------------------------------------------------------------- */
void pythia_list(int mlist)
{
  pylist(&mlist);
}
/* ---------------------------------------------------------------- */
void pythia_edit(int medit)
{
  pyedit(&medit);
}
/* ---------------------------------------------------------------- */
void pythia_robo(int imi, int ima, double the, double phi, 
	       double bex, double bey, double bez)
{
  pyrobo(&imi, &ima, &the, &phi, &bex, &bey, &bez);
}
/* ---------------------------------------------------------------- */
void pythia_slha(int mupda, int kf, int* ifail)
{
  pyslha(&mupda, &kf, ifail);
}
/* ---------------------------------------------------------------- */
void pythia_upda(int mupda, int lfn)
{
  pyupda(&mupda, &lfn);
}
/* ---------------------------------------------------------------- */
void pythia_logo()
{
  pylogo();
}
/* ---------------------------------------------------------------- */
double pythia_angl(double x, double y)
{
  return pyangl(&x, &y);
}
/* ---------------------------------------------------------------- */
void pythia_errm(int merr, const char* messag)
{
  pyerrm(&merr, PASSCHARD(messag) PASSCHARL(messag)); 
}
/* ---------------------------------------------------------------- */
int pythia_comp(int kf)
{
  return pycomp(&kf);
}
/* ---------------------------------------------------------------- */
int pythia_chge(int kf)
{
  return pychge(&kf);
}
/* ---------------------------------------------------------------- */
const char* pythia_name(int kf) 
{
  static char ret[17];
  pyname(&kf, PASSCHARD(ret) PASSCHARL(ret));
  return ret;
}

/* ---------------------------------------------------------------- */
double pythia_mass(int kf) 
{ 
  return pymass(&kf); 
}
/* ---------------------------------------------------------------- */
void pythia_decy(int ip) 
{ 
  pydecy(&ip); 
}
/* ---------------------------------------------------------------- */
void pythia_exec() 
{ 
  pyexec(); 
}
/* ---------------------------------------------------------------- */
void pythia_give(const char* str) 
{
  pygive(PASSCHARD(str) PASSCHARL(str));
}
/* ---------------------------------------------------------------- */
void pythia_1ent(int ip, int kf, double pe, double the, double phi)
{
  py1ent(&ip, &kf, &pe, &the, &phi);
}

/* ---------------------------------------------------------------- */
void pythia_2ent(int ip, int kf1, int kf2, double pecm)
{
  py2ent(&ip, &kf1, &kf2, &pecm);
}

/* ---------------------------------------------------------------- */
void pythia_3ent(int ip, int kf1, int kf2, int kf3,
	       double pecm, double x1, double x3)
{
  py3ent(&ip, &kf1, &kf2, &kf3, &pecm, &x1, &x3);
}

/* ---------------------------------------------------------------- */
void pythia_4ent(int ip, int kf1, int kf2, int kf3, int kf4,
		double pecm, double x1, double x2, double x4, 
	       double x12,  double x14)
{
  py4ent(&ip, &kf1, &kf2, &kf3, &kf4, &pecm, &x1, &x2, &x4, &x12, &x14);
}

/* ---------------------------------------------------------------- */
void pythia_show(int ip1, int ip2, double qmax)
{
  pyshow(&ip1, &ip2, &qmax);
}

/* ---------------------------------------------------------------- */
void pythia_time(int* idat) 
{ 
  pytime(idat); 
}

/* ---------------------------------------------------------------- */
void pythia_test(int what) 
{ 
  pytest(&what); 
}

/* ---------------------------------------------------------------- */
void pythia_message(const char* format, ...) 
{
  static char buf[1024];
  int lun = pythia_dat1->mstu[10];
  va_list ap;
  va_start(ap, format);
  vsprintf(buf, format, ap);
  pythia_write_string(lun, buf);
  va_end(ap);
}

/* ================================================================
 * I/O functions 
 */
int pythia_open(int lun, const char* file) 
{
  int ierr;
  f77open(&lun, PASSCHARD(file), &ierr PASSCHARL(file));
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_close(int lun) 
{
  int ierr;
  f77close(&lun, &ierr);
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_read_string(int lun, int len, char* str) 
{
  int ierr;
  f77readstring(&lun, &len, PASSCHARD(str), &ierr PASSCHARL(str));
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_read_int(int lun, int* val) 
{
  int ierr;
  f77readint(&lun, val, &ierr);
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_read_float(int lun, float* val) 
{
  int ierr;
  f77readfloat(&lun, val, &ierr);
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_read_double(int lun, double* val) 
{
  int ierr;
  f77readdouble(&lun, val, &ierr);
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_write_string(int lun, const char* str) 
{
  int ierr;
  f77writestring(&lun, PASSCHARD(str), &ierr PASSCHARL(str));
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_write_int(int lun, int val)
{
  int ierr;
  f77writeint(&lun, &val, &ierr);
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_write_float(int lun, float val)
{
  int ierr;
  f77writefloat(&lun, &val, &ierr);
  return ierr;
}
/* ---------------------------------------------------------------- */
int pythia_write_double(int lun, double val)
{
  int ierr;
  f77writedouble(&lun, &val, &ierr);
  return ierr;
}


/*********************************************************************
 * User defined functions 
 */
static pythia_evwt_t _evwt = 0;
static pythia_kcut_t _kcut = 0;
static pythia_taud_t _taud = 0;
static pythia_time_t _time = 0;
static pythia_upveto_t _upveto = 0;
void pythia_set_steer(pythia_evwt_t f1, pythia_kcut_t f2, pythia_taud_t f3, 
		      pythia_time_t f4, pythia_upveto_t f5)
{
  _evwt   = f1;
  _kcut   = f2;
  _taud   = f3;
  _time   = f4;
  _upveto = f5;
}

/* ---------------------------------------------------------------- */
static pythia_upinit_t _upinit = 0;
static pythia_upevnt_t _upevnt = 0;
void pythia_set_external(pythia_upinit_t f1, pythia_upinit_t f2)
{
  _upinit = f1;
  _upevnt = f2;
}

/* ---------------------------------------------------------------- */
static pythia_fhsetflags_t  _fhsetflags  = 0;
static pythia_fhsetpara_t   _fhsetpara   = 0;
static pythia_fhhiggscorr_t _fhhiggscorr = 0;
void pythia_set_feynhiggs(pythia_fhsetflags_t f1, pythia_fhsetpara_t f2, 
			  pythia_fhhiggscorr_t f3)
{
  _fhsetflags  = f1;
  _fhsetpara   = f2;
  _fhhiggscorr = f3;
}

/* ---------------------------------------------------------------- */
static pythia_pdfset_t  _pdfset  = 0;
static pythia_structm_t _structm = 0;
static pythia_structp_t _structp = 0;
void pythia_set_pdflib(pythia_pdfset_t f1, pythia_structm_t f2, 
		   pythia_structp_t f3)
{
  _pdfset  = f1;
  _structm = f2;
  _structp = f3;
}

/* ---------------------------------------------------------------- */
static pythia_visaje_t _visaje = 0;
static pythia_sugra_t  _sugra  = 0;
static pythia_ssmssm_t _ssmssm = 0;
void pythia_set_isajet(pythia_visaje_t f1, pythia_sugra_t f2, 
		       pythia_ssmssm_t f3)
{
  _visaje = f1;
  _sugra  = f2;
  _ssmssm = f3;
}

/*********************************************************************
 * Implemented dummy functions 
 */
/* ================================================================ */
void pyevwt(float* wtxs) 
{
  int isub, istsb;
  double tau, yst, cth, taup, x1, x2, xf, shat, that, uhat, pt2;
  if (_evwt) {
    (*_evwt)(wtxs);
    return;
  }
  pythia_message("Generic pyevwt function called\n");
  *wtxs = 1.;
  isub  = pythia_int1->mint[0];
  istsb = pythia_int2->iset[isub];
  tau   = pythia_int1->vint[20];
  yst   = pythia_int1->vint[21];
  cth   = (istsb == 2 || istsb == 4) ? pythia_int1->vint[22] : 0.;
  taup  = (istsb >= 3 && istsb <= 5) ? pythia_int1->vint[24] : 0.;
  x1    = pythia_int1->vint[40];
  x2    = pythia_int1->vint[41];
  xf    = x1 - x2;
  shat  = pythia_int1->vint[43];
  that  = pythia_int1->vint[44];
  uhat  = pythia_int1->vint[45];
  pt2   = pythia_int1->vint[46];
  if (pythia_r(0) < 10) exit(1);
}
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))

/* ---------------------------------------------------------------- */
void pykcut(int* mcut) 
{
  int isub, istsb;
  double tau, yst, cth, taup, x1, x2, xf, shat, that, uhat, pt2;
  double sqm3, sqm4, rm3, rm4, be34, rpts, be34l, rm34, rsqm, rthm;
  if (_kcut) {
    (*_kcut)(mcut);
    return;
  }
  pythia_message("Generic pykcut function called\n");
  *mcut = 0;
  isub  = pythia_int1->mint[1];
  istsb = pythia_int2->iset[isub];
  tau   = pythia_int1->vint[21];
  yst   = pythia_int1->vint[22];
  cth   = (istsb == 2 || istsb == 4) ? pythia_int1->vint[23] : 0.;
  taup  = (istsb >= 3 && istsb <= 5) ? pythia_int1->vint[26] : 0.;
  if (istsb <= 2 || istsb >= 5) {
    x1 = sqrt(tau) * exp(yst);
    x2 = sqrt(tau) * exp(-yst);
  } else {
    x1 = sqrt(taup) * exp(yst);
    x2 = sqrt(taup) * exp(-yst);
  }
  xf    = x1 - x2;
  shat  = tau * pythia_int1->vint[2];
  sqm3  = pythia_int1->vint[63];
  sqm4  = pythia_int1->vint[64];
  rm3   = sqm3 / shat;
  rm4   = sqm4 / shat;
  be34  = sqrt(MAX(0., pow(1. - rm3 - rm4, 2) - 4. * rm3 * rm4));
  rpts  = 4. * pow(pythia_int1->vint[71],2) / shat;
  be34l = sqrt(MAX(0., pow(1. - rm3 - rm4,2) - 4. * rm3 * rm4 - rpts));
  rm34  = 2. * rm3 * rm4;
  rsqm  = 1. + rm34;
  rthm  = (4. * rm3 * rm4 + rpts) / (1. - rm3 - rm4 + be34l);
  that  = -0.5 * shat * MAX(rthm, 1. - rm3 - rm4 - be34 * cth);
  uhat  = -0.5 * shat * MAX(rthm, 1. - rm3 - rm4 + be34 * cth);
  pt2   = MAX(pow(pythia_int1->vint[71],2), 
	      0.25 * shat * pow(be34,2) * (1. - pow(cth,2)));
  if (pythia_r(0) < 10) exit(1);
}
/* ---------------------------------------------------------------- */
void pytaud(int* itau, int* iorig, int* kforig, int* ndecay)
{
  if (_taud) {
    (*_taud)(itau, iorig, kforig, ndecay);
    return;
  }
  pythia_message("Generic pytaud function called\n");
  *ndecay = *itau + *iorig * *kforig;
  if (pythia_r(0) < 10) exit(1);
}

/* ---------------------------------------------------------------- */
void pytime(int* idat) 
{
  struct tm* now;
  time_t t;
  if (_time) {
    (*_time)(idat);
    return;
  }
  /* printf("Generic pytime function called\n"); */
  t        = time(NULL);
  now      = localtime(&t);
  idat[0] = now->tm_year + 1900;
  idat[1] = now->tm_mon + 1;
  idat[2] = now->tm_mday;
  idat[3] = now->tm_hour;
  idat[4] = now->tm_min;
  idat[5] = now->tm_sec;
}

/* ---------------------------------------------------------------- */
void upveto(int* veto)
{
  static int nlist = 0;
  if (_upveto) {
    (*_upveto)(veto);
    return;
  }
  pythia_message("Generic upveto function called\n");
  if (nlist <= 2) {
    pythia_message(" Full event record at time of upveto call: ");
    pythia_list(1);
    pythia_message(" Part of event record made available to upveto:");
    pythia_list(5);
    nlist++;
  }
  *veto = 0;
}

/* ================================================================ */
void upevnt()
{
  char str[200];
  int ret, i, found, lun;
  if (_upevnt) {
    (*_upevnt)();
    return;
  }
  pythia_message("Generic upevnt function called\n");
  lun = pythia_pars->mstp[160];
  do {
    pythia_read_string(lun, 200, str);
    if (ret < 0) break;
    found = 0;
    for (i = 0; i < 200-6; i++) {
      if (str[i] == ' ') continue;
      if (strncmp(&(str[i]), "<event>", 6) == 0 || 
	  strncmp(&(str[i]), "<event ", 6) == 0) 
	found = 1;
      break;
    }
    if (!found) continue;
    if ((ret = pythia_read_int(lun, &(hep_eup->nup)))) break;
    if ((ret = pythia_read_int(lun, &(hep_eup->idprup)))) break;
    if ((ret = pythia_read_double(lun, &(hep_eup->xwgtup)))) break;
    if ((ret = pythia_read_double(lun, &(hep_eup->scalup)))) break;
    if ((ret = pythia_read_double(lun, &(hep_eup->aqedup)))) break;
    if ((ret = pythia_read_double(lun, &(hep_eup->aqcdup)))) break;

    for (i = 0; i < hep_eup->nup; i++) {
      if ((ret = pythia_read_int(lun, &(hep_eup->idup[i])))) break;
      if ((ret = pythia_read_int(lun, &(hep_eup->istup[i])))) break;
      if ((ret = pythia_read_int(lun, &(hep_eup->mothup[i][0])))) break;
      if ((ret = pythia_read_int(lun, &(hep_eup->mothup[i][1])))) break;
      if ((ret = pythia_read_int(lun, &(hep_eup->icolup[i][0])))) break;
      if ((ret = pythia_read_int(lun, &(hep_eup->icolup[i][1])))) break;
      if ((ret = pythia_read_double(lun, &(hep_eup->pup[i][0])))) break;
      if ((ret = pythia_read_double(lun, &(hep_eup->pup[i][1])))) break;
      if ((ret = pythia_read_double(lun, &(hep_eup->pup[i][2])))) break;
      if ((ret = pythia_read_double(lun, &(hep_eup->pup[i][3])))) break;
      if ((ret = pythia_read_double(lun, &(hep_eup->pup[i][4])))) break;
      if ((ret = pythia_read_double(lun, &(hep_eup->vtimup[i])))) break;
      if ((ret = pythia_read_double(lun, &(hep_eup->spinup[i])))) break;
    }
    break;
  } while (1);
  if (ret) {
    hep_eup->nup = 0;
    pythia_pars->msti[49] = 0;
  }
}

/* ---------------------------------------------------------------- */
void upinit()
{
  char str[200];
  int ret, i, found, lun;
  if (_upinit) {
    (*_upinit)();
    return;
  }
  pythia_message("Generic upinit function called\n");
  lun = pythia_pars->mstp[160];
  do {
    pythia_read_string(lun, 200, str);
    if (ret < 0) break;
    found = 0;
    for (i = 0; i < 200-6; i++) {
      if (str[i] == ' ') continue;
      if (strncmp(&(str[i]), "<init>", 5) == 0 || 
	  strncmp(&(str[i]), "<init ", 5) == 0) 
	found = 1;
      break;
    }
    if (!found) continue;
    if ((ret = pythia_read_int(lun, &(hep_rup->idbmup[0])))) break;
    if ((ret = pythia_read_int(lun, &(hep_rup->idbmup[1])))) break;
    if ((ret = pythia_read_double(lun, &(hep_rup->ebmup[0])))) break;
    if ((ret = pythia_read_double(lun, &(hep_rup->ebmup[1])))) break;
    if ((ret = pythia_read_int(lun, &(hep_rup->pdfgup[0])))) break;
    if ((ret = pythia_read_int(lun, &(hep_rup->pdfgup[1])))) break;
    if ((ret = pythia_read_int(lun, &(hep_rup->pdfsup[0])))) break;
    if ((ret = pythia_read_int(lun, &(hep_rup->pdfsup[1])))) break;
    if ((ret = pythia_read_int(lun, &(hep_rup->idwtup)))) break;
    if ((ret = pythia_read_int(lun, &(hep_rup->nprup)))) break;

    for (i = 0; i < hep_rup->nprup; i++) {
      if ((ret = pythia_read_double(lun, &(hep_rup->xsecup[i])))) break;
      if ((ret = pythia_read_double(lun, &(hep_rup->xerrup[i])))) break;
      if ((ret = pythia_read_double(lun, &(hep_rup->xmaxup[i])))) break;
      if ((ret = pythia_read_int(lun, &(hep_rup->lprup[i])))) break;
    }
    break;
  } while (1);
  if (ret) exit(1);
}
/* ================================================================ */
void fhhiggscorr(int* ierr, double* rmhigg, double* saeff, double* uhiggs)
{
  if (_fhhiggscorr) {
    (*_fhhiggscorr)(ierr, rmhigg, saeff, uhiggs);
    return;
  }
  pythia_message("Generic fhhiggscorr function called\n");
  if (pythia_r(0) < 10) exit(0);
}


    
/* ---------------------------------------------------------------- */
void fhsetflags(int* ierr, int* imsp, int* ifr, int* itbr, 
		int* ihmx, int* ipa2, int* ilp, int* itr, 
		int* ibr)
{
  /* imsp = 4 - full MSSM 
   * ifr  = 0 - MSbar field renormalised 
   * itbr = 0 - MSbar TB renormalized
   * ihmx = 2 - 2x2 (h0 - HH) mixing in natural Higgs sector
   * ip2a = 0 - No approximation 
   * ilp  = 2 - Include 2-loop corrections 
   * itr  = 1 - Running tip mass in 2 loop corrections 
   * ibr  = 1 - resummed MB in 2-loop corrections 
   */
  if (_fhsetflags) {
    (*_fhsetflags)(ierr, imsp, ifr, itbr, ihmx, ipa2, ilp, itr, ibr);
    return;
  }
  pythia_message("Generic fhsetflags function called\n");
  if (pythia_r(0) < 10) exit(0);
}

/* ---------------------------------------------------------------- */
void fhsetpara(int*     ier,   double*  scf, 
	       double*  dmt,   double*  dmb,   double*  dmw,   
	       double*  dmz,   double*  dtanb, double*  dma,
	       double*  dmh, 
	       double*  dm3l,  double*  dm3e,  double*  dm3q, 
	       double*  dm3u,  double*  dm3d, 
	       double*  dm2l,  double*  dm2e,  double*  dm2q, 
	       double*  dm2u, 
	       double*  dm2d,  double*  dm1l,  double*  dm1e, 
	       double*  dm1q,  double*  dm1u,  double*  dm1d, 
	       double*  dmu,  
	       double*  ae33,  double*  au33,  double*  ad33, 
	       double*  ae22,  double*  au22,  double*  ad22, 
	       double*  ae11,  double*  au11,  double*  ad11, 
	       double*  dm1,   double*  dm2,   double*  dm3,
	       double*  rlt,   double*  rlb,   
	       double*  qtau,  double*  qt,    double*  qb)
{
  if (_fhsetpara) {
    (*_fhsetpara)(ier,  scf,  dmt,  dmb,  dmw,  dmz,  dtanb, dma,
		  dmh,  dm3l, dm3e, dm3q, dm3u, dm3d, dm2l, dm2e, dm2q, 
		  dm2u, dm2d, dm1l, dm1e, dm1q, dm1u, dm1d, dmu,  
		  ae33, au33, ad33, ae22, au22, ad22, ae11, au11, ad11, 
		  dm1,  dm2,  dm3,  rlt,  rlb,  qtau, qt,   qb);
    return;
  }
  pythia_message("Generic fhsetpara function called\n");
  if (pythia_r(0) < 10) exit(0);
}

/* ================================================================ */
void pdfset(char* para, double* value) 
{
  int i;
  if (_pdfset) {
    (*_pdfset)(para, value);
    return;
  }
  pythia_message("Generic pdfset function called\n");
  for (i = 0; i < 20; i++) 
    para[19 * 20 + i] = para[0 * 20 + i];
  value[19] = value[0];
}
  
/* ---------------------------------------------------------------- */
void structm(double* xx,  double* qq,   double* upv,
	     double* dnv, double* usea, double* dsea, 
	     double* str, double* chm,  double* bot, 
	     double* top, double* glu)
{
  if (_structm) {
    (*_structm)(xx, qq, upv, dnv, usea, dsea, str, chm, bot, top, glu);
    return;
  }
  pythia_message("Generic structm function called\n");
  if (pythia_r(0) < 10) exit(1);
  *upv  = *xx + *qq;
  *dnv  = *xx + 2. * *qq;
  *usea = *xx + 3. * *qq;
  *dsea = *xx + 4. * *qq;
  *str  = *xx + 5. * *qq;
  *chm  = *xx + 6. * *qq;
  *bot  = *xx + 7. * *qq;
  *top  = *xx + 8. * *qq;
  *glu  = *xx + 9. * *qq;
}

  
/* ---------------------------------------------------------------- */
void structp(double* xx,   double* qq2,  double* p2,  int* ip2, 
	     double* upv,  double* dnv,  double* usea, 
	     double* dsea, double* str,  double* chm, 
	     double* bot,  double* top,  double* glu)
{
  if (_structp) {
    (*_structp)(xx, qq2, p2, ip2, upv, dnv, 
	       usea, dsea, str, chm, bot, top, glu);
    return;
  }
  pythia_message("Generic structp function called\n");
  if (pythia_r(0) < 10) exit(1);
  *upv  = *xx + *qq2;
  *dnv  = *xx + 2. * *qq2;
  *usea = *xx + 3. * *qq2;
  *dsea = *xx + 4. * *qq2;
  *str  = *xx + 5. * *qq2;
  *chm  = *xx + 6. * *qq2;
  *bot  = *xx + 7. * *qq2;
  *top  = *xx + 8. * *qq2;
  *glu  = *xx + 9. * *qq2;
}

/* ================================================================ */
void ssmssm(float*  rdum1, float*  rdum2, float*  rdum3, 
	    float*  rdum4, float*  rdum5, float*  rdum6, 
	    float*  rdum7, float*  rdum8, float*  rdum9, 
	    float* rdum10, float* rdum11, float* rdum12, 
	    float* rdum13, float* rdum14, float* rdum15, 
	    float* rdum16, float* rdum17, float* rdum18, 
	    float* rdum19, float* rdum20, float* rdum21, 
	    float* rdum22, float* rdum23, float* rdum24, 
	    float* rdum25, int*    idum1, int*   idum2)
{
  if (_ssmssm) {
    (*_ssmssm)(rdum1,  rdum2,  rdum3,  rdum4,  rdum5,  rdum6, 
	       rdum7,  rdum8,  rdum9,  rdum10, rdum11, rdum12, 
	       rdum13, rdum14, rdum15, rdum16, rdum17, rdum18, 
	       rdum19, rdum20, rdum21, rdum22, rdum23, rdum24, 
	       rdum25, idum1, idum2);
    return;
  }
  pythia_message("Generic ssmssm function called\n");
  if (pythia_r(0) < 10) exit(1);
}

/* ---------------------------------------------------------------- */
void sugra(float* mzero, float* mhlf,  float* azero, 
	   float* tanb,  float* sgnmu, float* mtop, 
	   int*   imodl)
{
  if (_sugra) {
    (*_sugra)(mzero, mhlf, azero, tanb, sgnmu, mtop, imodl);
    return;
  }
  pythia_message("Generic sugra function called\n");
  if (pythia_r(0) < 10) exit(1);
}

/* ---------------------------------------------------------------- */
char* visaje()
{
  int i;
  static char name[41] = "";
  if (_visaje) return (*_visaje)();
  if (name[0] == '\0') {
    strcpy(name, "Undefined");
    for (i = 9; i < 40; i++) name[i] = ' ';
    name[40] = '\0';
  }
  return name;
}

/* ---------------------------------------------------------------- 
 * EOF
 */
