This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
analyse.py
executable file
·191 lines (179 loc) · 6.69 KB
/
analyse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python2
"""Functions to analyse and visualise simulation results."""
import argparse
from array import array
import numpy as np
import ROOT as r
import shipunit as u
import rootUtils as ut
from get_geo import get_geo
from disney_common import FCN
def graph_tracks(event):
"""Create graphs of tracks in event by interpolating between vetoPoints."""
hits = []
fPos = r.TVector3()
for n, track in enumerate(event.MCTrack):
track.GetStartVertex(fPos)
hitlist = {}
hitlist[fPos.Z()] = [
fPos.X(), fPos.Y(), fPos.Px(), fPos.Py(), fPos.Pz()
]
# loop over all sensitive volumes to find hits
for hit in event.vetoPoint:
if hit.GetTrackID() == (n - 1):
lp = hit.LastPoint()
lm = hit.LastMom()
assert not (lp.x() == lp.y() and lp.x() == lp.z() and
lp.x() == 0)
# must be old data, don't expect hit at 0,0,0
# first point:
# Entry point as p in centre of volume, halfway between
# entry and exit
hitlist[2. * hit.GetZ() - lp.z()] = [
2. * hit.GetX() - lp.x(), 2. * hit.GetY() - lp.y(), 0.0,
0.0, 0.0
]
# last point:
hitlist[lp.z()] = [lp.x(), lp.y(), lm.Px(), lm.Py(), lm.Pz()]
if len(hitlist) == 1:
if track.GetMotherId() < 0:
continue
zs = hitlist.keys()
for z in zs:
hits.append([hitlist[z][0], hitlist[z][1], z])
# sort in z
hits.sort(key=lambda x: x[2])
xs, ys, zs = zip(*hits)
return r.TGraph(len(hits), array('f', zs), array('f', xs)), r.TGraph(
len(hits), array('f', zs), array('f', ys))
def analyse(tree, outputfile):
"""Analyse tree to find hit positions and create histograms.
Parameters
----------
tree
Tree or Chain of trees with the usual `cbmsim` format
outputfile : str
Filename for the file in which the histograms are saved,
will be overwritten
Returns
-------
std::vector<double>
Vector of hit x-positions [cm]
"""
r.gROOT.SetBatch(True)
maxpt = 6.5
maxp = 360.
f = r.TFile.Open(outputfile, 'recreate')
f.cd()
h = {}
ut.bookHist(h, 'mu_pos', '#mu- hits;x[cm];y[cm]', 100, -1000, +1000, 100,
-800, 1000)
ut.bookHist(h, 'anti-mu_pos', '#mu+ hits;x[cm];y[cm]', 100, -1000, +1000,
100, -800, 1000)
ut.bookHist(h, 'mu_w_pos', '#mu- hits;x[cm];y[cm]', 100, -1000, +1000, 100,
-800, 1000)
ut.bookHist(h, 'anti-mu_w_pos', '#mu+ hits;x[cm];y[cm]', 100, -1000, +1000,
100, -800, 1000)
ut.bookHist(h, 'mu_p', '#mu+-;p[GeV];', 100, 0, maxp)
ut.bookHist(h, 'mu_p_original', '#mu+-;p[GeV];', 100, 0, maxp)
ut.bookHist(h, 'mu_pt_original', '#mu+-;p_t[GeV];', 100, 0, maxpt)
ut.bookHist(h, 'mu_ppt_original', '#mu+-;p[GeV];p_t[GeV];', 100, 0, maxp,
100, 0, maxpt)
ut.bookHist(h, 'smear', '#mu+- initial vertex;x[cm];y[cm]', 100, -10, +10,
100, -10, 10)
xs = r.std.vector('double')()
i, n = 0, tree.GetEntries()
print '0/{}\r'.format(n),
mom = r.TVector3()
for event in tree:
i += 1
if i % 1000 == 0:
print '{}/{}\r'.format(i, n),
original_muon = event.MCTrack[1]
h['smear'].Fill(original_muon.GetStartX(), original_muon.GetStartY())
draw = False
weight = 0.
for hit in event.vetoPoint:
if hit:
if not hit.GetEnergyLoss() > 0:
continue
pid = hit.PdgCode()
if (
hit.GetZ() > 0 and
abs(pid) == 13
):
hit.Momentum(mom)
P = mom.Mag() / u.GeV
y = hit.GetY()
x = hit.GetX()
if pid == 13:
h['mu_pos'].Fill(x, y)
else:
h['anti-mu_pos'].Fill(x, y)
x *= pid / 13.
if (
P > 1 and
abs(y) < 5 * u.m and
(x < 2.6 * u.m and x > -3 * u.m)
):
w = np.sqrt((560. - (x + 300.)) / 560.)
weight += w
h['mu_p'].Fill(P)
original_muon = event.MCTrack[1]
h['mu_p_original'].Fill(original_muon.GetP())
h['mu_pt_original'].Fill(original_muon.GetPt())
h['mu_ppt_original'].Fill(original_muon.GetP(),
original_muon.GetPt())
if pid == 13:
h['mu_w_pos'].Fill(x, y, w)
draw = 4
else:
h['anti-mu_w_pos'].Fill(-x, y, w)
draw = 6
xs.push_back(weight)
if draw:
graph_x, graph_y = graph_tracks(event)
graph_x.SetLineColor(draw)
graph_y.SetLineColor(draw)
name = 'c{}'.format(i)
c = r.TCanvas(name, name, 1600, 900)
multigraph = r.TMultiGraph(
'tracks_{}'.format(i),
'Tracks in acceptance and side;z [cm];x/y [cm]')
graph_x.SetLineStyle(1)
graph_x.SetMarkerStyle(20)
graph_x.SetTitle('x-projection')
graph_x.SetFillStyle(0)
graph_y.SetTitle('y-projection')
graph_y.SetLineStyle(2)
graph_y.SetMarkerStyle(20)
graph_y.SetFillStyle(0)
multigraph.Add(graph_x, 'lp')
multigraph.Add(graph_y, 'lp')
multigraph.Draw('Alp')
c.BuildLegend()
c.Write()
print 'Loop done'
for key in h:
classname = h[key].Class().GetName()
if 'TH' in classname or 'TP' in classname:
h[key].Write()
f.Close()
return xs
def main():
"""Run standalone analysis and print FCN."""
f = r.TFile.Open(args.input, 'read')
tree = f.cbmsim
xs = analyse(tree, args.output)
L, W = get_geo(args.geofile)
fcn = FCN(W, np.array(xs), L)
print fcn, len(xs)
if __name__ == '__main__':
r.gErrorIgnoreLevel = r.kWarning
r.gSystem.Load('libpythia8')
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--input', required=True)
parser.add_argument('-g', '--geofile', required=True)
parser.add_argument('-o', '--output', default='test.root')
args = parser.parse_args()
main()