Skip to content
Draft
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
1 change: 1 addition & 0 deletions sbndcode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_subdirectory(Commissioning)
add_subdirectory(Trigger)
add_subdirectory(Timing)
add_subdirectory(PTBAna)
add_subdirectory(FullOsc)

# subdirectory for gallery stuff
add_subdirectory(gallery)
Expand Down
29 changes: 29 additions & 0 deletions sbndcode/FullOsc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
set(
MODULE_LIBRARIES
PUBLIC
art::Framework_Core
art::Framework_Services_Registry
art_root_io::TFileService_service
art_root_io::TFileService_service
art_root_io::tfile_support
art::Framework_Principal
art::Persistency_Common
art::Utilities canvas::canvas
cetlib::cetlib cetlib_except::cetlib_except
CLHEP::CLHEP
nugen::EventGeneratorBase_GENIE
nusimdata::SimulationBase
lardata::Utilities
larcoreobj::SummaryData
fhiclcpp::fhiclcpp
messagefacility::MF_MessageLogger
)

cet_build_plugin(GENIEOscPileup art::module SOURCE GENIEOscPileup_module.cc LIBRARIES ${MODULE_LIBRARIES})
cet_build_plugin(GENIEFirst art::module SOURCE GENIEFirst_module.cc LIBRARIES ${MODULE_LIBRARIES})
cet_build_plugin(FullOscNuFilter art::module SOURCE FullOscNuFilter_module.cc
LIBRARIES ${MODULE_LIBRARIES} sbndcode_RecoUtils)

install_headers()
install_fhicl()
install_source()
102 changes: 102 additions & 0 deletions sbndcode/FullOsc/FullOscNuFilter_module.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
////////////////////////////////////////////////////////////////////////
// Class: FullOscNuFilter
// Module Type: filter
// File: FullOscNuFilter_module.cc
//
// Copy of GenNuFilter for the full-osc workflow. Reads a single labelled
// MCTruth collection and can restrict the selection to the first
// (oscillated) neutrino only, via OscOnly.
////////////////////////////////////////////////////////////////////////

#include <algorithm>
#include <string>
#include <vector>

#include "TVector3.h"

#include "art/Framework/Core/EDFilter.h"
#include "art/Framework/Core/ModuleMacros.h"
#include "art/Framework/Principal/Event.h"
#include "art/Framework/Principal/Handle.h"
#include "canvas/Persistency/Common/Ptr.h"
#include "fhiclcpp/ParameterSet.h"
#include "messagefacility/MessageLogger/MessageLogger.h"
#include "nusimdata/SimulationBase/MCTruth.h"
#include "nusimdata/SimulationBase/MCNeutrino.h"
#include "sbndcode/RecoUtils/RecoUtils.h"

