From 729f773ed00629e0f40e9291b0c9774605fe16e5 Mon Sep 17 00:00:00 2001
From: Cedric Verstege <cedric.verstege@kit.edu>
Date: Tue, 25 Jun 2024 17:21:08 +0200
Subject: [PATCH] Update subproc contribution script to work with python3

---
 .../yb_ystar/subproc_contrib_ybystar.py       | 155 +++++++++---------
 1 file changed, 80 insertions(+), 75 deletions(-)

diff --git a/tools/plotting/yb_ystar/subproc_contrib_ybystar.py b/tools/plotting/yb_ystar/subproc_contrib_ybystar.py
index 7b5e33ac..a82246d2 100755
--- a/tools/plotting/yb_ystar/subproc_contrib_ybystar.py
+++ b/tools/plotting/yb_ystar/subproc_contrib_ybystar.py
@@ -20,7 +20,7 @@ import matplotlib.gridspec as gridspec
 
 import fastnlo
 
-
+matplotlib.use('Agg')
 
 def main():
 	
@@ -36,7 +36,7 @@ def main():
 						
 	parser.add_argument('-s', '--subproc', default=['gg'], type=str, nargs='*',
 						help='Subprocesses that shall be compared. \n'
-							'Options: gg, gq, qiqi, qiai, qiqj, qiaj')
+							'Options: gg, gq, q=q, qiai, qiqj, qiaj')
 	parser.add_argument('-c', '--compare', default=False, const=True, type=bool, nargs='?',
 						help='Stackplot if option -c is chosen. Otherwise single-process-plots.')
 						#help='Stackplot if True. Single-Process-Plot if False.')
@@ -45,20 +45,24 @@ def main():
 
 
 	#Dictionary for how the subprocesses are called
-	processes = {"gg":"Gluon-Gluon",
-				"gq":"Gluon-Quark/Antiquark",
-				"qiqi":"Quark-Quark and $\mathrm{\overline{q_i} \; \overline{q_i}}$ (same flavor)",
-				"qiai":"Quark-Antiquark (same flavor)",
-				"qiqj":"Quark-Quark and $\mathrm{\overline{q_i} \; \overline{q_j}}}$ (different flavors)",
-				"qiaj":"Quark-Antiquark (different flavors)"}
+	processes = {
+		"qaq": "Quark-Antiquark",
+		"gaq": "Gluon-Antiquark",
+		"gq": "Gluon-Quark",
+		"gg": "Gluon-Gluon",
+		"qq": "Quark-Quark",
+		"aqaq": "Antiquark-Antiquark",
+	}
 
 	#shorter for labeling the stackplot
-	labeling = {"gg":r"$\mathrm{gg}$",
-				"gq":r"$\mathrm{gq} \ & \ \mathrm{g\overline{q}}$",
-				"qiqi":r"$\mathrm{q_i q_i} \ & \ \mathrm{\overline{q_i} \; \overline{q_i}}$",
-				"qiai":r"$\mathrm{q_i} \; \mathrm{\overline{q_i}}$",
-				"qiqj":r"$\mathrm{q_i q_j} \ & \ \mathrm{\overline{q_i} \; \overline{q_j}}$",
-				"qiaj":r"$\mathrm{q_i} \; \mathrm{\overline{q_j}}$"}
+	labeling = {
+		"qaq": r"$\mathrm{q\overline{q}}$",
+		"gaq": r"$\mathrm{g\overline{q}}$",
+		"gq": r"$\mathrm{gq}$",
+		"gg": r"$\mathrm{gg}$",
+		"qq": r"$\mathrm{qq}$",
+		"aqaq": r"$\mathrm{\overline{q}\overline{q}}$",
+	}
 
 
 	#parse arguments
@@ -68,13 +72,13 @@ def main():
 	table_ = os.path.basename(args['table'])
 	tablename = os.path.splitext(table_)[0]
 	tablename = os.path.splitext(tablename)[0] #because yb tables have .tab.gz ending (getting rid of both here)
-	print '\n'
-	print 'Table: ', tablename, '\n'
+	print('\n')
+	print('Table: ', tablename, '\n')
 	
 	#pdfset name
 	pdfset_ = os.path.basename(args['pdfset'])
 	pdfname = os.path.splitext(pdfset_)[0]
