CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes
TkAlMap.TkAlMap Class Reference

Public Member Functions

def __del__ (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 analyse (self)
 Do all. More...
 
def clean_up (self)
 Clean up. More...
 
def detect_tracker_version (self)
 
def draw_cms_prelim (self)
 
def draw_color_bar (self)
 
def draw_event_info (self, y)
 
def draw_text (self)
 Draw functions. More...
 
def draw_title (self)
 Titles and info. More...
 
def draw_TPL (self)
 
def fill_colors (self)
 
def get_color_rgb (self, val)
 
def get_color_rgb_idx (self, val)
 
def load_geometry (self)
 
def load_tree (self)
 Load functions. More...
 
def load_var (self)
 
def plot_variable_distribution (self, nbins=200, out_dir='.')
 Test functions. More...
 
def prepare_map_colors (self)
 
def save (self, out_dir='.', extension='pdf')
 
def set_canvas (self)
 
def set_colorbar_axis (self)
 
def set_colorbar_colors (self)
 
def set_palette (self, palette)
 
def set_var (self, var, var_range=[None, None)
 
def setup_colors (self)
 Color setup. More...
 

Public Attributes

 canvas
 
 cfg_path
 
 col_dic
 
 color_bar
 
 color_bar_axis
 
 color_bar_colors
 
 colors
 
 data_path
 
 default_range
 
 do_tanh
 
 GEO_file
 tree = r_file.Get(tree_name) tree_tmp = r_file.Get(tree_name) tree = copy.deepcopy(tree_tmp) r_file.Close() More...
 
 height
 
 image_x1
 
 image_x2
 
 image_y1
 
 image_y2
 
 is_cleaned
 
 max_val
 
 mean_val
 
 min_val
 
 mod_val_dict
 
 n_color_color_bar
 
 palette
 
 rgb_map
 
 root_file
 
 start_color_idx
 
 std_val
 
 title
 
 TkAlMap_text_dict
 
 TkAlMap_TPL_dict
 
 tmp_file
 
 tmp_file_name
 
 tracker
 
 tree
 
 two_sigma_cap
 
 val_list
 else: if 'full' in self.tracker: print('Warning: Unknown module '+str(module)) r_file.Close() More...
 
 var
 
 var_max
 
 var_min
 
 var_name
 
 var_scale
 
 var_units
 
 width
 
 x_scale
 
 y_scale
 

Detailed Description

Definition at line 151 of file TkAlMap.py.

Constructor & Destructor Documentation

◆ __init__()

def TkAlMap.TkAlMap.__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 
)

Definition at line 154 of file TkAlMap.py.

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)
156 
157  # Configuration parameters
158  self.GEO_file = GEO_file
159  self.tracker = tracker
160  self.width = height
161  self.height = height
162  self.title = title
163  self.default_range = use_default_range
164  self.two_sigma_cap = two_sigma_cap
165  self.root_file = root_file
166  self.do_tanh = do_tanh
167 
168  # Value Initialization
169  self.max_val = None
170  self.min_val = None
171  self.color_bar_colors = {}
172  self.tree = None
173  self.is_cleaned = False
174 
175  # Internal parameters
176  self.data_path = 'Alignment/OfflineValidation/data/TkAlMap/'
177  self.cfg_path = 'Alignment/OfflineValidation/python/TkAlMap_cfg/'
178 
179  # Colorbar stuff
180  self.start_color_idx = 1200
181  self.n_color_color_bar = 1000
182 
183  # Initialization functions
184  self.set_palette(palette)
185  self.set_var(variable)
186  self.load_tree()
187  if check_tracker: self.detect_tracker_version()
188  self.load_geometry()
189  self.set_colorbar_colors()
190 
def __init__(self, dataset, job_number, job_id, job_name, isDA, isMC, applyBOWS, applyEXTRACOND, extraconditions, runboundary, lumilist, intlumi, maxevents, gt, allFromGT, alignmentDB, alignmentTAG, apeDB, apeTAG, bowDB, bowTAG, vertextype, tracktype, refittertype, ttrhtype, applyruncontrol, ptcut, CMSSW_dir, the_dir)

◆ __del__()

def TkAlMap.TkAlMap.__del__ (   self)

Definition at line 756 of file TkAlMap.py.

References TkAlMap.TkAlMap.clean_up().

756  def __del__(self):
757  self.clean_up()
758 
759 

Member Function Documentation

◆ analyse()

def TkAlMap.TkAlMap.analyse (   self)

Do all.

Definition at line 705 of file TkAlMap.py.

References TkAlMap.TkAlMap.do_tanh, TkAlMap.TkAlMap.draw_color_bar(), TkAlMap.TkAlMap.draw_text(), TkAlMap.TkAlMap.draw_TPL(), TkAlMap.TkAlMap.set_colorbar_axis(), TkAlMap.TkAlMap.set_colorbar_colors(), and TkAlMap.TkAlMap.setup_colors().

705  def analyse(self):
706  self.setup_colors()
707  self.set_colorbar_axis()
708  if self.do_tanh: self.set_colorbar_colors()
709  self.draw_TPL()
710  self.draw_text()
711  self.draw_color_bar()
712 

◆ clean_up()

def TkAlMap.TkAlMap.clean_up (   self)

