<<

How to build a MC generator

Tomasz Golan University of Wroclaw

NuSTEC, Fermilab 2017 method Buffon’s needle problem

Suppose we have a floor made of parallel strips of wood, each the same width, and we drop a needle onto the floor. What is the probability that the needle will lie across a line between two strips?

Georges-Louis Leclerc, blue are good Comte de Buffon 18th century red are bad

Monte Carlo without computers

If needle length (l) < lines width (t): MC experiment was performed by Mario Lazzarini in 1901 by throwing 3408 2l P = needles: tπ which can be used to estimate π: 2l 3408 355 π = · = = 3.14159292 2l t #red 113 π = · tP

Tomasz Golan MC generators @ NuSTEC 3 / 40 From to Monte Carlo method

■ Stanislaw Ulam was a Polish mathematician

■ He invented the Monte Carlo method while playing solitaire

■ The method was used in Los Alamos, performed by ENIAC computer

■ What is a probability of success in solitaire? ◆ Too complex for an analytical calculations

◆ Lets try N = 100 times and count wins

◆ With N we are getting closer to→ correct ∞ result

Tomasz Golan MC generators @ NuSTEC 4 / 40 Newton-Pepys problem

Monte Carlo method Which of the three Buffon’s needle problem From Solitaire to MC propositions has the greatest Newton-Pepys problem PRNG chance of success? Hit-or-miss method MC integration results Optimization of MC Crude method A Six fair dice are tossed Methods comparison Random from PDF independently and at least CDF CDF discrete one “6” appears. CDF continuous Acceptance-rejection B Twelve fair dice are tossed Quasi-elastic scattering independently and at least two “6”s appear.

C Eighteen fair dice are tossed independently and at least three “6”s appear.

Tomasz Golan MC generators @ NuSTEC 5 / 40 Newton-Pepys problem: analytical attempt

■ First, lets go back to high school and calculate this analytically Monte Carlo method Buffon’s needle problem From Solitaire to MC ■ 1 Newton-Pepys problem Let p = 6 be the probability of rolling 6 PRNG Hit-or-miss method MC integration results ■ The probability of not rolling 6 is (1 p) Optimization of MC Crude method − Methods comparison Random from PDF A six attempts, at least one six CDF CDF discrete CDF continuous 6 Acceptance-rejection PA = 1 (1 p) 0.6651 Quasi-elastic scattering − − ≈ B twelve attempts, at least two sixes

12 12 11 PB = 1 (1 p) p(1 p) 0.6187 − − −  1  − ≈

C eighteen attempts, at least three sixes

18 18 17 18 2 16 PC = 1 (1 p) p(1 p) p (1 p) 0.5973 − − − 1  − − 2  − ≈

Tomasz Golan MC generators @ NuSTEC 6 / 40 Newton-Pepys problem: MC attempt

def throw (nSixes): ■ MC attempt is just “performing the n = 0 experiment”, so we will be rolling for _ in range (6 * nSixes): dices if random.randint (1, 6) == 6: n += 1 return n >= nSixes ■ Roll 6n times and check if number of def MC (nSixes, nAttempts): sixes is greater or equal n n = 0 for _ in range (nAttempts): ■ Repeat N times and your probability n += throw (nSixes) is given by: return float (n) / nAttempts number of successes P = if __name__ == "__main__": N for i in range (1, 4): print MC (i, 1000)

Tomasz Golan MC generators @ NuSTEC 7 / 40 Newton-Pepys problem: summary

■ Your MC result depends on N Monte Carlo method Buffon’s needle problem From Solitaire to MC ■ Newton-Pepys problem Results for N = 100: PRNG Hit-or-miss method MC integration results Optimization of MC true Crude method PA = 0.71, 0.68, 0.76, 0.65, 0.68 PA = 0.6651 Methods comparison true Random from PDF PB = 0.70, 0.56, 0.60, 0.63, 0.69 P = 0.6187 CDF B CDF discrete true CDF continuous PC = 0.62, 0.62, 0.53, 0.57, 0.62 PC = 0.5973 Acceptance-rejection

