Skip to content

Commit

Permalink
first commit for branch
Browse files Browse the repository at this point in the history
  • Loading branch information
sebouh137 committed Sep 25, 2024
1 parent b1055f7 commit f08596f
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ include:
- local: 'benchmarks/timing/config.yml'
- local: 'benchmarks/b0_tracker/config.yml'
- local: 'benchmarks/insert_muon/config.yml'
- local: 'benchmarks/insert_tau/config.yml'
- local: 'benchmarks/zdc_sigma/config.yml'
- local: 'benchmarks/zdc_lambda/config.yml'
- local: 'benchmarks/insert_neutron/config.yml'
Expand All @@ -172,6 +173,7 @@ deploy_results:
- "collect_results:zdc"
- "collect_results:zdc_lyso"
- "collect_results:insert_muon"
- "collect_results:insert_tau"
- "collect_results:zdc_photon"
- "collect_results:zdc_pi0"
script:
Expand Down
3 changes: 1 addition & 2 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ include: "benchmarks/tracking_performances_dis/Snakefile"
include: "benchmarks/zdc_lyso/Snakefile"
include: "benchmarks/insert_muon/Snakefile"
include: "benchmarks/zdc_lambda/Snakefile"
include: "benchmarks/zdc_lyso/Snakefile"
include: "benchmarks/zdc_photon/Snakefile"
include: "benchmarks/zdc_pi0/Snakefile"
include: "benchmarks/zdc_sigma/Snakefile"
include: "benchmarks/insert_neutron/Snakefile"

include: "benchmarks/insert_tau/Snakefile"

use_s3 = config["remote_provider"].lower() == "s3"
use_xrootd = config["remote_provider"].lower() == "xrootd"
Expand Down
57 changes: 57 additions & 0 deletions benchmarks/insert_tau/Snakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
rule insert_tau_generate:
input:
script="benchmarks/insert_tau/analysis/gen_particles.cxx",
params:
NEVENTS_GEN=1000,
th_max=7.0,
th_min=1.7
output:
GEN_FILE="sim_output/insert_tau/tau-_{P}GeV.hepmc"
shell:
"""
root -l -b -q '{input.script}({params.NEVENTS_GEN},"{output.GEN_FILE}", "tau-", {params.th_min}, {params.th_max}, 0., 360., {wildcards.P})'
"""

rule insert_tau_simulate:
input:
GEN_FILE="sim_output/insert_tau/tau-_{P}GeV.hepmc"
params:
PHYSICS_LIST="FTFP_BERT"
output:
SIM_FILE="sim_output/insert_tau/{DETECTOR_CONFIG}_sim_tau-_{P}GeV.edm4hep.root"
shell:
"""
NEVENTS_SIM=1000
# Running simulation
npsim \
--compactFile $DETECTOR_PATH/{wildcards.DETECTOR_CONFIG}.xml \
--numberOfEvents $NEVENTS_SIM \
--physicsList {params.PHYSICS_LIST} \
--inputFiles {input.GEN_FILE} \
--outputFile {output.SIM_FILE}
"""

rule insert_tau_recon:
input:
SIM_FILE="sim_output/insert_tau/{DETECTOR_CONFIG}_sim_tau-_{P}GeV.edm4hep.root"
output:
REC_FILE="sim_output/insert_tau/{DETECTOR_CONFIG}_rec_tau-_{P}GeV.edm4hep.root"
shell:
"""
NEVENTS_REC=1000
eicrecon {input.SIM_FILE} -Ppodio:output_file={output.REC_FILE} -Pdd4hep:xml_files=$DETECTOR_PATH/{wildcards.DETECTOR_CONFIG}.xml -Ppodio:output_collections=MCParticles,HcalEndcapPInsertRecHits,HcalEndcapPInsertClusters,HcalEndcapPInsertSubcellHits,EcalEndcapPInsertRecHits,EcalEndcapPInsertClusters -Pjana:nevents=$NEVENTS_REC
"""