Clean up.

Definition at line 749 of file TkAlMap.py.

References TkAlMap.TkAlMap.is_cleaned, print(), TkAlMap.TkAlMap.tmp_file, and TkAlMap.TkAlMap.tmp_file_name.

Referenced by TkAlMap.TkAlMap.__del__().

749  def clean_up(self):
750  if not self.is_cleaned:
751  print('TkAlMap: deleting temporary file "'+self.tmp_file_name+'"')
752  self.tmp_file.Close()
753  os.remove(self.tmp_file_name)
754  self.is_cleaned = True
755 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ detect_tracker_version()

def TkAlMap.TkAlMap.detect_tracker_version (   self)

Definition at line 477 of file TkAlMap.py.

References TkAlMap.TkAlMap.GEO_file, print(), str, HIPMonitorVariables.tree, AlignmentIORootBase.tree, HIPTwoBodyDecayAnalyzer.tree, TkOfflineVariables.tree, and TkAlMap.TkAlMap.tree.

477  def detect_tracker_version(self):
478  print('TkAlMap: detecting Tk version')
479  #tree_name = 'alignTree'
480  #r_file = ROOT.TFile(self.root_file)
481  #if r_file is None: raise ValueError('The file "'+self.root_file+'" could not be opened')
482 
483 
487 
488  #if tree is None: raise ValueError('The tree "'+tree_name+'" was not found in file "'+self.root_file+'"')
489  phase = None
490  for event in self.tree:
491  module = event.id
492  if module > 303040000 and module < 306450000:
493  phase = 1
494  break
495  elif module > 302055000 and module < 302198000:
496  phase = 0
497  break
498  #r_file.Close()
499 
500  if phase is None: raise ValueError('TkAlMap: unknown tracker detected, is this phase2?')
501 
502  pahse_str = 'phase'+str(phase)
503  print('TkAlMap: '+pahse_str+' tracker detected')
504  if not pahse_str in self.GEO_file:
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'
507  #self.load_geometry()
508 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define str(s)

◆ draw_cms_prelim()

def TkAlMap.TkAlMap.draw_cms_prelim (   self)

Definition at line 629 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, hippyaddtobaddatafiles.cd(), TkAlMap.TkAlMap.draw_event_info(), TkAlMap.TkAlMap.height, svgfig.Plot.height, svgfig.Frame.height, svgfig.Dots.height, TkAlMap.TkAlMap.image_x1, TkAlMap.TkAlMap.width, svgfig.Plot.width, svgfig.Frame.width, svgfig.Dots.width, and TkAlMap.TkAlMap.y_scale.

Referenced by TkAlMap.TkAlMap.draw_text().

629  def draw_cms_prelim(self):
630  TL = ROOT.TLatex()
631  factor = 1. / 0.82
632  TL.SetTextSize(0.035*factor)
633  TL.SetTextAlign(11)
634  TL.SetTextFont(61)
635 
636  w_cms = ctypes.c_uint(0)
637  h_cms = ctypes.c_uint(0)
638  TL.GetTextExtent(w_cms, h_cms, 'CMS')
639  x1 = self.image_x1
640  y1 = 1. - (h_cms.value+0.)/(self.height+0.) - (1./(self.y_scale+0.))
641  self.canvas.cd()
642  TL.DrawLatex(x1, y1, 'CMS')
643 
644  TL.SetTextSize(0.035)
645  TL.SetTextFont(42)
646  x1_prel = x1 + 1.1*(w_cms.value+0.)/(self.width+0.)
647  TL.DrawLatex(x1_prel, y1, '#it{Preliminary}')
648 
649  self.draw_event_info(y1)
650 

◆ draw_color_bar()

def TkAlMap.TkAlMap.draw_color_bar (   self)

Definition at line 684 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, hippyaddtobaddatafiles.cd(), TkAlMap.TkAlMap.color_bar, TkAlMap.TkAlMap.color_bar_axis, and print().

Referenced by TkAlMap.TkAlMap.analyse().

684  def draw_color_bar(self):
685  print('TkAlMap: drawing color bar')
686  self.canvas.cd()
687  for box in self.color_bar:
688  self.color_bar[box].Draw('f')
689  #self.color_bar[box].Draw()
690  self.color_bar_axis.Draw()
691  self.canvas.Update()
692 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ draw_event_info()

def TkAlMap.TkAlMap.draw_event_info (   self,
  y 
)

Definition at line 651 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, hippyaddtobaddatafiles.cd(), and TkAlMap.TkAlMap.image_x2.

Referenced by TkAlMap.TkAlMap.draw_cms_prelim().

651  def draw_event_info(self, y):
652  TL = ROOT.TLatex()
653  TL.SetTextSize(0.035)
654  TL.SetTextFont(42)
655  TL.SetTextAlign(31)
656 
657  x1 = self.image_x2
658  y1 = y
659  self.canvas.cd()
660  TL.DrawLatex(x1, y1, 'pp collisions 13TeV')
661 
662 

◆ draw_text()

def TkAlMap.TkAlMap.draw_text (   self)

Draw functions.

Definition at line 664 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, hippyaddtobaddatafiles.cd(), TkAlMap.TkAlMap.draw_cms_prelim(), TkAlMap.TkAlMap.draw_title(), print(), and TkAlMap.TkAlMap.TkAlMap_text_dict.

