Belle II Software  release-08-01-10
trg-cdc-neurotrigger-writedb.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/database/DBImportObjPtr.h>
10 #include <trg/cdc/dbobjects/CDCTriggerNeuroConfig.h>
11 #include <iostream>
12 #include <fstream>
13 
14 class InputParser {
15 public:
16  InputParser(const int& argc, char** argv)
17  {
18  for (int i = 1; i < argc; ++i)
19  this->tokens.push_back(std::string(argv[i]));
20  }
21  const std::string& getCmdOption(const std::string& option) const
22  {
23  std::vector<std::string>::const_iterator itr;
24  itr = std::find(this->tokens.begin(), this->tokens.end(), option);
25  if (itr != this->tokens.end() && ++itr != this->tokens.end()) {
26  return *itr;
27  }
28  static const std::string empty_string("");
29  return empty_string;
30  }
31  bool cmdOptionExists(const std::string& option) const
32  {
33  return std::find(this->tokens.begin(), this->tokens.end(), option)
34  != this->tokens.end();
35  }
36 private:
37  std::vector <std::string> tokens;
38 };
39 
40 using namespace Belle2;
41 
42 int main(int argc, char** argv)
43 {
44  int iovc = 0;
45  int nniov_exp_start;
46  int nniov_exp_end;
47  int nniov_run_start;
48  int nniov_run_end;
49  std::string configfilename = "";
50  std::string nnname;
51  std::string nnpath;
52  std::string nnnote;
53  std::string fwname;
54  std::string fwnote;
55  bool ppbool;
56  std::string ppnote;
57 
58 
59  InputParser input(argc, argv);
60  if (input.cmdOptionExists("-h")) {
61  std::cout << "A small tool to create ConDB payloads for the Neurotrigger." << std::endl;
62  std::cout << "Usage: \% trg-cdc-neurotrigger-writedb -f example.conf" << std::endl;
63  }
64  const std::string& filename = input.getCmdOption("-f");
65  if (!filename.empty()) {
66  configfilename = filename;
67  }
68 
69  // Creating payload object:
71  nc.construct();
72 
73  std::ifstream confile;
74  try {
75  confile.open(configfilename, std::ifstream::in);
76  } catch (int e) {
77  std::cout << "ERROR! While opening file: " << configfilename << " Error code: " << e << std::endl;
78  exit(EXIT_FAILURE);
79  }
80  std::string line_all;
81  if (!confile.is_open()) {
82  std::cout << "ERROR! While opening file: " << configfilename << std::endl;
83  exit(EXIT_FAILURE);
84  }
85  while (std::getline(confile, line_all)) { // remove comments
86  std::size_t hashtag = line_all.find('#');
87  std::string line = line_all.substr(0, hashtag);
88  std::string par;
89  std::string key;
90  if (line.length() < 3) {
91  continue;
92  // check, if line wasnt a pure comment line
93  }
94  if (line.find('=') == std::string::npos) {
95  continue;
96  }
97  par = line.substr(0, line.find('='));
98  par.erase(std::remove(par.begin(), par.end(), ' '), par.end()); // remove whitespaces in whole string
99 
100  if (par == "nniov_run_start") {
101  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
102  nniov_run_start = std::stoi(key);
103  iovc++;
104  }
105  if (par == "nniov_run_end") {
106  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
107  nniov_run_end = std::stoi(key);
108  iovc++;
109  }
110  if (par == "nniov_exp_start") {
111  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
112  nniov_exp_start = std::stoi(key);
113  iovc++;
114  }
115  if (par == "nniov_exp_end") {
116  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
117  nniov_exp_end = std::stoi(key);
118  iovc++;
119  }
120  if (par == "nnname") {
121  // may look confusing at a first glance, but cuts exactly the content between the first two occurrences of doublequotes
122  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
123  nnname = key;
124  //set neural network filename:
125  nc->setNNName(nnname);
126  }
127  if (par == "nnnote") {
128  // may look confusing at a first glance, but cuts exactly the content between the first two occurrences of doublequotes
129  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
130  nnnote = key;
131  //add notes for expert networks
132  nc->setNNNotes(nnnote);
133  }
134  if (par == "nnpath") {
135  // may look confusing at a first glance, but cuts exactly the content between the first two occurrences of doublequotes
136  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
137  nnpath = key;
138  // loading MLPs:
139  nc->loadMLPs(nnpath, "MLPs");
140  }
141  if (par == "fwname") {
142  // may look confusing at a first glance, but cuts exactly the content between the first two occurrences of doublequotes
143  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
144  fwname = key;
145  //set firmware version id:
146  nc->setNNTFirmwareVersionID(fwname);
147  }
148  if (par == "fwnote") {
149  // may look confusing at a first glance, but cuts exactly the content between the first two occurrences of doublequotes
150  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
151  fwnote = key;
152  // add comment about firmware:
153  nc->setNNTFirmwareComment(fwnote);
154 
155  }
156  if (par == "ppnote") {
157  // may look confusing at a first glance, but cuts exactly the content between the first two occurrences of doublequotes
158  key = line.substr((line.find('"') + 1), (line.find('"', line.find('"') + 1) - 1 - line.find('"')));
159  ppnote = key;
160  //add preprocessing notes:
161  nc->setPPNotes(ppnote);
162  }
163  if (par == "ppbool") {
164  if (line.find("alse") != std::string::npos) { //dirty case insensitive
165  ppbool = false;
166  } else if (line.find("rue") != std::string::npos) {
167  ppbool = true;
168  } else {
169  std::cout << "ERROR!: Wrong key argument for parameter ppbool:" << line << std::endl;
170  }
171  //define to use the ETF:
172  nc->setUseETF(ppbool);
173  }
174  if (par == "addb2formatline") {
175  // split key in data fields:
176  std::stringstream ss;
177  ss << line.substr((line.find('(') + 1), (line.find(')') - 1 - line.find('(')));
178  std::string uid;
179  std::string startstr;
180  std::string endstr;
181  std::string offsetstr;
182  std::string description;
183  std::getline(ss, uid, ',');
184  std::getline(ss, startstr, ',');
185  std::getline(ss, endstr, ',');
186  std::getline(ss, offsetstr, ',');
187  std::getline(ss, description, '\n');
188  nc->addB2FormatLine(std::stoi(startstr), std::stoi(endstr), std::stoi(offsetstr), uid.substr((uid.find('"') + 1), (uid.find('"',
189  uid.find('"') + 1) - 1 - uid.find('"'))), description.substr((description.find('"') + 1), (description.find('"',
190  description.find('"') + 1) - 1 - description.find('"'))));
191 
192  }
193  }
194  if (iovc > 3) { // >3 means all 4 iov numbers are there
195  // set interval of validity:
196  IntervalOfValidity iov(nniov_exp_start, nniov_run_start, nniov_exp_end, nniov_run_end);
197  nc.import(iov);
198  }
199  return 0;
200 }
Class for importing a single object to the database.
A class that describes the interval of experiments/runs for which an object in the database is valid.
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:91