-	print 'PDF Set: ', pdfname, '\n'
+	print('PDF Set: ', pdfname, '\n')
 	
 	#checks for investigated phase space region --> mainly for plot-labeling
 	#range of y_boost
@@ -95,7 +99,7 @@ def main():
 			yb_range = r'$\mathrm{2.0 \leq \/ y_b < 2.5}$'
 			info_yb = r'2.0 <= yb < 2.5'
 		else:
-			print 'No info about range of yb.'
+			print('No info about range of yb.')
 			yb_range = ''
 			info_yb = ''
 		return yb_range, info_yb		#yb_range is in correct format for labeling
@@ -103,23 +107,23 @@ def main():
 	
 	#range of ystar
 	def ystar():
-		if 'ystar0' in args['table']:
+		if 'ys0' in args['table']:
 			ystar_range = r'$\mathrm{0.0 \leq \/ y^{\ast} < 0.5}$'
 			info_ystar = r'0 <= ystar < 0.5'
-		elif 'ystar1' in args['table']:
+		elif 'ys1' in args['table']:
 			ystar_range = r'$\mathrm{0.5 \leq \/ y^{\ast} < 1.0}$'
 			info_ystar = r'0.5 <= ystar < 1.0'
-		elif 'ystar2' in args['table']:
+		elif 'ys2' in args['table']:
 			ystar_range = r'$\mathrm{1.0 \leq \/ y^{\ast} < 1.5}$'
 			info_ystar = r'1.0 <= ystar < 1.5'
-		elif 'ystar3' in args['table']:
+		elif 'ys3' in args['table']:
 			ystar_range = r'$\mathrm{1.5 \leq \/ y^{\ast} < 2.0}$'
 			info_ystar = r'1.5 <= ystar < 2.0'
-		elif 'ystar4' in args['table']:
+		elif 'ys4' in args['table']:
 			ystar_range = r'$\mathrm{2.0 \leq \/ y^{\ast} < 2.5}$'
 			info_ystar = r'2.0 <= ystar < 2.5'
 		else:
-			print 'No info about range of ystar.'
+			print('No info about range of ystar.')
 			ystar_range = ''
 			info_ystar = ''
 		return ystar_range, info_ystar
@@ -134,16 +138,16 @@ def main():
 	info_yb = yb()[1]
 	info_ystar = ystar()[1]
 
-	print 'Phase Space Region:'
-	print '%s' %info_yb
-	print '%s' %info_ystar, '\n'
+	print('Phase Space Region:')
+	print('%s' %info_yb)
+	print('%s' %info_ystar, '\n')
 	
 
 	
 	
 	#Which subprocesses are evaluated?
-	print 'Subprocesses that are investigated: '
-	print args['subproc']
+	print('Subprocesses that are investigated: ')
+	print(args['subproc'])
 	
 	
 	######## EVALUATING ########
@@ -154,32 +158,32 @@ def main():
 	#cross section with every process included
 	fnlo.CalcCrossSection()
 	xs_all = np.array(fnlo.GetCrossSection())
-	print '\n'
-	print 'Cross section with all subprocesses xs_all: \n'
-	print xs_all, '\n \n'
+	print('\n')
+	print('Cross section with all subprocesses xs_all: \n')
+	print(xs_all, '\n \n')
 	
 	#cross section of single processes
 	xs_subproc_list = []
 	for i in range(0, len(args['subproc'])):
 		subproc_name = args['subproc'][i]
-		print 'Selected subprocess: %s' %subproc_name
+		print('Selected subprocess: %s' %subproc_name)
 		fnlo.SelectProcesses(subproc_name)
 		fnlo.CalcCrossSection()
 		xs_subproc = np.array(fnlo.GetCrossSection())
 		xs_subproc_list.append(xs_subproc)
-		print 'XS for subprocess %s: \n' %args['subproc'][i]
-		print xs_subproc, '\n \n'
+		print('XS for subprocess %s: \n' %args['subproc'][i])
+		print(xs_subproc, '\n \n')
 	xs_sub = np.array(xs_subproc_list) #Array with xs for selected processes in certain pTZ-bins
