ATLAS Offline Software
calc_cool_offsets.py
Go to the documentation of this file.
1 #!/bin/env python
2 # Author: Michael Miller (miller@uchicago.edu)
3 
4 import os, struct, sys, math
5 import ROOT
6 from aux_functions import *
7 
8 
9 def readCOOLBestPhases(runnumber_lg, runnumber_hg, outdir):
10 
11  # reads TileInfoDump log
12 
13  print "Going to find COOL Best Phases."
14 
15 
16  infile=open('TileInfoDump_phases.txt', 'r')
17  outfile=open('%scool_values_%d_%d.txt' % (outdir, runnumber_lg, runnumber_hg), 'w')
18 
19  bestPhases = [[[ checkChannelValid(part, mod, chan) for chan in range(48)] for mod in range(64)] for part in range(4)]
20 
21  for line in infile:
22  a = line.strip().split(' ')
23  if a[0]!="TileInfoDump": # don't need this
24  continue
25  else:
26  b = a[10].strip().split('/') # module info part/mod/chan/gain
27  if b[0]=='0': # don't need this
28  continue
29 
30  if b[3]=='0': # two cases for gains - lg
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]))
33  part = int(b[0])-1
34  mod = int(b[1])
35  chan = int(b[2])
36  if bestPhases[part][mod][chan] == False:
37  pass
38  else:
39  bestPhases[part][mod][chan] = [float(c[0]), 'no_hg']
40 
41  elif b[3]=='1': # hg
42  c = a[14].strip().split('\t')
43  outfile.write(' '+str(c[0])+'\n')
44  part = int(b[0])-1
45  mod = int(b[1])
46  chan = int(b[2])
47 
48  if bestPhases[part][mod][chan]==False:
49  pass
50  elif bestPhases[part][mod][chan][1]=='no_hg':
51  bestPhases[part][mod][chan][1]=float(c[0])
52  else:
53  print "We should never get here."
54  continue
55 
56  else:
57  print "Something is wrong with the input file. Leaving."
58  break
59 
60  print "Done finding COOL Best Phases."
61  return bestPhases
62 
63 
64 
65 def SetupDraw():
66 
67  ROOT.gROOT.SetStyle("Plain")
68  ROOT.gStyle.SetCanvasBorderMode(0)
69  ROOT.gStyle.SetPadBorderMode(0)
70  #ROOT.gStyle.SetOptTitle(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()
80 
81  c1 = ROOT.TCanvas()
82  c1.SetFrameBorderMode(0)
83  c1.SetBorderSize(0)
84  c1.SetBorderMode(0)
85  c1.SetFillColor(0)
86 
87  return c1
88 
89 def cleanHisto(hist):
90 
91  hist.GetXaxis().CenterTitle()
92  #hist.GetXaxis().CenterLabels(ROOT.kTRUE)
93  hist.GetXaxis().SetTitleOffset(1.3)
94  #hist.GetXaxis().SetTitle('T_{fit} [ns]')
95  #hist.GetXaxis().SetLabelOffset(0.021)
96  #hist.GetXaxis().SetLabelSize(0.06)
97  hist.GetYaxis().CenterTitle()
98  hist.GetYaxis().SetTitleOffset(1.3)
99  #hist.GetYaxis().SetLabelOffset(0.015)
100  #hist.GetYaxis().SetLabelSize(0.06)
101  hist.SetFillColor(ROOT.kBlue)
102 
103  return hist
104 
105 
106 
107 
108 def calcAvgTimes(runnumber, bestPhases, gain, datadir, outdir):
109 
110  print "Going to calculate average times."
111  ROOT.gROOT.SetBatch()
112 
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)
116 
117  histos = [h_tfit, h_tfit_cool]
118 
119  xnames = ['t_{fit} (ns)', 't_{fit} - CoolBestPhase (ns)']
120 
121  for i in range(2):
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)
126 
127 
128  times = [[[ checkPMTValid(part, chan) for chan in range(48)] for mod in range(64)] for part in range(4)]
129 
130  dacvals = [120, 7]
131  partnames = ['LBA', 'LBC', 'EBA', 'EBC']
132  partmap = {'LBA': 'A', 'LBC': 'C', 'EBA': 'D', 'EBC': 'E'}
133 
134  for part in partnames:
135 
136  for mod in range(1,65):
137 
138  print 'Checking', part, mod
139  partInd = partnames.index(part)
140  modInd = mod - 1
141 
142  filename = '%stiletb_%d_MonoCis.%s%02d.0.aan.root' % (datadir, runnumber, part, mod)
143  print filename
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()
150 
151  for ev in range(1, nevents):
152 
153  tree.GetEntry(ev)
154 
155  pha = tree.m_cispar[5]
156  dac = tree.m_cispar[6]
157  cap = tree.m_cispar[7]
158 
159  injCharge = (2*4.096*dac*cap)/1023
160 
161  Tfit = getattr(tree, 'tfit%s%02d' % (partmap[part], mod))
162  Efit = getattr(tree, 'efit%s%02d' % (partmap[part], mod))
163 
164 
165  if cap!=100 or dac!=dacvals[gain] or pha!=0:
166  continue
167 
168  for pmt in range(48):
169 
170  tfit = Tfit[pmt]
171  efit = Efit[pmt]
172 
173  h_tfit.Fill(tfit, 1.)
174 
175  bestPhase = bestPhases[partInd][modInd][int(PMTtoChannel(partInd, pmt))]
176  if not isinstance(bestPhase, list):
177  continue
178  bestPhase = float(bestPhase[gain])
179  h_tfit_cool.Fill(tfit - bestPhase, 1.)
180 
181  if times[partInd][modInd][pmt] == 'noninst':
182  pass
183  elif 0.9*injCharge>efit or 1.1*injCharge<efit:
184  #don't use events which differ from injected charge by more than 10%
185  pass
186  elif times[partInd][modInd][pmt] == 'real':
187  times[partInd][modInd][pmt] = [tfit, 1]
188  else:
189  times[partInd][modInd][pmt][0] += tfit
190  times[partInd][modInd][pmt][1] += 1
191 
192  injection_yes = 0
193  injection_no = 0
194  for partition in times:
195  for module in partition:
196  for channel in module:
197  if isinstance(channel, (list, tuple)):
198  injection_yes += 1
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':
202  injection_no += 1
203 
204 
205  print 'Found corrections for ', 100*float(injection_yes) / (injection_yes + injection_no), '% of ', gainnames[gain], 'channels.'
206 
207 
208  for h in histos:
209  can = SetupDraw()
210  h.Draw()
211  can.Update()
212  can.SaveAs('%s%s.ps' % (outdir, h.GetName()))
213 
214  return times
215 
216 
217 def plot(bestPhases, times, runnumber, gain, outdir):
218 
219  print "Going to plot."
220  ROOT.gROOT.SetBatch()
221 
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)
228 
229 
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)]
234 
235 
236  histos = [h_avgtime, h_bestphase, h_diff, h_diff_zoom]
237 
238  xnames = ['<t_{fit}>_{chan} [ns]', 'BestPhase [ns]', \
239  '<t_{fit}>_{chan} - CoolBestPhase [ns]', \
240  '<t_{fit}>_{chan} - CoolBestPhase [ns]']
241 
242  for i in range(4):
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)
247 
248 
249  # careful - times is indexed by pmt-1 - bestPhases is indexed by channel number
250  for part in times:
251  for mod in part:
252  for chan in mod:
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]
256 
257  if ((time in ['noninst', 'real']) or (phase in ['noninst', 'real'])):
258  continue
259 
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.)
264 
265  if not checkChannelValid(loc[0]-1, loc[1], loc[2])=='noninst':
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))
268  else:
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)
271 
272  for hist in h_diff_part:
273  hist.GetXaxis().SetTitle('Module Number')
274  hist.GetYaxis().SetTitle('Channel Number')
275  hist.SetMinimum(-1.0)
276  hist.SetMaximum(1.0)
277  hist = cleanHisto(hist)
278 
279  for hist in h_diff_part_abs:
280  hist.GetXaxis().SetTitle('Module Number')
281  hist.GetYaxis().SetTitle('Channel Number')
282  #hist.SetMinimum(0.0)
283  #hist.SetMaximum(100.0)
284  hist = cleanHisto(hist)
285  hist.GetYaxis().SetTitleOffset(1.5)
286 
287 
288  for h in histos:
289  can = SetupDraw()
290  h.Draw()
291  can.Update()
292  can.SaveAs('%s%s.ps' % (outdir, h.GetName()))
293  can.Clear()
294 
295  del can
296 
297  pack = '%stime_locations_%d_%s.ps' % (outdir, runnumber, gainnames[gain])
298 
299  can = SetupDraw()
300  can.Print('%s[' % pack)
301 
302  #lat.Draw()
303  can.Divide(2,2)
304  cnt = 1
305  for hist in h_diff_part:
306  can.cd(cnt)
307  ROOT.gStyle.SetOptStat(0)
308  hist.Draw('COLZ')
309  can.Update()
310  can.Modified()
311  cnt = cnt + 1
312  can.Print(pack)
313  del can
314 
315 
316 
317  can = SetupDraw()
318  #can.SetLogz()
319  for hist in h_diff_part_abs:
320  ROOT.gStyle.SetOptStat(0)
321  hist.GetZaxis().SetTitle('ns')
322  hist.GetZaxis().CenterTitle()
323  hist.Draw('LEGO2Z')
324  can.Update()
325  can.Modified()
326  can.Print(pack)
327 
328  can.Print('%s]' % pack)
329 
330 
331 
332  print "Done plotting."
333 
334 def writeChannels(values_low, values_high, runnumber_lg, runnumber_hg):
335 
336  file = open('Tile.tccis.%d.%d' % (runnumber_lg, runnumber_hg), 'w')
337 
338  for i in range(4):
339  for j in range(0, 64):
340  for k in range(48):
341  lg = values_low[i][j][k]
342  if lg in ['real', 'noninst']:
343  lg = 0
344  hg = values_high[i][j][k]
345  if hg in ['real', 'noninst']:
346  hg = 0
347 
348  chan = PMTtoChannel(i, k)
349 
350  if lg != 0 and hg != 0: # only update if we have data for both adcs
351  file.write('Tccis ')
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')
356 
357  file.close()
358 
359 
360 
361 def main():
362 
363  datadir = '/location/of/ntuples/'
364  outdir = '/where/output/will/be/written/'
365 
366  if len(sys.argv)<3:
367  print "Please pass a run number for a low gain and high gain run."
368  return True
369 
370  else:
371  runnumber_lg = int(sys.argv[1])
372  runnumber_hg = int(sys.argv[2])
373 
374  outdir = outdir + str(runnumber_lg) + '_' + str(runnumber_hg) + '/'
375  datadir_lg = datadir + str(runnumber_lg) + '/'
376  datadir_hg = datadir + str(runnumber_hg) + '/'
377 
378  print 'Using run', runnumber_lg, 'for LG and', runnumber_hg, 'for HG'
379  print 'Output will be sent to', outdir
380 
381  if not os.path.exists(outdir):
382  os.mkdir(outdir)
383 
384  CoolBestPhases = readCOOLBestPhases(runnumber_lg, runnumber_hg, outdir)
385  TimesLG = calcAvgTimes(runnumber_lg, CoolBestPhases, 0, datadir_lg, outdir)
386  TimesHG = calcAvgTimes(runnumber_hg, CoolBestPhases, 1, datadir_hg, outdir)
387 
388  plot(CoolBestPhases, TimesLG, runnumber_lg, 0, outdir)
389  plot(CoolBestPhases, TimesHG, runnumber_hg, 1, outdir)
390 
391  writeChannels(TimesLG, TimesHG, runnumber_lg, runnumber_hg)
392 
393 if __name__ == '__main__':
394  main()
calc_cool_offsets.calcAvgTimes
def calcAvgTimes(runnumber, bestPhases, gain, datadir, outdir)
Definition: calc_cool_offsets.py:108
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
calc_cool_offsets.plot
def plot(bestPhases, times, runnumber, gain, outdir)
Definition: calc_cool_offsets.py:217
calc_cool_offsets.main
def main()
Definition: calc_cool_offsets.py:361
plot
Definition: PhysicsAnalysis/ElectronPhotonID/ElectronPhotonFourMomentumCorrection/python/plot.py:1
calc_cool_offsets.SetupDraw
def SetupDraw()
Definition: calc_cool_offsets.py:65
calc_cool_offsets.readCOOLBestPhases
def readCOOLBestPhases(runnumber_lg, runnumber_hg, outdir)
Definition: calc_cool_offsets.py:9
calc_cool_offsets.writeChannels
def writeChannels(values_low, values_high, runnumber_lg, runnumber_hg)
Definition: calc_cool_offsets.py:334
aux_functions.checkChannelValid
def checkChannelValid(ros, mod, chan)
Definition: aux_functions.py:29
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
aux_functions.checkPMTValid
def checkPMTValid(ros, pmt)
Definition: aux_functions.py:2
aux_functions.PMTtoChannel
def PMTtoChannel(ros, pmt)
Definition: aux_functions.py:75
calc_cool_offsets.cleanHisto
def cleanHisto(hist)
Definition: calc_cool_offsets.py:89
Trk::open
@ open
Definition: BinningType.h:40
str
Definition: BTagTrackIpAccessor.cxx:11
readCCLHist.float
float
Definition: readCCLHist.py:83
Trk::split
@ split
Definition: LayerMaterialProperties.h:38