Warning, /tutorial-analysis/episodes/04-full_chain_analysis.md is written in an unsupported language. File is not indexed.
0001 ---
0002 title: "Full Chain Analysis"
0003 teaching: 15
0004 exercises: 10
0005 ---
0006
0007 ::::::::::::::::::::::::::::::::::::::::::::: questions
0008
0009 - How do I bring all of this together if I'm starting from scratch?
0010
0011 :::::::::::::::::::::::::::::::::::::::::::::
0012
0013 ::::::::::::::::::::::::::::::::::::::::::::: objectives
0014
0015 - Become familiar with the full analysis chain.
0016
0017 :::::::::::::::::::::::::::::::::::::::::::::
0018
0019 In this short session, we'll go through a brief run through of how we actually ended up with a file like the one we ran our script on before. There are 5 basic steps which we'll look at individually, and then combine together:
0020
0021 1. Generate an input file (typically hepmc, other formats are useable). This is usually from some external event generator.
0022 2. Afterburn the file and apply beam effects (might be skipped in some cases).
0023 3. Process the input through the simulation, DD4HEP (covered in depth in the [Simulations with npsim and Geant4 tutorial](https://eic.github.io/tutorial-simulations-using-npsim-and-geant4/)).
0024 4. Reconstruct the DD4HEP output with EICrecon (covered in depth in the [Reconstruction framework](https://eic.github.io/tutorial-jana2/) and [Reconstruction algorithms](https://eic.github.io/tutorial-reconstruction-algorithms/) tutorials).
0025 5. Analyse the EICrecon output with analysis script.
0026
0027 Note that for low level analyses, you could also directly analyse the DD4HEP output from step 3. You may also wish to consult [Holly's slides from the April 2024 software meeting for an overview](https://indico.cern.ch/event/1343984/contributions/5927492/attachments/2843633/4971409/tutorial_overview.pdf) of each of these steps and how they fit into this production chain.
0028
0029 ## Event Generator Input Files
0030
0031 I won't say too much on this since this strongly depends upon the channel you want to simulate and analyse. The files here will likely come from an external event generator, for example -
0032
0033 - PYTHIA
0034 - BeAGLE
0035 - DJANGOH
0036 - MILOU
0037 - eSTARlight
0038 - LaGER
0039 - DEMPgen
0040 - elSPectro
0041
0042 ... and may others. However, regardless of what you use, the output is likely some form of .hepmc file with event by event particle/vertex info. For example -
0043
0044 ::::::::::::::::::::::::::::::::::::::::::::: callout
0045
0046 ## Example HEPMC Event
0047
0048 An example event from a HEPMC file is shown below. In this example event, we have an input 5 GeV electron on a 41 GeV proton. We have one vertex and three outgoing particles, a scattered electron, a pion, and a neutron. In our header, we also have an event weight included.
0049
0050 ```text
0051 E 1 1 5
0052 U GEV MM
0053 A 0 weight 4.813926604168258e-07
0054 P 1 0 11 6.123233963758798e-16 0.000000000000000e+00 -4.999999973888007e+00 5.000000000000000e+00 5.109989488070365e-04 4
0055 P 2 0 2212 -0.000000000000000e+00 -0.000000000000000e+00 4.100000000000000e+01 4.101073462535657e+01 9.382720881600054e-01 4
0056 V -1 0 [1,2]
0057 P 3 -1 11 -6.872312444834133e-01 1.924351128807063e+00 -4.281657822517654e+00 4.744260534644128e+00 5.109989488070365e-04 1
0058 P 4 -1 211 1.042011265882083e+00 -1.600831989262599e+00 1.404460452649878e+00 2.374960954263115e+00 1.395701800000037e-01 1
0059 P 5 -1 2112 -3.547800213986697e-01 -3.235191395444645e-01 3.887719739597977e+01 3.889151313644933e+01 9.395654204998098e-01 1
0060 ```
0061
0062 :::::::::::::::::::::::::::::::::::::::::::::
0063
0064 Typically, we also need to incorporate beam effects. This is done via the use of the afterburner.
0065
0066 ## Applying Beam Effects - Afterburner
0067
0068 Afterburner applies beam effects to an existing hepmc file. These include effects due to the crabbing of the beam bunches and the crossing angle. Afterburner is pre-installed in eic-shell. We can run it via -
0069
0070 ```bash
0071 abconv
0072 ```
0073
0074 However, we'll need an input file to do anything, we can also check other options quickly with -
0075
0076 ```bash
0077 abconv -h
0078 ```
0079
0080 Note that when we run Afterburner, it will try to pick up the input beam energies and apply the relevant configuration. We can force a different configuration if we want (see the options from the help printout). We could for example though run -
0081
0082 ```bash
0083 abconv $File -o $OutputFilename
0084 ```
0085
0086 where $File is our input hepmc file from our generator, and $OutputFilename is whatever we want our output to be called.
0087
0088 Regardless of whether we want or need to afterburn the file, we can feed in our hepmc file to DD4HEP and process our events through the detector simulation.
0089
0090 ## Simulation
0091
0092 To process our events through the simulation, we need to get the detector geometry. The simplest way is simply to source the nightly build within eic-shell -
0093
0094 ```bash
0095 ./eic-shell
0096 source /opt/detector/epic-main/bin/thisepic.sh
0097 ```
0098
0099 We can check this worked as intended by checking that the DETECTOR_PATH variable is now set. Do so via -
0100
0101 ```bash
0102 ls $DETECTOR_PATH
0103 ```
0104
0105 If we do this without sourcing thisepic.sh, we should get an error. Now, we should see a range of .xml files (which outline various detector configurations). We could also compile our own version of the detector within eic-shell. You might want to do so if you are actively iterating on the design of specific detector for example. See the [GitHub page](https://github.com/eic/epic) for instructions.
0106
0107 We can now process a simulation. Be aware that this may take some time, so to test it, try processing a small number of events first. Check the options we can provide via -
0108
0109 ```bash
0110 npsim -h
0111 ```
0112
0113 A typical simulation command might look something like -
0114
0115 ```bash
0116 npsim --compactFile $DETECTOR_PATH/epic_craterlake.xml --numberOfEvents 1000 --inputFiles input.hepmc --outputFile output.edm4hep.root
0117 ```
0118
0119 Most of the arguments are pretty self explanatory. As a quick demo, I'll run -
0120
0121 ```bash
0122 npsim --compactFile $DETECTOR_PATH/epic_craterlake_5x41.xml --numberOfEvents 10 --inputFiles eic_DEMPgen_5on41_ip6_pi+_1B_1.hepmc --outputFile DEMPgen_5on41_pi+_10_TestOutput.edm4hep.root
0123 ```
0124
0125 When we run this, we'll get lots of printouts to screen, we can supress this by adding the -v5 argument too (only errors will be printed). Once we have our simulation output, we can now reconstruct our events.
0126
0127 ## Reconstruction
0128
0129 We can run eicrecon pretty straightforwardly, within eicshell, try -
0130
0131 ```bash
0132 eicrecon -h
0133 ```
0134
0135 which should again, print out the various options we have available. An example command to run the reconstruction on a file might look like this -
0136
0137 ```bash
0138 eicrecon -Ppodio:output_file=eicrecon_out.root -Pjana:nevents=1000 -Pdd4hep:xml_files=epic_craterlake.xml sim_output.edm4hep.root
0139 ```
0140
0141 Again, this might take a long time. So test a small sample of events first. Following up on my simulation demo, I'll run -
0142
0143 ```bash
0144 eicrecon -Ppodio:output_file=DEMPgen_5on41_pi+_10_TestReconOutput.edm4hep.root -Pjana:nevents=10 -Pdd4hep:xml_files=epic_craterlake_5x41.xml DEMPgen_5on41_pi+_10_TestOutput.edm4hep.root
0145 ```
0146
0147 eicrecon will look for the detector .xml file in $DETECTOR_PATH, so make sure the detector geometry is sourced before running eicrecon.
0148
0149 ## Combining Everything
0150
0151 Ok, great. We now have a file we could run our earlier analysis script on. But what if we wanted to do all of this from scratch? Well, the easiest way might be to put all of this in a shell script. So, pulling all of our commands together -
0152
0153 ```bash
0154 #! /bin/bash
0155
0156 source /opt/detector/epic-main/bin/thisepic.sh
0157 eval npsim --compactFile $DETECTOR_PATH/epic_craterlake.xml --numberOfEvents 1000 --inputFiles input.hepmc --outputFile output.edm4hep.root
0158 sleep 3
0159 eval eicrecon -Ppodio:output_file=eicrecon_out.root -Pjana:nevents=1000 -Pdd4hep:xml_files=epic_craterlake.xml sim_output.edm4hep.root
0160
0161 exit 0
0162 ```
0163
0164 I've premade a version of this with the commands I ran earlier, so we can run it and see what happens.
0165
0166 ## Farming
0167
0168 Ok, great. We can do (almost) all of the processes we need in one command. But as we've seen, the processing can take a while. Realistically, we're probably going to want to parallelise this in some way. With access to the JLab iFarm or the BNL systems (Condor). We can create and submit compute jobs for this purpose. This is getting a bit beyond the scope of this tutorial, but some things to consider -
0169
0170 - Our job needs to either access the container, or process eic-shell within the job (more on this below)
0171 - The job itself should be as simple as possible, just exectuing a command with some arguments. Our script above is a good candidate (with some work)
0172 - As is, our script is fairly inflexible. We should probably make things like the input and output file names variables that are set based upon arguments we provide.
0173 - We need to consider the resource usage of our job carefully.
0174 - Pathing can be tricky, we need to make sure the farm/compute node picks up the correct paths such as $DETECTOR_PATH (this is a common job error).
0175 - As always, TEST first. Run a small job that runs quickly interactively, THEN submit it is a small compute job. Compare the outputs.
0176
0177 For our first point, one easy (and not recommended for Condor jobs!) way to do this is via an EOF line -
0178
0179 ```bash
0180 #! /bin/bash
0181
0182 cat <<EOF | eic-shell
0183 ./Basic_Bash.sh
0184 EOF
0185
0186 exit 0
0187 ```
0188 This just starts eic-shell and runs our earlier script. We can run this WITHOUT running eic-shell first. Note that this is a bit of a cheat to address a pathing issue. $DETECTOR_PATH will be interpreted by the script BEFORE the EOF script so our variable will be mis-set. We can get around this by running a script. Ideally, for our compute job, we should probably also explicitly set our paths to eic-shell and the bash script in some way.
0189
0190 With changes like this made, we could then make a quick job and submit it. This is a bit beyond the tutorials, but for some farm examples, see [this job script](https://github.com/eic/snippets/blob/main/JLab_Farm_Scripts/JLab_Farming_Job.sh) and this [job submission script](https://github.com/eic/snippets/blob/main/JLab_Farm_Scripts/JLab_Farming.sh) I use as a template. Feel free to use these as a template for your own jobs, but please thoroughly read through and understand them before submitting a huge number of jobs. Keep the comments above in mind too.
0191
0192 Also a quick disclaimer, my experience in running jobs is limited to systems I know (which does not include the BNL systems). As such, I can't advise much beyond general slurm job style questions on BNL/Condor. I'm also aware that using EOF in scripts was not encouraged in BNL jobs, see [this discussion on mattermost](https://chat.epic-eic.org/main/pl/fo9954siwigyjckasrnd4xufxw) for more.
0193
0194 ## Warnings
0195
0196 Finally, a major disclaimer. **A lot of the time, you should NOT be starting from scratch and processing through the simulation and reconstruction yourself.** There are numerous reasons -
0197
0198 - Computing time intensive
0199 - Versioning errors/mismatch
0200 - Not as reproducible (if you find an error, people will need to try and reproduce it from your environment)
0201
0202 Where possible, use files from official simulation campaigns (bringing us full circle, see the first lesson for using a simulation campaign file in a script!). That being said, for testing and iterating rapidly on a design change, running small jobs yourself may be the way to go. It may also help you to understand the full process by seeing the steps involved.
0203
0204 ::::::::::::::::::::::::::::::::::::::::::::: keypoints
0205
0206 - There are a few steps to go through before we get to the file we analysed previously.
0207 - Good for testing, but use simulation campaign output where possible!
0208
0209 :::::::::::::::::::::::::::::::::::::::::::::