-	print 'XS of the selected processes in different pTZ-regions: \n'
-	print xs_sub, '\n \n'
+	print('XS of the selected processes in different pTZ-regions: \n')
+	print(xs_sub, '\n \n')
 	
 	#how much do the selected subprocesses contribute to the total xs?
 	fractions = np.divide(xs_sub,xs_all)
-	print 'fractions: \n', fractions, '\n'
+	print('fractions: \n', fractions, '\n')
 	
 	for i in range(0, len(fractions)):
-		print 'fraction of %s on total XS: ' %args['subproc'][i]
-		print fractions[i,:], '\n \n'
+		print('fraction of %s on total XS: ' %args['subproc'][i])
+		print(fractions[i,:], '\n \n')
 	
 	
 	#checking whether contribution is positive or negative
@@ -193,37 +197,39 @@ def main():
 				pos_contr[i,j] = 0.0
 			else:
 				neg_contr[i,j] = 0.0
-		print 'Positive contributions of %s on total XS: \n' %args['subproc'][i]
-		print pos_contr[i,:], '\n'
-		print 'Negative contributions of %s on total XS: \n' %args['subproc'][i]
-		print neg_contr[i,:], '\n \n'
+		print('Positive contributions of %s on total XS: \n' %args['subproc'][i])
+		print(pos_contr[i,:], '\n')
+		print('Negative contributions of %s on total XS: \n' %args['subproc'][i])
+		print(neg_contr[i,:], '\n \n')
 	
-	print 'Summary of positive and negative contributions of the subprocesses: \n'
-	print pos_contr, '\n'
-	print neg_contr, '\n'
+	print('Summary of positive and negative contributions of the subprocesses: \n')
+	print(pos_contr, '\n')
+	print(neg_contr, '\n')
 	
 	
 	
 	######## PLOTTING ########
 	#Bins for x-axis (according to first pdf (0))
 	bin_bounds = np.array(fnlo.GetObsBinsBounds(0))
-	#print bin_bounds.T		#bin_bounds.T[0] = lower bounds, bin_bounds.T[1] upper bounds
-	#print bin_bounds.flatten()
+	#print(bin_bounds.T		#bin_bounds.T[0] = lower bounds, bin_bounds.T[1] upper bounds)
+	#print(bin_bounds.flatten())
 	
 
 	#dictionary for using specific color per process
-	process_color = {"gg":"g",
-					"gq":"r",
-					"qiqi":"y",
-					"qiai":"c",
-					"qiqj":"m",
-					"qiaj":"b"}
+	process_color = {
+		"qaq": "c",
+		"gaq": "y",
+		"gq": "r",
+		"gg": "g",
+		"qq": "m",
+		"aqaq": "b",
+	}
 
 	#Check whether comparison-plot or single-plot is required:
-	print args['compare']
+	print(args['compare'])
 	if (args['compare']==True):		#stackplot that compares the contribution of the subprocesses to the total XS
 		plt.close()
-		fig0 = plt.figure(figsize=(8,7))
+		fig0 = plt.figure(figsize=(6,5))
 		ax0 = fig0.add_subplot(111)
 		
 		patches = []  #later needed for legend
@@ -235,36 +241,35 @@ def main():
 			
 			#positive contributions
 			ax0.fill_between(bin_bounds.flatten(), steppify_bin(y0_pos), steppify_bin(y0_pos+pos_contr[i]), #label=labeling[args['subproc'][i]],
-							facecolor=process_color[args['subproc'][i]], alpha=0.4)
+							facecolor=process_color[args['subproc'][i]], alpha=0.6)
 			y0_pos += pos_contr[i] #calculate new height of stackplot
 							
 			#negative contributions
 			ax0.fill_between(bin_bounds.flatten(), steppify_bin(y0_neg), steppify_bin(y0_neg+neg_contr[i]), #label=labeling[args['subproc'][i]],
-							facecolor=process_color[args['subproc'][i]], hatch='X', alpha=0.4)
+							facecolor=process_color[args['subproc'][i]], alpha=0.6)
 			y0_neg += neg_contr[i] #new 'height' below x-axis
 
 			#patch for subprocess i (for the legend) ##labeling happens here
 			patches.append(matplotlib.patches.Rectangle((0, 0), 0, 0, color=process_color[args['subproc'][i]],
-							label=labeling[args['subproc'][i]], alpha=0.4))
+							label=labeling[args['subproc'][i]], alpha=0.6))
 			ax0.add_patch(patches[i])
 			
 		#settings for the whole stackplot
