//=//////////////////////////////////////////////////////////////////////
//=
//= creadparam            
//=
//= @file        creadparam.cxx
//= @desc        Reading of parameters file
//= @author      J C Gonzalez
//= @email       gonzalez@mppmu.mpg.de
//= @date        Thu May  7 16:24:22 1998
//=
//=----------------------------------------------------------------------
//=
//= Created: Thu May  7 16:24:22 1998
//= Author:  Jose Carlos Gonzalez
//= Purpose: Program for reflector simulation
//= Notes:   See files README for details
//=    
//=----------------------------------------------------------------------
//=
//= $RCSfile: creadparam.cxx,v $
//= $Revision: 1.1.1.1 $
//= $Author: harald $ 
//= $Date: 1999-11-05 11:59:34 $
//=
//=//////////////////////////////////////////////////////////////////////

// @T \newpage

//!@section Source code of |creadparam.cxx|.

/*!@"
  
  This section describes briefly the source code for the file
  |creadparam.cxx|. This file is very closely related to the file
  |readparams.cxx| from the |reflector| program. Actually, this later
  file was the ancestror of the file you are looking at.

  All the defines it uses are located in the file |creadparam.h|. In
  the first one we can see the definitions of the commands available
  for the parameters file. We describe these commands in a later
  section.
  
  @"*/

//!@subsection Includes and Global variables definition.

/*!@"
  
  All the defines are located in the file {\tt creadparam.h}.

  @"*/
 
//!@{

#include "creadparam.h"

//!@}

//!@subsection Definition of global variables.

/*!@"

  Here we define the global variables where the values from the
  parameters file are stored.
  
  @"*/

//!@{

static char Input_filename[PATH_MAX_LENGTH];  //@< input filename
static char Output_filename[PATH_MAX_LENGTH]; //@< output filename
static char Data_filename[PATH_MAX_LENGTH];   //@< data filename
static char DIAG_filename[PATH_MAX_LENGTH];  //@< data filename
static char ROOT_filename[PATH_MAX_LENGTH];   //@< data filename
static char CT_filename[PATH_MAX_LENGTH];     //@< name of the CT def. file
static int simulateNSB = TRUE;                //@< Will we simulate NSB?
static int anaPixels = -1;      //@< number of pixels for the analysis
static float meanNSB;           //@< NSB mean value (per pixel)
static float qThreshold;        //@< Threshold value
static float qTailCut;          //@< Tail Cut value
static int nIslandsCut;         //@< Islands Cut value
static int countIslands = TRUE; //@< Will we count the islands?
static long int Seeds[2]; 
static int *Skip;
static int nSkip=0;
static int Data_From_STDIN = FALSE;
static int Read_Phe = FALSE;
static int Read_Phe_All = FALSE;
static int Write_All_Images = FALSE;
static int Write_All_Data = FALSE;
static int Select_Energy = TRUE;
static float Select_Energy_le = 0.0;           //@< GeV
static float Select_Energy_ue = 100000.0;      //@< GeV
static float Trigger_Radius;
static int Set_Trigger_Radius=FALSE;
static float fCorrection;
static int Apply_Correction=FALSE;
//!@}

//!@subsection The function |readparam()|.

//!-----------------------------------------------------------
// @name  creadparam
//                                                
// @desc  read parameters from the stdin / parameters file
//
// @var   *filename  Name of the parameters file (NULL->STDIN)
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function

