SND@LHC Software
Loading...
Searching...
No Matches
makeGenieEvents.py
Go to the documentation of this file.
1#!/usr/bin/env python
2from __future__ import print_function
3from __future__ import division
4import ROOT,os,sys,time
5from subprocess import call
6import shipunit as u
7import shipRoot_conf
8import argparse
9import logging
10import genie_interface
12
13
14# IMPORTANT
15# Before runnig this script please run this command in FairShip bash if you are dealing with the neutrino detector:
16# export GXMLPATH='/eos/experiment/ship/user/aiuliano/GENIE_FNAL_nu_splines'
17# this will disable Genie decays for charm particles and tau
18
19
20
21xsec = "gxspl-FNAL-nuSHiP-minimal.xml"# new adapted splines from Genie site
22hfile = "pythia8_Geant4_1.0_withCharm_nu.root" #2018 background generation
23#xsec = "Nu_splines.xml"
24#hfile = "pythia8_Geant4-withCharm_onlyNeutrinos.root"
25
26
27defaultsplinedir = '/eos/experiment/ship/user/aiuliano/GENIE_FNAL_nu_splines' #path of splines
28defaultfiledir = '/eos/experiment/ship/data/Mbias/background-prod-2018' #path of flux
29
30
31
32
33
34
35def get_arguments(): #available options
36
37 parser = argparse.ArgumentParser(
38 description='Run GENIE neutrino" simulation')
39 subparsers = parser.add_subparsers()
40 ap = subparsers.add_parser('sim',help="make genie simulation file")
41
42 ap.add_argument('-s', '--seed', type=int, dest='seed', default=65539) #default seed in $GENIE/src/Conventions/Controls.h
43 ap.add_argument('-o','--output' , type=str, help="output directory", dest='work_dir', default=None)
44 ap.add_argument('-f','--filedir', type=str, help="directory with neutrino fluxes", dest='filedir', default=defaultfiledir)
45 ap.add_argument('-c','--crosssectiondir', type=str, help="directory with neutrino splines crosssection", dest='splinedir', default=defaultsplinedir)
46 ap.add_argument('-t', '--target', type=str, help="target material", dest='target', default='iron')
47 ap.add_argument('-n', '--nevents', type=int, help="number of events", dest='nevents', default=100)
48 ap.add_argument('-e', '--event-generator-list', type=str, help="event generator list", dest='evtype', default=None) # Possbile evtypes: CC, CCDIS, CCQE, CharmCCDIS, RES, CCRES, see other evtypes in $GENIE/config/EventGeneratorListAssembler.xml
49 ap.add_argument("--nudet", dest="nudet", help="option for neutrino detector", required=False, action="store_true")
50
51 ap1 = subparsers.add_parser('spline',help="make a new cross section spline file")
52 ap1.add_argument('-t', '--target', type=str, help="target material", dest='target', default='iron')
53 ap1.add_argument('-o','--output' , type=str, help="output directory", dest='work_dir', default=None)
54 args = parser.parse_args()
55 return args
56
57args = get_arguments() #getting options
58
59print('Target type: ', args.target)
60
61if args.target == 'iron':
62 targetcode = '1000260560'
63elif args.target == 'lead':
64 targetcode = '1000822040[0.014],1000822060[0.241],1000822070[0.221],1000822080[0.524]'
65elif args.target == 'tungsten':
66 targetcode = '1000741840'
67else:
68 print('only iron, lead and tunsgten target options available')
69 1/0
70
71if os.path.exists(args.work_dir): #if the directory is already there, leave a warning, otherwise create it
72 print('output directory already exists.')
73else:
74 os.makedirs(args.work_dir)
75
76os.chdir(args.work_dir)
77
79 '''first step, make cross section splines if not exist'''
80 nupdglist = [16,-16,14,-14,12,-12]
81 genie_interface.make_splines(nupdglist, targetcode, 400, nknots = 500, outputfile = "xsec_splines.xml")
82
83def makeEvents(nevents = 100):
84 run = 11
85 for p in pDict:
86 if p<0: print("scale number of "+sDict[p]+" events with %5.2F"%(1./nuOverNubar[abs(p)]))
87 if not sDict[p] in os.listdir('.'): call('mkdir '+sDict[p],shell = True)
88 os.chdir('./'+sDict[p])
89 # stop at 350 GeV, otherwise strange warning about "Lower energy neutrinos have a higher probability of
90 # interacting than those at higher energy. pmaxLow(E=386.715)=2.157e-13 and pmaxHigh(E=388.044)=2.15623e-13"
91 N = nevents
92 if p<0: N = int(nevents / nuOverNubar[abs(p)])
93 genie_interface.generate_genie_events(nevents = N, nupdg = p, targetcode = targetcode, emin = 0.5, emax = 350,\
94 inputflux = neutrinos, spline = splines, seed = args.seed, process = args.evtype, irun = run)
95 run +=1
96 os.chdir('../')
98 for p in pDict:
99 os.chdir('./'+sDict[p])
100 genie_interface.make_ntuples("gntp.0.ghep.root","genie-"+sDict[p]+".root")
101 genie_interface.add_hists(neutrinos, "genie-"+sDict[p]+".root", p)
102 os.chdir('../')
103
105 for p in pDict:
106 os.chdir('./'+sDict[p])
107 genie_interface.add_hists(neutrinos, "genie-"+sDict[p]+".root", p)
108 os.chdir('../')
109
110if ("splinedir" not in args):
112
113else:
114
115 if args.nudet:
116 if 'GXMLPATH' not in os.environ:
117 logging.warn('GXMLPATH is not set: Genie will decay charm and tau particles, which is usually not the desired behaviour')
118 else: logging.debug('GXMLPATH is set: Genie will not decay charm and tau particles')
119
120 splines = args.splinedir+'/'+xsec #path of splines
121 neutrinos = args.filedir+'/'+hfile #path of flux
122
123 print('Seed used in this generation: ', args.seed)
124 print('Splines file used', xsec)
125
126 pdg = ROOT.TDatabasePDG()
127 pDict = {}
128 sDict = {}
129 nuOverNubar = {}
130 f = ROOT.TFile(neutrinos)
131
132 for x in [16, 14,12]:
133 sDict[x] = pdg.GetParticle(x).GetName()
134 sDict[-x] = pdg.GetParticle(-x).GetName()
135 pDict[x] = "10"+str(x)
136 pDict[-x] = "20"+str(x)
137 nuOverNubar[x] = f.Get(pDict[x]).GetSumOfWeights()/f.Get(pDict[-x]).GetSumOfWeights()
138 f.Close()
139
140 makeEvents(args.nevents)
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)
makeEvents(nevents=100)
configure(darkphoton=None)