submitScan.py
Go to the documentation of this file.
1 import ROOT, glob, sys, os, re
2 from array import array
3 import Optimizations, OptimizationUtils
4 
5 ##### PARSE INPUT ARGUMENT #####
6 
7 if not len(sys.argv) > 2:
8  print "Please specify an optimization name and configuration number"
9  sys.exit()
10 
11 optimization_name = sys.argv[1]
12 configuration = int(sys.argv[2])
13 
14 optimization = Optimizations.Optimization(optimization_name)
15 macro_template = "../../macros/NoSnout.mac"
16 
17 ##### Loop over Parameters that were optimized ####
18 n_scan_points = 30
19 
20 #print optimization.parameter_names
21 #sys.exit()
22 
23 for par_iter in range(len(optimization.parameter_names)):
24  if par_iter < 2: continue
25 
26  lower_limit = optimization.parameter_lower_limits[par_iter]
27  upper_limit = optimization.parameter_upper_limits[par_iter]
28 
29 # if optimization.parameter_names[par_iter] not in ["FHCHorn1ICRadius1","FHCHorn1ICRadius2","FHCHorn1ICRadius4","FHCHorn1OCRadius","FHCHorn1ICLength1","FHCHorn1ICLength3","FHCHorn1ICLength4","FHCHorn1ICLength5","FHCHorn2LongRescale","FHCHorn2RadialRescale","FHCHorn2RadialRescaleCst","FHCProtonEnergy","RHCHorn1ICRadius1","RHCHorn2RadialRescaleCst"]:
30 # continue
31 
32  # Loop over values to scan for this parameter
33  for scan_iter in range(n_scan_points+1):
34 
35  scan_value = lower_limit + scan_iter*(upper_limit-lower_limit)/n_scan_points
36 
37  # Create new macro files
38  macro_output = "../../macros/Optimizations/"+optimization.optname+"/"+optimization.optname+"_"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value)+".mac"
39  macro_outputRHC = "../../macros/Optimizations/"+optimization.optname+"/"+optimization.optname+"_"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value)+".mac"
40  if optimization.rhc_parameters_float_separate:
41  macro_output = "../../macros/Optimizations/"+optimization.optname+"FHC/"+optimization.optname+"FHC_"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value)+".mac"
42  macro_outputRHC = "../../macros/Optimizations/"+optimization.optname+"RHC/"+optimization.optname+"RHC_"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value)+".mac"
43 
44 
45  oldmac = open(macro_template)
46  oldmac_lines = oldmac.readlines()
47  oldmac.close()
48  newmac = open(macro_output, 'w')
49  newmacRHC = open(macro_outputRHC, 'w')
50 
51  par_values = optimization.readVariationFromMacros(configuration)
52  par_values[par_iter] = scan_value
53 
54  modes = ["FHC"]
55  if optimization.rhc_parameters_float_separate:
56  modes = ["FHC","RHC"]
57 
58  for mode in modes:
59  for s in oldmac_lines:
60  output_file = newmac
61  if mode=="RHC":
62  output_file = newmacRHC
63 
64  if s.find("/LBNE/det/seHornCurrent")>=0:
65  if not mode+"HornCurrent" in optimization.parameter_names and not "HornCurrent" in optimization.parameter_names:
66  output_file.write(s)
67  for j in range(0,len(optimization.parameter_names)):
68  if optimization.rhc_parameters_float_separate:
69  if (optimization.parameter_names[j].startswith("FHC") and
70  mode=="RHC"):
71  continue
72  if (optimization.parameter_names[j].startswith("RHC") and
73  mode=="FHC"):
74  continue
75  parameter = optimization.parameter_names[j]
76  macro_set_stage = optimization.macro_set_stages[j]
77 
78  if macro_set_stage == "PreInit":
79  value = par_values[j]
80  output_file.write(optimization.macro_commands[j]+" "+str(value)+" "+optimization.parameter_units[j]+"\n")
81 
82  if("MultiSphereTargetRadius" in parameter):
83  output_file.write("/LBNE/det/UseMultiSphereTarget\n")
84  output_file.write("/LBNE/det/TargetMaterial Beryllium\n")
85  if("SimpleTargetLength" in parameter):
86  output_file.write("/LBNE/det/UseSimpleCylindricalTarget\n")
87 
88  prefix = ""
89  if optimization.rhc_parameters_float_separate:
90  prefix = mode
91 
92  # setup special horn shape
93  if optimization.optname in ["CP_run5","CP_run7","CP_run8","CP_run13"]:
94  Horn1OCRadius = par_values[optimization.getParameterIndex(prefix+"Horn1OCRadius")];
95  Horn1ICRadius1 = par_values[optimization.getParameterIndex(prefix+"Horn1ICRadius1")];
96  Horn1ICRadius2 = par_values[optimization.getParameterIndex(prefix+"Horn1ICRadius2")];
97  Horn1ICRadius3 = par_values[optimization.getParameterIndex(prefix+"Horn1ICRadius3")];
98  Horn1ICRadius4 = par_values[optimization.getParameterIndex(prefix+"Horn1ICRadius4")];
99  Horn1ICLength1 = par_values[optimization.getParameterIndex(prefix+"Horn1ICLength1")];
100  Horn1ICLength2 = par_values[optimization.getParameterIndex(prefix+"Horn1ICLength2")];
101  Horn1ICLength3 = par_values[optimization.getParameterIndex(prefix+"Horn1ICLength3")];
102  Horn1ICLength4 = par_values[optimization.getParameterIndex(prefix+"Horn1ICLength4")];
103  Horn1ICLength5 = par_values[optimization.getParameterIndex(prefix+"Horn1ICLength5")];
104  Horn1ICLength6 = par_values[optimization.getParameterIndex(prefix+"Horn1ICLength6")];
105  Horn1ICLength7 = par_values[optimization.getParameterIndex(prefix+"Horn1ICLength7")];
106  output_file.write("/LBNE/det/UseHorn1Polycone True \n")
107  output_file.write("/LBNE/det/NumInnerPtsHorn1Polycone 10 \n")
108  output_file.write("/LBNE/det/Horn1PolyPt0RinThickZ "+str(Horn1OCRadius)+" 2.000 0.00 mm\n");
109  output_file.write("/LBNE/det/Horn1PolyPt1RinThickZ "+str(Horn1ICRadius1)+" 2.000 1.00 mm\n");
110  output_file.write("/LBNE/det/Horn1PolyPt2RinThickZ "+str(Horn1ICRadius1)+" 2.000 "+str(1.00+Horn1ICLength1)+" mm\n");
111  output_file.write("/LBNE/det/Horn1PolyPt3RinThickZ "+str(Horn1ICRadius2)+" 2.000 "+str(1.00+Horn1ICLength1+Horn1ICLength2)+" mm\n")
112  output_file.write("/LBNE/det/Horn1PolyPt4RinThickZ "+str(Horn1ICRadius2)+" 2.000 "+str(1.00+Horn1ICLength1+Horn1ICLength2+Horn1ICLength3)+" mm\n")
113  output_file.write("/LBNE/det/Horn1PolyPt5RinThickZ "+str(Horn1ICRadius3)+" 2.000 "+str(1.00+Horn1ICLength1+Horn1ICLength2+Horn1ICLength3+Horn1ICLength4)+" mm\n")
114  output_file.write("/LBNE/det/Horn1PolyPt6RinThickZ "+str(Horn1ICRadius3)+" 2.000 "+str(1.00+Horn1ICLength1+Horn1ICLength2+Horn1ICLength3+Horn1ICLength4+Horn1ICLength5)+" mm\n")
115  output_file.write("/LBNE/det/Horn1PolyPt7RinThickZ "+str(Horn1ICRadius4)+" 2.000 "+str(1.00+Horn1ICLength1+Horn1ICLength2+Horn1ICLength3+Horn1ICLength4+Horn1ICLength5+Horn1ICLength6)+" mm\n")
116  output_file.write("/LBNE/det/Horn1PolyPt8RinThickZ "+str(Horn1ICRadius4)+" 2.000 "+str(1.00+Horn1ICLength1+Horn1ICLength2+Horn1ICLength3+Horn1ICLength4+Horn1ICLength5+Horn1ICLength6+Horn1ICLength7)+" mm\n")
117  output_file.write("/LBNE/det/Horn1PolyPt9RinThickZ "+str(Horn1OCRadius)+" 2.000 "+str(2.00+Horn1ICLength1+Horn1ICLength2+Horn1ICLength3+Horn1ICLength4+Horn1ICLength5+Horn1ICLength6+Horn1ICLength7)+" mm\n")
118  output_file.write("/LBNE/det/Horn1PolyOuterRadius "+str(Horn1OCRadius)+" mm\n")
119 
120  output_file.write("/LBNE/det/TargetLengthOutsideHorn 0 m\n")
121  elif optimization.optname in ["CP_run9","CP_run10","CP_run11","CP_run14","CP_run15","CP_run16","CP_run17","CP_run18","CP_run19"]:
122 
123 
124  HornARadiusOC = par_values[optimization.getParameterIndex(prefix+"HornARadiusOC")];
125  HornARadius1 = par_values[optimization.getParameterIndex(prefix+"HornARadius1")];
126  HornARadius2 = par_values[optimization.getParameterIndex(prefix+"HornARadius2")];
127  HornALength = par_values[optimization.getParameterIndex(prefix+"HornALength")];
128  HornAF1 = par_values[optimization.getParameterIndex(prefix+"HornAF1")];
129  HornBRadiusOC = par_values[optimization.getParameterIndex(prefix+"HornBRadiusOC")];
130  HornBRadius1 = par_values[optimization.getParameterIndex(prefix+"HornBRadius1")];
131  HornBRadius2 = par_values[optimization.getParameterIndex(prefix+"HornBRadius2")];
132  HornBRadius3 = par_values[optimization.getParameterIndex(prefix+"HornBRadius3")];
133  HornBLength = par_values[optimization.getParameterIndex(prefix+"HornBLength")];
134  HornBF1 = par_values[optimization.getParameterIndex(prefix+"HornBF1")];
135  HornBF2 = par_values[optimization.getParameterIndex(prefix+"HornBF2")];
136  HornBF3 = par_values[optimization.getParameterIndex(prefix+"HornBF3")];
137  HornBF4 = par_values[optimization.getParameterIndex(prefix+"HornBF4")];
138  HornBF5 = par_values[optimization.getParameterIndex(prefix+"HornBF5")];
139 
140 
141  HornCRadiusOC = par_values[optimization.getParameterIndex(prefix+"HornCRadiusOC")];
142  HornCRadius1 = par_values[optimization.getParameterIndex(prefix+"HornCRadius1")];
143  HornCRadius2 = par_values[optimization.getParameterIndex(prefix+"HornCRadius2")];
144  HornCRadius3 = par_values[optimization.getParameterIndex(prefix+"HornCRadius3")];
145  HornCLength = par_values[optimization.getParameterIndex(prefix+"HornCLength")];
146  HornCF1 = par_values[optimization.getParameterIndex(prefix+"HornCF1")];
147  HornCF2 = par_values[optimization.getParameterIndex(prefix+"HornCF2")];
148  HornCF3 = par_values[optimization.getParameterIndex(prefix+"HornCF3")];
149  HornCF4 = par_values[optimization.getParameterIndex(prefix+"HornCF4")];
150  HornCF5 = par_values[optimization.getParameterIndex(prefix+"HornCF5")];
151 
152  HornBLongPosition = par_values[optimization.getParameterIndex(prefix+"HornBLongPosition")]
153  HornCLongPosition = par_values[optimization.getParameterIndex(prefix+"HornCLongPosition")]
154 
155  newmac.write("/LBNE/det/NumberOfHornsPolycone 3 \n")
156  newmac.write("/LBNE/det/Horn2PolyZStartPos "+str(HornBLongPosition)+" \n")
157  newmac.write("/LBNE/det/Horn3PolyZStartPos "+str(HornCLongPosition)+" \n")
158  newmac.write("/LBNE/det/NumInnerPtsHorn1Polycone 5 \n")
159  newmac.write("/LBNE/det/NumInnerPtsHorn2Polycone 8 \n")
160  newmac.write("/LBNE/det/NumInnerPtsHorn3Polycone 8 \n")
161 
162  newmac.write("/LBNE/det/Horn1PolyPt0RinThickZ "+str(HornARadiusOC-15)+" 2.000 0.00 mm\n");
163  newmac.write("/LBNE/det/Horn1PolyPt1RinThickZ "+str(HornARadius1)+" 2.000 1.00 mm\n");
164  newmac.write("/LBNE/det/Horn1PolyPt2RinThickZ "+str(HornARadius1)+" 2.000 "+str(1.00+HornALength*HornAF1)+" mm\n");
165  newmac.write("/LBNE/det/Horn1PolyPt3RinThickZ "+str(HornARadius2)+" 2.000 "+str(1.00+HornALength)+" mm\n")
166  newmac.write("/LBNE/det/Horn1PolyPt4RinThickZ "+str(HornARadiusOC-15)+" 2.000 "+str(2.00+HornALength)+" mm\n")
167  newmac.write("/LBNE/det/Horn1PolyOuterRadius "+str(HornARadiusOC)+" mm\n")
168 
169  newmac.write("/LBNE/det/Horn2PolyPt0RinThickZ "+str(HornBRadiusOC-15)+" 2.000 0.00 mm\n");
170  newmac.write("/LBNE/det/Horn2PolyPt1RinThickZ "+str(HornBRadius1)+" 2.000 1.00 mm\n");
171  newmac.write("/LBNE/det/Horn2PolyPt2RinThickZ "+str(HornBRadius1)+" 2.000 "+str(1.00+HornBLength*HornBF1)+" mm\n");
172  newmac.write("/LBNE/det/Horn2PolyPt3RinThickZ "+str(HornBRadius2)+" 2.000 "+str(1.00+HornBLength*(HornBF1+HornBF2))+" mm\n")
173  newmac.write("/LBNE/det/Horn2PolyPt4RinThickZ "+str(HornBRadius2)+" 2.000 "+str(1.00+HornBLength*(HornBF1+HornBF2+HornBF3))+" mm\n")
174  newmac.write("/LBNE/det/Horn2PolyPt5RinThickZ "+str(HornBRadius3)+" 2.000 "+str(1.00+HornBLength*(HornBF1+HornBF2+HornBF3+HornBF4))+" mm\n")
175  newmac.write("/LBNE/det/Horn2PolyPt6RinThickZ "+str(HornBRadius3)+" 2.000 "+str(1.00+HornBLength)+" mm\n")
176  newmac.write("/LBNE/det/Horn2PolyPt7RinThickZ "+str(HornBRadiusOC-15)+" 2.000 "+str(2.00+HornBLength)+" mm\n")
177  newmac.write("/LBNE/det/Horn2PolyOuterRadius "+str(HornBRadiusOC)+" mm\n")
178 
179  newmac.write("/LBNE/det/Horn3PolyPt0RinThickZ "+str(HornCRadiusOC-15)+" 4.000 0.00 mm\n");
180  newmac.write("/LBNE/det/Horn3PolyPt1RinThickZ "+str(HornCRadius1)+" 4.000 1.00 mm\n");
181  newmac.write("/LBNE/det/Horn3PolyPt2RinThickZ "+str(HornCRadius1)+" 4.000 "+str(1.00+HornCLength*HornCF1)+" mm\n");
182  newmac.write("/LBNE/det/Horn3PolyPt3RinThickZ "+str(HornCRadius2)+" 4.000 "+str(1.00+HornCLength*(HornCF1+HornCF2))+" mm\n")
183  newmac.write("/LBNE/det/Horn3PolyPt4RinThickZ "+str(HornCRadius2)+" 4.000 "+str(1.00+HornCLength*(HornCF1+HornCF2+HornCF3))+" mm\n")
184  newmac.write("/LBNE/det/Horn3PolyPt5RinThickZ "+str(HornCRadius3)+" 4.000 "+str(1.00+HornCLength*(HornCF1+HornCF2+HornCF3+HornCF4))+" mm\n")
185  newmac.write("/LBNE/det/Horn3PolyPt6RinThickZ "+str(HornCRadius3)+" 4.000 "+str(1.00+HornCLength)+" mm\n")
186  newmac.write("/LBNE/det/Horn3PolyPt7RinThickZ "+str(HornCRadiusOC-15)+" 4.000 "+str(2.00+HornCLength)+" mm\n")
187  newmac.write("/LBNE/det/Horn3PolyOuterRadius "+str(HornCRadiusOC)+" mm\n")
188 
189  newmac.write("/LBNE/det/TargetLengthOutsideHorn 0 m\n")
190 
191  if optimization.optname=="CP_run19":
192  newmac.write("/LBNE/det/SetPolyconeHornParabolic 1 \n")
193  newmac.write("/LBNE/det/SetPolyconeHornParabolic 2 \n")
194  newmac.write("/LBNE/det/SetPolyconeHornParabolic 3 \n")
195 
196 
197 
198  elif s.find("/run/initialize")>=0:
199  output_file.write(s)
200  for j in range(0,len(optimization.parameter_names)):
201 
202  if (optimization.parameter_names[j].startswith("FHC") and
203  mode=="RHC"):
204  continue
205  if (optimization.parameter_names[j].startswith("RHC") and
206  mode=="FHC"):
207  continue
208 
209  parameter = optimization.parameter_names[j]
210 
211  macro_set_stage = optimization.macro_set_stages[j]
212  if(macro_set_stage == "Idle"):
213  value = par_values[j]
214  output_file.write(optimization.macro_commands[j]+" "+str(value)+" "+optimization.parameter_units[j]+"\n")
215  # Also scale beam width to match target fin size
216  # For optimizations other than "CP_test", which
217  # was started without this scaling and shouldn't be
218  # changed in the middle of the optimization
219  if(parameter==prefix+"GraphiteTargetFinWidth"):
220  if optimization.optname in ["CP_run5","CP_run7","CP_run8","CP_run13"]:
221  # Scale nominal size (1.7 mm) based on
222  # change in fin width from nominal (.10 m)
223  new_beam_size = 1.7 * par_values[j]/10
224  output_file.write("/LBNE/generator/beamSigmaX "+str(new_beam_size)+" mm \n");
225  output_file.write("/LBNE/generator/beamSigmaY "+str(new_beam_size)+" mm \n");
226  if parameter == "BeamSigma":
227  temp = optimization.macro_commands[j].replace("SigmaX","SigmaY")
228  newmac.write(temp+" "+str(value)+" "+optimization.parameter_units[j]+"\n")
229 
230 
231  else:
232  output_file.write(s);
233 
234  output_file.close();
235 
236  # submit jobs
237  os.chdir("../..")
238  print "prefix",prefix
239  print "opt parnames",optimization.parameter_names[par_iter]
240  if not optimization.rhc_parameters_float_separate:
241  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode neutrino --notuples -m Optimizations-"+optimization.optname+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
242  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode antineutrino --notuples -m Optimizations-"+optimization.optname+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
243  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode neutrino --notuples -p FTFP_BERT -m Optimizations-"+optimization.optname+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
244  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode antineutrino --notuples -p FTFP_BERT -m Optimizations-"+optimization.optname+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
245  elif mode=="FHC":
246  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode neutrino --notuples -m Optimizations-"+optimization.optname+mode+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
247  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode neutrino --notuples -p FTFP_BERT -m Optimizations-"+optimization.optname+mode+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
248  elif mode=="RHC":
249  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode antineutrino --notuples -m Optimizations-"+optimization.optname+mode+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
250  os.system("python ProductionScripts/submit_flux.py -n 100000 -f 1 -l 5 --mode antineutrino --notuples -p FTFP_BERT -m Optimizations-"+optimization.optname+mode+"-"+str(configuration)+"_Scan_"+optimization.parameter_names[par_iter]+"_"+str(scan_value))
251 
252  os.chdir("BeamSimStudies/GeneticOptimization")
253 
int open(const char *, int)
Opens a file descriptor.
if(!yymsg) yymsg
static QCString str