make_pion_angle.py
Go to the documentation of this file.
1 import ROOT as RT
2 from argparse import ArgumentParser as ap
3 from math import exp
4 parser = ap()
5 
6 parser.add_argument("-i", type=str, help='Input file', default = "")
7 parser.add_argument("-o", type=str, help='Output file', default = "angle_weight.root")
8 parser.add_argument("-t", type=int, help='Shift type', default=0)
9 parser.add_argument("-p", type=float, help='Position -- only for gaus', default=.5)
10 args = parser.parse_args()
11 
12 f = RT.TFile(args.i, "OPEN")
13 t = f.Get("pduneana/beamana")
14 
15 end_p = [0., .4, .6, .8, 1.]
16 nbins = [ 5, 25, 25, 25, 10]
17 shifts = [ 2, 12, 12, 12, 5]
18 
19 RT.gROOT.SetBatch()
20 fout = RT.TFile(args.o, "RECREATE")
21 
22 rs = []
23 
24 def shift_bins(n_bins, shift):
25  bins = range(1, n_bins+1)
26  new_bins = [i + shift for i in range(1, len(bins)-shift+1)] + [i - len(bins) + shift for i in range(len(bins)-shift+1, len(bins)+1)]
27  print(new_bins)
28  return new_bins
29 for i in range(1, 12):
30  shift_bins(10, i)
31 
32 def flip_pions(h_in):
33  h2 = h_in.Clone()
34  for j in range(1, h2.GetNbinsX()+1):
35  h2.SetBinContent(j, h.GetBinContent(h2.GetNbinsX() + 1 - j))
36  return h2
37 
38 def flat(h_in):
39  h2 = h_in.Clone()
40  for j in range(1, h2.GetNbinsX()+1):
41  h2.SetBinContent(j, 1.)
42  h2.Scale(1. / h2.Integral())
43  return h2
44 
45 def shift_pions(h_in, i):
46  new_bins = shift_bins(h_in.GetNbinsX(), shifts[i])
47  h2 = h_in.Clone()
48  for j in range(1, h2.GetNbinsX()+1):
49  h2.SetBinContent(j, h.GetBinContent(new_bins[j-1]))
50  h2.Scale(1. / h2.Integral())
51  return h2
52 
53 def gaus_pions(h_in):
54  n_bins = h_in.GetNbinsX()
55  width = n_bins/4.
56  mean = n_bins*args.p
57  h2 = h_in.Clone()
58  for i in range(1, n_bins+1):
59  h2.SetBinContent(i, exp(-.5*(((i - mean)/width)**2)))
60  h2.Scale(1. / h2.Integral())
61  return h2
62 
63 def check_bins(h_in):
64  for i in range(1, h_in.GetNbinsX()+1):
65  if h_in.GetBinContent(i) == 0.:
66  print(h_in.GetName(), "empty:", i)
67 
68 def get_variation(h_in, t, j = 0):
69  if t == 0:
70  return flip_pions(h_in)
71  elif t == 1:
72  return flat(h_in)
73  elif t == 2:
74  return shift_pions(h_in, j)
75  elif t == 3:
76  return gaus_pions(h_in)
77  else:
78  print("error, unkown type", t)
79 
80 for i in range(1, len(end_p)):
81  print("hist", i, "nbins:", nbins[i-1])
82  h = RT.TH1D("h" + str(i), "", nbins[i-1], -1., 1.)
83  t.Draw("(true_beam_daughter_startPz*true_beam_endPz + true_beam_daughter_startPx*true_beam_endPx + true_beam_daughter_startPy*true_beam_endPy)/(true_beam_daughter_startP*true_beam_endP)" +
84  ">>h" + str(i),
85  #">>h" + str(i) + "(" + str(nbins[i-1]) + "-1., 1.)",
86  "true_daughter_nPiPlus == 1 && true_daughter_nPiMinus == 0 && " +
87  "new_interaction_topology == 3 && true_beam_PDG == 211 && abs(true_beam_daughter_PDG) == 211 && true_beam_endP >= " + str(end_p[i-1]) + " && true_beam_endP < " + str(end_p[i]))
88  #h = RT.gDirectory.Get("h" + str(i))
89  h.Scale(1./h.Integral())
90  h.Write()
91 
92  #if args.t == 0:
93  # h2 = flip_pions(h)
94  #else:
95  # h2 = flat(h)
96  h2 = get_variation(h, args.t, i-1)
97  print(h2.Integral())
98  r = h2.Clone()
99  r.Divide(h)
100  h2.Write("h" + str(i) + "_rev")
101  r.Write("r" + str(i))
102  rs.append(r)
103 
104 h = RT.TH1D("h" + str(len(end_p)), "", nbins[-1], -1., 1.)
105 t.Draw("(true_beam_daughter_startPz*true_beam_endPz + true_beam_daughter_startPx*true_beam_endPx + true_beam_daughter_startPy*true_beam_endPy)/(true_beam_daughter_startP*true_beam_endP)" +
106  ">>h" + str(len(end_p)),
107  #">>h" + str(len(end_p)) + "(" + str(nbins[-1]) + "-1., 1.)",
108  "true_daughter_nPiPlus == 1 && true_daughter_nPiMinus == 0 && " +
109  "new_interaction_topology == 3 && true_beam_PDG == 211 && abs(true_beam_daughter_PDG) == 211 && true_beam_endP >= " + str(end_p[-1]))
110 #h = RT.gDirectory.Get("h" + str(len(end_p)))
111 h.Scale(1./h.Integral())
112 h.Write()
113 #if args.t == 0:
114 # h2 = flip_pions(h)
115 #else:
116 # h2 = flat(h)
117 h2 = get_variation(h, args.t, len(end_p)-1)
118 print(h2.Integral())
119 r = h2.Clone()
120 r.Divide(h)
121 h2.Write("h" + str(len(end_p)) + "_rev")
122 r.Write("r" + str(len(end_p)))
123 rs.append(r)
124 
125 
126 fout.Close()
127 
def check_bins(h_in)
def shift_pions(h_in, i)
def get_variation(h_in, t, j=0)
def gaus_pions(h_in)
def flip_pions(h_in)
def shift_bins(n_bins, shift)