8 from array
import array
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)
176 self.
data_path =
'Alignment/OfflineValidation/data/TkAlMap/' 177 self.
cfg_path =
'Alignment/OfflineValidation/python/TkAlMap_cfg/' 191 def set_var(self, var, var_range=[None, None]):
192 print(
'TkAlMap: setting variable to '+var)
199 if var
in KNOWN_VARIABLES:
200 self.
var_name = KNOWN_VARIABLES[var][
'name']
201 self.
var_units = KNOWN_VARIABLES[var][
'units']
202 self.
var_scale = KNOWN_VARIABLES[var][
'scale']
203 if self.
var_min is None: self.
var_min = KNOWN_VARIABLES[var][
'range'][0]
204 if self.
var_max is None: self.
var_max = KNOWN_VARIABLES[var][
'range'][1]
208 canv_name =
'canvas_'+self.
tracker+
'_'+self.
var 220 print(
'TkAlMap: preparing map colors')
235 if idx
in self.
colors:
continue 244 col = ROOT.gROOT.GetColor(col_idx)
245 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
248 self.
col_dic[idx] = ROOT.TColor(col_idx, (r+0.)/255., (g+0.)/255., (b+0.)/255.)
254 pal_str =
'TkAlMap: setting the palette to '+
str(palette)
255 if palette == 1: pal_str +=
' (rainbow)' 256 elif palette == 2: pal_str +=
' (B->R diverging)' 257 else:
raise ValueError(
'TkAlMap: unkown palette value '+
str(palette)+
', allowed values are 1 and 2')
271 value_frac = (val_th - min_th + 0.)/(max_th - min_th)
274 if value_range == 0.: value_frac = 0.5
275 else: value_frac = (val - self.
min_val + 0.)/(value_range + 0.)
282 if value_frac < 0.25:
284 g =
int(255.*((value_frac/0.25)))
286 elif value_frac < 0.5:
289 b =
int(255.*(1. -(value_frac - 0.25)/0.25))
290 elif value_frac < 0.75:
291 r =
int(255.*((value_frac - 0.5)/0.25))
296 g =
int(255.*(1. -(value_frac - 0.75)/0.25))
302 white = [255, 255, 255]
305 else:
raise ValueError(
'TkAlMap: unkown palette value '+
str(palette)+
', allowed values are 1 and 2')
311 return int(r*255*255 + g*255 + r + g + b + offset)
314 print(
'TkAlMap: filling the colors')
332 print(
'TkAlMap: setting color bar axis')
348 print(
'TkAlMap: initialize color bar colors')
355 val = self.
min_val + col_step/2.
362 b_xrange = b_x2 - b_x1
363 b_yrange = b_y2 - b_y1
376 col = ROOT.gROOT.GetColor(col_idx)
377 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
380 self.
color_bar_colors[col_idx] = ROOT.TColor(col_idx, (r+0.)/255., (g+0.)/255., (b+0.)/255.)
382 y2 = y1 + b_dy + b_width
383 x =
array(
'd', [x1, x1, x2, x2])
384 y =
array(
'd', [y1, y2, y2, y1])
385 self.
color_bar[col_idx] = ROOT.TPolyLine(len(x), x, y)
386 self.
color_bar[col_idx].SetFillColor(col_idx)
387 self.
color_bar[col_idx].SetLineColor(col_idx)
395 print(
'TkAlMap: loading tree ')
396 tree_name =
'alignTree' 398 if r_file
is None:
raise ValueError(
'The file "'+self.
root_file+
'" could not be opened')
400 tree_tmp = r_file.Get(tree_name)
404 self.
tree = tree_tmp.CloneTree()
408 if self.
tree is None:
raise ValueError(
'The tree "'+tree_name+
'" was not found in file "'+self.
root_file+
'"')
413 print(
'TkAlMap: loading variable values ')
426 for event
in self.
tree:
430 val = getattr(event,
'r')*getattr(event, 'dphi')
432 val = getattr(event, var)
442 print(
'Warning: no values filled, 0 moduleId\'s matched')
451 print(
'-- Capping max and min: ')
459 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')
461 print(
'-- Capping max and min to default ranges: ')
468 print(
'Warning: minimum value was equal to maximum value, '+
str(self.
max_val))
475 print(
'TkAlMap: detecting Tk version')
487 for event
in self.
tree:
489 if module > 303040000
and module < 306450000:
492 elif module > 302055000
and module < 302198000:
497 if phase
is None:
raise ValueError(
'TkAlMap: unknown tracker detected, is this phase2?')
499 pahse_str =
'phase'+
str(phase)
500 print(
'TkAlMap: '+pahse_str+
' tracker detected')
502 print(
'TkAlMap: changing tracker to '+pahse_str+
', if this is unwanted set "check_tracker" to False')
503 self.
GEO_file =
'TkAlMapDesign_'+pahse_str+
'_cfg.py' 507 source_path = os.getenv(
'CMSSW_BASE') +
'/src/' 509 if sys.version_info[0] == 2:
511 elif sys.version_info[0] == 3:
513 exec(compile(open(_filename,
"rb").
read(), _filename,
'exec'), var)
515 MapStructure = var[
'TkMap_GEO']
523 for det
in MapStructure:
525 if not 'pixel' in det:
continue 527 if not 'strips' in det:
continue 528 for sub
in MapStructure[det]:
529 for part
in MapStructure[det][sub]:
531 all_text[det+
'_'+sub] = MapStructure[det][sub][part]
533 if 'latex' in MapStructure[det][sub][part]:
534 all_text[det+
'_'+sub+
'_'+part] = MapStructure[det][sub][part][
'latex']
535 _filename = self.
data_path +MapStructure[det][sub][part][
'file']
536 TPL_file = [os.path.join(dir,_filename)
for dir
in os.getenv(
'CMSSW_SEARCH_PATH',
'').
split(
":")
if os.path.exists(os.path.join(dir,_filename))][0]
538 for module
in TPL_dict:
541 for idx
in range(len(TPL_dict[module][
'x'])):
542 x_canv.append(TPL_dict[module][
'x'][idx]*MapStructure[det][sub][part][
'x_scale'] + MapStructure[det][sub][part][
'x_off'])
543 y_canv.append(TPL_dict[module][
'y'][idx]*MapStructure[det][sub][part][
'y_scale'] + MapStructure[det][sub][part][
'y_off'])
544 if max(x_canv) > x_max: x_max =
max(x_canv)
545 if max(y_canv) > y_max: y_max =
max(y_canv)
546 if min(x_canv) < x_min: x_min =
min(x_canv)
547 if min(y_canv) < y_min: y_min =
min(y_canv)
548 TPL_dict[module][
'x'] = x_canv
549 TPL_dict[module][
'y'] = y_canv
550 all_modules.update(TPL_dict)
563 x_range = x_max - x_min
564 y_range = y_max - y_min
569 if (x_range + 0.)/(self.
width + 0.) > (y_range + 0.)/(self.
height + 0.):
571 y_scale = (self.
height + 0.)/(self.
width + 0.)*x_range
574 x_scale = (self.
width + 0.)/(self.
height + 0.)*y_range
576 for module
in all_modules:
579 for idx
in range(len(all_modules[module][
'x'])):
580 x.append((all_modules[module][
'x'][idx] - x_min + 0.)/(x_scale + 0.))
581 y.append((all_modules[module][
'y'][idx] - y_min + 0.)/(y_scale + 0.))
583 x.append((all_modules[module][
'x'][0] - x_min + 0.)/(x_scale + 0.))
584 y.append((all_modules[module][
'y'][0] - y_min + 0.)/(y_scale + 0.))
594 self.
image_x2 = (x_max - r_margin - x_min + 0.)/(x_scale + 0.)
596 self.
image_y2 = (y_max - t_margin - y_min + 0.)/(y_scale + 0.)
605 x = (all_text[key][
'x'] - x_min + 0.)/(x_scale + 0.)
606 y = (all_text[key][
'y'] - y_min + 0.)/(y_scale + 0.)
618 TL.SetTextSize(0.035)
624 TL.DrawLatex(x1, y1, self.
title)
629 TL.SetTextSize(0.035*factor)
633 w_cms = ctypes.c_uint(0)
634 h_cms = ctypes.c_uint(0)
635 TL.GetTextExtent(w_cms, h_cms,
'CMS')
637 y1 = 1. - (h_cms.value+0.)/(self.
height+0.) - (1./(self.
y_scale+0.))
639 TL.DrawLatex(x1, y1,
'CMS')
641 TL.SetTextSize(0.035)
643 x1_prel = x1 + 1.1*(w_cms.value+0.)/(self.
width+0.)
644 TL.DrawLatex(x1_prel, y1,
'#it{Preliminary}')
650 TL.SetTextSize(0.035)
657 TL.DrawLatex(x1, y1,
'pp collisions 13TeV')
662 print(
'TkAlMap: drawing text')
665 TL.SetTextSize(0.025)
674 print(
'TkAlMap: drawing PolyLines')
682 print(
'TkAlMap: drawing color bar')
690 def save(self, out_dir='.', extension='pdf'):
696 path = out_dir +
'/' + name +
'.' + extension
697 print(
'TkAlMap: saving canvas in "'+path+
'"')
712 print(
'TkAlMap: drawing variable distribution')
713 canv_name =
'histogram_canvas_'+self.
tracker+
'_'+self.
var 715 canvas = ROOT.TCanvas(canv_name,
'TkAlMap '+self.
var+
' histogram canvas', 800, 800)
719 hist = ROOT.TH1F(self.
var+
'_hist',
'Variable distribution', nbins, h_min, h_max)
723 hist.GetYaxis().SetTitle(
'modules')
724 ROOT.gStyle.SetOptStat(0)
732 right.SetLineColor(2)
733 right.SetLineStyle(9)
735 mid = ROOT.TLine(self.
mean_val, canvas.GetUymin(), self.
mean_val, canvas.GetUymax())
741 path = out_dir +
'/' + name +
'.png' 757 if __name__ ==
'__main__':
760 TkMap =
TkAlMap(
'test',
'Here goes the title',
'run/tree.root',
True, height=1400, GEO_file=
'TkAlMapDesign_phase0_cfg.py')
768 var_list = [
'dx',
'dy']
771 TkMap_temp = copy.deepcopy(TkMap)
772 TkMap_temp.set_var(var)
777 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)
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.
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def setup_colors(self)
Color setup.
def prepare_map_colors(self)
def set_colorbar_axis(self)
def draw_title(self)
Titles and info.
def split(sequence, size)
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()