Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:21

0001 #ifndef FUN4ALLRAW_MICROMEGASBCOMATCHINGINFORMATION_V2_H
0002 #define FUN4ALLRAW_MICROMEGASBCOMATCHINGINFORMATION_V2_H
0003 
0004 /*!
0005  * \file MicromegasBcoMatchingInformation_v2.h
0006  * \author Hugo Pereira Da Costa <hugo.pereira-da-costa@cea.fr>
0007  * \brief handles matching between GTM and FEE BCO clock
0008  */
0009 
0010 #include <cstdint>
0011 #include <list>
0012 #include <optional>
0013 #include <set>
0014 #include <vector>
0015 #include <utility>
0016 
0017 class MicromegasBcoMatchingInformation_v2
0018 {
0019  public:
0020 
0021   //! constructor
0022   MicromegasBcoMatchingInformation_v2() = default;
0023 
0024   //! gtm data
0025   class gtm_payload
0026   {
0027     public:
0028 
0029     uint16_t pkt_type = 0;
0030     bool is_endat = false;
0031     bool is_lvl1 = false;
0032     bool is_modebit = false;
0033     uint64_t bco = 0;
0034     uint32_t lvl1_count = 0;
0035     uint32_t endat_count = 0;
0036     uint64_t last_bco = 0;
0037     uint8_t modebits = 0;
0038     uint8_t userbits = 0;
0039 
0040   };
0041 
0042   //! fee data
0043   class fee_payload
0044   {
0045     public:
0046     uint16_t adc_length = 0;
0047     uint16_t data_parity = 0;
0048     uint16_t sampa_address = 0;
0049     uint16_t sampa_channel = 0;
0050     uint16_t channel = 0;
0051     uint16_t type = 0;
0052     uint16_t user_word = 0;
0053     uint32_t bx_timestamp = 0;
0054     uint16_t data_crc = 0;
0055     uint16_t calc_crc = 0;
0056 
0057     std::vector< std::pair< uint16_t , std::vector<uint16_t> > > waveforms;
0058   };
0059 
0060   //!@name accessor
0061   //@{
0062 
0063   //! verbosity
0064   int verbosity() const
0065   {
0066     return m_verbosity;
0067   }
0068 
0069   //! true if matching information is verified
0070   /**
0071    * matching information is verified if at least one match
0072    * between gtm_bco and fee_bco is found
0073    */
0074   bool is_verified() const
0075   {
0076     return m_verified_from_modebits || m_verified_from_data;
0077   }
0078 
0079   //! get predicted fee_bco from gtm_bco
0080   std::optional<uint32_t> get_predicted_fee_bco(uint64_t) const;
0081 
0082   //! multiplier
0083   static bool gtm_clock_multiplier_is_set()
0084   {
0085     return m_multiplier_is_set;
0086   }
0087 
0088   //! multiplier
0089   static double get_gtm_clock_multiplier()
0090   {
0091     return m_multiplier;
0092   }
0093 
0094   //! get adjusted multiplier
0095   double get_adjusted_multiplier() const;
0096 
0097   //! print gtm bco information
0098   void print_gtm_bco_information() const;
0099 
0100   //! get first gtm bco
0101   unsigned int get_fee_bco_first() const
0102   { return m_fee_bco_first; }
0103 
0104   //! get first gtm bco
0105   uint64_t get_gtm_bco_first() const
0106   { return m_gtm_bco_first; }
0107 
0108   //! get last gtm bco
0109   uint64_t get_gtm_bco_last() const
0110   { return m_gtm_bco_list.empty() ? 0:*m_gtm_bco_list.rbegin(); }
0111 
0112   //@}
0113 
0114   //!@name modifiers
0115   //@{
0116 
0117   //! verbosity
0118   void set_verbosity(int value)
0119   {
0120     m_verbosity = value;
0121   }
0122 
0123   /// set gtm clock multiplier
0124   static void set_gtm_clock_multiplier(double value)
0125   {
0126     m_multiplier_is_set = true;
0127     m_multiplier = value;
0128   }
0129 
0130   /// enable multiplier adjustment
0131   static void set_enable_multiplier_adjustment( bool value )
0132   { m_multiplier_adjustment_enabled = value; }
0133 
0134   /// muliplier adjustment count
0135   /** controls how often the gtm multiplier is automatically adjusted */
0136   static void set_max_multiplier_adjustment_count( unsigned int value )
0137   {
0138     m_max_multiplier_adjustment_count = value;
0139   }
0140 
0141   // define limit for matching fee_bco to fee_bco_predicted from gtm_bco
0142   static void set_gtm_bco_diff( unsigned int value )
0143   {
0144     m_max_gtm_bco_diff = value;
0145   }
0146 
0147   //! find reference from modebits
0148   bool find_reference_from_modebits(const gtm_payload&);
0149 
0150   //! find reference from data
0151   bool find_reference_from_data(const fee_payload&);
0152 
0153   //! save all GTM BCO clocks from packet data
0154   void save_gtm_bco_information(int /* packet_id */, const gtm_payload&);
0155 
0156   //! find gtm bco matching a given fee
0157   /**
0158    * packet and fee ids are not necessary to the calculation.
0159    * They are pased here for the clarity of debugging messages
0160    */
0161   std::optional<uint64_t> find_gtm_bco(int /*packet_id*/, unsigned int /*fee_id*/, uint32_t /*fee_gtm*/);
0162 
0163   //! cleanup
0164   void cleanup();
0165 
0166   //! cleanup
0167   void cleanup(uint64_t /*ref_bco*/);
0168 
0169   //@}
0170 
0171  private:
0172 
0173   //! update multiplier adjustment
0174   void update_multiplier_adjustment(uint64_t /* gtm_bco */, uint32_t /* fee_bco */);
0175 
0176   //! verbosity
0177   unsigned int m_verbosity = 0;
0178 
0179   //! verified
0180   bool m_verified_from_modebits = false;
0181 
0182   bool m_verified_from_data = false;
0183 
0184   //! first lvl1 bco (40 bits)
0185   uint64_t m_gtm_bco_first = 0;
0186 
0187   //! first fee bco (20 bits)
0188   uint32_t m_fee_bco_first = 0;
0189 
0190   //! last found fee_bco
0191   /** used to try finding bco reference from data */
0192   uint32_t m_fee_bco_prev = 0;
0193   bool m_has_fee_bco_prev = false;
0194 
0195   //! list of available bco
0196   std::list<uint64_t> m_gtm_bco_list;
0197 
0198   //! matching between fee bco and lvl1 bco
0199   using m_bco_matching_pair_t = std::pair<unsigned int, uint64_t>;
0200   std::list<m_bco_matching_pair_t> m_bco_matching_list;
0201 
0202   //! keep track or  fee_bco for which no gtm_bco is found
0203   std::set<uint32_t> m_orphans;
0204 
0205   //! true if multiplier is set
0206   static bool m_multiplier_is_set;
0207 
0208   //! gtm clock multiplier
0209   static double m_multiplier;
0210 
0211   //! true if multiplier adjustment is enabled
0212   static bool m_multiplier_adjustment_enabled;
0213 
0214   //! multiplier adjustment count
0215   /* controls how often the gtm multiplier is automatically adjusted */
0216   static unsigned int m_max_multiplier_adjustment_count;
0217 
0218   // define limit for matching fee_bco to fee_bco_predicted
0219   static unsigned int m_max_gtm_bco_diff;
0220 
0221   //! adjustment to multiplier
0222   double m_multiplier_adjustment = 0;
0223 
0224   //! running numerator for multiplier adjustment
0225   double m_multiplier_adjustment_numerator = 0;
0226 
0227   //! running denominator for multiplier adjustment
0228   double m_multiplier_adjustment_denominator = 0;
0229 
0230   //! running count for multiplier adjustment
0231   unsigned int m_multiplier_adjustment_count = 0;
0232 };
0233 
0234 #endif