Skip to content

Commit

Permalink
Update verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
EinarElen committed Aug 3, 2023
1 parent fedd4d0 commit 3602ca0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 46 deletions.
37 changes: 17 additions & 20 deletions DQM/include/DQM/ReSimVerifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,35 @@ class ReSimVerifier : public framework::Analyzer {
ReSimVerifier(const std::string &name, framework::Process &process)
: framework::Analyzer{name, process} {}
void configure(framework::config::Parameters &parameters) override;

void analyze(const framework::Event &event) override;

/*
* Check that the simhits between the two collections are identical.
*
* Determine which of x/y/z corresponds to the direction along, across, and
* through the bar respectively. Along corresponding to the length of the bar,
* across to the width of the bar, and through to the thickness of the bar.
*
* @return: False if any hit is different between the two
*
*/
std::array<int, 3> determine_indices(const ldmx::HcalID id);
**/
bool verifySimCalorimeterHits(
const std::vector<ldmx::SimCalorimeterHit> &simHits,
const std::vector<ldmx::SimCalorimeterHit> &reSimHits);

/*
* Check that all SimParticles are present in both passes of the event
*
* Check if the hit at position `position` is within the bounds of the
* scintillator bar with HcalID `id`.
* @return: False if any SimParticle is different between the two
*
* @note: On error, this function will raise an exception if `stop_on_error`
* is set to true or log the issue if false.
*
*/
bool hit_ok(const ldmx::HcalID id, const std::array<double, 3> &position);

void analyze(const framework::Event &event) override;
**/
bool verifySimParticles(const framework::Event &event);

private:
std::string hcalSimHitsCollection_{"HcalSimHits"};
std::string hcalReSimHitsCollection_{"HcalReSimHits"};
std::vector<std::string> collections;
std::string simPassName_{""};
std::string reSimPassName_{""};

// Maximum difference between position and bounds of the scintillator bar that
// will be accepted [mm]
double tolerance{};
/*
* If true, abort on the first mismatch. Otherwise, report and continue.
**/
bool stop_on_error{};
};
} // namespace dqm
Expand Down
27 changes: 17 additions & 10 deletions DQM/python/dqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@
from LDMX.Framework import ldmxcfg

