Back to home page

EIC code displayed by LXR

 
 

    


Warning, /tutorial-simulations-using-ddsim-and-geant4/_episodes/01-single-particle-simulations.md is written in an unsupported language. File is not indexed.

0001 ---
0002 title: "Single Particle Simulations with `ddsim`"
0003 teaching: 30
0004 exercises: 20
0005 questions:
0006 - "How can I simulate single particles for detetor studies?"
0007 objectives:
0008 - "Know where to find the available options for `ddsim`."
0009 - "Understand the differences between steering files and command line options."
0010 - "Know what the key output file collections are."
0011 keypoints:
0012 - "`ddsim` can be used with a particle gun to generate single particle simulations."
0013 ---
0014 In this first episode we will go through the running of single particle events with `ddsim`, using the built-in event generator of `ddsim`. This is the quickest way so run some straightforward tests of the geometry and produce output hits in the detectors for further analysis.
0015 
0016 ## Passing options to `ddsim`
0017 
0018 The program `ddsim` is part of the DD4hep installation when it is compiled with Geant4 support. In the EIC standard environment `eic-shell` it is available and used for many simulations in the suite of continuous integration and benchmarking checks for the geometry. Simply entering `ddsim --help` will show the large wealth of options that can be passed to `ddsim` on the command line.
0019 
0020 It is also possible to use a python steering file to control the simulations. This is often a more convenient approach for sharing settings with others or archiving them for later reference or to iteratively add functionality. However, it should be noted that this can also lead to divergence in 'default' running conditions when these are not propagated to other environments.
0021 
0022 > Exercise:
0023 > - Consult the avialable options of `ddsim` by passing it the `--help` flag.
0024 > - Find the appropriate option to specify the output file when using `ddsim`.
0025 > - Use the `--dumpSteeringFile` flag to print out a default steering file, and redirect it into a file `steering.py`.
0026 {: .challenge}
0027 
0028 ## Available options in `ddsim`
0029 
0030 There are several option blocks that can be used with `ddsim`. Those are best looked at in the `steering.py` file, where additional documentation is added.
0031 - `SIM.action.*` or `--action.*` options can be used to tune sensitive detector actions,
0032 - `SIM.field.*` or `--field.*` options affect the magnetic field steppers,
0033 - `SIM.filter.*` or `--filter.*` options can add filters to sensitive detectors,
0034 - `SIM.gun.*` or `--gun.*` options can set single particle gun settings,
0035 - `SIM.physics.*` or `--physics.*` options allow setting the physics list,
0036 - `SIM.random.*` or `--random.*` options can be used to fix the random seed.
0037 
0038 Some options, such as `SIM.physics.setupUserPhysics`, take as argument a python function, so they can only be used inside the python steering files. We will come back to this later.
0039 
0040 When using the steering file approach, it is often useful to remove all options which you will not change (this allows you to take advantage of updates to the `ddsim` command itself without being stuck on old default settings). In this case, you would simply start from a steering file that only contains:
0041 ```python
0042 from DDSim.DD4hepSimulation import DD4hepSimulation
0043 from g4units import mm, GeV, MeV
0044 SIM = DD4hepSimulation()
0045 ```
0046 and which can be passed to `ddsim` with the `--steeringFile` flag:
0047 ```console
0048 $ ddsim --steeringFile steering.py
0049 ```
0050 This steering file merely sets up the simulation object that can then be configured with settings that deviate from the default.
0051 
0052 > Exercise:
0053 > - Compose a 'minimal' steering file that only contains the required header line.
0054 > - Attempt to run this steering file and note what `ddsim` claims is missing (we will specify this on the command line next).
0055 {: .challenge}
0056 
0057 ## Running a first single-particle simulation
0058 
0059 When we used the minimal steering file, `ddsim` pointed out that we did not specify the 'geometry compact file', nor the number of events and source of those events. In this sections we'll specify the geometry and tell `ddsim` to use a particle gun.
0060 
0061 The compact file is the entry point of our geometry, for which we must load the geometry environment first
0062 ```console
0063 $ source /opt/detector/setup.sh
0064 $ ddsim --steeringFile steering.py --compactFile $DETECTOR_PATH/$DETECTOR_CONFIG.xml
0065 ```
0066 
0067 Next, we will specify that we want `ddsim` to use the DD4hep particle gun, with `--enableGun` (or `-G`), and that we want 10 events, with `--numberOfEvents 10` (or `-N 10`):
0068 ```console
0069 $ ddsim --steeringFile steering.py --compactFile $DETECTOR_PATH/$DETECTOR_CONFIG.xml -G -N 10
0070 ```
0071 
0072 > Note: If you are running `eic-shell` directly on cvmfs, this may take a little while the first time. All large Geant4 data files are accessed for the first time and need to be retrieved.
0073 {: .callout}
0074 
0075 When we run `ddsim`, by default it prints out some information for each generated event:
0076 ```console
0077 GenerationInit   INFO  +++ Initializing event 1. Within run:0 event 1.
0078 Gun              INFO  Particle [0] mu-          Mom:10.000 GeV vertex:( 0.000  0.000  0.000)[mm] direction:( 0.000  0.000  1.000)
0079 Gun              INFO  Shoot [0] 10.000 GeV mu- pos:(0.000 0.000 0.000)[mm] dir:( 0.000  0.000  1.000)
0080 Gun              INFO  +-> Interaction [0] 10.000 GeV mu- pos:(0.000 0.000 0.000)[mm]
0081 Gun              INFO  +++   +-> ID:  0 mu-          status:00000002 PDG:    13 Vtx:(+0.00e+00,+0.00e+00,+0.00e+00)[mm] time: +0.00e+00 [ns] #Dau:  0 #Par:0      
0082 PrimaryHandler   INFO  +++++ G4PrimaryVertex at (+0.00e+00,+0.00e+00,+0.00e+00) [mm] +0.00e+00 [ns]
0083 ParticleHandler  INFO  +++ Event 0 Begin event action. Access event related information.
0084 ```
0085 
0086 You will notice that the particle gun has reverted to a default particle in a default direction: a 10 GeV muon in the positive z direction. That is, of course, not going to result in many hits in our ePIC detector... We will now take a closer look at some of the particle gun options.
0087 ```console
0088   --gun.energy GUN.ENERGY
0089   --gun.particle GUN.PARTICLE
0090   --gun.multiplicity GUN.MULTIPLICITY
0091   --gun.phiMin GUN.PHIMIN
0092                         Minimal azimuthal angle for random distribution
0093   --gun.phiMax GUN.PHIMAX
0094   --gun.thetaMin GUN.THETAMIN
0095   --gun.thetaMax GUN.THETAMAX
0096   --gun.momentumMin GUN.MOMENTUMMIN
0097                         Minimal momentum when using distribution (default = 0.0)
0098   --gun.momentumMax GUN.MOMENTUMMAX
0099   --gun.direction GUN.DIRECTION
0100                          direction of the particle gun, 3 vector 
0101   --gun.distribution {uniform,cos(theta),eta,pseudorapidity,ffbar}
0102                         choose the distribution of the random direction for theta
0103                         
0104                             Options for random distributions:
0105                         
0106                             'uniform' is the default distribution, flat in theta
0107                             'cos(theta)' is flat in cos(theta)
0108                             'eta', or 'pseudorapidity' is flat in pseudorapity
0109                             'ffbar' is distributed according to 1+cos^2(theta)
0110                         
0111                             Setting a distribution will set isotrop = True
0112                             
0113   --gun.isotrop GUN.ISOTROP
0114                          isotropic distribution for the particle gun
0115                         
0116                             use the options phiMin, phiMax, thetaMin, and thetaMax to limit the range of randomly distributed directions
0117                             if one of these options is not None the random distribution will be set to True and cannot be turned off!
0118                             
0119   --gun.position GUN.POSITION
0120                          position of the particle gun, 3 vector 
0121 ```
0122 
0123 While many of the options have straighforward names, others may be more confusing. The `gun.distribution` option is particularly relevant when we want to distribute single particle events over a range of angles. Depending on your needs, you may prefer one over the other, but in this tutorial we will simply use `cos(theta)` to throw uniformly on the unit sphere in the forward direction:
0124 ```console
0125 $ ddsim --steeringFile steering.py --compactFile $DETECTOR_PATH/$DETECTOR_CONFIG.xml -G -N 10 --gun.thetaMin "3*deg" --gun.thetaMax "45*deg" --gun.distribution "cos(theta)" --gun.momentumMin "1*GeV" --gun.momentumMax "10*GeV" --gun.particle "pi+"
0126 ```
0127 
0128 > Note: Avoid the use of the `gun.energy` option, since it is inherently more ambiguous than `gun.momentum` for massive particles (in the context of EIC).
0129 {: .callout}
0130 
0131 Note how we pass arguments with units in this example. The double quotes are in many cases necessary on the command line to avoid having the `*` be expanded by your shell. In the python steering file they can be ommitted and one can simply write, e.g., `SIM.gun.thetaMin = 3*deg`.
0132 
0133 When running this simulation above, note the output on the command line
0134 ```console
0135 GenerationInit   INFO  +++ Initializing event 1. Within run:0 event 1.
0136 Gun              INFO  Particle [0] pi+          Mom:4.924 GeV vertex:( 0.000  0.000  0.000)[mm] direction:( 0.017  0.403  0.915)
0137 Gun              INFO  Shoot [0] 10.000 GeV pi+ pos:(0.000 0.000 0.000)[mm] dir:( 0.000  0.000  1.000)
0138 Gun              INFO  +-> Interaction [0] 10.000 GeV pi+ pos:(0.000 0.000 0.000)[mm]
0139 Gun              INFO  +++   +-> ID:  0 pi+          status:00000002 PDG:   211 Vtx:(+0.00e+00,+0.00e+00,+0.00e+00)[mm] time: +0.00e+00 [ns] #Dau:  0 #Par:0      
0140 PrimaryHandler   INFO  +++++ G4PrimaryVertex at (+0.00e+00,+0.00e+00,+0.00e+00) [mm] +0.00e+00 [ns]
0141 ParticleHandler  INFO  +++ Event 0 Begin event action. Access event related information.
0142 ```
0143 For historical reasons (which are being addressed), the relevant line is the one with `Particle [0]`.
0144 
0145 > Exercise:
0146 > - Simulate 10 events in the negative endcap, using an angle distribution that is uniform in `cos(theta)`, and with an electron multiplicity of 2.
0147 > - Add all gun options (but not the number of events) to the minimal steering file you constructed earlier and rename the steering file to `ee_1GeV_10GeV_EndcapN.py` and run the simulation again.
0148 > - Verify that the momentum ranges and angular ranges are correct in the output.
0149 {: .challenge}
0150 
0151 ## Output files
0152 
0153 Until now we have not bothered to check the output files (in case you were wondering and went exploring, you may have noticed that output went into `dummyOutput.slcio`). In this section we'll explore how to write output files.
0154 
0155 The command line option to use to specify the output file is the `--outputFile` option (or `SIM.outputFile` in the steering file). Depending on the extension of the output file, a specific output file format is chosen. The default output is in the slcio format, but we have standardized on the EDM4hep data model inside ROOT files. To choose this output file format, use a file extension `.edm4hep.root`. We could, for example, run the following command:
0156 ```console
0157 $ ddsim --steeringFile ee_1GeV_10GeV_EndcapN.py --compactFile $DETECTOR_PATH/$DETECTOR_CONFIG.xml --numberOfEvents 10 --outputFile ee_1GeV_10GeV_EndcapN_1e1.edm4hep.root
0158 ```
0159 > Note: The extension `.edm4hep.root` of the output file is important since `ddsim` infers the output file type from the extension.
0160 {: .callout}
0161 
0162 Let's take a look at the output file in ROOT (you can do this inside or outside the container, depending on your system and facility with opening ROOT browsers).
0163 ```console
0164 $ root -l ee_1GeV_10GeV_EndcapN_1e1.edm4hep.root
0165 root [1] .ls
0166 TFile**         ee_1GeV_10GeV_EndcapN_1e1.edm4hep.root  data file
0167  TFile*         ee_1GeV_10GeV_EndcapN_1e1.edm4hep.root  data file
0168   KEY: TTree    events;1        Events tree
0169   KEY: TTree    metadata;1      Metadata tree
0170   KEY: TTree    run_metadata;1  Run metadata tree
0171   KEY: TTree    evt_metadata;1  Event metadata tree
0172   KEY: TTree    col_metadata;1  Collection metadata tree
0173 ```
0174 All EDM4hep files will have the same structure. We focus here on the `events` tree (if you see `EVENT`, in capital letters, you will need to ensure that you are indeed using the `.edm4hep.root` extension).
0175 
0176 > Note: In a future version of `ddsim` the output file format will change slightly, and the various `metatadata` trees will not be there anymore.
0177 {: .callout}
0178 
0179 ### `MCParticles`
0180 
0181 Let's first focus on the `MCParticles` branch. This contains 'truth' information and exact Geant4 step output for selected tracks. It is filled independent of sensitive detectors defined in the geometry. It is most useful to analyze the initial state of the simulation (i.e. the final state of the event generator). In our case, the number of generated particles from the particle gun is always 2, which is what we expect to recover by looking at the `@MCParticles.size()` in the `events` tree:
0182 ```console
0183 root [2] events->Draw("@MCParticles.size()")
0184 ```
0185 Wait a minute. What happened here? The `MCParticles` branch contains more than just the generated events. To include only the generated particles that were 'thrown' into the detector, we can select those with `generatorStatus` equal to 1.
0186 ```console
0187 root [3] events->Draw("MCParticles.PDG","MCParticles.generatorStatus==1")
0188 ```
0189 Another approach which is sometimes useful to reduce the number of 'other' particles that are written is to pass the option `--part.minimalKineticEnergy "1*TeV"`. This restricts the particles that are written to those that have an energy higher than 1 TeV. Initial state event generator particles are exempt from this requirement and are always written.
0190 
0191 ### `*Hits`
0192 
0193 The second set of branches that are important, and that are used as input to the event reconstruction framework, are the `*Hits` branches. These branches include the hits in sensitive detectors: either tracker hits or calorimeter hits. You will notice that these two different types of hits contain slightly different information.
0194 
0195 > Exercise:
0196 > - Open the file `ee_1GeV_10GeV_EndcapN_1e1.edm4hep.root` which you just created in a local ROOT installation or online at [https://root.cern/js/latest](https://root.cern/js/latest).
0197 > - Plot the z component of the momentum for generated particles only and verify that this is indeed negative for particles going towards the negative endcap.
0198 > - Verify that the total number of entries is consistent with the multiplicity and number of events you have simulated.
0199 > - Plot the deposited energy of the hits in the endcap Ecal and compare the number of hits in the positive and negative endcaps.
0200 {: .challenge}
0201