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