Back to home page

EIC code displayed by LXR

 
 

    


Warning, /detector_benchmarks/benchmarks/lowq2_reconstruction/Snakefile is written in an unsupported language. File is not indexed.

0001 SIMOUTDIR="sim_output/lowq2_reconstruction/"
0002 ANALYSISDIR=SIMOUTDIR+"analysis/"
0003 
0004 ##########################################################################################
0005 ### Rules for checking the reconstruction of the electron momentum
0006 ##########################################################################################
0007 
0008 # Filter LowQ2 events from xrootd server - Using the acceptance events didn't work
0009 # ToDo: Do a proper investigation into how the feature to target mapping isn't unique
0010 rule filter_hepmc_for_training:
0011     input:
0012         warmup="warmup.edm4hep.root",
0013         script=workflow.source_path("filterHEPMC3.py"),        
0014     output:
0015         SIMOUTDIR+"Low-Q2_Training_Events.hepmc3.tree.root",
0016     params:
0017         events="root://dtn-eic.jlab.org//volatile/eic/EPIC/EVGEN/SIDIS/pythia6-eic/1.0.0/18x275/q2_0to1/pythia_ep_noradcor_18x275_q2_0.000000001_1.0_run1.ab.hepmc3.tree.root",
0018     singularity: EIC_SINGULARITY_CONTAINER,
0019     shell:
0020         """
0021         # Ensure ROOT 6.40.00-02 finds ROOT.modulemap
0022         export ROOT_INCLUDE_PATH="$(root-config --incdir)${{ROOT_INCLUDE_PATH:+:$ROOT_INCLUDE_PATH}}"
0023         export LD_LIBRARY_PATH="$(root-config --libdir)${{LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}}"
0024         # FIXME: Work around a ROOT upstream bug in progress; preloading libCore.so.6.40
0025         # forces resolution to the non-patch-level soname so ROOT.modulemap is found correctly.
0026         # With a single ROOT install in these containers, this should remain harmless after
0027         # upgrades away from ROOT 6.40.
0028         export LD_PRELOAD="${{LD_PRELOAD:+$LD_PRELOAD:}}/opt/local/lib/root/libCore.so.6.40"
0029         python {input.script} --outFile {output} --inFile {params.events}
0030         """
0031 
0032 # Run pythia6 Low-Q2 events through the simulation
0033 rule lowq2_training_sim:
0034     input:
0035         warmup="warmup.edm4hep.root",
0036         events=SIMOUTDIR+"Low-Q2_Training_Events.hepmc3.tree.root",
0037         geometry_lib=find_epic_libraries(),
0038     output:
0039         SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0040     params:
0041         DD4HEP_HASH=get_spack_package_hash("dd4hep"),
0042         NPSIM_HASH=get_spack_package_hash("npsim"),
0043     cache: True
0044     singularity: EIC_SINGULARITY_CONTAINER,
0045     shell:
0046         """
0047             exec npsim \
0048             --runType run \
0049             --random.seed 1 \
0050             --inputFiles {input.events} \
0051             --numberOfEvents 1000000 \
0052             --compactFile $DETECTOR_PATH/epic_ip6_extended.xml \
0053             --printLevel WARNING \
0054             --outputFile {output} \
0055             --physics.rangecut 100*m 
0056         """
0057 
0058 # Run EICrecon to create TaggerTrackerReconstructedParticles
0059 rule lowq2_reconstruction_particles_eicrecon:
0060     params:
0061         xml=os.getenv("DETECTOR_PATH")+"/epic_ip6_extended.xml",
0062         output_collections="TaggerTrackerReconstructedParticles,MCParticles,EventHeader",
0063         ignore_plugins="janatop,LUMISPECCAL,ECTOF,BTOF,FOFFMTRK,RPOTS,B0TRK,MPGD,ECTRK,DRICH,DIRC,pid,tracking,EEMC,BEMC,FEMC,EHCAL,BHCAL,FHCAL,B0ECAL,ZDC,BTRK,BVTX,PFRICH,richgeo,evaluator,pid_lut,reco,rootfile",
0064         EICRECON_HASH=get_spack_package_hash("eicrecon"),
0065     input:
0066         data=SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0067     output:
0068         eicreconfile=ANALYSISDIR+"Low-Q2_test_Particles_{CAMPAIGN}.eicrecon.edm4hep.root",
0069     cache: True
0070     singularity: EIC_SINGULARITY_CONTAINER,
0071     shell:
0072         """
0073         eicrecon -Ppodio:output_file={output.eicreconfile} -PLOWQ2:TaggerTrackerTransportationPreML:beamE=18.0 -PLOWQ2:TaggerTrackerTransportationPostML:beamE=18.0 -Pdd4hep:xml_files={params.xml} \
0074         -Pjana:nevents=500000 \
0075         -Ppodio:output_collections={params.output_collections} -Pplugins_to_ignore={params.ignore_plugins}  {input.data} 
0076         """
0077 
0078 
0079 ##########################################################################################
0080 ### Rules for training new onnx reconstruction neural network
0081 ##########################################################################################
0082 
0083 # Run EICrecon and create input tensors for reconstruction.
0084 rule lowq2_reconstruction_tensors_eicrecon:
0085     params:
0086         xml=os.getenv("DETECTOR_PATH")+"/epic_ip6_extended.xml",
0087         output_collections="BackwardsBeamlineHits,TaggerTrackerFeatureTensor,TaggerTrackerTargetTensor,MCParticles,EventHeader",
0088         ignore_plugins="janatop,LUMISPECCAL,ECTOF,BTOF,FOFFMTRK,RPOTS,B0TRK,MPGD,ECTRK,DRICH,DIRC,pid,tracking,EEMC,BEMC,FEMC,EHCAL,BHCAL,FHCAL,B0ECAL,ZDC,BTRK,BVTX,PFRICH,richgeo,evaluator,pid_lut,reco,rootfile",
0089         EICRECON_HASH=get_spack_package_hash("eicrecon"),
0090     input:
0091         data=SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0092     output:
0093         eicreconfile=ANALYSISDIR+"Low-Q2_Training_Tensors_{CAMPAIGN}.eicrecon.edm4hep.root",
0094     cache: True
0095     singularity: EIC_SINGULARITY_CONTAINER,
0096     shell:
0097         """
0098         eicrecon -Ppodio:output_file={output.eicreconfile} -PLOWQ2:TaggerTrackerTransportationPreML:beamE=18.0 -Pdd4hep:xml_files={params.xml} \
0099         -Pjana:nevents=500000 -Pjana:nskip=500000 \
0100         -Ppodio:output_collections={params.output_collections} -Pplugins_to_ignore={params.ignore_plugins}  {input.data} 
0101         """
0102 
0103 # Processes the simulation output data for training
0104 rule lowq2_steering_reconstruction_preparation:
0105     input:
0106         script=workflow.source_path("cleanData.C"),
0107         data=ANALYSISDIR+"Low-Q2_Training_Tensors_{CAMPAIGN}.eicrecon.edm4hep.root",
0108     output:
0109         rootfile=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Data{CAMPAIGN}.root",
0110     singularity: EIC_SINGULARITY_CONTAINER,
0111     shell:
0112         """
0113         root -l -b -q '{input.script}+("{input.data}", "{output.rootfile}",18.0)'
0114         """
0115 
0116 # Trains a regression model to predict the MCParticle momentum from the lowq2 tagger tracker hits.
0117 rule lowq2_steering_reconstruction_training:
0118     input:
0119         script=workflow.source_path("SteeringRegression.py"),
0120         scriptdata=workflow.source_path("LoadData.py"),
0121         scriptmodel=workflow.source_path("RegressionModel.py"),
0122         data=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Data{CAMPAIGN}.root",
0123     output:
0124         onnxfile=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Test_{CAMPAIGN}.onnx",
0125     singularity: EIC_SINGULARITY_CONTAINER,
0126     shell:
0127         """
0128         python {input.script} --dataFiles {input.data} --outModelFile {output.onnxfile} --epochs 1000
0129         """
0130 
0131 # Run eicrecon with the newly trained neural network
0132 rule lowq2_steering_reconstruction_particles_trained_eicrecon:
0133     params:
0134         xml=os.getenv("DETECTOR_PATH")+"/epic_ip6_extended.xml",
0135         output_collections="TaggerTrackerReconstructedParticles,MCParticles,EventHeader",
0136         ignore_plugins="janatop,LUMISPECCAL,ECTOF,BTOF,FOFFMTRK,RPOTS,B0TRK,MPGD,ECTRK,DRICH,DIRC,pid,tracking,EEMC,BEMC,FEMC,EHCAL,BHCAL,FHCAL,B0ECAL,ZDC,BTRK,BVTX,PFRICH,richgeo,evaluator,pid_lut,reco,rootfile",
0137         EICRECON_HASH=get_spack_package_hash("eicrecon"),
0138     input:
0139         data=SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0140         onnxfile=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Test_{CAMPAIGN}.onnx",
0141     output:
0142         eicreconfile=ANALYSISDIR+"Low-Q2_retrained_Particles_{CAMPAIGN}.eicrecon.edm4hep.root",
0143     cache: True
0144     singularity: EIC_SINGULARITY_CONTAINER,
0145     shell:
0146         """
0147         eicrecon -Ppodio:output_file={output.eicreconfile}  -Pjana:nevents=500000 -PLOWQ2:TaggerTrackerTransportationPreML:beamE=18.0 -PLOWQ2:TaggerTrackerTransportationPostML:beamE=18.0 -Pdd4hep:xml_files={params.xml} \
0148         -PLOWQ2:TaggerTrackerTransportationInference:modelPath={input.onnxfile} -Ppodio:output_collections={params.output_collections} -Pplugins_to_ignore={params.ignore_plugins}  {input.data} 
0149         """
0150 
0151 ##########################################################################################
0152 # Test and check resolutions
0153 ##########################################################################################
0154 
0155 # Produce hitsograms of th resolution of TaggerTrackerReconstructedParticles compared to their MCParticles
0156 rule lowq2_reconstruction_particles_test:
0157     input:
0158         script=workflow.source_path("reconstructionAnalysis.C"),
0159         data=ANALYSISDIR+"Low-Q2_{STAGE}_Particles_{CAMPAIGN}.eicrecon.edm4hep.root",
0160     output:
0161         rootfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.root",
0162         momentumCanvas=ANALYSISDIR+"{STAGE}_momentum_resolution_{CAMPAIGN}.png",
0163         energyThetaPhiCanvas=ANALYSISDIR+"{STAGE}_energy_theta_phi_resolution_{CAMPAIGN}.png",
0164         relationCanvas=ANALYSISDIR+"{STAGE}_relation_resolution_{CAMPAIGN}.png",
0165         resolutionGraphsCanvas=ANALYSISDIR+"{STAGE}_resolution_graphs_{CAMPAIGN}.png",
0166     singularity: EIC_SINGULARITY_CONTAINER,
0167     shell:
0168         """
0169         root -l -b -q '{input.script}+("{input.data}", 18, "{output.rootfile}", "{output.momentumCanvas}", "{output.energyThetaPhiCanvas}", "{output.relationCanvas}", "{output.resolutionGraphsCanvas}")'
0170         """
0171 
0172 # Check the resloutions and offsets are within tollerance
0173 rule lowq2_reconstruction_particles_check:
0174     input:
0175         script=workflow.source_path("checkResolutions.C"),
0176         rootfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.root"
0177     output:
0178         jsonfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.json"
0179     singularity: EIC_SINGULARITY_CONTAINER,
0180     shell:
0181         """
0182         root -l -b -q '{input.script}+("{input.rootfile}", "{output.jsonfile}")'
0183         """
0184 
0185 
0186 ##########################################################################################
0187 # Combine results
0188 ##########################################################################################
0189 def get_onnx_input(wildcards):
0190     if wildcards.STAGE == "retrained":
0191         return ANALYSISDIR + f"Low-Q2_Steering_Reconstruction_Test_{wildcards.CAMPAIGN}.onnx"
0192     else:
0193         return []
0194 
0195 rule lowq2_reconstruction:
0196     input: 
0197         rootfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.root",
0198         momentumCanvas=ANALYSISDIR+"{STAGE}_momentum_resolution_{CAMPAIGN}.png",
0199         energyThetaPhiCanvas=ANALYSISDIR+"{STAGE}_energy_theta_phi_resolution_{CAMPAIGN}.png",
0200         relationCanvas=ANALYSISDIR+"{STAGE}_relation_resolution_{CAMPAIGN}.png",
0201         resolutionGraphsCanvas=ANALYSISDIR+"{STAGE}_resolution_graphs_{CAMPAIGN}.png",
0202         jsonfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.json",
0203         onnxfile=get_onnx_input,
0204     output:
0205         directory("results/lowq2_reconstruction/{STAGE}_{CAMPAIGN}/")
0206     singularity: EIC_SINGULARITY_CONTAINER,
0207     shell:
0208         """
0209             mkdir {output}        
0210             cp -r {input} {output}
0211         """
0212 
0213 ##########################################################################################
0214 # Defualt running
0215 ##########################################################################################
0216 rule lowq2_reconstruction_local:
0217     input:
0218         "results/lowq2_reconstruction/test_local/"
0219 
0220 rule lowq2_reconstruction_retrained:
0221     input:
0222         "results/lowq2_reconstruction/retrained_local/"