LDMX-Software / SimCore

Integration of Geant4 simulation framework into a more generalized event processing framework.
2 stars 0 forks source link

SD (and more!) cleanup #66

Closed tomeichlersmith closed 2 years ago

tomeichlersmith commented 2 years ago

This resolves #39 , #36 , and un-does #55 at the moment. I am working on updating this branch that has gone stale in order to re-align with the current trunk.

Why?

Dynamically loading SDs will make it much easier to specialize SDs for specific use cases in the future. I think this is a necessary code clean step before we can start effectively wrestling with updating the simulation for the DR geometry.

omar-moreno commented 2 years ago

How do you handle un-doing #55? That was put there to support LCDD so I'm assuming that's no longer possible?

tomeichlersmith commented 2 years ago

Yes - I was not planning on keeping the un-doing of #55 , I just wanted to commit something that was working. I am hoping to pull out the necessary changes to AuxInfoReader and then I won't need to undo #55 .

tomeichlersmith commented 2 years ago

Things are compiling and running well, now onto the arduous task of validating that these changes that are supposed to change any of the physics don't.

Edit: Needs a small change to DecayChildrenKeeper to compile, which makes me think that this probably isn't working as intended since it uses the particle map without the ancestry being fully traced.

diff --git a/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx b/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx
index a52c7ca..e1c8d81 100644
--- a/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx
+++ b/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx
@@ -10,7 +10,6 @@
 /*   SimCore   */
 /*~~~~~~~~~~~~~*/
 #include "SimCore/UserTrackInformation.h"
