@@ -65,6 +65,8 @@ inline const std::string prototype_detector_decode(const std::size_t id) {
65
65
}
66
66
// ----------------------------------------------------------------------------------------------
67
67
68
+ namespace io { // ///////////////////////////////////////////////////////////////////////////////
69
+
68
70
namespace { // //////////////////////////////////////////////////////////////////////////////////
69
71
// __Recursive TSystemFile Traversal_____________________________________________________________
70
72
void _collect_paths (TSystemDirectory* dir,
@@ -86,13 +88,45 @@ void _collect_paths(TSystemDirectory* dir,
86
88
} /* anonymous namespace */ // //////////////////////////////////////////////////////////////////
87
89
88
90
// __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" ) {
90
93
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 );
92
95
return paths;
93
96
}
94
97
// ----------------------------------------------------------------------------------------------
95
98
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
+
96
130
namespace string { // ///////////////////////////////////////////////////////////////////////////
97
131
98
132
// __Split String on Delimeters__________________________________________________________________
@@ -144,6 +178,8 @@ inline std::string& strip(std::string& string) {
144
178
145
179
} /* namespace string */ // /////////////////////////////////////////////////////////////////////
146
180
181
+ namespace hist { // /////////////////////////////////////////////////////////////////////////////
182
+
147
183
// __Convert 1D Histogram to CSV File____________________________________________________________
148
184
inline bool to_csv (const std::string& path,
149
185
const TH1* hist,
@@ -214,6 +250,10 @@ inline bool to_csv(const std::string& path,
214
250
}
215
251
// ----------------------------------------------------------------------------------------------
216
252
253
+ } /* namespace hist */ // ///////////////////////////////////////////////////////////////////////
254
+
255
+ namespace tree { // /////////////////////////////////////////////////////////////////////////////
256
+
217
257
// __Convert CSV File to TTree___________________________________________________________________
218
258
inline TTree* from_csv (const std::string& name,
219
259
const std::string& path,
@@ -225,6 +265,72 @@ inline TTree* from_csv(const std::string& name,
225
265
}
226
266
// ----------------------------------------------------------------------------------------------
227
267
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
+
228
334
} /* namespace helper */ // /////////////////////////////////////////////////////////////////////
229
335
230
336
} } /* namespace MATHUSLA::MU */
0 commit comments