HIPO  4.3.0
High Performance Output data format for experimental physics
utils.cpp
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 
11 #include "utils.h"
12 
13 
14 namespace hipo {
15 
18 
19  void utils::tokenize(const std::string& str, std::vector<std::string>& tokens,
20  const std::string& delimiters)
21  {
22  // Skip delimiters at beginning.
23  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
24  // Find first "non-delimiter".
25  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
26 
27 
28  while (std::string::npos != pos || std::string::npos != lastPos)
29  {
30  // Found a token, add it to the vector.
31  tokens.push_back(str.substr(lastPos, pos - lastPos));
32  // Skip delimiters. Note the "not_of"
33  lastPos = str.find_first_not_of(delimiters, pos);
34  // Find next "non-delimiter"
35  pos = str.find_first_of(delimiters, lastPos);
36  }
37  }
41  int utils::findposition(const std::string &str, const char *delim, int order){
42  int found = 0;
43  int position = 0;
44  std::string::size_type firstPos = str.find_first_of(delim,position);
45  if(firstPos==std::string::npos) return -1;
46  position = firstPos;
47  while(found!=order){
48  firstPos = str.find_first_of(delim,position+1);
49  if(firstPos==std::string::npos) return -1;
50  position = firstPos;
51  found++;
52  }
53  return position;
54  }
60  std::string utils::substring(const std::string &str,
61  const char *start_delim, const char *end_delim, int order){
62  int firstPos = hipo::utils::findposition(str,start_delim,order);
63  if(firstPos<0) return std::string();
64  std::string::size_type lastPos = str.find_first_of(end_delim,firstPos+1);
65  if(lastPos==std::string::npos) return std::string();
66  int length = lastPos - firstPos - 1;
67  return str.substr(firstPos+1, length);
68  }
69 
70  void utils::writeInt(char *buffer, int position, int value){
71  int *ptr = reinterpret_cast<int*>(&buffer[position]);
72  *ptr = value;
73  }
74 
75  void utils::writeLong( char *buffer, int position, long value){
76  int64_t *ptr = reinterpret_cast<int64_t*>(&buffer[position]);
77  *ptr = value;
78  }
79 
80  void utils::writeByte( char *buffer, int position, uint8_t value ){
81  int8_t *ptr = reinterpret_cast<int8_t*>(&buffer[position]);
82  *ptr = value;
83  }
84 
86  std::cout << "************************************************" << std::endl;
87  std::cout << "* >=< : --------------------- *" << std::endl;
88  std::cout << "* ,.--' ''-. : HIPO 4.0 I/O Library *" << std::endl;
89  std::cout << "* ( ) ',_.' : Jefferson National Lab *" << std::endl;
90  std::cout << "* Xx'xX : Date: 01/24/2019 *" << std::endl;
91  std::cout << "************************************************" << std::endl;
92  std::cout << std::endl;
93  }
94 
95  std::string utils::getHeader(){
96  std::string header;
97  header.append("//***********************************************************************\n");
98  header.append("//***********************************************************************\n");
99  header.append("// ____ ____ _____ _______ ___ ______ __ \n");
100  header.append("// |_ || _||_ _||_ __ \\ .' `. / ____ `. / | \n");
101  header.append("// | |__| | | | | |__) |/ .-. \\ `' __) | `| | \n");
102  header.append("// | __ | | | | ___/ | | | | _ |__ '. | | \n");
103  header.append("// _| | | |_ _| |_ _| |_ \\ `-' / | \\____) | _ _| |_ \n");
104  header.append("// |____||____||_____||_____| `.___.' \\______.'(_)|_____| \n");
105  header.append("// \n");
106  header.append("//======================================================================= \n");
107  header.append("// Autogenerated code by HIPO 3.1 io library\n");
108  header.append("// Modify the main loop to suite your needs\n");
109  header.append("// Date: \n");
110  header.append("//***********************************************************************\n");
111  return header;
112  }
113 
114  std::string utils::getFileHeader(){
115  std::string file_header;
116  std::string comments = hipo::utils::getHeader();
117  file_header.append(comments);
118  file_header.append("#include <cstdlib>\n#include <iostream>\n\n");
119  file_header.append("#include \"reader.h\"\n#include \"node.h\"\n");
120  file_header.append("\nint main(int argc, char** argv) {\n");
121  file_header.append(" std::cout << \" reading file example program (HIPO) \" << std::endl;\n");
122  file_header.append(" char inputFile[256];\n\n" );
123  file_header.append(" if(argc>1) {\n sprintf(inputFile,\"%s\",argv[1]);\n } else {\n " );
124  file_header.append(" std::cout << \" *** please provide a file name...\" << std::endl;\n" );
125  file_header.append(" exit(1);\n }\n\n");
126  file_header.append(" hipo::reader reader;\n");
127  file_header.append(" reader.open(inputFile);\n\n" );
128  return file_header;
129  }
130  std::string utils::getFileTrailer(const char *code){
131  std::string file_trailer;
132  file_trailer.append("\n");
133  file_trailer.append(" //----------------------------------------------------\n");
134  file_trailer.append(" //-- Main LOOP running through events and printing\n");
135  file_trailer.append(" //-- values of the first decalred branch\n");
136  file_trailer.append(" //----------------------------------------------------\n");
137  file_trailer.append(" int entry = 0;\n");
138  file_trailer.append(" while(reader.next()==true){\n");
139  file_trailer.append(" entry++;\n");
140  file_trailer.append(" std::cout << \"event # \" << entry << std::endl;\n");
141  file_trailer.append(code);
142  file_trailer.append(" }\n");
143  file_trailer.append(" //----------------------------------------------------\n");
144  file_trailer.append("}\n");
145  file_trailer.append("//###### ENF OF GENERATED FILE #######\n");
146  return file_trailer;
147  }
148  std::string utils::getSConstruct(){
149  std::string std__string;
150  std__string.append("#=================================================\n");
151  std__string.append("# The SCONSTRUCT file for building HIPO project.\n");
152  std__string.append("# \n");
153  std__string.append("#=================================================\n");
154  std__string.append("import glob\n");
155  std__string.append("import os\n");
156  std__string.append("import sys\n");
157  std__string.append("#=================================================\n");
158  std__string.append("# LOADING THE ENVIRONMENT\n");
159  std__string.append("#=================================================\n");
160  std__string.append("env = Environment(CPPPATH=[\"include\",\".\",\"/usr/include\",\"/usr/local/include\",\"/opt/local/include\",\"/group/clas12/packages/lz4/lib\",\"/group/clas12/packages/hipo-io/libcpp\"])\n");
161  std__string.append("env.Append(ENV = os.environ)\n");
162  std__string.append("env.Append(CPPPATH=[\"src/root\",\"src/evio\"])\n");
163  std__string.append("env.Append(CCFLAGS=[\"-O2\",\"-fPIC\",\"-m64\",\"-fmessage-length=0\",\"-g\"])\n");
164  std__string.append("env.Append(LIBPATH=[\"/opt/local/lib\",\"/usr/lib\",\"/usr/local/lib\",\"/group/clas12/packages/lz4/lib\",\"lib\",\"/group/clas12/packages/hipo-io/lib\"])\n");
165  std__string.append("env.Append(CONFIGUREDIR=[\"/group/clas12/packages/lz4/lib\",\"/group/clas12/packages/hipo-io/lib\"])\n");
166  std__string.append("#=================================================\n");
167  std__string.append("# Check for compression libraries.\n");
168  std__string.append("#=================================================\n");
169  std__string.append("conf = Configure(env)\n");
170  std__string.append("\n");
171  std__string.append("if conf.CheckLib('libhipo'):\n");
172  std__string.append(" print '\\n\\033[32m[**] >>>>> found library : HIPO'\n");
173  std__string.append(" print ''\n");
174  std__string.append(" env.Append(CCFLAGS=\"-D__HIPO__\")\n");
175  std__string.append(" \n");
176  std__string.append("if conf.CheckLib('liblz4'):\n");
177  std__string.append(" print '\\n\\033[32m[**] >>>>> found library : LZ4'\n");
178  std__string.append(" print '[**] >>>>> enabling lz4 compression. \\033[0m'\n");
179  std__string.append(" print ''\n");
180  std__string.append(" env.Append(CCFLAGS=\"-D__LZ4__\")\n");
181  std__string.append("\n");
182  std__string.append("if conf.CheckLib('libz'):\n");
183  std__string.append(" print '\\n\\033[32m[**] >>>>> found library : libz'\n");
184  std__string.append(" print '[**] >>>>> enabling gzip compression. \\033[0m'\n");
185  std__string.append(" print ''\n");
186  std__string.append(" env.Append(CCFLAGS=\"-D__LIBZ__\")\n");
187  std__string.append("#=================================================\n");
188  std__string.append("# BUILDING EXECUTABLE PROGRAM\n");
189  std__string.append("#=================================================\n");
190  std__string.append("runFileLoop = env.Program(target=\"runFileLoop\",source=[\"runFileLoop.cc\"])\n");
191  return std__string;
192  }
193 
194 
195 
197  first = clock.now();
198  //auto start = clock.now();
199  counter++;
200  }
201 
203  printf("[benchmark] %24s : time = %12.4f \n", benchmarkName.c_str(),getTimeSec());
204  }
205 
207 
208  second = clock.now();
209  std::chrono::nanoseconds diff_ms =
210  std::chrono::duration_cast< std::chrono::nanoseconds >( second-first );
211  //printf(" count = %lld\n",diff_ms.count());
212  running_time += diff_ms.count();
213  }
214 
216  return running_time;
217  }
218 
220  return running_time*1e-9;
221  }
222 
223  int benchmark::getCounter(){ return counter;}
224 }
int getCounter()
Returns the number of resume/pause cycles recorded.
Definition: utils.cpp:223
void resume()
Starts or resumes the timer.
Definition: utils.cpp:196
void pause()
Pauses the timer and accumulates elapsed time.
Definition: utils.cpp:206
double getTimeSec()
Returns the accumulated time in seconds.
Definition: utils.cpp:219
void show()
Prints the benchmark name, accumulated time, and counter.
Definition: utils.cpp:202
long getTime()
Returns the accumulated time in microseconds.
Definition: utils.cpp:215
static std::string getFileHeader()
Returns the file header template string.
Definition: utils.cpp:114
static void writeByte(char *buffer, int position, uint8_t value)
Writes a single byte into a byte buffer at the given position.
Definition: utils.cpp:80
static std::string substring(const std::string &str, const char *start_delim, const char *end_delim, int order)
Extracts a substring between two delimiters.
Definition: utils.cpp:60
static std::string getSConstruct()
Returns the SCons build configuration string.
Definition: utils.cpp:148
static void writeLong(char *buffer, int position, long value)
Writes a 64-bit long into a byte buffer at the given position.
Definition: utils.cpp:75
utils()
Default constructor.
Definition: utils.cpp:16
static std::string getFileTrailer(const char *code)
Returns the file trailer template string.
Definition: utils.cpp:130
static std::string getHeader()
Returns the HIPO header string for code generation.
Definition: utils.cpp:95
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
Splits a string into tokens using the given delimiters.
Definition: utils.cpp:19
static void printLogo()
Prints the HIPO library logo to standard output.
Definition: utils.cpp:85
static int findposition(const std::string &str, const char *delim, int order)
Finds the position of the nth occurrence of a delimiter.
Definition: utils.cpp:41
static void writeInt(char *buffer, int position, int value)
Writes a 32-bit integer into a byte buffer at the given position.
Definition: utils.cpp:70
~utils()
Destructor.
Definition: utils.cpp:17
Definition: bank.cpp:47
Utility functions and benchmark timer for HIPO library operations.