diff --git a/app/BadChannelsTest.cpp b/app/BadChannelsTest.cpp new file mode 100644 index 00000000..8c48412e --- /dev/null +++ b/app/BadChannelsTest.cpp @@ -0,0 +1,74 @@ +// Unit test for the bad channel list (TMS_BadChannels) and the decoder's use of it. Writes small +// lists to the directory given as the first argument (default: the current directory). Exits +// non-zero if any check fails. Needs TMS_DIR set, as the managers do. +#include +#include +#include +#include +#include + +#include "TMS_BadChannels.h" +#include "TMS_Decoder.h" + +namespace { + +int n_failed = 0; + +void Check(bool ok, const char* what) { + std::cout << (ok ? "PASS " : "FAIL ") << what << "\n"; + if (!ok) n_failed++; +} + +std::string Write(const std::string& dir, const std::string& name, const std::string& text) { + const std::string path = dir + "/" + name; + std::ofstream(path) << text; + return path; +} + +template bool Throws(F f) { + try { f(); } catch (const std::exception&) { return true; } + return false; +} + +} // namespace + +int main(int argc, char** argv) { + // TMS_BadChannels resolves a relative file name under $TMS_DIR, so hand it absolute paths + std::string dir = argc > 1 ? argv[1] : "."; + if (dir.empty() || dir[0] != '/') { + char* resolved = realpath(dir.empty() ? "." : dir.c_str(), nullptr); + if (resolved == nullptr) { std::cerr << "Cannot resolve directory " << dir << "\n"; return 2; } + dir = resolved; + free(resolved); + } + + Check(TMS_BadChannels("").Size() == 0, "empty file name: no bad channels"); + Check(TMS_BadChannels(Write(dir, "bc_empty.toml", "# nothing\n")).Size() == 0, "file without entries: no bad channels"); + + const TMS_BadChannels list(Write(dir, "bc_two.toml", + "[[BadChannel]]\nPlane = 12\nBar = 40\nView = 3\nStatus = \"dead\"\nReason = \"broken fiber\"\n" + "[[BadChannel]]\nPlane = 12\nBar = 41\nView = 3\nStatus = \"missing\"\n")); + Check(list.Size() == 2, "two entries read"); + Check(list.GetStatus({12, 40, 3}) == TMS_BadChannels::Status::Dead, "dead entry"); + Check(list.GetStatus({12, 41, 3}) == TMS_BadChannels::Status::Missing, "missing entry"); + Check(list.GetChannels().at({12, 40, 3}).Reason == "broken fiber", "reason kept"); + Check(list.IsBad({12, 40, 3}) && list.IsBad({12, 41, 3}), "dead and missing are bad"); + Check(!list.IsBad({12, 40, 1}), "same plane and bar, other view: good"); + Check(!list.IsBad({13, 40, 3}), "same bar number, other plane: good"); + + Check(Throws([&] { TMS_BadChannels(Write(dir, "bc_state.toml", "[[BadChannel]]\nPlane=1\nBar=1\nView=1\nStatus=\"flaky\"\n")); }), + "unknown status is an error"); + Check(Throws([&] { TMS_BadChannels(Write(dir, "bc_dup.toml", + "[[BadChannel]]\nPlane=1\nBar=1\nView=1\nStatus=\"dead\"\n[[BadChannel]]\nPlane=1\nBar=1\nView=1\nStatus=\"missing\"\n")); }), + "channel listed twice is an error"); + Check(Throws([&] { TMS_BadChannels(Write(dir, "bc_key.toml", "[[BadChannel]]\nPlane=1\nBar=1\nStatus=\"dead\"\n")); }), + "missing key is an error"); + Check(Throws([&] { TMS_BadChannels(dir + "/does_not_exist.toml"); }), "missing file is an error"); + + // The shipped list is empty, so the decoder keeps every readout (the decoder's own use of + // the list is exercised by the real-spill comparison, as it needs the geometry for TMS_Bar). + Check(TMS_BadChannels::GetInstance().Size() == 0, "list named in the main TOML is empty by default"); + + std::cout << (n_failed ? "FAILED" : "ALL PASSED") << "\n"; + return n_failed ? 1 : 0; +} diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index cf249b26..c5c53fc7 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -13,6 +13,9 @@ add_executable(RealDataSmokeTest RealDataSmokeTest.cpp) add_executable(ArtificialResegmentationTest ArtificialResegmentationTest.cpp) add_executable(PassageUtilsTest PassageUtilsTest.cpp) add_executable(ChannelReadoutTest ChannelReadoutTest.cpp) +add_executable(BadChannelsTest BadChannelsTest.cpp) +add_executable(DumpChannels DumpChannels.cpp) +add_executable(DetSimStageScan DetSimStageScan.cpp) target_link_libraries(BField_tester LINK_PUBLIC TMS_Prod ROOT::EG) target_link_libraries(BetheBloch_Example LINK_PUBLIC TMS_Prod) @@ -26,6 +29,9 @@ target_link_libraries(RealDataSmokeTest LINK_PUBLIC TMS_Prod) target_link_libraries(ArtificialResegmentationTest LINK_PUBLIC TMS_Prod) target_link_libraries(PassageUtilsTest LINK_PUBLIC TMS_Prod) target_link_libraries(ChannelReadoutTest LINK_PUBLIC TMS_Prod) +target_link_libraries(BadChannelsTest LINK_PUBLIC TMS_Prod) +target_link_libraries(DumpChannels LINK_PUBLIC TMS_Prod) +target_link_libraries(DetSimStageScan LINK_PUBLIC TMS_Prod) install(TARGETS BField_tester RUNTIME DESTINATION bin) install(TARGETS BetheBloch_Example RUNTIME DESTINATION bin) @@ -39,3 +45,6 @@ install(TARGETS RealDataSmokeTest RUNTIME DESTINATION bin) install(TARGETS ArtificialResegmentationTest RUNTIME DESTINATION bin) install(TARGETS PassageUtilsTest RUNTIME DESTINATION bin) install(TARGETS ChannelReadoutTest RUNTIME DESTINATION bin) +install(TARGETS BadChannelsTest RUNTIME DESTINATION bin) +install(TARGETS DumpChannels RUNTIME DESTINATION bin) +install(TARGETS DetSimStageScan RUNTIME DESTINATION bin) diff --git a/app/DetSimStageScan.cpp b/app/DetSimStageScan.cpp new file mode 100644 index 00000000..a1f6a636 --- /dev/null +++ b/app/DetSimStageScan.cpp @@ -0,0 +1,407 @@ +// Single-stage scans of the detector simulation, for the stage-by-stage validation plots +// (scripts/Validation/DetSim/stages/). Like ArtificialResegmentationTest, synthetic muon crossings +// of one known bar are pushed through the real TMS_Event::FinalizeEvent(); the readout +// configuration (TMS_READOUT_TOML) selects the pipeline, so run once per configuration. +// +// Modes: +// path Perpendicular crossings of the reference bar at minimum-ionizing dE/dx, scanning the +// path length through the bar, up to its full 16 mm thickness (and, for a 9 mm crossing, the +// deposited energy). One row +// per throw: the light before and after the threshold, the hit time, ToT and photon count. +// position Full-thickness (16 mm) MIP crossings of the reference bar at a series of distances from its +// readout end, along the bar. One row per throw: the light and hit time, and the distance +// from the readout as the simulation itself computes it (for the PE-vs-position check +// against the PDR light-yield expectation, and the fiber propagation delay). +// pileup Several particles (tracks 1..N) crossing the same bar at staggered times and with +// different energies, to check how one channel combines them: readouts, summed light and +// energy, hit time, and the light provenance (which particle made most of the light, and +// which made the first photon). +// pair Two crossings of the reference bar a time dt apart, scanning dt, plus the two halves of +// a split X-bar crossed at once. One row per throw: the number of readouts. +// +// Usage: DetSimStageScan [nthrows] +// [distance_mm] (path mode only: distance of the crossing from the readout end; +// default is the reference position, 2.7 m) + +#include +#include +#include +#include +#include +#include +#include + +#include "TFile.h" +#include "TGeoManager.h" +#include "TLorentzVector.h" + +#include "EDepSim/TG4Event.h" +#include "EDepSim/TG4PrimaryVertex.h" +#include "EDepSim/TG4HitSegment.h" + +#include "TMS_Bar.h" +#include "TMS_Event.h" +#include "TMS_Geom.h" +#include "TMS_Manager.h" + +namespace { + +constexpr int kTrackId = 1; +constexpr int kPDGCodeMuMinus = 13; +constexpr double kSpeedOfLight_mm_per_ns = 299.792458; +// Minimum-ionizing dE/dx of the reference crossing in ArtificialResegmentationTest +constexpr double kMipdEdx_MeV_per_mm = 1.8207009 / 10.164372; + +struct Crossing { + double x, y, z; // mm, midpoint + double t; // ns + double path_mm; // along z + double edep_MeV; + int track_id = 1; +}; + +// Same reference bar as ArtificialResegmentationTest (plane 28, bar 28, view 1), but centered on the +// bar in z. The scintillator is 16 mm thick (z = 12924.5 to 12940.5 mm, center 12932.5, from the +// geometry), so a 16 mm path is a full perpendicular crossing; the 9 mm default path, kept for the +// scans that compare against ArtificialResegmentationTest, is 56% of it. +constexpr double kBarThickness_mm = 16.0; +const Crossing kRefBar = {-2434.968, -2302.8132, 12932.5, 0.0, 9.0, 9.0 * kMipdEdx_MeV_per_mm}; +// The two halves of split X-bar row y=-744.23 in plane 81 (bar 128 at x<0, bar 129 at x>0) +const Crossing kXBarNeg = {-1000.0, -744.23, 18502.5, 0.0, 9.0, 9.0 * kMipdEdx_MeV_per_mm}; +const Crossing kXBarPos = {1000.0, -744.23, 18502.5, 0.0, 9.0, 9.0 * kMipdEdx_MeV_per_mm}; + +TG4Event BuildEvent(const std::vector& crossings, int event_id) { + TG4Event event; + event.RunId = 1; + event.EventId = event_id; + TG4PrimaryVertex vtx; + vtx.Position = TLorentzVector(crossings[0].x, crossings[0].y, crossings[0].z - 100, 0); + vtx.GeneratorName = "DetSimStageScan"; + vtx.Reaction = "synthetic"; + // One muon per distinct track id, each with a trajectory from before its first crossing to + // after its last + std::map> tracks; + for (const auto& c : crossings) { + auto it = tracks.find(c.track_id); + if (it == tracks.end()) tracks[c.track_id] = {&c, &c}; + else it->second.second = &c; + } + for (const auto& kv : tracks) { + TG4PrimaryParticle primary; + primary.TrackId = kv.first; + primary.PDGCode = kPDGCodeMuMinus; + primary.Momentum = TLorentzVector(0, 0, 1, 1); + vtx.Particles.push_back(primary); + } + event.Primaries.push_back(vtx); + for (const auto& kv : tracks) { + TG4Trajectory traj; + traj.TrackId = kv.first; + traj.ParentId = -1; + traj.PDGCode = kPDGCodeMuMinus; + traj.Name = "mu-"; + TG4TrajectoryPoint p0, p1; + p0.Position = vtx.Position; + p1.Position = TLorentzVector(kv.second.second->x, kv.second.second->y, kv.second.second->z + 100, kv.second.second->t); + traj.Points.push_back(p0); + traj.Points.push_back(p1); + event.Trajectories.push_back(traj); + } + TG4HitSegmentContainer segs; + for (const auto& c : crossings) { + TG4HitSegment seg; + seg.PrimaryId = c.track_id; + seg.Contrib.push_back(c.track_id); + seg.EnergyDeposit = c.edep_MeV; + seg.SecondaryDeposit = 0; + seg.Start = TLorentzVector(c.x, c.y, c.z - c.path_mm / 2, c.t); + seg.Stop = TLorentzVector(c.x, c.y, c.z + c.path_mm / 2, c.t + c.path_mm / kSpeedOfLight_mm_per_ns); + seg.TrackLength = c.path_mm; + segs.push_back(seg); + } + event.SegmentDetectors["volTMS"] = segs; + return event; +} + +// The reference bar's type, length and readout end, found from a probe crossing, and the +// coordinate along the bar that puts a crossing at a given distance from the readout end (the +// same readout-end definition as TMS_DetectorSimulation) +struct BarAxis { + bool is_xbar = false; + double length = 0, center = 0; + bool Probe(); + void MoveTo(Crossing& c, double distance_mm) const; +}; + +bool BarAxis::Probe() { + TG4Event event = BuildEvent({kRefBar}, 0); + TMS_Event tms_event(event); + tms_event.FinalizeEvent(); + auto hits = tms_event.GetHits(-1, /*include_ped_sup=*/true); + if (hits.empty()) { std::cerr << "probe crossing made no hit\n"; return false; } + const TMS_Bar& bar = hits.front().GetBar(); + is_xbar = bar.GetBarType() == TMS_Bar::kXBar; + length = bar.GetBarLength(); + center = bar.GetAxisReadoutCenter(); + return true; +} + +void BarAxis::MoveTo(Crossing& c, double d) const { + const TMS_Geom& geo = TMS_Geom::GetInstance(); + if (is_xbar) c.x = (kRefBar.x < 0) ? geo.XBarNegReadoutLocation(center, length) + d + : geo.XBarPosReadoutLocation(center, length) - d; + else c.y = geo.YBarReadoutLocation(center, length) - d; +} + +// Return false when the scan could not run (e.g. the reference bar makes no hit, as when it is a listed +// bad channel): the CSV would hold only its header, so the program must not report success +bool RunPathScan(std::ofstream& out, int nthrows, double distance_mm) { + out << "path_mm,edep_MeV,throw_index,n_hits_total,n_hits_surviving,pe_all,pe_surviving,reco_energy," + "hit_time,true_time,tot,n_photons\n"; + // (path, energy scale): the path-length scan at MIP dE/dx, then brighter 9 mm crossings for ToT + std::vector> cells; + for (double p : {0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 7.5, 9.0, 12.0, kBarThickness_mm}) cells.push_back({p, 1.0}); + for (double s : {2.0, 3.0, 4.0, 6.0, 8.0}) cells.push_back({9.0, s}); + BarAxis axis; + if (!axis.Probe()) return false; // always: a reference bar that makes no hit would give an all-zero scan + int event_id = 0; + for (const auto& cell : cells) { + Crossing c = kRefBar; + if (distance_mm > 0) axis.MoveTo(c, distance_mm); + c.path_mm = cell.first; + c.edep_MeV = cell.first * kMipdEdx_MeV_per_mm * cell.second; + for (int t = 0; t < nthrows; ++t) { + TG4Event event = BuildEvent({c}, event_id++); + TMS_Event tms_event(event); + tms_event.FinalizeEvent(); + int n_total = 0, n_surviving = 0; + double pe_all = 0, pe_surviving = 0, reco_e = 0; + double hit_time = 0, true_time = -999, tot = -999; + int n_photons = -999; + double best_pe = -1; + for (const auto& hit : tms_event.GetHits(-1, /*include_ped_sup=*/true)) { + if (hit.GetBarNumber() < 0) continue; + n_total++; + pe_all += hit.GetPE(); + const TMS_TrueHit* true_hit = tms_event.GetTrueHit(hit.GetHitId()); + if (true_hit) { + true_time = true_hit->GetT(); + // -999 with the legacy pipeline, which draws no individual photons + if (true_hit->GetNPhotons() >= 0) n_photons = std::max(n_photons, 0) + true_hit->GetNPhotons(); + } + if (hit.GetPedSup()) continue; + n_surviving++; + pe_surviving += hit.GetPE(); + reco_e += hit.GetE(); + if (hit.GetPE() > best_pe) { + best_pe = hit.GetPE(); + hit_time = hit.GetT(); + tot = hit.GetToT(); + } + } + out << c.path_mm << "," << c.edep_MeV << "," << t << "," << n_total << "," << n_surviving << "," << pe_all + << "," << pe_surviving << "," << reco_e << "," << hit_time << "," << true_time << "," << tot << "," + << n_photons << "\n"; + } + } + std::cout << "path scan: " << cells.size() << " cells x " << nthrows << " throws\n"; + return true; +} + +bool RunPositionScan(std::ofstream& out, int nthrows) { + out << "distance_mm,x,y,bar_type,bar_number,bar_length_mm,throw_index,n_hits_surviving,pe_all,pe_surviving," + "hit_time,true_time\n"; + BarAxis axis; + if (!axis.Probe()) return false; + const bool is_xbar = axis.is_xbar; + const double length = axis.length; + std::vector distances; + for (double f : {0.02, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.98}) distances.push_back(f * length); + int event_id = 0; + for (double d : distances) { + Crossing c = kRefBar; + c.path_mm = kBarThickness_mm; + c.edep_MeV = kBarThickness_mm * kMipdEdx_MeV_per_mm; + axis.MoveTo(c, d); + for (int t = 0; t < nthrows; ++t) { + TG4Event event = BuildEvent({c}, event_id++); + TMS_Event tms_event(event); + tms_event.FinalizeEvent(); + int n_surviving = 0, bar_number = -1; + double pe_all = 0, pe_surviving = 0, hit_time = 0, true_time = -999; + for (const auto& hit : tms_event.GetHits(-1, /*include_ped_sup=*/true)) { + if (hit.GetBarNumber() < 0) continue; + bar_number = hit.GetBarNumber(); + pe_all += hit.GetPE(); + const TMS_TrueHit* true_hit = tms_event.GetTrueHit(hit.GetHitId()); + if (true_hit) true_time = true_hit->GetT(); + if (hit.GetPedSup()) continue; + n_surviving++; + pe_surviving += hit.GetPE(); + hit_time = hit.GetT(); + } + out << d << "," << c.x << "," << c.y << "," << (is_xbar ? "X" : "Y") << "," << bar_number << "," << length << "," + << t << "," << n_surviving << "," << pe_all << "," << pe_surviving << "," << hit_time << "," << true_time << "\n"; + } + } + std::cout << "position scan: " << distances.size() << " positions x " << nthrows << " throws, bar type " + << (is_xbar ? "X" : "Y") << ", length " << length << " mm\n"; + return true; +} + +void RunPileupScan(std::ofstream& out, int nthrows) { + out << "pattern,n_particles,dt,throw_index,n_readouts,pe_surviving,reco_energy,hit_time,first_true_time," + "n_photons,top_light_id,top_light_share,first_photon_id,n_contrib,min_contrib_id,max_energy_id\n"; + // Energy of each particle's crossing, relative to a full MIP crossing, for each pattern + // For n particles the first n entries of `scale` are used, in reverse order for bright_last, so + // bright_first and bright_last of the same n have the same total energy and differ only in the + // order of arrival. + struct Pattern { const char* name; std::vector scale; bool reverse; }; + const std::vector patterns = { + {"equal", {1, 1, 1, 1}, false}, + {"bright_first", {1, 0.5, 0.25, 0.125}, false}, + {"bright_last", {1, 0.5, 0.25, 0.125}, true}}; + const std::vector delays = {0, 5, 20, 50, 100, 150, 250}; + int event_id = 0; + auto run = [&](const Pattern& pat, int n, double dt) { + std::vector crossings; + std::vector scale(pat.scale.begin(), pat.scale.begin() + n); + if (pat.reverse) std::reverse(scale.begin(), scale.end()); + for (int i = 0; i < n; ++i) { + Crossing c = kRefBar; + c.path_mm = kBarThickness_mm; + c.edep_MeV = kBarThickness_mm * kMipdEdx_MeV_per_mm * scale[i]; + c.t = i * dt; + c.track_id = i + 1; + crossings.push_back(c); + } + for (int t = 0; t < nthrows; ++t) { + TG4Event event = BuildEvent(crossings, event_id++); + TMS_Event tms_event(event); + tms_event.FinalizeEvent(); + int n_readouts = 0, n_photons = 0, energy_id = -999, top_id = -999, first_id = -999, n_contrib = 0, min_contrib_id = -999; + double pe = 0, reco_e = 0, hit_time = 1e9, first_true = 1e9, top_share = -999, best_pe = -1; + for (const auto& hit : tms_event.GetHits(-1, /*include_ped_sup=*/false)) { + if (hit.GetBarNumber() < 0) continue; + n_readouts++; + pe += hit.GetPE(); + reco_e += hit.GetE(); + const TMS_TrueHit* true_hit = tms_event.GetTrueHit(hit.GetHitId()); + if (true_hit && true_hit->GetNPhotons() > 0) n_photons += true_hit->GetNPhotons(); + // Time and provenance of the brightest readout + if (hit.GetPE() > best_pe) { + best_pe = hit.GetPE(); + min_contrib_id = -999; // the minimum belongs to the selected readout only + energy_id = -999; // as does the energy-dominant particle + if (true_hit) { + energy_id = true_hit->GetPrimaryIds(true_hit->IndexOfHighestEnergyContributor()); + top_id = true_hit->GetPrimaryIdByLight(); + top_share = true_hit->GetLightShare(); + first_id = true_hit->GetFirstPhotonPrimaryId(); + n_contrib = static_cast(true_hit->GetNLightContributions()); + for (size_t c = 0; c < true_hit->GetNLightContributions(); ++c) { + const int id = true_hit->GetLightContributionPrimaryId(c); + if (min_contrib_id == -999 || id < min_contrib_id) min_contrib_id = id; + } + } + } + if (hit.GetT() < hit_time) hit_time = hit.GetT(); + if (true_hit && true_hit->GetT() < first_true) first_true = true_hit->GetT(); + } + if (n_readouts == 0) { hit_time = -999; first_true = -999; } + out << pat.name << "," << n << "," << dt << "," << t << "," << n_readouts << "," << pe << "," << reco_e << "," + << hit_time << "," << first_true << "," << n_photons << "," << top_id << "," << top_share << "," << first_id + << "," << n_contrib << "," << min_contrib_id << "," << energy_id << "\n"; + } + }; + int cells = 0; + run(patterns[0], 1, 0); cells++; + for (const auto& pat : patterns) + for (int n : {2, 3, 4}) + for (double dt : delays) { run(pat, n, dt); cells++; } + std::cout << "pileup scan: " << cells << " cells x " << nthrows << " throws\n"; +} + +void RunPairScan(std::ofstream& out, int nthrows) { + out << "case,dt,throw_index,n_readouts,n_channels,pe_surviving\n"; + std::vector delays; + for (double dt = 0; dt <= 1000; dt += 10) delays.push_back(dt); + int event_id = 0; + auto run = [&](const std::string& label, double dt, const std::vector& crossings) { + for (int t = 0; t < nthrows; ++t) { + TG4Event event = BuildEvent(crossings, event_id++); + TMS_Event tms_event(event); + tms_event.FinalizeEvent(); + int n = 0; + double pe = 0; + std::map channels; + for (const auto& hit : tms_event.GetHits(-1, /*include_ped_sup=*/false)) { + n++; + pe += hit.GetPE(); + channels[hit.GetChannelId()]++; + } + out << label << "," << dt << "," << t << "," << n << "," << channels.size() << "," << pe << "\n"; + } + }; + for (double dt : delays) { + Crossing second = kRefBar; + second.t = dt; + run("same_bar", dt, {kRefBar, second}); + } + run("xbar_halves", 0, {kXBarNeg, kXBarPos}); + run("single", 0, {kRefBar}); + std::cout << "pair scan: " << delays.size() + 2 << " cells x " << nthrows << " throws\n"; +} + +} // namespace + +int main(int argc, char** argv) { + if (argc < 4) { + std::cerr << "Usage: " << argv[0] << " [nthrows] [distance_mm]\n"; + return 1; + } + const std::string mode = argv[1]; + const std::string geom_file = argv[2]; + const std::string out_csv = argv[3]; + const int nthrows = (argc >= 5) ? std::atoi(argv[4]) : (mode == "pair" ? 200 : mode == "position" ? 1000 : mode == "pileup" ? 200 : 2000); + if (nthrows < 1) { + std::cerr << "nthrows must be at least 1\n"; + return 1; + } + const double distance_mm = (argc >= 6) ? std::atof(argv[5]) : -1; + if (mode != "path" && mode != "position" && mode != "pileup" && mode != "pair") { + std::cerr << "Unknown mode " << mode << "\n"; + return 1; + } + + TMS_Manager::GetInstance().SetFileName(geom_file); + TFile* input = new TFile(geom_file.c_str(), "open"); + TGeoManager* geom = dynamic_cast(input->Get("EDepSimGeometry")); + if (!geom) { + std::cerr << "Could not load EDepSimGeometry from " << geom_file << "\n"; + return 1; + } + TMS_Geom::GetInstance().SetGeometry(geom); + + std::ofstream out(out_csv); + if (!out) { + std::cerr << "Could not open " << out_csv << " for writing\n"; + return 1; + } + bool scanned = true; + if (mode == "path") scanned = RunPathScan(out, nthrows, distance_mm); + else if (mode == "position") scanned = RunPositionScan(out, nthrows); + else if (mode == "pileup") RunPileupScan(out, nthrows); + else RunPairScan(out, nthrows); + out.close(); + if (!scanned) { + std::cerr << "The " << mode << " scan could not run: " << out_csv << " has no data\n"; + return 1; + } + if (!out) { // a full disk or I/O error leaves a truncated CSV: do not report success + std::cerr << "Writing " << out_csv << " failed\n"; + return 1; + } + std::cout << "Wrote " << out_csv << "\n"; + return 0; +} diff --git a/app/DumpChannels.cpp b/app/DumpChannels.cpp new file mode 100644 index 00000000..7d66a7ad --- /dev/null +++ b/app/DumpChannels.cpp @@ -0,0 +1,106 @@ +// Lists every electronics channel of the detector geometry as "plane,bar,view" (the TMS_ChannelId +// the simulation and the bad channel list use), one per scintillator bar node. Used to draw bad +// channel lists. Usage: DumpChannels [channels.csv] +// (or --gdml-file in place of the input file). Needs TMS_DIR set. +#include +#include +#include +#include +#include +#include + +#include "TFile.h" +#include "TGeoBBox.h" +#include "TGeoManager.h" +#include "TGeoMatrix.h" +#include "TGeoNode.h" + +#include "TMS_Bar.h" +#include "TMS_Geom.h" +#include "TMS_Hit.h" +#include "TMS_Manager.h" + +namespace { + +struct Center { double x, y, z; }; + +// Collects the master-frame center of every scintillator bar node, as TMS_Bar::FindModules() uses it. +// Bars are only constructed after the walk, since building one moves the geometry navigator. +void Walk(TGeoManager* geom, const std::vector& bar_names, std::vector
& centers) { + TGeoNode* node = geom->GetCurrentNode(); + const std::string name(node->GetName()); + for (const std::string& bar_name : bar_names) { + if (name.find(bar_name) == std::string::npos) continue; + TGeoBBox* box = dynamic_cast(node->GetVolume()->GetShape()); + if (!box) return; + const double* origin = box->GetOrigin(); + double master[3]; + geom->GetCurrentMatrix()->LocalToMaster(origin, master); + TMS_Geom::GetInstance().Scale(master[0], master[1], master[2]); + centers.push_back({master[0], master[1], master[2]}); + return; + } + for (int i = 0; i < node->GetNdaughters(); ++i) { + geom->CdDown(i); + Walk(geom, bar_names, centers); + geom->CdUp(); + } +} + +} // namespace + +int main(int argc, char** argv) { + if (argc < 2) { + std::cerr << "Usage: DumpChannels [channels.csv]\n"; + return 1; + } + TGeoManager* geom = nullptr; + int next = 2; + if (std::string(argv[1]) == "--gdml-file") { + if (argc < 3) return 1; + geom = TGeoManager::Import(argv[2]); + next = 3; + } else { + TFile* input = TFile::Open(argv[1]); + if (input && !input->IsZombie()) geom = (TGeoManager*)input->Get("EDepSimGeometry"); + } + if (!geom) { std::cerr << "No geometry\n"; return 1; } + const std::string out_name = argc > next ? argv[next] : "channels.csv"; + + TMS_Geom::GetInstance().SetGeometry(geom); + TMS_Manager& manager = TMS_Manager::GetInstance(); + const std::vector bar_names = {manager.Get_GEOMETRY_VOLUME_ScintLayer(), + manager.Get_GEOMETRY_VOLUME_ScintLayerOrtho(), manager.Get_GEOMETRY_VOLUME_ScintLayerParallel()}; + + std::vector
centers; + geom->CdTop(); + Walk(geom, bar_names, centers); + geom->CdTop(); + + std::map channels; + int unplaced = 0; + for (const Center& c : centers) { + TMS_Bar bar(c.x, c.y, c.z); + if (bar.GetPlaneNumber() < 0 || bar.GetBarNumber() < 0 || bar.GetBarType() == TMS_Bar::kError) { ++unplaced; continue; } + channels[{bar.GetPlaneNumber(), bar.GetBarNumber(), bar.GetBarTypeNumber()}]++; + } + int duplicates = 0; + for (const auto& kv : channels) if (kv.second > 1) ++duplicates; + + std::ofstream out(out_name); + if (!out) { + std::cerr << "Could not open " << out_name << " for writing\n"; + return 1; + } + out << "plane,bar,view\n"; + for (const auto& kv : channels) out << kv.first.Plane << "," << kv.first.Bar << "," << kv.first.View << "\n"; + out.close(); + if (!out) { // a full disk or I/O error leaves an incomplete inventory: do not report success + std::cerr << "Writing " << out_name << " failed\n"; + return 1; + } + + std::cout << centers.size() << " bar nodes, " << channels.size() << " distinct channels written to " << out_name + << "; " << unplaced << " could not be placed, " << duplicates << " channel ids shared by more than one node\n"; + return (unplaced || duplicates) ? 2 : 0; +} diff --git a/app/Makefile b/app/Makefile index 283b019d..44280f7b 100755 --- a/app/Makefile +++ b/app/Makefile @@ -57,7 +57,10 @@ LIB_TMS = -lTMS_Prod EXE_DIR = ../bin MKDIR_P := mkdir -p -all: directories $(EXE_DIR)/ConvertToTMSTree.exe $(EXE_DIR)/BetheBloch_Example.exe $(EXE_DIR)/DBSCAN_test.exe $(EXE_DIR)/DrawEvents.exe $(EXE_DIR)/TrackLengthTester.exe $(EXE_DIR)/ShootRay.exe $(EXE_DIR)/BField_tester.exe $(EXE_DIR)/CherryPickEvents.exe $(EXE_DIR)/RealDataSmokeTest.exe $(EXE_DIR)/PassageUtilsTest.exe +# ChannelReadoutTest, DetSimStageScan and ArtificialResegmentationTest are CMake-only: they build +# synthetic edep-sim events by assigning TG4PrimaryParticle members, which are private in newer +# edep-sim releases (the CI Make build clones the latest one and fails to compile them). +all: directories $(EXE_DIR)/ConvertToTMSTree.exe $(EXE_DIR)/BetheBloch_Example.exe $(EXE_DIR)/DBSCAN_test.exe $(EXE_DIR)/DrawEvents.exe $(EXE_DIR)/TrackLengthTester.exe $(EXE_DIR)/ShootRay.exe $(EXE_DIR)/BField_tester.exe $(EXE_DIR)/CherryPickEvents.exe $(EXE_DIR)/RealDataSmokeTest.exe $(EXE_DIR)/PassageUtilsTest.exe $(EXE_DIR)/BadChannelsTest.exe $(EXE_DIR)/DumpChannels.exe # Sanitize target sanitize: CXXFLAGS += -fsanitize=address @@ -80,6 +83,12 @@ $(EXE_DIR)/RealDataSmokeTest.exe: RealDataSmokeTest.cpp $(EXE_DIR)/PassageUtilsTest.exe: PassageUtilsTest.cpp $(CXX) $(CXXFLAGS) -o $@ $^ -L $(LIB_DIR) $(LIB_TMS) $(LIB_OBJ) $(BUILD_FLAGS) +$(EXE_DIR)/BadChannelsTest.exe: BadChannelsTest.cpp + $(CXX) $(CXXFLAGS) -o $@ $^ -L $(LIB_DIR) $(LIB_TMS) $(LIB_OBJ) $(BUILD_FLAGS) + +$(EXE_DIR)/DumpChannels.exe: DumpChannels.cpp + $(CXX) $(CXXFLAGS) -o $@ $^ -L $(LIB_DIR) $(LIB_TMS) $(LIB_OBJ) $(BUILD_FLAGS) + $(EXE_DIR)/BetheBloch_Example.exe: BetheBloch_Example.cpp $(CXX) $(CXXFLAGS) $(ROOT_INC) -o $@ $^ $(ROOT_LIBS) diff --git a/config/TMS_BadChannels_Default.toml b/config/TMS_BadChannels_Default.toml new file mode 100644 index 00000000..eb1e89db --- /dev/null +++ b/config/TMS_BadChannels_Default.toml @@ -0,0 +1,12 @@ +# Electronics channels that give no usable readout; the decoder makes no TMS hit for them +# (see src/TMS_BadChannels.h). Empty: no channel is excluded. +# +# One entry per channel. Bar numbers are unique only within a plane and view, and the two halves of +# a split X-bar are separate channels with their own bar numbers. +# +# [[BadChannel]] +# Plane = 12 +# Bar = 40 +# View = 3 +# Status = "dead" # "missing" (no such bar) or "dead" (bar does not report) +# Reason = "example: broken WLS fiber" # optional diff --git a/config/TMS_Default_Config.toml b/config/TMS_Default_Config.toml index 91302be5..61000099 100644 --- a/config/TMS_Default_Config.toml +++ b/config/TMS_Default_Config.toml @@ -351,6 +351,12 @@ OuterShellThickness=300.0 # mm CutEnergy = 30.0 # MeV +# The decoder turns the electronics readouts (simulated now, real data later) into TMS hits +[Decoder] + # Channels known to be missing or dead: their readouts make no TMS hit (their truth is kept). + # Relative to $TMS_DIR, or absolute; "" = none. The shipped list is empty. + BadChannelFile = "config/TMS_BadChannels_Default.toml" + # Options for processing and saving truth information # LightWeight reduces run time signficantly because we only care about TMS objects (no LAr) [Truth] diff --git a/scripts/BadChannels/make_bad_channel_lists.py b/scripts/BadChannels/make_bad_channel_lists.py new file mode 100644 index 00000000..df29496d --- /dev/null +++ b/scripts/BadChannels/make_bad_channel_lists.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""Draw random bad channel lists for the bad channel scan (see src/TMS_BadChannels.h). + +Input: a channel list from `DumpChannels` (CSV: plane,bar,view). For each seed the channels are put +in one fixed random order and a fraction f of them is the first round(f * N) of that order, so the +lists of one seed are nested (5% inside 10% inside ...) and the scan over f differs only by the +added channels, not by a new random draw. Different seeds are independent draws. + +Output: /f_s.toml for each fraction and seed, plus f00.toml (no bad channels). +All channels are written with Status = "dead". Reproducible: same channels file, fractions and seeds +give identical files. + +Usage: make_bad_channel_lists.py channels.csv outdir [--fractions 5 10 15 20 25] [--seeds 1 2 3] +""" +import argparse +import csv +import os +import random + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("channels") + ap.add_argument("outdir") + ap.add_argument("--fractions", type=float, nargs="+", default=[5, 10, 15, 20, 25], help="percent of channels") + ap.add_argument("--seeds", type=int, nargs="+", default=[1, 2, 3]) + args = ap.parse_args() + # A negative fraction would select order[:-k] (nearly everything) and above 100 everything + bad = [f for f in args.fractions if not 0 <= f <= 100] + if bad: + ap.error(f"--fractions must be percentages between 0 and 100, got {bad}") + + with open(args.channels) as f: + rows = list(csv.DictReader(f)) + channels = sorted((int(r["plane"]), int(r["bar"]), int(r["view"])) for r in rows) + if len(set(channels)) != len(channels): + raise SystemExit("channel list has duplicates") + n = len(channels) + os.makedirs(args.outdir, exist_ok=True) + + def write(name, chosen, note): + with open(os.path.join(args.outdir, name), "w") as out: + out.write(f"# {note}\n# Channels: {len(chosen)} of {n} ({100.0 * len(chosen) / n:.2f}%). See src/TMS_BadChannels.h.\n") + out.write("BadChannel = [\n") + for p, b, v in sorted(chosen): + out.write(f' {{Plane = {p}, Bar = {b}, View = {v}, Status = "dead"}},\n') + out.write("]\n") + + write("f00.toml", [], "No bad channels.") + for seed in args.seeds: + order = list(channels) + random.Random(seed).shuffle(order) + for pct in args.fractions: + k = round(pct / 100.0 * n) + # Integer percentages keep the names f05, f10, ...; a fraction keeps its decimals (f05p1) + # so that two fractions that round to the same integer do not overwrite each other + tag = f"{int(pct):02d}" if float(pct).is_integer() else f"{pct:g}".replace(".", "p").zfill(4) + name = f"f{tag}_s{seed}.toml" + write(name, order[:k], f"Random bad channels, {pct:g}% of channels, seed {seed} (nested within the larger fractions of this seed).") + print(name, k) + + +if __name__ == "__main__": + main() diff --git a/scripts/Validation/DetSim/README.md b/scripts/Validation/DetSim/README.md new file mode 100644 index 00000000..4d7674fb --- /dev/null +++ b/scripts/Validation/DetSim/README.md @@ -0,0 +1,42 @@ +# Detector-simulation validation + +`detsim_validation.py` compares two or more `ConvertToTMSTree` runs over the same input files at the hit level (no +tracking or slicing) and writes `report.md`, `metrics.json` and six plots into an output directory. The first run is +the reference. + +``` +python3 detsim_validation.py --out [--files 1-15] off= on= +``` + +Each run directory holds `_RecoCandidates.root` and `_Readout.root` per file number. Needs uproot, +awkward, numpy and matplotlib (at FNAL: the CVMFS python v3_9_15 setup plus a user install of those packages). + +What it measures (lepton-dominated hits: `TrueLeptonicEnergy / TrueHitE > 0.95`; leptonic means electrons, muons, neutrinos and photons, because the output has no per-hit PDG code, so on shower-rich samples electron and photon hits are included): + +| Section | Quantity | Plot | +|---|---|---| +| 1 | readout hits and PE above threshold per spill; PE after fibers per MeV for lepton and hadronic hits | | +| 2 | hit survival vs true path length (`TrueHitDx`) and vs true deposited energy | `survival.png` | +| 3 | reco PE mean, relative spread and fraction below 3 PE per band; integer-PE fraction | `pe_partial_fill.png` | +| 4 | mean reco - true hit time vs number of Geant4 contributions, full crossings | `time_bias_vs_contributions.png` | +| 5 | one particle with more than one final hit in a bar and slice, by deposit-time gap (a close-deposit diagnostic: the readout window opens at the first photon, so pairs inside the 120 ns window can be split legitimately; `ChannelReadoutTest` is the correctness test) | `merge_gaps.png` | +| 6 | plane coverage: fraction of (particle, plane) deposits with a surviving hit, by deposited energy | `plane_coverage.png` | +| 7 | light provenance (`UseResponseElements = true` only): photon count vs PE, most-light vs most-energy particle | `light_share.png` | + +`TrueHitDx` is not written by default; without it, sections 2-4 use deposited-energy bands instead of path length. + +## Stage-by-stage scans (`stages/`) + +Controlled checks of each simulation stage on its own, legacy vs new pipeline: synthetic crossings of one known bar +through the real `TMS_Event::FinalizeEvent()` (`ArtificialResegmentationTest` and `DetSimStageScan`), reduced to a +self-contained HTML page. + +``` +stages/run_stage_scans.sh /scans # ~1 min on 16 cores +python3 stages/stage_page_data.py --scans /scans [--spills --files 1-20] \ + [--metrics ] --out /page_data.json +python3 stages/build_stage_page.py /page_data.json notes.json /detsim_stages.html +``` + +`notes.json` holds the page's prose (keys INTRO, META, N1-N6, N4X, OPEN, REGEN); it quotes numbers, so check it against +a new `page_data.json`. diff --git a/scripts/Validation/DetSim/detsim_validation.py b/scripts/Validation/DetSim/detsim_validation.py new file mode 100755 index 00000000..d0ce9a7e --- /dev/null +++ b/scripts/Validation/DetSim/detsim_validation.py @@ -0,0 +1,493 @@ +#!/usr/bin/env python3 +"""Detector-simulation validation: hit-level metrics and plots comparing ConvertToTMSTree outputs. + +Compares two or more runs over the same input files (the first is the reference) and writes a Markdown report, a +JSON file with every number in it, and a few plots. Only simulation-level quantities: no tracking or slicing. + +Sections (lepton-dominated hits = TrueLeptonicEnergy / TrueHitE > 0.95, as in the 2026-09 segmentation benchmark; +"leptonic" is TMS_TrueParticle::IsLeptonic(): electrons, muons, neutrinos and photons. The output has no per-hit PDG +code, so muons cannot be selected alone: on a shower-rich sample electron/photon hits are included): + 1. Totals: readout hits and PE above threshold per spill; light yield (PE after fibers per MeV) for lepton- and + hadron-dominated hits. + 2. Hit survival (not pedestal-suppressed) vs true path length in the bar (TrueHitDx, if the run has it) and vs true + deposited energy. + 3. Photostatistics: reco PE mean and relative spread per path-length (or energy) band; PE distribution at partial fill. + 4. Timing: mean (reco - true) hit time vs the number of Geant4 contributions (TrueNTrueParticles) at full crossings. + 5. Coincident-hit merging: groups of more than one final true hit for one particle in one bar and slice, by time gap. + Within the readout window these are merge failures; beyond it, separate readouts by design. + 6. Plane coverage: per (particle, plane) with >= 0.5 MeV deposited by a lepton-dominated particle, the fraction with a + surviving hit, by deposited energy. + 7. Light provenance (runs with Sim.DetSim.UseResponseElements = true): photon count vs detected PE, most-light vs + most-energy particle, first photon vs most-light particle. + +Inputs per run: a directory with _RecoCandidates.root (Truth_Info, Reco_Tree) and _Readout.root +(TMS tree) for each file number. + +Usage: + detsim_validation.py --out [--files 1-15]