namespace filt {

class FullOscNuFilter : public art::EDFilter {
public:
explicit FullOscNuFilter(fhicl::ParameterSet const& pset);
bool filter(art::Event& e) override;

private:
bool fVtxInTPC;
std::vector<int> fLepPDGs;
bool fCC;
bool fNC;
bool fOscOnly;
std::string fGenieModuleLabel;
};


FullOscNuFilter::FullOscNuFilter(fhicl::ParameterSet const& pset)
: EDFilter(pset)
, fVtxInTPC(pset.get<bool>("VtxInTPC"))
, fLepPDGs(pset.get<std::vector<int>>("LepPDGs"))
, fCC(pset.get<bool>("CC"))
, fNC(pset.get<bool>("NC"))
, fOscOnly(pset.get<bool>("OscOnly", false))
, fGenieModuleLabel(pset.get<std::string>("GenieModuleLabel", "generator"))
{}


bool FullOscNuFilter::filter(art::Event& e)
{
art::Handle<std::vector<simb::MCTruth>> mclist;
e.getByLabel(fGenieModuleLabel, mclist);

// Safety: missing or empty collection -> reject event instead of crashing
if (!mclist.isValid()) {
mf::LogWarning("FullOscNuFilter")
<< "No std::vector<simb::MCTruth> with label '" << fGenieModuleLabel
<< "' in event " << e.id() << "; rejecting event.";
return false;
}
if (mclist->empty()) return false;

const size_t end = fOscOnly ? 1 : mclist->size();

for (size_t i = 0; i < end; ++i) {
const art::Ptr<simb::MCTruth> mc_truth(mclist, i);
if (mc_truth->Origin() != simb::kBeamNeutrino) continue;

const simb::MCNeutrino& nu = mc_truth->GetNeutrino();

// Check vertex in TPC
TVector3 vtx(nu.Nu().Vx(), nu.Nu().Vy(), nu.Nu().Vz());
if (fVtxInTPC && !RecoUtils::IsInsideTPC(vtx, 0)) continue;

// Check if CC or NC specified
if (!(fCC && fNC)) {
if (fCC && nu.CCNC() == simb::kNC) continue;
if (fNC && nu.CCNC() == simb::kCC) continue;
}

// Check which flavour of interaction (outgoing lepton PDG)
const int lepPdg = nu.Lepton().PdgCode();
if (!fLepPDGs.empty() && fLepPDGs[0] != 0) {
if (std::find(fLepPDGs.begin(), fLepPDGs.end(), lepPdg) == fLepPDGs.end()) continue;
}

return true;
}
return false;
}


DEFINE_ART_MODULE(FullOscNuFilter)

}
171 changes: 171 additions & 0 deletions sbndcode/FullOsc/GENIEFirst_module.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#include "art/Framework/Core/EDProducer.h"
#include "art/Framework/Core/ModuleMacros.h"
#include "art/Framework/Principal/Event.h"
#include "art/Framework/Principal/Handle.h"
#include "art/Framework/Principal/Run.h"
#include "art/Framework/Principal/SubRun.h"
#include "art/Utilities/make_tool.h"
#include "art_root_io/TFileService.h"

#include "canvas/Utilities/InputTag.h"
#include "fhiclcpp/ParameterSet.h"
#include "fhiclcpp/types/Atom.h"
#include "fhiclcpp/types/Table.h"
#include "messagefacility/MessageLogger/MessageLogger.h"
#include "lardata/Utilities/AssociationUtil.h"

#include "nusimdata/SimulationBase/MCTruth.h"
#include "nusimdata/SimulationBase/MCFlux.h"
#include "nusimdata/SimulationBase/GTruth.h"
#include "lardataobj/Simulation/BeamGateInfo.h"
#include "larcoreobj/SummaryData/RunData.h"
#include "larcoreobj/SummaryData/POTSummary.h"

#include <memory>
#include <vector>
#include <string>

