CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/EventFilter/CSCTFRawToDigi/plugins/CSCTFUnpacker.cc

Go to the documentation of this file.
00001 #include "EventFilter/CSCTFRawToDigi/interface/CSCTFUnpacker.h"
00002 
00003 //Framework stuff
00004 #include "DataFormats/Common/interface/Handle.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 //FEDRawData
00009 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00010 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00011 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00012 
00013 //Digi
00014 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigi.h"
00015 #include "DataFormats/L1CSCTrackFinder/interface/L1Track.h"
00016 #include "DataFormats/L1CSCTrackFinder/interface/L1CSCSPStatusDigi.h"
00017 #include "DataFormats/L1CSCTrackFinder/interface/TrackStub.h"
00018 
00019 //Digi collections
00020 #include "DataFormats/CSCDigi/interface/CSCCorrelatedLCTDigiCollection.h"
00021 #include "DataFormats/L1CSCTrackFinder/interface/L1CSCTrackCollection.h"
00022 #include "DataFormats/L1CSCTrackFinder/interface/L1CSCStatusDigiCollection.h"
00023 #include "DataFormats/L1CSCTrackFinder/interface/CSCTriggerContainer.h"
00024 
00025 //Unique key
00026 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00027 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
00028 
00029 //Don't know what
00030 #include <EventFilter/CSCTFRawToDigi/interface/CSCTFMonitorInterface.h>
00031 #include "FWCore/ServiceRegistry/interface/Service.h"
00032 
00033 #include "CondFormats/CSCObjects/interface/CSCTriggerMappingFromFile.h"
00034 //#include <DataFormats/MuonDetId/interface/CSCTriggerNumbering.h>
00035 
00036 //#include <iostream>
00037 #include <sstream>
00038 
00039 CSCTFUnpacker::CSCTFUnpacker(const edm::ParameterSet& pset):edm::EDProducer(),mapping(0){
00040         LogDebug("CSCTFUnpacker|ctor")<<"Started ...";
00041 
00042         // Edges of the time window, which LCTs are put into (unlike tracks, which are always centred around 0):
00043         m_minBX = pset.getParameter<int>("MinBX"); //3
00044         m_maxBX = pset.getParameter<int>("MaxBX"); //9
00045 
00046         // Swap: if(swapME1strips && me1b && !zplus) strip = 65 - strip; // 1-64 -> 64-1 :
00047         swapME1strips = pset.getParameter<bool>("swapME1strips");
00048 
00049         // Initialize slot<->sector assignment
00050         slot2sector = pset.getParameter< std::vector<int> >("slot2sector");
00051         LogDebug("CSCTFUnpacker|ctor")<<"Verifying slot<->sector map from 'vint32 slot2sector'";
00052         for(int slot=0; slot<22; slot++)
00053                 if( slot2sector[slot]<0 || slot2sector[slot]>12 )
00054                         throw cms::Exception("Invalid configuration")<<"CSCTFUnpacker: sector index is set out of range (slot2sector["<<slot<<"]="<<slot2sector[slot]<<", should be [0-12])";
00055         // Just for safety (in case of bad data):
00056         slot2sector.resize(32);
00057 
00058         // As we use standard CSC digi containers, we have to initialize mapping:
00059         std::string mappingFile = pset.getParameter<std::string>("mappingFile");
00060         if( mappingFile.length() ){
00061                 LogDebug("CSCTFUnpacker|ctor") << "Define ``mapping'' only if you want to screw up real geometry";
00062                 mapping = new CSCTriggerMappingFromFile(mappingFile);
00063         } else {
00064                 LogDebug("CSCTFUnpacker|ctor") << "Generating default hw<->geometry mapping";
00065                 class M: public CSCTriggerSimpleMapping{ void fill(void){} };
00066                 mapping = new M();
00067                 for(int endcap=1; endcap<=2; endcap++)
00068                         for(int station=1; station<=4; station++)
00069                                 for(int sector=1; sector<=6; sector++)
00070                                         for(int csc=1; csc<=9; csc++){
00071                                                 if( station==1 ){
00072                                                         mapping->addRecord(endcap,station,sector,1,csc,endcap,station,sector,1,csc);
00073                                                         mapping->addRecord(endcap,station,sector,2,csc,endcap,station,sector,2,csc);
00074                                                 } else
00075                                                         mapping->addRecord(endcap,station,sector,0,csc,endcap,station,sector,0,csc);
00076                                         }
00077         }
00078 
00079         producer = pset.getParameter<edm::InputTag>("producer");
00080 
00081         produces<CSCCorrelatedLCTDigiCollection>();
00082         produces<L1CSCTrackCollection>();
00083         produces<L1CSCStatusDigiCollection>();
00084         produces<CSCTriggerContainer<csctf::TrackStub> >("DT");
00085 }
00086 
00087 CSCTFUnpacker::~CSCTFUnpacker(){
00088         if( mapping ) delete mapping;
00089 }
00090 
00091 void CSCTFUnpacker::produce(edm::Event& e, const edm::EventSetup& c){
00092         // Get a handle to the FED data collection
00093         edm::Handle<FEDRawDataCollection> rawdata;
00094         e.getByLabel(producer.label(),producer.instance(),rawdata);
00095 
00096         // create the collection of CSC wire and strip digis as well as of DT stubs, which we receive from DTTF
00097         std::auto_ptr<CSCCorrelatedLCTDigiCollection> LCTProduct(new CSCCorrelatedLCTDigiCollection);
00098         std::auto_ptr<L1CSCTrackCollection>           trackProduct(new L1CSCTrackCollection);
00099         std::auto_ptr<L1CSCStatusDigiCollection>      statusProduct(new L1CSCStatusDigiCollection);
00100         std::auto_ptr<CSCTriggerContainer<csctf::TrackStub> > dtProduct(new CSCTriggerContainer<csctf::TrackStub>);
00101 
00102         for(int fedid=FEDNumbering::MINCSCTFFEDID; fedid<=FEDNumbering::MAXCSCTFFEDID; fedid++){
00103                 const FEDRawData& fedData = rawdata->FEDData(fedid);
00104                 if( fedData.size()==0 ) continue;
00105                 //LogDebug("CSCTFUnpacker|produce");
00106                 //if( monitor ) monitor->process((unsigned short*)fedData.data());
00107                 unsigned int unpacking_status = tfEvent.unpack((unsigned short*)fedData.data(),fedData.size()/2);
00108                 if( unpacking_status==0 ){
00109                         // There may be several SPs in event
00110                         std::vector<const CSCSPEvent*> SPs = tfEvent.SPs_fast();
00111                         // Cycle over all of them
00112                         for(std::vector<const CSCSPEvent *>::const_iterator spItr=SPs.begin(); spItr!=SPs.end(); spItr++){
00113                                 const CSCSPEvent *sp = *spItr;
00114 
00115                                 L1CSCSPStatusDigi status; 
00116                                 status.sp_slot    = sp->header().slot();
00117                                 status.l1a_bxn    = sp->header().BXN();
00118                                 status.fmm_status = sp->header().status();
00119                                 status.track_cnt  = sp->counters().track_counter();
00120                                 status.orbit_cnt  = sp->counters().orbit_counter();
00121 
00122                                 // Finds central LCT BX
00123                                 // assumes window is odd number of bins
00124                                 int central_lct_bx = (m_maxBX + m_minBX)/2;
00125 
00126                                 // Find central SP BX
00127                                 // assumes window is odd number of bins
00128                                 int central_sp_bx = int(sp->header().nTBINs()/2);
00129 
00130                                 for(unsigned int tbin=0; tbin<sp->header().nTBINs(); tbin++){
00131 
00132                                         status.se |= sp->record(tbin).SEs();
00133                                         status.sm |= sp->record(tbin).SMs();
00134                                         status.bx |= sp->record(tbin).BXs();
00135                                         status.af |= sp->record(tbin).AFs();
00136                                         status.vp |= sp->record(tbin).VPs();
00137 
00138                                         for(unsigned int FPGA=0; FPGA<5; FPGA++)
00139                                                 for(unsigned int MPClink=0; MPClink<3; ++MPClink){
00140                                                         std::vector<CSCSP_MEblock> lct = sp->record(tbin).LCT(FPGA,MPClink);
00141                                                         if( lct.size()==0 ) continue;
00142 
00143                                                         status.link_status[lct[0].spInput()] |=
00144                                                                 (1<<lct[0].receiver_status_frame1())|
00145                                                                 (1<<lct[0].receiver_status_frame2())|
00146                                                                 ((lct[0].aligment_fifo()?1:0)<<4);
00147                                                         status.mpc_link_id |= (lct[0].link()<<2)|lct[0].mpc();
00148 
00149                                                         int station = ( FPGA ? FPGA : 1 );
00150                                                         int endcap=0, sector=0;
00151                                                         if( slot2sector[sp->header().slot()] ){
00152                                                                 endcap = slot2sector[sp->header().slot()]/7 + 1;
00153                                                                 sector = slot2sector[sp->header().slot()];
00154                                                                 if( sector>6 ) sector -= 6;
00155                                                         } else {
00156                                                                 endcap = (sp->header().endcap()?1:2);
00157                                                                 sector =  sp->header().sector();
00158                                                         }
00159                                                         int subsector = ( FPGA>1 ? 0 : FPGA+1 );
00160                                                         int cscid   = lct[0].csc() ;
00161 
00162                                                         try{
00163                                                                 CSCDetId id = mapping->detId(endcap,station,sector,subsector,cscid,0);
00164                                                                 // corrlcts now have no layer associated with them
00165                                                                 LCTProduct->insertDigi(id,
00166                                                                         CSCCorrelatedLCTDigi(
00167                                                                                 0,lct[0].vp(),lct[0].quality(),lct[0].wireGroup(),
00168                                                                                 (swapME1strips && cscid<=3 && station==1 && endcap==2 && lct[0].strip()<65 ? 65 - lct[0].strip() : lct[0].strip() ),
00169                                                                                 lct[0].pattern(),lct[0].l_r(),
00170                                                                                 (lct[0].tbin()+(central_lct_bx-central_sp_bx)),
00171                                                                                 lct[0].link(), lct[0].BXN(), 0, cscid )
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_MBblock> mbStubs = sp->record(tbin).mbStubs();
00181                                         for(std::vector<CSCSP_MBblock>::const_iterator iter=mbStubs.begin(); iter!=mbStubs.end(); iter++){
00182                                                 int endcap, sector;
00183                                                 if( slot2sector[sp->header().slot()] ){
00184                                                         endcap = slot2sector[sp->header().slot()]/7 + 1;
00185                                                         sector = slot2sector[sp->header().slot()];
00186                                                         if( sector>6 ) sector -= 6;
00187                                                 } else {
00188                                                         endcap = (sp->header().endcap()?1:2);
00189                                                         sector =  sp->header().sector();
00190                                                 }
00191                                                 const unsigned int csc2dt[6][2] = {{2,3},{4,5},{6,7},{8,9},{10,11},{12,1}};
00192                                                 DTChamberId id((endcap==1?2:-2),1, csc2dt[sector-1][iter->id()-1]);
00193                                                 CSCCorrelatedLCTDigi base(0,iter->vq(),iter->quality(),iter->cal(),iter->flag(),iter->bc0(),iter->phi_bend(),tbin+(central_lct_bx-central_sp_bx),iter->id(),iter->bxn(),iter->timingError(),iter->BXN());
00194                                                 csctf::TrackStub dtStub(base,id,iter->phi(),0);
00195                                                 dtProduct->push_back(dtStub);
00196                                         }
00197 
00198                                         std::vector<CSCSP_SPblock> tracks = sp->record(tbin).tracks();
00199                                         unsigned int trkNumber=0;
00200                                         for(std::vector<CSCSP_SPblock>::const_iterator iter=tracks.begin(); iter!=tracks.end(); iter++,trkNumber++){
00201                                                 L1CSCTrack track;
00202                                                 if( slot2sector[sp->header().slot()] ){
00203                                                         track.first.m_endcap = slot2sector[sp->header().slot()]/7 + 1;
00204                                                         track.first.m_sector = slot2sector[sp->header().slot()];
00205                                                         if(  track.first.m_sector>6 ) track.first.m_sector -= 6;
00206                                                 } else {
00207                                                         track.first.m_endcap = (sp->header().endcap()?1:2);
00208                                                         track.first.m_sector =  sp->header().sector();
00209                                                 }
00210 
00211                                                 track.first.m_lphi      = iter->phi();
00212                                                 track.first.m_ptAddress = iter->ptLUTaddress();
00213                                                 track.first.m_fr        = iter->f_r();
00214                                                 track.first.m_ptAddress|=(iter->f_r() << 21);
00215 
00216                                                 track.first.setStationIds(iter->ME1_id(),iter->ME2_id(),iter->ME3_id(),iter->ME4_id(),iter->MB_id());
00217                                                 track.first.setTbins(iter->ME1_tbin(), iter->ME2_tbin(), iter->ME3_tbin(), iter->ME4_tbin(), iter->MB_tbin() );
00218                                                 track.first.setBx(iter->tbin()-central_sp_bx);
00219                                                 track.first.setBits(iter->syncErr(), iter->bx0(), iter->bc0());
00220 
00221                                                 track.first.setLocalPhi(iter->phi());
00222                                                 track.first.setEtaPacked(iter->eta());
00223                                                 track.first.setChargePacked(iter->charge());
00224 
00225                                                 track.first.m_output_link = iter->id();
00226                                                 if( track.first.m_output_link ){
00227                                                         track.first.m_rank = (iter->f_r()?sp->record(tbin).ptSpy()&0x7F:(sp->record(tbin).ptSpy()&0x7F00)>>8);
00228                                                         track.first.setChargeValidPacked((iter->f_r()?(sp->record(tbin).ptSpy()&0x80)>>8:(sp->record(tbin).ptSpy()&0x8000)>>15));
00229                                                 } else {
00230                                                         track.first.m_rank = 0;
00231                                                         track.first.setChargeValidPacked(0);
00232                                                 }
00233                                                 track.first.setFineHaloPacked(iter->halo());
00234 
00235                                                 track.first.m_winner = iter->MS_id()&(1<<trkNumber);
00236 
00237                                                 std::vector<CSCSP_MEblock> lcts = iter->LCTs();
00238                                                 for(std::vector<CSCSP_MEblock>::const_iterator lct=lcts.begin(); lct!=lcts.end(); lct++){
00239                                                         int station   = ( lct->spInput()>6 ? (lct->spInput()-1)/3 : 1 );
00240                                                         int subsector = ( lct->spInput()>6 ? 0 : (lct->spInput()-1)/3 + 1 );
00241                                                         try{
00242                                                                 CSCDetId id = mapping->detId(track.first.m_endcap,station,track.first.m_sector,subsector,lct->csc(),0);
00243                                                                 track.second.insertDigi(id,
00244                                                                         CSCCorrelatedLCTDigi(
00245                                                                                 0,lct->vp(),lct->quality(),lct->wireGroup(),
00246                                                                                 (swapME1strips && lct->csc()<=3 && station==1 && track.first.m_endcap==2 && lct[0].strip()<65 ? 65 - lct[0].strip() : lct[0].strip() ),
00247                                                                                 lct->pattern(),lct->l_r(),
00248                                                                                 (lct->tbin()+(central_lct_bx-central_sp_bx)),
00249                                                                                 lct->link(), lct->BXN(), 0, lct->csc() )
00250                                                                         );
00251                                                         } catch(cms::Exception &e) {
00252                                                                 edm::LogInfo("CSCTFUnpacker|produce") << e.what() << "Not adding track digi to collection in event"
00253                                                                       <<sp->header().L1A()<<" (endcap="<<track.first.m_endcap<<",station="<<station<<",sector="<<track.first.m_sector<<",subsector="<<subsector<<",cscid="<<lct->csc()<<",spSlot="<<sp->header().slot()<<")";
00254                                                         }
00255                                                 }
00256 
00257                                                 std::vector<CSCSP_MBblock> mbStubs = iter->dtStub();
00258                                                 for(std::vector<CSCSP_MBblock>::const_iterator iter=mbStubs.begin(); iter!=mbStubs.end(); iter++){
00259                                                         CSCDetId id = mapping->detId(track.first.m_endcap,1,track.first.m_sector,iter->id(),1,0);
00260                                                         track.second.insertDigi(id,
00261                                                                 CSCCorrelatedLCTDigi(iter->phi(),iter->vq(),iter->quality()+100,iter->cal(),iter->flag(),iter->bc0(),iter->phi_bend(),tbin+(central_lct_bx-central_sp_bx),iter->id(),iter->bxn(),iter->timingError(),iter->BXN())
00262                                                         );
00263                                                 }
00264 
00265                                                 trackProduct->push_back( track );
00266                                         }
00267                                 }
00268                                 statusProduct->second.push_back( status );
00269                         }
00270                 } else {
00271                         edm::LogError("CSCTFUnpacker|produce")<<" problem of unpacking TF event: 0x"<<std::hex<<unpacking_status<<std::dec<<" code";
00272                 }
00273 
00274                 statusProduct->first  = unpacking_status;
00275 
00276         } //end of fed cycle
00277         e.put(dtProduct,"DT");
00278         e.put(LCTProduct); // put processed lcts into the event.
00279         e.put(trackProduct);
00280         e.put(statusProduct);
00281 }