Package madgraph :: Package various :: Module plot_djrs
[hide private]
[frames] | no frames]

Source Code for Module madgraph.various.plot_djrs

  1  #! /usr/bin/env python 
  2  ################################################################################ 
  3  # 
  4  # Copyright (c) 2010 The MadGraph5_aMC@NLO Development team and Contributors 
  5  # 
  6  # This file is a part of the MadGraph5_aMC@NLO project, an application which  
  7  # automatically generates Feynman diagrams and matrix elements for arbitrary 
  8  # high-energy processes in the Standard Model and beyond. 
  9  # 
 10  # It is subject to the MadGraph5_aMC@NLO license which should accompany this  
 11  # distribution. 
 12  # 
 13  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
 14  # 
 15  ################################################################################ 
 16  """Example code to plot custom curves based on djrs.dat with matplotlib""" 
 17  from __future__ import absolute_import 
 18  from __future__ import print_function 
 19  import os 
 20  import sys  
 21  import matplotlib.pyplot as plt 
 22  import matplotlib.gridspec as gridspec 
 23  import matplotlib.pylab as pylab 
 24  from six.moves import range 
 25   
 26   
 27  ################################################################################ 
 28  #  TRY TO LINK TO HISTOGRAMS.PY 
 29  ################################################################################ 
 30  # We need to link to histograms.dat. 
 31  # You can put the path to MG5_aMC main directory here to link from any directory 
 32  #sys.path.append('PATH/TO/MADGRAPH') 
 33  #try to find relatively to this file 
 34  sys.path.append(os.path.basename(os.path.basename(__file__))) #../ 
 35  sys.path.append(os.path.basename(os.path.basename(os.path.basename(__file__)))) #../../ 
 36  # lets try the import. 
 37  try: 
 38      import madgraph 
 39  except ImportError: 
 40      try: 
 41          import internal 
 42      except ImportError: 
 43          print("You need to specify the path to the MG5_aMC directory")     
 44          sys.exit(1) 
 45      else: 
 46          from internal.histograms import * 
 47  else: 
 48      from madgraph.various.histograms import * 
 49   
 50  ################################################################################ 
 51  #  CHOICE OF INPUT FILE 
 52  ################################################################################ 
 53  # check if an argument is passed as inputfiles: 
 54  if len(sys.argv) >1: 
 55      input_file = sys.argv[1] 
 56  else: 
 57      #take the default path 
 58      input_file = './Events/run_01/tag_1_djrs.dat'     
 59  print("Reading information from: ", input_file) 
 60   
 61   
 62  ################################################################################ 
 63  #  PARSING THE FILE AND ACCESS TO BASIC PROPERTY OF THE OBJECT 
 64  ################################################################################ 
 65  #parsing the data and create the object instance  
 66  hwu_list = HwUList(input_file, raw_labels=True) 
 67  # raw label prevent modification of the weight label. They will stay at their inputfile value 
 68   
 69  # get the list of the plot names 
 70  names =  hwu_list.get_hist_names() 
 71  #print names 
 72  # get the list of the weight label 
 73  weights_name = hwu_list.get_wgt_names() 
 74  #print weights_name 
 75  # In this example, I want to plot the DJR1 -> select the histograms with d01 in their name: 
 76  selected_hist = [hwu_list.get(n) for n in names if 'd01' in n] 
 77   
 78  ################################################################################ 
 79  #  CREATE THE PLOT AND THE ASSOCIATE RATIO PLOT 
 80  ################################################################################ 
 81  # define a multi-plot frame for the plot 
 82  gs1 = gridspec.GridSpec(2, 1, height_ratios=[5,1]) 
 83  gs1.update(wspace=0, hspace=0) # set the spacing between axes.  
 84  main_frame = plt.subplot(gs1[0]) # main frame/plot 
 85  ratio_frame = plt.subplot(gs1[1]) # ratio frame/plot 
 86   
 87  main_frame.set_yscale('log') 
 88  #main_frame.yaxis.set_label_coords(-0.07, 0.90)  
 89  main_frame.set_ylabel(r'$\frac{d\sigma_{LO}}{dDJR1} [pb]$') 
 90  main_frame.set_title('Differential Jet Rate') 
 91  main_frame.set_xticklabels([]) #remove x-axis in the main frame (due to the ratio frame) 
 92   
 93  #ratio_frame.xaxis.set_label_coords(0.90, -0.20)  
 94  ratio_frame.set_xlabel(r'$log(DJR1/1[GeV])$') 
 95  ratio_frame.set_ylabel(r'$ME/PS$') 
 96   
 97   
 98  ################################################################################ 
 99  #  SETTING THE CURVE 
