diff --git a/Validation/CMakeLists.txt b/Validation/CMakeLists.txt index d53ed0fd1d..5b3f5415b0 100644 --- a/Validation/CMakeLists.txt +++ b/Validation/CMakeLists.txt @@ -34,6 +34,8 @@ cet_make_library( src/ValTimeCluster.cc src/ValTriggerInfo.cc src/ValTriggerResults.cc + src/ValTrkCrvMatch.cc + src/ValTrkCrvMatchMC.cc LIBRARIES PUBLIC Offline::DataProducts diff --git a/Validation/inc/ValTrkCrvMatch.hh b/Validation/inc/ValTrkCrvMatch.hh new file mode 100644 index 0000000000..e48fa0ba0a --- /dev/null +++ b/Validation/inc/ValTrkCrvMatch.hh @@ -0,0 +1,66 @@ +#ifndef ValTrkCrvMatch_HH_ +#define ValTrkCrvMatch_HH_ + +#include "Offline/RecoDataProducts/inc/TrkCrvMatch.hh" +#include "Offline/Validation/inc/ValTrkCrvSectorAxis.hh" +#include "KinKal/General/Vectors.hh" +#include "art/Framework/Principal/Event.h" +#include "art_root_io/TFileDirectory.h" +#include "TH1D.h" +#include "TH2D.h" +#include "TProfile.h" +#include +#include +#include + +namespace mu2e { + +// track-CRV association: candidate counts, residuals and chi2 per CRV sector, dv against slope (alignment monitor), +// predicted vs matched sector, layer probe +class ValTrkCrvMatch { + public: + ValTrkCrvMatch(std::string name) : _name(name) {} + int declare(const art::TFileDirectory& tfs); + int fill(const TrkCrvMatchCollection& coll, art::Event const& event); + std::string& name() { return _name; } + + private: + std::string _name; + struct Plane { // a sector of this job's geometry: centre, across-bar direction, normal, dv vs slope + KinKal::VEC3 c, v, w; + TProfile* p = nullptr; + }; + ValTrkCrvSectorAxis _axis; + std::map _plane; // by axis bin + std::map _shieldBin; // product shield index (sector_) -> axis bin, from the crossings + static constexpr double _planeTol = 1.0; // max distance of a crossing from the job's sector mid-plane [mm] + + TH1D* _hVer; + TH1D* _hNTrk; + TH1D* _hNCand; + TH1D* _hNGeom; + TH1D* _hNXing; + TH1D* _hDtRef; + TH1D* _hDtRefBest; + TH1D* _hDtRefToF; + TH1D* _hChi2; + TH1D* _hNShare; + TH1D* _hFlags; + TH1D* _hXingShield; + TH1D* _hXingGeomShield; + TH2D* _hDtXing; + TH2D* _hDu; + TH2D* _hDv; + TH2D* _hPredMatched; + TH1D* _hSlopeNoGeom; + TH1D* _hProbePred; + TH1D* _hProbeHit; + TH1D* _hProbeInCluster; + TH1D* _hProbeDBar; + TH1D* _hProbeDt; + TH1D* _hProbePE; + TH1D* _hProbeDu; +}; +} // namespace mu2e + +#endif diff --git a/Validation/inc/ValTrkCrvMatchMC.hh b/Validation/inc/ValTrkCrvMatchMC.hh new file mode 100644 index 0000000000..3c27027f3a --- /dev/null +++ b/Validation/inc/ValTrkCrvMatchMC.hh @@ -0,0 +1,44 @@ +#ifndef ValTrkCrvMatchMC_HH_ +#define ValTrkCrvMatchMC_HH_ + +#include "Offline/MCDataProducts/inc/TrkCrvMatchMC.hh" +#include "Offline/Validation/inc/ValTrkCrvSectorAxis.hh" +#include "art/Framework/Principal/Event.h" +#include "art_root_io/TFileDirectory.h" +#include "TH1D.h" +#include "TH2D.h" +#include + +namespace mu2e { + +// track-CRV association truth: relation of the best candidates, own-cluster association and crossing truth per +// CRV sector, reco crossings against the true crossings +class ValTrkCrvMatchMC { + public: + ValTrkCrvMatchMC(std::string name) : _name(name) {} + int declare(const art::TFileDirectory& tfs); + int fill(const TrkCrvMatchMCCollection& coll, art::Event const& event); + std::string& name() { return _name; } + + private: + std::string _name; + ValTrkCrvSectorAxis _axis; + + TH1D* _hVer; + TH1D* _hBestGeomRel; + TH1D* _hBestTimeRel; + TH1D* _hGeomCandRel; + TH1D* _hTimeCandRel; + TH1D* _hTrueClu; + TH1D* _hTrueCluGeom; + TH1D* _hTrueCluBest; + TH1D* _hRecoXing; + TH1D* _hRecoXingTrue; + TH1D* _hTrueXing; + TH1D* _hTrueXingFound; + TH2D* _hXingDist; + TH2D* _hXingDt; +}; +} // namespace mu2e + +#endif diff --git a/Validation/inc/ValTrkCrvSectorAxis.hh b/Validation/inc/ValTrkCrvSectorAxis.hh new file mode 100644 index 0000000000..567d929743 --- /dev/null +++ b/Validation/inc/ValTrkCrvSectorAxis.hh @@ -0,0 +1,47 @@ +#ifndef ValTrkCrvSectorAxis_HH_ +#define ValTrkCrvSectorAxis_HH_ + +#include "Offline/DataProducts/inc/SurfaceId.hh" +#include "TAxis.h" +#include +#include +#include + +namespace mu2e { + +// fixed, labelled histogram axis of the CRV sector SurfaceIds, so the per-sector histograms do not depend on the job +// geometry; the last bin collects sectors without a SurfaceId +class ValTrkCrvSectorAxis { + public: + ValTrkCrvSectorAxis() { + for (auto const& [id, name] : SurfaceIdDetail::names()) { + if (name.rfind("CRV_", 0) != 0 || id == SurfaceIdDetail::CRV_StrongBack) continue; + _byName[name] = _bin[id] = static_cast(_names.size()); + _names.push_back(name); + } + _names.push_back("unknown"); + } + int n() const { return static_cast(_names.size()); } + int unknown() const { return n() - 1; } + double lo() const { return -0.5; } + double hi() const { return n() - 0.5; } + int bin(SurfaceId const& sid) const { + auto it = _bin.find(sid.id().id()); + return it == _bin.end() ? unknown() : it->second; + } + int bin(std::string const& name) const { + auto it = _byName.find(name); + return it == _byName.end() ? unknown() : it->second; + } + void label(TAxis* ax) const { + for (int i = 0; i < n(); ++i) ax->SetBinLabel(i + 1, _names[i].c_str()); + } + + private: + std::map _bin; + std::map _byName; + std::vector _names; +}; +} // namespace mu2e + +#endif diff --git a/Validation/src/ValTrkCrvMatch.cc b/Validation/src/ValTrkCrvMatch.cc new file mode 100644 index 0000000000..d39bbd6912 --- /dev/null +++ b/Validation/src/ValTrkCrvMatch.cc @@ -0,0 +1,125 @@ +#include "Offline/Validation/inc/ValTrkCrvMatch.hh" +#include "Offline/GeometryService/inc/GeomHandle.hh" +#include "Offline/KinKalGeom/inc/KinKalGeom.hh" +#include + +int mu2e::ValTrkCrvMatch::declare(const art::TFileDirectory& tfs) { + int ns = _axis.n(); + double lo = _axis.lo(), hi = _axis.hi(); + _hVer = tfs.make("Ver", "Version Number", 101, -0.5, 100.0); + _hNTrk = tfs.make("NTrk", "N tracks", 21, -0.5, 20.5); + _hNCand = tfs.make("NCand", "N candidates per track", 51, -0.5, 50.5); + _hNGeom = tfs.make("NGeom", "N geometric candidates per track", 21, -0.5, 20.5); + _hNXing = tfs.make("NXing", "N CRV crossings per track", 11, -0.5, 10.5); + _hDtRef = tfs.make("dtRef", "t_{trk}(ref) - t_{cluster}, all candidates;ns", 200, -1000., 1000.); + _hDtRefBest = tfs.make("dtRefBest", "t_{trk}(ref) - t_{cluster}, best candidate;ns", 200, -400., 400.); + _hDtRefToF = tfs.make("dtRefToF", "dtRef - flight time, best candidate;ns", 200, -400., 400.); + _hChi2 = tfs.make("chi2ndof", "#chi^{2}/ndof, best in sector", 100, 0., 20.); + _hNShare = tfs.make("NShare", "tracks sharing the cluster", 11, -0.5, 10.5); + _hFlags = tfs.make("Flags", "candidate flag bits", 16, -0.5, 15.5); + _hXingShield = tfs.make("XingShield", "CRV crossings per sector", ns, lo, hi); + _hXingGeomShield = tfs.make("XingGeomShield", "crossings with a geometric candidate in their sector", ns, lo, hi); + _hDtXing = tfs.make("dtXing", "t_{cluster} - t_{trk} at the plane, best in sector;cluster sector;ns", ns, lo, hi, 100, -100., 100.); + _hDu = tfs.make("du", "cluster - track along the bars, best in sector;cluster sector;mm", ns, lo, hi, 100, -3000., 3000.); + _hDv = tfs.make("dv", "cluster - track across the bars, best in sector;cluster sector;mm", ns, lo, hi, 100, -500., 500.); + _hPredMatched = tfs.make("PredMatched", "best geometric candidate;reference crossing sector;cluster sector", ns, lo, hi, ns, lo, hi); + _hSlopeNoGeom = tfs.make("SlopeNoGeom", "best in sector, not in dvVsSlope: sector not in this job's geometry;cluster sector", ns, lo, hi); + for (TH1* h : {static_cast(_hXingShield), static_cast(_hXingGeomShield), static_cast(_hDtXing), + static_cast(_hDu), static_cast(_hDv), static_cast(_hPredMatched), static_cast(_hSlopeNoGeom)}) + _axis.label(h->GetXaxis()); + _axis.label(_hPredMatched->GetYaxis()); + + // dv vs slope needs the sector directions, from this job's geometry: one profile per sector it has + GeomHandle kkg; + for (auto const& sec : kkg->CRV()->sectors()) { + int b = _axis.bin(sec.sname_); + if (b == _axis.unknown() || _plane.count(b)) continue; + auto& pl = _plane[b]; + pl.c = sec.sector_->center(); + pl.v = sec.sector_->vDirection(); + pl.w = sec.sector_->normal(); + pl.p = tfs.make(("dvVsSlope_" + sec.sname_).c_str(), ("dv vs across-bar slope, " + sec.sname_ + ";slope;dv [mm]").c_str(), 40, -2., 2.); + } + _hProbePred = tfs.make("ProbePred", "layer probe: predicted bar per layer", 6, -0.5, 5.5); + _hProbeHit = tfs.make("ProbeHit", "layer probe: pulse found per layer", 6, -0.5, 5.5); + _hProbeInCluster = tfs.make("ProbeInCluster", "layer probe: pulse in a cluster per layer", 6, -0.5, 5.5); + _hProbeDBar = tfs.make("ProbeDBar", "layer probe: selected - predicted bar", 21, -10.5, 10.5); + _hProbeDt = tfs.make("ProbeDt", "layer probe: pulse - predicted time;ns", 100, -60., 60.); + _hProbePE = tfs.make("ProbePE", "layer probe: PEs", 100, 0., 200.); + _hProbeDu = tfs.make("ProbeDu", "layer probe: two-end position - predicted;mm", 100, -1500., 1500.); + return 0; +} + +int mu2e::ValTrkCrvMatch::fill(const mu2e::TrkCrvMatchCollection& coll, art::Event const& event) { + // increment this by 1 any time the definitions of the histograms or the + // histogram contents change, and will not match previous versions + _hVer->Fill(1.0); + + // a cluster's sector: the SurfaceId of a crossing with the same shield index in the product's geometry + auto clusterBin = [&](int16_t shield) { + auto it = _shieldBin.find(shield); + return it == _shieldBin.end() ? _axis.unknown() : it->second; + }; + _hNTrk->Fill(coll.size()); + for (auto const& m : coll) { + for (auto const& x : m.xings_) _shieldBin[x.sector_] = _axis.bin(x.sid_); + _hNCand->Fill(m.cands_.size()); + _hNXing->Fill(m.xings_.size()); + int ngeom = 0; + for (auto const& c : m.cands_) { + _hDtRef->Fill(c.dtRef_); + _hNShare->Fill(c.nShare_); + for (int b = 0; b < 16; ++b) + if (c.flags_ & (1u << b)) _hFlags->Fill(b); + if (!c.hasFlag(TrkCrvCandidate::geomCand)) continue; + ++ngeom; + if (!c.hasFlag(TrkCrvCandidate::bestInSector)) continue; + int b = clusterBin(c.sector_); + if (c.ndof_ > 0) _hChi2->Fill(c.chi2_ / c.ndof_); + _hDtXing->Fill(b, c.dtXing_); + if (!c.hasFlag(TrkCrvCandidate::singleEnded)) _hDu->Fill(b, c.du_); + _hDv->Fill(b, c.dv_); + if (c.xing_ < 0 || static_cast(c.xing_) >= m.xings_.size()) continue; + // the job's directions apply only if its plane of the reference crossing's sector holds that crossing + auto const& x = m.xings_[c.xing_]; + auto ix = _plane.find(_axis.bin(x.sid_)); + auto ic = _plane.find(b); + KinKal::VEC3 pos(x.pos_.X(), x.pos_.Y(), x.pos_.Z()); + if (ix == _plane.end() || ic == _plane.end() || std::fabs((pos - ix->second.c).Dot(ix->second.w)) > _planeTol) { + _hSlopeNoGeom->Fill(b); + continue; + } + KinKal::VEC3 dir(x.dir_.X(), x.dir_.Y(), x.dir_.Z()); + double dw = dir.Dot(ic->second.w); + if (std::fabs(dw) > 1e-3) ic->second.p->Fill(dir.Dot(ic->second.v) / dw, c.dv_); + } + _hNGeom->Fill(ngeom); + int best = m.bestGeom_ >= 0 ? m.bestGeom_ : m.bestTime_; + if (best >= 0) { + _hDtRefBest->Fill(m.cands_[best].dtRef_); + _hDtRefToF->Fill(m.cands_[best].dtRefToF_); + } + if (m.bestGeom_ >= 0) { + auto const& c = m.cands_[m.bestGeom_]; + if (c.xing_ >= 0 && static_cast(c.xing_) < m.xings_.size()) + _hPredMatched->Fill(_axis.bin(m.xings_[c.xing_].sid_), clusterBin(c.sector_)); + } + for (auto const& x : m.xings_) { + _hXingShield->Fill(_axis.bin(x.sid_)); + if (x.best_ >= 0) _hXingGeomShield->Fill(_axis.bin(x.sid_)); + for (size_t L = 0; L < x.layers_.size(); ++L) { + auto const& p = x.layers_[L]; + if (p.bar_ < 0) continue; + _hProbePred->Fill(L); + if (p.dBar_ == INT8_MAX) continue; + _hProbeHit->Fill(L); + if (p.inCluster_) _hProbeInCluster->Fill(L); + _hProbeDBar->Fill(p.dBar_); + _hProbeDt->Fill(p.dt_); + _hProbePE->Fill(p.pe_); + if (!std::isnan(p.uTdiv_)) _hProbeDu->Fill(p.uTdiv_ - p.uPred_); + } + } + } + return 0; +} diff --git a/Validation/src/ValTrkCrvMatchMC.cc b/Validation/src/ValTrkCrvMatchMC.cc new file mode 100644 index 0000000000..8fa26f738a --- /dev/null +++ b/Validation/src/ValTrkCrvMatchMC.cc @@ -0,0 +1,71 @@ +#include "Offline/Validation/inc/ValTrkCrvMatchMC.hh" +#include + +int mu2e::ValTrkCrvMatchMC::declare(const art::TFileDirectory& tfs) { + int ns = _axis.n(); + double lo = _axis.lo(), hi = _axis.hi(); + _hVer = tfs.make("Ver", "Version Number", 101, -0.5, 100.0); + _hBestGeomRel = tfs.make("BestGeomRel", "best geometric candidate, MCRelationship to the track's particle: 0 no MC, 1 unrelated, 2 same, 3 daughter, 4 mother, 5 sibling, 6 udaughter, 7 umother, 8 usibling", 9, -0.5, 8.5); + _hBestTimeRel = tfs.make("BestTimeRel", "best time candidate, MCRelationship to the track's particle", 9, -0.5, 8.5); + _hGeomCandRel = tfs.make("GeomCandRel", "all geometric candidates, MCRelationship", 9, -0.5, 8.5); + _hTimeCandRel = tfs.make("TimeCandRel", "time-only candidates, MCRelationship", 9, -0.5, 8.5); + _hTrueClu = tfs.make("TrueClu", "own cluster of the track's particle per sector", ns, lo, hi); + _hTrueCluGeom = tfs.make("TrueCluGeom", "own cluster is a geometric candidate", ns, lo, hi); + _hTrueCluBest = tfs.make("TrueCluBest", "own cluster is the best in its sector", ns, lo, hi); + _hRecoXing = tfs.make("RecoXing", "reco crossings per sector", ns, lo, hi); + _hRecoXingTrue = tfs.make("RecoXingTrue", "reco crossings with a true crossing of the same sector", ns, lo, hi); + _hTrueXing = tfs.make("TrueXing", "true crossings of the track's particle per sector", ns, lo, hi); + _hTrueXingFound = tfs.make("TrueXingFound", "true crossings with a reco crossing", ns, lo, hi); + _hXingDist = tfs.make("XingDist", "|reco - true crossing|;sector;mm", ns, lo, hi, 100, 0., 500.); + _hXingDt = tfs.make("XingDt", "reco - true crossing time;sector;ns", ns, lo, hi, 100, -20., 20.); + for (TH1* h : {static_cast(_hTrueClu), static_cast(_hTrueCluGeom), static_cast(_hTrueCluBest), + static_cast(_hRecoXing), static_cast(_hRecoXingTrue), static_cast(_hTrueXing), + static_cast(_hTrueXingFound), static_cast(_hXingDist), static_cast(_hXingDt)}) + _axis.label(h->GetXaxis()); + return 0; +} + +int mu2e::ValTrkCrvMatchMC::fill(const mu2e::TrkCrvMatchMCCollection& coll, art::Event const& event) { + // increment this by 1 any time the definitions of the histograms or the + // histogram contents change, and will not match previous versions + _hVer->Fill(1.0); + + for (auto const& mc : coll) { + if (mc.match_.isNull() || !mc.match_.isAvailable()) continue; + auto const& m = *mc.match_; + if (mc.cands_.size() != m.cands_.size()) continue; + auto relBin = [](TrkCrvCandidateMC const& cm) { return cm.hasMC_ ? cm.rel_.relationship() + 2 : 0; }; // 0 no MC, 1 none, 2 same, ... + if (m.bestGeom_ >= 0) _hBestGeomRel->Fill(relBin(mc.cands_[m.bestGeom_])); + if (m.bestTime_ >= 0) _hBestTimeRel->Fill(relBin(mc.cands_[m.bestTime_])); + for (size_t j = 0; j < m.cands_.size(); ++j) + (m.cands_[j].hasFlag(TrkCrvCandidate::geomCand) ? _hGeomCandRel : _hTimeCandRel)->Fill(relBin(mc.cands_[j])); + for (auto const& xm : mc.xings_) { + if (xm.rel_ != MCRelationship::same) continue; + int b = _axis.bin(xm.sid_); + if (xm.geom_) { + _hTrueXing->Fill(b); + if (xm.recoXing_ >= 0) _hTrueXingFound->Fill(b); + } + if (xm.trueCluster_ < 0) continue; + _hTrueClu->Fill(b); + if (xm.trueCand_ < 0 || static_cast(xm.trueCand_) >= m.cands_.size()) continue; + auto const& c = m.cands_[xm.trueCand_]; + if (!c.hasFlag(TrkCrvCandidate::geomCand)) continue; + _hTrueCluGeom->Fill(b); + if (c.hasFlag(TrkCrvCandidate::bestInSector)) _hTrueCluBest->Fill(b); + } + for (size_t ir = 0; ir < m.xings_.size() && ir < mc.recoXingTruth_.size(); ++ir) { + auto const& rx = m.xings_[ir]; + int b = _axis.bin(rx.sid_); + _hRecoXing->Fill(b); + int t = mc.recoXingTruth_[ir]; + if (t < 0 || static_cast(t) >= mc.xings_.size()) continue; + _hRecoXingTrue->Fill(b); + auto const& xm = mc.xings_[t]; + if (!xm.geom_ || xm.rel_ != MCRelationship::same) continue; + _hXingDist->Fill(b, std::sqrt((rx.pos_ - xm.pos_).Mag2())); + _hXingDt->Fill(b, rx.time_ - xm.time_); + } + } + return 0; +} diff --git a/Validation/src/Validation_module.cc b/Validation/src/Validation_module.cc index 45f678791a..d8365aaa29 100644 --- a/Validation/src/Validation_module.cc +++ b/Validation/src/Validation_module.cc @@ -39,6 +39,8 @@ #include "Offline/Validation/inc/ValTimeCluster.hh" #include "Offline/Validation/inc/ValTriggerInfo.hh" #include "Offline/Validation/inc/ValTriggerResults.hh" +#include "Offline/Validation/inc/ValTrkCrvMatch.hh" +#include "Offline/Validation/inc/ValTrkCrvMatchMC.hh" #include "art/Framework/Core/EDAnalyzer.h" #include "art_root_io/TFileService.h" #include "fhiclcpp/types/Atom.h" @@ -103,6 +105,8 @@ class Validation : public art::EDAnalyzer { std::vector> _stht; std::vector> _trrs; std::vector> _tris; + std::vector> _tcrv; + std::vector> _tcmc; // Loop over the products of type T and // call fill() on validation histogram class V to make histograms. @@ -155,6 +159,8 @@ void mu2e::Validation::analyze(art::Event const& event) { analyzeProduct(_tmcl, event); analyzeProduct(_hxsd, event); analyzeProduct(_klsd, event); + analyzeProduct(_tcrv, event); + analyzeProduct(_tcmc, event); analyzeProduct(_stmw, event); analyzeProduct(_stmw, event); analyzeProduct(_trrs, event);