/* * et2xml.cc * * dump ET evio buffer in xml * * based on et2xml.c Elliott Wolin, JLab, 13-apr-2004 * * ejw, 28-jun-2013 * * * Still to do: * Use async mode to get events * Select on control words */ /* misc macros, etc. */ //#define min(a, b) ( ( (a) > (b) ) ? (b) : (a) ) /* include files */ #include #include #include #include #include #include #include #include #include #include // for et #include // for evio #include #include using namespace std; using namespace evio; /* for et */ static string etName = "etName"; static string et_station_name = "et2xml"; static bool remote = false; static bool block = false; static int qsize = 1; //static int select_control[4] = {-1,-1,-1,-1}; static int select_control[4] = {0,0,0,0}; static et_sys_id et_system_id; static et_openconfig openconfig; static string et_filename; static et_att_id et_attach_id; static et_statconfig et_station_config; static et_stat_id et_station_id = -1; static et_event *et_event_ptr; static int et_control[ET_STATION_SELECT_INTS]; /* misc variables */ static string dictfilename; static int nbuffer = 0; static int skip_buffer = 0; static int max_buffer = 0; static bool no_buffer = false; static int ndumpmax = 100; static bool no_control = false; static int max_depth = 0; static bool xtod = false; static bool no_data = false; static bool debug = false; static bool done = false; static bool xpause = false; static bool verbose = false; static bool raw = false; static int nraw = 10; static int int_count = 0; static vector evok; static vector noev; static vector fragok; static vector nofrag; /* prototypes */ void decode_command_line(int argc, char **argv); void quit_callback(int sig); void connect_to_et(); void controldump(int *control, int nbuffer, string &s); bool user_event_select(uint32_t *buf, int *control); //-------------------------------------------------------------------------- int main(int argc, char **argv) { char s[256]; uint32_t *data; size_t databytelen; /* decode command line */ decode_command_line(argc,argv); // set dump parameters evioToStringConfig config; config.xtod=xtod; config.maxDepth=max_depth; config.verbose=verbose; config.noData=no_data; config.bankOk=fragok; config.noBank=nofrag; /* set signal handlers */ if(signal(SIGTERM,quit_callback)==SIG_ERR)printf("?unable to set TERM signal handler\n");; if(signal(SIGQUIT,quit_callback)==SIG_ERR)printf("?unable to set QUIT signal handler\n");; if(signal(SIGHUP,quit_callback)==SIG_ERR)printf("?unable to set HUP signal handler\n");; if(signal(SIGINT,quit_callback)==SIG_ERR)printf("?unable to set INT signal handler\n");; /* connect to et */ connect_to_et(); /* loop over buffers, perhaps skip some, dump up to max_buffer buffers */ nbuffer=0; while (!done&&(et_event_get(et_system_id,et_attach_id,&et_event_ptr,ET_SLEEP,NULL)==ET_OK)) { nbuffer++; if(skip_buffer>=nbuffer)continue; et_event_getcontrol(et_event_ptr,et_control); et_event_getdata(et_event_ptr,(void**)&data); et_event_getlength(et_event_ptr,&databytelen); if(!user_event_select(data,et_control))continue; if(!no_control) { string s; controldump(et_control,nbuffer,s); printf("%s",s.c_str()); } // dump event in XML if(raw) { cout << endl; for(int i=0; i=max_buffer+skip_buffer)&&(max_buffer!=0))break; } /* done */ et_station_detach(et_system_id,et_attach_id); et_station_remove(et_system_id,et_station_id); et_forcedclose(et_system_id); exit(EXIT_SUCCESS); } /*---------------------------------------------------------------- */ void quit_callback(int sig) { int_count++; if(int_count>=5)exit(EXIT_FAILURE); done=1; } /*---------------------------------------------------------------- */ void connect_to_et() { int status; sigset_t sigblock; /* create config */ et_open_config_init(&openconfig); if(remote)et_open_config_sethost(openconfig,ET_HOST_ANYWHERE); /* connect to et system */ status=et_open(&et_system_id,et_filename.c_str(),openconfig); if(status!=ET_OK) { printf( "?Unable to connect to et system\n"); exit(EXIT_FAILURE); } /* kill station if it exists */ if(et_station_exists(et_system_id,&et_station_id,et_station_name.c_str()))et_station_remove(et_system_id,et_station_id); /* create station */ et_station_config_init(&et_station_config); if(block) { et_station_config_setblock(et_station_config,ET_STATION_BLOCKING); } else { et_station_config_setblock(et_station_config,ET_STATION_NONBLOCKING); } et_station_config_setselect(et_station_config,ET_STATION_SELECT_MATCH); et_station_config_setselectwords(et_station_config,select_control); et_station_config_setuser(et_station_config,ET_STATION_USER_MULTI); et_station_config_setrestore(et_station_config,ET_STATION_RESTORE_OUT); et_station_config_setprescale(et_station_config,1); et_station_config_setcue(et_station_config,qsize); status=et_station_create(et_system_id,&et_station_id,et_station_name.c_str(),et_station_config); if(status!=ET_OK) { et_forcedclose(et_system_id); printf("?Unable to create station %s\n",et_station_name.c_str()); exit(EXIT_FAILURE); } /* block signals to THIS thread and any thread created by this thread */ /* needed to keep signals from et threads */ sigfillset(&sigblock); pthread_sigmask(SIG_BLOCK,&sigblock,NULL); /* attach to station */ status=et_station_attach(et_system_id,et_station_id,&et_attach_id); if(status!=ET_OK) { et_forcedclose(et_system_id); printf("?Unable to attach to station %s\n",et_station_name.c_str()); exit(EXIT_FAILURE); } /* unblock signals */ pthread_sigmask(SIG_UNBLOCK,&sigblock,NULL); return; } /*----------------------------------------------------------------*/ bool user_event_select(uint32_t *buf, int *control) { int event_tag = buf[1]>>16; if((evok.size()<=0)&&(noev.size()<=0)) { return(true); } else if(evok.size()>0) { return(find(evok.begin(),evok.end(),event_tag)!=evok.end()); } else if (noev.size()>0) { return(find(noev.begin(),noev.end(),event_tag)!=noev.end()); } return(true); } /*---------------------------------------------------------------- */ void controldump(int *control, int evnum, string &s) { // int i,j; // s+="\n\n\n\n",evnum,ET_STATION_SELECT_INTS; // for(i=0; i