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         python {input.script} --outFile {output} --inFile {params.events}
0025         """
0026 
0027 # Run pythia6 Low-Q2 events through the simulation
0028 rule lowq2_training_sim:
0029     input:
0030         warmup="warmup.edm4hep.root",
0031         events=SIMOUTDIR+"Low-Q2_Training_Events.hepmc3.tree.root",
0032         geometry_lib=find_epic_libraries(),
0033     output:
0034         SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0035     params:
0036         DD4HEP_HASH=get_spack_package_hash("dd4hep"),
0037         NPSIM_HASH=get_spack_package_hash("npsim"),
0038     cache: True
0039     singularity: EIC_SINGULARITY_CONTAINER,
0040     shell:
0041         """
0042             exec npsim \
0043             --runType run \
0044             --random.seed 1 \
0045             --inputFiles {input.events} \
0046             --numberOfEvents 1000000 \
0047             --compactFile $DETECTOR_PATH/epic_ip6_extended.xml \
0048             --printLevel WARNING \
0049             --outputFile {output} \
0050             --physics.rangecut 100*m 
0051         """
0052 
0053 # Run EICrecon to create TaggerTrackerReconstructedParticles
0054 rule lowq2_reconstruction_particles_eicrecon:
0055     params:
0056         xml=os.getenv("DETECTOR_PATH")+"/epic_ip6_extended.xml",
0057         output_collections="TaggerTrackerReconstructedParticles,MCParticles,EventHeader",
0058         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",
0059         EICRECON_HASH=get_spack_package_hash("eicrecon"),
0060     input:
0061         data=SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0062     output:
0063         eicreconfile=ANALYSISDIR+"Low-Q2_test_Particles_{CAMPAIGN}.eicrecon.edm4hep.root",
0064     cache: True
0065     singularity: EIC_SINGULARITY_CONTAINER,
0066     shell:
0067         """
0068         eicrecon -Ppodio:output_file={output.eicreconfile} -PLOWQ2:TaggerTrackerTransportationPreML:beamE=18.0 -PLOWQ2:TaggerTrackerTransportationPostML:beamE=18.0 -Pdd4hep:xml_files={params.xml} \
0069         -Pjana:nevents=500000 \
0070         -Ppodio:output_collections={params.output_collections} -Pplugins_to_ignore={params.ignore_plugins}  {input.data} 
0071         """
0072 
0073 
0074 ##########################################################################################
0075 ### Rules for training new onnx reconstruction neural network
0076 ##########################################################################################
0077 
0078 # Run EICrecon and create input tensors for reconstruction.
0079 rule lowq2_reconstruction_tensors_eicrecon:
0080     params:
0081         xml=os.getenv("DETECTOR_PATH")+"/epic_ip6_extended.xml",
0082         output_collections="BackwardsBeamlineHits,TaggerTrackerFeatureTensor,TaggerTrackerTargetTensor,MCParticles,EventHeader",
0083         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",
0084         EICRECON_HASH=get_spack_package_hash("eicrecon"),
0085     input:
0086         data=SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0087     output:
0088         eicreconfile=ANALYSISDIR+"Low-Q2_Training_Tensors_{CAMPAIGN}.eicrecon.edm4hep.root",
0089     cache: True
0090     singularity: EIC_SINGULARITY_CONTAINER,
0091     shell:
0092         """
0093         eicrecon -Ppodio:output_file={output.eicreconfile} -PLOWQ2:TaggerTrackerTransportationPreML:beamE=18.0 -Pdd4hep:xml_files={params.xml} \
0094         -Pjana:nevents=500000 -Pjana:nskip=500000 \
0095         -Ppodio:output_collections={params.output_collections} -Pplugins_to_ignore={params.ignore_plugins}  {input.data} 
0096         """
0097 
0098 # Processes the simulation output data for training
0099 rule lowq2_steering_reconstruction_preparation:
0100     input:
0101         script=workflow.source_path("cleanData.C"),
0102         data=ANALYSISDIR+"Low-Q2_Training_Tensors_{CAMPAIGN}.eicrecon.edm4hep.root",
0103     output:
0104         rootfile=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Data{CAMPAIGN}.root",
0105     singularity: EIC_SINGULARITY_CONTAINER,
0106     shell:
0107         """
0108         root -l -b -q '{input.script}+("{input.data}", "{output.rootfile}",18.0)'
0109         """
0110 
0111 # Trains a regression model to predict the MCParticle momentum from the lowq2 tagger tracker hits.
0112 rule lowq2_steering_reconstruction_training:
0113     input:
0114         script=workflow.source_path("SteeringRegression.py"),
0115         scriptdata=workflow.source_path("LoadData.py"),
0116         scriptmodel=workflow.source_path("RegressionModel.py"),
0117         data=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Data{CAMPAIGN}.root",
0118     output:
0119         onnxfile=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Test_{CAMPAIGN}.onnx",
0120     singularity: EIC_SINGULARITY_CONTAINER,
0121     shell:
0122         """
0123         python {input.script} --dataFiles {input.data} --outModelFile {output.onnxfile} --epochs 1000
0124         """
0125 
0126 # Run eicrecon with the newly trained neural network
0127 rule lowq2_steering_reconstruction_particles_trained_eicrecon:
0128     params:
0129         xml=os.getenv("DETECTOR_PATH")+"/epic_ip6_extended.xml",
0130         output_collections="TaggerTrackerReconstructedParticles,MCParticles,EventHeader",
0131         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",
0132         EICRECON_HASH=get_spack_package_hash("eicrecon"),
0133     input:
0134         data=SIMOUTDIR+"Low-Q2_Training_SimEvents_{CAMPAIGN}.edm4hep.root",
0135         onnxfile=ANALYSISDIR+"Low-Q2_Steering_Reconstruction_Test_{CAMPAIGN}.onnx",
0136     output:
0137         eicreconfile=ANALYSISDIR+"Low-Q2_retrained_Particles_{CAMPAIGN}.eicrecon.edm4hep.root",
0138     cache: True
0139     singularity: EIC_SINGULARITY_CONTAINER,
0140     shell:
0141         """
0142         eicrecon -Ppodio:output_file={output.eicreconfile}  -Pjana:nevents=500000 -PLOWQ2:TaggerTrackerTransportationPreML:beamE=18.0 -PLOWQ2:TaggerTrackerTransportationPostML:beamE=18.0 -Pdd4hep:xml_files={params.xml} \
0143         -PLOWQ2:TaggerTrackerTransportationInference:modelPath={input.onnxfile} -Ppodio:output_collections={params.output_collections} -Pplugins_to_ignore={params.ignore_plugins}  {input.data} 
0144         """
0145 
0146 ##########################################################################################
0147 # Test and check resolutions
0148 ##########################################################################################
0149 
0150 # Produce hitsograms of th resolution of TaggerTrackerReconstructedParticles compared to their MCParticles
0151 rule lowq2_reconstruction_particles_test:
0152     input:
0153         script=workflow.source_path("reconstructionAnalysis.C"),
0154         data=ANALYSISDIR+"Low-Q2_{STAGE}_Particles_{CAMPAIGN}.eicrecon.edm4hep.root",
0155     output:
0156         rootfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.root",
0157         momentumCanvas=ANALYSISDIR+"{STAGE}_momentum_resolution_{CAMPAIGN}.png",
0158         energyThetaPhiCanvas=ANALYSISDIR+"{STAGE}_energy_theta_phi_resolution_{CAMPAIGN}.png",
0159         relationCanvas=ANALYSISDIR+"{STAGE}_relation_resolution_{CAMPAIGN}.png",
0160         resolutionGraphsCanvas=ANALYSISDIR+"{STAGE}_resolution_graphs_{CAMPAIGN}.png",
0161     singularity: EIC_SINGULARITY_CONTAINER,
0162     shell:
0163         """
0164         root -l -b -q '{input.script}+("{input.data}", 18, "{output.rootfile}", "{output.momentumCanvas}", "{output.energyThetaPhiCanvas}", "{output.relationCanvas}", "{output.resolutionGraphsCanvas}")'
0165         """
0166 
0167 # Check the resloutions and offsets are within tollerance
0168 rule lowq2_reconstruction_particles_check:
0169     input:
0170         script=workflow.source_path("checkResolutions.C"),
0171         rootfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.root"
0172     output:
0173         jsonfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.json"
0174     singularity: EIC_SINGULARITY_CONTAINER,
0175     shell:
0176         """
0177         root -l -b -q '{input.script}+("{input.rootfile}", "{output.jsonfile}")'
0178         """
0179 
0180 
0181 ##########################################################################################
0182 # Combine results
0183 ##########################################################################################
0184 def get_onnx_input(wildcards):
0185     if wildcards.STAGE == "retrained":
0186         return ANALYSISDIR + f"Low-Q2_Steering_Reconstruction_Test_{wildcards.CAMPAIGN}.onnx"
0187     else:
0188         return []
0189 
0190 rule lowq2_reconstruction:
0191     input: 
0192         rootfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.root",
0193         momentumCanvas=ANALYSISDIR+"{STAGE}_momentum_resolution_{CAMPAIGN}.png",
0194         energyThetaPhiCanvas=ANALYSISDIR+"{STAGE}_energy_theta_phi_resolution_{CAMPAIGN}.png",
0195         relationCanvas=ANALYSISDIR+"{STAGE}_relation_resolution_{CAMPAIGN}.png",
0196         resolutionGraphsCanvas=ANALYSISDIR+"{STAGE}_resolution_graphs_{CAMPAIGN}.png",
0197         jsonfile=ANALYSISDIR+"Low-Q2_{STAGE}_Resolution_Results_{CAMPAIGN}.json",
0198         onnxfile=get_onnx_input,
0199     output:
0200         directory("results/lowq2_reconstruction/{STAGE}_{CAMPAIGN}/")
0201     singularity: EIC_SINGULARITY_CONTAINER,
0202     shell:
0203         """
0204             mkdir {output}        
0205             cp -r {input} {output}
0206         """
0207 
0208 ##########################################################################################
0209 # Defualt running
0210 ##########################################################################################
0211 rule lowq2_reconstruction_local:
0212     input:
0213         "results/lowq2_reconstruction/test_local/"
0214 
0215 rule lowq2_reconstruction_retrained:
0216     input:
0217         "results/lowq2_reconstruction/retrained_local/"