Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:33

0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 8; -*-
0002 
0003 #ifndef MATACQRAWEVENT_H
0004 #define MATACQRAWEVENT_H
0005 
0006 #include <cinttypes>
0007 #include <cstddef>
0008 #include <vector>
0009 
0010 //replace 1 by 0 to remove XDAQ dependency. In this case it is assumed the machine is little endian.
0011 #if 0
0012 #include "i2o/utils/endian.h"  //from XDAQ
0013 #define UINT32_FROM_LE i2odecodel
0014 #define UINT16_FROM_LE i2odecodes
0015 #define INT16_FROM_LE i2odecodes
0016 
0017 #else  //assuming little endianness of the machine
0018 
0019 #define UINT32_FROM_LE
0020 #define UINT16_FROM_LE
0021 #define INT16_FROM_LE
0022 
0023 #endif
0024 
0025 #include <sys/time.h>  //for timeval definition
0026 
0027 /** Wrapper for matacq raw event fragments. This class provides the
0028  * method to interpret the data. 
0029  */
0030 class MatacqRawEvent {
0031   //typedefs, enums and static constants
0032 public:
0033   enum matacqError_t {
0034     /** Event length is specified both in the data header and the trailer. This
0035      * flags indicates an inconsitency between the two indications.
0036      */
0037     errorLengthConsistency = 1 << 0,
0038     /** Error in data length.
0039      */
0040     errorLength = 1 << 1,
0041     /** Wrong Begin of event flag
0042      */
0043     errorWrongBoe = 1 << 2
0044   };
0045 
0046   /* The following types are little-endian encoded types. Use of these types
0047    * for the I20 data block should offer portability to big-endian platforms.
0048    */
0049   //@{
0050   struct uint32le_t {
0051     uint32_t littleEndianInt;
0052     operator uint32_t() const { return UINT32_FROM_LE(littleEndianInt); }
0053   };
0054 
0055   struct uint16le_t {
0056     uint16_t littleEndianInt;
0057     operator uint16_t() const { return UINT16_FROM_LE(littleEndianInt); }
0058   };
0059 
0060   struct int16le_t {
0061     int16_t littleEndianInt;
0062     operator int16_t() const { return INT16_FROM_LE(littleEndianInt); }
0063   };
0064   //@}
0065 
0066   struct ChannelData {
0067     /** Matacq channel number. From 0 to 3.
0068      */
0069     int chId;
0070     /** Number of samples in following array
0071      */
0072     int nSamples;
0073     /** ADC count of time samples. Array of size nSamples
0074      */
0075     const int16le_t* samples;
0076   };
0077 
0078 private:
0079   /** Matacq header data structure
0080    */
0081   //   struct matacqHeader_t{
0082   //     uint16_t version;
0083   //     unsigned char freqGHz;
0084   //     unsigned char channelCount;
0085   //     uint32_t orbitId;
0086   //     unsigned char trigRec;
0087   //     uint16_t postTrig;
0088   //     uint16_t vernier[4];
0089   //     uint32_t timeStamp;
0090   //   };
0091 
0092   /** Specification of DAQ header field.
0093    */
0094   struct field32spec_t {
0095     int offset;
0096     unsigned int mask;
0097   };
0098 
0099   //@{
0100   /** DAQ header field specifications.
0101    */
0102   static const field32spec_t fov32;
0103   static const field32spec_t fedId32;
0104   static const field32spec_t bxId32;
0105   static const field32spec_t lv132;
0106   static const field32spec_t triggerType32;
0107   static const field32spec_t boeType32;
0108   static const field32spec_t dccLen32;
0109   static const field32spec_t dccErrors32;
0110   static const field32spec_t runNum32;
0111   static const field32spec_t h1Marker32;
0112   //@}
0113 
0114   //@{
0115   /** Matacq header field specifications.
0116    */
0117   static const field32spec_t formatVersion32;
0118   static const field32spec_t freqGHz32;
0119   static const field32spec_t channelCount32;
0120   static const field32spec_t timeStamp32;
0121   static const field32spec_t tTrigPs32;
0122   static const field32spec_t orbitId32;
0123   static const field32spec_t trigRec32;
0124   static const field32spec_t postTrig32;
0125   static const field32spec_t vernier0_32;
0126   static const field32spec_t vernier1_32;
0127   static const field32spec_t vernier2_32;
0128   static const field32spec_t vernier3_32;
0129   static const field32spec_t timeStampMicroSec32;
0130 
0131   static const field32spec_t laserPower32;
0132   static const field32spec_t attenuation_dB32;
0133   static const field32spec_t emtcPhase32;
0134   static const field32spec_t emtcDelay32;
0135   static const field32spec_t delayA32;
0136   static const field32spec_t dccId32;
0137   static const field32spec_t color32;
0138   static const field32spec_t trigType32;
0139   static const field32spec_t side32;
0140 
0141   //@}
0142 
0143   //constructors
0144 public:
0145   /** Constuctor.
0146    * @param dataBuffer pointer to the raw data. Beware the data are not copied,
0147    * therefore the data must be kept valid during the lifetime of the
0148    * constructed object. pData must be aligned at least on 32-bit words.
0149    * @param bufferSize size of the buffer pointed by dataBuffer and containing
0150    * the data. The data themselves are allowed to be smaller than the buffer.
0151    * @throw std::exception if the data cannot be decoded due to data corruption
0152    * or truncation.
0153    */
0154   MatacqRawEvent(const unsigned char* dataBuffer, size_t bufferSize) : vernier(std::vector<int>(4)) {
0155     setRawData(dataBuffer, bufferSize);
0156   }
0157   //methods
0158 public:
0159   /** Gets the Fed event fragment data format (FOV) field content.
0160    * Currently the FOV is not used for MATACQ.
0161    * Note that matacq data format has its own internal version. See
0162    * #getMatacqDataFormatVersion()
0163    * @return FOV
0164    */
0165   int getFov() const { return read32(daqHeader, fov32); }
0166 
0167   /** Gets the FED ID field contents. Should be 655.
0168    * @return FED ID
0169    */
0170   int getFedId() const { return read32(daqHeader, fedId32); }
0171 
0172   /** Gets the bunch crossing id field contents.
0173    * @return BX id
0174    */
0175   int getBxId() const { return read32(daqHeader, bxId32); }
0176 
0177   /** Gets the LV1 field contents.
0178    * @return LV1 id
0179    */
0180   unsigned getEventId() const { return read32(daqHeader, lv132); }
0181 
0182   /** Gets the trigger type field contents.
0183    * @return trigger type
0184    */
0185   int getTriggerType() const { return read32(daqHeader, triggerType32); }
0186 
0187   /** Gets the beging of event field contents (BOE). Must be 0x5.
0188    * @return BOE
0189    */
0190   int getBoe() const { return read32(daqHeader, boeType32); }
0191 
0192   /** Gets the event length specifies in the "a la DCC" header.
0193    * @return event length
0194    */
0195   unsigned getDccLen() const { return read32(daqHeader, dccLen32); }
0196 
0197   /** Gets the event length specifies in the DCC-type header of a matacq event.
0198     * @param data buffer. Needs to contains at least the 3 first 32-bit words
0199     * of the event.
0200     * @param buffer size
0201     * @return event length, 0xFFFFFFFF if failed to retrieve dcc length
0202     */
0203   static unsigned getDccLen(unsigned char* data, size_t size) {
0204     if (size < (unsigned)(dccLen32.offset + 1) * 4)
0205       return (unsigned)-1;
0206     return read32((uint32le_t*)data, dccLen32);
0207   }
0208 
0209   /** Gets the orbit id from the header of a matacq event. Data format
0210     * of the event must be >=3.
0211     * @param data buffer. Needs to contains at least the 8 first 32-bit words
0212     * of the event.
0213     * @param buffer size
0214     * @return event length, 0xFFFFFFFF if failed to retrieve dcc length
0215     */
0216   static unsigned getOrbitId(unsigned char* data, size_t size) {
0217     if (size < (unsigned)(orbitId32.offset + 1) * 8)
0218       return (unsigned)-1;
0219     return read32((uint32le_t*)data, orbitId32);
0220   }
0221 
0222   /** Gets the run number from the  header of a matacq event.
0223     * @param data buffer. Needs to contains at least the 4 first 32-bit words
0224     * of the event.
0225     * @param buffer size
0226     * @return event length, 0xFFFFFFFF if failed to retrieve dcc length
0227     */
0228   static unsigned getRunNum(unsigned char* data, size_t size) {
0229     if (size < (unsigned)(runNum32.offset + 1) * 8)
0230       return (unsigned)-1;
0231     return read32((uint32le_t*)data, runNum32);
0232   }
0233 
0234   /** Gets the event length specifies in the DAQ trailer
0235    * @return event length
0236    */
0237   unsigned getDaqLen() const { return fragLen; }
0238 
0239   /** Gets the contents of the DCC error field. Currently Not used for Matacq.
0240    * @return dcc error
0241    */
0242   int getDccErrors() const { return read32(daqHeader, dccErrors32); }
0243 
0244   /** Gets the run number field contents.
0245    * @return run number
0246    */
0247   unsigned getRunNum() const { return read32(daqHeader, runNum32); }
0248 
0249   /** Gets the header marker field contents. Must be 1
0250    * @return H1 header marker
0251    */
0252   int getH1Marker() const { return read32(daqHeader, h1Marker32); }
0253 
0254   /** Gets the matcq data format version
0255    * @return data version
0256    */
0257   int getMatacqDataFormatVersion() const { return matacqDataFormatVersion; }
0258 
0259   /** Gets the raw data status. Bitwise OR of the error flags
0260    * defined by matcqError_t
0261    * @return status
0262    */
0263   int32_t getStatus() const { return error; }
0264 
0265   /** Gets the matacq sampling frequency field contents.
0266    * @return sampling frequency in GHz: 1 or 2
0267    */
0268   int getFreqGHz() const { return /*matacqHeader->*/ freqGHz; }
0269 
0270   /** Gets the matacq channel count field contents.
0271    * @return number of channels
0272    */
0273   int getChannelCount() const { return /*matacqHeader->*/ channelCount; }
0274 
0275   /** Gets the matacq channel data. Beware that no copy is done and that
0276    * the returned data will be invalidated if the data contains in the buffer
0277    * is modified (see constructor and #setRawData().
0278    * @return matacq channel data.
0279    */
0280   const std::vector<ChannelData>& getChannelData() const { return channelData; }
0281 
0282   /** Gets the data length in number of 64-bit words computed by the data
0283    * parser.
0284    * @return event length
0285    */
0286   int getParsedLen() { return parsedLen; }
0287 
0288   /** Gets the matacq data timestamp field contents: 
0289    * @return acquisition date of the data expressed in number of "elapsed"
0290    * second since the EPOCH as defined in POSIX.1. See time()  standard c
0291    * function.
0292    */
0293   time_t getTimeStamp() const { return /*matacqHeader->*/ timeStamp.tv_sec; }
0294 
0295   /** Gets the matacq data timestamp with fine granularity (89.1us) 
0296    * @return acquisition date of the data expressed in number of "elapsed"
0297    * second and microseconds since the EPOCH as defined in POSIX.1.
0298    * See time() standard c function and gettimeofday UNIX function.
0299    */
0300   void getTimeStamp(struct timeval& t) const { t = timeStamp; }
0301 
0302   /** Gets the Matacq trigger time.
0303    * @return (t_trig-t_0) in ps, with t_0 the time of the first sample and
0304    * t_trig the trigger time.
0305    */
0306   int getTTrigPs() const { return tTrigPs; }
0307 
0308   /** Gets the LHC orbit ID of the event
0309    * Available only for Matacq data format version >=3 and for P5 data.
0310    * @return the LHC orbit ID
0311    */
0312   uint32_t getOrbitId() const { return orbitId; }
0313 
0314   /** Gets the Trig Rec value (see Matacq documentation)
0315    * Available only for Matacq data format version >=3.
0316    * @return the Trig Rec value
0317    */
0318   int getTrigRec() const { return trigRec; }
0319 
0320   /** Posttrig value (see Matacq documentation).
0321    * Available only for Matacq data format version >=3.
0322    */
0323   int getPostTrig() const { return postTrig; }
0324 
0325   /** Vernier values (see Matacq documentation)
0326    * Available only for Matacq data format version >=3.
0327    */
0328   std::vector<int> getVernier() const { return vernier; }
0329 
0330   /** "Delay A" setting of laser delay box in ns.
0331    */
0332   int getDelayA() const { return delayA; }
0333 
0334   /**  WTE-to-Laser delay of EMTC in LHC clock unit.
0335    */
0336   int getEmtcDelay() const { return emtcDelay; }
0337 
0338   /** EMTC laser phase in 1/8th LHC clock unit.
0339    */
0340   int getEmtcPhase() const { return emtcPhase; }
0341 
0342   /**  Logarithmic attenuator setting in -10dB unit. Between 0 and
0343    *  5*(-10dB), 0xF if unknown.
0344    */
0345   int getAttenuation_dB() const { return attenuation_dB; }
0346 
0347   /** Laser power in percents (set with the linear attenuator).
0348    */
0349   int getLaserPower() const { return laserPower; }
0350 
0351 private:
0352   /** Help function to decode header content.
0353    * @param data pointer
0354    * @param spec specification of the data field to read
0355    * @param ovfTrans switch of overflow translation. If true the
0356    * MSB of the data field is interpreted as an overflow bit. If
0357    * it is set, then -1 is returned.
0358    * @return content of data field specified by 'spec'
0359    */
0360   static int read32(const uint32le_t* pData, field32spec_t spec, bool ovfTrans = false);
0361 
0362   //   /** Help function to get the maximum value of a data field
0363   //    * @param spec32 data field specification
0364   //    * @return maximum value
0365   //    */
0366   //   int max32(field32spec_t spec32) const;
0367 
0368   /** Changes the raw data pointer and updates accordingly this object. 
0369    * @param buffer new pointer to the data buffer. Must be aligned at least
0370    * on 32-bit words.
0371    * @param size of the data buffer.
0372    * @throw std::exception if the data cannot be decoded due to data corruption
0373    * or truncation.
0374    */
0375   void setRawData(const unsigned char* buffer, size_t bufferSize);
0376 
0377   //fields
0378 private:
0379   /** Begin Of Event marker
0380    */
0381   int boe;
0382 
0383   /** Bunch crossing Id 
0384    */
0385   int bxId;
0386 
0387   /** Number of matacq channels in the data.
0388    */
0389   int channelCount;
0390 
0391   /** Channel samples
0392    */
0393   std::vector<ChannelData> channelData;
0394 
0395   /** Pointer to the standard CMS DAQ header
0396    */
0397   const uint32le_t* daqHeader;
0398 
0399   /** DCC error field content.
0400    */
0401   int dccErrors;
0402 
0403   /** Event length specified in 'DCC' header
0404    */
0405   unsigned dccLen;
0406 
0407   /** Event id. Actually LV1 ID.
0408    */
0409   unsigned eventId;
0410 
0411   /** Error code or 0 if no error.
0412    */
0413   int32_t error;
0414 
0415   /** FED ID
0416    */
0417   int fedId;
0418 
0419   /** FED data format version
0420    */
0421   int fov;
0422 
0423   /** event fragment length as read in the std DAQ trailer. In 64-bit words
0424    */
0425   int fragLen;
0426 
0427   /** MATACQ sampling frequency in GHz
0428    */
0429   int freqGHz;
0430 
0431   /** header marker
0432    */
0433   int h1Marker;
0434 
0435   /**Matacq header:
0436    */
0437   //  matacqHeader_t* matacqHeader;
0438 
0439   /** MATACQ data format internal version
0440    */
0441   int matacqDataFormatVersion;
0442 
0443   /** event lenght computed by the raw data parser
0444    */
0445   int parsedLen;
0446 
0447   /** Pointer to MATACQ samples block
0448    */
0449   uint16le_t* pSamples;
0450 
0451   /** Run number
0452    */
0453   unsigned runNum;
0454 
0455   /** Matacq acquisition time stamp
0456    */
0457   struct timeval timeStamp;
0458 
0459   /** MATACQ trigger time position in ps
0460    */
0461   int tTrigPs;
0462 
0463   /** Trigger type
0464    */
0465   int triggerType;
0466 
0467   /* LHC orbit ID
0468    */
0469   uint32_t orbitId;
0470 
0471   /** Trig Rec value (see Matacq documentation)
0472    */
0473   int trigRec;
0474 
0475   /** Posttrig value (see Matacq documentation)
0476    */
0477   int postTrig;
0478 
0479   /** Vernier values (see Matacq documentation)
0480    */
0481   std::vector<int> vernier;
0482 
0483   /** "Delay A" setting of laser delay box in ns.
0484    */
0485   int delayA;
0486 
0487   /**  WTE-to-Laser delay of EMTC in LHC clock unit.
0488    */
0489   int emtcDelay;
0490 
0491   /** EMTC laser phase in 1/8th LHC clock unit.
0492    */
0493   int emtcPhase;
0494 
0495   /**  Logarithmic attenuator setting in -10dB unit. Between 0 and
0496    *  5*(-10dB), 0xF if unknown.
0497    */
0498   int attenuation_dB;
0499 
0500   /** Laser power in percents (set with the linear attenuator).
0501    */
0502   int laserPower;
0503 };
0504 
0505 #endif  //MATACQRAWEVENT_H not defined