Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:16:16

0001 #ifndef FUN4ALLRAW_SINGLEMICROMEGASPOOLINPUT_V2_H
0002 #define FUN4ALLRAW_SINGLEMICROMEGASPOOLINPUT_V2_H
0003 
0004 #include "MicromegasBcoMatchingInformation_v2.h"
0005 #include "SingleStreamingInput.h"
0006 
0007 #include <phool/PHTimer.h>
0008 
0009 #include <array>
0010 #include <deque>
0011 #include <list>
0012 #include <map>
0013 #include <memory>
0014 #include <set>
0015 #include <string>
0016 #include <vector>
0017 
0018 class MicromegasRawHit;
0019 class Packet;
0020 
0021 class TFile;
0022 class TTree;
0023 class TH1;
0024 
0025 class SingleMicromegasPoolInput_v2 : public SingleStreamingInput
0026 {
0027  public:
0028   explicit SingleMicromegasPoolInput_v2(const std::string &name = "SingleMicromegasPoolInput_v2");
0029   ~SingleMicromegasPoolInput_v2() override;
0030   void FillPool(const unsigned int nevents = 1) override;
0031 
0032   void CleanupUsedPackets(const uint64_t bclk) override
0033   { CleanupUsedPackets(bclk,false); }
0034 
0035   //! specialized verion of cleaning up packets, with an extra flag about wheter the cleanup hits are dropped or not
0036   void CleanupUsedPackets(const uint64_t /* bclk */, bool /*dropped */) override;
0037 
0038   void ClearCurrentEvent() override;
0039   bool GetSomeMoreEvents();
0040   void Print(const std::string &what = "ALL") const override;
0041   void CreateDSTNode(PHCompositeNode *topNode) override;
0042 
0043   void SetBcoRange(const unsigned int value) { m_BcoRange = value; }
0044   void ConfigureStreamingInputManager() override;
0045   void SetNegativeBco(const unsigned int value) { m_NegativeBco = value; }
0046 
0047   //! define minimum pool size in terms of how many BCO are stored
0048   void SetBcoPoolSize(const unsigned int value) { m_BcoPoolSize = value; }
0049 
0050   //! save some statistics for BCO QA
0051   void FillBcoQA(uint64_t /*gtm_bco*/) override;
0052 
0053   // write the initial histograms for QA manager
0054   void createQAHistos() override;
0055 
0056   /// do evalutation
0057   void set_do_evaluation( bool value ) { m_do_evaluation = value; }
0058 
0059   /// output file name for evaluation histograms
0060   void set_evaluation_outputfile(const std::string& outputfile) { m_evaluation_filename = outputfile; }
0061 
0062   private:
0063 
0064   //!@name decoding constants
0065   //@{
0066   /// max number of FEE per OBDC
0067   static constexpr uint16_t MAX_FEECOUNT = 26;
0068 
0069   // Length for the 256-bit wide Round Robin Multiplexer for the data stream
0070   static constexpr size_t DAM_DMA_WORD_LENGTH = 16;
0071   //@}
0072 
0073   //! DMA word structure
0074   struct dma_word
0075   {
0076     uint16_t dma_header;
0077     uint16_t data[DAM_DMA_WORD_LENGTH - 1];
0078   };
0079 
0080   void process_packet(Packet*);
0081   void decode_gtm_data(int /*packet_id*/, const dma_word&);
0082   void process_fee_data(int /*packet_id*/, unsigned int /*fee_id*/);
0083 
0084   // fee data buffer
0085   std::vector<std::deque<uint16_t>> m_feeData{MAX_FEECOUNT};
0086 
0087   // list of packets from data stream
0088   std::array<Packet *, 10> plist{};
0089 
0090   /// keep track of number of non data events
0091   unsigned int m_NumSpecialEvents{0};
0092 
0093   /// bco adjustment for matching across subsystems
0094   unsigned int m_BcoRange{0};
0095 
0096   /// bco adjustment for matching across subsystems
0097   unsigned int m_NegativeBco{0};
0098 
0099   //! minimum number of BCO required in Micromegas Pools
0100   unsigned int m_BcoPoolSize{1};
0101 
0102   //! store list of packets that have data for a given beam clock
0103   /**
0104    * all packets in taggers are stored,
0105    * disregarding whether there is data associated to it or not
0106    * this allows to keep track of dropped data, also in zero-suppression mode
0107    */
0108   std::map<uint64_t, std::set<int>> m_BeamClockPacket;
0109 
0110   //! store list of FEE that have data for a given beam clock
0111   std::map<uint64_t, std::set<int>> m_BeamClockFEE;
0112 
0113   //! store list of raw hits matching a given bco
0114   std::map<uint64_t, std::vector<MicromegasRawHit *>> m_MicromegasRawHitMap;
0115 
0116   //! store current list of BCO on a per fee basis.
0117   /** only packets for which a given FEE have data are stored */
0118   std::map<int, uint64_t> m_FEEBclkMap;
0119 
0120   //! store current list of BCO
0121   /**
0122    * all packets in taggers are stored,
0123    * disregarding whether there is data associated to it or not
0124    * this allows to keep track of dropped data, also in zero-suppression mode
0125    */
0126   std::set<uint64_t> m_BclkStack;
0127 
0128   //! map bco_information_t to packet id
0129   using bco_matching_information_map_t = std::map<unsigned int, MicromegasBcoMatchingInformation_v2>;
0130   bco_matching_information_map_t m_bco_matching_information_map;
0131 
0132   class counter_t
0133   {
0134     public:
0135 
0136     //! total count
0137     uint64_t total = 0;
0138 
0139     //! drop count due to unmatched bco
0140     uint64_t dropped_bco = 0;
0141 
0142     //! drop count due to pools
0143     uint64_t dropped_pool = 0;
0144 
0145     //! dropped fraction (bco)
0146     double dropped_fraction_bco() const { return double(dropped_bco)/total; }
0147 
0148     //! dropped fraction (pool)
0149     double dropped_fraction_pool() const { return double(dropped_pool)/total; }
0150 
0151   };
0152 
0153   // keep track of waveform statistics per fee
0154   std::map<int,counter_t> m_fee_waveform_counters{};
0155 
0156   // keep track of waveform statistics per packet
0157   std::map<int,counter_t> m_waveform_counters{};
0158 
0159   // keep track of heartbeat statistics per fee
0160   std::map<int,counter_t> m_fee_heartbeat_counters{};
0161 
0162   // keep track of heartbeat statistics per packet
0163   std::map<int,counter_t> m_heartbeat_counters{};
0164 
0165   // timer
0166   PHTimer m_timer{ "SingleMicromegasPoolInput_v2" };
0167 
0168   //!@name QA histograms
0169   //@{
0170 
0171   //! keeps track of how often a given (or all) packets are found for a given BCO
0172   TH1 *h_packet_stat{nullptr};
0173 
0174   //! keep track of how many heartbeats are found per FEE sampa
0175   TH1 *h_heartbeat_stat{nullptr};
0176 
0177   //! keeps track of how many packets are found for a given BCO
0178   TH1 *h_packet{nullptr};
0179 
0180   //! keeps track of how many waveforms are found for a given BCO
0181   TH1 *h_waveform{nullptr};
0182 
0183   //! total number of waveforms per packet
0184   TH1 *h_waveform_count_total{nullptr};
0185 
0186   //! total number of dropped waveforms per packet due to bco mismatch
0187   /*! waveforms are dropped when their FEE-BCO cannot be associated to any global BCO */
0188   TH1 *h_waveform_count_dropped_bco{nullptr};
0189 
0190   //! total number of dropped waveforms per packet due to fun4all pool mismatch
0191   TH1 *h_waveform_count_dropped_pool{nullptr};
0192 
0193   //! total number of waveforms per packet
0194   TH1 *h_fee_waveform_count_total{nullptr};
0195 
0196   //! total number of dropped waveforms per fee due to bco mismatch
0197   /*! waveforms are dropped when their FEE-BCO cannot be associated to any global BCO */
0198   TH1 *h_fee_waveform_count_dropped_bco{nullptr};
0199 
0200   //! total number of dropped waveforms per fee due to fun4all pool mismatch
0201   TH1 *h_fee_waveform_count_dropped_pool{nullptr};
0202 
0203   //@}
0204 
0205   //!@name evaluation
0206   //@{
0207 
0208   //! evaluation
0209   bool m_do_evaluation = false;
0210 
0211   //! evaluation output filename
0212   std::string m_evaluation_filename = "SingleMicromegasPoolInput.root";
0213   std::unique_ptr<TFile> m_evaluation_file;
0214 
0215   /**
0216    * waveform is similar to sample except that there is only one of which per waveform,
0217    * and that it stores the max adc and corresponding sample_id
0218    */
0219   class Waveform
0220   {
0221    public:
0222 
0223     /// packet
0224     unsigned int packet_id = 0;
0225 
0226     /// fee
0227     unsigned short fee_id = 0;
0228 
0229     /// channel id
0230     unsigned short channel = 0;
0231 
0232     /// true if measurement is hearbeat
0233     bool is_heartbeat = false;
0234 
0235     /// ll1 bco
0236     uint64_t gtm_bco_first = 0;
0237 
0238     /// ll1 bco
0239     uint64_t gtm_bco = 0;
0240 
0241     /// ll1 bco
0242     uint64_t gtm_bco_matched = 0;
0243 
0244     /// fee bco
0245     unsigned int fee_bco_first = 0;
0246 
0247     /// fee bco
0248     unsigned int fee_bco = 0;
0249 
0250     /// fee bco predicted (from gtm)
0251     unsigned int fee_bco_predicted = 0;
0252 
0253     /// fee bco match (from gtm)
0254     unsigned int fee_bco_predicted_matched = 0;
0255   };
0256 
0257   Waveform m_waveform;
0258 
0259   //! tree
0260   TTree* m_evaluation_tree = nullptr;
0261 
0262   //*}
0263 
0264 };
0265 
0266 #endif