Quasi-elastic scattering ■ Results for N = 106:

PA = 0.6655, 0.6648, 0.6653, 0.6662, 0.6653

PB = 0.6188, 0.6191, 0.6191, 0.6190, 0.6182

PC = 0.5975, 0.5979, 0.5972, 0.5978, 0.5973

■ Your MC results also depends on the way how random numbers were generated

Tomasz Golan MC generators @ NuSTEC 8 / 40 Pseudorandom number generator

■ PRNG is an algorithm for generating a sequence of “random” Monte Carlo method Buffon’s needle problem numbers From Solitaire to MC Newton-Pepys problem PRNG ■ Hit-or-miss method Example: middle-square method (used in ENIAC) MC integration results Optimization of MC ◆ Crude method take n-digit number as your seed Methods comparison Random from PDF CDF ◆ square it to get 2n-digit number (add leading zeroes if CDF discrete CDF continuous necessary) Acceptance-rejection Quasi-elastic scattering ◆ n middle digits are the result and the seed for next number

■ Middle-square method for n = 4 and base seed = 1111: 11112 = 01234321 2343 → 23432 = 05489649 4896 . → . 11112 = 01234321 2343 →

Tomasz Golan MC generators @ NuSTEC 9 / 40 Pseudorandom number generator

■ Nowadays, more sophisticated PRNGs exist, but they also Monte Carlo method Buffon’s needle problem suffer on some common problems: From Solitaire to MC Newton-Pepys problem ◆ PRNG periodicity / different periodicity for different base seed Hit-or-miss method MC integration results Optimization of MC ◆ Crude method nonuniformity of number distributions Methods comparison Random from PDF CDF ◆ correlation of successive numbers CDF discrete CDF continuous Acceptance-rejection

Quasi-elastic scattering

Mathematics and Computers in Simulations 46 (1998) 485-505

Tomasz Golan MC generators @ NuSTEC 10 / 40 MC integration (hit-or-miss method)

Lets do the following integration using MC method: Monte Carlo method Buffon’s needle problem From Solitaire to MC 1 1 2 1 Newton-Pepys problem 1 1 x 1 PRNG f(x)dx = x dx = = Hit-or-miss method Z0 Z0 2  2 2 0 4 MC integration results Optimization of MC Crude method y Methods comparison Random from PDF ■ take a random point from CDF CDF discrete the [0, 1] [0, 1] square CDF continuous × 1 Acceptance-rejection ■ 1 Quasi-elastic scattering compare it to your f(x) f(x) = 2 x

■ repeat N times

■ count n points below the function

■ you results is given by 1 x

1 n n f(x)dx = P = Z0 · N N

Tomasz Golan MC generators @ NuSTEC 11 / 40 N = 100 (hit-or-miss) N = 10000 (hit-or-miss) 0.5 0.5

0.4 0.4

0.3 0.3

0.2 0.2 MC result MC result MC

0.1 0.1

0 0 0 200 400 600 800 1000 0 200 400 600 800 1000 Run number Run number

N = 1000 (hit-or-miss) N = 100000 (hit-or-miss) 0.5 0.5

0.4 0.4

0.3 0.3

0.2 0.2 MC result MC MC result

0.1 0.1

0 0 0 200 400 600 800 1000 0 200 400 600 800 1000 Run number Run number Optimization of MC

y y Monte Carlo method Buffon’s needle problem From Solitaire to MC Newton-Pepys problem 1 PRNG f(x) = 1 x f(x) = 1 x Hit-or-miss method 2 2 MC integration results Optimization of MC Crude method 0.5 Methods comparison Random from PDF CDF CDF discrete CDF continuous Acceptance-rejection 1 x x Quasi-elastic scattering 1

■ You want to avoid generating “red” points as they do not contribute to your integral

■ You can choose any rectangle as far as it contains maximum of f(x) in given range

