4 import os, struct, sys, math
6 from aux_functions
import *
13 print "Going to find COOL Best Phases."
16 infile=
open(
'TileInfoDump_phases.txt',
'r')
17 outfile=
open(
'%scool_values_%d_%d.txt' % (outdir, runnumber_lg, runnumber_hg),
'w')
22 a = line.strip().
split(
' ')
23 if a[0]!=
"TileInfoDump":
26 b = a[10].strip().
split(
'/')
31 c = a[14].strip().
split(
'\t')
32 outfile.write(
'Tccis '+
str(hex(
int(b[0]) * 0x100 +
int(b[1])))+
' '+
str(b[2])+
' '+
str(c[0]))
36 if bestPhases[part][mod][chan] ==
False:
39 bestPhases[part][mod][chan] = [
float(c[0]),
'no_hg']
42 c = a[14].strip().
split(
'\t')
43 outfile.write(
' '+
str(c[0])+
'\n')
48 if bestPhases[part][mod][chan]==
False:
50 elif bestPhases[part][mod][chan][1]==
'no_hg':
51 bestPhases[part][mod][chan][1]=
float(c[0])
53 print "We should never get here."
57 print "Something is wrong with the input file. Leaving."
60 print "Done finding COOL Best Phases."
67 ROOT.gROOT.SetStyle(
"Plain")
68 ROOT.gStyle.SetCanvasBorderMode(0)
69 ROOT.gStyle.SetPadBorderMode(0)
71 ROOT.gStyle.SetLabelFont(42,
"XYZ")
72 ROOT.gStyle.SetTextFont(42)
73 ROOT.gStyle.SetOptStat(111110)
74 ROOT.gStyle.SetPalette(1)
75 ROOT.gStyle.SetTitleFont(42,
"XYZ")
76 ROOT.gStyle.SetTitleBorderSize(0)
77 ROOT.gStyle.SetPadColor(0)
78 ROOT.gStyle.SetCanvasColor(0)
79 ROOT.gStyle.SetOptFit()
82 c1.SetFrameBorderMode(0)
91 hist.GetXaxis().CenterTitle()
93 hist.GetXaxis().SetTitleOffset(1.3)
97 hist.GetYaxis().CenterTitle()
98 hist.GetYaxis().SetTitleOffset(1.3)
101 hist.SetFillColor(ROOT.kBlue)
110 print "Going to calculate average times."
111 ROOT.gROOT.SetBatch()
113 gainnames = [
'LG',
'HG']
114 h_tfit=ROOT.TH1F(
"tfit_ev_%s" % gainnames[gain],
"Run %d: %s t_{fit} distribution" % (runnumber, gainnames[gain]), 200, -50., 50)
115 h_tfit_cool=ROOT.TH1F(
"tfit_cool_ev_%s" % gainnames[gain],
"Run %d: %s" % (runnumber, gainnames[gain]), 200, -50., 50)
117 histos = [h_tfit, h_tfit_cool]
119 xnames = [
't_{fit} (ns)',
't_{fit} - CoolBestPhase (ns)']
122 histos[i].GetXaxis().SetTitle(xnames[i])
123 histos[i].GetXaxis().CenterTitle()
124 histos[i].GetXaxis().SetTitleOffset(1.2)
125 histos[i].SetFillColor(ROOT.kGray)
131 partnames = [
'LBA',
'LBC',
'EBA',
'EBC']
132 partmap = {
'LBA':
'A',
'LBC':
'C',
'EBA':
'D',
'EBC':
'E'}
134 for part
in partnames:
136 for mod
in range(1,65):
138 print 'Checking', part, mod
139 partInd = partnames.index(part)
142 filename =
'%stiletb_%d_MonoCis.%s%02d.0.aan.root' % (datadir, runnumber, part, mod)
144 if os.path.exists(filename):
145 print 'Using', filename
146 file = ROOT.TFile.Open(filename)
147 if file
and not file.IsZombie():
148 tree = file.Get(
'h1000')
149 nevents = tree.GetEntries()
151 for ev
in range(1, nevents):
155 pha = tree.m_cispar[5]
156 dac = tree.m_cispar[6]
157 cap = tree.m_cispar[7]
159 injCharge = (2*4.096*dac*cap)/1023
161 Tfit = getattr(tree,
'tfit%s%02d' % (partmap[part], mod))
162 Efit = getattr(tree,
'efit%s%02d' % (partmap[part], mod))
165 if cap!=100
or dac!=dacvals[gain]
or pha!=0:
168 for pmt
in range(48):
173 h_tfit.Fill(tfit, 1.)
175 bestPhase = bestPhases[partInd][modInd][
int(
PMTtoChannel(partInd, pmt))]
176 if not isinstance(bestPhase, list):
178 bestPhase =
float(bestPhase[gain])
179 h_tfit_cool.Fill(tfit - bestPhase, 1.)
181 if times[partInd][modInd][pmt] ==
'noninst':
183 elif 0.9*injCharge>efit
or 1.1*injCharge<efit:
186 elif times[partInd][modInd][pmt] ==
'real':
187 times[partInd][modInd][pmt] = [tfit, 1]
189 times[partInd][modInd][pmt][0] += tfit
190 times[partInd][modInd][pmt][1] += 1
194 for partition
in times:
195 for module
in partition:
196 for channel
in module:
197 if isinstance(channel, (list, tuple)):
199 loc = times.index(partition), partition.index(module), module.index(channel)
200 times[loc[0]][loc[1]][loc[2]] = channel[0]/channel[1]
201 elif channel ==
'real':
205 print 'Found corrections for ', 100*
float(injection_yes) / (injection_yes + injection_no),
'% of ', gainnames[gain],
'channels.'
212 can.SaveAs(
'%s%s.ps' % (outdir, h.GetName()))
217 def plot(bestPhases, times, runnumber, gain, outdir):
219 print "Going to plot."
220 ROOT.gROOT.SetBatch()
222 partnames = [
'LBA',
'LBC',
'EBA',
'EBC']
223 gainnames = [
'LG',
'HG']
224 h_avgtime=ROOT.TH1F(
"avgtime_%s" % gainnames[gain],
"Run %d: %s <t_{fit}> distribution" % (runnumber, gainnames[gain]), 200, -50., 50)
225 h_bestphase=ROOT.TH1F(
"bestphases_%s" % gainnames[gain],
"Run %d: %s COOL best phase distribution" % (runnumber, gainnames[gain]), 200, -50., 50)
226 h_diff=ROOT.TH1F(
"diff_%s" % gainnames[gain],
"Run %d: %s Differences from COOL Phase" % (runnumber, gainnames[gain]), 50, -10., 10)
227 h_diff_zoom=ROOT.TH1F(
'diff_zoom_%s' % gainnames[gain],
'Run %d: %s Zoom differences from COOL Phase' % (runnumber, gainnames[gain]), 200, -1., 1)
230 h_diff_part = [ROOT.TProfile2D(
'diff_%s' % partnames[part],
'Run %d: (<t_{fit}>_{chan} - CoolBestPhase) [ns] in %s' % (runnumber, partnames[part]),\
231 64, 1., 65., 48, 0., 48.)
for part
in range(4)]
232 h_diff_part_abs = [ROOT.TProfile2D(
'diff_%s_abs' % partnames[part],
'Run %d: |<t_{fit}>_{chan} - CoolBestPhase| in %s' % (runnumber, partnames[part]),\
233 64, 1., 65., 48, 0., 48.)
for part
in range(4)]
236 histos = [h_avgtime, h_bestphase, h_diff, h_diff_zoom]
238 xnames = [
'<t_{fit}>_{chan} [ns]',
'BestPhase [ns]', \
239 '<t_{fit}>_{chan} - CoolBestPhase [ns]', \
240 '<t_{fit}>_{chan} - CoolBestPhase [ns]']
243 histos[i].GetXaxis().SetTitle(xnames[i])
244 histos[i].GetXaxis().CenterTitle()
245 histos[i].GetXaxis().SetTitleOffset(1.2)
246 histos[i].SetFillColor(ROOT.kBlue)
253 loc = times.index(part)+1, part.index(mod), mod.index(chan)
254 time = times[loc[0]-1][loc[1]][loc[2]]
255 phase = bestPhases[loc[0]-1][loc[1]][
int(
PMTtoChannel(loc[0]-1, loc[2]))][gain]
257 if ((time
in [
'noninst',
'real'])
or (phase
in [
'noninst',
'real'])):
260 h_avgtime.Fill(time, 1.)
261 h_bestphase.Fill(phase, 1.)
262 h_diff.Fill(time-phase, 1.)
263 h_diff_zoom.Fill(time-phase, 1.)
266 h_diff_part[loc[0]-1].Fill(loc[1]-1, loc[2], time-phase)
267 h_diff_part_abs[loc[0]-1].Fill(loc[1]-1, loc[2], abs(time-phase))
269 h_diff_part[loc[0]-1].Fill(loc[1]-1, loc[2], -10)
270 h_diff_part_abs[loc[0]-1].Fill(loc[1]-1, loc[2], -10)
272 for hist
in h_diff_part:
273 hist.GetXaxis().SetTitle(
'Module Number')
274 hist.GetYaxis().SetTitle(
'Channel Number')
275 hist.SetMinimum(-1.0)
279 for hist
in h_diff_part_abs:
280 hist.GetXaxis().SetTitle(
'Module Number')
281 hist.GetYaxis().SetTitle(
'Channel Number')
285 hist.GetYaxis().SetTitleOffset(1.5)
292 can.SaveAs(
'%s%s.ps' % (outdir, h.GetName()))
297 pack =
'%stime_locations_%d_%s.ps' % (outdir, runnumber, gainnames[gain])
300 can.Print(
'%s[' % pack)
305 for hist
in h_diff_part:
307 ROOT.gStyle.SetOptStat(0)
319 for hist
in h_diff_part_abs:
320 ROOT.gStyle.SetOptStat(0)
321 hist.GetZaxis().SetTitle(
'ns')
322 hist.GetZaxis().CenterTitle()
328 can.Print(
'%s]' % pack)
332 print "Done plotting."
336 file =
open(
'Tile.tccis.%d.%d' % (runnumber_lg, runnumber_hg),
'w')
339 for j
in range(0, 64):
341 lg = values_low[i][j][k]
342 if lg
in [
'real',
'noninst']:
344 hg = values_high[i][j][k]
345 if hg
in [
'real',
'noninst']:
350 if lg != 0
and hg != 0:
352 file.write(
str(hex((i+1) * 0x100 + j)) +
' ')
353 file.write(
str(chan) +
' ')
354 file.write(
str(lg) +
' ')
355 file.write(
str(hg) +
'\n')
363 datadir =
'/location/of/ntuples/'
364 outdir =
'/where/output/will/be/written/'
367 print "Please pass a run number for a low gain and high gain run."
371 runnumber_lg =
int(sys.argv[1])
372 runnumber_hg =
int(sys.argv[2])
374 outdir = outdir +
str(runnumber_lg) +
'_' +
str(runnumber_hg) +
'/'
375 datadir_lg = datadir +
str(runnumber_lg) +
'/'
376 datadir_hg = datadir +
str(runnumber_hg) +
'/'
378 print 'Using run', runnumber_lg,
'for LG and', runnumber_hg,
'for HG'
379 print 'Output will be sent to', outdir
381 if not os.path.exists(outdir):
385 TimesLG =
calcAvgTimes(runnumber_lg, CoolBestPhases, 0, datadir_lg, outdir)
386 TimesHG =
calcAvgTimes(runnumber_hg, CoolBestPhases, 1, datadir_hg, outdir)
388 plot(CoolBestPhases, TimesLG, runnumber_lg, 0, outdir)
389 plot(CoolBestPhases, TimesHG, runnumber_hg, 1, outdir)
393 if __name__ ==
'__main__':