HPS-MC
 
Loading...
Searching...
No Matches
merge_poisson.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
15int main(int argc,char** argv)
16{
17 int nevhep;
18 vector<stdhep_entry> new_event;
19
20 double poisson_mu = -1.0;
21 int output_n = 500000;
22 int max_output_files = 0;
23 int output_filename_digits = 2;
24
25 int only_this_file = 0;
26
27 int rseed = 0;
28
29 int c;
30
31 while ((c = getopt(argc,argv,"hn:m:N:s:O:")) != -1)
32 switch (c)
33 {
34 case 'h':
35 printf("-h: print this help\n");
36 printf("-m: mean number of events in an event\n");
37 printf("-N: max number of files to write\n");
38 printf("-n: output events per output file\n");
39 printf("-s: RNG seed\n");
40 printf("-O: only write this one file\n");
41 return(0);
42 break;
43 case 'm':
44 poisson_mu = atof(optarg);
45 break;
46 case 'n':
47 output_n = atoi(optarg);
48 break;
49 case 'N':
50 max_output_files = atoi(optarg);
51 output_filename_digits = strlen(optarg);
52 break;
53 case 's':
54 rseed = atoi(optarg);
55 break;
56 case 'O':
57 only_this_file = atoi(optarg);
58 break;
59 case '?':
60 printf("Invalid option or missing option argument; -h to list options\n");
61 return(1);
62 default:
63 abort();
64 }
65
66 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);
67
68 if (argc - optind < 2)
69 {
70 printf("<input stdhep filenames> <output stdhep basename>\n");
71 return 1;
72 }
73
74 //initialize the RNG
75 const gsl_rng_type * T;
76 gsl_rng * r;
77 gsl_rng_env_setup();
78
79 T = gsl_rng_mt19937;
80 r = gsl_rng_alloc (T);
81 gsl_rng_set(r, rseed);
82
83 int n_events;
84 int istream = 0;
85 int ostream = 1;
86 int ilbl;
87
88 if (max_output_files != 0 && poisson_mu < 0) {
89 int evcount = 0;
90 open_read(argv[optind], istream);
91 while (read_next(istream)) {
92 evcount++;
93 }
94 close_read(istream);
95 printf("Counted %d events\n", evcount);
96
97 poisson_mu = (double) evcount/(output_n*max_output_files*poisson_mu);
98 printf("set mu = %f\n", poisson_mu);
99 }
100
101 if (poisson_mu < 0) poisson_mu *= -1;
102
103 char *output_basename = argv[argc-1];
104 char output_filename[100];
105 int file_n = 1;
106
107 open_read(argv[optind++], istream);
108 while (max_output_files == 0 || file_n - 1 < max_output_files) {
109 bool write_this_file = true;
110 if (only_this_file != 0 && file_n != only_this_file) {
111 printf("skip file %d\n", file_n);
112 write_this_file = false;
113 }
114 sprintf(output_filename, "%s_%0*d.stdhep", output_basename, output_filename_digits, file_n);
115 if (write_this_file)
116 open_write(output_filename, ostream, output_n);
117 for (int nevhep = 0; nevhep < output_n; nevhep++)
118 {
119 int n_merge = gsl_ran_poisson(r, poisson_mu);
120 if (n_merge == 0 && write_this_file)
121 add_filler_particle(&new_event);
122 //if (n_merge!=0) printf("file %d, nevhep %d: n_merge %d\n",file_n,nevhep,n_merge);
123 for (int i = 0; i < n_merge; i++)
124 {
125 while (!read_next(istream)) {
126 close_read(istream);
127 if (optind < argc - 1)
128 {
129 open_read(argv[optind++], istream);
130 }
131 else
132 {
133 printf("Out of input events\n");
134 if (write_this_file)
135 close_write(ostream);
136 return(0);
137 }
138 }
139
140 if (write_this_file)
141 read_stdhep(&new_event);
142 }
143
144 if (write_this_file) {
145 write_stdhep(&new_event, nevhep+1);
146 write_file(ostream);
147 }
148 }
149 if (write_this_file)
150 close_write(ostream);
151 file_n++;
152 }
153}
154
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)
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)