BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
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
13namespace fs = std::filesystem;
14using namespace std::chrono;
15
19
21
22BmnStatus 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());
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
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
Define status code.
Definition BmnEnums.h:93
@ kBMNERROR
Error.
Definition BmnEnums.h:95
@ kBMNSUCCESS
Success.
Definition BmnEnums.h:94
#define ANSI_COLOR_RED
Definition BmnMath.h:16
#define ANSI_COLOR_RESET
Definition BmnMath.h:18
#define DBGERR(a)
Definition BmnMath.h:25
#define ANSI_COLOR_BLUE
Definition BmnMath.h:17
const uint32_t kT0_TXT_BLOCK_BYTES
Definition RawTypes.h:98
const uint32_t kT0_BIN_BLOCK_BYTES
Definition RawTypes.h:94
static BmnT0Raw * Process_T0Raw(const uint32_t *d, uint32_t len, TClonesArray *t0raw=nullptr)
virtual ~BmnT0RawReader()
BmnStatus ReadFiles(const vector< string > &fileNames, const string &outfile="")
std::map< TTimeStamp, int64_t > fTsMap
BmnStatus ParseFile(const string &file, TClonesArray *t0raw=nullptr)
T0 module spill settings and counts.
Definition BmnT0Raw.h:70
TTimeStamp GetTS()
Definition BmnT0Raw.h:96
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
void Delete(Option_t *option="") override
Int_t GetEntriesFast() const
static TClass * Class()
const char * AsString(const Option_t *option="") const