xsec_utils.py
Go to the documentation of this file.
1 import pandas as pd
2 import numpy as np
3 import h5py
4 
5 from PandAna.utils import *
6 
8  def __init__(self, df):
9  self.df = df
10  self.dim = (int)(len(self.df.columns)-1)/2
11 
12  self.xlow = df['xlow'].unique()
13  self.xhigh = df['xhigh'].unique()
14  if self.dim > 1:
15  self.ylow = df['ylow'].unique()
16  self.yhigh = df['yhigh'].unique()
17 
18  def GetNbinsX(self):
19  nxlow = len(self.xlow)
20  nxhigh = len(self.xhigh)
21  assert nxlow == nxhigh
22  return nxhigh
23 
24  def GetNbinsY(self):
25  assert self.dim > 1
26  nylow = len(self.ylow)
27  nyhigh = len(self.yhigh)
28  assert nylow == nyhigh
29  return nyhigh
30 
31  def FindXBin(self, xval):
32  if min(self.xlow) > xval:
33  return 0
34  if max(self.xhigh) <= xval:
35  return self.GetNbinsX()+1
36  else:
37  xbin_arr = np.where((self.xlow <= xval) & (self.xhigh > xval))[0]
38  assert len(xbin_arr) == 1
39  return xbin_arr[0]+1
40 
41  def FindYBin(self, yval):
42  assert self.dim > 1
43  if min(self.ylow) > yval:
44  return 0
45  if max(self.yhigh) <= yval:
46  return self.GetNbinsY()+1
47  else:
48  ybin_arr = np.where((self.ylow <= yval) & (self.yhigh > yval))[0]
49  assert len(ybin_arr) == 1
50  return ybin_arr[0]+1
51 
52  def GetBinContent(self, binx, biny=-1):
53  if biny < 0:
54  assert self.dim == 1
55  if binx < 1 or binx > self.GetNbinsX(): return 1.
56  return self.df['weight'][binx-1]
57  else:
58  assert self.dim > 1
59  if binx < 1 or biny < 1: return 1.
60  if binx > self.GetNbinsX() or biny > self.GetNbinsY(): return 1.
61  binidx = (binx-1)*self.GetNbinsY() + (biny-1)
62  return self.df['weight'][binidx]
63 
64  def FindFirstBinAbove(self, threshold, axis):
65  assert (axis == 1) or (axis < 3 and self.dim == 2)
66  firstbin = self.df[self.df['weight'] > threshold].index[0]
67  return (axis-1)*((firstbin % self.GetNbinsY())+1) + (2-axis)*((firstbin/self.GetNbinsX())+1)
68 
69  def GetValue(self, val):
70  assert self.dim == 1
71  return self.GetBinContent(self.FindXBin(val))
72 
73  def GetValueInRange(self, vals, maxrange=[-float("inf"), float("inf")], binranges=[[1,-1], [1,-1]]):
74  assert self.dim == 2
75  if binranges[0][1] < 0: binranges[0][1] = self.GetNbinsX()
76  if binranges[1][1] < 0: binranges[1][1] = self.GetNbinsY()
77 
78  binx = self.FindXBin(vals[0])
79  biny = self.FindYBin(vals[1])
80  if binx > binranges[0][1]: binx = binranges[0][1]
81  elif binx < binranges[0][0]: binx = binranges[0][0]
82  if biny > binranges[1][1]: biny = binranges[1][1]
83  elif biny < binranges[1][0]: biny = binranges[1][0]
84 
85  val = self.GetBinContent(binx, biny)
86  if val < maxrange[0]: val = maxrange[0]
87  if val > maxrange[1]: val = maxrange[1]
88  return val
89 
91  def __init__(self, fnu, fnubar, forcenu=False):
92  fnuh5 = h5py.File(fnu['file'], 'r')
94  pd.DataFrame(fnuh5.get(fnu['group']+'/block0_values')[()],
95  columns=fnuh5.get(fnu['group']+'/block0_items')[()]))
96  if not forcenu:
97  fnubarh5 = h5py.File(fnubar['file'], 'r')
99  pd.DataFrame(fnubarh5.get(fnubar['group']+'/block0_values')[()],
100  columns=fnubarh5.get(fnubar['group']+'/block0_items')[()]))
101  assert self.nu.dim == self.nubar.dim
102  self.dim = self.nu.dim
103 
104  def GetWeight(self):
105  return 1.
106 
108  def __init__(self):
109  fnu = {}
110  fnubar = {}
111  fnu['file'] = FindPandAnaDir()+"/Data/xs/RPA2017.h5"
112  fnubar['file'] = fnu['file']
113  fnu['group'] = "RPA_CV_nu"
114  fnubar['group'] = "RPA_CV_nubar"
115  NuWeightFromFile.__init__(self, fnu, fnubar)
116 
117  def GetWeight(self, params):
118  qmag = params['qmag']
119  q0 = params['q0']
120  isAntiNu = ('IsAntiNu' in params.keys()) and params['IsAntiNu']
121 
122  df = self.nu
123  if isAntiNu: df = self.nubar
124  minbin = df.FindFirstBinAbove(0, 2)
125  val = df.GetValueInRange([qmag, q0],
126  [0., 2.],
127  [[1, df.GetNbinsX()],[minbin,df.GetNbinsY()]])
128  if val == 0.: val = 1.
129  return val
130 
132  def __init__(self):
133  fnu = {}
134  fnubar = {}
135  fnu['file'] = FindPandAnaDir()+"/Data/xs/RPA2017.h5"
136  fnubar['file'] = fnu['file']
137  fnu['group'] = "RPA_Q2_CV_nu"
138  fnubar['group'] = "RPA_Q2_CV_nubar"
139  NuWeightFromFile.__init__(self, fnu, fnubar)
140 
141  def GetWeight(self, params):
142  q2 = params['q2']
143  isAntiNu = ('IsAntiNu' in params.keys()) and params['IsAntiNu']
144 
145  df = self.nu
146  if isAntiNu: df = self.nubar
147  return df.GetValue(q2)
148 
150  def __init__(self):
151  fnu = {}
152  fnubar = {}
153  fnu['file'] = FindPandAnaDir()+"/Data/xs/rw_empiricalMEC2018.h5"
154  fnubar['file'] = fnu['file']
155  fnu['group'] = "numu_mec_weights_smoothed"
156  fnubar['group'] = "numubar_mec_weights_smoothed"
157  NuWeightFromFile.__init__(self, fnu, fnubar)
158 
159  def GetWeight(self, params):
160  qmag = params['qmag']
161  q0 = params['q0']
162  isAntiNu = ('IsAntiNu' in params.keys()) and params['IsAntiNu']
163 
164  df = self.nu
165  if isAntiNu: df = self.nubar
166  val = df.GetValueInRange([qmag, q0])
167  if val < 0.: val = 0.
168  return val
def __init__(self, fnu, fnubar, forcenu=False)
Definition: xsec_utils.py:91
def GetBinContent(self, binx, biny=-1)
Definition: xsec_utils.py:52
def FindFirstBinAbove(self, threshold, axis)
Definition: xsec_utils.py:64
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
def FindPandAnaDir(folder=None)
Definition: misc.py:10
def GetValueInRange(self, vals, maxrange=[-float("inf"), float, inf, binranges=[[1)
Definition: xsec_utils.py:73
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68