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