The DetectorMessenger class cannot call a method in the DetectorConstruction class

Dear all,

  • I have written a method to get data from an external file in MyDetectorConstruction() class.
#include <fstream>
void MyDetectorConstruction::ReadFile(std::string fileName)
	G4cout << "Read file: " << fileName << G4endl;
	const int SZ = 100;
	char buf[SZ];
	phiAngles.clear();    // If called for the second time
	thetaAngles.clear();  // we won't have 402 positions...

	std::ifstream ifs;, std::ios::in);

	for (G4int i = 0; i < NS; i++)
		G4double phi, theta;

		/* Skip the "Axx" at the beginning of the line */
		for (G4int c = 0; c < 4; c++) ifs.get();

		ifs >> phi >> theta;
		ifs.getline(buf, SZ); // Next line

		phiAngles.push_back(phi * degree);
		thetaAngles.push_back(theta * degree);
	G4cout << "... done " << G4endl;
  • For convenience, I used MyDetectorMessenger class to get the filename and invoke the ReadFile() function.
	MyDetectorConstruction* detector)
	GammaKnifeDetector = detector;

	calorimeterDir = new G4UIdirectory("/gammaknife/");
	calorimeterDir->SetGuidance("Command to modify gamma knife simulation");

	calorimeterHelmetSizeCmd = new G4UIcmdWithAnInteger("/gammaknife/helmetSize", this);
	calorimeterHelmetSizeCmd->SetGuidance("Set helmet size(4, 8, 14, 18)");
	calorimeterHelmetSizeCmd->SetParameterName("Size", false);
	calorimeterHelmetSizeCmd->AvailableForStates(G4State_Idle, G4State_PreInit);

	calorimeterNumberOfSourceCmd = new G4UIcmdWithAnInteger("/gammaknife/NumberOfChannels", this);
	calorimeterNumberOfSourceCmd->SetGuidance("Set number of source (1, 30)");
	calorimeterNumberOfSourceCmd->SetParameterName("Number", false);
	calorimeterNumberOfSourceCmd->AvailableForStates(G4State_Idle, G4State_PreInit);

	loadAnglesCommand = new G4UIcmdWithAString("/gammaknife/loadAngles", this);
	loadAnglesCommand->SetParameterName("String", false);

	delete calorimeterDir;
	delete loadAnglesCommand;

void MyDetectorMessenger::SetNewValue(G4UIcommand* command, G4String newValue)
	if (command == calorimeterHelmetSizeCmd)
	else if (command == calorimeterNumberOfSourceCmd)
	else if (command == loadAnglesCommand)
  • But when I compile and open the GUI the program crashes and I think it cannot call the ReadFile() function and provide the information to build the geometry.
  • Can anyone help me to check my source code? Thank in advance.

What causes the crash? Is a script that calls the /gammaknife/loadAngles command being run, or is the main program calling the command directly at all? If neither of those apply, then it’s likely unrelated to the messenger. It’s best to run the application through a debugger to isolate the cause of the crash first.

Dear Sir, I called the /gammaknife/loadAngles command in the macro file and initialized the ReadFile() method in the constructor of DetectorConstruction but with 2 files with different names. The program still displays the normal graphics and it prints the filename given in the ReadFile() function, not the filename in the /gammaknife/loadAngles command.