00001 #include "EventFilter/CSCTFRawToDigi/interface/CSCTFUnpacker.h"
00002
00003
00004 #include "DataFormats/Common/interface/Handle.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007
00008
00009 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00010 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00011 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00012
00013
00014 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigi.h"
00015 #include "DataFormats/L1CSCTrackFinder/interface/L1Track.h"
00016 #include "DataFormats/L1CSCTrackFinder/interface/L1CSCSPStatusDigi.h"
00017
00018
00019 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigiCollection.h"
00020 #include "DataFormats/L1CSCTrackFinder/interface/L1CSCTrackCollection.h"
00021 #include "DataFormats/L1CSCTrackFinder/interface/L1CSCStatusDigiCollection.h"
00022
00023
00024 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00025
00026
00027 #include <EventFilter/CSCTFRawToDigi/interface/CSCTFMonitorInterface.h>
00028 #include "FWCore/ServiceRegistry/interface/Service.h"
00029
00030 #include "CondFormats/CSCObjects/interface/CSCTriggerMappingFromFile.h"
00031
00032
00033
00034 #include <sstream>
00035
00036 CSCTFUnpacker::CSCTFUnpacker(const edm::ParameterSet& pset):edm::EDProducer(),mapping(0){
00037 LogDebug("CSCTFUnpacker|ctor")<<"Started ...";
00038
00039
00040 m_minBX = pset.getParameter<int>("MinBX");
00041 m_maxBX = pset.getParameter<int>("MaxBX");
00042
00043
00044 swapME1strips = pset.getParameter<bool>("swapME1strips");
00045
00046
00047 slot2sector = pset.getParameter< std::vector<int> >("slot2sector");
00048 LogDebug("CSCTFUnpacker|ctor")<<"Verifying slot<->sector map from 'untracked vint32 slot2sector'";
00049 for(int slot=0; slot<22; slot++)
00050 if( slot2sector[slot]<0 || slot2sector[slot]>12 )
00051 throw cms::Exception("Invalid configuration")<<"CSCTFUnpacker: sector index is set out of range (slot2sector["<<slot<<"]="<<slot2sector[slot]<<", should be [0-12])";
00052
00053 slot2sector.resize(32);
00054
00055
00056 std::string mappingFile = pset.getParameter<std::string>("mappingFile");
00057 if( mappingFile.length() ){
00058 LogDebug("CSCTFUnpacker|ctor") << "Define ``mapping'' only if you want to screw up real geometry";
00059 mapping = new CSCTriggerMappingFromFile(mappingFile);
00060 } else {
00061 LogDebug("CSCTFUnpacker|ctor") << "Generating default hw<->geometry mapping";
00062 class M: public CSCTriggerSimpleMapping{ void fill(void){} };
00063 mapping = new M();
00064 for(int endcap=1; endcap<=2; endcap++)
00065 for(int station=1; station<=4; station++)
00066 for(int sector=1; sector<=6; sector++)
00067 for(int csc=1; csc<=9; csc++){
00068 if( station==1 ){
00069 mapping->addRecord(endcap,station,sector,1,csc,endcap,station,sector,1,csc);
00070 mapping->addRecord(endcap,station,sector,2,csc,endcap,station,sector,2,csc);
00071 } else
00072 mapping->addRecord(endcap,station,sector,0,csc,endcap,station,sector,0,csc);
00073 }
00074 }
00075
00076 producer = pset.getUntrackedParameter<edm::InputTag>("producer",edm::InputTag("source"));
00077
00078 produces<CSCCorrelatedLCTDigiCollection>();
00079 produces<L1CSCTrackCollection>();
00080 produces<L1CSCStatusDigiCollection>();
00081
00082 LogDebug("CSCTFUnpacker|ctor") << "... and finished";
00083 }
00084
00085 CSCTFUnpacker::~CSCTFUnpacker(){
00086 if( mapping ) delete mapping;
00087 }
00088
00089 void CSCTFUnpacker::produce(edm::Event& e, const edm::EventSetup& c){
00090
00091 edm::Handle<FEDRawDataCollection> rawdata;
00092 e.getByLabel(producer.label(),producer.instance(),rawdata);
00093
00094
00095 std::auto_ptr<CSCCorrelatedLCTDigiCollection> LCTProduct(new CSCCorrelatedLCTDigiCollection);
00096 std::auto_ptr<L1CSCTrackCollection> trackProduct(new L1CSCTrackCollection);
00097 std::auto_ptr<L1CSCStatusDigiCollection> statusProduct(new L1CSCStatusDigiCollection);
00098
00099 for(int fedid=FEDNumbering::getCSCTFFEDIds().first; fedid<=FEDNumbering::getCSCTFFEDIds().second; fedid++){
00100 const FEDRawData& fedData = rawdata->FEDData(fedid);
00101 if( fedData.size()==0 ) continue;
00102
00103
00104 unsigned int unpacking_status = tfEvent.unpack((unsigned short*)fedData.data(),fedData.size()/2);
00105 if( unpacking_status==0 ){
00106
00107 std::vector<const CSCSPEvent*> SPs = tfEvent.SPs_fast();
00108
00109 for(std::vector<const CSCSPEvent *>::const_iterator spItr=SPs.begin(); spItr!=SPs.end(); spItr++){
00110 const CSCSPEvent *sp = *spItr;
00111
00112 L1CSCSPStatusDigi status;
00113 status.sp_slot = sp->header().slot();
00114 status.l1a_bxn = sp->header().BXN();
00115 status.fmm_status = sp->header().status();
00116 status.track_cnt = sp->counters().track_counter();
00117 status.orbit_cnt = sp->counters().orbit_counter();
00118
00119
00120
00121 int central_lct_bx = (m_maxBX + m_minBX)/2;
00122
00123
00124
00125 int central_sp_bx = int(sp->header().nTBINs()/2);
00126
00127 for(unsigned int tbin=0; tbin<sp->header().nTBINs(); tbin++){
00128
00129 status.se |= sp->record(tbin).SEs();
00130 status.sm |= sp->record(tbin).SMs();
00131 status.bx |= sp->record(tbin).BXs();
00132 status.af |= sp->record(tbin).AFs();
00133 status.vp |= sp->record(tbin).VPs();
00134
00135 for(unsigned int FPGA=0; FPGA<5; FPGA++)
00136 for(unsigned int MPClink=0; MPClink<3; ++MPClink){
00137 std::vector<CSCSP_MEblock> lct = sp->record(tbin).LCT(FPGA,MPClink);
00138 if( lct.size()==0 ) continue;
00139
00140 status.link_status[lct[0].spInput()] |=
00141 (1<<lct[0].receiver_status_frame1())|
00142 (1<<lct[0].receiver_status_frame2())|
00143 ((lct[0].aligment_fifo()?1:0)<<4);
00144 status.mpc_link_id |= (lct[0].link()<<2)|lct[0].mpc();
00145
00146 int station = ( FPGA ? FPGA : 1 );
00147 int endcap=0, sector=0;
00148 if( slot2sector[sp->header().slot()] ){
00149 endcap = slot2sector[sp->header().slot()]/7 + 1;
00150 sector = slot2sector[sp->header().slot()];
00151 if( sector>6 ) sector -= 6;
00152 } else {
00153 endcap = (sp->header().endcap()?1:2);
00154 sector = sp->header().sector();
00155 }
00156 int subsector = ( FPGA>1 ? 0 : FPGA+1 );
00157 int cscid = lct[0].csc() ;
00158
00159 try{
00160 CSCDetId id = mapping->detId(endcap,station,sector,subsector,cscid,0);
00161
00162 LCTProduct->insertDigi(id,
00163 CSCCorrelatedLCTDigi(
00164 0,lct[0].vp(),lct[0].quality(),lct[0].wireGroup(),
00165 (swapME1strips && cscid<=3 && station==1 && endcap==2 && lct[0].strip()<65 ? 65 - lct[0].strip() : lct[0].strip() ),
00166 lct[0].pattern(),lct[0].l_r(),
00167 (lct[0].tbin()+(central_lct_bx-central_sp_bx)),
00168 lct[0].link(), lct[0].BXN(), 0, cscid )
00169 );
00170
00171
00172
00173 } catch(cms::Exception &e) {
00174 edm::LogInfo("CSCTFUnpacker|produce") << e.what() << "Not adding digi to collection in event "
00175 <<sp->header().L1A()<<" (endcap="<<endcap<<",station="<<station<<",sector="<<sector<<",subsector="<<subsector<<",cscid="<<cscid<<",spSlot="<<sp->header().slot()<<")";
00176 }
00177
00178 }
00179
00180 std::vector<CSCSP_SPblock> tracks = sp->record(tbin).tracks();
00181 unsigned int trkNumber=0;
00182 for(std::vector<CSCSP_SPblock>::const_iterator iter=tracks.begin(); iter!=tracks.end(); iter++,trkNumber++){
00183 L1CSCTrack track;
00184 if( slot2sector[sp->header().slot()] ){
00185 track.first.m_endcap = slot2sector[sp->header().slot()]/7 + 1;
00186 track.first.m_sector = slot2sector[sp->header().slot()];
00187 if( track.first.m_sector>6 ) track.first.m_sector -= 6;
00188 } else {
00189 track.first.m_endcap = (sp->header().endcap()?1:2);
00190 track.first.m_sector = sp->header().sector();
00191 }
00192
00193 track.first.m_lphi = iter->phi();
00194 track.first.m_ptAddress = iter->ptLUTaddress();
00195 track.first.m_ptAddress|=(iter->f_r() << 21);
00196 track.first.setStationIds(iter->ME1_id(),iter->ME2_id(),iter->ME3_id(),iter->ME4_id(),iter->MB_id());
00197 track.first.setBx(iter->tbin()-central_sp_bx);
00198
00199 track.first.setPhiPacked(iter->phi());
00200 track.first.setEtaPacked(iter->eta());
00201 track.first.setChargePacked((~iter->charge())&0x1);
00202
00203 track.first.m_output_link = sp->record(tbin).ptSpyTrack();
00204 if( track.first.m_output_link ){
00205 track.first.m_rank = (iter->f_r()?sp->record(tbin).ptSpy()&0x1F:(sp->record(tbin).ptSpy()&0x1F00)>>8);
00206 track.first.setChargeValidPacked((iter->f_r()?(sp->record(tbin).ptSpy()&0x80)>>8:(sp->record(tbin).ptSpy()&0x8000)>>15));
00207 } else {
00208 track.first.m_rank = 0;
00209 track.first.setChargeValidPacked(0);
00210 }
00211 track.first.setFineHaloPacked(iter->halo());
00212
00213
00214
00215 std::vector<CSCSP_MEblock> lcts = iter->LCTs();
00216
00217 for(std::vector<CSCSP_MEblock>::const_iterator lct=lcts.begin(); lct!=lcts.end(); lct++){
00218 int station = ( lct->spInput()>6 ? (lct->spInput()-1)/3 : 1 );
00219 int subsector = ( lct->spInput()>6 ? 0 : (lct->spInput()-1)/3 + 1 );
00220 try{
00221 CSCDetId id = mapping->detId(track.first.m_endcap,station,track.first.m_sector,subsector,lct->csc(),0);
00222 track.second.insertDigi(id,
00223 CSCCorrelatedLCTDigi(
00224 0,lct->vp(),lct->quality(),lct->wireGroup(),
00225 (swapME1strips && lct->csc()<=3 && station==1 && track.first.m_endcap==2 && lct[0].strip()<65 ? 65 - lct[0].strip() : lct[0].strip() ),
00226 lct->pattern(),lct->l_r(),
00227 (lct->tbin()+(central_lct_bx-central_sp_bx)),
00228 lct->link(), lct->BXN(), 0, lct->csc() )
00229 );
00230 } catch(cms::Exception &e) {
00231 edm::LogInfo("CSCTFUnpacker|produce") << e.what() << "Not adding track digi to collection in event"
00232 <<sp->header().L1A()<<" (endcap="<<track.first.m_endcap<<",station="<<station<<",sector="<<track.first.m_sector<<",subsector="<<subsector<<",cscid="<<lct->csc()<<",spSlot="<<sp->header().slot()<<")";
00233 }
00234 }
00235 trackProduct->push_back( track );
00236 }
00237 }
00238 statusProduct->second.push_back( status );
00239 }
00240 } else {
00241 edm::LogError("CSCTFUnpacker|produce")<<" problem of unpacking TF event: 0x"<<std::hex<<unpacking_status<<std::dec<<" code";
00242 }
00243
00244 statusProduct->first = unpacking_status;
00245
00246 }
00247 e.put(LCTProduct);
00248 e.put(trackProduct);
00249 e.put(statusProduct);
00250 }