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
PreselectAndCategorizeSimp2016.cxx
Go to the documentation of this file.
1/*
2* @file PreselectAndCategorizeSimp2016.cxx
3* @author Tom Eichlersmith
4* @date Dec 2024
5* @author Sarah Gaiser
6* @date Apr 2025
7*/
8
10#include <cmath>
11
12// json json_load(const std::string& filepath) {
13// json obj;
14// std::ifstream file{filepath};
15// file >> obj;
16// file.close();
17// return obj;
18// }
19
21 auto pSmearingFile = parameters.getString("pSmearingFile");
22
23 if (not pSmearingFile.empty()) {
24 // just using the same seed=42 for now
25 std::cout << "Loading momentum smearing from " << pSmearingFile << std::endl;
26 smearingTool_ = std::make_shared<TrackSmearingTool>(pSmearingFile,true);
27 }
28
29 auto beamPosCfg = parameters.getString("beamPosCfg");
30 if (not beamPosCfg.empty()) {
31 std::cout << "Loading beamspot corrections from " << beamPosCfg << std::endl;
32 auto beamspot_corrections_json = preselect_json_load(beamPosCfg);
33 for (const auto& [run, entry]: beamspot_corrections_json.items()) {
34 beamspot_corrections_[std::stoi(run)] = {
35 entry["beamspot_x"],
36 entry["beamspot_y"],
37 entry["beamspot_z"]
38 };
50 }
51 }
52
53 auto v0ProjectionFitsCfg = parameters.getString("v0ProjectionFitsCfg");
54 if (not v0ProjectionFitsCfg.empty()) {
55 std::cout << "Loading projection fits from " << v0ProjectionFitsCfg << std::endl;
56 v0proj_fits_ = preselect_json_load(v0ProjectionFitsCfg);
57 }
58
59 auto trackBiasCfg = parameters.getString("trackBiasCfg");
60 if (not trackBiasCfg.empty()) {
61 std::cout << "Loading track corrections from " << trackBiasCfg << std::endl;
62 auto track_corr = preselect_json_load(trackBiasCfg);
63 std::cout << track_corr << std::endl;
64
65 for (const auto& [name, corr]: track_corr.items()) {
66 // validate names of corrections, Track::applyCorrection silently ignores
67 // names that don't exactly match the names within it, so we check for
68 // misspellings once here
69 if (name != "track_z0" and name != "track_time") {
70 std::cerr << "WARNING: Unrecognized track correction "
71 << name << " : " << corr
72 << " will be skipped."
73 << std::endl;
74 continue;
75 }
76 track_corrections_[name] = corr;
77 }
78 }
79
80 calTimeOffset_ = parameters.getDouble("calTimeOffset");
81 isData_ = parameters.getInteger("isData") != 0;
82 isSignal_ = parameters.getInteger("isSignal") != 0;
83}
84
85
87 _ah = std::make_shared<AnaHelpers>();
88
89 //init Reading Tree
90 bus_.board_input<EventHeader>(tree, "EventHeader");
91 bus_.board_input<std::vector<Vertex*>>(tree, vtxColl_);
92 if (not isData_ and not mcColl_.empty())
93 bus_.board_input<std::vector<MCParticle*>>(tree, mcColl_);
94
95 /* pre-selection on vertices */
96 vertex_cf_.add("abs_ele_track_before_6ns", 120, 0.0, 24.0);
97 vertex_cf_.add("abs_pos_track_before_6ns", 120, 0.0, 24.0);
98 vertex_cf_.add("clusters_within_1.45ns", 100,0.0,10.0);
99 vertex_cf_.add("ele_track_cluster_within_4ns", 80, 0.0, 16.0);
100 vertex_cf_.add("pos_track_cluster_within_4ns", 80, 0.0, 16.0);
101 vertex_cf_.add("ele_track_chi2ndf", 100, 0.0, 20.0);
102 vertex_cf_.add("pos_track_chi2ndf", 100, 0.0, 20.0);
103 vertex_cf_.add("electron_below_1.75GeV", 230,0.0,2.3);
104 vertex_cf_.add("ele_min_8_hits", 14, 0, 14);
105 vertex_cf_.add("pos_min_8_hits", 14, 0, 14);
106 vertex_cf_.add("vertex_chi2", 100,0.0,50.0);
107 vertex_cf_.add("vtx_max_p_2.4GeV", 300, 0.0, 3.0);
108 vertex_cf_.init();
109
110 /* event selection after vertex selection */
111 event_cf_.add("pair1trigger", 2, -0.5, 1.5);
112 event_cf_.add("at_least_one_vertex", 10, 0.0, 10.0);
113 event_cf_.add("no_extra_vertices", 10, 0.0, 10.0);
114 if (isSignal_) {
115 event_cf_.add("at_least_one_true_vd", 3, 0.0, 2.0);
116 event_cf_.add("no_extra_true_vd", 3, 0.0, 2.0);
117 }
118 event_cf_.init();
119
120 n_vertices_h_ = std::make_unique<TH2F>(
121 "n_vertices_h",
122 "N Vertices in Event (readout and preselected)",
123 10,-0.5,9.5,
124 10,-0.5,9.5
125 );
126}
127
129 Processor::setFile(out_file);
130
131 // create output TTree in output file
132 output_tree_ = std::make_unique<TTree>("preselection",
133 "Preselected and Categorized Vertices",
134 99, out_file);
135 bus_.board_output<double>(output_tree_.get(), "weight");
136 bus_.board_output<Vertex>(output_tree_.get(), "vertex");
137 bus_.board_output<Particle>(output_tree_.get(), "ele");
138 bus_.board_output<Particle>(output_tree_.get(), "pos");
139 bus_.board_output<double>(output_tree_.get(), "psum");
140
141 for (const auto& name : {"eleL1", "eleL2", "posL1", "posL2"}) {
142 bus_.board_output<bool>(output_tree_.get(), name);
143 }
144
145 if (not v0proj_fits_.empty()) {
146 for (const auto& name : {"vtx_proj_sig", "vtx_proj_x", "vtx_proj_x_sig",
147 "vtx_proj_y", "vtx_proj_y_sig"}) {
148 bus_.board_output<double>(output_tree_.get(), name);
149 }
150 }
151
152 if (bus_.has(mcColl_) and isSignal_) {
153 bus_.board_output<MCParticle>(output_tree_.get(), "true_vd");
154 bus_.board_output<bool>(output_tree_.get(), "isRadEle");
155 }
156}
157
159 const auto& eh{bus_.get<EventHeader>("EventHeader")};
160 int run_number = eh.getRunNumber();
161
162 event_cf_.begin_event();
163
164 // re-apply trigger desicion to data sample since it contains
165 // other triggers within it
166 // MC is created with only this trigger AND the event header
167 // is not updated so we need to skip this check for MC
168 event_cf_.apply("pair1trigger", (not isData_ or eh.isPair1Trigger()));
169 if (not event_cf_.keep()) {
170 // we leave BEFORE filling the vertex counting histogram
171 // so that the vertex count histogram is relative to this trigger
172 // and does not include other triggers
173 return true;
174 }
175
176 const auto& vtxs{bus_.get<std::vector<Vertex*>>(vtxColl_)};
190 std::vector<std::tuple<Vertex,Particle,Particle>> preselected_vtx;
191 for (Vertex* vtx : vtxs) {
192 // access the indiviual Vertex, electron, and positron
193 // and add corrections to them before applying pre-selection
194 int i_ele{-1}, i_pos{-1};
195 for (int ipart = 0; ipart < vtx->getParticles().GetEntries(); ++ipart) {
196 int pdg_id = ((Particle*)vtx->getParticles().At(ipart))->getPDG();
197 if (pdg_id == 11) {
198 i_ele = ipart;
199 } else if (pdg_id == -11) {
200 i_pos = ipart;
201 }
202 }
203 if (i_ele < 0 or i_pos < 0) {
204 throw std::runtime_error("Vertex formed without either an electron or positron!");
205 }
206
207 Particle ele = *dynamic_cast<Particle*>(vtx->getParticles().At(i_ele));
208 Particle pos = *dynamic_cast<Particle*>(vtx->getParticles().At(i_pos));
209
210 // for both particles, get cluster, update, re-set cluster
211 CalCluster ele_clu = ele.getCluster();
212 CalCluster pos_clu = pos.getCluster();
213
214 ele_clu.setTime(ele_clu.getTime() - calTimeOffset_);
215 pos_clu.setTime(pos_clu.getTime() - calTimeOffset_);
216
217 ele.setCluster(&ele_clu);
218 pos.setCluster(&pos_clu);
219
220 Track ele_trk = ele.getTrack();
221 Track pos_trk = pos.getTrack();
222
223 // apply track_z0 and track_time corrections loaded from JSON
224 for (const auto& [name, corr]: track_corrections_) {
225 ele_trk.applyCorrection(name, corr);
226 pos_trk.applyCorrection(name, corr);
227 }
228
229 if (not beamspot_corrections_.empty()) {
246 auto bsit = beamspot_corrections_.find(eh.getRunNumber());
247 if (bsit == beamspot_corrections_.end()) {
248 // the current run number is not in the provided set of beamspots
249 // I don't think this should be the case, so I'm throwing an exception.
250 // We could also just pick the closest run number which is what
251 // the V0 projection fit code does.
252 throw std::runtime_error("The run number "+std::to_string(eh.getRunNumber())
253 +" is not in the loaded set of beamspot corrections.");
254 }
255 // assume iterator bsit is pointing to an element of beamspot_corrections_
256 // for the current run number.
257 // correction is the negative of the beamspot position to remove its shift
258 double bs_y = -1*bsit->second.at(1);
259 ele_trk.applyCorrection("track_z0", bs_y);
260 pos_trk.applyCorrection("track_z0", bs_y);
261 }
262
263 // smear track momentum
264 if (smearingTool_) {
265 double ele_smear = smearingTool_->updateWithSmearP(ele_trk);
266 double pos_smear = smearingTool_->updateWithSmearP(pos_trk);
267 smearingTool_->updateVertexWithSmearP(vtx, ele_smear, pos_smear);
268 }
269
270 // put tracks back into their particles
271 // with their new data
272 ele.setTrack(&ele_trk);
273 pos.setTrack(&pos_trk);
274
275 // corrections have been applied, now onto pre-selecting
276 double cluster_tdiff{std::fabs(ele.getCluster().getTime() - pos.getCluster().getTime())};
277 int ele_nhits = ele.getTrack().getTrackerHitCount();
278 if (not ele.getTrack().isKalmanTrack()) ele_nhits*=2;
279 int pos_nhits = pos.getTrack().getTrackerHitCount();
280 if (not pos.getTrack().isKalmanTrack()) pos_nhits*=2;
281
282 double ele_track_cluster_tdiff{
283 std::fabs(ele.getTrack().getTrackTime()-ele.getCluster().getTime())
284 };
285 double pos_track_cluster_tdiff{
286 std::fabs(pos.getTrack().getTrackTime()-pos.getCluster().getTime())
287 };
288
289 TVector3 ele_mom(
290 ele.getTrack().getMomentum()[0],
291 ele.getTrack().getMomentum()[1],
292 ele.getTrack().getMomentum()[2]
293 );
294 TVector3 pos_mom(
295 pos.getTrack().getMomentum()[0],
296 pos.getTrack().getMomentum()[1],
297 pos.getTrack().getMomentum()[2]
298 );
299
300 vertex_cf_.begin_event();
301 vertex_cf_.apply("abs_ele_track_before_6ns", abs(ele.getTrack().getTrackTime()) <= 6.0);
302 vertex_cf_.apply("abs_pos_track_before_6ns", abs(pos.getTrack().getTrackTime()) <= 6.0);
303 vertex_cf_.apply("clusters_within_1.45ns", cluster_tdiff <= 1.45);
304 vertex_cf_.apply("ele_track_cluster_within_4ns", ele_track_cluster_tdiff <= 4.0);
305 vertex_cf_.apply("pos_track_cluster_within_4ns", pos_track_cluster_tdiff <= 4.0);
306 vertex_cf_.apply("ele_track_chi2ndf", ele.getTrack().getChi2Ndf() <= 20.0);
307 vertex_cf_.apply("pos_track_chi2ndf", pos.getTrack().getChi2Ndf() <= 20.0);
308 vertex_cf_.apply("electron_below_1.75GeV", ele.getTrack().getP() <= 1.75);
309 vertex_cf_.apply("ele_min_8_hits", ele_nhits >= 7);
310 vertex_cf_.apply("pos_min_8_hits", pos_nhits >= 7);
311 vertex_cf_.apply("vertex_chi2", vtx->getChi2() <= 20.0);
312 double vtxmaxp = (ele_mom+pos_mom).Mag();
313 vertex_cf_.apply("vtx_max_p_2.4GeV", vtxmaxp <= 2.4);
314
315 vertex_cf_.fill_nm1("abs_ele_track_before_6ns", abs(ele.getTrack().getTrackTime()));
316 vertex_cf_.fill_nm1("abs_pos_track_before_6ns", abs(pos.getTrack().getTrackTime()));
317 vertex_cf_.fill_nm1("clusters_within_1.45ns", cluster_tdiff);
318 vertex_cf_.fill_nm1("ele_track_cluster_within_4ns", ele_track_cluster_tdiff);
319 vertex_cf_.fill_nm1("pos_track_cluster_within_4ns", pos_track_cluster_tdiff);
320 vertex_cf_.fill_nm1("ele_track_chi2ndf", ele.getTrack().getChi2Ndf());
321 vertex_cf_.fill_nm1("pos_track_chi2ndf", pos.getTrack().getChi2Ndf());
322 vertex_cf_.fill_nm1("electron_below_1.75GeV", ele.getTrack().getP());
323 vertex_cf_.fill_nm1("ele_min_8_hits", ele_nhits);
324 vertex_cf_.fill_nm1("pos_min_8_hits", pos_nhits);
325 vertex_cf_.fill_nm1("vertex_chi2", vtx->getChi2());
326 vertex_cf_.fill_nm1("vtx_max_p_2.4GeV", vtxmaxp);
327
328 if (vertex_cf_.keep()) {
329 preselected_vtx.emplace_back(*vtx, ele, pos);
330 }
331 }
332
333 n_vertices_h_->Fill(vtxs.size(), preselected_vtx.size());
334 event_cf_.apply("at_least_one_vertex", preselected_vtx.size() > 0);
335 event_cf_.apply("no_extra_vertices", preselected_vtx.size() < 2);
336 if (not event_cf_.keep()) {
337 return true;
338 }
339
340 // correct number of vertices (i.e. only one)
341 // unpack the vector of vertices into the single elements
342 auto [ vtx, ele, pos ] = preselected_vtx.at(0);
343
344 // earliest layer hit categories
345 bool eleL1{false}, eleL2{false}, posL1{false}, posL2{false};
346 Track ele_trk{ele.getTrack()}, pos_trk{pos.getTrack()};
347 _ah->InnermostLayerCheck(&ele_trk, eleL1, eleL2);
348 _ah->InnermostLayerCheck(&pos_trk, posL1, posL2);
349 bus_.set("eleL1", eleL1);
350 bus_.set("eleL2", eleL2);
351 bus_.set("posL1", posL1);
352 bus_.set("posL2", posL2);
353
354 TVector3 ele_mom(
355 ele_trk.getMomentum()[0],
356 ele_trk.getMomentum()[1],
357 ele_trk.getMomentum()[2]
358 );
359 TVector3 pos_mom(
360 pos_trk.getMomentum()[0],
361 pos_trk.getMomentum()[1],
362 pos_trk.getMomentum()[2]
363 );
364 bus_.set("psum", ele_mom.Mag() + pos_mom.Mag());
365
366 // calculate target projection and its significance
367 if (not v0proj_fits_.empty()) {
368 double vtx_proj_x{-1.0}, vtx_proj_y{-1.0};
369 double vtx_proj_x_sig{-1.0}, vtx_proj_y_sig{-1.0};
370 double vtx_proj_sig{-1.0};
371
372 double vtx_proj_x_centered_dummy, vtx_proj_y_centered_dummy;
374 v0proj_fits_, eh.getRunNumber(),
375 vtx_proj_x, vtx_proj_y, vtx_proj_x_sig, vtx_proj_y_sig,
376 vtx_proj_x_centered_dummy, vtx_proj_y_centered_dummy,
377 vtx.getX(), vtx.getY(), vtx.getZ(),
378 vtx.getP().X(), vtx.getP().Y(), vtx.getP().Z()
379 );
380
381 bus_.set("vtx_proj_sig", vtx_proj_sig);
382 bus_.set("vtx_proj_x", vtx_proj_x);
383 bus_.set("vtx_proj_x_sig", vtx_proj_x_sig);
384 bus_.set("vtx_proj_y", vtx_proj_y);
385 bus_.set("vtx_proj_y_sig", vtx_proj_y_sig);
386 }
387
388 bus_.set("weight", 1.);
389 bus_.set("vertex", vtx);
390 bus_.set("ele", ele);
391 bus_.set("pos", pos);
392
402 if (bus_.has(mcColl_) and isSignal_) {
408 std::map<int, int> count_per_particle_id;
409 for (const auto& [layer_id, particle_id] : ele_trk.getMcpHits()) {
410 if (count_per_particle_id.find(particle_id) == count_per_particle_id.end()) {
411 count_per_particle_id[particle_id] = 0;
412 }
413 count_per_particle_id[particle_id]++;
414 }
415 int truth_ele_id{-1}, max_nhits{0};
416 for (const auto& [particle_id, count] : count_per_particle_id) {
417 if (count > max_nhits) {
418 truth_ele_id = particle_id;
419 max_nhits = count;
420 }
421 }
432 const auto& mc_ptr{bus_.get<std::vector<MCParticle*>>(mcColl_)};
433 MCParticle* vd{nullptr};
434 int n_vd{0};
435 bool ele_is_rad_ele{false};
436 for (MCParticle* ptr : mc_ptr) {
437 if (ptr->getPDG() == 625) {
438 n_vd++;
439 vd = ptr;
440 } else if (ptr->getID() == truth_ele_id) {
441 ele_is_rad_ele = (ptr->getMomPDG() == 625);
442 }
443 }
444 event_cf_.apply("at_least_one_true_vd", n_vd > 0);
445 event_cf_.apply("no_extra_true_vd", n_vd < 2);
446 if (not event_cf_.keep()) {
447 return true;
448 }
449 if (not vd) {
450 throw std::runtime_error(
451 "ERROR: Logic error: checked for VD earlier but there isn't one."
452 );
453 }
454 bus_.set("true_vd", *vd);
455 bus_.set("isRadEle", ele_is_rad_ele);
456 }
457 output_tree_->Fill();
458 return true;
459}
460
462 outF_->cd();
463 output_tree_->Write();
464 n_vertices_h_->Write();
465 vertex_cf_.save();
466 event_cf_.save();
467}
468
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
void setTime(const double time)
Definition CalCluster.h:80
double getTime() const
Definition CalCluster.h:83
Definition IEvent.h:7
description
Track getTrack() const
Definition Particle.h:48
CalCluster getCluster() const
Definition Particle.h:62
void setTrack(Track *track)
Definition Particle.h:42
void setCluster(CalCluster *cluster)
Definition Particle.h:56
virtual void setFile(TFile *out_file) final
Set output TFile for AnaProcessors.
virtual void initialize(TTree *tree) final
Callback for the Processor to take any necessary action when the processing of events starts,...
virtual void finalize() final
Callback for the Processor to take any necessary action when the processing of events finishes,...
virtual void configure(const ParameterSet &parameters) final
Callback for the Processor to configure itself from the given set of parameters.
std::shared_ptr< TrackSmearingTool > smearingTool_
json preselect_json_load(const std::string &filepath)
std::string mcColl_
description
std::unique_ptr< TH2F > n_vertices_h_
std::shared_ptr< AnaHelpers > _ah
description
std::map< int, std::vector< double > > beamspot_corrections_
std::map< std::string, double > track_corrections_
std::unique_ptr< TTree > output_tree_
TFile * outF_
Definition Processor.h:125
virtual bool process()
Process the histograms and generate analysis output.
Definition Processor.h:95
virtual void setFile(TFile *outFile)
Set output TFile for AnaProcessors.
Definition Processor.h:88
Definition Track.h:32
bool isKalmanTrack() const
Definition Track.h:233
double getChi2Ndf() const
Definition Track.h:128
int getTrackerHitCount() const
Definition Track.h:347
double getTrackTime() const
Definition Track.h:162
std::vector< double > getMomentum()
Definition Track.h:287
std::vector< std::pair< int, int > > getMcpHits()
Definition Track.h:105
double getP() const
Definition Track.h:293
void applyCorrection(std::string var, double correction)
Definition Track.cxx:91
double v0_projection_to_target_significance(json v0proj_fits, int run, double &vtx_proj_x, double &vtx_proj_y, double &vtx_proj_x_signif, double &vtx_proj_y_signif, double &vtx_proj_x_centered, double &vtx_proj_y_centered, double vtx_x, double vtx_y, double vtx_z, double vtx_px, double vtx_py, double vtx_pz, bool debug=false)
description