#include "EventReadFromFile.h"

#include "Event.h"

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// read data from file "name"
EventReadFromFile::EventReadFromFile( const string& name ) {
  file = new ifstream( name.c_str(), ios::binary );
}


EventReadFromFile::~EventReadFromFile() {
  delete file;
}


// get an event
const Event* EventReadFromFile::get() {
  return readFile();
}


// read an event
const Event* EventReadFromFile::readFile() {

  int i;
  Event* ev;
  // try to read input file
  if ( file->read( reinterpret_cast<char*>( &i ), sizeof( i ) ) )
       ev = new Event( i );
  else return 0;

  // read number of particles
  int n;
  file->read( reinterpret_cast<char*>( &n ), sizeof( n ) );
  // read and store data for particles
  int j;
  float pt;
  float eta;
  float phi;
  int charge;
  int muonId;
  for ( j = 0; j < n; ++j ) {
    file->read( reinterpret_cast<char*>( &pt     ), sizeof( pt     ) );
    file->read( reinterpret_cast<char*>( &eta    ), sizeof( eta    ) );
    file->read( reinterpret_cast<char*>( &phi    ), sizeof( phi    ) );
    file->read( reinterpret_cast<char*>( &charge ), sizeof( charge ) );
    file->read( reinterpret_cast<char*>( &muonId ), sizeof( muonId ) );
    ev->add( pt, eta, phi, charge, muonId );
  }

  return ev;

}

