diff --git a/include/Application.h b/include/Application.h index 0408982a..ac061293 100644 --- a/include/Application.h +++ b/include/Application.h @@ -37,15 +37,17 @@ void ShowUsage(); } // namespace CommandLineOptions +class G4VSteppingVerbose; + class Application { public: void Run(const CommandLineOptions::Options& options); - ~Application() = default; + ~Application(); private: SimulationManager fSimulationManager; - + G4VSteppingVerbose* fSteppingVerbose; void ValidateOutputFile(const std::string& outputFile) const; }; diff --git a/src/Application.cxx b/src/Application.cxx index 658b553a..b10fa026 100644 --- a/src/Application.cxx +++ b/src/Application.cxx @@ -380,7 +380,8 @@ void Application::Run(const CommandLineOptions::Options& options) { long seed = metadata->GetSeed(); CLHEP::HepRandom::setTheSeed(seed); - G4VSteppingVerbose::SetInstance(new SteppingVerbose(&fSimulationManager)); + fSteppingVerbose = new SteppingVerbose(&fSimulationManager); + G4VSteppingVerbose::SetInstance(fSteppingVerbose); #ifndef GEANT4_WITHOUT_G4RunManagerFactory auto runManagerType = G4RunManagerType::Default; @@ -544,3 +545,5 @@ void Application::ValidateOutputFile(const string& filename) const { exit(1); } } + +Application::~Application() = default; diff --git a/test/src/examples.cxx b/test/src/examples.cxx index fa11e7c7..e2817ffc 100644 --- a/test/src/examples.cxx +++ b/test/src/examples.cxx @@ -167,6 +167,83 @@ TEST(restG4, Example_04_Muons) { cout << "Number of entries: " << run.GetEntries() << endl; } +TEST(restG4, MergeFiles) { + // cd into example + const auto originalPath = fs::current_path(); + const auto thisExamplePath = examplesPath / "04.MuonScan"; + fs::current_path(thisExamplePath); + + CommandLineOptions::Options options; + options.rmlFile = "CosmicMuonsFromWall.rml"; + + // create "merge" directory + auto mergeDirectory = thisExamplePath / "merge"; + fs::create_directory(mergeDirectory); + options.nEvents = 1000; + + options.outputFile = mergeDirectory / "muons1.root"; + { + string command = + "restG4 CosmicMuonsFromWall.rml -n " + to_string(options.nEvents) + " -o " + options.outputFile; + const auto result = system(command.c_str()); + EXPECT_EQ(result, 0); + } + + options.nEvents = 500; + options.outputFile = mergeDirectory / "muons2.root"; + { + string command = + "restG4 CosmicMuonsFromWall.rml -n " + to_string(options.nEvents) + " -o " + options.outputFile; + const auto result = system(command.c_str()); + EXPECT_EQ(result, 0); + } + + options.nEvents = 200; + options.outputFile = mergeDirectory / "muons3.root"; + { + string command = + "restG4 CosmicMuonsFromWall.rml -n " + to_string(options.nEvents) + " -o " + options.outputFile; + const auto result = system(command.c_str()); + EXPECT_EQ(result, 0); + } + + int nEntries = 0; + { + TRestRun run(mergeDirectory / "muons1.root"); + nEntries += run.GetEntries(); + } + { + TRestRun run(mergeDirectory / "muons2.root"); + nEntries += run.GetEntries(); + } + { + TRestRun run(mergeDirectory / "muons3.root"); + nEntries += run.GetEntries(); + } + + cout << "Computed number of entries: " << nEntries << endl; + + // run system command to merge files + const auto command = + "root -b -q \"$REST_PATH/macros/geant4/REST_Geant4_MergeRestG4Files.C(\\\"merge.root\\\", \\\"" + + mergeDirectory.string() + "\\\")\""; + cout << "Running command: " << command << endl; + const auto result = system(command.c_str()); + EXPECT_EQ(result, 0); + + TRestRun run("merge.root"); + TRestGeant4Metadata* metadata = (TRestGeant4Metadata*)run.GetMetadataClass("TRestGeant4Metadata"); + if (metadata == nullptr) { + cerr << "TRestGeant4Metadata not found!" << endl; + exit(1); + } + cout << "Number of entries: " << run.GetEntries() << endl; + cout << "Number of primaries: " << metadata->GetNumberOfEvents() << endl; + + EXPECT_EQ(metadata->GetNumberOfEvents(), 1700); + EXPECT_EQ(run.GetEntries(), nEntries); +} + TEST(restG4, Example_04_Muons_MT) { // cd into example const auto originalPath = fs::current_path();