BOSS 7.1.2
BESIII Offline Software System
Loading...
Searching...
No Matches
evt_filter.cxx File Reference
#include "RawFile/RawFileWriter.h"
#include "RawFile/RawFileReader.h"
#include "IRawFile/RawFileExceptions.h"
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <unistd.h>
#include <mysql.h>

Go to the source code of this file.

Typedefs

typedef map< int, vector< uint32_t > > EvtRunMap
 

Functions

EvtRunMap getEvtRunMap (const char *fconf)
 
vector< string > getFnamesAtDB (int run)
 
vector< string > getFnamesAtBkk (int run)
 
void listFnames (const vector< string > &fnames)
 
void listNotFound (EvtRunMap &map)
 
int main (int argc, char *argv[])
 

Typedef Documentation

◆ EvtRunMap

typedef map<int, vector<uint32_t> > EvtRunMap

Definition at line 18 of file evt_filter.cxx.

Function Documentation

◆ getEvtRunMap()

EvtRunMap getEvtRunMap ( const char * fconf)

Definition at line 21 of file evt_filter.cxx.

21 {
22 EvtRunMap map;
23
24 int run;
25 uint32_t evtId;
26 ifstream cfs(fconf);
27
28 while ( !(cfs>>run>>evtId).eof() ) {
29 //std::cout << "run: " << run << " evt: " << evtId << std::endl;
30 map[run].push_back(evtId);
31 }
32
33 return map;
34}
map< int, vector< uint32_t > > EvtRunMap

Referenced by main().

◆ getFnamesAtBkk()

vector< string > getFnamesAtBkk ( int run)

Definition at line 87 of file evt_filter.cxx.

88{
89 xmlrpc_c::value result;
90 xmlrpc_c::clientSimple aClient;
91
92 try {
93 aClient.call( "http://bes3db.ihep.ac.cn:8080/bemp/xmlrpc",
94 "FileFinder.getFileByRun",
95 "siss",
96 &result,
97 "REAL",
98 run,
99 "Full",
100 "disk" );
101 }
102 catch (...) {
103 cerr << "Failed to lookup bookkeeping for run: " << run << endl;
104 exit(1);
105 }
106
107 vector<string> fnames;
108 vector<xmlrpc_c::value> vItems = ((xmlrpc_c::value_array)result).vectorValueValue();
109
110 for ( vector<xmlrpc_c::value>::iterator it = vItems.begin();
111 it != vItems.end(); ++it ) {
112 //xmlrpc_c::value_struct* item = (xmlrpc_c::value_struct*)(&(*it));
113 map<string, xmlrpc_c::value> item = (xmlrpc_c::value_struct)(*it);
114 string fname = (xmlrpc_c::value_string)(item["Replica"]);
115 fnames.push_back(fname);
116 }
117
118 if ( fnames.empty() ) {
119 cout << "No files found in Bookkeeping !!!" << endl;
120 exit(1);
121 }
122
123 return fnames;
124}

Referenced by main().

◆ getFnamesAtDB()

vector< string > getFnamesAtDB ( int run)

Definition at line 36 of file evt_filter.cxx.

37{
38 vector<string> filesPath;
39 MYSQL conn;
40 const char host[] = "bes3db2.ihep.ac.cn";
41 const char user[] = "guest";
42 const char passwd[] = "guestpass";
43 const char db[] = "run";
44 unsigned int port = 3306;
45 const char *unix_socket = NULL;
46 unsigned long client_flag = 0;
47
48 if (mysql_init(&conn) == NULL) {
49 std::cout << "init db error" << std::endl;
50 }
51 if (!mysql_real_connect(&conn,host, user, passwd, db, port, unix_socket, client_flag))
52 {
53 std::cout << "db link error" << std::endl;
54 }
55
56 char sql[400];
57 MYSQL_RES *result;
58 sprintf(sql, "select fileLocation from filesOnDisk where run=%d",run);
59 if (mysql_query(&conn, sql)||!(result = mysql_store_result(&conn))) {
60 std::cout << "Get no filesPath from db run= "<<run << std::endl;
61 }
62 //result = mysql_store_result(&conn);
63 if (result) {
64 MYSQL_ROW rows;
65 while (rows = mysql_fetch_row(result)) {
66 string res = rows[0];
67 int iLast = res.length() - 1;
68 if ( res.at(iLast) == '\n' ) {
69 res.erase(iLast, 1);
70 }
71 filesPath.push_back(res);
72 }
73 }
74
75 if(result!=NULL)
76 mysql_free_result(result);
77 mysql_close(&conn);
78
79 if ( filesPath.empty() ) {
80 cerr << "Failed to lookup database for run: " << run << endl;
81 exit(1);
82 }
83
84 return filesPath;
85}
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
struct st_mysql_res MYSQL_RES
struct st_mysql MYSQL
#define NULL

