-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_final.py
148 lines (128 loc) · 4.47 KB
/
analysis_final.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
#!/usr/bin/env python3
import ROOT
from math import ceil, floor
import numpy as np
import os
ROOT.gROOT.SetBatch(True)
ROOT.gStyle.SetOptFit(1111)
ROOT.gStyle.SetPalette(ROOT.kRainBow)
ROOT.gStyle.SetMarkerSize(2)
ROOT.gStyle.SetMarkerStyle(33)
# ROOT.gStyle.SetMarkerStyle(ROOT.kDot)
# ParticleList = ["mu", "e", "pi"]
ParticleList = ["mu-", "e-"]
# ParticleList = ["mu"]
# ParticleList = ["e-"]
ThetaList = ["10", "20", "30", "40", "50", "60", "70", "80", "89"]
# ThetaList = ["10", "20"]
# ThetaList = ["89"]
# MomentumList = ["1", "2", "5", "10", "20", "50", "100", "200"]
MomentumList = ["1", "2", "5", "10", "20", "50", "100"]
stackMomentumList = ["1", "10", "100"]
stackThetaList = ["10", "30", "50", "70", "89"]
def pname(particle, theta, momentum):
return f"{particle}_{theta}deg_{momentum}GeV_1000evt"
# len(edges) == len(counts) + 1
def make_bins(edges, counts):
res = []
for i in range(len(counts)):
dist = edges[i + 1] - edges[i]
inc = dist / counts[i]
res.extend([edges[i] + j * inc for j in range(counts[i])])
return res
processList = {
pname(particle, theta, momentum): {}
for particle in ParticleList
for theta in ThetaList
for momentum in MomentumList
}
# print(processList)
# detectorModel = "CLD_o2_v05"
detectorModel = "FCCee_o1_v04"
outputDir = f"Output/final/{detectorModel}"
if not os.path.exists(outputDir):
os.makedirs(outputDir)
inputDir = f"Output/stage2/{detectorModel}"
residualList = ["d0", "z0", "phi0", "omega", "tanLambda", "phi", "theta"]
specialList = ["pt", "p"]
varList = [f"delta_{v}" for v in residualList] + [f"sdelta_{v}" for v in specialList]
title = {
"delta_d0": "#sigma(#Deltad_{0})[#mum]",
"delta_z0": "#sigma(#Deltaz_{0})[#mum]",
"delta_phi0": "#Delta#phi_{0}",
"delta_omega": "#Delta#Omega",
"delta_tanLambda": "tan#Lambda",
"delta_phi": "#sigma(#Delta#phi)[mrad]",
"delta_theta": "#sigma(#Delta#theta)[mrad]",
"sdelta_pt": "#sigma(#Deltap_{T}/p_{T,true}^{2})[GeV^{-1}]",
"sdelta_p": "#sigma(#Deltap/p_{true}^{2})[GeV^{-1}]",
}
df = {}
var_col_rp = {}
# get column for each variable by running event loop once
for p in processList:
df[p] = ROOT.RDataFrame("events", f"{inputDir}/{p}.root")
for v in specialList:
df[p] = df[p].Define(f"sdelta_{v}", f"delta_{v} / (true_{v} * true_{v})")
var_col_rp[p] = {}
for v in varList:
var_col_rp[p][v] = df[p].Take["double"](v)
var_col = {}
var_low = {}
var_high = {}
h = {}
# get bin borders and run again to make histograms
for p in processList:
var_col[p] = {}
var_low[p] = {}
var_high[p] = {}
h[p] = {}
for v in varList:
var_col[p][v] = sorted(var_col_rp[p][v].GetValue())
var_low[p][v] = var_col[p][v][ceil(0.05 * len(var_col[p][v]))]
var_high[p][v] = var_col[p][v][floor(0.95 * len(var_col[p][v]))]
# TODO: calculate special bins here
bins = (50, var_low[p][v], var_high[p][v])
if p.startswith("e") and v in ["delta_omega", "sdelta_pt", "sdelta_p"]:
low = var_low[p][v]
high = var_high[p][v]
dist = high - low
special_bins = make_bins(
[low, low + 0.7 * dist, low + 0.8 * dist, low + 0.9 * dist, high],
[1, 2, 3, 25],
)
bins = (len(special_bins) - 1, np.asarray(special_bins))
h[p][v] = (
df[p]
.Filter(f"{v} > {var_low[p][v]} && {v} < {var_high[p][v]}")
.Histo1D((v, f"{p};{title[v]}", *bins), v)
)
# after both runs do fits and make plots
outfile = ROOT.TFile(f"{outputDir}/resolutions.root", "recreate")
for p in processList:
dir = outfile.mkdir(p)
dir.cd()
# fname = f"{outputDir}/{p}.pdf"
c = ROOT.TCanvas()
# c.Print(f"{fname}[")
for v in varList:
f = None
# scale histogram bins here
h[p][v].Scale(1, "width")
if p.startswith("e") and v in ["delta_omega", "sdelta_pt", "sdelta_p"]:
f = ROOT.TF1(
f"f_{p}_{v}",
"ROOT::Math::crystalball_function(x, [0], [1], [2], [3])*[4]",
var_low[p][v],
var_high[p][v],
)
# alpha, n, sigma, mu
f.SetParameters(1, 1, abs(h[p][v].GetMean()), 0, h[p][v].GetMaximum())
else:
f = ROOT.TF1(f"f_{p}_{v}", "gaus", var_low[p][v], var_high[p][v])
h[p][v].Fit(f, "R")
h[p][v].Draw()
h[p][v].Write()
# c.Print(fname)
# c.Print(f"{fname}]")
outfile.Close()