6 #include "GaudiKernel/MsgStream.h"
7 #include "Gaudi/Property.h"
10 #include "eformat/Status.h"
12 #include "GaudiKernel/TypeNameString.h"
18 #include "GaudiKernel/ThreadLocalContext.h"
32 static const InterfaceID IID_ITBByteStreamCnvTool
33 (
"TBByteStreamCnvTool", 1, 0);
34 return IID_ITBByteStreamCnvTool;
40 (
const std::string&
type,
const std::string&
name,
const IInterface*
parent )
42 m_ByteStreamEventAccess(
"ByteStreamCnvSvc",
name),
43 m_rdpSvc(
"ROBDataProviderSvc",
name),
44 m_lastEventID(0, UINT_MAX), m_subdet_id(
eformat::TDAQ_BEAM_CRATE),
45 m_theRodBlock(0), m_theROB(0)
47 declareInterface< TBByteStreamCnvTool >(
this );
61 if (
sc!=StatusCode::SUCCESS)
67 sc =
detStore()->retrieve(online_id,
"LArOnlineID");
70 return StatusCode::FAILURE;
80 std::vector<std::string>::const_iterator
it =
m_keys.begin();
81 std::vector<std::string>::const_iterator it_e =
m_keys.end();
82 std::vector<std::string>
keys;
84 for(;
it!=it_e;++
it) {
85 const Gaudi::Utils::TypeNameString &
item(*
it);
86 const std::string &
t =
item.type();
87 const std::string &
nm =
item.name();
90 if(
t==
"TBTriggerPatternUnit")
keys[1]=
nm;
91 if(
t==
"TBTDCRawCont")
keys[2]=
nm;
92 if(
t==
"TBADCRawCont")
keys[3]=
nm;
93 if(
t==
"TBTailCatcherRaw")
keys[4]=
nm;
94 if(
t==
"TBBPCRawCont")
keys[5]=
nm;
95 if(
t==
"TBScintillatorRawCont")
keys[6]=
nm;
96 if(
t==
"TBMWPCRawCont")
keys[7]=
nm;
119 return StatusCode::SUCCESS;
124 return StatusCode::SUCCESS;
143 if (
sc!=StatusCode::SUCCESS)
144 {logstr << MSG::WARNING <<
"No EventInfo object found!" <<
endmsg;
149 {logstr << MSG::WARNING <<
"LArByteStrem persistency representation for Event #"<<thisEvent->event_number()
150 <<
" already created!" <<
endmsg;
151 return StatusCode::SUCCESS;
168 if (
sc!=StatusCode::SUCCESS)
177 const bool status_place=eformat::STATUS_BACK;
181 eformat::helper::SourceIdentifier robsid(
m_subdet_id, module_id);
183 (robsid.code(),run_no,lvl1_id,bc_id,lvl1_type,detev_type,
m_theRodBlock->size(),
187 logstr <<
MSG::DEBUG <<
"Appended TestBeam fragment to ByteStream" <<
endmsg;
188 return StatusCode::SUCCESS;
201 const TBTDC* tbtdc =
nullptr;
202 std::string tbtdc_Key(
"TBTDC");
204 if (
sc!=StatusCode::SUCCESS)
205 {logstr << MSG::ERROR <<
"Can't retrieve TBTDC with key "<< tbtdc_Key <<
" from StoreGate" <<
endmsg;
209 theRodBlock->push_back(4);
210 theRodBlock->push_back(0x03);
211 theRodBlock->push_back(tbtdc->
tdc());
212 theRodBlock->push_back(tbtdc->
tdcmin());
250 return StatusCode::SUCCESS;
276 if (
sc!=StatusCode::SUCCESS)
280 logstr <<
"ReadRodBlock: RodBlock too small!" <<
endmsg;
281 return StatusCode::FAILURE;
288 return StatusCode::SUCCESS;
368 logstr << MSG::ERROR <<
" Can not create a LArDigitContainer with key="<<
key<<
endmsg;
370 return StatusCode::FAILURE;
387 logstr << MSG::ERROR <<
" Can not create a LArCalibDigitContainer with key="<<
key<<
endmsg;
388 tblarcalibdigitcont =0;
389 return StatusCode::FAILURE;
396 eformat::SubDetector subdet_rod_id)
401 {logstr << MSG::ERROR <<
"ROBDataProviderSvc not loaded. Can't read ByteStream" <<
endmsg;
402 return StatusCode::FAILURE;
409 return StatusCode::FAILURE;
412 const size_t MAX_ROBFRAGMENTS = 2048*1024;
414 size_t robcount =
re->children(robF,MAX_ROBFRAGMENTS);
416 if (robcount == MAX_ROBFRAGMENTS)
419 return StatusCode::FAILURE;
422 for (
size_t irob=0; irob<robcount; ++irob)
428 MSG::hex(logstr) <<
MSG::DEBUG <<
"Frag Size : " << rob.fragment_size_word() <<
endmsg;
429 MSG::hex(logstr) <<
MSG::DEBUG <<
"Header Size: " << rob.header_size_word() <<
endmsg;
430 MSG::hex(logstr) <<
MSG::DEBUG <<
"Source ID : " << rob.source_id() <<
endmsg;
438 logstr << MSG::ERROR <<
"Got invalid ROB fragment!" <<
endmsg;
439 return StatusCode::FAILURE;
443 MSG::hex(logstr) <<
MSG::DEBUG <<
"Frag Size : " << rob.rod_fragment_size_word() <<
endmsg;
444 MSG::hex(logstr) <<
MSG::DEBUG <<
"Header Size: " << rob.rod_header_size_word() <<
endmsg;
445 MSG::hex(logstr) <<
MSG::DEBUG <<
"Source ID : " << rob.rod_source_id() <<
endmsg;
446 MSG::hex(logstr) <<
MSG::DEBUG <<
"Run num : " << rob.rod_run_no() <<
endmsg;
447 MSG::hex(logstr) <<
MSG::DEBUG <<
"Version : " << rob.rod_version() <<
endmsg;
449 eformat::helper::SourceIdentifier rod_sid(rob.rod_source_id());
452 MSG::hex(logstr) <<
MSG::DEBUG <<
"Found requested ROD (id="
453 << rod_sid.subdetector_id() <<
")" <<
endmsg;
457 logstr<< MSG::INFO <<
"Guessing from ROD header -> " ;
458 if (rob.rod_detev_type()&1)
460 logstr<<
" H6 !" <<
endmsg;
466 logstr<<
" H8 !" <<
endmsg;
477 rob.rod_data(rodPointer);
479 for(
size_t i=0;
i<rob.rod_ndata(); ++
i)
487 logstr <<
MSG::FATAL <<
"Error reading bytestream event: "
488 <<
"Empty ROD block" <<
endmsg;
489 return StatusCode::FAILURE;
491 logstr <<
MSG::DEBUG <<
"Got Rod Block for Test Beam Instrumentation data from ByteStream" <<
endmsg;
492 return StatusCode::SUCCESS;
496 MSG::hex(logstr) << MSG::WARNING <<
" Didn't find SubDet ID for Beam Instruments. Should be "<< subdet_id<<
endmsg;
497 return StatusCode::FAILURE;
516 const EventContext& ctx = Gaudi::Hive::currentContext();
523 std::ostringstream
os;
527 if (
sc!=StatusCode::SUCCESS)
531 logstr << MSG::ERROR <<
"ReadRodBlock: RodBlock too small!" <<
endmsg;
532 return StatusCode::FAILURE;
547 std::string
name1,name2;
596 short m_feedthrough[8] = {0,0,0,0,1,1,1,1};
597 int m_sampleOrder[32] = {
598 0,1,2,3,4,5,6,7,8,9,10,
599 11,12,13,14,15,16,17,18,19,20,
600 21,22,23,24,25,26,27,28,29,30,
602 int m_sort[64] = { 55, 39, 23, 7, 119, 103, 87, 71,
603 54, 38, 22, 6, 118, 102, 86, 70,
604 53, 37, 21, 5, 117, 101, 85, 69,
605 52, 36, 20, 4, 116, 100, 84, 68,
606 51, 35, 19, 3, 115, 99, 83, 67,
607 50, 34, 18, 2, 114, 98, 82, 66,
608 49, 33, 17, 1, 113, 97, 81, 65,
609 48, 32, 16, 0, 112, 96, 80, 64
613 constexpr
short m_slot[8]={5,7,9,11,3,4,5,6};
615 constexpr
int NWREC = 8;
617 for(
unsigned int nfeb=0;nfeb<
m_boards.size();nfeb++){
636 if((m_sampleOrder[0]=
m_febgain[nfeb])!=0){
637 for(
int j=0;j<
m_febgain[nfeb];j++) {m_sampleOrder[j+1]=j;}
640 samp=m_sampleOrder[
s];
643 for(
int i= 0 ;
i<64;
i++){
647 unsigned short gain1 = ((tmp1>>12)&3) - 1;
651 unsigned short gain2 = ((
tmp2>>12)&3) - 1;
678 for(
int i=0;
i<128;
i++) {
688 const std::vector<HWIdentifier>& calibChannelIDs=calibLine->
calibSlotLine(hwid);
689 if (calibChannelIDs.size() != 0) {
719 name1=
"word1frag0x03chan";
720 name2=
"word2frag0x03chan";
724 logstr<<MSG::ERROR<<
"Beam counter subfrag (0x03) has not expected size" <<
endmsg;
725 sc=StatusCode::FAILURE;
734 if(
m_dump) logstr << MSG::INFO <<
" sub frag 0x03 : word1="<<tmp1<<
" word2="<<
tmp2 <<
endmsg;
744 if(
m_dump) logstr << MSG::INFO <<
" sub frag 0x03 : word1="<<tmp1<<
" word2="<<
tmp2 <<
endmsg;
754 if(
m_dump) logstr << MSG::INFO <<
" sub frag 0x03 : word1="<<tmp1<<
" word2="<<
tmp2 <<
endmsg;
765 name1=
"word1frag0x06chan";
766 name2=
"word2frag0x06chan";
767 std::string tcname=
"tc";
768 std::vector<TBScintillatorRaw *> theScints;
770 logstr<<MSG::ERROR<<
"Beam counter subfrag (0x04) has not expected size" <<
endmsg;
771 sc=StatusCode::FAILURE;
775 for(
unsigned int i=0;
i<24;
i++){
798 os << tcname << 2*
i+1;
800 theScints.push_back(
s1);
801 theScints.push_back(
s2);
811 name1=
"word1frag0x05chan";
812 name2=
"word2frag0x05chan";
815 logstr <<
MSG::DEBUG <<
"H6BuildObject : number of BPCRaw "<< bpcnum <<
endmsg;
817 for(
int i=0;
i<bpcnum;
i++){
818 std::string bpcname =
"BPC";
822 std::vector<const TBTDCRaw*> theTDCs;
823 std::vector<const TBADCRaw*> theADCs;
826 unsigned int tmp1,
tmp2;
833 os << name2 << (
i*3);
842 os << name2 << (
i*3+1);
850 MSG::hex(logstr) <<
MSG::DEBUG << bpcname<<
" "<< tmp1;
851 MSG::hex(logstr) <<
" "<<
tmp2<<
" ";
859 os << name2 << (
i*3+2);
864 MSG::hex(logstr) <<
MSG::DEBUG << bpcname<<
" "<< tmp1;
868 theTDCs.push_back(tbtdcL);
869 theTDCs.push_back(tbtdcR);
870 theTDCs.push_back(tbtdcU);
871 theTDCs.push_back(tbtdcD);
872 theADCs.push_back(tbadcH);
873 theADCs.push_back(tbadcV);
884 name1=
"word1frag0x06chan";
885 name2=
"word2frag0x06chan";
887 logstr<<MSG::ERROR<<
"Beam counter subfrag (0x06) has not expected size" <<
endmsg;
888 sc=StatusCode::FAILURE;
902 logstr <<
MSG::DEBUG <<
" building muon counters:";
903 for(
int i=0;
i<8;
i++) {
904 std::string scintname=
"muon";
906 os << scintname << (
i+1);
942 for(
int i=0;
i<4;
i++) {
949 os << name2 << (0+3+
i);
954 if(
testbit(10,word2)) adc2->setOverflow();
1001 for(
int i=0;
i<8;
i++) {
1005 logstr <<
MSG::DEBUG <<
" Should I record TBScintillatorRawCont with key " <<
m_keys[6] <<
" ? " <<
endmsg;
1014 logstr <<
MSG::DEBUG <<
" Found MWPC subfragment. Nword= "<<nword <<
endmsg;
1016 std::string mwpcnames[8]= {
"X2",
"Y2",
"X3",
"Y3",
"X4",
"Y4",
"X5",
"Y5"};
1017 bool isX[9]={
true,
false,
true,
false,
true,
false,
true,
false};
1019 for(
int i=0;
i<8;
i++){
1068 w = (
int) (word2>>12);
c = (
int) ((0xfff & word2) -
w/2 + !(
w&1));
1083 for(
int i=0;
i<8;
i++){
1098 logstr << MSG::ERROR <<
"bad number of lines. nwords=" << nword <<
endmsg;
1101 int nline= nword / 16;
1103 for(
int i=0;
i<nline;
i++){
1105 std::string sline=
"";
1106 std::string skey=
"";
1107 for(
int j=0;j<63;j++) {
1108 if(strw[
i*64+j]==0)
break;
1109 sline+=strw[
i*64+j];
1112 std::istringstream iss (sline);
1114 if(skey==
"RunNumber") {
1118 if(skey==
"BeamMomentum") {
1122 if(skey==
"BeamParticle") {
1130 if(skey==
"CryoAngle") {
1134 if(skey==
"TableY") {
1138 if(skey==
"miniROD") {
1143 while(iss.good()) {iss>>board;
m_boards.push_back(board);}
1148 if(skey==
"FebSamples") {
1158 if(skey==
"FebGains") {
1170 if(skey==
"FebAuto") {
1180 if(skey==
"FebFirstSample") {
1185 while(iss.good()) {iss>>firstsample;
m_firstsamples.push_back(firstsample);}
1217 logstr <<
MSG::DEBUG <<
" Found Calib_ID subfragment. Nword= "<<nword <<
endmsg;
1219 logstr<<MSG::ERROR<<
"Calibration subfrag (0xff) has not expected size" <<
endmsg;
1220 sc=StatusCode::FAILURE;
1226 for(
int i=0;
i<4;
i++){
1238 for(
int byte=0;
byte<16;
byte++) {
1239 logstr <<
MSG::DEBUG <<
" byte " <<
byte <<
" : " ;
1240 for(
int bit=0; bit<8; bit++) {
1292 logstr <<
MSG::DEBUG <<
"H8BuildObject invoked for code " << unrec_code<<
endmsg;
1295 bool gotobject=
false;
1296 bool recordfailure=
false;
1298 bool goodRodBlock=
true;
1307 if (
sc!=StatusCode::SUCCESS) {
1309 logstr << MSG::ERROR <<
"ReadRodBlock: RodBlock error! Will try to fill SG with empty objects." <<
endmsg;
1313 logstr << MSG::ERROR <<
"ReadRodBlock: RodBlock too small! Will try to fill SG with empty objects." <<
endmsg;
1375 short tdc_order[4] = {2,3,0,1};
1376 for(
int i=0;
i<BPCNum;
i++){
1378 for(
int j=0;j<4;j++) BPCtdc[
i][j]=0;
1387 std::ostringstream
os;
1400 name=
"frag0x10chan";
1465 logstr << MSG::INFO <<
" DUMP : ";
1467 logstr <<
" " << S3rightadc->
getADC()<<
" " << muTagadc->
getADC() <<
" " << C1adc->
getADC();
1476 name=
"frag0x11chan";
1500 logstr << MSG::INFO <<
" DUMP : ";
1508 name=
"frag0x12chan";
1524 logstr <<
MSG::DEBUG << MSG::dec <<
"Pos=" <<
pos <<
" Chan "<<
chan <<
"=" << tdc <<
", OV=" << overflow
1525 <<
", UN=" <<underthresh <<
" EOD="<< endofdata <<
", corrupt=" << corrup
1531 if (!endofdata && !corrup) {
1536 ClockPhasetdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1540 S1tdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1544 S2downtdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1548 S3lefttdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1552 S2uptdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1556 S3righttdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1560 S0tdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1573 logstr << MSG::ERROR <<
" Unexpected end-of-data word found at pos " <<
pos <<
endmsg;
1576 logstr<< MSG::ERROR <<
"Corrupted data in SubFragment 0x12 pos=" <<
pos <<
"channel=" <<
chan <<
" [0x" <<
1584 name=
"frag0x13chan";
1595 logstr <<
MSG::DEBUG << MSG::dec <<
"Pos=" <<
pos <<
" Chan "<<
chan <<
"=" << tdc
1596 <<
" EOD="<< endofdata <<
", corrupt=" << corrup
1598 if (!endofdata && !corrup) {
1603 const short bpc_ind =
chan / 4 ;
1605 logstr<<
MSG::DEBUG <<
"Error in 0x13 : found chan="<<
chan<<
" corresponding to bpc "<<bpc_ind<<
endmsg;
1608 const short signaltype = tdc_order[
chan % 4];
1609 if (BPCtdc[bpc_ind][signaltype]==NULL)
1610 BPCtdc[bpc_ind][signaltype] = tbtdc;
1617 logstr << MSG::ERROR <<
" Unexpected end-of-data word found at pos " <<
pos <<
endmsg;
1620 logstr<< MSG::ERROR <<
"Corrupted data in SubFragment 0x13 pos=" <<
pos <<
"channel=" <<
chan <<
" [0x" <<
1628 name=
"frag0x14chan";
1646 logstr <<
MSG::DEBUG << MSG::dec <<
"Pos=" <<
pos <<
" Chan "<<
chan <<
"=" << tdc
1647 <<
" EOD="<< endofdata <<
", corrupt=" << corrup
1650 if (!endofdata && !corrup) {
1655 const short bpc_ind =
chan / 4 + 3;
1656 if(bpc_ind>4) { logstr<<
MSG::DEBUG <<
"Error in 0x14 : found chan="<<
chan<<
" corresponding to bpc "<<bpc_ind<<
endmsg;
continue;}
1657 const short signaltype = tdc_order[
chan % 4];
1658 if (BPCtdc[bpc_ind][signaltype]==NULL)
1659 BPCtdc[bpc_ind][signaltype] = tbtdc;
1666 logstr << MSG::ERROR <<
" Unexpected end-of-data word found at pos " <<
pos <<
endmsg;
1669 logstr<< MSG::ERROR <<
"Corrupted data in SubFragment 0x14 pos=" <<
pos <<
"channel=" <<
chan <<
" [0x" <<
1676 logstr << MSG::INFO <<
"Found trigger word fragment" <<
endmsg;
1738 for(
int i=0;
i<BPCNum;
i++){
1739 std::vector<const TBTDCRaw*> listtdc;
1740 std::vector<const TBADCRaw*> listadc;
1741 for(
int j=0;j<4;j++){
1742 if(BPCtdc[
i][j]==0) listtdc.push_back(dummytdc);
1743 else listtdc.push_back(BPCtdc[
i][j]);
1745 listadc.push_back(dummyadc); listadc.push_back(dummyadc);
1757 if (
sc!=StatusCode::SUCCESS) {
1759 goodRodBlock =
false;
1760 logstr << MSG::ERROR <<
"Tile laser crate ROD block error! Nothing valid in SG." <<
endmsg;
1763 logstr << MSG::ERROR <<
"Tile laser crate ROD block too small! Nothing valid in SG" <<
endmsg;
1765 goodRodBlock =
false;
1798 for(
int i=0;
i<8;
i++){
1800 std::stringstream RawName;
1801 RawName <<
"frag0x1chan"<<
i;
1805 std::stringstream MuWallName;
1806 MuWallName <<
"MuWall"<<
i;
1819 for(
int i=0;
i<4;
i++){
1821 std::stringstream RawName;
1822 RawName <<
"frag0x1chan"<<(
i+8);
1826 std::stringstream MuWallName;
1827 MuWallName <<
"MuWall"<<
i+8;
1860 if (
sc.isFailure( ) ) {recordfailure=
true;
1863 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1868 if (
sc.isFailure( ) ) {recordfailure=
true;
1871 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1876 if (
sc.isFailure( ) ) {recordfailure=
true;
1879 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1883 if (
sc.isFailure( ) ) {recordfailure=
true;
1886 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1895 if (
sc.isFailure( ) ) {
1896 logstr << MSG::ERROR <<
"Cannot record TBTrigPat " <<
endmsg;
1899 sc=StatusCode::SUCCESS;
1905 if(!gotobject) {logstr<< MSG::ERROR<<
" Could not find object of type "<<unrec_code <<
endmsg;
sc=StatusCode::FAILURE;}
1906 if(recordfailure) {logstr<< MSG::ERROR<<
" One object could not be recorded "<<
endmsg;
sc=StatusCode::FAILURE;}
1917 logstr <<
MSG::DEBUG <<
"About to try recording. unrec_code = " << unrec_code <<
endmsg;
1920 logstr <<
MSG::DEBUG <<
" WARNING! Did not find TailCatcher. Returning an empty one : this may crash every attempt to use it" <<
endmsg;
1927 if (
sc.isFailure( ) ) {
1928 logstr << MSG::ERROR <<
"Cannot record TBTrigPat " <<
endmsg;
1935 if (
sc.isFailure( ) ) {
1936 logstr << MSG::ERROR <<
"Cannot record TailCatcherRaw " <<
endmsg;
1944 if (
sc.isFailure( ) ) {
1945 logstr << MSG::ERROR <<
"Cannot record BPCRawCont" <<
endmsg;
1956 if (
sc.isFailure( ) ) {
1957 logstr << MSG::ERROR <<
"Cannot record ScintRawCont " <<
endmsg;
1968 if (
sc.isFailure( ) ) {
1969 logstr << MSG::ERROR <<
"Cannot record MWPCRawCont " <<
endmsg;
1983 if (
sc.isFailure( ) ) {
1984 logstr << MSG::ERROR <<
"Cannot record TDCCont " <<
endmsg;
1996 if (
sc.isFailure( ) ) {
2004 for(
int c=10;
c<14;
c++){
2009 if (
sc.isFailure( ) ) {
2015 for(
int c=10;
c<14;
c++){
2020 if (
sc.isFailure( ) ) {
2028 if (
sc.isFailure( ) ) {