CMS 3D CMS Logo

plotTTrigFromHistos.py
Go to the documentation of this file.
1 import ROOT
2 from drawHistoAllChambers import drawHisto
3 
4 def binNumber(station,sector):
5  start = (station - 1)*12
6  return start + sector
7 
8 def plot(fileName,sl,ymin=300,ymax=360,option="HISTOP",draw=True):
9 
10  slType = sl
11  slStr = "SL%d" % slType
12  verbose = False
13 
14  ROOT.TH1.AddDirectory(False)
15 
16  file = ROOT.TFile(fileName,'read')
17 
18  wheels = (-2,-1,0,1,2)
19  stations = (1,2,3,4)
20 
21  histosWheel = {}
22  for wh in wheels:
23  histoName = 'Wheel%d_%s_TTrig' % (wh,slStr)
24  print "Accessing",histoName
25  histosWheel[wh] = file.Get(histoName)
26 
27  # (Wh-2 MB1 Sec1 ... Wh-2 MB1 Sec12 ... Wh-1 MB1 Sec1 ... Wh-1 MB1 Sec12 ...)
28  # (Wh-2 MB2 Sec1 ... Wh-2 MB2 Sec12 ... Wh-1 MB2 Sec1 ... Wh-1 MB1 Sec12 ...) ...
29  nBins = 250
30  if slType == 2: nBins = 180
31  histo = ROOT.TH1F("h_TTrigAll","TTrig",nBins,0,nBins)
32  for st in stations:
33  nSectors = 12
34  if st == 4: nSectors = 14
35  if st == 4 and slType == 2: continue
36  if verbose: print "Station",st
37  for wh in wheels:
38  if verbose: print "Wheel",wh
39  for sec in range(1,nSectors+1):
40  if verbose: print "Sector",sec
41  binHisto = binNumber(st,sec)
42  if verbose: print "Bin from histos:",binHisto
43  value = histosWheel[wh].GetBinContent(binHisto)
44 
45  binHistoNew = (st - 1)*60 + (wh + 2)*nSectors + sec
46  if verbose: print "Bin final",binHistoNew
47  histo.SetBinContent(binHistoNew,value)
48 
49  if sec == 1:
50  label = "Wheel %d" % wh
51  if wh == -2: label += " MB%d" % st
52  histo.GetXaxis().SetBinLabel(binHistoNew,label)
53 
54  objects = drawHisto(histo,
55  title="t_{Trig} (ns)",
56  ymin=ymin,ymax=ymax,option=option,draw=draw)
57 
58  return objects
59 
60 def compare(fileNames,sl,ymin=300,ymax=360,labels=[]):
61  option = "HISTOP"
62  colors = (2,4,12,44,55,38,27,46)
63  markers = (24,25,26,27,28,30,32,5)
64 
65  idx = 0
66  canvas = None
67  objects = None
68  histos = []
69  for fileName in fileNames:
70  draw = False
71  if not idx: draw = True
72 
73  objs = plot(fileName,sl,ymin,ymax,option,draw)
74  histos.append(objs[1])
75  histos[-1].SetName( "%s_%d" % (histos[-1].GetName(),idx) )
76  if not idx:
77  canvas = objs[0]
78  objects = objs[2]
79 
80  canvas.cd()
81  if idx:
82  histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
83  histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
84  histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])
85 
86  histos[-1].Draw(option + "SAME")
87 
88  idx += 1
89 
90  legend = ROOT.TLegend(0.4,0.7,0.95,0.8)
91  for idx in range( len(histos) ):
92  histo = histos[idx]
93  label = histo.GetName()
94  if len(labels): label = labels[idx]
95  legend.AddEntry(histo,label,"LP")
96 
97  idx += 1
98 
99  canvas.cd()
100  legend.SetFillColor( canvas.GetFillColor() )
101  legend.Draw("SAME")
102 
103  objects.append(legend)
104 
105  return (canvas,histos,objects)
106 
107 def compareDiff(fileNames,sl,ymin=-15.,ymax=15.):
108  option = "HISTOP"
109  colors = (2,4,9,12,38,44,46,55)
110  markers = (24,25,26,27,28,30,32,5)
111 
112  idx = 0
113  canvases = [None,None]
114  objects = None
115  histoRef = None
116  histos = []
117  histosDist = []
118  for fileName in fileNames:
119  objs = plot(fileName,sl,300,360,'',False)
120  histos.append( objs[1].Clone(objs[1].GetName() + "_diff") )
121  histos[-1].SetName( "%s_%d" % (histos[-1].GetName(),idx) )
122  if not idx:
123  histoRef = objs[1]
124  histos[-1].Reset()
125  else:
126  histos[-1].Add(histoRef,-1.)
127 
128  draw = False
129  if not idx: draw = True
130 
131  objs = drawHisto(histos[-1],
132  title="t_{Trig} difference (ns)",
133  ymin=ymin,ymax=ymax,option=option,draw=draw)
134 
135  if not idx:
136  canvases[0] = objs[0]
137  objects = objs[2]
138 
139  if idx:
140  canvases[0].cd()
141  histos[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
142  histos[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
143  histos[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])
144 
145  histos[-1].Draw(option + "SAME")
146 
147  histosDist.append( ROOT.TH1F(histos[-1].GetName() + "_dist","tTrig distribution",200,ymin,ymax) )
148  for ibin in range(1,histos[-1].GetNbinsX()+1):
149  histosDist[-1].Fill( histos[-1].GetBinContent(ibin) )
150 
151  histosDist[-1].SetLineColor(colors[ (idx - 1) % len(colors) ])
152  histosDist[-1].SetMarkerColor(colors[ (idx - 1) % len(colors) ])
153  histosDist[-1].SetMarkerStyle(markers[ (idx - 1) % len(markers) ])
154 
155  idx += 1
156 
157 
158  canvases[1] = ROOT.TCanvas("c_tTrigDist")
159  canvases[1].SetGridy()
160  canvases[1].SetFillColor(0)
161  canvases[1].cd()
162  option = "HISTO"
163  idx = 0
164  for histo in histosDist:
165  if not idx:
166  histo.GetXaxis().SetTitle("t_{Trig} difference (ns)")
167  histo.GetYaxis().SetTitle("Number of chambers")
168  histo.Draw(option)
169  else:
170  histo.Draw(option + "SAME")
171  idx += 1
172 
173  return (canvases,histos,histosDist,objects)
def binNumber(station, sector)
def plot(fileName, sl, ymin=300, ymax=360, option="HISTOP", draw=True)
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
def compare(fileNames, sl, ymin=300, ymax=360, labels=[])
def compareDiff(fileNames, sl, ymin=-15., ymax=15.)
void Reset(std::vector< TH2F > &depth)
def drawHisto(histo, title, ymin, ymax, option="HISTOP", draw=True)