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"
50 base_class(name, svcloc){
52 declareProperty(
"Host" , host = std::string(
"bes3db2.ihep.ac.cn"));
53 declareProperty(
"DbName" , dbName = std::string(
"offlinedb"));
54 declareProperty(
"UserName" , userName = std::string(
"guest"));
55 declareProperty(
"Password" , password = std::string(
"guestpass"));
56 declareProperty(
"BossVer" , m_bossver = std::string(
"default"));
57 declareProperty(
"VerPar" , m_verpar = std::string(
"default"));
58 declareProperty(
"BossRelease",m_bossRelease = std::string(
"default"));
59 declareProperty(
"ReadOneTime",m_readOneTime=
false);
60 declareProperty(
"RunFrom", m_runFrom=8093);
61 declareProperty(
"RunTo",m_runTo=9025);
62 declareProperty(
"RunIdList",m_runIdList);
78 MsgStream log(messageService(), name());
79 log << MSG::INFO <<
"VertexDbSvc::initialize()" << endreq;
81 StatusCode sc = Service::initialize();
82 if( sc.isFailure() )
return sc;
86 sc = service(
"IncidentSvc", incsvc);
89 incsvc -> addListener(
this,
"NewRun", priority);
92 sc = serviceLocator()->service(
"DatabaseSvc",m_dbsvc,
true);
93 if (sc .isFailure() ) {
94 log << MSG::ERROR <<
"Unable to find DatabaseSvc " << endreq;
98 sc = serviceLocator()->service(
"EventDataSvc", m_eventSvc,
true);
99 if (sc .isFailure() ) {
100 log << MSG::ERROR <<
"Unable to find EventDataSvc " << endreq;
110 for(
unsigned int i = 0; i < m_runIdList.size(); i++) {
111 m_runFrom=m_runIdList[i];
113 m_runTo=m_runIdList[i];
116 getReadBunchInfo(m_runFrom, m_runTo);
119 std::cout<<
"VertexDbSvc, invalid RunFrom, RunFrom should be >=8093"<<std::endl;
123 return StatusCode::SUCCESS;
127 MsgStream log(messageService(), name());
128 log << MSG::INFO <<
"VertexDbSvc::finalize()" << endreq;
130 return StatusCode::SUCCESS;
134 MsgStream log( messageService(), name() );
135 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
137 if ( inc.type() ==
"NewRun" ){
138 log << MSG::DEBUG <<
"NewRun" << endreq;
140 getVertexTableInfo();
142 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
143 int run = eventHeader->runNumber();
146 if((m_mapPrimaryVertex[run]).size()>0) {
147 m_isRunNumberValid =
true;
148 m_primaryVertex[0] = (m_mapPrimaryVertex[run])[0];
149 m_primaryVertex[1] = (m_mapPrimaryVertex[run])[1];
150 m_primaryVertex[2] = (m_mapPrimaryVertex[run])[2];
151 m_sigmaPrimaryVertex[0] = (m_mapPrimaryVertex[run])[3];
152 m_sigmaPrimaryVertex[1] = (m_mapPrimaryVertex[run])[4];
153 m_sigmaPrimaryVertex[2] = (m_mapPrimaryVertex[run])[5];
156 std::cout<<
"VertexDbSvc, could not get vertex infor in handle new run"<<std::endl;
172 dbName =
"offlinedb";
173#ifdef ROOTEVENTDATAVERSION
174 m_bossver = ROOTEVENTDATAVERSION;
175 m_bossRelease = ROOTEVENTDATAVERSION;
177 m_bossver =
"default";
178 m_bossRelease =
"default";
180 m_verpar =
"default";
182 m_dbsvc = DatabaseSvc::instance();
189 static int save_run = 0;
190 if( new_run == save_run )
return;
191 getVertexTableInfo(new_run);
198 if( !m_isRunNumberValid ) {
199 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
200 memset(m_primaryVertex,0,
sizeof(m_primaryVertex));
202 return m_primaryVertex;
207 if( !m_isRunNumberValid ) {
208 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
209 memset(m_sigmaPrimaryVertex,0,
sizeof(m_sigmaPrimaryVertex));
211 return m_sigmaPrimaryVertex;
215StatusCode VertexDbSvc::getVertexTableInfo(){
216 MsgStream log(messageService(), name());
217 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
218 int run = eventHeader->runNumber();
221void VertexDbSvc::getVertexTableInfo(
int run)
225 m_isRunNumberValid =
false;
230 log << MSG::INFO <<
"This data is the MC sample with the Run Number: " << run << endreq;
232 cout <<
"This data is the MC sample with the Run Number: " << run << endl;
240 bool ret_vtx = getReadBunchInfo(run);
243 cout <<
" VertexDbSvc:: can not found vertex information for run:"
244 << run <<
", boss version " << m_bossver << endl;
277 m_isRunNumberValid =
true;
279 log << MSG::INFO <<
"Successfully fetch the vertex information for run: "
280 << save_run << endreq;
281 return StatusCode::SUCCESS;
283 cout <<
"Successfully fetch the vertex information for run: "
289bool VertexDbSvc::getReadBunchInfo(
int run)
292 if(m_bossver ==
"default") {
293 if(m_bossRelease ==
"default") {
295 MsgStream log(messageService(), name());
296 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
297 << m_bossRelease <<
"." << endreq;
299 cout <<
"ERROR BossRelease must be set! Current value is "
300 << m_bossRelease <<
"." << endl;
306 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);
309 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
312 MsgStream log(messageService(), name());
313 log << MSG::ERROR <<
"ERROR: can not find records for run = " << run
314 <<
" and BossRelease = " << m_bossRelease << endreq;
316 cout <<
"ERROR: can not find records for run = " << run
317 <<
" and BossRelease = " << m_bossRelease << endl;
322 m_bossver = recordst->
GetString(
"SftVer");
323 m_verpar = recordst->
GetString(
"ParVer");
324 cout <<
"Using the SftVer and ParVer (" << m_bossver
325 <<
", " << m_verpar <<
") for run " << run <<
". " << endl;
329 string stmt =
"select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
331 tmp <<
"from BeamPar where RunNo = " << run
332 <<
" and SftVer=\'" << m_bossver <<
"\'";
333 if( m_verpar ==
"default" ) {
334 tmp <<
" group by ParVer";
336 tmp <<
" and ParVer = " << m_verpar;
342 int row_no = m_dbsvc->
query(dbName,stmt,res);
346 m_primaryVertex[0] = dbrec.
GetDouble(
"Vx");
347 m_primaryVertex[1] = dbrec.
GetDouble(
"Vy");
348 m_primaryVertex[2] = dbrec.
GetDouble(
"Vz");
349 m_sigmaPrimaryVertex[0] = dbrec.
GetDouble(
"SigmaVx");
350 m_sigmaPrimaryVertex[1] = dbrec.
GetDouble(
"SigmaVy");
351 m_sigmaPrimaryVertex[2] = dbrec.
GetDouble(
"SigmaVz");
358bool VertexDbSvc::getReadBunchInfo(
int runFrom,
int runTo)
361 if(m_bossver ==
"default") {
362 if(m_bossRelease ==
"default") {
364 MsgStream log(messageService(), name());
365 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
366 << m_bossRelease <<
"." << endreq;
368 cout <<
"ERROR BossRelease must be set! Current value is "
369 << m_bossRelease <<
"." << endl;
375 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);
378 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
381 MsgStream log(messageService(), name());
382 log << MSG::ERROR <<
"ERROR: can not find records for run = " << runFrom
383 <<
" and BossRelease = " << m_bossRelease << endreq;
385 cout <<
"ERROR: can not find records for run = " << runFrom
386 <<
" and BossRelease = " << m_bossRelease << endl;
391 m_bossver = recordst->
GetString(
"SftVer");
392 m_verpar = recordst->
GetString(
"ParVer");
393 cout <<
"Using the SftVer and ParVer (" << m_bossver
394 <<
", " << m_verpar <<
") for run " << runFrom <<
". " << endl;
398 string stmt =
"select RunNo, Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
400 tmp <<
"from BeamPar where RunNo >= " << runFrom <<
" and RunNo <= "<< runTo
401 <<
" and SftVer=\'" << m_bossver <<
"\'";
402 if( m_verpar ==
"default" ) {
403 tmp <<
" group by ParVer";
405 tmp <<
" and ParVer = " << m_verpar;
411 int row_no = m_dbsvc->
query(dbName,stmt,res);
413 std::cout<<
"VertexDbSvc get all vertex info, row_no: "<<row_no<<std::endl;
415 for(
int i=0;i<row_no;i++){
417 int run = dbrec.
GetInt(
"RunNo");
418 std::vector<double> vertex;
422 vertex.push_back(dbrec.
GetDouble(
"SigmaVx"));
423 vertex.push_back(dbrec.
GetDouble(
"SigmaVy"));
424 vertex.push_back(dbrec.
GetDouble(
"SigmaVz"));
425 m_mapPrimaryVertex[run]=vertex;
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)
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()
double * SigmaPrimaryVertex()
virtual StatusCode finalize()