BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
evt_filter.cxx File Reference
#include "IRawFile/RawFileExceptions.h"
#include "RawFile/RawFileReader.h"
#include "RawFile/RawFileWriter.h"
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <mysql.h>
#include <string>
#include <unistd.h>
#include <vector>
#include <XmlRpcValue.h>
#include <XmlRpcClient.h>
#include <XmlRpcException.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 19 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 {
30 // std::cout << "run: " << run << " evt: " << evtId << std::endl;
31 map[run].push_back( evtId );
32 }
33
34 return map;
35}
map< int, vector< uint32_t > > EvtRunMap

Referenced by main().

◆ getFnamesAtBkk()

vector< string > getFnamesAtBkk ( int run)

Definition at line 82 of file evt_filter.cxx.

82 {
83 XmlRpc::XmlRpcValue params, result;
84 params.setSize( 4 );
85 params[0] = "REAL";
86 params[1] = run;
87 params[2] = "Full";
88 params[3] = "disk";
89
90 try
91 {
92 XmlRpc::XmlRpcClient aClient( "bes3db.ihep.ac.cn", 8080, "/bemp/xmlrpc" );
93 aClient.execute( "FileFinder.getFileByRun", params, result);
94 if ( aClient.isFault() ) {
95 throw XmlRpc::XmlRpcException( "Failed to lookup bookkeeping for run: " + run );
96 }
97 } catch ( XmlRpc::XmlRpcException& e )
98 {
99 cerr << e.getMessage() << endl;
100 exit( 1 );
101 }
102
103 vector<string> fnames;
104 if ( result.getType() == XmlRpc::XmlRpcValue::TypeArray ) {
105 for ( int i = 0; i < result.size(); ++i )
106 {
107 string fname = string( result[i]["Replica"] );
108 fnames.push_back( fname );
109 }
110 }
111
112 if ( fnames.empty() )
113 {
114 cout << "No files found in Bookkeeping !!!" << endl;
115 exit( 1 );
116 }
117
118 return fnames;
119}
A class to send XML RPC requests to a server and return the results.
const std::string & getMessage() const
Return the error message.
RPC method arguments and results are represented by Values.
Definition XmlRpcValue.h:22
int size() const
Return the size for string, base64, array, and struct values.
void setSize(int size)
Specify the size for array values. Array values will grow beyond this size if needed.
Type const & getType() const
Return the type of the value stored.

Referenced by main().

◆ getFnamesAtDB()

vector< string > getFnamesAtDB ( int run)

Definition at line 37 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 ) { std::cout << "init db error" << std::endl; }
49 if ( !mysql_real_connect( &conn, host, user, passwd, db, port, unix_socket, client_flag ) )
50 { std::cout << "db link error" << std::endl; }
51
52 char sql[400];
53 MYSQL_RES* result;
54 sprintf( sql, "select fileLocation from filesOnDisk where run=%d", run );
55 if ( mysql_query( &conn, sql ) || !( result = mysql_store_result( &conn ) ) )
56 { std::cout << "Get no filesPath from db run= " << run << std::endl; }
57 // result = mysql_store_result(&conn);
58 if ( result )
59 {
60 MYSQL_ROW rows;
61 while ( rows = mysql_fetch_row( result ) )
62 {
63 string res = rows[0];
64 int iLast = res.length() - 1;
65 if ( res.at( iLast ) == '\n' ) { res.erase( iLast, 1 ); }
66 filesPath.push_back( res );
67 }
68 }
69
70 if ( result != NULL ) mysql_free_result( result );
71 mysql_close( &conn );
72
73 if ( filesPath.empty() )
74 {
75 cerr << "Failed to lookup database for run: " << run << endl;
76 exit( 1 );
77 }
78
79 return filesPath;
80}
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)

Referenced by main().

◆ listFnames()

void listFnames ( const vector< string > & fnames)

Definition at line 121 of file evt_filter.cxx.

121 {
122 vector<string>::const_iterator it = fnames.begin();
123 for ( ; it != fnames.end(); ++it ) { std::cout << ( *it ) << std::endl; }
124}

◆ listNotFound()

void listNotFound ( EvtRunMap & map)

Definition at line 126 of file evt_filter.cxx.

126 {
127 EvtRunMap::iterator it = map.begin();
128
129 while ( it != map.end() )
130 {
131 vector<uint32_t>::iterator iit = it->second.begin();
132
133 while ( iit != it->second.end() )
134 {
135 std::cout << "Event " << *iit << " not found in run " << it->first << std::endl;
136 ++iit;
137 }
138 ++it;
139 }
140}

Referenced by main().

◆ main()

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

Definition at line 142 of file evt_filter.cxx.

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