Belle II Software development
RunNumberTable Class Reference

Public Member Functions

 RunNumberTable (DBInterface &db)
 
RunNumber add (const std::string &node, const std::string &runtype, int expno, int runno)
 
RunNumber add (const RunNumber &info)
 
int getRunNumber (const std::string &node, int expno)
 
int getExpNumber (const std::string &node)
 
void create ()
 
RunNumberList get (const std::string &node, int expno, int runno_min=0, int runno_max=-1)
 
RunNumberList get (int expno, int runno_min=0, int runno_max=-1)
 

Private Attributes

DBInterfacem_db
 

Detailed Description

Definition at line 21 of file RunNumberTable.h.

Constructor & Destructor Documentation

◆ RunNumberTable()

RunNumberTable ( DBInterface db)
inline

Definition at line 24 of file RunNumberTable.h.

24: m_db(db) {}

◆ ~RunNumberTable()

~RunNumberTable ( )
inline

Definition at line 25 of file RunNumberTable.h.

25{}

Member Function Documentation

◆ add() [1/2]

RunNumber add ( const RunNumber info)

Definition at line 67 of file RunNumberTable.cc.

68{
69 return add(rn.getNode(), rn.getRunType(), rn.getExpNumber(), rn.getRunNumber());
70}
TString rn()
Get random string.
Definition: tools.h:38

◆ add() [2/2]

RunNumber add ( const std::string &  node,
const std::string &  runtype,
int  expno,
int  runno 
)

Definition at line 17 of file RunNumberTable.cc.

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}

◆ create()

void create ( )

Definition at line 189 of file RunNumberTable.cc.

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}

◆ get() [1/2]

RunNumberList get ( const std::string &  node,
int  expno,
int  runno_min = 0,
int  runno_max = -1 
)

Definition at line 131 of file RunNumberTable.cc.

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}

◆ get() [2/2]

RunNumberList get ( int  expno,
int  runno_min = 0,
int  runno_max = -1 
)

Definition at line 184 of file RunNumberTable.cc.

185{
186 return get("", expno, runno_min, runno_max);
187}

◆ getExpNumber()

int getExpNumber ( const std::string &  node)

Definition at line 109 of file RunNumberTable.cc.

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}

◆ getRunNumber()

int getRunNumber ( const std::string &  node,
int  expno 
)

Definition at line 72 of file RunNumberTable.cc.

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}

Member Data Documentation

◆ m_db

DBInterface& m_db
private

Definition at line 41 of file RunNumberTable.h.


The documentation for this class was generated from the following files: