// M Hentz, 2017 // This class reads in the input phase space file in a buffered manner. #include "FileReader.hh" #include "G4ThreeVector.hh" FileReader::FileReader( G4String filename ) :fBufferSize(10000) { fInputFile.open( filename.data(), std::ios::in ); G4cout << "----------------- Input ----------------" << G4endl; G4cout << "Generating primaries from \"" << filename << "\"" << G4endl; // Skipping first line G4String commentLine = ""; getline(fInputFile, commentLine); // uncomment to skip first line G4cout << " Skipped line:" << G4endl; G4cout << " " << commentLine << G4endl; G4cout << "----------------------------------------" << G4endl; G4cout << G4endl; } FileReader::~FileReader() { fInputFile.close(); } // Primary class constructor FileReader::Primary::Primary() {;} // Primary class destructor FileReader::Primary::~Primary() {;} FileReader::Primary* FileReader::GetPrimary() { // Read in primaries if the primary list is empty if( fPrimaryList.size() == 0 ){ // Read 100 primaries at a time for( int i = 0; i < fBufferSize; i++ ){ // getline() requires strings to fill G4String blank; G4String name; G4String _x, _y, _z; G4String _momx, _momy, _momz; G4String _energy; getline( fInputFile, blank, ',' ); getline( fInputFile, name, ',' ); getline( fInputFile, _x, ',' ); getline( fInputFile, _y, ',' ); getline( fInputFile, _z, ',' ); getline( fInputFile, _momx, ',' ); getline( fInputFile, _momy, ',' ); getline( fInputFile, _momz, ',' ); getline( fInputFile, _energy, '\n' ); // Parse strings to doubles G4double x, y, z; G4double momx, momy, momz; G4double energy; x = stod( _x ); y = stod( _y ); z = stod( _z ); momx = stod( _momx ); momy = stod( _momy ); momz = stod( _momz ); energy = stod( _energy ); // Define position and momentum direction vectors G4ThreeVector pos = G4ThreeVector( x, y, z ); G4ThreeVector mom = G4ThreeVector( momx, momy, momz ); // Generate primary particle FileReader::Primary* primary = new Primary(); primary->SetName( name ); primary->SetPosition( pos ); primary->SetMomentum( mom ); primary->SetEnergy( energy ); // Populate the primaries list fPrimaryList.push_back( primary ); } } // Get first element and delete it so it's not reused FileReader::Primary* primary = fPrimaryList.front(); fPrimaryList.pop_front(); return primary; }