BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Coverage.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/calibUtil/src/dbIntegrity/Coverage.cxx,v 1.1.1.1
2// 2005/10/17 06:12:26 maqm Exp $
3/**
4 @file Coverage.cxx
5
6 Implementation of Coverage class
7
8 Also define and implement tiny data-only helper class
9*/
10#include "Coverage.h"
11#include "calibUtil/Metadata.h"
12#include "facilities/Util.h"
13#include "rdbModel/Management/Manager.h"
14#include "rdbModel/Management/XercesBuilder.h"
15#include <cstdio>
16#include <iostream>
17
18#include "rdbModel/Db/MysqlConnection.h"
19#include "rdbModel/Db/MysqlResults.h"
20#include "rdbModel/Rdb.h"
21#include "rdbModel/RdbException.h"
22#include "rdbModel/Tables/Assertion.h"
23#include "rdbModel/Tables/Column.h"
24#include "rdbModel/Tables/Table.h"
25
27public:
28 SelectInfo( int ser, const facilities::Timestamp& start, const facilities::Timestamp& end )
29 : m_ser( ser ) {
31 m_vstart = Timestamp( start.getClibTime() );
32 m_vend = Timestamp( end.getClibTime() );
33 }
34 int m_ser;
37};
38
39std::ostream& operator<<( std::ostream& out, const SelectInfo& info ) {
40 out << "Serial #" << info.m_ser << " validitiy interval [" << info.m_vstart.getString()
41 << ", " << info.m_vend.getString() << "]" << std::endl;
42 return out;
43}
44
45Coverage::Coverage( calibUtil::Metadata* meta, const std::string& instr,
46 const std::string& flavor, const std::string& level,
47 const facilities::Timestamp& ts )
48 : m_instr( instr ), m_flavor( flavor ), m_level( level ), m_overlap( 300 ) {
50 m_selects.reserve( 3 );
51 m_selects.push_back( "ser_no" );
52 m_selects.push_back( "vstart" );
53 m_selects.push_back( "vend" );
54
55 m_orderBy.reserve( 1 );
56 m_orderBy.push_back( "vstart asc" );
57
58 m_rdb = meta->getRdb();
59 m_conn = meta->getReadConnection();
60 m_table = meta->getTable();
61}
62
63// Implement wildcards later
64bool Coverage::expandTypes( std::string& nickname, std::vector<std::string>& types ) {
65 if ( ( nickname == "CAL" ) || ( nickname == "TKR" ) || ( nickname == "*" ) )
66 {
67 if ( !m_rdb )
68 {
69 std::cerr << "Unable to expand wildcard argument " << nickname
70 << " without schema for metadata dbs " << std::endl;
71 }
72 else
73 {
74 std::cerr << "Wildcard expansion of calib_type argument not yet supported";
75 std::cerr << std::endl << "Have a nice day" << std::endl;
76 }
77 std::cerr.flush();
78 exit( 1 );
79 }
80 types.push_back( nickname );
81 return true;
82}
83
84unsigned Coverage::checkType( std::string calibtype ) {
85 using namespace rdbModel;
87 // Make a query to find all rows matching criteria, ordered ascending
88 // by vstart
89 // match calib_type, flavor, instrument, proc_level
90 // completion = "OK"
91 // vstart >= ts
92 // Ask for return of ser_no, vstart, vend
93
94 std::vector<Assertion::Operator*> conditions;
95 conditions.reserve( 6 );
96
97 Assertion::Operator completeOp( OPTYPEequal, "completion", "OK", FIELDTYPEold,
99 // false, true);
100 Assertion::Operator instOp( OPTYPEequal, "instrument", m_instr, FIELDTYPEold, FIELDTYPElit );
101 // false, true);
102 Assertion::Operator calibTypeOp( OPTYPEequal, "calib_type", calibtype, FIELDTYPEold,
103 FIELDTYPElit );
104 // false, true);
105 Assertion::Operator flavorOp( OPTYPEequal, "flavor", m_flavor, FIELDTYPEold, FIELDTYPElit );
106 // false, true);
107 Assertion::Operator levelOp( OPTYPEequal, "proc_level", m_level, FIELDTYPEold,
108 FIELDTYPElit );
109 // false, true);
110 Assertion::Operator vstartOp( OPTYPElessThan, m_ts.getString(), "vstart", FIELDTYPElit,
111 FIELDTYPEold );
112 // true, false);
113 conditions.push_back( &calibTypeOp );
114 conditions.push_back( &instOp );
115 conditions.push_back( &flavorOp );
116 conditions.push_back( &levelOp );
117 conditions.push_back( &vstartOp );
118 conditions.push_back( &completeOp );
119 Assertion::Operator* andOp = new Assertion::Operator( OPTYPEand, conditions );
120 Assertion* whereClause = new Assertion( andOp );
121
122 ResultHandle* results = 0;
123
124 try
125 { // make the query
126 results = m_conn->select( m_table, m_selects, m_orderBy, whereClause );
127 } catch ( RdbException ex )
128 {
129 std::cerr << ex.getMsg() << std::endl;
130 std::cerr.flush(); // exit(1);
131 return false;
132 }
133
134 if ( !results )
135 { // Error. Should have ResultHandle even if 0 rows.
136 std::cerr << "Error making query " << std::endl;
137 std::cerr.flush();
138 exit( 1 );
139 }
140
141 // Save returned values
142 std::vector<SelectInfo> info;
143 unsigned nFound = results->getNRows();
144
145 if ( !nFound )
146 {
147 std::cout << "No calibrations found for calib_type '" << calibtype << "'" << std::endl;
148 return true;
149 }
150
151 info.reserve( results->getNRows() );
152
153 unsigned iRow = 0;
154 std::vector<std::string> fields;
155 fields.reserve( 3 );
156
157 bool ok = true;
158 unsigned retCode = 0;
159
160 // 1. For each row store columnes; check if vstart < vend
161 std::cout << std::endl << "Checking for valid timestamps, vstart < vend .. " << std::endl;
162 for ( iRow = 0; iRow < nFound; iRow++ )
163 {
164 results->getRow( fields, iRow );
165 int ser = facilities::Util::stringToInt( fields[0] );
166 try
167 {
168 Timestamp vstart = Timestamp( fields[1] );
169 Timestamp vend = Timestamp( fields[2] );
170 info.push_back( SelectInfo( ser, vstart, vend ) );
171
172 if ( vend.getClibTime() < vstart.getClibTime() )
173 {
174 std::cerr << "vend < vstart for " << info.back();
175 ok = false; // or could discard the row and try to continue
176 retCode = 1;
177 }
178 } catch ( facilities::BadTimeInput ex )
179 {
180 std::cerr << "Bad vstart or vend in row " << ser << std::endl;
181 ok = false;
182 retCode = 1;
183 }
184 }
185 if ( !ok ) return retCode;
186
187 // 2. Check if vend's also increase monotonically
188 std::cout << std::endl << "Checking for vends monotonic.. " << std::endl;
189 for ( iRow = 0; iRow < nFound - 1; iRow++ )
190 {
191 if ( info[iRow].m_vend.getClibTime() > info[iRow + 1].m_vend.getClibTime() )
192 {
193 std::cerr << "Validity interval for row with serial #" << info[iRow + 1].m_ser
194 << " completely contained in interval for row with serial #"
195 << info[iRow].m_ser << std::endl;
196 std::cerr << info[iRow];
197 std::cerr << info[iRow + 1] << std::endl;
198 ok = false;
199 retCode = 2;
200 }
201 }
202 if ( !ok ) return retCode;
203
204 // 3. Look for gaps
205 std::cout << std::endl << "Checking for gaps.. " << std::endl;
206 for ( iRow = 0; iRow < nFound - 1; iRow++ )
207 {
208 if ( info[iRow].m_vend < info[iRow + 1].m_vstart )
209 {
210 std::cerr << "Validity interval gap between calibrations with serial #"
211 << info[iRow].m_ser << " and #" << info[iRow + 1].m_ser << std::endl;
212 std::cerr << info[iRow];
213 std::cerr << info[iRow + 1] << std::endl;
214 ok = false;
215 retCode = 3;
216 }
217 }
218 // 4. Look for overlaps
219 std::cout << std::endl << "Checking for overlaps.. " << std::endl;
220 for ( iRow = 0; iRow < nFound - 1; iRow++ )
221 {
222 if ( ( info[iRow].m_vend ).getClibTime() >
223 ( info[iRow + 1].m_vstart ).getClibTime() + m_overlap )
224 {
225 std::cerr << "Unacceptable overlap between serial #" << info[iRow].m_ser << " and #"
226 << info[iRow + 1].m_ser << std::endl;
227 std::cerr << info[iRow];
228 std::cerr << info[iRow + 1] << std::endl;
229 ok = false;
230 if ( !retCode ) retCode = 4;
231 }
232 }
233 return retCode;
234}
std::ostream & operator<<(std::ostream &out, const SelectInfo &info)
Definition Coverage.cxx:39
double meta
unsigned checkType(std::string calibtype)
Definition Coverage.cxx:84
bool expandTypes(std::string &nickname, std::vector< std::string > &types)
Definition Coverage.cxx:64
Coverage(calibUtil::Metadata *meta, const std::string &instr, const std::string &flavor, const std::string &level, const facilities::Timestamp &ts)
Definition Coverage.cxx:45
facilities::Timestamp m_vstart
Definition Coverage.cxx:35
SelectInfo(int ser, const facilities::Timestamp &start, const facilities::Timestamp &end)
Definition Coverage.cxx:28
facilities::Timestamp m_vend
Definition Coverage.cxx:36
std::string getString() const
Return string representation of time, not including nanoseconds;.
Definition Timestamp.cxx:87
static int stringToInt(const std::string &InStr)
virtual bool getRow(std::vector< std::string > &fields, unsigned int i=0, bool clear=true)=0
virtual unsigned int getNRows() const =0
Return number of rows in results.