Tomasz Golan MC generators @ NuSTEC 13 / 40 Optimization of MC

■ Lets consider the following Monte Carlo method function: Buffon’s needle problem 0.2

From Solitaire to MC )

Newton-Pepys problem 2 PRNG Q Hit-or-miss method 1 ( MC integration results 2 F (Q ) = F 0.1 Optimization of MC 2 2 Crude method (1 + Q ) Methods comparison Random from PDF CDF more or less dipole form 0 CDF discrete CDF continuous factor 2 4 6 8 10 Acceptance-rejection Q2 Quasi-elastic scattering ■ Integrating this function over Q2 is highly inefficient 1

■ However, one can integrate ) x by substitution to get ( F 0.5 better performance, e.g.

2 x = log10(Q ) 0 2 1 0 1 − − 2 don’t forget about Jacobian x = log10(Q )

Tomasz Golan MC generators @ NuSTEC 14 / 40 MC integration (crude method)

Lets do the following integration using MC method once again: Monte Carlo method Buffon’s needle problem From Solitaire to MC 1 1 2 1 Newton-Pepys problem 1 1 x 1 PRNG f(x)dx = x dx = = Hit-or-miss method Z0 Z0 2  2 2 0 4 MC integration results Optimization of MC Crude method ■ One can approximate y Methods comparison Random from PDF integral CDF CDF discrete b N CDF continuous b a Acceptance-rejection f(x)dx − f(xi) 1 Quasi-elastic scattering Z ≈ N f(x) = x a Xi=1 2

where xi is a random number from [a, b]

■ It can be shown that crude method is more accurate x than hit-or-miss

■ We will skip the math and look at some comparisons

Tomasz Golan MC generators @ NuSTEC 15 / 40 N = 100 (hit-or-miss) N = 100 (crude) 0.5 0.5

0.4 0.4

0.3 0.3

0.2 0.2 MC result MC result MC

0.1 0.1

0 0 0 200 400 600 800 1000 0 200 400 600 800 1000 Run number Run number

N = 1000 (hit-or-miss) N = 1000 (crude) 0.5 0.5

0.4 0.4

0.3 0.3

0.2 0.2 MC result MC MC result

0.1 0.1

0 0 0 200 400 600 800 1000 0 200 400 600 800 1000 Run number Run number Random numbers from probability density function

Monte Carlo method Buffon’s needle problem From Solitaire to MC Newton-Pepys problem PRNG Hit-or-miss method ■ MC integration results How to generate a random 3 Optimization of MC Crude method number from probability Methods comparison Random from PDF density function? 2 CDF

CDF discrete y CDF continuous ■ Lets consider f(x) = 3x2 Acceptance-rejection 1 Quasi-elastic scattering ■ Which means that x = 1 should be thrown 2 times 0 √ 0 0.5 1 more often than x = 2 2 x

Tomasz Golan MC generators @ NuSTEC 17 / 40 Cumulative distribution function

■ Cumulative distribution function of a random variable X: Monte Carlo method Buffon’s needle problem From Solitaire to MC Newton-Pepys problem F (x) = P (X x) PRNG ≤ Hit-or-miss method MC integration results Optimization of MC Crude method Note: 0 F (x) 1 for all x Methods comparison ≤ ≤ Random from PDF CDF ■ CDF discrete Discrete random variable X: CDF continuous Acceptance-rejection Quasi-elastic scattering F (x) = f(xi) xXi x ≤ where f is probability mass function (PMF)

■ Continuous random variable X:

x F (x) = f(t)dt Z −∞ where f is probability density function (PDF)

Tomasz Golan MC generators @ NuSTEC 18 / 40 Cumulative distribution function - discrete example

■ Probability mass function f(x) = 3x2 Monte Carlo method Buffon’s needle problem From Solitaire to MC 1 2 3 4 Newton-Pepys problem with discrete random variables X is , , , , PRNG 30 30 30 30 Hit-or-miss method {q q q q } MC integration results Optimization of MC ■ CDF is given by: Crude method Methods comparison Random from PDF CDF 1 CDF discrete 1 1 CDF continuous 10 if x 30 Acceptance-rejection ≤  q ) 3 2 Quasi-elastic scattering x

 if x (  10 ≤ 30 0.5 F (x) =  q F  6 3  10 if x 30 ≤ q  10 if x 4  10 30 0  ≤ q  0 0.2 0.4 x

■ 4 With P = 1 the random number is less or equal to 30 , with q 3 P = 0.6 the random number is less or equal 30 ... q

Tomasz Golan MC generators @ NuSTEC 19 / 40 Cumulative distribution function - discrete example

■ To generate a random number from X according to 3x2: Monte Carlo method Buffon’s needle problem ◆ From Solitaire to MC generate a random number u from [0, 1] Newton-Pepys problem PRNG Hit-or-miss method ◆ 1 MC integration results if u 0.1: x = 30 Optimization of MC ≤ Crude method q Methods comparison Random from PDF ◆ 2 CDF else if u 0.3: x = 30 ... CDF discrete ≤ CDF continuous q Acceptance-rejection Quasi-elastic scattering ■ Results for N = 10000:

x n n/N f(x) 1 30 989 0.0989 0.1 q 2 30 1959 0.1959 0.2 q 3 30 2949 0.2949 0.3 q 4 30 4103 0.4103 0.4 q

Tomasz Golan MC generators @ NuSTEC 20 / 40 Cumulative distribution function - continuous example

■ Probability density function f(x) = 3x2 Monte Carlo method Buffon’s needle problem From Solitaire to MC Newton-Pepys problem with continuous random variables X range [0, 1] PRNG Hit-or-miss method MC integration results ■ CDF is given by: Optimization of MC Crude method Methods comparison Random from PDF x 1 CDF CDF discrete CDF continuous F (x) = f(t)dt Acceptance-rejection )

