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