rule insert_tau_analysis:
input:
expand("sim_output/insert_tau/{DETECTOR_CONFIG}_rec_tau-_{P}GeV.edm4hep.root",
P=[20,30, 40,50,60, 80, 100],
DETECTOR_CONFIG=["{DETECTOR_CONFIG}"]),
script="benchmarks/insert_tau/analysis/tau_plots.py",
output:
results_dir=directory("results/{DETECTOR_CONFIG}/insert_tau"),
shell:
"""
mkdir -p {output.results_dir}
python {input.script} {output.results_dir}
"""
57 changes: 57 additions & 0 deletions benchmarks/insert_tau/Snakefile~
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
rule insert_tau_generate:
input:
script="benchmarks/insert_tau/analysis/gen_particles.cxx",
params:
NEVENTS_GEN=1000,
th_max=7.0,
th_min=1.7
output:
GEN_FILE="sim_output/insert_tau/tau-_{P}GeV.hepmc"
shell:
"""
root -l -b -q '{input.script}({params.NEVENTS_GEN},"{output.GEN_FILE}", "tau-", {params.th_min}, {params.th_max}, 0., 360., {wildcards.P})'
"""

rule insert_tau_simulate:
input:
GEN_FILE="sim_output/insert_tau/tau-_{P}GeV.hepmc"
params:
PHYSICS_LIST="FTFP_BERT"
output:
SIM_FILE="sim_output/insert_tau/{DETECTOR_CONFIG}_sim_tau-_{P}GeV.edm4hep.root"
shell:
"""
NEVENTS_SIM=1000
# Running simulation
npsim \
--compactFile $DETECTOR_PATH/{wildcards.DETECTOR_CONFIG}.xml \
--numberOfEvents $NEVENTS_SIM \
--physicsList {params.PHYSICS_LIST} \
--inputFiles {input.GEN_FILE} \
--outputFile {output.SIM_FILE}
"""

rule insert_tau_recon:
input:
SIM_FILE="sim_output/insert_tau/{DETECTOR_CONFIG}_sim_tau-_{P}GeV.edm4hep.root"
output:
REC_FILE="sim_output/insert_tau/{DETECTOR_CONFIG}_rec_tau-_{P}GeV.edm4hep.root"
shell:
"""
NEVENTS_REC=1000
eicrecon {input.SIM_FILE} -Ppodio:output_file={output.REC_FILE} -Pdd4hep:xml_files=$DETECTOR_PATH/{wildcards.DETECTOR_CONFIG}.xml -Ppodio:output_collections=MCParticles,HcalEndcapPInsertRecHits,HcalEndcapPInsertClusters,HcalEndcapPInsertSubcellHits,EcalEndcapPInsertRecHits,EcalEndcapPInsertClusters -Pjana:nevents=$NEVENTS_REC
"""

rule insert_tau_analysis:
input:
expand("sim_output/insert_tau/{DETECTOR_CONFIG}_rec_tau-_{P}GeV.edm4hep.root",
P=[20,50, 100, 200],
DETECTOR_CONFIG=["{DETECTOR_CONFIG}"]),
script="benchmarks/insert_tau/analysis/tau_plots.py",
output:
results_dir=directory("results/{DETECTOR_CONFIG}/insert_tau"),
shell:
"""
mkdir -p {output.results_dir}
python {input.script} {output.results_dir}
"""
127 changes: 127 additions & 0 deletions benchmarks/insert_tau/analysis/gen_particles.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include "HepMC3/GenEvent.h"
#include "HepMC3/ReaderAscii.h"
#include "HepMC3/WriterAscii.h"
#include "HepMC3/Print.h"

#include "TRandom3.h"
#include "TVector3.h"

#include <iostream>
#include <random>
#include <cmath>
#include <math.h>
#include <TMath.h>
#include <TDatabasePDG.h>
#include <TParticlePDG.h>

using namespace HepMC3;

