4 from array
import array
6 from collections
import OrderedDict
10 ROOT.gSystem.Load(
"libFWCoreFWLite.so")
12 import CondCore.Utilities.conddblib
as conddb
13 import Alignment.OfflineValidation.TkAlAllInOneTool.findAndChange
as fnc
16 lumiScaleFactor = 1000
33 for arg
in parser.rargs:
35 if arg[:2] ==
"--" and len(arg) > 2:
38 if arg[:1] ==
"-" and len(arg) > 1
and not floatable(arg):
42 del parser.rargs[:len(value)]
43 setattr(parser.values, option.dest, value)
46 usage = (
'usage: %prog [options]\n' 47 +
'%prog -h for help')
48 parser = optparse.OptionParser(usage)
50 parser.add_option(
"--inputFileName", dest=
"inputFileName", default=
"PixelBaryCentre.root",help=
"name of the ntuple file that contains the barycentre tree")
51 parser.add_option(
"--plotConfigFile", dest=
"plotConfigFile", default=
"PixelBaryCentrePlotConfig.json",help=
"json file that configs the plotting")
53 parser.add_option(
"--usePixelQuality",action=
"store_true", dest=
"usePixelQuality", default=
False,help=
"whether use SiPixelQuality")
54 parser.add_option(
"--showLumi",action=
"store_true", dest=
"showLumi", default=
False,help=
"whether use integrated lumi as x-axis")
55 parser.add_option(
"--years", dest=
"years", default = [2017], action=
"callback", callback=vararg_callback, help=
"years to plot")
57 parser.add_option(
"-l",action=
"callback",callback=callback_rootargs)
58 parser.add_option(
"-q",action=
"callback",callback=callback_rootargs)
59 parser.add_option(
"-b",action=
"callback",callback=callback_rootargs)
67 print(
"Empty run list!")
73 print(
"Only one run but the run requested is before the run!")
80 elif(run >= runs[len(runs)-1]) :
83 return ROOT.TAxis(len(runs)-1,
array(
'd',runs)).FindBin(run) - 1
93 runs = list(accumulatedLumiPerRun.keys())
99 if(iov.run>runs[len(runs)-1]
or iov.run<runs[0]):
102 if(iov.run!=current_run) :
103 run_lumis[iov.run] = [iov.ls]
104 run_maxlumi[iov.run] = iov.ls
106 run_lumis[iov.run].
append(iov.ls)
107 if(run_maxlumi[iov.run]<iov.ls):
108 run_maxlumi[iov.run] = iov.ls
110 current_run = iov.run
114 for branch_name
in branch_names :
115 for coord
in [
"x",
"y",
"z"] :
116 pos[coord+
"_"+branch_name] =
array(
'd',[])
118 pos[coord+
"max_"+branch_name] = -9999
119 pos[coord+
"min_"+branch_name] = 9999
122 zeros =
array(
'd',[])
125 runlumi =
array(
'd',[])
126 runlumiplot =
array(
'd',[])
127 runlumiplot_error =
array(
'd',[])
133 if(iov.run>runs[len(runs)-1]
or iov.run<runs[0]):
136 if(isEOY
and iov.run>=320413
and iov.run<=325175):
144 instLumi = accumulatedLumiPerRun[ runs[run_index] ]
146 instLumi = accumulatedLumiPerRun[ runs[run_index] ] - accumulatedLumiPerRun[ runs[run_index-1] ]
151 if(len(run_lumis[iov.run])>1) :
153 runlumi.append(0.0+instLumi*iov.ls*1.0/run_maxlumi[iov.run])
155 runlumi.append(accumulatedLumiPerRun[ runs[run_index-1] ]+instLumi*iov.ls*1.0/run_maxlumi[iov.run])
158 runlumi.append(accumulatedLumiPerRun[ runs[run_index] ])
161 if(len(run_lumis[iov.run])>1) :
162 runlumi.append(iov.run+iov.ls*1.0/run_maxlumi[iov.run])
165 runlumi.append(iov.run)
170 for branch_name
in branch_names :
171 pos_ = {
"x":10000*getattr(iov, branch_name).x(),
172 "y":10000*getattr(iov, branch_name).y(),
173 "z":10000*getattr(iov, branch_name).z()}
175 for coord
in [
"x",
"y",
"z"] :
176 pos[coord+
"_"+branch_name].
append(pos_[coord])
178 if(pos_[coord]>pos[coord+
"max_"+branch_name]) :
179 pos[coord+
"max_"+branch_name] = pos_[coord]
181 if(pos_[coord]<pos[coord+
"min_"+branch_name]) :
182 pos[coord+
"min_"+branch_name] = pos_[coord]
185 for iov
in range(len(runlumi)-1) :
186 runlumiplot.append(0.5*(runlumi[iov]+runlumi[iov+1]))
187 runlumiplot_error.append(0.5*(runlumi[iov+1]-runlumi[iov]))
189 runlumiplot.append(runlumiplot[len(runlumiplot_error)-1]+2*runlumiplot_error[len(runlumiplot_error)-1])
190 runlumiplot_error.append(runlumiplot_error[len(runlumiplot_error)-1])
192 v_runlumiplot = ROOT.TVectorD(len(runlumiplot),runlumiplot)
193 v_runlumiplot_error = ROOT.TVectorD(len(runlumiplot_error),runlumiplot_error)
196 v_zeros = ROOT.TVectorD(len(zeros),zeros)
201 for branch_name
in branch_names :
202 for coord
in [
"x",
"y",
"z"] :
203 v_pos[coord] = ROOT.TVectorD(len(pos[coord+
"_"+branch_name]),pos[coord+
"_"+branch_name])
205 barryCentre[coord+
'_'+branch_name] = ROOT.TGraphErrors(v_runlumiplot, v_pos[coord], v_runlumiplot_error, v_zeros)
206 barryCentre[
'a_'+coord+
'_'+branch_name] = pos[coord+
"_"+branch_name]
208 barryCentre[coord+
'max_'+branch_name] = pos[coord+
"max_"+branch_name]
209 barryCentre[coord+
'min_'+branch_name] = pos[coord+
"min_"+branch_name]
211 barryCentre[
'v_runlumiplot'] = v_runlumiplot
212 barryCentre[
'v_runlumierror'] = v_runlumiplot_error
213 barryCentre[
'v_zeros'] = v_zeros
219 x =
array(
'd',[x1, x2, x2, x1, x1])
220 y =
array(
'd',[y1, y1, y2, y2, y1])
221 v_x = ROOT.TVectorD(len(x),x)
222 v_y = ROOT.TVectorD(len(y),y)
224 gr = ROOT.TGraph(v_x,v_y)
225 gr.SetLineColor(ROOT.kBlack)
230 def plotbarycenter(bc,coord,plotConfigJson, substructure,runsPerYear,pixelLocalRecos,accumulatedLumiPerRun, withPixelQuality,showLumi) :
231 runs = list(accumulatedLumiPerRun.keys())
233 years = list(runsPerYear.keys())
235 labels = list(bc.keys())
237 can = ROOT.TCanvas(
"barycentre_"+substructure+
"_"+coord,
"", 2000, 900)
248 for label
in labels :
249 gr[label] = ROOT.TGraph()
250 gr[label] = bc[label][coord+
"_"+substructure]
252 gr[label].SetMarkerStyle(8)
253 gr[label].SetMarkerSize(0)
254 gr[label].SetMarkerStyle(8)
255 gr[label].SetMarkerSize(0)
256 gr[label].SetLineColor(plotConfigJson[
"colorScheme"][label])
258 width_ = gr[label].GetXaxis().GetXmax() - gr[label].GetXaxis().GetXmin()
259 xmax = gr[label].GetXaxis().GetXmax()
262 upper = bc[label][coord+
"max_"+substructure]
263 lower = bc[label][coord+
"min_"+substructure]
266 upper =
max(upper, bc[label][coord+
"max_"+substructure])
267 lower =
min(lower, bc[label][coord+
"min_"+substructure])
271 upper = upper * scale
273 upper = upper / scale
275 lower = lower / scale
277 lower = lower * scale
278 range_ = upper - lower
281 for label
in labels :
283 gr[label].GetYaxis().SetRangeUser(lower, upper)
284 gr[label].GetYaxis().SetTitle(plotConfigJson[
"substructures"][substructure]+
" barycentre ("+coord+
") [#mum]")
285 gr[label].GetXaxis().SetTitle(
"Run Number")
286 gr[label].GetYaxis().CenterTitle(
True)
287 gr[label].GetXaxis().CenterTitle(
True)
288 gr[label].GetYaxis().SetTitleOffset(0.80)
289 gr[label].GetYaxis().SetTitleSize(0.055)
290 gr[label].GetXaxis().SetTitleOffset(0.80)
291 gr[label].GetXaxis().SetTitleSize(0.055)
292 gr[label].GetXaxis().SetMaxDigits(6)
294 gr[label].GetXaxis().SetTitle(
"Delivered luminosity [1/fb]")
302 gr_dummyFirstRunOfTheYear =
blackBox(-999, 10000, -999, -10000)
303 gr_dummyFirstRunOfTheYear.SetLineColor(ROOT.kBlack)
304 gr_dummyFirstRunOfTheYear.SetLineStyle(1)
305 gr_dummyFirstRunOfTheYear.Draw(
"L")
306 gr_dummyPixelReco =
blackBox(-999, 10000, -999, -10000)
307 gr_dummyPixelReco.SetLineColor(ROOT.kGray+1)
308 gr_dummyPixelReco.SetLineStyle(3)
309 gr_dummyPixelReco.Draw(
"L")
310 gr_dummyFirstRunOfTheYear.SetTitle(
"First run of the year")
311 gr_dummyPixelReco.SetTitle(
"Pixel calibration update")
313 for label
in labels :
314 gr[label].SetTitle(plotConfigJson[
"baryCentreLabels"][label])
315 legend = can.BuildLegend()
316 legend.SetShadowColor(0)
317 legend.SetFillColor(0)
318 legend.SetLineColor(1)
320 for label
in labels :
321 gr[label].SetTitle(
"")
327 years_label +=
str(year)
329 years_label = years_label.rstrip(
"+")
332 CMSworkInProgress = ROOT.TPaveText( xmax-0.3*width_, upper+range_*0.005,
333 xmax, upper+range_*0.055,
"nb")
334 CMSworkInProgress.AddText(
"CMS #bf{#it{Preliminary} ("+years_label+
" pp collisions)}")
335 CMSworkInProgress.SetTextAlign(32)
336 CMSworkInProgress.SetTextSize(0.04)
337 CMSworkInProgress.SetFillColor(10)
338 CMSworkInProgress.Draw()
343 for since
in pixelLocalRecos :
346 integrated_lumi = accumulatedLumiPerRun[runs[run_index]]
347 line_pixels[since] = ROOT.TLine(integrated_lumi, lower, integrated_lumi, upper)
350 line_pixels[since] = ROOT.TLine(since, lower, since, upper)
352 line_pixels[since].SetLineColor(ROOT.kGray+1)
353 line_pixels[since].SetLineStyle(3)
354 line_pixels[since].Draw()
360 if(len(years)>1
or (
not showLumi) ) :
365 integrated_lumi = accumulatedLumiPerRun[runs[run_index]]
366 line_years[year] = ROOT.TLine(integrated_lumi, lower, integrated_lumi, upper)
367 text_years[year] = ROOT.TPaveText( integrated_lumi+0.01*width_, upper-range_*0.05,
368 integrated_lumi+0.05*width_, upper-range_*0.015,
"nb")
369 box_years[year] =
blackBox(integrated_lumi+0.005*width_, upper-range_*0.01, integrated_lumi+0.055*width_, upper-range_*0.055)
371 line_years[year] = ROOT.TLine(runsPerYear[year][0], lower, runsPerYear[year][0], upper)
372 text_years[year] = ROOT.TPaveText( runsPerYear[year][0]+0.01*width_, upper-range_*0.05,
373 runsPerYear[year][0]+0.05*width_, upper-range_*0.015,
"nb")
374 box_years[year] =
blackBox(runsPerYear[year][0]+0.01*width_, upper-range_*0.015, runsPerYear[year][0]+0.05*width_, upper-range_*0.05)
377 box_years[year].Draw(
"L")
378 line_years[year].Draw()
381 text_years[year].AddText(
str(year))
382 text_years[year].SetTextAlign(22)
383 text_years[year].SetTextSize(0.025)
384 text_years[year].SetFillColor(10)
385 text_years[year].Draw()
391 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_IntegratedLumi.pdf")
392 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_IntegratedLumi.png")
394 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_RunNumber.pdf")
395 can.SaveAs(
"baryCentre"+withPixelQuality+
"_"+coord+
"_"+substructure+
"_"+years_label+
"_RunNumber.png")
405 (options,args) = parser.parse_args()
408 inputFileName = options.inputFileName
409 if os.path.isfile(inputFileName) ==
False :
410 print (
"File "+inputFileName+
" not exist!")
413 plotConfigFile = open(options.plotConfigFile)
414 plotConfigJson = json.load(plotConfigFile)
415 plotConfigFile.close()
417 usePixelQuality = options.usePixelQuality
418 withPixelQuality =
"" 419 if(usePixelQuality) :
420 withPixelQuality =
"WithPixelQuality" 421 showLumi = options.showLumi
423 years = options.years
429 runsPerYear[year] = []
431 accumulatedLumiPerRun = {}
438 inputLumiFile = fnc.digest_path(
"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)