-		ax0.set_xscale('log', nonposx='clip')
+		ax0.set_xscale('log', nonpositive='clip')
 		ax0.axis([30, 1000, -0.20, 1.20])
 		plt.axhline(y=1, xmin=0, xmax=1, color='k', linestyle='dotted') #dotted line at xs_sub/xs=1=100%
 		plt.axhline(y=0, xmin=0, xmax=1, color='k', linestyle='-')		#line at xs_sub/xs_tot=0
 		
 		#use phasespace region as title
-		title_phasespace = yb_range + ",     " + ystar_range
+		title_phasespace = yb_range + ", " + ystar_range
 		
-		plt.title(x=0.5, y=1.01, s='%s' %(title_phasespace), fontsize=22)
+		plt.title(x=0.5, y=1.01, label='%s' %(title_phasespace))
 
-		ax0.set_xlabel('$\mathrm{p_{T,Z}} \ \mathrm{[GeV]}$', fontsize=20)
+		ax0.set_xlabel('$\mathrm{p_{T,Z}} \ \mathrm{[GeV]}$')
 		ax0.xaxis.set_label_coords(0.9, -0.05)
-		ax0.set_ylabel(r'$\frac{\mathrm{XS_{subproc}}}{\mathrm{XS_{total}}}$', rotation=0, fontsize=24)
-		ax0.yaxis.set_label_coords(-0.16, 0.9)
+		ax0.set_ylabel("Subprocess decomposition")
 
-		ax0.text(0.96, 0.03, args['pdfset']+', NLO', transform=ax0.transAxes, fontsize=14, verticalalignment='bottom', horizontalalignment='right')
+		# ax0.text(0.96, 0.03, args['pdfset']+', NLO', transform=ax0.transAxes, fontsize=14, verticalalignment='bottom', horizontalalignment='right')
 		plt.legend()
 
 		plt.tight_layout()
@@ -277,7 +282,7 @@ def main():
 		stackplotname = 'compare%s_%s_%s.png' %(processnames, tablename, args['pdfset'])
 		fig0.savefig(stackplotname)
     
-		print 'Stackplot for subprocess comparison saved as: %s' %(stackplotname)
+		print('Stackplot for subprocess comparison saved as: %s' %(stackplotname))
 		
 		
 	
@@ -312,7 +317,7 @@ def main():
 
 
 			#info about phase space region included as text (see below)
-			ax0.set_xscale('log', nonposx='clip')
+			ax0.set_xscale('log', nonpositive='clip')
 			ax0.axis([30, 1000, -0.4, 1.2])
 			plt.axhline(y=1, xmin=0, xmax=1, color='k', linestyle='dotted')
 			plt.axhline(y=0, xmin=0, xmax=1, color='k', linestyle='-')
@@ -338,13 +343,13 @@ def main():
 		#save the plots
 			if os.path.exists(final_dir_date):
 				fig0.savefig(os.path.join(final_dir_date,plotname))
-				print 'Plot saved in %s' %(final_dir_date)
+				print('Plot saved in %s' %(final_dir_date))
 			else:
 				fig0.savefig(os.path.join(final_dir,plotname))
-				print 'Plot saved in %s' %(final_dir)
+				print('Plot saved in %s' %(final_dir))
 
-			print '\n'
-			print 'fraction plot saved as: %s' %(plotname)
+			print('\n')
+			print('fraction plot saved as: %s' %(plotname))
 
 
 
@@ -364,9 +369,9 @@ def steppify_bin(arr, isx=False):
     -> [5,5,6,6]
     """
     if isx:
-        newarr = np.array(zip(arr[:-1], arr[1:])).ravel()
+        newarr = np.array(list(zip(arr[:-1], arr[1:]))).ravel()
     else:
-        newarr = np.array(zip(arr, arr)).ravel()
+        newarr = np.array(list(zip(arr, arr))).ravel()
     return newarr
 
 
-- 
GitLab