/* * Time-stamp: <04/10/27 13:56:09 yuji> * Ver 0.0 by Yuji Takeuchi * mkntpl.C */ #include #include #include "TNtuple.h" #include "TFile.h" int strSplit(char* buff , char** adr , char rem); Bool_t mkntpl(char* tfname, int ncol) { char buff[1024], cbuff[1024], vartmp[4], vars[1024]; char* adr[99]; float var[99]; int line=0; // open txt data file ifstream txtfile(tfname); if (!txtfile) { cerr << "Can't open txt file." << endl; return kFALSE; } // open new root file TFile* hfile = new TFile("ntpl.root", "RECREATE"); if (!hfile) { cerr << "Fail to creat output root file." << endl; return kFALSE; } TNtuple* ntuple; while (txtfile.getline(buff, 1024)) { strcpy(cbuff, buff); int nelm=strSplit(cbuff, adr, ';'); // business in the 1st line if (line ==0 ) { cout << "1st line (" << nelm << " items):" << buff << endl; if (ncol==0) ncol=nelm; // if # of colmn is not specified // create ntuple vars[0]=0; for( int idx=0; idxFill(var); ++line; if (line%10000 == 0) { cout << '.'; cout.flush(); } } cout << endl << "total: " << line << " records" << endl; txtfile.close(); hfile->Write(); hfile->Close(); return kTRUE; } /* split line into elements, like awk * ver0.0 * */ int strSplit(char* buff , char** adr , char rem) { Int_t i , n_index; char ch, phase = 0; char* remadr; if ( rem !=0 && ( remadr = strchr( buff , rem ) ) != NULL ) *remadr = 0; i = n_index =0; // i : position in line buff while( 1 ) { ch = buff[i]; if ( strchr( " \t\n," , ch ) != NULL ) { if ( phase == 1) { buff[i] = 0; n_index++; phase = 2; // phase 1->2 } if ( ch == '\0' ) break; // end of line detection if ( ch == ',' ) { if (phase == 0 ) { buff[i] = 0; // case of phase 0->0 adr[n_index] = buff+i; n_index++; } phase = 0; // case of phase *->0 } } else if ( phase == 0 || phase == 2) { adr[ n_index ] = buff + i; phase = 1; // case of phase 0,2->1 } i++; } return n_index; } /* EOF */