hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
PreselectAndCategorize.h
Go to the documentation of this file.
1/*
2 * @file PreselectAndCategorize.h
3 * @author Tom Eichlersmith
4 * @date Dec 2024
5 * @author Sarah Gaiser
6 * @date Apr 2025
7 */
8
9#ifndef __PRESELECT_AND_CATEGORIZE_H__
10#define __PRESELECT_AND_CATEGORIZE_H__
11
12#include <type_traits>
13#include <vector>
14
15// HPSTR
16#include "HpsEvent.h"
17#include "Collections.h"
18#include "EventHeader.h"
19#include "TSData.h"
20#include "Vertex.h"
21#include "Track.h"
22#include "TrackerHit.h"
23#include "MCParticle.h"
24#include "Particle.h"
25#include "Processor.h"
26#include "BaseSelector.h"
27#include "TrackHistos.h"
28#include "MCAnaHistos.h"
29#include "utilities.h"
30
31#include "TrackSmearingTool.h"
32#include "FlatTupleMaker.h"
33#include "AnaHelpers.h"
34
35#include "Bus.h"
36
37// ROOT
38#include "TFile.h"
39#include "TTree.h"
40#include "TRefArray.h"
41#include "TBranch.h"
42#include "TVector3.h"
43#include "TLorentzVector.h"
44#include "TAxis.h"
45#include <optional>
46
47// C++
48#include <memory>
49#include <iostream>
50#include <fstream>
51#include <map>
52
53
54// json json_load(const std::string& filepath) {
55// json obj;
56// std::ifstream file{filepath};
57// file >> obj;
58// file.close();
59// return obj;
60// }
61
62
63class Cutflow {
64
65 public:
66 Cutflow(const std::string& name, const std::string& nocut_name):
67 name_{name}, nocut_name_{nocut_name} {}
68
69 void set_label_names(const std::vector<std::string>& label_names) {
70 label_names_ = label_names;
71 }
72
73 void add(const std::string& name, int nbins, float min, float max) {
74 if (h_cutflow_) {
75 throw std::runtime_error("Cannot add more cuts after init");
76 }
77
78 auto h = std::make_unique<TH1F>(
79 ("nm1_" + name + "_h").c_str(),
80 (name + " N-1").c_str(),
81 nbins, min, max);
82
83 cuts_.emplace(name, std::make_pair<std::size_t, std::unique_ptr<TH1F>>(cuts_.size(), std::move(h)));
84 }
85
86 void init() {
87 if (h_cutflow_) {
88 throw std::runtime_error("Cannot init twice.");
89 }
90
91 h_cutflow_ = std::make_unique<TH1F>(
92 (name_ + "_cutflow_h").c_str(),
93 (name_ + " cutflow;;N_{events}").c_str(),
94 cuts_.size()+1, -1.5, cuts_.size()-0.5
95 );
96
97 cut_desc_.resize(cuts_.size());
98 }
99
100 void begin_event() {
101 h_cutflow_->Fill(nocut_name_.c_str(), 1.);
102 keep_ = true;
103 for (auto& d : cut_desc_) d.reset();
104 }
105
106 void apply(const std::string& name, bool descision) {
107 if (cuts_.find(name) == cuts_.end()) {
108 throw std::runtime_error("Cut "+name+" not `add`ed to cutflow.");
109 }
110
111 keep_ = (keep_ and descision);
112 if (keep_) h_cutflow_->Fill(name.c_str(), 1.);
113 int i_cut = cuts_.at(name).first;
114 cut_desc_[i_cut] = descision;
115 }
116
117 template<typename T>
118 void fill_nm1(const std::string& name, T value) {
119 bool should_fill{true};
120
121 for (std::size_t i_cut{0}; i_cut < cuts_.size(); i_cut++) {
122 if (i_cut != cuts_[name].first and cut_desc_[i_cut].has_value()) {
123 should_fill = (should_fill and cut_desc_[i_cut].value());
124 }
125 }
126
127 if (should_fill) {
128 cuts_.at(name).second->Fill(value);
129 }
130 }
131
132 bool keep() const {
133 return keep_;
134 }
135
136 void save() {
137 // if label names not set or size mismatch, just skip setting labels
138 for (int i = 0; i < h_cutflow_->GetNbinsX(); ++i) {
139 if (label_names_.size() == (std::size_t)h_cutflow_->GetNbinsX()) {
140 h_cutflow_->GetXaxis()->SetBinLabel(i+1, label_names_.at(i).c_str());
141 }
142 }
143 h_cutflow_->Write();
144 for (const auto& [_name, entry] : cuts_) {
145 TH1F* h = entry.second.get();
146 // Fold the under/overflow into the first/last visible bins so tails beyond the
147 // axis range (e.g. events failing a cut placed at the high edge of the N-1 range)
148 // stay visible instead of disappearing into overflow.
149 int nb = h->GetNbinsX();
150 h->SetBinContent(1, h->GetBinContent(0) + h->GetBinContent(1));
151 h->SetBinContent(0, 0.);
152 h->SetBinContent(nb, h->GetBinContent(nb) + h->GetBinContent(nb + 1));
153 h->SetBinContent(nb + 1, 0.);
154 h->Write();
155 }
156 }
157
158 private:
159 std::string name_, nocut_name_;
160 std::unique_ptr<TH1F> h_cutflow_;
161 std::map<std::string,std::pair<std::size_t,std::unique_ptr<TH1F>>> cuts_;
162 bool keep_;
163 std::vector<std::optional<bool>> cut_desc_;
164 std::vector<std::string> label_names_;
165};
166
167class EventBus {
170 std::vector<std::string> clearable_output_names_;
171
182 template<typename T>
183 struct auto_clearable : std::is_arithmetic<T> {};
184 template<typename U>
185 struct auto_clearable<std::vector<U>> : std::true_type {};
186
187 public:
188 bool has(const std::string& name) {
189 return bus_.isOnBoard(name);
190 }
191
209 for (const auto& name : clearable_output_names_) bus_.clear(name);
210 }
211
212 template<typename BaggageType>
213 void board_input(TTree* tree, const std::string& name) {
214 if (bus_.isOnBoard(name)) {
215 throw std::runtime_error(name + " is already on board the bus.");
216 }
217
218 bus_.board<BaggageType>(name);
219 if (bus_.attach(tree, name, false /* create if missing */) == 0) {
220 throw std::runtime_error("Unable to find branch " + name + " in input TTree.");
221 }
222 }
223
224 template<typename BaggageType>
225 void board_output(TTree* tree, const std::string& name) {
226 if (bus_.isOnBoard(name)) {
227 throw std::runtime_error(name + " is already on board the bus.");
228 }
229 bus_.board<BaggageType>(name);
230 if (bus_.attach(tree, name, true /* create if missing */) == 0) {
231 throw std::runtime_error("Unable to create branch " + name + " in output TTree.");
232 }
234 clearable_output_names_.push_back(name);
235 }
236 }
237
238 template<typename BaggageType>
239 const BaggageType& get(const std::string& name) {
240 return bus_.get<BaggageType>(name);
241 }
242
243 template<typename BaggageType>
244 void set(const std::string& name, const BaggageType& obj) {
245 if (bus_.isOnBoard(name)) {
246 bus_.update(name, obj);
247 return;
248 }
249 throw std::runtime_error("No branch " + name + " boarded on the bus.");
250 }
251};
252
254 public:
255 PreselectAndCategorize(const std::string& name, Process& process):
256 Processor(name,process) {}
258 virtual void configure(const ParameterSet& parameters);
259 virtual void initialize(TTree* tree);
260 virtual void setFile(TFile* out_file);
261 virtual bool process(IEvent* ievent);
262 virtual void finalize();
263
264 virtual void setVtxColl(std::string vtx_coll) {
265 vtxColl_ = vtx_coll;
266 }
267 virtual void setMCColl(std::string mc_coll) {
268 mcColl_ = mc_coll;
269 }
270
271 json preselect_json_load(const std::string& filepath) {
272 json obj;
273 std::ifstream file{filepath};
274 file >> obj;
275 file.close();
276 return obj;
277 }
278
279 bool getIsSignal() const {
280 return isSignal_;
281 }
282
283 bool getIsData() const {
284 return isData_;
285 }
286
287 protected:
289 std::unique_ptr<TTree> output_tree_;
290
291 std::string vtxColl_{"UnconstrainedV0Vertices_KF"};
292 std::string mcColl_{"MCParticle"};
293 bool isData_{false};
294 bool isSignal_{false};
295 double calTimeOffset_{0.0};
296
297 Cutflow vertex_cf_{"vertex", "reconstructed"};
298 Cutflow event_cf_{"event", "readout"};
299 std::unique_ptr<TH2F> n_vertices_h_;
300 std::shared_ptr<TrackSmearingTool> smearingTool_;
301 std::shared_ptr<AnaHelpers> _ah;
302
306 std::map<int,std::vector<double>> beamspot_corrections_;
307
308 // config for projecting vertex candidates back to target
310
315 std::map<std::string,double> track_corrections_;
316};
317
318#endif //__PRESELECT_AND_CATEGORIZE_H__
Helper class for hipster analysis.
nlohmann::json json
Class used to encapsulate event information.
Class used to encapsulate information about a mc particle.
Class used to encapsulate information about a particle.
Base classes for all user event processing components to extend.
Class used to decode TS words.
Class used to encapsulate track information.
Class used to encapsulate tracker hit information.
Class used to encapsulate Vertex information.
void apply(const std::string &name, bool descision)
bool keep() const
std::map< std::string, std::pair< std::size_t, std::unique_ptr< TH1F > > > cuts_
std::unique_ptr< TH1F > h_cutflow_
std::vector< std::string > label_names_
Cutflow(const std::string &name, const std::string &nocut_name)
std::string name_
void add(const std::string &name, int nbins, float min, float max)
void fill_nm1(const std::string &name, T value)
std::vector< std::optional< bool > > cut_desc_
void set_label_names(const std::vector< std::string > &label_names)
std::string nocut_name_
void board_output(TTree *tree, const std::string &name)
std::vector< std::string > clearable_output_names_
names of the output passengers that are safe to reset each event
framework::Bus bus_
void board_input(TTree *tree, const std::string &name)
void set(const std::string &name, const BaggageType &obj)
const BaggageType & get(const std::string &name)
bool has(const std::string &name)
Definition IEvent.h:7
description
std::shared_ptr< TrackSmearingTool > smearingTool_
virtual bool process(IEvent *ievent)
Process the event and put new data products into it.
virtual void configure(const ParameterSet &parameters)
Callback for the Processor to configure itself from the given set of parameters.
virtual void setVtxColl(std::string vtx_coll)
virtual void finalize()
Callback for the Processor to take any necessary action when the processing of events finishes,...
virtual void initialize(TTree *tree)
Callback for the Processor to take any necessary action when the processing of events starts,...
json preselect_json_load(const std::string &filepath)
virtual void setMCColl(std::string mc_coll)
std::string mcColl_
description
std::unique_ptr< TH2F > n_vertices_h_
virtual void setFile(TFile *out_file)
Set output TFile for AnaProcessors.
std::shared_ptr< AnaHelpers > _ah
description
std::map< int, std::vector< double > > beamspot_corrections_
PreselectAndCategorize(const std::string &name, Process &process)
~PreselectAndCategorize()=default
std::map< std::string, double > track_corrections_
std::unique_ptr< TTree > output_tree_
Processor(const std::string &name, Process &process)
Class constructor.
Definition Processor.cxx:10
virtual bool process()
Process the histograms and generate analysis output.
Definition Processor.h:95