/* * * Libraries for reading Tektronix internal data format .isf from ROOT * for DPO40XX * * Usage: * #include "isf2RootDPO.cc" * * // In your main program * ISFDPO isf; * isf.open(isfFile); * isf.getPreamble(); * isf.storePreamble(); OR isf.storePreamble(preambleFile); * isf.getCurveData(); * // Then, you can use the following variables: * int isf.nPoints; * float isf.xp[]; * float isf.yp[]; * * To make sample executable * g++ -DISF2ROOTEXAMPLE -o isf2RootDPO_exe isf2RootDPO.cc * * http://hep-www.px.tsukuba.ac.jp/~yuji/myprod/isf2RootDPO.cc * Ver 0.00 2013/5/14 Y. Takeuchi * Ver 0.01 2013/8/22 Y. Takeuchi (minor bug fix) * Ver 0.02 2013/9/05 Y. Takeuchi (minor bug fix) * */ #ifndef ISF2ROOTDPO_CC #define ISF2ROOTDPO_CC #include #include #include #include #include using namespace std; // global variables for getopt extern char *optarg; extern int optind, opterr; // // class // class ISFDPO { public: // // preamble // int BYT_N; // 2 int BIT_N; // 16 char ENC[1024]; // BIN char BN_F[1024]; // RI char BYT_O[1024]; // MSB char WFI[1024]; // "Ch1, DC coupling, 20.00uV/div, 4.000us/div, 100000 points, Sample mode" int NR_P; // 100000 char PT_F[1024]; // Y char XUN[1024]; // "s" double XIN; // 400.0000E-12 double XZE; // -20.0000E-6 int PT_O; // 0 char YUN[1024]; // "V" double YMU; // 3.1250E-9 double YOF; // -1.0240E+3 double YZE; // 0.0E+0 double VSCALE; // 20.0000E-6 double HSCALE; // 2.0000E-6 double VPOS; // -160.0000E-3 double VOFFSET; // 0.0E+0 double HDELAY; // 0.0E+0 int Data_length; int nPoints; // actual # of points // // variables // bool quiet; // char sep; // =','; char preamble0[4096], preamble[4096]; ofstream* pref; // preamble output file ifstream isfp; float* xp, * yp; // data point // // constructor // ISFDPO() { quiet = false; sep=','; pref = NULL; xp=yp=NULL; } ~ISFDPO() { delete[] xp; delete[] yp; } int open(char* isfFile); int getPreamble(); int storePreamble(char* pFile = NULL); int getCurveData(); int dumpCSV(char* csvFile); }; ///////////////////////////////////////////////////////// // // functions // ///////////////////////////////////////////////////////// // // open input isf file // return 0 if success, 1 if fail // int ISFDPO::open(char* isfFile) { isfp.open(isfFile, ios::binary); if (isfp.fail()) { cerr << isfFile << ": Not found." << endl; return 1; } return 0; } // // get preamble // return 0 : success // 1,2 : No header // int ISFDPO::getPreamble() { // check header char buff[1024]; isfp.read(buff, 6); buff[6]=0; if (strcmp(buff, ":WFMP:") !=0) { cerr << "Fatal: Doesn't start from [:WFMP:] ." << endl; return 1; } // skip to next :WFMP: int cpre0=0; char ch; while (1) { isfp.read(&ch, 1); if (ch == ':') break; preamble0[cpre0++] = ch; } // check header of next WFMP: isfp.read(buff, 5); buff[5]=0; if (strncmp(buff, "WFMP:", 5) !=0) { cerr << "Fatal: Can't find the next [:WFMP:] ." << endl; return 2; } // // get preamble // int cpre=0; while (1) { isfp.read(&ch, 1); if (ch == ':') break; preamble[cpre++] = ch; } preamble[cpre]=0; if (!quiet) cout << preamble << endl; return 0; } // // storePreamble // 0: success // 1: Fail to open preamble dump file. // int ISFDPO::storePreamble(char* pFile) { // check file to dump preamble if (pFile) { pref = new ofstream(pFile); if (pref->fail()) { cerr << "Fail to open: " << pFile << endl; return 1; } } // if pFile char pre1[1024]; int pnt=0; for(int i=0;; ++i) { char ch=preamble[i]; if (ch == 0) break; if (ch == ';') { pre1[pnt] = 0; if (!quiet) cout << pre1 << endl; // analysis pre1 int d; float f; char buf[1024]; if (sscanf(pre1, "BYT_N %d", &d)) { // Byte # BYT_N = d; if (pref) *pref << "BYT_N = " << BYT_N << endl; } if (sscanf(pre1, "BIT_N %d", &d)) { // BIT # BIT_N = d; if (pref) *pref << "BIT_N = " << BIT_N << endl; } if (sscanf(pre1, "ENC %s", buf)) { // Encoding? BIN strcpy(ENC, buf); if (pref) *pref << "ENC = " << ENC << endl; } if (sscanf(pre1, "BN_F %s", buf)) { // BN?_Format RI strcpy(BN_F, buf); if (pref) *pref << "BN_F = " << BN_F << endl; } if (sscanf(pre1, "BYT_O %s", buf)) { // Byte order MSB strcpy(BYT_O, buf); if (pref) *pref << "BYT_O = " << BYT_O << endl; } if (sscanf(pre1, "WFI \"%[^\n\"]", buf)) { // WFID? strcpy(WFI, buf); if (pref) *pref << "WFI = " << WFI << endl; } if (sscanf(pre1, "NR_P %d", &d)) { // # of Record point NR_P = d; if (pref) *pref << "NR_P = " << NR_P << endl; } if (sscanf(pre1, "PT_F %s", buf)) { // PT_Format? strcpy(PT_F, buf); if (pref) *pref << "PT_F = " << PT_F << endl; } if (sscanf(pre1, "XUN \"%[^\n\"]", buf)) { // Xunit strcpy(XUN, buf); if (pref) *pref << "XUN = " << XUN << endl; } if (sscanf(pre1, "XIN %f", &f)) { // X increment XIN = f; if (pref) *pref << "XIN = " << XIN << endl; } if (sscanf(pre1, "XZE %f", &f)) { // X zero XZE = f; if (pref) *pref << "XZE = " << XZE << endl; } if (sscanf(pre1, "PT_O %d", &d)) { // Point offset PT_O = d; if (pref) *pref << "PT_O = " << PT_O << endl; } if (sscanf(pre1, "YUN \"%[^\n\"]", buf)) { // Y unit strcpy(YUN, buf); if (pref) *pref << "YUN = " << YUN << endl; } if (sscanf(pre1, "YMU %f", &f)) { // Y multiplification? YMU = f; if (pref) *pref << "YMU = " << YMU << endl; } if (sscanf(pre1, "YOF %f", &f)) { // Y offset YOF = f; if (pref) *pref << "YOF = " << YOF << endl; } if (sscanf(pre1, "YZE %f", &f)) { // Y Zero YZE = f; if (pref) *pref << "YZE = " << YZE << endl; } if (sscanf(pre1, "VSCALE %f", &f)) { // V scale VSCALE = f; if (pref) *pref << "VSCALE = " << VSCALE << endl; } if (sscanf(pre1, "HSCALE %f", &f)) { // H scale HSCALE = f; if (pref) *pref << "HSCALE = " << HSCALE << endl; } if (sscanf(pre1, "VPOS %f", &f)) { // V scale VPOS = f; if (pref) *pref << "VPOS = " << VPOS << endl; } if (sscanf(pre1, "VOFFSET %f", &f)) { // V scale VOFFSET = f; if (pref) *pref << "VOFFSET = " << VOFFSET << endl; } if (sscanf(pre1, "HDELAY %f", &f)) { // V scale HDELAY = f; if (pref) *pref << "HDELAY = " << HDELAY << endl; } // end of pre1 analysis pnt=0; continue; } // if (ch==';') pre1[pnt++]=ch; } // for i if (pref) pref->close(); return 0; } // // getCurveData // 0: Success // 1: header error // 2: Unsupported data // int ISFDPO::getCurveData() { // header check char buff[1024]; isfp.read(buff, 6); buff[6]=0; if (strncmp(buff, "CURV #", 6) !=0) { cerr << "Fatal: Curve section doesn't start from [CURV #] ." << endl; return 1; } // get Data Length char ch; isfp.read(&ch, 1); int dlLen=ch-'0'; isfp.read(buff, dlLen); buff[dlLen]=0; Data_length=atoi(buff); cout << "Data length:" << Data_length << endl; ////// currently only cope with BYT_NR=2 ////// if (BYT_N !=2) { cerr << "Sorry, this supports data only with BYT_N=2" << endl; return 2; } int np=Data_length/BYT_N; xp = new float[np]; yp = new float[np]; // // loop over point // int i=0; for(; ifail()) { cerr << "Fail to open: " << csvFile << endl; return 1; } cout << "Dump " << nPoints << " points into " << csvFile << endl; for(int i=0; iclose(); return 0; } #ifdef ISF2ROOTEXAMPLE /////////////////////////////////////////////////////////////////// // // main // /////////////////////////////////////////////////////////////////// // // help // void showUsage(char* com) { std::cout << com << " ver.0.00" << endl << "Usage: " << com << " [-h] [-q] [-p preamble] [-d]" << " isf_file csv_file"<< endl << endl << "Options:" << endl << " -h ... show this help" << endl << " -q ... don't print preamble" << endl << " -p preamble ... output preamble" << endl << " -d 0|1|2 ... Separator 0=,(default), 1=space, 2=tab" << endl; exit(1); } main(int argc, char** argv) { ISFDPO isf; //////////////// // option/argument analysis //////////////// char* idump = NULL; char ch; int sepNo; while ((ch = getopt(argc, argv, "hqp:d:")) != -1){ switch (ch){ case 'h': showUsage(argv[0]); case 'q': isf.quiet=true; break; case 'p': idump=optarg; break; case 'd': sepNo=atoi(optarg); if (sepNo==1) isf.sep=' '; if (sepNo==2) isf.sep='\t'; break; default: showUsage(argv[0]); } // switch } // while char* isfFile=argv[optind++], *csvFile=argv[optind++]; if (!isfFile || !csvFile) showUsage(argv[0]); if (idump) cout << "Preamble: " << idump << endl; cout << "isf: " << isfFile << endl; cout << "csv: " << csvFile << endl; // input ISF file open if (isf.open(isfFile)) exit(1); if (isf.getPreamble()) exit(1); if (isf.storePreamble(idump)) exit(1); if (isf.getCurveData()) exit(1); if (isf.dumpCSV(csvFile)) exit(1); } #endif // ifdef ISF2ROOTEXAMPLE #endif // ISF2ROOTDPO_CC //EOF