daq_bin2roto.c File Reference

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "OnlineUtil/msgLogLib/msgLog.h"
#include "OnlineUtil/rototalk.h"
#include "OnlineUtil/rotoMessages.h"

Go to the source code of this file.

Defines

#define MAXBUFFER   8388608

Functions

int send_whole_file (const char *fname, long *buffer, int bsize, int mxrec)
int config_autosave (const char *config)
int config_compress (const char *config)
int config_basketsize (const char *config)
int main (int argc, char **argv)

Define Documentation

#define MAXBUFFER   8388608

Definition at line 20 of file daq_bin2roto.c.


Function Documentation

int config_autosave ( const char *  config  ) 

Definition at line 342 of file daq_bin2roto.c.

References len, logInfo(), roto_send_autosave_config(), and rototalk_err_badconfig.

00343 {
00344    int nerr = 0;
00345    int len = strlen(config);
00346    char *wcopy = (char*)malloc(len+1);  /* a writable copy of config string */
00347    char stream[1024];
00348    const char *p1;
00349    char *p2;
00350    char achar;
00351    int nrec,nsec, nitems;
00352 
00353    /* make a copy, replacing all the commas with blanks*/
00354    p1 = config; p2 = wcopy;
00355    while (p2 < wcopy+len) {
00356       achar = *p1;
00357       if (achar != ',') *p2 = achar;
00358       else              *p2 = ' ';
00359       p1++; p2++;
00360    }
00361 
00362    p2 = wcopy;
00363    while (p2  < wcopy+len) {
00364       nitems = sscanf(p2,"%s %d %d",stream,&nrec,&nsec);
00365       if (nitems != 3) {
00366          logInfo("config_autosave: only %d items from \"%s\"\n",nitems,p2);
00367          nerr += rototalk_err_badconfig;
00368       }
00369       else 
00370          nerr += roto_send_autosave_config(stream,nrec,nsec);
00371 
00372       p2 = strchr(p2,';'); /* move to the semicolon         */
00373       if (!p2) break;      /* in case of no final semicolon */
00374       p2++;                /* start just beyond semicolon   */
00375    }
00376 
00377 
00378    free(wcopy);  /* return the allocated space */
00379    wcopy = 0;
00380 
00381    return nerr;
00382 }

int config_basketsize ( const char *  config  ) 

Definition at line 458 of file daq_bin2roto.c.

References len, logInfo(), roto_send_basketsize_config(), and rototalk_err_badconfig.

00459 {
00460    int nerr = 0;
00461    int len = strlen(config);
00462    char *wcopy = (char*)malloc(len+1);  /* a writable copy of config string */
00463    char stream[1024];
00464    const char *p1;
00465    char *p2;
00466    char achar;
00467    int bsize, nitems;
00468 
00469    /* make a copy, replacing all the commas with blanks*/
00470    p1 = config; p2 = wcopy;
00471    while (p2 < wcopy+len) {
00472       achar = *p1;
00473       if (achar != ',') *p2 = achar;
00474       else              *p2 = ' ';
00475       p1++; p2++;
00476    }
00477 
00478    p2 = wcopy;
00479    while (p2  < wcopy+len) {
00480       nitems = sscanf(p2,"%s %d",stream,&bsize);
00481       if (nitems != 2) {
00482          logInfo("config_basketsize: only %d items from \"%s\"\n",nitems,p2);
00483          nerr += rototalk_err_badconfig;
00484       }
00485       else 
00486          nerr += roto_send_basketsize_config(stream,bsize);
00487 
00488       p2 = strchr(p2,';'); /* move to the semicolon         */
00489       if (!p2) break;      /* in case of no final semicolon */
00490       p2++;                /* start just beyond semicolon   */
00491    }
00492 
00493 
00494    free(wcopy);  /* return the allocated space */
00495    wcopy = 0;
00496 
00497    return nerr;
00498 }

int config_compress ( const char *  config  ) 

Definition at line 400 of file daq_bin2roto.c.

