-
Notifications
You must be signed in to change notification settings - Fork 23
Add CLUE clustering #1699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add CLUE clustering #1699
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bcfba75
manually copy over CLUE clustering files from Ella's developments
tomeichlersmith ce75999
store the track ID of the 'origin' particle which may be more precise…
tomeichlersmith 0c4d9ea
bump ClassDef version after introducing new member
tomeichlersmith 6f62ea1
use variable that was determined earlier
tomeichlersmith 1f8b0a0
move CLUE into logging, transition member variables to snake_case
tomeichlersmith df49df7
dont override, that interacts poorly with ROOT's ClassDef macro
tomeichlersmith 91cf649
purge debug_ flag, drop debug messages to trace
tomeichlersmith e174c4c
Apply clang-format
github-actions[bot] 965bbda
parameter naming in python to snake_case
tomeichlersmith 944ac69
add example config to test-run clustering
tomeichlersmith bc0b4ab
merge Working clusters and prefer pointers to save on copying
tomeichlersmith f63f743
Apply clang-format
github-actions[bot] bff3594
remove unused variables
tomeichlersmith bab9bd3
rename WorkingCluster to IntermediateCluster for clarity
tomeichlersmith 929dac7
move cluster analyzer into DQM
tomeichlersmith 76337ee
move max origin track ID from hardcode to config parameter
tomeichlersmith 236cef6
add ecal clustering to ecal_pn, inclusive, it_pileup samples
tomeichlersmith eab73fd
clang-format
tomeichlersmith 9dd8195
fix typo in ecal_pn config
tomeichlersmith 68543d8
Merge branch 'trunk' into 1411-add-clue-clustering
tomeichlersmith 6b1b1b7
update declaration macro from Factory updates
tomeichlersmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @file EcalClusterAnalyzer.h | ||
* @brief Analysis of cluster performance | ||
* @author Ella Viirola, Lund University | ||
*/ | ||
|
||
#ifndef DQM_ECALCLUSTERANALYZER_H | ||
#define DQM_ECALCLUSTERANALYZER_H | ||
|
||
// LDMX Framework | ||
#include "Framework/Configure/Parameters.h" | ||
#include "Framework/EventProcessor.h" | ||
|
||
namespace dqm { | ||
|
||
/** | ||
* @class EcalClusterAnalyzer | ||
* @brief | ||
*/ | ||
class EcalClusterAnalyzer : public framework::Analyzer { | ||
public: | ||
EcalClusterAnalyzer(const std::string& name, framework::Process& process) | ||
: Analyzer(name, process) {} | ||
~EcalClusterAnalyzer() override = default; | ||
void configure(framework::config::Parameters& ps) override; | ||
void analyze(const framework::Event& event) override; | ||
|
||
private: | ||
int nbr_of_electrons_; | ||
|
||
// Collection Name for SimHits | ||
std::string ecal_sim_hit_coll_; | ||
|
||
// Pass Name for SimHits | ||
std::string ecal_sim_hit_pass_; | ||
|
||
// Collection Name for RecHits | ||
std::string rec_hit_coll_name_; | ||
|
||
// Pass Name for RecHits | ||
std::string rec_hit_pass_name_; | ||
|
||
// Collection Name for clusters | ||
std::string cluster_coll_name_; | ||
|
||
// Pass Name for clusters | ||
std::string cluster_pass_name_; | ||
}; | ||
|
||
} // namespace dqm | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* @file EcalClusterAnalyzer.cxx | ||
* @brief Analysis of cluster performance | ||
* @author Ella Viirola, Lund University | ||
*/ | ||
|
||
#include "DQM/EcalClusterAnalyzer.h" | ||
|
||
#include <algorithm> | ||
#include <fstream> | ||
#include <iostream> | ||
|
||
#include "DetDescr/SimSpecialID.h" | ||
#include "Ecal/Event/EcalCluster.h" | ||
#include "Ecal/Event/EcalHit.h" | ||
#include "SimCore/Event/SimCalorimeterHit.h" | ||
#include "SimCore/Event/SimTrackerHit.h" | ||
|
||
namespace dqm { | ||
|
||
void EcalClusterAnalyzer::configure(framework::config::Parameters& ps) { | ||
nbr_of_electrons_ = ps.getParameter<int>("nbr_of_electrons"); | ||
|
||
ecal_sim_hit_coll_ = ps.getParameter<std::string>("ecal_sim_hit_coll"); | ||
ecal_sim_hit_pass_ = ps.getParameter<std::string>("ecal_sim_hit_pass"); | ||
|
||
rec_hit_coll_name_ = ps.getParameter<std::string>("rec_hit_coll_name"); | ||
rec_hit_pass_name_ = ps.getParameter<std::string>("rec_hit_pass_name"); | ||
|
||
cluster_coll_name_ = ps.getParameter<std::string>("cluster_coll_name"); | ||
cluster_pass_name_ = ps.getParameter<std::string>("cluster_pass_name"); | ||
return; | ||
} | ||
|
||
void EcalClusterAnalyzer::analyze(const framework::Event& event) { | ||
const auto& ecal_rec_hits{event.getCollection<ldmx::EcalHit>( | ||
rec_hit_coll_name_, rec_hit_pass_name_)}; | ||
const auto& ecal_sim_hits{event.getCollection<ldmx::SimCalorimeterHit>( | ||
ecal_sim_hit_coll_, ecal_sim_hit_pass_)}; | ||
const auto& ecal_clusters{event.getCollection<ldmx::EcalCluster>( | ||
cluster_coll_name_, cluster_pass_name_)}; | ||
|
||
if (ecal_clusters.size() == nbr_of_electrons_) | ||
histograms_.fill("correctly_predicted_events", 1); // correct | ||
else if (ecal_clusters.size() < nbr_of_electrons_) | ||
histograms_.fill("correctly_predicted_events", 0); // undercounting | ||
else if (ecal_clusters.size() > nbr_of_electrons_) | ||
histograms_.fill("correctly_predicted_events", 2); // overcounting | ||
|
||
std::unordered_map<int, std::pair<int, std::vector<double>>> hitInfo; | ||
hitInfo.reserve(ecal_rec_hits.size()); | ||
|
||
double dist; | ||
if (nbr_of_electrons_ == 2) { | ||
// Measures distance between two electrons in the ECal scoring plane | ||
// TODO: generalize for n electrons | ||
std::vector<float> pos1; | ||
std::vector<float> pos2; | ||
bool p1 = false; | ||
bool p2 = false; | ||
|
||
const auto& ecal_sp_hits{ | ||
event.getCollection<ldmx::SimTrackerHit>("EcalScoringPlaneHits")}; | ||
for (const ldmx::SimTrackerHit& spHit : ecal_sp_hits) { | ||
if (spHit.getTrackID() == 1) { | ||
pos1 = spHit.getPosition(); | ||
p1 = true; | ||
} else if (spHit.getTrackID() == 2) { | ||
pos2 = spHit.getPosition(); | ||
p2 = true; | ||
} | ||
} | ||
if (p1 && p2) | ||
dist = std::sqrt(std::pow((pos1[0] - pos2[0]), 2) + | ||
std::pow((pos1[1] - pos2[1]), 2)); | ||
} | ||
|
||
for (const auto& hit : ecal_rec_hits) { | ||
auto it = std::find_if( | ||
ecal_sim_hits.begin(), ecal_sim_hits.end(), | ||
[&hit](const auto& simHit) { return simHit.getID() == hit.getID(); }); | ||
if (it != ecal_sim_hits.end()) { | ||
// if found a simhit matching this rechit | ||
int ancestor = 0; | ||
int prevAncestor = 0; | ||
bool tagged = false; | ||
int tag = 0; | ||
std::vector<double> edep; | ||
edep.resize(nbr_of_electrons_ + 1); | ||
for (int i = 0; i < it->getNumberOfContribs(); i++) { | ||
// for each contrib in this simhit | ||
const auto& c = it->getContrib(i); | ||
// get origin electron ID | ||
ancestor = c.originID; | ||
// store energy from this contrib at index = origin electron ID | ||
if (ancestor <= nbr_of_electrons_) edep[ancestor] += c.edep; | ||
if (!tagged && i != 0 && prevAncestor != ancestor) { | ||
// if origin electron ID does not match previous origin electron ID | ||
// this hit has contributions from several electrons, ie mixed case | ||
tag = 0; | ||
tagged = true; | ||
} | ||
prevAncestor = ancestor; | ||
} | ||
if (!tagged) { | ||
// if not tagged, hit was from a single electron | ||
tag = prevAncestor; | ||
} | ||
histograms_.fill("ancestors", tag); | ||
hitInfo.insert({hit.getID(), std::make_pair(tag, edep)}); | ||
} | ||
} | ||
|
||
int clusteredHits = 0; | ||
|
||
for (const auto& cl : ecal_clusters) { | ||
// for each cluster | ||
// total number of hits coming from electron, index = electron ID | ||
std::vector<double> n; | ||
n.resize(nbr_of_electrons_ + 1); | ||
// total number of energy coming from electron, index = electron ID | ||
std::vector<double> e; | ||
e.resize(nbr_of_electrons_ + 1); | ||
double eSum = 0.; | ||
double nSum = 0.; | ||
|
||
const auto& hitIDs = cl.getHitIDs(); | ||
for (const auto& id : hitIDs) { | ||
// for each hit in cluster, find previously stored info | ||
auto it = hitInfo.find(id); | ||
if (it != hitInfo.end()) { | ||
auto t = it->second; | ||
auto eId = t.first; // origin electron ID (or 0 for mixed) | ||
auto energies = t.second; // energy vector | ||
n[eId]++; // increment number of hits coming from this electron | ||
nSum++; | ||
|
||
double hitESum = 0.; | ||
for (int i = 1; i < nbr_of_electrons_ + 1; i++) { | ||
// loop through energy vector | ||
if (energies[i] > 0.) { | ||
hitESum += energies[i]; | ||
// add energy from electron i in this hit to total energy from | ||
// electron i in cluster | ||
e[i] += energies[i]; | ||
} | ||
} | ||
// if mixed hit, add the total energy of this hit to mixed hit energy | ||
// counter | ||
if (eId == 0) e[0] += hitESum; | ||
eSum += hitESum; | ||
|
||
clusteredHits++; | ||
} | ||
} | ||
|
||
if (eSum > 0) { | ||
// get largest energy contribution | ||
double eMax = *max_element(e.begin(), e.end()); | ||
// energy purity = largest contribution / all energy | ||
histograms_.fill("energy_percentage", 100. * (eMax / eSum)); | ||
if (e[0] > 0.) histograms_.fill("mixed_hit_energy", 100. * (e[0] / eSum)); | ||
|
||
histograms_.fill("total_energy_vs_hits", eSum, cl.getHitIDs().size()); | ||
histograms_.fill("total_energy_vs_purity", eSum, 100. * (eMax / eSum)); | ||
|
||
if (nbr_of_electrons_ == 2) | ||
histograms_.fill("distance_energy_purity", dist, 100. * (eMax / eSum)); | ||
} | ||
if (nSum > 0) { | ||
double nMax = *max_element(n.begin(), n.end()); | ||
histograms_.fill("same_ancestor", 100. * (nMax / nSum)); | ||
} | ||
} | ||
|
||
histograms_.fill("clusterless_hits", (ecal_rec_hits.size() - clusteredHits)); | ||
histograms_.fill("total_rechits_in_event", ecal_rec_hits.size()); | ||
histograms_.fill( | ||
"clusterless_hits_percentage", | ||
100. * (ecal_rec_hits.size() - clusteredHits) / ecal_rec_hits.size()); | ||
} | ||
|
||
} // namespace dqm | ||
|
||
DECLARE_ANALYZER(dqm::EcalClusterAnalyzer) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from LDMX.Framework import ldmxcfg | ||
p = ldmxcfg.Process('cluster') | ||
import sys | ||
p.inputFiles = sys.argv[1:] | ||
p.outputFiles = [ 'clusters.root' ] | ||
p.histogramFile = 'h_clusters.root' | ||
|
||
from LDMX.Ecal.ecalClusters import * | ||
p.sequence = [ | ||
EcalClusterProducer(), | ||
EcalClusterAnalyzer() | ||
] | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.