CMS 3D CMS Logo

GctRawToDigi.cc

Go to the documentation of this file.
00001 #include "EventFilter/GctRawToDigi/plugins/GctRawToDigi.h"
00002 
00003 // System headers
00004 #include <vector>
00005 #include <sstream>
00006 #include <iostream>
00007 
00008 // Framework headers
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include "FWCore/Framework/interface/MakerMacros.h"
00011 
00012 // Raw data collection headers
00013 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00014 
00015 // GCT Format Translators
00016 #include "EventFilter/GctRawToDigi/src/GctFormatTranslateMCLegacy.h"
00017 #include "EventFilter/GctRawToDigi/src/GctFormatTranslateV35.h"
00018 #include "EventFilter/GctRawToDigi/src/GctFormatTranslateV38.h"
00019 
00020 // Unpack collections class
00021 #include "EventFilter/GctRawToDigi/src/GctUnpackCollections.h"
00022 
00023 
00024 // Namespace resolution
00025 using std::cout;
00026 using std::endl;
00027 using std::vector;
00028 using std::string;
00029 using std::dec;
00030 using std::hex;
00031 
00032 
00033 GctRawToDigi::GctRawToDigi(const edm::ParameterSet& iConfig) :
00034   inputLabel_(iConfig.getParameter<edm::InputTag>("inputLabel")),
00035   fedId_(iConfig.getParameter<int>("gctFedId")),
00036   hltMode_(iConfig.getParameter<bool>("hltMode")),
00037   unpackSharedRegions_(iConfig.getParameter<bool>("unpackSharedRegions")),
00038   formatVersion_(iConfig.getParameter<unsigned>("unpackerVersion")),
00039   verbose_(iConfig.getUntrackedParameter<bool>("verbose",false)),
00040   formatTranslator_(0),
00041   unpackFailures_(0)
00042 {
00043   LogDebug("GCT") << "GctRawToDigi will unpack FED Id " << fedId_;
00044 
00045   // If the GctFormatTranslate version has been forced from config file, instantiate the relevant one.
00046   /***  THIS OBVIOUSLY STINKS - NEED TO REPLACE WITH SOMETHING BETTER THAN MASSIVE IF-ELSE SOON ***/
00047   if(formatVersion_ == 0) { edm::LogInfo("GCT") << "The required GCT Format Translator will be automatically determined from the first S-Link packet header."; }
00048   else if(formatVersion_ == 1)
00049   {
00050     edm::LogInfo("GCT") << "You have selected to use GctFormatTranslateMCLegacy";
00051     formatTranslator_ = new GctFormatTranslateMCLegacy(hltMode_, unpackSharedRegions_);
00052   }
00053   else if(formatVersion_ == 2)
00054   {
00055     edm::LogInfo("GCT") << "You have selected to use GctFormatTranslateV35";
00056     formatTranslator_ = new GctFormatTranslateV35(hltMode_, unpackSharedRegions_);
00057   }
00058   else if(formatVersion_ == 3)
00059   {
00060     edm::LogInfo("GCT") << "You have selected to use GctFormatTranslateV38";
00061     formatTranslator_ = new GctFormatTranslateV38(hltMode_, unpackSharedRegions_);    
00062   }
00063   else
00064   { 
00065     edm::LogWarning("GCT") << "You have requested a version of GctFormatTranslate that does not exist! Will attempt to auto-detect "
00066                               "the required GCT Format Translator from the first S-Link packet header instead.";
00067   }
00068 
00069   if(hltMode_) { edm::LogInfo("GCT") << "HLT unpack mode selected: HLT unpack optimisations will be used."; }
00070   if(unpackSharedRegions_) { edm::LogInfo("GCT") << "You have selected to unpack shared RCT calo regions - be warned: "
00071                                                     "this is for commissioning purposes only!"; }
00072 
00074   // GCT input collections
00075   produces<L1GctFibreCollection>();
00076   produces<L1CaloEmCollection>();
00077   produces<L1CaloRegionCollection>();
00078 
00079   // GCT internal collections
00080   produces<L1GctInternEmCandCollection>();
00081   produces<L1GctInternJetDataCollection>();
00082   produces<L1GctInternEtSumCollection>();
00083 
00084   // GCT output collections
00085   produces<L1GctEmCandCollection>("isoEm");
00086   produces<L1GctEmCandCollection>("nonIsoEm");
00087   produces<L1GctJetCandCollection>("cenJets");
00088   produces<L1GctJetCandCollection>("forJets");
00089   produces<L1GctJetCandCollection>("tauJets");
00090   produces<L1GctHFBitCountsCollection>();
00091   produces<L1GctHFRingEtSumsCollection>();
00092   produces<L1GctEtTotalCollection>();
00093   produces<L1GctEtHadCollection>();
00094   produces<L1GctEtMissCollection>();
00095   produces<L1GctJetCountsCollection>();  // Deprecated (empty collection still needed by GT)
00096 }
00097 
00098 
00099 GctRawToDigi::~GctRawToDigi()
00100 {
00101   // do anything here that needs to be done at destruction time
00102   // (e.g. close files, deallocate resources etc.)
00103   delete formatTranslator_;
00104 }
00105 
00106 
00107 //
00108 // member functions
00109 //
00110 
00111 // ------------ method called once each job just before starting event loop  ------------
00112 void GctRawToDigi::beginJob(const edm::EventSetup&)
00113 {
00114 }
00115 
00116 
00117 // ------------ method called to produce the data  ------------
00118 void GctRawToDigi::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00119 {
00120   using namespace edm;
00121 
00122   // get raw data collection
00123   edm::Handle<FEDRawDataCollection> feds;
00124   iEvent.getByLabel(inputLabel_, feds);
00125   const FEDRawData& gctRcd = feds->FEDData(fedId_);
00126  
00127   LogDebug("GCT") << "Upacking FEDRawData of size " << std::dec << gctRcd.size();
00128 
00129   // Instantiate all the collections the unpacker needs; puts them in event when this object goes out of scope.
00130   std::auto_ptr<GctUnpackCollections> colls(new GctUnpackCollections(iEvent, hltMode_));
00131   
00132   // do a simple check of the raw data - this will detect empty events
00133   if(gctRcd.size() < 16)
00134   {
00135     LogDebug("GCT") << "Cannot unpack: empty/invalid GCT raw data (size = "
00136                     << gctRcd.size() << "). Returning empty collections!";
00137     ++unpackFailures_;
00138   }
00139   else{ unpack(gctRcd, iEvent, colls.get()); }
00140 }
00141 
00142 
00143 void GctRawToDigi::unpack(const FEDRawData& d, edm::Event& e, GctUnpackCollections * const colls)
00144 {
00145   const unsigned char * data = d.data();  // The 8-bit wide raw-data array.  
00146 
00147   // If no format translator yet set, need to auto-detect from header.
00148   if(!formatTranslator_)
00149   {
00150     // If auto format detection fails, we have no concrete format
00151     // translator instantiated... so bail from event.
00152     if(!autoDetectRequiredFormatTranslator(data)) { return; }
00153   }
00154 
00155   // We should now have a valid formatTranslator pointer  
00156   formatTranslator_->setUnpackCollections(colls);
00157 
00158   // Data offset - starts at 16 as there is a 64-bit S-Link header followed
00159   // by a 64-bit software-controlled header (for pipeline format version
00160   // info that is not yet used).
00161   unsigned dPtr = 16;
00162 
00163   const unsigned dEnd = d.size() - 8; // End of payload is at (packet size - final slink header)
00164 
00165   GctBlockHeaderCollection bHdrs; // Vector for storing block headers for verbosity print-out.
00166 
00167   // read blocks
00168   for (unsigned nb=0; dPtr<dEnd; ++nb)
00169   {
00170     if(nb >= MAX_BLOCKS) { LogDebug("GCT") << "Reached block limit - bailing out from this event!"; ++unpackFailures_; break; }
00171   
00172     // read block header
00173     GctBlockHeader blockHeader = formatTranslator_->generateBlockHeader(&data[dPtr]);
00174   
00175     // unpack the block; dPtr+4 is to get to the block data.
00176     if(!formatTranslator_->convertBlock(&data[dPtr+4], blockHeader)) // Record if we had an unpack problem then skip rest of event.
00177     {
00178       LogDebug("GCT") << "Encountered block unpack error - bailing out from this event!";
00179       ++unpackFailures_; break;
00180     } 
00181 
00182     // advance pointer
00183     dPtr += 4*(blockHeader.blockLength()*blockHeader.nSamples()+1); // *4 because blockLen is in 32-bit words, +1 for header
00184 
00185     // If verbose, store the header in vector.
00186     if(verbose_) { bHdrs.push_back(blockHeader); }
00187   }
00188 
00189   // dump summary in verbose mode
00190   if(verbose_) { doVerboseOutput(bHdrs, colls); }
00191 }
00192 
00193 
00194 bool GctRawToDigi::autoDetectRequiredFormatTranslator(const unsigned char * d)
00195 {
00196   LogDebug("GCT") << "About to auto-detect the required format translator from the firmware version header.";
00197     
00198   const uint32_t * p32 = reinterpret_cast<const uint32_t *>(d);
00199   unsigned firmwareHeader = p32[2];
00200 
00201   /***  THIS OBVIOUSLY STINKS - NEED TO REPLACE WITH SOMETHING BETTER THAN MASSIVE IF-ELSE SOON ***/
00202   if( firmwareHeader >= 25 && firmwareHeader <= 35 )
00203   {
00204     edm::LogInfo("GCT") << "Firmware Version V" << firmwareHeader << " detected: GctFormatTranslateV" << firmwareHeader << " will be used to unpack.";
00205     formatTranslator_ = new GctFormatTranslateV35(hltMode_, unpackSharedRegions_);
00206     return true;
00207   }
00208   else if( firmwareHeader == 38 )
00209   {
00210     edm::LogInfo("GCT") << "Firmware Version V" << firmwareHeader << " detected: GctFormatTranslateV" << firmwareHeader << " will be used to unpack.";
00211     formatTranslator_ = new GctFormatTranslateV38(hltMode_, unpackSharedRegions_);
00212     return true;
00213   }
00214   else if( firmwareHeader == 0x00000000 )
00215   {
00216     edm::LogInfo("GCT") << "Legacy Monte-Carlo data detected: GctFormatTranslateMCLegacy will be used to unpack.";
00217     formatTranslator_ = new GctFormatTranslateMCLegacy(hltMode_, unpackSharedRegions_);
00218     return true;
00219   }
00220   else if(firmwareHeader == 0xdeadffff) { /* Driver detected unknown firmware version. L1TriggerError code? */ }
00221   else if( firmwareHeader == 0xaaaaaaaa) { /* Before driver firmware version checks implemented. L1TriggerError code?  */ }
00222   else { /* Totally unknown firmware header. L1TriggerError code?  */ }
00223   
00224   LogDebug("GCT") << "Failed to determine unpacker to use from the firmware version header! "
00225                      "(firmware header = 0x" << hex << firmwareHeader << dec << ")";
00226 
00227   ++unpackFailures_;
00228   return false;
00229 }
00230 
00231 void GctRawToDigi::doVerboseOutput(const GctBlockHeaderCollection& bHdrs, const GctUnpackCollections * const colls) const
00232 {
00233   std::ostringstream os;
00234   os << "Found " << bHdrs.size() << " GCT block headers" << endl;
00235   for (unsigned i=0, size = bHdrs.size(); i < size; ++i)
00236   {
00237     os << "GCT Raw Data Block : " << formatTranslator_->getBlockDescription(bHdrs[i]) << " : " << bHdrs[i] << endl;
00238   }
00239   os << *colls << endl;
00240   edm::LogVerbatim("GCT") << os.str();
00241 }
00242 
00243 // ------------ method called once each job just after ending the event loop  ------------
00244 void GctRawToDigi::endJob()
00245 {
00246   if(unpackFailures_ > 0)
00247   {
00248     edm::LogError("GCT") << "GCT unpacker encountered " << unpackFailures_
00249                          << " unpack errors in total during this job!";
00250   }  
00251 }
00252 
00254 DEFINE_FWK_MODULE(GctRawToDigi);

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