Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hddm_cull_events/Process_s.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_s.hpp>
8 #include <iostream>
9 #include <fstream>
10 
11 #include <stdlib.h>
12 
13 //-----------
14 // Process_s -- HDDM simulation format
15 //-----------
16 void Process_s(unsigned int &NEvents, unsigned int &NEvents_read)
17 {
18  // Output file
19  std::ofstream *ofs = new std::ofstream(OUTFILENAME);
20  if (! ofs->is_open()) {
21  std::cout << " Error opening output file \"" << OUTFILENAME << "\"!"
22  << std::endl;
23  exit(-1);
24  }
25 
26  hddm_s::ostream *fout = new hddm_s::ostream(*ofs);
28  std::cout << " Enabling bz2 compression of output HDDM file stream"
29  << std::endl;
30  fout->setCompression(hddm_s::k_bz2_compression);
31  }
32  else {
33  std::cout << " HDDM compression disabled on output" << std::endl;
34  }
35 
37  std::cout << " Enabling data integrity check on output HDDM file stream"
38  << std::endl;
39  fout->setIntegrityChecks(hddm_s::k_crc32_integrity);
40  }
41  else {
42  std::cout << " HDDM integrity checks disabled on output" << std::endl;
43  }
44 
45  // Loop over input files
46  time_t last_time = time(NULL);
47  for (unsigned int i=0; i < INFILENAMES.size(); i++) {
48  std::cout << " input file: " << INFILENAMES[i] << std::endl;
49  std::ifstream *ifs = new std::ifstream(INFILENAMES[i]);
50  if (! ifs->is_open()) {
51  std::cout << " Error opening input file \"" << INFILENAMES[i]
52  << "\"!" << std::endl;
53  exit(-1);
54  }
55  hddm_s::istream *fin = new hddm_s::istream(*ifs);
56 
57  // Loop over all events in input
58  while (ifs->good()) {
59  hddm_s::HDDM record;
60  *fin >> record;
61  NEvents_read++;
62 
63  bool write_this_event = false;
64 
65  // Loop over physics events within this event and see if one
66  // has the event number of interest
67  if (EVENT_TO_KEEP_MODE) {
68  hddm_s::PhysicsEventList pes = record.getPhysicsEvents();
69  hddm_s::PhysicsEventList::iterator eviter;
70  for (eviter = pes.begin(); eviter != pes.end(); ++eviter) {
71  uint64_t eventNo = eviter->getEventNo();
72  if ((unsigned int)eventNo == SPECIFIC_EVENT_TO_KEEP) {
73  write_this_event = true;
74  QUIT = true;
75  }
76  }
77  }
78 
79  // Check if we're in the range of offsets to write out
80  if (NEvents_read > EVENTS_TO_SKIP)
81  write_this_event = true;
82 
83  // Write this output event to file and free its memory
84  if (write_this_event) {
85  *fout << record;
86  NEvents++;
87  }
88  record.clear();
89 
90  // Update ticker
91  time_t now = time(NULL);
92  if (now != last_time) {
93  std::cout << " " << NEvents_read << " events read ("
94  << NEvents << " event written) \r";
95  std::cout.flush();
96  last_time = now;
97  }
98 
99  // Quit as soon as we wrote all of the events we're going to
100  if (NEvents_read >= (EVENTS_TO_SKIP+EVENTS_TO_KEEP))
101  break;
102 
103  if (QUIT)break;
104  }
105 
106  // Close input file
107  delete fin;
108  delete ifs;
109  }
110 
111  // Close output file
112  delete fout;
113  delete ofs;
114 }
unsigned int EVENTS_TO_KEEP
void Process_s(unsigned int &NEvents, unsigned int &NEvents_read)
unsigned int SPECIFIC_EVENT_TO_KEEP
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