Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-08 03:11:32

0001 /****************************************************************************
0002 *
0003 * This is a part of TOTEM offline software.
0004 * Authors:
0005 *   Jan Kašpar (jan.kaspar@gmail.com)
0006 *
0007 ****************************************************************************/
0008 
0009 #include "FWCore/Framework/interface/stream/EDProducer.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "FWCore/Utilities/interface/InputTag.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 
0015 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0016 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0017 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0018 
0019 #include "DataFormats/CTPPSDigi/interface/TotemTriggerCounters.h"
0020 
0021 #include <string>
0022 #include <iostream>
0023 
0024 class TotemTriggerRawToDigi : public edm::stream::EDProducer<> {
0025 public:
0026   explicit TotemTriggerRawToDigi(const edm::ParameterSet &);
0027   ~TotemTriggerRawToDigi() override;
0028 
0029   void produce(edm::Event &, const edm::EventSetup &) override;
0030 
0031 private:
0032   unsigned int fedId;
0033 
0034   edm::EDGetTokenT<FEDRawDataCollection> fedDataToken;
0035 
0036   /// Process one LoneG frame.
0037   int ProcessLoneGFrame(uint64_t *oBuf, unsigned long size, TotemTriggerCounters &data);
0038 };
0039 
0040 using namespace edm;
0041 using namespace std;
0042 
0043 TotemTriggerRawToDigi::TotemTriggerRawToDigi(const edm::ParameterSet &conf)
0044     : fedId(conf.getParameter<unsigned int>("fedId")) {
0045   fedDataToken = consumes<FEDRawDataCollection>(conf.getParameter<edm::InputTag>("rawDataTag"));
0046 
0047   if (fedId == 0)
0048     throw cms::Exception("TotemTriggerRawToDigi")
0049         << "Invalid FED id for TOTEM trigger. Please specify it through the 'fedId' parameter.";
0050 
0051   produces<TotemTriggerCounters>();
0052 }
0053 
0054 TotemTriggerRawToDigi::~TotemTriggerRawToDigi() {}
0055 
0056 void TotemTriggerRawToDigi::produce(edm::Event &event, const edm::EventSetup &es) {
0057   // raw data handle
0058   edm::Handle<FEDRawDataCollection> rawData;
0059   event.getByToken(fedDataToken, rawData);
0060 
0061   // book output products
0062   TotemTriggerCounters totemTriggerCounters;
0063 
0064   // unpack trigger data
0065   const FEDRawData &data = rawData->FEDData(fedId);
0066   uint64_t *buf = (uint64_t *)data.data();
0067   unsigned int sizeInWords = data.size() / 8;  // bytes -> words
0068   if (data.size() > 0)
0069     ProcessLoneGFrame(buf + 2, sizeInWords - 4, totemTriggerCounters);
0070 
0071   // commit products to event
0072   event.put(make_unique<TotemTriggerCounters>(totemTriggerCounters));
0073 }
0074 
0075 int TotemTriggerRawToDigi::ProcessLoneGFrame(uint64_t *oBuf, unsigned long size, TotemTriggerCounters &td) {
0076   if (size != 20) {
0077     LogError("Totem") << "Error in TotemTriggerRawToDigi::ProcessLoneGFrame > "
0078                       << "Wrong LoneG frame size: " << size << " (shall be 20)." << endl;
0079     return 1;
0080   }
0081 
0082   // buffer mapping: OptoRx buffer --> LoneG buffer
0083   uint64_t buf[5];
0084   for (unsigned int i = 0; i < 5; i++)
0085     buf[i] = 0;
0086 
0087   for (unsigned int i = 0; i < 20; i++) {
0088     int row = i / 4;
0089     int col = i % 4;
0090     buf[row] |= (oBuf[i] & 0xFFFF) << (col * 16);
0091   }
0092 
0093   td.type = (buf[0] >> 56) & 0xF;
0094   td.event_num = (buf[0] >> 32) & 0xFFFFFF;
0095   td.bunch_num = (buf[0] >> 20) & 0xFFF;
0096   td.src_id = (buf[0] >> 8) & 0xFFF;
0097 
0098   td.orbit_num = (buf[1] >> 32) & 0xFFFFFFFF;
0099   td.revision_num = (buf[1] >> 24) & 0xFF;
0100 
0101   td.run_num = (buf[2] >> 32) & 0xFFFFFFFF;
0102   td.trigger_num = (buf[2] >> 0) & 0xFFFFFFFF;
0103 
0104   td.inhibited_triggers_num = (buf[3] >> 32) & 0xFFFFFFFF;
0105   td.input_status_bits = (buf[3] >> 0) & 0xFFFFFFFF;
0106 
0107 #ifdef DEBUG
0108   printf(">> RawDataUnpacker::ProcessLoneGFrame > size = %li\n", size);
0109   printf(
0110       "\ttype = %x, event number = %x, bunch number = %x, id = %x\n", td.type, td.event_num, td.bunch_num, td.src_id);
0111   printf("\torbit number = %x, revision = %x\n", td.orbit_num, td.revision_num);
0112   printf("\trun number = %x, trigger number = %x\n", td.run_num, td.trigger_num);
0113   printf("\tinhibited triggers = %x, input status bits = %x\n", td.inhibited_triggers_num, td.input_status_bits);
0114 #endif
0115 
0116   return 0;
0117 }
0118 
0119 DEFINE_FWK_MODULE(TotemTriggerRawToDigi);