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