2#include "GaudiKernel/Kernel.h"
3#include "GaudiKernel/IInterface.h"
4#include "GaudiKernel/StatusCode.h"
5#include "GaudiKernel/SvcFactory.h"
6#include "GaudiKernel/MsgStream.h"
8#include "GaudiKernel/IIncidentSvc.h"
9#include "GaudiKernel/Incident.h"
10#include "GaudiKernel/IIncidentListener.h"
12#include "GaudiKernel/ISvcLocator.h"
13#include "GaudiKernel/Bootstrap.h"
15#include "GaudiKernel/IDataProviderSvc.h"
16#include "GaudiKernel/SmartDataPtr.h"
17#include "GaudiKernel/DataSvc.h"
18#include "GaudiKernel/IJobOptionsSvc.h"
24#include "GaudiKernel/SmartDataPtr.h"
38 declareProperty(
"PrintInfo", m_printInfo=
false);
39 declareProperty(
"TimeFitFile", m_timeFitFile);
40 declareProperty(
"TimeFitFilePath", m_timeFitFilePath);
41 declareProperty(
"TimeWalkCalibFile", m_timeWalkFileName);
42 declareProperty(
"LUTFile", m_fileLUT);
49 if( IID_ICgemCalibFunSvc.versionMatch(riid) ){
52 return Service::queryInterface(riid, ppvInterface);
54 return StatusCode::SUCCESS;
58 MsgStream log(messageService(), name());
59 log << MSG::INFO <<
"CgemCalibFunSvc::initialize()" << endreq;
61 StatusCode sc = Service::initialize();
62 if( sc.isFailure() )
return sc;
72 static IJobOptionsSvc* jobSvc = 0;
74 sc = service(
"JobOptionsSvc", jobSvc);
75 if ( sc.isFailure() ) {
76 std::cout <<
"Can't get the JobOptionsSvc @ DistBoss::GetPropertyValue()" << std::endl;
80 const vector<const Property*>* properties = jobSvc->getProperties(
"ReadCosmicRayData");
81 if ( properties == NULL ) {
82 log << MSG::WARNING <<
"can not get runNo from ReadCosmicRayData" << endreq;
84 for (
unsigned int i = 0; i < properties->size(); ++i ) {
85 if ( properties->at(i)->name() ==
"runNo" ) {
86 cout <<
"name of property: " << properties->at(i)->name() << endl;
87 string strRnd = properties->at(i)->toString();
88 sscanf(strRnd.c_str(),
"%d", &m_run);
95 if( !initCalibConst() ){
96 log << MSG::ERROR <<
"can not initialize CGEM calibration constants" << endreq;
97 return StatusCode::FAILURE;
100 sc = service(
"CalibDataSvc", m_pCalDataSvc,
true);
101 if( sc == StatusCode::SUCCESS ){
102 log << MSG::INFO <<
"Retrieve IDataProviderSvc" << endreq;
104 log << MSG::FATAL <<
"can not get IDataProviderSvc" << endreq;
109 cout <<
"LUT file: " << m_fileLUT << endl;
110 if(!m_cgemLUTReader->
ReadLUT()){
112 log << MSG::FATAL <<
"can not read LUT " << m_fileLUT << endl;
115 return StatusCode::SUCCESS;
119 MsgStream log(messageService(), name());
120 log << MSG::INFO <<
"CgemCalibFunSvc::finalize()" << endreq;
122 return StatusCode::SUCCESS;
126 MsgStream log( messageService(), name() );
127 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
129 if ( inc.type() ==
"NewRun" ){
130 log << MSG::DEBUG <<
"NewRun" << endreq;
132 if( ! initCalibConst() ){
134 <<
"can not initilize Cgem Calib Constants" << endl
135 <<
" Please insert the following statement "
136 <<
"in your \"jobOption.txt\" "
137 <<
"before the include file of Cgem Reconstruction: "
139 <<
"#include \"$CALIBSVCROOT/share/job-CalibData.txt\""
145bool CgemCalibFunSvc::initCalibConst(){
146 MsgStream log(messageService(), name());
147 log << MSG::INFO <<
"read calib const from TCDS" << endreq;
169 if(!m_timeFitFile.empty()){
170 sprintf(fname,
"%s", m_timeFitFile.c_str());
171 }
else if(m_getRunNo && m_timeFitFile.empty()){
172 sprintf(fname,
"%s/timeFit_Run%d.txt", m_timeFitFilePath.c_str(), m_run);
174 cout <<
"ERROR: can not get runNo and CGEM calibration file" << endl;
178 cout <<
"Open CGEM calibration file: " << fname << endl;
179 ifstream fconst(fname);
181 log << MSG::ERROR <<
"can not find calibration constants. Please check runNo or calibration file" << endreq;
186 getline(fconst, str);
187 for(
int layer=0; layer<NLAYER; layer++){
189 for(
int i=0; i<5; i++) fconst >> str;
190 for(
int xv=0; xv<NXV; xv++){
191 fconst >> str >> m_tRising[layer][xv][0] >> m_tFalling[layer][xv][0];
192 m_tRising[layer][xv][1] = m_tRising[layer][xv][0];
193 m_tFalling[layer][xv][1] = m_tFalling[layer][xv][0];
196 for(
int i=0; i<7; i++) fconst >> str;
197 for(
int xv=0; xv<NXV; xv++){
198 fconst >> str >> m_tRising[layer][xv][0] >> m_tFalling[layer][xv][0]
199 >> m_tRising[layer][xv][1] >> m_tFalling[layer][xv][1];
206 cout <<
"print CGEM time fit calibration constants" << endl;
207 for(
int layer=0; layer<
NLAYER; layer++){
208 cout <<
"layer " << layer << endl;
209 for(
int xv=0; xv<NXV; xv++){
210 for(
int sheet=0; sheet<NSHEET; sheet++){
211 if((0==layer) && (1==sheet))
continue;
212 cout << setw(15) << m_tRising[layer][xv][sheet] << setw(15) << m_tFalling[layer][xv][sheet];
221 TFile ftw(m_timeWalkFileName.c_str());
222 cout <<
"time walk file: " << m_timeWalkFileName << endl;
223 TTree* trTw = (TTree*)ftw.Get(
"tree");
224 trTw->SetBranchAddress(
"time_correction", &tw);
225 trTw->SetBranchAddress(
"charge", &charge);
226 trTw->SetBranchAddress(
"threshold", &threshold);
227 int nEntries = trTw->GetEntries();
228 for(
int i=0; i<nEntries; i++){
230 m_tw.push_back((
double)tw);
231 m_charge.push_back((
double)charge);
232 m_threshold.push_back((
double)threshold);
235 m_valThreshold.push_back(threshold);
236 m_thIdFrom.push_back(i);
238 bool pushNewThreshold =
true;
239 int vecSize = m_valThreshold.size();
240 for(
int k=0; k<vecSize; k++){
241 if(fabs(threshold-m_valThreshold[k])<0.1){
242 pushNewThreshold =
false;
246 if(pushNewThreshold){
247 m_valThreshold.push_back(threshold);
248 m_thIdEnd.push_back(i-1);
249 m_thIdFrom.push_back(i);
254 m_thIdEnd.push_back(nEntries-1);
256 cout <<
"Print time walk calibration info" << endl;
261 for(
int i=0; i<m_valThreshold.size(); i++){
262 cout << setw(10) << m_valThreshold[i] << setw(10) << m_thIdFrom[i]
263 << setw(10) << m_thIdEnd[i] << setw(15)<<m_charge[m_thIdFrom[i]]
264 << setw(15) << m_charge[m_thIdEnd[i]] << endl;
277 return m_tRising[layer][xvFlag][sheet];
281 return m_tFalling[layer][xvFlag][sheet];
287 cout <<
"ERROR: can not calculate time walk with LUT, return tw=0." << endl;
290 double threshold = m_cgemLUTReader->
Get_thr_T_fC(layer, sheet, xvFlag, stripID);
292 cout << setw(8)<<layer <<setw(8)<<sheet<<setw(8)<<xvFlag<<setw(8)<<stripID
293 <<setw(15)<<threshold<<endl;
301 MsgStream log(messageService(), name());
304 int thresholdId = -1;
305 double minval = 1000.;
306 for(
int i=0; i<m_valThreshold.size(); i++){
307 double val = fabs(threshold-m_valThreshold[i]);
314 int binMin = m_thIdFrom[thresholdId];
315 int binMax = m_thIdEnd[thresholdId];
316 double qMin = m_charge[binMin];
317 double qMax = m_charge[binMax];
320 log << MSG::WARNING <<
"can not find time walk calibration constants for threshold="
321 << threshold <<
" , use the parameters with threshold=" << m_valThreshold[thresholdId] << endreq;
323 cout <<
"threshold_binId = " << thresholdId <<
" qMin = " << qMin
324 <<
" qMax = " << qMax << endl;
329 log << MSG::WARNING <<
"charge<0, calculate time walk with charge=0" << endreq;
330 tw = (0.0-m_charge[binMin])*(m_tw[binMin]-m_tw[binMin+1])/(m_charge[binMin]-m_charge[binMin+1]) + m_tw[binMin];
332 log << MSG::WARNING <<
"charge < minimumCharge in time walk calibration function " << qMin << endreq;
333 tw = (Q-m_charge[binMin])*(m_tw[binMin]-m_tw[binMin+1])/(m_charge[binMin]-m_charge[binMin+1]) + m_tw[binMin];
335 log << MSG::WARNING <<
"charge > maximumCharge in time walk calibration function " << qMax << endreq;
336 if(m_printInfo) cout <<
"binMin = " << binMin <<
" binMax = " << binMax << endl;
337 tw = (Q-m_charge[binMax])*(m_tw[binMax]-m_tw[binMax-1])/(m_charge[binMax]-m_charge[binMax-1]) + m_tw[binMax];
339 for(
int i=binMin; i<binMax; i++){
340 if((Q>=m_charge[i]) && (Q<m_charge[i+1])){
341 tw = (m_tw[i+1]-m_tw[i])*(Q-m_charge[i])/(m_charge[i+1]-m_charge[i]) + m_tw[i];
virtual StatusCode initialize()
double getTimeWalk(int layer, int xvFlag, int sheet, int stripID, double Q) const
CgemCalibFunSvc(const std::string &name, ISvcLocator *svcloc)
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
double getSigma(int layer, int xvFlag, int readoutMode, double angle, double Q, double T) const
virtual StatusCode finalize()
void handle(const Incident &)
double getTimeFalling(int layer, int xvFlag, int sheet, int stripID, double Q=100., double z=0.) const
double getTimeRising(int layer, int xvFlag, int sheet, int stripID, double Q=100., double z=0.) const
float Get_thr_T_fC(int ilayer, int isheet, int iview, int istrip)