References len, logInfo(), roto_send_compress_config(), and rototalk_err_badconfig.

00401 {
00402    int nerr = 0;
00403    int len = strlen(config);
00404    char *wcopy = (char*)malloc(len+1);  /* a writable copy of config string */
00405    char stream[1024];
00406    const char *p1;
00407    char *p2;
00408    char achar;
00409    int level, nitems;
00410 
00411    /* make a copy, replacing all the commas with blanks*/
00412    p1 = config; p2 = wcopy;
00413    while (p2 < wcopy+len) {
00414       achar = *p1;
00415       if (achar != ',') *p2 = achar;
00416       else              *p2 = ' ';
00417       p1++; p2++;
00418    }
00419 
00420    p2 = wcopy;
00421    while (p2  < wcopy+len) {
00422       nitems = sscanf(p2,"%s %d",stream,&level);
00423       if (nitems != 2) {
00424          logInfo("config_compress: only %d items from \"%s\"\n",nitems,p2);
00425          nerr += rototalk_err_badconfig;
00426       }
00427       else 
00428          nerr += roto_send_compress_config(stream,level);
00429 
00430       p2 = strchr(p2,';'); /* move to the semicolon         */
00431       if (!p2) break;      /* in case of no final semicolon */
00432       p2++;                /* start just beyond semicolon   */
00433    }
00434 
00435 
00436    free(wcopy);  /* return the allocated space */
00437    wcopy = 0;
00438 
00439    return nerr;
00440 }

int main ( int  argc,
char **  argv 
)

Definition at line 55 of file daq_bin2roto.c.

References config_autosave(), config_basketsize(), config_compress(), exit(), logDebugLevelSet(), logNotice(), MAXBUFFER, MINOS_ROOTER_BEAMMON, MINOS_ROOTER_DCP, MINOS_ROOTER_DCS, MINOS_ROOTER_UNIDENTIFIED_CLIENT, msgLogCleanup(), msgLogInit(), msgLogLocalEchoSet(), msgLogNodeIdSet(), port, producer(), roto_close_connection(), roto_open_connection(), roto_open_DCP_connection(), roto_set_connection_nodelay(), roto_verbose, and send_whole_file().