Referenced by main().

◆ listFnames()

void listFnames ( const vector< string > & fnames)

Definition at line 126 of file evt_filter.cxx.

127{
128 vector<string>::const_iterator it = fnames.begin();
129 for ( ; it != fnames.end(); ++it ) {
130 std::cout << (*it) << std::endl;
131 }
132}

◆ listNotFound()

void listNotFound ( EvtRunMap & map)

Definition at line 134 of file evt_filter.cxx.

135{
136 EvtRunMap::iterator it = map.begin();
137
138 while ( it != map.end() ) {
139 vector<uint32_t>::iterator iit = it->second.begin();
140
141 while ( iit != it->second.end() ) {
142 std::cout << "Event " << *iit << " not found in run " << it->first << std::endl;
143 ++iit;
144 }
145 ++it;
146 }
147}

Referenced by main().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 149 of file evt_filter.cxx.

150{
151 if ( argc != 3 ) {
152 cout << "Usage: " << endl
153 << " " << argv[0] << " EvtList.txt <data.raw|bookkeeping|database>" << endl
154 << endl
155 << "Parameters:" << endl
156 << " EvtList.txt: a text file with a runNumber and an eventNumber in each line" << endl
157 << " <event source>: must be one of the following 3" << endl
158 << " data.raw: the raw data file name to be filtered" << endl
159 << " bookkeeping: lookup raw data files automatically in Bookkeeping system" << endl
160 << " database: lookup raw data files automatically in Offline Database" << endl
161 << endl
162 << "For example:" << endl
163 << " $ raw_evt_filter.exe filter_run_8093.txt database" << std::endl;
164 exit(0);
165 }
166
167 if ( access( argv[1], F_OK ) < 0 ) {
168 std::cerr << "Invalid file: " << argv[1] << std::endl;
169 exit(1);
170 }
171 EvtRunMap map = getEvtRunMap(argv[1]);
172
173 int mode = 0;
174
175 if ( strcmp(argv[2], "bookkeeping") == 0 ) {
176 mode = 1;
177 }
178 else if ( strcmp(argv[2], "database") == 0 ) {
179 mode = 2;
180 }
181
182 string ofname = string(argv[1]) + ".raw";
183 RawFileWriter* pfw = new RawFileWriter(ofname.c_str());
184
185 const uint32_t* data;
186
187 if ( mode != 0 ) {
188
189 EvtRunMap::iterator it = map.begin();
190 while( it != map.end() ) {
191 //listFnames( getFnamesAtDB(it->first) );
192 RawFileReader* pfr = (mode==1)
193 ?
194 (new RawFileReader( getFnamesAtBkk(it->first) )) //bookkeeping
195 :
196 (new RawFileReader( getFnamesAtDB(it->first) )); //database
197 /*
198 * can be optimized by pfr->findEventById() when IDX exist!!!
199 */
200 while ( ! it->second.empty() ) {
201 try {
202 data = pfr->nextEvent();
203 }
204 catch ( RawFileException& e ) {
205 e.print();
206 break;
207 }
208
209 uint32_t evtId = data[8 + data[5]];
210 vector<uint32_t>::iterator iit = find(it->second.begin(), it->second.end(), evtId);
211
212 if ( iit != it->second.end() ) {
213 pfw->writeEvent(data);
214 it->second.erase(iit);
215 }
216 }
217
218 delete pfr;
219
220 ++it;
221 }
222 }
223 else {
224
225 RawFileReader* pfr = new RawFileReader( argv[2] );
226 while ( true ) {
227 try {
228 data = pfr->nextEvent();
229 }
230 catch ( RawFileException& e ) {
231 e.print();
232 break;
233 }
234
235 uint32_t evtId = data[8 + data[5]];
236 uint32_t runId = data[9 + data[5]];
237 EvtRunMap::iterator it = map.find( runId );
238 if ( it != map.end() ) {
239 vector<uint32_t>::iterator iit = find(it->second.begin(), it->second.end(), evtId);
240
241 if ( iit != it->second.end() ) {
242 pfw->writeEvent(data);
243 it->second.erase(iit);
244
245 if ( it->second.empty() ) {
246 map.erase( it );
247 if ( map.empty() ) {
248 break;
249 }
250 }
251 }
252 }
253 }
254
255 delete pfr;
256 }
257
258 delete pfw;
259
260 // list events NOT found
261 listNotFound(map);
262
263 return 0;
264}
TTree * data
virtual void print() const
const uint32_t * nextEvent()
int writeEvent(const uint32_t *pevt)
void listNotFound(EvtRunMap &map)
EvtRunMap getEvtRunMap(const char *fconf)
vector< string > getFnamesAtBkk(int run)
vector< string > getFnamesAtDB(int run)