17#include "VertexFit/VertexDbSvc.h"
18#include "GaudiKernel/Kernel.h"
19#include "GaudiKernel/IInterface.h"
20#include "GaudiKernel/StatusCode.h"
21#include "GaudiKernel/SvcFactory.h"
22#include "GaudiKernel/MsgStream.h"
23#include "GaudiKernel/ISvcLocator.h"
24#include "GaudiKernel/SmartDataPtr.h"
25#include "GaudiKernel/IDataProviderSvc.h"
26#include "GaudiKernel/PropertyMgr.h"
29#include "GaudiKernel/IIncidentSvc.h"
30#include "GaudiKernel/Incident.h"
31#include "GaudiKernel/IIncidentListener.h"
33#include "GaudiKernel/ISvcLocator.h"
34#include "GaudiKernel/Bootstrap.h"
35#include "EventModel/EventModel.h"
36#include "EventModel/Event.h"
37#include "EventModel/EventHeader.h"
41#include "VertexFit/VertexDbSvc.h"
49 Service (name, svcloc){
51 declareProperty(
"Host" , host = std::string(
"bes3db2.ihep.ac.cn"));
52 declareProperty(
"DbName" , dbName = std::string(
"offlinedb"));
53 declareProperty(
"UserName" , userName = std::string(
"guest"));
54 declareProperty(
"Password" , password = std::string(
"guestpass"));
55 declareProperty(
"BossVer" , m_bossver = std::string(
"default"));
56 declareProperty(
"VerPar" , m_verpar = std::string(
"default"));
57 declareProperty(
"BossRelease",m_bossRelease = std::string(
"default"));
64 if( IID_IVertexDbSvc.versionMatch(riid) ){
67 return Service::queryInterface(riid, ppvInterface);
69 return StatusCode::SUCCESS;
73 MsgStream log(messageService(), name());
74 log << MSG::INFO <<
"VertexDbSvc::initialize()" << endreq;
76 StatusCode sc = Service::initialize();
77 if( sc.isFailure() )
return sc;
81 sc = service(
"IncidentSvc", incsvc);
84 incsvc -> addListener(
this,
"NewRun", priority);
87 sc = serviceLocator()->service(
"DatabaseSvc",m_dbsvc,
true);
88 if (sc .isFailure() ) {
89 log << MSG::ERROR <<
"Unable to find DatabaseSvc " << endreq;
93 sc = serviceLocator()->service(
"EventDataSvc", m_eventSvc,
true);
94 if (sc .isFailure() ) {
95 log << MSG::ERROR <<
"Unable to find EventDataSvc " << endreq;
98 return StatusCode::SUCCESS;
102 MsgStream log(messageService(), name());
103 log << MSG::INFO <<
"VertexDbSvc::finalize()" << endreq;
105 return StatusCode::SUCCESS;
109 MsgStream log( messageService(), name() );
110 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
112 if ( inc.type() ==
"NewRun" ){
113 log << MSG::DEBUG <<
"NewRun" << endreq;
114 getVertexTableInfo();
129 dbName =
"offlinedb";
130#ifdef ROOTEVENTDATAVERSION
131 m_bossver = ROOTEVENTDATAVERSION;
132 m_bossRelease = ROOTEVENTDATAVERSION;
134 m_bossver =
"default";
135 m_bossRelease =
"default";
137 m_verpar =
"default";
139 m_dbsvc = DatabaseSvc::instance();
146 static int save_run = 0;
147 if( new_run == save_run )
return;
149 cout <<
"Begin New Run " << new_run << endl;
150 getVertexTableInfo(new_run);
157 if( !m_isRunNumberValid ) {
158 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
159 memset(m_primaryVertex,0,
sizeof(m_primaryVertex));
161 return m_primaryVertex;
166 if( !m_isRunNumberValid ) {
167 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
168 memset(m_sigmaPrimaryVertex,0,
sizeof(m_sigmaPrimaryVertex));
170 return m_sigmaPrimaryVertex;
174StatusCode VertexDbSvc::getVertexTableInfo(){
175 MsgStream log(messageService(), name());
176 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
177 int run = eventHeader->runNumber();
180void VertexDbSvc::getVertexTableInfo(
int run)
184 m_isRunNumberValid =
false;
189 log << MSG::INFO <<
"This data is the MC sample with the Run Number: " << run << endreq;
191 cout <<
"This data is the MC sample with the Run Number: " << run << endl;
199 bool ret_vtx = getReadBunchInfo(run);
202 cout <<
" VertexDbSvc:: can not found vertex information for run:"
203 << run <<
", boss version " << m_bossver << endl;
236 m_isRunNumberValid =
true;
238 log << MSG::INFO <<
"Successfully fetch the vertex information for run: "
239 << save_run << endreq;
240 return StatusCode::SUCCESS;
242 cout <<
"Successfully fetch the vertex information for run: "
248bool VertexDbSvc::getReadBunchInfo(
int run)
251 if(m_bossver ==
"default") {
252 if(m_bossRelease ==
"default") {
254 MsgStream log(messageService(), name());
255 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
256 << m_bossRelease <<
"." << endreq;
258 cout <<
"ERROR BossRelease must be set! Current value is "
259 << m_bossRelease <<
"." << endl;
265 sprintf(stmt1,
"select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", m_bossRelease.c_str(), run, run);
268 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
271 MsgStream log(messageService(), name());
272 log << MSG::ERROR <<
"ERROR: can not find records for run = " << run
273 <<
" and BossRelease = " << m_bossRelease << endreq;
275 cout <<
"ERROR: can not find records for run = " << run
276 <<
" and BossRelease = " << m_bossRelease << endl;
281 m_bossver = recordst->
GetString(
"SftVer");
282 m_verpar = recordst->
GetString(
"ParVer");
283 cout <<
"Using the SftVer and ParVer (" << m_bossver
284 <<
", " << m_verpar <<
") for run " << run <<
". " << endl;
288 string stmt =
"select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
290 tmp <<
"from BeamPar where RunNo = " << run
291 <<
" and SftVer=\'" << m_bossver <<
"\'";
292 if( m_verpar ==
"default" ) {
293 tmp <<
" group by ParVer";
295 tmp <<
" and ParVer = " << m_verpar;
301 int row_no = m_dbsvc->
query(dbName,stmt,res);
305 m_primaryVertex[0] = dbrec.
GetDouble(
"Vx");
306 m_primaryVertex[1] = dbrec.
GetDouble(
"Vy");
307 m_primaryVertex[2] = dbrec.
GetDouble(
"Vz");
308 m_sigmaPrimaryVertex[0] = dbrec.
GetDouble(
"SigmaVx");
309 m_sigmaPrimaryVertex[1] = dbrec.
GetDouble(
"SigmaVy");
310 m_sigmaPrimaryVertex[2] = dbrec.
GetDouble(
"SigmaVz");
double GetDouble(std::string key)
std::string GetString(std::string key)
virtual int query(const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0
VertexDbSvc(const std::string &name, ISvcLocator *svcloc)
void handle(const Incident &)
virtual StatusCode initialize()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
double * SigmaPrimaryVertex()
virtual StatusCode finalize()