LDMX Software
HcalDigiPipelineTest.cxx
1#include <catch2/catch_approx.hpp>
2#include <catch2/catch_test_macros.hpp>
3#include <catch2/matchers/catch_matchers.hpp>
4
5using Catch::Approx;
6
7#include "DetDescr/HcalID.h" //creating unique hcal IDs
8#include "Framework/Configure/Python.h"
10#include "Framework/Process.h"
11#include "Hcal/Event/HcalHit.h"
14
15namespace hcal {
16namespace test {
17
24static const double PE_ENERGY = 4.66 / 68; // 0.069 MeV
25
43// static const double MAX_ENERGY_ERROR_DAQ = 4 * PE_ENERGY;
44// static const double MAX_ENERGY_PERCENT_ERROR_DAQ = 0.2;
45static const double MAX_PE_ERROR_DAQ = 40;
46// large percentage error for now
47static const double MAX_PE_PERCENT_ERROR_DAQ = 0.4;
48
59static const int NUM_TEST_SIM_HITS = 1000;
60
66class IsCloseEnough : public Catch::Matchers::MatcherBase<double> {
67 private:
69 double truth_;
70
72 const double MAX_ABSOLUTE_DIFF;
73
75 const double MAX_RELATIVE_DIFF;
76
77 public:
83 IsCloseEnough(double const &truth, double const &abs_diff,
84 double const &rel_diff)
85 : truth_{truth},
86 MAX_ABSOLUTE_DIFF{abs_diff},
87 MAX_RELATIVE_DIFF{rel_diff} {}
88
96 bool match(const double &daq) const override {
97 return (daq == Approx(truth_).epsilon(MAX_RELATIVE_DIFF) or
98 daq == Approx(truth_).margin(MAX_ABSOLUTE_DIFF));
99 }
100
104 virtual std::string describe() const override {
105 std::ostringstream ss;
106 ss << "is within an absolute difference of " << MAX_ABSOLUTE_DIFF
107 << " OR a relative difference of " << MAX_RELATIVE_DIFF << " with "
108 << truth_;
109 return ss.str();
110 }
111};
112
125 // Based on the current gain settings for the ADC readout mode
126 // we will reach saturation ~ 20 MeV ~ 290 PEs
127 const double MAX_ENERGY = 200 * PE_ENERGY; // ~ 13 MeV
128
133 const double MIN_ENERGY = 4 * PE_ENERGY;
139 const double ENERGY_STEP = (MAX_ENERGY - MIN_ENERGY) / NUM_TEST_SIM_HITS;
140
143
144 public:
145 HcalFakeSimHits(const std::string &name, framework::Process &p)
146 : framework::Producer(name, p) {}
148
149 void beforeNewRun(ldmx::RunHeader &header) final override {
150 header.setDetectorName("ldmx-det-v12");
151 }
152
153 void produce(framework::Event &event) final override {
154 // put in a single sim hit
155 std::vector<ldmx::SimCalorimeterHit> pretend_sim_hits(1);
156
157 // We hard-code the position of one hit: back hcal, layer_ 1, strip 31
158 // This real simHit position is obtained by looking at calorimeter
159 // SimHits of a 4 GeV muon shoot through the beamline
160 ldmx::HcalID id(0, 1, 31);
161 pretend_sim_hits[0].setPosition(-6.70265, 3.70265, 879); // mm
162 pretend_sim_hits[0].setID(id.raw());
163 pretend_sim_hits[0].addContrib(
164 -1, // incidentID
165 -1, // trackID
166 0, // pdg ID
167 curr_energy_, // edep
168 2.96628 // time - 299mm is about 1ns from target and in middle of HCal
169 );
170
171 // needs to be correct collection name
172 // REQUIRE_NOTHROW(event.add("HcalSimHits", pretendSimHits));
173 REQUIRE_NOTHROW(event.add("HcalFakeSimHits", pretend_sim_hits));
175
176 return;
177 }
178}; // HcalFakeSimHits
179
193 // save ntuple? False by default because if ntuplizer is on, the HcalGeometry
194 // test cannot be run
195 const bool SAVE = false;
196
197 private:
198 std::string hcal_fake_sim_hits_passname_;
199 std::string hcal_rec_hits_passname_;
200 std::string hcal_digis_passname_;
201
202 public:
203 HcalCheckReconstruction(const std::string &name, framework::Process &p)
204 : framework::Analyzer(name, p) {}
206
208 hcal_fake_sim_hits_passname_ =
209 ps.getParameter("hcal_fake_sim_hits_passname", "");
210 hcal_digis_passname_ = ps.getParameter("hcal_digis_passname", "");
211 hcal_rec_hits_passname_ = ps.getParameter("hcal_rec_hits_passname", "");
212 }
213
214 void onProcessStart() final override {
215 if (SAVE) {
217 ntuple_.create("HcalDigiTest");
218 ntuple_.addVar<float>("HcalDigiTest", "SimEnergy");
219 ntuple_.addVar<float>("HcalDigiTest", "RecEnergy");
220 ntuple_.addVar<float>("HcalDigiTest", "SimX");
221 ntuple_.addVar<float>("HcalDigiTest", "SimY");
222 ntuple_.addVar<float>("HcalDigiTest", "SimZ");
223 ntuple_.addVar<float>("HcalDigiTest", "SimTime");
224 ntuple_.addVar<float>("HcalDigiTest", "RecX");
225 ntuple_.addVar<float>("HcalDigiTest", "RecY");
226 ntuple_.addVar<float>("HcalDigiTest", "RecZ");
227 ntuple_.addVar<float>("HcalDigiTest", "RecTime");
228 ntuple_.addVar<int>("HcalDigiTest", "DaqDigi");
229 ntuple_.addVar<int>("HcalDigiTest", "DaqDigiIsADC");
230 ntuple_.addVar<int>("HcalDigiTest", "DaqDigiADC");
231 ntuple_.addVar<int>("HcalDigiTest", "DaqDigiTOT");
232 }
233 }
234
235 void analyze(const framework::Event &event) final override {
236 const auto sim_hits = event.getCollection<ldmx::SimCalorimeterHit>(
237 "HcalFakeSimHits", hcal_fake_sim_hits_passname_);
238
239 REQUIRE(sim_hits.size() == 1);
240
241 float truth_energy = sim_hits.at(0).getEdep();
242
243 if (SAVE) {
244 ntuple_.setVar<float>("SimEnergy", truth_energy);
245 ntuple_.setVar<float>("SimX", sim_hits.at(0).getPosition()[0]);
246 ntuple_.setVar<float>("SimY", sim_hits.at(0).getPosition()[1]);
247 ntuple_.setVar<float>("SimZ", sim_hits.at(0).getPosition()[2]);
248 ntuple_.setVar<float>("SimTime", sim_hits.at(0).getContrib(0).time_);
249 }
250
251 const auto daq_digis{event.getObject<ldmx::HgcrocDigiCollection>(
252 "HcalDigis", hcal_digis_passname_)};
253 auto daq_digi = daq_digis.getDigi(0);
254 bool is_in_adc_mode = daq_digi.isADC();
255
256 if (SAVE) {
257 ntuple_.setVar<int>("DaqDigi", daq_digi.soi().raw());
258 ntuple_.setVar<int>("DaqDigiIsADC", is_in_adc_mode);
259 ntuple_.setVar<int>("DaqDigiADC", daq_digi.soi().adcT());
260 ntuple_.setVar<int>("DaqDigiTOT", daq_digi.tot());
261 }
262
263 const auto rec_hits = event.getCollection<ldmx::HcalHit>(
264 "HcalRecHits", hcal_rec_hits_passname_);
265 CHECK(rec_hits.size() == 1);
266
267 auto hit = rec_hits.at(0);
268 ldmx::HcalID id(hit.getID());
269 CHECK_FALSE(hit.isNoise());
270 CHECK(id.raw() == sim_hits.at(0).getID());
271
272 if (SAVE) {
273 ntuple_.setVar<float>("RecX", hit.getXPos());
274 ntuple_.setVar<float>("RecY", hit.getYPos());
275 ntuple_.setVar<float>("RecZ", hit.getZPos());
276 ntuple_.setVar<float>("RecTime", hit.getTime());
277 ntuple_.setVar<float>("RecPE", hit.getPE());
278 ntuple_.setVar<float>("RecEnergy", hit.getEnergy());
279 }
280
281 // define target pe by using the settings at the top
282 double daq_pe{hit.getPE()};
283 CHECK_THAT(daq_pe, IsCloseEnough(truth_energy / PE_ENERGY, MAX_PE_ERROR_DAQ,
284 MAX_PE_PERCENT_ERROR_DAQ));
285
286 // std::cout << "rec energy " << hit.getEnergy() << " * approx sampl
287 // fraction " << hit.getEnergy()*sampling_fraction << " truth " <<
288 // truth_energy
289 // << std::endl;
290 // std::cout << "npes " << hit.getPE() << " approx PE " << int(truth_energy
291 // / PE_ENERGY) << std::endl;
292 /*
293 if (id.section() == 0) {
294 double truth_pos, rec_pos;
295 if ((id.layer() % 2) == 1) {
296 truth_pos = simHits.at(0).getPosition()[0];
297 rec_pos = hit.getXPos();
298 } else {
299 truth_pos = simHits.at(0).getPosition()[1];
300 rec_pos = hit.getYPos();
301 }
302 // std::cout << "rec pos_ " << rec_pos << " truth " << truth_pos <<
303 // std::endl;
304 // comment position check for now
305 // CHECK_THAT(rec_pos, isCloseEnough(truth_pos,
306 MAX_POSITION_ERROR_DAQ,
307 // MAX_POSITION_PERCENT_ERROR_DAQ));
308 }
309 */
310 return;
311 }
312}; // HcalCheckReconstruction
313
314} // namespace test
315} // namespace hcal
316
319
334TEST_CASE("Hcal Digi Pipeline test", "[Hcal][functionality]") {
335 const std::string config_file{"hcal_digi_pipeline_test_config.py"};
336 char **args{nullptr};
337
338 auto cfg{framework::config::run("ldmxcfg.Process.lastProcess", config_file,
339 args, 0)};
340 auto p{std::make_unique<framework::Process>(cfg)};
341 p->run();
342}
Base classes for all user event processing components to extend.
#define DECLARE_ANALYZER(CLASS)
Macro which allows the framework to construct an analyzer given its name during configuration.
#define DECLARE_PRODUCER(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Class that stores Stores reconstructed hit information from the HCAL.
Class that defines an HCal sensitive detector.
Class that represents a digitized hit in a calorimeter cell readout by an HGCROC.
Class which represents the process under execution.
Class which stores simulated calorimeter hit information.
Base class for a module which does not produce a data product.
NtupleManager & ntuple_
Manager for any ntuples.
TDirectory * getHistoDirectory()
Access/create a directory in the histogram file for this event processor to create histograms and ana...
Implements an event buffer system for storing event data.
Definition Event.h:42
void addVar(const std::string &tname, const std::string &vname)
Add a variable of type VarType to the ROOT tree with name 'tname'.
void create(const std::string &tname)
Create a ROOT tree to hold the ntuple variables (ROOT leaves).
void setVar(const std::string &vname, const T &value)
Set the value of the variable named 'vname'.
Class which represents the process under execution.
Definition Process.h:36
Base class for a module which produces a data product.
Producer(const std::string &name, Process &process)
Class constructor.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
void analyze(const framework::Event &event) final override
Process the event and make histograms or summaries.
void onProcessStart() final override
Callback for the EventProcessor to take any necessary action when the processing of events starts,...
void configure(framework::config::Parameters &ps) override
Callback for the EventProcessor to configure itself from the given set of parameters.
const double ENERGY_STEP
The step between energies is calculated depending on the min, max energy and the total number of sim ...
const double MAX_ENERGY
Maximum energy to make a simulated hit for [MeV].
double curr_energy_
current energy of the sim hit we are on
const double MIN_ENERGY
Minimum energy to make a sim hit for [MeV] Needs to be above readout threshold (after internal HcalDi...
void beforeNewRun(ldmx::RunHeader &header) final override
Callback for Producers to add parameters to the run header before conditions are initialized.
void produce(framework::Event &event) final override
Process the event and put new data products into it.
Our custom checker which makes sure that the input energy/position is "close enough" to the truth ene...
virtual std::string describe() const override
Describes matcher for printing to terminal.
bool match(const double &daq) const override
Performs the test for this matcher.
const double MAX_ABSOLUTE_DIFF
maximum absolute difference
IsCloseEnough(double const &truth, double const &abs_diff, double const &rel_diff)
Constructor.
const double MAX_RELATIVE_DIFF
maximum relative difference
double truth_
correct (sim-level)
Stores reconstructed hit information from the HCAL.
Definition HcalHit.h:24
Implements detector ids for HCal subdetector.
Definition HcalID.h:19
Represents a collection of the digi hits readout by an HGCROC.
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:57
Stores simulated calorimeter hit information.
Parameters run(const std::string &root_object, const std::string &pythonScript, char *args[], int nargs)
run the python script and extract the parameters
Definition Python.cxx:300
All classes in the ldmx-sw project use this namespace.