HIPO  4.3.0
High Performance Output data format for experimental physics
dictionary.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 "dictionary.h"
12 #include "utils.h"
13 #include <cstdlib>
14 
15 namespace hipo {
16 
17  void schema::parse(const std::string& schString){
18  std::vector<std::string> entries;
19  std::vector<std::string> entry;
20  hipo::utils::tokenize(schString, entries, ",");
21  int offset = 0;
22  for(int i = 0; i < entries.size(); i++){
23  entry.clear();
24  hipo::utils::tokenize(entries[i],entry, "/");
25  schemaEntry_t e;
26  e.name = hipo::utils::trim(entry[0]);
27  e.type = hipo::utils::trim(entry[1]);
28  e.typeId = getTypeByString(e.type);
29  e.typeSize = getTypeSize(e.typeId);
30  e.offset = offset;
31  offset += e.typeSize;
32  schemaEntries.push_back(e);
33  schemaEntriesMap[e.name] = i;
34  }
35  }
36 
37  int schema::getTypeByString(std::string &typeName){
38  if(typeName=="B"){
39  return kByte;
40  } else if(typeName=="S") {
41  return kShort;
42  } else if(typeName=="I") {
43  return kInt;
44  } else if(typeName=="F") {
45  return kFloat;
46  } else if(typeName=="D") {
47  return kDouble;
48  } else if(typeName=="L") {
49  return kLong;
50  }
51  return -1;
52  }
53 
54  int schema::getTypeSize(int id){
55  switch(id){
56  case kByte: return 1;
57  case kShort: return 2;
58  case kInt: return 4;
59  case kFloat: return 4;
60  case kDouble: return 8;
61  case kLong: return 8;
62  default: return 0;
63  }
64  return 0;
65  }
66 
67  void schema::show(){
68  printf("schema : %14s , group = %6d, item = %3d\n",
69  schemaName.c_str(),groupid,itemid);
70  for(auto & schemaEntrie : schemaEntries){
71  printf("%16s : (%3s) %5d %5d , offset = %3d --> [%s]\n",
72  schemaEntrie.name.c_str(),schemaEntrie.type.c_str(),
73  schemaEntrie.typeId,schemaEntrie.typeSize, schemaEntrie.offset,
74  schemaEntrie.name.c_str()
75  );
76  }
77  }
78 
79 
80  int schema::getSizeForRows(int rows){
81  int nentries = schemaEntries.size();
82  int offset = getOffset(nentries-1,rows-1,rows) + schemaEntries[nentries-1].typeSize;
83  return offset;
84  }
85 
86  std::string schema::getSchemaString(){
87  char parts[256];
88  std::string result;
89  snprintf(parts,256,"{%s/%d/%d}{",schemaName.c_str(),groupid,itemid);
90  result.append(parts);
91  for(int loop = 0; loop < schemaEntries.size(); loop++){
92  snprintf(parts,256,"%s/%s",schemaEntries[loop].name.c_str(), schemaEntries[loop].type.c_str());
93  if(loop!=0) result.append(",");
94  result.append(parts);
95  }
96  result.append("}");
97  return result;
98  }
99 
101  char parts[256];
102  std::string result;
103  snprintf(parts,256,"{ \"name\": \"%s\", \"group\": %d, \"item\": %d, \"info\": \" \",",
104  schemaName.c_str(),groupid,itemid);
105  result.append(parts);
106  result.append("\"entries\": [ ");
107  for(int loop = 0; loop < schemaEntries.size(); loop++){
108  snprintf(parts,256,"{\"name\":\"%s\", \"type\":\"%s\", \"info\":\" \"}",
109  schemaEntries[loop].name.c_str(), schemaEntries[loop].type.c_str());
110  if(loop!=0) result.append(",");
111  result.append(parts);
112  }
113  result.append("] }");
114  return result;
115  }
116 
117 
118  //=============================================
119  // Implementation of dictionary class
120  //=============================================
121  std::vector<std::string> dictionary::getSchemaList(){
122  std::map<std::string, schema>::iterator it;
123  std::vector<std::string> vec;
124  for ( it = factory.begin(); it != factory.end(); it++ ){
125  vec.push_back(it->first);
126  }
127  return vec;
128  }
129 
130  bool dictionary::parse(const char *schemaString){
131  std::vector<std::string> tokens;
132  std::string schemahead = hipo::utils::substring(schemaString,"{","}",0);
133  hipo::utils::tokenize(schemahead, tokens, "/");
134  int group = std::atoi(tokens[1].c_str());
135  int item = std::atoi(tokens[2].c_str());
136  hipo::schema schema (tokens[0].c_str(),group,item);
137  std::string schemabody = hipo::utils::substring(schemaString,"{","}",1);
138  schema.parse(schemabody.c_str());
139  addSchema(schema);
140  return true;
141  }
142 
144  std::vector<std::string> list = getSchemaList();
145  for(auto & i : list){
146  schema sc = getSchema(i.c_str());
147  printf("%24s : %5d %5d %5d\n", sc.getName().c_str(),
148  sc.getGroup(), sc.getItem(),sc.getEntries());
149  }
150  }
151 }
bool parse(const char *schemaString)
Parse a full schema string and add it to the dictionary.
Definition: dictionary.cpp:130
void addSchema(schema sc)
Add a schema to the dictionary.
Definition: dictionary.h:260
std::vector< std::string > getSchemaList()
Get a list of all schema names in the dictionary.
Definition: dictionary.cpp:121
void show()
Print all schemas in the dictionary to standard output.
Definition: dictionary.cpp:143
schema & getSchema(const char *name)
Retrieve a schema by name.
Definition: dictionary.h:271
Schema definition for a HIPO bank.
Definition: dictionary.h:73
void parse(const std::string &schString)
Parse a schema definition string and populate entries.
Definition: dictionary.cpp:17
std::string getName() const
Get the schema name.
Definition: dictionary.h:125
std::string getSchemaString()
Serialize the schema to a definition string (e.g. "pid/I,px/F").
Definition: dictionary.cpp:86
int getSizeForRows(int rows)
Compute the total buffer size needed for a given number of rows.
Definition: dictionary.cpp:80
int getOffset(int item, int order, int rows) const
Compute the byte offset for a column value by column index.
Definition: dictionary.h:171
void show()
Print the schema to standard output.
Definition: dictionary.cpp:67
int getItem()
Get the item identifier.
Definition: dictionary.h:131
int getEntries() const noexcept
Get the number of columns in the schema.
Definition: dictionary.h:217
int getGroup()
Get the group identifier.
Definition: dictionary.h:128
std::string getSchemaStringJson()
Serialize the schema to a JSON-formatted string.
Definition: dictionary.cpp:100
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 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 std::string & trim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trims both leading and trailing whitespace from a string.
Definition: utils.h:103
Schema definitions and schema dictionary for HIPO banks.
Definition: bank.cpp:47
@ kLong
64-bit signed integer.
Definition: dictionary.h:57
@ kInt
32-bit signed integer.
Definition: dictionary.h:54
@ kFloat
32-bit IEEE 754 floating point.
Definition: dictionary.h:55
@ kShort
16-bit signed integer.
Definition: dictionary.h:53
@ kByte
8-bit signed integer.
Definition: dictionary.h:52
@ kDouble
64-bit IEEE 754 floating point.
Definition: dictionary.h:56
Describes a single column (entry) within a schema.
Definition: dictionary.h:40
int typeId
Numeric type identifier (see Type).
Definition: dictionary.h:43
int offset
Byte offset of this column within a row.
Definition: dictionary.h:45
std::string type
Type string (e.g. "B", "S", "I", "F", "D", "L").
Definition: dictionary.h:42
std::string name
Column name.
Definition: dictionary.h:41
int typeSize
Size of the type in bytes.
Definition: dictionary.h:44
Utility functions and benchmark timer for HIPO library operations.