LDMX Software
Event.cxx
1#include "Framework/Event.h"
2
3#include "TBranchElement.h"
4
5namespace framework {
6
7Event::Event(const std::string& thePassName) : passName_(thePassName) {}
8
10 for (regex_t& reg : regexDropCollections_) {
11 regfree(&reg);
12 }
13}
14
15void Event::Print() const {
16 for (const ProductTag& tag : getProducts()) {
17 std::cout << tag << std::endl;
18 }
19}
20
21void Event::addDrop(const std::string& exp) {
22 regex_t reg;
23 if (!regcomp(&reg, exp.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
24 regexDropCollections_.push_back(reg);
25 } else {
26 EXCEPTION_RAISE("InvalidRegex", "The passed drop rule regex '" + exp +
27 "' is not a valid regex.");
28 }
29}
30
47static regex_t construct_regex(const std::string& pattern,
48 bool full_string_match) {
49 std::string pattern_regex{pattern};
50 if (pattern_regex.empty())
51 pattern_regex = ".*";
52 else if (full_string_match)
53 pattern_regex = "^" + pattern_regex + "$";
54
55 regex_t reg;
56 if (regcomp(&reg, pattern_regex.c_str(),
57 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
58 // use input value in exception since we expect our code above evolving
59 // the regex to be accurate
60 EXCEPTION_RAISE("InvalidRegex", "The passed regex '" + pattern +
61 "' is not a valid regular expression.");
62 }
63 return reg;
64}
65
66std::vector<ProductTag> Event::searchProducts(const std::string& namematch,
67 const std::string& passmatch,
68 const std::string& typematch,
69 bool full_string_match) const {
70 std::vector<ProductTag> retval;
71 regex_t reg_name{construct_regex(namematch, full_string_match)},
72 reg_pass{construct_regex(passmatch, full_string_match)},
73 reg_type{construct_regex(typematch, full_string_match)};
74
75 // all passed expressions are valid regular expressions
76 const std::vector<ProductTag>& products = getProducts();
77 for (std::vector<ProductTag>::const_iterator i = products.begin();
78 i != products.end(); i++) {
79 if (!regexec(&reg_name, i->name().c_str(), 0, 0, 0) &&
80 !regexec(&reg_pass, i->passname().c_str(), 0, 0, 0) &&
81 !regexec(&reg_type, i->type().c_str(), 0, 0, 0))
82 retval.push_back(*i);
83 }
84
85 regfree(&reg_type);
86 regfree(&reg_pass);
87 regfree(&reg_name);
88
89 return retval;
90}
91
92bool Event::exists(const std::string& name, const std::string& passName,
93 bool unique) const {
94 static const bool require_full_string_match = true;
95 auto matches = searchProducts(name, passName, "", require_full_string_match);
96 if (unique)
97 return (matches.size() == 1);
98 else
99 return (matches.size() > 0);
100}
101
103 outputTree_ = new TTree("LDMX_Events", "LDMX Events");
104
105 return outputTree_;
106}
107
108void Event::setOutputTree(TTree* tree) { outputTree_ = tree; }
109
110void Event::setInputTree(TTree* tree) {
111 inputTree_ = tree;
112
113 // in some cases, setInputTree is called more than once,
114 // so reset branch listing before starting
115 products_.clear();
116 knownLookups_.clear(); // reset caching of empty pass requests
118
119 // put in EventHeader (only one without pass name)
120 products_.emplace_back(ldmx::EventHeader::BRANCH, "", "ldmx::EventHeader");
121
122 // find the names of all the existing branches
123 TObjArray* branches = inputTree_->GetListOfBranches();
124 if (!branches) {
125 EXCEPTION_RAISE(
126 "BadInputFile",
127 "Input tree doesn't have a list of branches but still made it to this "
128 "point. This is bad and has never been seen before!");
129 return;
130 }
131 for (int i = 0; i < branches->GetEntriesFast(); i++) {
132 std::string brname;
133 if (branches->At(i)) {
134 brname = branches->At(i)->GetName();
135 }
136 if (brname != ldmx::EventHeader::BRANCH) {
137 size_t j = brname.find("_");
138 auto br = dynamic_cast<TBranchElement*>(branches->At(i));
139 // can't determine type if branch isn't
140 // the higher-level TBranchElement type
141 // Only occurs if the type on the bus is one of:
142 // bool, short, int, long, float, double (BSILFD)
143 products_.emplace_back(
144 brname.substr(0, j), // collection name is before '_'
145 brname.substr(j + 1), // pass name is after
146 br ? br->GetClassName() : "BSILFD");
147 }
148 }
149}
150
152 eventHeader_ = getObject<ldmx::EventHeader>(ldmx::EventHeader::BRANCH);
153 return true;
154}
155
158 branchesFilled_.end()) {
159 // Event Header not copied from input and hasn't been added yet, need to put
160 // it in
162 }
163}
164
166 branchesFilled_.clear(); // forget names of branches we filled
167 bus_.clear(); // clear the event objects individually but leave them on bus
168}
169
171
173 if (outputTree_)
174 outputTree_->ResetBranchAddresses(); // reset addresses for output branch
175 if (inputTree_)
176 inputTree_ = nullptr; // detach old inputTree (owned by EventFile)
177 knownLookups_.clear(); // reset caching of empty pass requests
178 bus_.everybodyOff(); // delete buffer objects
179}
180
181bool Event::shouldDrop(const std::string& branchName) const {
182 for (const regex_t& exp : regexDropCollections_) {
183 if (!regexec(&exp, branchName.c_str(), 0, 0, 0)) return true;
184 }
185 return false;
186}
187
188} // namespace framework
Class implementing an event buffer system for storing event data.
void clear()
Reset the objects carried by the passengers.
Definition Bus.h:126
void everybodyOff()
Kicks all of the passengers off the bus and therefore destroys any objects they are carrying.
Definition Bus.h:140
void Print() const
Print event bus.
Definition Event.cxx:15
std::vector< ProductTag > products_
List of all the event products.
Definition Event.h:570
std::vector< ProductTag > searchProducts(const std::string &namematch, const std::string &passmatch, const std::string &typematch, bool full_string_match=false) const
Get a list of products which match the given POSIX-Extended, case-insenstive regular-expressions.
Definition Event.cxx:66
std::vector< regex_t > regexDropCollections_
Regex of collection names to not store in event.
Definition Event.h:560
std::map< std::string, std::string > knownLookups_
Efficiency cache for empty pass name lookups.
Definition Event.h:565
~Event()
Class destructor.
Definition Event.cxx:9
void onEndOfFile()
Perform end of file action.
Definition Event.cxx:172
TTree * createTree()
Create the output data tree.
Definition Event.cxx:102
TTree * inputTree_
The input tree for reading existing data.
Definition Event.h:532
void setOutputTree(TTree *tree)
Set the output data tree.
Definition Event.cxx:108
void beforeFill()
Action to be executed before the tree is filled.
Definition Event.cxx:156
void onEndOfEvent()
Perform end of event action (doesn't do anything right now).
Definition Event.cxx:170
void Clear()
Clear this object's data (including passengers).
Definition Event.cxx:165
const std::vector< ProductTag > & getProducts() const
Get a list of the data products in the event.
Definition Event.h:431
TTree * outputTree_
The output tree for writing a new file.
Definition Event.h:527
ldmx::EventHeader eventHeader_
The event header object.
Definition Event.h:517
framework::Bus bus_
The Bus.
Definition Event.h:545
Event(const std::string &passName)
Class constructor.
Definition Event.cxx:7
bool nextEvent()
Go to the next event by retrieving the event header.
Definition Event.cxx:151
std::set< std::string > branchesFilled_
Names of branches filled during this event.
Definition Event.h:555
void setInputTree(TTree *tree)
Set the input data tree.
Definition Event.cxx:110
void add(const std::string &collectionName, T &obj)
Adds an object to the event bus.
Definition Event.h:173
bool shouldDrop(const std::string &collName) const
Check if collection should be dropped.
Definition Event.cxx:181
bool exists(const std::string &name, const std::string &passName="", bool unique=true) const
Check for the existence of an object or collection with the given name and pass name in the event.
Definition Event.cxx:92
void addDrop(const std::string &exp)
Add a drop rule to the list of regex expressions to drop.
Definition Event.cxx:21
Defines the identity of a product and can be used for searches.
Definition ProductTag.h:23
static const std::string BRANCH
Name of EventHeader branch.
Definition EventHeader.h:49
All classes in the ldmx-sw project use this namespace.
Definition PerfDict.cxx:45
static regex_t construct_regex(const std::string &pattern, bool full_string_match)
Construct an actual regex from the pass pattern (and full-string flag)
Definition Event.cxx:47