Referenced by TkAlMap.TkAlMap.analyse().

664  def draw_text(self):
665  print('TkAlMap: drawing text')
666  self.canvas.cd()
667  TL = ROOT.TLatex()
668  TL.SetTextSize(0.025)
669  for key in self.TkAlMap_text_dict:
670  TL.SetTextAlign(self.TkAlMap_text_dict[key]['alignment'])
671  TL.DrawLatex(self.TkAlMap_text_dict[key]['x'], self.TkAlMap_text_dict[key]['y'], self.TkAlMap_text_dict[key]['text'])
672  self.draw_cms_prelim()
673  self.draw_title()
674  self.canvas.Update()
675 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ draw_title()

def TkAlMap.TkAlMap.draw_title (   self)

Titles and info.

Definition at line 619 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, hippyaddtobaddatafiles.cd(), TkAlMap.TkAlMap.image_x1, classes.PlotData.title, Geometry.title, TkAlMap.TkAlMap.title, and TkAlMap.TkAlMap.y_scale.

Referenced by TkAlMap.TkAlMap.draw_text().

619  def draw_title(self):
620  TL = ROOT.TLatex()
621  TL.SetTextSize(0.035)
622  TL.SetTextFont(42)
623  TL.SetTextAlign(13)
624  x1 = self.image_x1
625  y1 = 1-(5./(self.y_scale+0.))
626  self.canvas.cd()
627  TL.DrawLatex(x1, y1, self.title)
628 

◆ draw_TPL()

def TkAlMap.TkAlMap.draw_TPL (   self)

Definition at line 676 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, hippyaddtobaddatafiles.cd(), print(), and TkAlMap.TkAlMap.TkAlMap_TPL_dict.

Referenced by TkAlMap.TkAlMap.analyse().

676  def draw_TPL(self):
677  print('TkAlMap: drawing PolyLines')
678  self.canvas.cd()
679  for module in self.TkAlMap_TPL_dict:
680  self.TkAlMap_TPL_dict[module].Draw('f')
681  self.TkAlMap_TPL_dict[module].Draw()
682  self.canvas.Update()
683 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ fill_colors()

def TkAlMap.TkAlMap.fill_colors (   self)

Definition at line 313 of file TkAlMap.py.

References TkAlMap.TkAlMap.get_color_rgb_idx(), TkAlMap.TkAlMap.max_val, TkAlMap.TkAlMap.min_val, TkAlMap.TkAlMap.mod_val_dict, print(), TkAlMap.TkAlMap.rgb_map, and TkAlMap.TkAlMap.TkAlMap_TPL_dict.

Referenced by TkAlMap.TkAlMap.setup_colors().

313  def fill_colors(self):
314  print('TkAlMap: filling the colors')
315  #self.set_palette()
316  for module in self.mod_val_dict:
317  if module in self.TkAlMap_TPL_dict:
318  val = self.mod_val_dict[module]
319  cap_val = val
320  if cap_val > self.max_val: cap_val = self.max_val
321  if cap_val < self.min_val: cap_val = self.min_val
322  rgb = self.get_color_rgb_idx(cap_val)
323  col = self.rgb_map[rgb]
324  #col = self.pal_map[rgb]
325  #col = self.col_dic[rgb]
326  self.TkAlMap_TPL_dict[module].SetFillColor(col)
327  #self.TkAlMap_TPL_dict[module].SetFillColor(TEST_COLOR_IDX)
328 
329 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ get_color_rgb()

def TkAlMap.TkAlMap.get_color_rgb (   self,
  val 
)

Definition at line 264 of file TkAlMap.py.

References TkAlMap.TkAlMap.do_tanh, createfilelist.int, TkAlMap.TkAlMap.max_val, TkAlMap.TkAlMap.mean_val, TkAlMap.TkAlMap.min_val, TkAlMap.TkAlMap.palette, TkAlMap.TkAlMap.std_val, and str.

Referenced by TkAlMap.TkAlMap.get_color_rgb_idx().

264  def get_color_rgb(self, val):
265  if self.max_val is None or self.min_val is None:
266  value_frac = val
267  else:
268  if self.do_tanh:
269  val_th = math.tanh((val - self.mean_val)/(self.std_val))
270  max_th = math.tanh((self.max_val - self.mean_val)/(self.std_val))
271  min_th = math.tanh((self.min_val - self.mean_val)/(self.std_val))
272  value_frac = (val_th - min_th + 0.)/(max_th - min_th)
273  else:
274  value_range = self.max_val - self.min_val
275  if value_range == 0.: value_frac = 0.5
276  else: value_frac = (val - self.min_val + 0.)/(value_range + 0.)
277  if self.palette == 1:
278  r = 255
279  g = 255
280  b = 255
281 
282  if value_frac < 0.25:
283  r = 0
284  g = int(255.*((value_frac/0.25)))
285  b = 255
286  elif value_frac < 0.5:
287  r = 0
288  g = 255
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))
292  g = 255
293  b = 0
294  else:
295  r = 255
296  g = int(255.*(1. -(value_frac - 0.75)/0.25))
297  b = 0
298  return r, g, b
299  elif self.palette == 2:
300  red = [59, 76, 192]
301  blue = [180, 4, 38]
302  white = [255, 255, 255]
303  r, g, b = DivergingColor(red, blue, white, value_frac)
304  return r, g, b
305  else: raise ValueError('TkAlMap: unkown palette value '+str(palette)+', allowed values are 1 and 2')
306 
#define str(s)

