Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:45:14

0001 #include "EventFilter/CSCTFRawToDigi/src/CSCSPEvent.h"
0002 #include <map>
0003 #include <list>
0004 #include <iostream>
0005 
0006 bool CSCSPEvent::unpack(const unsigned short *&buf) throw() {
0007   bool unpackError = false;
0008 
0009   if ((buf[0] & 0xF000) != 0x9000 || (buf[1] & 0xF000) != 0x9000 || (buf[2] & 0xF000) != 0x9000 ||
0010       (buf[3] & 0xF000) != 0x9000 || (buf[4] & 0xF000) != 0xA000 || (buf[5] & 0xF000) != 0xA000 ||
0011       (buf[6] & 0xF000) != 0xA000 || (buf[7] & 0xF000) != 0xA000)
0012     return true;
0013   else
0014     unpackError |= header_.unpack(buf);
0015 
0016   if (!header_.empty()) {
0017     // Block of Counters is added in format version 4.3 (dated by 05/27/2007)
0018     if (header_.format_version())
0019       unpackError |= counters_.unpack(buf);
0020 
0021     if (header_.format_version() < 3 || !header_.suppression()) {
0022       for (unsigned short tbin = 0; tbin < header_.nTBINs(); tbin++)
0023         unpackError |= record_[tbin].unpack(buf, header_.active(), header_.suppression(), tbin);
0024     } else {
0025       // For the v.5.3 zero supression tbin has to be identified from the Data Block Header (BH2d word), as opposed to plain counting when empty records are not suppressed
0026       for (unsigned short tbin = 0, actual_tbin = (buf[7] >> 8) & 0x7; tbin < header_.nTBINs(); tbin++) {
0027         bzero(&(record_[tbin]), sizeof(record_[tbin]));
0028         // Check if we ran into the trailer (happens once all the records were read out)
0029         if ((buf[0] & 0xF000) == 0xF000 && (buf[1] & 0xF000) == 0xF000 && (buf[2] & 0xF000) == 0xF000 &&
0030             (buf[3] & 0xF000) == 0xF000 && (buf[4] & 0xF000) == 0xE000 && (buf[5] & 0xF000) == 0xE000 &&
0031             (buf[6] & 0xF000) == 0xE000 && (buf[7] & 0xF000) == 0xE000)
0032           break;
0033         // Skip supressed empty tbins in the format version >=5.3
0034         if (tbin + 1 != actual_tbin)
0035           continue;
0036         // Unpack the record
0037         unpackError |= record_[tbin].unpack(buf, header_.active(), header_.suppression(), tbin);
0038         actual_tbin = (buf[7] >> 8) & 0x7;
0039       }
0040     }
0041 
0042     // Link initial LCTs to the tracks in each time bin
0043     for (unsigned short tbin = 0; tbin < header_.nTBINs(); tbin++) {
0044       for (unsigned short trk = 0; trk < 3; trk++) {
0045         CSCSP_SPblock &track = record_[tbin].sp[trk];
0046         if (track.ME1_id() == 0 && track.ME2_id() == 0 && track.ME3_id() == 0 && track.ME4_id() == 0 &&
0047             track.MB_id() == 0)
0048           continue;
0049         // The key LCT identified by the BXA algorithm is the second earliest LCT
0050         int second_earliest_lct_delay =
0051             -1;  // this is going to be a # tbins the key LCT was delayed to allign with the latest LCT
0052         if (track.mode() != 15 && track.mode() != 11) {  // BXA works only on non halo tracks and non-singles
0053           // BXA algorithm is not trivial: first let's order all the delays (MEx_tbin), which are aligning LCTs to the tbin of the latest LCT
0054           std::map<int, std::list<int> > timeline;
0055           if (track.ME1_id())
0056             timeline[track.ME1_tbin()].push_back(1);
0057           if (track.ME2_id())
0058             timeline[track.ME2_tbin()].push_back(2);
0059           if (track.ME3_id())
0060             timeline[track.ME3_tbin()].push_back(3);
0061           if (track.ME4_id())
0062             timeline[track.ME4_tbin()].push_back(4);
0063           if (track.MB_id())
0064             timeline[track.MB_tbin()].push_back(5);
0065           int earliest_lct_delay = -1;  //, second_earliest_lct_delay = -1;
0066           // Going from largest to smallest delay (earliest LCT pops up first in the loop)
0067           for (int delay = 7; delay >= 0 && second_earliest_lct_delay == -1; delay--) {
0068             std::list<int>::const_iterator iter = timeline[delay].begin();
0069             while (iter != timeline[delay].end() && second_earliest_lct_delay == -1) {
0070               if (earliest_lct_delay == -1)
0071                 earliest_lct_delay = delay;
0072               else if (second_earliest_lct_delay == -1)
0073                 second_earliest_lct_delay = delay;
0074               iter++;
0075             }
0076           }
0077         } else
0078           second_earliest_lct_delay = 0;
0079 
0080         // MEx_tbin are LCTs delays shifting all of them to the bx of last LCT used to build a track
0081         //  let's convert delays to TBINs keeping in mind that absolute_lct_tbin = track_tbin + (second_earliest_delay - lct_delay)
0082         if (track.ME1_id()) {  // if track contains LCT from the ME1
0083           unsigned int mpc = (track.ME1_id() > 3 ? 1 : 0);
0084           int ME1_tbin = tbin + second_earliest_lct_delay - track.ME1_tbin();
0085           ///                                     if( track.ME1_tbin()>2 ) unpackError |= true; // because bxaDepth<=2
0086           if (ME1_tbin >= 0 && ME1_tbin < 7) {
0087             std::vector<CSCSP_MEblock> lcts = record_[ME1_tbin].LCTs(mpc);
0088             for (std::vector<CSCSP_MEblock>::const_iterator lct = lcts.begin(); lct != lcts.end(); lct++)
0089               // Due to old MPC firmware link information was not accessible for some data:
0090               //if( lct->link()==(mpc?track.ME1_id()-3:track.ME1_id()) ){
0091               if (((lct->spInput() - 1) % 3 + 1) == (mpc ? track.ME1_id() - 3 : track.ME1_id())) {
0092                 track.lct_[0] = *lct;
0093                 track.lctFilled[0] = true;
0094               }
0095           }
0096         }
0097         if (track.ME2_id()) {  // ... ME2
0098           int ME2_tbin = tbin + second_earliest_lct_delay - track.ME2_tbin();
0099           ///                                     if( track.ME2_tbin()>2 ) unpackError |= true; // because bxaDepth<=2
0100           if (ME2_tbin >= 0 && ME2_tbin < 7) {
0101             std::vector<CSCSP_MEblock> lcts = record_[ME2_tbin].LCTs(2);
0102             for (std::vector<CSCSP_MEblock>::const_iterator lct = lcts.begin(); lct != lcts.end(); lct++)
0103               // Due to old MPC firmware link information was not accessible for some data:
0104               //if( lct->link()==track.ME2_id() ){
0105               if (((lct->spInput() - 1) % 3 + 1) == track.ME2_id()) {
0106                 track.lct_[1] = *lct;
0107                 track.lctFilled[1] = true;
0108               }
0109           }
0110         }
0111         if (track.ME3_id()) {  // ... ME3
0112           int ME3_tbin = tbin + second_earliest_lct_delay - track.ME3_tbin();
0113           ///                                     if( track.ME3_tbin()>2 ) unpackError |= true; // because bxaDepth<=2
0114           if (ME3_tbin >= 0 && ME3_tbin < 7) {
0115             std::vector<CSCSP_MEblock> lcts = record_[ME3_tbin].LCTs(3);
0116             for (std::vector<CSCSP_MEblock>::const_iterator lct = lcts.begin(); lct != lcts.end(); lct++)
0117               // Due to old MPC firmware link information was not accessible for some data:
0118               //if( lct->link()==track.ME3_id() ){
0119               if (((lct->spInput() - 1) % 3 + 1) == track.ME3_id()) {
0120                 track.lct_[2] = *lct;
0121                 track.lctFilled[2] = true;
0122               }
0123           }
0124         }
0125         if (track.ME4_id()) {  // ... fourth station
0126           int ME4_tbin = tbin + second_earliest_lct_delay - track.ME4_tbin();
0127           ///                                     if( track.ME4_tbin()>2 ) unpackError |= true; // because bxaDepth<=2
0128           if (ME4_tbin >= 0 && ME4_tbin < 7) {
0129             std::vector<CSCSP_MEblock> lcts = record_[ME4_tbin].LCTs(4);
0130             for (std::vector<CSCSP_MEblock>::const_iterator lct = lcts.begin(); lct != lcts.end(); lct++)
0131               // Due to old MPC firmware link information was not accessible for some data:
0132               //if( lct->link()==track.ME4_id() ){
0133               if (((lct->spInput() - 1) % 3 + 1) == track.ME4_id()) {
0134                 track.lct_[3] = *lct;
0135                 track.lctFilled[3] = true;
0136               }
0137           }
0138         }
0139         if (track.MB_id()) {  // ... barrel
0140           int MB_tbin = tbin + second_earliest_lct_delay - track.MB_tbin();
0141           ///                                     if( track.MB_tbin()>2 ) unpackError |= true; // because bxaDepth<=2
0142           if (MB_tbin >= 0 && MB_tbin < 7) {
0143             std::vector<CSCSP_MBblock> stubs = record_[MB_tbin].mbStubs();
0144             for (std::vector<CSCSP_MBblock>::const_iterator stub = stubs.begin(); stub != stubs.end(); stub++)
0145               if ((stub->id() == 1 && track.MB_id() % 2 == 1) || (stub->id() == 2 && track.MB_id() % 2 == 0)) {
0146                 track.dt_ = *stub;
0147                 track.dtFilled = true;
0148               }
0149           }
0150         }
0151       }
0152     }
0153   }
0154   unpackError |= trailer_.unpack(buf);
0155 
0156   return unpackError;
0157 }