File indexing completed on 2023-03-17 10:40:25
0001 import os
0002 import sys
0003 import ROOT
0004 import copy
0005 import math
0006 import time
0007 import ctypes
0008 from array import array
0009 from Alignment.OfflineValidation.DivergingColor import DivergingColor
0010
0011 '''
0012 Class for the TkAlMap plots
0013 to produce the plots use Alignment/OfflineValidation/python/runGCPTkAlMap.py
0014 '''
0015
0016
0017
0018
0019 KNOWN_VARIABLES = {
0020 'dr': {
0021 'name' : '#Deltar',
0022 'units': '#mum',
0023 'scale': 10000.,
0024 'range': [-200., 200.],
0025 },
0026 'dx': {
0027 'name' : '#Deltax',
0028 'units': '#mum',
0029 'scale': 10000.,
0030 'range': [-200., 200.],
0031 },
0032 'dy': {
0033 'name' : '#Deltay',
0034 'units': '#mum',
0035 'scale': 10000.,
0036 'range': [-200., 200.],
0037 },
0038 'dz': {
0039 'name' : '#Deltaz',
0040 'units': '#mum',
0041 'scale': 10000.,
0042 'range': [-200., 200.],
0043 },
0044 'rdphi': {
0045 'name' : 'r #Delta#phi',
0046 'units': '#mum rad',
0047 'scale': 10000.,
0048 'range': [-200., 200.],
0049 },
0050 'dphi': {
0051 'name' : '#Delta#phi',
0052 'units': 'mrad',
0053 'scale': 1000.,
0054 'range': [-100., 100.],
0055 },
0056 'dalpha': {
0057 'name' : '#Delta#alpha',
0058 'units': 'mrad',
0059 'scale': 1000.,
0060 'range': [-100., 100.],
0061 },
0062 'dbeta': {
0063 'name' : '#Delta#beta',
0064 'units': 'mrad',
0065 'scale': 1000.,
0066 'range': [-100., 100.],
0067 },
0068 'dgamma': {
0069 'name' : '#Delta#gamma',
0070 'units': 'mrad',
0071 'scale': 1000.,
0072 'range': [-100., 100.],
0073 },
0074 'du': {
0075 'name' : '#Deltau',
0076 'units': '#mum',
0077 'scale': 10000.,
0078 'range': [-200., 200.],
0079 },
0080 'dv': {
0081 'name' : '#Deltav',
0082 'units': '#mum',
0083 'scale': 10000.,
0084 'range': [-200., 200.],
0085 },
0086 'dw': {
0087 'name' : '#Deltaw',
0088 'units': '#mum',
0089 'scale': 10000.,
0090 'range': [-200., 200.],
0091 },
0092 'da': {
0093 'name' : '#Deltaa',
0094 'units': 'mrad',
0095 'scale': 1000.,
0096 'range': [-100., 100.],
0097 },
0098 'db': {
0099 'name' : '#Deltab',
0100 'units': 'mrad',
0101 'scale': 1000.,
0102 'range': [-100., 100.],
0103 },
0104 'dg': {
0105 'name' : '#Deltag',
0106 'units': 'mrad',
0107 'scale': 1000.,
0108 'range': [-100., 100.],
0109 },
0110 }
0111
0112
0113
0114 def mean(data_list):
0115 return sum(data_list)/(len(data_list)+0.)
0116
0117 def StdDev(data_list):
0118 s2 = 0.
0119 m = mean(data_list)
0120 for point in data_list:
0121 s2 += (point-m)**2
0122 return math.sqrt(s2/(len(data_list)+0.))
0123
0124 def read_TPLfile(file_name):
0125 o_file = open(file_name, 'r')
0126 lines = o_file.readlines()
0127 o_file.close()
0128
0129 TPL_dict = {}
0130 for line in lines:
0131 if '#' in line: continue
0132 splt_line = line.replace('\n', '').split(' ')
0133 det_id = int(splt_line[0])
0134 x = []
0135 y = []
0136 for idx,coo in enumerate(splt_line[1:]):
0137
0138 try:
0139 val = float(coo)
0140 if (idx%2) == 0:
0141 y.append(val)
0142 else:
0143 x.append(val)
0144 except ValueError:
0145 continue
0146 TPL_dict[det_id] = {}
0147 TPL_dict[det_id]['x'] = x
0148 TPL_dict[det_id]['y'] = y
0149 return TPL_dict
0150
0151 class TkAlMap:
0152
0153
0154 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):
0155 ROOT.gStyle.SetLineScalePS(1)
0156
0157
0158 self.GEO_file = GEO_file
0159 self.tracker = tracker
0160 self.width = height
0161 self.height = height
0162 self.title = title
0163 self.default_range = use_default_range
0164 self.two_sigma_cap = two_sigma_cap
0165 self.root_file = root_file
0166 self.do_tanh = do_tanh
0167
0168
0169 self.max_val = None
0170 self.min_val = None
0171 self.color_bar_colors = {}
0172 self.tree = None
0173 self.is_cleaned = False
0174
0175
0176 self.data_path = 'Alignment/OfflineValidation/data/TkAlMap/'
0177 self.cfg_path = 'Alignment/OfflineValidation/python/TkAlMap_cfg/'
0178
0179
0180 self.start_color_idx = 1200
0181 self.n_color_color_bar = 1000
0182
0183
0184 self.set_palette(palette)
0185 self.set_var(variable)
0186 self.load_tree()
0187 if check_tracker: self.detect_tracker_version()
0188 self.load_geometry()
0189 self.set_colorbar_colors()
0190
0191 def set_var(self, var, var_range=[None, None]):
0192 print('TkAlMap: setting variable to '+var)
0193 self.var = var
0194 self.var_name = var
0195 self.var_units = 'cm'
0196 self.var_scale = 1.
0197 self.var_min = var_range[0]
0198 self.var_max = var_range[1]
0199 if var in KNOWN_VARIABLES:
0200 self.var_name = KNOWN_VARIABLES[var]['name']
0201 self.var_units = KNOWN_VARIABLES[var]['units']
0202 self.var_scale = KNOWN_VARIABLES[var]['scale']
0203 if self.var_min is None: self.var_min = KNOWN_VARIABLES[var]['range'][0]
0204 if self.var_max is None: self.var_max = KNOWN_VARIABLES[var]['range'][1]
0205 self.set_canvas()
0206
0207 def set_canvas(self):
0208 canv_name = 'canvas_'+self.tracker+'_'+self.var
0209 if self.two_sigma_cap: canv_name += '_cap'
0210 self.canvas = ROOT.TCanvas(canv_name, 'TkAlMap '+self.var+' canvas', self.width, self.height)
0211 print('Actual w: '+str(self.canvas.GetWw())+', Actual h: '+str(self.canvas.GetWh()))
0212
0213
0214 def setup_colors(self):
0215 self.load_var()
0216 self.prepare_map_colors()
0217 self.fill_colors()
0218
0219 def prepare_map_colors(self):
0220 print('TkAlMap: preparing map colors')
0221
0222 self.colors = []
0223
0224 col_idx = self.start_color_idx + self.n_color_color_bar + 10
0225 self.col_dic = {}
0226 self.rgb_map = {}
0227
0228
0229 for val in self.val_list:
0230 cap_val = val
0231 if cap_val > self.max_val: cap_val = self.max_val
0232 if cap_val < self.min_val: cap_val = self.min_val
0233 r, g, b = self.get_color_rgb(cap_val)
0234 idx = self.get_color_rgb_idx(cap_val)
0235 if idx in self.colors: continue
0236 self.colors.append(idx)
0237 col_idx +=1
0238 self.rgb_map[idx] = col_idx
0239
0240
0241
0242
0243 try:
0244 col = ROOT.gROOT.GetColor(col_idx)
0245 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
0246 self.col_dic[idx] = col
0247 except:
0248 self.col_dic[idx] = ROOT.TColor(col_idx, (r+0.)/255., (g+0.)/255., (b+0.)/255.)
0249
0250 print('TkAlMap: map contains '+str(len(self.colors))+' colors')
0251
0252 def set_palette(self, palette):
0253 self.palette = palette
0254 pal_str = 'TkAlMap: setting the palette to '+str(palette)
0255 if palette == 1: pal_str += ' (rainbow)'
0256 elif palette == 2: pal_str += ' (B->R diverging)'
0257 else: raise ValueError('TkAlMap: unkown palette value '+str(palette)+', allowed values are 1 and 2')
0258 print(pal_str)
0259
0260
0261 pass
0262
0263 def get_color_rgb(self, val):
0264 if self.max_val is None or self.min_val is None:
0265 value_frac = val
0266 else:
0267 if self.do_tanh:
0268 val_th = math.tanh((val - self.mean_val)/(self.std_val))
0269 max_th = math.tanh((self.max_val - self.mean_val)/(self.std_val))
0270 min_th = math.tanh((self.min_val - self.mean_val)/(self.std_val))
0271 value_frac = (val_th - min_th + 0.)/(max_th - min_th)
0272 else:
0273 value_range = self.max_val - self.min_val
0274 if value_range == 0.: value_frac = 0.5
0275 else: value_frac = (val - self.min_val + 0.)/(value_range + 0.)
0276
0277 if self.palette == 1:
0278 r = 255
0279 g = 255
0280 b = 255
0281
0282 if value_frac < 0.25:
0283 r = 0
0284 g = int(255.*((value_frac/0.25)))
0285 b = 255
0286 elif value_frac < 0.5:
0287 r = 0
0288 g = 255
0289 b = int(255.*(1. -(value_frac - 0.25)/0.25))
0290 elif value_frac < 0.75:
0291 r = int(255.*((value_frac - 0.5)/0.25))
0292 g = 255
0293 b = 0
0294 else:
0295 r = 255
0296 g = int(255.*(1. -(value_frac - 0.75)/0.25))
0297 b = 0
0298 return r, g, b
0299 elif self.palette == 2:
0300 red = [59, 76, 192]
0301 blue = [180, 4, 38]
0302 white = [255, 255, 255]
0303 r, g, b = DivergingColor(red, blue, white, value_frac)
0304 return r, g, b
0305 else: raise ValueError('TkAlMap: unkown palette value '+str(palette)+', allowed values are 1 and 2')
0306
0307 def get_color_rgb_idx(self, val):
0308 r, g, b = self.get_color_rgb(val)
0309
0310 offset = 100
0311 return int(r*255*255 + g*255 + r + g + b + offset)
0312
0313 def fill_colors(self):
0314 print('TkAlMap: filling the colors')
0315
0316 for module in self.mod_val_dict:
0317 if module in self.TkAlMap_TPL_dict:
0318 val = self.mod_val_dict[module]
0319 cap_val = val
0320 if cap_val > self.max_val: cap_val = self.max_val
0321 if cap_val < self.min_val: cap_val = self.min_val
0322 rgb = self.get_color_rgb_idx(cap_val)
0323 col = self.rgb_map[rgb]
0324
0325
0326
0327 self.TkAlMap_TPL_dict[module].SetFillColor(col)
0328
0329
0330
0331 def set_colorbar_axis(self):
0332 print('TkAlMap: setting color bar axis')
0333 b_x1 = self.image_x1
0334 b_x2 = self.image_x2
0335 b_y1 = 0.06
0336 b_y2 = 0.06
0337 b_width = 0.01
0338 self.color_bar_axis = ROOT.TGaxis(b_x1, b_y1, b_x2, b_y2, self.min_val, self.max_val, 50510, '+S')
0339 self.color_bar_axis.SetName('color_bar_axis')
0340 self.color_bar_axis.SetLabelSize(0.02)
0341 self.color_bar_axis.SetTickSize(0.01)
0342 if self.two_sigma_cap and not self.default_range: self.color_bar_axis.SetTitle('{#mu - 2#sigma #leq '+self.var_name+' #leq #mu + 2#sigma} ['+self.var_units+']')
0343 elif self.default_range: self.color_bar_axis.SetTitle('{'+str(self.min_val)+' #leq '+self.var_name+' #leq '+str(self.max_val)+'} ['+self.var_units+']')
0344 else: self.color_bar_axis.SetTitle(self.var_name+' ['+self.var_units+']')
0345 self.color_bar_axis.SetTitleSize(0.025)
0346
0347 def set_colorbar_colors(self):
0348 print('TkAlMap: initialize color bar colors')
0349 if self.max_val is None or self.min_val is None:
0350 col_step = 1./(self.n_color_color_bar + 0.)
0351 val = col_step/2.
0352 else:
0353 b_range = self.max_val - self.min_val
0354 col_step = (b_range + 0.)/(self.n_color_color_bar + 0.)
0355 val = self.min_val + col_step/2.
0356
0357 b_x1 = self.image_x1
0358 b_x2 = self.image_x2
0359 b_y1 = 0.06
0360 b_y2 = 0.06
0361 b_width = 0.01
0362 b_xrange = b_x2 - b_x1
0363 b_yrange = b_y2 - b_y1
0364 b_dx = (b_xrange + 0.)/(self.n_color_color_bar + 0.)
0365 b_dy = (b_yrange + 0.)/(self.n_color_color_bar + 0.)
0366
0367 self.color_bar = {}
0368 x1 = b_x1
0369 y1 = b_y1
0370
0371 col_idx = self.start_color_idx
0372 for i_c in range(self.n_color_color_bar):
0373 col_idx += 1
0374 r, g, b = self.get_color_rgb(val)
0375 try:
0376 col = ROOT.gROOT.GetColor(col_idx)
0377 col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
0378 self.color_bar_colors[col_idx] = col
0379 except:
0380 self.color_bar_colors[col_idx] = ROOT.TColor(col_idx, (r+0.)/255., (g+0.)/255., (b+0.)/255.)
0381 x2 = x1 + b_dx
0382 y2 = y1 + b_dy + b_width
0383 x = array('d', [x1, x1, x2, x2])
0384 y = array('d', [y1, y2, y2, y1])
0385 self.color_bar[col_idx] = ROOT.TPolyLine(len(x), x, y)
0386 self.color_bar[col_idx].SetFillColor(col_idx)
0387 self.color_bar[col_idx].SetLineColor(col_idx)
0388
0389 x1 += b_dx
0390 y1 += b_dy
0391 val += col_step
0392
0393
0394 def load_tree(self):
0395 print('TkAlMap: loading tree ')
0396 tree_name = 'alignTree'
0397 r_file = ROOT.TFile(self.root_file)
0398 if r_file is None: raise ValueError('The file "'+self.root_file+'" could not be opened')
0399
0400 tree_tmp = r_file.Get(tree_name)
0401
0402 self.tmp_file_name = str(time.time()).replace('.', '_')+'_TkAlMapTempFile.root'
0403 self.tmp_file = ROOT.TFile(self.tmp_file_name, 'recreate')
0404 self.tree = tree_tmp.CloneTree()
0405 r_file.Close()
0406 self.is_cleaned = False
0407
0408 if self.tree is None: raise ValueError('The tree "'+tree_name+'" was not found in file "'+self.root_file+'"')
0409
0410
0411
0412 def load_var(self):
0413 print('TkAlMap: loading variable values ')
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 self.mod_val_dict = {}
0425 self.val_list = []
0426 for event in self.tree:
0427 module = event.id
0428 var = self.var
0429 if var == 'rdphi':
0430 val = getattr(event, 'r')*getattr(event, 'dphi')
0431 else:
0432 val = getattr(event, var)
0433 val *= self.var_scale
0434 self.mod_val_dict[module] = val
0435
0436 if module in self.TkAlMap_TPL_dict: self.val_list.append(val)
0437
0438
0439
0440
0441 if len(self.val_list) == 0:
0442 print('Warning: no values filled, 0 moduleId\'s matched')
0443 self.val_list = [-10+idx*0.5 for idx in range(41)]
0444 self.val_list.sort()
0445 self.mean_val = mean(self.val_list)
0446 self.std_val = StdDev(self.val_list)
0447 self.min_val = min(self.val_list)
0448 self.max_val = max(self.val_list)
0449
0450 if self.two_sigma_cap and not self.default_range:
0451 print('-- Capping max and min: ')
0452 print('---- True values : '+str(self.max_val)+', '+str(self.min_val))
0453 self.min_val = max(min(self.val_list), self.mean_val - 2*self.std_val)
0454 self.max_val = min(max(self.val_list), self.mean_val + 2*self.std_val)
0455 print('---- Capped values : '+str(self.max_val)+', '+str(self.min_val))
0456
0457 if self.default_range:
0458
0459 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')
0460 else:
0461 print('-- Capping max and min to default ranges: ')
0462 print('---- True values : '+str(self.max_val)+', '+str(self.min_val))
0463 self.min_val = self.var_min
0464 self.max_val = self.var_max
0465 print('---- Capped values : '+str(self.max_val)+', '+str(self.min_val))
0466
0467 if self.min_val == self.max_val:
0468 print('Warning: minimum value was equal to maximum value, '+str(self.max_val))
0469 self.min_val = self.mean_val - 1.
0470 self.max_val = self.mean_val + 1.
0471
0472
0473
0474 def detect_tracker_version(self):
0475 print('TkAlMap: detecting Tk version')
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486 phase = None
0487 for event in self.tree:
0488 module = event.id
0489 if module > 303040000 and module < 306450000:
0490 phase = 1
0491 break
0492 elif module > 302055000 and module < 302198000:
0493 phase = 0
0494 break
0495
0496
0497 if phase is None: raise ValueError('TkAlMap: unknown tracker detected, is this phase2?')
0498
0499 pahse_str = 'phase'+str(phase)
0500 print('TkAlMap: '+pahse_str+' tracker detected')
0501 if not pahse_str in self.GEO_file:
0502 print('TkAlMap: changing tracker to '+pahse_str+ ', if this is unwanted set "check_tracker" to False')
0503 self.GEO_file = 'TkAlMapDesign_'+pahse_str+'_cfg.py'
0504
0505
0506 def load_geometry(self):
0507 source_path = os.getenv('CMSSW_BASE') + '/src/'
0508 var = {}
0509 if sys.version_info[0] == 2:
0510 execfile(source_path + self.cfg_path + self.GEO_file, var)
0511 elif sys.version_info[0] == 3:
0512 _filename = source_path + self.cfg_path + self.GEO_file
0513 exec(compile(open(_filename, "rb").read(), _filename, 'exec'), var)
0514
0515 MapStructure = var['TkMap_GEO']
0516
0517 all_modules = {}
0518 all_text = {}
0519 x_max = -9999.
0520 y_max = -9999.
0521 x_min = 9999.
0522 y_min = 9999.
0523 for det in MapStructure:
0524 if 'pixel' in self.tracker:
0525 if not 'pixel' in det: continue
0526 elif 'strips' in self.tracker:
0527 if not 'strips' in det: continue
0528 for sub in MapStructure[det]:
0529 for part in MapStructure[det][sub]:
0530 if part == 'latex':
0531 all_text[det+'_'+sub] = MapStructure[det][sub][part]
0532 continue
0533 if 'latex' in MapStructure[det][sub][part]:
0534 all_text[det+'_'+sub+'_'+part] = MapStructure[det][sub][part]['latex']
0535 _filename = self.data_path +MapStructure[det][sub][part]['file']
0536 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]
0537 TPL_dict = read_TPLfile(TPL_file)
0538 for module in TPL_dict:
0539 x_canv = []
0540 y_canv = []
0541 for idx in range(len(TPL_dict[module]['x'])):
0542 x_canv.append(TPL_dict[module]['x'][idx]*MapStructure[det][sub][part]['x_scale'] + MapStructure[det][sub][part]['x_off'])
0543 y_canv.append(TPL_dict[module]['y'][idx]*MapStructure[det][sub][part]['y_scale'] + MapStructure[det][sub][part]['y_off'])
0544 if max(x_canv) > x_max: x_max = max(x_canv)
0545 if max(y_canv) > y_max: y_max = max(y_canv)
0546 if min(x_canv) < x_min: x_min = min(x_canv)
0547 if min(y_canv) < y_min: y_min = min(y_canv)
0548 TPL_dict[module]['x'] = x_canv
0549 TPL_dict[module]['y'] = y_canv
0550 all_modules.update(TPL_dict)
0551
0552 r_margin = 3
0553 l_margin = 3
0554
0555 t_margin = 11
0556 b_margin = 8
0557
0558 x_max += r_margin
0559 x_min -= l_margin
0560 y_max += t_margin
0561 y_min -= b_margin
0562
0563 x_range = x_max - x_min
0564 y_range = y_max - y_min
0565
0566 self.width = int(self.height*(x_range + 0.)/(y_range + 0.))
0567 self.canvas.SetWindowSize(self.width, self.height)
0568
0569 if (x_range + 0.)/(self.width + 0.) > (y_range + 0.)/(self.height + 0.):
0570 x_scale = x_range
0571 y_scale = (self.height + 0.)/(self.width + 0.)*x_range
0572 else:
0573 y_scale = y_range
0574 x_scale = (self.width + 0.)/(self.height + 0.)*y_range
0575 self.TkAlMap_TPL_dict = {}
0576 for module in all_modules:
0577 x = array('d', [])
0578 y = array('d', [])
0579 for idx in range(len(all_modules[module]['x'])):
0580 x.append((all_modules[module]['x'][idx] - x_min + 0.)/(x_scale + 0.))
0581 y.append((all_modules[module]['y'][idx] - y_min + 0.)/(y_scale + 0.))
0582
0583 x.append((all_modules[module]['x'][0] - x_min + 0.)/(x_scale + 0.))
0584 y.append((all_modules[module]['y'][0] - y_min + 0.)/(y_scale + 0.))
0585
0586 self.TkAlMap_TPL_dict[module] = ROOT.TPolyLine(len(x), x, y)
0587
0588 self.TkAlMap_TPL_dict[module].SetLineColor(1)
0589
0590
0591
0592
0593 self.image_x1 = (l_margin + 0.)/(x_scale + 0.)
0594 self.image_x2 = (x_max - r_margin - x_min + 0.)/(x_scale + 0.)
0595 self.image_y1 = (b_margin + 0.)/(y_scale + 0.)
0596 self.image_y2 = (y_max - t_margin - y_min + 0.)/(y_scale + 0.)
0597
0598 self.x_scale = x_scale
0599 self.y_scale = y_scale
0600
0601
0602
0603 self.TkAlMap_text_dict = {}
0604 for key in all_text:
0605 x = (all_text[key]['x'] - x_min + 0.)/(x_scale + 0.)
0606 y = (all_text[key]['y'] - y_min + 0.)/(y_scale + 0.)
0607 self.TkAlMap_text_dict[key] = {}
0608 self.TkAlMap_text_dict[key]['x'] = x
0609 self.TkAlMap_text_dict[key]['y'] = y
0610 self.TkAlMap_text_dict[key]['alignment'] = all_text[key]['alignment']
0611 self.TkAlMap_text_dict[key]['text'] = all_text[key]['text']
0612
0613
0614
0615
0616 def draw_title(self):
0617 TL = ROOT.TLatex()
0618 TL.SetTextSize(0.035)
0619 TL.SetTextFont(42)
0620 TL.SetTextAlign(13)
0621 x1 = self.image_x1
0622 y1 = 1-(5./(self.y_scale+0.))
0623 self.canvas.cd()
0624 TL.DrawLatex(x1, y1, self.title)
0625
0626 def draw_cms_prelim(self):
0627 TL = ROOT.TLatex()
0628 factor = 1. / 0.82
0629 TL.SetTextSize(0.035*factor)
0630 TL.SetTextAlign(11)
0631 TL.SetTextFont(61)
0632
0633 w_cms = ctypes.c_uint(0)
0634 h_cms = ctypes.c_uint(0)
0635 TL.GetTextExtent(w_cms, h_cms, 'CMS')
0636 x1 = self.image_x1
0637 y1 = 1. - (h_cms.value+0.)/(self.height+0.) - (1./(self.y_scale+0.))
0638 self.canvas.cd()
0639 TL.DrawLatex(x1, y1, 'CMS')
0640
0641 TL.SetTextSize(0.035)
0642 TL.SetTextFont(42)
0643 x1_prel = x1 + 1.1*(w_cms.value+0.)/(self.width+0.)
0644 TL.DrawLatex(x1_prel, y1, '#it{Preliminary}')
0645
0646 self.draw_event_info(y1)
0647
0648 def draw_event_info(self, y):
0649 TL = ROOT.TLatex()
0650 TL.SetTextSize(0.035)
0651 TL.SetTextFont(42)
0652 TL.SetTextAlign(31)
0653
0654 x1 = self.image_x2
0655 y1 = y
0656 self.canvas.cd()
0657 TL.DrawLatex(x1, y1, 'pp collisions 13TeV')
0658
0659
0660
0661 def draw_text(self):
0662 print('TkAlMap: drawing text')
0663 self.canvas.cd()
0664 TL = ROOT.TLatex()
0665 TL.SetTextSize(0.025)
0666 for key in self.TkAlMap_text_dict:
0667 TL.SetTextAlign(self.TkAlMap_text_dict[key]['alignment'])
0668 TL.DrawLatex(self.TkAlMap_text_dict[key]['x'], self.TkAlMap_text_dict[key]['y'], self.TkAlMap_text_dict[key]['text'])
0669 self.draw_cms_prelim()
0670 self.draw_title()
0671 self.canvas.Update()
0672
0673 def draw_TPL(self):
0674 print('TkAlMap: drawing PolyLines')
0675 self.canvas.cd()
0676 for module in self.TkAlMap_TPL_dict:
0677 self.TkAlMap_TPL_dict[module].Draw('f')
0678 self.TkAlMap_TPL_dict[module].Draw()
0679 self.canvas.Update()
0680
0681 def draw_color_bar(self):
0682 print('TkAlMap: drawing color bar')
0683 self.canvas.cd()
0684 for box in self.color_bar:
0685 self.color_bar[box].Draw('f')
0686
0687 self.color_bar_axis.Draw()
0688 self.canvas.Update()
0689
0690 def save(self, out_dir='.', extension='pdf'):
0691 name = '_'.join(['TkAlMap', self.tracker, self.var])
0692 if self.two_sigma_cap and not self.default_range:
0693 name += '_4sig'
0694 elif self.default_range:
0695 name += '_drange'
0696 path = out_dir + '/' + name + '.' + extension
0697 print('TkAlMap: saving canvas in "'+path+'"')
0698 self.canvas.SaveAs(path)
0699
0700
0701
0702 def analyse(self):
0703 self.setup_colors()
0704 self.set_colorbar_axis()
0705 if self.do_tanh: self.set_colorbar_colors()
0706 self.draw_TPL()
0707 self.draw_text()
0708 self.draw_color_bar()
0709
0710
0711 def plot_variable_distribution(self, nbins=200, out_dir='.'):
0712 print('TkAlMap: drawing variable distribution')
0713 canv_name = 'histogram_canvas_'+self.tracker+'_'+self.var
0714 if self.two_sigma_cap: canv_name += '_cap'
0715 canvas = ROOT.TCanvas(canv_name, 'TkAlMap '+self.var+' histogram canvas', 800, 800)
0716
0717 h_min = min(min(self.val_list), self.mean_val - 2*self.std_val) - self.std_val
0718 h_max = max(max(self.val_list), self.mean_val + 2*self.std_val) + self.std_val
0719 hist = ROOT.TH1F(self.var+'_hist', 'Variable distribution', nbins, h_min, h_max)
0720 for val in self.val_list:
0721 hist.Fill(val)
0722 hist.GetXaxis().SetTitle(self.var_name+' ['+self.var_units+']')
0723 hist.GetYaxis().SetTitle('modules')
0724 ROOT.gStyle.SetOptStat(0)
0725 hist.Draw('e1')
0726 canvas.Update()
0727 left = ROOT.TLine(self.mean_val - 2*self.std_val, canvas.GetUymin(), self.mean_val - 2*self.std_val, canvas.GetUymax())
0728 left.SetLineColor(2)
0729 left.SetLineStyle(9)
0730 left.Draw()
0731 right = ROOT.TLine(self.mean_val + 2*self.std_val, canvas.GetUymin(), self.mean_val + 2*self.std_val, canvas.GetUymax())
0732 right.SetLineColor(2)
0733 right.SetLineStyle(9)
0734 right.Draw()
0735 mid = ROOT.TLine(self.mean_val, canvas.GetUymin(), self.mean_val, canvas.GetUymax())
0736 mid.SetLineColor(1)
0737 mid.SetLineStyle(9)
0738 mid.Draw()
0739 canvas.Update()
0740 name = '_'.join(['VariableDistribution', self.var, self.tracker])
0741 path = out_dir + '/' + name + '.png'
0742 canvas.SaveAs(path)
0743
0744
0745
0746 def clean_up(self):
0747 if not self.is_cleaned:
0748 print('TkAlMap: deleting temporary file "'+self.tmp_file_name+'"')
0749 self.tmp_file.Close()
0750 os.remove(self.tmp_file_name)
0751 self.is_cleaned = True
0752
0753 def __del__(self):
0754 self.clean_up()
0755
0756
0757 if __name__ == '__main__':
0758
0759
0760 TkMap = TkAlMap('test', 'Here goes the title', 'run/tree.root', True, height=1400, GEO_file='TkAlMapDesign_phase0_cfg.py')
0761
0762
0763
0764
0765
0766
0767
0768 var_list = ['dx', 'dy']
0769
0770 for var in var_list:
0771 TkMap_temp = copy.deepcopy(TkMap)
0772 TkMap_temp.set_var(var)
0773
0774
0775
0776 TkMap_temp.analyse()
0777 TkMap_temp.save(extension='png')
0778 TkMap_temp.save()
0779 raw_input('exit')