BOSS 7.0.7
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
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"
35#include "EventModel/Event.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 declareProperty("TofEnergyPar",m_calPar="default");
53 declareProperty("TofEnergyBossVer",m_bossVer="default");
54 // m_dbsvc = DatabaseSvc::instance();
55 }
56
58}
59
60StatusCode TofEnergyCalibSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
61 if( IID_ITofEnergyCalibSvc.versionMatch(riid) ){
62 *ppvInterface = static_cast<ITofEnergyCalibSvc*> (this);
63 } else{
64 return Service::queryInterface(riid, ppvInterface);
65 }
66 return StatusCode::SUCCESS;
67}
68
70 MsgStream log(messageService(), name());
71 log << MSG::INFO << "TofEnergyCalibSvc::initialize()" << endreq;
72
73 StatusCode sc = Service::initialize();
74 if( sc.isFailure() ) return sc;
75
76
77 IIncidentSvc* incsvc;
78 sc = service("IncidentSvc", incsvc);
79 int priority = 100;
80 if( sc.isSuccess() ){
81 incsvc -> addListener(this, "NewRun", priority);
82 }
83
84 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
85 if (sc .isFailure() ) {
86 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
87 return sc;
88 }
89
90 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
91 if (sc .isFailure() ) {
92 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
93 return sc;
94 }
95
96 return StatusCode::SUCCESS;
97}
98
100 MsgStream log(messageService(), name());
101 log << MSG::INFO << "TofEnergyCalibSvc::finalize()" << endreq;
102 // if(m_connect_offline) delete m_connect_offline;
103 return StatusCode::SUCCESS;
104}
105
106void TofEnergyCalibSvc::handle(const Incident& inc){
107 MsgStream log( messageService(), name() );
108 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
109
110 if ( inc.type() == "NewRun" ){
111 log << MSG::DEBUG << "NewRun" << endreq;
113 log << MSG::ERROR << "can not initilize Tof energy Calib Constants" << endreq;
114 }
115 }
116}
117
118//select sftver and parver frm table CalVtxLum
120 std::string& CalParVer,
121 int &runfrm,
122 int &runto,
123 int RunNo,
124 std::string BossRelease,
125 std::string DataType
126 )
127{
128 MsgStream log(msgSvc(), "TofEnergyCalibSvc" );
129 char stmt[300];
130 int run_No =RunNo;
131 MYSQL_RES *res_set;
134 StatusCode sc=serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
135 if (sc .isFailure() ) {
136 log << MSG::ERROR << " Unable to find DatabaseSvc " << endreq;
137 return sc;
138 }
139
140 const char* bossRelease = BossRelease.c_str();
141 const char* dataType = DataType.c_str();
142
143 sprintf(stmt,"select RunFrom,RunTo,SftVer,ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType='%s' ",bossRelease,run_No,run_No,dataType);
144
145 int row_no = m_dbsvc->query("offlinedb",stmt,res);
146 if(row_no<1){
147 std::cout<<"ERROR:error searching with:"<<stmt<<std::endl;
148 exit(1);
149 }
150 if(row_no=1){
151 DatabaseRecord* records1 = res[0];
152 runfrm=records1->GetInt("RunFrom");
153 runto=records1->GetInt("RunTo");
154 cout<<dataType<<" runfrm,runto in getSftParVer is:"<<runfrm<<"::::"<<runto<<endl;
155 SftVer=records1->GetString("SftVer");
156 CalParVer=records1->GetString("ParVer");
157 }
158 if(row_no>1){
159 cout<<"ERROR: "<<dataType<<" set overlapped run number in the table CalVtxLumVer"<<endl;
160 exit(1);
161 }
162 return StatusCode::SUCCESS;
163 }
165 MsgStream log(messageService(), name());
166 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
167 int run = eventHeader->runNumber();
168 //cout<<"Run in getTofEnergyCalibSvcInfo() is: "<<run<<endl;
169
170 char stmt1[400];
171 StatusCode st1;
172 //runfrm,runto are the value from the table ***CalConst;runfrm1,runto1 are the value from the table CalVtxLumVer
173 int runfrm,runfrm1;
174 int runto,runto1;
175 std:string cType;
176
177 if(run<0) {
178 cout << "This data is the MC sample with the Run Number: " << run << endl;
179 run = -run;
180 }
181
182 if(m_bossRelease=="default") m_bossRelease = getenv("BES_RELEASE");
183 if(m_isData=="default")
184 {
185 cout<<"Please set the type of TofEnergy"<<endl;
186 exit(1);
187 }
188 if(m_isData=="Data") cType="TofEnergyData";
189 if(m_isData=="MC") cType="TofEnergyMC";
190 st1=getSftParVer(m_bossVer,m_calPar,runfrm1,runto1,run,m_bossRelease,cType);
191 if(cType=="TofEnergyData"){
192 sprintf(stmt1, "select CalibConst,Para1,Para2,Para3,Para4,Para5 from TofEnergyCalib where BossVer = '%s' and RunFrom <= %d and RunTo >= %d and isData = '%s'", m_bossVer.c_str(), run, run, m_isData.c_str());}
193 else if(cType=="TofEnergyMC"){
194 sprintf(stmt1, "select CalibConst,Para1,Para2,Para3,Para4,Para5 from TofEnergySim where BossVer = '%s' and RunFrom <= %d and RunTo >= %d and isData = '%s'", m_bossVer.c_str(), run, run, m_isData.c_str());}
195 //cout<<"stmt is:"<<stmt1<<endl;
197 int row_no = m_dbsvc->query("offlinedb",stmt1,res);
198 if( row_no > 0 ) {
199 DatabaseRecord& dbrec = *res[row_no-1];
200 m_CalibConst = dbrec.GetDouble("CalibConst");
201 m_Para1 = dbrec.GetDouble("Para1");
202 m_Para2 = dbrec.GetDouble("Para2");
203 m_Para3 = dbrec.GetDouble("Para3");
204 m_Para4 = dbrec.GetDouble("Para4");
205 m_Para5 = dbrec.GetDouble("Para5");
206 cout<<stmt1<<endl;
207 cout<<"BossReleaseVer="<<m_bossRelease.c_str()<<"\t"<<"TofEnergyBossVer="<<m_bossVer.c_str()<<endl;
208 cout<<"m_CalibConst is:"<<m_CalibConst<<endl;
209 cout<<"Para1="<<m_Para1<<"\t"
210 <<"Para2="<<m_Para2<<"\t"
211 <<"Para3="<<m_Para3<<"\t"
212 <<"Para4="<<m_Para4<<"\t"
213 <<"Para5="<<m_Para5<<endl;
214 cout << "Successfully fetch TofEnergyCalibSvc information for run: "
215 << run << endl;
216 return true;
217 } else if( row_no<=0 ) {
218 cout << " TofEnergyCalibSvc:: can not found TofEnergyCalibSvc information of run:"
219 << run << ", boss version " << m_bossRelease << endl;
220 exit(1);
221 return false;
222 }
223 return true;
224}
225
char * SftVer
Definition: DQA_TO_DB.cxx:18
struct st_mysql_res MYSQL_RES
IMessageSvc * msgSvc()
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
IDatabaseSvc * m_dbsvc
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 &)
StatusCode getSftParVer(std::string &SftVer, std::string &CalParVer, int &runfrm, int &runto, int RunNo, std::string BossRelease, std::string DataType)
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)