make_THn_beam_input.py
Go to the documentation of this file.
1 import ROOT as RT
2 from array import array
3 from argparse import ArgumentParser as ap
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="beam_pdf.root")
8 parser.add_argument("-n", type=int, help='Max number of entries', default=-1)
9 parser.add_argument("-p", type=str, help='Which momentum', default="1")
10 args = parser.parse_args()
11 
12 
13 if not args.i:
14  exit()
15 
16 inputFile = RT.TFile(args.i, "OPEN")
17 tree = inputFile.Get("beamreco/tree")
18 outputFile = RT.TFile(args.o, "RECREATE")
19  #p, fvu, fhu, fvd, fhd
20 nBins = array("i", [100, 32, 32, 32, 32])
21 
22 if args.p == "1":
23  min_p = 0.
24  max_p = 2.
25 elif args.p == "0.5":
26  nBins[0] = 40
27  min_p = 0.4
28  max_p = 0.6
29 elif args.p == "2":
30  min_p = 1.
31  max_p = 3.
32 elif args.p == "3":
33  min_p = 2.
34  max_p = 4.
35 elif args.p == "6":
36  min_p = 4.
37  max_p = 8.
38 elif args.p == "7":
39  nBins = array("i", [50, 32, 32, 32, 32])
40  min_p = 5.
41  max_p = 9.
42 
43 mins = array("d", [min_p, 0., 0., 0., 0.])
44 maxes = array("d", [max_p, 192., 192., 192., 192.])
45 
46 Pions = RT.THnSparseD("Pions", "", 5, nBins, mins, maxes)
47 Protons = RT.THnSparseD("Protons", "", 5, nBins, mins, maxes)
48 Electrons = RT.THnSparseD("Electrons", "", 5, nBins, mins, maxes)
49 Kaons = RT.THnSparseD("Kaons", "", 5, nBins, mins, maxes)
50 
51 if args.p in ["0.5", "1", "2", "3"]:
52  pdfs = [Pions, Protons, Electrons]
53 elif args.p in ["6", "7"]:
54  pdfs = [Pions, Protons, Kaons]
55 
56 counter = 0
57 
58 if args.n < 0:
59  max_entries = tree.GetEntries()
60 else:
61  max_entries = args.n
62 
63 for e in tree:
64  if counter >= max_entries: break
65  f_v_up = [i for i in e.fibers_v_upstream]
66  f_h_up = [i for i in e.fibers_h_upstream]
67  f_v_down = [i for i in e.fibers_v_downstream]
68  f_h_down = [i for i in e.fibers_h_downstream]
69  perfectP = e.perfectP
70  Momentum = e.Momentum
71  pdgs = [i for i in e.possible_pdg]
72 
73 
74  if not (perfectP and len(f_v_up) == 1 and len(f_h_up) == 1 and
75  len(f_v_down) == 1 and len(f_h_down) == 1 and len(pdgs) > 0):
76  continue
77 
78  if Momentum < min_p or Momentum > max_p: continue
79 
80  data = array("d", [Momentum, f_v_up[0], f_h_up[0], f_v_down[0], f_h_down[0]])
81 
82  if args.p in ["1", "2", "0.5"]:
83  if pdgs[0] == 2212:
84  Protons.Fill(data)
85  elif pdgs[0] == 13:
86  Pions.Fill(data)
87  elif pdgs[0] == 11:
88  Electrons.Fill(data)
89 
90  elif args.p == "3":
91  if 13 in pdgs:
92  Pions.Fill(data)
93  elif 2212 in pdgs:
94  Protons.Fill(data)
95  elif 11 in pdgs:
96  Electrons.Fill(data)
97 
98  elif args.p in ["6", "7"]:
99  if 13 in pdgs:
100  Pions.Fill(data)
101  elif 2212 in pdgs:
102  Protons.Fill(data)
103  elif 321 in pdgs:
104  Kaons.Fill(data)
105 
106  counter += 1
107 
108 outputFile.cd()
109 
110 for pdf in pdfs:
111  if pdf.Projection(0).Integral() > 0:
112  pdf.Scale(1./pdf.Projection(0).Integral())
113  pdf.Write()
114 
115 outputFile.Close()
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228