Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hddm_cull_events/Process_r.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // Created Oct 10, 2013 David Lawrence
4 
5 #include "hddm_cull_events.h"
6 
7 #include <HDDM/hddm_r.hpp>
8 using namespace hddm_r;
9 
10 
11 // NOTE: The REST format files claim they are in a compressed format that
12 // the C API cannot handle so we use the C++ API here
13 
14 
15 //-----------
16 // Process_r -- HDDM REST format
17 //-----------
18 void Process_r(unsigned int &NEvents, unsigned int &NEvents_read)
19 {
20  // Output file
21  std::cout << " output file: " << OUTFILENAME << std::endl;
22  ofstream ofs(OUTFILENAME);
23  if (!ofs.is_open()) {
24  std::cout << " Error opening output file \"" << OUTFILENAME << "\"!" << std::endl;
25  exit(-1);
26  }
27 
28  hddm_r::ostream ostr(ofs);
30  std::cout << " Enabling bz2 compression of output HDDM file stream"
31  << std::endl;
32  ostr.setCompression(hddm_r::k_bz2_compression);
33  }
34  else {
35  std::cout << " HDDM compression disabled on output" << std::endl;
36  }
37 
39  std::cout << " Enabling data integrity check on output HDDM file stream"
40  << std::endl;
41  ostr.setIntegrityChecks(hddm_r::k_crc32_integrity);
42  }
43  else {
44  std::cout << " HDDM integrity checks disabled on output" << std::endl;
45  }
46 
47  // Loop over input files
48  time_t last_time = time(NULL);
49  for (unsigned int i=0; i<INFILENAMES.size(); i++) {
50  std::cout << " input file: " << INFILENAMES[i] << std::endl;
51 
52  // Open hddm file for reading
53  ifstream ifs(INFILENAMES[i]);
54 
55  // Associate input file stream with HDDM record
56  hddm_r::istream istr(ifs);
57 
58  // Loop over events
59  while (!ifs.eof() && ifs.good()) {
60  try{
61  HDDM xrec;
62  istr >> xrec;
63  NEvents_read++;
64 
65  bool write_this_event = false;
66 
67  // Loop over physics events within this event and see if one
68  // has the event number of interest
69  class ReconstructedPhysicsEvent &reconstructedPhysicsEvent = xrec.getReconstructedPhysicsEvent();
70  if (EVENT_TO_KEEP_MODE) { // need to check if reconstructedPhysicsEvent is valid!!
71  uint64_t eventNo = reconstructedPhysicsEvent.getEventNo();
72  if ((unsigned int)eventNo == SPECIFIC_EVENT_TO_KEEP) {
73  write_this_event = true;
74  QUIT = true;
75  }
76  }
77 
78  // Check if we're in the range of offsets to write out
79  if (NEvents_read>EVENTS_TO_SKIP)write_this_event = true;
80 
81  // Write this output event to file and free its memory
82  if (write_this_event) {
83  ostr << xrec;
84  NEvents++;
85  }
86 
87  // Update ticker
88  time_t now = time(NULL);
89  if (now != last_time) {
90  std::cout << " " << NEvents_read << " events read (" << NEvents << " event written) \r"; std::cout.flush();
91  last_time = now;
92  }
93 
94  // Quit as soon as we wrote all of the events we're going to
95  if (NEvents_read>=(EVENTS_TO_SKIP+EVENTS_TO_KEEP))break;
96 
97  if (QUIT)break;
98 
99 
100  }catch(...) {
101  break;
102  }
103  }
104  }
105 }
106 
unsigned int EVENTS_TO_KEEP
unsigned int SPECIFIC_EVENT_TO_KEEP
void Process_r(unsigned int &NEvents, unsigned int &NEvents_read)
char * OUTFILENAME
bool EVENT_TO_KEEP_MODE
int QUIT
bool HDDM_USE_COMPRESSION
bool HDDM_USE_INTEGRITY_CHECKS
vector< char * > INFILENAMES
unsigned int EVENTS_TO_SKIP