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 m_bossver =
"default";
115 getVertexTableInfo();
130 dbName =
"offlinedb";
131#ifdef ROOTEVENTDATAVERSION
132 m_bossver = ROOTEVENTDATAVERSION;
133 m_bossRelease = ROOTEVENTDATAVERSION;
135 m_bossver =
"default";
136 m_bossRelease =
"default";
138 m_verpar =
"default";
140 m_dbsvc = DatabaseSvc::instance();
147 static int save_run = 0;
148 if( new_run == save_run )
return;
150 cout <<
"Begin New Run " << new_run << endl;
151 getVertexTableInfo(new_run);
158 if( !m_isRunNumberValid ) {
159 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
160 memset(m_primaryVertex,0,
sizeof(m_primaryVertex));
162 return m_primaryVertex;
167 if( !m_isRunNumberValid ) {
168 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
169 memset(m_sigmaPrimaryVertex,0,
sizeof(m_sigmaPrimaryVertex));
171 return m_sigmaPrimaryVertex;
175StatusCode VertexDbSvc::getVertexTableInfo(){
176 MsgStream log(messageService(), name());
177 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
178 int run = eventHeader->runNumber();
181void VertexDbSvc::getVertexTableInfo(
int run)
185 m_isRunNumberValid =
false;
190 log << MSG::INFO <<
"This data is the MC sample with the Run Number: " << run << endreq;
192 cout <<
"This data is the MC sample with the Run Number: " << run << endl;
200 bool ret_vtx = getReadBunchInfo(run);
203 cout <<
" VertexDbSvc:: can not found vertex information for run:"
204 << run <<
", boss version " << m_bossver << endl;
237 m_isRunNumberValid =
true;
239 log << MSG::INFO <<
"Successfully fetch the vertex information for run: "
240 << save_run << endreq;
241 return StatusCode::SUCCESS;
243 cout <<
"Successfully fetch the vertex information for run: "
249bool VertexDbSvc::getReadBunchInfo(
int run)
252 if(m_bossver ==
"default") {
253 if(m_bossRelease ==
"default") {
255 MsgStream log(messageService(), name());
256 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
257 << m_bossRelease <<
"." << endreq;
259 cout <<
"ERROR BossRelease must be set! Current value is "
260 << m_bossRelease <<
"." << endl;
266 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);
269 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
272 MsgStream log(messageService(), name());
273 log << MSG::ERROR <<
"ERROR: can not find records for run = " << run
274 <<
" and BossRelease = " << m_bossRelease << endreq;
276 cout <<
"ERROR: can not find records for run = " << run
277 <<
" and BossRelease = " << m_bossRelease << endl;
282 m_bossver = recordst->
GetString(
"SftVer");
283 m_verpar = recordst->
GetString(
"ParVer");
284 cout <<
"Using the SftVer and ParVer (" << m_bossver
285 <<
", " << m_verpar <<
") for run " << run <<
". " << endl;
289 string stmt =
"select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
291 tmp <<
"from BeamPar where RunNo = " << run
292 <<
" and SftVer=\'" << m_bossver <<
"\'";
293 if( m_verpar ==
"default" ) {
294 tmp <<
" group by ParVer";
296 tmp <<
" and ParVer = " << m_verpar;
302 int row_no = m_dbsvc->
query(dbName,stmt,res);
306 m_primaryVertex[0] = dbrec.
GetDouble(
"Vx");
307 m_primaryVertex[1] = dbrec.
GetDouble(
"Vy");
308 m_primaryVertex[2] = dbrec.
GetDouble(
"Vz");
309 m_sigmaPrimaryVertex[0] = dbrec.
GetDouble(
"SigmaVx");
310 m_sigmaPrimaryVertex[1] = dbrec.
GetDouble(
"SigmaVy");
311 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()
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)