◆ get_color_rgb_idx()

def TkAlMap.TkAlMap.get_color_rgb_idx (   self,
  val 
)

Definition at line 307 of file TkAlMap.py.

References TkAlMap.TkAlMap.get_color_rgb(), and createfilelist.int.

Referenced by TkAlMap.TkAlMap.fill_colors().

307  def get_color_rgb_idx(self, val):
308  r, g, b = self.get_color_rgb(val)
309  #return r*1000000+g*1000+b+1000000000
310  offset = 100
311  return int(r*255*255 + g*255 + r + g + b + offset)
312 

◆ load_geometry()

def TkAlMap.TkAlMap.load_geometry (   self)

Definition at line 509 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, TkAlMap.TkAlMap.cfg_path, TkAlMap.TkAlMap.data_path, TkAlMap.TkAlMap.GEO_file, TkAlMap.TkAlMap.height, svgfig.Plot.height, svgfig.Frame.height, svgfig.Dots.height, createfilelist.int, SiStripPI.max, SiStripPI.min, FastTimerService_cff.range, fileinputsource_cfi.read, TkAlMap.read_TPLfile(), submitPVValidationJobs.split(), TkAlMap.TkAlMap.tracker, TkAlMap.TkAlMap.width, svgfig.Plot.width, svgfig.Frame.width, and svgfig.Dots.width.

509  def load_geometry(self):
510  source_path = os.getenv('CMSSW_BASE') + '/src/'
511  var = {}
512  if sys.version_info[0] == 2:
513  execfile(source_path + self.cfg_path + self.GEO_file, var)
514  elif sys.version_info[0] == 3:
515  _filename = source_path + self.cfg_path + self.GEO_file
516  exec(compile(open(_filename, "rb").read(), _filename, 'exec'), var)
517 
518  MapStructure = var['TkMap_GEO']
519 
520  all_modules = {}
521  all_text = {}
522  x_max = -9999.
523  y_max = -9999.
524  x_min = 9999.
525  y_min = 9999.
526  for det in MapStructure:
527  if 'pixel' in self.tracker:
528  if not 'pixel' in det: continue
529  elif 'strips' in self.tracker:
530  if not 'strips' in det: continue
531  for sub in MapStructure[det]:
532  for part in MapStructure[det][sub]:
533  if part == 'latex':
534  all_text[det+'_'+sub] = MapStructure[det][sub][part]
535  continue
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]
540  TPL_dict = read_TPLfile(TPL_file)
541  for module in TPL_dict:
542  x_canv = []
543  y_canv = []
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)
554 
555  r_margin = 3
556  l_margin = 3
557  #t_margin = 15
558  t_margin = 11
559  b_margin = 8
560 
561  x_max += r_margin
562  x_min -= l_margin
563  y_max += t_margin
564  y_min -= b_margin
565 
566  x_range = x_max - x_min
567  y_range = y_max - y_min
568 
569  self.width = int(self.height*(x_range + 0.)/(y_range + 0.))
570  self.canvas.SetWindowSize(self.width, self.height)
571 
572  if (x_range + 0.)/(self.width + 0.) > (y_range + 0.)/(self.height + 0.):
573  x_scale = x_range
574  y_scale = (self.height + 0.)/(self.width + 0.)*x_range
575  else:
576  y_scale = y_range
577  x_scale = (self.width + 0.)/(self.height + 0.)*y_range
578  self.TkAlMap_TPL_dict = {}
579  for module in all_modules:
580  x = array('d', [])
581  y = array('d', [])
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.))
585  # Begin point is end point
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.))
588  #print(x, y)
589  self.TkAlMap_TPL_dict[module] = ROOT.TPolyLine(len(x), x, y)
590  #self.TkAlMap_TPL_dict[module].SetFillColor(1)
591  self.TkAlMap_TPL_dict[module].SetLineColor(1)
592  #print('lineW', self.TkAlMap_TPL_dict[module].GetLineWidth())
593  #self.TkAlMap_TPL_dict[module].Draw('f')
594  #self.TkAlMap_TPL_dict[module].Draw()
595 
596  self.image_x1 = (l_margin + 0.)/(x_scale + 0.)
597  self.image_x2 = (x_max - r_margin - x_min + 0.)/(x_scale + 0.)
598  self.image_y1 = (b_margin + 0.)/(y_scale + 0.)
599  self.image_y2 = (y_max - t_margin - y_min + 0.)/(y_scale + 0.)
600 
601  self.x_scale = x_scale
602  self.y_scale = y_scale
603 
604  #TL = ROOT.TLatex()
605  #TL.SetTextSize(0.025)
606  self.TkAlMap_text_dict = {}
607  for key in all_text:
608  x = (all_text[key]['x'] - x_min + 0.)/(x_scale + 0.)
609  y = (all_text[key]['y'] - y_min + 0.)/(y_scale + 0.)
610  self.TkAlMap_text_dict[key] = {}
611  self.TkAlMap_text_dict[key]['x'] = x
612  self.TkAlMap_text_dict[key]['y'] = y
613  self.TkAlMap_text_dict[key]['alignment'] = all_text[key]['alignment']
614  self.TkAlMap_text_dict[key]['text'] = all_text[key]['text']
615  #TL.SetTextAlign(all_text[key]['alignment'])
616  #TL.DrawLatex(x, y, all_text[key]['text'])
617 
def read_TPLfile(file_name)
Definition: TkAlMap.py:124

