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)
555 unsigned int iden_value = iden.
get_value();
562 unsigned int tdcChannel = (*iter)->getTimeChannel();
563 unsigned int tdc2Channel = (*iter)->getChargeChannel();
567 unsigned int pulse_length_channel_without_correction = tdc2Channel-tdcChannel;
570 if(correction_ta<0)
continue;
572 time_of_flight=time_of_flight - correction_ta/1000.;
582 adc->
setValue_mrpc( pulse_length_channel_without_correction,(tdcChannel & 0x07e000) );
610 unsigned int iden_value_key_mrpc = (iden_value & 0xffffffff );
620 m_tofDataMap.insert( make_pair( iden_value_key_mrpc, tof ) );
643 log << MSG::INFO <<
"TofRawDataProvider::tofDataMapOnlineMode()" << endreq;
645 if( ! m_tofDataMapOnline.empty() ) {
648 for( ;
iter != m_tofDataMapOnline.end();
iter++ ) {
649 delete (*iter).second;
651 m_tofDataMapOnline.clear();
653 else return m_tofDataMapOnline;
656 std::string evtDataSvc_name(
"EventDataSvc");
657 if(isGaudiThreaded(
m_name)){
658 evtDataSvc_name += getGaudiThreadIDfromName(
m_name);
662 IDataProviderSvc* eventSvc;
663 StatusCode sc =
m_svcLocator->service( evtDataSvc_name.c_str(), eventSvc,
true );
664 if( !sc.isSuccess() ) {
665 log << MSG::FATAL <<
"TofRawDataProvider::tofDataMapOnlineMode(): ERROR Could not load EventDataSvc" << endreq;
666 return m_tofDataMapOnline;
670 SmartDataPtr<TofDigiCol> tofDigiCol(eventSvc,
"/Event/Digi/TofDigiCol");
672 log << MSG::WARNING <<
"TofRawDataProvider::tofDataMapOnlineMode(): Could not find Tof Digi Data!" << endreq;
673 return m_tofDataMapOnline;
676 TofDigiCol::iterator
iter = tofDigiCol->begin();
677 for( ;
iter != tofDigiCol->end();
iter++ ) {
678 unsigned int overflow = (*iter)->getOverflow();
679 if( ( overflow & 0xfe000000 ) == 0xfe000000 )
continue;
683 bool noT = ( ( overflow & 0x10 ) != 0 );
689 unsigned int iden_value = iden.
get_value();
693 if( ( barrel_ec != 1 ) && ( end == 1 ) )
continue;
694 if( ( barrel_ec != 1 ) && (
id == 48 ) )
continue;
696 unsigned int tdcChannel = (*iter)->getTimeChannel();
697 if( tdcChannel == 0x7fffffff )
continue;
704 unsigned int iden_value_key = ( iden_value & 0xfffffffe );
706 if( barrel_ec == 0 || barrel_ec == 2 ) {
710 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
712 else if( barrel_ec == 1 ) {
713 unsigned int count = m_tofDataMapOnline.count( iden_value_key );
724 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
728 pair< IterTofDataMap, IterTofDataMap > range = m_tofDataMapOnline.equal_range( iden_value_key );
730 for(
unsigned int i=0; i<count; i++,
iter++ ) {
731 if( ( end == 0 ) && ( ( ( (*iter).second)->quality() & 0x1 ) != 0 ) ) {
732 double time1 = pmt->
tdc();
733 double time2 = ((*iter).second)->tdc2();
736 if( ( ( (*iter).second)->quality() & 0x4 ) == 0 ) {
737 ((*iter).second)->setForward( pmt );
744 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
748 else if( ( end == 1 ) && ( ( ((*iter).second)->quality() & 0x4 ) != 0 ) ) {
749 double time1 = ((*iter).second)->tdc1();
750 double time2 = pmt->
tdc();
754 if( ( ( (*iter).second)->quality() & 0x1 ) == 0 ) {
755 ((*iter).second)->setBackward( pmt );
760 tof->
setForward( ((*iter).second)->forward() );
762 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
776 m_tofDataMapOnline.insert( make_pair( iden_value_key, tof ) );
782 return m_tofDataMapOnline;
790 log << MSG::INFO <<
"TofRawDataProvider::tofDataVectorOnlineMode()" << endreq;
792 if( m_tofDataVectorOnline.size() != 0 ) {
793 if(!control)
return m_tofDataVectorOnline;
794 else m_tofDataVectorOnline.clear();
798 for( ;
iter != tofRawDataMap.end();
iter++ ) {
799 m_tofDataVectorOnline.push_back(
iter->second );
802 return m_tofDataVectorOnline;
814 if( !m_tofDataMapEstime.empty() ) {
815 m_tofDataMapEstime.clear();
819 for( ;
iter != m_tofDataMap.end();
iter++ ) {
820 if( ( ( ((*iter).second)->quality() & 0x5 ) != 0 ) && ((*iter).second)->tmatched() ) {
821 m_tofDataMapEstime.insert( make_pair( (*iter).first, (*iter).second ) );
825 iter = m_tofDataMapEstime.begin();
826 while(
iter != m_tofDataMapEstime.end() ) {
827 unsigned int iden_value = (*iter).first;
828 unsigned int count = m_tofDataMapEstime.count( iden_value );
829 for(
unsigned int i=0; i != count; i++,
iter++ ) {
830 ((*iter).second)->setTimes( count );
834 return m_tofDataMapEstime;
839 if( m_tofDataVectorEstime.size() != 0 )
return m_tofDataVectorEstime;
844 m_tofDataVectorEstime.push_back(
iter->second );
846 return m_tofDataVectorEstime;
858 if( !m_tofDataMapTof.empty() ) {
859 m_tofDataMapTof.clear();
863 for( ;
iter != m_tofDataMap.end();
iter++ ) {
864 if( ( ( ((*iter).second)->quality() & 0xc ) == 0xc ) || ( ( ((*iter).second)->quality() & 0x3 ) == 0x3 ) ) {
866 if( estime > 1.0e-6 ) {
867 double tdc1 = ((*iter).second)->tdc1();
868 double tdc2 = ((*iter).second)->tdc2();
878 if( !forward && !backward )
continue;
883 m_tofDataMapTof.insert( make_pair( (*iter).first, (*iter).second ) );
887 iter = m_tofDataMapTof.begin();
888 while(
iter != m_tofDataMapTof.end() ) {
889 unsigned int iden_value = (*iter).first;
890 unsigned int count = m_tofDataMapTof.count( iden_value );
891 for(
unsigned int i=0; i != count; i++,
iter++ ) {
892 ((*iter).second)->setTimes( count );
896 return m_tofDataMapTof;
901 if( m_tofDataVectorTof.size() != 0 ) {
902 m_tofDataVectorTof.clear();
908 m_tofDataVectorTof.push_back(
iter->second );
910 return m_tofDataVectorTof;
922 if( !m_tofDataMapEmc.empty() ) {
923 m_tofDataMapEmc.clear();
927 for( ;
iter != m_tofDataMap.end();
iter++ ) {
928 if( ( ((*iter).second)->quality() & 0xa ) != 0 ) {
930 if( estime > 1.0e-6 ) {
931 int t0clock =
static_cast<int>(estime/24.0);
932 int clock1 = ((*iter).second)->qclock1();
933 int clock2 = ((*iter).second)->qclock2();
936 if( !forward && !backward )
continue;
939 m_tofDataMapEmc.insert( make_pair( (*iter).first, (*iter).second ) );
943 iter = m_tofDataMapEmc.begin();
944 while(
iter != m_tofDataMapEmc.end() ) {
945 unsigned int iden_value = (*iter).first;
946 unsigned int count = m_tofDataMapEmc.count( iden_value );
947 for(
unsigned int i=0; i != count; i++,
iter++ ) {
948 ((*iter).second)->setTimes( count );
952 return m_tofDataMapEmc;
957 if( m_tofDataVectorEmc.size() != 0 ) {
958 m_tofDataVectorEmc.clear();
964 m_tofDataVectorEmc.push_back(
iter->second );
966 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
double Time_Amplitude_Correction(unsigned int tdcChannel, unsigned int tdc2Channel)
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)
MrpcCalibSvc * mrpcCaliSvc
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)