4 from array
import array
6 from collections
import OrderedDict
10 ROOT.gSystem.Load(
"libFWCoreFWLite.so")
12 import CondCore.Utilities.conddblib
as conddb
15 lumiScaleFactor = 1000
32 for arg
in parser.rargs:
34 if arg[:2] ==
"--" and len(arg) > 2:
37 if arg[:1] ==
"-" and len(arg) > 1
and not floatable(arg):
41 del parser.rargs[:len(value)]
42 setattr(parser.values, option.dest, value)
45 usage = (
'usage: %prog [options]\n' 46 +
'%prog -h for help')
47 parser = optparse.OptionParser(usage)
49 parser.add_option(
"--inputFileName", dest=
"inputFileName", default=
"PixelBaryCentre.root",help=
"name of the ntuple file that contains the barycentre tree")
50 parser.add_option(
"--plotConfigFile", dest=
"plotConfigFile", default=
"PixelBaryCentrePlotConfig.json",help=
"json file that configs the plotting")
52 parser.add_option(
"--usePixelQuality",action=
"store_true", dest=
"usePixelQuality", default=
False,help=
"whether use SiPixelQuality")
53 parser.add_option(
"--showLumi",action=
"store_true", dest=
"showLumi", default=
False,help=
"whether use integrated lumi as x-axis")
54 parser.add_option(
"--years", dest=
"years", default = [2017], action=
"callback", callback=vararg_callback, help=
"years to plot")
56 parser.add_option(
"-l",action=
"callback",callback=callback_rootargs)
57 parser.add_option(
"-q",action=
"callback",callback=callback_rootargs)
58 parser.add_option(
"-b",action=
"callback",callback=callback_rootargs)
66 print(
"Empty run list!")
72 print(
"Only one run but the run requested is before the run!")
79 elif(run >= runs[len(runs)-1]) :
82 return ROOT.TAxis(len(runs)-1,
array(
'd',runs)).FindBin(run) - 1
92 runs = list(accumulatedLumiPerRun.keys())
98 if(iov.run>runs[len(runs)-1]
or iov.run<runs[0]):
101 if(iov.run!=current_run) :
102 run_lumis[iov.run] = [iov.ls]
103 run_maxlumi[iov.run] = iov.ls
105 run_lumis[iov.run].
append(iov.ls)
106 if(run_maxlumi[iov.run]<iov.ls):
107 run_maxlumi[iov.run] = iov.ls
109 current_run = iov.run
113 for branch_name
in branch_names :
114 for coord
in [
"x",
"y",
"z"] :
115 pos[coord+
"_"+branch_name] =
array(
'd',[])
117 pos[coord+
"max_"+branch_name] = -9999
118 pos[coord+
"min_"+branch_name] = 9999
121 zeros =
array(
'd',[])
124 runlumi =
array(
'd',[])
125 runlumiplot =
array(
'd',[])
126 runlumiplot_error =
array(
'd',[])
132 if(iov.run>runs[len(runs)-1]
or iov.run<runs[0]):
135 if(isEOY
and iov.run>=320413
and iov.run<=325175):
143 instLumi = accumulatedLumiPerRun[ runs[run_index] ]
145 instLumi = accumulatedLumiPerRun[ runs[run_index] ] - accumulatedLumiPerRun[ runs[run_index-1] ]
150 if(len(run_lumis[iov.run])>1) :
152 runlumi.append(0.0+instLumi*iov.ls*1.0/run_maxlumi[iov.run])
154 runlumi.append(accumulatedLumiPerRun[ runs[run_index-1] ]+instLumi*iov.ls*1.0/run_maxlumi[iov.run])
157 runlumi.append(accumulatedLumiPerRun[ runs[run_index] ])
160 if(len(run_lumis[iov.run])>1) :
161 runlumi.append(iov.run+iov.ls*1.0/run_maxlumi[iov.run])
164 runlumi.append(iov.run)
169 for branch_name
in branch_names :
170 pos_ = {
"x":10000*getattr(iov, branch_name).x(),
171 "y":10000*getattr(iov, branch_name).y(),
172 "z":10000*getattr(iov, branch_name).z()}
174 for coord
in [
"x",
"y",
"z"] :
175 pos[coord+
"_"+branch_name].
append(pos_[coord])
177 if(pos_[coord]>pos[coord+
"max_"+branch_name]) :
178 pos[coord+
"max_"+branch_name] = pos_[coord]
180 if(pos_[coord]<pos[coord+
"min_"+branch_name]) :
181 pos[coord+
"min_"+branch_name] = pos_[coord]
184 for iov
in range(len(runlumi)-1) :
185 runlumiplot.append(0.5*(runlumi[iov]+runlumi[iov+1]))
186 runlumiplot_error.append(0.5*(runlumi[iov+1]-runlumi[iov]))
188 runlumiplot.append(runlumiplot[len(runlumiplot_error)-1]+2*runlumiplot_error[len(runlumiplot_error)-1])
189 runlumiplot_error.append(runlumiplot_error[len(runlumiplot_error)-1])
191 v_runlumiplot = ROOT.TVectorD(len(runlumiplot),runlumiplot)
192 v_runlumiplot_error = ROOT.TVectorD(len(runlumiplot_error),runlumiplot_error)
195 v_zeros = ROOT.TVectorD(len(zeros),zeros)
200 for branch_name
in branch_names :
201 for coord
in [
"x",
"y",
"z"] :
202 v_pos[coord] = ROOT.TVectorD(len(pos[coord+
"_"+branch_name]),pos[coord+
"_"+branch_name])
204 barryCentre[coord+
'_'+branch_name] = ROOT.TGraphErrors(v_runlumiplot, v_pos[coord], v_runlumiplot_error, v_zeros)
205 barryCentre[
'a_'+coord+
'_'+branch_name] = pos[coord+
"_"+branch_name]
207 barryCentre[coord+
'max_'+branch_name] = pos[coord+
"max_"+branch_name]
208 barryCentre[coord+
'min_'+branch_name] = pos[coord+
"min_"+branch_name]
210 barryCentre[
'v_runlumiplot'] = v_runlumiplot
211 barryCentre[
'v_runlumierror'] = v_runlumiplot_error
212 barryCentre[
'v_zeros'] = v_zeros
218 x =
array(
'd',[x1, x2, x2, x1, x1])
219 y =
array(
'd',[y1, y1, y2, y2, y1])
220 v_x = ROOT.TVectorD(len(x),x)
221 v_y = ROOT.TVectorD(len(y),y)
223 gr = ROOT.TGraph(v_x,v_y)
224 gr.SetLineColor(ROOT.kBlack)
229 def plotbarycenter(bc,coord,plotConfigJson, substructure,runsPerYear,pixelLocalRecos,accumulatedLumiPerRun, withPixelQuality,showLumi) :
230 runs = list(accumulatedLumiPerRun.keys())
232 years = list(runsPerYear.keys())
234 labels = list(bc.keys())
236 can = ROOT.TCanvas(
"barycentre_"+substructure+
"_"+coord,
"", 2000, 900)
247 for label
in labels :
248 gr[label] = ROOT.TGraph()
249 gr[label] = bc[label][coord+
"_"+substructure]
251 gr[label].SetMarkerStyle(8)
252 gr[label].SetMarkerSize(0)
253 gr[label].SetMarkerStyle(8)
254 gr[label].SetMarkerSize(0)
255 gr[label].SetLineColor(plotConfigJson[
"colorScheme"][label])
257 width_ = gr[label].GetXaxis().GetXmax() - gr[label].GetXaxis().GetXmin()
258 xmax = gr[label].GetXaxis().GetXmax()
261 upper = bc[label][coord+
"max_"+substructure]
262 lower = bc[label][coord+
"min_"+substructure]
265 upper =
max(upper, bc[label][coord+
"max_"+substructure])
266 lower =
min(lower, bc[label][coord+
"min_"+substructure])
270 upper = upper * scale
272 upper = upper / scale
274 lower = lower / scale
276 lower = lower * scale
277 range_ = upper - lower
280 for label
in labels :
282 gr[label].GetYaxis().SetRangeUser(lower, upper)
283 gr[label].GetYaxis().SetTitle(plotConfigJson[
"substructures"][substructure]+
" barycentre ("+coord+
") [#mum]")
284 gr[label].GetXaxis().SetTitle(
"Run Number")
285 gr[label].GetYaxis().CenterTitle(
True)
286 gr[label].GetXaxis().CenterTitle(
True)
287 gr[label].GetYaxis().SetTitleOffset(0.80)
288 gr[label].GetYaxis().SetTitleSize(0.055)
289 gr[label].GetXaxis().SetTitleOffset(0.80)
290 gr[label].GetXaxis().SetTitleSize(0.055)
291 gr[label].GetXaxis().SetMaxDigits(6)
293 gr[label].GetXaxis().SetTitle(
"Delivered luminosity [1/fb]")
301 gr_dummyFirstRunOfTheYear =
blackBox(-999, 10000, -999, -10000)
302 gr_dummyFirstRunOfTheYear.SetLineColor(ROOT.kBlack)
303 gr_dummyFirstRunOfTheYear.SetLineStyle(1)
304 gr_dummyFirstRunOfTheYear.Draw(
"L")
305 gr_dummyPixelReco =
blackBox(-999, 10000, -999, -10000)
306 gr_dummyPixelReco.SetLineColor(ROOT.kGray+1)
307 gr_dummyPixelReco.SetLineStyle(3)
308 gr_dummyPixelReco.Draw(
"L")
309 gr_dummyFirstRunOfTheYear.SetTitle(
"First run of the year")
310 gr_dummyPixelReco.SetTitle(
"Pixel calibration update")
312 for label
in labels :
313 gr[label].SetTitle(plotConfigJson[
"baryCentreLabels"][label])
314 legend = can.BuildLegend()
315 legend.SetShadowColor(0)
316 legend.SetFillColor(0)
317 legend.SetLineColor(1)
319 for label
in labels :
320 gr[label].SetTitle(
"")
326 years_label +=
str(year)
328 years_label = years_label.rstrip(
"+")
331 CMSworkInProgress = ROOT.TPaveText( xmax-0.3*width_, upper+range_*0.005,
332 xmax, upper+range_*0.055,
"nb")
333 CMSworkInProgress.AddText(
"CMS #bf{#it{Preliminary} ("+years_label+
" pp collisions)}")
334 CMSworkInProgress.SetTextAlign(32)
335 CMSworkInProgress.SetTextSize(0.04)
336 CMSworkInProgress.SetFillColor(10)
337 CMSworkInProgress.Draw()
342 for since
in pixelLocalRecos :
345 integrated_lumi = accumulatedLumiPerRun[runs[run_index]]
346 line_pixels[since] = ROOT.TLine(integrated_lumi, lower, integrated_lumi, upper)
349 line_pixels[since] = ROOT.TLine(since, lower, since, upper)
351 line_pixels[since].SetLineColor(ROOT.kGray+1)
352 line_pixels[since].SetLineStyle(3)
353 line_pixels[since].Draw()
359 if(len(years)>1
or (
not showLumi) ) :
364 integrated_lumi = accumulatedLumiPerRun[runs[run_index]]
365 line_years[year] = ROOT.TLine(integrated_lumi, lower, integrated_lumi, upper)
366 text_years[year] = ROOT.TPaveText( integrated_lumi+0.01*width_, upper-range_*0.05,
367 integrated_lumi+0.05*width_, upper-range_*0.015,
"nb")
368 box_years[year] =
blackBox(integrated_lumi+0.005*width_, upper-range_*0.01, integrated_lumi+0.055*width_, upper-range_*0.055)
370 line_years[year] = ROOT.TLine(runsPerYear[year][0], lower, runsPerYear[year][0], upper)
371 text_years[year] = ROOT.TPaveText( runsPerYear[year][0]+0.01*width_, upper-range_*0.05,
372 runsPerYear[year][0]+0.05*width_, upper-range_*0.015,
"nb")
373 box_years[year] =
blackBox(runsPerYear[year][0]+0.01*width_, upper-range_*0.015, runsPerYear[year][0]+0.05*width_, upper-range_*0.05)
376 box_years[year].Draw(
"L")
377 line_years[year].Draw()
380 text_years[year].AddText(
str(year))
381 text_years[year].SetTextAlign(22)
382 text_years[year].SetTextSize(0.025)
383 text_years[year].SetFillColor(10)
384 text_years[year].Draw()
390 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_IntegratedLumi.pdf")
391 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_IntegratedLumi.png")
393 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_RunNumber.pdf")
394 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_RunNumber.png")
404 (options,args) = parser.parse_args()
407 inputFileName = options.inputFileName
408 if os.path.isfile(inputFileName) ==
False :
409 print (
"File "+inputFileName+
" not exist!")
412 plotConfigFile = open(options.plotConfigFile)
413 plotConfigJson = json.load(plotConfigFile)
414 plotConfigFile.close()
416 usePixelQuality = options.usePixelQuality
417 withPixelQuality =
"" 418 if(usePixelQuality) :
419 withPixelQuality =
"WithPixelQuality" 420 showLumi = options.showLumi
422 years = options.years
428 runsPerYear[year] = []
430 accumulatedLumiPerRun = {}
436 CMSSW_Dir = os.getenv(
"CMSSW_BASE")
438 inputLumiFile = CMSSW_Dir +
"/src/Alignment/OfflineValidation/data/lumiperrun"+
str(year)+
".txt" 439 if os.path.isfile(inputLumiFile) ==
False :
440 print (
"File "+inputLumiFile+
" not exist!")
442 lumiFile = open(inputLumiFile,
'r') 443 lines = lumiFile.readlines() 447 run =
int(line.split()[0])
448 integrated_lumi =
float(line.split()[1])/lumiScaleFactor
451 runsPerYear[year].
append(run)
455 accumulatedLumiPerRun[run] = integrated_lumi
457 accumulatedLumiPerRun[run] = accumulatedLumiPerRun[lastRun]+integrated_lumi
466 runsPerYear = OrderedDict(sorted(runsPerYear.items(), key=
lambda t: t[0]))
468 accumulatedLumiPerRun = OrderedDict(sorted(accumulatedLumiPerRun.items(), key=
lambda t: t[0]))
473 db = plotConfigJson[
"pixelDataBase"]
474 pixel_template = plotConfigJson[
"pixelLocalReco"]
475 db = db.replace(
"sqlite_file:",
"").
replace(
"sqlite:",
"")
476 db = db.replace(
"frontier://FrontierProd/CMS_CONDITIONS",
"pro")
477 db = db.replace(
"frontier://FrontierPrep/CMS_CONDITIONS",
"dev")
479 con = conddb.connect(url = conddb.make_url(db))
480 session = con.session()
482 IOV = session.get_dbtype(conddb.IOV)
483 iovs = set(session.query(IOV.since).
filter(IOV.tag_name == pixel_template).
all())
485 pixelLocalRecos = sorted([
int(item[0])
for item
in iovs])
489 substructures = list(plotConfigJson[
"substructures"].
keys())
494 f = ROOT.TFile(inputFileName,
"READ")
496 for label
in list(plotConfigJson[
"baryCentreLabels"].
keys()) :
500 t = f.Get(
"PixelBaryCentreAnalyzer"+withPixelQuality+
"/PixelBarycentre")
502 t = f.Get(
"PixelBaryCentreAnalyzer"+withPixelQuality+
"/PixelBarycentre_"+label)
509 print(
"File "+inputFileName+
" not accessible")
512 for substructure
in substructures :
513 for coord
in [
'x',
'y',
'z'] :
514 plotbarycenter(bc,coord,plotConfigJson,substructure, runsPerYear,pixelLocalRecos,accumulatedLumiPerRun, withPixelQuality,showLumi)
517 if __name__ ==
"__main__":
def replace(string, replacements)
def readBaryCentreAnalyzerTree(t, branch_names, accumulatedLumiPerRun, showLumi, isEOY)
def blackBox(x1, y1, x2, y2)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def plotbarycenter(bc, coord, plotConfigJson, substructure, runsPerYear, pixelLocalRecos, accumulatedLumiPerRun, withPixelQuality, showLumi)
def callback_rootargs(option, opt, value, parser)
def vararg_callback(option, opt_str, value, parser)
def findRunIndex(run, runs)