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);
77 MsgStream log(messageService(), name());
78 log << MSG::INFO <<
"VertexDbSvc::initialize()" << endreq;
80 StatusCode sc = Service::initialize();
81 if( sc.isFailure() )
return sc;
85 sc = service(
"IncidentSvc", incsvc);
88 incsvc -> addListener(
this,
"NewRun", priority);
91 sc = serviceLocator()->service(
"DatabaseSvc",m_dbsvc,
true);
92 if (sc .isFailure() ) {
93 log << MSG::ERROR <<
"Unable to find DatabaseSvc " << endreq;
97 sc = serviceLocator()->service(
"EventDataSvc", m_eventSvc,
true);
98 if (sc .isFailure() ) {
99 log << MSG::ERROR <<
"Unable to find EventDataSvc " << endreq;
104 getReadBunchInfo(m_runFrom, m_runTo);
107 std::cout<<
"VertexDbSvc, invalid RunFrom, RunFrom should be >=8093"<<std::endl;
109 return StatusCode::SUCCESS;
113 MsgStream log(messageService(), name());
114 log << MSG::INFO <<
"VertexDbSvc::finalize()" << endreq;
116 return StatusCode::SUCCESS;
120 MsgStream log( messageService(), name() );
121 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
123 if ( inc.type() ==
"NewRun" ){
124 log << MSG::DEBUG <<
"NewRun" << endreq;
126 getVertexTableInfo();
128 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
129 int run = eventHeader->runNumber();
132 if((m_mapPrimaryVertex[run]).size()>0) {
133 m_isRunNumberValid =
true;
134 m_primaryVertex[0] = (m_mapPrimaryVertex[run])[0];
135 m_primaryVertex[1] = (m_mapPrimaryVertex[run])[1];
136 m_primaryVertex[2] = (m_mapPrimaryVertex[run])[2];
137 m_sigmaPrimaryVertex[0] = (m_mapPrimaryVertex[run])[3];
138 m_sigmaPrimaryVertex[1] = (m_mapPrimaryVertex[run])[4];
139 m_sigmaPrimaryVertex[2] = (m_mapPrimaryVertex[run])[5];
142 std::cout<<
"VertexDbSvc, could not get vertex infor in handle new run"<<std::endl;
158 dbName =
"offlinedb";
159#ifdef ROOTEVENTDATAVERSION
160 m_bossver = ROOTEVENTDATAVERSION;
161 m_bossRelease = ROOTEVENTDATAVERSION;
163 m_bossver =
"default";
164 m_bossRelease =
"default";
166 m_verpar =
"default";
168 m_dbsvc = DatabaseSvc::instance();
175 static int save_run = 0;
176 if( new_run == save_run )
return;
177 getVertexTableInfo(new_run);
184 if( !m_isRunNumberValid ) {
185 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
186 memset(m_primaryVertex,0,
sizeof(m_primaryVertex));
188 return m_primaryVertex;
193 if( !m_isRunNumberValid ) {
194 cerr <<
"WARNING in VertexDbSvc: runNo is invalid!\n";
195 memset(m_sigmaPrimaryVertex,0,
sizeof(m_sigmaPrimaryVertex));
197 return m_sigmaPrimaryVertex;
201StatusCode VertexDbSvc::getVertexTableInfo(){
202 MsgStream log(messageService(), name());
203 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
204 int run = eventHeader->runNumber();
207void VertexDbSvc::getVertexTableInfo(
int run)
211 m_isRunNumberValid =
false;
216 log << MSG::INFO <<
"This data is the MC sample with the Run Number: " << run << endreq;
218 cout <<
"This data is the MC sample with the Run Number: " << run << endl;
226 bool ret_vtx = getReadBunchInfo(run);
229 cout <<
" VertexDbSvc:: can not found vertex information for run:"
230 << run <<
", boss version " << m_bossver << endl;
263 m_isRunNumberValid =
true;
265 log << MSG::INFO <<
"Successfully fetch the vertex information for run: "
266 << save_run << endreq;
267 return StatusCode::SUCCESS;
269 cout <<
"Successfully fetch the vertex information for run: "
275bool VertexDbSvc::getReadBunchInfo(
int run)
278 if(m_bossver ==
"default") {
279 if(m_bossRelease ==
"default") {
281 MsgStream log(messageService(), name());
282 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
283 << m_bossRelease <<
"." << endreq;
285 cout <<
"ERROR BossRelease must be set! Current value is "
286 << m_bossRelease <<
"." << endl;
292 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);
295 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
298 MsgStream log(messageService(), name());
299 log << MSG::ERROR <<
"ERROR: can not find records for run = " << run
300 <<
" and BossRelease = " << m_bossRelease << endreq;
302 cout <<
"ERROR: can not find records for run = " << run
303 <<
" and BossRelease = " << m_bossRelease << endl;
308 m_bossver = recordst->
GetString(
"SftVer");
309 m_verpar = recordst->
GetString(
"ParVer");
310 cout <<
"Using the SftVer and ParVer (" << m_bossver
311 <<
", " << m_verpar <<
") for run " << run <<
". " << endl;
315 string stmt =
"select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
317 tmp <<
"from BeamPar where RunNo = " << run
318 <<
" and SftVer=\'" << m_bossver <<
"\'";
319 if( m_verpar ==
"default" ) {
320 tmp <<
" group by ParVer";
322 tmp <<
" and ParVer = " << m_verpar;
328 int row_no = m_dbsvc->
query(dbName,stmt,res);
332 m_primaryVertex[0] = dbrec.
GetDouble(
"Vx");
333 m_primaryVertex[1] = dbrec.
GetDouble(
"Vy");
334 m_primaryVertex[2] = dbrec.
GetDouble(
"Vz");
335 m_sigmaPrimaryVertex[0] = dbrec.
GetDouble(
"SigmaVx");
336 m_sigmaPrimaryVertex[1] = dbrec.
GetDouble(
"SigmaVy");
337 m_sigmaPrimaryVertex[2] = dbrec.
GetDouble(
"SigmaVz");
344bool VertexDbSvc::getReadBunchInfo(
int runFrom,
int runTo)
347 if(m_bossver ==
"default") {
348 if(m_bossRelease ==
"default") {
350 MsgStream log(messageService(), name());
351 log << MSG::FATAL <<
"ERROR BossRelease must be set! Current value is "
352 << m_bossRelease <<
"." << endreq;
354 cout <<
"ERROR BossRelease must be set! Current value is "
355 << m_bossRelease <<
"." << endl;
361 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);
364 int rowNo = m_dbsvc->
query(
"offlinedb",stmt1,records);
367 MsgStream log(messageService(), name());
368 log << MSG::ERROR <<
"ERROR: can not find records for run = " << runFrom
369 <<
" and BossRelease = " << m_bossRelease << endreq;
371 cout <<
"ERROR: can not find records for run = " << runFrom
372 <<
" and BossRelease = " << m_bossRelease << endl;
377 m_bossver = recordst->
GetString(
"SftVer");
378 m_verpar = recordst->
GetString(
"ParVer");
379 cout <<
"Using the SftVer and ParVer (" << m_bossver
380 <<
", " << m_verpar <<
") for run " << runFrom <<
". " << endl;
384 string stmt =
"select RunNo, Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
386 tmp <<
"from BeamPar where RunNo >= " << runFrom <<
" and RunNo <= "<< runTo
387 <<
" and SftVer=\'" << m_bossver <<
"\'";
388 if( m_verpar ==
"default" ) {
389 tmp <<
" group by ParVer";
391 tmp <<
" and ParVer = " << m_verpar;
397 int row_no = m_dbsvc->
query(dbName,stmt,res);
399 std::cout<<
"VertexDbSvc get all vertex info, row_no: "<<row_no<<std::endl;
401 for(
int i=0;i<row_no;i++){
403 int run = dbrec.
GetInt(
"RunNo");
404 std::vector<double> vertex;
408 vertex.push_back(dbrec.
GetDouble(
"SigmaVx"));
409 vertex.push_back(dbrec.
GetDouble(
"SigmaVy"));
410 vertex.push_back(dbrec.
GetDouble(
"SigmaVz"));
411 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()