From 10104f122b2c0f56c6fa4f4ba7c20dbbae82568b Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 15:30:28 +0100 Subject: [PATCH 1/6] add example to validate merge --- test/src/examples.cxx | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/src/examples.cxx b/test/src/examples.cxx index fa11e7c7..258ccf14 100644 --- a/test/src/examples.cxx +++ b/test/src/examples.cxx @@ -167,6 +167,50 @@ 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 = fs::create_directory(thisExamplePath / "merge"); + options.nEvents = 1000; + + options.outputFile = mergeDirectory / "muons1.root"; + { + Application app; + app.Run(options); + } + + options.nEvents = 500; + options.outputFile = mergeDirectory / "muons2.root"; + { + Application app; + app.Run(options); + } + + options.nEvents = 200; + options.outputFile = mergeDirectory / "muons3.root"; + { + Application app; + app.Run(options); + } + + // run system command to merge files + const auto command = "restMergeFiles merge.root " + mergeDirectory.string(); + cout << "Running command: " << command << endl; + const auto result = system(command.c_str()); + EXPECT_EQ(result, 0); + + TRestRun run("merge.root"); + cout << "Number of entries: " << run.GetEntries() << endl; + EXPECT_EQ(run.GetEntries(), 1700); +} + TEST(restG4, Example_04_Muons_MT) { // cd into example const auto originalPath = fs::current_path(); From 6486349ec89bb0e317dac3a4761ef24bc1f2c268 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 15:36:00 +0100 Subject: [PATCH 2/6] update validation example --- test/src/examples.cxx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/src/examples.cxx b/test/src/examples.cxx index 258ccf14..ce837e37 100644 --- a/test/src/examples.cxx +++ b/test/src/examples.cxx @@ -206,9 +206,29 @@ TEST(restG4, MergeFiles) { const auto result = system(command.c_str()); EXPECT_EQ(result, 0); + int nEntries = 0; + { + TRestRun run("muons1.root"); + nEntries += run.GetEntries(); + } + { + TRestRun run("muons2.root"); + nEntries += run.GetEntries(); + } + { + TRestRun run("muons3.root"); + nEntries += run.GetEntries(); + } + + cout << "Computed number of entries: " << nEntries << endl; + TRestRun run("merge.root"); + auto metadata = (TRestGeant4Metadata*)run.GetMetadataClass("TRestGeant4Metadata"); cout << "Number of entries: " << run.GetEntries() << endl; - EXPECT_EQ(run.GetEntries(), 1700); + cout << "Number of primaries: " << metadata->GetNumberOfEvents() << endl; + + EXPECT_EQ(metadata->GetNumberOfEvents(), 1700); + EXPECT_EQ(run.GetEntries(), nEntries); } TEST(restG4, Example_04_Muons_MT) { From 9f3b72bb94ded3a4b015febd9cd6f53c26107142 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 16:42:31 +0100 Subject: [PATCH 3/6] better memory management --- include/Application.h | 6 ++++-- src/Application.cxx | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) 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..e3bf13ea 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() { delete fSteppingVerbose; } From d1e223658fd4781175348f2e99c9986d66946b78 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 17:05:58 +0100 Subject: [PATCH 4/6] fix merge validation --- test/src/examples.cxx | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/test/src/examples.cxx b/test/src/examples.cxx index ce837e37..048dc56d 100644 --- a/test/src/examples.cxx +++ b/test/src/examples.cxx @@ -177,51 +177,60 @@ TEST(restG4, MergeFiles) { options.rmlFile = "CosmicMuonsFromWall.rml"; // create "merge" directory - auto mergeDirectory = fs::create_directory(thisExamplePath / "merge"); + auto mergeDirectory = thisExamplePath / "merge"; + fs::create_directory(mergeDirectory); options.nEvents = 1000; options.outputFile = mergeDirectory / "muons1.root"; { - Application app; - app.Run(options); + 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"; { - Application app; - app.Run(options); + 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"; { - Application app; - app.Run(options); + string command = + "restG4 CosmicMuonsFromWall.rml -n " + to_string(options.nEvents) + " -o " + options.outputFile; + const auto result = system(command.c_str()); + EXPECT_EQ(result, 0); } - // run system command to merge files - const auto command = "restMergeFiles merge.root " + mergeDirectory.string(); - cout << "Running command: " << command << endl; - const auto result = system(command.c_str()); - EXPECT_EQ(result, 0); - int nEntries = 0; { - TRestRun run("muons1.root"); + TRestRun run(mergeDirectory / "muons1.root"); nEntries += run.GetEntries(); } { - TRestRun run("muons2.root"); + TRestRun run(mergeDirectory / "muons2.root"); nEntries += run.GetEntries(); } { - TRestRun run("muons3.root"); + 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"); auto metadata = (TRestGeant4Metadata*)run.GetMetadataClass("TRestGeant4Metadata"); cout << "Number of entries: " << run.GetEntries() << endl; From 3b0c5e590cb1b28d2090c4665ca392dad02f0de5 Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 17:30:13 +0100 Subject: [PATCH 5/6] remove delete --- src/Application.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application.cxx b/src/Application.cxx index e3bf13ea..b10fa026 100644 --- a/src/Application.cxx +++ b/src/Application.cxx @@ -546,4 +546,4 @@ void Application::ValidateOutputFile(const string& filename) const { } } -Application::~Application() { delete fSteppingVerbose; } +Application::~Application() = default; From fc35218d40b164f590f88207d4f3f16b7b1a959c Mon Sep 17 00:00:00 2001 From: Luis Antonio Obis Aparicio Date: Tue, 7 Mar 2023 21:13:22 +0100 Subject: [PATCH 6/6] fix segfault --- test/src/examples.cxx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/src/examples.cxx b/test/src/examples.cxx index 048dc56d..e2817ffc 100644 --- a/test/src/examples.cxx +++ b/test/src/examples.cxx @@ -232,7 +232,11 @@ TEST(restG4, MergeFiles) { EXPECT_EQ(result, 0); TRestRun run("merge.root"); - auto metadata = (TRestGeant4Metadata*)run.GetMetadataClass("TRestGeant4Metadata"); + 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;