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': [-100., 100.],
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 241 col = ROOT.gROOT.GetColor(col_idx)
242 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
247 col = ROOT.TColor(col_idx, 0., 0., 0.,
"", 1)
248 col.SetRGB((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.)
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')
331 print(
'TkAlMap: setting color bar axis')
347 print(
'TkAlMap: initialize color bar colors')
354 val = self.
min_val + col_step/2.
361 b_xrange = b_x2 - b_x1
362 b_yrange = b_y2 - b_y1
375 col = ROOT.gROOT.GetColor(col_idx)
376 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
381 col = ROOT.TColor(col_idx, 0., 0., 0.,
"", 1)
382 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
385 y2 = y1 + b_dy + b_width
386 x =
array(
'd', [x1, x1, x2, x2])
387 y =
array(
'd', [y1, y2, y2, y1])
388 self.
color_bar[col_idx] = ROOT.TPolyLine(len(x), x, y)
389 self.
color_bar[col_idx].SetFillColor(col_idx)
390 self.
color_bar[col_idx].SetLineColor(col_idx)
398 print(
'TkAlMap: loading tree ')
399 tree_name =
'alignTree' 401 if r_file
is None:
raise ValueError(
'The file "'+self.
root_file+
'" could not be opened')
403 tree_tmp = r_file.Get(tree_name)
407 self.
tree = tree_tmp.CloneTree()
411 if self.
tree is None:
raise ValueError(
'The tree "'+tree_name+
'" was not found in file "'+self.
root_file+
'"')
416 print(
'TkAlMap: loading variable values ')
429 for event
in self.
tree:
433 val = getattr(event,
'r')*getattr(event, 'dphi')
435 val = getattr(event, var)
445 print(
'Warning: no values filled, 0 moduleId\'s matched')
454 print(
'-- Capping max and min: ')
462 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')
464 print(
'-- Capping max and min to default ranges: ')
471 print(
'Warning: minimum value was equal to maximum value, '+
str(self.
max_val))
478 print(
'TkAlMap: detecting Tk version')
490 for event
in self.
tree:
492 if module > 303040000
and module < 306450000:
495 elif module > 302055000
and module < 302198000:
500 if phase
is None:
raise ValueError(
'TkAlMap: unknown tracker detected, is this phase2?')
502 pahse_str =
'phase'+
str(phase)
503 print(
'TkAlMap: '+pahse_str+
' tracker detected')
505 print(
'TkAlMap: changing tracker to '+pahse_str+
', if this is unwanted set "check_tracker" to False')
506 self.
GEO_file =
'TkAlMapDesign_'+pahse_str+
'_cfg.py' 510 source_path = os.getenv(
'CMSSW_BASE') +
'/src/' 512 if sys.version_info[0] == 2:
514 elif sys.version_info[0] == 3:
516 exec(compile(open(_filename,
"rb").
read(), _filename,
'exec'), var)
518 MapStructure = var[
'TkMap_GEO']
526 for det
in MapStructure:
528 if not 'pixel' in det:
continue 530 if not 'strips' in det:
continue 531 for sub
in MapStructure[det]:
532 for part
in MapStructure[det][sub]:
534 all_text[det+
'_'+sub] = MapStructure[det][sub][part]
536 if 'latex' in MapStructure[det][sub][part]:
537 all_text[det+
'_'+sub+
'_'+part] = MapStructure[det][sub][part][
'latex']
538 _filename = self.
data_path +MapStructure[det][sub][part][
'file']
539 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]
541 for module
in TPL_dict:
544 for idx
in range(len(TPL_dict[module][
'x'])):
545 x_canv.append(TPL_dict[module][
'x'][idx]*MapStructure[det][sub][part][
'x_scale'] + MapStructure[det][sub][part][
'x_off'])
546 y_canv.append(TPL_dict[module][
'y'][idx]*MapStructure[det][sub][part][
'y_scale'] + MapStructure[det][sub][part][
'y_off'])
547 if max(x_canv) > x_max: x_max =
max(x_canv)
548 if max(y_canv) > y_max: y_max =
max(y_canv)
549 if min(x_canv) < x_min: x_min =
min(x_canv)
550 if min(y_canv) < y_min: y_min =
min(y_canv)
551 TPL_dict[module][
'x'] = x_canv
552 TPL_dict[module][
'y'] = y_canv
553 all_modules.update(TPL_dict)
566 x_range = x_max - x_min
567 y_range = y_max - y_min
572 if (x_range + 0.)/(self.
width + 0.) > (y_range + 0.)/(self.
height + 0.):
574 y_scale = (self.
height + 0.)/(self.
width + 0.)*x_range
577 x_scale = (self.
width + 0.)/(self.
height + 0.)*y_range
579 for module
in all_modules:
582 for idx
in range(len(all_modules[module][
'x'])):
583 x.append((all_modules[module][
'x'][idx] - x_min + 0.)/(x_scale + 0.))
584 y.append((all_modules[module][
'y'][idx] - y_min + 0.)/(y_scale + 0.))
586 x.append((all_modules[module][
'x'][0] - x_min + 0.)/(x_scale + 0.))
587 y.append((all_modules[module][
'y'][0] - y_min + 0.)/(y_scale + 0.))
597 self.
image_x2 = (x_max - r_margin - x_min + 0.)/(x_scale + 0.)
599 self.
image_y2 = (y_max - t_margin - y_min + 0.)/(y_scale + 0.)
608 x = (all_text[key][
'x'] - x_min + 0.)/(x_scale + 0.)
609 y = (all_text[key][
'y'] - y_min + 0.)/(y_scale + 0.)
621 TL.SetTextSize(0.035)
627 TL.DrawLatex(x1, y1, self.
title)
632 TL.SetTextSize(0.035*factor)
636 w_cms = ctypes.c_uint(0)
637 h_cms = ctypes.c_uint(0)
638 TL.GetTextExtent(w_cms, h_cms,
'CMS')
640 y1 = 1. - (h_cms.value+0.)/(self.
height+0.) - (1./(self.
y_scale+0.))
642 TL.DrawLatex(x1, y1,
'CMS')
644 TL.SetTextSize(0.035)
646 x1_prel = x1 + 1.1*(w_cms.value+0.)/(self.
width+0.)
647 TL.DrawLatex(x1_prel, y1,
'#it{Preliminary}')
653 TL.SetTextSize(0.035)
660 TL.DrawLatex(x1, y1,
'pp collisions 13TeV')
665 print(
'TkAlMap: drawing text')
668 TL.SetTextSize(0.025)
677 print(
'TkAlMap: drawing PolyLines')
685 print(
'TkAlMap: drawing color bar')
693 def save(self, out_dir='.', extension='pdf'):
699 path = out_dir +
'/' + name +
'.' + extension
700 print(
'TkAlMap: saving canvas in "'+path+
'"')
715 print(
'TkAlMap: drawing variable distribution')
716 canv_name =
'histogram_canvas_'+self.
tracker+
'_'+self.
var 718 canvas = ROOT.TCanvas(canv_name,
'TkAlMap '+self.
var+
' histogram canvas', 800, 800)
722 hist = ROOT.TH1F(self.
var+
'_hist',
'Variable distribution', nbins, h_min, h_max)
726 hist.GetYaxis().SetTitle(
'modules')
727 ROOT.gStyle.SetOptStat(0)
735 right.SetLineColor(2)
736 right.SetLineStyle(9)
738 mid = ROOT.TLine(self.
mean_val, canvas.GetUymin(), self.
mean_val, canvas.GetUymax())
744 path = out_dir +
'/' + name +
'.png' 760 if __name__ ==
'__main__':
763 TkMap =
TkAlMap(
'test',
'Here goes the title',
'run/tree.root',
True, height=1400, GEO_file=
'TkAlMapDesign_phase0_cfg.py')
771 var_list = [
'dx',
'dy']
774 TkMap_temp = copy.deepcopy(TkMap)
775 TkMap_temp.set_var(var)
780 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()