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