LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
resolveRangeSet.cc
Go to the documentation of this file.
2 #include "sqlite3.h"
3 
4 namespace {
5 
6  void
7  successful_prepare(int const rc,
8  std::string const& filename,
9  std::string const& statement)
10  {
11  if (rc == SQLITE_OK)
12  return;
14  << "Error in preparing statement for getting contributors.\n"
15  << "File: " << filename << '\n'
16  << "Preparation statement: " << statement << '\n';
17  }
18 
19  void
20  successful_step(int const rc,
21  std::string const& sqlErrMsg,
22  std::string const& filename)
23  {
24  if (rc == SQLITE_DONE)
25  return;
27  << "Unexpected status from table read: " << sqlErrMsg << " (" << rc
28  << ").\n"
29  << "File: " << filename << '\n';
30  }
31 
32  void
33  successful_finalize(int const rc,
34  std::string const& sqlErrMsg,
35  std::string const& filename)
36  {
37  if (rc == SQLITE_OK)
38  return;
40  << "Unexpected status from DB status cleanup: " << sqlErrMsg << " (" << rc
41  << ").\n"
42  << "File: " << filename << '\n';
43  }
44 }
45 
46 using art::EventRange;
47 using art::RangeSet;
48 
51  std::string const& filename,
52  BranchType const bt,
53  unsigned const rangeSetID,
54  bool const compact)
55 {
56  // Invalid rangeSetID check
57  if (rangeSetID == std::numeric_limits<unsigned>::max())
58  return RangeSetInfo::invalid();
59 
60  sqlite3_stmt* stmt{nullptr};
61  std::string const run_ddl{
62  "SELECT Run FROM " + BranchTypeToString(bt) +
63  "RangeSets WHERE rowid==" + std::to_string(rangeSetID) + ";"};
64  auto rc = sqlite3_prepare_v2(db, run_ddl.c_str(), -1, &stmt, nullptr);
65  successful_prepare(rc, filename, run_ddl);
66 
67  rc = sqlite3_step(stmt);
68  auto const r = static_cast<RunNumber_t>(sqlite3_column_int(stmt, 0));
69  rc = sqlite3_finalize(stmt);
70  successful_finalize(rc, sqlite3_errmsg(db), filename);
71 
72  std::string const result_column_begin{compact ? "min(begin)" : "begin"};
73  std::string const result_column_end{compact ? "max(end)" : "end"};
74  std::string const maybe_suffix{compact ? " GROUP BY SubRun" : ""};
75  std::string const ddl{
76  "SELECT SubRun," + result_column_begin + ',' + result_column_end +
77  " FROM EventRanges WHERE rowid IN"
78  "(SELECT EventRangesID FROM " +
79  BranchTypeToString(bt) + "RangeSets_EventRanges WHERE RangeSetsID==" +
80  std::to_string(rangeSetID) + ')' + maybe_suffix + ';'};
81  rc = sqlite3_prepare_v2(db, ddl.c_str(), -1, &stmt, nullptr);
82  successful_prepare(rc, filename, ddl);
83 
84  std::vector<EventRange> ranges;
85  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
86  ranges.emplace_back(sqlite3_column_int(stmt, 0),
87  sqlite3_column_int(stmt, 1),
88  sqlite3_column_int(stmt, 2));
89  }
90  successful_step(rc, sqlite3_errmsg(db), filename);
91 
92  rc = sqlite3_finalize(stmt);
93  successful_finalize(rc, sqlite3_errmsg(db), filename);
94 
95  return RangeSetInfo{r, std::move(ranges)};
96 }
97 
100 {
101  if (rsi.is_invalid()) {
102  return RangeSet::invalid();
103  }
104  return RangeSet{rsi.run, rsi.ranges};
105 }
106 
109  std::string const& filename,
110  BranchType const bt,
111  unsigned const rangeSetID,
112  bool const compact)
113 {
114  auto const& rsInfo =
115  resolveRangeSetInfo(db, filename, bt, rangeSetID, compact);
116  return resolveRangeSet(rsInfo);
117 }
RangeSetInfo resolveRangeSetInfo(sqlite3 *, std::string const &filename, BranchType, unsigned RangeSetID, bool compact)
std::vector< EventRange > ranges
Definition: RangeSetInfo.h:35
RangeSet resolveRangeSet(RangeSetInfo const &rs)
Int_t max
Definition: plot.C:27
std::string const & BranchTypeToString(BranchType const bt)
Definition: BranchType.cc:65
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
BranchType
Definition: BranchType.h:18
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:119