DynExp
Highly flexible laboratory automation for dynamically changing experiments.
HardwareAdapterQutoolsTDC.h
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
9 #pragma once
10 
11 #include "stdafx.h"
12 #include "HardwareAdapter.h"
13 #include "../MetaInstruments/TimeTagger.h"
14 
16 {
17  #include "../include/qutools/tdcbase.h"
18  #include "../include/qutools/tdcmultidev.h"
19  #include "../include/qutools/tdchbt.h"
20 }
21 
22 namespace DynExpHardware
23 {
25 
27  {
28  public:
29  QutoolsTDCException(std::string Description, const int ErrorCode,
30  const std::source_location Location = std::source_location::current()) noexcept
31  : Exception(std::move(Description), Util::ErrorType::Error, ErrorCode, Location)
32  {}
33  };
34 
36  {
37  public:
39 
41 
43  virtual ~QutoolsTDCHardwareAdapterParams() = default;
44 
45  virtual const char* GetParamClassTag() const noexcept override { return "QutoolsTDCHardwareAdapterParams"; }
46 
47  Param<TextList> DeviceDescriptor = { *this, {}, "DeviceDescriptor", "Device serial",
48  "Serial of the qutools TDC device to connect with" };
49  Param<ParamsConfigDialog::NumberType> DefaultTimestampBufferSize = { *this, "DefaultTimestampBufferSize", "Default timestamp buffer size",
50  "Size of the qutools TDC device's timestamp buffer in samples (default value - can be changed by instruments).",
51  false, 1000, 1, 1000000, 1, 0 };
52  Param<ParamsConfigDialog::NumberType> DefaultThresholdInVolts = { *this, "DefaultThresholdInVolts", "Default trigger threshold (V)",
53  "Determines on which voltage of the connected signal to trigger (default value - can be changed by instruments).",
54  false, 1, -2, 3, .1, 3 };
55  Param<EdgeType> DefaultTriggerEdge = { *this, EdgeTypeStrList(), "DefaultTriggerEdge", "Default trigger edge",
56  "Determines on which edges of the connected signal to trigger (default value - can be changed by instruments).",
57  false, EdgeType::RisingEdge};
58  Param<ParamsConfigDialog::NumberType> DefaultExposureTime = { *this, "DefaultExposureTimeInMs", "Default exposure time (ms)",
59  "Exposure time denoting an interval in which events are accumulated (default value - can be changed by instruments).",
60  false, 1000, 1, 65535, 100, 0 };
61  Param<ParamsConfigDialog::NumberType> DefaultCoincidenceWindow = { *this, "DefaultCoincidenceWindowInPs", "Default coincidence window (ps)",
62  "Time interval in which events are considered to be coincident (default value - can be changed by instruments).",
63  false, 1000, 1, std::numeric_limits<ParamsConfigDialog::NumberType>::max(), 100, 0 };
64 
65  private:
66  void ConfigureParamsImpl(dispatch_tag<HardwareAdapterParamsBase>) override final;
68  };
69 
71  {
72  public:
75 
78 
79  private:
80  virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore& Core) const override { return DynExp::MakeParams<QutoolsTDCHardwareAdapterConfigurator>(ID, Core); }
81  };
82 
84  {
94  {
95  private:
98 
99  public:
100  // Synchronizes every call to qutools TDC library from anywhere.
101  static [[nodiscard]] LockType Lock(const std::chrono::milliseconds Timeout = std::chrono::milliseconds(100));
102 
103  private:
104  static QutoolsTDCSynchronizer& GetInstance() noexcept;
105  };
106 
107  public:
109  {
110  using CountsType = std::vector<QutoolsTDCSyms::Int32>;
111 
112  CoincidenceDataType() : NumUpdates(0) {}
113  CoincidenceDataType(CountsType&& Counts, QutoolsTDCSyms::Int32 NumUpdates);
114 
116  QutoolsTDCSyms::Int32 NumUpdates;
117 
118  // Just to store boolean flags. But std::vector<bool> is evil...
119  std::vector<char> HasBeenRead;
120  };
121 
124  using ChannelType = QutoolsTDCSyms::Uint8;
127 
128  constexpr static auto Name() noexcept { return "qutools TDC"; }
129  constexpr static auto Category() noexcept { return "I/O"; }
130  static auto Enumerate();
131 
133  virtual ~QutoolsTDCHardwareAdapter();
134 
135  virtual std::string GetName() const override { return Name(); }
136  virtual std::string GetCategory() const override { return Category(); }
137 
138  bool IsOpened() const noexcept { return DeviceConnected; }
139 
140  void EnableChannels(bool EnableStartChannel, QutoolsTDCSyms::Int32 ChannelMask) const;
141  void EnableChannel(ChannelType Channel) const;
142  void DisableChannel(ChannelType Channel) const;
143  void SetExposureTime(std::chrono::milliseconds ExposureTime) const;
144  void SetCoincidenceWindow(ValueType CoincidenceWindow) const;
145  void SetChannelDelay(ChannelType Channel, Util::picoseconds ChannelDelay) const;
146  void SetTimestampBufferSize(QutoolsTDCSyms::Int32 Size) const;
148  QutoolsTDCSyms::TDC_SignalCond Conditioning = QutoolsTDCSyms::SCOND_MISC, bool UseRisingEdge = true, double ThresholdInVolts = 1) const;
149  void ConfigureFilter(ChannelType Channel,
150  QutoolsTDCSyms::TDC_FilterType FilterType = QutoolsTDCSyms::TDC_FilterType::FILTER_NONE, QutoolsTDCSyms::Int32 ChannelMask = 0) const;
151  void EnableHBT(bool Enable) const;
152  void ConfigureHBTChannels(ChannelType FirstChannel, ChannelType SecondChannel) const;
153  void ConfigureHBTParams(Util::picoseconds BinWidth, QutoolsTDCSyms::Int32 BinCount) const;
154  void ResetHBT() const;
155  QutoolsTDCSyms::Int64 GetHBTEventCounts() const;
156  std::chrono::microseconds GetHBTIntegrationTime() const;
158 
159  ValueType GetTimebase() const;
160  QutoolsTDCSyms::Int32 GetBufferSize() const;
161  std::vector<ValueType> GetTimestamps(ChannelType Channel) const;
162  size_t GetCountsFromTimestamps(ChannelType Channel) const;
163  const CoincidenceDataType& GetCoincidenceCounts() const;
164  std::pair<QutoolsTDCSyms::Int32, QutoolsTDCSyms::Int32> GetCoincidenceCounts(ChannelType Channel) const;
165 
166  void ClearTimestamps(ChannelType Channel) const;
167 
168  private:
169  void Init();
170 
171  void ResetImpl(dispatch_tag<HardwareAdapterBase>) override final;
173 
174  void EnsureReadyStateChild() override final;
175  bool IsReadyChild() const override final;
176  bool IsConnectedChild() const noexcept override final;
177 
178  // Not thread-safe, must be called from function calling AcquireLock().
179  void CheckError(const int Result, const std::source_location Location = std::source_location::current()) const;
180 
181  void OpenUnsafe();
182  void CloseUnsafe();
183 
184  void AddressThisTDCDeviceUnsafe() const;
185  void ReadTimestampsUnsafe() const;
186  void EnableChannelsUnsafe(bool EnableStartChannel, QutoolsTDCSyms::Int32 ChannelMask) const;
187  std::pair<bool, QutoolsTDCSyms::Int32> GetEnabledChannelsUnsafe() const;
188  void SetExposureTimeUnsafe(std::chrono::milliseconds ExposureTime) const;
189  void SetCoincidenceWindowUnsafe(ValueType CoincidenceWindow) const;
190  void SetChannelDelayUnsafe(ChannelType Channel, Util::picoseconds ChannelDelay) const;
191  void SetTimestampBufferSizeUnsafe(QutoolsTDCSyms::Int32 Size) const;
193  QutoolsTDCSyms::TDC_SignalCond Conditioning = QutoolsTDCSyms::SCOND_MISC, bool UseRisingEdge = true, double ThresholdInVolts = 1) const;
194  void ConfigureFilterUnsafe(ChannelType Channel,
195  QutoolsTDCSyms::TDC_FilterType FilterType = QutoolsTDCSyms::TDC_FilterType::FILTER_NONE, QutoolsTDCSyms::Int32 ChannelMask = 0) const;
196  void EnableHBTUnsafe(bool Enable) const;
197  void ConfigureHBTChannelsUnsafe(ChannelType FirstChannel, ChannelType SecondChannel) const;
198  void ConfigureHBTParamsUnsafe(Util::picoseconds BinWidth, QutoolsTDCSyms::Int32 BinCount) const;
199  void ResetHBTUnsafe() const;
200  QutoolsTDCSyms::Int64 GetHBTEventCountsUnsafe() const;
201  std::chrono::microseconds GetHBTIntegrationTimeUnsafe() const;
203 
204  std::atomic<bool> DeviceConnected;
205  unsigned int DeviceNumber;
207  mutable QutoolsTDCSyms::Int32 BufferSize; // Can be changed by SetTimestampBufferSize().
208  QutoolsTDCSyms::Int32 ChannelCount;
209 
211  mutable std::unordered_map<ChannelType, std::vector<ValueType>> TimestampsPerChannel;
212  };
213 }
Implementation of DynExp hardware adapter objects.
QutoolsTDCException(std::string Description, const int ErrorCode, const std::source_location Location=std::source_location::current()) noexcept
virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core) const override
Override to make derived classes call DynExp::MakeParams with the correct configurator type derived f...
Param< ParamsConfigDialog::NumberType > DefaultThresholdInVolts
static Util::TextValueListType< EdgeType > EdgeTypeStrList()
Param< ParamsConfigDialog::NumberType > DefaultTimestampBufferSize
void ConfigureParamsImpl(dispatch_tag< HardwareAdapterParamsBase >) override final
virtual void ConfigureParamsImpl(dispatch_tag< QutoolsTDCHardwareAdapterParams >)
Param< ParamsConfigDialog::NumberType > DefaultExposureTime
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
QutoolsTDCHardwareAdapterParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
Param< ParamsConfigDialog::NumberType > DefaultCoincidenceWindow
Only one instance of this class is allowed for synchronizing calls to connected qutools time taggers....
static LockType Lock(const std::chrono::milliseconds Timeout=std::chrono::milliseconds(100))
QutoolsTDCHardwareAdapter(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
void ConfigureFilterUnsafe(ChannelType Channel, QutoolsTDCSyms::TDC_FilterType FilterType=QutoolsTDCSyms::TDC_FilterType::FILTER_NONE, QutoolsTDCSyms::Int32 ChannelMask=0) const
bool IsConnectedChild() const noexcept override final
Determines the connection status of the hardware interface.
const CoincidenceDataType & GetCoincidenceCounts() const
void SetExposureTime(std::chrono::milliseconds ExposureTime) const
void ConfigureSignalConditioningUnsafe(ChannelType Channel, QutoolsTDCSyms::TDC_SignalCond Conditioning=QutoolsTDCSyms::SCOND_MISC, bool UseRisingEdge=true, double ThresholdInVolts=1) const
void SetTimestampBufferSize(QutoolsTDCSyms::Int32 Size) const
void EnableChannels(bool EnableStartChannel, QutoolsTDCSyms::Int32 ChannelMask) const
void SetCoincidenceWindow(ValueType CoincidenceWindow) const
void SetExposureTimeUnsafe(std::chrono::milliseconds ExposureTime) const
std::pair< bool, QutoolsTDCSyms::Int32 > GetEnabledChannelsUnsafe() const
void ConfigureFilter(ChannelType Channel, QutoolsTDCSyms::TDC_FilterType FilterType=QutoolsTDCSyms::TDC_FilterType::FILTER_NONE, QutoolsTDCSyms::Int32 ChannelMask=0) const
std::chrono::microseconds GetHBTIntegrationTime() const
virtual std::string GetName() const override
Returns the name of this Object type.
void ConfigureSignalConditioning(ChannelType Channel, QutoolsTDCSyms::TDC_SignalCond Conditioning=QutoolsTDCSyms::SCOND_MISC, bool UseRisingEdge=true, double ThresholdInVolts=1) const
bool IsReadyChild() const override final
Returns wheter this Object instance is ready (e.g. it is running or connected to a hardware device) a...
void SetChannelDelayUnsafe(ChannelType Channel, Util::picoseconds ChannelDelay) const
void CheckError(const int Result, const std::source_location Location=std::source_location::current()) const
void ConfigureHBTParamsUnsafe(Util::picoseconds BinWidth, QutoolsTDCSyms::Int32 BinCount) const
void ResetImpl(dispatch_tag< HardwareAdapterBase >) override final
std::chrono::microseconds GetHBTIntegrationTimeUnsafe() const
DynExpInstr::TimeTaggerData::HBTResultsType::ResultVectorType HBTResultsType
void SetCoincidenceWindowUnsafe(ValueType CoincidenceWindow) const
void SetTimestampBufferSizeUnsafe(QutoolsTDCSyms::Int32 Size) const
virtual std::string GetCategory() const override
Returns the category of this Object type.
virtual void ResetImpl(dispatch_tag< QutoolsTDCHardwareAdapter >)
void EnsureReadyStateChild() override final
Ensures that this Object instance is ready by possibly starting its worker thread or by opening conne...
void ConfigureHBTParams(Util::picoseconds BinWidth, QutoolsTDCSyms::Int32 BinCount) const
void ConfigureHBTChannelsUnsafe(ChannelType FirstChannel, ChannelType SecondChannel) const
void EnableChannelsUnsafe(bool EnableStartChannel, QutoolsTDCSyms::Int32 ChannelMask) const
void SetChannelDelay(ChannelType Channel, Util::picoseconds ChannelDelay) const
void ConfigureHBTChannels(ChannelType FirstChannel, ChannelType SecondChannel) const
std::unordered_map< ChannelType, std::vector< ValueType > > TimestampsPerChannel
size_t GetCountsFromTimestamps(ChannelType Channel) const
std::vector< ValueType > GetTimestamps(ChannelType Channel) const
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition: DynExpCore.h:127
Defines the base class for a hardware adapter object. Hardware adapters describe interfaces/connectio...
Configurator class for HardwareAdapterBase.
Parameter class for HardwareAdapterBase.
HardwareAdapterParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a HardwareAdapterBase instance.
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition: Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition: Object.h:2303
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition: Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition: Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition: Object.h:349
DynExp exceptions are derived from this class. It contains basic information about the cause of the e...
Definition: Exception.h:51
Exception(std::string Description, const ErrorType Type=ErrorType::Error, const int ErrorCode=-1, const std::source_location Location=std::source_location::current()) noexcept
Constructs an exception. Constructor is noexcept, although std::runtime_error() might throw std::bad_...
Definition: Exception.cpp:8
const int ErrorCode
DynExp error code from DynExpErrorCodes::DynExpErrorCodes
Definition: Exception.h:106
Interface to allow synchronizing the access to derived classes between different threads by providing...
Definition: Util.h:56
std::unique_lock< MutexType > LockType
Definition: Util.h:66
DynExp's hardware namespace contains the implementation of DynExp hardware adapters which extend DynE...
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition: Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
Definition: circularbuf.cpp:7
std::chrono::duration< double, std::pico > picoseconds
Extends std::chrono by a duration data type for picoseconds.
Definition: Util.h:762
ErrorType
DynExp's error types
Definition: Exception.h:15
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Definition: QtUtil.h:37
Accumulates include statements to provide a precompiled header.
std::vector< BasicSample > ResultVectorType
List type to store the samples g^(2)(t)
Definition: TimeTagger.h:108