//!@{ 
void 
readparam(char * filename)
{
  char sign[] = GLUE_postp( PROGRAM, VERSION ); //@< initialize sign
  char line[LINE_MAX_LENGTH];    //@< line to get from the stdin
  char token[ITEM_MAX_LENGTH];   //@< a single token
  int i, j;                      //@< dummy counters
  ifstream ifile;

  // use cin or ifile (reading from STDIN or from parameters file?
  if ( filename != NULL )
    ifile.open( filename );

  // get signature
  if ( filename != NULL )
    ifile.getline(line, LINE_MAX_LENGTH);
  else
    cin.getline(line, LINE_MAX_LENGTH);
  line[strlen(SIGNATURE)] = '\0';
  strcpy(line, sign);
  if (strcmp(sign, SIGNATURE) != 0) {
    cerr << "ERROR: Signature of parameters file is not correct\n";
    cerr << '"' << sign << '"' << '\n';
    cerr << "should be: " << SIGNATURE << '\n';
    exit(1);
  }

  // loop till the "end" directive is reached
  int is_end = FALSE;
  while (! is_end) {          

    // get line from file or stdin
    if ( filename != NULL )
      ifile.getline(line, LINE_MAX_LENGTH);
    else
      cin.getline(line, LINE_MAX_LENGTH);

    // skip comments (start with '#')
    if (line[0] == '#')
      continue;

    // show user comments (start with '>')
    if (line[0] == '>') {
      cout << line << endl << flush;
      continue;
    }

    // look for each item at the beginning of the line
    for (i=0; i<=end_file; i++) 
      if (strstr(line, ITEM_NAMES[i]) == line)
        break;
        
    // if it is not a valid line, just ignore it
    if (i == end_file+1) {
      cerr << "Skipping unknown token in [" << line << "]\n";
      continue;
    }

    // case block for each directive
    switch ( i ) {

    case input_file:          //@< name of the input file
          
      // get the name of the input_file from the line
      sscanf(line, "%s %s", token, Input_filename);

      break;

    case output_file:         //@< name of the output file
          
      // get the name of the output_file from the line
      sscanf(line, "%s %s", token, Output_filename);

      break;

    case data_file:           //@< name of the data file
          
      // get the name of the data_file from the line
      sscanf(line, "%s %s", token, Data_filename);

      break;

    case diag_file:          //@< name of the DIAG file
          
      // get the name of the data_file from the line
      sscanf(line, "%s %s", token, DIAG_filename);
      cout << '[' << DIAG_filename << ']' << endl << flush;

      break;

    case root_file:          //@< name of the ROOT file
          
      // get the name of the data_file from the line
      sscanf(line, "%s %s", token, ROOT_filename);
      cout << '[' << ROOT_filename << ']' << endl << flush;

      break;

    case ct_file:             //@< name of the telescope file
          
      // get the name of the ct_file from the line
      sscanf(line, "%s %s", token, CT_filename);

      break;

    case nsb_on:              //@< simulate NSB?
          
      // we will simulate NSB
      simulateNSB = TRUE;
          
      break;

    case nsb_off:             //@< simulate NSB?
          
      // we will NOT simulate NSB
      simulateNSB = FALSE;
          
      break;

    case nsb_mean:            //@< value of <NSB> per pixel
          
      // get value of <NSB> (in photons)
      sscanf(line, "%s %f", token, &meanNSB);
      simulateNSB = TRUE;

      break;

    case ana_pixels:          //@< number of pixels for analysis
          
      // number of pixels for analysis
      sscanf(line, "%s %d", token, &anaPixels);

      break;

    case threshold:           //@< value of threshold for trigger (q0)
          
      // get value of threshold (in ph.e.)
      sscanf(line, "%s %f", token, &qThreshold);

      break;

    case tail_cut:            //@< value of tail_cut (t0)
          
      // get value of tail_cut (in ph.e.)
      sscanf(line, "%s %f", token, &qTailCut);

      break;

    case islands_on:          //@< DO count islands
          
      // DO count islands
      countIslands = TRUE;
          
      break;

    case islands_off:         //@< do NOT count islands
          
      // do NOT count islands
      countIslands = FALSE;
          
      break;

    case islands_cut:         //@< value of islands_cut (i0)
          
      // get value of islands_cut (in ph.e.)
      sscanf(line, "%s %d", token, &nIslandsCut);
      countIslands = TRUE;

      break;

    case seeds:               //@< values of seeds for random numbers
          
      // get seeds
      sscanf(line, "%s %ld %ld", token, &Seeds[0], &Seeds[1]);

      break;

    case skip:                //@< skip pathological showers
          
      // get showers to skip
      cin >> nSkip;
      Skip = new int[nSkip];
      for (j=0; j<nSkip; ++j) {
        cin >> Skip[j];
        cout << Skip[j] << endl << flush;
      }

      break;

    case data_from_stdin:     //@< to read data from stdin
          
      // change boolean value
      Data_From_STDIN = TRUE;

      break;

    case read_phe:            //@< to read PHE files
          
      // change boolean value
      Read_Phe = TRUE;

      break;

    case read_phe_all:        //@< to read PHE files from write_all_images
          
      // change boolean value
      Read_Phe_All = TRUE;

      break;

    case write_all_images:    //@< to write ALL the images
          
      // change boolean value
      Write_All_Images = TRUE;

      break;

    case write_all_data:      //@< to write single pixel data
          
      // change boolean value
      Write_All_Data = TRUE;

      break;

    case select_energy:       //@< value of islands_cut (i0)
          
      // get energy range
      sscanf(line, "%s %f %f", token, &Select_Energy_le, &Select_Energy_ue);
      Select_Energy = TRUE;

      break;

    case trigger_radius:      //@< set radius of trigger area 
          
      // get trigger radius
      sscanf(line, "%s %f", token, &Trigger_Radius);
      Set_Trigger_Radius = TRUE;

      break;

    case correction:          //@< apply a kind of correction to pixel values
          
      // get trigger radius
      sscanf(line, "%s %f", token, &fCorrection);
      Apply_Correction = TRUE;

      break;

    case end_file:            //@< end of the parameters file

      // show a short message
      is_end = TRUE;

      break;

    } // switch ( i ) 

  } // while (! is_end)

  // after the loop is finished, return to the main function
  return;
}
//!@}


