HPS-MC
 
All Classes Namespaces Files Functions Variables Pages
Loading...
Searching...
No Matches
space_events.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 <unistd.h>
8
13int main(int argc, char** argv)
14{
15 int nevhep;
16 vector<stdhep_entry> new_event;
17
18 int event_interval = 500;
19 int output_n = 500000;
20 int max_output_files = 0;
21 int output_filename_digits = 2;
22
23 int c;
24
25 while ((c = getopt(argc, argv, "hn:e:N:")) != -1)
26 switch (c)
27 {
28 case 'h':
29 printf("-h: print this help\n");
30 printf("-e: interval between events\n");
31 printf("-N: max number of files to write\n");
32 printf("-n: output events per output file\n");
33 return(0);
34 break;
35 case 'e':
36 event_interval = atoi(optarg);
37 break;
38 case 'n':
39 output_n = atoi(optarg);
40 break;
41 case 'N':
42 max_output_files = atoi(optarg);
43 output_filename_digits = strlen(optarg);
44 break;
45 case '?':
46 printf("Invalid option or missing option argument; -h to list options\n");
47 return(1);
48 default:
49 abort();
50 }
51
52 printf("Interval between nonempty events %d, output events per output file %d, max output files %d\n", event_interval, output_n, max_output_files);
53
54 if (argc - optind < 2)
55 {
56 printf("<input stdhep filenames> <output stdhep basename>\n");
57 return 1;
58 }
59
60 int n_events;
61 int istream = 0;
62 int ostream = 1;
63 int ilbl;
64
65 char *output_basename = argv[argc-1];
66 char output_filename[100];
67 int file_n = 1;
68
69 int events_written = 0;
70
71 open_read(argv[optind++], istream);
72 while (max_output_files == 0 || file_n - 1 < max_output_files) {
73 sprintf(output_filename, "%s_%0*d.stdhep", output_basename, output_filename_digits, file_n++);
74 open_write(output_filename, ostream, output_n);
75 for (int nevhep = 0; nevhep < output_n; nevhep++)
76 {
77 if (events_written%event_interval == 0)
78 {
79 while (!read_next(istream)) {
80 close_read(istream);
81 if (optind < argc - 1)
82 {
83 open_read(argv[optind++], istream);
84 }
85 else
86 {
87 close_write(ostream);
88 return(0);
89 }
90 }
91 read_stdhep(&new_event);
92 }
93 else
94 {
95 add_filler_particle(&new_event);
96 }
97
98 write_stdhep(&new_event, nevhep+1);
99 write_file(ostream);
100 events_written++;
101 }
102 close_write(ostream);
103 }
104}
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)