R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BFileSource2.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#pragma once
15
16#include "R3BShared.h"
17#include <FairFileSourceBase.h>
18#include <TObjString.h>
19#include <chrono>
20#include <optional>
21
22class FairRootManager;
23class TChain;
24class TFolder;
25
27{
28 public:
30 void SetRunID(unsigned int runID) { run_id_ = runID; }
31 void SetMaxEventNum(unsigned int max_event_num) { max_event_num_ = max_event_num; }
32 void SetRefreshRate_Hz(float rate);
33 void ShowProgress(uint64_t event_num);
34
35 private:
36 uint64_t max_event_num_ = 0;
37 float refresh_rate_ = 2.; // Hz
38 std::chrono::milliseconds refresh_period_{ static_cast<int>(1000. / refresh_rate_) };
39 unsigned int run_id_ = 0;
40 std::chrono::time_point<std::chrono::steady_clock> previous_t_ = std::chrono::steady_clock::now();
41 uint64_t previous_event_num_ = 0;
42
43 void Print(uint64_t event_num, double speed_per_ms);
44};
45
47{
48 public:
49 using Strings = std::vector<std::string>;
50 R3BInputRootFiles() = default;
51 auto AddFileName(std::string name, bool is_tree_file = false) -> std::optional<std::string>;
52 void SetInputFileChain(TChain* chain);
53 void RegisterTo(FairRootManager*);
54 [[nodiscard]] auto is_empty() const -> bool { return fileNames_.empty(); }
55
56 [[nodiscard]] auto is_friend() const -> bool { return is_friend_; }
57 void Make_as_friend() { is_friend_ = true; }
58 void SetFriend(R3BInputRootFiles& friendFiles);
59 // Getters:
60 [[nodiscard]] auto GetBranchListRef() const -> const auto& { return branchList_; }
61 [[nodiscard]] auto GetBaseFileName() const -> const auto& { return fileNames_.front(); }
62 [[nodiscard]] auto GetTreeName() const -> const auto& { return treeName_; }
63 [[nodiscard]] auto GetFolderName() const -> const auto& { return folderName_; }
64 [[nodiscard]] auto GetTitle() const -> const auto& { return title_; }
65 [[nodiscard]] auto GetEntries() const -> int64_t;
66 [[nodiscard]] auto GetChain() const -> TChain* { return rootChain_; }
67 [[nodiscard]] auto GetInitialRunID() const { return initial_RunID_; }
68
69 // Setters:
70 void SetTreeName(std::string_view treeName) { treeName_ = treeName; }
71 void SetTitle(std::string_view title) { title_ = title; }
72 void SetFileHeaderName(std::string_view fileHeader) { fileHeader_ = fileHeader; }
73 void SetRunID(uint run_id) { initial_RunID_ = run_id; }
74
75 // rule of five:
76 ~R3BInputRootFiles() = default;
81
82 private:
83 bool is_friend_ = false;
84 uint initial_RunID_ = 0;
85 // TODO: title of each file group seems not necessary. Consider to remove it in the future.
86 std::string title_;
87 std::string treeName_ = "evt";
88 std::string folderName_;
89 std::string fileHeader_;
90 Strings fileNames_;
91 Strings branchList_;
92 std::vector<TObjString> timeBasedBranchList_;
93 std::vector<R3B::unique_rootfile> validRootFiles_;
94 std::vector<TFolder*> validMainFolders_;
95 TChain* rootChain_ = nullptr;
96
97 void Intitialize(std::string_view filename, bool is_tree_file = false);
98 auto ValidateFile(const std::string& filename, bool is_tree_file = false) -> bool;
99 static auto ExtractMainFolder(TFile*) -> std::optional<TKey*>;
100 auto ExtractRunId(TFile* rootFile) -> std::optional<uint>;
101 void register_branch_name();
102};
103
104class R3BFileSource2 : public FairFileSourceBase
105{
106 public:
108 explicit R3BFileSource2(std::string file, std::string_view title = "InputRootFile");
109 R3BFileSource2(std::vector<std::string> fileNames, std::string_view title);
110 explicit R3BFileSource2(std::vector<std::string> fileNames);
111
112 void AddFile(std::string file_name, bool is_tree_file = false);
113 void AddFile(std::vector<std::string> file_names, bool is_tree_file = false);
114 void AddFriend(std::string, bool is_tree_file = false);
115 void AddFriend(std::vector<std::string> file_names, bool is_tree_file = false);
116
117 [[nodiscard]] auto GetEventEnd() const { return event_end_; }
118 [[nodiscard]] auto IsEmpty() const -> bool { return inputDataFiles_.is_empty(); }
119
120 // setters:
121 void SetFileHeaderName(std::string_view fileHeaderName) { inputDataFiles_.SetFileHeaderName(fileHeaderName); }
122 // Set event print refresh rate in Hz
123 void SetEventPrintRefreshRate(float rate) { event_progress_.SetRefreshRate_Hz(rate); }
124 void SetInitRunID(int run_id)
125 {
126 inputDataFiles_.SetRunID(run_id);
127 SetRunId(run_id);
128 }
129
130 private:
131 int event_end_ = 0;
132 R3BInputRootFiles inputDataFiles_;
133 R3BEventProgressPrinter event_progress_;
134 FairEventHeader* evtHeader_ = nullptr;
135 std::vector<R3BInputRootFiles> inputFriendFiles_;
136 std::vector<std::string> dataFileNames_;
137 std::vector<std::string> friendFileNames_;
138
139 Bool_t Init() override;
140 Int_t ReadEvent(UInt_t eventID = 0) override;
141 void Close() override {}
142 void Reset() override {}
143 Bool_t InitUnpackers() override { return kTRUE; }
144 Bool_t ReInitUnpackers() override { return kTRUE; }
145 Source_Type GetSourceType() override { return kFILE; }
146 void SetParUnpackers() override {}
147 Int_t CheckMaxEventNo(Int_t EvtEnd = 0) override;
148 void ReadBranchEvent(const char* BrName) override;
149 void ReadBranchEvent(const char* BrName, Int_t Entry) override;
150 void FillEventHeader(FairEventHeader* evtHeader) override;
151 Bool_t ActivateObject(TObject** obj, const char* BrName) override;
152 Bool_t ActivateObjectAny(void** obj, const std::type_info& info, const char* BrName) override;
153 // WTF is this?
154 Bool_t SpecifyRunId() override { return true; }
155
156 public:
157 ClassDefOverride(R3BFileSource2, 0) // NOLINT
158};
void SetRefreshRate_Hz(float rate)
void SetRunID(unsigned int runID)
R3BEventProgressPrinter()=default
void SetMaxEventNum(unsigned int max_event_num)
void ShowProgress(uint64_t event_num)
Int_t ReadEvent(UInt_t eventID=0) override
Bool_t ActivateObject(TObject **obj, const char *BrName) override
void ReadBranchEvent(const char *BrName) override
void SetFileHeaderName(std::string_view fileHeaderName)
void SetEventPrintRefreshRate(float rate)
void AddFriend(std::string, bool is_tree_file=false)
auto GetEventEnd() const
auto IsEmpty() const -> bool
void Close() override
Int_t CheckMaxEventNo(Int_t EvtEnd=0) override
Bool_t ReInitUnpackers() override
Bool_t ActivateObjectAny(void **obj, const std::type_info &info, const char *BrName) override
void AddFile(std::string file_name, bool is_tree_file=false)
Bool_t SpecifyRunId() override
Bool_t InitUnpackers() override
Source_Type GetSourceType() override
void FillEventHeader(FairEventHeader *evtHeader) override
void Reset() override
void SetInitRunID(int run_id)
Bool_t Init() override
void SetParUnpackers() override
auto GetTitle() const -> const auto &
R3BInputRootFiles()=default
auto GetEntries() const -> int64_t
void SetFriend(R3BInputRootFiles &friendFiles)
R3BInputRootFiles & operator=(const R3BInputRootFiles &)=delete
R3BInputRootFiles(const R3BInputRootFiles &)=delete
auto GetChain() const -> TChain *
R3BInputRootFiles(R3BInputRootFiles &&)=default
auto is_empty() const -> bool
~R3BInputRootFiles()=default
void RegisterTo(FairRootManager *)
auto GetInitialRunID() const
auto GetTreeName() const -> const auto &
auto AddFileName(std::string name, bool is_tree_file=false) -> std::optional< std::string >
void SetInputFileChain(TChain *chain)
void SetRunID(uint run_id)
auto GetFolderName() const -> const auto &
std::vector< std::string > Strings
auto GetBaseFileName() const -> const auto &
void SetTitle(std::string_view title)
R3BInputRootFiles & operator=(R3BInputRootFiles &&)=default
void SetFileHeaderName(std::string_view fileHeader)
void SetTreeName(std::string_view treeName)
auto GetBranchListRef() const -> const auto &
auto is_friend() const -> bool