BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnHgndRaw2Digit.cxx
Go to the documentation of this file.
1
#include "
BmnHgndRaw2Digit.h
"
2
3
#include "
TMath.h
"
4
#include "
TSystem.h
"
5
6
#include <bitset>
7
#include <
nlohmann/json.hpp
>
8
9
#define TDCBIN 0.1
// 100 ps
10
11
void
BmnHgndRaw2Digit::print
()
12
{
13
LOG(info) <<
Form
(
"BmnHgndRaw2Digit"
);
14
}
15
16
BmnHgndRaw2Digit::BmnHgndRaw2Digit
(
int
period,
int
run,
const
std::string& mappingFile,
const
std::string& calibFile)
17
{
18
std::string resolvedMapFile = mappingFile.empty() ?
Form
(
"HGND_map_period%d.json"
, period) : mappingFile;
19
std::string resolvedCalibFile = calibFile.empty() ?
Form
(
"HGND_calibration_period%d.json"
, period) : calibFile;
20
21
LOG(info) <<
"BmnHgndRaw2Digit: Using mapping file: "
<< resolvedMapFile;
22
LOG(info) <<
"BmnHgndRaw2Digit: Using calibration file: "
<< resolvedCalibFile;
23
24
ParseConfig
(resolvedMapFile);
25
ParseCalibration
(resolvedCalibFile);
26
}
27
28
void
BmnHgndRaw2Digit::ParseConfig
(
const
std::string& mappingFile)
29
{
30
std::string fullPath = std::string(getenv(
"VMCWORKDIR"
)) +
"/input/"
+ mappingFile;
31
std::ifstream config_file(fullPath);
32
if
(!config_file.is_open()) {
33
LOG(error) <<
Form
(
"BmnHgndRaw2Digit::ParseConfig: Cannot open config file: %s"
, fullPath.c_str());
34
return
;
35
}
36
37
nlohmann::json
j;
38
try
{
39
config_file >> j;
40
}
catch
(
const
std::exception& e) {
41
LOG(error) <<
"BmnHgndRaw2Digit::ParseConfig: Failed to parse JSON: "
<< e.what();
42
return
;
43
}
44
45
std::string version = j.value(
"version"
,
"unknown"
);
46
std::string comment = j.value(
"comment"
,
"none"
);
47
48
LOG(debug) <<
"BmnHgndRaw2Digit::ParseConfig. Mapping version: "
<< version;
49
LOG(debug) <<
"BmnHgndRaw2Digit::ParseConfig. Comment: "
<< comment;
50
51
fuoChannelMap
.clear();
52
for
(
const
auto
& entry : j[
"mapping"
]) {
53
int
gl_ch = entry[
"global_ch"
];
54
int
det = entry[
"detector"
];
55
int
lay = entry[
"layer"
];
56
int
row = entry[
"row"
];
57
int
col = entry[
"column"
];
58
59
fuoChannelMap
[gl_ch] =
BmnNdetAddress::GetAddress
(det, row, col, lay);
60
}
61
}
62
63
void
BmnHgndRaw2Digit::ParseCalibration
(
const
std::string& calibrationFile)
64
{
65
std::string fullPath = std::string(getenv(
"VMCWORKDIR"
)) +
"/parameters/hgnd/"
+ calibrationFile;
66
std::ifstream input(fullPath);
67
if
(!input.is_open()) {
68
LOG(error) <<
"BmnHgndRaw2Digit::ParseCalibration: Cannot open file "
<< fullPath;
69
return
;
70
}
71
72
nlohmann::json
j;
73
try
{
74
input >> j;
75
}
catch
(
const
std::exception& e) {
76
LOG(error) <<
"BmnHgndRaw2Digit::ParseCalibration: Failed to parse JSON: "
<< e.what();
77
return
;
78
}
79
80
std::string version = j.value(
"version"
,
"unknown"
);
81
std::string comment = j.value(
"comment"
,
"none"
);
82
double
totThreshold = j.value(
"totThreshold"
, 20.0);
// default in mV
83
double
Rload = j.value(
"Rload"
, 33.0);
// default in Ohm
84
85
LOG(debug) <<
"BmnHgndRaw2Digit::ParseCalibration. Calibration version: "
<< version;
86
LOG(debug) <<
"BmnHgndRaw2Digit::ParseCalibration. Comment: "
<< comment;
87
88
fuoCalibMap
.clear();
89
for
(
const
auto
& entry : j[
"calibration"
]) {
90
int
det = entry[
"detector"
];
91
int
lay = entry[
"layer"
];
92
int
row = entry[
"row"
];
93
int
col = entry[
"column"
];
94
double
off = entry[
"offset"
];
95
double
p = entry[
"p"
];
96
double
tau = entry[
"tau"
];
97
double
RC = entry[
"RC"
];
98
99
uint32_t address =
BmnNdetAddress::GetAddress
(det, row, col, lay);
100
TdcFromTot
tdc(off, p, tau, RC);
101
QdcFromTotApprox
qdc(p, tau, RC, totThreshold, Rload);
102
fuoCalibMap
.emplace(address, std::make_pair(tdc, qdc));
103
}
104
}
105
106
std::optional<uint32_t>
BmnHgndRaw2Digit::GetAddressFromBoard
(uint32_t key)
const
107
{
108
try
{
109
return
fuoChannelMap
.at(key);
110
}
catch
(
const
std::out_of_range& e) {
111
LOG(debug) <<
"BmnHgndRaw2Digit::GetAddressFromBoard. Invalid key "
<< key;
112
return
std::nullopt;
113
}
114
}
115
116
std::optional<std::pair<TdcFromTot, QdcFromTotApprox>>
BmnHgndRaw2Digit::GetCalibPairFromAddress
(uint32_t address)
const
117
{
118
try
{
119
return
fuoCalibMap
.at(address);
120
}
catch
(
const
std::out_of_range& e) {
121
LOG(debug) <<
"BmnHgndRaw2Digit::GetCalibPairFromAddress "
<< std::bitset<32>(address)
122
<<
" is marked badly in calibration file."
;
123
return
std::nullopt;
124
}
125
}
126
127
void
BmnHgndRaw2Digit::FillEvent
(
TClonesArray
* tdc,
BmnEventHeader
* hdr,
TClonesArray
* digits)
128
{
129
LOG(debug) <<
"BmnHgndRaw2Digit::fillEvent"
;
130
for
(
Int_t
iTdc = 0, nEntries = tdc->
GetEntriesFast
(); iTdc < nEntries; ++iTdc) {
131
const
auto
* tdcDig =
static_cast<
const
BmnAbstractTDCDigit
*
>
(tdc->
At
(iTdc));
132
133
const
auto
catch_address =
GetAddressFromBoard
(tdcDig->GetChannel());
134
if
(!catch_address.has_value())
135
continue
;
136
137
const
auto
address = catch_address.value();
138
if
(address == 0)
139
continue
;
140
141
const
auto
catch_calib =
GetCalibPairFromAddress
(address);
142
if
(!catch_calib.has_value())
143
continue
;
144
145
const
auto
& [slew_func, charge_func] = catch_calib.value();
146
147
LOG(debug4) <<
Form
(
"BmnHgndRaw2Digit::fillEvent: Ev %d Hdr ns %d. Hdr+ST %d. Hgnd l%d ns %ld"
,
148
hdr->
GetEventId
(), hdr->
GetEventTimeTS
().
GetNanoSec
(),
149
static_cast<
int
>
(hdr->
GetEventTimeTS
().
GetNanoSec
() + hdr->
GetStartSignalTime
()),
150
BmnNdetAddress::GetLayerId
(address), tdcDig->GetTimeNsec());
151
const
int64_t delta_sec = tdcDig->GetTimeSec() - hdr->
GetEventTimeTS
().
GetSec
();
152
const
int64_t delta_nsec = tdcDig->GetTimeNsec() - hdr->
GetEventTimeTS
().
GetNanoSec
();
153
const
int64_t wr_tof_ns = delta_sec * 1000000000LL + delta_nsec;
154
155
if
(delta_sec != 0)
156
LOG(warn) <<
Form
(
"BmnHgndRaw2Digit::fillEvent: Sync lost. Hdr sec %ld Hgnd sec %ld"
,
157
hdr->
GetEventTimeTS
().
GetSec
(), tdcDig->GetTimeSec());
158
159
const
double
raw_time = wr_tof_ns + tdcDig->GetToa() *
TDCBIN
;
// - hdr->GetStartSignalTime();
160
const
double
tot = tdcDig->GetTot() *
TDCBIN
;
161
const
double
time = raw_time - slew_func(tot);
162
const
double
signal = charge_func(tot);
163
164
new
((*digits)[digits->
GetEntriesFast
()])
BmnHgndDigi
(address, time, signal, raw_time, tot);
165
}
166
}
167
168
BmnHgndRaw2Digit::~BmnHgndRaw2Digit
() {}
TDCBIN
#define TDCBIN
Definition
BmnHgndRaw2Digit.cxx:9
Int_t
int Int_t
TMath.h
Form
char * Form(const char *fmt,...)
TSystem.h
BmnAbstractTDCDigit
Definition
BmnAbstractTDCDigit.h:7
BmnEventHeader
Definition
BmnEventHeader.h:24
BmnEventHeader::GetEventId
UInt_t GetEventId()
Get the event ID for this event.
Definition
BmnEventHeader.h:89
BmnEventHeader::GetStartSignalTime
Double_t GetStartSignalTime()
Definition
BmnEventHeader.h:110
BmnEventHeader::GetEventTimeTS
TTimeStamp GetEventTimeTS()
Get the time for this event.
Definition
BmnEventHeader.h:95
BmnHgndDigi
Data structure for a single HGND digit.
Definition
BmnHgndDigi.h:20
BmnHgndRaw2Digit::fuoChannelMap
std::unordered_map< uint32_t, uint32_t > fuoChannelMap
Definition
BmnHgndRaw2Digit.h:40
BmnHgndRaw2Digit::GetAddressFromBoard
std::optional< uint32_t > GetAddressFromBoard(uint32_t key) const
Definition
BmnHgndRaw2Digit.cxx:106
BmnHgndRaw2Digit::FillEvent
void FillEvent(TClonesArray *tdc_data, BmnEventHeader *hdr, TClonesArray *digits)
Definition
BmnHgndRaw2Digit.cxx:127
BmnHgndRaw2Digit::ParseConfig
void ParseConfig(const std::string &mappingFile)
Definition
BmnHgndRaw2Digit.cxx:28
BmnHgndRaw2Digit::GetCalibPairFromAddress
std::optional< std::pair< TdcFromTot, QdcFromTotApprox > > GetCalibPairFromAddress(uint32_t address) const
Definition
BmnHgndRaw2Digit.cxx:116
BmnHgndRaw2Digit::ParseCalibration
void ParseCalibration(const std::string &calibrationFile)
Definition
BmnHgndRaw2Digit.cxx:63
BmnHgndRaw2Digit::fuoCalibMap
std::unordered_map< uint32_t, std::pair< TdcFromTot, QdcFromTotApprox > > fuoCalibMap
Definition
BmnHgndRaw2Digit.h:42
BmnHgndRaw2Digit::print
void print()
Definition
BmnHgndRaw2Digit.cxx:11
BmnHgndRaw2Digit::~BmnHgndRaw2Digit
virtual ~BmnHgndRaw2Digit()
Definition
BmnHgndRaw2Digit.cxx:168
BmnHgndRaw2Digit::BmnHgndRaw2Digit
BmnHgndRaw2Digit()=default
BmnNdetAddress::GetAddress
static uint32_t GetAddress(uint32_t ArmId, uint32_t RowId, uint32_t ColumnId, uint32_t LayerId)
Return address.
Definition
BmnNdetAddress.h:39
BmnNdetAddress::GetLayerId
static uint32_t GetLayerId(uint32_t address)
Return Layer id from address.
Definition
BmnNdetAddress.h:109
TClonesArray
TObjArray::GetEntriesFast
Int_t GetEntriesFast() const
TObjArray::At
TObject * At(Int_t idx) const override
TTimeStamp::GetSec
time_t GetSec() const
TTimeStamp::GetNanoSec
Int_t GetNanoSec() const
BmnHgndRaw2Digit.h
json.hpp
nlohmann::json
basic_json<> json
default specialization
Definition
json.hpp:3365
BmnHgndCalibration::QdcFromTotApprox
Definition
BmnHgndCalibration.h:96
BmnHgndCalibration::TdcFromTot
Definition
BmnHgndCalibration.h:11
decoder
BmnHgndRaw2Digit.cxx
Generated on
for BmnRoot by
1.17.0