Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:23

0001 /** \file
0002  *
0003  *  $Date: 2010/02/03 16:58:24 $
0004  *  $Revision: 1.10 $
0005  *  \author M. Zanetti
0006  */
0007 
0008 #include <IORawData/DTCommissioning/plugins/DTSpyReader.h>
0009 #include <IORawData/DTCommissioning/plugins/DTFileReaderHelpers.h>
0010 
0011 #include <DataFormats/FEDRawData/interface/FEDHeader.h>
0012 #include <DataFormats/FEDRawData/interface/FEDTrailer.h>
0013 #include <DataFormats/FEDRawData/interface/FEDNumbering.h>
0014 
0015 #include "DataFormats/Provenance/interface/EventID.h"
0016 #include <DataFormats/Provenance/interface/Timestamp.h>
0017 
0018 #include <DataFormats/FEDRawData/interface/FEDRawDataCollection.h>
0019 
0020 #include <FWCore/ParameterSet/interface/ParameterSet.h>
0021 
0022 #include <string>
0023 #include <iosfwd>
0024 #include <iostream>
0025 #include <algorithm>
0026 #include <cstdio>
0027 
0028 using namespace std;
0029 using namespace edm;
0030 
0031 DTSpyReader::DTSpyReader(const edm::ParameterSet& pset) : runNumber(1), eventNumber(0) {
0032   // instatiating Sandro's spy (My name is Bond, Sandro Bond)
0033   mySpy = new DTSpy();
0034 
0035   /// connecting to XDAQ note ("0.0.0.0" = localhost)
0036   string connectionParameters = pset.getUntrackedParameter<string>("connectionParameters");
0037   mySpy->Connect(connectionParameters.c_str(), 10000);
0038 
0039   cout << endl;
0040   cout << "DT Local DAQ online spy. Connected to IP " << connectionParameters.c_str()
0041        << ". Waiting for the data to be flushed" << endl;
0042   cout << endl;
0043 
0044   debug = pset.getUntrackedParameter<bool>("debug", false);
0045   dduID = pset.getUntrackedParameter<int32_t>("dduID", 770);  // NOT needed
0046 
0047   produces<FEDRawDataCollection>();
0048 }
0049 
0050 DTSpyReader::~DTSpyReader() { delete mySpy; }
0051 
0052 int DTSpyReader::fillRawData(Event& e,
0053                              //Timestamp& tstamp,
0054                              FEDRawDataCollection*& data) {
0055   EventID eID = e.id();
0056 
0057   // ask for a new buffer
0058   mySpy->getNextBuffer();
0059 
0060   // get the pointer to the beginning of the buffer
0061   const char* rawDTData = mySpy->getEventPointer();
0062 
0063   const uint32_t* rawDTData32 = reinterpret_cast<const uint32_t*>(rawDTData);
0064 
0065   // instantiate the FEDRawDataCollection
0066   data = new FEDRawDataCollection();
0067 
0068   vector<uint64_t> eventData;
0069   uint64_t word = 0;
0070   int wordCount = 0;
0071   int wordCountCheck = 0;
0072 
0073   bool headerTag = false;
0074   bool dataTag = true;
0075 
0076   // Advance at long-word steps until the trailer is reached.
0077   // Skipped whatever else is in the buffer (e.g. another event..)
0078   while (!isTrailer(word, dataTag, wordCount)) {
0079     // dma gets 4 32-bits words and create a 64 bit one
0080     word = dmaUnpack(rawDTData32, dataTag);
0081 
0082     // look for the DDU header
0083     if (isHeader(word, dataTag))
0084       headerTag = true;
0085 
0086     // check whether the first word is a DDU header
0087     if (wordCountCheck > 0 && !headerTag && debug)
0088       cout << "[DTSpyReader]: WARNING: header still not found!!" << endl;
0089 
0090     // from now on fill the eventData with the ROS data
0091     if (headerTag) {
0092       // swapping only the 32 bits words
0093       if (dataTag)
0094         swap(word);
0095       // WARNING also the ddu status words have been swapped!
0096       // Control the correct interpretation in DDUUnpacker
0097 
0098       eventData.push_back(word);
0099       wordCount++;
0100     }
0101 
0102     // advancing by 4 32-bits words
0103     rawDTData32 += 4;
0104 
0105     // counting the total number of group of 128 bits (=4*32) in the buffer
0106     wordCountCheck++;
0107   }
0108 
0109   // Setting the Event ID
0110   runNumber = mySpy->getRunNo();
0111   eID = EventID(runNumber, 1U, eventNumber);
0112 
0113   // eventDataSize = (Number Of Words)* (Word Size)
0114   int eventDataSize = eventData.size() * dduWordLength;
0115 
0116   if (debug)
0117     cout << " DDU ID = " << dduID << endl;
0118 
0119   FEDRawData& fedRawData = data->FEDData(dduID);
0120   fedRawData.resize(eventDataSize);
0121 
0122   copy(reinterpret_cast<unsigned char*>(&eventData[0]),
0123        reinterpret_cast<unsigned char*>(&eventData[0]) + eventDataSize,
0124        fedRawData.data());
0125 
0126   mySpy->setlastPointer((char*)rawDTData32);
0127 
0128   return true;
0129 }
0130 
0131 void DTSpyReader::produce(Event& e, EventSetup const& es) {
0132   edm::Handle<FEDRawDataCollection> rawdata;
0133   FEDRawDataCollection* fedcoll = nullptr;
0134   fillRawData(e, fedcoll);
0135   std::unique_ptr<FEDRawDataCollection> bare_product(fedcoll);
0136   e.put(std::move(bare_product));
0137 }
0138 
0139 void DTSpyReader::swap(uint64_t& word) {
0140   twoNibble64* newWorld = reinterpret_cast<twoNibble64*>(&word);
0141 
0142   uint32_t msBits_tmp = newWorld->msBits;
0143   newWorld->msBits = newWorld->lsBits;
0144   newWorld->lsBits = msBits_tmp;
0145 }
0146 
0147 uint64_t DTSpyReader::dmaUnpack(const uint32_t* dmaData, bool& isData) {
0148   uint32_t unpackedData[2] = {0, 0};
0149   // adjust 4 32-bits words  into 2 32-bits words
0150   unpackedData[0] |= dmaData[3] & 0x3ffff;
0151   unpackedData[0] |= (dmaData[2] << 18) & 0xfffc0000;
0152   unpackedData[1] |= (dmaData[2] >> 14) & 0x0f;
0153   unpackedData[1] |= (dmaData[1] << 4) & 0x3ffff0;
0154   unpackedData[1] |= (dmaData[0] << 22) & 0xffc00000;
0155 
0156   isData = (dmaData[0] >> 10) & 0x01;
0157 
0158   //printf ("Lower part: %x \n", unpackedData[0]);
0159   //printf ("Upper part: %x \n", unpackedData[1]);
0160 
0161   // push_back to a 64 word
0162   uint64_t dduWord = (uint64_t(unpackedData[1]) << 32) | unpackedData[0];
0163 
0164   return dduWord;
0165 }
0166 
0167 bool DTSpyReader::isHeader(uint64_t word, bool dataTag) {
0168   bool it_is = false;
0169   FEDHeader candidate(reinterpret_cast<const unsigned char*>(&word));
0170   if (candidate.check()) {
0171     // if ( candidate.check() && !dataTag) {
0172     it_is = true;
0173     dduID = candidate.sourceID();
0174     eventNumber++;
0175   }
0176   return it_is;
0177 }
0178 
0179 bool DTSpyReader::isTrailer(uint64_t word, bool dataTag, unsigned int wordCount) {
0180   bool it_is = false;
0181   FEDTrailer candidate(reinterpret_cast<const unsigned char*>(&word));
0182   if (candidate.check()) {
0183     //  if ( candidate.check() && !dataTag) {
0184     if (wordCount == candidate.fragmentLength())
0185       it_is = true;
0186   }
0187   return it_is;
0188 }