00056 {
00057 
00058    /* this is C ... all declarations must come first */
00059    const char* iphost = "localhost";
00060    const char* default_producer = "DCP";       /* default producer */
00061    const int   default_port     = 9011;        /* default port     */
00062    const char* producer = default_producer;
00063    const char* autosave_config = 0;
00064    const char* compress_config = 0;
00065    const char* basketsize_config = 0;
00066    int port = default_port;
00067    int whoami = MINOS_ROOTER_DCP;       
00068    int bsize = MAXBUFFER;
00069    long* buffer = 0;
00070    int nerr = 0;
00071    int mxrec = 0;
00072    int nodelay_flag = 1;  /* on by default */
00073    int echoMsgLog = 0;
00074    int copt;
00075 
00076    /*
00077     * parse the options and filenames
00078     * -i and -p take args
00079     */
00080    while ((copt = getopt(argc, argv, "i:b:c:C:B:w:p:ev:n:D:h")) != EOF) {
00081       switch (copt) {
00082       case 'i':  /* internet host */
00083          iphost = optarg;
00084          break;
00085       case 'b':  /* buffer size */
00086          bsize = atoi(optarg);
00087          break;
00088       case 'c':  /* autosave config string */
00089          autosave_config = optarg;
00090          break;
00091       case 'C':  /* compression config string */
00092          compress_config = optarg;
00093          break;
00094       case 'B':  /* basketsize config string */
00095          basketsize_config = optarg;
00096          break;
00097       case 'w':  /* whoami:  DCP, DCS, other */
00098          producer = optarg;
00099          break;
00100       case 'p':  /* port # */
00101          port = atoi(optarg);
00102          break;
00103       case 'e':  /* msgLog messages to stdout as well */
00104          echoMsgLog = 1;
00105          break;
00106       case 'v':  /* verbosity */
00107          roto_verbose = atoi(optarg);
00108          break;
00109       case 'n':  /* maximum number of records */
00110          mxrec = atoi(optarg);
00111          break;
00112       case 'D':  /* set nodelay_flag? */
00113          nodelay_flag = atoi(optarg);
00114          break;
00115       case 'h':  /* help */
00116          printf(" usage: %s -i<hostname> -b<buffer size> -w<whoami> -p<port #> <filenames..>\n", argv[0]);
00117          printf("   -i: hostname where rotorooter is running\n");
00118          printf("   -b: buffer size to use\n");
00119          printf("   -w: DCP or DCS\n");
00120          printf("   -p: port number rotorooter is listening on\n");
00121          printf("   -v: how verbose to be\n");
00122          printf("   -c: autosave config \"streamName,nrec,nsec[;streamName,nrec,nsec]\"\n");
00123          printf("   -C: compression config \"streamName,level[;streamName,level]\"\n");
00124          printf("   -B: basketsize config \"streamName,size[;streamName,size]\"\n");
00125 
00126          printf("   -n: maximum number of records from each file\n");
00127 
00128          printf("   -h: print this message\n");
00129          exit(1);
00130       default:
00131          printf(" unrecognized option '%c' ignored\n",(char)optopt);
00132          break;
00133       }
00134    }
00135 
00136    msgLogInit(argv[0]);
00137    msgLogNodeIdSet(whoami);
00138    msgLogLocalEchoSet(echoMsgLog);
00139    logDebugLevelSet(3);
00140    logNotice("starting %s",argv[0]);
00141 
00142    /* allocate a buffer to use */
00143    buffer = (long*) malloc(bsize);
00144    if (!buffer) {
00145       printf("failed to allocate buffer of size %d\n",bsize);
00146       exit(1);
00147    }
00148    if (roto_verbose>1) printf("allocated buffer of size %d\n",bsize);
00149 
00150    /* open a connection to the Rotorooter */
00151    if (0 == strcmp(producer,default_producer) && 
00152        port == default_port) {
00153       /* standard connection */
00154       if (roto_verbose>1) 
00155          printf("connect via roto_open_DCP_connection\n");
00156       nerr += roto_open_DCP_connection(iphost);
00157    } else {
00158       /* non-standard connection requested (either producer or port) */
00159       if (0 == strcmp(producer,"DCP")) {
00160          whoami  = MINOS_ROOTER_DCP;
00161       }
00162       else if (0 == strcmp(producer,"DCS")) {
00163          whoami = MINOS_ROOTER_DCS;
00164       }
00165       else if (0 == strcmp(producer,"BEAMMON")) {
00166          whoami = MINOS_ROOTER_BEAMMON;
00167       }
00168       else 
00169          whoami = MINOS_ROOTER_UNIDENTIFIED_CLIENT;
00170 
00171       if (roto_verbose>1) 
00172          printf("connect via roto_open_connection: port %d, whoami %s (0x%2.2x)\n",port,producer,whoami);
00173       nerr += roto_open_connection(iphost,port,whoami);
00174    }
00175    if (nerr) exit(nerr);
00176    
00177    nerr += roto_set_connection_nodelay(nodelay_flag);
00178 
00179    if (autosave_config) nerr += config_autosave(autosave_config);
00180    if (compress_config) nerr += config_compress(compress_config);
00181    if (basketsize_config) nerr += config_basketsize(basketsize_config);
00182 
00183    /* process each file in turn */
00184    while (optind < argc) {
00185       nerr += send_whole_file(argv[optind++],buffer,bsize,mxrec);
00186    }   
00187 
00188    /* close the connection */
00189    nerr += roto_close_connection();
00190 
00191    /* set my buffer free */
00192    free(buffer);
00193 
00194    logNotice("stopping %s",argv[0]);
00195    msgLogCleanup();
00196 
00197    exit(nerr);
00198 }

int send_whole_file ( const char *  fname,
long *  buffer,
int  bsize,
int  mxrec 
)

Definition at line 217 of file daq_bin2roto.c.

