//###########################################
//
//
// Datei: rausch.cpp
//
// Funktion: Liest eine Rauschmessung ein und
//           analysiert diese mittels Histrogrammierung
//
// Autor: Oliver Schitthelm
//
//
//###########################################

#include <iostream>
#include <fstream>      // for file handling
#include <string>       // for string handling

const int ndata_max     = 1000; // maximum number of (x,y) pairs
                                //    allowed
int ndata         = 0;
double index [ndata_max + 1];
double x_data[ndata_max + 1];
double y_data[ndata_max + 1];


// bekannt von frueher: Einlesen einer Messreihe
int read_input_file(string filename)
{
  double i_file;    // index   found on file
  double x_file;    // x value found on file
  double y_file;    // y value found on file

  ndata = 0;

// define input stream "in", open input file

  ifstream in(filename.c_str()); // this can be understood only later...
  if (!in)
  {
    return 1;
  }

// read all data and store numbers in arrays

  while(1)
  {
    in >> i_file >> x_file >> y_file;
    if (in.eof()) break;
    ndata++;
    if(ndata <= ndata_max)
    {
      index [ndata] = i_file;
      x_data[ndata] = x_file;
      y_data[ndata] = y_file;
    }
  }

  if (ndata>ndata_max)
  {
    return 2;
  }

// close input stream

   in.close();

   return 0;
}


void rausch(){

  // erstmal vorgeplaenkel, kann gerne verstanden werden
  // ist aber nicht zwingend erforderlich
  cout << endl
       << " program   === noise analysis ===   version 1.0 "
       << endl;


  // zunaechst die Datei einlesen:
  string input_file;

  cout << " enter input file name: ";
  cin  >> input_file;
  cout << " input file = " << input_file << endl;

  int result = read_input_file(input_file);

  if(1 == result)
  {
    cout << " ERROR: missing or bad input file = "
         << input_file << endl;
    return 1;
  }
  else if (2 == result)
  {
    cout << " WARNING: too many input data pairs = "
         << ndata << endl;
    cout << "          only first " <<  ndata_max
         << " values used in fit " << endl;
    ndata = ndata_max;
  }

  // wenn alles richtig gemacht wurde finden sich in den
  // einzelnen Arrays:
  // index:  Durchnummerierung der Messereignisse, startend bei 1
  // x_data: Die Zeit bei der der Messwert genommen wurde
  // y_wert: Der gemessene Widerstand der Probe.

  // nun geht es darum die Werte fein darzustellen, lege dazu ein Histogramm an:

  // Hier wird das Histogramm vereinbart
  TH1D* noise = new TH1D( "noise", "Widerstand der Probe", 20, 167, 172);

  // Hier muss das Histogramm gefuellt werden

  // nachdem es gefuellt wurde soll ein Plot erzeugt werden.


}
