7 from array
import array
8 import FWCore.ParameterSet.Config
as cms
9 from Alignment.OfflineValidation.DivergingColor
import DivergingColor
12 Class for the TkAlMap plots 13 to produce the plots use Alignment/OfflineValidation/python/runGCPTkAlMap.py 24 'range': [-200., 200.],
30 'range': [-200., 200.],
36 'range': [-200., 200.],
42 'range': [-200., 200.],
45 'name' :
'r #Delta#phi',
48 'range': [-200., 200.],
51 'name' :
'#Delta#phi',
54 'range': [-100., 100.],
57 'name' :
'#Delta#alpha',
60 'range': [-100., 100.],
63 'name' :
'#Delta#beta',
66 'range': [-100., 100.],
69 'name' :
'#Delta#gamma',
72 'range': [-100., 100.],
78 'range': [-200., 200.],
84 'range': [-200., 200.],
90 'range': [-200., 200.],
96 'range': [-100., 100.],
102 'range': [-100., 100.],
108 'range': [-100., 100.],
115 return sum(data_list)/(len(data_list)+0.)
120 for point
in data_list:
122 return math.sqrt(s2/(len(data_list)+0.))
125 o_file = open(file_name,
'r') 126 lines = o_file.readlines() 131 if '#' in line:
continue 132 splt_line = line.replace(
'\n',
'').
split(
' ')
133 det_id =
int(splt_line[0])
136 for idx,coo
in enumerate(splt_line[1:]):
146 TPL_dict[det_id] = {}
147 TPL_dict[det_id][
'x'] = x
148 TPL_dict[det_id][
'y'] = y
154 def __init__(self, variable, title, root_file, use_default_range=False, two_sigma_cap=False, height=1400, GEO_file='TkAlMapDesign_phase1_cfg.py', tracker='full', palette=2, do_tanh=False, check_tracker=True):
155 ROOT.gStyle.SetLineScalePS(1)
177 self.
data_path =
'Alignment/OfflineValidation/data/TkAlMap/' 178 self.
cfg_path =
'Alignment/OfflineValidation/python/TkAlMap_cfg/' 192 def set_var(self, var, var_range=[None, None]):
193 print(
'TkAlMap: setting variable to '+var)
200 if var
in KNOWN_VARIABLES:
201 self.
var_name = KNOWN_VARIABLES[var][
'name']
202 self.
var_units = KNOWN_VARIABLES[var][
'units']
203 self.
var_scale = KNOWN_VARIABLES[var][
'scale']
204 if self.
var_min is None: self.
var_min = KNOWN_VARIABLES[var][
'range'][0]
205 if self.
var_max is None: self.
var_max = KNOWN_VARIABLES[var][
'range'][1]
209 canv_name =
'canvas_'+self.
tracker+
'_'+self.
var 212 print(
'Actual w: '+
str(self.canvas.GetWw())+
', Actual h: '+
str(self.canvas.GetWh()))
221 print(
'TkAlMap: preparing map colors')
236 if idx
in self.
colors:
continue 237 self.colors.append(idx)
245 col = ROOT.gROOT.GetColor(col_idx)
246 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
249 self.
col_dic[idx] = ROOT.TColor(col_idx, (r+0.)/255., (g+0.)/255., (b+0.)/255.)
255 pal_str =
'TkAlMap: setting the palette to '+
str(palette)
256 if palette == 1: pal_str +=
' (rainbow)' 257 elif palette == 2: pal_str +=
' (B->R diverging)' 258 else:
raise ValueError(
'TkAlMap: unkown palette value '+
str(palette)+
', allowed values are 1 and 2')
272 value_frac = (val_th - min_th + 0.)/(max_th - min_th)
275 if value_range == 0.: value_frac = 0.5
276 else: value_frac = (val - self.
min_val + 0.)/(value_range + 0.)
283 if value_frac < 0.25:
285 g =
int(255.*((value_frac/0.25)))
287 elif value_frac < 0.5:
290 b =
int(255.*(1. -(value_frac - 0.25)/0.25))
291 elif value_frac < 0.75:
292 r =
int(255.*((value_frac - 0.5)/0.25))
297 g =
int(255.*(1. -(value_frac - 0.75)/0.25))
303 white = [255, 255, 255]
306 else:
raise ValueError(
'TkAlMap: unkown palette value '+
str(palette)+
', allowed values are 1 and 2')
312 return int(r*255*255 + g*255 + r + g + b + offset)
315 print(
'TkAlMap: filling the colors')
333 print(
'TkAlMap: setting color bar axis')
340 self.color_bar_axis.SetName(
'color_bar_axis')
341 self.color_bar_axis.SetLabelSize(0.02)
342 self.color_bar_axis.SetTickSize(0.01)
346 self.color_bar_axis.SetTitleSize(0.025)
349 print(
'TkAlMap: initialize color bar colors')
356 val = self.
min_val + col_step/2.
363 b_xrange = b_x2 - b_x1
364 b_yrange = b_y2 - b_y1
377 col = ROOT.gROOT.GetColor(col_idx)
378 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
381 self.
color_bar_colors[col_idx] = ROOT.TColor(col_idx, (r+0.)/255., (g+0.)/255., (b+0.)/255.)
383 y2 = y1 + b_dy + b_width
384 x =
array(
'd', [x1, x1, x2, x2])
385 y =
array(
'd', [y1, y2, y2, y1])
386 self.
color_bar[col_idx] = ROOT.TPolyLine(len(x), x, y)
387 self.
color_bar[col_idx].SetFillColor(col_idx)
388 self.
color_bar[col_idx].SetLineColor(col_idx)
396 print(
'TkAlMap: loading tree ')
397 tree_name =
'alignTree' 399 if r_file
is None:
raise ValueError(
'The file "'+self.
root_file+
'" could not be opened')
401 tree_tmp = r_file.Get(tree_name)
405 self.
tree = tree_tmp.CloneTree()
409 if self.
tree is None:
raise ValueError(
'The tree "'+tree_name+
'" was not found in file "'+self.
root_file+
'"')
414 print(
'TkAlMap: loading variable values ')
427 for event
in self.
tree:
431 val = getattr(event,
'r')*getattr(event, 'dphi')
433 val = getattr(event, var)
443 print(
'Warning: no values filled, 0 moduleId\'s matched')
444 self.
val_list = [-10+idx*0.5
for idx
in range(41)]
452 print(
'-- Capping max and min: ')
460 if self.
var_min is None or self.
var_max is None:
print(
'Warning: capping to default range for unknown variable "'+self.
var+
'" while range was not set is not possible')
462 print(
'-- Capping max and min to default ranges: ')
469 print(
'Warning: minimum value was equal to maximum value, '+
str(self.
max_val))
476 print(
'TkAlMap: detecting Tk version')
488 for event
in self.
tree:
490 if module > 303040000
and module < 306450000:
493 elif module > 302055000
and module < 302198000:
498 if phase
is None:
raise ValueError(
'TkAlMap: unknown tracker detected, is this phase2?')
500 pahse_str =
'phase'+
str(phase)
501 print(
'TkAlMap: '+pahse_str+
' tracker detected')
503 print(
'TkAlMap: changing tracker to '+pahse_str+
', if this is unwanted set "check_tracker" to False')
504 self.
GEO_file =
'TkAlMapDesign_'+pahse_str+
'_cfg.py' 508 source_path = os.getenv(
'CMSSW_BASE') +
'/src/' 512 MapStructure = var[
'TkMap_GEO']
520 for det
in MapStructure:
522 if not 'pixel' in det:
continue 524 if not 'strips' in det:
continue 525 for sub
in MapStructure[det]:
526 for part
in MapStructure[det][sub]:
528 all_text[det+
'_'+sub] = MapStructure[det][sub][part]
530 if 'latex' in MapStructure[det][sub][part]:
531 all_text[det+
'_'+sub+
'_'+part] = MapStructure[det][sub][part][
'latex']
533 TPL_file = cms.FileInPath(self.
data_path +MapStructure[det][sub][part][
'file'])
535 for module
in TPL_dict:
538 for idx
in range(len(TPL_dict[module][
'x'])):
539 x_canv.append(TPL_dict[module][
'x'][idx]*MapStructure[det][sub][part][
'x_scale'] + MapStructure[det][sub][part][
'x_off'])
540 y_canv.append(TPL_dict[module][
'y'][idx]*MapStructure[det][sub][part][
'y_scale'] + MapStructure[det][sub][part][
'y_off'])
541 if max(x_canv) > x_max: x_max =
max(x_canv)
542 if max(y_canv) > y_max: y_max =
max(y_canv)
543 if min(x_canv) < x_min: x_min =
min(x_canv)
544 if min(y_canv) < y_min: y_min =
min(y_canv)
545 TPL_dict[module][
'x'] = x_canv
546 TPL_dict[module][
'y'] = y_canv
547 all_modules.update(TPL_dict)
560 x_range = x_max - x_min
561 y_range = y_max - y_min
566 if (x_range + 0.)/(self.
width + 0.) > (y_range + 0.)/(self.
height + 0.):
568 y_scale = (self.
height + 0.)/(self.
width + 0.)*x_range
571 x_scale = (self.
width + 0.)/(self.
height + 0.)*y_range
573 for module
in all_modules:
576 for idx
in range(len(all_modules[module][
'x'])):
577 x.append((all_modules[module][
'x'][idx] - x_min + 0.)/(x_scale + 0.))
578 y.append((all_modules[module][
'y'][idx] - y_min + 0.)/(y_scale + 0.))
580 x.append((all_modules[module][
'x'][0] - x_min + 0.)/(x_scale + 0.))
581 y.append((all_modules[module][
'y'][0] - y_min + 0.)/(y_scale + 0.))
591 self.
image_x2 = (x_max - r_margin - x_min + 0.)/(x_scale + 0.)
593 self.
image_y2 = (y_max - t_margin - y_min + 0.)/(y_scale + 0.)
602 x = (all_text[key][
'x'] - x_min + 0.)/(x_scale + 0.)
603 y = (all_text[key][
'y'] - y_min + 0.)/(y_scale + 0.)
615 TL.SetTextSize(0.035)
621 TL.DrawLatex(x1, y1, self.
title)
626 TL.SetTextSize(0.035*factor)
630 w_cms = ctypes.c_uint(0)
631 h_cms = ctypes.c_uint(0)
632 TL.GetTextExtent(w_cms, h_cms,
'CMS')
634 y1 = 1. - (h_cms.value+0.)/(self.
height+0.) - (1./(self.
y_scale+0.))
636 TL.DrawLatex(x1, y1,
'CMS')
638 TL.SetTextSize(0.035)
640 x1_prel = x1 + 1.1*(w_cms.value+0.)/(self.
width+0.)
641 TL.DrawLatex(x1_prel, y1,
'#it{Preliminary}')
647 TL.SetTextSize(0.035)
654 TL.DrawLatex(x1, y1,
'pp collisions 13TeV')
659 print(
'TkAlMap: drawing text')
662 TL.SetTextSize(0.025)
671 print(
'TkAlMap: drawing PolyLines')
679 print(
'TkAlMap: drawing color bar')
684 self.color_bar_axis.Draw()
687 def save(self, out_dir='.', extension='pdf'):
693 path = out_dir +
'/' + name +
'.' + extension
694 print(
'TkAlMap: saving canvas in "'+path+
'"')
695 self.canvas.SaveAs(path)
709 print(
'TkAlMap: drawing variable distribution')
710 canv_name =
'histogram_canvas_'+self.
tracker+
'_'+self.
var 712 canvas = ROOT.TCanvas(canv_name,
'TkAlMap '+self.
var+
' histogram canvas', 800, 800)
716 hist = ROOT.TH1F(self.
var+
'_hist',
'Variable distribution', nbins, h_min, h_max)
720 hist.GetYaxis().SetTitle(
'modules')
721 ROOT.gStyle.SetOptStat(0)
729 right.SetLineColor(2)
730 right.SetLineStyle(9)
732 mid = ROOT.TLine(self.
mean_val, canvas.GetUymin(), self.
mean_val, canvas.GetUymax())
738 path = out_dir +
'/' + name +
'.png' 746 self.tmp_file.Delete()
754 if __name__ ==
'__main__':
757 TkMap =
TkAlMap(
'test',
'Here goes the title',
'run/tree.root',
True, height=1400, GEO_file=
'TkAlMapDesign_phase0_cfg.py')
765 var_list = [
'dx',
'dy']
768 TkMap_temp = copy.deepcopy(TkMap)
769 TkMap_temp.set_var(var)
774 TkMap_temp.save(extension=
'png')
def draw_cms_prelim(self)
def draw_text(self)
Draw functions.
def replace(string, replacements)
def get_color_rgb_idx(self, val)
S & print(S &os, JobReport::InputFile const &f)
def detect_tracker_version(self)
def __init__(self, variable, title, root_file, use_default_range=False, two_sigma_cap=False, height=1400, GEO_file='TkAlMapDesign_phase1_cfg.py', tracker='full', palette=2, do_tanh=False, check_tracker=True)
def save(self, out_dir='.', extension='pdf')
def read_TPLfile(file_name)
def load_tree(self)
Load functions.
def setup_colors(self)
Color setup.
def prepare_map_colors(self)
def set_colorbar_axis(self)
def draw_title(self)
Titles and info.
static std::string join(char **cmd)
def set_colorbar_colors(self)
def plot_variable_distribution(self, nbins=200, out_dir='.')
Test functions.
def clean_up(self)
Clean up.
GEO_file
tree = r_file.Get(tree_name) tree_tmp = r_file.Get(tree_name) tree = copy.deepcopy(tree_tmp) r_file...
def set_var(self, var, var_range=[None, None)
def get_color_rgb(self, val)
def set_palette(self, palette)
def draw_event_info(self, y)
val_list
else: if 'full' in self.tracker: print('Warning: Unknown module '+str(module)) r_file.Close()