00001
00009 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00010 #include "EventFilter/CSCRawToDigi/src/CSCDigiToRaw.h"
00011 #include "EventFilter/CSCRawToDigi/interface/CSCDCCEventData.h"
00012 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00013 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00014 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00015 #include "boost/dynamic_bitset.hpp"
00016 #include "boost/foreach.hpp"
00017 #include "EventFilter/CSCRawToDigi/src/bitset_append.h"
00018 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00019 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00020 #include "FWCore/Utilities/interface/CRC16.h"
00021 #include "CondFormats/CSCObjects/interface/CSCChamberMap.h"
00022 #include "FWCore/Utilities/interface/Exception.h"
00023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include <algorithm>
00026
00027 using namespace edm;
00028 using namespace std;
00029
00030 namespace cscd2r {
00032 CSCDetId chamberID(const CSCDetId & cscDetId) {
00033 CSCDetId chamberId = cscDetId.chamberId();
00034 if(chamberId.ring() ==4)
00035 {
00036 chamberId = CSCDetId(chamberId.endcap(), chamberId.station(), 1, chamberId.chamber(), 0);
00037 }
00038 return chamberId;
00039 }
00040
00041 template<typename LCTCollection>
00042 bool accept(const CSCDetId & cscId, const LCTCollection & lcts,
00043 int bxMin, int bxMax)
00044 {
00045 if(bxMin == -999) return true;
00046 int nominalBX = 6;
00047 CSCDetId chamberId = chamberID(cscId);
00048 typename LCTCollection::Range lctRange = lcts.get(chamberId);
00049 bool result = false;
00050 for(typename LCTCollection::const_iterator lctItr = lctRange.first;
00051 lctItr != lctRange.second; ++lctItr)
00052 {
00053 int bx = lctItr->getBX() - nominalBX;
00054 if(bx >= bxMin && bx <= bxMax)
00055 {
00056 result = true;
00057 break;
00058 }
00059 }
00060 return result;
00061 }
00062
00063
00064 template<>
00065 bool accept(const CSCDetId & cscId, const CSCCLCTPreTriggerCollection & lcts,
00066 int bxMin, int bxMax)
00067 {
00068 if(bxMin == -999) return true;
00069 int nominalBX = 6;
00070 CSCDetId chamberId = chamberID(cscId);
00071 CSCCLCTPreTriggerCollection::Range lctRange = lcts.get(chamberId);
00072 bool result = false;
00073 for(CSCCLCTPreTriggerCollection::const_iterator lctItr = lctRange.first;
00074 lctItr != lctRange.second; ++lctItr)
00075 {
00076 int bx = *lctItr - nominalBX;
00077 if(bx >= bxMin && bx <= bxMax)
00078 {
00079 result = true;
00080 break;
00081 }
00082 }
00083 return result;
00084 }
00085
00086 }
00087
00088
00089 CSCDigiToRaw::CSCDigiToRaw(const edm::ParameterSet & pset)
00090 : alctWindowMin_(pset.getParameter<int>("alctWindowMin")),
00091 alctWindowMax_(pset.getParameter<int>("alctWindowMax")),
00092 clctWindowMin_(pset.getParameter<int>("clctWindowMin")),
00093 clctWindowMax_(pset.getParameter<int>("clctWindowMax")),
00094 preTriggerWindowMin_(pset.getParameter<int>("preTriggerWindowMin")),
00095 preTriggerWindowMax_(pset.getParameter<int>("preTriggerWindowMax"))
00096 {}
00097
00098 void CSCDigiToRaw::beginEvent(const CSCChamberMap* electronicsMap)
00099 {
00100 theChamberDataMap.clear();
00101 theElectronicsMap = electronicsMap;
00102 }
00103
00104
00105 CSCEventData & CSCDigiToRaw::findEventData(const CSCDetId & cscDetId)
00106 {
00107 CSCDetId chamberId = cscd2r::chamberID(cscDetId);
00108
00109 map<CSCDetId, CSCEventData>::iterator chamberMapItr = theChamberDataMap.find(chamberId);
00110 if(chamberMapItr == theChamberDataMap.end())
00111 {
00112
00113 int chamberType = chamberId.iChamberType();
00114 chamberMapItr = theChamberDataMap.insert(pair<CSCDetId, CSCEventData>(chamberId, CSCEventData(chamberType))).first;
00115 }
00116 CSCEventData & cscData = chamberMapItr->second;
00117 cscData.dmbHeader()->setCrateAddress(theElectronicsMap->crate(cscDetId), theElectronicsMap->dmb(cscDetId));
00118 return cscData;
00119 }
00120
00121
00122
00123 void CSCDigiToRaw::add(const CSCStripDigiCollection& stripDigis,
00124 const CSCCLCTPreTriggerCollection & preTriggers)
00125 {
00126 for (CSCStripDigiCollection::DigiRangeIterator j=stripDigis.begin(); j!=stripDigis.end(); ++j)
00127 {
00128 CSCDetId cscDetId=(*j).first;
00129
00130 if(cscd2r::accept(cscDetId, preTriggers, preTriggerWindowMin_, preTriggerWindowMax_))
00131 {
00132 bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
00133 bool zplus = (cscDetId.endcap() == 1);
00134 bool me1b = (cscDetId.station()==1) && (cscDetId.ring()==1);
00135
00136 CSCEventData & cscData = findEventData(cscDetId);
00137
00138 std::vector<CSCStripDigi>::const_iterator digiItr = (*j).second.first;
00139 std::vector<CSCStripDigi>::const_iterator last = (*j).second.second;
00140 for( ; digiItr != last; ++digiItr)
00141 {
00142 CSCStripDigi digi = *digiItr;
00143 int strip = digi.getStrip();
00144 if ( me1a && zplus ) { digi.setStrip(17-strip); }
00145 if ( me1b && !zplus) { digi.setStrip(65-strip);}
00146 if ( me1a ) { strip = digi.getStrip(); digi.setStrip(strip+64);}
00147 cscData.add(digi, cscDetId.layer() );
00148 }
00149 }
00150 }
00151 }
00152
00153
00154 void CSCDigiToRaw::add(const CSCWireDigiCollection& wireDigis,
00155 const CSCALCTDigiCollection & alctDigis)
00156 {
00157 add(alctDigis);
00158 for (CSCWireDigiCollection::DigiRangeIterator j=wireDigis.begin(); j!=wireDigis.end(); ++j)
00159 {
00160 CSCDetId cscDetId=(*j).first;
00161 if(cscd2r::accept(cscDetId, alctDigis, alctWindowMin_, alctWindowMax_))
00162 {
00163 CSCEventData & cscData = findEventData(cscDetId);
00164 std::vector<CSCWireDigi>::const_iterator digiItr = (*j).second.first;
00165 std::vector<CSCWireDigi>::const_iterator last = (*j).second.second;
00166 for( ; digiItr != last; ++digiItr)
00167 {
00168 cscData.add(*digiItr, cscDetId.layer() );
00169 }
00170 }
00171 }
00172
00173 }
00174
00175 void CSCDigiToRaw::add(const CSCComparatorDigiCollection & comparatorDigis,
00176 const CSCCLCTDigiCollection & clctDigis)
00177 {
00178 add(clctDigis);
00179 for (CSCComparatorDigiCollection::DigiRangeIterator j=comparatorDigis.begin(); j!=comparatorDigis.end(); ++j)
00180 {
00181 CSCDetId cscDetId=(*j).first;
00182 CSCEventData & cscData = findEventData(cscDetId);
00183 if(cscd2r::accept(cscDetId, clctDigis, clctWindowMin_, clctWindowMax_))
00184 {
00185 bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
00186
00187 BOOST_FOREACH(CSCComparatorDigi digi, (*j).second)
00188 {
00189
00190
00191 if (me1a && digi.getStrip() <= 16) {
00192 CSCComparatorDigi digi_corr(64+digi.getStrip(),
00193 digi.getComparator(),
00194 digi.getTimeBinWord());
00195 cscData.add(digi_corr, cscDetId.layer());
00196 }
00197 else {
00198 cscData.add(digi, cscDetId.layer());
00199 }
00200 }
00201 }
00202 }
00203 }
00204
00205 void CSCDigiToRaw::add(const CSCALCTDigiCollection & alctDigis)
00206 {
00207 for (CSCALCTDigiCollection::DigiRangeIterator j=alctDigis.begin(); j!=alctDigis.end(); ++j)
00208 {
00209 CSCDetId cscDetId=(*j).first;
00210 CSCEventData & cscData = findEventData(cscDetId);
00211
00212 cscData.add(std::vector<CSCALCTDigi>((*j).second.first, (*j).second.second));
00213 }
00214 }
00215
00216 void CSCDigiToRaw::add(const CSCCLCTDigiCollection & clctDigis)
00217 {
00218 for (CSCCLCTDigiCollection::DigiRangeIterator j=clctDigis.begin(); j!=clctDigis.end(); ++j)
00219 {
00220 CSCDetId cscDetId=(*j).first;
00221 CSCEventData & cscData = findEventData(cscDetId);
00222
00223 cscData.add(std::vector<CSCCLCTDigi>((*j).second.first, (*j).second.second));
00224 }
00225 }
00226
00227 void CSCDigiToRaw::add(const CSCCorrelatedLCTDigiCollection & corrLCTDigis)
00228 {
00229 for (CSCCorrelatedLCTDigiCollection::DigiRangeIterator j=corrLCTDigis.begin(); j!=corrLCTDigis.end(); ++j)
00230 {
00231 CSCDetId cscDetId=(*j).first;
00232 CSCEventData & cscData = findEventData(cscDetId);
00233
00234 cscData.add(std::vector<CSCCorrelatedLCTDigi>((*j).second.first, (*j).second.second));
00235 }
00236
00237 }
00238
00239
00240 void CSCDigiToRaw::createFedBuffers(const CSCStripDigiCollection& stripDigis,
00241 const CSCWireDigiCollection& wireDigis,
00242 const CSCComparatorDigiCollection& comparatorDigis,
00243 const CSCALCTDigiCollection& alctDigis,
00244 const CSCCLCTDigiCollection& clctDigis,
00245 const CSCCLCTPreTriggerCollection & preTriggers,
00246 const CSCCorrelatedLCTDigiCollection& correlatedLCTDigis,
00247 FEDRawDataCollection& fed_buffers,
00248 const CSCChamberMap* mapping,
00249 Event & e)
00250 {
00251
00252
00253
00254
00255
00256 beginEvent(mapping);
00257 add(stripDigis, preTriggers);
00258 add(wireDigis, alctDigis);
00259 add(comparatorDigis, clctDigis);
00260 add(correlatedLCTDigis);
00261
00262 int l1a=e.id().event();
00263 int bx = 0;
00264
00265
00266 std::map<int, CSCDCCEventData> dccMap;
00267 for (int idcc=FEDNumbering::MINCSCFEDID;
00268 idcc<=FEDNumbering::MAXCSCFEDID;++idcc)
00269 {
00270
00271
00272
00273
00274
00275
00276 int nDDUs = 5;
00277 dccMap.insert(std::pair<int, CSCDCCEventData>(idcc, CSCDCCEventData(idcc, nDDUs, bx, l1a) ) );
00278
00279
00280 for(map<CSCDetId, CSCEventData>::iterator chamberItr = theChamberDataMap.begin();
00281 chamberItr != theChamberDataMap.end(); ++chamberItr)
00282 {
00283
00284 int indexDCC = mapping->slink(chamberItr->first);
00285 if(indexDCC == idcc)
00286 {
00287
00288 std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.find(indexDCC);
00289 if(dccMapItr == dccMap.end())
00290 {
00291 throw cms::Exception("CSCDigiToRaw") << "Bad DCC number:" << indexDCC;
00292 }
00293
00294
00295 int dduId = mapping->ddu(chamberItr->first);
00296 int dduSlot = mapping->dduSlot(chamberItr->first);
00297 int dduInput = mapping->dduInput(chamberItr->first);
00298 int dmbId = mapping->dmb(chamberItr->first);
00299 dccMapItr->second.addChamber(chamberItr->second, dduId, dduSlot, dduInput, dmbId);
00300 }
00301 }
00302 }
00303
00304
00305 for(std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.begin();
00306 dccMapItr != dccMap.end(); ++dccMapItr)
00307 {
00308 boost::dynamic_bitset<> dccBits = dccMapItr->second.pack();
00309 FEDRawData & fedRawData = fed_buffers.FEDData(dccMapItr->first);
00310 fedRawData.resize(dccBits.size());
00311
00312 bitset_utilities::bitsetToChar(dccBits, fedRawData.data());
00313 FEDTrailer cscFEDTrailer(fedRawData.data()+(fedRawData.size()-8));
00314 cscFEDTrailer.set(fedRawData.data()+(fedRawData.size()-8),
00315 fedRawData.size()/8,
00316 evf::compute_crc(fedRawData.data(),fedRawData.size()), 0, 0);
00317 }
00318 }
00319
00320
00321