namespace evgen {

class GENIEFirst : public art::EDProducer {
public:

struct Config {
fhicl::Atom<art::InputTag> InputTag {
fhicl::Name("InputTag"),
fhicl::Comment("InputTag for the GENIEGen module")
};
};

using Parameters = art::EDProducer::Table<Config>;

explicit GENIEFirst(Parameters const& p);

void produce(art::Event& evt) override;
void beginRun(art::Run& run) override;
void endSubRun(art::SubRun& subrun) override;

private:
art::InputTag fInputTag;

void firstosc(art::Event& evt,
std::vector<simb::MCTruth>& firstTruth,
std::vector<simb::MCFlux>& firstFlux,
std::vector<simb::GTruth>& firstGTruth,
art::Assns<simb::MCTruth, simb::MCFlux>& firstTFassn,
art::Assns<simb::MCTruth, simb::GTruth>& firstTGTassn,
std::vector<sim::BeamGateInfo>& firstGate) const;
};


GENIEFirst::GENIEFirst(Parameters const& p)
: EDProducer{p}
, fInputTag{p().InputTag()}
{
produces<std::vector<simb::MCTruth>>();
produces<std::vector<simb::MCFlux>>();
produces<std::vector<simb::GTruth>>();
produces< sumdata::RunData, art::InRun >();
produces< sumdata::POTSummary, art::InSubRun >();
produces<art::Assns<simb::MCTruth, simb::MCFlux>>();
produces<art::Assns<simb::MCTruth, simb::GTruth>>();
produces<std::vector<sim::BeamGateInfo>>();
}

void GENIEFirst::beginRun(art::Run& run)
{
art::Handle<sumdata::RunData> hRunData;
run.getByLabel(fInputTag, hRunData);
auto fullOscRunData = std::make_unique<sumdata::RunData>(*hRunData);
run.put(std::move(fullOscRunData), art::fullRun());
}

void GENIEFirst::endSubRun(art::SubRun& subrun)
{
art::Handle<sumdata::POTSummary> hPOT;
subrun.getByLabel(fInputTag, hPOT);
auto fullOscPOT = std::make_unique<sumdata::POTSummary>(*hPOT);
subrun.put(std::move(fullOscPOT), art::subRunFragment());
}

void GENIEFirst::produce(art::Event& evt)
{
auto firstTruth = std::make_unique<std::vector<simb::MCTruth>>();
auto firstFlux = std::make_unique<std::vector<simb::MCFlux>>();
auto firstGTruth = std::make_unique<std::vector<simb::GTruth>>();
auto firstTFassn = std::make_unique<art::Assns<simb::MCTruth, simb::MCFlux>>();
auto firstTGTassn = std::make_unique<art::Assns<simb::MCTruth, simb::GTruth>>();
auto firstGate = std::make_unique<std::vector<sim::BeamGateInfo>>();

firstosc(evt, *firstTruth, *firstFlux, *firstGTruth,
*firstTFassn, *firstTGTassn, *firstGate);

evt.put(std::move(firstTruth));
evt.put(std::move(firstFlux));
evt.put(std::move(firstGTruth));
evt.put(std::move(firstTFassn));
evt.put(std::move(firstTGTassn));
evt.put(std::move(firstGate));
}


void GENIEFirst::firstosc(art::Event& evt,
std::vector<simb::MCTruth>& firstTruth,
std::vector<simb::MCFlux>& firstFlux,
std::vector<simb::GTruth>& firstGTruth,
art::Assns<simb::MCTruth, simb::MCFlux>& firstTFassn,
art::Assns<simb::MCTruth, simb::GTruth>& firstTGTassn,
std::vector<sim::BeamGateInfo>& firstGate) const
{
art::PtrMaker<simb::MCTruth> makeTruthPtr{evt};
art::PtrMaker<simb::MCFlux> makeFluxPtr{evt};
art::PtrMaker<simb::GTruth> makeGTruthPtr{evt};

// ===== Full-osc sample =====
art::Handle<std::vector<simb::MCTruth>> hFullOscTruth;
art::Handle<std::vector<simb::MCFlux>> hFullOscFlux;
art::Handle<std::vector<simb::GTruth>> hFullOscGTruth;
art::Handle<std::vector<sim::BeamGateInfo>> hFullOscGate;

evt.getByLabel(fInputTag, hFullOscTruth);
evt.getByLabel(fInputTag, hFullOscFlux);
evt.getByLabel(fInputTag, hFullOscGTruth);
evt.getByLabel(fInputTag, hFullOscGate);

if (hFullOscTruth->size() != hFullOscFlux->size() ||
hFullOscTruth->size() != hFullOscGTruth->size()) {
throw art::Exception(art::errors::DataCorruption)
<< "GENIEFirst: full-osc collections have mismatched sizes in event "
<< evt.id() << " (MCTruth=" << hFullOscTruth->size()
<< ", MCFlux=" << hFullOscFlux->size()
<< ", GTruth=" << hFullOscGTruth->size() << ")\n";
}

if (hFullOscTruth->size() > 1) {
mf::LogWarning("GENIEFirst")
<< "Full-osc collections have " << hFullOscTruth->size()
<< " entries in event " << evt.id() << "; taking only the first one.";
}

// Take index 0 from fullosc collection
std::size_t idx0 = firstTruth.size();

firstTruth.emplace_back((*hFullOscTruth)[0]);
firstFlux.emplace_back((*hFullOscFlux)[0]);
firstGTruth.emplace_back((*hFullOscGTruth)[0]);
firstGate.emplace_back((*hFullOscGate)[0]);

// Associations
art::Ptr<simb::MCTruth> t0 = makeTruthPtr(idx0);
art::Ptr<simb::MCFlux> f0 = makeFluxPtr(idx0);
art::Ptr<simb::GTruth> g0 = makeGTruthPtr(idx0);

firstTFassn.addSingle(t0, f0);
firstTGTassn.addSingle(t0, g0);


}

}

DEFINE_ART_MODULE(evgen::GENIEFirst)
Loading