7 from __future__
import print_function
25 from matplotlib
import pyplot
as plt
29 from RecoLuminosity.LumiDB.mpl_axes_hist_fix
import hist
30 if matplotlib.__version__ !=
'1.0.1':
31 print(
"ERROR The %s script contains a hard-coded bug-fix " \
32 "for Matplotlib 1.0.1. The Matplotlib version loaded " \
33 "is %s" % (__file__, matplotlib.__version__), file=sys.stderr)
35 matplotlib.axes.Axes.hist = hist
38 from RecoLuminosity.LumiDB.public_plots_tools
import ColorScheme
39 from RecoLuminosity.LumiDB.public_plots_tools
import LatexifyUnits
40 from RecoLuminosity.LumiDB.public_plots_tools
import AddLogo
41 from RecoLuminosity.LumiDB.public_plots_tools
import InitMatplotlib
42 from RecoLuminosity.LumiDB.public_plots_tools
import SavePlot
43 from RecoLuminosity.LumiDB.public_plots_tools
import FONT_PROPS_SUPTITLE
44 from RecoLuminosity.LumiDB.public_plots_tools
import FONT_PROPS_TITLE
45 from RecoLuminosity.LumiDB.public_plots_tools
import FONT_PROPS_AX_TITLE
46 from RecoLuminosity.LumiDB.public_plots_tools
import FONT_PROPS_TICK_LABEL
58 DATE_FMT_STR_LUMICALC =
"%m/%d/%y %H:%M:%S" 59 DATE_FMT_STR_LUMICALC_DAY =
"%m/%d/%y" 60 DATE_FMT_STR_OUT =
"%Y-%m-%d %H:%M" 61 DATE_FMT_STR_AXES =
"%-d %b" 62 DATE_FMT_STR_CFG =
"%Y-%m-%d" 63 NUM_SEC_IN_LS = 2**18 / 11246.
65 KNOWN_ACCEL_MODES = [
"PROTPHYS",
"IONPHYS",
"PAPHYS",
66 "2013_amode_bug_workaround"]
67 LEAD_SCALE_FACTOR = 82. / 208.
75 results[y]=[maxputimelist,maxpulist]
78 lumifilename=
str(y)+
'lumibyls.csv' 79 f=open(lumifilename,
'rb')
81 print(
'failed to open file ',lumifilename)
83 freader=csv.reader(f,delimiter=
',')
89 if row[0].
find(
'#')==1:
91 [runnum,fillnum]=
map(
lambda i:
int(i),row[0].
split(
':'))
97 if fillnum
not in fills:
99 results[y][0].
append(max_putime)
100 results[y][1].
append(max_avgpu)
102 results[y][0][-1]=max_putime
103 results[y][1][-1]=max_avgpu
111 cache_file_path = os.path.abspath(cache_file_dir)
113 cache_file_name =
"lumicalc_cache_%s.csv" % day.isoformat()
114 cache_file_path = os.path.join(cache_file_path, cache_file_name)
115 return cache_file_path
119 """Pick a DateLocator based on the range of the x-axis.""" 120 (x_lo, x_hi) = ax.get_xlim()
121 num_days = x_hi - x_lo
122 min_num_ticks =
min(num_days, 5)
123 locator = matplotlib.dates.AutoDateLocator(minticks=min_num_ticks,
131 add_extra_head_room=
False):
134 (time_begin, time_end) = time_range
136 ax.autoscale_view(
False,
True,
True)
137 for label
in ax.get_xticklabels():
138 label.set_ha(
"right")
139 label.set_rotation(30.)
143 if add_extra_head_room:
144 y_ticks = ax.get_yticks()
145 (y_min, y_max) = ax.get_ylim()
146 is_log = (ax.get_yscale() ==
"log")
149 tmp = y_ticks[-1] / y_ticks[-2]
150 y_max_new = y_max * math.pow(tmp, add_extra_head_room)
152 tmp = y_ticks[-1] - y_ticks[-2]
153 y_max_new = y_max + add_extra_head_room * tmp
154 ax.set_ylim(y_min, y_max_new)
158 ax_sec.set_ylim(ax.get_ylim())
159 ax_sec.set_yscale(ax.get_yscale())
161 for ax_tmp
in fig.axes:
162 for sub_ax
in [ax_tmp.xaxis, ax_tmp.yaxis]:
163 for label
in sub_ax.get_ticklabels():
164 label.set_font_properties(FONT_PROPS_TICK_LABEL)
166 ax.set_xlim(time_begin, time_end)
169 minorXlocator=matplotlib.ticker.AutoMinorLocator()
172 ax.xaxis.set_major_locator(locator)
173 ax.xaxis.set_minor_locator(minorXlocator)
175 formatter = matplotlib.dates.DateFormatter(DATE_FMT_STR_AXES)
176 ax.xaxis.set_major_formatter(formatter)
178 fig.subplots_adjust(top=.85, bottom=.14, left=.13, right=.91)
183 if __name__ ==
"__main__":
185 desc_str =
"test pu" \
187 arg_parser = optparse.OptionParser(description=desc_str)
188 arg_parser.add_option(
"--ignore-cache", action=
"store_true",
189 help=
"Ignore all cached lumiCalc results " \
190 "and re-query lumiCalc. " \
191 "(Rebuilds the cache as well.)")
192 (options, args) = arg_parser.parse_args()
194 print(
"ERROR Need exactly one argument: a config file name", file=sys.stderr)
196 config_file_name = args[0]
197 ignore_cache = options.ignore_cache
200 "lumicalc_flags" :
"",
202 "color_schemes" :
"Joe, Greg",
203 "beam_energy" :
None,
204 "beam_fluctuation" :
None,
206 "oracle_connection" :
None 208 cfg_parser = ConfigParser.SafeConfigParser(cfg_defaults)
209 if not os.path.exists(config_file_name):
210 print(
"ERROR Config file '%s' does not exist" % config_file_name, file=sys.stderr)
212 cfg_parser.read(config_file_name)
215 color_scheme_names_tmp = cfg_parser.get(
"general",
"color_schemes")
216 color_scheme_names = [i.strip()
for i
in color_scheme_names_tmp.split(
",")]
218 cache_file_dir = cfg_parser.get(
"general",
"cache_dir")
220 verbose = cfg_parser.getboolean(
"general",
"verbose")
223 lumicalc_script = cfg_parser.get(
"general",
"lumicalc_script")
224 lumicalc_flags_from_cfg = cfg_parser.get(
"general",
"lumicalc_flags")
225 accel_mode = cfg_parser.get(
"general",
"accel_mode")
227 if not accel_mode
in KNOWN_ACCEL_MODES:
228 print(
"ERROR Unknown accelerator mode '%s'" % \
229 accel_mode, file=sys.stderr)
232 amodetag_bug_workaround =
False 233 if accel_mode ==
"2013_amode_bug_workaround":
234 amodetag_bug_workaround =
True 235 accel_mode =
"PAPHYS" 238 beam_energy_tmp = cfg_parser.get(
"general",
"beam_energy")
242 beam_energy_from_cfg =
None 243 if not beam_energy_tmp:
244 print(
"No beam energy specified --> using defaults for '%s'" % \
246 beam_energy_from_cfg =
False 248 beam_energy_from_cfg =
True 249 beam_energy =
float(beam_energy_tmp)
251 beam_fluctuation_tmp = cfg_parser.get(
"general",
"beam_fluctuation")
254 beam_fluctuation =
None 255 beam_fluctuation_from_cfg =
None 256 if not beam_fluctuation_tmp:
257 print(
"No beam energy fluctuation specified --> using the defaults to '%s'" % \
259 beam_fluctuation_from_cfg =
False 261 beam_fluctuation_from_cfg =
True 262 beam_fluctuation =
float(beam_fluctuation_tmp)
265 tmp = cfg_parser.get(
"general",
"date_begin")
266 date_begin = datetime.datetime.strptime(tmp, DATE_FMT_STR_CFG).
date()
267 tmp = cfg_parser.get(
"general",
"date_end")
270 date_end = datetime.datetime.strptime(tmp, DATE_FMT_STR_CFG).
date()
272 today = datetime.datetime.utcnow().
date()
274 print(
"No end date given --> using today")
278 print(
"End date lies in the future --> using today instead")
281 if date_end < date_begin:
282 print(
"ERROR End date before begin date (%s < %s)" % \
283 (date_end.isoformat(), date_begin.isoformat()), file=sys.stderr)
289 oracle_connection_string = cfg_parser.get(
"general",
"oracle_connection")
290 use_oracle = (len(oracle_connection_string) != 0)
296 particle_type_strings = {
301 particle_type_str = particle_type_strings[accel_mode]
303 beam_energy_defaults = {
304 "PROTPHYS" : {2010 : 3500.,
308 "IONPHYS" : {2010 : 3500.,
310 "PAPHYS" : {2013 : 4000.}
312 beam_fluctuation_defaults = {
313 "PROTPHYS" : {2010 : .15,
317 "IONPHYS" : {2010 : .15,
319 "PAPHYS" : {2013 : .15}
326 os.putenv(
"TNS_ADMIN",
"/afs/cern.ch/cms/lumi/DB")
331 print(
"Using configuration from file '%s'" % config_file_name)
333 print(
"Ignoring all cached lumiCalc results (and rebuilding the cache)")
335 print(
"Using cached lumiCalc results from %s" % \
337 print(
"Using color schemes '%s'" %
", ".
join(color_scheme_names))
338 print(
"Using lumiCalc script '%s'" % lumicalc_script)
339 print(
"Using additional lumiCalc flags from configuration: '%s'" % \
340 lumicalc_flags_from_cfg)
341 print(
"Selecting data for accelerator mode '%s'" % accel_mode)
342 if beam_energy_from_cfg:
343 print(
"Selecting data for beam energy %.0f GeV" % beam_energy)
345 print(
"Selecting data for default beam energy for '%s' from:" % accel_mode)
346 for (key, val)
in six.iteritems(beam_energy_defaults[accel_mode]):
347 print(
" %d : %.1f GeV" % (key, val))
348 if beam_fluctuation_from_cfg:
349 print(
"Using beam energy fluctuation of +/- %.0f%%" % \
350 (100. * beam_fluctuation))
352 print(
"Using default beam energy fluctuation for '%s' from:" % accel_mode)
353 for (key, val)
in six.iteritems(beam_fluctuation_defaults[accel_mode]):
354 print(
" %d : +/- %.0f%%" % (key, 100. * val))
356 print(
"Using direct access to the Oracle luminosity database")
358 print(
"Using access to the luminosity database through the Frontier cache")
364 if not os.path.exists(path_name):
366 print(
"Cache file path does not exist: creating it")
368 os.makedirs(path_name)
369 except Exception
as err:
370 print(
"ERROR Could not create cache dir: %s" % path_name, file=sys.stderr)
376 years=[2010,2011,2012]
378 lumi_data_by_fill_per_year={}
390 year_begin = date_begin.isocalendar()[0]
391 year_end = date_end.isocalendar()[0]
393 assert year_end >= year_begin
397 print(
"Drawing things...")
398 ColorScheme.InitColors()
403 print(
" peak interactions for %s together" %
", ".
join([
str(i)
for i
in years]))
406 """Mode 1: years side-by-side""" 409 for color_scheme_name
in color_scheme_names:
410 print(
" color scheme '%s'" % color_scheme_name)
411 color_scheme = ColorScheme(color_scheme_name)
412 color_by_year = color_scheme.color_by_year
413 logo_name = color_scheme.logo_name
414 file_suffix = color_scheme.file_suffix
416 for type
in [
"lin",
"log"]:
417 is_log = (type ==
"log")
418 aspect_ratio = matplotlib.figure.figaspect(1. / 2.5)
419 fig = plt.figure(figsize=aspect_ratio)
420 ax = fig.add_subplot(111)
422 time_begin_ultimate = datetime.datetime.strptime(lumi_data_by_fill_per_year[years[0]][0][0],DATE_FMT_STR_LUMICALC).
date()
423 str_begin_ultimate = time_begin_ultimate.strftime(DATE_FMT_STR_OUT)
424 for (year_index, year)
in enumerate(years):
426 lumi_data = lumi_data_by_fill_per_year[year]
427 times_tmp = [datetime.datetime.strptime(tmp,DATE_FMT_STR_LUMICALC).
date()
for tmp
in lumi_data[0]]
428 times = [matplotlib.dates.date2num(i)
for i
in times_tmp]
429 maxpus = lumi_data[1]
433 if year == 2010
or year == 2011 :
434 label =
r"%d, %s" % \
435 (year,
r'7TeV $\sigma$=71.5mb')
437 label =
r"%d, %s" % \
438 (year,
r'8TeV $\sigma$=73mb')
439 ax.plot(times,maxpus,
440 color=color_by_year[year],
442 marker=
"o", linestyle=
'none',
448 time_begin = datetime.datetime(years[0], 1, 1, 0, 0, 0)
449 time_end = datetime.datetime(years[-1], 12, 16, 20, 50,9)
450 str_begin = time_begin.strftime(DATE_FMT_STR_OUT)
451 str_end = time_end.strftime(DATE_FMT_STR_OUT)
454 num_cols = len(years)
458 leg = ax.legend(loc=
"upper left", bbox_to_anchor=(tmp_x, 0., 1., tmp_y),
459 frameon=
False, ncol=num_cols)
460 for t
in leg.get_texts():
461 t.set_font_properties(FONT_PROPS_TICK_LABEL)
464 fig.suptitle(
r"CMS peak interactions per crossing, %s" % particle_type_str,
465 fontproperties=FONT_PROPS_SUPTITLE)
466 ax.set_title(
"Data included from %s to %s UTC \n" % \
467 (str_begin_ultimate, str_end),
468 fontproperties=FONT_PROPS_TITLE)
469 ax.set_xlabel(
r"Date (UTC)", fontproperties=FONT_PROPS_AX_TITLE)
470 ax.set_ylabel(
r"Peak interactions per crossing",\
471 fontproperties=FONT_PROPS_AX_TITLE)
476 AddLogo(logo_name, ax, zoom=zoom)
483 TweakPlot(fig, ax, (time_begin, time_end),
485 add_extra_head_room=
True)
491 (particle_type_str.lower(),
492 log_suffix, file_suffix))
def TweakPlot(fig, ax, time_range, add_extra_head_room=False)
S & print(S &os, JobReport::InputFile const &f)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
static std::string join(char **cmd)
def PlotPeakPUAllYears(lumi_data_by_fill_per_year)
def CacheFilePath(cache_file_dir, day=None)