ATLAS Offline Software
AtlRunQueryRoot.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 #
5 # ----------------------------------------------------------------
6 # Script : AtlRunQueryRoot.py
7 # Project: AtlRunQuery
8 # Purpose: ROOT interactions
9 # Authors: Andreas Hoecker (CERN), Joerg Stelzer (DESY)
10 # Created: Dec 13, 2008
11 # ----------------------------------------------------------------
12 #
13 # ---------------------------------------------------------------------------------------------------
14 # ROOT TTree making and plotting
15 # ---------------------------------------------------------------------------------------------------
16 
17 import datetime, sys, os
18 from array import array
19 from CoolRunQuery.utils.AtlRunQueryUtils import importroot
20 importroot() # needed before any root import to switch to batch mode
21 from ROOT import TFile, TTree, TCanvas, gPad, TGraph, TColor, TStyle, TGaxis, gROOT, gStyle, TText, TH1F, TLegend, TF1
22 
23 # ---------------------------------------------------------------------------------------------------
24 # Creation of ROOT output file
25 # ---------------------------------------------------------------------------------------------------
26 
27 # general style
28 def SetStyle( whiteCanvas = False ):
29 
30  # colors
31  c_Canvas = TColor.GetColor( "#f0f0f0" ) # TColor.GetColor( "#e9e6da" )
32  if whiteCanvas:
33  c_Canvas = TColor.GetColor( 10 )
34  c_FrameFill = 10
35  c_TitleBox = TColor.GetColor( "#dae1e6" )
36 
37  myStyle = TStyle( gROOT.GetStyle("Plain") )
38  myStyle.SetName("myStyle")
39 
40  myStyle.SetLineStyleString( 5, "[52 12]" )
41  myStyle.SetLineStyleString( 6, "[22 12]" )
42  myStyle.SetLineStyleString( 7, "[22 10 7 10]" )
43 
44  # pretty colors
45  myStyle.SetPalette(1)
46 
47  # use plain black on white colors
48  myStyle.SetFrameBorderMode(0)
49  myStyle.SetCanvasBorderMode(0)
50  myStyle.SetPadBorderMode(0)
51  myStyle.SetPadColor(0)
52  myStyle.SetFillStyle(0)
53 
54  myStyle.SetLegendBorderSize(0)
55 
56  myStyle.SetTitleFillColor( c_TitleBox )
57  myStyle.SetFrameFillColor( c_FrameFill )
58  myStyle.SetCanvasColor( c_Canvas )
59 
60  # set the paper & margin sizes
61  myStyle.SetPaperSize(20,26)
62  myStyle.SetPadTopMargin(0.11)
63  myStyle.SetPadRightMargin(0.05)
64  myStyle.SetPadBottomMargin(0.11)
65  myStyle.SetPadLeftMargin(0.12)
66 
67  # use bold lines and markers
68  myStyle.SetMarkerStyle(21)
69  myStyle.SetMarkerSize(0.3)
70  myStyle.SetHistLineWidth(2)
71  myStyle.SetLineStyleString(2,"[12 12]") # postscript dashes
72 
73  # do not display any of the standard histogram decorations
74  myStyle.SetOptTitle(1)
75  myStyle.SetTitleH(0.052)
76 
77  myStyle.SetOptStat(0)
78  myStyle.SetOptFit(0)
79 
80  # put tick marks on top and RHS of plots
81  myStyle.SetPadTickX(1)
82  myStyle.SetPadTickY(1)
83 
84  # use this style
85  gROOT.GetListOfStyles().Add(myStyle)
86  gROOT.SetStyle("myStyle")
87 
88 def SaveGraphsToFile( filename, varnames, xvecs, yvecs, addparamName = None, addparamVal = None ):
89  f = TFile.Open( filename, 'RECREATE' )
90  if len(xvecs) != len(yvecs) or len(varnames) != len(xvecs):
91  print ('ERROR: wrong dimensions in "AtlRunQueryRoot.SaveGraphsToFile"')
92  return
93  for ik in range(len(xvecs)):
94  xv = xvecs[ik]
95  yv = yvecs[ik]
96  g = TGraph( len(xv) )
97  g.SetName ( "graph%i" % (ik+1) )
98  g.SetTitle( varnames[ik] )
99  for i in range(len(xv)):
100  g.SetPoint( i, xv[i], yv[i] )
101  g.Write()
102 
103  # save additional parameter
104  if addparamName is not None:
105  addparam = TF1( addparamName, "[0]", 0, 1 )
106  addparam.SetParameter( 0, addparamVal )
107  addparam.Write()
108 
109  f.Close()
110 
111 def MakePlots( tree, datapath ):
112 
113  # batch mode -> no windows
114 
115  gROOT.SetBatch( 1 )
116  if tree is None:
117  print ('ERROR: input tree is None')
118  return
119 
120  # style --------------------------------------------
121  #steelBlue = TColor.GetColor( "#4863A0" )
122  #slateBlue = TColor.GetColor( "#574EC7" )
123  dogerBlue = TColor.GetColor( "#1569C7" )
124  dogerBlue3 = TColor.GetColor( "#1569C7" )
125  graphCol = dogerBlue
126  markerCol = dogerBlue3
127 
128  SetStyle()
129  # --------------------------------------------------
130 
131  # reference
132  reflist = [ 'Run' ]
133 
134  # types that can be plotted
135  validtypes = [ 'int', 'float', 'double', 'short', 'long' ]
136 
137  # get list of leaves for plotting
138  leafList = tree.GetListOfLeaves()
139  varlist = []
140 
141  for leaf in leafList:
142  if leaf is not None:
143  # check type, only values are plotted
144  typename = leaf.GetTypeName().lower()
145  valid = False
146  for vt in validtypes:
147  if vt in typename:
148  valid = True
149  if valid:
150  varlist.append( leaf.GetName() )
151 
152  # plot
153  TGaxis.SetMaxDigits(6)
154  c = []
155  g = []
156  fnames = []
157  hnames = []
158  for ref in reflist:
159  # sanity check
160  if ref not in varlist:
161  print ('Big troubles in "MakePlots" --> reference variable "%s" not in TTree' % ref)
162  sys.exit(1)
163 
164  for var in varlist:
165  if var == ref:
166  continue
167 
168  vreg = var.replace('-','_')
169  rreg = ref.replace('-','_')
170 
171  hname = "h_%s_%s" % (ref, var)
172  c.append( TCanvas( "c_%s" % hname, "ATLAS Run Query: %s vs. %s" % (var, ref), 0, 0, 600, 500 ) )
173  tree.Draw( "%s:%s" % (vreg, rreg), "", "goff" )
174  g.append( TGraph( tree.GetSelectedRows(), tree.GetV2(), tree.GetV1() ) )
175  g[-1].SetTitle( "ATLAS Run Query: %s vs. %s" % (var, ref) )
176  g[-1].SetMarkerStyle( 20 )
177  g[-1].SetMarkerColor( markerCol )
178  g[-1].SetMarkerSize( 1.2 )
179  g[-1].SetLineWidth( 2 )
180  g[-1].SetLineColor( graphCol )
181  g[-1].GetXaxis().SetTitleOffset( 1.25 )
182  g[-1].GetYaxis().SetTitleOffset( 1.75 )
183  g[-1].GetXaxis().SetTitle( ref )
184  g[-1].GetYaxis().SetTitle( var )
185  g[-1].GetXaxis().SetLabelOffset( 0.012 )
186  g[-1].GetYaxis().SetLabelOffset( 0.012 )
187  g[-1].GetXaxis().SetNdivisions( 506 )
188 
189  # some global style settings
190  scale = 1
191  gPad.SetTicks()
192  gPad.SetLeftMargin ( 0.138*scale )
193  gPad.SetRightMargin ( 0.050*scale )
194  gPad.SetBottomMargin( 0.120*scale )
195 
196  g[-1].Draw("alp")
197 
198  c[-1].Update()
199  fnames.append( '%s/atlrunquery_%s.png' % (datapath, hname) )
200  hnames.append( '%s versus %s' % (var, ref ) )
201  c[-1].Print( fnames[-1] )
202 
203  return hnames, fnames
204 
205 # ---------------------------------------------------------------------------------------------------
206 # Creation of ROOT output file
207 # ---------------------------------------------------------------------------------------------------
208 
209 def makeLBPlotSummaryForLHC( lbrange, xvecStb, yvec, runNr, datapath, printText = '' ):
210 
211  plotDevelop = False
212  if plotDevelop:
213  # pickle input
214  import json
215  store = (list(lbrange), xvecStb, yvec, runNr, datapath)
216  pf = open( '%s/plotinput.json' % datapath, 'w' )
217  try:
218  json.dump(store, pf)
219  except Exception as ex:
220  print ('ERROR: could not store plotinput data')
221  print("Reason: %s" % ex)
222  sys.exit(1)
223  pf.close()
224 
225  # sanity check
226  if not lbrange or len(yvec)==0 or not yvec[0]:
227  return "None"
228 
229  ylegend = ['Intensity Beam-1', 'Intensity Beam-2', 'Beam energy', 'Online inst. luminosity' ]
230 
231  SetStyle()
232  name = 'LHCsummary_vs_lb_run_%i' % (runNr)
233  #print ("Attempt printing", name)
234  title = 'LHC summary vs. LB for run_%i' % (runNr)
235  szescale = 1.2
236  c = TCanvas( name, title, 0, 0, int(530*szescale), int(400*szescale) )
237  c.GetPad(0).SetTopMargin(0.13)
238  c.GetPad(0).SetLeftMargin(0.09)
239  c.GetPad(0).SetRightMargin(1.3)
240  # c.GetPad(0).SetGrid()
241  x1 = lbrange[0]
242  x2 = lbrange[-1]
243 
244  h = TH1F( name, title, x2 - x1 + 1, x1, x2 + 1 ) # for stable beams
245  h.GetXaxis().SetTitle( 'Luminosity block number' )
246  h.GetXaxis().SetTitleOffset( 1.3 )
247  h.GetYaxis().SetTitle( 'Beam intensity (10^{11} protons)' )
248  h.GetYaxis().SetTitleOffset( 1.0 )
249  h.SetTitle( title )
250 
251  hg = []
252  for iy,y in enumerate(yvec):
253  hg.append( TH1F( name + 'g%i' % iy, title, x2 - x1 + 1, x1, x2 + 1 ) )
254  for lb in lbrange:
255  hg[iy].SetBinContent( lb, y[lb] )
256 
257  # first draw histogram
258  ymax = max(hg[0].GetMaximum(),hg[1].GetMaximum())
259  y2 = ymax*1.3
260  if y2 <= 0:
261  y2 = 1
262  h.SetMinimum(0)
263  h.SetMaximum(y2)
264  h.Draw("0")
265  gPad.SetTicks(1, 0)
266 
267  # beam energy (rescale)
268  ebcol = TColor.GetColor( "#306754" )
269  heb = hg[2]
270  heb.Scale( 1./1000.0, "nosw2" ) # in TeV
271  if heb.GetMaximum()==0:
272  heb.SetMaximum(1)
273  ebmax = heb.GetMaximum()*1.3
274 
275  # one more sanity check
276  if ebmax == 0:
277  return "None"
278 
279  heb.SetLineColor( ebcol )
280  heb.SetLineWidth( 1 )
281  scale = h.GetMaximum()/ebmax
282  heb.SetFillColor( TColor.GetColor("#C3FDB8") )
283  heb.Scale( scale, "nosw2" )
284  heb.Draw("same")
285 
286  if xvecStb:
287  for lb in lbrange:
288  if lbrange[lb] in xvecStb:
289  h.SetBinContent( lbrange[lb], yvec[2][lb]*scale/1000.0 ) # draw for beam energy (in TeV)
290  h.SetFillColor( TColor.GetColor( "#63bD58" ) )
291  h.SetFillStyle( 3007 )
292  h.SetLineColor( TColor.GetColor( "#C3FDB8" ) )
293  h.SetLineWidth( 1 )
294  h.SetLineStyle( 1 )
295  h.Draw("same")
296  hebp = TH1F( heb )
297  hebp.SetFillColor( 0 )
298  hebp.Draw("same")
299 
300 
301  colorList = [TColor.GetColor( "#255EC7" ), TColor.GetColor( "#E42217" ), TColor.GetColor( "#2212EE" ), TColor.GetColor( "#22EE33" )]
302 
303  for ig in range(2):
304  if ig < len(colorList):
305  hg[ig].SetLineColor( colorList[ig] )
306  hg[ig].SetLineWidth( 2 )
307  hg[ig].Draw("same")
308 
309  # logo
310  now = str(datetime.datetime.today())
311  t = TText( h.GetXaxis().GetXmax(), y2*1.01, 'ATLAS Run Query %s' % now[:now.find('.')] )
312  t.SetTextAlign( 31 )
313  t.SetTextSize( 0.035 )
314  t.SetTextSize( 0.030 )
315  t.SetTextColor( TColor.GetColor( "#888888" ) )
316  t.Draw()
317 
318  drawLegend = False
319  dx = 1 - c.GetRightMargin() - c.GetLeftMargin() - 0.05
320  dy = 0.1
321  legend = TLegend( 1 - c.GetRightMargin() - dx, 1 - c.GetTopMargin() - dy - 0.035,
322  1 - c.GetRightMargin(), 1 - c.GetTopMargin() - 0.035)
323  # legend.SetFillStyle( 1001 )
324  # legend.SetFillColor( TColor.GetColor( "#FFFFFF" ) )
325  legend.SetNColumns(2)
326  legend.SetBorderSize( 1 )
327  legend.SetMargin( 0.15 )
328  legend.SetLineColor( 0 )
329  legend.SetTextColor( 1 )
330  for ig,hgg in enumerate(hg):
331  if ylegend[ig]:
332  opt = "l"
333  if ig == 2:
334  opt = "FL"
335  legend.AddEntry( hgg, ylegend[ig], opt )
336  drawLegend = True
337 
338  if xvecStb:
339  legend.AddEntry( h, "LBs with stable beams","F" )
340  drawLegend = True
341  if drawLegend:
342  legend.Draw("same")
343 
344  # redraw axis
345  h.Draw( "sameaxis" )
346 
347  # draw beam energy axis on the right side
348  if h.GetMaximum() > 0:
349  axis = TGaxis( x2 + 1, 0, x2 + 1, h.GetMaximum(), 0, ebmax, 510, "+L" )
350  axis.SetLabelOffset( 0.007 )
351  axis.SetTitleOffset( 1.2 )
352  axis.SetTitleSize( h.GetXaxis().GetTitleSize() )
353  axis.SetLabelSize( 0.04 )
354  axis.SetLineColor( ebcol )
355  axis.SetTitleColor( ebcol )
356  axis.SetLabelColor( ebcol )
357  axis.SetTitle( "Beam energy (TeV)" )
358  axis.Draw()
359 
360  c.Update()
361  fnames = '%s/atlrunquery_%s.png' % (datapath, name)
362  c.Print( fnames )
363  #print ("Printing",fnames)
364 
365  return fnames
366 
367 def makeTimePlotList( xvec, xveclb, yvec, toffset, dt, ymin, ymax,
368  xtit, ytit, ylegend, name, title, datapath, printText = '' ):
369 
370  # sanity check
371  if not xvec or len(yvec)==0 or not yvec[0]:
372  return "None"
373 
374  SetStyle()
375 
376  gStyle.SetPadTickX(0)
377  gStyle.SetPadTickY(1)
378 
379  c = TCanvas( name, title, 0, 0, 530, 430 )
380  c.GetPad(0).SetTopMargin(0.22)
381  c.GetPad(0).SetGrid()
382 
383  # create the graphs
384  graphs = []
385  y1 = 1e30
386  y2 = -1e30
387  for ig in range(len(yvec)):
388  n = len(yvec[ig])
389  g = TGraph( n )
390  for i in range(n):
391  g.SetPoint( i, xvec[ig][i], yvec[ig][i] )
392  if ymin == ymax:
393  y1 = min(y1,yvec[ig][i])
394  y2 = max(y2,yvec[ig][i])
395  graphs.append( g )
396 
397  if ymin != ymax:
398  y1 = ymin
399  y2 = ymax
400 
401  # create the frame
402  frame = TH1F( name, title, int(dt/2), 0, dt )
403 
404  # x-axis is time format
405  frame.GetXaxis().SetTimeDisplay(1)
406  # use rather default: frame.GetXaxis().SetTimeFormat("%d-%Hh")
407 
408  # =================================
409  # toffset += 7*3600 # BUG in ROOT ???
410  # =================================
411 
412  frame.GetXaxis().SetTimeOffset(toffset, "gmt") # assumes time given was local
413 
414  frame.GetXaxis().SetTitle( xtit )
415  frame.GetXaxis().SetTitleOffset( 1.3 )
416  frame.GetYaxis().SetTitle( ytit )
417  frame.GetYaxis().SetTitleOffset( 1.4 )
418  frame.SetTitle( title )
419  frame.SetMinimum(y1 - (y2-y1)*0.1)
420  frame.SetMaximum(y2 + (y2-y1)*0.3)
421  frame.Draw()
422 
423  colorList = [TColor.GetColor( "#154EB7" ), TColor.GetColor( "#82CAFF" ), TColor.GetColor( "#D11B17" ), TColor.GetColor( "#FF9A4D" )]
424  styleList = [1,1,1,1]
425 
426  for ig,g in enumerate(graphs):
427  if ig < len(colorList):
428  g.SetLineColor( colorList[ig] )
429  g.SetLineStyle( styleList[ig] )
430  g.SetLineWidth( 2 )
431  g.Draw("same")
432 
433  if printText:
434  t = TText( frame.GetXaxis().GetXmax(), y2*1.01, printText )
435  t.SetTextAlign( 31 )
436  t.SetTextSize( 0.035 )
437  t.Draw()
438 
439  # logo
440  now = str(datetime.datetime.today())
441  tlogo = TText( frame.GetXaxis().GetXmax()*1.025 - frame.GetXaxis().GetXmin()*0.025, y1,
442  'ATLAS Run Query %s' % now[:now.find('.')] )
443  tlogo.SetTextSize( 0.030 )
444  tlogo.SetTextColor( TColor.GetColor( "#aaaaaa" ) )
445  tlogo.SetTextAngle(90)
446  tlogo.Draw()
447 
448  # axis for LBs
449  lbcol = TColor.GetColor( "#777777" )
450  axis = TGaxis( frame.GetXaxis().GetXmin(), frame.GetMaximum(),
451  frame.GetXaxis().GetXmax(), frame.GetMaximum(),
452  1, len(xveclb), 505, "-L" )
453  axis.SetLabelOffset( frame.GetXaxis().GetLabelOffset() )
454  axis.SetTitleOffset( 1.4 )
455  axis.SetTitleSize( frame.GetXaxis().GetTitleSize() )
456  axis.SetLabelSize( frame.GetXaxis().GetLabelSize() )
457  axis.SetLineColor( lbcol )
458  axis.SetTitleColor( lbcol )
459  axis.SetLabelColor( lbcol )
460  axis.SetTitle( "Luminosity block number (indicative)" )
461  axis.Draw()
462 
463  # legend
464  drawLegend = False
465  dx = 1 - c.GetRightMargin() - c.GetLeftMargin() - 0.05
466  dy = 0.1
467  legend = TLegend( 1 - c.GetRightMargin() - dx, 1 - c.GetTopMargin() - dy - 0.035,
468  1 - c.GetRightMargin(), 1 - c.GetTopMargin() - 0.035)
469  # legend.SetFillStyle( 1001 )
470  # legend.SetFillColor( TColor.GetColor( "#FFFFFF" ) )
471  legend.SetNColumns(2)
472  legend.SetBorderSize( 1 )
473  legend.SetMargin( 0.15 )
474  legend.SetLineColor( 0 )
475  legend.SetTextColor( 1 )
476  legend.SetTextSize( 0.037 )
477  for ig,g in enumerate(graphs):
478  if ylegend[ig]:
479  h = g.GetHistogram()
480  h.SetLineColor( colorList[ig] )
481  h.SetLineStyle( styleList[ig] )
482  h.SetLineWidth( 2 )
483  legend.AddEntry( h, ylegend[ig], "l" )
484  drawLegend = True
485  if drawLegend:
486  legend.Draw("same")
487 
488  # redraw axis
489  frame.Draw( "sameaxis" )
490 
491  c.Update()
492  fnames = '%s/atlrunquery_%s.png' % (datapath, name)
493  c.Print( fnames )
494 
495  return fnames
496 
497 
498 def makeLBPlotList( xvec, xvecStb, yvec, xtitle, ytitle, ylegend, histname, histtitle, datapath, printText = '', ymin = None , ymax = None ):
499 
500  # sanity check
501  if not xvec or len(yvec)==0 or not yvec[0]:
502  return "None"
503 
504  SetStyle()
505  c = TCanvas( histname, histtitle, 0, 0, 530, 400 )
506  c.GetPad(0).SetTopMargin(0.13)
507  c.GetPad(0).SetGrid()
508  x1 = xvec[0]
509  x2 = xvec[-1]
510 
511  h = TH1F( histname, histtitle, x2 - x1 + 1, x1, x2 + 1 ) # for stable beams
512  hg = []
513  for iy in range(len(yvec)):
514  hg.append( TH1F( histname + 'g%i' % iy, histtitle, x2 - x1 + 1, x1, x2 + 1 ) )
515  h.GetXaxis().SetTitle( xtitle )
516  h.GetXaxis().SetTitleOffset( 1.3 )
517  h.GetYaxis().SetTitle( ytitle )
518  h.GetYaxis().SetTitleOffset( 1.4 )
519  h.SetTitle( histtitle )
520 
521 
522  for i in range(len(xvec)):
523  for iy,y in enumerate(yvec):
524  if type(y[i]) is tuple:
525  val,valerr = y[i]
526  else:
527  val,valerr = y[i],0
528  hg[iy].SetBinContent( xvec[i], val )
529  hg[iy].SetBinError( xvec[i], valerr )
530  if xvec[i] in xvecStb:
531  h.SetBinContent( xvec[i], yvec[0][i] )
532 
533  if not ymax:
534  ymax = max([h.GetMaximum() for h in hg])
535  if ymax <= 0:
536  ymax = 1
537  ymax *= 1.2
538 
539  if not ymin:
540  ymin=0
541 
542  # first draw histogram
543  h.SetMinimum(ymin)
544  h.SetMaximum(ymax)
545  if xvecStb:
546  h.SetFillColor( TColor.GetColor( "#98AFC7" ) )
547  h.SetLineColor( TColor.GetColor( "#788FA7" ) )
548  h.SetLineWidth( 1 )
549  h.SetLineStyle( 1 )
550  h.Draw("")
551  else:
552  h.Draw("0")
553 
554  colorList = [TColor.GetColor( "#255EC7" ), TColor.GetColor( "#E42217" ), TColor.GetColor( "#2212EE" ), TColor.GetColor( "#22EE33" )]
555 
556  for ig,hgg in enumerate(hg):
557  if ig < len(colorList):
558  hgg.SetLineColor( colorList[ig] )
559  hgg.SetLineWidth( 2 )
560  hgg.Draw("e2same")
561 
562  if printText:
563  t = TText( h.GetXaxis().GetXmax(), (ymax-ymin)*1.01 + ymin, printText )
564  t.SetTextAlign( 31 )
565  t.SetTextSize( 0.035 )
566  t.Draw()
567 
568  # logo
569  now = str(datetime.datetime.today())
570  tlogo = TText( h.GetXaxis().GetXmax()*1.025, (ymax-ymin)*0.0+ymin, 'ATLAS Run Query %s' % now[:now.find('.')] )
571  tlogo.SetTextSize( 0.030 )
572  tlogo.SetTextColor( TColor.GetColor( "#888888" ) )
573  tlogo.SetTextAngle(90)
574  tlogo.Draw()
575 
576  drawLegend = False
577  dx = 0.4
578  dy = 0.10 + 0.03*(len(hg)-1)
579  legend = TLegend( 1 - c.GetRightMargin() - dx, 1 - c.GetTopMargin() - dy - 0.015,
580  1 - c.GetRightMargin(), 1 - c.GetTopMargin() - 0.015)
581  # legend.SetFillStyle( 1001 )
582  # legend.SetFillColor( TColor.GetColor( "#FFFFFF" ) )
583  legend.SetBorderSize( 1 )
584  legend.SetMargin( 0.15 )
585  legend.SetLineColor( 0 )
586  legend.SetTextColor( 1 )
587  for ig,hgg in enumerate(hg):
588  if ylegend[ig]:
589  legend.AddEntry( hgg, ylegend[ig], "l" )
590  drawLegend = True
591 
592  if xvecStb:
593  legend.AddEntry( h, "LBs with stable beams","F" )
594  drawLegend = True
595  if drawLegend:
596  legend.Draw("same")
597 
598  # redraw axis
599  h.Draw( "sameaxis" )
600 
601  c.Update()
602  fnames = '%s/atlrunquery_%s.png' % (datapath, histname)
603  c.Print( fnames )
604 
605  return fnames
606 
607 
608 def makeBSPlots( xvec, yvec, xtitle, ytitle, histname, histtitle, datapath, ymin, ymax, printText = '' ):
609 
610  # sanity check
611  if not xvec or len(yvec)==0 or not yvec[0]:
612  return None
613 
614  ROOT = importroot()
615 
616  SetStyle()
617  c = TCanvas( histname, histtitle, 0, 0, 530, 400 )
618  c.GetPad(0).SetTopMargin(0.13)
619  c.GetPad(0).SetGrid()
620 
621  bounds = [1]
622  for nlb in xvec:
623  bounds += [bounds[-1]+nlb]
624 
625  from array import array
626  h = TH1F( histname + 'g', histtitle, len(bounds)-1, array('f',bounds) )
627  h.GetXaxis().SetTitle( xtitle )
628  h.GetXaxis().SetTitleOffset( 1.3 )
629  h.GetYaxis().SetTitle( ytitle )
630  h.GetYaxis().SetTitleOffset( 1.4 )
631  h.SetTitle( histtitle )
632  h.SetMinimum(ymin)
633  h.SetMaximum(ymax)
634  h.SetFillColor( TColor.GetColor( "#56A5EC" ) )
635  for bin,y in enumerate(yvec):
636  val,valerr = y
637  h.SetBinContent( bin+1, val )
638  h.SetBinError ( bin+1, valerr )
639  h.Draw("e2")
640 
641  hc = h.Clone()
642  hc.SetLineColor( ROOT.kBlack )
643  hc.SetLineWidth(2)
644  hc.Draw("e0same")
645 
646  if printText:
647  t = TText( h.GetXaxis().GetXmax(), (ymax-ymin)*1.01 + ymin, printText )
648  t.SetTextAlign( 31 )
649  t.SetTextSize( 0.035 )
650  t.Draw()
651 
652  # logo
653  now = str(datetime.datetime.today())
654  tlogo = TText( h.GetXaxis().GetXmax()*1.025, (ymax-ymin)*0.0+ymin, 'ATLAS Run Query %s' % now[:now.find('.')] )
655  tlogo.SetTextSize( 0.030 )
656  tlogo.SetTextColor( TColor.GetColor( "#888888" ) )
657  tlogo.SetTextAngle(90)
658  tlogo.Draw()
659 
660  c.Update()
661  fnames = '%s/atlrunquery_%s.png' % (datapath, histname)
662  c.Print( fnames )
663 
664  return fnames
665 
666 
667 def makeLBPlot( xvec, xvecStb, yvecScalar,
668  xtitle, ytitle, ylegendScalar,
669  histname, histtitle,
670  datapath, printText = '', ymin = None, ymax = None ):
671  yvec = [yvecScalar]
672  ylegend = [ylegendScalar]
673  return makeLBPlotList( xvec, xvecStb, yvec, xtitle, ytitle, ylegend, histname, histtitle, datapath, printText, ymin, ymax )
674 
675 def InttypeTrf( tree, var, vlist, value, kcoord ):
676  # initialisation
677  if kcoord < 0:
678  vlist.append( array( 'i', [ 0 ] ) )
679  vreg = var.replace('-','_')
680  tree.Branch( vreg, vlist[-1], "%s/I" % vreg )
681  return
682  # fill tree
683  try:
684  vlist[kcoord][0] = int(value)
685  except (ValueError, TypeError):
686  vlist[kcoord][0] = -1
687 
688 def FloattypeTrf( tree, var, vlist, value, kcoord ):
689  # initialisation
690  if kcoord < 0:
691  vlist.append( array( 'f', [ 0 ] ) )
692  vreg = var.replace('-','_')
693  tree.Branch( vreg, vlist[-1], "%s/F" % vreg )
694  return
695  # fill tree
696  try:
697  vlist[kcoord][0] = float(value)
698  except (ValueError, TypeError):
699  vlist[kcoord][0] = -1
700 
701 def StringtypeTrf( tree, var, vlist, value, kcoord ):
702  # initialisation
703  if kcoord < 0:
704  vlist.append( array( 'b', 'ab\0'.encode() ) )
705  tree.Branch( var, vlist[-1], "%s/C" % var )
706  return
707  # fill tree
708  try:
709  vlist[kcoord] = array( 'b', (value + '\0').encode() )
710  tree.SetBranchAddress( var, vlist[kcoord] )
711  except (ValueError, TypeError):
712  vlist[kcoord] = array( 'b', 'unknown\0'.encode() )
713  tree.SetBranchAddress( var, vlist[kcoord] )
714 
715 def TimetypeTrf( tree, var, vlist, value, kcoord ):
716  return StringtypeTrf( tree, var, vlist, value, kcoord )
717 
718 def DurationtypeTrf( tree, var, vlist, value, kcoord ):
719  # original format: '[2d] 13h 15m 45s' --> transform in seconds
720  if kcoord >= 0:
721  try:
722  dur = value.split()
723  if len(dur) == 3:
724  h, m, s = dur
725  d = '0d'
726  elif len(dur) == 4:
727  d, h, m, s = dur
728  else:
729  print ('Unknown format in "DurationtypeTrf:"')
730  print (dur)
731  sys.exit(1)
732 
733  value = str( ( (int(d.replace('d','').strip())*24 + int(h.replace('h','').strip()))*60 +
734  int(m.replace('m','').strip()) )*60 + int(s.replace('s','').strip()) )
735  except (ValueError, TypeError):
736  print (value)
737  sys.exit(1)
738  value = 0
739  InttypeTrf( tree, var, vlist, value, kcoord )
740 
741 
742 def MakeHtml( hnames, fnames, uselarge = False, iconwidth = 70, nbreakline = 10 ):
743  windowstyle = 'openWindow'
744  if uselarge:
745  windowstyle = 'openLargeWindow'
746  iconwidth = 120
747  s = ''
748  s += '<table><tr>'
749  for i in range(0,len(fnames)):
750  f = fnames[i]
751  h = hnames[i]
752  s += """<td valign="top"><a STYLE="text-decoration: none" href=" """
753  s += """javascript:%s('Plot','<!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;><html xmlns:my><head><title>%s</title><LINK href=&quot;atlas-runquery.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;><body><table style=&quot;font-family: sans-serif; font-size: 85%%&quot;><tr><td valign=&quot;top&quot;>""" % (windowstyle,h)
754  s += """<img src=&quot;%s&quot;>""" % f
755  s += """<hr color=&quot;red&quot; size=1><font color=&quot;#777777&quot;><font size=&quot;-1&quot;><i><font size=&quot;-2&quot;>Created by AtlRunQuery on: %s</font></i></font></td></tr></table><script type=&quot;text/javascript&quot;></script></body></html>""" % str(datetime.datetime.now())
756  s += """')"""
757  s += """ "><img vspace=0 src="%s" title="Plot showing query result for %s" width="%i"></a><br><font size="-2">%s</font></td>""" % (f,h,iconwidth,h)
758  s += """<td>&nbsp;</td>"""
759  if (i+1)%nbreakline == 0:
760  s += '</tr><tr>'
761 
762  s += '</tr></table>'
763  return s
764 
766  # define all actions (choose small letters)
767  # format: 'column header' 'include' 'type' '...'
768  actions = { "run": ( True, InttypeTrf ),
769  "links": ( False, StringtypeTrf ),
770  "#lb": ( True, InttypeTrf ),
771  "#events": ( True, InttypeTrf ),
772  "smk": ( True, InttypeTrf ),
773  "start and endtime": ( True, TimetypeTrf ),
774  "duration": ( True, DurationtypeTrf ),
775  "torcurrent": ( True, FloattypeTrf ),
776  "solcurrent": ( True, FloattypeTrf ),
777  "detector systems": ( False, StringtypeTrf ),
778  "stream": ( True, InttypeTrf ),
779  "lumi": ( True, FloattypeTrf ),
780  "other": ( True, StringtypeTrf )
781  }
782  return actions
783 
784 def CreateRootFile( dic ):
785 
786  from CoolRunQuery.selector.AtlRunQuerySelectorBase import DataKey
787  from CoolRunQuery.AtlRunQueryQueryConfig import QC
788  datapath = QC.datapath
789 
790  actions = GetActions()
791 
792  if 'data' not in os.listdir('.'):
793  os.mkdir('data')
794 
795  f = open( '%s/dummyroot.txt' % datapath, 'w' )
796 
797  froot = TFile( '%s/atlrunquery.root' % datapath, 'RECREATE' )
798 
799  tree = TTree( 'RunQuery', 'ATLAS Run Query' )
800 
801  varlist = []
802 
803  # create branches
804  keylist = []
805  for data_key in dic:
806 
807  #print ("DATA_KEY",type(data_key),data_key)
808  f.write( 'key: %s \n' % (data_key.ResultKey))
809 
810  # the actual variable name used in the tree
811  var = data_key.ResultKey.replace('#',' ').strip().replace(' ','_').replace('STR:','').replace('-','_').replace(':','_')
812 
813  k = data_key.ResultKey.lower()
814  if data_key.Type==DataKey.STREAM:
815  k = 'stream'
816  elif 'olc_' == data_key.ResultKey[0:4]:
817  k = 'lumi'
818 
819  # is key included in 'actions' dictionary ?
820  if k not in actions:
821  k = 'other'
822 
823  useit, function = actions[k]
824  if useit:
825  function( tree, var, varlist, -1, -1 ) # create branch
826  keylist.append( (data_key,var, function) )
827 
828  # loop over runs
829  for ev in range(len(dic[DataKey('Run')])):
830  for k,(data_key,var,function) in enumerate(keylist):
831 
832  val = dic[data_key][ev]
833  if data_key.Type==DataKey.STREAM:
834  val = val[0]
835 
836  function( tree, var, varlist, val, k )
837 
838  tree.Fill()
839 
840  # make plots ?
841  hnames, fnames = MakePlots( tree, datapath )
842  htmlstr = MakeHtml( hnames, fnames )
843 
844  f.close()
845  tree.Write()
846  froot.Close()
847 
848  return froot.GetName(), htmlstr
849 
850 
851 
852 def makeRatePlot( v, lbduration, plottriggers, averrate, xtit, ytit, name, title, datapath, printText = '' ):
853 
854 
864 
865  ROOT = importroot()
866 
867  SetStyle()
868  c = TCanvas( name, title, 0, 0, 530, 400 )
869  c.GetPad(0).SetTopMargin(0.13)
870  c.GetPad(0).SetGrid()
871 
872  firstlb = min([lbinfo[0][0] for lbinfo in v.values()])
873  lastlb = max([lbinfo[-1][0] for lbinfo in v.values()])
874 
875  frame = TH1F( name, title, lastlb-firstlb+1, firstlb, lastlb+1 )
876  frame.GetXaxis().SetTitle( xtit )
877  frame.GetXaxis().SetTitleOffset( 1.3 )
878  frame.GetYaxis().SetTitle( ytit )
879  frame.GetYaxis().SetTitleOffset( 1.4 )
880 
881 
882  hTAP = {} # trigger after prescale (not realy, it contains (tap+tav)/2 with and error of (tap-tav)/2)
883  hTAV = {} # trigger after veto
884  ymax = -1
885 
886  # see http://root.cern.ch/root/html526/TColorWheel.html
887  colorList = [ (ROOT.kBlue,ROOT.kAzure+1),
888  (ROOT.kGreen+2,ROOT.kGreen+2),
889  (ROOT.kRed+1,ROOT.kRed),
890  (ROOT.kTeal-1,ROOT.kTeal),
891  (ROOT.kOrange+8,ROOT.kOrange),
892  (ROOT.kYellow+1,ROOT.kYellow),
893  (ROOT.kOrange+3,ROOT.kOrange+5),
894  (ROOT.kPink+8,ROOT.kPink+9) ]
895 
896  clrIdx = 0
897  for trname,avrate in plottriggers:
898  hTAP[trname] = TH1F( '%s_ap_%s' % (name,trname), title, lastlb-firstlb+1, firstlb, lastlb+1 )
899  hTAV[trname] = TH1F( '%s_av_%s' % (name,trname), title, lastlb-firstlb+1, firstlb, lastlb+1 )
900  hap = hTAP[trname]
901  hav = hTAV[trname]
902  hav.SetLineWidth( 2 )
903  if clrIdx < len(colorList):
904  dark,bright = colorList[clrIdx]
905  hav.SetLineColor( dark )
906  hap.SetFillColor( bright )
907  hap.SetMarkerColor( bright )
908  clrIdx += 1
909 
910  counts = v[trname]
911  for (lb,bp,ap,av),(lb2,lbstart,dt) in zip(counts,lbduration):
912  _apr = float(ap)/dt
913  _avr = float(av)/dt
914  hap.SetBinContent( lb-firstlb+1, (_apr+_avr)/2 )
915  hap.SetBinError( lb-firstlb+1, (_apr-_avr)/2 )
916  hav.SetBinContent( lb-firstlb+1, _avr )
917  ymax = max(ymax,_apr)
918 
919 
920  if ymax <= 0:
921  ymax = 1
922 
923  # first draw histogram
924  y2 = ymax*1.2
925  y1 = 0.01
926  frame.SetMinimum(y1)
927  frame.SetMaximum(y2)
928  frame.Draw("0")
929 
930  for h in hTAP.values():
931  h.Draw("same e2")
932  for h in hTAV.values():
933  h.Draw("same")
934 
935  if printText:
936  t = TText( frame.GetXaxis().GetXmax(), y2*1.01, printText )
937  t.SetTextAlign( 31 )
938  t.SetTextSize( 0.035 )
939  t.Draw()
940 
941  # logo
942  now = str(datetime.datetime.today())
943  tlogo = TText( frame.GetXaxis().GetXmax()*1.025, y1, 'ATLAS Run QUERY %s' % now[:now.find('.')] )
944  tlogo.SetTextSize( 0.030 )
945  tlogo.SetTextColor( TColor.GetColor( "#888888" ) )
946  tlogo.SetTextAngle(90)
947  tlogo.Draw()
948 
949  dx = 0.4
950  dy = 0.10 + 0.01*(len(hTAV)-1)
951  legend = TLegend( 1 - c.GetRightMargin() - dx, 1 - c.GetTopMargin() - dy - 0.015,
952  1 - c.GetRightMargin(), 1 - c.GetTopMargin() - 0.015)
953  legend.SetNColumns(2)
954  legend.SetBorderSize( 0 )
955  legend.SetMargin( 0.15 )
956  legend.SetLineColor( 0 )
957  legend.SetTextColor( 1 )
958  # fill legend - highest average rate first
959  for trname,avtrrate in averrate:
960  if trname not in hTAV:
961  continue
962  legend.AddEntry( hTAV[trname], trname, "l" )
963  legend.Draw("same")
964 
965  # redraw axis
966  frame.Draw( "sameaxis" )
967 
968  c.Update()
969  fnames = '%s/atlrunquery_%s.png' % (datapath, name)
970  c.Print( fnames )
971  frame.SetMaximum(ymax*5)
972  c.SetLogy(1)
973  c.Update()
974  fnames2 = '%s/atlrunquery_%s_log.png' % (datapath, name)
975  c.Print( fnames2 )
976 
977  return fnames
978 
979 
980 
981 
982 
983 # command line driver for convenience
984 if __name__=='__main__':
985 
986  import json
987  datapath = "."
988  pf = open( '%s/plotinput.json' % datapath, 'r' )
989  (lbrange, xvecStb, yvec, runNr, datapath) = json.load(pf)
990  lbrange = range(len(lbrange))
991  datapath = "test"
992  makeLBPlotSummaryForLHC( lbrange, xvecStb, yvec, runNr, datapath, printText = '' )
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
python.output.AtlRunQueryRoot.StringtypeTrf
def StringtypeTrf(tree, var, vlist, value, kcoord)
Definition: AtlRunQueryRoot.py:701
python.output.AtlRunQueryRoot.MakeHtml
def MakeHtml(hnames, fnames, uselarge=False, iconwidth=70, nbreakline=10)
Definition: AtlRunQueryRoot.py:742
max
#define max(a, b)
Definition: cfImp.cxx:41
python.output.AtlRunQueryRoot.MakePlots
def MakePlots(tree, datapath)
Definition: AtlRunQueryRoot.py:111
python.utils.AtlRunQueryUtils.importroot
def importroot(batch=True)
Definition: AtlRunQueryUtils.py:58
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.output.AtlRunQueryRoot.makeLBPlotList
def makeLBPlotList(xvec, xvecStb, yvec, xtitle, ytitle, ylegend, histname, histtitle, datapath, printText='', ymin=None, ymax=None)
Definition: AtlRunQueryRoot.py:498
python.output.AtlRunQueryRoot.SaveGraphsToFile
def SaveGraphsToFile(filename, varnames, xvecs, yvecs, addparamName=None, addparamVal=None)
Definition: AtlRunQueryRoot.py:88
python.output.AtlRunQueryRoot.SetStyle
def SetStyle(whiteCanvas=False)
Definition: AtlRunQueryRoot.py:28
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
python.output.AtlRunQueryRoot.makeBSPlots
def makeBSPlots(xvec, yvec, xtitle, ytitle, histname, histtitle, datapath, ymin, ymax, printText='')
Definition: AtlRunQueryRoot.py:608
min
#define min(a, b)
Definition: cfImp.cxx:40
array
python.grid.Add
def Add(name)
Definition: grid.py:41
python.output.AtlRunQueryRoot.makeTimePlotList
def makeTimePlotList(xvec, xveclb, yvec, toffset, dt, ymin, ymax, xtit, ytit, ylegend, name, title, datapath, printText='')
Definition: AtlRunQueryRoot.py:367
python.output.AtlRunQueryRoot.FloattypeTrf
def FloattypeTrf(tree, var, vlist, value, kcoord)
Definition: AtlRunQueryRoot.py:688
Trk::open
@ open
Definition: BinningType.h:40
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
python.output.AtlRunQueryRoot.TimetypeTrf
def TimetypeTrf(tree, var, vlist, value, kcoord)
Definition: AtlRunQueryRoot.py:715
python.output.AtlRunQueryRoot.InttypeTrf
def InttypeTrf(tree, var, vlist, value, kcoord)
Definition: AtlRunQueryRoot.py:675
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
dqm_persistency::Print
void Print(const PParameter *param, TDirectory *topdir, Option_t *opt="")
Definition: dqm_persistency_impl.cxx:161
python.output.AtlRunQueryRoot.GetActions
def GetActions()
Definition: AtlRunQueryRoot.py:765
str
Definition: BTagTrackIpAccessor.cxx:11
python.output.AtlRunQueryRoot.makeRatePlot
def makeRatePlot(v, lbduration, plottriggers, averrate, xtit, ytit, name, title, datapath, printText='')
Definition: AtlRunQueryRoot.py:852
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
python.PerfMonSerializer.encode
def encode(data, use_base64=True)
Definition: PerfMonSerializer.py:375
python.output.AtlRunQueryRoot.makeLBPlot
def makeLBPlot(xvec, xvecStb, yvecScalar, xtitle, ytitle, ylegendScalar, histname, histtitle, datapath, printText='', ymin=None, ymax=None)
Definition: AtlRunQueryRoot.py:667
python.output.AtlRunQueryRoot.makeLBPlotSummaryForLHC
def makeLBPlotSummaryForLHC(lbrange, xvecStb, yvec, runNr, datapath, printText='')
Definition: AtlRunQueryRoot.py:209
python.output.AtlRunQueryRoot.DurationtypeTrf
def DurationtypeTrf(tree, var, vlist, value, kcoord)
Definition: AtlRunQueryRoot.py:718
python.output.AtlRunQueryRoot.CreateRootFile
def CreateRootFile(dic)
Definition: AtlRunQueryRoot.py:784
readCCLHist.float
float
Definition: readCCLHist.py:83