Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CRVReco/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ cet_make_library(
SOURCE
src/CrvHelper.cc
src/MakeCrvRecoPulses.cc
src/TrkCrvGeom.cc
src/TrkCrvMatchTool.cc
LIBRARIES PUBLIC

Offline::CosmicRayShieldGeom
Offline::DataProducts
Offline::GeometryService
Offline::KinKalGeom
Offline::RecoDataProducts
KinKal::General
KinKal::Geometry
art::Framework_Services_Registry
fhiclcpp::types
messagefacility::MF_MessageLogger
ROOT::Hist
)

Expand All @@ -30,6 +39,19 @@ cet_build_plugin(CrvCoincidenceFinder art::module
Offline::RecoDataProducts
)

cet_build_plugin(TrkCrvMatcher art::module
REG_SOURCE src/TrkCrvMatcher_module.cc
LIBRARIES REG
Offline::CRVReco
Offline::CosmicRayShieldGeom
Offline::DataProducts
Offline::GeometryService
Offline::KinKalGeom
Offline::RecoDataProducts
KinKal::General
KinKal::Geometry
)


cet_build_plugin(CrvFPGArate art::module
REG_SOURCE src/CrvFPGArate_module.cc
Expand Down
39 changes: 39 additions & 0 deletions CRVReco/fcl/prolog_v12.fcl
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,45 @@ BEGIN_PROLOG
addOppositeSidePulses : true //add pulses without a coincidence at their own side to the cluster containing the other end of their counter
}

# track-CRV association, one instance per KalSeed collection (set KalSeedCollection); see TrkCrvMatchTool.hh.
# Resolutions and the track error model are from an MC truth study of 1.07M KKLine tracks (50 MDC2025bb cosmic
# files, extracted v06 geometry); the windows are loose storage windows, not selections.
TrkCrvMatcher :
{
module_type : TrkCrvMatcher
CrvCoincidenceClusterCollection : "SelectReco:CrvCoincidenceClusterFinder"
CrvRecoPulseCollection : "CrvRecoPulses"
ProbeAllPulses : true # CrvRecoPulses holds every reco pulse (SelectReco only the clustered ones)
ReferenceSurfaces : [ "TT_Front", "TT_Back", "TT_Outer" ]
EarliestReference : true # reference time = tracker entry
DtRefWindow : [ -350, 350 ] # ns
# DtRefCenter centres the best-time ranking only (the window is not shifted). It depends on the fit type and the
# geometry: this default is the median dtRef of each track's own cluster for KKLine on extracted-geometry MC
# (MDC2025bb cosmics); set it per instance for other fits (Production JobConfig/reco/prolog.fcl).
DtRefCenter : 13 # ns
DtXingWindow : [ -100, 100 ] # ns
MaxDv : 500 # mm
MaxDu : 3000 # mm
DoGeometric : true
# CRV resolution per cluster sector type (type 1 first). Sector type numbering depends on the geometry: these are
# the extracted geometry's types 1-4 (EX, T1, T2, muon taggers; crv_counters_extracted_v04.txt); other geometries
# need their own lists. SigmaV: robust spread of (cluster - true crossing) across the bars; SigmaU: along the bars,
# two-ended clusters (T1 and the taggers are single-ended).
SigmaV : [ 7, 13, 7, 17 ] # mm
SigmaU : [ 300 ] # mm
SigmaT : [ 5 ] # ns provisional (MC dtXing robust width 2.0-2.4 ns, two-ended)
SigmaTSingleEnded : 15 # ns provisional (MC dtXing robust width about 4 ns)
# track position error model: k^2 C + a^2 I, from pulls of the track against the true CRV crossing
TrackErrorScale : 1.5
TrackPosErrQuad : 24 # mm
MinTrackTimeErr : 1 # ns
LayerProbe : true
ProbeBarWindow : 1 # bars on each side of the predicted bar
ProbeDtWindow : [ -50, 50 ] # ns
FiberSignalSpeed : @local::CrvCoincidenceClusterFinder.fiberSignalSpeed
ProbePulseDelay : @local::CrvCoincidenceClusterFinder.timeOffset
}


