BOSS 7.0.4
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}
59
61}
62
63StatusCode VertexDbSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
64 if( IID_IVertexDbSvc.versionMatch(riid) ){
65 *ppvInterface = static_cast<IVertexDbSvc*> (this);
66 } else{
67 return Service::queryInterface(riid, ppvInterface);
68 }
69 return StatusCode::SUCCESS;
70}
71
73 MsgStream log(messageService(), name());
74 log << MSG::INFO << "VertexDbSvc::initialize()" << endreq;
75
76 StatusCode sc = Service::initialize();
77 if( sc.isFailure() ) return sc;
78
79
80 IIncidentSvc* incsvc;
81 sc = service("IncidentSvc", incsvc);
82 int priority = 100;
83 if( sc.isSuccess() ){
84 incsvc -> addListener(this, "NewRun", priority);
85 }
86
87 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
88 if (sc .isFailure() ) {
89 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
90 return sc;
91 }
92
93 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
94 if (sc .isFailure() ) {
95 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
96 return sc;
97 }
98 return StatusCode::SUCCESS;
99}
100
102 MsgStream log(messageService(), name());
103 log << MSG::INFO << "VertexDbSvc::finalize()" << endreq;
104 // if(m_connect_offline) delete m_connect_offline;
105 return StatusCode::SUCCESS;
106}
107
108void VertexDbSvc::handle(const Incident& inc){
109 MsgStream log( messageService(), name() );
110 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
111
112 if ( inc.type() == "NewRun" ){
113 log << MSG::DEBUG << "NewRun" << endreq;
114 m_bossver = "default";
115 getVertexTableInfo();
116 }
117}
118
119
120#else
121// -------------------------- BEAN ------------------------------------
122
123VertexDbSvc* VertexDbSvc::m_vdb = 0;
124
125//-----------------------------------------------------------------------------
127//-----------------------------------------------------------------------------
128{
129 // use functions instead of "declareProperty"
130 dbName = "offlinedb";
131#ifdef ROOTEVENTDATAVERSION
132 m_bossver = ROOTEVENTDATAVERSION;
133 m_bossRelease = ROOTEVENTDATAVERSION;
134#else
135 m_bossver = "default";
136 m_bossRelease = "default";
137#endif
138 m_verpar = "default";
139
140 m_dbsvc = DatabaseSvc::instance();
141}
142
143//-----------------------------------------------------------------------------
144void VertexDbSvc::handle(int new_run)
145//-----------------------------------------------------------------------------
146{
147 static int save_run = 0;
148 if( new_run == save_run ) return;
149
150 cout << "Begin New Run " << new_run << endl;
151 getVertexTableInfo(new_run);
152 save_run = new_run;
153}
154#endif
155
157{
158 if( !m_isRunNumberValid ) {
159 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
160 memset(m_primaryVertex,0,sizeof(m_primaryVertex));
161 }
162 return m_primaryVertex;
163}
164
166{
167 if( !m_isRunNumberValid ) {
168 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
169 memset(m_sigmaPrimaryVertex,0,sizeof(m_sigmaPrimaryVertex));
170 }
171 return m_sigmaPrimaryVertex;
172}
173
174#ifndef BEAN
175StatusCode VertexDbSvc::getVertexTableInfo(){
176 MsgStream log(messageService(), name());
177 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
178 int run = eventHeader->runNumber();
179#else
180//-----------------------------------------------------------------------------
181void VertexDbSvc::getVertexTableInfo(int run)
182//-----------------------------------------------------------------------------
183{
184#endif
185 m_isRunNumberValid = false;
186 int save_run = run;
187
188 if( run < 0 ) {
189#ifndef BEAN
190 log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
191#else
192 cout << "This data is the MC sample with the Run Number: " << run << endl;
193#endif
194 run = -run;
195 }
196//#ifndef BEAN
197// if(m_bossver=="default") m_bossver = getenv("BES_RELEASE");
198//#endif
199
200 bool ret_vtx = getReadBunchInfo(run);
201
202 if( !ret_vtx ) {
203 cout << " VertexDbSvc:: can not found vertex information for run:"
204 << run << ", boss version " << m_bossver << endl;
205 exit(1);
206 }
207
208/* if( !ret_vtx && save_run<0 ) {
209 bool ret = false;
210 int real_run = run;
211 for(int kk = 1; kk <= 10000; kk++) {
212 real_run = run+kk;
213 if( (ret = getReadBunchInfo(real_run)) ) break;
214
215 if( run-kk > 0 ) {
216 real_run = run-kk;
217 if( (ret = getReadBunchInfo(real_run)) ) break;
218 }
219 }
220 if( !ret ) {
221#ifndef BEAN
222 log << MSG::ERROR << "Can not find vertex information for run:" <<run<< endreq;
223#else
224 cout << "Can not find vertex information for run:" << run << endl;
225#endif
226 exit(1);
227 }
228#ifndef BEAN
229 log << MSG::INFO << "Use Bunch infor. of run " << real_run
230 << " instead of run " << run << endreq;
231#else
232 cout << "Use Bunch infor. of run " << real_run
233 << " instead of run " << run << endl;
234#endif
235 }
236*/
237 m_isRunNumberValid = true;
238#ifndef BEAN
239 log << MSG::INFO << "Successfully fetch the vertex information for run: "
240 << save_run << endreq;
241 return StatusCode::SUCCESS;
242#else
243 cout << "Successfully fetch the vertex information for run: "
244 << save_run << endl;
245#endif
246}
247
248//-----------------------------------------------------------------------------
249bool VertexDbSvc::getReadBunchInfo(int run)
250//-----------------------------------------------------------------------------
251{
252 if(m_bossver == "default") {
253 if(m_bossRelease == "default") {
254#ifndef BEAN
255 MsgStream log(messageService(), name());
256 log << MSG::FATAL << "ERROR BossRelease must be set! Current value is "
257 << m_bossRelease << "." << endreq;
258#else
259 cout << "ERROR BossRelease must be set! Current value is "
260 << m_bossRelease << "." << endl;
261#endif
262 exit(1);
263 }
264 else {
265 char stmt1[400];
266 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);
267
268 DatabaseRecordVector records;
269 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
270 if(rowNo == 0) {
271#ifndef BEAN
272 MsgStream log(messageService(), name());
273 log << MSG::ERROR << "ERROR: can not find records for run = " << run
274 << " and BossRelease = " << m_bossRelease << endreq;
275#else
276 cout << "ERROR: can not find records for run = " << run
277 << " and BossRelease = " << m_bossRelease << endl;
278#endif
279 exit(1);
280 }
281 DatabaseRecord* recordst = records[0];
282 m_bossver = recordst->GetString("SftVer");
283 m_verpar = recordst->GetString("ParVer");
284 cout << "Using the SftVer and ParVer (" << m_bossver
285 << ", " << m_verpar << ") for run " << run << ". " << endl;
286 }
287 }
288
289 string stmt = "select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
290 stringstream tmp;
291 tmp << "from BeamPar where RunNo = " << run
292 << " and SftVer=\'" << m_bossver << "\'";
293 if( m_verpar == "default" ) {
294 tmp << " group by ParVer";
295 } else {
296 tmp << " and ParVer = " << m_verpar;
297 }
298 stmt += tmp.str();
299 // cerr << "query(" << dbName << ", " << stmt << ", res);" << endl;
300
302 int row_no = m_dbsvc->query(dbName,stmt,res);
303
304 if( row_no > 0 ) {
305 DatabaseRecord& dbrec = *res[row_no-1];
306 m_primaryVertex[0] = dbrec.GetDouble("Vx");
307 m_primaryVertex[1] = dbrec.GetDouble("Vy");
308 m_primaryVertex[2] = dbrec.GetDouble("Vz");
309 m_sigmaPrimaryVertex[0] = dbrec.GetDouble("SigmaVx");
310 m_sigmaPrimaryVertex[1] = dbrec.GetDouble("SigmaVy");
311 m_sigmaPrimaryVertex[2] = dbrec.GetDouble("SigmaVz");
312 return true;
313 }
314
315 return false;
316}
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:72
double * PrimaryVertex()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
Definition: VertexDbSvc.cxx:63
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)