LDMX Software
TrigElectronProducer.cxx
2
3// #include "SimCore/Event/SimParticle.h"
4#include "TCanvas.h"
5#include "TFile.h"
6#include "TString.h"
7#include "TrigScint/Event/TrigScintTrack.h"
8#include "Trigger/Event/TrigCaloCluster.h"
9#include "Trigger/Event/TrigParticle.h"
10
11namespace trigger {
12
14 cluster_coll_name_ = ps.get<std::string>("cluster_coll_name");
15 ele_coll_name_ = ps.get<std::string>("ele_coll_name");
16 prop_map_name_ = ps.get<std::string>("prop_map_name");
17 ecal_front_z_ = ps.get<double>("ecal_front_z");
18 cluster_coll_passname_ = ps.get<std::string>("cluster_coll_passname");
19 cluster_collname_events_passname_ =
20 ps.get<std::string>("cluster_collname_events_passname");
21 ts_track_coll_name_ = ps.get<std::string>("ts_track_coll_name");
22 ts_passname_ = ps.get<std::string>("ts_passname");
23 ts_y0_ = static_cast<float>(ps.get<double>("ts_y0"));
24 ts_channel_pitch_ = static_cast<float>(ps.get<double>("ts_channel_pitch"));
25}
26
28 if (!event.exists(cluster_coll_name_, cluster_collname_events_passname_))
29 return;
30 auto ecal_clusters{event.getObject<TrigCaloClusterCollection>(
31 cluster_coll_name_, cluster_coll_passname_)};
32
33 // target y from the trigger scintillator
34 const auto y_t{tsTargetY(event)};
35
36 // std::vector<ldmx::SimParticle> eles;
37 TrigParticleCollection eles;
38 for (const auto& clus : ecal_clusters) {
39 TrigParticle el;
40 // ldmx::SimParticle el;
41
42 // We fit for the response in an eGun sample like this:
43 // Events->Draw("Electron_e[0]/Truth_e[0]:Truth_e[0]","Truth_e[0]>500 &&
44 // Truth_e[0]<3500","prof")
45 // --> update fit to use the electron energy at the ecal face (not target)
46 // -->
47 // Events->Draw("Electron_eClus[0]/TruthEcal_e[0]:TruthEcal_e[0]","TruthEcal_e[0]>500
48 // && TruthEcal_e[0]<3500","prof") TF1* func = new
49 // TF1("func","[0]/x+[1]",500,3500); htemp->Fit(func) float calib_e =
50 // clus.e() / (98.099700/clus.e() + 0.77202700);
51 float calib_e =
52 clus.e() / (184.103 / clus.e() + 0.753756); // divide by response
53
54 // const float dX = clus.x() - xT;
55 // float R = calib_e*(11500/4000.); // convert 11.5m/4GeV (in mm/MeV)
56 // float zd = 240.; // 240mm z detector
57 // float a = dX/zd;
58 // float b = (dX*dX+zd*zd)/(2*R*zd);
59 // float pred_px = clus.e() * (-b+a*sqrt(1+a*a-b*b))/(1+a*a);
60 // float pred_py = clus.e()/4e3 * (clus.y() - yT) * 13.3; //mev/mm
61
62 // Events->Draw("TruthEcal_y-Truth_y -
63 // Electron_dy*240/Electron_zClus","TruthEcal_e>1e3")
64
65 // the TS measures only y, so px is not predicted
66 const float pred_px = 0;
67 // y deflection, tracing back to the ECal face
68 const float pred_py =
69 y_t && clus.z() > 0
70 ? getPy(calib_e, (ecal_front_z_ / clus.z()) * (clus.y() - *y_t))
71 : 0;
72
73 // std::cout << "expectX " << pred_px << " versus " << fit_px << std::endl;
74 // std::cout << "expectY " << pred_py << " versus " << fit_py << std::endl;
75 float pred_pz = sqrt(
76 std::max(pow(calib_e, 2) - (pow(pred_px, 2) + pow(pred_py, 2)), 0.));
77
78 // produce el
79 Point targ(0, y_t.value_or(0), 0);
80 LorentzVector p4(pred_px, pred_py, pred_pz, calib_e);
81 eles.emplace_back(p4, targ, 11);
82 Point calo(clus.x(), clus.y(),
83 clus.z()); // n.b. "Z" is not at the ECal Face!
84 eles.back().setEndPoint(
85 calo); // clus.x(), clus.y(), clus.z()); how to handle?
86 eles.back().setClusEnergy(clus.e());
87 eles.back().setClusTP(clus.nTP());
88 eles.back().setClusDepth(clus.depth());
89
90 // el.setEnergy(clus.e());
91 // el.setPdgID(11);
92 // el.setCharge(-1);
93 // el.setMass(0.000511);
94 // el.setVertex(0, xT, yT);
95 // el.setMomentum(pred_px, pred_py, pred_pz);
96 // eles.push_back(el);
97 }
98 event.add(ele_coll_name_, eles);
99}
100
101void TrigElectronProducer::setupMaps(bool isX) {
102 TProfile2D* prof = isX ? prop_mapx_ : prop_mapy_;
103 const int n = prof->GetXaxis()->GetNbins();
104 for (int i = 1; i <= n; i++) {
105 TF1* func = new TF1("func", "pol1", -20, 20); // going to fit for px/e
106 TProfile* proj = prof->ProfileY("h", i, i);
107 proj->Fit("func", "q", "", -1, 1);
108 bool debug = false;
109 if (debug) {
110 TCanvas c("c", "");
111 proj->Draw();
112 func->Draw("same");
113 c.SaveAs(TString::Format("debug_fit_%d_%d.pdf", int(isX), i));
114 }
115 delete proj;
116 if (isX) {
117 fits_x_.push_back(func);
118 } else {
119 fits_y_.push_back(func);
120 }
121 }
122 return;
123}
124
125float TrigElectronProducer::getP(bool isX, float e, float d) {
126 if (fabs(d) > 300) return 0; // something has gone very wrong
127 const bool debug = false;
128 TProfile2D* prof = isX ? prop_mapx_ : prop_mapy_;
129 if (!prof) {
130 if (debug) std::cout << "null pointer" << std::endl;
131 return 0;
132 }
133 int bin1{-9999}, bin2{-9999};
134 bin1 = prof->GetXaxis()->FindBin(e);
135 float frac = e - prof->GetXaxis()->GetBinCenter(bin1);
136 float diff = fabs(prof->GetXaxis()->GetBinCenter(bin1) -
137 prof->GetXaxis()->GetBinCenter(bin2));
138 bin2 = diff > 0 ? bin1 + 1 : bin1 - 1;
139 bin1 = std::max(1, std::min(bin1, prof->GetXaxis()->GetNbins()));
140 bin2 = std::max(1, std::min(bin2, prof->GetXaxis()->GetNbins()));
141 float res1, res2;
142 if (isX) {
143 res1 = fits_x_[bin1 - 1]->GetX(d);
144 res2 = fits_x_[bin2 - 1]->GetX(d);
145 } else {
146 res1 = fits_y_[bin1 - 1]->GetX(d);
147 res2 = fits_y_[bin2 - 1]->GetX(d);
148 }
149
150 if (debug)
151 printf("%f %f %f %f :: %f %f %f \n", d, e,
152 prof->GetXaxis()->GetBinCenter(bin1),
153 prof->GetXaxis()->GetBinCenter(bin2), res1, res2,
154 std::abs(frac / diff) * res2 + (1 - std::abs(frac / diff)) * res1);
155 return e *
156 (std::abs(frac / diff) * res2 + (1 - std::abs(frac / diff)) * res1);
157}
158
160 ldmx_log(debug) << "Process starts!";
161
162 TFile* f = new TFile(prop_map_name_.c_str(), "read");
163 prop_mapx_ = (TProfile2D*)f->Get("profx");
164 prop_mapx_->SetDirectory(0);
165 prop_mapy_ = (TProfile2D*)f->Get("profy");
166 prop_mapy_->SetDirectory(0);
167 f->Close();
168
169 setupMaps(0); // X
170 setupMaps(1); // Y
171
172 return;
173}
174
176 ldmx_log(debug) << "Process ends!";
177
178 // free maps
179 for (int i = 0; i < fits_x_.size(); i++) delete fits_x_[i];
180 for (int i = 0; i < fits_y_.size(); i++) delete fits_y_[i];
181
182 return;
183}
184
185std::optional<float> TrigElectronProducer::tsTargetY(
186 const framework::Event& event) {
187 const auto& coll{ts_track_coll_name_};
188 if (!event.exists(coll, ts_passname_)) {
189 if (!warned_missing_ts_) {
190 ldmx_log(warn) << "No " << coll << " (pass '" << ts_passname_
191 << "') in the event: trigger electrons get py = 0.";
192 warned_missing_ts_ = true;
193 }
194 return std::nullopt;
195 }
196 // lowest residual with ties going to the highest PE
197 std::optional<float> centroid;
198 float residual{0};
199 float pe{0};
200 for (const auto& track :
201 event.getCollection<ldmx::TrigScintTrack>(coll, ts_passname_)) {
202 if (centroid &&
203 (track.getResidual() > residual ||
204 (track.getResidual() == residual && track.getPE() <= pe))) {
205 continue;
206 }
207 centroid = track.getCentroid();
208 residual = track.getResidual();
209 pe = track.getPE();
210 }
211 if (!centroid) return std::nullopt;
212 return ts_y0_ + ts_channel_pitch_ * (*centroid);
213}
214
215} // namespace trigger
216
#define DECLARE_PRODUCER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Trigger electron reconstruction algorithm.
Implements an event buffer system for storing event data.
Definition Event.h:40
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:107
const std::vector< ContentType > & getCollection(const std::string &collectionName, const std::string &passName) const
Get a collection (std::vector) of objects from the event bus.
Definition Event.h:409
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
Represents a track of trigger scintillator clusters.
virtual void onProcessStart()
Callback for the EventProcessor to take any necessary action when the processing of events starts,...
virtual void produce(framework::Event &event)
Process the event and put new data products into it.
virtual void onProcessEnd()
Callback for the EventProcessor to take any necessary action when the processing of events finishes,...
virtual void configure(framework::config::Parameters &ps)
Callback for the EventProcessor to configure itself from the given set of parameters.
Class for particles reconstructed by the trigger system.