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