BOSS 7.1.0
BESIII Offline Software System
Loading...
Searching...
No Matches
VertexDbSvc.cxx
Go to the documentation of this file.
1// This service is used to read the vertex information from the database
2//
3// an example to use this service is shown in test/test_read.cxx
4//
5// the joboption for the example is shown in share/job-test.txt
6//
7// the joboption for this service is shown in share/jobOptions_VertexDbSvc.txt
8
9
10#include <iostream>
11#include <sstream>
12#include <cstring>
13#include <cstdlib>
14#include <cstdio>
15
16#ifndef BEAN
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"
27
28
29#include "GaudiKernel/IIncidentSvc.h"
30#include "GaudiKernel/Incident.h"
31#include "GaudiKernel/IIncidentListener.h"
32
33#include "GaudiKernel/ISvcLocator.h"
34#include "GaudiKernel/Bootstrap.h"
36#include "EventModel/Event.h"
38
39#include <TMath.h>
40#else
42#endif
43
44using namespace std;
45
46
47#ifndef BEAN
48DECLARE_COMPONENT(VertexDbSvc)
49VertexDbSvc::VertexDbSvc( const string& name, ISvcLocator* svcloc) :
50 base_class(name, svcloc){
51 // declare properties
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}
63
65}
66
67/*StatusCode VertexDbSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
68 if( IID_IVertexDbSvc.versionMatch(riid) ){
69 *ppvInterface = static_cast<IVertexDbSvc*> (this);
70 } else{
71 return Service::queryInterface(riid, ppvInterface);
72 }
73 return StatusCode::SUCCESS;
74}*/
75
77 MsgStream log(messageService(), name());
78 log << MSG::INFO << "VertexDbSvc::initialize()" << endreq;
79
80 StatusCode sc = Service::initialize();
81 if( sc.isFailure() ) return sc;
82
83
84 IIncidentSvc* incsvc;
85 sc = service("IncidentSvc", incsvc);
86 int priority = 100;
87 if( sc.isSuccess() ){
88 incsvc -> addListener(this, "NewRun", priority);
89 }
90
91 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
92 if (sc .isFailure() ) {
93 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
94 return sc;
95 }
96
97 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
98 if (sc .isFailure() ) {
99 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
100 return sc;
101 }
102 if(m_readOneTime){
103 if(m_runFrom>=8093){
104 getReadBunchInfo(m_runFrom, m_runTo);
105 }
106 else
107 std::cout<<"VertexDbSvc, invalid RunFrom, RunFrom should be >=8093"<<std::endl;
108 }
109 return StatusCode::SUCCESS;
110}
111
113 MsgStream log(messageService(), name());
114 log << MSG::INFO << "VertexDbSvc::finalize()" << endreq;
115 // if(m_connect_offline) delete m_connect_offline;
116 return StatusCode::SUCCESS;
117}
118
119void VertexDbSvc::handle(const Incident& inc){
120 MsgStream log( messageService(), name() );
121 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
122
123 if ( inc.type() == "NewRun" ){
124 log << MSG::DEBUG << "NewRun" << endreq;
125 if(!m_readOneTime) {
126 getVertexTableInfo();
127 } else {
128 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
129 int run = eventHeader->runNumber();
130 //cout << endl << "New Run: " << run << " VertexDB vertex= ";
131 if(run<0) run=-run;
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];
140 //cout << m_primaryVertex[0] << "," << m_primaryVertex[1] << "," << m_primaryVertex[2] << " sigma= " << m_sigmaPrimaryVertex[0] << "," << m_sigmaPrimaryVertex[1] << "," << m_sigmaPrimaryVertex[2] << endl;
141 } else {
142 std::cout<<"VertexDbSvc, could not get vertex infor in handle new run"<<std::endl;
143 }
144 }
145 }
146}
147
148#else
149// -------------------------- BEAN ------------------------------------
150
151VertexDbSvc* VertexDbSvc::m_vdb = 0;
152
153//-----------------------------------------------------------------------------
155//-----------------------------------------------------------------------------
156{
157 // use functions instead of "declareProperty"
158 dbName = "offlinedb";
159#ifdef ROOTEVENTDATAVERSION
160 m_bossver = ROOTEVENTDATAVERSION;
161 m_bossRelease = ROOTEVENTDATAVERSION;
162#else
163 m_bossver = "default";
164 m_bossRelease = "default";
165#endif
166 m_verpar = "default";
167
168 m_dbsvc = DatabaseSvc::instance();
169}
170
171//-----------------------------------------------------------------------------
172void VertexDbSvc::handle(int new_run)
173//-----------------------------------------------------------------------------
174{
175 static int save_run = 0;
176 if( new_run == save_run ) return;
177 getVertexTableInfo(new_run);
178 save_run = new_run;
179}
180#endif
181
183{
184 if( !m_isRunNumberValid ) {
185 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
186 memset(m_primaryVertex,0,sizeof(m_primaryVertex));
187 }
188 return m_primaryVertex;
189}
190
192{
193 if( !m_isRunNumberValid ) {
194 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
195 memset(m_sigmaPrimaryVertex,0,sizeof(m_sigmaPrimaryVertex));
196 }
197 return m_sigmaPrimaryVertex;
198}
199
200#ifndef BEAN
201StatusCode VertexDbSvc::getVertexTableInfo(){
202 MsgStream log(messageService(), name());
203 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
204 int run = eventHeader->runNumber();
205#else
206//-----------------------------------------------------------------------------
207void VertexDbSvc::getVertexTableInfo(int run)
208//-----------------------------------------------------------------------------
209{
210#endif
211 m_isRunNumberValid = false;
212 int save_run = run;
213
214 if( run < 0 ) {
215#ifndef BEAN
216 log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
217#else
218 cout << "This data is the MC sample with the Run Number: " << run << endl;
219#endif
220 run = -run;
221 }
222//#ifndef BEAN
223// if(m_bossver=="default") m_bossver = getenv("BES_RELEASE");
224//#endif
225
226 bool ret_vtx = getReadBunchInfo(run);
227
228 if( !ret_vtx ) {
229 cout << " VertexDbSvc:: can not found vertex information for run:"
230 << run << ", boss version " << m_bossver << endl;
231 exit(1);
232 }
233
234/* if( !ret_vtx && save_run<0 ) {
235 bool ret = false;
236 int real_run = run;
237 for(int kk = 1; kk <= 10000; kk++) {
238 real_run = run+kk;
239 if( (ret = getReadBunchInfo(real_run)) ) break;
240
241 if( run-kk > 0 ) {
242 real_run = run-kk;
243 if( (ret = getReadBunchInfo(real_run)) ) break;
244 }
245 }
246 if( !ret ) {
247#ifndef BEAN
248 log << MSG::ERROR << "Can not find vertex information for run:" <<run<< endreq;
249#else
250 cout << "Can not find vertex information for run:" << run << endl;
251#endif
252 exit(1);
253 }
254#ifndef BEAN
255 log << MSG::INFO << "Use Bunch infor. of run " << real_run
256 << " instead of run " << run << endreq;
257#else
258 cout << "Use Bunch infor. of run " << real_run
259 << " instead of run " << run << endl;
260#endif
261 }
262*/
263 m_isRunNumberValid = true;
264#ifndef BEAN
265 log << MSG::INFO << "Successfully fetch the vertex information for run: "
266 << save_run << endreq;
267 return StatusCode::SUCCESS;
268#else
269 cout << "Successfully fetch the vertex information for run: "
270 << save_run << endl;
271#endif
272}
273
274//-----------------------------------------------------------------------------
275bool VertexDbSvc::getReadBunchInfo(int run)
276//-----------------------------------------------------------------------------
277{
278 if(m_bossver == "default") {
279 if(m_bossRelease == "default") {
280#ifndef BEAN
281 MsgStream log(messageService(), name());
282 log << MSG::FATAL << "ERROR BossRelease must be set! Current value is "
283 << m_bossRelease << "." << endreq;
284#else
285 cout << "ERROR BossRelease must be set! Current value is "
286 << m_bossRelease << "." << endl;
287#endif
288 exit(1);
289 }
290 else {
291 char stmt1[400];
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);
293
294 DatabaseRecordVector records;
295 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
296 if(rowNo == 0) {
297#ifndef BEAN
298 MsgStream log(messageService(), name());
299 log << MSG::ERROR << "ERROR: can not find records for run = " << run
300 << " and BossRelease = " << m_bossRelease << endreq;
301#else
302 cout << "ERROR: can not find records for run = " << run
303 << " and BossRelease = " << m_bossRelease << endl;
304#endif
305 exit(1);
306 }
307 DatabaseRecord* recordst = records[0];
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;
312 }
313 }
314
315 string stmt = "select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
316 stringstream tmp;
317 tmp << "from BeamPar where RunNo = " << run
318 << " and SftVer=\'" << m_bossver << "\'";
319 if( m_verpar == "default" ) {
320 tmp << " group by ParVer";
321 } else {
322 tmp << " and ParVer = " << m_verpar;
323 }
324 stmt += tmp.str();
325 // cerr << "query(" << dbName << ", " << stmt << ", res);" << endl;
326
328 int row_no = m_dbsvc->query(dbName,stmt,res);
329
330 if( row_no > 0 ) {
331 DatabaseRecord& dbrec = *res[row_no-1];
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");
338 return true;
339 }
340
341 return false;
342}
343
344bool VertexDbSvc::getReadBunchInfo(int runFrom, int runTo)
345//-----------------------------------------------------------------------------
346{
347 if(m_bossver == "default") {
348 if(m_bossRelease == "default") {
349#ifndef BEAN
350 MsgStream log(messageService(), name());
351 log << MSG::FATAL << "ERROR BossRelease must be set! Current value is "
352 << m_bossRelease << "." << endreq;
353#else
354 cout << "ERROR BossRelease must be set! Current value is "
355 << m_bossRelease << "." << endl;
356#endif
357 exit(1);
358 }
359 else {
360 char stmt1[400];
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);
362
363 DatabaseRecordVector records;
364 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
365 if(rowNo == 0) {
366#ifndef BEAN
367 MsgStream log(messageService(), name());
368 log << MSG::ERROR << "ERROR: can not find records for run = " << runFrom
369 << " and BossRelease = " << m_bossRelease << endreq;
370#else
371 cout << "ERROR: can not find records for run = " << runFrom
372 << " and BossRelease = " << m_bossRelease << endl;
373#endif
374 exit(1);
375 }
376 DatabaseRecord* recordst = records[0];
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;
381 }
382 }
383
384 string stmt = "select RunNo, Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
385 stringstream tmp;
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";
390 } else {
391 tmp << " and ParVer = " << m_verpar;
392 }
393 stmt += tmp.str();
394 // cerr << "query(" << dbName << ", " << stmt << ", res);" << endl;
395
397 int row_no = m_dbsvc->query(dbName,stmt,res);
398
399 std::cout<<"VertexDbSvc get all vertex info, row_no: "<<row_no<<std::endl;
400 if( row_no > 0 ) {
401 for(int i=0;i<row_no;i++){
402 DatabaseRecord& dbrec = *res[i];
403 int run = dbrec.GetInt("RunNo");
404 std::vector<double> vertex;
405 vertex.push_back(dbrec.GetDouble("Vx"));
406 vertex.push_back(dbrec.GetDouble("Vy"));
407 vertex.push_back(dbrec.GetDouble("Vz"));
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;
412 }
413 return true;
414 }
415
416 return false;
417}
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)
Definition: VertexDbSvc.cxx:49
void handle(const Incident &)
virtual StatusCode initialize()
Definition: VertexDbSvc.cxx:76
double * PrimaryVertex()
double * SigmaPrimaryVertex()
virtual StatusCode finalize()