◆ load_tree()

def TkAlMap.TkAlMap.load_tree (   self)

Load functions.

Definition at line 397 of file TkAlMap.py.

References print(), and TkAlMap.TkAlMap.root_file.

397  def load_tree(self):
398  print('TkAlMap: loading tree ')
399  tree_name = 'alignTree'
400  r_file = ROOT.TFile(self.root_file)
401  if r_file is None: raise ValueError('The file "'+self.root_file+'" could not be opened')
402 
403  tree_tmp = r_file.Get(tree_name)
404  #self.tree = copy.deepcopy(tree_tmp)
405  self.tmp_file_name = str(time.time()).replace('.', '_')+'_TkAlMapTempFile.root'
406  self.tmp_file = ROOT.TFile(self.tmp_file_name, 'recreate')
407  self.tree = tree_tmp.CloneTree()
408  r_file.Close()
409  self.is_cleaned = False
410 
411  if self.tree is None: raise ValueError('The tree "'+tree_name+'" was not found in file "'+self.root_file+'"')
412 
413 
414 
def replace(string, replacements)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define str(s)

◆ load_var()

def TkAlMap.TkAlMap.load_var (   self)

Definition at line 415 of file TkAlMap.py.

References print().

Referenced by TkAlMap.TkAlMap.setup_colors().

415  def load_var(self):
416  print('TkAlMap: loading variable values ')
417  #tree_name = 'alignTree'
418  #r_file = ROOT.TFile(self.root_file)
419  #if r_file is None: raise ValueError('The file "'+self.root_file+'" could not be opened')
420 
421  #tree_tmp = r_file.Get(tree_name)
422  #tree = copy.deepcopy(tree_tmp)
423  #r_file.Close()
424 
425  #if tree is None: raise ValueError('The tree "'+tree_name+'" was not found in file "'+self.root_file+'"')
426 
427  self.mod_val_dict = {}
428  self.val_list = []
429  for event in self.tree:
430  module = event.id
431  var = self.var
432  if var == 'rdphi':
433  val = getattr(event, 'r')*getattr(event, 'dphi') else:
434  val = getattr(event, var)
435  val *= self.var_scale
436  self.mod_val_dict[module] = val
437  #if val not in self.val_list: self.val_list.append(val)
438  if module in self.TkAlMap_TPL_dict: self.val_list.append(val)
439 
443  if len(self.val_list) == 0:
444  print('Warning: no values filled, 0 moduleId\'s matched')
445  self.val_list = [-10+idx*0.5 for idx in range(41)]
446  self.val_list.sort()
447  self.mean_val = mean(self.val_list)
448  self.std_val = StdDev(self.val_list)
449  self.min_val = min(self.val_list)
450  self.max_val = max(self.val_list)
451 
452  if self.two_sigma_cap and not self.default_range:
453  print('-- Capping max and min: ')
454  print('---- True values : '+str(self.max_val)+', '+str(self.min_val))
455  self.min_val = max(min(self.val_list), self.mean_val - 2*self.std_val)
456  self.max_val = min(max(self.val_list), self.mean_val + 2*self.std_val)
457  print('---- Capped values : '+str(self.max_val)+', '+str(self.min_val))
458 
459  if self.default_range:
460  #if not self.var in KNOWN_VARIABLES: print('Warning: capping to default range not possible for unknown variable "'+self.var+'"')
461  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  else:
463  print('-- Capping max and min to default ranges: ')
464  print('---- True values : '+str(self.max_val)+', '+str(self.min_val))
465  self.min_val = self.var_min
466  self.max_val = self.var_max
467  print('---- Capped values : '+str(self.max_val)+', '+str(self.min_val))
468 
469  if self.min_val == self.max_val:
470  print('Warning: minimum value was equal to maximum value, '+str(self.max_val))
471  self.min_val = self.mean_val - 1.
472  self.max_val = self.mean_val + 1.
473 
474  #print(self.val_list)
475 
476 
def mean(data_list)
Definition: TkAlMap.py:114
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define str(s)
def StdDev(data_list)
Definition: TkAlMap.py:117

◆ plot_variable_distribution()

def TkAlMap.TkAlMap.plot_variable_distribution (   self,
  nbins = 200,
  out_dir = '.' 
)

Test functions.

Definition at line 714 of file TkAlMap.py.

References join(), SiStripPI.max, TkAlMap.TkAlMap.mean_val, SiStripPI.min, print(), TkAlMap.TkAlMap.std_val, TkAlMap.TkAlMap.tracker, TkAlMap.TkAlMap.two_sigma_cap, TkAlMap.TkAlMap.val_list, TkAlMap.TkAlMap.var, TkAlMap.TkAlMap.var_name, and TkAlMap.TkAlMap.var_units.

