From 92aea807dc063cd0767be93c49d388dccc9d6213 Mon Sep 17 00:00:00 2001 From: Joseph Zennamo Date: Wed, 21 Jun 2023 16:16:26 -0500 Subject: [PATCH 01/12] Updated to TriggerV3 --- .../BNBRetriever/BNBRetriever_module.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc index a907341d5..db124cf36 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc @@ -20,7 +20,7 @@ #include "larcorealg/CoreUtils/counter.h" #include "artdaq-core/Data/Fragment.hh" -#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV2Fragment.hh" +#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" @@ -245,23 +245,23 @@ sbn::BNBRetriever::TriggerInfo_t sbn::BNBRetriever::extractTriggerInfo(art::Even // 2. the time of the previously triggered event, t_previous_event (NOTE: Events are non-sequential!) // 3. the number of beam spills since the previously triggered event, number_of_gates_since_previous_event - auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV2" }); + auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV3" }); TriggerInfo_t triggerInfo; for(auto raw_datum : raw_data){ uint64_t artdaq_ts = raw_datum.timestamp(); - icarus::ICARUSTriggerV2Fragment frag(raw_datum); + icarus::ICARUSTriggerV3Fragment frag(raw_datum); std::string data = frag.GetDataString(); char *buffer = const_cast(data.c_str()); - icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV2String(buffer); + icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); triggerInfo.gate_type = datastream_info.gate_type; - triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNB(); + triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNBMaj(); triggerInfo.t_current_event = static_cast(artdaq_ts)/(1000000000.0); //check this offset... if(triggerInfo.gate_type == 1) - triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNB()))/(1e9); + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()))/(1e9); else triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()))/(1000000000.0); From 021d19a4333737612ffbb5fa3594e485dae00b83 Mon Sep 17 00:00:00 2001 From: Joseph Zennamo Date: Wed, 21 Jun 2023 17:28:50 -0500 Subject: [PATCH 02/12] Updated to Trigger V3 --- .../BNBEXTRetriever_module.cc} | 41 +-- .../CMakeLists.txt | 2 +- sbncode/BeamSpillInfoRetriever/CMakeLists.txt | 3 +- .../NuMIEXTRetriever/CMakeLists.txt | 20 ++ .../NuMIEXTRetriever_module.cc | 155 +++++++++++ .../NuMIRetriever/NuMIRetriever_module.cc | 254 +++++++++--------- .../job/bnbextcountinfo.fcl | 10 + .../job/extcountinfo.fcl | 10 - .../job/numiextcountinfo.fcl | 10 + ...extinfo_sbn.fcl => run_bnbextinfo_sbn.fcl} | 10 +- .../job/run_numiextinfo_sbn.fcl | 32 +++ 11 files changed, 383 insertions(+), 164 deletions(-) rename sbncode/BeamSpillInfoRetriever/{EXTRetriever/EXTRetriever_module.cc => BNBEXTRetriever/BNBEXTRetriever_module.cc} (79%) rename sbncode/BeamSpillInfoRetriever/{EXTRetriever => BNBEXTRetriever}/CMakeLists.txt (92%) create mode 100644 sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/CMakeLists.txt create mode 100644 sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/NuMIEXTRetriever_module.cc create mode 100644 sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/extcountinfo.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/numiextcountinfo.fcl rename sbncode/BeamSpillInfoRetriever/job/{run_extinfo_sbn.fcl => run_bnbextinfo_sbn.fcl} (53%) create mode 100644 sbncode/BeamSpillInfoRetriever/job/run_numiextinfo_sbn.fcl diff --git a/sbncode/BeamSpillInfoRetriever/EXTRetriever/EXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc similarity index 79% rename from sbncode/BeamSpillInfoRetriever/EXTRetriever/EXTRetriever_module.cc rename to sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc index 95bcbf149..1d8f6bd60 100644 --- a/sbncode/BeamSpillInfoRetriever/EXTRetriever/EXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////// -// Class: EXTRetriever +// Class: BNBEXTRetriever // Plugin Type: producer -// File: EXTRetriever_module.cc +// File: BNBEXTRetriever_module.cc // // Created by hand Thurs June 24th 2021 by J. Zennamo (FNAL) // @@ -22,7 +22,7 @@ #include "larcorealg/Geometry/Exceptions.h" #include "artdaq-core/Data/Fragment.hh" -#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerUDPFragment.hh" +#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" #include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" #include "lardata/DetectorInfoServices/DetectorPropertiesService.h" @@ -36,10 +36,10 @@ #include namespace sbn { - class EXTRetriever; + class BNBEXTRetriever; } -class sbn::EXTRetriever : public art::EDProducer { +class sbn::BNBEXTRetriever : public art::EDProducer { public: struct Config { @@ -57,15 +57,15 @@ class sbn::EXTRetriever : public art::EDProducer { using Parameters = art::EDProducer::Table; - explicit EXTRetriever(Parameters const& params); + explicit BNBEXTRetriever(Parameters const& params); // The compiler-generated destructor is fine for non-base // classes without bare pointers or other resource use. // Plugins should not be copied or assigned. - EXTRetriever(EXTRetriever const&) = delete; - EXTRetriever(EXTRetriever&&) = delete; - EXTRetriever& operator=(EXTRetriever const&) = delete; - EXTRetriever& operator=(EXTRetriever&&) = delete; + BNBEXTRetriever(BNBEXTRetriever const&) = delete; + BNBEXTRetriever(BNBEXTRetriever&&) = delete; + BNBEXTRetriever& operator=(BNBEXTRetriever const&) = delete; + BNBEXTRetriever& operator=(BNBEXTRetriever&&) = delete; // Required functions. void produce(art::Event& e) override; @@ -82,7 +82,7 @@ class sbn::EXTRetriever : public art::EDProducer { }; -sbn::EXTRetriever::EXTRetriever(Parameters const& params) +sbn::BNBEXTRetriever::BNBEXTRetriever(Parameters const& params) : EDProducer{params}, raw_data_label_(params().RawDataLabel()) { @@ -91,7 +91,7 @@ sbn::EXTRetriever::EXTRetriever(Parameters const& params) TotalEXTCounts = 0; } -void sbn::EXTRetriever::produce(art::Event& e) +void sbn::BNBEXTRetriever::produce(art::Event& e) { //Here we read in the artdaq Fragments and extract three pieces of information: @@ -100,19 +100,22 @@ void sbn::EXTRetriever::produce(art::Event& e) // 3. the number of beam spills since the previously triggered event, number_of_gates_since_previous_event int gate_type = 0; - auto const & raw_data = e.getProduct< std::vector >({ raw_data_label_, "ICARUSTriggerUDP" }); + auto const & raw_data = e.getProduct< std::vector >({ raw_data_label_, "ICARUSTriggerV3" }); unsigned int number_of_gates_since_previous_event = 0; for(auto raw_datum : raw_data){ - icarus::ICARUSTriggerUDPFragment frag(raw_datum); + icarus::ICARUSTriggerV3Fragment frag(raw_datum); std::string data = frag.GetDataString(); char *buffer = const_cast(data.c_str()); - icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerString(buffer); + icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); gate_type = datastream_info.gate_type; - number_of_gates_since_previous_event = frag.getDeltaGatesBNB(); + number_of_gates_since_previous_event = frag.getDeltaGatesBNBOffMaj(); + std::cout << "BNB OFF MAJ : " << frag.getDeltaGatesBNBOffMaj() << std::endl; + std::cout << "NuMI OFF MAJ : " << frag.getDeltaGatesNuMIOffMaj() << std::endl; + } //We only want to process EXT gates, i.e. type 3 @@ -135,13 +138,13 @@ void sbn::EXTRetriever::produce(art::Event& e) } //end loop over events -void sbn::EXTRetriever::beginSubRun(art::SubRun& sr) +void sbn::BNBEXTRetriever::beginSubRun(art::SubRun& sr) { return; } //____________________________________________________________________________ -void sbn::EXTRetriever::endSubRun(art::SubRun& sr) +void sbn::BNBEXTRetriever::endSubRun(art::SubRun& sr) { // We will add all of the EXTCountInfo data-products to the // art::SubRun so it persists @@ -152,4 +155,4 @@ void sbn::EXTRetriever::endSubRun(art::SubRun& sr) return; } -DEFINE_ART_MODULE(sbn::EXTRetriever) +DEFINE_ART_MODULE(sbn::BNBEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/EXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt similarity index 92% rename from sbncode/BeamSpillInfoRetriever/EXTRetriever/CMakeLists.txt rename to sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt index 316419a52..1c49f8646 100644 --- a/sbncode/BeamSpillInfoRetriever/EXTRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt @@ -1,5 +1,5 @@ -cet_build_plugin(EXTRetriever art::module +cet_build_plugin(BNBEXTRetriever art::module LIBRARIES art::Persistency_Common art::Utilities canvas::canvas diff --git a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt index 1a6fecf80..0caadf2a5 100644 --- a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt @@ -1,6 +1,7 @@ add_subdirectory(BNBRetriever) add_subdirectory(NuMIRetriever) -add_subdirectory(EXTRetriever) +add_subdirectory(BNBEXTRetriever) +add_subdirectory(NuMIEXTRetriever) add_subdirectory(job) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/CMakeLists.txt new file mode 100644 index 000000000..f11657b00 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/CMakeLists.txt @@ -0,0 +1,20 @@ + +cet_build_plugin(NuMIEXTRetriever art::module + LIBRARIES + art::Persistency_Common + art::Utilities canvas::canvas + cetlib::cetlib cetlib_except::cetlib_except + messagefacility::MF_MessageLogger + sbnobj::Common_POTAccounting + sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_Common + sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays + sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_ICARUS + artdaq_core::artdaq-core_Utilities + lardata::Utilities + larcore::Geometry_AuxDetGeometry_service +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/NuMIEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/NuMIEXTRetriever_module.cc new file mode 100644 index 000000000..7fdc974e2 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/NuMIEXTRetriever/NuMIEXTRetriever_module.cc @@ -0,0 +1,155 @@ +//////////////////////////////////////////////////////////////////////// +// Class: NuMIEXTRetriever +// Plugin Type: producer +// File: NuMIEXTRetriever_module.cc +// +// Created by hand Thurs June 24th 2021 by J. Zennamo (FNAL) +// +//////////////////////////////////////////////////////////////////////// + +#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 "canvas/Utilities/InputTag.h" +#include "fhiclcpp/types/Atom.h" +#include "messagefacility/MessageLogger/MessageLogger.h" +#include "lardata/Utilities/AssociationUtil.h" +#include "lardataobj/Utilities/sparse_vector.h" +#include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" +#include "larcorealg/Geometry/Exceptions.h" + +#include "artdaq-core/Data/Fragment.hh" +#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" + +#include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" +#include "lardata/DetectorInfoServices/DetectorPropertiesService.h" +#include "sbnobj/Common/POTAccounting/EXTCountInfo.h" + +#include +#include +#include +#include +#include +#include + +namespace sbn { + class NuMIEXTRetriever; +} + +class sbn::NuMIEXTRetriever : public art::EDProducer { +public: + + struct Config { + + using Name = fhicl::Name; + using Comment = fhicl::Comment; + + fhicl::Atom RawDataLabel { + Name{ "raw_data_label" }, + Comment{ "art data product instance name for trigger information (product label is 'daq')" } + }; + + }; // Config + + using Parameters = art::EDProducer::Table; + + + explicit NuMIEXTRetriever(Parameters const& params); + // The compiler-generated destructor is fine for non-base + // classes without bare pointers or other resource use. + + // Plugins should not be copied or assigned. + NuMIEXTRetriever(NuMIEXTRetriever const&) = delete; + NuMIEXTRetriever(NuMIEXTRetriever&&) = delete; + NuMIEXTRetriever& operator=(NuMIEXTRetriever const&) = delete; + NuMIEXTRetriever& operator=(NuMIEXTRetriever&&) = delete; + + // Required functions. + void produce(art::Event& e) override; + void beginSubRun(art::SubRun& sr) override; + void endSubRun(art::SubRun& sr) override; + +private: + std::vector< sbn::EXTCountInfo > fOutExtInfos; + + // input labels + std::string raw_data_label_; + int TotalEXTCounts; + +}; + + +sbn::NuMIEXTRetriever::NuMIEXTRetriever(Parameters const& params) + : EDProducer{params}, + raw_data_label_(params().RawDataLabel()) +{ + + produces< std::vector< sbn::EXTCountInfo >, art::InSubRun >(); + TotalEXTCounts = 0; +} + +void sbn::NuMIEXTRetriever::produce(art::Event& e) +{ + + //Here we read in the artdaq Fragments and extract three pieces of information: + // 1. The time of the current event, t_current_event + // 2. the time of the previously triggered event, t_previous_event (NOTE: Events are non-sequential!) + // 3. the number of beam spills since the previously triggered event, number_of_gates_since_previous_event + + //int gate_type = 0; + auto const & raw_data = e.getProduct< std::vector >({ raw_data_label_, "ICARUSTriggerV3" }); + + unsigned int number_of_gates_since_previous_event = 0; + + for(auto raw_datum : raw_data){ + + icarus::ICARUSTriggerV3Fragment frag(raw_datum); + std::string data = frag.GetDataString(); + char *buffer = const_cast(data.c_str()); + icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); + //gate_type = datastream_info.gate_type; + number_of_gates_since_previous_event = frag.getDeltaGatesNuMIOffMaj(); + + + } + + //We only want to process EXT gates, i.e. type 3 + + // Keep track of the number of beam gates the DAQ thinks + // are in this file + TotalEXTCounts += number_of_gates_since_previous_event; + + //Store everything in our data-product + sbn::EXTCountInfo extInfo; + extInfo.gates_since_last_trigger = number_of_gates_since_previous_event; + + fOutExtInfos.push_back(extInfo); + // We do not write these to the art::Events because + // we can filter events but want to keep all the POT + // information, so we'll write it to the SubRun + + +} //end loop over events + + +void sbn::NuMIEXTRetriever::beginSubRun(art::SubRun& sr) +{ + return; +} + +//____________________________________________________________________________ +void sbn::NuMIEXTRetriever::endSubRun(art::SubRun& sr) +{ + // We will add all of the EXTCountInfo data-products to the + // art::SubRun so it persists + auto p = std::make_unique< std::vector< sbn::EXTCountInfo > >(fOutExtInfos); + + sr.put(std::move(p)); + + return; +} + +DEFINE_ART_MODULE(sbn::NuMIEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc index 84e4c6d03..a0eb1e48a 100644 --- a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc @@ -19,7 +19,7 @@ #include "larcorealg/Geometry/Exceptions.h" #include "artdaq-core/Data/Fragment.hh" -#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV2Fragment.hh" +#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" #include "lardataalg/DetectorInfo/DetectorPropertiesStandard.h" #include "lardata/DetectorInfoServices/DetectorPropertiesService.h" @@ -104,9 +104,9 @@ void sbn::NuMIRetriever::produce(art::Event &e) // TODO: long-term goal -- can we fix this? if (e.event() == 1) return; - int gate_type = 0; + // int gate_type = 0; art::Handle< std::vector > raw_data_ptr; - e.getByLabel(raw_data_label_, "ICARUSTriggerV2", raw_data_ptr); + e.getByLabel(raw_data_label_, "ICARUSTriggerV3", raw_data_ptr); auto const & raw_data = (*raw_data_ptr); double t_current_event = 0; @@ -116,145 +116,143 @@ void sbn::NuMIRetriever::produce(art::Event &e) for(auto raw_datum : raw_data){ uint64_t artdaq_ts = raw_datum.timestamp(); - icarus::ICARUSTriggerV2Fragment frag(raw_datum); + icarus::ICARUSTriggerV3Fragment frag(raw_datum); std::string data = frag.GetDataString(); char *buffer = const_cast(data.c_str()); - icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV2String(buffer); - gate_type = datastream_info.gate_type; - number_of_gates_since_previous_event = frag.getDeltaGatesNuMI(); + icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); + //gate_type = datastream_info.gate_type; + number_of_gates_since_previous_event = frag.getDeltaGatesNuMIMaj(); t_current_event = static_cast(artdaq_ts)/(1000000000.); //check this offset... - if(gate_type == 2) - t_previous_event = (static_cast(frag.getLastTimestampNuMI()))/(1000000000.); - else - t_previous_event = (static_cast(frag.getLastTimestampOther()))/(1000000000.); + + t_previous_event = (static_cast(frag.getLastTimestampNuMIMaj()))/(1000000000.); + } std::cout << std::setprecision(19) << "Previous : " << t_previous_event << ", Current : " << t_current_event << std::endl; //We only want to process NuMI gates, i.e. type 2 - if(gate_type == 2) - { - // Keep track of the number of beam gates the DAQ thinks - // are in this job - TotalBeamSpills += number_of_gates_since_previous_event; + + // Keep track of the number of beam gates the DAQ thinks + // are in this job + TotalBeamSpills += number_of_gates_since_previous_event; + + // Fill up the BFP cache with times starting at the previous event + // + // If the difference in time between events is bigger than the fcl-provided TimeWindow, then + // we won't be able to fill up the cache to be big enough. In that case, resize the window as + // necessary. + double this_window_size = t_current_event - t_previous_event + 2*fTimePad; + if (this_window_size > fTimeWindow) { + std::cout << "Resizing time window from: " << fTimeWindow << " to: " << this_window_size << std::endl; + fTimeWindow = this_window_size; + MakeBFP(); + } + + // DO NOT CHANGE THESE LINES OR THEIR ORDER + // If you really think you need to, please reach out to grayputnam uchicago.edu + bfp->FillCache(t_current_event + fTimePad); + bfp->FillCache(t_previous_event - fTimePad); + std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); + + int spill_count = 0; + // Iterating through each of the beamline times + for (size_t i = 0; i < times_temps.size(); i++) { - // Fill up the BFP cache with times starting at the previous event - // - // If the difference in time between events is bigger than the fcl-provided TimeWindow, then - // we won't be able to fill up the cache to be big enough. In that case, resize the window as - // necessary. - double this_window_size = t_current_event - t_previous_event + 2*fTimePad; - if (this_window_size > fTimeWindow) { - std::cout << "Resizing time window from: " << fTimeWindow << " to: " << this_window_size << std::endl; - fTimeWindow = this_window_size; - MakeBFP(); + // Only continue if these times are matched to our DAQ time + // plus or minus some time padding, currently using 3.3 ms + // which is half the Booster Rep Rate + if(e.event() != 1){//We already addressed the "first event" above + if(times_temps[i] > t_current_event){continue;} + if(times_temps[i] <= t_previous_event){continue;} } - - // DO NOT CHANGE THESE LINES OR THEIR ORDER - // If you really think you need to, please reach out to grayputnam uchicago.edu - bfp->FillCache(t_current_event + fTimePad); - bfp->FillCache(t_previous_event - fTimePad); - std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); - - int spill_count = 0; - // Iterating through each of the beamline times - for (size_t i = 0; i < times_temps.size(); i++) { - - // Only continue if these times are matched to our DAQ time - // plus or minus some time padding, currently using 3.3 ms - // which is half the Booster Rep Rate - if(e.event() != 1){//We already addressed the "first event" above - if(times_temps[i] > t_current_event){continue;} - if(times_temps[i] <= t_previous_event){continue;} - } - - //count found spills - spill_count++; - - //initialize all devices found in NuMISpillInfo.h in sbnobj - double HRNDIR = -1.; - double NSLINA = -1.; - double NSLINB = -1.; - double NSLINC = -1.; - double NSLIND = -1.; - double TOR101 = -1.; - double TORTGT = -1.; - double TR101D = -1.; - double TRTGTD = -1.; - std::vector< double > HP121; - std::vector< double > VP121; - std::vector< double > HPTGT; - std::vector< double > VPTGT; - std::vector< double > HITGT; - std::vector< double > VITGT; - std::vector< double > MTGTDS; - double TRTGTD_time = -1.; - std::cout << "Grabbing IFBeam info!" << std::endl; - try{bfp->GetNamedData(times_temps[i], "E:TRTGTD@",&TRTGTD,&TRTGTD_time);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:TR101D",&TR101D);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:HRNDIR",&HRNDIR);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:NSLINA",&NSLINA);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:NSLINB",&NSLINB);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:NSLINC",&NSLINC);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:NSLIND",&NSLIND);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:TOR101",&TOR101);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(times_temps[i], "E:TORTGT",&TORTGT);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - // BPM Positions and Intensities - they each have 7 elements - // First is an average value of a few batches (often 2,3,4) - // used for auto-tuning, so we should disregard it - - try{HP121 = bfp->GetNamedVector(times_temps[i], "E:HP121[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} - try{VP121 = bfp->GetNamedVector(times_temps[i], "E:VP121[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} - try{HPTGT = bfp->GetNamedVector(times_temps[i], "E:HPTGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} - try{VPTGT = bfp->GetNamedVector(times_temps[i], "E:VPTGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} - try{HITGT = bfp->GetNamedVector(times_temps[i], "E:HITGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} - try{VITGT = bfp->GetNamedVector(times_temps[i], "E:VITGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} - try{MTGTDS = bfp->GetNamedVector(times_temps[i], "E:MTGTDS[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} - - std::cout << "Finished getting IFBeam info" << std::endl; - std::cout << "BFP Time: " << times_temps[i] << " TOROID Time: " << TRTGTD_time << " TOROID COUNT: " << TRTGTD << std::endl; - unsigned long int time_closest_int = (int) TRTGTD_time; - double time_closest_ns = (TRTGTD_time - time_closest_int)*1000000000; - - sbn::NuMISpillInfo NuMIbeamInfo; - NuMIbeamInfo.TORTGT = TORTGT*1e12; //include factor of 1e12 protons in POT calculation - NuMIbeamInfo.TOR101 = TOR101*1e12; //include factor of 1e12 protons in POT calculation - NuMIbeamInfo.TRTGTD = TRTGTD*1e12; //include factor of 1e12 protons in POT calculation - NuMIbeamInfo.TR101D = TR101D*1e12; //include factor of 1e12 protons in POT calculation - NuMIbeamInfo.HRNDIR = HRNDIR; - NuMIbeamInfo.NSLINA = NSLINA; - NuMIbeamInfo.NSLINB = NSLINB; - NuMIbeamInfo.NSLINC = NSLINC; - NuMIbeamInfo.NSLIND = NSLIND; - - NuMIbeamInfo.HP121 = HP121; - NuMIbeamInfo.VP121 = VP121; - NuMIbeamInfo.HPTGT = HPTGT; - NuMIbeamInfo.VPTGT = VPTGT; - NuMIbeamInfo.HITGT = HITGT; - NuMIbeamInfo.VITGT = VITGT; - NuMIbeamInfo.MTGTDS = MTGTDS; - - NuMIbeamInfo.time = times_temps[i]; - NuMIbeamInfo.event = e.event(); - NuMIbeamInfo.spill_time_s = time_closest_int; - NuMIbeamInfo.spill_time_ns = time_closest_ns; + + //count found spills + spill_count++; + + //initialize all devices found in NuMISpillInfo.h in sbnobj + double HRNDIR = -1.; + double NSLINA = -1.; + double NSLINB = -1.; + double NSLINC = -1.; + double NSLIND = -1.; + double TOR101 = -1.; + double TORTGT = -1.; + double TR101D = -1.; + double TRTGTD = -1.; + std::vector< double > HP121; + std::vector< double > VP121; + std::vector< double > HPTGT; + std::vector< double > VPTGT; + std::vector< double > HITGT; + std::vector< double > VITGT; + std::vector< double > MTGTDS; + double TRTGTD_time = -1.; + std::cout << "Grabbing IFBeam info!" << std::endl; + try{bfp->GetNamedData(times_temps[i], "E:TRTGTD@",&TRTGTD,&TRTGTD_time);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:TR101D",&TR101D);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:HRNDIR",&HRNDIR);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:NSLINA",&NSLINA);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:NSLINB",&NSLINB);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:NSLINC",&NSLINC);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:NSLIND",&NSLIND);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:TOR101",&TOR101);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(times_temps[i], "E:TORTGT",&TORTGT);}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + // BPM Positions and Intensities - they each have 7 elements + // First is an average value of a few batches (often 2,3,4) + // used for auto-tuning, so we should disregard it + + try{HP121 = bfp->GetNamedVector(times_temps[i], "E:HP121[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " << "got exception: " << we.what() << "\n";} + try{VP121 = bfp->GetNamedVector(times_temps[i], "E:VP121[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} + try{HPTGT = bfp->GetNamedVector(times_temps[i], "E:HPTGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} + try{VPTGT = bfp->GetNamedVector(times_temps[i], "E:VPTGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} + try{HITGT = bfp->GetNamedVector(times_temps[i], "E:HITGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} + try{VITGT = bfp->GetNamedVector(times_temps[i], "E:VITGT[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} + try{MTGTDS = bfp->GetNamedVector(times_temps[i], "E:MTGTDS[]");}catch (WebAPIException &we) {std::cout << "At time : " << times_temps[i] << " " <<"got exception: " << we.what() << "\n";} + + std::cout << "Finished getting IFBeam info" << std::endl; + std::cout << "BFP Time: " << times_temps[i] << " TOROID Time: " << TRTGTD_time << " TOROID COUNT: " << TRTGTD << std::endl; + unsigned long int time_closest_int = (int) TRTGTD_time; + double time_closest_ns = (TRTGTD_time - time_closest_int)*1000000000; + + sbn::NuMISpillInfo NuMIbeamInfo; + NuMIbeamInfo.TORTGT = TORTGT*1e12; //include factor of 1e12 protons in POT calculation + NuMIbeamInfo.TOR101 = TOR101*1e12; //include factor of 1e12 protons in POT calculation + NuMIbeamInfo.TRTGTD = TRTGTD*1e12; //include factor of 1e12 protons in POT calculation + NuMIbeamInfo.TR101D = TR101D*1e12; //include factor of 1e12 protons in POT calculation + NuMIbeamInfo.HRNDIR = HRNDIR; + NuMIbeamInfo.NSLINA = NSLINA; + NuMIbeamInfo.NSLINB = NSLINB; + NuMIbeamInfo.NSLINC = NSLINC; + NuMIbeamInfo.NSLIND = NSLIND; + + NuMIbeamInfo.HP121 = HP121; + NuMIbeamInfo.VP121 = VP121; + NuMIbeamInfo.HPTGT = HPTGT; + NuMIbeamInfo.VPTGT = VPTGT; + NuMIbeamInfo.HITGT = HITGT; + NuMIbeamInfo.VITGT = VITGT; + NuMIbeamInfo.MTGTDS = MTGTDS; + + NuMIbeamInfo.time = times_temps[i]; + NuMIbeamInfo.event = e.event(); + NuMIbeamInfo.spill_time_s = time_closest_int; + NuMIbeamInfo.spill_time_ns = time_closest_ns; // Save the Number of DAQ Gates in the first saved spill - if (spill_count == 1) { - NuMIbeamInfo.daq_gates = number_of_gates_since_previous_event; - } - else { - NuMIbeamInfo.daq_gates = 0; + if (spill_count == 1) { + NuMIbeamInfo.daq_gates = number_of_gates_since_previous_event; } - - fOutbeamInfos.push_back(NuMIbeamInfo); + else { + NuMIbeamInfo.daq_gates = 0; } - if(spill_count > number_of_gates_since_previous_event) - std::cout << "Event Spills : " << spill_count << ", DAQ Spills : " << number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; - else - std::cout << "Event Spills : " << spill_count << ", DAQ Spills : " << number_of_gates_since_previous_event << std::endl; - } + + fOutbeamInfos.push_back(NuMIbeamInfo); + } + if(spill_count > number_of_gates_since_previous_event) + std::cout << "Event Spills : " << spill_count << ", DAQ Spills : " << number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; + else + std::cout << "Event Spills : " << spill_count << ", DAQ Spills : " << number_of_gates_since_previous_event << std::endl; + } void sbn::NuMIRetriever::beginSubRun(art::SubRun& sr) diff --git a/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl new file mode 100644 index 000000000..e8191e2dc --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl @@ -0,0 +1,10 @@ + +BEGIN_PROLOG + +bnbextcountinfo: { + + module_type: "BNBEXTRetriever" + raw_data_label: "daq" + +} +END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/extcountinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/extcountinfo.fcl deleted file mode 100644 index 1ca870996..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/extcountinfo.fcl +++ /dev/null @@ -1,10 +0,0 @@ - -BEGIN_PROLOG - -extcountinfo: { - - module_type: "EXTRetriever" - raw_data_label: "daq" - -} -END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/numiextcountinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/numiextcountinfo.fcl new file mode 100644 index 000000000..f9a9583f5 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/numiextcountinfo.fcl @@ -0,0 +1,10 @@ + +BEGIN_PROLOG + +numiextcountinfo: { + + module_type: "NuMIEXTRetriever" + raw_data_label: "daq" + +} +END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/run_extinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl similarity index 53% rename from sbncode/BeamSpillInfoRetriever/job/run_extinfo_sbn.fcl rename to sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl index e55cb29fe..fbee5871f 100644 --- a/sbncode/BeamSpillInfoRetriever/job/run_extinfo_sbn.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl @@ -1,6 +1,6 @@ -#include "extcountinfo.fcl" +#include "bnbextcountinfo.fcl" -process_name: EXTInfoGen +process_name: BNBEXTInfoGen services:{ IFBeam:{} @@ -13,10 +13,10 @@ source: { physics: { producers: { - extinfo: @local::extcountinfo + bnbextinfo: @local::bnbextcountinfo } - simulate: [extinfo ] + simulate: [bnbextinfo ] stream1: [ out1 ] } @@ -24,7 +24,7 @@ physics: { outputs: { out1: { module_type: RootOutput - fileName: "%ifb_%tc_extinfo.root" + fileName: "%ifb_%tc_bnbextinfo.root" dataTier: "raw" compressionLevel: 1 } diff --git a/sbncode/BeamSpillInfoRetriever/job/run_numiextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_numiextinfo_sbn.fcl new file mode 100644 index 000000000..e41261cda --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/run_numiextinfo_sbn.fcl @@ -0,0 +1,32 @@ +#include "numiextcountinfo.fcl" + +process_name: NuMIEXTInfoGen + +services:{ + IFBeam:{} +} + + +source: { + +} + +physics: { + producers: { + numiextinfo: @local::numiextcountinfo + } + + simulate: [numiextinfo ] + stream1: [ out1 ] + +} + +outputs: { + out1: { + module_type: RootOutput + fileName: "%ifb_%tc_numiextinfo.root" + dataTier: "raw" + compressionLevel: 1 + } +} + From 93935e720eee7b8ee46528e244dcd83a8137ef5a Mon Sep 17 00:00:00 2001 From: jzennamo Date: Mon, 18 Sep 2023 14:36:25 -0500 Subject: [PATCH 03/12] Taking into account MinBias Triggers --- .../BNBEXTRetriever/BNBEXTRetriever_module.cc | 10 +- .../BNBRetriever/BNBRetriever_module.cc | 153 +++++++++++++++--- .../BNBRetriever/CMakeLists.txt | 1 + .../job/bnbspillinfo.fcl | 2 +- 4 files changed, 142 insertions(+), 24 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc index 1d8f6bd60..d3b847df9 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc @@ -77,7 +77,8 @@ class sbn::BNBEXTRetriever : public art::EDProducer { // input labels std::string raw_data_label_; - int TotalEXTCounts; + float TotalEXTCounts; + float scale_factor; }; @@ -89,6 +90,7 @@ sbn::BNBEXTRetriever::BNBEXTRetriever(Parameters const& params) produces< std::vector< sbn::EXTCountInfo >, art::InSubRun >(); TotalEXTCounts = 0; + scale_factor = 0; } void sbn::BNBEXTRetriever::produce(art::Event& e) @@ -112,9 +114,9 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); gate_type = datastream_info.gate_type; number_of_gates_since_previous_event = frag.getDeltaGatesBNBOffMaj(); - + scale_factor = 1. - (1./frag.getDeltaGatesBNBOffMinbias()); std::cout << "BNB OFF MAJ : " << frag.getDeltaGatesBNBOffMaj() << std::endl; - std::cout << "NuMI OFF MAJ : " << frag.getDeltaGatesNuMIOffMaj() << std::endl; + std::cout << "Scale Factor : " << scale_factor << std::endl; } @@ -123,7 +125,7 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) { // Keep track of the number of beam gates the DAQ thinks // are in this file - TotalEXTCounts += number_of_gates_since_previous_event; + TotalEXTCounts += number_of_gates_since_previous_event*scale_factor; //Store everything in our data-product sbn::EXTCountInfo extInfo; diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc index db124cf36..e2bc1feb7 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc @@ -12,7 +12,7 @@ #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/Run.h" #include "art/Framework/Principal/SubRun.h" #include "canvas/Utilities/InputTag.h" #include "fhiclcpp/types/Atom.h" @@ -21,6 +21,7 @@ #include "artdaq-core/Data/Fragment.hh" #include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" +#include "sbnobj/Common/Trigger/ExtraTriggerInfo.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" @@ -32,6 +33,10 @@ #include #include +#include +#include +#include + namespace sbn { class BNBRetriever; } @@ -111,6 +116,7 @@ class sbn::BNBRetriever : public art::EDProducer { double fTimePad; std::string fURL; MWRData mwrdata; + int run_number; std::string raw_data_label; std::string fDeviceUsedForTiming; unsigned int TotalBeamSpills; @@ -118,12 +124,17 @@ class sbn::BNBRetriever : public art::EDProducer { art::ServiceHandle ifbeam_handle; std::unique_ptr bfp; std::unique_ptr bfp_mwr; + + // + sqlite3 *db; + int rc; struct TriggerInfo_t { int gate_type = 0; ///< Source of the spill: `1`: BNB, `2`: NuMI double t_current_event = 0; double t_previous_event = 0; unsigned int number_of_gates_since_previous_event = 0; // FIXME needs to be integral type + std::int64_t WR_to_Spill_conversion = 0; }; struct MWRdata_t { @@ -171,8 +182,70 @@ class sbn::BNBRetriever : public art::EDProducer { sbn::BNBSpillInfo makeBNBSpillInfo (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; +/** + * @brief SQLite callback function for retrieving trigger_type from a query. + * @param data Pointer to the integer where the trigger type will be stored. + * @param argc Count of the number of columns returned by the query. + * @param argv Array of c-strings containing the column data of the query. + * @param columns Array of c-strings listing the names of the columns. + * @return 0 if successful. + */ +// int callback_trigger_type(void *data, +// int argc, +// char **argv, +// char **columns); + +/** + * @brief Queries the trigger database and finds the trigger_type of the matching trigger (if any). + * @param db The pointer to the SQLite database instance. + * @param run The run number of the current event (helps with queries). + * @param gate_time The time in milliseconds of the gate. + * @param threshold The required absolute time difference between gate and trigger. + * @return trigger_type -1: No matching trigger, 0: Majority, 1: MinBias + */ + int get_trigger_type_matching_gate(sqlite3 *db, + int func(void*,int,char**,char**), + int run, + long long int gate_time, + int threshold) const; + }; +int callback_trigger_type(void *data, int argc, char **argv, char **columns) +{ + int *result = static_cast(data); + // Does this query return non-NULL values? + if(argc > 0 && argv[0]) + *result = std::stoi(argv[0]); + else + *result = -1; + + return 0; +} + +int sbn::BNBRetriever::get_trigger_type_matching_gate(sqlite3 *db, int func(void*,int,char**,char**), int run, long long int gate_time, int threshold) const +{ + int trigger_type(-1), query_status; + std::stringstream query; + query << "SELECT trigger_type FROM triggerdata WHERE gate_type=1 AND run_number =" + << run + << " AND ABS(1000000000*wr_seconds + wr_nanoseconds - " + << std::fixed << gate_time + << ") < " + << threshold*1000000 + << " ORDER BY ABS(1000000000*wr_seconds + wr_nanoseconds - " + << std::fixed << gate_time + << ") LIMIT 1;"; + + query_status = sqlite3_exec(db, query.str().c_str(), func, &trigger_type, NULL); + if (query_status != SQLITE_OK) + { + fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); + trigger_type = -1; + } + return trigger_type; +} + sbn::BNBRetriever::BNBRetriever(Parameters const& params) : EDProducer{params}, fTimePad(params().TimePadding()), @@ -201,6 +274,13 @@ sbn::BNBRetriever::BNBRetriever(Parameters const& params) bfp_mwr->setValidWindow(3605); produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); TotalBeamSpills = 0; + + rc = sqlite3_open("/pnfs/icarus/scratch/users/mueller/icarus_triggers.db", &db); + if(rc) + { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + } + } @@ -213,8 +293,13 @@ void sbn::BNBRetriever::produce(art::Event& e) // spill information // // TODO: long-term goal -- can we fix this? + // FIXME This is wrong.... + // Need to use: ICARUSTriggerV3Fragment long getTotalTriggerBNBMaj() const + if (e.event() == 1) return; + run_number = e.id().run(); + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); //We only want to process BNB gates, i.e. type 1 @@ -232,8 +317,8 @@ void sbn::BNBRetriever::produce(art::Event& e) if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) mf::LogDebug("BNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; - else - mf::LogDebug("BNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; + else + mf::LogDebug("BNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; }//end iteration over art::Events @@ -246,9 +331,12 @@ sbn::BNBRetriever::TriggerInfo_t sbn::BNBRetriever::extractTriggerInfo(art::Even // 3. the number of beam spills since the previously triggered event, number_of_gates_since_previous_event auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV3" }); - - TriggerInfo_t triggerInfo; + auto const & extraTrigInfo = e.getProduct< sbn::ExtraTriggerInfo >("daqTrigger"); + TriggerInfo_t triggerInfo; + + triggerInfo.WR_to_Spill_conversion = extraTrigInfo.WRtimeToTriggerTime; + for(auto raw_datum : raw_data){ uint64_t artdaq_ts = raw_datum.timestamp(); @@ -258,16 +346,16 @@ sbn::BNBRetriever::TriggerInfo_t sbn::BNBRetriever::extractTriggerInfo(art::Even icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); triggerInfo.gate_type = datastream_info.gate_type; triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNBMaj(); - - triggerInfo.t_current_event = static_cast(artdaq_ts)/(1000000000.0); //check this offset... + + triggerInfo.t_current_event = static_cast(artdaq_ts-3.6e7)/(1000000000.0); //check this offset... if(triggerInfo.gate_type == 1) - triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()))/(1e9); + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); else - triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()))/(1000000000.0); + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()-3.6e7))/(1000000000.0); } - mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Previous : " << triggerInfo.t_previous_event << ", Current : " << triggerInfo.t_current_event << std::endl; + mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Previous : " << triggerInfo.t_previous_event << ", Current : " << triggerInfo.t_current_event << ", Spill Count " << triggerInfo.number_of_gates_since_previous_event << std::endl; return triggerInfo; } @@ -277,10 +365,10 @@ sbn::BNBRetriever::MWRdata_t sbn::BNBRetriever::extractSpillTimes(TriggerInfo_t // These lines get everything primed within the IFBeamDB // They seem redundant but they are needed - try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_previous_event)-fTimePad,"E:THCURR");} catch (WebAPIException &we) {} + // try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_previous_event)-fTimePad,"E:THCURR");} catch (WebAPIException &we) {} + try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {} try{auto packed_M876BB_temp = bfp_mwr->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:M875BB{4440:888}.RAW");} catch (WebAPIException &we) {} - //The multiwire chambers provide their // data in a vector format but we'll have // to sort through it in std::string format @@ -317,7 +405,7 @@ sbn::BNBRetriever::MWRdata_t sbn::BNBRetriever::extractSpillTimes(TriggerInfo_t //Make sure we have a device if(var.empty()){ - mf::LogDebug("BNBRetriever") << " NO MWR DEVICES?!" << std::endl; + //mf::LogDebug("BNBRetriever") << " NO MWR DEVICES?!" << std::endl; continue; } /// Check the device name and interate the double-vector index @@ -325,7 +413,7 @@ sbn::BNBRetriever::MWRdata_t sbn::BNBRetriever::extractSpillTimes(TriggerInfo_t else if(var.find("M876BB") != std::string::npos ) dev = 1; else if(var.find("MMBTBB") != std::string::npos ) dev = 2; else{ - mf::LogDebug("BNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; + //mf::LogDebug("BNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; continue;} time_for_mwr = 0; @@ -404,9 +492,12 @@ int sbn::BNBRetriever::matchMultiWireData( // we have to pick a specific variable to use std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); + mf::LogDebug("BNBRetriever") << "matchMultiWireData:: Number of time spills : " << times_temps.size() << std::endl; + // We'll keep track of how many of these spills match to our // DAQ trigger times int spill_count = 0; + int spills_removed = 0; std::vector matched_MWR; matched_MWR.resize(3); @@ -434,20 +525,42 @@ int sbn::BNBRetriever::matchMultiWireData( // Remove the spills before the start of our Run times_temps.erase(times_temps.begin(), times_temps.end() - std::min(int(triggerInfo.number_of_gates_since_previous_event), int(times_temps.size()))); - + }//end fix for "first event" + ///reject time_stamps which have a trigger_type == 1 from data-base + //To-Do + + // mf::LogDebug("BNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; + // mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; + // mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Lower Limit : " << (triggerInfo.t_previous_event)+fTimePad << std::endl; + // Iterating through each of the beamline times for (size_t i = 0; i < times_temps.size(); i++) { // Only continue if these times are matched to our DAQ time - + //mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; + if(!isFirstEventInRun){//We already addressed the "first event" above - if(times_temps[i] > (triggerInfo.t_current_event)){continue;} - if(times_temps[i] <= (triggerInfo.t_previous_event)){continue;} + if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ + //mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + spills_removed++; + continue;} + if(times_temps[i] <= (triggerInfo.t_previous_event)+fTimePad){ + spills_removed++; + //mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + continue;} } + //check if this spill is is minbias + mf::LogDebug("BNBRetriever") << std::setprecision(19) << "matchMultiWireData:: trigger type : " << get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+3.6e7, 40.) << " times : spill " << times_temps[i]*1.e9 << " - " << triggerInfo.WR_to_Spill_conversion << " + " << 3.6e7 << std::endl; + if(get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+3.6e7, 40.) == 1){ + mf::LogDebug("BNBRetriever") << std::setprecision(19) << "matchMultiWireData:: Skipped a MinBias gate at : " << times_temps[i]*1000. << std::endl; + + continue; + } + //Great we found a matched spill! Let's count it spill_count++; @@ -470,7 +583,7 @@ int sbn::BNBRetriever::matchMultiWireData( for (size_t j = 0; j < times_temps.size(); j++) { if( j == i) continue; if(times_temps[j] > (triggerInfo.t_current_event+fTimePad)){continue;} - if(times_temps[j] <= (triggerInfo.t_previous_event-fTimePad)){continue;} + if(times_temps[j] <= (triggerInfo.t_previous_event+fTimePad)){continue;} //is there a better match later in the spill sequence if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < @@ -501,6 +614,8 @@ int sbn::BNBRetriever::matchMultiWireData( }//end iteration over beam device times + // mf::LogDebug("BNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; + return spill_count; } diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt index f6ec367a4..a34b60d6a 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt @@ -18,6 +18,7 @@ cet_build_plugin(BNBRetriever art::module messagefacility::MF_MessageLogger ifbeam::ifbeam ifdh_art::IFBeam_service + SQLite::SQLite3 sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_Common sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_ICARUS diff --git a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl index 4959a0aaa..aad4daeb8 100644 --- a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl @@ -8,7 +8,7 @@ bnbspillinfo: { URL: "" #keep this blank and we're good Bundle: "BoosterNeutrinoBeam_read" MultiWireBundle: "BNBMultiWire" - TimeWindow: "500" #seconds + TimeWindow: "700" #seconds MWR_TimeWindow: "3601" #seconds raw_data_label: "daq" DeviceUsedForTiming: "E:TOR860" From 29eee27b14458a47085116e5bb0b570b3c0f1145 Mon Sep 17 00:00:00 2001 From: jzennamo Date: Tue, 3 Oct 2023 09:08:38 -0500 Subject: [PATCH 04/12] fix bug, forgot to add minbias scale_factor --- .../BNBEXTRetriever/BNBEXTRetriever_module.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc index d3b847df9..243bb29f0 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc @@ -129,7 +129,7 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) //Store everything in our data-product sbn::EXTCountInfo extInfo; - extInfo.gates_since_last_trigger = number_of_gates_since_previous_event; + extInfo.gates_since_last_trigger = number_of_gates_since_previous_event*scale_factor; fOutExtInfos.push_back(extInfo); // We do not write these to the art::Events because From 8493e09e45eeea79788a5e2ff90a9e65df609efb Mon Sep 17 00:00:00 2001 From: jzennamo Date: Wed, 1 Nov 2023 15:56:41 -0500 Subject: [PATCH 05/12] protect against events before minbias trigger --- .../BNBEXTRetriever/BNBEXTRetriever_module.cc | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc index 243bb29f0..185cb4469 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc @@ -78,6 +78,8 @@ class sbn::BNBEXTRetriever : public art::EDProducer { // input labels std::string raw_data_label_; float TotalEXTCounts; + float totalMinBias; + float evtCount; float scale_factor; }; @@ -90,6 +92,8 @@ sbn::BNBEXTRetriever::BNBEXTRetriever(Parameters const& params) produces< std::vector< sbn::EXTCountInfo >, art::InSubRun >(); TotalEXTCounts = 0; + totalMinBias = 0; + evtCount = 0; scale_factor = 0; } @@ -114,9 +118,14 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); gate_type = datastream_info.gate_type; number_of_gates_since_previous_event = frag.getDeltaGatesBNBOffMaj(); - scale_factor = 1. - (1./frag.getDeltaGatesBNBOffMinbias()); - std::cout << "BNB OFF MAJ : " << frag.getDeltaGatesBNBOffMaj() << std::endl; - std::cout << "Scale Factor : " << scale_factor << std::endl; + + if(frag.getDeltaGatesBNBOffMinbias() > 0){ + evtCount++; + totalMinBias += frag.getDeltaGatesBNBOffMinbias(); + } + + // std::cout << "BNB OFF MAJ : " << frag.getDeltaGatesBNBOffMaj() << std::endl; + // std::cout << "Scale Factor : " << scale_factor << std::endl; } @@ -125,11 +134,11 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) { // Keep track of the number of beam gates the DAQ thinks // are in this file - TotalEXTCounts += number_of_gates_since_previous_event*scale_factor; + TotalEXTCounts += number_of_gates_since_previous_event; //Store everything in our data-product sbn::EXTCountInfo extInfo; - extInfo.gates_since_last_trigger = number_of_gates_since_previous_event*scale_factor; + extInfo.gates_since_last_trigger = number_of_gates_since_previous_event; fOutExtInfos.push_back(extInfo); // We do not write these to the art::Events because @@ -142,14 +151,31 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) void sbn::BNBEXTRetriever::beginSubRun(art::SubRun& sr) { + TotalEXTCounts = 0; + totalMinBias = 0; + evtCount = 0; + scale_factor = 0; return; } -//____________________________________________________________________________ +//____________________________________________________________________________ void sbn::BNBEXTRetriever::endSubRun(art::SubRun& sr) { // We will add all of the EXTCountInfo data-products to the // art::SubRun so it persists + + if(evtCount != 0 && totalMinBias != 0) + scale_factor = 1. - (evtCount/totalMinBias); + else + std::cout << "FAILED! " << std::endl; + // probably want to throw an exception here + + for(auto ExtInfo : fOutExtInfos){ + + ExtInfo.gates_since_last_trigger *= scale_factor; + + } + auto p = std::make_unique< std::vector< sbn::EXTCountInfo > >(fOutExtInfos); sr.put(std::move(p), art::subRunFragment()); From b3cc268f5313c034d876fd55e20a0fc073797243 Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Fri, 8 Sep 2023 17:56:55 -0500 Subject: [PATCH 06/12] Add individual BNBInfo and NuMIInfo to SRHeader to enable saving of the spill quality for the spill that goes with a given event, and code supporting that. --- sbncode/CAFMaker/CAFMaker_module.cc | 52 +++++++++++++++++++++++++++++ sbncode/CAFMaker/FillExposure.cxx | 30 +++++++++++++++++ sbncode/CAFMaker/FillExposure.h | 1 + 3 files changed, 83 insertions(+) diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index e6efee59d..082ca2062 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -195,6 +195,10 @@ class CAFMaker : public art::EDProducer { double fPrescaleEvents; std::vector fBNBInfo; ///< Store detailed BNB info to save into the first StandardRecord of the output file std::vector fNuMIInfo; ///< Store detailed NuMI info to save into the first StandardRecord of the output file + std::map fBNBInfoEventMap; ///< Store detailed BNB info to save for the particular spills of events + std::map fNuMIInfoEventMap; ///< Store detailed NuMI info to save for the particular spills of events + bool fHasBNBInfo; + bool fHasNuMIInfo; // int fCycle; // int fBatch; @@ -747,15 +751,49 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { // get POT information fBNBInfo.clear(); fNuMIInfo.clear(); + + fBNBInfoEventMap.clear(); + fNuMIInfoEventMap.clear(); + fHasBNBInfo = false; + fHasNuMIInfo = false; + fSubRunPOT = 0; if(auto bnb_spill = sr.getHandle>(fParams.BNBPOTDataLabel())){ FillExposure(*bnb_spill, fBNBInfo, fSubRunPOT); fTotalPOT += fSubRunPOT; + + // Find the spill for each event and fill the event map: + // We take the latest spill for a given event number to be the one to keep + fHasBNBInfo = true; + for(const sbn::BNBSpillInfo& info: *bnb_spill) + { + if ( fBNBInfoEventMap.find(info.event)==fBNBInfoEventMap.end() ) { + fBNBInfoEventMap[info.event] = info; + } + else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > + (fBNBInfoEventMap[info.event].spill_time_s+(fBNBInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { + fBNBInfoEventMap[info.event] = info; + } + } } else if (auto numi_spill = sr.getHandle>(fParams.NuMIPOTDataLabel())) { FillExposureNuMI(*numi_spill, fNuMIInfo, fSubRunPOT); fTotalPOT += fSubRunPOT; + + // Find the spill for each event and fill the event map: + // We take the latest spill for a given event number to be the one to keep + fHasNuMIInfo = true; + for(const sbn::NuMISpillInfo& info: *numi_spill) + { + if ( fNuMIInfoEventMap.find(info.event)==fNuMIInfoEventMap.end() ) { + fNuMIInfoEventMap[info.event] = info; + } + else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > + (fNuMIInfoEventMap[info.event].spill_time_s+(fNuMIInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { + fNuMIInfoEventMap[info.event] = info; + } + } } else if(auto pot_handle = sr.getHandle(fParams.GenLabel())){ fSubRunPOT = pot_handle->totgoodpot; @@ -1956,6 +1994,20 @@ void CAFMaker::produce(art::Event& evt) noexcept { // rec.hdr.blind = 0; // rec.hdr.filt = rb::IsFiltered(evt, slices, sliceID); + // Fill the header info for the given event's spill quality info + if ( fHasBNBInfo && fHasNuMIInfo ) { + std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Will not fill event-specific spill quality info for this event..." << std::endl; + } + else if ( fHasBNBInfo ) { + if ( fBNBInfoEventMap.find(evt.id().event()) == fBNBInfoEventMap.end() ) + std::cout << "We think (since fHasBNBInfo) that this event should be BNB, but did not find this event in the spill info map." << std::endl; + else rec.hdr.spillbnbinfo = makeSRBNBInfo(fBNBInfoEventMap.at(evt.id().event())); + } + else if ( fHasNuMIInfo ) { + if ( fNuMIInfoEventMap.find(evt.id().event()) == fNuMIInfoEventMap.end() ) + std::cout << "We think (since fHasNuMIInfo) that this event should be NuMI, but did not find this event in the spill info map." << std::endl; + else rec.hdr.spillnumiinfo = makeSRNuMIInfo(fNuMIInfoEventMap.at(evt.id().event())); + } if(fRecTree){ // Save the standard-record diff --git a/sbncode/CAFMaker/FillExposure.cxx b/sbncode/CAFMaker/FillExposure.cxx index 544be125e..01dd11b80 100644 --- a/sbncode/CAFMaker/FillExposure.cxx +++ b/sbncode/CAFMaker/FillExposure.cxx @@ -72,4 +72,34 @@ namespace caf NuMIInfo.back().daq_gates = info.daq_gates; } } + + caf::SRNuMIInfo makeSRNuMIInfo(sbn::NuMISpillInfo const& info) + { + caf::SRNuMIInfo single_store; + + single_store.HP121 = info.HP121; + single_store.VP121 = info.VP121; + single_store.HPTGT = info.HPTGT; + single_store.VPTGT = info.VPTGT; + single_store.HITGT = info.HITGT; + single_store.VITGT = info.VITGT; + single_store.MTGTDS = info.MTGTDS; + single_store.HRNDIR = info.HRNDIR; + single_store.NSLINA = info.NSLINA; + single_store.NSLINB = info.NSLINB; + single_store.NSLINC = info.NSLINC; + single_store.NSLIND = info.NSLIND; + single_store.TRTGTD = info.TRTGTD; + single_store.TR101D = info.TR101D; + single_store.TORTGT = info.TORTGT; + single_store.TOR101 = info.TOR101; + single_store.time = info.time; + single_store.spill_time_s = info.spill_time_s; + single_store.spill_time_ns = info.spill_time_ns; + single_store.event = info.event; + single_store.daq_gates = info.daq_gates; + + return single_store; + } + } diff --git a/sbncode/CAFMaker/FillExposure.h b/sbncode/CAFMaker/FillExposure.h index 69df72ab8..ec8def3d2 100644 --- a/sbncode/CAFMaker/FillExposure.h +++ b/sbncode/CAFMaker/FillExposure.h @@ -20,6 +20,7 @@ namespace caf std::vector& NuMIInfo, double& subRunPOT); + caf::SRNuMIInfo makeSRNuMIInfo(sbn::NuMISpillInfo const& info); } From 39fe109c040ab8795fa25cadb21d1664d5aa086b Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Tue, 12 Sep 2023 17:38:28 -0500 Subject: [PATCH 07/12] Fix a few issues - one adds a check on PadTime before deciding if it wants to use this spill. The other checks not just for the first event but the first event OF A GIVEN STREAM for the numi case. --- .../NuMIRetriever/NuMIRetriever_module.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc index a0eb1e48a..432a6f0de 100644 --- a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc @@ -109,6 +109,13 @@ void sbn::NuMIRetriever::produce(art::Event &e) e.getByLabel(raw_data_label_, "ICARUSTriggerV3", raw_data_ptr); auto const & raw_data = (*raw_data_ptr); + // NOTE: Really we should skip the first event of each trigger type, so let's make this look at that too... + if ( raw_data.size()==0 ) return; + else { + icarus::ICARUSTriggerV3Fragment frag(raw_data.at(0)); + if ( frag.getTotalTriggerNuMIMaj() <= 1 ) return; + } + double t_current_event = 0; double t_previous_event = 0; double number_of_gates_since_previous_event = 0; @@ -163,8 +170,8 @@ void sbn::NuMIRetriever::produce(art::Event &e) // plus or minus some time padding, currently using 3.3 ms // which is half the Booster Rep Rate if(e.event() != 1){//We already addressed the "first event" above - if(times_temps[i] > t_current_event){continue;} - if(times_temps[i] <= t_previous_event){continue;} + if(times_temps[i] > t_current_event+fTimePad){continue;} + if(times_temps[i] <= t_previous_event+fTimePad){continue;} } //count found spills From 20ba04fec7f079f5c1f6a6b852107058f26f389c Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Tue, 14 Nov 2023 11:34:15 -0600 Subject: [PATCH 08/12] Updated code with some of Gianluca P's suggestions (need to test and add another). Thanks Gianluca --- sbncode/CAFMaker/CAFMaker_module.cc | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index 082ca2062..ca969852c 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -150,6 +150,13 @@ namespace sbn{ namespace caf { +/// Function to calculate a timestamp from the spill info product +template +double spillInfoToTimestamp(SpillInfo const& info) { + return static_cast(info.spill_time_s) + + static_cast(info.spill_time_ns)*1.0e-9; +} + /// Module to create Common Analysis Files from ART files class CAFMaker : public art::EDProducer { public: @@ -768,12 +775,9 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { fHasBNBInfo = true; for(const sbn::BNBSpillInfo& info: *bnb_spill) { - if ( fBNBInfoEventMap.find(info.event)==fBNBInfoEventMap.end() ) { - fBNBInfoEventMap[info.event] = info; - } - else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > - (fBNBInfoEventMap[info.event].spill_time_s+(fBNBInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { - fBNBInfoEventMap[info.event] = info; + auto& storedInfo = fBNBInfoEventMap[info.event]; // creates if needed + if ( (storedInfo.event == UINT_MAX) || spillInfoToTimestamp(info) > spillInfoToTimestamp(storedInfo) ) { + storedInfo = std::move(info); } } } @@ -786,12 +790,9 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { fHasNuMIInfo = true; for(const sbn::NuMISpillInfo& info: *numi_spill) { - if ( fNuMIInfoEventMap.find(info.event)==fNuMIInfoEventMap.end() ) { - fNuMIInfoEventMap[info.event] = info; - } - else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > - (fNuMIInfoEventMap[info.event].spill_time_s+(fNuMIInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { - fNuMIInfoEventMap[info.event] = info; + auto& storedInfo = fNuMIInfoEventMap[info.event]; // creates if needed + if ( (storedInfo.event == UINT_MAX) || spillInfoToTimestamp(info) > spillInfoToTimestamp(storedInfo) ) { + storedInfo = std::move(info); } } } @@ -1996,7 +1997,8 @@ void CAFMaker::produce(art::Event& evt) noexcept { // Fill the header info for the given event's spill quality info if ( fHasBNBInfo && fHasNuMIInfo ) { - std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Will not fill event-specific spill quality info for this event..." << std::endl; + std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Throwing..." << std::endl; + abort(); } else if ( fHasBNBInfo ) { if ( fBNBInfoEventMap.find(evt.id().event()) == fBNBInfoEventMap.end() ) From 2b6c7ed844a011d51dc46defc484817ec8123f86 Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Tue, 14 Nov 2023 15:14:22 -0600 Subject: [PATCH 09/12] Second part of incorporating Gianluca's comments. Thanks --- sbncode/CAFMaker/CAFMaker_module.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index ca969852c..7744547b2 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -2000,15 +2000,15 @@ void CAFMaker::produce(art::Event& evt) noexcept { std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Throwing..." << std::endl; abort(); } - else if ( fHasBNBInfo ) { - if ( fBNBInfoEventMap.find(evt.id().event()) == fBNBInfoEventMap.end() ) - std::cout << "We think (since fHasBNBInfo) that this event should be BNB, but did not find this event in the spill info map." << std::endl; - else rec.hdr.spillbnbinfo = makeSRBNBInfo(fBNBInfoEventMap.at(evt.id().event())); - } - else if ( fHasNuMIInfo ) { - if ( fNuMIInfoEventMap.find(evt.id().event()) == fNuMIInfoEventMap.end() ) - std::cout << "We think (since fHasNuMIInfo) that this event should be NuMI, but did not find this event in the spill info map." << std::endl; - else rec.hdr.spillnumiinfo = makeSRNuMIInfo(fNuMIInfoEventMap.at(evt.id().event())); + unsigned int const eventNo = evt.id().event(); + if ( fBNBInfoEventMap.count(eventNo) > 0 ) { + rec.hdr.spillbnbinfo = makeSRBNBInfo(fBNBInfoEventMap.at(eventNo)); + } + else if ( fNuMIInfoEventMap.count(eventNo) > 0 ) { + rec.hdr.spillnumiinfo = makeSRNuMIInfo(fNuMIInfoEventMap.at(eventNo)); + } + else { + std::cout << "Did not find this event in the spill info map." << std::endl; } if(fRecTree){ From 15f8421aca1d2441842de20bced078eee6e07e97 Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Wed, 6 Dec 2023 22:42:12 -0600 Subject: [PATCH 10/12] Adding to fix a pet peeve of Gianluca since I was re-testing things anyway :) --- .../NuMIRetriever/NuMIRetriever_module.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc index 432a6f0de..ba3396c99 100644 --- a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc @@ -110,7 +110,7 @@ void sbn::NuMIRetriever::produce(art::Event &e) auto const & raw_data = (*raw_data_ptr); // NOTE: Really we should skip the first event of each trigger type, so let's make this look at that too... - if ( raw_data.size()==0 ) return; + if ( raw_data.empty() ) return; else { icarus::ICARUSTriggerV3Fragment frag(raw_data.at(0)); if ( frag.getTotalTriggerNuMIMaj() <= 1 ) return; From 31b87be8b90d5a3a4796f70415df63dfb4743cbe Mon Sep 17 00:00:00 2001 From: jzennamo Date: Tue, 16 Jan 2024 13:00:43 -0600 Subject: [PATCH 11/12] adding guard for non-offbeam triggers --- .../BNBEXTRetriever/BNBEXTRetriever_module.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc index 185cb4469..1b7c79dd5 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc @@ -118,8 +118,11 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) icarus::ICARUSTriggerInfo datastream_info = icarus::parse_ICARUSTriggerV3String(buffer); gate_type = datastream_info.gate_type; number_of_gates_since_previous_event = frag.getDeltaGatesBNBOffMaj(); + + if(gate_type != 3) + return; - if(frag.getDeltaGatesBNBOffMinbias() > 0){ + if(frag.getDeltaGatesBNBOffMinbias() > 0 && gate_type == 3){ evtCount++; totalMinBias += frag.getDeltaGatesBNBOffMinbias(); } From 3c21acc0152c42365394f87a54fddaaa34f535c9 Mon Sep 17 00:00:00 2001 From: jzennamo Date: Wed, 17 Jan 2024 09:38:19 -0600 Subject: [PATCH 12/12] updates with changes --- .../BNBRetriever/BNBRetriever_module.cc | 41 ++++++++++++++++--- .../job/bnbspillinfo.fcl | 1 + 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc index e2bc1feb7..2265d0e2c 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc @@ -6,13 +6,14 @@ * Based heavily on code by Z. Pavlovic written for MicroBooNE * Based heavily on code by NOvA collaboration (Thanks NOvA!): * https://cdcvs.fnal.gov/redmine/projects/novaart/repository/entry/trunk/IFDBSpillInfo/BNBInfo_module.cc + * Database implementation by Justin Mueller */ #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/Run.h" #include "art/Framework/Principal/SubRun.h" #include "canvas/Utilities/InputTag.h" #include "fhiclcpp/types/Atom.h" @@ -89,7 +90,13 @@ class sbn::BNBRetriever : public art::EDProducer { Name{ "MWR_TimeWindow" }, Comment{ "" } // explain what this is, what's for and its unit }; + + fhicl::Atom TriggerDatabaseFile { + Name{ "TriggerDatabaseFile" }, + Comment{ "" } // explain what this is, what's for and its unit + }; + }; // Config using Parameters = art::EDProducer::Table; @@ -126,6 +133,7 @@ class sbn::BNBRetriever : public art::EDProducer { std::unique_ptr bfp_mwr; // + std::string fTriggerDatabaseFile; sqlite3 *db; int rc; @@ -207,7 +215,7 @@ class sbn::BNBRetriever : public art::EDProducer { int func(void*,int,char**,char**), int run, long long int gate_time, - int threshold) const; + float threshold) const; }; @@ -223,7 +231,7 @@ int callback_trigger_type(void *data, int argc, char **argv, char **columns) return 0; } -int sbn::BNBRetriever::get_trigger_type_matching_gate(sqlite3 *db, int func(void*,int,char**,char**), int run, long long int gate_time, int threshold) const +int sbn::BNBRetriever::get_trigger_type_matching_gate(sqlite3 *db, int func(void*,int,char**,char**), int run, long long int gate_time, float threshold) const { int trigger_type(-1), query_status; std::stringstream query; @@ -240,7 +248,7 @@ int sbn::BNBRetriever::get_trigger_type_matching_gate(sqlite3 *db, int func(void query_status = sqlite3_exec(db, query.str().c_str(), func, &trigger_type, NULL); if (query_status != SQLITE_OK) { - fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); + mf::LogError("BNBEXTRetriever") << "SQL error: " << sqlite3_errmsg(db); trigger_type = -1; } return trigger_type; @@ -252,7 +260,8 @@ sbn::BNBRetriever::BNBRetriever(Parameters const& params) raw_data_label(params().RawDataLabel()), fDeviceUsedForTiming(params().DeviceUsedForTiming()), bfp( ifbeam_handle->getBeamFolder(params().Bundle(), params().URL(), params().TimeWindow())), - bfp_mwr( ifbeam_handle->getBeamFolder(params().MultiWireBundle(), params().URL(), params().MWR_TimeWindow())) + bfp_mwr( ifbeam_handle->getBeamFolder(params().MultiWireBundle(), params().URL(), params().MWR_TimeWindow())), + fTriggerDatabaseFile(params().TriggerDatabaseFile()) { // Check fTimePad is positive @@ -275,10 +284,15 @@ sbn::BNBRetriever::BNBRetriever(Parameters const& params) produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); TotalBeamSpills = 0; - rc = sqlite3_open("/pnfs/icarus/scratch/users/mueller/icarus_triggers.db", &db); + cet::search_path sp("FW_SEARCH_PATH"); + std::string trigDB_path = sp.find_file(fTriggerDatabaseFile.c_str()); + + rc = sqlite3_open(trigDB_path.c_str(), &db); if(rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + throw art::Exception(art::errors::NotFound) + << "Can't open database: " << sqlite3_errmsg(db); } } @@ -347,6 +361,15 @@ sbn::BNBRetriever::TriggerInfo_t sbn::BNBRetriever::extractTriggerInfo(art::Even triggerInfo.gate_type = datastream_info.gate_type; triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNBMaj(); + /* + The DAQ trigger time is issued at the Beam Extraction Signal (BES) which is issued + 36 ms *after* the $1D of the BNB, which is what is used in the IFBeam database + + We subtract 36ms from the Trigger time to match our triggers to the spills in the + IFBeam database + + */ + triggerInfo.t_current_event = static_cast(artdaq_ts-3.6e7)/(1000000000.0); //check this offset... if(triggerInfo.gate_type == 1) triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); @@ -553,6 +576,12 @@ int sbn::BNBRetriever::matchMultiWireData( } //check if this spill is is minbias + /* + 40 ms was selected to be close to but outside the 66 ms + time of the next spill (when the beam is running at 15 Hz) + DocDB 33155 provides documentation of this + */ + mf::LogDebug("BNBRetriever") << std::setprecision(19) << "matchMultiWireData:: trigger type : " << get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+3.6e7, 40.) << " times : spill " << times_temps[i]*1.e9 << " - " << triggerInfo.WR_to_Spill_conversion << " + " << 3.6e7 << std::endl; if(get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+3.6e7, 40.) == 1){ diff --git a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl index aad4daeb8..3aece8658 100644 --- a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl @@ -12,5 +12,6 @@ bnbspillinfo: { MWR_TimeWindow: "3601" #seconds raw_data_label: "daq" DeviceUsedForTiming: "E:TOR860" + TriggerDatabaseFile: "triggerDatabase/icarus_triggers.db" } END_PROLOG