/*
* Copyright (C) 2001-2008, CompHEP COllaboration
* ------------------------------------------------------
* $Id: event_reader.c,v 1.5 2003/06/25 18:46:41 kryukov Exp $
*
* $Log: event_reader.c,v $
* Revision 1.5  2003/06/25 18:46:41  kryukov
* *** empty log message ***
*
* Revision 1.4  2003/06/17 11:15:26  sherstnv
* An error in memory usage has been fixed.
*
* Revision 1.3  2003/04/22 09:01:18  kryukov
* Apply indent to improve readability of files
*
* Revision 1.2  2003/04/20 17:55:02  kryukov
* Clean up warnings during make
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "service2/include/chep_limits.h"

#include "LesHouches.h"
#include "tag_reader.h"
#include "tag_parser.h"
#include "event_reader.h"

elementary_tag **
preparer (FILE * f)
{
  int n, i;
  int Nproc;
  tags *head;
  elementary_tag **format;
  string_comnd com;

  head = init_cap (1);
  cup_reader (f, head);

  strcpy (com.name, "Nproc");
  get_tag_with1com (0, head, "total", &com);
  Nproc = atoi (com.value);

  strcpy (com.name, "IDprocess");
  n = -1;
  for (i = 1; i <= Nproc; ++i) {
    sprintf (com.value, "%i", i);
    n = get_tag_with_exactcom (n + 1, head, "format", com);
    if (n != -1) {
      format[i] = head->tag[n];
    } else {
      fprintf (stderr, "***Error! The format tag for the %i-th subprocess has been lost.\n", i);
      exit (1);
    }
  }

  return format;
}

int getLHAevent (char fname[], FILE * source, long startpos, eventUP * evt) {
  int i;
  int go = 1;
  char buff[4096];

  int nup, idproc, id, stat;
  int moth1, moth2, color1, color2;
  double x, y, z, e, m, time, spin;
  double xwgt, aqed, aqcd, scale;

  fseek (source, startpos, SEEK_SET);

  if (!fgets (buff, 4096, source)) {
    if (feof (source)) return 1;
    fprintf (stderr, "comphep (error): %li - wrong position for LesHouches event in %s (no <event> start-tag)\n", startpos, fname);
    return -1;
  }
  if (!strcmp ("<event>", buff)) {
    fprintf (stderr, "comphep (error): %li - wrong position for LesHouches event in %s (no <event> start-tag)\n", startpos, fname);
    return -2;
  }

  fgets (buff, 4096, source);
  if (feof (source)) goto enderror;
  if (6 != sscanf (buff, "%i %i %lf %le %le %le", &nup, &idproc, &xwgt, &scale, &aqed, &aqcd)) {
    fprintf (stderr, "comphep (error): wrong LesHouches event format in %s \n", fname);
    return -3;
  }
  evt->NpartUP    = nup;
  evt->IDprocUP   = idproc;
  evt->XweightUP  = xwgt;
  evt->QEDalphaUP = aqed;
  evt->QCDalphaUP = aqcd;
  evt->QscaleUP   = scale;

  for (i = 0; i < nup; ++i) {
    fgets (buff, 4096, source);
    if (feof (source)) goto enderror;
    if (13 != sscanf (buff, "%i %i %i %i %i %i %le %le %le %le %le %le %le", &id, &stat, &moth1, &moth2, &color1, &color2, &x, &y, &z, &e, &m, &time, &spin)) {
      fprintf (stderr, "comphep (error): wrong LesHouches event format in %s \n", fname);
      return -4;
    }
    evt->IDpartUP[i]       = id;
    evt->statusUP[i]       = 3;
    evt->motherUP[0][i]    = moth1;
    evt->motherUP[1][i]    = moth2;
    evt->colorflowUP[0][i] = color1;
    evt->colorflowUP[1][i] = color2;

    evt->momentumUP[0][i] = x;
    evt->momentumUP[1][i] = y;
    evt->momentumUP[2][i] = z;
    evt->momentumUP[3][i] = e;
    evt->momentumUP[4][i] = m;
    evt->timelifeUP[i]    = time;
    evt->spinUP[i]        = spin;
  }

  while (go) {
    fgets (buff, 4096, source);
    if (feof (source)) goto enderror;
    if ('#' != buff[0]) go = 0;
  }

  if (!strcmp ("</event>", buff)) {
    fprintf (stderr, "comphep (error): no </event> end-tag and no #-comment in %s\n", fname);
    return -5;
  }

  return 0;
enderror:
    fprintf (stderr, "comphep (error): unexpected end of LHE event file %s \n", fname);
    return -6;
}

int setLHAevent (char fname[], FILE * s, long startpos, eventUP * evt) {
  int i;
  int stat = 0;

  fseek (s, startpos, SEEK_SET);
  fprintf (s, "<event>\n");
  fprintf (s, "%i %i %17.10E %17.10E %17.10E %17.10E", 
    evt->NpartUP, evt->IDprocUP, evt->XweightUP, evt->QscaleUP, evt->QEDalphaUP, evt->QCDalphaUP);

  for (i = 0; i < evt->NpartUP; ++i) {
    fprintf (s, "%i %i %i %i %i %i %17.10E %17.10E %17.10E %17.10E %17.10E %17.10E %17.10E\n", 
    evt->IDpartUP[i], 
    stat, 
    evt->motherUP[0][i], 
    evt->motherUP[1][i], 
    evt->colorflowUP[0][i], 
    evt->colorflowUP[1][i], 
    evt->momentumUP[0][i], 
    evt->momentumUP[1][i], 
    evt->momentumUP[2][i], 
    evt->momentumUP[3][i], 
    evt->momentumUP[4][i], 
    evt->timelifeUP[i], 
    evt->spinUP[i]);
  }
  fprintf (s, "</event>\n");
}