class ReSimVerifier(ldmxcfg.Analyzer) :
"""Configured HCalGeometryVerifier python object
Contains an instance of the verifier that has already been configured.
This analyzer verifies that all simhits and rechits for the hcal are within
the bounds of the scintillator strips as set in the geometry condition.
If the analyzer encounters an error and `stop_on_error` is true, raises an
exception with details about the issue. Otherwise, the error is logged and
histograms for each section is produced.
"""Configured ReSimVerifier python object
Contains an instance of the verifier that has already been configured. This
analyzer does not produce anything, it just checks that the sim hits and sim
particles between two different passes are the same.
Examples
--------
from LDMX.DQM import dqm
p.sequence.append( dqm.ReSimVerifier() )
"""

def __init__(self,name="hcal_geometry_verifier", stop_on_error=False) :
super().__init__(name,'dqm::ReSimVerifier','DQM')
self.hcal_sim_coll_name = 'HcalSimHits'
self.hcal_resim_coll_name = 'HcalReSimHits'
self.collections = [
'HcalSimHits',
'EcalSimHits',
'TargetSimHits',
'TriggerPad1SimHits',
'TriggerPad2SimHits',
'TriggerPad3SimHits',
'RecoilSimHits',
'TaggerSimHits',
]
self.sim_pass_name = ''
self.resim_pass_name = 'resim'
self.stop_on_error=stop_on_error
self.tolerance=1e-3 # mm


class HCalDQM(ldmxcfg.Analyzer) :
Expand Down
104 changes: 88 additions & 16 deletions DQM/src/DQM/ReSimVerifier.cxx
Original file line number Diff line number Diff line change
@@ -1,28 +1,100 @@
#include "DQM/ReSimVerifier.h"
namespace dqm {

void ReSimVerifier::configure(framework::config::Parameters &parameters) {
hcalSimHitsCollection_ =
parameters.getParameter<std::string>("hcal_sim_coll_name");
void ReSimVerifier::configure(framework::config::Parameters& parameters) {
simPassName_ = parameters.getParameter<std::string>("sim_pass_name");
reSimPassName_ = parameters.getParameter<std::string>("resim_pass_name");
hcalReSimHitsCollection_ =
parameters.getParameter<std::string>("hcal_resim_coll_name");
stop_on_error = parameters.getParameter<bool>("stop_on_error");
tolerance = parameters.getParameter<double>("tolerance");
collections =
parameters.getParameter<std::vector<std::string>>("collections");
}
void ReSimVerifier::analyze(const framework::Event &event) {
const auto hcalSimHits = event.getCollection<ldmx::SimCalorimeterHit>(
hcalSimHitsCollection_, simPassName_);
bool ReSimVerifier::verifySimCalorimeterHits(
const std::vector<ldmx::SimCalorimeterHit>& simHits,
const std::vector<ldmx::SimCalorimeterHit>& reSimHits) {
for (auto i{0}; i < simHits.size(); ++i) {
auto hit{simHits[i]};
auto rehit{reSimHits[i]};

const auto hcalReSimHits = event.getCollection<ldmx::SimCalorimeterHit>(
hcalSimHitsCollection_, reSimPassName_);

for (auto i{0}; i < hcalSimHits.size(); ++i) {
auto hit{hcalSimHits[i]};
auto rehit{hcalReSimHits[i]};
if (hit.getEdep() != rehit.getEdep()) {
throw 32;
return false;
}
if (hit.getID() != rehit.getID()) {
return false;
}
if (hit.getTime() != rehit.getTime()) {
return false;
}
if (hit.getNumberOfContribs() != rehit.getNumberOfContribs()) {
return false;
}
auto pos{hit.getPosition()};
auto repos{rehit.getPosition()};
if (pos[0] != repos[0] || pos[1] != repos[1] || pos[2] != repos[2]) {
return false;
}
}
return true;
}

bool ReSimVerifier::verifySimParticles(const framework::Event& event) {
const auto& simParticles{
event.getMap<int, ldmx::SimParticle>("SimParticles", simPassName_)};
const auto& reSimParticles{
event.getMap<int, ldmx::SimParticle>("SimParticles", reSimPassName_)};
for (auto [id, simParticle] : simParticles) {
if (!reSimParticles.count(id)) {
return false;
}
auto reSimParticle{reSimParticles.at(id)};
if (simParticle.getEnergy() != reSimParticle.getEnergy()) {
return false;
}
if (simParticle.getPdgID() != reSimParticle.getPdgID()) {
return false;
}
if (simParticle.getTime() != reSimParticle.getTime()) {
return false;
}
}
return true;
}
void ReSimVerifier::analyze(const framework::Event& event) {
std::stringstream ss;
bool passing{true};
bool skipped{false};
auto eventNumber{event.getEventNumber()};
for (auto collection : collections) {
const auto SimHits =
event.getCollection<ldmx::SimCalorimeterHit>(collection, simPassName_);
const auto ReSimHits = event.getCollection<ldmx::SimCalorimeterHit>(
collection, reSimPassName_);
if (ReSimHits.size() == 0) {
skipped = true;
continue;
} else {
skipped = false;
}

if (!verifySimCalorimeterHits(SimHits, ReSimHits)) {
passing = false;
ss << "Event " << eventNumber << " has different simhits for "
<< collection << std::endl;
}
}
if (skipped) {
std::cout << "Skipping event " << eventNumber
<< "since it was not resimulated" << std::endl;
}
if (!verifySimParticles(event)) {
passing = false;
ss << "Event " << eventNumber
<< " has different SimParticles between the two passes" << std::endl;
}
if (!passing) {
if (stop_on_error) {
EXCEPTION_RAISE("ReSimVerify", ss.str());
} else {
std::cout << ss.str();
}
}

Expand Down

0 comments on commit 3602ca0

Please sign in to comment.