Z x Quasi-elastic scattering 0 ( 0.5 x F = 3t2dt Z 0 0 x 0 0.5 1 = t3 = x3 0 x

■ Blue area gives the probability that x 0.75 ≤

Tomasz Golan MC generators @ NuSTEC 21 / 40 Cumulative distribution function - continuous example

■ To generate a random number from X according to 3x2: Monte Carlo method Buffon’s needle problem ◆ From Solitaire to MC generate a random number u from [0, 1] Newton-Pepys problem PRNG Hit-or-miss method ◆ 1 MC integration results find x for which F (x) = u, i.e. x = F − (u) Optimization of MC Crude method Methods comparison ◆ x is your guy Random from PDF CDF CDF discrete CDF continuous ■ Acceptance-rejection Results for N = 10000:

Quasi-elastic scattering 3

1 2 Unfortunately, usually F − is width unknown, which makes this 1 method pretty useless (at least n/N/ directly). 0 0 0.5 1 x

Tomasz Golan MC generators @ NuSTEC 22 / 40 Acceptance-rejection method

■ Lets consider Monte Carlo method 0.4 3 −x2 Buffon’s needle problem 2 f(x) = x · e From Solitaire to MC 3 x Newton-Pepys problem f(x) = A x e− PRNG · · Hit-or-miss method 2e y MC integration results with x [0, 1], A = 0.2 Optimization of MC e 2 Crude method ∈ − Methods comparison Random from PDF ■ CDF is given by CDF CDF discrete 0 CDF continuous N 2 0 0.5 1 Acceptance-rejection 2 x F (x) = (x 1)e− x Quasi-elastic scattering 2 −

1 ■ Since, we do not know F − we have to find another way to generate x from f(x) distribution

■ We will use acceptance-rejection method (do you remember MC integration via hit-or-miss?)

Tomasz Golan MC generators @ NuSTEC 23 / 40 Acceptance-rejection method

■ Evaluate fmax max(f) ≥ fmax Monte Carlo method 0.6 Buffon’s needle problem From Solitaire to MC Note: fmax > max(f) will Newton-Pepys problem PRNG affect performance, but the y 0.4 Hit-or-miss method MC integration results result will be still correct Optimization of MC Crude method 0.2 Methods comparison ■ Random from PDF Generate random x CDF 0 CDF discrete f(x) 0 0.5 1 CDF continuous ■ Accept x with P = Acceptance-rejection fmax x Quasi-elastic scattering ◆ generate a random u from [0, f ] max 0.4 ◆

accept if u < f(x) width / 0.2 ■ The plot on the right shows sum the results for N = 105 n/A/ 0 0 0.5 1 x

Tomasz Golan MC generators @ NuSTEC 24 / 40 Acceptance-rejection method - optimization

■ The area under of 0.4 Monte Carlo method f(x) is 0.13 Buffon’s needle problem From Solitaire to MC ∼ Newton-Pepys problem PRNG ■

The total area is 0.4 y Hit-or-miss method 0.2 MC integration results Optimization of MC ■ Crude method Thus, only about 30% of Methods comparison Random from PDF points gives contribution to CDF 0 CDF discrete the final distribution CDF continuous 0 0.5 1 Acceptance-rejection ■ x Quasi-elastic scattering One can find g(x) for which CDF method is possible and which 0.4 encapsulates f(x) in given range and generate x according to g(x) y 0.2

■ For g(x) = 0.4x the total area is 0.2, so we speed up 0 twice 0 0.5 1 x

Tomasz Golan MC generators @ NuSTEC 25 / 40 Acceptance-rejection method - optimization

Monte Carlo method Buffon’s needle problem From Solitaire to MC ■ Newton-Pepys problem Cumulative distribution function for g(x) = 2x PRNG Hit-or-miss method x MC integration results 2 1 Optimization of MC G(x) = g(t)dt = x G− (x) = √x Crude method Z ⇒ Methods comparison 0 Random from PDF CDF Note: PDF must be normalized to 1 for CDF CDF discrete CDF continuous Acceptance-rejection ■ Generate random number u [0, 1] Quasi-elastic scattering ∈ 1 ■ Calculate your x = G− (u)

■ Accept x with probability P = f(x)/g(x)

instead of using constant fmax we are using fmax(x) g(x) ≡

Tomasz Golan MC generators @ NuSTEC 26 / 40 Quasi-elastic scattering Building a generator step by step Quasi-elastic scattering on a free nucleon

Llewellyn-Smith formula

2 2 2 dσ νl + n l− + p M GF cos θC 2 2 (s u) 2 (s u) 2 → + = 2 A(q ) B(q ) −2 + C(q ) − 4 d q ν¯l + p l + n 8πE  ∓ M M  | | → ν

Notation

■ Constants: M - nucleon mass, GF - Fermi constant, θC - Cabibbo angle,

2 2 2 ■ q =(k k′) =(p′ p) - four-momentum squared, where k, k′, p, p′ are four-momenta− of initial− and final lepton, initial and final nucleon

■ Eν - neutrino energy

2 2 ■ s =(k + k′) and u =(k p′) - Mandelstam variables −

Tomasz Golan MC generators @ NuSTEC 28 / 40 Quasi-elastic scattering on a free nucleon

Llewellyn-Smith formula

2 2 2 dσ νl + n l− + p M GF cos θC 2 2 (s u) 2 (s u) 2 → + = 2 A(q ) B(q ) −2 + C(q ) − 4 d q ν¯l + p l + n 8πE  ∓ M M  | | → ν

General idea

■ Having k and p, generate k′ and p′

2 2 2 ■ Calculate q and (s u) = 4MEν + q m based on generated kinematics − − ■ Calculate cross section

■ Repeat N times and the result is given by:

N 1 2 σtotal σ(q ) ∼ N i Xi=1

Tomasz Golan MC generators @ NuSTEC 29 / 40 Generating kinematics

Monte Carlo method LAB CMS Quasi-elastic scattering ~pN QEL on free N Generating kinematics ~pν ~p ~p LAB ⇆ CMS ∗ ∗ Cross section Generating events A few more steps ■ Lets consider kinematics in center-of-mass system

■ Mandelstam s is invariant under Lorentz transformation

2 2 2 2 s =(k + p) =(E + Ep) (~k + ~p) =(E∗ + E∗) − p

■ √s is the total energy in CMS

2 2 2 2 √s = E∗ + Ep∗ = p∗ + m + p∗ + M p p ■ We will use it to calculate p ∗

Tomasz Golan MC generators @ NuSTEC 30 / 40 Generating kinematics

■ Lets do some simple algebra: Monte Carlo method Quasi-elastic scattering 2 2 2 2 QEL on free N √s = E∗ + Ep∗ = p∗ + m + p∗ + M Generating kinematics LAB ⇆ CMS 2p 2 2 p Cross section √s = E∗ + E∗ m + M Generating events − 2 p 2 2 2 A few more steps s = E∗ + E∗ m + M + 2E∗E∗ − p 2 2 s = 2E∗(E∗ + E∗) m + M p − 2 2 s = 2E∗√s m + M − s + m2 M 2 E∗ = − 2√s s + M 2 m2 E∗ = − (analogously) p 2√s

■ After more algebra we get:

2 2 2 2 [s (m M) ] [s (m + M) ] p∗ = E∗ m = − − · − p − 2√s

Tomasz Golan MC generators @ NuSTEC 31 / 40 Generating kinematics

z Monte Carlo method ■ We use spherical coordinate Quasi-elastic scattering QEL on free N system to determine Generating kinematics θ p∗ LAB ⇆ CMS momentum direction in Cross section Generating events CMS: A few more steps ~p∗ = p∗ (sin θ cos φ, sin θ sin φ, cos θ) · φ y x

■ Generate random angles:

φ = 2π random[0, 1] sin φ, cos φ · ⇒ cos θ = 2 random[0, 1] 1 sin θ, cos θ · − ⇒

■ All we need to do is to go back to LAB frame

Tomasz Golan MC generators @ NuSTEC 32 / 40 LAB ⇆ CMS

■ ~v Lorentz boost in direction nˆ = v of (t, ~r): Monte Carlo method

Quasi-elastic scattering QEL on free N Generating kinematics t′ = γ (t vnˆ ~r) LAB ⇆ CMS Cross section − · Generating events ~r′ = ~r +(γ 1)(ˆn ~r)ˆn γtvnˆ A few more steps − · − ■ In our case LAB

~pν + ~pN ~pN ~v = ~pν Eν + EN

■ Boost from LAB to CMS in ~v direction CMS ■ Boost from CMS to LAB in ~v direction ~p ~p − ∗ ∗

Tomasz Golan MC generators @ NuSTEC 33 / 40 Calculating cross section

Llewellyn-Smith formula

2 2 2 dσ νl + n l− + p M GF cos θC 2 2 (s u) 2 (s u) 2 → + = 2 A(q ) B(q ) −2 + C(q ) − 4 d q ν¯l + p l + n 8πE  ∓ M M  | | → ν

Calculation

2 ■ Once we have p′ and k′ in LAB frame we can calculate q and (s u) − ■ Once we have q2 we can calculate A(q2), B(q2), C(q2)

■ We have everything to calculate cross section

■ Do we? Or maybe we are still missing something?

Tomasz Golan MC generators @ NuSTEC 34 / 40 Calculating cross section

Llewellyn-Smith formula

2 2 2 dσ νl + n l− + p M GF cos θC 2 2 (s u) 2 (s u) 2 → + = 2 A(q ) B(q ) −2 + C(q ) − 4 d q ν¯l + p l + n 8πE  ∓ M M  | | → ν

Calculation

2 ■ Once we have p′ and k′ in LAB frame we can calculate q and (s u) − ■ Once we have q2 we can calculate A(q2), B(q2), C(q2)

■ We have everything to calculate cross section

■ Do we? Or maybe we are still missing something?

We change the variable we integrate over! We need Jacobian!

Tomasz Golan MC generators @ NuSTEC 35 / 40 Calculating cross section

■ Express q2 in terms of angle:

2 2 2 2 q =(k k′) = m 2kk′ = m 2EE′ + 2 ~k ~k′ cos θ − − − | || |

■ Thus, the Jacobian is given by:

2 dq = 2 ~k ~k′ d(cos θ) | || | Note: must be calculated in CMS

■ Total cross section is given by:

1 2 2 2 M GF cos θC 2 2 (s − u) 2 (s − u) ~ ~ ′ σ = 2 A(q ) ∓ B(q ) 2 + C(q ) 4 2|k||k |d cos θ Z 8πEν  M M  −1

N 2 2 2 2 M GF cos θC 2 2 (si − ui) 2 (si − ui) ~ ~ ′ σMC = 2 A(qi ) ∓ B(qi ) 2 + C(qi ) 4 2|ki||ki| N 8πEν  M M  Xi=1

Tomasz Golan MC generators @ NuSTEC 36 / 40 Calculating cross section

Monte Carlo method ■ We want to avoid any Quasi-elastic scattering 1 QEL on free N sharp peaks Generating kinematics LAB ⇆ CMS Cross section ■ They affect our efficiency Generating events 0.5 A few more steps and accuracy [arbitrary units] θ ■ dσ Lets change variable once cos

d 0 again: 1 0.5 0 0.5 1 − − cos θ cos θ = 1 2x2 − where x [0, 1] 1 ∈ ■ Note extra Jacobian and new integration limits 0.5

1 0 1 [arbitrary units] dx 2 d(cos θ) dx( 4x) 4xdx dσ 0 Z → Z − → Z 1 1 0 0 0.5 1 − x

Tomasz Golan MC generators @ NuSTEC 37 / 40 Calculating cross section

■ Finally, the cross section is given by:

1 2 2 2 M GF cos θC 2 2 (s − u) 2 (s − u) ~ ~ ′ σ = 2 A(q ) ∓ B(q ) 2 + C(q ) 4 2|k||k |4xdx Z 8πEν  M M  0 N 2 2 2 1 M GF cos θC 2 2 (si − ui) 2 (si − ui) ~ ~ ′ σMC = 2 A(qi ) ∓ B(qi ) 2 + C(qi ) 4 2|ki||ki|4x N 8πEν  M M  Xi=1

■ In conclusion: do some kinematics and some boosts between CMS and LAB, change integration variable several times... and you are ready to calculate total cross section

■ Now we need to generate some events. We want them to be distributed according to our cross section formula.

Tomasz Golan MC generators @ NuSTEC 38 / 40 Generating events

■ Generate x [0 : 1] Monte Carlo method ∈ Quasi-elastic scattering ■ Do kinematics QEL on free N Generating kinematics LAB ⇆ CMS σmax Cross section Generating events x cos θ A few more steps → cos θ k′∗,p′∗ → k′∗,p′∗ k′,p′ → [arbitrary units] . dx . dσ . 0 0 0.5 1 x ■ Calculate cross section σ

■ Accept an event with the probability given by σ P = σmax ■ And you almost have you MC neutrino-event generator, just a few more steps...

Tomasz Golan MC generators @ NuSTEC 39 / 40 A few more steps

■ add other dynamics: resonance pion production, deep inelastic Monte Carlo method scattering... Quasi-elastic scattering QEL on free N Generating kinematics LAB ⇆ CMS ■ add support for nucleus as a target Cross section Generating events A few more steps ■ if you have nucleus add some two-body current interactions

■ if you have nucleus add some nuclear effects: Pauli blocking, final state interactions, formation zone... ■ add support for neutrino beam

■ add support for detector geometry

■ add some interface to set up simulations parameters and saving the output

■ and your MC is done!

Tomasz Golan MC generators @ NuSTEC 40 / 40