Belle II Software  release-05-02-19
prepareAsicCrosstalkSimDB.py
1 ''' Prepare CDC x-talk simulation DB object '''
2 
3 from root_pandas import read_root
4 import matplotlib.pyplot as plt
5 import numpy as np
6 from scipy.interpolate import UnivariateSpline
7 
8 import basf2
9 import ROOT
10 from ROOT.Belle2 import FileSystem
11 from ROOT.Belle2 import CDCDatabaseImporter
12 from ROOT import TH1F, TFile
13 
14 
15 def getEff(var, cut, n=40, limits=(0., 2500.)):
16  ''' Simple efficiency estimator
17  var: pandas series/np array of variable to study
18  cut: bool series/np array pass/fail
19  n : number of bins
20  limits : histogram limits
21  '''
22  a = np.histogram(var, n, limits)
23  b = np.histogram(var[cut], n, limits)
24 
25  eff = b[0]/a[0]
26  # simple binomial formula:
27  effErr = 1/a[0]*np.sqrt(a[0]*eff*(1-eff))
28  x = 0.5*(a[1][1:]+a[1][:-1])
29  return x, eff, effErr
30 
31 #
32 # Specify file name here:
33 #
34 
35 
36 InputFile = "cosmic.0008.03420_03427.root"
37 
38 
39 df = read_root(InputFile, columns=["Channel", "ADC", "Board", "Nhit", "Asic"])
40 df['asic'] = df.Channel//8
41 
42 u1 = getEff(df[(df.asic % 3 == 1)].ADC_ADC_Sig, df.Nhit > 1, 128, (0, 1024.))
43 
44 u2 = getEff(df[(df.asic % 3 == 1)].ADC_ADC_Sig, df.Nhit > 1, 16, (1024, 7800.))
45 
46 u = np.append(u1, u2, axis=1)
47 
48 plt.figure()
49 
50 
51 x = np.nan_to_num(u[0])
52 
53 e = np.nan_to_num(u[1])
54 
55 ee = np.where(np.nan_to_num(u[2]) == 0, 1000., u[2])
56 
57 
58 f = UnivariateSpline(x, e, 1/ee)
59 
60 
61 xp = np.arange(-0.5, 8197.5, 1)
62 
63 plt.subplot(211)
64 plt.xlim(0, 8196.)
65 plt.ylim(0., 1.02)
66 
67 plt.errorbar(x, e, u[2], fmt='.')
68 plt.ylabel('Fraction of $N_{hit}>1$')
69 plt.plot(xp, f(xp))
70 
71 plt.subplot(212)
72 plt.xlim(0, 1000.)
73 plt.ylim(0., 0.5)
74 plt.errorbar(x, e, u[2], fmt='.')
75 plt.plot(xp, f(xp))
76 plt.xlabel('ADC')
77 plt.savefig("xTalkProb.pdf")
78 
79 # Write out root file
80 
81 
82 names = ["Board", "Channel"]
83 for i in range(8):
84  names += ['Asic_ADC{:d}'.format(i), 'Asic_TDC{:d}'.format(i), 'Asic_TOT{:d}'.format(i)]
85 
86 df[(df.Nhit > 1) & (df.asic % 3 == 1)][names].to_root("t.root", index=False)
87 
88 fi = TFile("t.root", "update")
89 
90 t = TH1F("ProbXTalk", "Prob xTalk", 8196, 0, 8196)
91 t.SetContent(f(xp))
92 fi.Write()
93 fi.Close()
94 
95 
96 INPUT = FileSystem.findFile("t.root")
97 
98 # Specify the exp and run where iov is valid.
99 # N.B. -1 means unbound.
100 
101 expFirst = 0
102 
103 expLast = -1
104 
105 runFirst = 0
106 
107 runLast = -1
108 basf2.use_local_database("localdb/database.txt", "localdb")
109 
110 
111 main = basf2.create_path()
112 
113 
114 eventinfosetter = basf2.register_module('EventInfoSetter')
115 main.add_module(eventinfosetter)
116 
117 # process single event
118 basf2.process(main)
119 
120 
121 dbImporter = CDCDatabaseImporter(expFirst, runFirst, expLast, runLast)
122 dbImporter.importCDCCrossTalkLibrary(INPUT)
123 
124 dbImporter.printCDCCrossTalkLibrary()
125 dbImporter.testCDCCrossTalkLibrary()
prepareAsicCrosstalkSimDB.getEff
def getEff(var, cut, n=40, limits=(0., 2500.))
Definition: prepareAsicCrosstalkSimDB.py:15
prepareAsicCrosstalkSimDB.f
f
spline parameterisation of efficiency
Definition: prepareAsicCrosstalkSimDB.py:58
CDCDatabaseImporter
Definition: CDCDatabaseImporter.py:1
basf2.process
def process(path, max_event=0)
Definition: __init__.py:25