diff --git a/examples/Makefile b/examples/Makefile index e62c778..5d2f0f5 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -14,7 +14,7 @@ LD := g++ LDFLAGS := -all: writeFile readFile readFileTags showFile schema writeUserHeader tupleFile readJson histograms +all: writeFile readFile readFileTags showFile schema writeUserHeader tupleFile readJson dataframe histograms: histograms.o $(CXX) -o histograms.exe $< $(HIPOLIBS) $(LZ4LIBS) @@ -25,6 +25,9 @@ readEvents: readEvents.o node: node.o $(CXX) -o node.exe $< $(HIPOLIBS) $(LZ4LIBS) +dataframe: dataframe.o + $(CXX) -o dataframe.exe $< $(HIPOLIBS) $(LZ4LIBS) + readHist: readHist.o $(CXX) -o readHist.exe $< $(HIPOLIBS) $(LZ4LIBS) diff --git a/examples/dataframe.cc b/examples/dataframe.cc new file mode 100644 index 0000000..710544b --- /dev/null +++ b/examples/dataframe.cc @@ -0,0 +1,69 @@ +//****************************************************************** +//* ██╗ ██╗██╗██████╗ ██████╗ ██╗ ██╗ ██████╗ +//* ██║ ██║██║██╔══██╗██╔═══██╗ ██║ ██║ ██╔═████╗ +//* ███████║██║██████╔╝██║ ██║ ███████║ ██║██╔██║ +//* ██╔══██║██║██╔═══╝ ██║ ██║ ╚════██║ ████╔╝██║ +//* ██║ ██║██║██║ ╚██████╔╝ ██║██╗╚██████╔╝ +//* ╚═╝ ╚═╝╚═╝╚═╝ ╚═════╝ ╚═╝╚═╝ ╚═════╝ +//************************ Jefferson National Lab (2017) *********** +//****************************************************************** +//* Example program for reading HIPO-4 file. +//* shows initial implementation of data frames style columnar data reading. +//* run the example with following command line: +//* ./dataframe.exe rec_clas_005197.evio.00100-00104.hipo REC::Particle px py pz chi2pid +//*-- +//* Author: G.Gavalian +//* + +#include +#include +#include "reader.h" +#include "twig.h" +#include "reaction.h" + + +int main(int argc, char** argv) { + + std::cout << " reading CLAS12 hipo file and plotting missing mass (ep->epi+pi-X) " << __cplusplus << std::endl; + //axis x(120,0.,1.0); + //printf("bin = %d\n",x.find(0.4)); + + char inputFile[256]; + char inputBank[256]; + char variable[256]; + + std::vector variables; + + if(argc>3) { + snprintf(inputFile,256,"%s",argv[1]); + snprintf(inputBank,256,"%s",argv[2]); + for(int k = 3; k < argc; k++){ + snprintf(variable,256,"%s",argv[k]); + variables.push_back(std::string(variable)); + } + //sprintf(outputFile,"%s",argv[2]); + } else { + std::cout << " *** please provide a file name..." << std::endl; + exit(1); + } + + printf("reading file : %s\n",inputFile); + printf("reading bank : %s\n",inputBank); + + for(int i = 0; i < (int) variables.size(); i++){ + printf("\t%6d : %s \n",i+1,variables[i].c_str()); + } + + hipo::reader r(inputFile); + + for(int k = 0; k < (int) variables.size(); k++){ + r.rewind(); + printf("printing vector [%s]\n",variables[k].c_str()); + std::vector vec = r.getFloat(inputBank,variables[k].c_str(), 100000); + for(int r = 0 ; r < (int) vec.size(); r++){ + printf(" row %6d = %f\n",r+1,vec[r]); + } + } + +} +//### END OF GENERATED CODE diff --git a/hipo4/bank.h b/hipo4/bank.h index 7322dc3..0baa70f 100644 --- a/hipo4/bank.h +++ b/hipo4/bank.h @@ -263,6 +263,11 @@ class iterator { float getFloat( int item, int index) const noexcept; double getDouble( int item, int index) const noexcept; long getLong( int item, int index) const noexcept; + + std::vector getInt( int item) const noexcept; + std::vector getFloat( int item) const noexcept; + std::vector getDouble( int item) const noexcept; + template T get(int item, int index) const noexcept { auto type = bankSchema.getEntryType(item); auto offset = bankSchema.getOffset(item, index, bankRows); @@ -280,11 +285,17 @@ class iterator { } int getInt( const char *name, int index) const noexcept; + int getShort( const char *name, int index) const noexcept; int getByte( const char *name, int index) const noexcept; float getFloat( const char *name, int index) const noexcept; double getDouble( const char *name, int index) const noexcept; long getLong( const char *name, int index) const noexcept; + + std::vector getInt( const char *name) const noexcept; + std::vector getFloat( const char *name) const noexcept; + std::vector getDouble( const char *name) const noexcept; + template T get(const char *name, int index) const noexcept { return get(bankSchema.getEntryOrder(name), index); } @@ -406,6 +417,25 @@ class iterator { return 0; } + inline std::vector bank::getInt(int item) const noexcept{ + int type = bankSchema.getEntryType(item); + + std::vector row; + int nrows = getRows(); + + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + switch(type){ + case kByte: row.push_back((int) getByteAt(offset)); break; + case kShort: row.push_back((int) getShortAt(offset)); break; + case kInt: row.push_back((int) getIntAt(offset)); break; + default: printf("---> error : requested INT for [%s] type = %d\n", + bankSchema.getEntryName(item).c_str(),type); break; + } + } + return row; + } + inline int bank::getShort(int item, int index) const noexcept{ int type = bankSchema.getEntryType(item); int offset = bankSchema.getOffset(item, index, bankRows); @@ -441,6 +471,24 @@ class iterator { return 0; } + inline std::vector bank::getInt(const char *name) const noexcept{ + int item = bankSchema.getEntryOrder(name); + int type = bankSchema.getEntryType(item); + std::vector row; + + int nrows = getRows(); + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + switch(type){ + case kByte: row.push_back((int) getByteAt(offset)); break; + case kShort: row.push_back((int) getShortAt(offset)); break; + case kInt: row.push_back((int) getIntAt(offset)); break; + default: printf("---> error : requested INT for [%s] type = %d\n",name,type); break; + } + } + return row; + } + inline int bank::getShort(const char *name, int index) const noexcept{ int item = bankSchema.getEntryOrder(name); int type = bankSchema.getEntryType(item); @@ -474,6 +522,30 @@ class iterator { return 0.0; } + inline std::vector bank::getFloat(const char *name) const noexcept{ + int item = bankSchema.getEntryOrder(name); + std::vector row; + int nrows = getRows(); + if(bankSchema.getEntryType(item)==kFloat){ + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + row.push_back( getFloatAt(offset)); + } + } + return row; + } + + inline std::vector bank::getFloat(int item) const noexcept{ + std::vector row; + int nrows = getRows(); + if(bankSchema.getEntryType(item)==kFloat){ + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + row.push_back( getFloatAt(offset)); + } + } + return row; + } inline double bank::getDouble(const char *name, int index) const noexcept{ int item = bankSchema.getEntryOrder(name); if(bankSchema.getEntryType(item)==kDouble){ @@ -487,6 +559,45 @@ class iterator { return 0.0; } + inline std::vector bank::getDouble(int item) const noexcept{ + std::vector row; + int nrows = getRows(); + + if(bankSchema.getEntryType(item)==kDouble){ + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + row.push_back(getDoubleAt(offset)); + } + } + if(bankSchema.getEntryType(item)==kFloat){ + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + row.push_back((double) getFloatAt(offset)); + } + } + return row; + } + + inline std::vector bank::getDouble(const char *name) const noexcept{ + std::vector row; + int nrows = getRows(); + int item = bankSchema.getEntryOrder(name); + + if(bankSchema.getEntryType(item)==kDouble){ + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + row.push_back(getDoubleAt(offset)); + } + } + if(bankSchema.getEntryType(item)==kFloat){ + for(int j = 0; j < nrows; j++){ + int offset = bankSchema.getOffset(item, j, bankRows); + row.push_back((double) getFloatAt(offset)); + } + } + return row; + } + inline long bank::getLong(const char *name, int index) const noexcept{ int item = bankSchema.getEntryOrder(name); if(bankSchema.getEntryType(item)==kLong){ diff --git a/hipo4/reader.cpp b/hipo4/reader.cpp index 3de6ce2..9d36d83 100644 --- a/hipo4/reader.cpp +++ b/hipo4/reader.cpp @@ -416,6 +416,37 @@ bool reader::loadRecord(int irec){ record.readRecord(inputStream,position,0); return true; } + +std::vector reader::getInt( const char *bank, const char *column, int max ){ + std::vector rowvec; + std::vector b = getBanks({bank}); + int item = b[0].getSchema().getEntryOrder(column); + int counter = 0; + while(next(b)==true){ + for(int row = 0 ; row < b[0].getRows();row++){ + rowvec.push_back(b[0].getInt(item,row)); + counter++; + } + if(max>0&&counter>max) break; + } + return rowvec; +} + +std::vector reader::getFloat(const char *bank, const char *column, int max ){ + std::vector rowvec; + std::vector b = getBanks({bank}); + int item = b[0].getSchema().getEntryOrder(column); + int counter = 0; + while(next(b)==true){ + for(int row = 0 ; row < b[0].getRows();row++){ + rowvec.push_back(b[0].getFloat(item,row)); + counter++; + } + if(max>0&&counter>max) break; + } + return rowvec; +} + //dglazier bool reader::nextInRecord(){ if(readerEventIndex.canAdvanceInRecord()==false) return false; @@ -514,6 +545,7 @@ bool readerIndex::gotoEvent(int eventNumber){ return true; } + void readerIndex::show(){ for(int i = 0; i < recordEvents.size(); i++){ printf("record = %8d, %8d\n",i,recordEvents[i]); diff --git a/hipo4/reader.h b/hipo4/reader.h index 7ddc9ea..dfa2150 100644 --- a/hipo4/reader.h +++ b/hipo4/reader.h @@ -242,6 +242,7 @@ namespace hipo { ~reader(); void about(); + void rewind(){ readerEventIndex.rewind();} void readDictionary(hipo::dictionary &dict); void getStructure(hipo::structure &structure,int group, int item); void getStructureNoCopy(hipo::structure &structure,int group, int item); @@ -272,6 +273,8 @@ namespace hipo { bool loadRecord(int irec); bool loadRecord(hipo::record &record, int irec); int getEntries(){return readerEventIndex.getMaxEvents();} + std::vector getInt( const char *bank, const char *column, int max = -1); + std::vector getFloat(const char *bank, const char *column, int max = -1); }; } #endif /* HIPOREADER_H */