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 "R3BEventHeader.h"
17#include "R3BShared.h"
18#include <FairFileSourceBase.h>
19#include <FairMCEventHeader.h>
20#include <FairSource.h>
21#include <Rtypes.h>
22#include <RtypesCore.h>
23#include <TDirectory.h>
24#include <TFile.h>
25#include <TObjString.h>
26#include <TObject.h>
27#include <chrono>
28#include <cstdint>
29#include <optional>
30#include <string>
31#include <string_view>
32#include <sys/types.h>
33#include <typeinfo>
34#include <vector>
35
36class FairRootManager;
37class TChain;
38class TFolder;
39
41{
42 public:
43 enum class Mode : uint8_t
44 {
47 };
49 void SetRunID(unsigned int runID) { run_id_ = runID; }
50 void SetMaxEventNum(unsigned int max_event_num) { max_event_num_ = max_event_num; }
51 void SetRefreshRate_Hz(float rate);
52 void ShowProgress(uint64_t event_num);
53
54 private:
56 uint64_t max_event_num_ = 0;
57 float refresh_rate_ = 2.; // Hz
58 std::chrono::milliseconds refresh_period_{ static_cast<int>(1000. / refresh_rate_) };
59 unsigned int run_id_ = 0;
60 std::chrono::time_point<std::chrono::steady_clock> begin_t_ = std::chrono::steady_clock::now();
61 std::chrono::time_point<std::chrono::steady_clock> previous_t_ = std::chrono::steady_clock::now();
62 uint64_t previous_event_num_ = 0;
63
64 void Print(uint64_t event_num, double speed_per_ms);
65};
66
68{
69 public:
70 using Strings = std::vector<std::string>;
71 R3BInputRootFiles() = default;
72 auto AddFileName(std::string name, bool is_tree_file = false) -> std::optional<std::string>;
73 void SetInputFileChain(TChain* chain);
74 void RegisterTo(FairRootManager*);
75 [[nodiscard]] auto is_empty() const -> bool { return fileNames_.empty(); }
76
77 [[nodiscard]] auto is_friend() const -> bool { return is_friend_; }
78 void Make_as_friend() { is_friend_ = true; }
79 void SetFriend(R3BInputRootFiles& friendFiles);
80 // Getters:
81 [[nodiscard]] auto GetBranchListRef() const -> const auto& { return branchList_; }
82 [[nodiscard]] auto GetBaseFileName() const -> const auto& { return fileNames_.front(); }
83 [[nodiscard]] auto GetTreeName() const -> const auto& { return treeName_; }
84 [[nodiscard]] auto GetFolderName() const -> const auto& { return folderName_; }
85 [[nodiscard]] auto GetTitle() const -> const auto& { return title_; }
86 [[nodiscard]] auto GetEntries() const -> int64_t;
87 [[nodiscard]] auto GetChain() const -> TChain* { return rootChain_; }
88 [[nodiscard]] auto GetInitialRunID() const { return initial_RunID_; }
89
90 // Setters:
91 void SetTreeName(std::string_view treeName) { treeName_ = treeName; }
92 void SetTitle(std::string_view title) { title_ = title; }
93 void SetFileHeaderName(std::string_view fileHeader) { fileHeader_ = fileHeader; }
94 void SetRunID(int run_id) { initial_RunID_ = run_id; }
95
96 // rule of five:
97 ~R3BInputRootFiles() = default;
102
103 private:
104 bool is_friend_ = false;
106 // TODO: title of each file group seems not necessary. Consider to remove it in the future.
107 std::string title_;
108 std::string treeName_ = "evt";
109 std::string folderName_;
110 std::string fileHeader_;
113 std::vector<TObjString> timeBasedBranchList_;
114 std::vector<R3B::unique_rootfile> validRootFiles_;
115 std::vector<TFolder*> validMainFolders_;
116 TChain* rootChain_ = nullptr;
117
118 void Intitialize(std::string_view filename, bool is_tree_file = false);
119 auto ValidateFile(const std::string& filename, bool is_tree_file = false) -> bool;
120 static auto ExtractMainFolder(TFile*) -> std::optional<TKey*>;
121 auto ExtractRunId(TFile* rootFile) -> std::optional<int>;
123};
124
125class R3BFileSource2 : public FairFileSourceBase
126{
127 public:
129 explicit R3BFileSource2(std::string file, std::string_view title = "InputRootFile");
130 R3BFileSource2(std::vector<std::string> fileNames, std::string_view title);
131 explicit R3BFileSource2(std::vector<std::string> fileNames);
132
133 void AddFile(std::string file_name, bool is_tree_file = false);
134 void AddFile(std::vector<std::string> file_names, bool is_tree_file = false);
135 void AddFriend(std::string, bool is_tree_file = false);
136 void AddFriend(std::vector<std::string> file_names, bool is_tree_file = false);
137
138 [[nodiscard]] auto GetEventEnd() const { return event_end_; }
139 [[nodiscard]] auto IsEmpty() const -> bool { return inputDataFiles_.is_empty(); }
140
141 // setters:
142 void SetFileHeaderName(std::string_view fileHeaderName) { inputDataFiles_.SetFileHeaderName(fileHeaderName); }
143 // Set event print refresh rate in Hz
144 void SetEventPrintRefreshRate(float rate) { event_progress_.SetRefreshRate_Hz(rate); }
145 void SetInitRunID(int run_id)
146 {
147 inputDataFiles_.SetRunID(run_id);
148 SetRunId(run_id);
149 }
150
151 private:
152 int event_end_ = 0;
156 FairMCEventHeader* mc_event_header_ = nullptr;
157 std::vector<R3BInputRootFiles> inputFriendFiles_;
158 std::vector<std::string> dataFileNames_;
159 std::vector<std::string> friendFileNames_;
160
161 Bool_t Init() override;
162 Int_t ReadEvent(UInt_t eventID = 0) override;
163 void Close() override {}
164 void Reset() override {}
165 Bool_t InitUnpackers() override { return kTRUE; }
166 Bool_t ReInitUnpackers() override { return kTRUE; }
167 Source_Type GetSourceType() override { return kFILE; }
168 void SetParUnpackers() override {}
169 Int_t CheckMaxEventNo(Int_t EvtEnd = 0) override;
170 void ReadBranchEvent(const char* BrName) override;
171 void ReadBranchEvent(const char* BrName, Int_t Entry) override;
172 void FillEventHeader(FairEventHeader* evtHeader) override;
173 Bool_t ActivateObject(TObject** obj, const char* BrName) override;
174 Bool_t ActivateObjectAny(void** obj, const std::type_info& info, const char* BrName) override;
175 // WTF is this?
176 Bool_t SpecifyRunId() override { return true; }
177
178 public:
179 ClassDefOverride(R3BFileSource2, 0) // NOLINT
180};
void SetRefreshRate_Hz(float rate)
void SetRunID(unsigned int runID)
std::chrono::milliseconds refresh_period_
R3BEventProgressPrinter()=default
void SetMaxEventNum(unsigned int max_event_num)
std::chrono::time_point< std::chrono::steady_clock > begin_t_
void Print(uint64_t event_num, double speed_per_ms)
void ShowProgress(uint64_t event_num)
std::chrono::time_point< std::chrono::steady_clock > previous_t_
Int_t ReadEvent(UInt_t eventID=0) override
Bool_t ActivateObject(TObject **obj, const char *BrName) override
void ReadBranchEvent(const char *BrName) override
R3BEventProgressPrinter event_progress_
void SetFileHeaderName(std::string_view fileHeaderName)
void SetEventPrintRefreshRate(float rate)
std::vector< R3BInputRootFiles > inputFriendFiles_
void AddFriend(std::string, bool is_tree_file=false)
FairMCEventHeader * mc_event_header_
auto GetEventEnd() const
auto IsEmpty() const -> bool
void Close() override
Int_t CheckMaxEventNo(Int_t EvtEnd=0) override
Bool_t ReInitUnpackers() override
R3BInputRootFiles inputDataFiles_
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)
std::vector< std::string > dataFileNames_
Bool_t SpecifyRunId() override
Bool_t InitUnpackers() override
Source_Type GetSourceType() override
void FillEventHeader(FairEventHeader *evtHeader) override
std::vector< std::string > friendFileNames_
void Reset() override
void ReadBranchEvent(const char *BrName, Int_t Entry) override
void SetInitRunID(int run_id)
R3BEventHeader * r3b_event_header_
Bool_t Init() override
void SetParUnpackers() override
static auto ExtractMainFolder(TFile *) -> std::optional< TKey * >
std::vector< TObjString > timeBasedBranchList_
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
std::string folderName_
auto is_empty() const -> bool
void Intitialize(std::string_view filename, bool is_tree_file=false)
~R3BInputRootFiles()=default
void RegisterTo(FairRootManager *)
std::string fileHeader_
auto GetInitialRunID() const
auto GetTreeName() const -> const auto &
auto AddFileName(std::string name, bool is_tree_file=false) -> std::optional< std::string >
std::vector< TFolder * > validMainFolders_
void SetInputFileChain(TChain *chain)
auto ExtractRunId(TFile *rootFile) -> std::optional< int >
auto GetFolderName() const -> const auto &
auto ValidateFile(const std::string &filename, bool is_tree_file=false) -> bool
std::vector< std::string > Strings
std::string treeName_
auto GetBaseFileName() const -> const auto &
std::vector< R3B::unique_rootfile > validRootFiles_
void SetRunID(int run_id)
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