Files
musrsim/src/musrParameters.cc
T

169 lines
8.0 KiB
C++

/***************************************************************************
* musrSim - the program for the simulation of (mainly) muSR instruments. *
* More info on http://lmu.web.psi.ch/simulation/index.html . *
* musrSim is based od Geant4 (http://geant4.web.cern.ch/geant4/) *
* *
* Copyright (C) 2009 by Paul Scherrer Institut, 5232 Villigen PSI, *
* Switzerland *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
***************************************************************************/
#include "musrParameters.hh"
// #include "musrErrorMessage.hh" - musrErrorMessage class can not be used inside "musrParameters" constructor, because
// musrErrorMessage is crated later!
#include "CLHEP/Units/SystemOfUnits.h"
#include <charconv>
#include <sstream>
#include <string>
musrParameters::musrParameters(G4String steeringFileName)
{
pointerToParameters = this;
boolG4RegionRequested = false;
mySteeringFileName = steeringFileName;
myStopFileName = mySteeringFileName;
myStopFileName.replace(myStopFileName.length()-3,myStopFileName.length()-1,"stop");
myRandomNumberFileName = mySteeringFileName;
myRandomNumberFileName.replace(myRandomNumberFileName.length()-3,myRandomNumberFileName.length()-1,"rndm");
// Read in the parameters, which have to be known before the detector construction is run
// (and therefore the parameters can not be read in in the musrDetectorConstruction.cc class).
FILE *fSteeringFile=fopen(steeringFileName.c_str(),"r");
if (fSteeringFile==nullptr) {
G4cout<<"musrParameters::musrParameters: steeringFileName=\""<<steeringFileName
<<"\" not opened for some reason."<<G4endl;
G4cout << "S T O P F O R C E D" << G4endl;
exit(1);
}
G4cout<<"musrParameters::musrParameters: steeringFileName=\""<<steeringFileName<<"\" opened."<<G4endl;
char line[501];
G4int lineNumber = 0;
const auto reportParseError = [&](const G4String& message) {
G4cerr<<steeringFileName<<":"<<lineNumber<<": "<<message<<G4endl;
G4cerr<<"S T O P F O R C E D"<<G4endl;
fclose(fSteeringFile);
exit(1);
};
while (fgets(line,sizeof(line),fSteeringFile)) {
++lineNumber;
std::istringstream lineStream(line);
std::string command;
if (lineStream >> command) {
// First find out how many events will be generated (needs to be known at an early stage, if the
// field is to be set in steps):
if (command=="/run/beamOn") {
std::string eventCount;
if (!(lineStream >> eventCount)) {
reportParseError("/run/beamOn: expected an integer event count");
}
G4int numberOfEvents = 0;
const char* first = eventCount.data();
const char* last = first + eventCount.size();
const std::from_chars_result result = std::from_chars(first,last,numberOfEvents);
if (result.ec!=std::errc() || result.ptr!=last || numberOfEvents<0) {
reportParseError("/run/beamOn: invalid event count '"+eventCount+"'");
}
musrParameters::nrOfEventsToBeGenerated = numberOfEvents;
}
if (command.compare(0,5,"/gps/")==0) {
musrParameters::boolG4GeneralParticleSource = true;
G4cout<<"\n========================================================================"<<G4endl;
G4cout<<"musrParameters.cc: GPS (General Particle Source) requested in the macro."<<G4endl;
G4cout<<" GPS will be used instead of the primary generator action."<<G4endl;
}
// Now find some private parameters that need to be initialised at early stage
if (command=="/musr/ignore") continue;
if (command!="/musr/command") continue;
std::string operation;
if (!(lineStream >> operation)) {
reportParseError("/musr/command: expected an operation");
}
// if (strcmp(tmpString1,"G4GeneralParticleSource")==0){
// if (strcmp(tmpString2,"true")==0){ musrParameters::boolG4GeneralParticleSource = true; }
// }
if (operation=="G4OpticalPhotons") {
std::string enabled;
if (!(lineStream >> enabled) || (enabled!="true" && enabled!="false")) {
reportParseError("G4OpticalPhotons: expected 'true' or 'false'");
}
if (enabled=="true") musrParameters::boolG4OpticalPhotons = true;
}
if (operation=="G4OpticalPhotonsUnprocess") {
std::string enabled;
if (!(lineStream >> enabled) || (enabled!="true" && enabled!="false")) {
reportParseError("G4OpticalPhotonsUnprocess: expected 'true' or 'false'");
}
if (enabled=="true") musrParameters::boolG4OpticalPhotonsUnprocess = true;
}
if (operation=="region") {
boolG4RegionRequested = true;
}
if (operation=="construct" && boolG4RegionRequested) {
std::string volumeType;
std::string volumeName;
if (!(lineStream >> volumeType >> volumeName)) {
reportParseError("construct: expected a volume type and name");
}
G4cout<<"musrParameters.cc: User requests to construct a detector volume "<<volumeName<<G4endl;
G4cout<<" after a previous G4Region definition."<<G4endl;
G4cout<<" Perhaps not a crutial problem, but the execution will be stopped"<<G4endl;
G4cout<<" anyway just to be on the safe side. Please correct "<<steeringFileName<<" file"<<G4endl;
G4cout<<" such that G4Region is called only after all detector volumes have been created."<<G4endl;
G4cout<<" S T O P F O R C E D!"<<G4endl;
exit(1);
}
}
}
fclose(fSteeringFile);
}
musrParameters::~musrParameters() {}
musrParameters* musrParameters::pointerToParameters=nullptr;
musrParameters* musrParameters::GetInstance() {
return pointerToParameters;
}
G4String musrParameters::mySteeringFileName="Unset";
G4String musrParameters::myStopFileName="Unsetblablabla034tdk40928jfmfnakfh921djf02UNSET";
G4String musrParameters::myRandomNumberFileName="Unsetblablabla034tdk40928jfmfnakfh921djf02UNSED";
G4bool musrParameters::storeOnlyEventsWithHits=true;
G4int musrParameters::storeOnlyEventsWithHitInDetID=0;
G4double musrParameters::signalSeparationTime=100*CLHEP::nanosecond;
G4bool musrParameters::storeOnlyTheFirstTimeHit=false;
G4int musrParameters::storeOnlyEventsWithMuonsDecayedInDetID=0;
G4bool musrParameters::field_DecayWithSpin=false;
G4bool musrParameters::killAllElectrons=false;
G4bool musrParameters::killAllPositrons=false;
G4bool musrParameters::killAllGammas=false;
G4bool musrParameters::killAllNeutrinos=true;
G4bool musrParameters::boolG4GeneralParticleSource=false;
G4bool musrParameters::boolG4OpticalPhotons=false;
G4bool musrParameters::boolG4OpticalPhotonsUnprocess=false;
//cks G4bool musrParameters::includeMuoniumProcesses =true; // TS
//G4bool musrParameters::boolG4GeneralParticleSource=true;
G4int musrParameters::nrOfEventsToBeGenerated=0;
G4bool musrParameters::finiteRiseTimeInScintillator=false;
G4int musrParameters::maximumTimePerEvent=60;
G4int musrParameters::maximumNrOfStepsPerTrack=100000;