Belle II Software  release-05-01-25
RunNumberTable.cc
1 #include "daq/slc/database/RunNumberTable.h"
2 
3 #include <daq/slc/database/DBHandlerException.h>
4 #include <daq/slc/database/DBInterface.h>
5 
6 #include <daq/slc/system/LogFile.h>
7 
8 using namespace Belle2;
9 
10 RunNumber RunNumberTable::add(const std::string& node,
11  const std::string& runtype,
12  int expno, int runno)
13 {
14  if (runtype.size() == 0 || node.size() == 0) {
15  LogFile::fatal("Empty node name : node='%s' runtype='%s'", node.c_str(), runtype.c_str());
16  return RunNumber();
17  }
18  if (runtype.size() == 0 || node.size() == 0) {
19  LogFile::fatal("Empty runtype : node='%s' runtype='%s'", node.c_str(), runtype.c_str());
20  return RunNumber();
21  }
22  if (node == "pxd" || node == "svd" || node == "cdc" || node == "top" ||
23  node == "arich" || node == "ecl" || node == "klm" || node == "trg" || node == "b4test") {
24  if (node != runtype) {
25  LogFile::fatal("wrong node name or runtype : node='%s' != runtype='%s'", node.c_str(), runtype.c_str());
26  return RunNumber();
27  }
28  } else if (node == "global") {
29  if (runtype != "physics" && runtype != "cosmic" &&
30  runtype != "beam" && runtype != "null" && runtype != "debug") {
31  LogFile::fatal("wrong node name or runtype : node='%s' runtype='%s'!=physics/cosmic/beam/debug/null", node.c_str(),
32  runtype.c_str());
33  return RunNumber();
34  }
35  } else {
36  LogFile::fatal("wrong node name : node='%s'!=global/pxd/svd/cdc/top/arich/ecl/klm/trg/b4test runtype='%s'", node.c_str(),
37  runtype.c_str());
38  }
39  try {
40  int expno_tmp = getExpNumber("");
41  if (node != "global" || expno < expno_tmp) expno = expno_tmp;
42  int runno_tmp = getRunNumber(node, expno);
43  if (runno < runno_tmp) runno = runno_tmp;
44  m_db.execute("insert into runnumber_new (node, runtype, expno, runno) "
45  "values ('%s', '%s', %d, %d) "
46  "returning id, extract(epoch from record_time) as tstart;",
47  node.c_str(), runtype.c_str(), expno, runno);
48  DBRecordList record_v(m_db.loadRecords());
49  if (record_v.size() > 0) {
50  int id = record_v[0].getInt("id");
51  int record_time = record_v[0].getInt("tstart");
52  return RunNumber(node, runtype, expno, runno, id, record_time);
53  }
54  } catch (const DBHandlerException& e) {
55  LogFile::error("DB access error : %s", e.what());
56  }
57  return RunNumber();
58 }
59 
60 RunNumber RunNumberTable::add(const RunNumber& rn)
61 {
62  return add(rn.getNode(), rn.getRunType(), rn.getExpNumber(), rn.getRunNumber());
63 }
64 
65 int RunNumberTable::getRunNumber(const std::string& node, int expno)
66 {
67  if (expno <= 0) return -1;
68  int runno = 1;
69  try {
70  //if (node.size() == 0) {
71  if (expno == 0) {
72  m_db.execute("select max(runno)+1 as runno from runnumber_new "
73  "limit 1;");
74  } else {
75  m_db.execute("select max(runno)+1 as runno from runnumber_new "
76  "where expno = %d limit 1;", expno);
77  }
78  /*
79  } else {
80  if (expno == 0) {
81  m_db.execute("select max(runno)+1 as runno from runnumber_new "
82  "where node = '%s' limit 1;", node.c_str());
83  } else {
84  m_db.execute("select max(runno)+1 as runno from runnumber_new "
85  "where node = '%s' and expno = %d limit 1;",
86  node.c_str(), expno);
87  }
88  }
89  */
90  DBRecordList record_v(m_db.loadRecords());
91  if (record_v.size() > 0) {
92  runno = record_v[0].getInt("runno");
93  if (runno <= 0) runno = 1;
94  }
95  } catch (const DBHandlerException& e) {
96  LogFile::error("DB access error : %s", e.what());
97  return -1;
98  }
99  return runno;
100 }
101 
102 int RunNumberTable::getExpNumber(const std::string& node)
103 {
104  int expno = 1;
105  try {
106  if (node.size() == 0) {
107  m_db.execute("select max(expno) as expno from runnumber_new limit 1;");
108  } else {
109  m_db.execute("select max(expno) as expno from runnumber_new "
110  "where node = '%s' limit 1;", node.c_str());
111  }
112  DBRecordList record_v(m_db.loadRecords());
113  if (record_v.size() > 0) {
114  expno = record_v[0].getInt("expno");
115  if (expno <= 0) expno = 1;
116  }
117  } catch (const DBHandlerException& e) {
118  LogFile::error("DB access error : %s", e.what());
119  return -1;
120  }
121  return expno;
122 }
123 
124 RunNumberList RunNumberTable::get(const std::string& node, int expno, int runno_min, int runno_max)
125 {
126  RunNumberList list;
127  try {
128  const char* cmd = "select node, runtype, expno, runno, id, "
129  "extract(epoch from record_time) as record_time from runnumber_new";
130  if (node.size() == 0) {
131  if (expno == 0) {
132  if (runno_max > 0) {
133  m_db.execute("%s where runno >= %d and runno <= %d order by id", cmd, runno_min, runno_max);
134  } else {
135  m_db.execute("%s where runno >= %d order by id", cmd, runno_min);
136  }
137  } else {
138  if (runno_max > 0) {
139  m_db.execute("%s where expno = %d and runno >= %d and runno <= %d order by id",
140  cmd, expno, runno_min, runno_max);
141  } else {
142  m_db.execute("%s where expno = %d and runno >= %d order by id", cmd, expno, runno_min);
143  }
144  }
145  } else {
146  if (expno == 0) {
147  if (runno_max > 0) {
148  m_db.execute("%s where node = '%s' and runno >= %d and runno <= %d order by id",
149  cmd, node.c_str(), runno_min, runno_max);
150  } else {
151  m_db.execute("%s where node = '%s' and runno >= %d order by id", cmd, node.c_str(), runno_min);
152  }
153  } else {
154  if (runno_max > 0) {
155  m_db.execute("%s where node = '%s' and expno = %d and runno >= %d and runno <= %d order by id",
156  cmd, node.c_str(), expno, runno_min, runno_max);
157  } else {
158  m_db.execute("%s where node = '%s' and expno = %d and runno >= %d order by id",
159  cmd, node.c_str(), expno, runno_min);
160  }
161  }
162  }
163  const DBRecordList record_v(m_db.loadRecords());
164  for (DBRecordList::const_iterator it = record_v.begin();
165  it != record_v.end(); it++) {
166  const DBRecord& record(*it);
167  list.push_back(RunNumber(record.get("node"), record.get("runtype"),
168  record.getInt("expno"), record.getInt("runno"),
169  record.getInt("id"), record.getInt("record_time")));
170  }
171  } catch (const DBHandlerException& e) {
172  LogFile::error("DB access error : %s", e.what());
173  }
174  return list;
175 }
176 
177 RunNumberList RunNumberTable::get(int expno, int runno_min, int runno_max)
178 {
179  return get("", expno, runno_min, runno_max);
180 }
181 
182 void RunNumberTable::create()
183 {
184  if (!m_db.checkTable("runnumber_new")) {
185  m_db.execute("create table runnumber_new ("
186  "record_time timestamp with time zone "
187  "not null default current_timestamp, "
188  "id serial not null primary key, "
189  "node text not null, "
190  "runtype text not null, "
191  "expno int not null, "
192  "runno int not null, "
193  "unique (node, expno, runno));");
194  m_db.execute("create index runnumber_new_id_index on runnumber_new(id);");
195  }
196 }
Belle2::DBHandlerException
Definition: DBHandlerException.h:12
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
Belle2::DBRecord
Definition: DBRecord.h:16
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RunNumber
Definition: RunNumber.h:13
Belle2::rn
TString rn()
Get random string.
Definition: tools.h:41