CMS 3D CMS Logo

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