714  def plot_variable_distribution(self, nbins=200, out_dir='.'):
715  print('TkAlMap: drawing variable distribution')
716  canv_name = 'histogram_canvas_'+self.tracker+'_'+self.var
717  if self.two_sigma_cap: canv_name += '_cap'
718  canvas = ROOT.TCanvas(canv_name, 'TkAlMap '+self.var+' histogram canvas', 800, 800)
719 
720  h_min = min(min(self.val_list), self.mean_val - 2*self.std_val) - self.std_val
721  h_max = max(max(self.val_list), self.mean_val + 2*self.std_val) + self.std_val
722  hist = ROOT.TH1F(self.var+'_hist', 'Variable distribution', nbins, h_min, h_max)
723  for val in self.val_list:
724  hist.Fill(val)
725  hist.GetXaxis().SetTitle(self.var_name+' ['+self.var_units+']')
726  hist.GetYaxis().SetTitle('modules')
727  ROOT.gStyle.SetOptStat(0)
728  hist.Draw('e1')
729  canvas.Update()
730  left = ROOT.TLine(self.mean_val - 2*self.std_val, canvas.GetUymin(), self.mean_val - 2*self.std_val, canvas.GetUymax())
731  left.SetLineColor(2)
732  left.SetLineStyle(9)
733  left.Draw()
734  right = ROOT.TLine(self.mean_val + 2*self.std_val, canvas.GetUymin(), self.mean_val + 2*self.std_val, canvas.GetUymax())
735  right.SetLineColor(2)
736  right.SetLineStyle(9)
737  right.Draw()
738  mid = ROOT.TLine(self.mean_val, canvas.GetUymin(), self.mean_val, canvas.GetUymax())
739  mid.SetLineColor(1)
740  mid.SetLineStyle(9)
741  mid.Draw()
742  canvas.Update()
743  name = '_'.join(['VariableDistribution', self.var, self.tracker])
744  path = out_dir + '/' + name + '.png'
745  canvas.SaveAs(path)
746 
747 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19

◆ prepare_map_colors()

def TkAlMap.TkAlMap.prepare_map_colors (   self)

Definition at line 219 of file TkAlMap.py.

References print().

Referenced by TkAlMap.TkAlMap.setup_colors().

219  def prepare_map_colors(self):
220  print('TkAlMap: preparing map colors')
221 
222  self.colors = []
223  #self.palette = array('i', [])
224  col_idx = self.start_color_idx + self.n_color_color_bar + 10
225  self.col_dic = {} #Never used explicitly but needs to keep TColor values in the global memory
226  self.rgb_map = {}
227  #pal_idx = 0
228  #self.pal_map = {}
229  for val in self.val_list:
230  cap_val = val
231  if cap_val > self.max_val: cap_val = self.max_val
232  if cap_val < self.min_val: cap_val = self.min_val
233  r, g, b = self.get_color_rgb(cap_val)
234  idx = self.get_color_rgb_idx(cap_val)
235  if idx in self.colors: continue
236  self.colors.append(idx)
237  col_idx +=1
238  self.rgb_map[idx] = col_idx
239 
240  try:
241  col = ROOT.gROOT.GetColor(col_idx) #Here still OK
242  col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.) #Here segmentation if color index does not exist
243  self.col_dic[idx] = col #Here the new color is saved in in the global variable
244  except:
245  #Here the 6 argument TColor init with dummy rgb must be used, 4 argument one does not work (ROOT bug?)
246  #New color index is defined and SetRGB must also be called to overwrite dummy rgb
247  col = ROOT.TColor(col_idx, 0., 0., 0., "", 1)
248  col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
249  self.col_dic[idx] = col #Here the new color is saved in in the global variable
250  #self.palette.append(col_idx)
251  print('TkAlMap: map contains '+str(len(self.colors))+' colors')
252 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define str(s)

◆ save()

def TkAlMap.TkAlMap.save (   self,
  out_dir = '.',
  extension = 'pdf' 
)

Definition at line 693 of file TkAlMap.py.

References TkAlMap.TkAlMap.canvas, TkAlMap.TkAlMap.default_range, join(), print(), TkAlMap.TkAlMap.tracker, TkAlMap.TkAlMap.two_sigma_cap, and TkAlMap.TkAlMap.var.

Referenced by SpecificationBuilder_cfi.Specification.saveAll().

693  def save(self, out_dir='.', extension='pdf'):
694  name = '_'.join(['TkAlMap', self.tracker, self.var])
695  if self.two_sigma_cap and not self.default_range:
696  name += '_4sig'
697  elif self.default_range:
698  name += '_drange'
699  path = out_dir + '/' + name + '.' + extension
700  print('TkAlMap: saving canvas in "'+path+'"')
701  self.canvas.SaveAs(path)
702 
703 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
save
Definition: cuy.py:1164

◆ set_canvas()

def TkAlMap.TkAlMap.set_canvas (   self)

Definition at line 207 of file TkAlMap.py.

References TkAlMap.TkAlMap.tracker, TkAlMap.TkAlMap.two_sigma_cap, and TkAlMap.TkAlMap.var.

