1#include "GaudiKernel/Kernel.h"
2#include "GaudiKernel/IInterface.h"
3#include "GaudiKernel/IIncidentSvc.h"
4#include "GaudiKernel/Incident.h"
5#include "GaudiKernel/IIncidentListener.h"
6#include "GaudiKernel/StatusCode.h"
7#include "GaudiKernel/SvcFactory.h"
8#include "GaudiKernel/MsgStream.h"
9#include "GaudiKernel/IDataProviderSvc.h"
10#include "GaudiKernel/SmartDataPtr.h"
11#include "GaudiKernel/DataSvc.h"
25#include "GaudiKernel/ThreadGaudi.h"
48 for( ;
iter != m_tofDataMap.end();
iter++ ) {
49 delete (*iter).second;
53 m_tofDataMapOnline.clear();
54 m_tofDataVectorOnline.clear();
55 m_tofDataMapEstime.clear();
56 m_tofDataVectorEstime.clear();
57 m_tofDataMapTof.clear();
58 m_tofDataVectorTof.clear();
59 m_tofDataMapEmc.clear();
60 m_tofDataVectorEmc.clear();
72 if (scc == StatusCode::SUCCESS) {
73 log << MSG::INFO <<
"TofRec Get Calibration Service Sucessfully!" << endreq;
75 log << MSG::ERROR <<
"TofRec Get Calibration Service Failed !" << endreq;
76 return StatusCode::FAILURE;
81 if (scq == StatusCode::SUCCESS) {
82 log << MSG::INFO <<
"TofRec Get Q Correction Service Sucessfully!" << endreq;
84 log << MSG::ERROR <<
"TofRec Get Q Correction Service Failed !" << endreq;
85 return StatusCode::FAILURE;
90 if (sce == StatusCode::SUCCESS) {
91 log << MSG::INFO <<
"TofRec Get Q Electronics Calibration Service Sucessfully!" << endreq;
93 log << MSG::ERROR <<
"TofRec Get Q Electronics Calibration Service Failed !" << endreq;
94 return StatusCode::FAILURE;
97 return StatusCode::SUCCESS;
103 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
104 if ( inc.type() ==
"BeginEvent" ){
105 log << MSG::DEBUG <<
"Begin Event" << endreq;
107 for( ;
iter != m_tofDataMap.end();
iter++ ) {
108 delete (*iter).second;
111 for( ; iterOnline != m_tofDataMapOnline.end(); iterOnline++ ) {
112 delete (*iterOnline).second;
114 m_tofDataMap.clear();
115 m_tofDataMapOnline.clear();
116 m_tofDataVectorOnline.clear();
117 m_tofDataMapEstime.clear();
118 m_tofDataVectorEstime.clear();
119 m_tofDataMapTof.clear();
120 m_tofDataVectorTof.clear();
121 m_tofDataMapEmc.clear();
122 m_tofDataVectorEmc.clear();
134 log << MSG::INFO <<
"TofRawDataProvider::tofDataMapFull()" << endreq;
136 if( !m_tofDataMap.empty() ) {
138 log << MSG::WARNING <<
"TofRawDataProvider::tofDataMapFull: m_tofDataMap is NOT empty!" << endreq;
141 for( ;
iter != m_tofDataMap.end();
iter++ ) {
142 delete (*iter).second;
144 m_tofDataMap.clear();
147 std::string evtDataSvc_name(
"EventDataSvc");
148 if(isGaudiThreaded(
m_name)){
149 evtDataSvc_name += getGaudiThreadIDfromName(
m_name);
153 IDataProviderSvc* eventSvc;
154 StatusCode sc =
m_svcLocator->service( evtDataSvc_name.c_str(),eventSvc,
true );
155 if( !sc.isSuccess() ) {
156 log << MSG::FATAL <<
"TofRawDataProvider::tofDataMapFull: ERROR Could not load EventDataSvc" << endreq;
161 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc,
"/Event/EventHeader");
163 log << MSG::FATAL <<
"TofRawDataProvider could not find Event Header!" << endreq;
166 int run = eventHeader->runNumber();
168 bool mc = ( run < 0 ) ?
true:
false;
170 qCorr = qCorr && (!mc);
173 misLable = ( !mc && misLable );
177 for(
unsigned int i=0; i<5; i++ ) {
180 if( identmp != 0x2fffffff ) {
182 deadId.push_back( identmp );
187 if( identmp != 0x2fffffff ) {
189 deadId.push_back( identmp );
193 if( identmp != 0x2fffffff ) {
195 deadId.push_back( identmp );
200 SmartDataPtr<TofDigiCol> tofDigiCol(eventSvc,
"/Event/Digi/TofDigiCol");
202 log << MSG::WARNING <<
"TofRawDataProvider::tofDataMapFull: Could not find Tof Digi Data!" << endreq;
209 std::vector<Adc*> chargeVec;
210 std::vector<Tdc*> timeVec;
215 TofDigiCol::iterator
iter = tofDigiCol->begin();
217for( ;
iter != tofDigiCol->end();
iter++ ) {
225 if(barrel_ec_help == 0 || barrel_ec_help ==1 || barrel_ec_help==2)
228 unsigned int overflow = (*iter)->getOverflow();
232 if( ( overflow & 0xfe000000 ) == 0xfe000000 )
240 bool multiQ = ( ( overflow & 0x8 ) != 0 );
241 bool multiT = ( ( overflow & 0x4 ) != 0 );
245 unsigned int iden_value = iden.
get_value();
251 vector<int>::iterator iter_dead = deadId.begin();
252 for( ; iter_dead != deadId.end(); iter_dead++ ) {
253 if( iden_value == (*iter_dead) ) {
259 if( mc ) {
continue; }
261 log << MSG::ERROR <<
"TofRawDataProvider::tofDataMapFull: Dead Channel Number is not Correct, please check TOF Calibration Constants!" << endreq;
272 if( ( barrel_ec != 1 ) && ( end == 1 ) )
continue;
274 if( ( barrel_ec != 1 ) && (
id == 48 ) )
continue;
276 unsigned int tdcChannel = (*iter)->getTimeChannel();
277 unsigned int adcChannel = (*iter)->getChargeChannel();
282 if( ( overflow & 0x2 ) != 0 )
284 adcChannel = ( adcChannel | 0x80000 );
287 if( (adcChannel&0x7fffffff) != 0x7fffffff ) {
296 chargeVec.push_back( adc );
301 if( tdcChannel != 0x7fffffff ) {
306 timeVec.push_back( tdc );
311 if( multiT || multiQ ) {
312 TofDigiCol::iterator iter_next =
iter + 1;
313 if( iter_next != tofDigiCol->end() ) {
314 unsigned int idenNext_value = ((*iter_next)->identify()).get_value();
315 if( iden_value == idenNext_value )
continue;
324 std::vector<PmtData*> pmtDataVec;
325 std::vector<Tdc*>::iterator iter_t = timeVec.begin();
326 for( ; iter_t != timeVec.end(); iter_t++ ) {
327 int tclock = (*iter_t)->clock();
331 std::vector<Adc*>::iterator iter_q = chargeVec.begin();
332 for( ; iter_q != chargeVec.end(); iter_q++ ) {
333 int qclock = (*iter_q)->clock();
335 if(
abs(tclock-qclock)<2 ) {
336 if( ( pmt->
quality() & 0x2 ) != 0 ) {
337 if( (*iter_q)->value() > pmt->
adc() ) {
349 pmtDataVec.push_back( pmt );
354 std::vector<Adc*>::iterator iter_q = chargeVec.begin();
355 for( ; iter_q != chargeVec.end(); iter_q++ ) {
357 if( (*iter_q)->times() != -1 )
365 pmtDataVec.push_back( pmt );
374 unsigned int iden_value_key = (iden_value & 0xfffffffe );
375 if( ( barrel_ec == 0 ) || ( barrel_ec == 2 ) ) {
376 std::vector<PmtData*>::iterator iter_pmt = pmtDataVec.begin();
377 for( ; iter_pmt != pmtDataVec.end(); iter_pmt++ ) {
382 if( barrel_ec==2 &&
id==42 ) {
385 iden_value_key = iden91;
388 if( barrel_ec==2 &&
id==43 ) {
390 iden_value_key = iden90;
399 m_tofDataMap.insert( make_pair( iden_value_key, tof ) );
402 else if( barrel_ec == 1 ) {
403 unsigned int count = m_tofDataMap.count( iden_value_key );
405 std::vector<PmtData*>::iterator iter_pmt = pmtDataVec.begin();
406 for( ; iter_pmt != pmtDataVec.end(); iter_pmt++ ) {
417 m_tofDataMap.insert( make_pair( iden_value_key, tof ) );
421 pair< IterTofDataMap, IterTofDataMap > range = m_tofDataMap.equal_range( iden_value_key );
422 std::vector<PmtData*>::iterator iter_pmt = pmtDataVec.begin();
423 for( ; iter_pmt != pmtDataVec.end(); iter_pmt++ ) {
426 for(
unsigned int tofDataNumber = 0; tofDataNumber < count; tofDataNumber++,
iter++ ) {
427 bool matched =
false;
429 if( ( ( (*iter_pmt)->quality() & 0x1 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x1 ) != 0 ) ) {
430 double time1 = (*iter_pmt)->tdc();
431 double time2 = ((*iter).second)->tdc2();
434 else if( ( ( (*iter_pmt)->quality() & 0x1 ) == 0 ) && ( ( (*iter_pmt)->quality() & 0x2 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x1 ) != 0 ) ) {
435 double time1 = (*iter_pmt)->qclock();
436 double time2 = ((*iter).second)->tclock2();
437 double time3 = ((*iter).second)->qclock2();
440 else if( ( ( (*iter_pmt)->quality() & 0x1 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x1 ) == 0 ) && ( ( ((*iter).second)->quality() & 0x2 ) != 0 ) ) {
441 double time1 = (*iter_pmt)->tclock();
442 double time2 = (*iter_pmt)->qclock();
443 double time3 = ((*iter).second)->qclock2();
446 else if( ( ( (*iter_pmt)->quality() & 0x1 ) == 0 ) && ( ( (*iter_pmt)->quality() & 0x2 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x1 ) == 0 ) && ( ( ((*iter).second)->quality() & 0x2 ) != 0 ) ) {
447 double time1 = (*iter_pmt)->qclock();
448 double time2 = ((*iter).second)->qclock2();
454 if( ( ( (*iter).second)->quality() & 0xc ) == 0 ) {
455 ((*iter).second)->setForward( (*iter_pmt) );
463 m_tofDataMap.insert( make_pair( iden_value_key, tof ) );
469 if( ( ( (*iter_pmt)->quality() & 0x1 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x4 ) != 0 ) ) {
470 double time1 = (*iter_pmt)->tdc();
471 double time2 = ((*iter).second)->tdc1();
474 else if( ( ( (*iter_pmt)->quality() & 0x1 ) == 0 ) && ( ( (*iter_pmt)->quality() & 0x2 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x4 ) != 0 ) ) {
475 double time1 = (*iter_pmt)->qclock();
476 double time2 = ((*iter).second)->tclock1();
477 double time3 = ((*iter).second)->qclock1();
480 else if( ( ( (*iter_pmt)->quality() & 0x1 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x4 ) == 0 ) && ( ( ((*iter).second)->quality() & 0x8 ) != 0 ) ) {
481 double time1 = (*iter_pmt)->tclock();
482 double time2 = (*iter_pmt)->qclock();
483 double time3 = ((*iter).second)->qclock1();
486 else if( ( ( (*iter_pmt)->quality() & 0x1 ) == 0 ) && ( ( (*iter_pmt)->quality() & 0x2 ) != 0 ) && ( ( ((*iter).second)->quality() & 0x4 ) == 0 ) && ( ( ((*iter).second)->quality() & 0x8 ) != 0 ) ) {
487 double time1 = (*iter_pmt)->qclock();
488 double time2 = ((*iter).second)->qclock1();
494 if( ( ( (*iter).second)->quality() & 0x3 ) == 0 ) {
495 ((*iter).second)->setBackward( (*iter_pmt) );
496 if( ( ( ((*iter).second)->quality() & 0x5 )==0x5 ) || ( ( ( ((*iter).second)->quality() & 0x5 )==0x4 ) && ( ((*iter).second)->ttimes1()==1 ) ) || ( ( ( ((*iter).second)->quality() & 0x5 )==0x1 ) && ( ((*iter).second)->ttimes2()==1 ) ) ) {
497 ((*iter).second)->setTMatched(
true );
500 ((*iter).second)->setTMatched(
false );
506 tof->
setForward( ((*iter).second)->forward() );
508 if( ( ( tof->
quality() & 0x5 )==0x5 ) || ( ( ( tof->
quality() & 0x5 )==0x4 ) && ( tof->
ttimes1()==1 ) ) || ( ( ( tof->
quality() & 0x5 )==0x1 ) && ( tof->
ttimes2()==1 ) ) ) {
514 m_tofDataMap.insert( make_pair( iden_value_key, tof ) );
529 m_tofDataMap.insert( make_pair( iden_value_key, tof ) );
540 else if(barrel_ec_help==3 || barrel_ec_help==4 || barrel_ec_help==5 || barrel_ec_help==6)
557 unsigned int iden_value = iden.
get_value();
603 unsigned int tdcChannel = (*iter)->getTimeChannel();
604 unsigned int tdc2Channel = (*iter)->getChargeChannel();
609 unsigned int pulse_length_channel_without_correction = tdc2Channel-tdcChannel;
611 if(pulse_length < 4.164){
continue;}
620 double correction_ta=0;
621 if(pulse_length<=13.9)
623 correction_ta= 321.2;
627 correction_ta = 72310.5 - 15162.2*pulse_length + 1066.06*pulse_length*pulse_length - 25.0252*pulse_length*pulse_length*pulse_length;
628 if(correction_ta<0)correction_ta=0;
632 time_of_flight=time_of_flight - correction_ta/1000.;
656 adc->
setValue_mrpc( pulse_length_channel_without_correction,(tdcChannel & 0x07e000) );
688 unsigned int iden_value_key_mrpc = (iden_value & 0xffffffff );
707 m_tofDataMap.insert( make_pair( iden_value_key_mrpc, tof ) );
730 log << MSG::INFO <<
"TofRawDataProvider::tofDataMapOnlineMode()" << endreq;
732 if( ! m_tofDataMapOnline.empty() ) {
735 for( ;
iter != m_tofDataMapOnline.end();
iter++ ) {
736 delete (*iter).second;
738 m_tofDataMapOnline.clear();
740 else return m_tofDataMapOnline;
743 std::string evtDataSvc_name(
"EventDataSvc");
744 if(isGaudiThreaded(
m_name)){
745 evtDataSvc_name += getGaudiThreadIDfromName(
m_name);
749 IDataProviderSvc* eventSvc;
750 StatusCode sc =
m_svcLocator->service( evtDataSvc_name.c_str(), eventSvc,
true );
751 if( !sc.isSuccess() ) {
752 log << MSG::FATAL <<
"TofRawDataProvider::tofDataMapOnlineMode(): ERROR Could not load EventDataSvc" << endreq;
753 return m_tofDataMapOnline;
757 SmartDataPtr<TofDigiCol> tofDigiCol(eventSvc,
"/Event/Digi/TofDigiCol");
759 log << MSG::WARNING <<
"TofRawDataProvider::tofDataMapOnlineMode(): Could not find Tof Digi Data!" << endreq;
760 return m_tofDataMapOnline;
763 TofDigiCol::iterator
iter = tofDigiCol->begin();
764 for( ;
iter != tofDigiCol->end();
iter++ ) {
765 unsigned int overflow = (*iter)->getOverflow();
766 if( ( overflow & 0xfe000000 ) == 0xfe000000 )
continue;
770 bool noT = ( ( overflow & 0x10 ) != 0 );
776 unsigned int iden_value = iden.
get_value();
780 if( ( barrel_ec != 1 ) && ( end == 1 ) )
continue;
781 if( ( barrel_ec != 1 ) && (
id == 48 ) )
continue;
783 unsigned int tdcChannel = (*iter)->getTimeChannel();
784 if( tdcChannel == 0x7fffffff )
continue;
791 unsigned int iden_value_key = ( iden_value & 0xfffffffe );
793 if( barrel_ec == 0 || barrel_ec == 2 ) {
797 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
799 else if( barrel_ec == 1 ) {
800 unsigned int count = m_tofDataMapOnline.count( iden_value_key );
811 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
815 pair< IterTofDataMap, IterTofDataMap > range = m_tofDataMapOnline.equal_range( iden_value_key );
817 for(
unsigned int i=0; i<count; i++,
iter++ ) {
818 if( ( end == 0 ) && ( ( ( (*iter).second)->quality() & 0x1 ) != 0 ) ) {
819 double time1 = pmt->
tdc();
820 double time2 = ((*iter).second)->tdc2();
823 if( ( ( (*iter).second)->quality() & 0x4 ) == 0 ) {
824 ((*iter).second)->setForward( pmt );
831 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
835 else if( ( end == 1 ) && ( ( ((*iter).second)->quality() & 0x4 ) != 0 ) ) {
836 double time1 = ((*iter).second)->tdc1();
837 double time2 = pmt->
tdc();
841 if( ( ( (*iter).second)->quality() & 0x1 ) == 0 ) {
842 ((*iter).second)->setBackward( pmt );
847 tof->
setForward( ((*iter).second)->forward() );
849 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
863 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
869 return m_tofDataMapOnline;
877 log << MSG::INFO <<
"TofRawDataProvider::tofDataVectorOnlineMode()" << endreq;
879 if( m_tofDataVectorOnline.size() != 0 ) {
880 if(!control)
return m_tofDataVectorOnline;
881 else m_tofDataVectorOnline.clear();
885 for( ;
iter != tofRawDataMap.end();
iter++ ) {
886 m_tofDataVectorOnline.push_back(
iter->second );
889 return m_tofDataVectorOnline;
901 if( !m_tofDataMapEstime.empty() ) {
902 m_tofDataMapEstime.clear();
906 for( ;
iter != m_tofDataMap.end();
iter++ ) {
907 if( ( ( ((*iter).second)->quality() & 0x5 ) != 0 ) && ((*iter).second)->tmatched() ) {
908 m_tofDataMapEstime.insert( make_pair( (*iter).first, (*iter).second ) );
912 iter = m_tofDataMapEstime.begin();
913 while(
iter != m_tofDataMapEstime.end() ) {
914 unsigned int iden_value = (*iter).first;
915 unsigned int count = m_tofDataMapEstime.count( iden_value );
916 for(
unsigned int i=0; i != count; i++,
iter++ ) {
917 ((*iter).second)->setTimes( count );
921 return m_tofDataMapEstime;
926 if( m_tofDataVectorEstime.size() != 0 )
return m_tofDataVectorEstime;
931 m_tofDataVectorEstime.push_back(
iter->second );
933 return m_tofDataVectorEstime;
945 if( !m_tofDataMapTof.empty() ) {
946 m_tofDataMapTof.clear();
950 for( ;
iter != m_tofDataMap.end();
iter++ ) {
951 if( ( ( ((*iter).second)->quality() & 0xc ) == 0xc ) || ( ( ((*iter).second)->quality() & 0x3 ) == 0x3 ) ) {
953 if( estime > 1.0e-6 ) {
954 double tdc1 = ((*iter).second)->tdc1();
955 double tdc2 = ((*iter).second)->tdc2();
965 if( !forward && !backward )
continue;
970 m_tofDataMapTof.insert( make_pair( (*iter).first, (*iter).second ) );
974 iter = m_tofDataMapTof.begin();
975 while(
iter != m_tofDataMapTof.end() ) {
976 unsigned int iden_value = (*iter).first;
977 unsigned int count = m_tofDataMapTof.count( iden_value );
978 for(
unsigned int i=0; i != count; i++,
iter++ ) {
979 ((*iter).second)->setTimes( count );
983 return m_tofDataMapTof;
988 if( m_tofDataVectorTof.size() != 0 ) {
989 m_tofDataVectorTof.clear();
995 m_tofDataVectorTof.push_back(
iter->second );
997 return m_tofDataVectorTof;
1004 if( !m_hasFilled ) {
1009 if( !m_tofDataMapEmc.empty() ) {
1010 m_tofDataMapEmc.clear();
1014 for( ;
iter != m_tofDataMap.end();
iter++ ) {
1015 if( ( ((*iter).second)->quality() & 0xa ) != 0 ) {
1017 if( estime > 1.0e-6 ) {
1018 int t0clock =
static_cast<int>(estime/24.0);
1019 int clock1 = ((*iter).second)->qclock1();
1020 int clock2 = ((*iter).second)->qclock2();
1023 if( !forward && !backward )
continue;
1026 m_tofDataMapEmc.insert( make_pair( (*iter).first, (*iter).second ) );
1030 iter = m_tofDataMapEmc.begin();
1031 while(
iter != m_tofDataMapEmc.end() ) {
1032 unsigned int iden_value = (*iter).first;
1033 unsigned int count = m_tofDataMapEmc.count( iden_value );
1034 for(
unsigned int i=0; i != count; i++,
iter++ ) {
1035 ((*iter).second)->setTimes( count );
1039 return m_tofDataMapEmc;
1044 if( m_tofDataVectorEmc.size() != 0 ) {
1045 m_tofDataVectorEmc.clear();
1051 m_tofDataVectorEmc.push_back(
iter->second );
1053 return m_tofDataVectorEmc;
double abs(const EvtComplex &c)
ITofQElecSvc * tofQElecSvc
ITofQCorrSvc * tofQCorrSvc
std::multimap< unsigned int, TofData * > TofDataMap
std::vector< TofData * > TofDataVector
std::multimap< unsignedint, TofData * >::iterator IterTofDataMap
ITofQElecSvc * tofQElecSvc
ITofQCorrSvc * tofQCorrSvc
const double tClockBackward
const double tClockForward
const double timeBackward
void setValue_mrpc(int value, int clock)
virtual const int BrWest(unsigned int No)=0
virtual const int QElec()=0
virtual const int BrEast(unsigned int No)=0
virtual const int QCorr()=0
virtual const int MisLable()=0
virtual const int Endcap(unsigned int No)=0
value_type get_value() const
unsigned int quality() const
StatusCode initialize(ISvcLocator *svcLoc=0, IMessageSvc *pMsg=0)
ISvcLocator * m_svcLocator
static unsigned int TofTimeChannel(double time)
static double TofTime(unsigned int timeChannel)
unsigned int quality() const
void setForward(PmtData *pmt)
void setTMatched(bool tmatched)
void setBackward(PmtData *pmt)
void setIdentify(Identifier identify)
static int end(const Identifier &id)
static int phi_module(const Identifier &id)
static int barrel_ec(const Identifier &id)
Values of different levels (failure returns 0)
static value_type getIntID(int barrel_ec, int layer, int phi_module, int end)
TofDataMap & tofDataMapTof(double estime=0.0)
void handle(const Incident &)
TofDataVector & tofDataVectorTof(double estime=0.0)
TofDataMap & tofDataMapOnlineMode(uint32_t control=1)
TofDataMap & tofDataMapEstime()
TofDataVector & tofDataVectorEmc(double estime=0.0)
TofDataVector & tofDataVectorEstime()
TofDataVector & tofDataVectorOnlineMode(uint32_t control=1)
StatusCode initialize(bool mode=false, ISvcLocator *svcLoc=0, IMessageSvc *pMsg=0)
TofDataMap & tofDataMapEmc(double estime=0.0)
void setNumber(int number)