readPTU: a Python Library to Analyse Time Tagged Time Resolved Data

readPTU is a python package designed to analyze time-correlated single-photon counting data. The use of the library promotes the storage of the complete time arrival information of the photons and full flexibility in post-processing data for analysis. The library supports the computation of time resolved signal with external triggers and second order autocorrelation function analysis can be performed using multiple algorithms that provide the user with different trade-offs with regards to speed and accuracy. Additionally, a thresholding algorithm to perform time post-selection is also available. The library has been designed with performance and extensibility in mind to allow future users to implement support for additional file extensions and algorithms without having to deal with low level details. We demonstrate the performance of readPTU by analyzing the second-order autocorrelation function of the resonance fluorescence from a single quantum dot in a two-dimensional semiconductor.


I. MOTIVATION AND SIGNIFICANCE
Time-correlated single-photon counting (TCSPC) experiments have found widespread applications in different disciplines, including the characterization of individual optical emitters [1,2], advanced microscopy [3,4], Bell's inequalities verification using astronomical sources [5], or the real time tracking of physiochemical reactions [6]. This technique is also a major tool for the characterization of single photon emission [2,7,8], relevant for quantum technologies such as linear optics quantum computing [9] and spin-photon interfaces for quantum communication networks [10,11]. TCSPC measurements can be used to characterize the statistical properties of the emitted light [12] and it is thus a tool to prove the single-photon nature of a source and provide useful information about its internal dynamics.
When recording TCSPC data, one usually only directly computes the quantities of interest, such as a histogram of the delays between photon clicks at two channels, since storing the timing information for all detector clicks can easily grow above gigabyte sizes. On the other hand, storing this information can be advantageous to perform more sophisticated data analysis. For example, time traces of the detected detector click rates can reveal intensity spikes and the emitters' dynamics, or enable post selection of specific time intervals.
The readPTU [19] library presented here enables the researcher to achieve this, providing added flexibility in their experiments.
The library has been written as a Python module that interfaces with a C library that handles the most computing-intensive aspects. This combination provides a user-friendly interface to a high performance set of underlying routines. The underlying C library provides functionalities to efficiently read a stream of detector click records, with no required knowledge about the low level details of how the information is encoded on the binary files, making the library of algorithms easy to extend.
Our hope is that the work presented here will allow researchers across a variety of disciplines to analyze raw TCSPC data more efficiently.

II. AUTOCORRELATION MEASUREMENTS USING TCSPC
One of the main applications of TCSPC is the characterization of the statistical properties of a light source, such as the anti-bunched nature of single photon sources, through the second 2 order autocorrelation of the field. The right panel of Fig. 1 shows a simplified setup of such an experiment. The emitted photon field is split by a 50/50 beam splitter and directed to two single photon detectors, such as avalanche photo diodes (APS) or super conducting nanowire single photon detectors (SNSPD). Upon photon detection, detector clicks are recorded by channels A and B of time-Correlated single photon counting system (TCSPC). Since a single photon source cannot produce a click on both channels simultaneously, a dip is observed in the coincidence rate when the delay between the channels is equal (Fig. 1, left panel).
This result is only possible under the assumption that the electromagnetic field is quantized [12]. These results can be extended to the n-th order correlation functions and provide a full characterization of the coherence properties of an electromagnetic field.
In this section we will introduce the basic theory behind the second order autocorrelation function and how it can be experimentally measured using TCSPC. To compute the second order autocorrelation function, g (2) (τ ), we need to consider first what is the probability amplitude for measuring a photon with a delay of ∆t in detector B after a having measured one in detector A. This is given by [13]: where E (+) (r, t) is the photon annihilation operator and |i and |f are the initial and final states respectively. Similarly we can define the probability of detecting two photons in different detectors (r = A, B). We will show in Sect. II A how considering all final states and averaging over the ensemble of initial states leads to the definition of the second order autocorrelation function G (2) (τ ): In the latter expressions we have fixed t = 0 under the assumption that we are dealing with a stationary process, in that the value of the autocorrelation function will only depend on time differences.
A. From photon statistics to g (2) (τ ) We show now how building a histogram of time delays between photons in channel A and channel B of the TCSPC system gives us access to the second order autocorrelation function 3 of a stream of photons. Put another way, this histogram gives an approximation to the joint probability distribution of measuring a photon in the first detector in between times t + dt and t + τ + dt on the second detector [14]. The probability per unit time to obtain the final state |f after the detection of a photon at t in channel A and at t + τ in channel B from the initial state |i is proportional to: If we sum over all possible final states and introduce the density operator (ρ = i P i |i i |) to consider the mixture of initial states we conclude that: Algorithms readPTU provides multiple algorithms to postprocess TCSPC data, each featuring different trade-offs.

C. Intensity time trace
The first algorithm provided by readPTU enables user to obtain a time-trace of the photon count. Its pseudo-code is shown in Alg. 1. The algorithm goes through the recorded photon events and assigns them to the current time bin as long as its time-tag falls within it. Once the time tag is larger than the end time of the current time bin, CurrBin is updated and the next time bin starts to be filled. The length of each time bin sets the trade-off between time resolution and the error in the photon count due to Poissonian statistics. The intensity time-trace can be used to study emitter dynamics as will be demonstrated in Sect. III.

