Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved GFlash and Geant4 fast simulation handling #933

Merged
merged 28 commits into from
Jul 19, 2022

Conversation

MarkusFrankATcernch
Copy link
Contributor

@MarkusFrankATcernch MarkusFrankATcernch commented Jul 19, 2022

BEGINRELEASENOTES
The fast simulation handling in Geant4 includes the GFlash interface
as implemented in <geant40-source>/source/parameterisations/gflash
and a fast simulation interface as provided by
<geant40-source>/source/processes/parameterisation

This MR allows to handle both mechanisms using the same callbacks
in the Geant4SensitiveActions using the callback specialization:

      /// GFLASH/FastSim interface: Method for generating hit(s) using the information of the fast simulation spot object.
      virtual bool processFastSim(const Geant4FastSimSpot* spot, G4TouchableHistory* history)  final;

in addition to the regular callback for full simulation:

      /// G4VSensitiveDetector interface: Method for generating hit(s) using the G4Step object.
      virtual bool process(const G4Step* step,G4TouchableHistory* history)  final;

The Geant4FastSimSpot is inspired by the G4GFlashSpot, but avoids the specialization towards GFlash
given that the G4FastHit and the G4GFlashEnergySpot are equivalent.
Together with the helper class Geant4FastSimHandler simple sensitive detector callbacks
can be implemented like:

    /// GFlash/FastSim interface: Method for generating hit(s) using the information of Geant4FastSimSpot object.
    template <> bool
    Geant4SensitiveAction<Geant4Tracker>::processFastSim(const Geant4FastSimSpot* spot,
							 G4TouchableHistory* /* hist */)
    {
      typedef Geant4Tracker::Hit Hit;
      Geant4FastSimHandler h(spot);
      Hit* hit = new Hit(h.trkID(), h.trkPdgID(), h.deposit(), h.track->GetGlobalTime());
      hit->cellID        = cellID(h.touchable(), h.avgPositionG4());
      hit->energyDeposit = h.deposit();
      hit->position      = h.avgPosition();
      hit->momentum      = h.momentum();
      hit->length        = 0e0;
      collection(m_collectionID)->add(hit);
      return true;
    }

Callbacks with this signature are called both for GFlash and the G4 fast simulation.

To simplify the user defined construction of fast simulation shower models
the helper class Geant4FastSimShowerModel is implemented as a Geant4Action
and hence allows to have options to be set by the user (including python).

As an illustration two fast simulation shower models were implemented in
<dd4hep>/DDG4/plugins/Geant4P1ShowerModel.cpp which were directly deduced
from the Geant4 example <geant4-source>/examples/extended/parameterisations/Par01.
Please see the source files for details.

To execute GFlash or fast simulation to simple python examples are provided
using simple silicon blocks as sensitive volumes:

  • GFlash <dd4hep-dir>/examples/ClientTests/scripts/SiliconBlockGFlash.py
  • Fast simulation <dd4hep-dir>/examples/ClientTests/scripts/SiliconBlockFastSim.py

To enable either the following actions are required:

  1. Enable fast simulation for certain particles in the physics list:
  phys = geant4.setupPhysics('FTFP_BERT')
  ph = DDG4.PhysicsList(kernel, str('Geant4FastPhysics/FastPhysicsList'))
  ph.EnabledParticles = ['e+', 'e-']
  ph.enableUI()
  phys.adopt(ph)

This step is identical for GFlash and fast simulation physics.
2. Enable the shower mode:

  # Mandatory model parameters
  model.RegionName = 'SiRegion'
  model.Material = 'Silicon'
  model.ApplicableParticles = ['e+', 'e-']
  model.Etrigger = {'e+': 0.1 * GeV, 'e-': 0.1 * GeV}
  model.Enable = True
  model.enableUI()

Here the options must be set according to the parameters required by the shower models.

The G4 fast simulation interface is only functional for Geant4 version >= 10.07.
For lower versions of Geant4 missing classes are stubbed to allow the compilation.

ENDRELEASENOTES

@MarkusFrankATcernch
Copy link
Contributor Author

@andresailer In the Geant4Sensitives: Is it a good idea to have G4Step as a "const" object?
I have not found any occurrence where this would be violated, but certain calls of the G4Step require write access
like e.g. SetNonIonizingEnergyDeposit or ClearLastStepFlag but I think this is only called by Geant4 internally.

@MarkusFrankATcernch MarkusFrankATcernch enabled auto-merge (rebase) July 19, 2022 11:50
auto-merge was automatically disabled July 19, 2022 12:50

Rebase failed

@andresailer
Copy link
Member

@MarkusFrankATcernch If it compiles without a const_cast, I don't see a reason not to leave it const. The SensitiveDetectors shouldn't modify the G4Step.

@MarkusFrankATcernch MarkusFrankATcernch merged commit 2837b53 into AIDASoft:master Jul 19, 2022
@MarkusFrankATcernch
Copy link
Contributor Author

@EnginEren
This MR is better to have fast sumlation in dd4hep using gflash or user defined shower model.
You are welcom to join the next DD4hep developers meeting.

