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 {
27  namespace sqlite {
28 
29  template <typename T = double>
30  T min(sqlite3* const db,
31  std::string const& table_name,
32  std::string const& column_name);
33 
34  template <typename T = double>
35  T max(sqlite3* const db,
36  std::string const& table_name,
37  std::string const& column_name);
38 
39  double mean(sqlite3* db,
40  std::string const& table_name,
41  std::string const& column_name);
42  double median(sqlite3* db,
43  std::string const& table_name,
44  std::string const& column_name);
45  double rms(sqlite3* db,
46  std::string const& table_name,
47  std::string const& column_name);
48 
49  } // sqlite
50 } // cet
51 
52 //==================================================================
53 // Implementation below
54 
55 template <typename T>
56 T
57 cet::sqlite::min(sqlite3* const db,
58  std::string const& table_name,
59  std::string const& column_name)
60 {
62  r << select("min(" + column_name + ")").from(db, table_name);
63  return unique_value(r);
64 }
65 
66 template <typename T>
67 T
68 cet::sqlite::max(sqlite3* const db,
69  std::string const& table_name,
70  std::string const& column_name)
71 {
73  r << select("max(" + column_name + ")").from(db, table_name);
74  return unique_value(r);
75 }
76 
77 #endif /* cetlib_sqlite_statistics_h */
78 
79 // Local Variables:
80 // mode: c++
81 // End:
T unique_value(query_result< T > const &r)
Definition: query_result.h:101
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
auto select(T const &...t)
Definition: select.h:155
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:57
TRandom3 r(0)
double T
Definition: Xdiff_gwt.C:5
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
double median(sqlite3 *db, std::string const &table_name, std::string const &column_name)