-#include "SimCore/UserTrackingAction.h"

 namespace biasing {
 namespace utility {
@@ -24,8 +23,7 @@ DecayChildrenKeeper::DecayChildrenKeeper(const std::string& name,
 DecayChildrenKeeper::~DecayChildrenKeeper() {}

 void DecayChildrenKeeper::PostUserTrackingAction(const G4Track* track) {
-  const auto& particle_map{
-    simcore::UserTrackingAction::getUserTrackingAction()->getTrackMap()->getParticleMap()};
+  const auto& particle_map{getCurrentParticleMap()};
   if (particle_map.find(track->GetParentID()) != particle_map.end()) {
     const int& parent_pdg{particle_map.at(track->GetParentID()).getPdgID()};
     for (const int& parent : parents_) {

Edit 2: Nevermind about DecayChildrenKeeper being broken. It is merely checking the PDG if a parent is being saved. It does not rely on the ParticleMap's ancestry using G4Track::GetParentID instead.

tomeichlersmith commented 2 years ago

Using SimCore/test/basic.py as a first testing and my tree-diff program from root-diff to deduce different branches.

Output:

LDMX_Events mismatched between files
== Branches Only in 'ldmx-sw/trunk/justSim_10_events.root' ==
TargetSimHits_test.id_
TargetSimHits_test.edep_
TargetSimHits_test.x_
TargetSimHits_test.y_
TargetSimHits_test.z_
TargetSimHits_test.time_
TargetSimHits_test.trackIDContribs_
TargetSimHits_test.incidentIDContribs_
TargetSimHits_test.pdgCodeContribs_
TargetSimHits_test.edepContribs_
TargetSimHits_test.timeContribs_
TargetSimHits_test.nContribs_
TrackerScoringPlaneHits_test.id_
TrackerScoringPlaneHits_test.layerID_
TrackerScoringPlaneHits_test.moduleID_
TrackerScoringPlaneHits_test.edep_
TrackerScoringPlaneHits_test.time_
TrackerScoringPlaneHits_test.px_
TrackerScoringPlaneHits_test.py_
TrackerScoringPlaneHits_test.pz_
TrackerScoringPlaneHits_test.energy_
TrackerScoringPlaneHits_test.x_
TrackerScoringPlaneHits_test.y_
TrackerScoringPlaneHits_test.z_
TrackerScoringPlaneHits_test.pathLength_
TrackerScoringPlaneHits_test.trackID_
TrackerScoringPlaneHits_test.pdgID_
== Branches with different content ==
EcalSimHits_test.x_
EcalSimHits_test.y_
EcalSimHits_test.z_
EventHeader.timestamp_.fSec
EventHeader.timestamp_.fNanoSec
tomeichlersmith commented 2 years ago

Alright, I've fixed up the differences except for the incident ID contribs for the TargetSimHits. It looks like the new design is catching another hit that has a non-primary incident particle where-as before it was just going up to the default primary.

Note I'm only comparing ten events right now. I will update with a larger 10k run.

@omar-moreno Do you think these are close enough to not care? @bryngemark Do y'all use the incident ID contribs for the TargetSimHits in any TS analyses?

target-sim-hits-incident-id-contribs

bryngemark commented 2 years ago

@bryngemark Do y'all use the incident ID contribs for the TargetSimHits in any TS analyses?

No, not that I'm aware of. (We're not really using contribs at all at the moment.) So far we've relied on truth info in the TS itself, not in the target.

tomeichlersmith commented 2 years ago

Cool, I'm going to make larger samples to make sure that is the only part changing, and if it is then I will call it validated :+1:

tomeichlersmith commented 2 years ago

Validation of SD Redesign

Comparison of sim-level information between trunk and 39-sd-cleanup of SimCore.

trunk

These samples were generated with the production container built from the latest commit to trunk 1c446d2

ldmx pull pro sha-1c446d24

39-sd-cleanup

These samples were generated with a local build using dev/latest.

Comparison

The comparison was done using the tree-diff executable from tomeichlersmith/root-diff repository. This lists the branches with different content on a bit-by-bit level. These developments are not intended to change the simulated physics at all, so I am working towards making the files differ only in their time stamps.

Sample Configs

They are listed with respect to ldmx-sw. All of these were run with the developments in SimCore and the attached updates to ldmx-sw/Biasing and match files generated with trunk as described above. Some did have slightly different incident ID contribs for the Target/TriggerPad sim hits. Downstream usage of these sim hits do not use the incident ID because that was designed for the calorimeters, so that difference is ignored.

ldmx-sw Changes

Below is a git diff of what needs to be changes in ldmx-sw/Biasing after these changes. All of the changes are simply updating the Biasing/test configs that had gone stale except the compilation-necessary change in DecayChildrenKeeper.

diff --git a/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx b/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx
index a52c7ca..6d8268e 100644
--- a/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx
+++ b/Biasing/src/Biasing/Utility/DecayChildrenKeeper.cxx
@@ -10,7 +10,7 @@
 /*   SimCore   */
 /*~~~~~~~~~~~~~*/
 #include "SimCore/UserTrackInformation.h"
-#include "SimCore/UserTrackingAction.h"
+#include "SimCore/G4User/TrackingAction.h"

 namespace biasing {
 namespace utility {
@@ -24,8 +24,7 @@ DecayChildrenKeeper::DecayChildrenKeeper(const std::string& name,
 DecayChildrenKeeper::~DecayChildrenKeeper() {}

 void DecayChildrenKeeper::PostUserTrackingAction(const G4Track* track) {
-  const auto& particle_map{
-    simcore::UserTrackingAction::getUserTrackingAction()->getTrackMap()->getParticleMap()};
+  const auto& particle_map{simcore::g4user::TrackingAction::get()->getTrackMap().getParticleMap()};
   if (particle_map.find(track->GetParentID()) != particle_map.end()) {
     const int& parent_pdg{particle_map.at(track->GetParentID()).getPdgID()};
     for (const int& parent : parents_) {
diff --git a/Biasing/test/ecal_db.py b/Biasing/test/ecal_db.py
index dc4c58b..1d128c6 100644
--- a/Biasing/test/ecal_db.py
+++ b/Biasing/test/ecal_db.py
@@ -3,6 +3,7 @@ p = ldmxcfg.Process('ecal_db')
 from LDMX.Biasing import eat
 from LDMX.SimCore import makePath
 from LDMX.Ecal import EcalGeometry
+from LDMX.Hcal import HcalGeometry
 p.sequence = [
     eat.dark_brem( 
         10., #MeV - mass of A'
@@ -10,5 +11,6 @@ p.sequence = [
         'ldmx-det-v12' , #name of geometry to use
         )
     ]
+p.maxTriesPerEvent = 1000
 p.maxEvents = 100
-p.outputFiles = [ '/tmp/ecal_db.root' ]
+p.outputFiles = [ 'ecal_db.root' ]
diff --git a/Biasing/test/ecal_pn.py b/Biasing/test/ecal_pn.py
index f6d51f1..be25175 100644
--- a/Biasing/test/ecal_pn.py
+++ b/Biasing/test/ecal_pn.py
@@ -4,6 +4,7 @@ p = ldmxcfg.Process('ecal_pn')
 from LDMX.Biasing import ecal
 from LDMX.SimCore import generators
 import LDMX.Ecal.EcalGeometry
+import LDMX.Hcal.HcalGeometry
 p.sequence = [
     ecal.photo_nuclear( 
         'ldmx-det-v12' , 
@@ -11,4 +12,5 @@ p.sequence = [
         )
     ]
 p.maxEvents = 1000
-p.outputFiles = [ '/tmp/ecal_pn.root' ]
+p.maxTriesPerEvent = 1000
+p.outputFiles = [ 'ecal_pn.root' ]
diff --git a/Biasing/test/energy_sort.py b/Biasing/test/energy_sort.py
index 0cafe1e..4133621 100644
--- a/Biasing/test/energy_sort.py
+++ b/Biasing/test/energy_sort.py
@@ -1,17 +1,17 @@
 from LDMX.Framework import ldmxcfg
 # create my process object
 p = ldmxcfg.Process( "test" )
+p.run = 9001
 p.maxEvents = 10
-p.outputFiles = [ "/tmp/energy_sort.root" ]
+p.outputFiles = [ "energy_sort.root" ]
 from LDMX.SimCore import simulator as sim
-from LDMX.Ecal import EcalGeometry
+import LDMX.Ecal.EcalGeometry
+import LDMX.Hcal.HcalGeometry
 mySim = sim.simulator( "mySim" )
 mySim.setDetector( 'ldmx-det-v12' )
-mySim.runNumber = 9001
 from LDMX.SimCore import generators as gen
 mySim.generators.append( gen.single_4gev_e_upstream_tagger() )
 mySim.description = 'Basic test Simulation'
-mySim.randomSeeds = [ 1 , 2 ]
 from LDMX.Biasing import util
 mySim.actions = [ util.PartialEnergySorter( 3000. ) ]
 p.sequence.append( mySim )
diff --git a/Biasing/test/target_db.py b/Biasing/test/target_db.py
index 88f81c5..e1cd2a1 100644
--- a/Biasing/test/target_db.py
+++ b/Biasing/test/target_db.py
@@ -2,7 +2,8 @@ from LDMX.Framework import ldmxcfg
 p = ldmxcfg.Process('target_db')
 from LDMX.Biasing import target
 from LDMX.SimCore import makePath
-from LDMX.Ecal import EcalGeometry
+import LDMX.Ecal.EcalGeometry
+import LDMX.Hcal.HcalGeometry
 p.sequence = [
     target.dark_brem( 
         10., #MeV - mass of A'
@@ -11,4 +12,5 @@ p.sequence = [
         )
     ]
 p.maxEvents = 1000
-p.outputFiles = [ '/tmp/target_db.root' ]
+p.maxTriesPerEvent = 1000
+p.outputFiles = [ 'target_db.root' ]
diff --git a/Biasing/test/target_en.py b/Biasing/test/target_en.py
index 6c99345..37f6481 100644
--- a/Biasing/test/target_en.py
+++ b/Biasing/test/target_en.py
@@ -3,6 +3,7 @@ p = ldmxcfg.Process('target_en')
 from LDMX.Biasing import target
 from LDMX.SimCore import generators
 import LDMX.Ecal.EcalGeometry
+import LDMX.Hcal.HcalGeometry
 p.sequence = [
     target.electro_nuclear( 
         'ldmx-det-v12' , 
@@ -10,4 +11,5 @@ p.sequence = [
         )
     ]
 p.maxEvents = 1000
-p.outputFiles = [ '/tmp/target_en.root' ]
+p.maxTriesPerEvent = 1000
+p.outputFiles = [ 'target_en.root' ]
diff --git a/Biasing/test/target_mumu.py b/Biasing/test/target_mumu.py
index cb7d664..6addac0 100644
--- a/Biasing/test/target_mumu.py
+++ b/Biasing/test/target_mumu.py
@@ -3,6 +3,7 @@ p = ldmxcfg.Process('target_mumu')
 from LDMX.Biasing import target
 from LDMX.SimCore import generators
 import LDMX.Ecal.EcalGeometry
+import LDMX.Hcal.HcalGeometry
 p.sequence = [
     target.gamma_mumu(
         'ldmx-det-v12' ,
@@ -10,4 +11,5 @@ p.sequence = [
         )
     ]
 p.maxEvents = 1000
-p.outputFiles = [ '/tmp/target_mumu.root' ]
+p.maxTriesPerEvent = 1000
+p.outputFiles = [ 'target_mumu.root' ]
diff --git a/Biasing/test/target_pn.py b/Biasing/test/target_pn.py
index c8d4fff..dde9e04 100644
--- a/Biasing/test/target_pn.py
+++ b/Biasing/test/target_pn.py
@@ -3,6 +3,7 @@ p = ldmxcfg.Process('target_pn')
 from LDMX.Biasing import target
 from LDMX.SimCore import generators
 import LDMX.Ecal.EcalGeometry
+import LDMX.Hcal.HcalGeometry
 p.sequence = [
     target.photo_nuclear( 
         'ldmx-det-v12' , 
@@ -10,4 +11,5 @@ p.sequence = [
         )
     ]
 p.maxEvents = 1000
-p.outputFiles = [ '/tmp/target_pn.root' ]
+p.maxTriesPerEvent = 1000
+p.outputFiles = [ 'target_pn.root' ]
tomeichlersmith commented 2 years ago

@omar-moreno @bryngemark I feel like this is fully validated, but I want to get confirmation before I merge such a large change. Any final thoughts/concerns?