6 #include "GaudiKernel/MsgStream.h"
7 #include "Gaudi/Property.h"
11 #include "eformat/Status.h"
13 #include "GaudiKernel/ListItem.h"
19 #include "GaudiKernel/ThreadLocalContext.h"
33 static const InterfaceID IID_ITBByteStreamCnvTool
34 (
"TBByteStreamCnvTool", 1, 0);
35 return IID_ITBByteStreamCnvTool;
41 (
const std::string&
type,
const std::string&
name,
const IInterface*
parent )
43 m_theRodBlock(0), m_theROB(0)
45 declareInterface< TBByteStreamCnvTool >(
this );
59 if (
sc!=StatusCode::SUCCESS)
65 sc =
detStore()->retrieve(online_id,
"LArOnlineID");
68 return StatusCode::FAILURE;
76 sc=service(
"ToolSvc",toolSvc );
78 logstr << MSG::ERROR <<
"Unable to retrieve ToolSvc" <<
endmsg;
79 return StatusCode::FAILURE;
83 sc= service(
"ByteStreamCnvSvc",
svc);
84 if (
sc!=StatusCode::SUCCESS)
85 {logstr << MSG::ERROR <<
" Can't get ByteStreamEventAccess interface " <<
endmsg;
86 return StatusCode::FAILURE;
92 logstr <<MSG::ERROR<<
" Can't cast to ByteStreamCnvSvc " <<
endmsg;
93 return StatusCode::FAILURE ;
96 sc=service(
"ROBDataProviderSvc",
svc);
97 if (
sc!=StatusCode::SUCCESS)
98 {logstr << MSG::WARNING <<
" Can't get ROBDataProviderSvc. Reading of ByteStream Data not possible " <<
endmsg;
104 {logstr <<MSG::ERROR<<
"Can't cast to ROBDataProviderSvc " <<
endmsg;
105 return StatusCode::FAILURE;
110 std::vector<std::string>::const_iterator
it =
m_keys.begin();
111 std::vector<std::string>::const_iterator it_e =
m_keys.end();
112 std::vector<std::string>
keys;
114 for(;
it!=it_e;++
it) {
115 const ListItem &
item(*
it);
116 const std::string &
t =
item.type();
117 const std::string &
nm =
item.name();
120 if(
t==
"TBTriggerPatternUnit")
keys[1]=
nm;
121 if(
t==
"TBTDCRawCont")
keys[2]=
nm;
122 if(
t==
"TBADCRawCont")
keys[3]=
nm;
123 if(
t==
"TBTailCatcherRaw")
keys[4]=
nm;
124 if(
t==
"TBBPCRawCont")
keys[5]=
nm;
125 if(
t==
"TBScintillatorRawCont")
keys[6]=
nm;
126 if(
t==
"TBMWPCRawCont")
keys[7]=
nm;
149 return StatusCode::SUCCESS;
154 return StatusCode::SUCCESS;
173 if (
sc!=StatusCode::SUCCESS)
174 {logstr << MSG::WARNING <<
"No EventInfo object found!" <<
endmsg;
179 {logstr << MSG::WARNING <<
"LArByteStrem persistency representation for Event #"<<thisEvent->event_number()
180 <<
" already created!" <<
endmsg;
181 return StatusCode::SUCCESS;
198 if (
sc!=StatusCode::SUCCESS)
207 const bool status_place=eformat::STATUS_BACK;
211 eformat::helper::SourceIdentifier robsid(
m_subdet_id, module_id);
213 (robsid.code(),run_no,lvl1_id,bc_id,lvl1_type,detev_type,
m_theRodBlock->size(),
217 logstr <<
MSG::DEBUG <<
"Appended TestBeam fragment to ByteStream" <<
endmsg;
218 return StatusCode::SUCCESS;
231 const TBTDC* tbtdc =
nullptr;
232 std::string tbtdc_Key(
"TBTDC");
234 if (
sc!=StatusCode::SUCCESS)
235 {logstr << MSG::ERROR <<
"Can't retrieve TBTDC with key "<< tbtdc_Key <<
" from StoreGate" <<
endmsg;
239 theRodBlock->push_back(4);
240 theRodBlock->push_back(0x03);
241 theRodBlock->push_back(tbtdc->
tdc());
242 theRodBlock->push_back(tbtdc->
tdcmin());
280 return StatusCode::SUCCESS;
306 if (
sc!=StatusCode::SUCCESS)
310 logstr <<
"ReadRodBlock: RodBlock too small!" <<
endmsg;
311 return StatusCode::FAILURE;
318 return StatusCode::SUCCESS;
398 logstr << MSG::ERROR <<
" Can not create a LArDigitContainer with key="<<
key<<
endmsg;
400 return StatusCode::FAILURE;
417 logstr << MSG::ERROR <<
" Can not create a LArCalibDigitContainer with key="<<
key<<
endmsg;
418 tblarcalibdigitcont =0;
419 return StatusCode::FAILURE;
426 eformat::SubDetector subdet_rod_id)
431 {logstr << MSG::ERROR <<
"ROBDataProviderSvc not loaded. Can't read ByteStream" <<
endmsg;
432 return StatusCode::FAILURE;
439 return StatusCode::FAILURE;
442 const size_t MAX_ROBFRAGMENTS = 2048*1024;
444 size_t robcount =
re->children(robF,MAX_ROBFRAGMENTS);
446 if (robcount == MAX_ROBFRAGMENTS)
449 return StatusCode::FAILURE;
452 for (
size_t irob=0; irob<robcount; ++irob)
458 MSG::hex(logstr) <<
MSG::DEBUG <<
"Frag Size : " << rob.fragment_size_word() <<
endmsg;
459 MSG::hex(logstr) <<
MSG::DEBUG <<
"Header Size: " << rob.header_size_word() <<
endmsg;
460 MSG::hex(logstr) <<
MSG::DEBUG <<
"Source ID : " << rob.source_id() <<
endmsg;
468 logstr << MSG::ERROR <<
"Got invalid ROB fragment!" <<
endmsg;
469 return StatusCode::FAILURE;
473 MSG::hex(logstr) <<
MSG::DEBUG <<
"Frag Size : " << rob.rod_fragment_size_word() <<
endmsg;
474 MSG::hex(logstr) <<
MSG::DEBUG <<
"Header Size: " << rob.rod_header_size_word() <<
endmsg;
475 MSG::hex(logstr) <<
MSG::DEBUG <<
"Source ID : " << rob.rod_source_id() <<
endmsg;
476 MSG::hex(logstr) <<
MSG::DEBUG <<
"Run num : " << rob.rod_run_no() <<
endmsg;
477 MSG::hex(logstr) <<
MSG::DEBUG <<
"Version : " << rob.rod_version() <<
endmsg;
479 eformat::helper::SourceIdentifier rod_sid(rob.rod_source_id());
482 MSG::hex(logstr) <<
MSG::DEBUG <<
"Found requested ROD (id="
483 << rod_sid.subdetector_id() <<
")" <<
endmsg;
487 logstr<< MSG::INFO <<
"Guessing from ROD header -> " ;
488 if (rob.rod_detev_type()&1)
490 logstr<<
" H6 !" <<
endmsg;
496 logstr<<
" H8 !" <<
endmsg;
507 rob.rod_data(rodPointer);
509 for(
size_t i=0;
i<rob.rod_ndata(); ++
i)
517 logstr <<
MSG::FATAL <<
"Error reading bytestream event: "
518 <<
"Empty ROD block" <<
endmsg;
519 return StatusCode::FAILURE;
521 logstr <<
MSG::DEBUG <<
"Got Rod Block for Test Beam Instrumentation data from ByteStream" <<
endmsg;
522 return StatusCode::SUCCESS;
526 MSG::hex(logstr) << MSG::WARNING <<
" Didn't find SubDet ID for Beam Instruments. Should be "<< subdet_id<<
endmsg;
527 return StatusCode::FAILURE;
546 const EventContext& ctx = Gaudi::Hive::currentContext();
553 std::ostringstream
os;
557 if (
sc!=StatusCode::SUCCESS)
561 logstr << MSG::ERROR <<
"ReadRodBlock: RodBlock too small!" <<
endmsg;
562 return StatusCode::FAILURE;
577 std::string
name1,name2;
626 short m_feedthrough[8] = {0,0,0,0,1,1,1,1};
627 int m_sampleOrder[32] = {
628 0,1,2,3,4,5,6,7,8,9,10,
629 11,12,13,14,15,16,17,18,19,20,
630 21,22,23,24,25,26,27,28,29,30,
632 int m_sort[64] = { 55, 39, 23, 7, 119, 103, 87, 71,
633 54, 38, 22, 6, 118, 102, 86, 70,
634 53, 37, 21, 5, 117, 101, 85, 69,
635 52, 36, 20, 4, 116, 100, 84, 68,
636 51, 35, 19, 3, 115, 99, 83, 67,
637 50, 34, 18, 2, 114, 98, 82, 66,
638 49, 33, 17, 1, 113, 97, 81, 65,
639 48, 32, 16, 0, 112, 96, 80, 64
643 constexpr
short m_slot[8]={5,7,9,11,3,4,5,6};
645 constexpr
int NWREC = 8;
647 for(
unsigned int nfeb=0;nfeb<
m_boards.size();nfeb++){
666 if((m_sampleOrder[0]=
m_febgain[nfeb])!=0){
667 for(
int j=0;j<
m_febgain[nfeb];j++) {m_sampleOrder[j+1]=j;}
670 samp=m_sampleOrder[
s];
673 for(
int i= 0 ;
i<64;
i++){
677 unsigned short gain1 = ((tmp1>>12)&3) - 1;
681 unsigned short gain2 = ((
tmp2>>12)&3) - 1;
708 for(
int i=0;
i<128;
i++) {
718 const std::vector<HWIdentifier>& calibChannelIDs=calibLine->
calibSlotLine(hwid);
719 if (calibChannelIDs.size() != 0) {
749 name1=
"word1frag0x03chan";
750 name2=
"word2frag0x03chan";
754 logstr<<MSG::ERROR<<
"Beam counter subfrag (0x03) has not expected size" <<
endmsg;
755 sc=StatusCode::FAILURE;
764 if(
m_dump) logstr << MSG::INFO <<
" sub frag 0x03 : word1="<<tmp1<<
" word2="<<
tmp2 <<
endmsg;
774 if(
m_dump) logstr << MSG::INFO <<
" sub frag 0x03 : word1="<<tmp1<<
" word2="<<
tmp2 <<
endmsg;
784 if(
m_dump) logstr << MSG::INFO <<
" sub frag 0x03 : word1="<<tmp1<<
" word2="<<
tmp2 <<
endmsg;
795 name1=
"word1frag0x06chan";
796 name2=
"word2frag0x06chan";
797 std::string tcname=
"tc";
798 std::vector<TBScintillatorRaw *> theScints;
800 logstr<<MSG::ERROR<<
"Beam counter subfrag (0x04) has not expected size" <<
endmsg;
801 sc=StatusCode::FAILURE;
805 for(
unsigned int i=0;
i<24;
i++){
828 os << tcname << 2*
i+1;
830 theScints.push_back(
s1);
831 theScints.push_back(
s2);
841 name1=
"word1frag0x05chan";
842 name2=
"word2frag0x05chan";
845 logstr <<
MSG::DEBUG <<
"H6BuildObject : number of BPCRaw "<< bpcnum <<
endmsg;
847 for(
int i=0;
i<bpcnum;
i++){
848 std::string bpcname =
"BPC";
852 std::vector<const TBTDCRaw*> theTDCs;
853 std::vector<const TBADCRaw*> theADCs;
856 unsigned int tmp1,
tmp2;
863 os << name2 << (
i*3);
872 os << name2 << (
i*3+1);
880 MSG::hex(logstr) <<
MSG::DEBUG << bpcname<<
" "<< tmp1;
881 MSG::hex(logstr) <<
" "<<
tmp2<<
" ";
889 os << name2 << (
i*3+2);
894 MSG::hex(logstr) <<
MSG::DEBUG << bpcname<<
" "<< tmp1;
898 theTDCs.push_back(tbtdcL);
899 theTDCs.push_back(tbtdcR);
900 theTDCs.push_back(tbtdcU);
901 theTDCs.push_back(tbtdcD);
902 theADCs.push_back(tbadcH);
903 theADCs.push_back(tbadcV);
914 name1=
"word1frag0x06chan";
915 name2=
"word2frag0x06chan";
917 logstr<<MSG::ERROR<<
"Beam counter subfrag (0x06) has not expected size" <<
endmsg;
918 sc=StatusCode::FAILURE;
932 logstr <<
MSG::DEBUG <<
" building muon counters:";
933 for(
int i=0;
i<8;
i++) {
934 std::string scintname=
"muon";
936 os << scintname << (
i+1);
972 for(
int i=0;
i<4;
i++) {
979 os << name2 << (0+3+
i);
984 if(
testbit(10,word2)) adc2->setOverflow();
1031 for(
int i=0;
i<8;
i++) {
1035 logstr <<
MSG::DEBUG <<
" Should I record TBScintillatorRawCont with key " <<
m_keys[6] <<
" ? " <<
endmsg;
1044 logstr <<
MSG::DEBUG <<
" Found MWPC subfragment. Nword= "<<nword <<
endmsg;
1046 std::string mwpcnames[8]= {
"X2",
"Y2",
"X3",
"Y3",
"X4",
"Y4",
"X5",
"Y5"};
1047 bool isX[9]={
true,
false,
true,
false,
true,
false,
true,
false};
1049 for(
int i=0;
i<8;
i++){
1098 w = (
int) (word2>>12);
c = (
int) ((0xfff & word2) -
w/2 + !(
w&1));
1113 for(
int i=0;
i<8;
i++){
1128 logstr << MSG::ERROR <<
"bad number of lines. nwords=" << nword <<
endmsg;
1131 int nline= nword / 16;
1133 for(
int i=0;
i<nline;
i++){
1135 std::string sline=
"";
1136 std::string skey=
"";
1137 for(
int j=0;j<63;j++) {
1138 if(strw[
i*64+j]==0)
break;
1139 sline+=strw[
i*64+j];
1142 std::istringstream iss (sline);
1144 if(skey==
"RunNumber") {
1148 if(skey==
"BeamMomentum") {
1152 if(skey==
"BeamParticle") {
1160 if(skey==
"CryoAngle") {
1164 if(skey==
"TableY") {
1168 if(skey==
"miniROD") {
1173 while(iss.good()) {iss>>board;
m_boards.push_back(board);}
1178 if(skey==
"FebSamples") {
1188 if(skey==
"FebGains") {
1200 if(skey==
"FebAuto") {
1210 if(skey==
"FebFirstSample") {
1215 while(iss.good()) {iss>>firstsample;
m_firstsamples.push_back(firstsample);}
1247 logstr <<
MSG::DEBUG <<
" Found Calib_ID subfragment. Nword= "<<nword <<
endmsg;
1249 logstr<<MSG::ERROR<<
"Calibration subfrag (0xff) has not expected size" <<
endmsg;
1250 sc=StatusCode::FAILURE;
1256 for(
int i=0;
i<4;
i++){
1268 for(
int byte=0;
byte<16;
byte++) {
1269 logstr <<
MSG::DEBUG <<
" byte " <<
byte <<
" : " ;
1270 for(
int bit=0; bit<8; bit++) {
1322 logstr <<
MSG::DEBUG <<
"H8BuildObject invoked for code " << unrec_code<<
endmsg;
1325 bool gotobject=
false;
1326 bool recordfailure=
false;
1328 bool goodRodBlock=
true;
1337 if (
sc!=StatusCode::SUCCESS) {
1339 logstr << MSG::ERROR <<
"ReadRodBlock: RodBlock error! Will try to fill SG with empty objects." <<
endmsg;
1343 logstr << MSG::ERROR <<
"ReadRodBlock: RodBlock too small! Will try to fill SG with empty objects." <<
endmsg;
1405 short tdc_order[4] = {2,3,0,1};
1406 for(
int i=0;
i<BPCNum;
i++){
1408 for(
int j=0;j<4;j++) BPCtdc[
i][j]=0;
1417 std::ostringstream
os;
1430 name=
"frag0x10chan";
1495 logstr << MSG::INFO <<
" DUMP : ";
1497 logstr <<
" " << S3rightadc->
getADC()<<
" " << muTagadc->
getADC() <<
" " << C1adc->
getADC();
1506 name=
"frag0x11chan";
1530 logstr << MSG::INFO <<
" DUMP : ";
1538 name=
"frag0x12chan";
1554 logstr <<
MSG::DEBUG << MSG::dec <<
"Pos=" <<
pos <<
" Chan "<<
chan <<
"=" << tdc <<
", OV=" << overflow
1555 <<
", UN=" <<underthresh <<
" EOD="<< endofdata <<
", corrupt=" << corrup
1561 if (!endofdata && !corrup) {
1566 ClockPhasetdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1570 S1tdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1574 S2downtdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1578 S3lefttdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1582 S2uptdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1586 S3righttdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1590 S0tdc =
new TBTDCRaw(
os.str(),overflow,tdc,underthresh);
1603 logstr << MSG::ERROR <<
" Unexpected end-of-data word found at pos " <<
pos <<
endmsg;
1606 logstr<< MSG::ERROR <<
"Corrupted data in SubFragment 0x12 pos=" <<
pos <<
"channel=" <<
chan <<
" [0x" <<
1614 name=
"frag0x13chan";
1625 logstr <<
MSG::DEBUG << MSG::dec <<
"Pos=" <<
pos <<
" Chan "<<
chan <<
"=" << tdc
1626 <<
" EOD="<< endofdata <<
", corrupt=" << corrup
1628 if (!endofdata && !corrup) {
1633 const short bpc_ind =
chan / 4 ;
1635 logstr<<
MSG::DEBUG <<
"Error in 0x13 : found chan="<<
chan<<
" corresponding to bpc "<<bpc_ind<<
endmsg;
1638 const short signaltype = tdc_order[
chan % 4];
1639 if (BPCtdc[bpc_ind][signaltype]==NULL)
1640 BPCtdc[bpc_ind][signaltype] = tbtdc;
1647 logstr << MSG::ERROR <<
" Unexpected end-of-data word found at pos " <<
pos <<
endmsg;
1650 logstr<< MSG::ERROR <<
"Corrupted data in SubFragment 0x13 pos=" <<
pos <<
"channel=" <<
chan <<
" [0x" <<
1658 name=
"frag0x14chan";
1676 logstr <<
MSG::DEBUG << MSG::dec <<
"Pos=" <<
pos <<
" Chan "<<
chan <<
"=" << tdc
1677 <<
" EOD="<< endofdata <<
", corrupt=" << corrup
1680 if (!endofdata && !corrup) {
1685 const short bpc_ind =
chan / 4 + 3;
1686 if(bpc_ind>4) { logstr<<
MSG::DEBUG <<
"Error in 0x14 : found chan="<<
chan<<
" corresponding to bpc "<<bpc_ind<<
endmsg;
continue;}
1687 const short signaltype = tdc_order[
chan % 4];
1688 if (BPCtdc[bpc_ind][signaltype]==NULL)
1689 BPCtdc[bpc_ind][signaltype] = tbtdc;
1696 logstr << MSG::ERROR <<
" Unexpected end-of-data word found at pos " <<
pos <<
endmsg;
1699 logstr<< MSG::ERROR <<
"Corrupted data in SubFragment 0x14 pos=" <<
pos <<
"channel=" <<
chan <<
" [0x" <<
1706 logstr << MSG::INFO <<
"Found trigger word fragment" <<
endmsg;
1768 for(
int i=0;
i<BPCNum;
i++){
1769 std::vector<const TBTDCRaw*> listtdc;
1770 std::vector<const TBADCRaw*> listadc;
1771 for(
int j=0;j<4;j++){
1772 if(BPCtdc[
i][j]==0) listtdc.push_back(dummytdc);
1773 else listtdc.push_back(BPCtdc[
i][j]);
1775 listadc.push_back(dummyadc); listadc.push_back(dummyadc);
1787 if (
sc!=StatusCode::SUCCESS) {
1789 goodRodBlock =
false;
1790 logstr << MSG::ERROR <<
"Tile laser crate ROD block error! Nothing valid in SG." <<
endmsg;
1793 logstr << MSG::ERROR <<
"Tile laser crate ROD block too small! Nothing valid in SG" <<
endmsg;
1795 goodRodBlock =
false;
1828 for(
int i=0;
i<8;
i++){
1830 std::stringstream RawName;
1831 RawName <<
"frag0x1chan"<<
i;
1835 std::stringstream MuWallName;
1836 MuWallName <<
"MuWall"<<
i;
1849 for(
int i=0;
i<4;
i++){
1851 std::stringstream RawName;
1852 RawName <<
"frag0x1chan"<<(
i+8);
1856 std::stringstream MuWallName;
1857 MuWallName <<
"MuWall"<<
i+8;
1890 if (
sc.isFailure( ) ) {recordfailure=
true;
1893 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1898 if (
sc.isFailure( ) ) {recordfailure=
true;
1901 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1906 if (
sc.isFailure( ) ) {recordfailure=
true;
1909 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1913 if (
sc.isFailure( ) ) {recordfailure=
true;
1916 }
else {
sc=StatusCode::SUCCESS; gotobject=
true;}
1925 if (
sc.isFailure( ) ) {
1926 logstr << MSG::ERROR <<
"Cannot record TBTrigPat " <<
endmsg;
1929 sc=StatusCode::SUCCESS;
1935 if(!gotobject) {logstr<< MSG::ERROR<<
" Could not find object of type "<<unrec_code <<
endmsg;
sc=StatusCode::FAILURE;}
1936 if(recordfailure) {logstr<< MSG::ERROR<<
" One object could not be recorded "<<
endmsg;
sc=StatusCode::FAILURE;}
1947 logstr <<
MSG::DEBUG <<
"About to try recording. unrec_code = " << unrec_code <<
endmsg;
1950 logstr <<
MSG::DEBUG <<
" WARNING! Did not find TailCatcher. Returning an empty one : this may crash every attempt to use it" <<
endmsg;
1957 if (
sc.isFailure( ) ) {
1958 logstr << MSG::ERROR <<
"Cannot record TBTrigPat " <<
endmsg;
1965 if (
sc.isFailure( ) ) {
1966 logstr << MSG::ERROR <<
"Cannot record TailCatcherRaw " <<
endmsg;
1974 if (
sc.isFailure( ) ) {
1975 logstr << MSG::ERROR <<
"Cannot record BPCRawCont" <<
endmsg;
1986 if (
sc.isFailure( ) ) {
1987 logstr << MSG::ERROR <<
"Cannot record ScintRawCont " <<
endmsg;
1998 if (
sc.isFailure( ) ) {
1999 logstr << MSG::ERROR <<
"Cannot record MWPCRawCont " <<
endmsg;
2013 if (
sc.isFailure( ) ) {
2014 logstr << MSG::ERROR <<
"Cannot record TDCCont " <<
endmsg;
2026 if (
sc.isFailure( ) ) {
2034 for(
int c=10;
c<14;
c++){
2039 if (
sc.isFailure( ) ) {
2045 for(
int c=10;
c<14;
c++){
2050 if (
sc.isFailure( ) ) {
2058 if (
sc.isFailure( ) ) {