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 MAP_WORDS = false; |
30 | string ROOT_FILENAME = "hdevio_scan.root"; |
31 | uint64_t MAX_EVIO_EVENTS = 20000; |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | int main(int narg, char *argv[]) |
38 | { |
39 | |
40 | ParseCommandLineArguments(narg, argv); |
41 | |
42 | if(PRINT_SUMMARY) PrintSummary(); |
43 | |
44 | if(MAP_WORDS ) MapEVIOWords(); |
45 | |
46 | return 0; |
47 | } |
48 | |
49 | |
50 | |
51 | |
52 | void Usage(string mess="") |
53 | { |
54 | cout << endl; |
55 | cout << "Usage:" << endl; |
56 | cout << endl; |
57 | cout <<" hdevio [options] file.evio [file2.evio ...]" << endl; |
58 | cout << endl; |
59 | cout << "options:" << endl; |
60 | cout << " -h, --help Print this usage statement" << endl; |
61 | cout << " -w Make histogram of population by word type" << endl; |
62 | cout << " -r file.root Set name of ROOT file to save histo to. " << endl; |
63 | cout << " (implies -w)" << endl; |
64 | cout << " -m max_events Max. EVIO events (not physics events) to process." << endl; |
65 | cout << endl; |
66 | |
67 | if(mess != "") cout << endl << mess << endl << endl; |
68 | |
69 | exit(0); |
70 | } |
71 | |
72 | |
73 | |
74 | |
75 | void ParseCommandLineArguments(int narg, char *argv[]) |
76 | { |
77 | |
78 | if(narg<2) Usage("You must supply a filename!"); |
79 | |
80 | for(int i=1; i<narg; i++){ |
81 | string arg = argv[i]; |
82 | string next = (i+1)<narg ? argv[i+1]:""; |
83 | |
84 | if(arg == "-h" || arg == "--help") Usage(); |
85 | else if(arg == "-w"){ MAP_WORDS = true; PRINT_SUMMARY = false; } |
86 | else if(arg == "-r"){ MAP_WORDS = true; PRINT_SUMMARY = false; ROOT_FILENAME = next; i++;} |
87 | else if(arg == "-m"){ MAX_EVIO_EVENTS = atoi(next.c_str()); i++;} |
88 | else if(arg[0] == '-') {cout << "Unknown option \""<<arg<<"\" !" << endl; exit(-1);} |
89 | else filenames.push_back(arg); |
90 | } |
91 | } |
92 | |
93 | |
94 | |
95 | |
96 | void PrintSummary(void) |
97 | { |
98 | |
99 | for(uint32_t i=0; i<filenames.size(); i++){ |
| 1 | Loop condition is true. Entering loop body | |
|
| 3 | | Loop condition is true. Entering loop body | |
|
| 5 | | Loop condition is true. Entering loop body | |
|
100 | string &filename = filenames[i]; |
101 | cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl; |
102 | |
103 | HDEVIO *hdevio = new HDEVIO(filename); |
| |
104 | if(!hdevio->is_open){ |
| |
| |
| |
105 | cout << hdevio->err_mess.str() << endl; |
| 8 | | Potential leak of memory pointed to by 'hdevio' |
|
106 | continue; |
107 | } |
108 | |
109 | time_t start_time = time(NULL__null); |
110 | hdevio->PrintFileSummary(); |
111 | time_t end_time = time(NULL__null); |
112 | |
113 | delete hdevio; |
114 | |
115 | cout << (end_time - start_time) << " sec " << endl; |
116 | } |
117 | } |
118 | |
119 | |
120 | |
121 | |
122 | void MapEVIOWords(void) |
123 | { |
124 | cout << endl; |
125 | cout << "Mapping will be limited to first " << MAX_EVIO_EVENTS << " events per input file" << endl; |
126 | |
127 | |
128 | TFile *rootfile = new TFile(ROOT_FILENAME.c_str(), "RECREATE"); |
129 | |
130 | DMapEVIOWords mapevio; |
131 | |
132 | |
133 | uint64_t Nevents = 0; |
134 | for(uint32_t i=0; i<filenames.size(); i++){ |
135 | |
136 | |
137 | string &filename = filenames[i]; |
138 | cout << "Processing file " << (i+1) << "/" << filenames.size() << " : " << filename << endl; |
139 | HDEVIO *hdevio = new HDEVIO(filename); |
140 | if(!hdevio->is_open){ |
141 | cout << hdevio->err_mess.str() << endl; |
142 | continue; |
143 | } |
144 | |
145 | |
146 | uint32_t buff_len = 1000; |
147 | uint32_t *buff = new uint32_t[buff_len]; |
148 | bool done = false; |
149 | while(!done){ |
150 | hdevio->readNoFileBuff(buff, buff_len); |
151 | switch(hdevio->err_code){ |
152 | case HDEVIO::HDEVIO_OK: |
153 | mapevio.ParseEvent(buff); |
154 | break; |
155 | case HDEVIO::HDEVIO_USER_BUFFER_TOO_SMALL: |
156 | buff_len = hdevio->last_event_len; |
157 | delete buff; |
158 | buff = new uint32_t[buff_len]; |
159 | break; |
160 | case HDEVIO::HDEVIO_EOF: |
161 | cout << endl << " end of file" << endl; |
162 | break; |
163 | default: |
164 | cout << endl; |
165 | cout << hdevio->err_mess.str() << endl; |
166 | done = true; |
167 | break; |
168 | |
169 | } |
170 | |
171 | if((++Nevents % 1000) == 0) { |
172 | int percent_done = (100*Nevents)/MAX_EVIO_EVENTS; |
173 | cout << " " << Nevents << "/" << MAX_EVIO_EVENTS << " (" << percent_done << "%) processed \r"; cout.flush(); |
174 | } |
175 | if(Nevents>MAX_EVIO_EVENTS) break; |
176 | } |
177 | cout << endl; |
178 | |
179 | |
180 | delete hdevio; |
181 | } |
182 | |
183 | |
184 | rootfile->Write(); |
185 | rootfile->Close(); |
186 | delete rootfile; |
187 | } |
188 | |
189 | |
190 | |