CMS 3D CMS Logo

CSCDigiToRaw.cc

Go to the documentation of this file.
00001 
00008 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00009 #include "EventFilter/CSCRawToDigi/src/CSCDigiToRaw.h"
00010 #include "EventFilter/CSCRawToDigi/interface/CSCEventData.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 "EventFilter/Utilities/interface/Crc.h"
00021 #include "CondFormats/CSCObjects/interface/CSCChamberMap.h"
00022 #include "FWCore/Utilities/interface/Exception.h"
00023 
00024 #include <algorithm>
00025 
00026 
00027 
00028 using namespace edm;
00029 using namespace std;
00030 
00031 CSCDigiToRaw::CSCDigiToRaw(){}
00032 
00033 void CSCDigiToRaw::beginEvent(const CSCChamberMap* electronicsMap)
00034 {
00035   theChamberDataMap.clear();
00036   theElectronicsMap = electronicsMap;
00037 }
00038 
00039 
00040 CSCEventData & CSCDigiToRaw::findEventData(const CSCDetId & cscDetId) 
00041 {
00042   CSCDetId chamberID = cscDetId.chamberId();
00043   // translate ME1A to ME11
00044   if(chamberID.ring() ==4)
00045   {
00046     chamberID = CSCDetId(chamberID.endcap(), chamberID.station(), 1, chamberID.chamber(), 0);
00047   }
00048   //std::cout<<"wire id"<<cscDetId<<std::endl;
00049   // find the entry into the map
00050   map<CSCDetId, CSCEventData>::iterator chamberMapItr = theChamberDataMap.find(chamberID);
00051   if(chamberMapItr == theChamberDataMap.end())
00052     {
00053       // make an entry, telling it the correct chamberType
00054       int chamberType = chamberID.iChamberType();
00055       chamberMapItr = theChamberDataMap.insert(pair<CSCDetId, CSCEventData>(chamberID, CSCEventData(chamberType))).first;
00056     }
00057   CSCEventData & cscData = chamberMapItr->second;
00058   cscData.dmbHeader()->setCrateAddress(theElectronicsMap->crate(cscDetId), theElectronicsMap->dmb(cscDetId));
00059   return cscData;
00060 }
00061 
00062 
00063   
00064 void CSCDigiToRaw::add(const CSCStripDigiCollection& stripDigis)
00065 {  //iterate over chambers with strip digis in them
00066   for (CSCStripDigiCollection::DigiRangeIterator j=stripDigis.begin(); j!=stripDigis.end(); ++j)
00067     {
00068       CSCDetId cscDetId=(*j).first;
00069 
00070       bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
00071       bool zplus = (cscDetId.endcap() == 1);
00072       bool me1b = (cscDetId.station()==1) && (cscDetId.ring()==1);
00073 
00074       CSCEventData & cscData = findEventData(cscDetId);
00075 
00076       std::vector<CSCStripDigi>::const_iterator digiItr = (*j).second.first;
00077       std::vector<CSCStripDigi>::const_iterator last = (*j).second.second;
00078       for( ; digiItr != last; ++digiItr)
00079         {
00080           CSCStripDigi digi = *digiItr;
00081           int strip = digi.getStrip();
00082           if ( me1a && zplus ) { digi.setStrip(17-strip); } // 1-16 -> 16-1
00083           if ( me1b && !zplus) { digi.setStrip(65-strip);} // 1-64 -> 64-1
00084           if ( me1a ) { strip = digi.getStrip(); digi.setStrip(strip+64);} // reset back 1-16 to 65-80 digi
00085           cscData.add(digi, cscDetId.layer() );
00086         }
00087     }
00088 }
00089 
00090 
00091 void CSCDigiToRaw::add(const CSCWireDigiCollection& wireDigis) 
00092 {
00093   for (CSCWireDigiCollection::DigiRangeIterator j=wireDigis.begin(); j!=wireDigis.end(); ++j)
00094     {
00095       CSCDetId cscDetId=(*j).first;
00096       CSCEventData & cscData = findEventData(cscDetId);
00097 
00098       std::vector<CSCWireDigi>::const_iterator digiItr = (*j).second.first;
00099       std::vector<CSCWireDigi>::const_iterator last = (*j).second.second;
00100       for( ; digiItr != last; ++digiItr)
00101         {
00102           cscData.add(*digiItr, cscDetId.layer() );
00103         }
00104     }
00105 
00106 }
00107 
00108 void CSCDigiToRaw::add(const CSCComparatorDigiCollection & comparatorDigis)
00109 {
00110   for (CSCComparatorDigiCollection::DigiRangeIterator j=comparatorDigis.begin(); j!=comparatorDigis.end(); ++j)
00111     {
00112       CSCDetId cscDetId=(*j).first;
00113       CSCEventData & cscData = findEventData(cscDetId);
00114 
00115       bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
00116       BOOST_FOREACH(CSCComparatorDigi digi, (*j).second)
00117         {
00118           // Move ME1/A comparators from CFEB=0 to CFEB=4 if this has not
00119           // been done already.
00120           if (me1a && digi.getStrip() <= 16) {
00121             CSCComparatorDigi digi_corr(64+digi.getStrip(),
00122                                         digi.getComparator(),
00123                                         digi.getTimeBinWord());
00124             cscData.add(digi_corr, cscDetId.layer());
00125           }
00126           else {
00127             cscData.add(digi, cscDetId.layer());
00128           }
00129         }
00130     }
00131 }
00132 
00133 void CSCDigiToRaw::add(const CSCALCTDigiCollection & alctDigis)
00134 {
00135   for (CSCALCTDigiCollection::DigiRangeIterator j=alctDigis.begin(); j!=alctDigis.end(); ++j)
00136     {
00137       CSCDetId cscDetId=(*j).first;
00138       CSCEventData & cscData = findEventData(cscDetId);
00139 
00140        cscData.add(std::vector<CSCALCTDigi>((*j).second.first, (*j).second.second));
00141     }
00142 }
00143 
00144 void CSCDigiToRaw::add(const CSCCLCTDigiCollection & clctDigis)
00145 {
00146   for (CSCCLCTDigiCollection::DigiRangeIterator j=clctDigis.begin(); j!=clctDigis.end(); ++j)
00147     {
00148       CSCDetId cscDetId=(*j).first;
00149       CSCEventData & cscData = findEventData(cscDetId);
00150 
00151       cscData.add(std::vector<CSCCLCTDigi>((*j).second.first, (*j).second.second));
00152     }
00153 }
00154 
00155 void CSCDigiToRaw::add(const CSCCorrelatedLCTDigiCollection & corrLCTDigis)
00156 {
00157   for (CSCCorrelatedLCTDigiCollection::DigiRangeIterator j=corrLCTDigis.begin(); j!=corrLCTDigis.end(); ++j)
00158     {
00159       CSCDetId cscDetId=(*j).first;
00160       CSCEventData & cscData = findEventData(cscDetId);
00161 
00162       cscData.add(std::vector<CSCCorrelatedLCTDigi>((*j).second.first, (*j).second.second));
00163     }
00164 
00165 }
00166 
00167 
00168 void CSCDigiToRaw::createFedBuffers(const CSCStripDigiCollection& stripDigis,
00169                                     const CSCWireDigiCollection& wireDigis,
00170                                     const CSCComparatorDigiCollection& comparatorDigis,
00171                                     const CSCALCTDigiCollection& alctDigis,
00172                                     const CSCCLCTDigiCollection& clctDigis,
00173                                     const CSCCorrelatedLCTDigiCollection& correlatedLCTDigis,
00174                                     FEDRawDataCollection& fed_buffers,
00175                                     const CSCChamberMap* mapping,
00176                                     Event & e)
00177 {
00178 
00179   //bits of code from ORCA/Muon/METBFormatter - thanks, Rick:)!
00180   
00181   //get fed object from fed_buffers
00182   // make a map from the index of a chamber to the event data from it
00183   beginEvent(mapping);
00184   add(stripDigis);
00185   add(wireDigis);
00186   add(comparatorDigis);
00187   add(alctDigis);
00188   add(clctDigis);
00189   add(correlatedLCTDigis);
00190   
00191   int l1a=e.id().event(); //need to add increments or get it from lct digis 
00192   int bx = 0;//same as above
00193   //int startingFED = FEDNumbering::getCSCFEDIds().first;
00194 
00195   std::map<int, CSCDCCEventData> dccMap;
00196   for (int idcc=FEDNumbering::getCSCFEDIds().first;
00197        idcc<=FEDNumbering::getCSCFEDIds().second;++idcc) 
00198   {
00199     //idcc goes from startingFed to startingFED+7
00200     // @@ if ReadoutMapping changes, this'll have to change
00201     // DCCs 1, 2,4,5have 5 DDUs.  Otherwise, 4
00202     //int nDDUs = (idcc < 2) || (idcc ==4) || (idcc ==5)
00203     //          ? 5 : 4; 
00204     //@@ WARNING some DCCs only have 4 DDUs, but I'm giving them all 5, for now
00205     int nDDUs = 5;
00206     dccMap.insert(std::pair<int, CSCDCCEventData>(idcc, CSCDCCEventData(idcc, nDDUs, bx, l1a) ) );
00207 
00208     // for every chamber with data, add to a DDU in this DCC Event
00209     for(map<CSCDetId, CSCEventData>::iterator chamberItr = theChamberDataMap.begin();
00210         chamberItr != theChamberDataMap.end(); ++chamberItr)
00211     {
00212       //std::cout<<"inside the pack loop" <<std::endl;
00213       int indexDCC = mapping->slink(chamberItr->first);
00214       if(indexDCC == idcc)
00215       {
00216         //FIXME
00217         std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.find(indexDCC);
00218         if(dccMapItr == dccMap.end())
00219         {
00220           throw cms::Exception("CSCDigiToRaw") << "Bad DCC number:" << indexDCC;
00221         } 
00222         // get ddu id based on ChamberId from mapping
00223 
00224         int dduId = mapping->ddu(chamberItr->first);
00225         int dduSlot = mapping->dduSlot(chamberItr->first);
00226         int dmbId = mapping->dmb(chamberItr->first);
00227         dccMapItr->second.addChamber(chamberItr->second, dduId, dduSlot, dmbId);
00228       }
00229     }
00230   }
00231       
00232   // FIXME: FEDRawData size set to 2*64 to add FED header and trailer
00233   for(std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.begin(); 
00234       dccMapItr != dccMap.end(); ++dccMapItr)
00235   {
00236      boost::dynamic_bitset<> dccBits = dccMapItr->second.pack();
00237      FEDRawData & fedRawData = fed_buffers.FEDData(dccMapItr->first);
00238      fedRawData.resize(dccBits.size());
00239      //fill data with dccEvent
00240      bitset_utilities::bitsetToChar(dccBits, fedRawData.data());
00241      FEDTrailer cscFEDTrailer(fedRawData.data()+(fedRawData.size()-8));
00242      cscFEDTrailer.set(fedRawData.data()+(fedRawData.size()-8), 
00243                        fedRawData.size()/8, 
00244                        evf::compute_crc(fedRawData.data(),fedRawData.size()), 0, 0);
00245   }
00246 }
00247 
00248 
00249 

Generated on Tue Jun 9 17:34:24 2009 for CMSSW by  doxygen 1.5.4