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 CSCDigiToRaw::CSCDigiToRaw(const edm::ParameterSet & pset)
00031 : requirePreTrigger_(pset.getParameter<bool>("requirePreTrigger")),
00032 requireCLCTForComparators_(pset.getParameter<bool>("requireCLCTForComparators"))
00033 {}
00034
00035 void CSCDigiToRaw::beginEvent(const CSCChamberMap* electronicsMap)
00036 {
00037 theChamberDataMap.clear();
00038 theElectronicsMap = electronicsMap;
00039 }
00040
00041
00042 CSCEventData & CSCDigiToRaw::findEventData(const CSCDetId & cscDetId)
00043 {
00044 CSCDetId chamberId = chamberID(cscDetId);
00045
00046
00047
00048 map<CSCDetId, CSCEventData>::iterator chamberMapItr = theChamberDataMap.find(chamberId);
00049 if(chamberMapItr == theChamberDataMap.end())
00050 {
00051
00052 int chamberType = chamberId.iChamberType();
00053 chamberMapItr = theChamberDataMap.insert(pair<CSCDetId, CSCEventData>(chamberId, CSCEventData(chamberType))).first;
00054 }
00055 CSCEventData & cscData = chamberMapItr->second;
00056 cscData.dmbHeader()->setCrateAddress(theElectronicsMap->crate(cscDetId), theElectronicsMap->dmb(cscDetId));
00057 return cscData;
00058 }
00059
00060
00061
00062 void CSCDigiToRaw::add(const CSCStripDigiCollection& stripDigis,
00063 const CSCCLCTPreTriggerCollection & preTriggers)
00064 {
00065 for (CSCStripDigiCollection::DigiRangeIterator j=stripDigis.begin(); j!=stripDigis.end(); ++j)
00066 {
00067 CSCDetId cscDetId=(*j).first;
00068 CSCDetId chamberId = chamberID(cscDetId);
00069
00070
00071 CSCCLCTPreTriggerCollection::Range preTriggerRange = preTriggers.get(chamberId);
00072 if(!requirePreTrigger_ || preTriggerRange.first != preTriggerRange.second)
00073 {
00074 bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
00075 bool zplus = (cscDetId.endcap() == 1);
00076 bool me1b = (cscDetId.station()==1) && (cscDetId.ring()==1);
00077
00078 CSCEventData & cscData = findEventData(cscDetId);
00079
00080 std::vector<CSCStripDigi>::const_iterator digiItr = (*j).second.first;
00081 std::vector<CSCStripDigi>::const_iterator last = (*j).second.second;
00082 for( ; digiItr != last; ++digiItr)
00083 {
00084 CSCStripDigi digi = *digiItr;
00085 int strip = digi.getStrip();
00086 if ( me1a && zplus ) { digi.setStrip(17-strip); }
00087 if ( me1b && !zplus) { digi.setStrip(65-strip);}
00088 if ( me1a ) { strip = digi.getStrip(); digi.setStrip(strip+64);}
00089 cscData.add(digi, cscDetId.layer() );
00090 }
00091 }
00092 }
00093 }
00094
00095
00096 void CSCDigiToRaw::add(const CSCWireDigiCollection& wireDigis)
00097 {
00098 for (CSCWireDigiCollection::DigiRangeIterator j=wireDigis.begin(); j!=wireDigis.end(); ++j)
00099 {
00100 CSCDetId cscDetId=(*j).first;
00101 CSCEventData & cscData = findEventData(cscDetId);
00102
00103 std::vector<CSCWireDigi>::const_iterator digiItr = (*j).second.first;
00104 std::vector<CSCWireDigi>::const_iterator last = (*j).second.second;
00105 for( ; digiItr != last; ++digiItr)
00106 {
00107 cscData.add(*digiItr, cscDetId.layer() );
00108 }
00109 }
00110
00111 }
00112
00113 void CSCDigiToRaw::add(const CSCComparatorDigiCollection & comparatorDigis,
00114 const CSCCLCTDigiCollection & clctDigis)
00115 {
00116 for (CSCComparatorDigiCollection::DigiRangeIterator j=comparatorDigis.begin(); j!=comparatorDigis.end(); ++j)
00117 {
00118 CSCDetId cscDetId=(*j).first;
00119 CSCEventData & cscData = findEventData(cscDetId);
00120
00121 CSCDetId chamberId = chamberID(cscDetId);
00122 CSCCLCTDigiCollection::Range chamberClcts = clctDigis.get(chamberId);
00123 if(!requireCLCTForComparators_ || chamberClcts.first != chamberClcts.second)
00124 {
00125 bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
00126
00127 BOOST_FOREACH(CSCComparatorDigi digi, (*j).second)
00128 {
00129
00130
00131 if (me1a && digi.getStrip() <= 16) {
00132 CSCComparatorDigi digi_corr(64+digi.getStrip(),
00133 digi.getComparator(),
00134 digi.getTimeBinWord());
00135 cscData.add(digi_corr, cscDetId.layer());
00136 }
00137 else {
00138 cscData.add(digi, cscDetId.layer());
00139 }
00140 }
00141 }
00142 }
00143 }
00144
00145 void CSCDigiToRaw::add(const CSCALCTDigiCollection & alctDigis)
00146 {
00147 for (CSCALCTDigiCollection::DigiRangeIterator j=alctDigis.begin(); j!=alctDigis.end(); ++j)
00148 {
00149 CSCDetId cscDetId=(*j).first;
00150 CSCEventData & cscData = findEventData(cscDetId);
00151
00152 cscData.add(std::vector<CSCALCTDigi>((*j).second.first, (*j).second.second));
00153 }
00154 }
00155
00156 void CSCDigiToRaw::add(const CSCCLCTDigiCollection & clctDigis)
00157 {
00158 for (CSCCLCTDigiCollection::DigiRangeIterator j=clctDigis.begin(); j!=clctDigis.end(); ++j)
00159 {
00160 CSCDetId cscDetId=(*j).first;
00161 CSCEventData & cscData = findEventData(cscDetId);
00162
00163 cscData.add(std::vector<CSCCLCTDigi>((*j).second.first, (*j).second.second));
00164 }
00165 }
00166
00167 void CSCDigiToRaw::add(const CSCCorrelatedLCTDigiCollection & corrLCTDigis)
00168 {
00169 for (CSCCorrelatedLCTDigiCollection::DigiRangeIterator j=corrLCTDigis.begin(); j!=corrLCTDigis.end(); ++j)
00170 {
00171 CSCDetId cscDetId=(*j).first;
00172 CSCEventData & cscData = findEventData(cscDetId);
00173
00174 cscData.add(std::vector<CSCCorrelatedLCTDigi>((*j).second.first, (*j).second.second));
00175 }
00176
00177 }
00178
00179
00180 CSCDetId CSCDigiToRaw::chamberID(const CSCDetId & cscDetId) const {
00181 CSCDetId chamberId = cscDetId.chamberId();
00182 if(chamberId.ring() ==4)
00183 {
00184 chamberId = CSCDetId(chamberId.endcap(), chamberId.station(), 1, chamberId.chamber(), 0);
00185 }
00186 return chamberId;
00187 }
00188
00189
00190 void CSCDigiToRaw::createFedBuffers(const CSCStripDigiCollection& stripDigis,
00191 const CSCWireDigiCollection& wireDigis,
00192 const CSCComparatorDigiCollection& comparatorDigis,
00193 const CSCALCTDigiCollection& alctDigis,
00194 const CSCCLCTDigiCollection& clctDigis,
00195 const CSCCLCTPreTriggerCollection & preTriggers,
00196 const CSCCorrelatedLCTDigiCollection& correlatedLCTDigis,
00197 FEDRawDataCollection& fed_buffers,
00198 const CSCChamberMap* mapping,
00199 Event & e)
00200 {
00201
00202
00203
00204
00205
00206 beginEvent(mapping);
00207 add(stripDigis, preTriggers);
00208 add(wireDigis);
00209 add(comparatorDigis, clctDigis);
00210 add(alctDigis);
00211 add(clctDigis);
00212 add(correlatedLCTDigis);
00213
00214 int l1a=e.id().event();
00215 int bx = 0;
00216
00217
00218 std::map<int, CSCDCCEventData> dccMap;
00219 for (int idcc=FEDNumbering::MINCSCFEDID;
00220 idcc<=FEDNumbering::MAXCSCFEDID;++idcc)
00221 {
00222
00223
00224
00225
00226
00227
00228 int nDDUs = 5;
00229 dccMap.insert(std::pair<int, CSCDCCEventData>(idcc, CSCDCCEventData(idcc, nDDUs, bx, l1a) ) );
00230
00231
00232 for(map<CSCDetId, CSCEventData>::iterator chamberItr = theChamberDataMap.begin();
00233 chamberItr != theChamberDataMap.end(); ++chamberItr)
00234 {
00235
00236 int indexDCC = mapping->slink(chamberItr->first);
00237 if(indexDCC == idcc)
00238 {
00239
00240 std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.find(indexDCC);
00241 if(dccMapItr == dccMap.end())
00242 {
00243 throw cms::Exception("CSCDigiToRaw") << "Bad DCC number:" << indexDCC;
00244 }
00245
00246
00247 int dduId = mapping->ddu(chamberItr->first);
00248 int dduSlot = mapping->dduSlot(chamberItr->first);
00249 int dduInput = mapping->dduInput(chamberItr->first);
00250 int dmbId = mapping->dmb(chamberItr->first);
00251 dccMapItr->second.addChamber(chamberItr->second, dduId, dduSlot, dduInput, dmbId);
00252 }
00253 }
00254 }
00255
00256
00257 for(std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.begin();
00258 dccMapItr != dccMap.end(); ++dccMapItr)
00259 {
00260 boost::dynamic_bitset<> dccBits = dccMapItr->second.pack();
00261 FEDRawData & fedRawData = fed_buffers.FEDData(dccMapItr->first);
00262 fedRawData.resize(dccBits.size());
00263
00264 bitset_utilities::bitsetToChar(dccBits, fedRawData.data());
00265 FEDTrailer cscFEDTrailer(fedRawData.data()+(fedRawData.size()-8));
00266 cscFEDTrailer.set(fedRawData.data()+(fedRawData.size()-8),
00267 fedRawData.size()/8,
00268 evf::compute_crc(fedRawData.data(),fedRawData.size()), 0, 0);
00269 }
00270 }
00271
00272
00273