SND@LHC Software
Loading...
Searching...
No Matches
extract_interacting_neutrinos.py
Go to the documentation of this file.
1"""from the neutrinos produced by the FLUKA simulation, extract a subsample of interacting according to the GENIE cross section"""
2
3import argparse
4import ROOT as r
5
6parser = argparse.ArgumentParser(description="Extract interacting neutrinos")
7parser.add_argument(
8 "--geniepath",
9 type=str,
10 dest="geniepath",
11 default=None,
12 help="Path of Genie simulated neutrinos interactions, where also to store the subsample of interacting Fluka neutrinos",
13)
14parser.add_argument(
15 "--flukapath",
16 type=str,
17 dest="flukapath",
18 default=None,
19 help="Path of Fluka produced neutrinos",
20)
21
22options = parser.parse_args()
23
24# inputfiles
25geniefile = r.TFile.Open(options.geniepath, "UPDATE")
26flukafile = r.TFile.Open(options.flukapath, "READ")
27# Retrieve neutrino energy histogram from FLUKA file
28
29
30def lookfornuhist(flukafile):
31 """the actual name of the histograms depend on the neutrino we are reading,
32 i loop over all possible candidates"""
33 nup_histnames = ["1012", "1014", "1016", "2012", "2014", "2016"]
34 myhist = r.TH1F()
35 for name in nup_histnames:
36 temphist = flukafile.Get(name)
37 if temphist:
38 myhist = temphist
39 return myhist
40
41
42hfluka_nuE = lookfornuhist(flukafile)
43nbins = hfluka_nuE.GetNbinsX()
44minE = hfluka_nuE.GetXaxis().GetBinLowEdge(1)
45maxE = hfluka_nuE.GetXaxis().GetBinUpEdge(nbins)
46
47# Draw neutrino energy from GENIE file (bins must be the same of fluka files)
48df = r.RDataFrame("gst", geniefile)
49hgenie_nuE = df.Histo1D(
50 (
51 "hgenie_nuE",
52 "Energy of muon neutrinos from genie spectrum;E[GeV]",
53 nbins,
54 minE,
55 maxE,
56 ),
57 "pzv",
58)
59
60# drawing distributions to check them
61
62c1 = r.TCanvas()
63hfluka_nuE.Draw("histo")
64
65c2 = r.TCanvas()
66hgenie_nuE.DrawClone("histo")
67
68# computing cross section distributions
69# correctly handling errors in division
70
71hfluka_nuE.Sumw2()
72hgenie_nuE.Sumw2()
73
74hnuratio = r.TH1D(
75 "hnuratio",
76 "ratio between Genie and Fluka energies, proportional to the cross section",
77 nbins,
78 minE,
79 maxE,
80)
81# making division and plotting ratio distribution
82hnuratio.Divide(hgenie_nuE.GetPtr(), hfluka_nuE)
83
84c3 = r.TCanvas()
85hnuratio.Draw("histo")
86
87# hit or miss section
88maxratio = hnuratio.GetMaximum()
89
90# cloning fluka tree into an empty output tree
91flukatree = flukafile.Get("t")
92output_nutree = flukatree.CloneTree(0)
93output_nutree.SetName("fluka_neutrinos_selected")
94output_nutree.SetTitle("Neutrinos from Fluka simulation after hit and miss selection")
95
96nneutrinos = flukatree.GetEntries()
97print("Starting loop over {} neutrinos".format(nneutrinos))
98
99for nuevent in flukatree:
100 Ekin = nuevent.Ekin
101 # get uniform random number between 0 and maximum ratio
102 randomratio = r.gRandom.Uniform(0, maxratio)
103 # finding ratio for that neutrino energy
104 matchingratio = hnuratio.GetBinContent(hnuratio.FindBin(Ekin))
105 # if the found ratio is larger than random number, accept neutrino as interacting
106 if matchingratio > randomratio:
107 output_nutree.Fill()
108# writing tree and closing file
109
110# writing tree, closing file
111geniefile.cd()
112output_nutree.Write("fluka_neutrinos_selected",r.TFile.kOverwrite)
113geniefile.Close()