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"
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"));
58 declareProperty(
"ReadOneTime",m_readOneTime=
false);
59 declareProperty(
"RunFrom", m_runFrom=8093);
60 declareProperty(
"RunTo",m_runTo=9025);
67 if( IID_IVertexDbSvc.versionMatch(riid) ){
70 return Service::queryInterface(riid, ppvInterface);
72 return StatusCode::SUCCESS;
76 MsgStream log(messageService(), name());
77 log << MSG::INFO <<
"VertexDbSvc::initialize()" << endreq;
79 StatusCode sc = Service::initialize();
80 if( sc.isFailure() )
return sc;
84 sc = service(
"IncidentSvc", incsvc);
87 incsvc -> addListener(
this,
"NewRun", priority);
90 sc = serviceLocator()->service(
"DatabaseSvc",m_dbsvc,
true);
91 if (sc .isFailure() ) {
92 log << MSG::ERROR <<
"Unable to find DatabaseSvc " << endreq;
96 sc = serviceLocator()->service(
"EventDataSvc", m_eventSvc,
true);
97 if (sc .isFailure() ) {
98 log << MSG::ERROR <<
"Unable to find EventDataSvc " << endreq;
103 getReadBunchInfo(m_runFrom, m_runTo);
106 std::cout<<
"VertexDbSvc, invalid RunFrom, RunFrom should be >=8093"<<std::endl;
108 return StatusCode::SUCCESS;
112 MsgStream log(messageService(), name());
113 log << MSG::INFO <<
"VertexDbSvc::finalize()" << endreq;
115 return StatusCode::SUCCESS;
119 MsgStream log( messageService(), name() );
120 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
122 if ( inc.type() ==
"NewRun" ){
123 log << MSG::DEBUG <<
"NewRun" << endreq;
125 getVertexTableInfo();
127 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
128 int run = eventHeader->runNumber();
131 if((m_mapPrimaryVertex[run]).size()>0) {
132 m_isRunNumberValid =
true;
133 m_primaryVertex[0] = (m_mapPrimaryVertex[run])[0];
134 m_primaryVertex[1] = (m_mapPrimaryVertex[run])[1];
135 m_primaryVertex[2] = (m_mapPrimaryVertex[run])[2];
136 m_sigmaPrimaryVertex[0] = (m_mapPrimaryVertex[run])[3];
137 m_sigmaPrimaryVertex[1] = (m_mapPrimaryVertex[run])[4];
138 m_sigmaPrimaryVertex[2] = (m_mapPrimaryVertex[run])[5];
141 std::cout<<
"VertexDbSvc, could not get vertex infor in handle new run"<<std::endl;
157 dbName =
"offlinedb";
158#ifdef ROOTEVENTDATAVERSION
159 m_bossver = ROOTEVENTDATAVERSION;
160 m_bossRelease = ROOTEVENTDATAVERSION;
162 m_bossver =
"default";
163 m_bossRelease =
"default";
165 m_verpar =
"default";
167 m_dbsvc = DatabaseSvc::instance();
174 static int save_run = 0;
175 if( new_run == save_run )
return;
176 getVertexTableInfo(new_run);
183 if( !m_isRunNumberValid ) {
184 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
185 memset(m_primaryVertex,0,
sizeof(m_primaryVertex));
187 return m_primaryVertex;
192 if( !m_isRunNumberValid ) {
193 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
194 memset(m_sigmaPrimaryVertex,0,
sizeof(m_sigmaPrimaryVertex));
196 return m_sigmaPrimaryVertex;
200StatusCode VertexDbSvc::getVertexTableInfo(){
201 MsgStream log(messageService(), name());
202 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
203 int run = eventHeader->runNumber();
206void VertexDbSvc::getVertexTableInfo(
int run)
210 m_isRunNumberValid =
false;
215 log << MSG::INFO <<
"This data is the MC sample with the Run Number: " << run << endreq;
217 cout <<
"This data is the MC sample with the Run Number: " << run << endl;
225 bool ret_vtx = getReadBunchInfo(run);
228 cout <<
" VertexDbSvc:: can not found vertex information for run:"
229 << run <<
", boss version " << m_bossver << endl;
262 m_isRunNumberValid =
true;
264 log << MSG::INFO <<
"Successfully fetch the vertex information for run: "
265 << save_run << endreq;
266 return StatusCode::SUCCESS;
268 cout <<
"Successfully fetch the vertex information for run: "
274bool VertexDbSvc::getReadBunchInfo(
int run)
277 if(m_bossver ==
"default") {
278 if(m_bossRelease ==
"default") {
280 MsgStream log(messageService(), name());
281 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
282 << m_bossRelease <<
"." << endreq;
284 cout <<
"ERROR BossRelease must be set! Current value is "
285 << m_bossRelease <<
"." << endl;
291 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);
294 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
297 MsgStream log(messageService(), name());
298 log << MSG::ERROR <<
"ERROR: can not find records for run = " << run
299 <<
" and BossRelease = " << m_bossRelease << endreq;
301 cout <<
"ERROR: can not find records for run = " << run
302 <<
" and BossRelease = " << m_bossRelease << endl;
307 m_bossver = recordst->
GetString(
"SftVer");
308 m_verpar = recordst->
GetString(
"ParVer");
309 cout <<
"Using the SftVer and ParVer (" << m_bossver
310 <<
", " << m_verpar <<
") for run " << run <<
". " << endl;
314 string stmt =
"select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
316 tmp <<
"from BeamPar where RunNo = " << run
317 <<
" and SftVer=\'" << m_bossver <<
"\'";
318 if( m_verpar ==
"default" ) {
319 tmp <<
" group by ParVer";
321 tmp <<
" and ParVer = " << m_verpar;
327 int row_no = m_dbsvc->
query(dbName,stmt,res);
331 m_primaryVertex[0] = dbrec.
GetDouble(
"Vx");
332 m_primaryVertex[1] = dbrec.
GetDouble(
"Vy");
333 m_primaryVertex[2] = dbrec.
GetDouble(
"Vz");
334 m_sigmaPrimaryVertex[0] = dbrec.
GetDouble(
"SigmaVx");
335 m_sigmaPrimaryVertex[1] = dbrec.
GetDouble(
"SigmaVy");
336 m_sigmaPrimaryVertex[2] = dbrec.
GetDouble(
"SigmaVz");
343bool VertexDbSvc::getReadBunchInfo(
int runFrom,
int runTo)
346 if(m_bossver ==
"default") {
347 if(m_bossRelease ==
"default") {
349 MsgStream log(messageService(), name());
350 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
351 << m_bossRelease <<
"." << endreq;
353 cout <<
"ERROR BossRelease must be set! Current value is "
354 << m_bossRelease <<
"." << endl;
360 sprintf(stmt1,
"select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", m_bossRelease.c_str(), runFrom, runTo);
363 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
366 MsgStream log(messageService(), name());
367 log << MSG::ERROR <<
"ERROR: can not find records for run = " << runFrom
368 <<
" and BossRelease = " << m_bossRelease << endreq;
370 cout <<
"ERROR: can not find records for run = " << runFrom
371 <<
" and BossRelease = " << m_bossRelease << endl;
376 m_bossver = recordst->
GetString(
"SftVer");
377 m_verpar = recordst->
GetString(
"ParVer");
378 cout <<
"Using the SftVer and ParVer (" << m_bossver
379 <<
", " << m_verpar <<
") for run " << runFrom <<
". " << endl;
383 string stmt =
"select RunNo, Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
385 tmp <<
"from BeamPar where RunNo >= " << runFrom <<
" and RunNo <= "<< runTo
386 <<
" and SftVer=\'" << m_bossver <<
"\'";
387 if( m_verpar ==
"default" ) {
388 tmp <<
" group by ParVer";
390 tmp <<
" and ParVer = " << m_verpar;
396 int row_no = m_dbsvc->
query(dbName,stmt,res);
398 std::cout<<
"VertexDbSvc get all vertex info, row_no: "<<row_no<<std::endl;
400 for(
int i=0;i<row_no;i++){
402 int run = dbrec.
GetInt(
"RunNo");
403 std::vector<double> vertex;
407 vertex.push_back(dbrec.
GetDouble(
"SigmaVx"));
408 vertex.push_back(dbrec.
GetDouble(
"SigmaVy"));
409 vertex.push_back(dbrec.
GetDouble(
"SigmaVz"));
410 m_mapPrimaryVertex[run]=vertex;
double GetDouble(std::string key)
int GetInt(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)