BOSS 7.0.2
BESIII Offline Software System
Loading...
Searching...
No Matches
TofEnergyCalibSvc.cxx
Go to the documentation of this file.
1// This service is used to read the TofEnergyCalibSvc 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_TofEnergyCalibSvc.txt
8
9
10#include <iostream>
11#include <sstream>
12#include <cstring>
13#include <cstdlib>
14#include <cstdio>
15
16#include "TofEnergyCalibSvc/TofEnergyCalibSvc.h"
17#include "GaudiKernel/Kernel.h"
18#include "GaudiKernel/IInterface.h"
19#include "GaudiKernel/StatusCode.h"
20#include "GaudiKernel/SvcFactory.h"
21#include "GaudiKernel/MsgStream.h"
22#include "GaudiKernel/ISvcLocator.h"
23#include "GaudiKernel/SmartDataPtr.h"
24#include "GaudiKernel/IDataProviderSvc.h"
25#include "GaudiKernel/PropertyMgr.h"
26
27
28#include "GaudiKernel/IIncidentSvc.h"
29#include "GaudiKernel/Incident.h"
30#include "GaudiKernel/IIncidentListener.h"
31
32#include "GaudiKernel/ISvcLocator.h"
33#include "GaudiKernel/Bootstrap.h"
34#include "EventModel/EventModel.h"
35#include "EventModel/Event.h"
36#include "EventModel/EventHeader.h"
37
38#include <TMath.h>
39
40using namespace std;
41
42
43TofEnergyCalibSvc::TofEnergyCalibSvc( const string& name, ISvcLocator* svcloc) :
44 Service (name, svcloc){
45 // declare properties
46 declareProperty("Host" , host = std::string("bes3db2.ihep.ac.cn"));
47 declareProperty("DbName" , dbName = std::string("offlinedb"));
48 declareProperty("UserName" , userName = std::string("guest"));
49 declareProperty("Password" , password = std::string("guestpass"));
50 declareProperty("IsData" , m_IsData = std::string("default"));
51 declareProperty("BossRelease",m_bossRelease = std::string("default"));
52 // m_dbsvc = DatabaseSvc::instance();
53 }
54
56}
57
58StatusCode TofEnergyCalibSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
59 if( IID_ITofEnergyCalibSvc.versionMatch(riid) ){
60 *ppvInterface = static_cast<ITofEnergyCalibSvc*> (this);
61 } else{
62 return Service::queryInterface(riid, ppvInterface);
63 }
64 return StatusCode::SUCCESS;
65}
66
68 MsgStream log(messageService(), name());
69 log << MSG::INFO << "TofEnergyCalibSvc::initialize()" << endreq;
70
71 StatusCode sc = Service::initialize();
72 if( sc.isFailure() ) return sc;
73
74
75 IIncidentSvc* incsvc;
76 sc = service("IncidentSvc", incsvc);
77 int priority = 100;
78 if( sc.isSuccess() ){
79 incsvc -> addListener(this, "NewRun", priority);
80 }
81
82 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
83 if (sc .isFailure() ) {
84 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
85 return sc;
86 }
87
88 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
89 if (sc .isFailure() ) {
90 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
91 return sc;
92 }
93
94 return StatusCode::SUCCESS;
95}
96
98 MsgStream log(messageService(), name());
99 log << MSG::INFO << "TofEnergyCalibSvc::finalize()" << endreq;
100 // if(m_connect_offline) delete m_connect_offline;
101 return StatusCode::SUCCESS;
102}
103
104void TofEnergyCalibSvc::handle(const Incident& inc){
105 MsgStream log( messageService(), name() );
106 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
107
108 if ( inc.type() == "NewRun" ){
109 log << MSG::DEBUG << "NewRun" << endreq;
111 log << MSG::ERROR << "can not initilize Tof energy Calib Constants" << endreq;
112 }
113 }
114}
115
116
118 MsgStream log(messageService(), name());
119 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
120 int run = eventHeader->runNumber();
121 cout<<"Run in getTofEnergyCalibSvcInfo() is: "<<run<<endl;
122
123 char stmt1[400];
124 if(run<0) {
125 cout << "This data is the MC sample with the Run Number: " << run << endl;
126 run = -run;
127 }
128
129 if(m_bossRelease=="default") m_bossRelease = getenv("BES_RELEASE");
130 sprintf(stmt1, "select CalibConst,Para1,Para2,Para3,Para4,Para5 from TofEnergyCalib where BossVer = '%s' and RunFrom <= %d and RunTo >= %d and IsData = '%s'", m_bossRelease.c_str(), run, run, m_IsData.c_str());
131 cout<<"stmt is:"<<stmt1<<endl;
133 int row_no = m_dbsvc->query("offlinedb",stmt1,res);
134 if( row_no > 0 ) {
135 DatabaseRecord& dbrec = *res[row_no-1];
136 m_CalibConst = dbrec.GetDouble("CalibConst");
137 m_Para1 = dbrec.GetDouble("Para1");
138 m_Para2 = dbrec.GetDouble("Para2");
139 m_Para3 = dbrec.GetDouble("Para3");
140 m_Para4 = dbrec.GetDouble("Para4");
141 m_Para5 = dbrec.GetDouble("Para5");
142 cout<<"m_CalibConst is:"<<m_CalibConst<<endl;
143 cout << "Successfully fetch TofEnergyCalibSvc information for run: "
144 << run << endl;
145 return true;
146 } else if( row_no<=0 ) {
147 cout << " TofEnergyCalibSvc:: can not found TofEnergyCalibSvc information of run:"
148 << run << ", boss version " << m_bossRelease << endl;
149 exit(1);
150 return false;
151 }
152 return true;
153}
154
virtual int query(const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0
virtual StatusCode initialize()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
TofEnergyCalibSvc(const std::string &name, ISvcLocator *svcloc)
virtual StatusCode finalize()
void handle(const Incident &)