BOSS 8.0.0
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#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <iostream>
13#include <sstream>
14
15#include "GaudiKernel/IDataProviderSvc.h"
16#include "GaudiKernel/IInterface.h"
17#include "GaudiKernel/ISvcLocator.h"
18#include "GaudiKernel/Kernel.h"
19#include "GaudiKernel/MsgStream.h"
20#include "GaudiKernel/PropertyMgr.h"
21#include "GaudiKernel/SmartDataPtr.h"
22#include "GaudiKernel/StatusCode.h"
23#include "TofEnergyCalibSvc.h"
24
25#include "GaudiKernel/IIncidentListener.h"
26#include "GaudiKernel/IIncidentSvc.h"
27#include "GaudiKernel/Incident.h"
28
29#include "EventModel/Event.h"
30#include "EventModel/EventHeader.h"
31#include "EventModel/EventModel.h"
32#include "GaudiKernel/Bootstrap.h"
33#include "GaudiKernel/ISvcLocator.h"
34
35#include <TMath.h>
36
37using namespace std;
38
40TofEnergyCalibSvc::TofEnergyCalibSvc( const string& name, ISvcLocator* svcloc )
41 : base_class( name, svcloc ) {
42 // declare properties
43 declareProperty( "Host", host = std::string( "bes3db2.ihep.ac.cn" ) );
44 declareProperty( "DbName", dbName = std::string( "offlinedb" ) );
45 declareProperty( "UserName", userName = std::string( "guest" ) );
46 declareProperty( "Password", password = std::string( "guestpass" ) );
47 declareProperty( "IsData", m_isData = std::string( "default" ) );
48 declareProperty( "BossRelease", m_bossRelease = std::string( "default" ) );
49 declareProperty( "TofEnergyPar", m_calPar = "default" );
50 declareProperty( "TofEnergyBossVer", m_bossVer = "default" );
51 // m_dbsvc = DatabaseSvc::instance();
52}
53
55
57 MsgStream log( msgSvc(), name() );
58 log << MSG::INFO << "TofEnergyCalibSvc::initialize()" << endmsg;
59
60 StatusCode sc = Service::initialize();
61 if ( sc.isFailure() ) return sc;
62
63 IIncidentSvc* incsvc;
64 sc = service( "IncidentSvc", incsvc );
65 int priority = 100;
66 if ( sc.isSuccess() ) { incsvc->addListener( this, "NewRun", priority ); }
67
68 sc = serviceLocator()->service( "DatabaseSvc", m_dbsvc, true );
69 if ( sc.isFailure() )
70 {
71 log << MSG::ERROR << "Unable to find DatabaseSvc " << endmsg;
72 return sc;
73 }
74
75 sc = serviceLocator()->service( "EventDataSvc", m_eventSvc, true );
76 if ( sc.isFailure() )
77 {
78 log << MSG::ERROR << "Unable to find EventDataSvc " << endmsg;
79 return sc;
80 }
81
82 return StatusCode::SUCCESS;
83}
84
86 MsgStream log( msgSvc(), name() );
87 log << MSG::INFO << "TofEnergyCalibSvc::finalize()" << endmsg;
88 // if(m_connect_offline) delete m_connect_offline;
89 return StatusCode::SUCCESS;
90}
91
92void TofEnergyCalibSvc::handle( const Incident& inc ) {
93 MsgStream log( msgSvc(), name() );
94 log << MSG::DEBUG << "handle: " << inc.type() << endmsg;
95
96 if ( inc.type() == "NewRun" )
97 {
98 log << MSG::DEBUG << "NewRun" << endmsg;
100 { log << MSG::ERROR << "can not initilize Tof energy Calib Constants" << endmsg; }
101 }
102}
103
104// select sftver and parver frm table CalVtxLum
105StatusCode TofEnergyCalibSvc::getSftParVer( std::string& SftVer, std::string& CalParVer,
106 int& runfrm, int& runto, int RunNo,
107 std::string BossRelease, std::string DataType ) {
108 MsgStream log( msgSvc(), "TofEnergyCalibSvc" );
109 char stmt[300];
110 int run_No = RunNo;
111 MYSQL_RES* res_set;
114 StatusCode sc = serviceLocator()->service( "DatabaseSvc", m_dbsvc, true );
115 if ( sc.isFailure() )
116 {
117 log << MSG::ERROR << " Unable to find DatabaseSvc " << endmsg;
118 return sc;
119 }
120
121 const char* bossRelease = BossRelease.c_str();
122 const char* dataType = DataType.c_str();
123
124 sprintf( stmt,
125 "select RunFrom,RunTo,SftVer,ParVer from CalVtxLumVer where BossRelease = '%s' and "
126 "RunFrom <= %d and RunTo "
127 ">= %d and DataType='%s' ",
128 bossRelease, run_No, run_No, dataType );
129
130 int row_no = m_dbsvc->query( "offlinedb", stmt, res );
131 if ( row_no < 1 )
132 {
133 std::cout << "ERROR:error searching with:" << stmt << std::endl;
134 exit( 1 );
135 }
136 if ( row_no == 1 )
137 {
138 DatabaseRecord* records1 = res[0];
139 runfrm = records1->GetInt( "RunFrom" );
140 runto = records1->GetInt( "RunTo" );
141 cout << dataType << " runfrm,runto in getSftParVer is:" << runfrm << "::::" << runto
142 << endl;
143 SftVer = records1->GetString( "SftVer" );
144 CalParVer = records1->GetString( "ParVer" );
145 }
146 if ( row_no > 1 )
147 {
148 cout << "ERROR: " << dataType << " set overlapped run number in the table CalVtxLumVer"
149 << endl;
150 exit( 1 );
151 }
152 return StatusCode::SUCCESS;
153}
155 MsgStream log( msgSvc(), name() );
156 SmartDataPtr<Event::EventHeader> eventHeader( m_eventSvc, "/Event/EventHeader" );
157 int run = eventHeader->runNumber();
158 // cout<<"Run in getTofEnergyCalibSvcInfo() is: "<<run<<endl;
159
160 char stmt1[400];
161 StatusCode st1;
162 // runfrm,runto are the value from the table ***CalConst;runfrm1,runto1 are the value from
163 // the table CalVtxLumVer
164 int runfrm, runfrm1;
165 int runto, runto1;
166std:
167 string cType;
168
169 if ( run < 0 )
170 {
171 cout << "This data is the MC sample with the Run Number: " << run << endl;
172 run = -run;
173 }
174
175 if ( m_bossRelease == "default" ) m_bossRelease = getenv( "BES_RELEASE" );
176 if ( m_isData == "default" )
177 {
178 cout << "Please set the type of TofEnergy" << endl;
179 exit( 1 );
180 }
181 if ( m_isData == "Data" ) cType = "TofEnergyData";
182 if ( m_isData == "MC" ) cType = "TofEnergyMC";
183 st1 = getSftParVer( m_bossVer, m_calPar, runfrm1, runto1, run, m_bossRelease, cType );
184 if ( cType == "TofEnergyData" )
185 {
186 sprintf( stmt1,
187 "select CalibConst,Para1,Para2,Para3,Para4,Para5 from TofEnergyCalib where "
188 "BossVer = '%s' and RunFrom <= "
189 "%d and RunTo >= %d and isData = '%s'",
190 m_bossVer.c_str(), run, run, m_isData.c_str() );
191 }
192 else if ( cType == "TofEnergyMC" )
193 {
194 sprintf( stmt1,
195 "select CalibConst,Para1,Para2,Para3,Para4,Para5 from TofEnergySim where BossVer "
196 "= '%s' and RunFrom <= %d "
197 "and RunTo >= %d and isData = '%s'",
198 m_bossVer.c_str(), run, run, m_isData.c_str() );
199 }
200 // cout<<"stmt is:"<<stmt1<<endl;
202 int row_no = m_dbsvc->query( "offlinedb", stmt1, res );
203 if ( row_no > 0 )
204 {
205 DatabaseRecord& dbrec = *res[row_no - 1];
206 m_CalibConst = dbrec.GetDouble( "CalibConst" );
207 m_Para1 = dbrec.GetDouble( "Para1" );
208 m_Para2 = dbrec.GetDouble( "Para2" );
209 m_Para3 = dbrec.GetDouble( "Para3" );
210 m_Para4 = dbrec.GetDouble( "Para4" );
211 m_Para5 = dbrec.GetDouble( "Para5" );
212 cout << stmt1 << endl;
213 cout << "BossReleaseVer=" << m_bossRelease.c_str() << "\t"
214 << "TofEnergyBossVer=" << m_bossVer.c_str() << endl;
215 cout << "m_CalibConst is:" << m_CalibConst << endl;
216 cout << "Para1=" << m_Para1 << "\t"
217 << "Para2=" << m_Para2 << "\t"
218 << "Para3=" << m_Para3 << "\t"
219 << "Para4=" << m_Para4 << "\t"
220 << "Para5=" << m_Para5 << endl;
221 cout << "Successfully fetch TofEnergyCalibSvc information for run: " << run << endl;
222 return true;
223 }
224 else if ( row_no <= 0 )
225 {
226 cout << " TofEnergyCalibSvc:: can not found TofEnergyCalibSvc information of run:" << run
227 << ", boss version " << m_bossRelease << endl;
228 exit( 1 );
229 return false;
230 }
231 return true;
232}
DECLARE_COMPONENT(BesBdkRc)
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)
char * SftVer
Definition DQA_TO_DB.cxx:19
IMessageSvc * msgSvc()
IDatabaseSvc * m_dbsvc
virtual StatusCode initialize()
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)