207  def set_canvas(self):
208  canv_name = 'canvas_'+self.tracker+'_'+self.var
209  if self.two_sigma_cap: canv_name += '_cap'
210  self.canvas = ROOT.TCanvas(canv_name, 'TkAlMap '+self.var+' canvas', self.width, self.height)
211  print('Actual w: '+str(self.canvas.GetWw())+', Actual h: '+str(self.canvas.GetWh()))
212 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define str(s)

◆ set_colorbar_axis()

def TkAlMap.TkAlMap.set_colorbar_axis (   self)

Definition at line 330 of file TkAlMap.py.

References TkAlMap.TkAlMap.image_x1, TkAlMap.TkAlMap.image_x2, and print().

Referenced by TkAlMap.TkAlMap.analyse().

330  def set_colorbar_axis(self):
331  print('TkAlMap: setting color bar axis')
332  b_x1 = self.image_x1
333  b_x2 = self.image_x2
334  b_y1 = 0.06
335  b_y2 = 0.06
336  b_width = 0.01
337  self.color_bar_axis = ROOT.TGaxis(b_x1, b_y1, b_x2, b_y2, self.min_val, self.max_val, 50510, '+S')
338  self.color_bar_axis.SetName('color_bar_axis')
339  self.color_bar_axis.SetLabelSize(0.02)
340  self.color_bar_axis.SetTickSize(0.01)
341  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+']')
342  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+']')
343  else: self.color_bar_axis.SetTitle(self.var_name+' ['+self.var_units+']')
344  self.color_bar_axis.SetTitleSize(0.025)
345 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
#define str(s)

◆ set_colorbar_colors()

def TkAlMap.TkAlMap.set_colorbar_colors (   self)

Definition at line 346 of file TkAlMap.py.

References TkAlMap.TkAlMap.image_x1, TkAlMap.TkAlMap.image_x2, TkAlMap.TkAlMap.max_val, TkAlMap.TkAlMap.min_val, TkAlMap.TkAlMap.n_color_color_bar, and print().

Referenced by TkAlMap.TkAlMap.analyse().

346  def set_colorbar_colors(self):
347  print('TkAlMap: initialize color bar colors')
348  if self.max_val is None or self.min_val is None:
349  col_step = 1./(self.n_color_color_bar + 0.)
350  val = col_step/2.
351  else:
352  b_range = self.max_val - self.min_val
353  col_step = (b_range + 0.)/(self.n_color_color_bar + 0.)
354  val = self.min_val + col_step/2.
355 
356  b_x1 = self.image_x1
357  b_x2 = self.image_x2
358  b_y1 = 0.06
359  b_y2 = 0.06
360  b_width = 0.01
361  b_xrange = b_x2 - b_x1
362  b_yrange = b_y2 - b_y1
363  b_dx = (b_xrange + 0.)/(self.n_color_color_bar + 0.)
364  b_dy = (b_yrange + 0.)/(self.n_color_color_bar + 0.)
365 
366  self.color_bar = {}
367  x1 = b_x1
368  y1 = b_y1
369 
370  col_idx = self.start_color_idx
371  for i_c in range(self.n_color_color_bar):
372  col_idx += 1
373  r, g, b = self.get_color_rgb(val)
374  try:
375  col = ROOT.gROOT.GetColor(col_idx) #Here still OK
376  col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.) #Here segmentation if color index does not exist
377  self.color_bar_colors[col_idx] = col #Here the new color is saved in in the global variable
378  except:
379  #Here the 6 argument TColor init with dummy rgb must be used, 4 argument one does not work (ROOT bug?)
380  #New color index is defined and SetRGB must also be called to overwrite dummy rgb
381  col = ROOT.TColor(col_idx, 0., 0., 0., "", 1)
382  col.SetRGB((r+0.)/255., (g+0.)/255., (b+0.)/255.)
383  self.color_bar_colors[col_idx] = col #Here the new color is saved in in the global variable
384  x2 = x1 + b_dx
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)
391 
392  x1 += b_dx
393  y1 += b_dy
394  val += col_step
395 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ set_palette()

def TkAlMap.TkAlMap.set_palette (   self,
  palette 
)

Definition at line 253 of file TkAlMap.py.

253  def set_palette(self, palette):
254  self.palette = palette
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')
259  print(pal_str)
260  #ROOT.gstyle.SetPalette(len(self.colors), self.colors)
261  #ROOT.gStyle.SetPalette(len(self.palette), self.palette)
262  pass
263 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def set_palette(name=None, ncontours=999)
Definition: plotscripts.py:228
#define str(s)

◆ set_var()

def TkAlMap.TkAlMap.set_var (   self,
  var,
  var_range = [None,
  None 
)

Definition at line 191 of file TkAlMap.py.

References print().

191  def set_var(self, var, var_range=[None, None]):
192  print('TkAlMap: setting variable to '+var)
193  self.var = var
194  self.var_name = var
195  self.var_units = 'cm'
196  self.var_scale = 1.
197  self.var_min = var_range[0]
198  self.var_max = var_range[1]
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]
205  self.set_canvas()
206 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ setup_colors()

def TkAlMap.TkAlMap.setup_colors (   self)

Color setup.

Definition at line 214 of file TkAlMap.py.

