Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hdevio_sample.cc
Go to the documentation of this file.
1 
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <time.h>
6 
7 #include <iostream>
8 #include <string>
9 #include <vector>
10 #include <stack>
11 #include <thread>
12 using namespace std;
13 
14 #include <TFile.h>
15 
16 #include <DAQ/HDEVIO.h>
17 
18 
19 void Usage(string mess);
20 void ParseCommandLineArguments(int narg, char *argv[]);
21 
22 
23 vector<string> filenames;
24 
25 bool KEEP_BOR = true;
26 bool KEEP_EPICS = false;
27 bool KEEP_CODA = false;
28 string OUTPUT_FILENAME = "hdevio_pare.evio";
29  uint64_t PRESCALE = 100;
30 
31 //----------------
32 // main
33 //----------------
34 int main(int narg, char *argv[])
35 {
36 
37  ParseCommandLineArguments(narg, argv);
38 
39  // Open output file
40  ofstream ofs(OUTPUT_FILENAME);
41  if(!ofs.is_open()){
42  _DBG_ << "Unable to open " << OUTPUT_FILENAME << " for writing!" << endl;
43  exit(-1);
44  }
45 
46  // Create buffer for reading blocks into.
47  uint32_t *buff = new uint32_t[1000000];
48 
49  // Loop over input files
50  for(auto filename : filenames){
51 
52  // Open input file
53  HDEVIO *hdevio = new HDEVIO(filename);
54  if(!hdevio->is_open){
55  cout << hdevio->err_mess.str() << endl;
56  continue;
57  }
58 
59  // Get file map
60  vector<HDEVIO::EVIOBlockRecord> brs = hdevio->GetEVIOBlockRecords();
61 
62  // Have HDEVIO close file and open it ourselves
63  delete hdevio;
64  ifstream ifs(filename);
65 
66  // Loop over blocks
67  uint64_t idx = 0;
68  for(auto &br : brs){
69 
70  bool write_block = false;
71  switch(br.block_type){
72  case HDEVIO::kBT_BOR : if(KEEP_BOR ) write_block = true; break;
73  case HDEVIO::kBT_EPICS: if(KEEP_EPICS) write_block = true; break;
75  case HDEVIO::kBT_GO:
76  case HDEVIO::kBT_PAUSE:
77  case HDEVIO::kBT_END:
78  if(KEEP_CODA) write_block = true;
79  break;
81  default:
82  break;
83  }
84 
85  // Any events we're not keeping, prescale
86  if(!write_block){
87  write_block = ((idx++)%PRESCALE) == 0;
88  }
89 
90  if(write_block){
91  ifs.seekg(br.pos, ios::beg);
92  int nbytes = br.block_len*4;
93  ifs.read((char*)buff, nbytes);
94  if( ifs.gcount() != nbytes ){
95  _DBG_<<"Unable to read entire block (is file truncated?)" << endl;
96  }
97 
98  ofs.write((char*)buff, nbytes);
99  }
100  }
101 
102  ifs.close();
103  }
104 
105  ofs.close();
106  if(buff) delete[] buff;
107 
108  return 0;
109 }
110 
111 //----------------
112 // Usage
113 //----------------
114 void Usage(string mess="")
115 {
116  cout << endl;
117  cout << "Usage:" << endl;
118  cout << endl;
119  cout <<" hdevio_pare [options] file.evio [file2.evio ...]" << endl;
120  cout << endl;
121  cout << "options:" << endl;
122  cout << " -h, --help Print this usage statement" << endl;
123  cout << " -o file.evio Set name of EVIO ouput file" << endl;
124  cout << " -p prescale Prescale factor for EVIO events (not L1 trigger events)" << endl;
125  cout << " -bor Don't save BOR events" << endl;
126  cout << " -epics Don't save EPICS events" << endl;
127  cout << " -coda Don't save CODA control events" << endl;
128  cout << endl;
129 
130  if(mess != "") cout << endl << mess << endl << endl;
131 
132  exit(0);
133 }
134 
135 //----------------
136 // ParseCommandLineArguments
137 //----------------
138 void ParseCommandLineArguments(int narg, char *argv[])
139 {
140 
141  if(narg<2) Usage("You must supply a filename!");
142 
143  for(int i=1; i<narg; i++){
144  string arg = argv[i];
145  string next = (i+1)<narg ? argv[i+1]:"";
146 
147  if(arg == "-h" || arg == "--help") Usage();
148  else if(arg == "-o" ){ OUTPUT_FILENAME = next.c_str(); i++;}
149  else if(arg == "-p" ){ PRESCALE = atoi(next.c_str()); i++;}
150  else if(arg == "-bor" ){ KEEP_BOR = false;}
151  else if(arg == "-epics"){ KEEP_EPICS = false;}
152  else if(arg == "-coda" ){ KEEP_CODA = false;}
153  else if(arg[0] == '-') {cout << "Unknown option \""<<arg<<"\" !" << endl; exit(-1);}
154  else filenames.push_back(arg);
155  }
156 }
157 
string OUTPUT_FILENAME
Definition: hd_root.cc:17
uint64_t PRESCALE
TString filename
bool is_open
Definition: HDEVIO.h:166
bool KEEP_BOR
vector< EVIOBlockRecord > & GetEVIOBlockRecords(void)
Definition: HDEVIO.cc:723
stringstream err_mess
Definition: HDEVIO.h:191
void ParseCommandLineArguments(int &narg, char *argv[])
Definition: hd_dump.cc:124
#define _DBG_
Definition: HDEVIO.h:12
Definition: HDEVIO.h:45
bool KEEP_EPICS
bool KEEP_CODA
void Usage(JApplication &app)
Definition: hd_ana.cc:33
int main(int argc, char *argv[])
Definition: gendoc.cc:6
vector< string > filenames