tttrlib
A library for time-tagged time resolved data
Loading...
Searching...
No Matches
TTTR.h
Go to the documentation of this file.
1#define _CRT_SECURE_NO_DEPRECATE
2
3#ifndef TTTRLIB_TTTR_H
4#define TTTRLIB_TTTR_H
5
6#include <cstdint>
7#include <string>
8#include <cmath>
9#include <algorithm>
10#include <iostream>
11#include <vector>
12#include <cstdio>
13#include <map>
14#include <array>
15#include <memory> /* shared_ptr */
16#include <stdlib.h> /* malloc, calloc, realloc, exit, free */
17#include <numeric>
18#include <cinttypes> /* uint64, int64, etc */
19#include <fstream> /* ifstream */
20
21#include <boost/bimap.hpp>
22//#include <boost/filesystem.hpp> // std::filesystem is not in osx 10.14
23
24#ifdef BUILD_PHOTON_HDF
25#include "hdf5.h"
26#endif
27
28#include "Histogram.h"
29#include "TTTRHeader.h"
30#include "TTTRMask.h"
31#include "TTTRRecordReader.h"
32#include "TTTRRecordTypes.h"
33#include "info.h"
34
35
64 int **output, int *n_output,
65 unsigned long long *time, int n_time,
66 double time_window, int n_ph_max,
67 double macro_time_calibration=1.0,
68 bool invert=false, bool make_mask=false
69);
70
71
91 int **output, int *n_output,
92 unsigned long long *input, int n_input,
93 double minimum_window_length,
94 double maximum_window_length = -1,
95 int minimum_number_of_photons_in_time_window = -1,
96 int maximum_number_of_photons_in_time_window = -1,
97 double macro_time_calibration = 1.0,
98 bool invert = false
99);
100
101
126 int **output, int *n_output,
127 unsigned long long *input, int n_input,
128 double time_window_length,
129 double macro_time_resolution = 1.0
130);
131
132
152template <typename T>
153inline void get_array(
154 size_t n_valid_events,
155 T *array,
156 T **out,
157 int *n_out
158) {
159 *n_out = static_cast<int>(n_valid_events);
160 *out = static_cast<T*>(malloc(sizeof(T) * n_valid_events));
161
162 for (size_t i = 0; i < n_valid_events; ++i) {
163 (*out)[i] = array[i];
164 }
165}
166
167
168class TTTRMask;
169
195class TTTR : public std::enable_shared_from_this<TTTR>{
196
197 friend class CLSMImage;
198 friend class TTTRRange;
199 friend class TTTRMask;
201
202private:
203
205 uint64_t overflow_counter;
206
208 std::string filename;
209
210 TTTRHeader *header = nullptr;
211
213 boost::bimap<std::string, int> container_names = {};
214
215 typedef bool (*processRecord_t)(
216 uint32_t&, // input
217 uint64_t&, // overflow counter
218 uint64_t&, // true number of sync pulses
219 uint32_t&, // microtime
220 int16_t&, // channel number (16bit more than enough, negative numbers - potential future special cases
221 int16_t& // the event type: photon, or marker (overflows are treated separately and removed during reading)
222 );
223
224 std::map<int, processRecord_t> processRecord_map = {
235 };
236
253 int tttr_container_type; // e.g. Becker&Hickl (BH) SPC, PicoQuant (PQ) HT3, PQ-PTU
254
256 std::string tttr_container_type_str; // e.g. Becker&Hickl (BH) SPC, PicoQuant (PQ) HT3, PQ-PTU
257 int tttr_record_type; // e.g. BH spc132, PQ HydraHarp (HH) T3, PQ HH T2, etc.
258
260 std::FILE *fp; /* File handle for all other file types */
261
262#ifdef BUILD_PHOTON_HDF
263 hid_t hdf5_file; /*HDF5 file handle */
264#endif
265
266
268 size_t fp_records_begin;
269
271 uint64_t TTTRRecord;
272
279 bool (*processRecord)(
280 uint32_t&, // input
281 uint64_t&, // overflow counter
282 uint64_t&, // true number of sync pulses
283 uint32_t&, // microtime
284 int16_t&, // channel number (16bit more than enough, negative numbers - potential future special cases
285 int16_t& // the event type: photon, or marker (overflows are treated separately and removed during reading)
286 );
287
289 unsigned long long *macro_times;
290
292 unsigned short *micro_times;
293
295 signed char *routing_channels;
296
298 signed char *event_types;
299
301 size_t n_records_in_file = 0;
302
304 size_t n_records_read = 0;
305
307 size_t n_valid_events = 0;
308
322 void allocate_memory_for_records(size_t n_rec);
323
333 void deallocate_memory_of_records();
334
344 int read_hdf_file(const char *fn);
345
358 void read_records(size_t n_rec, bool rewind, size_t chunk);
359
368 void read_records(size_t n_rec);
369
376 void read_records();
377
378protected:
379
387
391 std::vector<signed char> used_routing_channels;
392
393
394public:
395
403 std::shared_ptr<TTTR> Get() { return shared_from_this(); }
404
415 void copy_from(const TTTR &p2, bool include_big_data = true);
416
429 int read_file(const char *fn = nullptr, int container_type = -1);
430
446 std::FILE *fp,
447 size_t offset,
448 size_t bytes_per_record
449 );
450
470 unsigned long long *macro_times, int n_macrotimes,
471 unsigned short *micro_times, int n_microtimes,
472 signed char *routing_channels, int n_routing_channels,
473 signed char *event_types, int n_event_types,
474 bool shift_macro_time = true,
475 long long macro_time_offset = 0
476 );
477
492 unsigned long long macro_time,
493 unsigned short micro_time,
494 signed char routing_channel,
495 signed char event_type,
496 bool shift_macro_time = true,
497 long long macro_time_offset = 0
498 );
499
510 void append(
511 const TTTR *other,
512 bool shift_macro_time=true,
513 long long macro_time_offset=0
514 );
515
524 size_t size() {
525 return get_n_valid_events();
526 }
527
538 void get_used_routing_channels(signed char **output, int *n_output);
539
550 void get_macro_times(unsigned long long **output, int *n_output);
551
562 void get_micro_times(unsigned short **output, int *n_output);
563
576 void get_intensity_trace(int **output, int *n_output, double time_window_length = 1.0);
577
587 void get_routing_channel(signed char** output, int* n_output);
588
598 void get_event_type(signed char** output, int* n_output);
599
609
619
629 return tttr_container_type_str;
630 }
631
642 std::shared_ptr<TTTR> select(int *selection, int n_selection);
643
650
658 TTTR(const TTTR &p2);
659
674 TTTR(const char *filename, int container_type, bool read_input);
675
689 TTTR(const char *filename, int container_type);
690
704 TTTR(const char *filename, const char* container_type);
705
722 TTTR(unsigned long long *macro_times, int n_macrotimes,
723 unsigned short *micro_times, int n_microtimes,
724 signed char *routing_channels, int n_routing_channels,
725 signed char *event_types, int n_event_types,
726 bool find_used_channels = true
727 );
728
741 TTTR(const TTTR &parent,
742 int *selection, int n_selection,
743 bool find_used_channels = true);
744
750
756 std::string get_filename();
757
766 std::shared_ptr<TTTR> get_tttr_by_selection(int *selection, int n_selection){
767 auto p = std::make_shared<TTTR>(*this, selection, n_selection, true);
768 return p;
769 }
770
790 int **output, int *n_output,
791 double minimum_window_length,
792 double maximum_window_length=-1,
793 int minimum_number_of_photons_in_time_window=-1,
794 int maximum_number_of_photons_in_time_window=-1,
795 double macro_time_calibration = -1,
796 bool invert=false
797 ){
798 if(macro_time_calibration < 0){
799 macro_time_calibration = header->get_macro_time_resolution();
800 }
802 output, n_output,
803 macro_times, n_valid_events,
804 minimum_window_length,
805 maximum_window_length,
806 minimum_number_of_photons_in_time_window,
807 maximum_number_of_photons_in_time_window,
808 macro_time_calibration,
809 invert
810 );
811 }
812
825 int **output, int *n_output,
826 signed char *input, int n_input
827 );
828
839 std::shared_ptr<TTTR> get_tttr_by_channel(signed char *input, int n_input){
840 int* sel; int nsel;
841 get_selection_by_channel(&sel, &nsel, input, n_input);
842 return get_tttr_by_selection(sel, nsel);
843 }
844
859 int **output, int *n_output,
860 double time_window, int n_ph_max,
861 bool invert=false, bool make_mask=false
862 );
863
864
876 std::shared_ptr<TTTR> get_tttr_by_count_rate(
877 double time_window, int n_ph_max,
878 bool invert=false, bool make_mask=false
879 ){
880 int* sel; int nsel;
882 &sel, &nsel,
883 time_window, n_ph_max,
884 invert, make_mask);
885 return get_tttr_by_selection(sel, nsel);
886 }
887
908 int **output, int *n_output,
909 double minimum_window_length,
910 int minimum_number_of_photons_in_time_window,
911 int maximum_number_of_photons_in_time_window=-1,
912 double maximum_window_length=-1.0,
913 double macro_time_calibration=-1,
914 bool invert = false
915 );
916
924
931
937 size_t get_n_events();
938
946 bool write(std::string filename, TTTRHeader* header = nullptr);
947
954 void write_spc132_events(FILE* fp, TTTR* tttr);
955
962 void write_hht3v2_events(FILE* fp, TTTR* tttr);
963
970 void write_header(std::string &fn, TTTRHeader* header = nullptr);
971
977 void shift_macro_time(int shift);
978
985 TTTR* operator+(const TTTR* other) const {
986 auto re = new TTTR();
987 re->copy_from(*this, true);
988 re->append(other);
989 return re;
990 }
991
1004 TTTR *tttr_data,
1005 double** output, int* n_output,
1006 double **time, int *n_time,
1007 unsigned short micro_time_coarsening = 1,
1008 std::vector<int> *tttr_indices = nullptr
1009 );
1010
1021 double **histogram, int *n_histogram,
1022 double **time, int *n_time,
1023 unsigned short micro_time_coarsening = 1
1024 ){
1026 this, histogram, n_histogram,
1027 time, n_time,
1028 micro_time_coarsening
1029 );
1030 }
1031
1054 TTTR *tttr_data,
1055 TTTR *tttr_irf = nullptr,
1056 double m0_irf = 1, double m1_irf = 0,
1057 std::vector<int> *tttr_indices = nullptr,
1058 double dt = -1.0,
1059 int minimum_number_of_photons = 1,
1060 std::vector<double> *background = nullptr,
1061 double m0_bg = 0.0, double m1_bg = 0.0,
1062 double background_fraction = -1.0
1063 );
1064
1077 TTTR *tttr_irf = nullptr,
1078 int m0_irf = 1, int m1_irf = 1,
1079 std::vector<int> *tttr_indices = nullptr,
1080 double dt = -1.0,
1081 int min_ph = 1
1082 ){
1083 return compute_mean_lifetime(
1084 this,
1085 tttr_irf, m0_irf, m1_irf,
1086 tttr_indices, dt, min_ph
1087 );
1088 }
1089
1098 static double compute_count_rate(
1099 TTTR *tttr_data,
1100 std::vector<int> *tttr_indices = nullptr,
1101 double macrotime_resolution = -1.0
1102 );
1103
1112 std::vector<int> *tttr_indices = nullptr,
1113 double macrotime_resolution = -1.0
1114 ){
1115 return compute_count_rate(this, tttr_indices, macrotime_resolution);
1116 }
1117
1128 TTTR *tttr_data,
1129 std::vector<int> *tttr_indices = nullptr,
1130 double microtime_resolution = -1.0,
1131 int minimum_number_of_photons = 1
1132 );
1133
1134
1144 std::vector<int> *tttr_indices = nullptr,
1145 double microtime_resolution = -1.0,
1146 int minimum_number_of_photons = 1
1147 ){
1149 this, tttr_indices,
1150 microtime_resolution, minimum_number_of_photons);
1151 }
1152
1153};
1154
1155
1156#endif //TTTRLIB_TTTR_H
void selection_by_count_rate(int **output, int *n_output, unsigned long long *time, int n_time, double time_window, int n_ph_max, double macro_time_calibration=1.0, bool invert=false, bool make_mask=false)
A count rate (cr) filter that returns an array containing a list of indices where the cr was smaller ...
void compute_intensity_trace(int **output, int *n_output, unsigned long long *input, int n_input, double time_window_length, double macro_time_resolution=1.0)
Computes the intensity trace for a sequence of time events.
void get_array(size_t n_valid_events, T *array, T **out, int *n_out)
Extracts a subarray of valid events from the input array.
Definition TTTR.h:153
void ranges_by_time_window(int **output, int *n_output, unsigned long long *input, int n_input, double minimum_window_length, double maximum_window_length=-1, int minimum_number_of_photons_in_time_window=-1, int maximum_number_of_photons_in_time_window=-1, double macro_time_calibration=1.0, bool invert=false)
Returns time windows (tw), i.e., the start and stop indices for a minimum tw size and a minimum numbe...
#define PQ_RECORD_TYPE_HHT2v1
Definition TTTRHeaderTypes.h:44
#define BH_RECORD_TYPE_SPC600_256
Definition TTTRHeaderTypes.h:50
#define PQ_RECORD_TYPE_PHT2
Definition TTTRHeaderTypes.h:48
#define PQ_RECORD_TYPE_HHT3v1
Definition TTTRHeaderTypes.h:45
#define CZ_RECORD_TYPE_CONFOCOR3
Definition TTTRHeaderTypes.h:52
#define BH_RECORD_TYPE_SPC600_4096
Definition TTTRHeaderTypes.h:51
#define PQ_RECORD_TYPE_HHT3v2
Definition TTTRHeaderTypes.h:46
#define BH_RECORD_TYPE_SPC130
Definition TTTRHeaderTypes.h:49
#define PQ_RECORD_TYPE_PHT3
Definition TTTRHeaderTypes.h:47
#define PQ_RECORD_TYPE_HHT2v2
Definition TTTRHeaderTypes.h:43
bool ProcessCzRaw(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessSPC130(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessPHT3(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessSPC600_256(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessHHT3v1(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessHHT3v2(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessHHT2v1(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessSPC600_4096(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessPHT2(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
bool ProcessHHT2v2(uint32_t &TTTRRecord, uint64_t &overflow_counter, uint64_t &true_nsync, uint32_t &micro_time, int16_t &channel, int16_t &record_type)
Definition CLSMImage.h:161
CorrelatorPhotonStream class gathers event times and weights.
Definition CorrelatorPhotonStream.h:14
Definition TTTRHeader.h:47
double get_macro_time_resolution()
Resolution for the macro time in nanoseconds.
Time-Tagged Time-Resolved (TTTR) data class.
Definition TTTR.h:195
TTTR(const char *filename, int container_type)
std::shared_ptr< TTTR > get_tttr_by_count_rate(double time_window, int n_ph_max, bool invert=false, bool make_mask=false)
Get a TTTR object filtered by count rate criteria.
Definition TTTR.h:876
void append_events(unsigned long long *macro_times, int n_macrotimes, unsigned short *micro_times, int n_microtimes, signed char *routing_channels, int n_routing_channels, signed char *event_types, int n_event_types, bool shift_macro_time=true, long long macro_time_offset=0)
Appends events to the TTTR object.
void write_header(std::string &fn, TTTRHeader *header=nullptr)
Writes the header information to a TTTR file.
void set_header(TTTRHeader *v)
Set the header for the TTTR object.
static double compute_mean_microtime(TTTR *tttr_data, std::vector< int > *tttr_indices=nullptr, double microtime_resolution=-1.0, int minimum_number_of_photons=1)
Computes the mean microtime.
void append_event(unsigned long long macro_time, unsigned short micro_time, signed char routing_channel, signed char event_type, bool shift_macro_time=true, long long macro_time_offset=0)
Appends a single event to the TTTR object.
void shift_macro_time(int shift)
Shifts the macro time by adding an integer value to each macro time entry.
TTTR(unsigned long long *macro_times, int n_macrotimes, unsigned short *micro_times, int n_microtimes, signed char *routing_channels, int n_routing_channels, signed char *event_types, int n_event_types, bool find_used_channels=true)
static double compute_mean_lifetime(TTTR *tttr_data, TTTR *tttr_irf=nullptr, double m0_irf=1, double m1_irf=0, std::vector< int > *tttr_indices=nullptr, double dt=-1.0, int minimum_number_of_photons=1, std::vector< double > *background=nullptr, double m0_bg=0.0, double m1_bg=0.0, double background_fraction=-1.0)
Computes the mean lifetime by the moments of the decay and the instrument response function.
size_t size()
Returns the number of valid events in the TTTR data.
Definition TTTR.h:524
void get_selection_by_count_rate(int **output, int *n_output, double time_window, int n_ph_max, bool invert=false, bool make_mask=false)
Get indices where the count rate is below a specified maximum.
size_t get_n_valid_events()
Returns the number of valid events in the TTTR file.
void append(const TTTR *other, bool shift_macro_time=true, long long macro_time_offset=0)
Appends events from another TTTR object to the current TTTR object.
void write_spc132_events(FILE *fp, TTTR *tttr)
Write events from the TTTR object to a file as SPC-132.
TTTR(const TTTR &p2)
Copy constructor for the TTTR (Time-Tagged Time-Resolved) class.
static void compute_microtime_histogram(TTTR *tttr_data, double **output, int *n_output, double **time, int *n_time, unsigned short micro_time_coarsening=1, std::vector< int > *tttr_indices=nullptr)
Computes a histogram of the TTTR data's micro times.
int read_file(const char *fn=nullptr, int container_type=-1)
Reads TTTR data from a file into the TTTR object.
TTTR(const char *filename, int container_type, bool read_input)
std::string get_filename()
std::string get_tttr_container_type()
Returns the container type used to open the TTTR file.
Definition TTTR.h:628
double mean_lifetime(TTTR *tttr_irf=nullptr, int m0_irf=1, int m1_irf=1, std::vector< int > *tttr_indices=nullptr, double dt=-1.0, int min_ph=1)
Computes the mean lifetime by moments of decay and instrument response.
Definition TTTR.h:1076
void get_selection_by_channel(int **output, int *n_output, signed char *input, int n_input)
Get events indices by the routing channel number.
void copy_from(const TTTR &p2, bool include_big_data=true)
Copies information from another TTTR object.
double get_mean_microtime(std::vector< int > *tttr_indices=nullptr, double microtime_resolution=-1.0, int minimum_number_of_photons=1)
Gets the mean microtime.
Definition TTTR.h:1143
void get_microtime_histogram(double **histogram, int *n_histogram, double **time, int *n_time, unsigned short micro_time_coarsening=1)
Computes and returns a histogram of the TTTR data's micro times.
Definition TTTR.h:1020
TTTR * operator+(const TTTR *other) const
Adds the events of another TTTR object to the current TTTR object.
Definition TTTR.h:985
void get_used_routing_channels(signed char **output, int *n_output)
Retrieves the used routing channel numbers from the TTTR data.
std::shared_ptr< TTTR > select(int *selection, int n_selection)
Creates a new TTTR object by selecting specific events based on the provided indices.
void get_ranges_by_time_window(int **output, int *n_output, double minimum_window_length, double maximum_window_length=-1, int minimum_number_of_photons_in_time_window=-1, int maximum_number_of_photons_in_time_window=-1, double macro_time_calibration=-1, bool invert=false)
Returns time windows (tw), i.e., the start and the stop indices for a minimum tw size,...
Definition TTTR.h:789
std::shared_ptr< TTTR > get_tttr_by_selection(int *selection, int n_selection)
Definition TTTR.h:766
void find_used_routing_channels()
Traverses the routing channel array and identifies used routing channel numbers.
size_t get_n_events()
Returns the number of events in the TTTR file, or the number of selected events if a selection is app...
void get_micro_times(unsigned short **output, int *n_output)
Retrieves the micro times of valid TTTR events.
TTTRHeader * get_header()
Get the header as a map of strings.
std::vector< signed char > used_routing_channels
A routing channel is a numeric identifier associated with each photon in the time-tagged time-resolve...
Definition TTTR.h:391
void get_time_window_ranges(int **output, int *n_output, double minimum_window_length, int minimum_number_of_photons_in_time_window, int maximum_number_of_photons_in_time_window=-1, double maximum_window_length=-1.0, double macro_time_calibration=-1, bool invert=false)
Get time windows (tw) based on specified criteria.
void get_intensity_trace(int **output, int *n_output, double time_window_length=1.0)
Computes and returns an intensity trace for a specified integration window.
static size_t get_number_of_records_by_file_size(std::FILE *fp, size_t offset, size_t bytes_per_record)
Determines the number of records in a TTTR file (not for use with HDF5).
TTTR()
Default constructor for the TTTR (Time-Tagged Time-Resolved) class.
void write_hht3v2_events(FILE *fp, TTTR *tttr)
Write events from the TTTR object to a file as HHT3v2.
unsigned int get_number_of_micro_time_channels()
Returns the number of micro time channels that fit between two macro time clocks.
void get_macro_times(unsigned long long **output, int *n_output)
Retrieves the macro times of valid TTTR events.
std::shared_ptr< TTTR > Get()
Returns a shared pointer to the current instance of TTTR.
Definition TTTR.h:403
double get_count_rate(std::vector< int > *tttr_indices=nullptr, double macrotime_resolution=-1.0)
Gets the count rate.
Definition TTTR.h:1111
void get_event_type(signed char **output, int *n_output)
Returns an array containing the event types of the valid TTTR events.
TTTR(const char *filename, const char *container_type)
static double compute_count_rate(TTTR *tttr_data, std::vector< int > *tttr_indices=nullptr, double macrotime_resolution=-1.0)
Computes the count rate.
std::shared_ptr< TTTR > get_tttr_by_channel(signed char *input, int n_input)
Get a TTTR object based on a selection by routing channel numbers.
Definition TTTR.h:839
TTTR(const TTTR &parent, int *selection, int n_selection, bool find_used_channels=true)
void get_routing_channel(signed char **output, int *n_output)
Returns an array containing the routing channel numbers of the valid TTTR events.
bool write(std::string filename, TTTRHeader *header=nullptr)
Writes the contents of an opened TTTR file to a new TTTR file.
Definition TTTRMask.h:9
Represents a range of TTTR indices.
Definition TTTRRange.h:15