|
| 1 | +// |
| 2 | +// Filter for mobile sync detector hits |
| 3 | +// Michael MacKenzie, 2026 |
| 4 | +// |
| 5 | + |
| 6 | +// Framework |
| 7 | +#include "art/Framework/Core/EDFilter.h" |
| 8 | +#include "art/Framework/Principal/Event.h" |
| 9 | +#include "art/Framework/Principal/Handle.h" |
| 10 | +#include "fhiclcpp/ParameterSet.h" |
| 11 | + |
| 12 | +// Offline |
| 13 | +#include "Offline/RecoDataProducts/inc/MSDHit.hh" |
| 14 | + |
| 15 | +// C++ |
| 16 | +#include <string> |
| 17 | + |
| 18 | +// TRACE |
| 19 | +#include "TRACE/tracemf.h" |
| 20 | +#define TRACE_NAME "MSDHitFilter" |
| 21 | + |
| 22 | +namespace mu2e { |
| 23 | + class MSDHitFilter : public art::EDFilter { |
| 24 | + public: |
| 25 | + |
| 26 | + // Main configuration |
| 27 | + struct Config { |
| 28 | + using Name = fhicl::Name; |
| 29 | + using Comment = fhicl::Comment; |
| 30 | + fhicl::Atom<std::string> tag {Name("tag") , Comment("Collection tag")}; |
| 31 | + fhicl::Atom<int> minHits{Name("minHits"), Comment("Minimum number of hits")}; |
| 32 | + }; |
| 33 | + |
| 34 | + using Parameters = art::EDFilter::Table<Config>; |
| 35 | + |
| 36 | + explicit MSDHitFilter(const Parameters& config); |
| 37 | + |
| 38 | + |
| 39 | + private: |
| 40 | + bool filter (art::Event& event) override; |
| 41 | + bool endRun (art::Run& run ) override; |
| 42 | + bool goodHit (const MSDHit& hit); |
| 43 | + |
| 44 | + // Inputs |
| 45 | + std::string _tag; |
| 46 | + int _minHits = -1; |
| 47 | + |
| 48 | + // Data |
| 49 | + unsigned long _nevt, _npass; |
| 50 | + |
| 51 | + }; |
| 52 | + |
| 53 | + //----------------------------------------------------------------------------- |
| 54 | + MSDHitFilter::MSDHitFilter(const Parameters& conf) |
| 55 | + : art::EDFilter{conf} |
| 56 | + , _tag(conf().tag()) |
| 57 | + , _minHits(conf().minHits()) |
| 58 | + , _nevt(0) |
| 59 | + , _npass(0) |
| 60 | + { |
| 61 | + } |
| 62 | + |
| 63 | + //----------------------------------------------------------------------------- |
| 64 | + // Check if the hit is accepted |
| 65 | + bool MSDHitFilter::goodHit(const MSDHit& hit) { |
| 66 | + if(!hit.hasTime()) return false; // time must be defined for the hit |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + //----------------------------------------------------------------------------- |
| 71 | + bool MSDHitFilter::filter(art::Event& event) { |
| 72 | + |
| 73 | + // Count total events seen |
| 74 | + ++_nevt; |
| 75 | + |
| 76 | + // Filter flag |
| 77 | + bool passed = true; |
| 78 | + |
| 79 | + auto handle = event.getValidHandle<MSDHitCollection>(_tag); |
| 80 | + const auto hits = handle.product(); |
| 81 | + int naccepted = 0; |
| 82 | + for(const auto& hit : *hits) if(goodHit(hit)) ++naccepted; |
| 83 | + |
| 84 | + passed &= naccepted >= _minHits; |
| 85 | + |
| 86 | + if (passed) ++_npass; |
| 87 | + |
| 88 | + // Return the result |
| 89 | + return passed; |
| 90 | + } |
| 91 | + |
| 92 | + //----------------------------------------------------------------------------- |
| 93 | + bool MSDHitFilter::endRun(art::Run& run) { |
| 94 | + // Print a summary of the filter results |
| 95 | + const float rate = (_nevt > 0) ? float(_npass)/float(_nevt) : 0.f; |
| 96 | + TLOG(TLVL_DEBUG + 2) << "passed " << _npass << " events out of " << _nevt << " for a ratio of " << rate; |
| 97 | + _nevt = 0; _npass = 0; // reset for the next run |
| 98 | + return true; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +DEFINE_ART_MODULE(mu2e::MSDHitFilter) |
0 commit comments