CrvRecoPackage :
{
Expand Down
34 changes: 34 additions & 0 deletions CRVReco/inc/TrkCrvGeom.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// CRV geometry used by the track-CRV association and its truth: the sector planes (KinKalGeom CRV rectangles, per
// CRSScintillatorShield index) and the dominant shield of a coincidence cluster.
// Sector frame: u along the bars (uDirection), v across the bars (vDirection), w the sector normal, all in the
// detector frame; TrkCrvMatch residuals and covariances are expressed in (u, v).
//
#ifndef CRVReco_TrkCrvGeom_hh
#define CRVReco_TrkCrvGeom_hh
#include "Offline/DataProducts/inc/SurfaceId.hh"
#include "KinKal/General/Vectors.hh"
#include "KinKal/Geometry/Rectangle.hh"
#include <memory>
#include <string>
#include <vector>

namespace mu2e {
class CosmicRayShield;
class CrvCoincidenceCluster;

struct TrkCrvSector {
bool valid = false; // the shield has a KinKalGeom CRV rectangle
std::string name; // CRSScintillatorShield name
SurfaceId sid; // its SurfaceId (unknown if the name has none)
std::shared_ptr<KinKal::Rectangle> rect; // mid-plane rectangle, detector frame
KinKal::VEC3 center, u, v, w; // rectangle centre and axes
double uHalf = 0, vHalf = 0; // half lengths along u and v [mm]
};

// one entry per CRSScintillatorShield, in shield order (the sector_ index of TrkCrvMatch); call once per run
std::vector<TrkCrvSector> trkCrvSectors();
// the shield with the largest PE sum among the cluster's pulses, -1 if none; multiSector set if pulses span shields
int trkCrvDominantShield(CrvCoincidenceCluster const& cluster, CosmicRayShield const& crs, bool* multiSector = nullptr);
}
#endif
108 changes: 108 additions & 0 deletions CRVReco/inc/TrkCrvMatchTool.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// Track-CRV matching shared by the TrkCrvMatcher producer and any fit that uses CRV clusters as constraints, so that
// both apply one definition of a match. See TrkCrvMatch.hh for the output.
//
#ifndef CRVReco_TrkCrvMatchTool_hh
#define CRVReco_TrkCrvMatchTool_hh
#include "Offline/RecoDataProducts/inc/CrvCoincidenceCluster.hh"
#include "Offline/RecoDataProducts/inc/CrvRecoPulse.hh"
#include "Offline/RecoDataProducts/inc/KalSeed.hh"
#include "Offline/RecoDataProducts/inc/TrkCrvMatch.hh"
#include "Offline/CRVReco/inc/TrkCrvGeom.hh"
#include "Offline/DataProducts/inc/SurfaceId.hh"
#include "KinKal/General/Vectors.hh"
#include "canvas/Persistency/Common/Ptr.h"
#include "fhiclcpp/types/Atom.h"
#include "fhiclcpp/types/Sequence.h"
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

namespace mu2e {
class CosmicRayShield;
class DetectorSystem;

class TrkCrvMatchTool {
public:
// physics settings have no C++ default: CRVReco/fcl/prolog_v12.fcl (TrkCrvMatcher) sets them with their sources
struct Config {
using Name = fhicl::Name;
using Comment = fhicl::Comment;
fhicl::Sequence<std::string> refSurfaces{Name("ReferenceSurfaces"), Comment("surfaces whose KalSeed intersection gives the track reference time; KalSeed t0 if none present")};
fhicl::Atom<bool> earliestReference{Name("EarliestReference"), Comment("true: the earliest intersection on any of ReferenceSurfaces (the tracker entry); false: the first surface in list order present, nearest in time to t0")};
fhicl::Sequence<double> dtRefWindow{Name("DtRefWindow"), Comment("storage window on t_track(ref) - t_cluster [ns]")};
fhicl::Atom<double> dtRefCenter{Name("DtRefCenter"), Comment("dtRef value the time ranking is centred on [ns]")};
fhicl::Sequence<double> dtXingWindow{Name("DtXingWindow"), Comment("storage window on t_cluster - t_track at the cluster plane [ns]")};
fhicl::Atom<double> maxDv{Name("MaxDv"), Comment("storage window on |across-bar residual| [mm]")};
fhicl::Atom<double> maxDu{Name("MaxDu"), Comment("storage window on |along-bar residual| for two-ended clusters [mm]")};
fhicl::Atom<bool> doGeometric{Name("DoGeometric"), Comment("form geometric candidates from CRV KalIntersections")};
fhicl::Atom<bool> requireSameSector{Name("RequireSameSector"), Comment("project only from a crossing of the cluster's own sector (A/B tests)"), false};
fhicl::Sequence<double> sigmaV{Name("SigmaV"), Comment("CRV across-bar resolution [mm], indexed by the cluster's sector type (1 first; the last entry serves higher types); sector type numbering depends on the geometry")};
fhicl::Sequence<double> sigmaU{Name("SigmaU"), Comment("CRV along-bar resolution of two-ended clusters [mm], by sector type")};
fhicl::Sequence<double> sigmaT{Name("SigmaT"), Comment("CRV time resolution of two-ended clusters [ns], by sector type")};
fhicl::Atom<double> sigmaTSingleEnded{Name("SigmaTSingleEnded"), Comment("CRV time resolution of single-ended clusters [ns]")};
fhicl::Atom<double> trackErrorScale{Name("TrackErrorScale"), Comment("scale k on the propagated track position covariance: k^2 C + TrackPosErrQuad^2 I")};
fhicl::Atom<double> trackPosErrQuad{Name("TrackPosErrQuad"), Comment("added in quadrature to the scaled track position errors [mm]")};
fhicl::Atom<double> minTrackTimeErr{Name("MinTrackTimeErr"), Comment("floor on the (unscaled) track time error [ns]")};
fhicl::Atom<double> timeOffset{Name("TimeOffset"), Comment("added to cluster and pulse times [ns]; test knob, data offsets belong in conditions"), 0.0};
fhicl::Atom<bool> keepAll{Name("KeepAllClusters"), Comment("ignore every storage window (commissioning)"), false};
fhicl::Atom<bool> layerProbe{Name("LayerProbe"), Comment("probe each layer of every crossed sector for pulses near the predicted bar; needs the reco pulses")};
fhicl::Atom<bool> probeAllPulses{Name("ProbeAllPulses"), Comment("the pulse collection holds every reco pulse (true) or only the clustered ones, e.g. SelectReco (false)")};
fhicl::Atom<int> probeBarWindow{Name("ProbeBarWindow"), Comment("layer probe: bars searched on each side of the predicted bar")};
fhicl::Sequence<double> probeDtWindow{Name("ProbeDtWindow"), Comment("layer probe: window on pulse time - predicted pulse time [ns]")};
fhicl::Atom<double> fiberSignalSpeed{Name("FiberSignalSpeed"), Comment("layer probe: signal speed in the fibres [mm/ns]")};
fhicl::Atom<double> pulseDelay{Name("ProbePulseDelay"), Comment("layer probe: electronics and scintillation delay subtracted from pulse times [ns]")};
};
explicit TrkCrvMatchTool(Config const& conf);

void beginRun(); // caches the CRV geometry
// per-event inputs; pulses may be null (the layer probe then records predicted bars only)
void setEvent(std::vector<art::Ptr<CrvCoincidenceCluster>> const& clusters, CrvRecoPulseCollection const* pulses);
// one track; usedInFit lists the clusters its fit used as constraints
TrkCrvMatch match(art::Ptr<KalSeed> const& seed, std::vector<art::Ptr<CrvCoincidenceCluster>> const& usedInFit = {}) const;
// cross-track pass over one collection: nShare, bestForCluster, exclusive
void resolve(TrkCrvMatchCollection& matches) const;
bool probeActive() const { return layerProbe_; }
std::vector<TrkCrvSector> const& sectors() const { return sectors_; } // per shield index, after beginRun

private:
struct ClusterInfo { // per-event cluster quantities
KinKal::VEC3 pos; // detector frame
double time = 0;
int shield = -1;
int sectorType = 0;
bool multiSector = false;
bool singleEnded = false;
};
double resolution(std::vector<double> const& tab, int sectorType) const;
TrkCrvPlaneCov errorModel(ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2>> const& cov) const; // k^2 C + a^2 I
void probe(TrkCrvXing& x, double speed) const;
static bool sameCluster(art::Ptr<CrvCoincidenceCluster> const& a, art::Ptr<CrvCoincidenceCluster> const& b);

std::vector<SurfaceId> refSurfaces_;
bool earliestRef_;
double dtRefLo_, dtRefHi_, dtRefCenter_, dtXingLo_, dtXingHi_, maxDv_, maxDu_;
bool doGeometric_, requireSameSector_;
std::vector<double> sigmaV_, sigmaU_, sigmaT_;
double sigmaTSingle_, trkErrScale_, posErrQuad_, minTimeErr_, timeOffset_;
bool keepAll_, layerProbe_, probeAllPulses_;
int probeBarWindow_;
double probeDtLo_, probeDtHi_, fiberSpeed_, pulseDelay_;

CosmicRayShield const* crs_ = nullptr;
DetectorSystem const* det_ = nullptr;
std::map<SurfaceId,int> sidToShield_;
std::vector<TrkCrvSector> sectors_; // indexed by shield

std::vector<art::Ptr<CrvCoincidenceCluster>> clusters_;
std::vector<ClusterInfo> cinfo_;
CrvRecoPulseCollection const* pulses_ = nullptr;
std::unordered_map<int, std::vector<size_t>> pulsesByBar_; // bar index -> pulses
std::set<std::tuple<int,int,double>> clusteredPulses_; // (bar, SiPM, time) of the pulses in clusters
};
}
#endif
44 changes: 44 additions & 0 deletions CRVReco/inc/TrkCrvPlaneError.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
Comment thread
RobMina marked this conversation as resolved.
Resolved
// Temporary stand-in for a KinKal projection of a track state's spatial covariance onto a plane; replace with the
// KinKal implementation when it exists (both modules call only trkCrvPlaneCovariance).
//
#ifndef CRVReco_TrkCrvPlaneError_hh
#define CRVReco_TrkCrvPlaneError_hh
#include "KinKal/General/Vectors.hh"
#include "Math/SMatrix.h"
#include <cmath>

namespace mu2e {
using TrkCrvCov2 = ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2>>;

// Covariance of the track position on the plane with normal w, in the in-plane axes (a, b), after a straight step of
// signed path length s from a state (position, momentum p) with 6x6 covariance C (position first). With d = p/|p|
// and P = I - d w^T/(d.w) (move along d onto the plane), the gradient of e.(position on the plane) is P^T e for the
// position block and (s/|p|)(I - d d^T) P^T e for the momentum block. s = 0 gives the covariance of the state itself,
// measured in the plane. Returns a zero matrix for a track parallel to the plane.
template<class COV> TrkCrvCov2 trkCrvPlaneCovariance(COV const& C, KinKal::VEC3 const& p, KinKal::VEC3 const& a,
KinKal::VEC3 const& b, KinKal::VEC3 const& w, double s) {
TrkCrvCov2 out;
double pm = std::sqrt(p.Mag2());
if(pm <= 0) return out;
KinKal::VEC3 d = p / pm;
double dw = d.Dot(w);
if(std::fabs(dw) < 1e-3) return out;
double g[2][6];
KinKal::VEC3 const* axes[2] = {&a, &b};
for(int k = 0; k < 2; ++k) {
KinKal::VEC3 gp = *axes[k] - w * (d.Dot(*axes[k]) / dw);
KinKal::VEC3 gm = (s / pm) * (gp - d * d.Dot(gp));
double gk[6] = {gp.X(), gp.Y(), gp.Z(), gm.X(), gm.Y(), gm.Z()};
for(int i = 0; i < 6; ++i) g[k][i] = gk[i];
}
for(int k = 0; k < 2; ++k)
for(int l = k; l < 2; ++l) {
double v = 0;
for(int i = 0; i < 6; ++i) for(int j = 0; j < 6; ++j) v += g[k][i] * C(i, j) * g[l][j];
out(k, l) = v;
}
return out;
}
}
#endif
12 changes: 12 additions & 0 deletions CRVReco/src/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ mainlib = helper.make_mainlib ( [ 'CLHEP',
'mu2e_RecoDataProducts',
'mu2e_DataProducts',
'mu2e_GeomPrimitives',
'mu2e_GeometryService',
'mu2e_CosmicRayShieldGeom',
'mu2e_KinKalGeom',
'KinKal_Geometry',
'KinKal_General',
'fhiclcpp',
'fhiclcpp_types',
'MF_MessageLogger',
'art_Framework_Principal',
'art_Framework_Services_Registry',
'art_Persistency_Provenance',
'art_Persistency_Common',
'art_Utilities',
Expand All @@ -36,6 +45,9 @@ helper.make_plugins( [ mainlib,
'mu2e_GlobalConstantsService',
'mu2e_RecoDataProducts',
'mu2e_CosmicRayShieldGeom',
'mu2e_KinKalGeom',
'KinKal_Geometry',
'KinKal_General',
'mu2e_ConfigTools',
'mu2e_DataProducts',
'mu2e_DbTables',
Expand Down
48 changes: 48 additions & 0 deletions CRVReco/src/TrkCrvGeom.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "Offline/CRVReco/inc/TrkCrvGeom.hh"
#include "Offline/CosmicRayShieldGeom/inc/CosmicRayShield.hh"
#include "Offline/GeometryService/inc/GeomHandle.hh"
#include "Offline/KinKalGeom/inc/KinKalGeom.hh"
#include "Offline/RecoDataProducts/inc/CrvCoincidenceCluster.hh"
#include "messagefacility/MessageLogger/MessageLogger.h"
#include <map>

namespace mu2e {

std::vector<TrkCrvSector> trkCrvSectors() {
GeomHandle<CosmicRayShield> crs;
GeomHandle<KinKalGeom> kkg;
auto const& shields = crs->getCRSScintillatorShields();
std::vector<TrkCrvSector> sectors(shields.size());
std::map<std::string,int> nameToShield;
for(size_t i = 0; i < shields.size(); ++i) {
sectors[i].name = shields[i].getName();
nameToShield[sectors[i].name] = static_cast<int>(i);
}
for(auto const& sec : kkg->CRV()->sectors()) {
auto it = nameToShield.find(sec.sname_);
if(it == nameToShield.end()) continue;
auto& g = sectors[it->second];
g.valid = true;
g.rect = sec.sector_;
g.center = sec.sector_->center();
g.u = sec.sector_->uDirection();
g.v = sec.sector_->vDirection();
g.w = sec.sector_->normal();
g.uHalf = sec.sector_->uHalfLength();
g.vHalf = sec.sector_->vHalfLength();
try { g.sid = SurfaceId(sec.sname_); }
catch(std::exception const&) { mf::LogWarning("TrkCrvGeom") << "no SurfaceId for CRV sector " << sec.sname_; }
}
return sectors;
}

int trkCrvDominantShield(CrvCoincidenceCluster const& cluster, CosmicRayShield const& crs, bool* multiSector) {
std::map<int,double> pes;
for(auto const& rp : cluster.GetCrvRecoPulses()) pes[crs.getBar(rp->GetScintillatorBarIndex()).id().getShieldNumber()] += rp->GetPEs();
int shield = -1;
double best = -1;
for(auto const& [s, pe] : pes) if(pe > best) { best = pe; shield = s; }
if(multiSector) *multiSector = pes.size() > 1;
return shield;
}
}
Loading