/* 
   * Copyright (C) 1997, Slava Ilyin 
   * Copyright (C) 2001-2003, CompHEP Collaboration
   *------------------------------------------------------
   * $Id: rw_sess.c,v 1.8 2003/08/25 09:15:35 sherstnv Exp $
   *
   * $Log: rw_sess.c,v $
   * Revision 1.8  2003/08/25 09:15:35  sherstnv
   * Some changes in r/w of the session.dat file.
   * Bug fixed in the Width_scheme position
   *
   * Revision 1.7  2003/07/20 14:34:37  kryukov
   * Change width menu. Remove rwidth global vars.
   * gwidth=0(fix), 1(overall), 2(running).
   *
   * Revision 1.6  2003/06/25 18:46:41  kryukov
   * *** empty log message ***
   *
   * Revision 1.5  2003/04/22 09:01:19  kryukov
   * Apply indent to improve readability of files
   *
   * Revision 1.4  2003/04/20 17:55:02  kryukov
   * Clean up warnings during make
   *
 */
#include "chep_crt/include/crt_util.h"
#include "service2/include/tptcmac.h"
#include "service2/include/files.h"
#include "service2/include/drandXX.h"
#include "service2/include/paragraphs.h"
#include "out_ext.h"

#include "cut.h"
#include "kinaux.h"
#include "kininpt.h"
#include "regul.h"
#include "alphas2.h"
#include "histogram.h"
#include "n_comphep_.h"
#include "runVegas.h"
#include "strfun.h"
#include "subproc.h"
#include "events.h"
#include "rw_sess.h"


static int 
writeIntegral (FILE * f)
{
  fprintf (f, " %.17E %.17E %.17E %d %d %d", integral.s0, integral.s1, integral.s2,
	   integral.n_it, integral.old, (int) integral.nCallTot);
  return 0;
}

static int 
readIntegral (FILE * f)
{
  fscanf (f, " %lf %lf %lf %d %d %ld", &(integral.s0), &(integral.s1),
   &(integral.s2), &(integral.n_it), &(integral.old), &(integral.nCallTot));
  return 0;
}


/*  read-write the number of session *** */
static int 
wnsess_ (FILE * mode)
{
  fprintf (mode, "%d", nSess);
  return 0;
}

static int 
insess_ (void)
{
  nSess = 1;
  return 0;
}

static int 
rnsess_ (FILE * mode)
{
  fscanf (mode, "%d", &nSess);
  return 0;
}

/**********************************************/
/*           write process string             */
int 
wrtprc_ (void)
{
  char buff[10];
  int pos = 0;
  int i;

  for (i = 1; i <= nin_ + nout_; i++)
    {
      pinf_ (proces_1.nsub, i, buff, NULL);
      strcpy (proces_1.proces + pos, buff);
      pos += strlen (buff);
      if (i < nin_ + nout_)
	{
	  if (i == nin_)
	    {
	      strcpy (proces_1.proces + pos, " -> ");
	      pos += 4;
	    }
	  else
	    {
	      strcpy (proces_1.proces + pos, ",");
	      pos += 1;
	    }
	}
    }
  return 0;
}

static int 
i_prc__ (void)
{
  proces_1.nsub = 1;
  wrtprc_ ();
  return 0;
}

static int 
w_prc__ (FILE * mode)
{
  return fprintf (mode, "%d (%s)", proces_1.nsub, proces_1.proces);
}

static int 
r_prc__ (FILE * mode)
{
  shortstr proc_name;

  fscanf (mode, "%d (%[^)]", &proces_1.nsub, proc_name);
  if (proces_1.nsub > nprc_)
    return 2;
  wrtprc_ ();
  if (strcmp (proc_name, proces_1.proces) == 0)
    return 0;
  else
    return 2;
}


/* *  read-write MC parameters       *** */
static int 
w_mc__ (FILE * mode)
{
  fprintf (mode, "%dx%d", (int) mcintr_1.ncall0, mcintr_1.itmx0);
  return 0;
}


static int 
r_mc__ (FILE * mode)
{
  fscanf (mode, "%ldx%d", &mcintr_1.ncall0, &mcintr_1.itmx0);
  return 0;
}


static int 
w_mdl__ (FILE * mode)
{
  long i;
  double val;
  char name[10];

  fprintf (mode, "\n");
  for (i = 1; i <= nvar_; ++i)
    {
      vinf_ (i, name, &val);
      fprintf (mode, "%10s = %.15E\n", name, val);
    }
  return 0;
}