Naive Algorithm
The derivation in section II A shows how g (2) (τ ) can be approximated by a histogram of the delays between photon clicks in channels A and B of the autocorrelation card. The simplest implementation of this idea is described in Fig. 2 and Algorithm 2.
When a photon is detected in channel A, a stopwatch is started. When, after a time delay dt a second photon is recorded at channel B, dt is assigned to the histogram in the call to UpdateHistogram and the process repeated. The UpdateHistogram function additionally checks if the measured delay is within a user defined interval (the autocorrelation window) that establishes the longest delay that will be stored in the output histogram.
An artificial delay between the two channels is typically added to shift t = 0 from the origin, so that g (2) (t = 0) can be see when running the autocorrelator in histogram mode.
Reversing start and stop channels brings the t = 0 autocorrelation out of the histogram: readPTU library has an operation mode that removes this issue by running an algorithm symmetric on the assignment of start and stop channels. Although this algorithm is extremely fast and good enough for quickly exploring the data it has two major shortcomings.
First, the time delay between two consecutive photons follows an exponential distribution.
Since the algorithm described above only looks at consecutive pairs, an exponential decay 5 artifact is introduced. Second, photons in channel A are ignored while waiting for a click in channel B, wasting available information (see the photons greyed out in Fig. 2). Instructions on how to add new files formats is explained in the package documentation.
Each file format requires a parser to be defined in parsers.c. Through using different libraries, the overhead from dynamically selecting (via function pointers or switch statements) what specific file format parser to use is completely removed, while keeping a single codebase and an easily extensible system for future file formats. The whole library has been designed to primarily target performance: to this end, buffered input and output is used by reading multiple records from the TCSPC files simultaneously. This optimization reduces by three orders of magnitude the number of file read operations required to process the input. Memory records. Only clicks belonging to records within one of those ranges are used to compute g (2) .
As discussed above, this option allows users, for example, to exclude time ranges where they suspect photons likely only correspond to laser background. readPTU provides the function construct postselect vector to automatically generate ranges based on a hard threshold. The script used to analyze and produce the plots in Fig. 5 is available on the code repository together with instructions on how to extend the library to manipulate other file formats.

III. EXAMPLE: RESONANCE FLUORESCENCE OF A QUANTUM EMITTER WITH SPECTRAL FLUCTUATIONS
One application of the functionalities offered by readPTU is to study the dynamics of single photon emitters. For solid-state emitters, fluctuations in the surrounding environment result in randoms shifts of the absorption/emission wavelength [15]. When exciting narrow absorption lines with a narrowband lasers, these random shift bring the emitter in and out of resonance with the laser, resulting in intermittent optical emission [16,17]. When the emitter is not resonant with the laser, only background light is collected, degrading the Signal to Background Ratio (SBR), an important parameter for antibunching experiments.
In resonance fluorescence, the background laser can be filtered by polarization from the signal [18], but it remains challenging to completely remove it. This problem can be solved by post-selecting such that only the photons detected in time intervals when the emitter was resonant with the laser, i.e. when the emission rate is above a given threshold, contribute to the g (2) (τ ) measurement.
Here we show how readPTU can be used to analyze TCSPC data from a single photon emitter in an atomically-thin monolayer of WSe 2 [17], at cryogenic temperature. The emitters were addressed by resonance fluorescence in a confocal microscope. The laser linewidth is 10 kHz, while the emitter (typical linewidth ≈ 100 MHz) wavelength fluctuates on 10 GHz scale. While the timescales of the spectral fluctuations vary, they are relatively slow with dynamics longer than a second which can be easily post-selected for.
First, we extract an intensity timetrace. To this end, the user only has to specify the size of time bin over which the number of photons will be averaged, optionally the channel 9 number. The timetrace helps us identify problems during the experiment, such as drifts of the experimental setup or blinking, as can be observed in Fig. 4. When the emitter goes out of resonance, the count number falls drastically to the level of the background produced by the uncancelled excitation laser. It can also be observed how the background laser cancellation fidelity drifts throughout the experiment. We use this information to design a piece-wise linear post-selection thresholding function adapted to our data.
Once a threshold has been established, we can look at the distribution of the duration of periods for which the emitter has been in/out of resonance. This shows that the emitter was above threshold for just 0. Since only laser light is detected most of the time, the mininmum achieavable g 2 (0) is limited by the SBR averaged over the whole duration of the experiment, which is much smaller than the peak SBR. By post-selecting with the threshold function shown in Fig. 4, the minimum g 2 (0) is improved from 0.74 to 0.23 which demonstrates that the observed system is really a single emitter, since g (2) < 0.5 [12].

IV. CONCLUSIONS
Here we present the implementation of multiple algorithms to compute the second order autocorrelation function from TCSPC data. Lifetime measurements can also be performed by choosing the appropiate parameters. The use of raw TCSPC data allows us to perform time post-selection of the data based on the intensity. This can be used to significantly improve experimental results in the resence of emitter blinking or spectral fluctuations [17].
Thanks to the focus on performance and extensibility multi-GB files can be analyzed in a few seconds and new algorithms can be implemented without having to worry about low levels details. More importantly, we have found that keeping raw TCSPC data has helped us improve our experimental setups and data analysis routines and having a library capable of efficiently analyzing it has promoted that it is always stored.