Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hdevio_scan.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 "DMapEVIOWords.h"
17 
18 #include <DAQ/HDEVIO.h>
19 
20 
21 void Usage(string mess);
22 void ParseCommandLineArguments(int narg, char *argv[]);
23 void PrintSummary(void);
24 void MapEVIOWords(void);
25 
26 
27 vector<string> filenames;
28 bool PRINT_SUMMARY = true;
29 bool SAVE_FILE_MAP = false;
30 bool SKIP_EVENT_MAPPING = false;
31 bool MAP_WORDS = false;
32 bool GENERATE_ERROR_REPORT = false;
33 string ROOT_FILENAME = "hdevio_scan.root";
34 string MAP_FILENAME = "";
35 uint64_t MAX_EVIO_EVENTS = 20000;
36 uint64_t SKIP_EVIO_EVENTS = 0;
37 uint32_t BLOCK_SIZE = 20; // used for daq_block_size histogram
38 uint32_t MAX_HISTORY_BUFF_SIZE = 400;
39 
40 //----------------
41 // main
42 //----------------
43 int main(int narg, char *argv[])
44 {
45 
46  ParseCommandLineArguments(narg, argv);
47 
49 
50  if(MAP_WORDS ) MapEVIOWords();
51 
52  return 0;
53 }
54 
55 //----------------
56 // Usage
57 //----------------
58 void Usage(string mess="")
59 {
60  cout << endl;
61  cout << "Usage:" << endl;
62  cout << endl;
63  cout <<" hdevio [options] file.evio [file2.evio ...]" << endl;
64  cout << endl;
65  cout << "options:" << endl;
66  cout << " -h, --help Print this usage statement" << endl;
67  cout << " -w Make histogram of population by word type" << endl;
68  cout << " -r file.root Set name of ROOT file to save histo to. " << endl;
69  cout << " (implies -w)" << endl;
70  cout << " -m max_events Max. EVIO events (not physics events) to process." << endl;
71  cout << " -i ignore_events Num. EVIO events (not physics events) to ignore at start." << endl;
72  cout << " -b block_size EVIO events to add for daq_block_size histo." << endl;
73  cout << " -e Write details of bad event tag location to file" << endl;
74  cout << " -n max_buff Max. events to keep timing info for (only valid)" << endl;
75  cout << " with -w option)" << endl;
76  cout << " -s Save file block/event map" << endl;
77  cout << " -blocksonly Save only block map not events. (Only use with -s)" << endl;
78  cout << " -f file.map Set name of file to save block/event to. " << endl;
79  cout << " (implies -s)" << endl;
80  cout << endl;
81  cout << "n.b. When using the -i (ignore) flag, the total number of events" << endl;
82  cout << " read in will be the sum of how many are ignored and the \"max\"" << endl;
83  cout << " events with only the last max_events being processed." << endl;
84  cout << endl;
85 
86  if(mess != "") cout << endl << mess << endl << endl;
87 
88  exit(0);
89 }
90 
91 //----------------
92 // ParseCommandLineArguments
93 //----------------
94 void ParseCommandLineArguments(int narg, char *argv[])
95 {
96 
97  if(narg<2) Usage("You must supply a filename!");
98 
99  for(int i=1; i<narg; i++){
100  string arg = argv[i];
101  string next = (i+1)<narg ? argv[i+1]:"";
102 
103  if(arg == "-h" || arg == "--help") Usage();
104  else if(arg == "-w"){ MAP_WORDS = true; PRINT_SUMMARY = false; }
105  else if(arg == "-r"){ MAP_WORDS = true; PRINT_SUMMARY = false; ROOT_FILENAME = next; i++;}
106  else if(arg == "-i"){ SKIP_EVIO_EVENTS = atoi(next.c_str()); i++;}
107  else if(arg == "-m"){ MAX_EVIO_EVENTS = atoi(next.c_str()); i++;}
108  else if(arg == "-b"){ BLOCK_SIZE = atoi(next.c_str()); i++;}
109  else if(arg == "-e"){ GENERATE_ERROR_REPORT = true; }
110  else if(arg == "-n"){ MAX_HISTORY_BUFF_SIZE = atoi(next.c_str()); i++;}
111  else if(arg == "-s"){ SAVE_FILE_MAP = true;}
112  else if(arg == "-f"){ SAVE_FILE_MAP = true; MAP_FILENAME = next; i++;}
113  else if(arg == "-blocksonly") { SKIP_EVENT_MAPPING = true;}
114  else if(arg[0] == '-') {cout << "Unknown option \""<<arg<<"\" !" << endl; exit(-1);}
115  else filenames.push_back(arg);
116  }
117 }
118 
119 //----------------
120 // PrintSummary
121 //----------------
122 void PrintSummary(void)
123 {
124  // Loop over input files
125  for(uint32_t i=0; i<filenames.size(); i++){
126  string &filename = filenames[i];
127  cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl;
128 
129  HDEVIO *hdevio = new HDEVIO(filename);
130  if(!hdevio->is_open){
131  cout << hdevio->err_mess.str() << endl;
132  continue;
133  }
134 
135  if(SKIP_EVENT_MAPPING) hdevio->SKIP_EVENT_MAPPING = true;
136 
137  time_t start_time = time(NULL);
138  hdevio->PrintFileSummary();
139  time_t end_time = time(NULL);
140 
142  ofstream ofs("hdevio_scan.err");
143  ofs << "#";
144  ofs << "# hdevio_scan report for " << filename << endl;
145  ofs << "#";
146  ofs << "# The following list is for each EVIO event that contained an" << endl;
147  ofs << "# unknown top-level bank tag. Since any corruption may have" << endl;
148  ofs << "# started in the previous event, locations for that are also" << endl;
149  ofs << "# provided (when available)." <<endl;
150  ofs << "# columns are:" << endl;
151  ofs << "# 1 block number in file (starting from 0)" << endl;
152  ofs << "# 2 block offset in file (hex)" <<endl;
153  ofs << "# 3 block number of previous event" << endl;
154  ofs << "# 4 block offset of previous event" <<endl;
155  ofs << "# 5 event number in block (starting from 1)" << endl;
156  ofs << "# 6 number of events in block" << endl;
157  ofs << "# 7 event offset in file (hex)" <<endl;
158  ofs << "# 8 event length (inclusive)" <<endl;
159  ofs << "# 9 event header (hex) <-- This value is what indicates a corrupt event" <<endl;
160  ofs << "# 10 event number of previous event" << endl;
161  ofs << "# 11 event offset of previous event" <<endl;
162  ofs << "# 12 event length of previous event" <<endl;
163  ofs << "# 13 event header of previous event" <<endl;
164  ofs << "#" << endl;
165  ofs << "# 1 2 3 4 5 6 7 8 9 10 11 12 13" <<endl;
166 
167  vector<HDEVIO::EVIOBlockRecord> brs = hdevio->GetEVIOBlockRecords();
168 
169  HDEVIO::EVIOBlockRecord *br_prev = NULL;
170  HDEVIO::EVIOEventRecord *er_prev = NULL;
171  int32_t ibr = 0;
172  for(HDEVIO::EVIOBlockRecord &br : brs){
173  int32_t ier = 1;
174  for(HDEVIO::EVIOEventRecord &er : br.evio_events){
176  char str[512];
177  // 1 2 3 4 5 6 7 8 9 10 11 12 13
178  sprintf(str, "%04u 0x%08x %04d 0x%08x %3u/%3u 0x%08x %10u 0x%08x %03d 0x%08x %10u 0x%08x"
179  /* 1 */ , ibr
180  /* 2 */ , (unsigned int)br.pos
181  /* 3 */ , ibr-1
182  /* 4 */ , br_prev!=NULL ? (unsigned int)br_prev->pos:0
183  /* 5 */ , ier
184  /* 6 */ , (unsigned int)br.evio_events.size()
185  /* 7 */ , (unsigned int)er.pos
186  /* 8 */ , (unsigned int)er.event_len
187  /* 9 */ , (unsigned int)er.event_header
188  /* 10 */ , ier-1
189  /* 11 */ , er_prev!=NULL ? (unsigned int)er_prev->pos:0
190  /* 12 */ , er_prev!=NULL ? (unsigned int)er_prev->event_len:0
191  /* 13 */ , er_prev!=NULL ? (unsigned int)er_prev->event_header:0);
192  ofs << str << endl;
193  }
194 
195  er_prev = &er;
196  ier++;
197  }
198 
199  br_prev = &br;
200  ibr++;
201  }
202 
203  ofs.close();
204  }
205 
207 
208  delete hdevio;
209 
210  cout << (end_time - start_time) << " sec " << endl;
211  }
212 }
213 
214 //----------------
215 // MapEVIOWords
216 //----------------
217 void MapEVIOWords(void)
218 {
219  cout << endl;
220  cout << "Mapping will be limited to first " << MAX_EVIO_EVENTS << " events per input file" << endl;
221 
222  // Open ROOT file
223  TFile *rootfile = new TFile(ROOT_FILENAME.c_str(), "RECREATE");
224 
225  DMapEVIOWords mapevio;
227 
228  // Loop over input files
229  uint64_t Nevents = 0;
230  for(uint32_t i=0; i<filenames.size(); i++){
231 
232  // Open EVIO file
233  string &filename = filenames[i];
234  cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl;
235  HDEVIO *hdevio = new HDEVIO(filename);
236  if(!hdevio->is_open){
237  cout << hdevio->err_mess.str() << endl;
238  continue;
239  }
240 
241  // Read all events in file
242  uint32_t buff_len = 1000;
243  uint32_t *buff = new uint32_t[buff_len];
244  bool done = false;
245  while(!done){
246  hdevio->readNoFileBuff(buff, buff_len);
247  switch(hdevio->err_code){
248  case HDEVIO::HDEVIO_OK:
249  if( Nevents>= SKIP_EVIO_EVENTS ) mapevio.ParseEvent(buff);
250  break;
252  buff_len = hdevio->last_event_len;
253  delete[] buff;
254  buff = new uint32_t[buff_len];
255  break;
256  case HDEVIO::HDEVIO_EOF:
257  cout << endl << " end of file" << endl;
258  done = true;
259  break;
260  default:
261  cout << endl;
262  cout << hdevio->err_mess.str() << endl;
263  done = true;
264  break;
265 
266  }
267 
268  if((++Nevents % 1000) == 0) {
269  int percent_done = (100*Nevents)/MAX_EVIO_EVENTS;
270  cout << " " << Nevents << "/" << MAX_EVIO_EVENTS << " (" << percent_done << "%) processed \r"; cout.flush();
271  }
272  if( Nevents > (SKIP_EVIO_EVENTS+MAX_EVIO_EVENTS) ) break;
273  }
274  cout << endl;
275 
276  // Close EVIO file
277  delete hdevio;
278  }
279 
280  // Flush and close ROOT file
281  rootfile->Write();
282  rootfile->Close();
283  delete rootfile;
284 }
285 
286 
287 
bool GENERATE_ERROR_REPORT
Definition: hdevio_scan.cc:32
BLOCKTYPE event_type
Definition: HDEVIO.h:150
uint32_t BLOCK_SIZE
Definition: hdevio_scan.cc:37
uint32_t err_code
Definition: HDEVIO.h:192
char str[256]
sprintf(text,"Post KinFit Cut")
TString filename
bool is_open
Definition: HDEVIO.h:166
bool SKIP_EVENT_MAPPING
Definition: HDEVIO.h:189
uint64_t MAX_EVIO_EVENTS
Definition: hdevio_scan.cc:35
void MapEVIOWords(void)
Definition: hdevio_scan.cc:217
vector< EVIOBlockRecord > & GetEVIOBlockRecords(void)
Definition: HDEVIO.cc:723
bool PRINT_SUMMARY
Definition: hdevio_scan.cc:28
uint32_t event_header
Definition: HDEVIO.h:147
stringstream err_mess
Definition: HDEVIO.h:191
uint32_t last_event_len
Definition: HDEVIO.h:184
void ParseCommandLineArguments(int &narg, char *argv[])
Definition: hd_dump.cc:124
Definition: HDEVIO.h:45
bool readNoFileBuff(uint32_t *user_buff, uint32_t user_buff_len, bool allow_swap=true)
Definition: HDEVIO.cc:499
void SaveFileMap(string fname="")
Definition: HDEVIO.cc:1362
uint32_t MAX_HISTORY_BUFF_SIZE
Definition: hdevio_scan.cc:38
string ROOT_FILENAME
Definition: hdevio_scan.cc:33
static double start_time
Definition: mc2coda.c:26
void PrintFileSummary(void)
Definition: HDEVIO.cc:1246
bool SAVE_FILE_MAP
Definition: hdevio_scan.cc:29
bool SKIP_EVENT_MAPPING
Definition: hdevio_scan.cc:30
double Nevents
uint64_t SKIP_EVIO_EVENTS
Definition: hdevio_scan.cc:36
string MAP_FILENAME
Definition: hdevio_scan.cc:34
bool MAP_WORDS
Definition: hdevio_scan.cc:31
void PrintSummary(void)
Definition: hdevio_scan.cc:122
uint32_t max_history_buff_size
void Usage(JApplication &app)
Definition: hd_ana.cc:33
int main(int argc, char *argv[])
Definition: gendoc.cc:6
vector< string > filenames