CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/EventFilter/CSCRawToDigi/src/CSCTMBHeader.cc

Go to the documentation of this file.
00001 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader.h"
00002 #include "EventFilter/CSCRawToDigi/interface/CSCDMBHeader.h"
00003 #include "EventFilter/CSCRawToDigi/src/cscPackerCompare.h"
00004 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2006.h"
00005 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2007.h"
00006 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader2007_rev0x50c3.h"
00007 #include "DataFormats/CSCDigi/interface/CSCCLCTDigi.h"
00008 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigi.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include <math.h>
00011 #include <string.h> // memcpy
00012 
00013 bool CSCTMBHeader::debug = false;
00014 
00015 CSCTMBHeader::CSCTMBHeader(int firmwareVersion, int firmwareRevision):
00016  theHeaderFormat(),
00017  theFirmwareVersion(firmwareVersion)
00018 {
00019   if(firmwareVersion == 2006)
00020   {
00021     theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2006());
00022   }
00023   else if(firmwareVersion == 2007)
00024   {
00025     if(firmwareRevision >= 0x50c3) 
00026     {
00027       theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007_rev0x50c3());
00028     }
00029     else
00030     {
00031       theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007());
00032     }
00033   }
00034   else
00035   {
00036     edm::LogError("CSCTMBHeader|CSCRawToDigi") <<"failed to determine TMB firmware version!!";
00037   }
00038 }
00039 
00040 //CSCTMBHeader::CSCTMBHeader(const CSCTMBStatusDigi & digi) {
00041 //  CSCTMBHeader(digi.header());
00042 //}
00043 
00044 CSCTMBHeader::CSCTMBHeader(const unsigned short * buf)
00045 : theHeaderFormat()
00046 {
00048   if (buf[0]==0xDB0C) {
00049     theFirmwareVersion=2007;
00050     theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007(buf));
00051     if(theHeaderFormat->firmwareRevision() >= 0x50c3)
00052       {
00053         theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007_rev0x50c3(buf));
00054       }
00055   }
00056   else if (buf[0]==0x6B0C) {
00057     theFirmwareVersion=2006;
00058     theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2006(buf));
00059   }
00060   else {
00061     edm::LogError("CSCTMBHeader|CSCRawToDigi") <<"failed to determine TMB firmware version!!";
00062   }
00063 }
00064 
00065 /*
00066 void CSCTMBHeader::swapCLCTs(CSCCLCTDigi& digi1, CSCCLCTDigi& digi2)
00067 {
00068   bool me11 = (theChamberId.station() == 1 && 
00069                (theChamberId.ring() == 1 || theChamberId.ring() == 4));
00070   if (!me11) return;
00071 
00072   int cfeb1 = digi1.getCFEB();
00073   int cfeb2 = digi2.getCFEB();
00074   if (cfeb1 != cfeb2) return;
00075 
00076   bool me1a = (cfeb1 == 4);
00077   bool me1b = (cfeb1 != 4);
00078   bool zplus = (theChamberId.endcap() == 1);
00079 
00080   if ( (me1a && zplus) || (me1b && !zplus)) {
00081     // Swap CLCTs if they have the same quality and pattern # (priority
00082     // has to be given to the lower key).
00083     if (digi1.getQuality() == digi2.getQuality() &&
00084         digi1.getPattern() == digi2.getPattern()) {
00085       CSCCLCTDigi temp = digi1;
00086       digi1 = digi2;
00087       digi2 = temp;
00088 
00089       // Also re-number them.
00090       digi1.setTrknmb(1);
00091       digi2.setTrknmb(2);
00092     }
00093   }
00094 }
00095 */
00096 
00097 
00098 //FIXME Pick which LCT goes first
00099 void CSCTMBHeader::add(const std::vector<CSCCLCTDigi> & digis)
00100 {
00101   // sort???
00102   if(digis.size() > 0) addCLCT0(digis[0]);
00103   if(digis.size() > 1) addCLCT1(digis[1]);
00104 }
00105 
00106 void CSCTMBHeader::add(const std::vector<CSCCorrelatedLCTDigi> & digis)
00107 {
00108   // sort???
00109   if(digis.size() > 0) addCorrelatedLCT0(digis[0]);
00110   if(digis.size() > 1) addCorrelatedLCT1(digis[1]);
00111 }
00112 
00113 
00114 CSCTMBHeader2007 CSCTMBHeader::tmbHeader2007()   const {
00115   CSCTMBHeader2007 * result = dynamic_cast<CSCTMBHeader2007 *>(theHeaderFormat.get());
00116   if(result == 0) 
00117   {
00118     throw cms::Exception("Could not get 2007 TMB header format");
00119   }
00120   return *result;
00121 }
00122 
00123 
00124 CSCTMBHeader2006 CSCTMBHeader::tmbHeader2006()   const {
00125   CSCTMBHeader2006 * result = dynamic_cast<CSCTMBHeader2006 *>(theHeaderFormat.get());
00126   if(result == 0)
00127   {
00128     throw cms::Exception("Could not get 2006 TMB header format");
00129   }
00130   return *result;
00131 }
00132 
00133 
00134 void CSCTMBHeader::selfTest()
00135 {
00136   static bool debug = false;
00137 
00138   // tests packing and unpacking
00139   for(int station = 1; station <= 4; ++station) {
00140     for(int iendcap = 1; iendcap <= 2; ++iendcap) {
00141       CSCDetId detId(iendcap, station, 1, 1, 0);
00142 
00143       // the next-to-last is the BX, which only gets
00144       // saved in two bits and must be the same for clct0 and clct1.
00145       //CSCCLCTDigi clct0(1, 1, 4, 0, 0, 30, 3, 0, 1); // valid for 2006
00146       // In 2007 firmware, there are no distrips, so the 4th argument (strip
00147       // type) should always be set to 1 (halfstrips).
00148       CSCCLCTDigi clct0(1, 1, 4, 1, 0, 30, 4, 2, 1); // valid for 2007
00149       CSCCLCTDigi clct1(1, 1, 2, 1, 1, 31, 1, 2, 2);
00150 
00151       // BX of LCT (8th argument) is 1-bit word (the least-significant bit
00152       // of ALCT's bx).
00153       CSCCorrelatedLCTDigi lct0(1, 1, 2, 10, 98, 5, 0, 1, 0, 0, 0, 0);
00154       CSCCorrelatedLCTDigi lct1(2, 1, 2, 20, 15, 9, 1, 0, 0, 0, 0, 0);
00155 
00156       CSCTMBHeader tmbHeader(2007, 0x50c3);
00157       tmbHeader.addCLCT0(clct0);
00158       tmbHeader.addCLCT1(clct1);
00159       tmbHeader.addCorrelatedLCT0(lct0);
00160       tmbHeader.addCorrelatedLCT1(lct1);
00161       std::vector<CSCCLCTDigi> clcts = tmbHeader.CLCTDigis(detId.rawId());
00162       // guess they got reordered
00163       assert(cscPackerCompare(clcts[0],clct0));
00164       assert(cscPackerCompare(clcts[1],clct1));
00165       if (debug) {
00166         std::cout << "Match for: " << clct0 << "\n";
00167         std::cout << "           " << clct1 << "\n \n";
00168       }
00169 
00170       std::vector<CSCCorrelatedLCTDigi> lcts = tmbHeader.CorrelatedLCTDigis(detId.rawId());
00171       assert(cscPackerCompare(lcts[0], lct0));
00172       assert(cscPackerCompare(lcts[1], lct1));
00173       if (debug) {
00174         std::cout << "Match for: " << lct0 << "\n";
00175         std::cout << "           " << lct1 << "\n";
00176       }
00177 
00178       // try packing and re-packing, to make sure they're the same
00179       unsigned short int * data = tmbHeader.data();
00180       CSCTMBHeader newHeader(data);
00181       clcts = newHeader.CLCTDigis(detId.rawId());
00182       assert(cscPackerCompare(clcts[0],clct0));
00183       assert(cscPackerCompare(clcts[1],clct1));
00184       lcts = newHeader.CorrelatedLCTDigis(detId.rawId());
00185       assert(cscPackerCompare(lcts[0], lct0));
00186       assert(cscPackerCompare(lcts[1], lct1));
00187 
00188 
00189     }
00190   }
00191 }
00192 
00193 
00194 std::ostream & operator<<(std::ostream & os, const CSCTMBHeader & hdr) {
00195   hdr.theHeaderFormat->print(os);
00196   return os;
00197 }