|
| 1 | +//////////////////////////////////////////////////////////////////////// |
| 2 | +// FilterMCTruthVolume_module.cc |
| 3 | +// |
| 4 | +// art::EDFilter that selects events containing at least one MCTruth |
| 5 | +// neutrino interaction with a vertex __outside__ a user-specified box. |
| 6 | +// Produces filtered MCTruth, GTruth, and MCFlux collections with |
| 7 | +// their associations. Passes through POT summary at the SubRun level. |
| 8 | +//////////////////////////////////////////////////////////////////////// |
| 9 | + |
| 10 | +#include "art/Framework/Core/EDFilter.h" |
| 11 | +#include "art/Framework/Core/ModuleMacros.h" |
| 12 | +#include "art/Framework/Principal/Event.h" |
| 13 | +#include "art/Framework/Principal/Handle.h" |
| 14 | +#include "art/Framework/Principal/SubRun.h" |
| 15 | +#include "art/Persistency/Common/PtrMaker.h" |
| 16 | +#include "canvas/Persistency/Common/Assns.h" |
| 17 | +#include "canvas/Persistency/Common/FindOneP.h" |
| 18 | +#include "canvas/Utilities/InputTag.h" |
| 19 | +#include "fhiclcpp/ParameterSet.h" |
| 20 | +#include "messagefacility/MessageLogger/MessageLogger.h" |
| 21 | + |
| 22 | +#include "larcoreobj/SummaryData/POTSummary.h" |
| 23 | +#include "nusimdata/SimulationBase/MCTruth.h" |
| 24 | +#include "nusimdata/SimulationBase/GTruth.h" |
| 25 | +#include "nusimdata/SimulationBase/MCFlux.h" |
| 26 | + |
| 27 | +#include <array> |
| 28 | +#include <memory> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +class FilterMCTruthVolume : public art::EDFilter { |
| 32 | +public: |
| 33 | + explicit FilterMCTruthVolume(fhicl::ParameterSet const& pset); |
| 34 | + |
| 35 | + bool filter(art::Event& evt) override; |
| 36 | + bool endSubRun(art::SubRun& sr) override; |
| 37 | + |
| 38 | +private: |
| 39 | + art::InputTag fMCTruthLabel; |
| 40 | + art::InputTag fPOTLabel; |
| 41 | + std::array<double, 3> fVolumeLow; |
| 42 | + std::array<double, 3> fVolumeHigh; |
| 43 | + |
| 44 | + bool inVolume(double x, double y, double z) const; |
| 45 | +}; |
| 46 | + |
| 47 | +// --------------------------------------------------------------------------- |
| 48 | +FilterMCTruthVolume::FilterMCTruthVolume(fhicl::ParameterSet const& pset) |
| 49 | + : EDFilter{pset} |
| 50 | + , fMCTruthLabel{pset.get<art::InputTag>("MCTruthLabel", "generator")} |
| 51 | + , fPOTLabel{pset.get<art::InputTag>("POTLabel", "generator")} |
| 52 | +{ |
| 53 | + auto lo = pset.get<std::vector<double>>("VolumeLow"); |
| 54 | + auto hi = pset.get<std::vector<double>>("VolumeHigh"); |
| 55 | + if (lo.size() != 3 || hi.size() != 3) |
| 56 | + throw art::Exception(art::errors::Configuration) |
| 57 | + << "VolumeLow and VolumeHigh must each have exactly 3 elements (x, y, z)."; |
| 58 | + std::copy(lo.begin(), lo.end(), fVolumeLow.begin()); |
| 59 | + std::copy(hi.begin(), hi.end(), fVolumeHigh.begin()); |
| 60 | + |
| 61 | + produces<std::vector<simb::MCTruth>>(); |
| 62 | + produces<std::vector<simb::GTruth>>(); |
| 63 | + produces<std::vector<simb::MCFlux>>(); |
| 64 | + produces<art::Assns<simb::MCTruth, simb::GTruth>>(); |
| 65 | + produces<art::Assns<simb::MCTruth, simb::MCFlux>>(); |
| 66 | + produces<sumdata::POTSummary, art::InSubRun>(); |
| 67 | +} |
| 68 | + |
| 69 | +// --------------------------------------------------------------------------- |
| 70 | +bool FilterMCTruthVolume::inVolume(double x, double y, double z) const |
| 71 | +{ |
| 72 | + return x >= fVolumeLow[0] && x <= fVolumeHigh[0] && |
| 73 | + y >= fVolumeLow[1] && y <= fVolumeHigh[1] && |
| 74 | + z >= fVolumeLow[2] && z <= fVolumeHigh[2]; |
| 75 | +} |
| 76 | + |
| 77 | +// --------------------------------------------------------------------------- |
| 78 | +bool FilterMCTruthVolume::filter(art::Event& evt) |
| 79 | +{ |
| 80 | + auto outMCTruth = std::make_unique<std::vector<simb::MCTruth>>(); |
| 81 | + auto outGTruth = std::make_unique<std::vector<simb::GTruth>>(); |
| 82 | + auto outMCFlux = std::make_unique<std::vector<simb::MCFlux>>(); |
| 83 | + auto assnsTG = std::make_unique<art::Assns<simb::MCTruth, simb::GTruth>>(); |
| 84 | + auto assnsTF = std::make_unique<art::Assns<simb::MCTruth, simb::MCFlux>>(); |
| 85 | + |
| 86 | + auto const& mctruthHandle = evt.getValidHandle<std::vector<simb::MCTruth>>(fMCTruthLabel); |
| 87 | + |
| 88 | + art::FindOneP<simb::GTruth> findGTruth(mctruthHandle, evt, fMCTruthLabel); |
| 89 | + art::FindOneP<simb::MCFlux> findMCFlux(mctruthHandle, evt, fMCTruthLabel); |
| 90 | + |
| 91 | + art::PtrMaker<simb::MCTruth> makeTruthPtr(evt); |
| 92 | + art::PtrMaker<simb::GTruth> makeGTruthPtr(evt); |
| 93 | + art::PtrMaker<simb::MCFlux> makeMCFluxPtr(evt); |
| 94 | + |
| 95 | + for (size_t i = 0; i < mctruthHandle->size(); ++i) { |
| 96 | + auto const& mct = mctruthHandle->at(i); |
| 97 | + |
| 98 | + if (!mct.NeutrinoSet()) continue; |
| 99 | + |
| 100 | + double vx = mct.GetNeutrino().Nu().Vx(); |
| 101 | + double vy = mct.GetNeutrino().Nu().Vy(); |
| 102 | + double vz = mct.GetNeutrino().Nu().Vz(); |
| 103 | + |
| 104 | + if (inVolume(vx, vy, vz)) continue; |
| 105 | + |
| 106 | + outMCTruth->push_back(mct); |
| 107 | + size_t idx = outMCTruth->size() - 1; |
| 108 | + |
| 109 | + if (findGTruth.isValid()) { |
| 110 | + auto const& gtp = findGTruth.at(i); |
| 111 | + if (gtp.isNonnull()) { |
| 112 | + outGTruth->push_back(*gtp); |
| 113 | + assnsTG->addSingle(makeTruthPtr(idx), makeGTruthPtr(outGTruth->size() - 1)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (findMCFlux.isValid()) { |
| 118 | + auto const& mfp = findMCFlux.at(i); |
| 119 | + if (mfp.isNonnull()) { |
| 120 | + outMCFlux->push_back(*mfp); |
| 121 | + assnsTF->addSingle(makeTruthPtr(idx), makeMCFluxPtr(outMCFlux->size() - 1)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + bool pass = !outMCTruth->empty(); |
| 127 | + |
| 128 | + evt.put(std::move(outMCTruth)); |
| 129 | + evt.put(std::move(outGTruth)); |
| 130 | + evt.put(std::move(outMCFlux)); |
| 131 | + evt.put(std::move(assnsTG)); |
| 132 | + evt.put(std::move(assnsTF)); |
| 133 | + |
| 134 | + return pass; |
| 135 | +} |
| 136 | + |
| 137 | +// --------------------------------------------------------------------------- |
| 138 | +bool FilterMCTruthVolume::endSubRun(art::SubRun& sr) |
| 139 | +{ |
| 140 | + auto const& potHandle = sr.getValidHandle<sumdata::POTSummary>(fPOTLabel); |
| 141 | + sr.put(std::make_unique<sumdata::POTSummary>(*potHandle), art::subRunFragment()); |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +DEFINE_ART_MODULE(FilterMCTruthVolume) |
0 commit comments