// Generate single electron as input to the Insert simulation.
// --
// We generate events with a constant polar angle with respect to
// the proton direction and then rotate so that the events are given
// in normal lab coordinate system
// --
void gen_particles(
int n_events = 1000,
const char* out_fname = "gen_particles.hepmc",
TString particle_name = "e-",
double th_min = 3., // Minimum polar angle, in degrees
double th_max = 3., // Maximum polar angle, in degrees
double phi_min = 0., // Minimum azimuthal angle, in degrees
double phi_max = 360., // Maximum azimuthal angle, in degrees
double p = 10., // Momentum in GeV/c
int dist = 0, //Momentum distribution: 0=fixed, 1=uniform, 2=Gaussian
int useCrossingAngle=1 // 0= no rotation, 1 = -25 mrad
)
{
WriterAscii hepmc_output(out_fname);
int events_parsed = 0;
GenEvent evt(Units::GEV, Units::MM);

// Random number generator
TRandom3 *r1 = new TRandom3(0); //Use time as random seed

// Getting generated particle information
TDatabasePDG *pdg = new TDatabasePDG();
TParticlePDG *particle = pdg->GetParticle(particle_name);
const double mass = particle->Mass();
const int pdgID = particle->PdgCode();

for (events_parsed = 0; events_parsed < n_events; events_parsed++) {

//Set the event number
evt.set_event_number(events_parsed);

// FourVector(px,py,pz,e,pdgid,status)
// type 4 is beam
// pdgid 11 - electron
// pdgid 111 - pi0
// pdgid 2212 - proton
GenParticlePtr p1 =
std::make_shared<GenParticle>(FourVector(0.0, 0.0, 10.0, 10.0), 11, 4);
GenParticlePtr p2 = std::make_shared<GenParticle>(
FourVector(0.0, 0.0, 0.0, 0.938), 2212, 4);

// Define momentum with respect to proton direction
double phi = r1->Uniform(phi_min*TMath::DegToRad(),phi_max*TMath::DegToRad());
double th = r1->Uniform(th_min*TMath::DegToRad(),th_max*TMath::DegToRad());

//Total momentum distribution
double pevent = -1;
if(dist==0){ //fixed
pevent = p;
}
else if(dist==1){ //Uniform: +-50% variation
pevent = p*(1. + r1->Uniform(-0.5,0.5) );
}
else if(dist==2){ //Gaussian: Sigma = 0.1*mean
while(pevent<0) //Avoid negative values
pevent = r1->Gaus(p,0.1*p);
}

double px = pevent * std::cos(phi) * std::sin(th);
double py = pevent * std::sin(phi) * std::sin(th);
double pz = pevent * std::cos(th);
TVector3 pvec(px,py,pz);

//Rotate to lab coordinate system
double cross_angle = -25./1000.*useCrossingAngle; //in Rad
TVector3 pbeam_dir(sin(cross_angle),0,cos(cross_angle)); //proton beam direction
pvec.RotateY(-pbeam_dir.Theta()); // Theta is returned positive, beam in negative X
// type 1 is final state
// pdgid 11 - electron 0.510 MeV/c^2
GenParticlePtr p3 = std::make_shared<GenParticle>(
FourVector(
pvec.X(), pvec.Y(), pvec.Z(),
sqrt(pevent*pevent + (mass * mass))),
pdgID, 1);

//If wanted, set non-zero vertex
double vx = 0.;
double vy = 0.;
double vz = 0.;
double vt = 0.;

GenVertexPtr v1 = std::make_shared<GenVertex>();
evt.shift_position_by(FourVector(vx, vy, vz, vt));
v1->add_particle_in(p1);
v1->add_particle_in(p2);

v1->add_particle_out(p3);
evt.add_vertex(v1);

if (events_parsed == 0) {
std::cout << "First event: " << std::endl;
Print::listing(evt);
}

hepmc_output.write_event(evt);
if (events_parsed % 100 == 0) {
std::cout << "Event: " << events_parsed << std::endl;
}
evt.clear();
}
hepmc_output.close();
std::cout << "Events parsed and written: " << events_parsed << std::endl;
}
86 changes: 86 additions & 0 deletions benchmarks/insert_tau/analysis/muon_plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import numpy as np, pandas as pd, matplotlib.pyplot as plt, matplotlib as mpl, awkward as ak, sys
import mplhep as hep
hep.style.use("CMS")

plt.rcParams['figure.facecolor']='white'
plt.rcParams['savefig.facecolor']='white'
plt.rcParams['savefig.bbox']='tight'

plt.rcParams["figure.figsize"] = (7, 7)

config=sys.argv[1].split("/")[1] #results/{config}/{benchmark_name}
outdir=sys.argv[1]+"/"
try:
import os
os.mkdir(outdir[:-1])
except:
pass