@EnginEren
Copy link

Hi @MarkusFrankATcernch, thanks. I'll let you know when I have updates

@EnginEren
Copy link

Hi Markus, I cloned and build master version of dd4hep. Then I went to examples folder and tried to build (as written in README):

mkdir build
cd build
cmake ..
...
CMake Error at examples/ClientTests/CMakeLists.txt:216 (if):
  if given arguments:

    "VERSION_GREATER" "10.2.99"

  Unknown arguments specified

I'm currently lacking the knowledge how to run/test GFlash and FastSim

Cheers,
Engin

@MarkusFrankATcernch
Copy link
Contributor Author

MarkusFrankATcernch commented Jul 20, 2022

Hi Engin,
which Geant4 version do you use?
It must be at least 10.07 otherwise fast simulation is implemented differently.
Perhaps for lower versions the variable Geant4_VERSION is not specified and then

 if(Geant4_VERSION VERSION_LESS 10.7)

would fail.
I tested with 10.6 and there the check seems to work, because this is also tested in the github checks.

@EnginEren
Copy link

That's weird..

-bash-4.2$ geant4-config --version
10.7.2

it comes from LCG101

@MarkusFrankATcernch
Copy link
Contributor Author

MarkusFrankATcernch commented Jul 20, 2022

Hmm. I have also built against LCG 101. I get the same output, but it seems to build:

 > which cmake
/cvmfs/sft.cern.ch/lcg/views/LCG_101/x86_64-centos7-gcc11-dbg/bin/cmake
 > geant4-config --version
10.7.2
 > cmake --version
cmake version 3.20.0

CMake suite maintained and supported by Kitware (kitware.com/cmake).

I followed the build commands on the dd4hep page:

    cmake ${CMAKE_COMPILER_CHOICE} \
	  -DDD4HEP_USE_GEANT4=ON \
	  -DBoost_NO_BOOST_CMAKE=ON \
	  -DDD4HEP_USE_LCIO=OFF \
	  -DDD4HEP_USE_TBB=OFF \
	  -DBUILD_TESTING=ON \
	  -DROOT_DIR=${ROOTSYS} \
	  -DCMAKE_INSTALL_PREFIX=${DD4hepINSTALL}  \
	  -DCMAKE_BUILD_TYPE=Debug \
	  ../DD4hep;
    make -j 22;
    make -j 12 install;
    source ${DD4hepINSTALL}/bin/thisdd4hep.sh;

@EnginEren
Copy link

hmm.. I can also build the dd4hep. There is no problem.

Based on the user guide:

$ git clone https://github.com/AIDASoft/DD4hep.git
$ cd DD4hep
$ source /cvmfs/sft.cern.ch/lcg/views/LCG_101/x86_64-centos7-gcc11-opt/setup.sh
$ mkdir build
$ cd build
$ cmake -DDD4HEP_USE_GEANT4=ON -DBoost_NO_BOOST_CMAKE=ON -DDD4HEP_USE_LCIO=ON -DBUILD_TESTING=ON -DGeant4_DIR=G4INSTALL/lib64/Geant4-10.7.2 -DROOT_DIR=$ROOTSYS -DDD4HEP_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release ..
$ make
$ make install
$ cd ..
$ source bin/thisdd4hep.sh

but I cannot build the examples folder, based on the information on this README

@MarkusFrankATcernch
Copy link
Contributor Author

I continue then aftwards simply like this:

    source ${DD4hepINSTALL}/bin/thisdd4hep.sh;
    mkdir examples;
    export DD4hepExamplesINSTALL=......
    cd examples;
    cmake ${CMAKE_COMPILER_CHOICE} \
	  -DDD4HEP_USE_GEANT4=ON \
	  -DBoost_NO_BOOST_CMAKE=ON \
	  -DDD4HEP_USE_LCIO=OFF \
	  -DDD4HEP_USE_TBB=OFF \
	  -DBUILD_TESTING=ON \
	  -DROOT_DIR=$ROOTSYS \
	  -DDD4hep_DIR=${DD4hepINSTALL}  \
	  -DCMAKE_INSTALL_PREFIX=${DD4hepExamplesINSTALL}  \
	  -DCMAKE_BUILD_TYPE=Debug \
	  ../../DD4hep/examples;
    make -j 22;
    make -j 12 install;

This works fine for me.

vvolkl added a commit to vvolkl/lcgeo that referenced this pull request Jul 22, 2022
@wdconinc
Copy link
Contributor

wdconinc commented Aug 5, 2022

The SensitiveDetectors shouldn't modify the G4Step.

Some sensitive detectors, e.g. Geant4OpticalCalorimeterAction, kill the incident optical photon and absorb the energy completely. See e.g.
https://github.com/AIDASoft/DD4hep/blob/master/DDG4/plugins/Geant4SDActions.cpp#L312
The only reason why I think this doesn't fail on compilation is because Geant4OpticalCalorimeterAction is not used anywhere and probably no compilation is even attempted for this template specialization.

This is failing in our EIC code for the exact same reason (but in a PMT detector SD action that absorbs the optical photon).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants