7 #include <gsl/gsl_rng.h>
8 #include <gsl/gsl_randist.h>
21 int main(
int argc,
char** argv)
23 double poisson_mu = -1.0;
24 double poisson_mu_correction = 0;
25 int output_n = 500000;
26 int max_output_files = 1;
27 int output_filename_digits = 1;
28 int n_events_per_batch = 1000000;
33 while ((c = getopt(argc, argv,
"hn:m:t:N:b:s:")) != -1)
37 printf(
"-h: print this help\n");
38 printf(
"-m: mean number of events in an event\n");
39 printf(
"-t: corretion parameter for mu\n");
40 printf(
"-N: max number of files to write\n");
41 printf(
"-b: number of events per batch for caching input events\n");
43 printf(
"-s: RNG seed\n");
47 poisson_mu = atof(optarg);
50 poisson_mu_correction = atof(optarg);
53 output_n = atoi(optarg);
56 max_output_files = atoi(optarg);
57 output_filename_digits = strlen(optarg);
63 printf(
"Invalid option or missing option argument; -h to list options\n");
69 printf(
"Mean events per output event %f, output events per output file %d, max output files %d\n", poisson_mu, output_n, max_output_files);
71 if (argc - optind < 2)
73 printf(
"<input stdhep filenames> <output stdhep basename>\n");
78 const gsl_rng_type * T;
83 r = gsl_rng_alloc (T);
84 gsl_rng_set(r, rseed);
90 char *output_basename = argv[argc-1];
91 char output_filename[100];
95 int input_ind = optind;
96 while(input_ind < argc - 1){
97 n_events +=
open_read(argv[input_ind++], istream);
100 printf(
"read %d events\n", n_events);
101 printf(
"poisson_mu_correction: %i \n", poisson_mu_correction);
102 printf(
"output_n: %i \n", output_n);
104 if (poisson_mu < 0) {
105 poisson_mu = ((double) n_events)/((double) output_n*(1-poisson_mu_correction));
106 printf(
"Setting mu to %f\n", poisson_mu);
109 int n_batches = n_events/n_events_per_batch + 1;
110 printf(
"Number of batches for caching: %d\n", n_batches);
112 vector<stdhep_entry> new_event;
113 vector<vector<stdhep_entry> *> input_events;
114 vector<int> event_list;
116 int n_events_used = 0;
119 vector<vector<stdhep_entry> *> events_no_used;
121 while (file_n <= max_output_files) {
122 sprintf(output_filename,
"%s_%0*d.stdhep", output_basename, output_filename_digits, file_n++);
123 open_write(output_filename, ostream, output_n);
128 bool no_more_data =
false;
131 if (input_ind < argc - 1)
140 vector<stdhep_entry> *read_event =
new vector<stdhep_entry>;
142 input_events.push_back(read_event);
143 event_list.push_back(event_num++);
145 if( ((input_events.size() == n_events_per_batch) && (batch_num < n_batches - 1)) || (no_more_data && (batch_num == n_batches - 1)) )
148 shuffle(event_list.begin(), event_list.end(), default_random_engine(rseed + 10));
149 int event_list_index = 0;
150 while (nevhep < output_n) {
151 int n_merge = gsl_ran_poisson(r, poisson_mu);
155 if(event_list_index + n_merge > event_list.size()) {
156 if(batch_num == n_batches) {
157 for(
int i = 0; i < input_events.size(); i++)
158 delete input_events[i];
159 input_events.clear();
163 n_events_used += event_list_index;
164 for(
int i = 0; i < event_list_index; i++)
165 delete input_events[event_list[i]];
166 events_no_used.clear();
167 for(
int i = event_list_index; i < event_list.size(); i++)
168 events_no_used.push_back(input_events[event_list[i]]);
169 input_events.clear();
172 for(
int i = 0; i < events_no_used.size(); i++){
173 input_events.push_back(events_no_used[i]);
174 event_list.push_back(event_num++);
180 for (
int i = 0; i < n_merge; i++)
181 append_stdhep(&new_event, input_events[event_list[event_list_index + i]]);
182 event_list_index += n_merge;
188 if(nevhep == output_n) {
193 if (no_more_data)
break;
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)
int append_stdhep(vector< stdhep_entry > *event, const vector< stdhep_entry > *new_event)
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)