def Landau(x, normalization,location,stdev):
#print(type(x))
u=(x-location)*3.591/stdev/2.355
renormalization = 1.64872*normalization
return renormalization * np.exp(-u/2 - np.exp(-u)/2)

import uproot as ur
arrays_sim={}
momenta=50,
for p in momenta:
filename=f'sim_output/insert_muon/{config}_sim_mu-_{p}GeV.edm4hep.root'
print("opening file", filename)
events = ur.open(filename+':events')
arrays_sim[p] = events.arrays()
import gc
gc.collect()
print("read", filename)

for array in arrays_sim.values():
tilt=-0.025
px=array['MCParticles.momentum.x'][:,2]
py=array['MCParticles.momentum.y'][:,2]
pz=array['MCParticles.momentum.z'][:,2]
p=np.sqrt(px**2+py**2+pz**2)

pxp=px*np.cos(tilt)-pz*np.sin(tilt)
pyp=py
pzp=pz*np.cos(tilt)+px*np.sin(tilt)

array['eta_truth']=1/2*np.log((p+pzp)/(p-pzp))
array['phi_truth']=np.arctan2(pyp,pxp)

for p in 50,:
E=arrays_sim[p]["HcalEndcapPInsertHits.energy"]
y, x,_=plt.hist(1e3*ak.flatten(E),bins=100, range=(0, 1.2), histtype='step')
bc=(x[1:]+x[:-1])/2
from scipy.optimize import curve_fit
slc=abs(bc-.48)<.15
fnc=Landau
p0=[100, .5, .05]
#print(list(y), list(x))
coeff, var_matrix = curve_fit(fnc, list(bc[slc]), list(y[slc]), p0=p0,
sigma=list(np.sqrt(y[slc])+(y[slc]==0)))
print(coeff)
xx=np.linspace(0,.7, 100)
MIP=coeff[1]/1000
plt.plot(xx, fnc(xx,*coeff), label=f'Landau fit:\nMIP={coeff[1]*1e3:.0f}$\\pm${1e3*np.sqrt(var_matrix[1][1]):.0f} keV')
plt.xlabel("hit energy [MeV]")
plt.ylabel("hits")
plt.title(f"$E_{{\\mu^-}}=${p} GeV")
plt.legend(fontsize=20)
plt.savefig(outdir+"/MIP.pdf")

plt.figure(figsize=(10,7))
array=arrays_sim[p]
bins=30; r=((-np.pi, np.pi),(2.8, 4.2))
selection=np.sum(array["HcalEndcapPInsertHits.energy"]>0.5*MIP,axis=-1)>0
h1, xedges, yedges = np.histogram2d(list(array[selection]['phi_truth']),list(array[selection]['eta_truth']), bins=bins, range=r)
h2, xedges, yedges = np.histogram2d(list(array['phi_truth']),list(array['eta_truth']), bins=bins, range=r)

h = h1 / h2
pc=plt.pcolor(xedges, yedges, h.T,linewidth=0)
plt.xlabel("$\\phi^*$ [rad]")
plt.ylabel("$\\eta^*$")
cb = plt.colorbar(pc)
cb.set_label("acceptance")
plt.title(f"$E_{{\\mu^-}}=${p} GeV")
plt.savefig(outdir+"/acceptance.pdf")
Empty file.
30 changes: 30 additions & 0 deletions benchmarks/insert_tau/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
sim:insert_muon:
stage: simulate
extends: .det_benchmark
needs: ["common:detector"]
parallel:
matrix:
- P: 50
timeout: 1 hours
script:
- snakemake --cores 1 sim_output/insert_muon/epic_craterlake_sim_mu-_${P}GeV.edm4hep.root
retry:
max: 2
when:
- runner_system_failure

bench:insert_muon:
stage: benchmarks
extends: .det_benchmark
needs: ["sim:insert_muon"]
script:
- snakemake --cores 1 results/epic_craterlake/insert_muon

collect_results:insert_muon:
stage: collect
needs: ["bench:insert_muon"]
script:
- ls -al
- mv results{,_save}/ # move results directory out of the way to preserve it
- snakemake --cores 1 --delete-all-output results/epic_craterlake/insert_muon
- mv results{_save,}/

0 comments on commit f08596f

Please sign in to comment.