HPS-MC
random_sample_usingInputEventsInOrder.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 #include <iostream>
12 using namespace std;
13 
18 int main(int argc, char** argv)
19 {
20  double poisson_mu = -1;
21  double poisson_mu_correction = 0;
22  int output_n = 2500000;
23  int output_filename_digits = 1;
24 
25  int rseed = 0;
26  int c;
27 
28  while ((c = getopt(argc, argv, "hn:m:t:s:")) != -1)
29  switch (c)
30  {
31  case 'h':
32  printf("-h: print this help\n");
33  printf("-m: mean number of events in an event\n");
34  printf("-t: corretion parameter for mu\n");
35  printf("-n: output events per output file\n");
36  printf("-s: RNG seed\n");
37  return(0);
38  break;
39  case 'm':
40  poisson_mu = atof(optarg);
41  break;
42  case 't':
43  poisson_mu_correction = atof(optarg);
44  break;
45  case 'n':
46  output_n = atoi(optarg);
47  break;
48  case 's':
49  rseed = atoi(optarg);
50  break;
51  case '?':
52  printf("Invalid option or missing option argument; -h to list options\n");
53  return(1);
54  default:
55  abort();
56  }
57 
58  printf("Mean events per output event %f, output events per output file %d\n", poisson_mu, output_n);
59 
60  if (argc - optind < 2)
61  {
62  printf("<input stdhep filenames> <output stdhep basename>\n");
63  return 1;
64  }
65 
66  //initialize the RNG
67  const gsl_rng_type * T;
68  gsl_rng * r;
69  gsl_rng_env_setup();
70 
71  T = gsl_rng_mt19937;
72  r = gsl_rng_alloc (T);
73  gsl_rng_set(r, rseed);
74 
75  int istream = 0;
76  int ostream = 1;
77  int ilbl;
78 
79  char *output_basename = argv[argc-1];
80  char output_filename[100];
81 
82  int n_events = 0;
83  int mark_optind = optind;
84  while(optind < argc - 1){
85  n_events += open_read(argv[optind++], istream);
86  close_read(istream);
87  }
88  printf("read %d events\n", n_events);
89 
90  if (poisson_mu < 0) {
91  poisson_mu = ((double) n_events)/output_n*(1-poisson_mu_correction);
92  printf("Setting mu to %f\n", poisson_mu);
93  }
94 
95  vector<stdhep_entry> new_event;
96  open_read(argv[mark_optind++], istream);
97  sprintf(output_filename, "%s_%0*d.stdhep", output_basename, output_filename_digits, 1);
98  open_write(output_filename, ostream, output_n);
99  for (int nevhep = 0; nevhep < output_n; nevhep++)
100  {
101  int n_merge = gsl_ran_poisson(r, poisson_mu);
102  if (n_merge == 0)
103  add_filler_particle(&new_event);
104 
105  for (int i = 0; i < n_merge; i++)
106  {
107  while (!read_next(istream)) {
108  close_read(istream);
109  if (mark_optind < argc - 1)
110  {
111  open_read(argv[mark_optind++], istream);
112  }
113  else
114  {
115  printf("Out of input events\n");
116  close_write(ostream);
117  return(0);
118  }
119  }
120 
121  read_stdhep(&new_event);
122  }
123 
124  write_stdhep(&new_event, nevhep+1);
125  write_file(ostream);
126  }
127  close_write(ostream);
128 }
int main(int argc, char **argv)
void add_filler_particle(vector< stdhep_entry > *new_event)
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)
Definition: stdhep_util.cpp:69
int read_stdhep(vector< stdhep_entry > *new_event)
Definition: stdhep_util.cpp:14
bool read_next(int istream)
void close_read(int istream)
void write_file(int ostream)
void close_write(int ostream)