Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hddm_select_events.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // Created Oct 09, 2013 Kei Moriya
4 
5 #include "hddm_select_events.h"
6 #include <fstream>
7 
8 void Usage(void);
9 void ctrlCHandle(int x);
10 
11 string INFILENAME = "";
12 string OUTFILENAME = "";
13 bool saveRemainder = false;
15 int selectType = 0;
16 unsigned int MAX = 100 * 1000 * 1000;
17 bool debug = false;
18 string HDDM_CLASS = "s";
19 int QUIT = 0;
20 int seed = 0;
21 
22 bool HDDM_USE_COMPRESSION = false;
24 
25 TRandom2 *rndm;
26 
27 //-----------
28 // main
29 //-----------
30 int main(int argc,char* argv[]) {
31  // Set up to catch SIGINTs for graceful exits
32  signal(SIGINT,ctrlCHandle);
33 
34  extern char* optarg;
35  // Check command line arguments
36  int c;
37  while ((c = getopt(argc,argv,"ho:i:ars:M:dR:")) != -1) {
38  switch(c) {
39  case 'h':
40  Usage();
41  exit(-1);
42  break;
43  case 'i':
44  // Specify infile name.
45  // This will nullify options -d and -c
46  INFILENAME = optarg;
47  std::cout << "infile name: " << INFILENAME << std::endl;
48  break;
49  case 'o':
50  OUTFILENAME = optarg;
51  std::cout << "outfile name: " << OUTFILENAME << std::endl;
52  break;
53  case 'r':
54  HDDM_CLASS = "r";
55  break;
56  case 'a':
57  saveRemainder = true;
58  if (OUTFILENAME.find(".hddm") != std::string::npos) {
59  // if we found .hddm, then add "_remainder" in front
61  OUTFILENAME_REMAINDER.replace(OUTFILENAME.find(".hddm"),0,"_remainder");
62  }
63  else {
64  OUTFILENAME_REMAINDER = OUTFILENAME + "_remainder";
65  }
66  std::cout << "remainder outfile name: " << OUTFILENAME_REMAINDER
67  << std::endl;
68  break;
69  case 's':
70  selectType = atoi(optarg);
71  std::cout << "event selection type: " << selectType << std::endl;
72  break;
73  case 'M':
74  MAX = atoi(optarg);
75  std::cout << "maximum number of events: " << MAX << std::endl;
76  break;
77  case 'd':
78  debug = true;
79  std::cout << "debug mode" << std::endl;
80  break;
81  case 'R':
82  if (selectType != 4) {
83  std::cout << "Random seed is only needed for select type 4"
84  << std::endl;
85  abort();
86  }
87  seed = atoi(optarg);
88  std::cout << "random seed: " << seed << std::endl;
89  break;
90  case 'C':
91  HDDM_USE_COMPRESSION = true;
92  break;
93  case 'I':
95  break;
96  default:
97  break;
98  }
99  }
100  //___________________________________________________________________________________________
101 
102  if (INFILENAME == "" || OUTFILENAME == "" ||
103  (selectType < 1 || 7 < selectType))
104  {
105  Usage();
106  }
107 
108  // if selectType == 4, we need the random generator
109  rndm = new TRandom2(seed);
110 
111  // standard hddm
112  hddm_s::istream *istr_s;
113  hddm_s::ostream *ostr_s;
114  hddm_s::ostream *ostr_s_remainder;
115  // REST
116  hddm_r::istream *istr_r;
117  hddm_r::ostream *ostr_r;
118  hddm_r::ostream *ostr_r_remainder;
119 
120  ifstream ifs;
121  ofstream ofs;
122  ofstream ofs_remainder;
123 
124  if (HDDM_CLASS == "s") {
125  ifs.open(INFILENAME.c_str());
126  if (! ifs.is_open()) {
127  std::cout << " Error opening input file \"" << INFILENAME
128  << "\"!" << std::endl;
129  exit(-1);
130  }
131  istr_s = new hddm_s::istream(ifs);
132 
133  ofs.open(OUTFILENAME.c_str());
134  if (! ofs.is_open()) {
135  std::cout << " Error opening output file \"" << OUTFILENAME
136  << "\"!" << std::endl;
137  exit(-1);
138  }
139  ostr_s = new hddm_s::ostream(ofs);
140  if (HDDM_USE_COMPRESSION) {
141  ostr_s->setCompression(hddm_r::k_bz2_compression);
142  }
144  ostr_s->setIntegrityChecks(hddm_r::k_crc32_integrity);
145  }
146 
147  if (saveRemainder) {
148  ofs_remainder.open(OUTFILENAME_REMAINDER.c_str());
149  if (! ofs_remainder.is_open()) {
150  std::cout << " Error opening output file \"" << OUTFILENAME_REMAINDER
151  << "\"!" << std::endl;
152  exit(-1);
153  }
154  ostr_s_remainder = new hddm_s::ostream(ofs_remainder);
155  if (HDDM_USE_COMPRESSION) {
156  ostr_s_remainder->setCompression(hddm_r::k_bz2_compression);
157  }
159  ostr_s_remainder->setIntegrityChecks(hddm_r::k_crc32_integrity);
160  }
161  }
162  else {
163  ostr_s_remainder = NULL;
164  }
165 
166  // Make sure compiler doesn't give warnings
167  istr_r = NULL;
168  ostr_r = NULL;
169  ostr_r_remainder = NULL;
170  }
171 
172  else {
173  ifs.open(INFILENAME.c_str());
174  if (! ifs.is_open()) {
175  std::cout << " Error opening input file \"" << INFILENAME
176  << "\"!" << std::endl;
177  exit(-1);
178  }
179  istr_r = new hddm_r::istream(ifs);
180 
181  ofs.open(OUTFILENAME.c_str());
182  if (! ofs.is_open()) {
183  std::cout << " Error opening output file \"" << OUTFILENAME
184  << "\"!" << std::endl;
185  exit(-1);
186  }
187  ostr_r = new hddm_r::ostream(ofs);
188  if (HDDM_USE_COMPRESSION) {
189  ostr_r->setCompression(hddm_r::k_bz2_compression);
190  }
192  ostr_r->setIntegrityChecks(hddm_r::k_crc32_integrity);
193  }
194  if (saveRemainder) {
195  ofs_remainder.open(OUTFILENAME_REMAINDER.c_str());
196  if (! ofs_remainder.is_open()) {
197  std::cout << " Error opening output file \"" << OUTFILENAME_REMAINDER
198  << "\"!" << std::endl;
199  exit(-1);
200  }
201  ostr_r_remainder = new hddm_r::ostream(ofs_remainder);
202  if (HDDM_USE_COMPRESSION) {
203  ostr_r_remainder->setCompression(hddm_r::k_bz2_compression);
204  }
206  ostr_r_remainder->setIntegrityChecks(hddm_r::k_crc32_integrity);
207  }
208  }
209  else
210  ostr_r_remainder = NULL;
211 
212  // Make sure compiler doesn't give warnings
213  istr_s = NULL;
214  ostr_s = NULL;
215  ostr_s_remainder = NULL;
216  }
217 
218  // Loop over input files
219  unsigned int NEvents = 0;
220  unsigned int NEvents_read = 0;
221  time_t last_time = time(NULL);
222 
223  // Loop over all events in input
224  while (true && NEvents_read < MAX) {
225 
226  /////////////////////////////////////////////////////
227  // //
228  // At this stage we have the current event in //
229  // hddm_s, so we can choose our events with //
230  // any information that is contained. //
231  // //
232  /////////////////////////////////////////////////////
233 
234  if (HDDM_CLASS == "s") {
235  if (! ifs.good())
236  break;
237  hddm_s::HDDM record;
238  *istr_s >> record;
239  NEvents_read++;
240  if (debug)
241  std::cout << NEvents_read << std::endl;
242  if (selectEvent_s(selectType, record, NEvents_read, debug)) {
243  // Write this output event to file
244  *ostr_s << record;
245  NEvents++;
246  }
247  else if (saveRemainder)
248  *ostr_s_remainder << record;
249  }
250  else {
251  if (! ifs.good())
252  break;
253  hddm_r::HDDM record;
254  *istr_r >> record;
255  NEvents_read++;
256  if (debug)
257  std::cout << NEvents_read << std::endl;
258  hddm_r::ReconstructedPhysicsEvent &re =
259  record.getReconstructedPhysicsEvent();
260  int runno = re.getRunNo();
261  int eventno = re.getEventNo();
262  if (debug)
263  std::cout << runno << "\t" << eventno << std::endl;
264  if (selectEvent_r(selectType, record, NEvents_read, debug)) {
265  // Write this output event to file
266  *ostr_r << record;
267  NEvents++;
268  }
269  else if (saveRemainder)
270  *ostr_r_remainder << record;
271  }
272 
273  // Update ticker
274  time_t now = time(NULL);
275  if (now != last_time) {
276  std::cout << " " << NEvents_read << " events read ("
277  << NEvents << " event written) \r";
278  std::cout.flush();
279  last_time = now;
280  }
281 
282  if (QUIT)
283  break;
284  }
285 
286  if (HDDM_CLASS == "s") {
287  // Close input file
288  delete istr_s;
289  ifs.close();
290 
291  // Close output files
292  delete ostr_s;
293  ofs.close();
294  if (ofs_remainder.is_open()) {
295  delete ostr_s_remainder;
296  ofs_remainder.close();
297  }
298  }
299  else {
300  delete istr_r;
301  ifs.close();
302  delete ostr_r;
303  ofs.close();
304  if (ofs_remainder.is_open()) {
305  delete ostr_r_remainder;
306  ofs_remainder.close();
307  }
308  }
309 
310  std::cout << std::endl;
311  std::cout << " " << NEvents_read << " events read, "
312  << NEvents << " events written" << std::endl;
313 
314  return 0;
315 }
316 
317 //-----------
318 // Usage
319 //-----------
320 void Usage(void)
321 {
322  std::cout << std::endl << "Usage:" << std::endl;
323  std::cout << " hddm_select_events [-i Inputfile] [-o Outputfile]"
324  " [-r REST format] [-a save remainder events too]"
325  " [-s selection type] [-M maximum number of events]"
326  << std::endl;
327  std::cout << std::endl;
328  std::cout << "options:" << std::endl;
329  std::cout << " -i Inputfile Set input filename" << std::endl;
330  std::cout << " -o Outputfile Set output filename" << std::endl;
331  std::cout << " -r Input is REST format" << std::endl;
332  std::cout << " -a Save remainder events in a file too"
333  << std::endl;
334  std::cout << " -s selectType Set which type of cut to do"
335  << std::endl;
336  std::cout << " -M MAX Set maximum number of events"
337  << std::endl;
338  std::cout << " -C Enable compression in the output"
339  " hddm streams" << std::endl;
340  std::cout << " -I Enable data integrity checks in the"
341  " output hddm streams" << std::endl;
342  std::cout << std::endl;
343 
344  exit(0);
345 }
346 
347 //-----------------------------------------------------------------
348 // ctrlCHandle
349 //-----------------------------------------------------------------
350 void ctrlCHandle(int x)
351 {
352  QUIT++;
353  std::cerr << std::endl << "SIGINT received (" << QUIT << ")....."
354  << std::endl;
355 }
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define c
TRandom2 * rndm
bool selectEvent_r(int select_type, hddm_r::HDDM &record, int nevents, bool debug)
string HDDM_CLASS
Definition: hddm2root.cc:18
char * OUTFILENAME
unsigned int MAX
string OUTFILENAME_REMAINDER
int selectType
bool saveRemainder
int QUIT
bool selectEvent_s(int select_type, hddm_s::HDDM &record, int nevents, bool debug)
bool debug
bool HDDM_USE_COMPRESSION
string INFILENAME
bool HDDM_USE_INTEGRITY_CHECKS
void ctrlCHandle(int x)
void Usage(JApplication &app)
Definition: hd_ana.cc:33
int seed
int main(int argc, char *argv[])
Definition: gendoc.cc:6