//!-----------------------------------------------------------
// @name get_input_filename
//                                                
// @desc get name of the input file
//
// @return   Name of the Input file
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
char *
get_input_filename(void)
{
  return (Input_filename);
}
//!@}


//!-----------------------------------------------------------
// @name get_output_filename
//                                                
// @desc get name of the output file
//
// @return   Name of the Output file
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
char *
get_output_filename(void)
{
  return (Output_filename);
}
//!@}


//!-----------------------------------------------------------
// @name get_data_filename
//                                                
// @desc get name of the data file
//
// @return   Name of the Data file
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
char *
get_data_filename(void)
{
  return (Data_filename);
}
//!@}


//!-----------------------------------------------------------
// @name get_diag_filename
//                                                
// @desc get name of the diagnostic output file
//
// @return   Name of the DIAG file
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
char *
get_diag_filename(void)
{
  return (DIAG_filename);
}
//!@}


//!-----------------------------------------------------------
// @name get_root_filename
//                                                
// @desc get name of the ROOT file
//
// @return   Name of the ROOT file
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
char *
get_root_filename(void)
{
  return (ROOT_filename);
}
//!@}



//!-----------------------------------------------------------
// @name get_ct_filename
//                                                
// @desc get name of CT definition file
//
// @return   Name of the CT definition file
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
char *
get_ct_filename(void)
{
  return (CT_filename);
}
//!@}


//!-----------------------------------------------------------
// @name get_nsb
//                                                
// @desc are we going to simulate NSB ?
//
// @var  *n  Mean value for the NSB (ph.e./pixel)
// @return   TRUE: we'll simulate NSB; FALSE: we won't
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_nsb(float *n)
{
  *n = meanNSB;
  return ( simulateNSB );
}
//!@}


//!-----------------------------------------------------------
// @name get_threshold   
//                                                
// @desc get threshold value
//
// @return   Value of the threshold q$_0$
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
float 
get_threshold(void)
{
  return( qThreshold );
}
//!@}


//!-----------------------------------------------------------
// @name get_tail_cut   
//                                                
// @desc get tail cut value
//
// @return   Value for the Tail-cut
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
float 
get_tail_cut(void)
{
  return( qTailCut );
}
//!@}


//!-----------------------------------------------------------
// @name get_islands_cut
//                                                
// @desc are we going to count the islands ?
//
// @var  *n  Cut on islands number
// @return   TRUE: we'll count the islands; FALSE: we won't
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_islands_cut(int *n)
{
  *n = nIslandsCut;
  return ( countIslands );
}
//!@}


//!-----------------------------------------------------------
// @name get_ana_pixels
//                                                
// @desc number of pixels for the analysis
//
// @return  Number of pixels to use in the image parameters
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_ana_pixels(void)
{
  return ( anaPixels );
}
//!@}


//!-----------------------------------------------------------
// @name get_seeds
//                                                
// @desc are we going to count the islands ?
//
// @var  *n  Number of the seed
// @return   N-th random-number Seed
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
long int
get_seeds(int n)
{
  return ( Seeds[n] );
}
//!@}


//!-----------------------------------------------------------
// @name get_skip_showers
//                                                
// @desc get list of showers to skip
//
// @var *s1  Pointer to a vector of number of showers
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
void 
get_skip_showers( int *s )
{
  int i;
  for (i=0; i<nSkip; ++i)
    s[i] = Skip[i];
}
//!@}


