BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnT0RawReader.cxx
Go to the documentation of this file.
1
#include "
BmnT0RawReader.h
"
2
// C++
3
#include <chrono>
4
#include <filesystem>
5
#include <iostream>
6
#include <sys/mman.h>
7
// FairRoot
8
#include "FairLogger.h"
9
// BmnRoot
10
#include "
BmnConverterTools.h
"
11
#include "RawTypes.h"
12
13
namespace
fs = std::filesystem;
14
using namespace
std::chrono;
15
16
BmnT0RawReader::BmnT0RawReader
()
17
:
fTreeT0
(nullptr)
18
{}
19
20
BmnT0RawReader::~BmnT0RawReader
() {}
21
22
BmnStatus
BmnT0RawReader::ReadFiles
(
const
vector<string>& fileNames,
const
string
& outFileName)
23
{
24
const
string
tree_name =
"T0RAW_ALL"
;
25
const
string
t0raw_branch_name =
"T0RAW"
;
26
const
string
ts_map_name =
"T0TsMap"
;
27
unique_ptr<TFile> out_file(make_unique<TFile>(outFileName.data(),
"recreate"
));
28
fTreeT0
=
new
TTree
(tree_name.data(), tree_name.data());
29
TClonesArray
* arr =
new
TClonesArray
(
BmnT0Raw::Class
());
30
fTreeT0
->Branch(t0raw_branch_name.data(), &arr);
31
32
printf(
ANSI_COLOR_RED
"\n================ PARSING T0 RAW FILES ================\n"
ANSI_COLOR_RESET
);
33
for
(
auto
& fileName : fileNames) {
34
ParseFile
(fileName, arr);
35
}
36
out_file->WriteObjectAny(&
fTsMap
,
TClass::GetClass
(
typeid
(
fTsMap
)), ts_map_name.data());
37
out_file->Write();
38
out_file->Close();
39
return
kBMNSUCCESS
;
40
}
41
42
BmnStatus
BmnT0RawReader::ParseFile
(
const
string
& file,
TClonesArray
* t0raw)
43
{
44
const
uint8_t cfg_name[] =
".BMNCfg"
;
45
const
uint8_t cfg_line_breaks[] = {0x0D, 0x0A, 0x20, 0x0D, 0x0A};
46
const
ptrdiff_t t0raw_block_len =
kT0_BIN_BLOCK_BYTES
+
kT0_TXT_BLOCK_BYTES
;
47
const
ptrdiff_t max_cfg_len = 1024;
// search back no more than
48
const
ptrdiff_t cfg_breaks_shift = 1023;
// between block start and line breaks
49
const
TTimeStamp
ZeroTS{0, 0};
50
51
fs::path raw_file_path(file.data());
52
size_t
raw_file_size = fs::file_size(raw_file_path);
53
printf(
"\nRAW FILE: "
);
54
printf(
ANSI_COLOR_BLUE
"%s"
ANSI_COLOR_RESET
, file.data());
55
printf(
"\nRAW FILE LENGTH: "
);
56
printf(
ANSI_COLOR_BLUE
"%.3f MB\n"
ANSI_COLOR_RESET
, raw_file_size / 1024. / 1024.);
57
58
int32_t fd = open(file.data(), O_RDONLY);
59
if
(fd < 0) {
60
DBGERR
(
"Cannot open file!"
)
61
return
kBMNERROR
;
62
}
63
void
* map = mmap(
nullptr
, raw_file_size, PROT_READ, MAP_PRIVATE, fd, 0);
64
close(fd);
65
if
(map == MAP_FAILED) {
66
DBGERR
(
"Map Failed!"
)
67
return
kBMNERROR
;
68
}
69
uint8_t* mptr =
static_cast<
uint8_t*
>
(map);
70
uint8_t* end = mptr + raw_file_size;
71
ptrdiff_t block_len = t0raw_block_len;
72
while
(mptr < end) {
73
if
(t0raw)
74
t0raw->
Delete
();
75
auto
it = std::search(mptr, end, std::begin(cfg_name), std::end(cfg_name) - 1
// exclude null-terminator
76
);
77
78
if
(it != end) {
79
LOG(debug) <<
"Found at offset "
<< (it - mptr) <<
"\n"
;
80
auto
it_lb = std::search(it - max_cfg_len, it, std::begin(cfg_line_breaks), std::end(cfg_line_breaks));
81
uint8_t* mptr_now = mptr;
82
mptr += block_len;
83
if
(it_lb != end) {
84
LOG(debug) <<
".BMNCfg - breaks offset "
<< (it - it_lb);
85
auto
it_block_start = it_lb - cfg_breaks_shift;
86
if
(it_block_start < mptr_now)
87
continue
;
// counters block incomplete (from start) -> skip
88
block_len = (end - it_block_start >= t0raw_block_len) ? t0raw_block_len : end - it_block_start;
89
if
(block_len <
kT0_BIN_BLOCK_BYTES
)
90
break
;
// counters block incomplete (EOF) -> skip
91
92
BmnT0Raw
* lastT0 =
BmnConverterTools::Process_T0Raw
(
reinterpret_cast<
const
uint32_t*
>
(it_block_start),
93
block_len, t0raw);
94
if
(t0raw && lastT0)
95
if
(t0raw->
GetEntriesFast
() && (lastT0->
GetTS
() > ZeroTS)) {
96
fTsMap
[lastT0->
GetTS
()] =
fTreeT0
->GetEntriesFast();
97
LOG(debug2) <<
"Add "
<< lastT0->
GetTS
().
AsString
() <<
" "
<<
fTsMap
[lastT0->
GetTS
()];
98
fTreeT0
->Fill();
99
}
100
}
101
}
else
102
break
;
103
}
104
munmap(map, raw_file_size);
105
return
kBMNSUCCESS
;
106
}
BmnStatus
BmnStatus
Define status code.
Definition
BmnEnums.h:93
kBMNERROR
@ kBMNERROR
Error.
Definition
BmnEnums.h:95
kBMNSUCCESS
@ kBMNSUCCESS
Success.
Definition
BmnEnums.h:94
ANSI_COLOR_RED
#define ANSI_COLOR_RED
Definition
BmnMath.h:16
ANSI_COLOR_RESET
#define ANSI_COLOR_RESET
Definition
BmnMath.h:18
DBGERR
#define DBGERR(a)
Definition
BmnMath.h:25
ANSI_COLOR_BLUE
#define ANSI_COLOR_BLUE
Definition
BmnMath.h:17
kT0_TXT_BLOCK_BYTES
const uint32_t kT0_TXT_BLOCK_BYTES
Definition
RawTypes.h:98
kT0_BIN_BLOCK_BYTES
const uint32_t kT0_BIN_BLOCK_BYTES
Definition
RawTypes.h:94
BmnConverterTools::Process_T0Raw
static BmnT0Raw * Process_T0Raw(const uint32_t *d, uint32_t len, TClonesArray *t0raw=nullptr)
Definition
BmnConverterTools.cxx:45
BmnT0RawReader::~BmnT0RawReader
virtual ~BmnT0RawReader()
Definition
BmnT0RawReader.cxx:20
BmnT0RawReader::fTreeT0
TTree * fTreeT0
Definition
BmnT0RawReader.h:35
BmnT0RawReader::ReadFiles
BmnStatus ReadFiles(const vector< string > &fileNames, const string &outfile="")
Definition
BmnT0RawReader.cxx:22
BmnT0RawReader::fTsMap
std::map< TTimeStamp, int64_t > fTsMap
Definition
BmnT0RawReader.h:33
BmnT0RawReader::BmnT0RawReader
BmnT0RawReader()
Definition
BmnT0RawReader.cxx:16
BmnT0RawReader::ParseFile
BmnStatus ParseFile(const string &file, TClonesArray *t0raw=nullptr)
Definition
BmnT0RawReader.cxx:42
BmnT0Raw
T0 module spill settings and counts.
Definition
BmnT0Raw.h:70
BmnT0Raw::GetTS
TTimeStamp GetTS()
Definition
BmnT0Raw.h:96
TClass::GetClass
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
TClonesArray
TClonesArray::Delete
void Delete(Option_t *option="") override
TObjArray::GetEntriesFast
Int_t GetEntriesFast() const
TObject::Class
static TClass * Class()
TTimeStamp
TTimeStamp::AsString
const char * AsString(const Option_t *option="") const
TTree
BmnConverterTools.h
BmnT0RawReader.h
decoder
BmnT0RawReader.cxx
Generated on
for BmnRoot by
1.17.0