Skip to content

Commit

Permalink
added functionality to read vectors from bank columns and to read ent…
Browse files Browse the repository at this point in the history
…ire column of one bank from the file, example is in dataframe.cc in examples directory
  • Loading branch information
gavalian committed Apr 3, 2024
1 parent 3c32f56 commit a8342ee
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
69 changes: 69 additions & 0 deletions examples/dataframe.cc
Original file line number Diff line number Diff line change
@@ -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 <cstdlib>
#include <iostream>
#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<std::string> 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<float> 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
111 changes: 111 additions & 0 deletions hipo4/bank.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> getInt( int item) const noexcept;
std::vector<float> getFloat( int item) const noexcept;
std::vector<double> getDouble( int item) const noexcept;

template<typename T = double> T get(int item, int index) const noexcept {
auto type = bankSchema.getEntryType(item);
auto offset = bankSchema.getOffset(item, index, bankRows);
Expand All @@ -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<int> getInt( const char *name) const noexcept;
std::vector<float> getFloat( const char *name) const noexcept;
std::vector<double> getDouble( const char *name) const noexcept;

template<typename T = double> T get(const char *name, int index) const noexcept {
return get<T>(bankSchema.getEntryOrder(name), index);
}
Expand Down Expand Up @@ -406,6 +417,25 @@ class iterator {
return 0;
}

inline std::vector<int> bank::getInt(int item) const noexcept{
int type = bankSchema.getEntryType(item);

std::vector<int> 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);
Expand Down Expand Up @@ -441,6 +471,24 @@ class iterator {
return 0;
}

inline std::vector<int> bank::getInt(const char *name) const noexcept{
int item = bankSchema.getEntryOrder(name);
int type = bankSchema.getEntryType(item);
std::vector<int> 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);
Expand Down Expand Up @@ -474,6 +522,30 @@ class iterator {
return 0.0;
}

inline std::vector<float> bank::getFloat(const char *name) const noexcept{
int item = bankSchema.getEntryOrder(name);
std::vector<float> 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<float> bank::getFloat(int item) const noexcept{
std::vector<float> 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){
Expand All @@ -487,6 +559,45 @@ class iterator {
return 0.0;
}

inline std::vector<double> bank::getDouble(int item) const noexcept{
std::vector<double> 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<double> bank::getDouble(const char *name) const noexcept{
std::vector<double> 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){
Expand Down
32 changes: 32 additions & 0 deletions hipo4/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,37 @@ bool reader::loadRecord(int irec){
record.readRecord(inputStream,position,0);
return true;
}

std::vector<int> reader::getInt( const char *bank, const char *column, int max ){
std::vector<int> rowvec;
std::vector<hipo::bank> 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<float> reader::getFloat(const char *bank, const char *column, int max ){
std::vector<float> rowvec;
std::vector<hipo::bank> 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;
Expand Down Expand Up @@ -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]);
Expand Down
3 changes: 3 additions & 0 deletions hipo4/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -272,6 +273,8 @@ namespace hipo {
bool loadRecord(int irec);
bool loadRecord(hipo::record &record, int irec);
int getEntries(){return readerEventIndex.getMaxEvents();}
std::vector<int> getInt( const char *bank, const char *column, int max = -1);
std::vector<float> getFloat(const char *bank, const char *column, int max = -1);
};
}
#endif /* HIPOREADER_H */

0 comments on commit a8342ee

Please sign in to comment.