|
| 1 | +/** |
| 2 | + * @file FilterTauDecayType.cc |
| 3 | + * @brief A simple filter to select events with specified tau decay types (leptonic or hadronic) |
| 4 | + * @author Yun-Tse Tsai (yuntse@slac.stanford.edu) |
| 5 | + * @date August 1, 2019 |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | +// LArSoft libraries |
| 11 | +#include "nusimdata/SimulationBase/MCTruth.h" |
| 12 | +#include "nusimdata/SimulationBase/MCParticle.h" |
| 13 | + |
| 14 | + |
| 15 | +// framework libraries |
| 16 | +#include "art/Framework/Core/EDFilter.h" |
| 17 | +#include "art/Framework/Core/ModuleMacros.h" |
| 18 | +#include "art/Framework/Principal/Event.h" |
| 19 | +#include "art/Framework/Principal/Handle.h" // art::ValidHandle<> |
| 20 | +#include "art/Persistency/Common/PtrMaker.h" |
| 21 | +#include "canvas/Persistency/Common/Ptr.h" |
| 22 | +#include "canvas/Utilities/InputTag.h" |
| 23 | +#include "messagefacility/MessageLogger/MessageLogger.h" |
| 24 | +#include "fhiclcpp/types/Atom.h" |
| 25 | +#include "fhiclcpp/types/Sequence.h" |
| 26 | +#include "fhiclcpp/types/Table.h" |
| 27 | +#include "cetlib_except/exception.h" |
| 28 | + |
| 29 | +// C++ libraries |
| 30 | +#include <vector> |
| 31 | +#include <set> |
| 32 | +#include <algorithm> // std::copy() |
| 33 | +#include <iterator> // std::inserter() |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | + template <typename T> |
| 38 | + std::set<T> vectorToSet(std::vector<T> const& v) { |
| 39 | + std::set<T> s; |
| 40 | + std::copy(v.begin(), v.end(), std::inserter(s, s.begin())); |
| 41 | + return s; |
| 42 | + } // vectorToSet() |
| 43 | + |
| 44 | +} // local namespace |
| 45 | + |
| 46 | + |
| 47 | +namespace bdm { |
| 48 | + |
| 49 | + class FilterTauDecayType : public art::EDFilter |
| 50 | + { |
| 51 | + public: |
| 52 | + |
| 53 | + /// Algorithm configuration |
| 54 | + struct Config { |
| 55 | + |
| 56 | + using Name = fhicl::Name; |
| 57 | + using Comment = fhicl::Comment; |
| 58 | + |
| 59 | + fhicl::Atom< art::InputTag > MCTruthLabel{ |
| 60 | + Name("MCTruthLabel"), |
| 61 | + Comment("the producer of the MCTruth used in the filter.") |
| 62 | + }; |
| 63 | + |
| 64 | + fhicl::Atom< bool > vetoMode{ |
| 65 | + Name("VetoMode"), |
| 66 | + Comment("whether to veto the events containing the specified decay products.") |
| 67 | + }; |
| 68 | + |
| 69 | + fhicl::Sequence< int > SpecifiedParticlePdgCode{ |
| 70 | + Name("SpecifiedParticlePdgCode"), |
| 71 | + Comment("the pdgcodes of the specified decay products for the events.") |
| 72 | + }; |
| 73 | + |
| 74 | + }; // Config |
| 75 | + |
| 76 | + using Parameters = art::EDFilter::Table<Config>; |
| 77 | + |
| 78 | + explicit FilterTauDecayType( Parameters const& config ); |
| 79 | + |
| 80 | + virtual bool filter(art::Event&) override; |
| 81 | + |
| 82 | + private: |
| 83 | + |
| 84 | + art::InputTag fMCTruthLabel; |
| 85 | + bool fVetoMode; |
| 86 | + std::set< int > fSpecifiedParticles; |
| 87 | + int fNEvents; |
| 88 | + int fNDesiredEvents; |
| 89 | + }; |
| 90 | + |
| 91 | +} // namespace bdm |
| 92 | + |
| 93 | +// ------------------------------------------------------------------------- |
| 94 | +// --- FilterTauDecayType |
| 95 | +// --- |
| 96 | +bdm::FilterTauDecayType::FilterTauDecayType( Parameters const& config ) |
| 97 | + : fMCTruthLabel( config().MCTruthLabel() ) |
| 98 | + , fVetoMode( config().vetoMode() ) |
| 99 | + , fSpecifiedParticles( ::vectorToSet(config().SpecifiedParticlePdgCode()) ) |
| 100 | + , fNEvents( 0 ) |
| 101 | + , fNDesiredEvents( 0 ) |
| 102 | +{ |
| 103 | + consumes< std::vector< simb::MCTruth > >( fMCTruthLabel ); |
| 104 | +} |
| 105 | + |
| 106 | +bool bdm::FilterTauDecayType::filter( art::Event& evt ) |
| 107 | +{ |
| 108 | + |
| 109 | + auto MCTruthHandle = evt.getValidHandle< std::vector< simb::MCTruth > >( fMCTruthLabel ); |
| 110 | + auto const& MCTruthObjs = *MCTruthHandle; |
| 111 | + |
| 112 | + bool isDesired = fVetoMode; |
| 113 | + ++fNEvents; |
| 114 | + |
| 115 | + for ( size_t iMCTruth = 0; iMCTruth < MCTruthObjs.size(); ++iMCTruth ) { |
| 116 | + simb::MCTruth const& MCTruthObj = MCTruthObjs[iMCTruth]; |
| 117 | + auto const& lepton = MCTruthObj.GetNeutrino().Lepton(); |
| 118 | + auto const leptonPdgCode = lepton.PdgCode(); |
| 119 | + |
| 120 | + if ( abs( leptonPdgCode ) != 15 ) continue; |
| 121 | + |
| 122 | + int tauTrackID = lepton.TrackId(); |
| 123 | + |
| 124 | + for ( int iMCParticle = 0; iMCParticle < MCTruthObj.NParticles(); ++iMCParticle ) { |
| 125 | + const simb::MCParticle& thisMCParticle = MCTruthObj.GetParticle( iMCParticle ); |
| 126 | + if ( thisMCParticle.Mother() != tauTrackID ) continue; |
| 127 | + int thisPdgCode = thisMCParticle.PdgCode(); |
| 128 | + // std::cout << "MCParticle " << iMCParticle << " with PDGCode: " << thisPdgCode << std::endl; |
| 129 | + if ( fSpecifiedParticles.count( thisPdgCode ) > 0 ) { |
| 130 | + isDesired = !( fVetoMode ); |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if ( isDesired != fVetoMode ) break; |
| 136 | + |
| 137 | + } |
| 138 | + if ( isDesired ) ++fNDesiredEvents; |
| 139 | + std::cout << "Number of events: " << fNEvents << ", number of filtered events: " << fNDesiredEvents |
| 140 | + << std::endl; |
| 141 | + return isDesired; |
| 142 | +} |
| 143 | + |
| 144 | +//------------------------------------------------------------------------------ |
| 145 | +DEFINE_ART_MODULE(bdm::FilterTauDecayType) |
0 commit comments