00001 #include "EventFilter/CSCTFRawToDigi/src/CSCSPEvent.h"
00002 #include <map>
00003 #include <list>
00004
00005 bool CSCSPEvent::unpack(const unsigned short *&buf) throw() {
00006 bool unpackError = false;
00007
00008 if( (buf[0]&0xF000) != 0x9000 || (buf[1]&0xF000) != 0x9000 || (buf[2]&0xF000) != 0x9000 || (buf[3]&0xF000) != 0x9000 ||
00009 (buf[4]&0xF000) != 0xA000 || (buf[5]&0xF000) != 0xA000 || (buf[6]&0xF000) != 0xA000 || (buf[7]&0xF000) != 0xA000 )
00010 return true;
00011 else
00012 unpackError |= header_.unpack(buf);
00013
00014 if( !header_.empty() ){
00015
00016 if( header_.format_version() )
00017 unpackError |= counters_.unpack(buf);
00018
00019 for(unsigned short tbin=0; tbin<header_.nTBINs(); tbin++)
00020 unpackError |= record_[tbin].unpack(buf,header_.active(),header_.suppression(),tbin);
00021
00022
00023 for(unsigned short tbin=0; tbin<header_.nTBINs(); tbin++){
00024 for(unsigned short trk=0; trk<3; trk++){
00025 CSCSP_SPblock &track = record_[tbin].sp[trk];
00026 if( track.ME1_id()==0 && track.ME2_id()==0 && track.ME3_id()==0 && track.ME4_id()==0 && track.MB_id()==0 ) continue;
00027
00028 std::map< int, std::list<int> > timeline;
00029 if( track.ME1_id() ) timeline[track.ME1_tbin()].push_back(1);
00030 if( track.ME2_id() ) timeline[track.ME2_tbin()].push_back(2);
00031 if( track.ME3_id() ) timeline[track.ME3_tbin()].push_back(3);
00032 if( track.ME4_id() ) timeline[track.ME4_tbin()].push_back(4);
00033 int earliest_tbin=0, second_earliest_tbin=0;
00034 for(int bx=7; bx>=0; bx--){
00035 std::list<int>::const_iterator iter = timeline[bx].begin();
00036 while( iter != timeline[bx].end() ){
00037 if( earliest_tbin==0 ) earliest_tbin=bx;
00038 else if( second_earliest_tbin==0 ) second_earliest_tbin=bx;
00039 iter++;
00040 }
00041 }
00042
00043
00044 if( track.ME1_id() ){
00045 unsigned int mpc = ( track.ME1_id()>3 ? 1 : 0 );
00046 int ME1_tbin = tbin - track.ME1_tbin() + second_earliest_tbin;
00047 if( ME1_tbin<0 || ME1_tbin>7 ) unpackError |= true;
00048 else {
00049 std::vector<CSCSP_MEblock> lcts = record_[ME1_tbin].LCTs(mpc);
00050 for(std::vector<CSCSP_MEblock>::const_iterator lct=lcts.begin(); lct!=lcts.end(); lct++)
00051
00052
00053 if( ((lct->spInput()-1)%3+1)==(mpc?track.ME1_id()-3:track.ME1_id()) ){
00054 track.lct_[0] = *lct;
00055 track.lctFilled[0] = true;
00056 }
00057 }
00058 }
00059 if( track.ME2_id() ){
00060 int ME2_tbin = tbin - track.ME2_tbin() + second_earliest_tbin;
00061 if( ME2_tbin<0 || ME2_tbin>7 ) unpackError |= true;
00062 else {
00063 std::vector<CSCSP_MEblock> lcts = record_[ME2_tbin].LCTs(2);
00064 for(std::vector<CSCSP_MEblock>::const_iterator lct=lcts.begin(); lct!=lcts.end(); lct++)
00065
00066
00067 if( ((lct->spInput()-1)%3+1)==track.ME2_id() ){
00068 track.lct_[1] = *lct;
00069 track.lctFilled[1] = true;
00070 }
00071 }
00072 }
00073 if( track.ME3_id() ){
00074 int ME3_tbin = tbin - track.ME3_tbin() + second_earliest_tbin;
00075 if( ME3_tbin<0 || ME3_tbin>7 ) unpackError |= true;
00076 else {
00077 std::vector<CSCSP_MEblock> lcts = record_[ME3_tbin].LCTs(3);
00078 for(std::vector<CSCSP_MEblock>::const_iterator lct=lcts.begin(); lct!=lcts.end(); lct++)
00079
00080
00081 if( ((lct->spInput()-1)%3+1)==track.ME3_id() ){
00082 track.lct_[2] = *lct;
00083 track.lctFilled[2] = true;
00084 }
00085 }
00086 }
00087 if( track.ME4_id() ){
00088 int ME4_tbin = tbin - track.ME4_tbin() + second_earliest_tbin;
00089 if( ME4_tbin<0 || ME4_tbin>7 ) unpackError |= true;
00090 else {
00091 std::vector<CSCSP_MEblock> lcts = record_[ME4_tbin].LCTs(4);
00092 for(std::vector<CSCSP_MEblock>::const_iterator lct=lcts.begin(); lct!=lcts.end(); lct++)
00093
00094
00095 if( ((lct->spInput()-1)%3+1)==track.ME4_id() ){
00096 track.lct_[3] = *lct;
00097 track.lctFilled[3] = true;
00098 }
00099 }
00100 }
00101 if( track.MB_id() ){
00102 if( (tbin==0 && track.MB_tbin()) || (tbin==6 && track.MB_id()%2==0) ) unpackError |= true;
00103 else {
00104 std::vector<CSCSP_MBblock> stubs = ( track.MB_id()%2==0 ? record_[tbin+1].mbStubs() : record_[tbin].mbStubs() );
00105 for(std::vector<CSCSP_MBblock>::const_iterator stub=stubs.begin(); stub!=stubs.end(); stub++)
00106 if( (stub->id()==1 && track.MB_id()<=2) || (stub->id()==2 && track.MB_id()>2) ){
00107 track.dt_ = *stub;
00108 track.dtFilled = true;
00109 }
00110 if( !track.dtFilled ) unpackError |= true;
00111 }
00112 }
00113
00114 }
00115 }
00116 }
00117
00118 unpackError |= trailer_.unpack(buf);
00119
00120 return unpackError;
00121 }
00122