SND@LHC Software
Loading...
Searching...
No Matches
genie_interface.py
Go to the documentation of this file.
1from __future__ import print_function
2from __future__ import division
3import ROOT, os, sys, time
4from subprocess import call
5
6
7def get_1D_flux_name(nupdg):
8 """returns name of TH1D p spectrum as stored in input files:
9 example: nue: 12 -> 1012, anue: -12 -> 2012
10 """
11 x = ROOT.TMath.Abs(nupdg)
12 if nupdg > 0:
13 return "10" + str(x)
14 else:
15 return "20" + str(x)
16
17
19 """returns name of TH2D p-pt flux as stored in input files:
20 ie for nue: 12 -> 1212, anue: -12 -> 2212
21 nupdg: neutrino pdg
22 """
23 x = ROOT.TMath.Abs(nupdg)
24 if nupdg > 0:
25 return "12" + str(x)
26 else:
27 return "22" + str(x)
28
29
30def make_splines(nupdglist, targetcode, emax, nknots, outputfile):
31 """prepare splines with neutrino interaction cross sections
32 nupdg = list of input neutrino pdgs
33 targetcode = string with target material in GENIE code
34 outputfile = path of outputfile
35 """
36 inputnupdg = ""
37 for ipdg, nupdg in enumerate(nupdglist):
38 if ipdg > 0:
39 inputnupdg = inputnupdg + ","
40 inputnupdg = inputnupdg + str(nupdg)
41 cmd = (
42 "gmkspl -p "
43 + inputnupdg
44 + " -t "
45 + targetcode
46 + " -n "
47 + str(nknots)
48 + " -e "
49 + str(emax)
50 + " -o "
51 + outputfile
52 )
53 print("Starting GENIE with the following command: ")
54 print(cmd)
55 call(cmd, shell=True)
56
57
59 nevents,
60 nupdg,
61 emin,
62 emax,
63 targetcode,
64 inputflux,
65 spline,
66 process=None,
67 seed=None,
68 irun=None,
69):
70 """make Genie simulation, parameters:
71 events = number of events to generate
72 nupdg = neutrino pdg
73 targetcode = string with target material in GENIE code
74 emin, emax = min and max neutrino energy to generate
75 process = simulate a specific neutrino process (CCDIS, CCQE, CC, NC, CCRES, NCRES, etc.),
76 if not set, GENIE's comprehensive collection of event generators will be used.
77 inputflux = input neutrino flux
78 spline = input neutrino spline
79 """
80 # prepare command functions
81 cmd = (
82 "gevgen -n "
83 + str(nevents)
84 + " -p "
85 + str(nupdg)
86 + " -t "
87 + targetcode
88 + " -e "
89 + str(emin)
90 + ","
91 + str(emax)
92 )
93 cmd = (
94 cmd
95 + " -f "
96 + inputflux
97 + ","
98 + get_1D_flux_name(nupdg)
99 + " --cross-sections "
100 + spline
101 )
102 # optional additional arguments
103 if process is not None:
104 cmd = cmd + " --event-generator-list " + process # add a specific process
105 if seed is not None:
106 cmd = cmd + " --seed " + str(seed) # set a seed for the generator
107 if irun is not None:
108 cmd = cmd + " --run " + str(irun)
109
110 print("Starting GENIE with the following command: ")
111 print(cmd)
112 call(cmd, shell=True)
113
114
115def make_ntuples(inputfile, outputfile):
116 """convert gntp GENIE file to gst general ROOT file
117 inputfile = path of gntp inputfile (gntp.0.ghep.root)
118 outputfile = path of gst outputfile
119 """
120
121 cmd = "gntpc -i " + inputfile + " -f gst -o " + outputfile
122 print("Starting GENIE conversion with the following command: ")
123 print(cmd)
124 call(cmd, shell=True)
125
126
127def add_hists(inputflux, simfile, nupdg):
128 """add histogram with p-pt flux to simulation file
129 inputflux = path of neutrino inputflux
130 simfile = path of simulation file to UPDATE
131 nupdg = neutrino pdg
132 """
133 inputfile = ROOT.TFile(inputflux, "read")
134 simfile = ROOT.TFile(simfile, "update")
135 # adding 2D histogram
136 inputfile.Get(get_2D_flux_name(nupdg)).Write()
137 # closinsg files
138 inputfile.Close()
139 simfile.Close()
make_ntuples(inputfile, outputfile)
make_splines(nupdglist, targetcode, emax, nknots, outputfile)
generate_genie_events(nevents, nupdg, emin, emax, targetcode, inputflux, spline, process=None, seed=None, irun=None)
add_hists(inputflux, simfile, nupdg)