100  ################################################################################ 
101  # Adding the curves. Here I want to plot two curves: 
102  # the curve with the maximum value of QCUT from the 0 jet sample 
103  # the curve with the minimal value of QCUT from the highest multiplicity sample 
104  qcut= [l for l in weights_name if l.startswith('MUF=1_MUR=1_PDF=247000_MERGING=')] 
105  min_qcut,max_qcut = qcut[0],qcut[-1] 
106   
107  #get the histo 
108  h_0j = [h for h in selected_hist if 'Jet sample 0' in h.get_HwU_histogram_name()][0] 
109  h_1j = [h for h in selected_hist if 'Jet sample 1' in h.get_HwU_histogram_name()][0] 
110   
111  y_0j = h_0j.get(min_qcut) 
112  y_1j = h_1j.get(max_qcut)  
113  l_0j, = main_frame.plot(h_0j.get('bins'), y_0j, label='0j', linestyle='steps')  
114  l_1j, = main_frame.plot(h_1j.get('bins'), y_1j, label='1j', linestyle='steps')  
115   
116   
117  ################################################################################ 
118  #  ADDING UNCERTAINTY BAND 
119  ################################################################################ 
120  # Add the PDF uncertainty on the 0j sample 
121  # the attribute of get_uncertainty_band can be a regular expression, a list of weight name, or a function returning 0/1 
122  # Special attributes exists: PDF, QCUT, ALPSFACT, SCALE # this assumes standard name formatting 
123  # For PDF you can force the type of uncertainty band by specifying mode='gaussian' or mode='hessian' 
124  # if using 'PDF' attributes the correct type should be found automatically 
125  pdfmin, pdfmax = h_0j.get_uncertainty_band('PDF') 
126  fill_between_steps(h_0j.get('bins'), pdfmin, pdfmax, ax=main_frame, facecolor=l_0j.get_color(), alpha=0.5, 
127                          edgecolor=l_0j.get_color() 
128  ) 
129  # use a second method for h_1j 
130  pdfmin, pdfmax = h_1j.get_uncertainty_band(['MUF=1_MUR=1_PDF=%i_MERGING=30' % i for i in range(247000,247100)], mode='hessian') 
131  fill_between_steps(h_1j.get('bins'), pdfmin, pdfmax, ax=main_frame, facecolor=l_1j.get_color(), alpha=0.5, 
132                          edgecolor=l_1j.get_color() 
133  ) 
134   
135   
136  ################################################################################ 
137  #  ADDING RATIO PLOT 
138  ################################################################################ 
139  ratio = [y_0j[i]/y_1j[i] if y_1j[i] else 0 for i in range(len(y_0j))] 
140  ratio_frame.plot(h_0j.get('bins'), ratio, linestyle='steps') 
141   
142   
143  ################################################################################ 
144  #  SETTING SOME STYLE IMPROVMENT FOR THE PLOT 
145  ################################################################################ 
146  # Some final style processing of matplotlib 
147  main_frame.legend(ncol=2, prop={'size':12}, loc=4) 
148  ratio_frame.set_yticks(ratio_frame.get_yticks()[:-1]) # remove upper tick of the ratio plot 
149  # Adding the MadGraph5_aMC@NLO flag on the plot (likely overcomplicated plot.text() should be better) 
150  ax_c = main_frame.twinx() 
151  ax_c.set_ylabel('MadGraph5_aMC@NLO') 
152  ax_c.yaxis.set_label_coords(1.01, 0.25) 
153  ax_c.set_yticks(main_frame.get_yticks()) 
154  ax_c.set_yticklabels([]) 
155   
156   
157  ################################################################################ 
158  #  WRITE THE OUTPUT FILE 
159  ################################################################################ 
160  plt.savefig("DJR1.pdf") # many extension possible (jpg/png/...) 
161