References roto_close_named_file(), roto_open_named_file(), roto_send_record(), and size.

Referenced by main().

00218 {
00219 
00220    FILE* file;
00221    long  size;
00222    int   nRecords=0;
00223    int   nbytes;
00224    const char* lastslash;
00225 #ifdef DUMPALL
00226    int   i;
00227 #endif
00228 
00229 
00230    char rootfname[1024];
00231    int nerr = 0;
00232 
00233    nerr = 0;
00234 
00235    /*
00236     * strip off any leading path
00237     */
00238    lastslash = strrchr(fname,'/');
00239    if (lastslash) ++lastslash;
00240    else           lastslash = fname;
00241    sprintf(rootfname,"%s",lastslash);
00242 
00243    file=fopen(fname,"rb");
00244    if (file==NULL) {
00245       printf("Failed to open input file: '%s'\n",fname);
00246       return 1;
00247    }
00248 
00249    /* 
00250     * WARNING, WARNING, WARNING!
00251     * this should be the ONLY place roto_open_file() is ever used
00252     * code in the DCP one should use
00253     *    roto_open_daqfile(int detector, int run, int subrun);
00254     * in order that multi-file capabilities are enabled
00255     */
00256    nerr += roto_open_named_file(rootfname);
00257    if (nerr) {
00258       printf("Rotorooter failed to open output file: '%s'\n",rootfname);
00259       return 1;
00260    }
00261 
00262 /*
00263  * NB These binary files are a temporary measure
00264  *    ROOT files will replace them soon
00265  *
00266  * Format is
00267  * [#bytes in record] [record data]
00268  * [#bytes in record] [record data]
00269  * and so on
00270  *
00271  * The record data contains the 'data' DAQ blocks
00272  * : TP singles summary
00273  * : Snarl header
00274  * : Crate Readout
00275  * - identify them by their block ID. 
00276  *
00277  * You may find others that are of no interest so branch on block ID
00278  *
00279  * See http://hepunx.rl.ac.uk/minos/daq/data-format/blocks/tp-singles.html
00280  * See http://hepunx.rl.ac.uk/minos/daq/data-format/blocks/snarl-header.html
00281  * See http://hepunx.rl.ac.uk/minos/daq/data-format/blocks/crate-readout.html
00282  *
00283  */
00284 
00285    while(!feof(file)){
00286       if (mxrec>0 && nRecords==mxrec) break;
00287       if (fread(&size,sizeof(long),1,file)==1) {
00288          nRecords++;
00289          nbytes = size-sizeof(long);  /* byte count is inclusive */
00290          if (nbytes>bsize) {
00291             printf("Record %d Size %d bytes too large for buffer (%d)\n",
00292                    nRecords,nbytes,bsize);
00293             printf("Skip the rest of '%s'\n",fname);
00294             break;
00295          }
00296 #ifdef DUMPALL
00297          printf("Record %d Size = %d bytes (0x%03x words follow)\n",
00298                 nRecords,size,(size-sizeof(long))/sizeof(long));
00299 #endif
00300          if (fread(buffer,nbytes,1,file)==1) {
00301 #ifdef DUMPALL
00302          /* this is the old "dump" code */
00303             for (i=0;i<(size-4)/sizeof(int);i++) {
00304                printf("0x%03x: 0x%08x\n",i,buffer[i]);
00305             }
00306 #endif
00307             /* send the record */
00308             nerr += roto_send_record(buffer,nbytes);
00309          }
00310       }
00311    }
00312 
00313    fclose(file);
00314 
00315    /* 
00316     * WARNING, WARNING, WARNING!
00317     * this should be the ONLY place roto_close_file() is ever used
00318     * code in the DCP one should use
00319     *    roto_close_daqfile(int detector, int run, int subrun);
00320     * in order that multi-file capabilities are enabled
00321     */
00322    nerr += roto_close_named_file(rootfname);
00323    return nerr;
00324 }


Generated on 16 Jan 2018 for loon by  doxygen 1.6.1