statistics.h
Go to the documentation of this file.
1 #ifndef cetlib_sqlite_statistics_h
2 #define cetlib_sqlite_statistics_h
3 
4 // ===================================================================
5 // The statistics utilities provided below allow a user to specify a
6 // database handle, the table name, and the column name from which
7 // the statistics quantity will be calculated.
8 //
9 // For more complicated statistics calculations, explicit querying
10 // should be made, either through using the cet::sqlite::select
11 // facility, or by using the more general std::sqlite::query facility.
12 //
13 // N.B. The calculated rms is actually the biased (division over N
14 // instead of N-1) standard deviation of a sample. The name of
15 // this function was chosen to comport with the parlance used in
16 // high-energy physics.
17 // ===================================================================
18 
20 #include "cetlib/sqlite/select.h"
21 
22 #include "sqlite3.h"
23 
24 #include <string>
25 
26 namespace cet::sqlite {
27 
28  template <typename T = double>
29  T min(sqlite3* const db,
30  std::string const& table_name,
31  std::string const& column_name);
32 
33  template <typename T = double>
34  T max(sqlite3* const db,
35  std::string const& table_name,
36  std::string const& column_name);
37 
38  double mean(sqlite3* db,
39  std::string const& table_name,
40  std::string const& column_name);
41  double median(sqlite3* db,
42  std::string const& table_name,
43  std::string const& column_name);
44  double rms(sqlite3* db,
45  std::string const& table_name,
46  std::string const& column_name);
47 
48 } // cet::sqlite
49 
50 //==================================================================
51 // Implementation below
52 
53 template <typename T>
54 T
56  std::string const& table_name,
57  std::string const& column_name)
58 {
60  r << select("min(" + column_name + ")").from(db, table_name);
61  return unique_value(r);
62 }
63 
64 template <typename T>
65 T
67  std::string const& table_name,
68  std::string const& column_name)
69 {
71  r << select("max(" + column_name + ")").from(db, table_name);
72  return unique_value(r);
73 }
74 
75 #endif /* cetlib_sqlite_statistics_h */
76 
77 // Local Variables:
78 // mode: c++
79 // End:
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:40
std::string string
Definition: nybbler.cc:12
auto select(T const &...t)
Definition: select.h:146
struct sqlite3 sqlite3
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:66
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
double median(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:26