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