Skip to content

Commit dc6de2c

Browse files
author
bhgomes
committed
update helper file
1 parent bad96b2 commit dc6de2c

File tree

1 file changed

+108
-2
lines changed

1 file changed

+108
-2
lines changed

studies/helper.hh

+108-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ inline const std::string prototype_detector_decode(const std::size_t id) {
6565
}
6666
//----------------------------------------------------------------------------------------------
6767

68+
namespace io { /////////////////////////////////////////////////////////////////////////////////
69+
6870
namespace { ////////////////////////////////////////////////////////////////////////////////////
6971
//__Recursive TSystemFile Traversal_____________________________________________________________
7072
void _collect_paths(TSystemDirectory* dir,
@@ -86,13 +88,45 @@ void _collect_paths(TSystemDirectory* dir,
8688
} /* anonymous namespace */ ////////////////////////////////////////////////////////////////////
8789

8890
//__Search and Collect ROOT File Paths__________________________________________________________
89-
inline std::vector<std::string> search_directory(const std::string& path) {
91+
inline std::vector<std::string> search_directory(const std::string& path,
92+
const std::string& ext="root") {
9093
std::vector<std::string> paths{};
91-
_collect_paths(new TSystemDirectory("data", path.c_str()), paths, "root");
94+
_collect_paths(new TSystemDirectory("data", path.c_str()), paths, ext);
9295
return paths;
9396
}
9497
//----------------------------------------------------------------------------------------------
9598

99+
//__Save TObjects to File_______________________________________________________________________
100+
void save_objects(TFile* file,
101+
const TObject* object) {
102+
file->WriteTObject(object);
103+
}
104+
template<class ...Args>
105+
void save_objects(TFile* file,
106+
const TObject* object,
107+
Args&& ...args) {
108+
save_objects(file, object);
109+
save_objects(file, std::forward<Args>(args)...);
110+
}
111+
//----------------------------------------------------------------------------------------------
112+
113+
//__Run Function While File is Open_____________________________________________________________
114+
template<class UnaryFunction>
115+
UnaryFunction while_open(const std::string& path,
116+
const std::string& mode,
117+
UnaryFunction f) {
118+
auto file = TFile::Open(path.c_str(), mode.c_str());
119+
if (file && !file->IsZombie()) {
120+
file->cd();
121+
f(file);
122+
file->Close();
123+
}
124+
return std::move(f);
125+
}
126+
//----------------------------------------------------------------------------------------------
127+
128+
} /* namespace io */ ///////////////////////////////////////////////////////////////////////////
129+
96130
namespace string { /////////////////////////////////////////////////////////////////////////////
97131

98132
//__Split String on Delimeters__________________________________________________________________
@@ -144,6 +178,8 @@ inline std::string& strip(std::string& string) {
144178

145179
} /* namespace string */ ///////////////////////////////////////////////////////////////////////
146180

181+
namespace hist { ///////////////////////////////////////////////////////////////////////////////
182+
147183
//__Convert 1D Histogram to CSV File____________________________________________________________
148184
inline bool to_csv(const std::string& path,
149185
const TH1* hist,
@@ -214,6 +250,10 @@ inline bool to_csv(const std::string& path,
214250
}
215251
//----------------------------------------------------------------------------------------------
216252

253+
} /* namespace hist */ /////////////////////////////////////////////////////////////////////////
254+
255+
namespace tree { ///////////////////////////////////////////////////////////////////////////////
256+
217257
//__Convert CSV File to TTree___________________________________________________________________
218258
inline TTree* from_csv(const std::string& name,
219259
const std::string& path,
@@ -225,6 +265,72 @@ inline TTree* from_csv(const std::string& name,
225265
}
226266
//----------------------------------------------------------------------------------------------
227267

268+
//__Get Tree from File__________________________________________________________________________
269+
TTree* get(TFile* file,
270+
const std::string& name) {
271+
return dynamic_cast<TTree*>(file->Get(name.c_str()));
272+
}
273+
//----------------------------------------------------------------------------------------------
274+
275+
//__Set TTree Branch Base Function______________________________________________________________
276+
void set_addresses(TTree* tree,
277+
const std::string& name,
278+
void* address) {
279+
tree->SetBranchAddress(name.c_str(), address);
280+
}
281+
template<class... Args>
282+
void set_addresses(TTree* tree,
283+
const std::string& name,
284+
void* address,
285+
Args&& ...args) {
286+
set_addresses(tree, name, address);
287+
set_addresses(tree, std::forward<Args>(args)...);
288+
}
289+
//----------------------------------------------------------------------------------------------
290+
291+
//__Linear Traverse Entries in TTree____________________________________________________________
292+
template<class PreCondition, class Function>
293+
std::pair<PreCondition, Function> traverse(std::vector<TTree*> trees,
294+
PreCondition pre_condition,
295+
Function f) {
296+
const auto size = trees.size();
297+
std::vector<int64_t> size_vector;
298+
size_vector.reserve(size);
299+
for (const auto& tree : trees) {
300+
const auto entries = tree->GetEntries();
301+
if (!pre_condition(entries)) {
302+
return std::make_pair(std::move(pre_condition), std::move(f));
303+
}
304+
size_vector.push_back(entries);
305+
}
306+
307+
std::size_t counter{};
308+
bool active = true;
309+
for (; active; ++counter) {
310+
active = false;
311+
for (std::size_t i{}; i < size; ++i) {
312+
if (size_vector[i] > counter) {
313+
trees[i]->GetEntry(counter);
314+
active = true;
315+
}
316+
}
317+
f(counter);
318+
}
319+
320+
return std::make_pair(std::move(pre_condition), std::move(f));
321+
}
322+
//----------------------------------------------------------------------------------------------
323+
324+
//__Linear Traverse Entries in TTree____________________________________________________________
325+
template<class Function>
326+
Function traverse(std::vector<TTree*> trees,
327+
Function f) {
328+
return traverse(trees, [](const int64_t) { return true; }, f).second;
329+
}
330+
//----------------------------------------------------------------------------------------------
331+
332+
} /* namespace tree */ /////////////////////////////////////////////////////////////////////////
333+
228334
} /* namespace helper */ ///////////////////////////////////////////////////////////////////////
229335

230336
} } /* namespace MATHUSLA::MU */

0 commit comments

Comments
 (0)