Message: Problem with file input Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

Question Problem with file input 

Keywords: input file i/o read
Forum: Event and Track Management
Date: 19 Sep, 2005
From: Tim Cohen <tcohen@knology.net>

Hi, I am trying to use a file as input for my simulation. All file manipulation is done in the PrimaryGeneratorAction code. I open the file in the constructor. I then read one line of my input file into an array. I use this array to set my particleGun parameters. (see the attached code below).

The problem is that in the output file it appears that the second line of the input file (event 2), is the first event to be processed and output to the data file. Is the first line of an input data file ignored? Any comments or help you can offer would be much appreciated.

Thanks, Tim Cohen

#include "moon_PrimaryGeneratorAction.hh"

#include <iostream>
#include <fstream>

#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "globals.hh"
#include "G4ThreeVector.hh"

std::ifstream input_file;

moon_PrimaryGeneratorAction::moon_PrimaryGeneratorAction()
{
  G4int n_particle = 1;
  particleGun = new G4ParticleGun(n_particle);

  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
  G4String particleName;
  particleGun->SetParticleDefinition(particleTable-> FindParticle(particleName="mu+"));
  particleGun->SetParticleEnergy(1*GeV);

input_file.open("/Volumes/RAID/Users/tcohen/Desktop/input_test.dat",std::ios::binary|std::ios::in); }

moon_PrimaryGeneratorAction::~moon_PrimaryGeneratorAction()
{
  delete particleGun;
  input_file.close();
}

void moon_PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
  double data_array[6]; 
  int i;

  for(i = 0; i < 6; i ++) {
  input_file.read((char *)&(data_array[i]),sizeof(double));
  }

  std::cout << data_array[0] << data_array[1] << data_array[2] << data_array[3] << data_array[4] << data_array[5] << std::endl;

  G4ThreeVector q(data_array[0],data_array[1],data_array[2]);
  G4ThreeVector v(data_array[3],data_array[4],data_array[5]);

  particleGun->SetParticlePosition(q);
  particleGun->SetParticleMomentumDirection(v);

particleGun->GeneratePrimaryVertex(anEvent); }

Inline Depth:
 1 1
 All All
Outline Depth:
 1 1
 2 2
 All All
Add message: (add)

 Add Message Add Message
to: "Problem with file input"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]