forked from SNOwGLoBES/snowglobes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supernova.pl
executable file
·450 lines (314 loc) · 10.8 KB
/
supernova.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/perl
# Script to run supernova rates programs
# K. Scholberg July 2010
# arguments: flux name, channel file name, experiment configuration name, noweight
# e.g. ./supernova.pl livermore water wc100kt30prct 0
# If noweight is set, then the output spectra will not have channel weighting factors applied (default is that weighting factors are applied)
$fluxname = $ARGV[0];
$channame = $ARGV[1];
$expt_config = $ARGV[2];
$noweight = $ARGV[3];
$exename = "bin/supernova";
unless (-f $exename) {
print $exename," executable not found. Please compile and install, with SNOWGLOBES variable set.", "\n";
exit;
}
$chanfilename = "channels/channels_".$channame.".dat";
# If we are using a non-standard binning, this binning must be defined at the top of
# the channel file. The format is:
# % bins emin emax sampling_points sampling_min sampling_max
# and all energy values must be in GeV
# the default values are...
$ewins{'bins'} = "200";
$ewins{'emin'} = "0.0005"; #GeV
$ewins{'emax'} = "0.100"; #GeV
$ewins{'sampling_points'} = "200";
$ewins{'sampling_min'} = "0.0005"; #GeV
$ewins{'sampling_max'} = "0.100"; #GeV
$energywindowset = "standard";
open(CHANFILE,$chanfilename);
$firstchanline = <CHANFILE>;
$firstchanline =~ s/^s+//; # remove leading whitespace
if (index($firstchanline, "%") != -1){
@binningarray = split /\s+/, $firstchanline;
$ewins{'bins'} = @binningarray[1];
$ewins{'emin'} = @binningarray[2];
$ewins{'emax'} = @binningarray[3];
$ewins{'sampling_points'} = @binningarray[4];
$ewins{'sampling_min'} = @binningarray[5];
$ewins{'sampling_max'} = @binningarray[6];
}
close(CHANFILE);
# these energy window/binning values are now ready for the preamble
# now that we've read in the channel file, we should know what energy windows we have
if ($ewins{'emax'} > 0.199 and $ewins{'emax'} < 0.201) {
$energywindowset = "_he";
print "using high energy window!\n";
}
# Create the globes file
$globesfilename = "supernova.glb";
open(GLOBESFILE,">$globesfilename");
# Here we add the globes preamble, with any modifications needed for rebinning taken care of
open(PREAMBLE,"glb/preamble.glb");
$ready_to_modify = 0;
while(<PREAMBLE>) {
if (index($_, "Energy window") != -1){$ready_to_modify = 1;}
$ourline = $_;
if($ready_to_modify) {
keys %ewins; # reset the internal iterator so a prior each() doesn't affect the loop
while(my($k, $v) = each %ewins){
if (index($ourline, $k) != -1){
@vallist = split /\s+/, $ourline;
$ourline =~ s/$vallist[2]/$v/;
last; # we have done the replacement, no need to continue
}
} # end while loop over ewin replacements
} # any necessary modification is done
print GLOBESFILE $ourline;
}
close(PREAMBLE);
# Put in the flux info
$fluxfilename = "fluxes/".$fluxname.".dat";
unless(-f $fluxfilename) {
print "Flux file name ",$fluxfilename," not found\n";
exit;
}
open(FLUX,"glb/flux.glb");
while(<FLUX>) {
# Replace the flux file name with the input argument
if (/flux_file/) {
$_ = " \@flux_file= \"".$fluxfilename."\"\n";
}
print GLOBESFILE $_;
}
close(FLUX);
# Now go through the channels and put in the relevant lines
# First, smearing for each channel
unless (-f $chanfilename) {
print "Channel file name ",$chanfilename," not found\n";
exit;
}
open(CHANFILE,$chanfilename);
while(<CHANFILE>) {
# skip energy window info line
if (/%/) {next;}
# Grab the channel name
$_=~s/\s*//;
$_=~s/\s+/ /g;
@stuff = split(/\ /,$_);
$chan_name = $stuff[0];
$output_line = "include \"smear/smear_".$chan_name."_".$expt_config.".dat\"\n";
print GLOBESFILE $output_line;
}
close(CHANFILE);
# Detector info
$detfilename = "detector_configurations.dat";
unless (-f $detfilename) {
print "Detector file name ",$detfilename," not found\n";
exit;
}
open(DETFILENAME,$detfilename);
while(<DETFILENAME>) {
chop($_);
# Skip comments
if (/\#/) {next;}
$_=~s/\s*//;
$_=~s/\s+/ /g;
@stuff = split(/\ /,$_);
$detname = $stuff[0];
$masses{$detname} = $stuff[1];
$normfact{$detname} = $stuff[2];
if ($detname eq "" || $masses{$detname} eq "" || $normfact{$detname} eq ""){ next;}
}
close(DETFILENAME);
# Mass and target normalization by species
#%masses = ("sk1",22.5,"sk2",22.5,"100kt15pc",100, "100kt30pc",100,"ar17",17,"scint50kt",50,"halo",1.0);
#%normfact = ("sk1",2/18,"sk2",2/18,"100kt15pc",2/18, "100kt30pc",2/18,"ar17kt",1/40,"scint50kt",2/14,"halo",1/208);
if ($masses{$expt_config} == 0){
print "Error: please enter a valid experiment configuration\n";
exit;
}
$target_mass = sprintf("%13.6f",$masses{$expt_config}*$normfact{$expt_config}); # This is ktons of free protons
print "Experiment config: ",$expt_config," Mass: ",$masses{$expt_config}," kton \n";
#print " Target mass: ",$normfact{$expt_config}," ",$target_mass,"\n";
# Add the background smearing here, for the given detector configuration
# (Not yet implemented: for multiple background channels,
# read them from a file labeled by detector configuration)
$do_bg = 0;
$bg_chan_name = "bg_chan";
$bg_filename = "backgrounds/".$bg_chan_name."_".$expt_config.".dat";
if (-e $bg_filename) {
$do_bg = 1;
print "Using background file ",$bg_filename,"\n";
} else {
print "No background file for this configuration\n";
}
if ($do_bg == 1) {
$output_line = "include \"smear/smear_".$bg_chan_name."_".$expt_config.".dat\"\n";
print GLOBESFILE $output_line;
}
open(DETECTOR,"glb/detector.glb");
while(<DETECTOR>) {
# Replace the flux file name with the input argument
if (/mass/) {
$_ = "\$target_mass= ".$target_mass."\n";
}
print GLOBESFILE $_;
}
close(DETECTOR);
print GLOBESFILE "\n /******** Cross-sections *********/\n \n";
# Now the cross-sections. Note that some of these are repeated
# even thougn it is not necessary (xscns for several flavors can be in the
# same file).
# This is just to make a consistent loop over channels.
open(CHANFILE,$chanfilename);
while(<CHANFILE>) {
# skip energy window info line
if (/%/) {next;}
# Grab the channel name
$_=~s/\s*//;
$_=~s/\s+/ /g;
@stuff = split(/\ /,$_);
$chan_name = $stuff[0];
$output_line = "cross(\#".$chan_name.")<\n";
print GLOBESFILE $output_line;
$output_line = " \@cross_file= \"xscns/xs_".$chan_name.".dat\"\n";
# print $output_line;
print GLOBESFILE $output_line;
$output_line = "\>\n";
# print $output_line;
print GLOBESFILE $output_line;
}
close(CHANFILE);
# Now the fake bg channel cross section, if it exists
if ($do_bg == 1) {
$output_line = "cross(\#".$bg_chan_name.")<\n";
print GLOBESFILE $output_line;
$output_line = " \@cross_file= \"xscns/xs_zero.dat\"\n";
# print $output_line;
print GLOBESFILE $output_line;
$output_line = "\>\n";
# print $output_line;
print GLOBESFILE $output_line;
}
print GLOBESFILE "\n \/******** Channels *********\/\n \n";
# Now, the channel definitions
unless(-f $chanfilename) {
print "Channel file name ",$chanfilename," not found\n";
exit;
}
open(CHANFILE,$chanfilename);
while(<CHANFILE>) {
# skip energy window info line
if (/%/) {next;}
# Grab the channel name
$_=~s/\s*//;
$_=~s/\s+/ /g;
@stuff = split(/\ /,$_);
$chan_name = $stuff[0];
$cpstate = $stuff[2];
$inflav = $stuff[3];
$output_line = "channel(\#".$chan_name."_signal)<\n";
print GLOBESFILE $output_line;
$output_line = " \@channel= \#supernova_flux: ".$cpstate.": ".$inflav.": ".$inflav.": \#".$chan_name.": \#".$chan_name."_smear\n";
# print $output_line;
print GLOBESFILE $output_line;
# Get the post-smearing efficiencies by channel
$eff_file = "effic/effic_".$chan_name."_".$expt_config.".dat";
# print $eff_file,"\n";
open(EFF_FILE,$eff_file);
while(<EFF_FILE>) {
$output_line = " \@post_smearing_efficiencies = ".$_;
print GLOBESFILE $output_line;
}
close(EFF_FILE);
# Try crazy reformatting
# or else get mysterious (but apparently harmless?) error from GLoBES
# Should try to track it down... -> error goes away with development GLoBES version
$output_line = "\n\>\n\n";
print GLOBESFILE $output_line;
}
close(CHANFILE);
if ($do_bg == 1) {
# Now make a fake channel for the background (just one possible bg file for now)
# This is dummy info
$cpstate = "-"; $inflav = "e";
$output_line = "channel(\#".$bg_chan_name."_signal)<\n";
print GLOBESFILE $output_line;
$output_line = " \@channel= \#supernova_flux: ".$cpstate.": ".$inflav.": ".$inflav.": \#".$bg_chan_name.": \#".$bg_chan_name."_smear\n";
# print $output_line;
print GLOBESFILE $output_line;
# Get the pre-smearing backgrounds by channel
$bg_file = "backgrounds/".$bg_chan_name."_".$expt_config.".dat";
print $bg_file,"\n";
open(BG_FILE,$bg_file);
while(<BG_FILE>) {
$output_line = " \@pre_smearing_background = ".$_;
print GLOBESFILE $output_line;
}
close (BG_FILE);
# Try crazy reformatting
# or else get mysterious (but apparently harmless?) error from GLoBES
# Should try to track it down... -> error goes away with development GLoBES version
$output_line = "\n\>\n\n";
print GLOBESFILE $output_line;
}
# End-matter
if ($energywindowset eq "standard"){
open(POSTAMBLE,"glb/postamble.glb");
}
elsif ($energywindowset eq "_he"){
open(POSTAMBLE,"glb/postamble_he.glb");
}
while(<POSTAMBLE>) {
print GLOBESFILE $_;
}
close(POSTAMBLE);
close(GLOBESFILE);
# Now run the executable
$comstring = $exename." ".$fluxname." ".$chanfilename." ".$expt_config;
system($comstring);
# Now apply channel-weighting factors-- this is the default
unless($noweight==1) {
print "Applying channel weighting factors to output\n";
&apply_weights("");
&apply_weights("_smeared");
}
sub apply_weights
{
open(CHANFILE,$chanfilename);
while(<CHANFILE>) {
# skip energy window info line
if (/%/) {next;}
# Grab the channel name
$_=~s/\s*//;
$_=~s/\s+/ /g;
@stuff = split(/\ /,$_);
$chan_name = $stuff[0];
$cpstate = $stuff[2];
$inflav = $stuff[3];
$num_target_factor = $stuff[4];
# Open the unweighted output file to read and the weighted file to write
$unweightfilename = "out/".$fluxname."_".$chan_name."_".$expt_config."_events".$_[0]."_unweighted.dat";
$weightedfilename = "> out/".$fluxname."_".$chan_name."_".$expt_config."_events".$_[0].".dat";
open (UNWEIGHT,$unweightfilename);
open (WEIGHTED,$weightedfilename);
while(<UNWEIGHT>) {
$_=~s/\s*//;
$_=~s/\s+/ /g;
if (/---/) {
print WEIGHTED $_,"\n";
} else {
@stuff2 = split(/\ /,$_);
$enbin = $stuff2[0];
$evrate = $stuff2[1];
if ($enbin ne "") {
print WEIGHTED $enbin," ",$evrate*$num_target_factor,"\n";
}
}
}
close(UNWEIGHT);
close(WEIGHTED);
}
close(CHANFILE);
}