AnalysisTree
Loading...
Searching...
No Matches
GenericContainerFiller.hpp
1//
2// Created by oleksii on 09.04.25.
3//
4
5#ifndef ANALYSISTREE_GENERICCONTAINERFILLER_HPP
6#define ANALYSISTREE_GENERICCONTAINERFILLER_HPP
7
8#include "Configuration.hpp"
9#include "Container.hpp"
10#include "Detector.hpp"
11
12#include <TChain.h>
13#include <TFile.h>
14
15#include <string>
16#include <vector>
17
18namespace AnalysisTree {
19
20struct IndexMap {
21 std::string name_;
22 std::string field_type_;
23 short index_;
24};
25
29struct LeafBuffer {
30 double double_{-199.};
31 float float_{-199.f};
32 int int_{-199};
33 char char_{static_cast<char>(-199)};
34 short short_{static_cast<short>(-199)};
35
36 double get() const {
37 if (std::fabs(double_ + 199.) > 1e-4) return double_;
38 if (std::fabs(float_ + 199.f) > 1e-4) return float_;
39 if (int_ != -199) return static_cast<double>(int_);
40 if (char_ != static_cast<char>(-199)) return static_cast<double>(char_);
41 if (short_ != static_cast<short>(-199)) return static_cast<double>(short_);
42 throw std::runtime_error("GenericContainerFiller, LeafBuffer::get(): none of values initialized");
43 }
44};
45
46class GenericContainerFiller {
47 public:
48 GenericContainerFiller() = delete;
49 explicit GenericContainerFiller(std::string fileInName, std::string treeInName = "pTree");
50 virtual ~GenericContainerFiller() = default;
51
52 void SetOutputFileName(const std::string& name) { file_out_name_ = name; }
53 void SetOutputTreeName(const std::string& name) { tree_out_name_ = name; }
54 void SetOutputBranchName(const std::string& name) { branch_out_name_ = name; }
55
56 void SetFieldsToIgnore(const std::vector<std::string>& fields) { fields_to_ignore_ = fields; }
57 void SetFieldsToPreserve(const std::vector<std::string>& fields) { fields_to_preserve_ = fields; }
58
59 void SetEntrySwitchTriggerVarName(const std::string& name) { entry_switch_trigger_var_name_ = name; }
60
61 void SetNChannelsPerEntry(int n) { n_channels_per_entry_ = n; }
62
63 void Run(int nEntries = -1);
64
65 protected:
66 void Init();
67 int Exec(int iEntry, int previousTriggerVar);
68 void Finish();
69
70 static int DetermineFieldIdByName(const std::vector<IndexMap>& iMap, const std::string& name);
71 void SetLeafAddress(const std::string& branchName, const IndexMap& imap, LeafBuffer& buffer);
72 static void SetFieldsFromLeaves(const std::vector<IndexMap>& imap, AnalysisTree::Container& container, const std::vector<LeafBuffer>& buffers);
73
74 std::string file_in_name_;
75 std::string tree_in_name_;
76
77 std::string file_out_name_{"AnalysisTree.root"};
78 std::string tree_out_name_{"aTree"};
79 std::string branch_out_name_{"PlainBranch"};
80
81 TChain* tree_in_{nullptr};
82 TFile* file_out_{nullptr};
83 TTree* tree_out_{nullptr};
84
86 AnalysisTree::GenericDetector* generic_detector_{nullptr};
87 std::vector<IndexMap> branch_map_;
88 std::vector<LeafBuffer> branch_values_;
89
90 // variable, change of value of which triggers switch to a new AT event
91 std::string entry_switch_trigger_var_name_;
92
93 int entry_switch_trigger_id_{-1};
94
95 // if entry_switch_trigger_var_name_ is not empty, this field does not matter
96 // if entry_switch_trigger_var_name_ is empty, sets how many AT channels
97 // will constitute a single AT entry (event)
98 int n_channels_per_entry_{-1};
99
100 std::vector<std::string> fields_to_ignore_{};
101 std::vector<std::string> fields_to_preserve_{};
102};
103}// namespace AnalysisTree
104#endif//ANALYSISTREE_GENERICCONTAINERFILLER_HPP
A class to store configuration of the whole AnalysisTree object.
Definition Configuration.hpp:58
A class to store any number of doubles, floats, integers and bools.
Definition Container.hpp:18
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10
Definition GenericContainerFiller.hpp:20
Definition GenericContainerFiller.hpp:29