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 (runtype.size() == 0 || 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 || node.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 if (expno == 0) {
79 m_db.execute("select max(runno)+1 as runno from runnumber_new "
80 "limit 1;");
81 } else {
82 m_db.execute("select max(runno)+1 as runno from runnumber_new "
83 "where expno = %d limit 1;", expno);
84 }
85 /*
86 } else {
87 if (expno == 0) {
88 m_db.execute("select max(runno)+1 as runno from runnumber_new "
89 "where node = '%s' limit 1;", node.c_str());
90 } else {
91 m_db.execute("select max(runno)+1 as runno from runnumber_new "
92 "where node = '%s' and expno = %d limit 1;",
93 node.c_str(), expno);
94 }
95 }
96 */
97 DBRecordList record_v(m_db.loadRecords());
98 if (record_v.size() > 0) {
99 runno = record_v[0].getInt("runno");
100 if (runno <= 0) runno = 1;
101 }
102 } catch (const DBHandlerException& e) {
103 LogFile::error("DB access error : %s", e.what());
104 return -1;
105 }
106 return runno;
107}
108
109int RunNumberTable::getExpNumber(const std::string& node)
110{
111 int expno = 1;
112 try {
113 if (node.size() == 0) {
114 m_db.execute("select max(expno) as expno from runnumber_new limit 1;");
115 } else {
116 m_db.execute("select max(expno) as expno from runnumber_new "
117 "where node = '%s' limit 1;", node.c_str());
118 }
119 DBRecordList record_v(m_db.loadRecords());
120 if (record_v.size() > 0) {
121 expno = record_v[0].getInt("expno");
122 if (expno <= 0) expno = 1;
123 }
124 } catch (const DBHandlerException& e) {
125 LogFile::error("DB access error : %s", e.what());
126 return -1;
127 }
128 return expno;
129}
130
131RunNumberList RunNumberTable::get(const std::string& node, int expno, int runno_min, int runno_max)
132{
133 RunNumberList list;
134 try {
135 const char* cmd = "select node, runtype, expno, runno, id, "
136 "extract(epoch from record_time) as record_time from runnumber_new";
137 if (node.size() == 0) {
138 if (expno == 0) {
139 if (runno_max > 0) {
140 m_db.execute("%s where runno >= %d and runno <= %d order by id", cmd, runno_min, runno_max);
141 } else {
142 m_db.execute("%s where runno >= %d order by id", cmd, runno_min);
143 }
144 } else {
145 if (runno_max > 0) {
146 m_db.execute("%s where expno = %d and runno >= %d and runno <= %d order by id",
147 cmd, expno, runno_min, runno_max);
148 } else {
149 m_db.execute("%s where expno = %d and runno >= %d order by id", cmd, expno, runno_min);
150 }
151 }
152 } else {
153 if (expno == 0) {
154 if (runno_max > 0) {
155 m_db.execute("%s where node = '%s' and runno >= %d and runno <= %d order by id",
156 cmd, node.c_str(), runno_min, runno_max);
157 } else {
158 m_db.execute("%s where node = '%s' and runno >= %d order by id", cmd, node.c_str(), runno_min);
159 }
160 } else {
161 if (runno_max > 0) {
162 m_db.execute("%s where node = '%s' and expno = %d and runno >= %d and runno <= %d order by id",
163 cmd, node.c_str(), expno, runno_min, runno_max);
164 } else {
165 m_db.execute("%s where node = '%s' and expno = %d and runno >= %d order by id",
166 cmd, node.c_str(), expno, runno_min);
167 }
168 }
169 }
170 const DBRecordList record_v(m_db.loadRecords());
171 for (DBRecordList::const_iterator it = record_v.begin();
172 it != record_v.end(); ++it) {
173 const DBRecord& record(*it);
174 list.push_back(RunNumber(record.get("node"), record.get("runtype"),
175 record.getInt("expno"), record.getInt("runno"),
176 record.getInt("id"), record.getInt("record_time")));
177 }
178 } catch (const DBHandlerException& e) {
179 LogFile::error("DB access error : %s", e.what());
180 }
181 return list;
182}
183
184RunNumberList RunNumberTable::get(int expno, int runno_min, int runno_max)
185{
186 return get("", expno, runno_min, runno_max);
187}
188
189void RunNumberTable::create()
190{
191 if (!m_db.checkTable("runnumber_new")) {
192 m_db.execute("create table runnumber_new ("
193 "record_time timestamp with time zone "
194 "not null default current_timestamp, "
195 "id serial not null primary key, "
196 "node text not null, "
197 "runtype text not null, "
198 "expno int not null, "
199 "runno int not null, "
200 "unique (node, expno, runno));");
201 m_db.execute("create index runnumber_new_id_index on runnumber_new(id);");
202 }
203}
TString rn()
Get random string.
Definition: tools.h:38
Abstract base class for different kinds of events.