1 | |
2 | |
3 | |
4 | |
5 | #include <iostream> |
6 | #include <iomanip> |
7 | #include <vector> |
8 | using namespace std; |
9 | |
10 | #include <signal.h> |
11 | #include <time.h> |
12 | |
13 | #include "HDDM/hddm_s.h" |
14 | |
15 | void Smear(s_HDDM_t *hddm_s); |
16 | void ParseCommandLineArguments(int narg, char* argv[]); |
17 | void Usage(void); |
18 | void ctrlCHandle(int x); |
19 | |
20 | vector<char*> INFILENAMES; |
21 | char *OUTFILENAME = NULL__null; |
22 | int QUIT = 0; |
23 | unsigned int EVENTS_TO_SKIP = 0; |
24 | unsigned int EVENTS_TO_KEEP = 1; |
25 | unsigned int SPECIFIC_OFFSET_TO_KEEP = 0; |
26 | unsigned int SPECIFIC_EVENT_TO_KEEP = 0; |
27 | bool EVENT_TO_KEEP_MODE = false; |
28 | |
29 | #define _DBG_cout<<"hddm_cull_events.cc"<<":"<<29<< " " cout<<__FILE__"hddm_cull_events.cc"<<":"<<__LINE__29<<" " |
30 | #define _DBG__cout<<"hddm_cull_events.cc"<<":"<<30<< endl cout<<__FILE__"hddm_cull_events.cc"<<":"<<__LINE__30<<endl |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | int main(int narg,char* argv[]) |
37 | { |
38 | |
39 | signal(SIGINT2,ctrlCHandle); |
40 | |
41 | ParseCommandLineArguments(narg, argv); |
42 | |
43 | cout<<"Skipping "<<EVENTS_TO_SKIP<<endl; |
44 | cout<<"Keeping "<<EVENTS_TO_KEEP<<endl; |
45 | |
46 | |
47 | cout<<" output file: "<<OUTFILENAME<<endl; |
48 | s_iostream_t *fout = init_s_HDDM(OUTFILENAME); |
49 | if(!fout){ |
| |
50 | cout<<" Error opening output file \""<<OUTFILENAME<<"\"!"<<endl; |
51 | exit(-1); |
52 | } |
53 | |
54 | |
55 | unsigned int NEvents = 0; |
56 | unsigned int NEvents_read = 0; |
57 | time_t last_time = time(NULL__null); |
58 | for(unsigned int i=0; i<INFILENAMES.size(); i++){ |
| 2 | | Loop condition is true. Entering loop body | |
|
59 | cout<<" input file: "<<INFILENAMES[i]<<endl; |
| 3 | | Dereference of null pointer |
|
60 | s_iostream_t *fin = open_s_HDDM(INFILENAMES[i]); |
61 | if(!fin){ |
62 | cout<<" Error opening input file \""<<INFILENAMES[i]<<"\"!"<<endl; |
63 | exit(-1); |
64 | } |
65 | |
66 | |
67 | while(true){ |
68 | s_HDDM_t *hddm_s = read_s_HDDM(fin); |
69 | if(!hddm_s)break; |
70 | NEvents_read++; |
71 | |
72 | bool write_this_event = false; |
73 | |
74 | |
75 | |
76 | if(EVENT_TO_KEEP_MODE && hddm_s->physicsEvents!=HDDM_NULL(void*)&hddm_s_nullTarget){ |
77 | for(unsigned int i=0; i<hddm_s->physicsEvents->mult; i++){ |
78 | int eventNo = hddm_s->physicsEvents->in[i].eventNo; |
79 | if((unsigned int)eventNo == SPECIFIC_EVENT_TO_KEEP){ |
80 | write_this_event = true; |
81 | QUIT = true; |
82 | } |
83 | } |
84 | } |
85 | |
86 | |
87 | if(NEvents_read>EVENTS_TO_SKIP)write_this_event = true; |
88 | |
89 | |
90 | if(write_this_event){ |
91 | flush_s_HDDM(hddm_s, fout); |
92 | NEvents++; |
93 | }else{ |
94 | flush_s_HDDM(hddm_s, NULL__null); |
95 | } |
96 | |
97 | |
98 | time_t now = time(NULL__null); |
99 | if(now != last_time){ |
100 | cout<<" "<<NEvents_read<<" events read ("<<NEvents<<" event written) \r";cout.flush(); |
101 | last_time = now; |
102 | } |
103 | |
104 | |
105 | if(NEvents_read>=(EVENTS_TO_SKIP+EVENTS_TO_KEEP))break; |
106 | |
107 | if(QUIT)break; |
108 | } |
109 | |
110 | |
111 | close_s_HDDM(fin); |
112 | } |
113 | |
114 | |
115 | close_s_HDDM(fout); |
116 | |
117 | cout<<endl; |
118 | cout<<" "<<NEvents_read<<" events read, "<<NEvents<<" events written"<<endl; |
119 | |
120 | return 0; |
121 | } |
122 | |
123 | |
124 | |
125 | |
126 | void ParseCommandLineArguments(int narg, char* argv[]) |
127 | { |
128 | INFILENAMES.clear(); |
129 | |
130 | for(int i=1; i<narg; i++){ |
131 | char *ptr = argv[i]; |
132 | |
133 | if(ptr[0] == '-'){ |
134 | switch(ptr[1]){ |
135 | case 'h': Usage(); break; |
136 | case 'o': OUTFILENAME=&ptr[2]; break; |
137 | case 's': EVENTS_TO_SKIP=atoi(&ptr[2]); break; |
138 | case 'k': EVENTS_TO_KEEP=atoi(&ptr[2]); break; |
139 | case 'e': SPECIFIC_OFFSET_TO_KEEP=atoi(&ptr[2]); break; |
140 | case 'E': SPECIFIC_EVENT_TO_KEEP=atoi(&ptr[2]); EVENT_TO_KEEP_MODE=true; break; |
141 | } |
142 | }else{ |
143 | INFILENAMES.push_back(argv[i]); |
144 | } |
145 | } |
146 | |
147 | if(INFILENAMES.size()==0){ |
148 | cout<<endl<<"You must enter a filename!"<<endl<<endl; |
149 | Usage(); |
150 | } |
151 | |
152 | if(SPECIFIC_OFFSET_TO_KEEP>0){ |
153 | EVENTS_TO_KEEP=1; |
154 | EVENTS_TO_SKIP=SPECIFIC_OFFSET_TO_KEEP-1; |
155 | } |
156 | |
157 | if(SPECIFIC_EVENT_TO_KEEP>0){ |
158 | EVENTS_TO_KEEP=1; |
159 | EVENTS_TO_SKIP=1000000000; |
160 | } |
161 | |
162 | if(OUTFILENAME==NULL__null){ |
163 | if(SPECIFIC_OFFSET_TO_KEEP>0){ |
164 | OUTFILENAME = new char[256]; |
165 | sprintf(OUTFILENAME,"evt%d.hddm", SPECIFIC_OFFSET_TO_KEEP); |
166 | }else{ |
167 | OUTFILENAME = (char*)"culled.hddm"; |
168 | } |
169 | } |
170 | } |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | void Usage(void) |
177 | { |
178 | cout<<endl<<"Usage:"<<endl; |
179 | cout<<" hddm_cull_events [-oOutputfile] [-sNeventsToSkip] [-kNeventsToKeep] file1.hddm file2.hddm ..."<<endl; |
180 | cout<<endl; |
181 | cout<<"options:"<<endl; |
182 | cout<<" -oOutputfile Set output filename (def. culled.hddm)"<<endl; |
183 | cout<<" -sNeventsToSkip Set number of events to skip (def. 0)"<<endl; |
184 | cout<<" -kNeventsToKeep Set number of events to keep (def. 1)"<<endl; |
185 | cout<<" -eSingleEvent Keep only the single, specified event (file pos.)"<<endl; |
186 | cout<<" -ESingleEvent Keep only the single, specified event (event number)"<<endl; |
187 | cout<<endl; |
188 | cout<<" This will copy a continguous set of events from the combined event streams"<<endl; |
189 | cout<<" into a seperate output file. The primary use for this would be to copy"<<endl; |
190 | cout<<" a single, problematic event into a seperate file for easier debugging."<<endl; |
191 | cout<<endl; |
192 | cout<<" If the -eNNN option is used then only a single event is extracted"<<endl; |
193 | cout<<" (the NNN-th event) and written to a file with the name evtNNN.hddm."<<endl; |
194 | cout<<" Note that the event is counted from the begining of the file, starting"<<endl; |
195 | cout<<" with \"1\". This does NOT look at the event number stored in the event itself."<<endl; |
196 | cout<<" "<<endl; |
197 | cout<<" If the -ENNN option is used then only a single event is extracted"<<endl; |
198 | cout<<" (the specified event number) and written to a file with the name evtNNN.hddm."<<endl; |
199 | cout<<" "<<endl; |
200 | cout<<endl; |
201 | |
202 | exit(0); |
203 | } |
204 | |
205 | |
206 | |
207 | |
208 | void ctrlCHandle(int x) |
209 | { |
210 | QUIT++; |
211 | cerr<<endl<<"SIGINT received ("<<QUIT<<")....."<<endl; |
212 | } |