static int 
r_mdl__ (FILE * mode)
{
  static double val;
  long i;
  char name1[20], name2[20];

  for (i = 1; i <= nvar_; ++i)
    {
      if (2 != fscanf (mode, "%s = %lf", name1, &val))
	return 2;
      vinf_ (i, name2, NULL);
      if (strcmp (name1, name2))
	return 2;
      asgn_ (i, val);
    }
  return 0;
}

/* *  read-write IN-state parameters  *** */
static int 
w_in__ (FILE * mode)
{
  double sqrt_S;
  vinf_ (0, NULL, &sqrt_S);
  fprintf (mode, "\n  SQRT(S) %E\n  Rapidity(c.m.s) %E\n", sqrt_S, rapidity);
  wrt_sf__ (mode);
  return 0;
}


static int 
r_in__ (FILE * mode)
{
  double sqrt_S;

  fscanf (mode, "\n  SQRT(S) %lf\n  Rapidity(c.m.s) %lf\n", &sqrt_S, &rapidity);
  asgn_ (0, sqrt_S);
  rd_sf__ (mode);

  return 0;
}


static int 
saveRandom (FILE * f)
{
  fprintf (f, "%s\n", seedXX (NULL));
  return 0;
}

static int 
readRandom (FILE * f)
{
  char s[20];
  fscanf (f, "%s", s);
  seedXX (s);

  return 0;
}

static int 
w_Gwidth (FILE * f)
{
  fprintf (f, " %i\n", gwidth);
  return 0;
}

static int 
r_Gwidth (FILE * f)
{
  fscanf (f, " %i", &gwidth);
  return 0;
}

int 
w_sess__ (FILE * mode_)
{
  FILE *mode;
  rw_paragraph wrt_array[16] =
  {
    {"Subprocess", w_prc__},
    {"Session_number", wnsess_},
    {"Initial_state", w_in__},
    {"Physical_Parameters", w_mdl__},
    {"Width_scheme", w_Gwidth},
    {"Kinematical_scheme", wrtkin_},
    {"Cuts", wrtcut_},
    {"Regularization", wrtreg_},
    {"QCD", w_qcd__},
    {"Vegas_calls", w_mc__},
    {"Vegas_integral", writeIntegral},
    {"Distributions", wrt_hist},
    {"Events", saveEventSettings},
    {"Random", saveRandom},
    {"VEGAS_Grid", saveVegasGrid},
    {"MAX", saveEventMax}
  };

  if (nin_ == 1)
    wrt_array[2].rw_command = NULL;

  if (mode_ == NULL)
    {
      char fname[100];
      sprintf (fname, "%ssession.dat", outputDir);
      mode = fopen (fname, "w");
      if (mode == NULL)
	return 0;
    }
  else
    mode = mode_;

  if (mode_ == NULL)
    {
      writeParagraphs (mode, 16, wrt_array);
      fclose (mode);
    }
  else
    writeParagraphs (mode, 10, wrt_array);

  return 0;
}

int 
r_sess__ (FILE * mode_)
{
  FILE *mode;

  rw_paragraph rd_array[16] =
  {
    {"Subprocess", r_prc__},
    {"Session_number", rnsess_},
    {"Initial_state", r_in__},
    {"Physical_Parameters", r_mdl__},
    {"Width_scheme", r_Gwidth},
    {"Kinematical_scheme", rdrkin_},
    {"Cuts", rdrcut_},
    {"Regularization", rdrreg_},
    {"QCD", r_qcd__},
    {"Distributions", rdr_hist},
    {"Vegas_integral", readIntegral},
    {"Vegas_calls", r_mc__},
    {"Events", readEventSettings},
    {"Random", readRandom},
    {"VEGAS_Grid", readVegasGrid},
    {"MAX", readEventMax}
  };


  if (interpret)
    return 0;

  if (mode_ == NULL)
    {
      mode = fopen ("session.dat", "r");
      if (mode == NULL)
	return 0;
    }
  else
    mode = mode_;
  readParagraphs (mode, 17, rd_array);
  if (mode_ == NULL)
    fclose (mode);
  return 0;
}


void 
inipar_ (void)
{
  i_prc__ ();  /* zapisivaetsya structure process iz comphep output file*/
  insess_ ();  /* session number = 1 */
  stdkin_ ();
  i_qcd ();
  rapidity = 0;
  gwidth = 0;
}


void 
clearSession (void)
{
  cleartab (&cutTab);
  cleartab (&regTab);
  cleartab (&histTab);
  clearHists ();
}
