/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Event/RawFile/RawFile-00-00-10/test/evt_filter.cxx

Go to the documentation of this file.
00001 #include "RawFile/RawFileWriter.h"
00002 #include "RawFile/RawFileReader.h"
00003 #include "IRawFile/RawFileExceptions.h"
00004 #include <xmlrpc-c/base.hpp>
00005 #include <xmlrpc-c/client_simple.hpp>
00006 #include <iostream>
00007 #include <fstream>
00008 #include <algorithm>
00009 #include <vector>
00010 #include <map>
00011 #include <string>
00012 #include <unistd.h>
00013 
00014 using namespace std;
00015 
00016 typedef map<int, vector<uint32_t> >  EvtRunMap;
00017 
00018 
00019 EvtRunMap getEvtRunMap(const char* fconf) {
00020    EvtRunMap map;
00021 
00022    int      run;
00023    uint32_t evtId;
00024    ifstream cfs(fconf);
00025 
00026    while ( !(cfs>>run>>evtId).eof() ) {
00027       //std::cout << "run: " << run << "  evt: " << evtId << std::endl;
00028       map[run].push_back(evtId);
00029    }
00030 
00031    return map;
00032 }
00033 
00034 vector<string> getFnamesAtBkk(int run)
00035 {
00036    xmlrpc_c::value result;
00037    xmlrpc_c::clientSimple aClient;
00038 
00039    try {
00040       aClient.call( "http://bes3db.ihep.ac.cn:8080/bemp/xmlrpc",
00041                     "FileFinder.getFileByRun",
00042                     "siss",
00043                     &result,
00044                     "REAL",
00045                     run,
00046                     "Full",
00047                     "disk" );
00048    }
00049    catch (...) {
00050       cerr << "Failed to lookup the Bookkeeping Server !!!" << endl;
00051       exit(1);
00052    }
00053 
00054    vector<string> fnames;
00055    vector<xmlrpc_c::value> vItems = ((xmlrpc_c::value_array)result).vectorValueValue();
00056 
00057    for ( vector<xmlrpc_c::value>::iterator it = vItems.begin();
00058          it != vItems.end(); ++it ) {
00059       //xmlrpc_c::value_struct* item = (xmlrpc_c::value_struct*)(&(*it));
00060       map<string, xmlrpc_c::value> item = (xmlrpc_c::value_struct)(*it);
00061       string fname = (xmlrpc_c::value_string)(item["Replica"]);
00062       fnames.push_back(fname);
00063    }
00064 
00065    if ( fnames.empty() ) {
00066       cout << "No files found in Bookkeeping !!!" << endl;
00067       exit(1);
00068    }
00069 
00070    return fnames;
00071 }
00072 
00073 void listFnames(const vector<string>& fnames)
00074 {
00075    vector<string>::const_iterator it = fnames.begin();
00076    for ( ; it != fnames.end(); ++it ) {
00077       std::cout << (*it) << std::endl;
00078    }
00079 }
00080 
00081 void listNotFound(EvtRunMap& map)
00082 {
00083    EvtRunMap::iterator it = map.begin();
00084 
00085    while ( it != map.end() ) {
00086       vector<uint32_t>::iterator iit = it->second.begin();
00087 
00088       while ( iit != it->second.end() ) {
00089          std::cout << "Event " << *iit << " not found in run " << it->first << std::endl;
00090          ++iit;
00091       }
00092       ++it;
00093    }
00094 }
00095 
00096 int main(int argc, char* argv[])
00097 {
00098    if ( argc != 2 && argc != 3 ) {
00099       cout << "Usage: " << argv[0] << "  EvtList.txt  [data.raw]" << endl;
00100       exit(0);
00101    }
00102 
00103    if ( access( argv[1], F_OK ) < 0 ) {
00104       std::cerr << "Invalid file: " << argv[1] << std::endl;
00105       exit(1);
00106    }
00107    EvtRunMap map = getEvtRunMap(argv[1]);
00108 
00109    string ofname = string(argv[1]) + ".raw";
00110    RawFileWriter* pfw = new RawFileWriter(ofname.c_str());
00111 
00112    const uint32_t* data;
00113 
00114    if ( argc == 2 ) {
00115 
00116       EvtRunMap::iterator it = map.begin();
00117       while( it != map.end() ) {
00118          RawFileReader* pfr = new RawFileReader( getFnamesAtBkk(it->first) );
00119          /*
00120           * can be optimized by pfr->findEventById() when IDX exist!!!
00121           */
00122          while ( ! it->second.empty() ) {
00123             try {
00124                data = pfr->nextEvent();
00125             }
00126             catch ( RawFileException& e ) {
00127                e.print();
00128                break;
00129             }
00130 
00131             uint32_t evtId = data[8 + data[5]];
00132             vector<uint32_t>::iterator iit = find(it->second.begin(), it->second.end(), evtId);
00133 
00134             if ( iit != it->second.end() ) {
00135                pfw->writeEvent(data);
00136                it->second.erase(iit);
00137             }
00138          }
00139 
00140          delete pfr;
00141 
00142          ++it;
00143       }
00144    }
00145    else {
00146 
00147       RawFileReader* pfr = new RawFileReader( argv[2] );
00148       while ( true ) {
00149          try {
00150             data = pfr->nextEvent();
00151          }
00152          catch ( RawFileException& e ) {
00153             e.print();
00154             break;
00155          }
00156 
00157          uint32_t evtId = data[8 + data[5]];
00158          uint32_t runId = data[9 + data[5]];
00159          EvtRunMap::iterator it = map.find( runId );
00160          if ( it != map.end() ) {
00161             vector<uint32_t>::iterator iit = find(it->second.begin(), it->second.end(), evtId);
00162 
00163             if ( iit != it->second.end() ) {
00164                pfw->writeEvent(data);
00165                it->second.erase(iit);
00166 
00167                if ( it->second.empty() ) {
00168                   map.erase( it );
00169                   if ( map.empty() ) {
00170                      break;
00171                   }
00172                }
00173             }
00174          }
00175       }
00176 
00177       delete pfr;
00178    }
00179 
00180    delete pfw;
00181 
00182    // list events NOT found
00183    listNotFound(map);
00184 
00185    return 0;
00186 }

Generated on Tue Nov 29 22:58:36 2016 for BOSS_7.0.2 by  doxygen 1.4.7