00001 #include "EventFilter/CSCRawToDigi/interface/CSCCLCTData.h"
00002 #include "EventFilter/CSCRawToDigi/interface/CSCTMBHeader.h"
00003 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include <iostream>
00006 #include <cstdio>
00007 #include <cstring>
00008
00009 bool CSCCLCTData::debug = false;
00010
00011
00012 CSCCLCTData::CSCCLCTData(const CSCTMBHeader * tmbHeader)
00013 : ncfebs_(tmbHeader->NCFEBs()), ntbins_(tmbHeader->NTBins())
00014 {
00015 size_ = nlines();
00016 zero();
00017 }
00018
00019
00020 CSCCLCTData::CSCCLCTData(int ncfebs, int ntbins)
00021 : ncfebs_(ncfebs), ntbins_(ntbins)
00022 {
00023 size_ = nlines();
00024 zero();
00025 }
00026
00027
00028
00029
00030 CSCCLCTData::CSCCLCTData(int ncfebs, int ntbins, const unsigned short * buf)
00031 : ncfebs_(ncfebs), ntbins_(ntbins)
00032 {
00033
00034
00035 size_ = (nlines()%2==1)? nlines()+2 : nlines();
00036
00037 memcpy(theData, buf, size_*2);
00038
00039 }
00040
00041
00042 void CSCCLCTData::zero()
00043 {
00044 for(int ifeb = 0; ifeb < ncfebs_; ++ifeb)
00045 {
00046 for(int tbin = 0; tbin < ntbins_; ++tbin)
00047 {
00048 for(int layer = 1; layer <= 6; ++layer)
00049 {
00050 dataWord(ifeb, tbin, layer) = CSCCLCTDataWord(ifeb, tbin, 0);
00051 }
00052 }
00053 }
00054
00055 }
00056
00057
00058 std::vector<CSCComparatorDigi> CSCCLCTData::comparatorDigis(uint32_t idlayer, unsigned cfeb)
00059 {
00060 static const bool doStripSwapping = true;
00061 bool me1a = (CSCDetId::station(idlayer)==1) && (CSCDetId::ring(idlayer)==4);
00062 bool zplus = (CSCDetId::endcap(idlayer) == 1);
00063 bool me1b = (CSCDetId::station(idlayer)==1) && (CSCDetId::ring(idlayer)==1);
00064 unsigned layer = CSCDetId::layer(idlayer);
00065
00066
00067 std::vector<CSCComparatorDigi> result;
00068 assert(layer>0 && layer<= 6);
00069
00070
00071
00072 for(int distrip = 0; distrip < 8; ++distrip)
00073 {
00074 uint16_t tbinbitsS0HS0=0;
00075 uint16_t tbinbitsS0HS1=0;
00076 uint16_t tbinbitsS1HS0=0;
00077 uint16_t tbinbitsS1HS1=0;
00078 for(int tbin = 0; tbin < ntbins_-2; ++tbin)
00079 {
00080 if(bitValue(cfeb, tbin, layer, distrip))
00081 {
00083 CSCCLCTDataWord word = dataWord(cfeb, tbin, layer);
00084 assert(word.tbin_ == tbin);
00085 assert(word.cfeb_ == cfeb);
00086
00087
00088 int bit2 = bitValue(cfeb, tbin+1, layer, distrip);
00089 int bit3 = bitValue(cfeb, tbin+2, layer, distrip);
00090
00091 int chamberDistrip = distrip + cfeb*8;
00092 int HalfStrip = 4*chamberDistrip + bit2*2 + bit3;
00093 int output = 4 + bit2*2 + bit3;
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 if (debug)
00105 LogTrace ("CSCCLCTData|CSCRawToDigi")
00106 << "fillComparatorOutputs: layer = "
00107 << layer << " timebin = " << tbin
00108 << " cfeb = " << cfeb << " distrip = " << chamberDistrip
00109 << " HalfStrip = " << HalfStrip
00110 << " Output " << output << std::endl;
00112
00114 if (output==4) tbinbitsS0HS0=tbinbitsS0HS0+(1<<tbin);
00115 if (output==5) tbinbitsS0HS1=tbinbitsS0HS1+(1<<tbin);
00116 if (output==6) tbinbitsS1HS0=tbinbitsS1HS0+(1<<tbin);
00117 if (output==7) tbinbitsS1HS1=tbinbitsS1HS1+(1<<tbin);
00118
00119 tbin += 2;
00120 }
00121 }
00122
00123
00124
00125
00126 if (tbinbitsS0HS0 || tbinbitsS0HS1 || tbinbitsS1HS0 || tbinbitsS1HS1) {
00127 unsigned int cfeb_corr = cfeb;
00128 unsigned int distrip_corr = distrip;
00129
00130 if (doStripSwapping) {
00131
00132
00133
00134
00135 if ( me1a && zplus ) {distrip_corr = 7-distrip;}
00136 if ( me1b && !zplus ) {distrip_corr = 7-distrip; cfeb_corr = 3-cfeb;}
00137 }
00138
00139 int strip = 16*cfeb_corr + 2*distrip_corr + 1;
00140
00141 if (debug)
00142 LogTrace ("CSCCLCTData|CSCRawToDigi")
00143 << "fillComparatorOutputs: cfeb_corr = " << cfeb_corr
00144 << " distrip_corr = " << distrip_corr << " strip = " << strip;
00145
00146 if (doStripSwapping && (( me1a && zplus ) || ( me1b && !zplus ))) {
00147
00148 if (tbinbitsS1HS1) result.push_back(CSCComparatorDigi(strip, 0, tbinbitsS1HS1));
00149 if (tbinbitsS1HS0) result.push_back(CSCComparatorDigi(strip, 1, tbinbitsS1HS0));
00150 if (tbinbitsS0HS1) result.push_back(CSCComparatorDigi(strip+1, 0, tbinbitsS0HS1));
00151 if (tbinbitsS0HS0) result.push_back(CSCComparatorDigi(strip+1, 1, tbinbitsS0HS0));
00152 }
00153 else {
00154 if (tbinbitsS0HS0) result.push_back(CSCComparatorDigi(strip, 0, tbinbitsS0HS0));
00155 if (tbinbitsS0HS1) result.push_back(CSCComparatorDigi(strip, 1, tbinbitsS0HS1));
00156 if (tbinbitsS1HS0) result.push_back(CSCComparatorDigi(strip+1, 0, tbinbitsS1HS0));
00157 if (tbinbitsS1HS1) result.push_back(CSCComparatorDigi(strip+1, 1, tbinbitsS1HS1));
00158 }
00159
00160 }
00161 }
00162 return result;
00163 }
00164
00165
00166
00167 std::vector<CSCComparatorDigi> CSCCLCTData::comparatorDigis(int layer)
00168 {
00169
00170 std::vector<CSCComparatorDigi> result;
00171 assert(layer>0 && layer<= 6);
00172
00173 for(int cfeb = 0; cfeb < ncfebs_; ++cfeb)
00174 {
00175 std::vector<CSCComparatorDigi> oneCfebDigi = comparatorDigis(layer,cfeb);
00176 result.insert(result.end(), oneCfebDigi.begin(), oneCfebDigi.end());
00177 }
00178
00179 return result;
00180 }
00181
00182
00183 void CSCCLCTData::add(const CSCComparatorDigi & digi, int layer)
00184 {
00185
00186 int strip = digi.getStrip();
00187 int halfStrip = (strip-1)*2 + digi.getComparator();
00188 int cfeb = (strip-1)/16;
00189 int distrip = ((strip-1)%16) / 2;
00190 assert(distrip < 8 && cfeb < 6 && halfStrip < 161);
00191
00192 std::vector<int> timeBinsOn = digi.getTimeBinsOn();
00193 for(std::vector<int>::const_iterator tbinItr = timeBinsOn.begin();
00194 tbinItr != timeBinsOn.end(); ++tbinItr)
00195 {
00196 int tbin = *tbinItr;
00197 if(tbin >= 0 && tbin < ntbins_-2) {
00198
00199 dataWord(cfeb, tbin, layer).set(distrip, true);
00200
00201 if (strip%2 == 0)
00202 dataWord(cfeb, tbin+1, layer).set(distrip, true);
00203
00204
00205 if (digi.getComparator())
00206 dataWord(cfeb, tbin+2, layer).set(distrip, true);
00207 }
00208 }
00209 }
00210
00211
00212 bool CSCCLCTData::check() const
00213 {
00214 bool result = true;
00215 for(int cfeb = 0; cfeb < ncfebs_; ++cfeb)
00216 {
00217 for(int tbin = 0; tbin < ntbins_; ++tbin)
00218 {
00219 for(int layer = 1; layer <= 6; ++layer)
00220 {
00222 const CSCCLCTDataWord & word = dataWord(cfeb, tbin, layer);
00223 bool wordIsGood = (word.tbin_ == tbin) && (word.cfeb_ == cfeb);
00224 result = result && wordIsGood;
00225 if(!wordIsGood && debug)
00226 {
00227 LogTrace("CSCCLCTData|CSCRawToDigi") << "Bad CLCT data in layer " << layer
00228 << " expect CFEB " << cfeb << " tbin " << tbin;
00229 LogTrace("CSCCLCTData|CSCRawToDigi") << " See " << word.cfeb_ << " "
00230 << word.tbin_;
00231 }
00232 }
00233 }
00234 }
00235 if(!result) LogTrace("CSCCLCTData|CSCRawToDigi") << "++ Bad CLCT Data ++ ";
00236 return result;
00237 }
00238
00239
00240 void CSCCLCTData::dump() const {
00241 for (int i=0;i<size_;i++) {
00242 printf("%04x %04x %04x %04x\n", theData[i+3], theData[i+2], theData[i+1], theData[i]);
00243 i+=3;
00244 }
00245 }
00246
00247
00248 void CSCCLCTData::selfTest()
00249 {
00250 CSCCLCTData clctData(5, 16);
00251
00252 CSCComparatorDigi comparatorDigi1(1, 0, 0x10);
00253
00254 CSCComparatorDigi comparatorDigi2(39, 1, 0x20);
00255
00256 CSCComparatorDigi comparatorDigi3(80, 1, 0x40);
00257
00258 clctData.add(comparatorDigi1,1);
00259 clctData.add(comparatorDigi2,4);
00260 clctData.add(comparatorDigi3,6);
00261
00262 CSCDetId layer1(1,4,1,2,1);
00263 CSCDetId layer4(1,4,1,2,4);
00264 CSCDetId layer6(1,4,1,2,6);
00265
00266 std::vector<CSCComparatorDigi> digis1 = clctData.comparatorDigis(1);
00267 std::vector<CSCComparatorDigi> digis2 = clctData.comparatorDigis(4);
00268 std::vector<CSCComparatorDigi> digis3 = clctData.comparatorDigis(6);
00269
00270 assert(digis1.size() == 1);
00271 assert(digis2.size() == 1);
00272 assert(digis3.size() == 1);
00273
00274 assert(digis1[0].getStrip() == 1);
00275 assert(digis1[0].getComparator() == 0);
00276 assert(digis1[0].getTimeBin() == 4);
00277
00278 assert(digis2[0].getStrip() == 39);
00279 assert(digis2[0].getComparator() == 1);
00280 assert(digis2[0].getTimeBin() == 5);
00281
00282 assert(digis3[0].getStrip() == 80);
00283 assert(digis3[0].getComparator() == 1);
00284 assert(digis3[0].getTimeBin() == 6);
00285 }
00286