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
FeeAnaProcessor.cxx
Go to the documentation of this file.
1#include "FeeAnaProcessor.h"
2#include <iomanip>
3#include <cmath>
4#include <algorithm>
5#include <fstream>
6#include "utilities.h"
7
9 : Processor(name, process) {
10 }
11
14
16
17 std::cout << "Configuring FeeAnaProcessor" << std::endl;
18 try
19 {
20 debug_ = parameters.getInteger("debug",debug_);
21 seed_ = parameters.getInteger("seed",seed_);
22 trkCollName_ = parameters.getString("trkCollName",trkCollName_);
23 fspCollName_ = parameters.getString("fspCollName",fspCollName_);
24 mcColl_ = parameters.getString("mcColl",mcColl_);
25 histCfgFilename_ = parameters.getString("histCfg",histCfgFilename_);
26 doTruth_ = (bool) parameters.getInteger("doTruth",doTruth_);
27 truthHistCfgFilename_ = parameters.getString("truthHistCfg",truthHistCfgFilename_);
28 selectionCfg_ = parameters.getString("selectionjson",selectionCfg_);
29 isData_ = parameters.getInteger("isData",isData_);
30 regionSelections_ = parameters.getVString("regionDefinitions",regionSelections_);
31
32 // Track corrections (scale corrections, smearing): one JSON for TrackCorrector.
33 // The per-job settings stay processor parameters.
34 trackCorrectionsCfg_ = parameters.getString("trackCorrectionsCfg", trackCorrectionsCfg_);
35 doSmearing_ = parameters.getInteger("doSmearing", 0) != 0;
36 requireTruthMatch_ = parameters.getInteger("requireTruthMatch", -1); // -1: use the JSON value
37 // These settings moved into the trackCorrectionsCfg JSON; refuse them rather than ignore them.
38 for (const char* moved : {"smearingCfg", "pSmearingFile", "runScaleCfg", "meanCorrCfg"}) {
39 if (!parameters.getString(moved, "").empty())
40 throw std::runtime_error(std::string("[FeeAnaProcessor] parameter '") + moved +
41 "' moved to the trackCorrectionsCfg JSON");
42 }
43
44 hitPatternScaleFile_ = parameters.getString("hitPatternScaleFile", hitPatternScaleFile_);
45 doHitPatternScale_ = parameters.getInteger("doHitPatternScale", doHitPatternScale_ ? 1 : 0) != 0;
46 hitPatternScaleDataOnly_ = parameters.getInteger("hitPatternScaleDataOnly", hitPatternScaleDataOnly_ ? 1 : 0) != 0;
47
48 // FEE selection
49 feeClusterEnergyMin_ = parameters.getDouble("feeClusterEnergyMin", feeClusterEnergyMin_);
50 clusterTimeMin_ = parameters.getDouble("clusterTimeMin", clusterTimeMin_);
51 clusterTimeMax_ = parameters.getDouble("clusterTimeMax", clusterTimeMax_);
52 clusterTimeMinMC_ = parameters.getDouble("clusterTimeMinMC", clusterTimeMinMC_);
53 clusterTimeMaxMC_ = parameters.getDouble("clusterTimeMaxMC", clusterTimeMaxMC_);
54 calTimeOffset_ = parameters.getDouble("CalTimeOffset", calTimeOffset_);
55 calTimeOffsetMC_ = parameters.getDouble("CalTimeOffsetMC", calTimeOffsetMC_);
56 mcTimeOffset_ = parameters.getDouble("mcTimeOffset", mcTimeOffset_);
57 requireElectron_ = parameters.getInteger("requireElectron", requireElectron_ ? 1 : 0) != 0;
58 requireCluster_ = parameters.getInteger("requireCluster", requireCluster_ ? 1 : 0) != 0;
59 requireFeeTrigger_ = parameters.getInteger("requireFeeTrigger", requireFeeTrigger_ ? 1 : 0) != 0;
60 eopMin_ = parameters.getDouble("eopMin", eopMin_);
61 eopMax_ = parameters.getDouble("eopMax", eopMax_);
62
63 // In-time isolation veto
64 vetoExtraClusters_ = parameters.getInteger("vetoExtraClusters", vetoExtraClusters_ ? 1 : 0) != 0;
65 vetoExtraTracks_ = parameters.getInteger("vetoExtraTracks", vetoExtraTracks_ ? 1 : 0) != 0;
66 vetoClusterTimeWindow_ = parameters.getDouble("vetoClusterTimeWindow", vetoClusterTimeWindow_);
67 vetoTrackTimeWindow_ = parameters.getDouble("vetoTrackTimeWindow", vetoTrackTimeWindow_);
68 vetoClusterEnergyMin_ = parameters.getDouble("vetoClusterEnergyMin", vetoClusterEnergyMin_);
69
70 }
71 catch (std::runtime_error& error)
72 {
73 std::cout << error.what() << std::endl;
74 }
75
76 if (!isData_)
78
79 // Load the per-hit_pattern momentum scale corrections (data -> beam energy).
81 std::ifstream ifs(hitPatternScaleFile_);
82 if (!ifs.good()) {
83 std::cout << "[FeeAnaProcessor] ERROR: cannot open hitPatternScaleFile "
84 << hitPatternScaleFile_ << " -- hit-pattern scale correction DISABLED" << std::endl;
85 doHitPatternScale_ = false;
86 } else {
87 nlohmann::json j;
88 ifs >> j;
89 hitPatternDefaultScale_ = j.value("default_scale", 1.0);
90 if (j.contains("pattern_to_scale")) {
91 const nlohmann::json& p2s = j["pattern_to_scale"];
92 for (auto it = p2s.begin(); it != p2s.end(); ++it)
93 hitPatternScale_[std::stoi(it.key())] = it.value().get<double>();
94 }
95 std::cout << "[FeeAnaProcessor] Loaded " << hitPatternScale_.size()
96 << " hit-pattern scale corrections (default_scale=" << hitPatternDefaultScale_
97 << (hitPatternScaleDataOnly_ ? ", data-only" : ", data+MC")
98 << ") from " << hitPatternScaleFile_ << std::endl;
99 }
100 }
101}
102
103void FeeAnaProcessor::setFile(TFile* outFile) {
104 Processor::setFile(outFile);
105 output_tree_ = std::make_unique<TTree>("tracks", "Selected FEE Tracks", 99, outFile);
106 output_tree_->Branch("track.", &track_out_, 100000, 3);
107 output_tree_->Branch("track_smeared.", &track_smeared_out_, 100000, 3);
108 output_tree_->Branch("p_smear_ratio", &p_smear_ratio_out_);
109 output_tree_->Branch("run_scale", &run_scale_out_);
110 output_tree_->Branch("clu_E", &clu_E_out_);
111 output_tree_->Branch("clu_time", &clu_time_out_);
112 output_tree_->Branch("eop", &eop_out_);
113 output_tree_->Branch("n_clusters_intime", &n_clusters_intime_out_);
114 output_tree_->Branch("n_tracks_intime", &n_tracks_intime_out_);
115 output_tree_->Branch("L1_axial", &L1_axial_out_);
116 output_tree_->Branch("L1_stereo", &L1_stereo_out_);
117 output_tree_->Branch("L2_axial", &L2_axial_out_);
118 output_tree_->Branch("L2_stereo", &L2_stereo_out_);
119 output_tree_->Branch("L3_axial", &L3_axial_out_);
120 output_tree_->Branch("L3_stereo", &L3_stereo_out_);
121 output_tree_->Branch("L4_axial", &L4_axial_out_);
122 output_tree_->Branch("L4_stereo", &L4_stereo_out_);
123 output_tree_->Branch("L1", &L1_out_);
124 output_tree_->Branch("L2", &L2_out_);
125 output_tree_->Branch("L3", &L3_out_);
126 output_tree_->Branch("L4", &L4_out_);
127 output_tree_->Branch("is_L1", &is_L1_out_);
128 output_tree_->Branch("is_L2", &is_L2_out_);
129 output_tree_->Branch("is_L3", &is_L3_out_);
130 output_tree_->Branch("hit_pattern", &hit_pattern_out_);
131 output_tree_->Branch("n_hits", &n_hits_out_);
132 output_tree_->Branch("track_corr.", &track_corr_out_, 100000, 3);
133 output_tree_->Branch("hit_pattern_scale", &hit_pattern_scale_out_);
134 output_tree_->Branch("hit_pattern_scale_isdefault", &hit_pattern_scale_isdefault_out_);
135 output_tree_->Branch("truth_matched", &truth_matched_out_);
136 output_tree_->Branch("has_truth_link", &has_truth_link_out_);
137}
138
140
141 ah_ = std::make_shared<AnaHelpers>();
142
143 //Init histos
145 trkHistos_->loadHistoConfig(histCfgFilename_);
146 trkHistos_->doTrackComparisonPlots(false);
147 trkHistos_->DefineHistos();
148
149 // FEE tracks come from the FinalStateParticle collection, where the track and its
150 // ECal cluster are already associated at the LCIO stage (no manual matching needed).
151 tree->SetBranchAddress(fspCollName_.c_str(), &fsps_, &bfsps_);
152
153 if (!selectionCfg_.empty()) {
154 trkSelector_ = std::make_shared<BaseSelector>(name_+"_trkSelector",selectionCfg_);
155 trkSelector_->setDebug(debug_);
156 trkSelector_->LoadSelection();
157 }
158
159 if (doTruth_) {
160 truthHistos_ = new TrackHistos(trkCollName_+"_truthComparison");
161 truthHistos_->loadHistoConfig(histCfgFilename_);
162 truthHistos_->DefineHistos();
163 truthHistos_->loadHistoConfig(truthHistCfgFilename_);
164 truthHistos_->DefineHistos();
165 truthHistos_->doTrackComparisonPlots(false);
166 }
167
168 // Setup track selections plots
169 for (unsigned int i_reg = 0;
170 i_reg < regionSelections_.size();
171 i_reg++) {
172 std::string regname = AnaHelpers::getFileName(regionSelections_[i_reg],false);
173 std::cout<< "Setting up region "<< regname<<std::endl;
174
175 reg_selectors_[regname] = std::make_shared<BaseSelector>(regname, regionSelections_[i_reg]);
176 reg_selectors_[regname]->setDebug(false);
177 reg_selectors_[regname]->LoadSelection();
178
179 reg_histos_[regname] = std::make_shared<TrackHistos>(regname);
180 reg_histos_[regname]->loadHistoConfig(histCfgFilename_);
181 reg_histos_[regname]->doTrackComparisonPlots(false);
182 reg_histos_[regname]->DefineTrkHitHistos();
183
184 regions_.push_back(regname);
185
186 }
187
188 //Get event header information for trigger
189 tree->SetBranchAddress("EventHeader", &evth_ , &bevth_);
190
191 // 2021 trigger-scaler bank, needed to require the FEE trigger (data only).
193 tree->SetBranchAddress("TSBank", &tsdata_, &btsdata_);
194
195 // Full event collections, used to count in-time tracks/clusters (and optionally veto).
196 // These are always branched in so the in-time multiplicity is recorded in the output
197 // tree regardless of whether the veto is applied.
198 tree->SetBranchAddress("RecoEcalClusters", &ecalClusters_, &becalClusters_);
199 tree->SetBranchAddress(trkCollName_.c_str(), &allTracks_, &ballTracks_);
200
201 // MCParticle collection for hit-based truth matching (MC only; absent on data).
202 if (!isData_ && !mcColl_.empty())
203 tree->SetBranchAddress(mcColl_.c_str(), &mcParticles_, &bmcParticles_);
204
205 if (!trackCorrectionsCfg_.empty()) {
207 opts.isData = isData_;
208 opts.seed = seed_;
209 opts.doSmearing = doSmearing_;
211 opts.debug = debug_ > 0;
212 corrector_ = std::make_shared<TrackCorrector>(trackCorrectionsCfg_, opts);
213 corrector_->printConfig();
214 }
215
216 if (corrector_ && corrector_->hasSmearing()) {
217
218 psmear_h_ = new TH1D("psmear_h",
219 "psmear_h",200,2,6);
220 psmear_top_h_ = new TH1D("psmear_top_h",
221 "psmear_top_h",200,2,6);
222 psmear_bot_h_ = new TH1D("psmear_bot_h",
223 "psmear_bot_h",200,2,6);
224
225 psmear_vs_nHits_hh_ = new TH2D("psmear_vs_nHits_hh",
226 "psmear_vs_nHits_hh",
227 5,8,13,
228 200,0,6);
229 psmear_vs_nHits_top_hh_ = new TH2D("psmear_vs_nHits_top_hh",
230 "psmear_vs_nHits_top_hh",
231 5,8,13,
232 200,0,6);
233 psmear_vs_nHits_bot_hh_ = new TH2D("psmear_vs_nHits_bot_hh",
234 "psmear_vs_nHits_bot_hh",
235 5,8,13,
236 200,0,6);
237
238
239 psmear_rel_h_ = new TH1D("psmear_rel_h",
240 "psmear_rel_h",200,0,6);
241
242 psmear_vs_nHits_rel_hh_ = new TH2D("psmear_vs_nHits_rel_hh",
243 "psmear_vs_nHits_rel_hh",
244 5,8,13,
245 200,0,6);
246 psmear_vs_nHits_top_rel_hh_ = new TH2D("psmear_vs_nHits_top_rel_hh",
247 "psmear_vs_nHits_top_rel_hh",
248 5,8,13,
249 200,0,6);
250 psmear_vs_nHits_bot_rel_hh_ = new TH2D("psmear_vs_nHits_bot_rel_hh",
251 "psmear_vs_nHits_bot_rel_hh",
252 5,8,13,
253 200,0,6);
254
255 // z0 smearing validation histograms (binning matches Z0_h from feeSmearing_2021.json)
256 z0smear_h_ = new TH1D("z0smear_h", "z0smear_h", 100, -0.5, 0.5);
257 z0smear_top_h_ = new TH1D("z0smear_top_h", "z0smear_top_h", 100, -0.5, 0.5);
258 z0smear_bot_h_ = new TH1D("z0smear_bot_h", "z0smear_bot_h", 100, -0.5, 0.5);
259
260 // omega smearing validation histograms
261 omega_h_ = new TH1D("omega_h", "omega_h", 100, -0.00015, 0.00015);
262 omega_top_h_ = new TH1D("omega_top_h", "omega_top_h", 100, -0.00015, 0.00015);
263 omega_bot_h_ = new TH1D("omega_bot_h", "omega_bot_h", 100, -0.00015, 0.00015);
264 omegasmear_h_ = new TH1D("omegasmear_h", "omegasmear_h", 100, -0.00015, 0.00015);
265 omegasmear_top_h_ = new TH1D("omegasmear_top_h", "omegasmear_top_h", 100, -0.00015, 0.00015);
266 omegasmear_bot_h_ = new TH1D("omegasmear_bot_h", "omegasmear_bot_h", 100, -0.00015, 0.00015);
267 omega_vs_p_hh_ = new TH2D("omega_vs_p_hh", "omega_vs_p_hh", 100, 1.0, 4.5, 100, -0.00015, 0.00015);
268
269 // omega vs tanLambda and phi0 (unsmeared and smeared)
270 omega_vs_tanL_hh_ = new TH2D("omega_vs_tanL_hh", "omega_vs_tanL_hh", 80, -0.08, 0.08, 100, -0.00015, 0.00015);
271 omega_vs_tanL_top_hh_ = new TH2D("omega_vs_tanL_top_hh", "omega_vs_tanL_top_hh", 40, 0.00, 0.08, 100, -0.00015, 0.00015);
272 omega_vs_tanL_bot_hh_ = new TH2D("omega_vs_tanL_bot_hh", "omega_vs_tanL_bot_hh", 40, -0.08, 0.00, 100, -0.00015, 0.00015);
273 omegasmear_vs_tanL_hh_ = new TH2D("omegasmear_vs_tanL_hh", "omegasmear_vs_tanL_hh", 80, -0.08, 0.08, 100, -0.00015, 0.00015);
274 omegasmear_vs_tanL_top_hh_ = new TH2D("omegasmear_vs_tanL_top_hh", "omegasmear_vs_tanL_top_hh", 40, 0.00, 0.08, 100, -0.00015, 0.00015);
275 omegasmear_vs_tanL_bot_hh_ = new TH2D("omegasmear_vs_tanL_bot_hh", "omegasmear_vs_tanL_bot_hh", 40, -0.08, 0.00, 100, -0.00015, 0.00015);
276
277 omega_vs_phi0_hh_ = new TH2D("omega_vs_phi0_hh", "omega_vs_phi0_hh", 80, -0.2, 0.2, 100, -0.00015, 0.00015);
278 omega_vs_phi0_top_hh_ = new TH2D("omega_vs_phi0_top_hh", "omega_vs_phi0_top_hh", 80, -0.2, 0.2, 100, -0.00015, 0.00015);
279 omega_vs_phi0_bot_hh_ = new TH2D("omega_vs_phi0_bot_hh", "omega_vs_phi0_bot_hh", 80, -0.2, 0.2, 100, -0.00015, 0.00015);
280 omegasmear_vs_phi0_hh_ = new TH2D("omegasmear_vs_phi0_hh", "omegasmear_vs_phi0_hh", 80, -0.2, 0.2, 100, -0.00015, 0.00015);
281 omegasmear_vs_phi0_top_hh_ = new TH2D("omegasmear_vs_phi0_top_hh", "omegasmear_vs_phi0_top_hh", 80, -0.2, 0.2, 100, -0.00015, 0.00015);
282 omegasmear_vs_phi0_bot_hh_ = new TH2D("omegasmear_vs_phi0_bot_hh", "omegasmear_vs_phi0_bot_hh", 80, -0.2, 0.2, 100, -0.00015, 0.00015);
283
284 }
285
286
287}
288
290
291 double weight = 1.;
292 int n_sel_tracks = 0;
293
294 if (corrector_) corrector_->beginEvent(evth_->getRunNumber(), mcParticles_);
295
296 double minTime = isData_ ? clusterTimeMin_ : clusterTimeMinMC_;
297 double maxTime = isData_ ? clusterTimeMax_ : clusterTimeMaxMC_;
298 double calOffset = isData_ ? calTimeOffset_ : calTimeOffsetMC_;
299
300 if (debug_ > 0) {
301 std::cout << "[FeeAna] --- new event isData=" << isData_
302 << " feeEnergyMin=" << feeClusterEnergyMin_
303 << " timeWindow=[" << minTime << "," << maxTime << "]"
304 << " nFSPs=" << fsps_->size() << std::endl;
305 }
306
307 // Require the FEE trigger on data. MC is generated with a single trigger and the
308 // TSBank bits are not filled, so the requirement is applied to data only (mirrors
309 // the trigger handling in PreselectAndCategorize2021).
311 if (!tsdata_ || !tsdata_->isFEETrigger()) {
312 if (debug_ > 0) std::cout << "[FeeAna] FAIL FEE trigger" << std::endl;
313 return true;
314 }
315 }
316
317 // FEE selection: loop over FinalStateParticles. Each particle carries its track and
318 // (if matched at the LCIO stage) its ECal cluster, so no manual track-cluster matching
319 // is needed. We require an electron whose associated cluster looks like a full-energy
320 // electron (energy / time / E-over-p), then apply the usual track-quality cuts.
321 for (unsigned int ipart = 0; ipart < fsps_->size(); ++ipart) {
322
323 if (trkSelector_) trkSelector_->getCutFlowHisto()->Fill(0.,weight);
324
325 Particle* part = fsps_->at(ipart);
326
327 // Electron requirement (FEEs are electrons; charge < 0 in HPS convention)
328 if (requireElectron_ && part->getCharge() >= 0) {
329 if (debug_ > 0) std::cout << "[FeeAna] FAIL not an electron: charge=" << part->getCharge() << std::endl;
330 continue;
331 }
332
333 // Associated FEE cluster (already matched to the track at the LCIO stage).
334 // A particle with no associated cluster has the sentinel cluster energy.
335 CalCluster clu = part->getCluster();
336 double clu_E = clu.getEnergy();
337 double clu_traw = clu.getTime(); // raw cluster time
338 double clu_t = clu_traw - calOffset; // offset-corrected (centered near 0)
339
340 // DIAGNOSTIC: dump cluster time for every electron with a full-energy cluster,
341 // *before* the time cut, to verify the CalTimeOffset centers the prompt peak.
342 if (debug_ > 0 && part->getCharge() < 0 && clu_E > feeClusterEnergyMin_) {
343 std::cout << "[FeeAna][cluTime] rawCluTime=" << clu_traw
344 << " corrCluTime=" << clu_t
345 << " cluE=" << clu_E
346 << " trkTime=" << part->getTrack().getTrackTime()
347 << std::endl;
348 }
349
350 if (requireCluster_) {
351 if (clu_E < feeClusterEnergyMin_) {
352 if (debug_ > 0) std::cout << "[FeeAna] FAIL cluster E: " << clu_E << " < " << feeClusterEnergyMin_ << std::endl;
353 continue;
354 }
355 if (clu_t < minTime || clu_t > maxTime) {
356 if (debug_ > 0) std::cout << "[FeeAna] FAIL cluster time (corrected): " << clu_t << " not in [" << minTime << "," << maxTime << "]" << std::endl;
357 continue;
358 }
359 }
360
361 // Track associated to this particle (returned by value)
362 Track track_obj = part->getTrack();
363 Track* track = &track_obj;
364 int n2dhits_onTrack = !track->isKalmanTrack() ? track->getTrackerHitCount() * 2 : track->getTrackerHitCount();
365
366 // E/p of the FEE candidate
367 double eop = track->getP() > 0 ? clu_E / track->getP() : -1.0;
368 if (requireCluster_ && (eop < eopMin_ || eop > eopMax_)) {
369 if (debug_ > 0) std::cout << "[FeeAna] FAIL E/p: " << eop << " not in [" << eopMin_ << "," << eopMax_ << "]" << std::endl;
370 continue;
371 }
372
373 TVector3 trk_mom;
374 trk_mom.SetX(track->getMomentum()[0]);
375 trk_mom.SetY(track->getMomentum()[1]);
376 trk_mom.SetZ(track->getMomentum()[2]);
377
378 // Decode per-sensor hit layer vector and determine top/bottom.
379 bool isTop = track->getTanLambda() > 0;
380 // The FEE track comes from the FinalStateParticle, whose embedded Track does NOT
381 // carry the per-sensor hit-layer vector: FinalStateParticleProcessor fills the hit
382 // count and the SvtHit refs but never calls addHitLayer(), so getHitLayers() is
383 // empty here. The matching KalmanFullTracks entry (same track ID) does carry it, so
384 // match by ID and decode the layers from that. Fall back to the FSP track's own
385 // SvtHits if no match is found, and finally to the (empty) FSP layer vector.
386 Track* layerTrack = track;
387 if (allTracks_) {
388 for (Track* t : *allTracks_) {
389 if (t && t->getID() == track->getID()) { layerTrack = t; break; }
390 }
391 }
392 std::vector<int> layers = ah_->GetTrackHitLayers(layerTrack);
393 if (std::none_of(layers.begin(), layers.end(), [](int v){ return v == 1; })) {
394 // Matched full track had no layer vector; decode from this track's SvtHits.
395 TRefArray hits = track->getSvtHits();
396 for (int ih = 0; ih < hits.GetEntries(); ++ih) {
397 TrackerHit* th = (TrackerHit*) hits.At(ih);
398 if (th && th->getLayer() >= 0 && th->getLayer() < (int) layers.size())
399 layers.at(th->getLayer()) = 1;
400 }
401 }
402 // Encode the full per-sensor hit pattern as a bitmask (bit i = sensor slot i hit).
403 int hit_pattern = 0;
404 for (int i = 0; i < (int) layers.size(); ++i)
405 if (layers.at(i) == 1) hit_pattern |= (1 << i);
406
407 // Per-hit_pattern momentum scale correction (data momentum -> beam energy).
408 // p_corr = p * scale, keyed by hit_pattern (default_scale for unlisted patterns).
409 double hitPatternScale = 1.0;
410 bool hitPatternScaleIsDefault = false;
412 auto it = hitPatternScale_.find(hit_pattern);
413 if (it != hitPatternScale_.end()) {
414 hitPatternScale = it->second;
415 } else {
416 hitPatternScale = hitPatternDefaultScale_;
417 hitPatternScaleIsDefault = true;
418 }
419 }
420 // Corrected track: scale the momentum magnitude by hitPatternScale (omega scales as
421 // 1/scale since p is inversely proportional to |omega|).
422 Track trk_corr = *track;
423 if (hitPatternScale != 1.0) {
424 std::vector<double> mom = trk_corr.getMomentum();
425 for (double& c : mom) c *= hitPatternScale;
426 trk_corr.setMomentum(mom);
427 trk_corr.setOmega(trk_corr.getOmega() / hitPatternScale);
428 }
429
430 // Convention from sensor_locations.txt:
431 // Top: even sensor index = axial, odd = stereo
432 // Bottom: even sensor index = stereo, odd = axial
433 bool L1_axial = isTop ? (layers.at(0) == 1) : (layers.at(1) == 1);
434 bool L1_stereo = isTop ? (layers.at(1) == 1) : (layers.at(0) == 1);
435 bool L2_axial = isTop ? (layers.at(2) == 1) : (layers.at(3) == 1);
436 bool L2_stereo = isTop ? (layers.at(3) == 1) : (layers.at(2) == 1);
437 bool L3_axial = isTop ? (layers.at(4) == 1) : (layers.at(5) == 1);
438 bool L3_stereo = isTop ? (layers.at(5) == 1) : (layers.at(4) == 1);
439 bool L4_axial = isTop ? (layers.at(6) == 1) : (layers.at(7) == 1);
440 bool L4_stereo = isTop ? (layers.at(7) == 1) : (layers.at(6) == 1);
441
442 // Per-layer "has both axial+stereo hit" flags (top/bottom independent),
443 // matching the eleLN/posLN definition in PreselectAndCategorize2021.
444 bool L1 = (layers.at(0) == 1 && layers.at(1) == 1);
445 bool L2 = (layers.at(2) == 1 && layers.at(3) == 1);
446 bool L3 = (layers.at(4) == 1 && layers.at(5) == 1);
447 bool L4 = (layers.at(6) == 1 && layers.at(7) == 1);
448
449 // Earliest-layer category with the outward hit requirement, identical logic to the
450 // ele_L1/ele_L2/ele_L3 vertex categories in PreselectAndCategorize2021. For a single
451 // FEE track these are the per-track analog of the isL1L1/isL2L2/isL3L3 vertex flags:
452 // is_L1 = track starts at L1, is_L2 = starts at L2, is_L3 = starts at L3.
453 bool is_L1 = L1 && L2 && L3;
454 bool is_L2 = !L1 && L2 && L3;
455 bool is_L3 = !L1 && !L2 && L3;
456
457 if (debug_ > 0) {
458 std::cout << "[FeeAna] fsp[" << ipart << "]"
459 << " charge=" << part->getCharge()
460 << " nHits=" << n2dhits_onTrack
461 << " chi2ndf=" << track->getChi2Ndf()
462 << " p=" << trk_mom.Mag()
463 << " cluE=" << clu_E
464 << " E/p=" << eop
465 << " tanL=" << track->getTanLambda()
466 << " ecalX=" << track->getPositionAtEcal()[0]
467 << " trkTime=" << track->getTrackTime()
468 << " trkTime-offset=" << track->getTrackTime() - time_offset_
469 << std::endl;
470 }
471
472 //Track Selection
473 if (trkSelector_ && !trkSelector_->passCutGt("n_hits_gt",n2dhits_onTrack,weight)) {
474 if (debug_ > 0) std::cout << "[FeeAna] FAIL n_hits_gt: " << n2dhits_onTrack << std::endl;
475 continue;
476 }
477
478 if (trkSelector_ && !trkSelector_->passCutLt("chi2ndf_lt",track->getChi2Ndf(),weight)) {
479 if (debug_ > 0) std::cout << "[FeeAna] FAIL chi2ndf_lt: " << track->getChi2Ndf() << std::endl;
480 continue;
481 }
482
483 if (trkSelector_ && !trkSelector_->passCutGt("p_gt",trk_mom.Mag(),weight)) {
484 if (debug_ > 0) std::cout << "[FeeAna] FAIL p_gt: " << trk_mom.Mag() << std::endl;
485 continue;
486 }
487
488 if (trkSelector_ && !trkSelector_->passCutLt("p_lt",trk_mom.Mag(),weight)) {
489 if (debug_ > 0) std::cout << "[FeeAna] FAIL p_lt: " << trk_mom.Mag() << std::endl;
490 continue;
491 }
492
493 if (trkSelector_ && !trkSelector_->passCutLt("trk_ecal_lt",track->getPositionAtEcal()[0],weight)) {
494 if (debug_ > 0) std::cout << "[FeeAna] FAIL trk_ecal_lt: " << track->getPositionAtEcal()[0] << std::endl;
495 continue;
496 }
497
498 if (trkSelector_ && !trkSelector_->passCutGt("trk_ecal_gt",track->getPositionAtEcal()[0],weight)) {
499 if (debug_ > 0) std::cout << "[FeeAna] FAIL trk_ecal_gt: " << track->getPositionAtEcal()[0] << std::endl;
500 continue;
501 }
502
503 if (trkSelector_ && !trkSelector_->passCutGt("trk_time_gt",track->getTrackTime()-time_offset_,weight)) {
504 if (debug_ > 0) std::cout << "[FeeAna] FAIL trk_time_gt: " << track->getTrackTime() - time_offset_ << std::endl;
505 continue;
506 }
507
508 if (trkSelector_ && !trkSelector_->passCutLt("trk_time_lt",track->getTrackTime()-time_offset_,weight)) {
509 if (debug_ > 0) std::cout << "[FeeAna] FAIL trk_time_lt: " << track->getTrackTime() - time_offset_ << std::endl;
510 continue;
511 }
512
513 // In-time isolation: a clean FEE event has a single in-time cluster and a single
514 // in-time track. Count objects in-time with the primary FEE cluster (the primary
515 // itself is always counted). The counts are always computed and written to the
516 // output tree; the veto flags below only control whether a candidate is dropped.
517 // Cluster-cluster timing uses raw times (the offset cancels); track timing is
518 // compared to the offset-corrected cluster time, using the same MC track-time
519 // convention (time_offset_) as the track-time selection above.
520 int n_clusters_intime = 0;
521 int n_tracks_intime = 0;
522
523 if (ecalClusters_) {
524 for (CalCluster* c : *ecalClusters_) {
525 if (!c || c->getEnergy() < vetoClusterEnergyMin_) continue;
526 if (std::fabs(c->getTime() - clu_traw) < vetoClusterTimeWindow_) ++n_clusters_intime;
527 }
528 }
529 if (allTracks_) {
530 for (Track* t : *allTracks_) {
531 if (!t) continue;
532 if (std::fabs((t->getTrackTime() - time_offset_) - clu_t) < vetoTrackTimeWindow_) ++n_tracks_intime;
533 }
534 }
535
536 n_clusters_intime_out_ = n_clusters_intime;
537 n_tracks_intime_out_ = n_tracks_intime;
538
539 if (vetoExtraClusters_ && n_clusters_intime > 1) {
540 if (debug_ > 0) std::cout << "[FeeAna] FAIL extra in-time clusters: " << n_clusters_intime << std::endl;
541 continue;
542 }
543 if (vetoExtraTracks_ && n_tracks_intime > 1) {
544 if (debug_ > 0) std::cout << "[FeeAna] FAIL extra in-time tracks: " << n_tracks_intime << std::endl;
545 continue;
546 }
547
548 if (debug_ > 0) std::cout << "[FeeAna] PASS all cuts" << std::endl;
549
550 Track* truth_track = nullptr;
551
552 // Truth matching (MC only; always false on data). Computed independently of doTruth_
553 // so the flags are available in the output tree.
554 // truth_matched : hit-based majority match to an e^-/e^+, using the same
555 // utils::hasTruthMatch definition as the 2021 preselection.
556 // has_truth_link: raw Track TRef truth link resolves to an object (no PDG check).
557 bool truth_matched = utils::hasTruthMatch(track_obj, mcParticles_, debug_ > 0);
558 bool has_truth_link = (track->getTruthLink().GetObject() != nullptr);
559
560 //Get the truth track
561 if (doTruth_) {
562 truth_track = (Track*) track->getTruthLink().GetObject();
563 if (!truth_track)
564 std::cout<<"Warnings::FeeAnaProcessor::Requested Truth track but couldn't find it in the ntuple"<<std::endl;
565 }
566
567 if(debug_ > 0)
568 {
569 std::cout<<"========================================="<<std::endl;
570 std::cout<<"========================================="<<std::endl;
571 std::cout<<"Track params: "<<std::endl;
572 track->Print();
573 }
574
575 trkHistos_->Fill1DHistograms(track);
576 trkHistos_->Fill2DTrack(track);
577
578 if (truthHistos_) {
579 truthHistos_->Fill1DHistograms(truth_track);
580 truthHistos_->Fill2DTrack(track);
581 truthHistos_->Fill1DTrackTruth(track, truth_track);
582 }
583
584 n_sel_tracks++;
585
586 //Fill histograms for FEE smearing analysis
587 trkHistos_->Fill2DHisto("xypos_at_ecal_hh",
588 track->getPositionAtEcal()[0],
589 track->getPositionAtEcal()[1]);
590
591 trkHistos_->Fill3DHisto("p_vs_TanLambda_Phi_hhh",
592 track->getPhi(),
593 track->getTanLambda(),
594 track->getP());
595
596 trkHistos_->Fill2DHisto("p_vs_nHits_hh",
597 track->getTrackerHitCount(),
598 track->getP());
599
600 if (track->getTanLambda() > 0 )
601 trkHistos_->Fill2DHisto("p_vs_nHits_top_hh",
602 track->getTrackerHitCount(),
603 track->getP());
604 else
605 trkHistos_->Fill2DHisto("p_vs_nHits_bot_hh",
606 track->getTrackerHitCount(),
607 track->getP());
608
609 trkHistos_->Fill3DHisto("p_vs_TanLambda_nHits_hhh",
610 track->getTanLambda(),
611 track->getTrackerHitCount(),
612 track->getP());
613
614
615
616 // Track-level quantities for output tree and smearing histograms.
617 // trk_smeared starts as a copy of the original; update* methods modify it in-place.
618 Track trk_smeared = *track;
619
620 // All track corrections (data scale corrections, MC smearing) go into this one
621 // collection, so track_smeared is the calibrated track for data and MC while track
622 // stays raw.
623 double omega_unsmeared = track->getOmega();
624 double omega_smeared = omega_unsmeared;
625 double p_smear_ratio = 1.0;
626 run_scale_out_ = 1.0;
627 if (corrector_) {
628 TrackCorrector::Result corr = corrector_->correct({&trk_smeared}).front();
629 run_scale_out_ = corr.pScale;
630 p_smear_ratio = corr.pSmear;
631 // pSmear = |omega/omega_smeared| = |p_smeared/p_original|
632 if (corrector_->smearsOmega()) omega_smeared = omega_unsmeared / corr.pSmear;
633 }
634
635 if (corrector_ && corrector_->hasSmearing()) {
636
637 double nhits = track->getTrackerHitCount();
638
639 double pval = trk_smeared.getP();
640 double z0val = trk_smeared.getZ0();
641
642 psmear_h_->Fill(pval);
643 psmear_vs_nHits_hh_->Fill(nhits, pval);
644
645 if (isTop) {
646 psmear_top_h_->Fill(pval);
647 psmear_vs_nHits_top_hh_->Fill(nhits, pval);
648 }
649 else {
650 psmear_bot_h_->Fill(pval);
651 psmear_vs_nHits_bot_hh_->Fill(nhits, pval);
652 }
653
654 psmear_rel_h_->Fill(pval);
655 psmear_vs_nHits_rel_hh_->Fill(nhits, pval);
656
657 if (isTop) {
658 psmear_vs_nHits_top_rel_hh_->Fill(nhits, pval);
659 }
660 else {
661 psmear_vs_nHits_bot_rel_hh_->Fill(nhits, pval);
662 }
663
664 // z0 smearing validation
665 z0smear_h_->Fill(z0val);
666 if (isTop) {
667 z0smear_top_h_->Fill(z0val);
668 }
669 else {
670 z0smear_bot_h_->Fill(z0val);
671 }
672
673 // omega smearing validation
674 double tanL = track->getTanLambda();
675 double phi0 = track->getPhi();
676
677 omega_h_->Fill(omega_unsmeared);
678 omegasmear_h_->Fill(omega_smeared);
679 omega_vs_p_hh_->Fill(track->getP(), omega_unsmeared);
680 omega_vs_tanL_hh_->Fill(tanL, omega_unsmeared);
681 omega_vs_phi0_hh_->Fill(phi0, omega_unsmeared);
682 omegasmear_vs_tanL_hh_->Fill(tanL, omega_smeared);
683 omegasmear_vs_phi0_hh_->Fill(phi0, omega_smeared);
684
685 if (isTop) {
686 omega_top_h_->Fill(omega_unsmeared);
687 omegasmear_top_h_->Fill(omega_smeared);
688 omega_vs_tanL_top_hh_->Fill(tanL, omega_unsmeared);
689 omega_vs_phi0_top_hh_->Fill(phi0, omega_unsmeared);
690 omegasmear_vs_tanL_top_hh_->Fill(tanL, omega_smeared);
691 omegasmear_vs_phi0_top_hh_->Fill(phi0, omega_smeared);
692 }
693 else {
694 omega_bot_h_->Fill(omega_unsmeared);
695 omegasmear_bot_h_->Fill(omega_smeared);
696 omega_vs_tanL_bot_hh_->Fill(tanL, omega_unsmeared);
697 omega_vs_phi0_bot_hh_->Fill(phi0, omega_unsmeared);
698 omegasmear_vs_tanL_bot_hh_->Fill(tanL, omega_smeared);
699 omegasmear_vs_phi0_bot_hh_->Fill(phi0, omega_smeared);
700 }
701
702 } // smearing validation
703
704 // Fill output tree (one entry per selected FEE track)
705 if (output_tree_) {
706 track_out_ = *track; // original, unsmeared
707 track_smeared_out_ = trk_smeared; // smeared (same as original if doSmearing_=false)
708 p_smear_ratio_out_ = p_smear_ratio;
709 clu_E_out_ = clu_E;
710 clu_time_out_ = clu_t;
711 eop_out_ = eop;
712 L1_axial_out_ = L1_axial;
713 L1_stereo_out_ = L1_stereo;
714 L2_axial_out_ = L2_axial;
715 L2_stereo_out_ = L2_stereo;
716 L3_axial_out_ = L3_axial;
717 L3_stereo_out_ = L3_stereo;
718 L4_axial_out_ = L4_axial;
719 L4_stereo_out_ = L4_stereo;
720 L1_out_ = L1;
721 L2_out_ = L2;
722 L3_out_ = L3;
723 L4_out_ = L4;
724 is_L1_out_ = is_L1;
725 is_L2_out_ = is_L2;
726 is_L3_out_ = is_L3;
727 hit_pattern_out_ = hit_pattern;
728 n_hits_out_ = n2dhits_onTrack;
729 track_corr_out_ = trk_corr;
730 hit_pattern_scale_out_ = hitPatternScale;
731 hit_pattern_scale_isdefault_out_ = hitPatternScaleIsDefault;
732 truth_matched_out_ = truth_matched;
733 has_truth_link_out_ = has_truth_link;
734 output_tree_->Fill();
735 }
736
737 }//Loop on FinalStateParticles
738
739 trkHistos_->Fill1DHisto("n_tracks_h",n_sel_tracks);
740
741 return true;
742}
743
745
746 if (output_tree_) {
747 outF_->cd();
748 output_tree_->Write();
749 }
750
751 trkHistos_->saveHistos(outF_,trkCollName_);
752 delete trkHistos_;
753 trkHistos_ = nullptr;
754 if (trkSelector_)
755 trkSelector_->getCutFlowHisto()->Write();
756
757 if (truthHistos_) {
758 truthHistos_->saveHistos(outF_,trkCollName_+"_truth");
759 delete truthHistos_;
760 truthHistos_ = nullptr;
761 }
762
763 for (reg_it it = reg_histos_.begin(); it!=reg_histos_.end(); ++it) {
764 std::string dirName = it->first;
765 (it->second)->saveHistos(outF_,dirName);
766 outF_->cd(dirName.c_str());
767 reg_selectors_[it->first]->getCutFlowHisto()->Write();
768 }
769
770 if (corrector_ && corrector_->hasSmearing()) {
771 outF_->cd(trkCollName_.c_str());
772 psmear_h_->Write();
773 psmear_top_h_->Write();
774 psmear_bot_h_->Write();
775
776 psmear_vs_nHits_hh_->Write();
779 delete psmear_h_;
780 delete psmear_top_h_;
781 delete psmear_bot_h_;
782 delete psmear_vs_nHits_hh_;
785
786 psmear_rel_h_->Write();
790 delete psmear_rel_h_;
794
795 // z0 smearing validation histograms
796 z0smear_h_->Write();
797 z0smear_top_h_->Write();
798 z0smear_bot_h_->Write();
799 delete z0smear_h_;
800 delete z0smear_top_h_;
801 delete z0smear_bot_h_;
802
803 // omega smearing validation histograms
804 omega_h_->Write();
805 omega_top_h_->Write();
806 omega_bot_h_->Write();
807 omegasmear_h_->Write();
808 omegasmear_top_h_->Write();
809 omegasmear_bot_h_->Write();
810 omega_vs_p_hh_->Write();
811 omega_vs_tanL_hh_->Write();
812 omega_vs_tanL_top_hh_->Write();
813 omega_vs_tanL_bot_hh_->Write();
814 omegasmear_vs_tanL_hh_->Write();
817 omega_vs_phi0_hh_->Write();
818 omega_vs_phi0_top_hh_->Write();
819 omega_vs_phi0_bot_hh_->Write();
820 omegasmear_vs_phi0_hh_->Write();
823
824 delete omega_h_;
825 delete omega_top_h_;
826 delete omega_bot_h_;
827 delete omegasmear_h_;
828 delete omegasmear_top_h_;
829 delete omegasmear_bot_h_;
830 delete omega_vs_p_hh_;
831 delete omega_vs_tanL_hh_;
837 delete omega_vs_phi0_hh_;
843
844 }
845
846}
847
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
static std::string getFileName(std::string filePath, bool withExtension)
Definition AnaHelpers.cxx:5
double getEnergy() const
Definition CalCluster.h:73
double getTime() const
Definition CalCluster.h:83
Select Full Energy Electron (FEE) events for the momentum-smearing calibration.
bool vetoExtraClusters_
veto if extra clusters are in-time with the FEE cluster
TBranch * becalClusters_
TH2D * omegasmear_vs_phi0_bot_hh_
double run_scale_out_
data momentum scale applied to this track (1 if none); raw p = p / scale
TBranch * bfsps_
description
virtual bool process(IEvent *ievent)
Process the event and put new data products into it.
bool hitPatternScaleDataOnly_
only apply the scale on data (MC left untouched)
std::vector< CalCluster * > * ecalClusters_
std::map< int, double > hitPatternScale_
hit_pattern -> scale factor
std::string trkCollName_
Track Collection name (histo/smearing naming).
double hit_pattern_scale_out_
scale factor applied to this track's momentum
std::string selectionCfg_
std::string truthHistCfgFilename_
description
int n_tracks_intime_out_
tracks in-time with the FEE cluster
bool requireCluster_
require an associated FEE cluster passing E/time/(E/p)
double calTimeOffsetMC_
cluster-time offset subtracted before the cut (MC)
TH2D * omegasmear_vs_tanL_top_hh_
virtual void configure(const ParameterSet &parameters)
Configure the Ana Processor.
bool doTruth_
description
Track track_out_
original (unsmeared) track
double clusterTimeMax_
maximum corrected cluster time (data)
TH2D * psmear_vs_nHits_rel_hh_
virtual void finalize()
Callback for the Processor to take any necessary action when the processing of events finishes.
TH2D * omegasmear_vs_tanL_bot_hh_
std::vector< Track * > * allTracks_
TH2D * omegasmear_vs_phi0_hh_
std::map< std::string, std::shared_ptr< TrackHistos > > reg_histos_
description
bool doSmearing_
MC smearing on/off for this job.
std::vector< std::string > regionSelections_
track selections
double clusterTimeMinMC_
minimum corrected cluster time (MC)
std::vector< Particle * > * fsps_
virtual void initialize(TTree *tree)
Callback for the Processor to take any necessary action when the processing of events starts.
virtual void setFile(TFile *outFile)
Set output TFile and create the output TTree.
std::string fspCollName_
FinalStateParticle collection name.
double hitPatternDefaultScale_
fallback scale for unlisted patterns
double vetoClusterEnergyMin_
only clusters above this energy count toward the cluster veto (GeV)
std::string mcColl_
MCParticle collection name.
std::vector< MCParticle * > * mcParticles_
TH2D * omegasmear_vs_tanL_hh_
Track track_smeared_out_
smeared track (identical to track_out_ if no smearing)
bool vetoExtraTracks_
veto if extra tracks are in-time with the FEE cluster
std::string hitPatternScaleFile_
JSON of per-hit_pattern scale factors.
std::map< std::string, std::shared_ptr< TrackHistos > >::iterator reg_it
description
bool doHitPatternScale_
master switch for the hit-pattern scale correction
TBranch * bmcParticles_
EventHeader * evth_
double eop_out_
associated cluster E / track p
double clu_E_out_
associated cluster energy
double vetoTrackTimeWindow_
|t_trk - t_FEEclu(corrected)| window for the track veto (ns)
std::string histCfgFilename_
description
TrackHistos * trkHistos_
description
std::shared_ptr< TrackCorrector > corrector_
int n_hits_out_
number of tracker hits on the FEE track
FeeAnaProcessor(const std::string &name, Process &process)
Class constructor.
std::map< std::string, std::shared_ptr< BaseSelector > > reg_selectors_
description
int n_clusters_intime_out_
clusters in-time with the FEE cluster (>energy threshold)
TH2D * omegasmear_vs_phi0_top_hh_
double p_smear_ratio_out_
smeared_p / original_p (1 if no smearing)
bool hit_pattern_scale_isdefault_out_
true if the default (fallback) scale was used
double clusterTimeMin_
minimum corrected cluster time (data)
double calTimeOffset_
cluster-time offset subtracted before the cut (data)
double clu_time_out_
associated cluster time
double feeClusterEnergyMin_
time offset
double clusterTimeMaxMC_
maximum corrected cluster time (MC)
TH2D * psmear_vs_nHits_bot_rel_hh_
bool requireElectron_
require the FSP to be an electron (charge < 0)
double eopMax_
maximum cluster-energy / track-momentum (E/p)
TrackHistos * truthHistos_
description
Track track_corr_out_
momentum-scale-corrected track
int requireTruthMatch_
-1: use the JSON value; 0/1: override it
TH2D * psmear_vs_nHits_bot_hh_
std::shared_ptr< AnaHelpers > ah_
double eopMin_
minimum cluster-energy / track-momentum (E/p)
TH2D * psmear_vs_nHits_top_hh_
std::string trackCorrectionsCfg_
TrackCorrector JSON ("" = no corrections).
double vetoClusterTimeWindow_
|t_clu - t_FEEclu| window for the cluster veto (ns)
TH2D * psmear_vs_nHits_top_rel_hh_
bool requireFeeTrigger_
require the FEE trigger (TSBank FEE_Top||FEE_Bot); data only
std::vector< std::string > regions_
double mcTimeOffset_
time offset applied to MC tracks
std::shared_ptr< BaseSelector > trkSelector_
description
std::unique_ptr< TTree > output_tree_
Definition IEvent.h:7
description
Track getTrack() const
Definition Particle.h:48
CalCluster getCluster() const
Definition Particle.h:62
int getCharge() const
Definition Particle.h:131
TFile * outF_
Definition Processor.h:125
std::string name_
Definition Processor.h:128
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
virtual void setFile(TFile *outFile)
Set output TFile for AnaProcessors.
Definition Processor.h:88
description
Definition TrackHistos.h:17
Definition Track.h:32
double getOmega() const
Definition Track.h:90
double getTrackTime() const
Definition Track.h:162
void setOmega(const double omega)
Definition Track.h:91
std::vector< double > getMomentum()
Definition Track.h:287
double getZ0() const
Definition Track.h:93
double getP() const
Definition Track.h:293
void setMomentum(double bfield=0.52)
Definition Track.cxx:63
int getLayer() const
Definition TrackerHit.h:109
bool hasTruthMatch(Track &trk, const std::vector< MCParticle * > *mcParticles, bool debug=false)
Check if a track has a truth-matched electron/positron.
bool doSmearing
MC Gaussian smearing on/off for this job.
int requireTruthMatch
-1: use the JSON value; 0/1: override it
double pSmear
smearing step (MC)
double pScale
momentumScale step (data)