HPS-MC
 
All Classes Namespaces Files Functions Variables Pages
Loading...
Searching...
No Matches
mix_signal.cc
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
4#include <string.h>
5#include <stdhep_util.hh>
6
7#include <gsl/gsl_rng.h>
8#include <gsl/gsl_randist.h>
9
10#include <unistd.h>
11
12/*
13 * Randomly replace at most 1 event in a background file with a selected event from a signal file.
14 */
15int main(int argc,char** argv)
16{
17 int nevhep;
18 vector<stdhep_entry> new_event;
19 vector<stdhep_entry> sig_event;
20
21 double signal_prob = 0.001;
22 int n_events = 500000;
23
24 int signal_n = 1;
25 int rseed = 0;
26 int c;
27
28 while ((c = getopt(argc,argv,"hm:s:n:")) != -1)
29 switch (c)
30 {
31 case 'h':
32 printf("-h: print this help\n");
33 printf("-m: probability of a signal event\n");
34 printf("-s: RNG seed\n");
35 printf("-n: which signal event to use\n");
36 return(0);
37 break;
38 case 'm':
39 signal_prob = atof(optarg);
40 break;
41 case 's':
42 rseed = atoi(optarg);
43 break;
44 case 'n':
45 signal_n = atoi(optarg);
46 break;
47 case '?':
48 printf("Invalid option or missing option argument; -h to list options\n");
49 return(1);
50 default:
51 abort();
52 }
53
54 printf("Mixing in the %dth signal event, with probability %f\n", signal_n, signal_prob);
55
56 if (argc - optind != 3)
57 {
58 printf("<input background stdhep filename> <input signal stdhep filename> <output stdhep filename>\n");
59 return 1;
60 }
61
62 //initialize the RNG
63 const gsl_rng_type * T;
64 gsl_rng * r;
65 gsl_rng_env_setup();
66
67 T = gsl_rng_mt19937;
68 r = gsl_rng_alloc (T);
69 gsl_rng_set(r, rseed);
70
71 int bkgd_stream = 0;
72 int sig_stream = 1;
73 int ostream = 2;
74 int ilbl;
75
76 open_read(argv[optind+1], sig_stream);
77 for (int i = 0; i < signal_n; i++) {
78 if (!read_next(sig_stream)) {
79 close_read(sig_stream);
80 return(1);
81 }
82 }
83 read_stdhep(&sig_event);
84 close_read(sig_stream);
85
86 open_read(argv[optind], bkgd_stream);
87 open_write(argv[optind+2], ostream, n_events);
88 bool sig_used = false;
89
90 int evcount = 0;
91 while (read_next(bkgd_stream)) {
92 nevhep = read_stdhep(&new_event);
93 if (!sig_used && gsl_rng_uniform(r) < signal_prob) {
94 write_stdhep(&sig_event, nevhep);
95 sig_used = true;
96 } else {
97 write_stdhep(&new_event, nevhep);
98 }
99 evcount++;
100 write_file(ostream);
101 }
102 close_read(bkgd_stream);
103 close_write(ostream);
104 printf("Counted %d events\n", evcount);
105}
int main(int argc, char **argv)
Definition mix_signal.cc:15
void open_write(char *filename, int ostream, int n_events)
int open_read(char *filename, int istream, int n_events)
void write_stdhep(vector< stdhep_entry > *new_event, int nevhep)
int read_stdhep(vector< stdhep_entry > *new_event)
bool read_next(int istream)
void close_read(int istream)
void write_file(int ostream)
void close_write(int ostream)