//!-----------------------------------------------------------
// @name get_nskip_showers
//                                                
// @desc get number of showers to skip
//
// @return  Number of showers to be skipped
//
// @date Mon Sep 14 13:27:56 MET DST 1998
//------------------------------------------------------------
// @function 

//!@{
int 
get_nskip_showers( void )
{
  return( nSkip );
}
//!@}


//!-----------------------------------------------------------
// @name get_data_from_stdin
//                                                
// @desc get whether we will read the data from the STDIN
//
// @return  TRUE: we'll read data from STDIN; FALSE: we won't
//
// @date Wed Nov 25 13:21:00 MET 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_data_from_stdin(void)
{
  return ( Data_From_STDIN );
}
//!@}


//!-----------------------------------------------------------
// @name get_read_phe
//                                                
// @desc get whether we will read PHE files
//
// @return  TRUE: we'll read PHE files; FALSE: we won't
//
// @date Wed Nov 25 13:21:00 MET 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_read_phe(void)
{
  return ( Read_Phe );
}
//!@}


//!-----------------------------------------------------------
// @name get_read_phe_all
//                                                
// @desc get whether we will read PHE files, with write_all_images
//
// @return  TRUE: we'll do it; FALSE: we won't
//
// @date Wed Nov 25 13:21:00 MET 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_read_phe_all(void)
{
  return ( Read_Phe_All );
}
//!@}


//!-----------------------------------------------------------
// @name write_all_images
//                                                
// @desc write all images to .phe, even those without trigger
//
// @return  TRUE: we'll write everything; FALSE: we won't
//
// @date Wed Nov 25 13:21:00 MET 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_write_all_images(void)
{
  return ( Write_All_Images );
}
//!@}


//!-----------------------------------------------------------
// @name write_all_data
//                                                
// @desc write single pixel data to file .dat
//
// @return  TRUE: we'll write everything; FALSE: we won't
//
// @date Wed Nov 25 13:21:00 MET 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_write_all_data(void)
{
  return ( Write_All_Data );
}
//!@}


//!-----------------------------------------------------------
// @name get_select_energy                                      
//
// @desc return energy range allowed for showers from .phe file
//
// @var *le  Lower limit in the allowed energy range
// @var *ue  Lower limit in the allowed energy range
// @return  TRUE: we make selection on the energy; FALSE: we don't
//
// @date Wed Nov 25 13:21:00 MET 1998
//------------------------------------------------------------
// @function 

//!@{
int
get_select_energy(float *le, float *ue)
{
  *le = Select_Energy_le;
  *ue = Select_Energy_ue;
  return ( Select_Energy );
}
//!@}


//!-----------------------------------------------------------
// @name get_trigger_radius                                  
//
// @desc return the radius of the trigger area in the camera (if set)
//
// @var *radius  Radius of the trigger area in the camera
// @return  TRUE: we choose a given radius for the trigger area
//
// @date Fri May  7 11:07:43 MET DST 1999
//------------------------------------------------------------
// @function 

//!@{
int get_trigger_radius(float *radius)
{
  *radius = Trigger_Radius;
  return ( Set_Trigger_Radius );
}
//!@}


//!-----------------------------------------------------------
// @name get_trigger_radius                                  
//
// @desc return the radius of the trigger area in the camera (if set)
//
// @var *radius  Radius of the trigger area in the camera
// @return  TRUE: we choose a given radius for the trigger area
//
// @date Fri May  7 11:07:43 MET DST 1999
//------------------------------------------------------------
// @function 

//!@{
int get_correction(float *corr)
{
  *corr = fCorrection;
  return ( Apply_Correction );
}
//!@}


//=------------------------------------------------------------
//!@subsection Log of this file.

//!@{
//
// $Log: not supported by cvs2svn $
// Revision 1.2  1999/10/22 15:01:28  petry
// version sent to H.K. and N.M. on Fri Oct 22 1999
//
// Revision 1.1.1.1  1999/10/21 16:35:11  petry
// first synthesised version
//
// Revision 1.6  1999/03/15  14:59:08  gonzalez
// camera-1_1
//
// Revision 1.5  1999/03/02  09:56:12  gonzalez
// *** empty log message ***
//
// Revision 1.4  1999/01/14  17:32:41  gonzalez
// Added to camera the STDIN input option (data_from_input)
//
//!@}

//=EOF
