Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:59:46

0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 8; -*-
0002 
0003 #ifndef MATACQTBRAWEVENT_H
0004 #define MATACQTBRAWEVENT_H
0005 
0006 #include <cinttypes>
0007 #include <ctime>
0008 #include <vector>
0009 //replace 1 by 0 to remove XDAQ dependency. In this case it is assumed the machine is little endian.
0010 #if 0
0011 #include "i2o/utils/endian.h"  //from XDAQ
0012 #define UINT32_FROM_LE i2odecodel
0013 #define UINT16_FROM_LE i2odecodes
0014 #define INT16_FROM_LE i2odecodes
0015 
0016 #else  //assuming little endianness of the machine
0017 
0018 #define UINT32_FROM_LE
0019 #define UINT16_FROM_LE
0020 #define INT16_FROM_LE
0021 
0022 #endif
0023 
0024 /** Wrapper for matacq raw event fragments. This class provides the
0025  * method to interpret the data. 
0026  */
0027 class MatacqTBRawEvent {
0028   //typedefs, enums and static constants
0029 public:
0030   enum matacqError_t {
0031     /** Event length is specified both in the data header and the trailer. This
0032      * flags indicates an inconsitency between the two indications.
0033      */
0034     errorLengthConsistency = 1 << 0,
0035     /** Error in data length.
0036      */
0037     errorLength = 1 << 1,
0038     /** Wrong Begin of eevent flag
0039      */
0040     errorWrongBoe = 1 << 2
0041   };
0042 
0043   /* The following types are little-endian encoded types. Use of these types
0044    * for the I20 data block should offer portability to big-endian platforms.
0045    */
0046   //@{
0047   struct uint32le_t {
0048     uint32_t littleEndianInt;
0049     operator uint32_t() const { return UINT32_FROM_LE(littleEndianInt); }
0050   };
0051 
0052   struct uint16le_t {
0053     uint16_t littleEndianInt;
0054     operator uint16_t() const { return UINT16_FROM_LE(littleEndianInt); }
0055   };
0056 
0057   struct int16le_t {
0058     int16_t littleEndianInt;
0059     operator int16_t() const { return INT16_FROM_LE(littleEndianInt); }
0060   };
0061   //@}
0062 
0063   struct ChannelData {
0064     /** Matacq channel number. From 0 to 3.
0065      */
0066     int chId;
0067     /** Number of samples in following array
0068      */
0069     int nSamples;
0070     /** ADC count of time samples. Array of size nSamples
0071      */
0072     const int16le_t* samples;
0073   };
0074 
0075 private:
0076   /** Matacq header data structure
0077    */
0078   struct matacqHeader_t {
0079     uint16le_t version;
0080     unsigned char freqGHz;
0081     unsigned char channelCount;
0082     uint32_t timeStamp;
0083   };
0084 
0085   /** Specification of DAQ header field.
0086    */
0087   struct field32spec_t {
0088     int offset;
0089     unsigned int mask;
0090   };
0091 
0092   /** DAQ header field specifications.
0093    */
0094   static const field32spec_t fov32;
0095   static const field32spec_t fedId32;
0096   static const field32spec_t bxId32;
0097   static const field32spec_t lv132;
0098   static const field32spec_t triggerType32;
0099   static const field32spec_t boeType32;
0100   static const field32spec_t dccLen32;
0101   static const field32spec_t dccErrors32;
0102   static const field32spec_t runNum32;
0103   static const field32spec_t h1Marker32;
0104 
0105   //constructors
0106 public:
0107   /** Constuctor.
0108    * @param dataBuffer pointer to the raw data. Beware the data are not copied,
0109    * therefore the data must be kept valid during the lifetime of the
0110    * constructed object. pData must be aligned at least on 32-bit words.
0111    * @param bufferSize size of the buffer pointed by dataBuffer and containing
0112    * the data. The data themselves are allowed to be smaller than the buffer.
0113    * @throw std::exception if the data cannot be decoded due to data corruption
0114    * or truncation.
0115    */
0116   MatacqTBRawEvent(const unsigned char* dataBuffer, std::size_t bufferSize) { setRawData(dataBuffer, bufferSize); }
0117   //methods
0118 public:
0119   /** Gets the Fed event fragment data format (FOV) field content.
0120    * Currently the FOV is not used for MATACQ.
0121    * Note that matacq data format has its own internal version. See
0122    * #getMatacqDataFormatVersion()
0123    * @return FOV
0124    */
0125   int getFov() const { return read32(daqHeader, fov32); }
0126 
0127   /** Gets the FED ID field contents. Should be 43.
0128    * @return FED ID
0129    */
0130   int getFedId() const { return read32(daqHeader, fedId32); }
0131 
0132   /** Gets the bunch crossing id field contents.
0133    * @return BX id
0134    */
0135   int getBxId() const { return read32(daqHeader, bxId32); }
0136 
0137   /** Gets the LV1 field contents.
0138    * @return LV1 id
0139    */
0140   unsigned getEventId() const { return read32(daqHeader, lv132); }
0141 
0142   /** Gets the trigger type field contents.
0143    * @return trigger type
0144    */
0145   int getTriggerType() const { return read32(daqHeader, triggerType32); }
0146 
0147   /** Gets the beging of event field contents (BOE). Must be 0x5.
0148    * @return BOE
0149    */
0150   int getBoe() const { return read32(daqHeader, boeType32); }
0151 
0152   /** Gets the event length specifies in the "a la DCC" header.
0153    * @return event length
0154    */
0155   unsigned getDccLen() const { return read32(daqHeader, dccLen32); }
0156 
0157   /** Gets the event length specifies in the DAQ trailer
0158    * @return event length
0159    */
0160   unsigned getDaqLen() const { return fragLen; }
0161 
0162   /** Gets the contents of the DCC error field. Currently Not used for Matacq.
0163    * @return dcc error
0164    */
0165   int getDccErrors() const { return read32(daqHeader, dccErrors32); }
0166 
0167   /** Gets the run number field contents.
0168    * @return run number
0169    */
0170   unsigned getRunNum() const { return read32(daqHeader, runNum32); }
0171 
0172   /** Gets the header marker field contents. Must be 1
0173    * @return H1 header marker
0174    */
0175   int getH1Marker() const { return read32(daqHeader, h1Marker32); }
0176 
0177   /** Gets the matcq data format version
0178    * @return data version
0179    */
0180   int getMatacqDataFormatVersion() const { return matacqHeader->version; }
0181 
0182   /** Gets the raw data status. Bitwise OR of the error flags
0183    * defined by matcqError_t
0184    * @return status
0185    */
0186   int32_t getStatus() const { return error; }
0187 
0188   /** Gets the matacq sampling frequency field contents.
0189    * @return sampling frequency in GHz: 1 or 2
0190    */
0191   int getFreqGHz() const { return matacqHeader->freqGHz; }
0192 
0193   /** Gets the matacq channel count field contents.
0194    * @return number of channels
0195    */
0196   int getChannelCount() const { return matacqHeader->channelCount; }
0197 
0198   /** Gets the matacq channel data. Beware that no copy is done and that
0199    * the returned data will be invalidated if the data contains in the buffer
0200    * is modified (see constructor and #setRawData().
0201    * @return matacq channel data.
0202    */
0203   const std::vector<ChannelData>& getChannelData() const { return channelData; }
0204 
0205   /** Gets the data length in number of 64-bit words computed by the data
0206    * parser.
0207    * @return event length
0208    */
0209   int getParsedLen() { return parsedLen; }
0210 
0211   /** Gets the matacq data timestamp field contents: 
0212    * @return acquisition date of the data expressed in number of "elapsed"
0213    * second since the EPOCH as defined in POSIX.1. See time()  standard c
0214    * function.
0215    */
0216   time_t getTimeStamp() const { return matacqHeader->timeStamp; }
0217 
0218   /** Gets the Matacq trigger time.
0219    * @return (t_trig-t_0) in ps, with t_0 the time of the first sample and
0220    * t_trig the trigger time.
0221    */
0222   int getTTrigPs() const { return tTrigPs; }
0223 
0224 private:
0225   /** Help function to decode header content.
0226    * @param data pointer
0227    * @param spec specification of the data field to read
0228    * @return content of data field specified by 'spec'
0229    */
0230   int read32(const uint32le_t* pData, field32spec_t spec) const;
0231 
0232   /** Changes the raw data pointer and updates accordingly this object. 
0233    * @param buffer new pointer to the data buffer. Must be aligned at least
0234    * on 32-bit words.
0235    * @param size of the data buffer.
0236    * @throw std::exception if the data cannot be decoded due to data corruption
0237    * or truncation.
0238    */
0239   void setRawData(const unsigned char* buffer, std::size_t bufferSize);
0240 
0241   //fields
0242 private:
0243   /** Begin Of Event marker
0244    */
0245   int boe;
0246 
0247   /** Bunch crossing Id 
0248    */
0249   int bxId;
0250 
0251   /** Number of matacq channels in the data.
0252    */
0253   int channelCount;
0254 
0255   /** Channel samples
0256    */
0257   std::vector<ChannelData> channelData;
0258 
0259   /** Pointer to the standard CMS DAQ header
0260    */
0261   const uint32le_t* daqHeader;
0262 
0263   /** DCC error field content.
0264    */
0265   int dccErrors;
0266 
0267   /** Event length specified in 'DCC' header
0268    */
0269   unsigned dccLen;
0270 
0271   /** Event id. Actually LV1 ID.
0272    */
0273   unsigned eventId;
0274 
0275   /** Error code or 0 if no error.
0276    */
0277   int32_t error;
0278 
0279   /** FED ID
0280    */
0281   int fedId;
0282 
0283   /** FED data format version
0284    */
0285   int fov;
0286 
0287   /** event fragment length as read in the std DAQ trailer. In 64-bit words
0288    */
0289   int fragLen;
0290 
0291   /** MATACQ sampling frequency in GHz
0292    */
0293   int freqGHz;
0294 
0295   /** header marker
0296    */
0297   int h1Marker;
0298 
0299   /**Matacq header:
0300    */
0301   const matacqHeader_t* matacqHeader;
0302 
0303   /** MATACQ data format internal version
0304    */
0305   int matacqDataFormatVersion;
0306 
0307   /** event lenght computed by the raw data parser
0308    */
0309   int parsedLen;
0310 
0311   /** Pointer to MATACQ samples block
0312    */
0313   uint16le_t* pSamples;
0314 
0315   /** Run number
0316    */
0317   unsigned runNum;
0318 
0319   /** Matacq acquisition time stamp
0320    */
0321   time_t timeStamp;
0322 
0323   /** MATACQ trigger time position in ps
0324    */
0325   int tTrigPs;
0326 
0327   /** Trigger type
0328    */
0329   int triggerType;
0330 };
0331 
0332 #endif  //MATACQRAWEVENT_H not defined