References TkAlMap.TkAlMap.fill_colors(), TkAlMap.TkAlMap.load_var(), and TkAlMap.TkAlMap.prepare_map_colors().

Referenced by TkAlMap.TkAlMap.analyse().

214  def setup_colors(self):
215  self.load_var()
216  self.prepare_map_colors()
217  self.fill_colors()
218 

Member Data Documentation

◆ canvas

◆ cfg_path

TkAlMap.TkAlMap.cfg_path

Definition at line 177 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.load_geometry().

◆ col_dic

TkAlMap.TkAlMap.col_dic

Definition at line 225 of file TkAlMap.py.

◆ color_bar

TkAlMap.TkAlMap.color_bar

Definition at line 366 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.draw_color_bar().

◆ color_bar_axis

TkAlMap.TkAlMap.color_bar_axis

Definition at line 337 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.draw_color_bar().

◆ color_bar_colors

TkAlMap.TkAlMap.color_bar_colors

Definition at line 171 of file TkAlMap.py.

◆ colors

TkAlMap.TkAlMap.colors

Definition at line 222 of file TkAlMap.py.

◆ data_path

TkAlMap.TkAlMap.data_path

Definition at line 176 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.load_geometry().

◆ default_range

TkAlMap.TkAlMap.default_range

Definition at line 163 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.save().

◆ do_tanh

TkAlMap.TkAlMap.do_tanh

Definition at line 166 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.analyse(), and TkAlMap.TkAlMap.get_color_rgb().

◆ GEO_file

TkAlMap.TkAlMap.GEO_file

tree = r_file.Get(tree_name) tree_tmp = r_file.Get(tree_name) tree = copy.deepcopy(tree_tmp) r_file.Close()

Definition at line 158 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.detect_tracker_version(), and TkAlMap.TkAlMap.load_geometry().

◆ height

TkAlMap.TkAlMap.height

Definition at line 161 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.draw_cms_prelim(), and TkAlMap.TkAlMap.load_geometry().

◆ image_x1

◆ image_x2

TkAlMap.TkAlMap.image_x2

◆ image_y1

TkAlMap.TkAlMap.image_y1

Definition at line 598 of file TkAlMap.py.

◆ image_y2

TkAlMap.TkAlMap.image_y2

Definition at line 599 of file TkAlMap.py.

◆ is_cleaned

TkAlMap.TkAlMap.is_cleaned

Definition at line 173 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.clean_up().

◆ max_val

TkAlMap.TkAlMap.max_val

◆ mean_val

TkAlMap.TkAlMap.mean_val

◆ min_val

TkAlMap.TkAlMap.min_val

◆ mod_val_dict

TkAlMap.TkAlMap.mod_val_dict

Definition at line 427 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.fill_colors().

◆ n_color_color_bar

TkAlMap.TkAlMap.n_color_color_bar

Definition at line 181 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.set_colorbar_colors().

◆ palette

TkAlMap.TkAlMap.palette

Definition at line 254 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.get_color_rgb().

◆ rgb_map

TkAlMap.TkAlMap.rgb_map

Definition at line 226 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.fill_colors().

◆ root_file

TkAlMap.TkAlMap.root_file

Definition at line 165 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.load_tree().

◆ start_color_idx

TkAlMap.TkAlMap.start_color_idx

Definition at line 180 of file TkAlMap.py.

◆ std_val

TkAlMap.TkAlMap.std_val

◆ title

◆ TkAlMap_text_dict

TkAlMap.TkAlMap.TkAlMap_text_dict

Definition at line 606 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.draw_text().

◆ TkAlMap_TPL_dict

TkAlMap.TkAlMap.TkAlMap_TPL_dict

Definition at line 578 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.draw_TPL(), and TkAlMap.TkAlMap.fill_colors().

◆ tmp_file

TkAlMap.TkAlMap.tmp_file

Definition at line 406 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.clean_up().

◆ tmp_file_name

TkAlMap.TkAlMap.tmp_file_name

Definition at line 405 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.clean_up().

◆ tracker

◆ tree

◆ two_sigma_cap

TkAlMap.TkAlMap.two_sigma_cap

◆ val_list

TkAlMap.TkAlMap.val_list

else: if 'full' in self.tracker: print('Warning: Unknown module '+str(module)) r_file.Close()

Definition at line 428 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.plot_variable_distribution().

◆ var

◆ var_max

TkAlMap.TkAlMap.var_max

Definition at line 198 of file TkAlMap.py.

◆ var_min

TkAlMap.TkAlMap.var_min

Definition at line 197 of file TkAlMap.py.

◆ var_name

TkAlMap.TkAlMap.var_name

Definition at line 194 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.plot_variable_distribution().

◆ var_scale

TkAlMap.TkAlMap.var_scale

Definition at line 196 of file TkAlMap.py.

◆ var_units

TkAlMap.TkAlMap.var_units

Definition at line 195 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.plot_variable_distribution().

◆ width

◆ x_scale

TkAlMap.TkAlMap.x_scale

Definition at line 601 of file TkAlMap.py.

◆ y_scale

TkAlMap.TkAlMap.y_scale

Definition at line 602 of file TkAlMap.py.

Referenced by TkAlMap.TkAlMap.draw_cms_prelim(), and TkAlMap.TkAlMap.draw_title().