tttrlib
A library for time-tagged time resolved data
Loading...
Searching...
No Matches
TTTRHeader.h
Go to the documentation of this file.
1#ifndef TTTRLIB_READHEADER_H
2#define TTTRLIB_READHEADER_H
3
4#include <stdlib.h> /* malloc, calloc, realloc, exit, free */
5#include <cstdint>
6#include <cstdio>
7#include <iostream>
8#include <map>
9#include <cmath> /* floor, ceil */
10#include <string>
11#include <string.h> /* strcmp */
12#include <algorithm>
13#include <vector>
14#include <array>
15#include <memory>
16#include <numeric>
17#include <iostream>
18#include <sstream> // std::stringstream
19#include <iomanip> /* std::setfill */
20#include <fstream> /* ifstream */
21
22#include <any>
23// #include <boost/any.hpp>
24//#include <boost/filesystem.hpp>
25//#include <boost/locale.hpp>
26
27#include "json.hpp"
28
29#include "Histogram.h"
30#include "TTTRRecordReader.h"
31#include "TTTRRecordTypes.h"
32#include "TTTRHeaderTypes.h"
33
34// some important Tag Idents (TTagHead.Ident) that we will need to read the most common content of a PTU file
35// check the output of this program and consult the tag dictionary if you need more
36const std::string TTTRTagRes = "MeasDesc_Resolution"; // Resolution for the Dtime (T3 Only) - in seconds
37const std::string TTTRTagGlobRes = "MeasDesc_GlobalResolution"; // Global Resolution of TimeTag(T2) /NSync (T3) - in seconds
38const std::string TTTRSyncRate = "SyncRate"; // SyncRate - in Hz
39const std::string TTTRNMicroTimes = "MeasDesc_NumberMicrotimes"; // The number of micro time channels
40const std::string TTTRRecordType = "MeasDesc_RecordType"; // Internal record type (see tttrlib record type identifier definitions)
41const std::string TTTRContainerType = "MeasDesc_ContainerType"; // Internal container type (see tttrlib record type identifier definitions)
42const std::string TTTRTagTTTRRecType = "TTResultFormat_TTTRRecType";
43const std::string TTTRTagBits = "TTResultFormat_BitsPerRecord"; // Bits per TTTR record
44const std::string FileTagEnd = "Header_End"; // Always appended as last tag (BLOCKEND)
45
46
48
49 friend class TTTR;
50
51protected:
52
53 // JSON object used to store all the header information
54 nlohmann::json json_data;
55
59 size_t header_end = 0;
60
61public:
62
67 return (int) json_data[TTTRRecordType];
68 }
69
76 }
77
85
93
102 static nlohmann::json get_tag(
103 nlohmann::json json_data,
104 const std::string &name,
105 int idx = -1
106 );
107
116 static int find_tag(
117 nlohmann::json &json_data,
118 const std::string &name,
119 int idx = -1
120 );
121
132 static void add_tag(
133 nlohmann::json &json_data,
134 const std::string &name,
135 std::any value,
136 // boost::any value,
137 unsigned int type = tyAnsiString,
138 int idx = -1
139 );
140
146 return (size_t) get_tag(json_data, TTTRTagBits)["value"] / 8;
147 }
148
149 size_t end() const{
150 return header_end;
151 }
152
156 size_t size(){
157 return json_data["tags"].size();
158 }
159
160 nlohmann::json& operator[](std::size_t idx){
161 return json_data["tags"][idx];
162 }
163
164 const nlohmann::json& operator[](std::size_t idx) const {
165 return json_data["tags"][idx];
166 }
167
175 return (unsigned int) get_tag(json_data, TTTRNMicroTimes)["value"];
176 }
177
180
183 return get_tag(json_data, TTTRTagRes)["value"];
184 }
185
188 double pixel_duration_d = TTTRHeader::get_tag(
189 json_data, "$TimePerPixel")["value"];
190 double global_res = TTTRHeader::get_tag(
191 json_data, "MeasDesc_GlobalResolution")["value"];
192 long pixel_duration = std::round(pixel_duration_d / global_res);
193 return pixel_duration;
194 }
195
198 double pixel_duration_d = TTTRHeader::get_tag(
199 json_data, "$TimePerPixel")["value"];
200 double global_res_d = TTTRHeader::get_tag(
201 json_data, "MeasDesc_GlobalResolution")["value"];
202 double n_pixel = TTTRHeader::get_tag(json_data, "ImgHdr_PixX")["value"];
203 return std::ceil((pixel_duration_d * n_pixel) / global_res_d);
204 }
205
218 double macro_time_resolution = get_macro_time_resolution();
219 double micro_time_resolution = get_micro_time_resolution();
220 return (unsigned int) std::floor(macro_time_resolution / micro_time_resolution);
221 }
222
227 TTTRHeader(int tttr_container_type);
228
231
244 TTTRHeader(std::FILE *fpin, int tttr_container_type=0, bool close_file=false);
245 TTTRHeader(std::string fn, int tttr_container_type=0);
246 ~TTTRHeader() = default;
247
257 static size_t read_ptu_header(
258 std::FILE *fpin,
259 int &tttr_record_type,
260 nlohmann::json &json_data,
261 bool rewind = true
262 );
263
272 static size_t read_ht3_header(
273 std::FILE *fpin,
274 nlohmann::json &data,
275 bool rewind = true
276 );
277
286 static size_t read_bh132_header(
287 std::FILE *fpin,
288 nlohmann::json &data,
289 bool rewind = true
290 );
291
301 std::FILE *fpin,
302 nlohmann::json &data,
303 bool rewind = true
304 );
305
316 std::string fn,
317 TTTRHeader* header,
318 std::string modes = "w"
319 );
320
330 static void write_ptu_header(
331 std::string fn,
332 TTTRHeader* header,
333 std::string modes = "wb"
334 );
335
345 static void write_ht3_header(
346 std::string fn,
347 TTTRHeader* header,
348 std::string modes = "wb"
349 );
350
361 std::string get_json(std::string tag_name="", int idx=-1, int indent=1);
362
368 void set_json(std::string json_string){
369 json_data = nlohmann::json::parse(json_string);
370 }
371
372};
373
374
375#endif //TTTRLIB_READHEADER_H
const std::string TTTRTagRes
Definition TTTRHeader.h:36
const std::string TTTRContainerType
Definition TTTRHeader.h:41
const std::string TTTRTagGlobRes
Definition TTTRHeader.h:37
const std::string TTTRTagTTTRRecType
Definition TTTRHeader.h:42
const std::string TTTRNMicroTimes
Definition TTTRHeader.h:39
const std::string FileTagEnd
Definition TTTRHeader.h:44
const std::string TTTRSyncRate
Definition TTTRHeader.h:38
const std::string TTTRTagBits
Definition TTTRHeader.h:43
const std::string TTTRRecordType
Definition TTTRHeader.h:40
#define tyAnsiString
Definition TTTRHeaderTypes.h:16
Definition TTTRHeader.h:47
void set_tttr_record_type(int v)
Definition TTTRHeader.h:74
int get_tttr_container_type()
Definition TTTRHeader.h:82
TTTRHeader(std::FILE *fpin, int tttr_container_type=0, bool close_file=false)
size_t end() const
Definition TTTRHeader.h:149
const nlohmann::json & operator[](std::size_t idx) const
Definition TTTRHeader.h:164
static void add_tag(nlohmann::json &json_data, const std::string &name, std::any value, unsigned int type=tyAnsiString, int idx=-1)
static nlohmann::json get_tag(nlohmann::json json_data, const std::string &name, int idx=-1)
static size_t read_bh132_header(std::FILE *fpin, nlohmann::json &data, bool rewind=true)
Reads the header of a Becker & Hickel SPC132 file and sets the reading routing.
void set_tttr_container_type(int v)
Definition TTTRHeader.h:90
double get_micro_time_resolution()
Resolution for the micro time in nanoseconds.
Definition TTTRHeader.h:182
~TTTRHeader()=default
unsigned int get_number_of_micro_time_channels()
Definition TTTRHeader.h:174
TTTRHeader(const TTTRHeader &p2)
Copy constructor.
int get_line_duration()
Duration of a line in LSM in units of macro time clock.
Definition TTTRHeader.h:197
TTTRHeader(int tttr_container_type)
nlohmann::json & operator[](std::size_t idx)
Definition TTTRHeader.h:160
unsigned int get_effective_number_of_micro_time_channels()
Definition TTTRHeader.h:217
static size_t read_ptu_header(std::FILE *fpin, int &tttr_record_type, nlohmann::json &json_data, bool rewind=true)
Reads the header of a PTU file and sets the reading routing.
int get_pixel_duration()
Duration of a pixel in LSM in units of macro time clock.
Definition TTTRHeader.h:187
static size_t read_ht3_header(std::FILE *fpin, nlohmann::json &data, bool rewind=true)
Reads the header of an HT3 file and sets the reading routing.
static void write_ht3_header(std::string fn, TTTRHeader *header, std::string modes="wb")
TTTRHeader(std::string fn, int tttr_container_type=0)
static void write_spc132_header(std::string fn, TTTRHeader *header, std::string modes="w")
static size_t read_cz_confocor3_header(std::FILE *fpin, nlohmann::json &data, bool rewind=true)
Reads the header of a Carl Zeiss (CZ) Confocor3 file and sets the reading routing.
int get_tttr_record_type()
Definition TTTRHeader.h:66
nlohmann::json json_data
Definition TTTRHeader.h:54
size_t size()
Definition TTTRHeader.h:156
static void write_ptu_header(std::string fn, TTTRHeader *header, std::string modes="wb")
static int find_tag(nlohmann::json &json_data, const std::string &name, int idx=-1)
void set_json(std::string json_string)
Definition TTTRHeader.h:368
size_t header_end
Definition TTTRHeader.h:59
size_t get_bytes_per_record()
Definition TTTRHeader.h:145
std::string get_json(std::string tag_name="", int idx=-1, int indent=1)
double get_macro_time_resolution()
Resolution for the macro time in nanoseconds.
Time-Tagged Time-Resolved (TTTR) data class.
Definition TTTR.h:195