From 1df323461441deb1801aeff852b0c2addbedbc9c Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Wed, 9 Oct 2024 14:50:43 -0500 Subject: [PATCH 01/26] initial commit --- .../BNBRetriever/SBNDBNBDefaults.fcl | 22 + .../BNBRetriever/SBNDBNB_module.cc | 601 ++++++++++++++++++ .../BNBRetriever/runbnb.fcl | 31 + 3 files changed, 654 insertions(+) create mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNB_module.cc create mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl new file mode 100644 index 000000000..9ee036aac --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl @@ -0,0 +1,22 @@ +BEGIN_PROLOG + +SBNDBNBDefaults: { + module_type: SBNDBNB + InputLabel: "bnb" + InputContainerInstance: "ContainerBNB" + InputNonContainerInstance: "BNB" + OutputInstance: "" + DebugLevel: 0 + TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away + URL: "" #https://dbdata3vm.fnal.gov:9443/ifbeam + Bundle: "BoosterNeutrinoBeam_read" + MultiWireBundle: "BNBMultiWire" + TimeWindow: "700" #seconds + MWR_TimeWindow: "3601" #seconds + RawDataLabel: "ptbdecoder" + DeviceUsedForTiming: "E:TOR860" + TriggerDatabaseFile: "triggerDatabase/icarus_triggers.db" +} + +END_PROLOG + diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNB_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNB_module.cc new file mode 100644 index 000000000..934065d73 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNB_module.cc @@ -0,0 +1,601 @@ +//////////////////////////////////////////////////////////////////////// +// Class: SBNDBNB +// Plugin Type: producer +// File: SBNDBNB_module.cc +// +//////////////////////////////////////////////////////////////////////// + +#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/ParameterSet.h" +#include "messagefacility/MessageLogger/MessageLogger.h" + +#include +#include +#include + +#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" +#include "artdaq-core/Data/ContainerFragment.hh" +#include "sbndcode/Decoders/PTB/sbndptb.h" +#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" + +#include "ifdh_art/IFBeamService/IFBeam_service.h" +#include "ifbeam_c.h" +#include "MWRData.h" + +#include "larcorealg/CoreUtils/counter.h" + +class SBNDBNB : public art::EDProducer { +public: + explicit SBNDBNB(fhicl::ParameterSet const & params); + + // Plugins should not be copied or assigned. + SBNDBNB(SBNDBNB const &) = delete; + SBNDBNB(SBNDBNB &&) = delete; + SBNDBNB & operator = (SBNDBNB const &) = delete; + SBNDBNB & operator = (SBNDBNB &&) = delete; + + // Required functions. + void produce(art::Event & e) override; + void beginSubRun(art::SubRun& sr) override; + void endSubRun(art::SubRun& sr) override; + +private: + // Declare member data here. + std::vector< sbn::BNBSpillInfo > fOutbeamInfos; + double fTimePad; + std::string fInputLabel; + std::string fInputNonContainerInstance; + std::string fDeviceUsedForTiming; + std::string fOutputInstance; + std::string raw_data_label; + int fDebugLevel; + sbn::MWRData mwrdata; + art::ServiceHandle ifbeam_handle; + std::unique_ptr bfp; + std::unique_ptr bfp_mwr; + + 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 { + std::vector< std::vector > MWR_times; + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + }; + + static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] + + TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; + int matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos + ) const; + + sbn::BNBSpillInfo makeBNBSpillInfo + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; +}; + +SBNDBNB::SBNDBNB(fhicl::ParameterSet const & params) + : EDProducer{params} { + raw_data_label = params.get("RawDataLabel", "ptbdecoder"); + fInputLabel = params.get("InputLabel"); + fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); + fTimePad = params.get("TimePadding"); + fInputNonContainerInstance = params.get("InputNonContainerInstance"); + fOutputInstance = params.get("OutputInstance"); + fDebugLevel = params.get("DebugLevel",0); + bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); + bfp->set_epsilon(0.05); + bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); + bfp_mwr->set_epsilon(0.5); + bfp_mwr->setValidWindow(3605); +} + +int eventNum =0; +int _run; +int _subrun; +int _event; + +void SBNDBNB::produce(art::Event & e) +{ + + // If this is the first event in the run, then ignore it + // We do not currently have the ability to figure out the first + // spill that the DAQ was sensitive to, so don't try to save any + // spill information + +/* + art::InputTag ptb_tag { "ptbdecoder" }; + auto const& ptb_handle = e.getValidHandle< std::vector >(ptb_tag); + auto const& ptb_vec(*ptb_handle); + int ptb_size = ptb_vec.size(); + int NHL_trig; + if(ptb_size > 0){ + for (int j = 0; j < ptb_size; j++){ + if(ptb_vec.at(j).GetNHLTriggers() > 0){ + NHL_trig = ptb_vec.at(j).GetNHLTriggers(); + for(int k = 0; k < NHL_trig; k++){ + ULong64_t trig_time = ptb_vec.at(j).GetHLTrigger(k).timestamp; + if(ptb_vec.at(j).GetHLTrigger(k).trigger_word != 1048576){ + ULong64_t curr_min = 1e19; + //int curr_min_ind; + std::cout << "Zero Bias Trigger" << std::endl; + try{ + //auto vec_temp = bfp_mwr->GetNamedVector(trig_time/1e9,"E:M875BB{4440:888}.RAW"); + auto vec_temp = bfp->GetNamedVector(trig_time/1e9,"E:TOR875"); + //std::vector times = bfp->GetTimeList("E:M875BB"); + std::vector times = bfp->GetTimeList("E:TOR875"); + int times_size = times.size(); + for (int i = 0; i < times_size; i++) { + if(times[i] < trig_time/1e9){ + if(trig_time/1e9 - times[i] < curr_min){ + curr_min = trig_time - times[i]; + std::cout << "trig_time: " << trig_time << std::endl; + //curr_min_ind = i; + } + } + } + }catch(WebAPIException &we){std::cout << "Failed GetNamedVector" << std::endl;} + } + else{ + std::cout << "CRT Reset Trigger" << std::endl; + } + } + } + } + } +*/ + //sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); + //fOutbeamInfos.push_back(std::move(spillInfo)); + + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + + std::cout << "Gate Type: " << triggerInfo.gate_type << std::endl; + + MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); + + int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); + + 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; +} + +SBNDBNB::TriggerInfo_t SBNDBNB::extractTriggerInfo(art::Event const& e) const { + //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 + auto const & raw_data = e.getProduct< std::vector >({ "ptbdecoder", "" }); + TriggerInfo_t triggerInfo; + /// Correction added to the White Rabbit time to get the trigger time. + triggerInfo.WR_to_Spill_conversion = 0; + triggerInfo.gate_type = 1; + + int raw_data_size = raw_data.size(); + if(raw_data_size > 0){ + for (int j = 0; j < raw_data_size; j++){ + if(raw_data.at(j).GetNHLTriggers() > 0){ + int NHL_trig = raw_data.at(j).GetNHLTriggers(); + for(int k = 0; k < NHL_trig; k++){ + ULong64_t trig_time = raw_data.at(j).GetHLTrigger(k).timestamp; + if(raw_data.at(j).GetHLTrigger(k).trigger_word != 1048576){ + std::cout << "Zero Bias Trigger" << std::endl; + triggerInfo.t_current_event = trig_time/1e9; + triggerInfo.t_previous_event = trig_time/1e9 - 10; + + triggerInfo.number_of_gates_since_previous_event = 100; + } + else{ + std::cout << "CRT Reset Trigger" << std::endl; + } + } + } + } + } + return triggerInfo; +} + +SBNDBNB::MWRdata_t SBNDBNB::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { + + // These lines get everything primed within the IFBeamDB + // They seem redundant but they are needed + try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {std::cout << "caught 1" << std::endl;} + try{auto packed_M876BB_temp = bfp_mwr->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:M875BB{4440:888}.RAW");} catch (WebAPIException &we) {std::cout << "caught 2" << std::endl;} + + //The multiwire chambers provide their + // data in a vector format but we'll have + // to sort through it in std::string format + // to correctly unpack it + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + std::vector< std::vector< double> > MWR_times; + unpacked_MWR.resize(3); + MWR_times.resize(3); + std::string packed_data_str; + + //Create a list of all the MWR devices with their different + // memory buffer increments + // generally in the format: "E:.{Memory Block}" + std::vector vars = bfp_mwr->GetDeviceList(); + mf::LogDebug("BNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; + // Tracking the time from the IFBeamDB + double time_for_mwr; + + // this is an iterator to track which of the + // three devices we will be working with + int dev = 0; + + // The MWR devices are annoying and have confusing buffer + // what we'll do is sort through all of them first and then + // match them to the closest spills in time + // + + // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; + int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; + mf::LogDebug("BNBRetriever") << " t_steps " << t_steps << std::endl; + + for(int t = 0; t < t_steps; t++){//Iterate through time increments + for (std::string const& var : vars) {// Iterate through the devices + + //Make sure we have a device + if(var.empty()){ + //mf::LogDebug("BNBRetriever") << " NO MWR DEVICES?!" << std::endl; + continue; + } + /// Check the device name and interate the double-vector index + if(var.find("M875BB") != std::string::npos ) dev = 0; + 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; + continue;} + + time_for_mwr = 0; + + try{ + + //Pull the MWR data for the device + // these data are "packed" + std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); + + //We'll convert this into a format + // that we can unpack doubles >> strings + // + packed_data_str.clear(); + packed_data_str += std::to_string(int(time_for_mwr)); + packed_data_str.append(","); + packed_data_str.append(var); + packed_data_str.append(",,"); + + /* for(auto const value: packed_MWR){ + packed_data_str += ','; + packed_data_str += std::to_string(int(value)); + }*/ + for(int j = 0; j < int(packed_MWR.size()); j++){ + packed_data_str += std::to_string(int(packed_MWR[j])); + if(j < int(packed_MWR.size())-1) + packed_data_str.append(","); + } + + // Use Zarko's unpacking function to turn this into consumeable data + std::vector MWR_times_temp; + + // There is a 35 ms offset between the toriod and the MWR times + // we'll just remove that here to match to the spill times + std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); + + //There are four events that are packed into one MWR IFBeam entry + for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ + + // If this entry has a unique time them store it for later + if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ + unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); + MWR_times[dev].push_back(MWR_times_temp[s]); + }//check for unique time + }//Iterate through the unpacked events + }//try + catch (WebAPIException &we) { + //Ignore when we can't find the MWR devices + // they don't always report and the timing of them can be annoying + }//catch + }// Iterate over all the multiwire devices + }// Iterate over all times + + mf::LogDebug("BNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; + mf::LogDebug("BNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; + mf::LogDebug("BNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; + mf::LogDebug("BNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; + mf::LogDebug("BNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; + mf::LogDebug("BNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; + + return { std::move(MWR_times), std::move(unpacked_MWR) }; +} + +int SBNDBNB::matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos +) const { + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + //Here we will start collecting all the other beamline devices + // First we get the times that the beamline device fired + // 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); + + std::cout << std::setprecision(19) << "t_current_event: " << triggerInfo.t_current_event << std::endl; + // NOTE: for now, this is dead code because we don't + // do anything for the first event in a run. We may want to revisit + // this later to understand if there is a way we can do the POT + // accounting in the first event. + // + // Need to handle the first event in a run differently + if(isFirstEventInRun){ + + //We'll remove the spills after our event + int spills_after_our_target = 0; + // iterate through all the spills to find the + // spills that are after our triggered event + for (size_t i = 0; i < times_temps.size(); i++) { + if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ + spills_after_our_target++; + } + }//end loop through spill times + + // Remove the spills after our trigger + times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); + + // 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; + + std::cout << std::setprecision(19) << "times_temps[i]: " << times_temps[i] << std::endl; + if(!isFirstEventInRun){//We already addressed the "first event" above + 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 + /* + 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){ + // 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++; + + //Loop through the multiwire devices: + + for(int dev = 0; dev < int(MWR_times.size()); dev++){ + + //Loop through the multiwire times: + double Tdiff = 1000000000.; + matched_MWR[dev] = 0; + + for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ + + //found a candidate match! + if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} + + bool best_match = true; + + //Check for a better match... + 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;} + + //is there a better match later in the spill sequence + if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < + fabs((MWR_times[dev][mwrt] - times_temps[i]))){ + //we can have patience... + best_match = false; + break; + } + }//end better match check + + //Verified best match! + if(best_match == true){ + matched_MWR[dev] = mwrt; + Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); + } + + }//end loop over MWR times + + }//end loop over MWR devices + + sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); + + beamInfos.push_back(std::move(spillInfo)); + + // 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 iteration over beam device times + + // mf::LogDebug("BNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; + + return spill_count; +} + +sbn::BNBSpillInfo SBNDBNB::makeBNBSpillInfo + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const +{ + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + // initializing all of our device carriers + // device definitions can be found in BNBSpillInfo.h + + double TOR860 = 0; // units e12 protons + double TOR875 = 0; // units e12 protons + double LM875A = 0; // units R/s + double LM875B = 0; // units R/s + double LM875C = 0; // units R/s + double HP875 = 0; // units mm + double VP875 = 0; // units mm + double HPTG1 = 0; // units mm + double VPTG1 = 0; // units mm + double HPTG2 = 0; // units mm + double VPTG2 = 0; // units mm + double BTJT2 = 0; // units Deg C + double THCURR = 0; // units kiloAmps + + double TOR860_time = 0; // units s + + // Here we request all the devices + // since sometimes devices fail to report we'll + // allow each to throw an exception but still move forward + // interpreting these failures will be part of the beam quality analyses + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + + //crunch the times + unsigned long int time_closest_int = (int) TOR860_time; + double time_closest_ns = (TOR860_time - time_closest_int)*1e9; + + //Store everything in our data-product + sbn::BNBSpillInfo beamInfo; + beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.LM875A = LM875A; + beamInfo.LM875B = LM875B; + beamInfo.LM875C = LM875C; + beamInfo.HP875 = HP875; + beamInfo.VP875 = VP875; + beamInfo.HPTG1 = HPTG1; + beamInfo.VPTG1 = VPTG1; + beamInfo.HPTG2 = HPTG2; + beamInfo.VPTG2 = VPTG2; + beamInfo.BTJT2 = BTJT2; + beamInfo.THCURR = THCURR; + beamInfo.spill_time_s = time_closest_int; + beamInfo.spill_time_ns = time_closest_ns; + + std::cout << "TOR860: " << TOR860 << std::endl; + + for(auto const& MWRdata: unpacked_MWR){ + std::ignore = MWRdata; + assert(!MWRdata.empty()); + } + + if(unpacked_MWR[0].empty()){ + beamInfo.M875BB.clear(); + beamInfo.M875BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; + beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); + } + + if(unpacked_MWR[1].empty()){ + beamInfo.M876BB.clear(); + beamInfo.M876BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; + beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); + } + + if(unpacked_MWR[2].empty()){ + beamInfo.MMBTBB.clear(); + beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; + beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); + } + // 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 + + beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun + + return beamInfo; +} + +void SBNDBNB::beginSubRun(art::SubRun& sr) +{ + return; +} + +void SBNDBNB::endSubRun(art::SubRun& sr) +{ + //mf::LogDebug("BNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; + //mf::LogDebug("BNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; + + //auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); + //std::swap(*p, fOutbeamInfos); + + //sr.put(std::move(p), art::subRunFragment()); + + return; +} + +DEFINE_ART_MODULE(SBNDBNB) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl b/sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl new file mode 100644 index 000000000..780b32bb3 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl @@ -0,0 +1,31 @@ +#include "SBNDBNBDefaults.fcl" + +services:{ + IFBeam:{} +} + +physics: +{ + producers: + { + bnb: @local::SBNDBNBDefaults + } + + analyzers: {} + + my_producer_modules: [bnb] + trigger_paths: [my_producer_modules] + a: [rootout] + end_paths: [a] +} + +source: {} + +outputs: { + rootout: { + module_type: "RootOutput" + fileName: "BNB_out.root" + } +} + +process_name: BNBTEST From 357c6e577630060c18f1ebc0f828de19cb91602b Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Thu, 10 Oct 2024 15:37:00 -0500 Subject: [PATCH 02/26] Develop change not in release 09_91_02_02 --- sbncode/CAFMaker/FillReco.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sbncode/CAFMaker/FillReco.cxx b/sbncode/CAFMaker/FillReco.cxx index a478bc840..0c35b8a59 100644 --- a/sbncode/CAFMaker/FillReco.cxx +++ b/sbncode/CAFMaker/FillReco.cxx @@ -119,8 +119,8 @@ namespace caf srspacepoint.position = SRVector3D(spacepoint.X(), spacepoint.Y(), spacepoint.Z()); srspacepoint.position_err = SRVector3D(spacepoint.XErr(), spacepoint.YErr(), spacepoint.ZErr()); srspacepoint.pe = spacepoint.PE(); - srspacepoint.time = spacepoint.Ts1(); - srspacepoint.time_err = spacepoint.Ts1Err(); + srspacepoint.time = spacepoint.Time(); + srspacepoint.time_err = spacepoint.TimeErr(); srspacepoint.complete = spacepoint.Complete(); } @@ -131,8 +131,8 @@ namespace caf for(auto const& point : track.Points()) srsbndcrttrack.points.emplace_back(point.X(), point.Y(), point.Z()); - srsbndcrttrack.time = track.Ts1(); - srsbndcrttrack.time_err = track.Ts1Err(); + srsbndcrttrack.time = track.Time(); + srsbndcrttrack.time_err = track.TimeErr(); srsbndcrttrack.pe = track.PE(); srsbndcrttrack.tof = track.ToF(); } From b205ea95d76819e3d1bf3609c5eab5dfdc72ee9c Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Tue, 15 Oct 2024 13:50:05 -0500 Subject: [PATCH 03/26] Builds and runs --- .../BNBRetriever/CMakeLists.txt | 21 +++ .../BNBRetriever/SBNDBNBDefaults.fcl | 7 +- ...B_module.cc => SBNDBNBRetriever_module.cc} | 169 +++++++++--------- 3 files changed, 110 insertions(+), 87 deletions(-) rename sbncode/BeamSpillInfoRetriever/BNBRetriever/{SBNDBNB_module.cc => SBNDBNBRetriever_module.cc} (70%) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt index a34b60d6a..1f0dab80a 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt @@ -28,6 +28,27 @@ cet_build_plugin(BNBRetriever art::module larcorealg::CoreUtils ) +cet_build_plugin(SBNDBNBRetriever art::module + LIBRARIES + art::Persistency_Common + art::Utilities canvas::canvas + cetlib::cetlib cetlib_except::cetlib_except + ROOT::X3d + Boost::system + 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 + sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND + artdaq_core::artdaq-core_Utilities + sbn_BNBSpillInfoRetriever_MWRData + sbnobj::Common_POTAccounting + larcorealg::CoreUtils +) + install_headers() install_fhicl() install_source() diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl index 9ee036aac..03e3cdc87 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl @@ -1,21 +1,20 @@ BEGIN_PROLOG SBNDBNBDefaults: { - module_type: SBNDBNB + module_type: SBNDBNBRetriever InputLabel: "bnb" InputContainerInstance: "ContainerBNB" InputNonContainerInstance: "BNB" OutputInstance: "" DebugLevel: 0 TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away - URL: "" #https://dbdata3vm.fnal.gov:9443/ifbeam + URL: "" Bundle: "BoosterNeutrinoBeam_read" MultiWireBundle: "BNBMultiWire" TimeWindow: "700" #seconds MWR_TimeWindow: "3601" #seconds - RawDataLabel: "ptbdecoder" + raw_data_label: "daq" DeviceUsedForTiming: "E:TOR860" - TriggerDatabaseFile: "triggerDatabase/icarus_triggers.db" } END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNB_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc similarity index 70% rename from sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNB_module.cc rename to sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 934065d73..2ceaf32bc 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNB_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////// -// Class: SBNDBNB +// Class: SBNDBNBRetriever // Plugin Type: producer -// File: SBNDBNB_module.cc +// File: SBNDBNBRetriever_module.cc // //////////////////////////////////////////////////////////////////////// @@ -21,7 +21,7 @@ #include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" #include "artdaq-core/Data/ContainerFragment.hh" -#include "sbndcode/Decoders/PTB/sbndptb.h" +//#include "sbndcode/Decoders/PTB/sbndptb.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" #include "ifdh_art/IFBeamService/IFBeam_service.h" @@ -30,15 +30,15 @@ #include "larcorealg/CoreUtils/counter.h" -class SBNDBNB : public art::EDProducer { +class SBNDBNBRetriever : public art::EDProducer { public: - explicit SBNDBNB(fhicl::ParameterSet const & params); + explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); // Plugins should not be copied or assigned. - SBNDBNB(SBNDBNB const &) = delete; - SBNDBNB(SBNDBNB &&) = delete; - SBNDBNB & operator = (SBNDBNB const &) = delete; - SBNDBNB & operator = (SBNDBNB &&) = delete; + SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; // Required functions. void produce(art::Event & e) override; @@ -61,11 +61,9 @@ class SBNDBNB : public art::EDProducer { std::unique_ptr bfp_mwr; 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 { @@ -88,9 +86,9 @@ class SBNDBNB : public art::EDProducer { (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; }; -SBNDBNB::SBNDBNB(fhicl::ParameterSet const & params) +SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) : EDProducer{params} { - raw_data_label = params.get("RawDataLabel", "ptbdecoder"); + raw_data_label = params.get("raw_data_label", "daq"); fInputLabel = params.get("InputLabel"); fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); fTimePad = params.get("TimePadding"); @@ -109,7 +107,7 @@ int _run; int _subrun; int _event; -void SBNDBNB::produce(art::Event & e) +void SBNDBNBRetriever::produce(art::Event & e) { // If this is the first event in the run, then ignore it @@ -170,47 +168,52 @@ void SBNDBNB::produce(art::Event & e) int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); 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; + mf::LogDebug("SBNDBNBRetriever")<< "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; + mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; } -SBNDBNB::TriggerInfo_t SBNDBNB::extractTriggerInfo(art::Event const& e) const { +SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { //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 - auto const & raw_data = e.getProduct< std::vector >({ "ptbdecoder", "" }); + auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ContainerTDCTIMESTAMP" }); TriggerInfo_t triggerInfo; /// Correction added to the White Rabbit time to get the trigger time. - triggerInfo.WR_to_Spill_conversion = 0; - triggerInfo.gate_type = 1; - - int raw_data_size = raw_data.size(); - if(raw_data_size > 0){ - for (int j = 0; j < raw_data_size; j++){ - if(raw_data.at(j).GetNHLTriggers() > 0){ - int NHL_trig = raw_data.at(j).GetNHLTriggers(); - for(int k = 0; k < NHL_trig; k++){ - ULong64_t trig_time = raw_data.at(j).GetHLTrigger(k).timestamp; - if(raw_data.at(j).GetHLTrigger(k).trigger_word != 1048576){ - std::cout << "Zero Bias Trigger" << std::endl; - triggerInfo.t_current_event = trig_time/1e9; - triggerInfo.t_previous_event = trig_time/1e9 - 10; + for(auto raw_datum : raw_data){ + + uint64_t artdaq_ts = raw_datum.timestamp(); + //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); + //triggerInfo.gate_type = datastream_info.gate_type; + //triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNBMaj(); + triggerInfo.number_of_gates_since_previous_event = 1; + + /* + 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.number_of_gates_since_previous_event = 100; - } - else{ - std::cout << "CRT Reset Trigger" << std::endl; - } - } - } - } + triggerInfo.t_current_event = static_cast(artdaq_ts-3.6e7)/(1000000000.0); //check this offset... + if(triggerInfo.gate_type == 1) + triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); + else + triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(frag.getLastTimestampOther()-3.6e7))/(1000000000.0); + } + + 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; } -SBNDBNB::MWRdata_t SBNDBNB::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { +SBNDBNBRetriever::MWRdata_t SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { // These lines get everything primed within the IFBeamDB // They seem redundant but they are needed @@ -231,7 +234,7 @@ SBNDBNB::MWRdata_t SBNDBNB::extractSpillTimes(TriggerInfo_t const& triggerInfo) // memory buffer increments // generally in the format: "E:.{Memory Block}" std::vector vars = bfp_mwr->GetDeviceList(); - mf::LogDebug("BNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; // Tracking the time from the IFBeamDB double time_for_mwr; @@ -246,14 +249,14 @@ SBNDBNB::MWRdata_t SBNDBNB::extractSpillTimes(TriggerInfo_t const& triggerInfo) // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; - mf::LogDebug("BNBRetriever") << " t_steps " << t_steps << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " t_steps " << t_steps << std::endl; for(int t = 0; t < t_steps; t++){//Iterate through time increments for (std::string const& var : vars) {// Iterate through the devices //Make sure we have a device if(var.empty()){ - //mf::LogDebug("BNBRetriever") << " NO MWR DEVICES?!" << std::endl; + //mf::LogDebug("SBNDBNBRetriever") << " NO MWR DEVICES?!" << std::endl; continue; } /// Check the device name and interate the double-vector index @@ -261,7 +264,7 @@ SBNDBNB::MWRdata_t SBNDBNB::extractSpillTimes(TriggerInfo_t const& triggerInfo) 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("SBNDBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; continue;} time_for_mwr = 0; @@ -315,17 +318,17 @@ SBNDBNB::MWRdata_t SBNDBNB::extractSpillTimes(TriggerInfo_t const& triggerInfo) }// Iterate over all the multiwire devices }// Iterate over all times - mf::LogDebug("BNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; return { std::move(MWR_times), std::move(unpacked_MWR) }; } -int SBNDBNB::matchMultiWireData( +int SBNDBNBRetriever::matchMultiWireData( art::EventID const& eventID, TriggerInfo_t const& triggerInfo, MWRdata_t const& MWRdata, bool isFirstEventInRun, @@ -339,7 +342,7 @@ int SBNDBNB::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; + mf::LogDebug("SBNDBNBRetriever") << "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 @@ -378,25 +381,25 @@ int SBNDBNB::matchMultiWireData( ///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; + // mf::LogDebug("SBNDBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << 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; + //mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; - std::cout << std::setprecision(19) << "times_temps[i]: " << times_temps[i] << std::endl; + //std::cout << std::setprecision(19) << "times_temps[i]: " << times_temps[i] << std::endl; if(!isFirstEventInRun){//We already addressed the "first event" above if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ - //mf::LogDebug("BNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + //mf::LogDebug("SBNDBNBRetriever") << 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; + //mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; continue;} } @@ -407,10 +410,10 @@ int SBNDBNB::matchMultiWireData( 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; + // mf::LogDebug("SBNDBNBRetriever") << 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; + // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "matchMultiWireData:: Skipped a MinBias gate at : " << times_temps[i]*1000. << std::endl; // continue; //} @@ -468,12 +471,12 @@ int SBNDBNB::matchMultiWireData( }//end iteration over beam device times - // mf::LogDebug("BNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; return spill_count; } -sbn::BNBSpillInfo SBNDBNB::makeBNBSpillInfo +sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const { @@ -502,19 +505,19 @@ sbn::BNBSpillInfo SBNDBNB::makeBNBSpillInfo // since sometimes devices fail to report we'll // allow each to throw an exception but still move forward // interpreting these failures will be part of the beam quality analyses - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} //crunch the times unsigned long int time_closest_int = (int) TOR860_time; @@ -538,7 +541,7 @@ sbn::BNBSpillInfo SBNDBNB::makeBNBSpillInfo beamInfo.spill_time_s = time_closest_int; beamInfo.spill_time_ns = time_closest_ns; - std::cout << "TOR860: " << TOR860 << std::endl; + // std::cout << "TOR860: " << TOR860 << std::endl; for(auto const& MWRdata: unpacked_MWR){ std::ignore = MWRdata; @@ -580,15 +583,15 @@ sbn::BNBSpillInfo SBNDBNB::makeBNBSpillInfo return beamInfo; } -void SBNDBNB::beginSubRun(art::SubRun& sr) +void SBNDBNBRetriever::beginSubRun(art::SubRun& sr) { return; } -void SBNDBNB::endSubRun(art::SubRun& sr) +void SBNDBNBRetriever::endSubRun(art::SubRun& sr) { - //mf::LogDebug("BNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; - //mf::LogDebug("BNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; + //mf::LogDebug("SBNDBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; + //mf::LogDebug("SBNDBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; //auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); //std::swap(*p, fOutbeamInfos); @@ -598,4 +601,4 @@ void SBNDBNB::endSubRun(art::SubRun& sr) return; } -DEFINE_ART_MODULE(SBNDBNB) +DEFINE_ART_MODULE(SBNDBNBRetriever) From 225eaf9ce98265eb81a42fb4e871800f9fc8f36d Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Mon, 21 Oct 2024 15:21:13 -0500 Subject: [PATCH 04/26] debug and cache changes --- .../BNBRetriever/BNBRetriever_module.cc | 2 +- .../BNBRetriever/SBNDBNBRetriever_module.cc | 47 ++++++++++++------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc index 2265d0e2c..e8a20cb0e 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc @@ -388,7 +388,7 @@ 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) {} diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 2ceaf32bc..1ed19a34d 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" #include "artdaq-core/Data/ContainerFragment.hh" @@ -96,6 +97,7 @@ SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) fOutputInstance = params.get("OutputInstance"); fDebugLevel = params.get("DebugLevel",0); bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); + //bfp->set_epsilon(0.02); bfp->set_epsilon(0.05); bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); bfp_mwr->set_epsilon(0.5); @@ -161,10 +163,9 @@ void SBNDBNBRetriever::produce(art::Event & e) TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - std::cout << "Gate Type: " << triggerInfo.gate_type << std::endl; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); + //std::cout << "event: " << e.event() << std::endl; int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) @@ -188,7 +189,6 @@ SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event //std::string data = frag.GetDataString(); //char *buffer = const_cast(data.c_str()); //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.number_of_gates_since_previous_event = 1; @@ -202,11 +202,7 @@ SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event */ triggerInfo.t_current_event = static_cast(artdaq_ts-3.6e7)/(1000000000.0); //check this offset... - if(triggerInfo.gate_type == 1) - triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); - else - triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(frag.getLastTimestampOther()-3.6e7))/(1000000000.0); - + triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); } 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; @@ -217,9 +213,11 @@ SBNDBNBRetriever::MWRdata_t SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t co // These lines get everything primed within the IFBeamDB // They seem redundant but they are needed - try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {std::cout << "caught 1" << std::endl;} - try{auto packed_M876BB_temp = bfp_mwr->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:M875BB{4440:888}.RAW");} catch (WebAPIException &we) {std::cout << "caught 2" << std::endl;} - + // try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_previous_event)-fTimePad,"E:THCURR");} catch (WebAPIException &we) {std::cout << "caught 1" << std::endl;} + // try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {std::cout << "caught 1" << std::endl;} + try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {} + try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 @@ -341,7 +339,7 @@ int SBNDBNBRetriever::matchMultiWireData( // First we get the times that the beamline device fired // we have to pick a specific variable to use std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); - + mf::LogDebug("SBNDBNBRetriever") << "matchMultiWireData:: Number of time spills : " << times_temps.size() << std::endl; // We'll keep track of how many of these spills match to our @@ -351,6 +349,7 @@ int SBNDBNBRetriever::matchMultiWireData( std::vector matched_MWR; matched_MWR.resize(3); + std::cout << std::setprecision(19) << "t_previous_event: " << triggerInfo.t_previous_event << std::endl; std::cout << std::setprecision(19) << "t_current_event: " << triggerInfo.t_current_event << std::endl; // NOTE: for now, this is dead code because we don't // do anything for the first event in a run. We may want to revisit @@ -359,7 +358,6 @@ int SBNDBNBRetriever::matchMultiWireData( // // Need to handle the first event in a run differently if(isFirstEventInRun){ - //We'll remove the spills after our event int spills_after_our_target = 0; // iterate through all the spills to find the @@ -389,9 +387,8 @@ int SBNDBNBRetriever::matchMultiWireData( for (size_t i = 0; i < times_temps.size(); i++) { // Only continue if these times are matched to our DAQ time - //mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; - //std::cout << std::setprecision(19) << "times_temps[i]: " << times_temps[i] << std::endl; if(!isFirstEventInRun){//We already addressed the "first event" above if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ //mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; @@ -505,6 +502,7 @@ sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo // since sometimes devices fail to report we'll // allow each to throw an exception but still move forward // interpreting these failures will be part of the beam quality analyses + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} @@ -518,7 +516,24 @@ sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - + + std::cout << std::setprecision(19) << "spill_time: " << TOR860_time << std::endl; + +/* + std::cout << std::setprecision(13) << "TOR860: " << TOR860 << std::endl; + std::cout << std::setprecision(13) << "TOR875: " << TOR875 << std::endl; + std::cout << std::setprecision(13) << "LM875A: " << LM875A << std::endl; + std::cout << std::setprecision(13) << "LM875B: " << LM875B << std::endl; + std::cout << std::setprecision(13) << "LM875C: " << LM875C << std::endl; + std::cout << std::setprecision(13) << "HP875: " << HP875 << std::endl; + std::cout << std::setprecision(13) << "VP875: " << VP875 << std::endl; + std::cout << std::setprecision(13) << "HPTG1: " << HPTG1 << std::endl; + std::cout << std::setprecision(13) << "VPTG1: " << VPTG1 << std::endl; + std::cout << std::setprecision(13) << "HPTG2: " << HPTG2 << std::endl; + std::cout << std::setprecision(13) << "VPTG2: " << VPTG2 << std::endl; + std::cout << std::setprecision(13) << "BTJT2: " << BTJT2 << std::endl; + std::cout << std::setprecision(13) << "THCURR: " << THCURR << std::endl; +*/ //crunch the times unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; From 030248d8b22a895ccaa807116903734860dc337b Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Fri, 25 Oct 2024 09:26:53 -0500 Subject: [PATCH 05/26] Fill cache --- .../BNBRetriever/SBNDBNBRetriever_module.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 1ed19a34d..c09d7c878 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -214,9 +214,11 @@ SBNDBNBRetriever::MWRdata_t SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t co // 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) {std::cout << "caught 1" << std::endl;} - // try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {std::cout << "caught 1" << std::endl;} - try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {} + // try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {std::cout << "caught 1" << std::endl;} + try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {} try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} + //std::cout << std::setprecision(19) << "bfp->GetCacheStartTime()" << bfp->GetCacheStartTime() << std::endl; + //std::cout << std::setprecision(19) << "bfp->GetCacheEndTime()" << bfp->GetCacheEndTime() << std::endl; 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 @@ -519,7 +521,7 @@ sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo std::cout << std::setprecision(19) << "spill_time: " << TOR860_time << std::endl; -/* + std::cout << std::setprecision(13) << "TOR860: " << TOR860 << std::endl; std::cout << std::setprecision(13) << "TOR875: " << TOR875 << std::endl; std::cout << std::setprecision(13) << "LM875A: " << LM875A << std::endl; @@ -533,7 +535,7 @@ sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo std::cout << std::setprecision(13) << "VPTG2: " << VPTG2 << std::endl; std::cout << std::setprecision(13) << "BTJT2: " << BTJT2 << std::endl; std::cout << std::setprecision(13) << "THCURR: " << THCURR << std::endl; -*/ + //crunch the times unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; From 076033d70e5e6c4aaad13cc017aa5e2226331065 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Mon, 28 Oct 2024 10:18:37 -0500 Subject: [PATCH 06/26] Get timestamp from PTB --- .../BNBRetriever/SBNDBNBRetriever_module.cc | 64 ++++++++++--------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index c09d7c878..84689ea43 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -175,37 +175,43 @@ void SBNDBNBRetriever::produce(art::Event & e) } SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { - //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 - auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ContainerTDCTIMESTAMP" }); + art::InputTag itag("daq", "ContainerPTB"); + auto cont_frags = e.getHandle(itag); + int numcont = 0; TriggerInfo_t triggerInfo; - /// Correction added to the White Rabbit time to get the trigger time. - for(auto raw_datum : raw_data){ - - uint64_t artdaq_ts = raw_datum.timestamp(); - //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); - //triggerInfo.number_of_gates_since_previous_event = frag.getDeltaGatesBNBMaj(); - triggerInfo.number_of_gates_since_previous_event = 1; - - /* - 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... - triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); - } - - 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; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag =0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck + for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) + { + if(ctb_frag.Trigger(word_i)){ + uint32_t wt = 0; + uint32_t word_type = ctb_frag.Word(word_i)->word_type; + wt = word_type; + uint64_t NR_trigger_word = ctb_frag.Trigger(word_i)->trigger_word & 0x1FFFFFFFFFFFFFFF; //& 0x1FFFFFFFFFFF extracts the 61 bit payload + uint64_t NR_timestamp = ctb_frag.Trigger(word_i)->timestamp * 20; + if (wt == 2) + { + std::cout << "PTB Word type [" << std::bitset<3>(ctb_frag.Word(word_i)->word_type) << "] "; + std::cout << std::bitset<32>(NR_trigger_word) << " HLT Timestamp: " << std::bitset<64>(NR_timestamp/20) <(NR_timestamp/20).to_ullong()/50e6; + triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(ctb_frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); + std::cout << std::setprecision(19) << "triggerInfo.t_current_event: " << triggerInfo.t_current_event << std::endl; + std::cout << std::setprecision(19) << "triggerInfo.t_previous_event: " << triggerInfo.t_previous_event << std::endl; + } + } + } //End of loop over the number of trigger words + } //End of loop over the number of fragments per container + } //End of loop over the number of containers return triggerInfo; } From 8b5216d6738b2356c2dcf22069a8949b89065376 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Mon, 28 Oct 2024 13:19:05 -0500 Subject: [PATCH 07/26] Refactor PTB ts extraction --- .../BNBRetriever/SBNDBNBRetriever_module.cc | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 84689ea43..856747f2e 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -75,6 +75,7 @@ class SBNDBNBRetriever : public art::EDProducer { static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + double extractPTBTimeStamp(art::Handle > cont_frags) const; MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; int matchMultiWireData( art::EventID const& eventID, @@ -174,17 +175,15 @@ void SBNDBNBRetriever::produce(art::Event & e) mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; } -SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { - art::InputTag itag("daq", "ContainerPTB"); - auto cont_frags = e.getHandle(itag); +double SBNDBNBRetriever::extractPTBTimeStamp(art::Handle > cont_frags) const { int numcont = 0; - TriggerInfo_t triggerInfo; - + uint64_t PTBTimeStamp = 0; + uint64_t PTBTriggerWord; for (auto const& cont : *cont_frags) - { + { artdaq::ContainerFragment cont_frag(cont); numcont++; - int numfrag =0; + int numfrag = 0; for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) { numfrag++; @@ -196,22 +195,35 @@ SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event uint32_t wt = 0; uint32_t word_type = ctb_frag.Word(word_i)->word_type; wt = word_type; - uint64_t NR_trigger_word = ctb_frag.Trigger(word_i)->trigger_word & 0x1FFFFFFFFFFFFFFF; //& 0x1FFFFFFFFFFF extracts the 61 bit payload - uint64_t NR_timestamp = ctb_frag.Trigger(word_i)->timestamp * 20; - if (wt == 2) + PTBTriggerWord = ctb_frag.Trigger(word_i)->trigger_word & 0x1FFFFFFFFFFFFFFF; //& 0x1FFFFFFFFFFF extracts the 61 bit payload + std::bitset<32> desired_word{"00000000000000000000000000000010"};// Only use timestamp from specific HLT trigger word + if (wt == 2 && std::bitset<32>(PTBTriggerWord) == desired_word) { + PTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; std::cout << "PTB Word type [" << std::bitset<3>(ctb_frag.Word(word_i)->word_type) << "] "; - std::cout << std::bitset<32>(NR_trigger_word) << " HLT Timestamp: " << std::bitset<64>(NR_timestamp/20) <(NR_timestamp/20).to_ullong()/50e6; - triggerInfo.t_previous_event = triggerInfo.t_current_event - 10;//(static_cast(ctb_frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); - std::cout << std::setprecision(19) << "triggerInfo.t_current_event: " << triggerInfo.t_current_event << std::endl; - std::cout << std::setprecision(19) << "triggerInfo.t_previous_event: " << triggerInfo.t_previous_event << std::endl; + std::cout << std::bitset<32>(PTBTriggerWord) << " HLT Timestamp: " << std::bitset<64>(PTBTimeStamp/20) <(PTBTimeStamp/20).to_ullong()/50e6; +} + +SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { + art::InputTag itag("daq", "ContainerPTB"); + auto cont_frags = e.getHandle(itag); + + TriggerInfo_t triggerInfo; + double PTBTimeStamp = extractPTBTimeStamp(cont_frags); + + triggerInfo.t_current_event = PTBTimeStamp; + triggerInfo.t_previous_event = PTBTimeStamp-10; + triggerInfo.number_of_gates_since_previous_event = 1; + + std::cout << std::setprecision(19) << "triggerInfo.t_current_event: " << triggerInfo.t_current_event << std::endl; + std::cout << std::setprecision(19) << "triggerInfo.t_previous_event: " << triggerInfo.t_previous_event << std::endl; + return triggerInfo; } From 574348ae922ab6f0af237323e58fe87994172cf8 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Fri, 1 Nov 2024 17:41:54 -0500 Subject: [PATCH 08/26] Add TDC Timestamp func --- .../BNBRetriever/SBNDBNBRetriever_module.cc | 83 ++++++++----------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 856747f2e..0ba84d7c3 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -21,6 +21,7 @@ #include #include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" +#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" #include "artdaq-core/Data/ContainerFragment.hh" //#include "sbndcode/Decoders/PTB/sbndptb.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" @@ -76,6 +77,7 @@ class SBNDBNBRetriever : public art::EDProducer { TriggerInfo_t extractTriggerInfo(art::Event const& e) const; double extractPTBTimeStamp(art::Handle > cont_frags) const; + double extractTDCTimeStamp(art::Handle > cont_frags) const; MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; int matchMultiWireData( art::EventID const& eventID, @@ -118,50 +120,6 @@ void SBNDBNBRetriever::produce(art::Event & e) // spill that the DAQ was sensitive to, so don't try to save any // spill information -/* - art::InputTag ptb_tag { "ptbdecoder" }; - auto const& ptb_handle = e.getValidHandle< std::vector >(ptb_tag); - auto const& ptb_vec(*ptb_handle); - int ptb_size = ptb_vec.size(); - int NHL_trig; - if(ptb_size > 0){ - for (int j = 0; j < ptb_size; j++){ - if(ptb_vec.at(j).GetNHLTriggers() > 0){ - NHL_trig = ptb_vec.at(j).GetNHLTriggers(); - for(int k = 0; k < NHL_trig; k++){ - ULong64_t trig_time = ptb_vec.at(j).GetHLTrigger(k).timestamp; - if(ptb_vec.at(j).GetHLTrigger(k).trigger_word != 1048576){ - ULong64_t curr_min = 1e19; - //int curr_min_ind; - std::cout << "Zero Bias Trigger" << std::endl; - try{ - //auto vec_temp = bfp_mwr->GetNamedVector(trig_time/1e9,"E:M875BB{4440:888}.RAW"); - auto vec_temp = bfp->GetNamedVector(trig_time/1e9,"E:TOR875"); - //std::vector times = bfp->GetTimeList("E:M875BB"); - std::vector times = bfp->GetTimeList("E:TOR875"); - int times_size = times.size(); - for (int i = 0; i < times_size; i++) { - if(times[i] < trig_time/1e9){ - if(trig_time/1e9 - times[i] < curr_min){ - curr_min = trig_time - times[i]; - std::cout << "trig_time: " << trig_time << std::endl; - //curr_min_ind = i; - } - } - } - }catch(WebAPIException &we){std::cout << "Failed GetNamedVector" << std::endl;} - } - else{ - std::cout << "CRT Reset Trigger" << std::endl; - } - } - } - } - } -*/ - //sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); - //fOutbeamInfos.push_back(std::move(spillInfo)); - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); @@ -210,15 +168,42 @@ double SBNDBNBRetriever::extractPTBTimeStamp(art::Handle(PTBTimeStamp/20).to_ullong()/50e6; } +double SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { + int numcont = 0; + uint64_t TDCTimeStamp = 0; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag = 0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::TDCTimestampFragment tdc_frag(frag); + TDCTimeStamp = tdc_frag.getTDCTimestamp()->timestamp_ns()/1e9; + } //End of loop over the number of fragments per container + } //End of loop over the number of containers + return TDCTimeStamp; +} + SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { - art::InputTag itag("daq", "ContainerPTB"); - auto cont_frags = e.getHandle(itag); + // Using TDC for current event, but PTB for previous event + art::InputTag PTB_itag("daq", "ContainerPTB"); + auto PTB_cont_frags = e.getHandle(PTB_itag); + + art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); + auto TDC_cont_frags = e.getHandle(TDC_itag); TriggerInfo_t triggerInfo; - double PTBTimeStamp = extractPTBTimeStamp(cont_frags); + double PTBTimeStamp = extractPTBTimeStamp(PTB_cont_frags); + double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + + std::cout << "PTBTimeStamp: " << PTBTimeStamp << std::endl; + std::cout << "TDCTimeStamp: " << TDCTimeStamp << std::endl; - triggerInfo.t_current_event = PTBTimeStamp; - triggerInfo.t_previous_event = PTBTimeStamp-10; + triggerInfo.t_current_event = TDCTimeStamp; + triggerInfo.t_previous_event = TDCTimeStamp-10; triggerInfo.number_of_gates_since_previous_event = 1; std::cout << std::setprecision(19) << "triggerInfo.t_current_event: " << triggerInfo.t_current_event << std::endl; From fc0776af32f9a95eda2e61c7b34161595d729c3b Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Sun, 10 Nov 2024 19:32:03 -0600 Subject: [PATCH 09/26] Add prev ptb timestamp --- Module.txt | 1 + .../BNBRetriever/BNBRetriever_module.cc | 5 ++--- .../BNBRetriever/SBNDBNBRetriever_module.cc | 14 ++++---------- 3 files changed, 7 insertions(+), 13 deletions(-) create mode 100644 Module.txt diff --git a/Module.txt b/Module.txt new file mode 100644 index 000000000..16f27c055 --- /dev/null +++ b/Module.txt @@ -0,0 +1 @@ +Art has completed and will exit with status 90. diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc index e8a20cb0e..88c20b40d 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc @@ -388,9 +388,8 @@ 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_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) {} + try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} + try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} //The multiwire chambers provide their // data in a vector format but we'll have diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 0ba84d7c3..54bd2f29c 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -157,7 +157,7 @@ double SBNDBNBRetriever::extractPTBTimeStamp(art::Handle desired_word{"00000000000000000000000000000010"};// Only use timestamp from specific HLT trigger word if (wt == 2 && std::bitset<32>(PTBTriggerWord) == desired_word) { - PTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; + PTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; std::cout << "PTB Word type [" << std::bitset<3>(ctb_frag.Word(word_i)->word_type) << "] "; std::cout << std::bitset<32>(PTBTriggerWord) << " HLT Timestamp: " << std::bitset<64>(PTBTimeStamp/20) <(PTB_itag); - art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); - auto TDC_cont_frags = e.getHandle(TDC_itag); + // art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); + // auto TDC_cont_frags = e.getHandle(TDC_itag); TriggerInfo_t triggerInfo; double PTBTimeStamp = extractPTBTimeStamp(PTB_cont_frags); double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - std::cout << "PTBTimeStamp: " << PTBTimeStamp << std::endl; - std::cout << "TDCTimeStamp: " << TDCTimeStamp << std::endl; - triggerInfo.t_current_event = TDCTimeStamp; - triggerInfo.t_previous_event = TDCTimeStamp-10; + triggerInfo.t_previous_event = PTBTimeStamp-10; triggerInfo.number_of_gates_since_previous_event = 1; - std::cout << std::setprecision(19) << "triggerInfo.t_current_event: " << triggerInfo.t_current_event << std::endl; - std::cout << std::setprecision(19) << "triggerInfo.t_previous_event: " << triggerInfo.t_previous_event << std::endl; - return triggerInfo; } From c5aaeb9fb55e3a4cd5f37e4e1f13d38fad9de02f Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Sun, 10 Nov 2024 19:52:48 -0600 Subject: [PATCH 10/26] Cleaning print statements --- .../BNBRetriever/SBNDBNBRetriever_module.cc | 70 ++++++------------- 1 file changed, 21 insertions(+), 49 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 54bd2f29c..91b1694c2 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -100,8 +100,7 @@ SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) fOutputInstance = params.get("OutputInstance"); fDebugLevel = params.get("DebugLevel",0); bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); - //bfp->set_epsilon(0.02); - bfp->set_epsilon(0.05); + bfp->set_epsilon(0.02); bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); bfp_mwr->set_epsilon(0.5); bfp_mwr->setValidWindow(3605); @@ -124,7 +123,6 @@ void SBNDBNBRetriever::produce(art::Event & e) MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); - //std::cout << "event: " << e.event() << std::endl; int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) @@ -133,9 +131,9 @@ void SBNDBNBRetriever::produce(art::Event & e) mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; } -double SBNDBNBRetriever::extractPTBTimeStamp(art::Handle > cont_frags) const { +double SBNDBNBRetriever::extractprevPTBTimeStamp(art::Handle > cont_frags) const { int numcont = 0; - uint64_t PTBTimeStamp = 0; + uint64_t prevPTBTimeStamp = 0; uint64_t PTBTriggerWord; for (auto const& cont : *cont_frags) { @@ -157,15 +155,13 @@ double SBNDBNBRetriever::extractPTBTimeStamp(art::Handle desired_word{"00000000000000000000000000000010"};// Only use timestamp from specific HLT trigger word if (wt == 2 && std::bitset<32>(PTBTriggerWord) == desired_word) { - PTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; - std::cout << "PTB Word type [" << std::bitset<3>(ctb_frag.Word(word_i)->word_type) << "] "; - std::cout << std::bitset<32>(PTBTriggerWord) << " HLT Timestamp: " << std::bitset<64>(PTBTimeStamp/20) <prevTS * 20; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers - return std::bitset<64>(PTBTimeStamp/20).to_ullong()/50e6; + return std::bitset<64>(prevPTBTimeStamp/20).to_ullong()/50e6; } double SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { @@ -192,32 +188,29 @@ SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event art::InputTag PTB_itag("daq", "ContainerPTB"); auto PTB_cont_frags = e.getHandle(PTB_itag); - // art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); - // auto TDC_cont_frags = e.getHandle(TDC_itag); + art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); + auto TDC_cont_frags = e.getHandle(TDC_itag); TriggerInfo_t triggerInfo; - double PTBTimeStamp = extractPTBTimeStamp(PTB_cont_frags); + double prevPTBTimeStamp = extractprevPTBTimeStamp(PTB_cont_frags); double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); triggerInfo.t_current_event = TDCTimeStamp; - triggerInfo.t_previous_event = PTBTimeStamp-10; - triggerInfo.number_of_gates_since_previous_event = 1; + triggerInfo.t_previous_event = prevPTBTimeStamp; + triggerInfo.number_of_gates_since_previous_event = -999; return triggerInfo; } SBNDBNBRetriever::MWRdata_t SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { - // 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) {std::cout << "caught 1" << std::endl;} - // try{auto cur_vec_temp = bfp->GetNamedVector((triggerInfo.t_current_event)+fTimePad,"E:THCURR");} catch (WebAPIException &we) {std::cout << "caught 1" << std::endl;} + // These lines get everything primed within the IFBeamDB. try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {} try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} - //std::cout << std::setprecision(19) << "bfp->GetCacheStartTime()" << bfp->GetCacheStartTime() << std::endl; - //std::cout << std::setprecision(19) << "bfp->GetCacheEndTime()" << bfp->GetCacheEndTime() << std::endl; - 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 + try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad)} catch (WebAPIException &we) {} + try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad)} 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 // to correctly unpack it @@ -227,7 +220,7 @@ SBNDBNBRetriever::MWRdata_t SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t co MWR_times.resize(3); std::string packed_data_str; - //Create a list of all the MWR devices with their different + // Create a list of all the MWR devices with their different // memory buffer increments // generally in the format: "E:.{Memory Block}" std::vector vars = bfp_mwr->GetDeviceList(); @@ -348,8 +341,6 @@ int SBNDBNBRetriever::matchMultiWireData( std::vector matched_MWR; matched_MWR.resize(3); - std::cout << std::setprecision(19) << "t_previous_event: " << triggerInfo.t_previous_event << std::endl; - std::cout << std::setprecision(19) << "t_current_event: " << triggerInfo.t_current_event << std::endl; // NOTE: for now, this is dead code because we don't // do anything for the first event in a run. We may want to revisit // this later to understand if there is a way we can do the POT @@ -516,23 +507,6 @@ sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - std::cout << std::setprecision(19) << "spill_time: " << TOR860_time << std::endl; - - - std::cout << std::setprecision(13) << "TOR860: " << TOR860 << std::endl; - std::cout << std::setprecision(13) << "TOR875: " << TOR875 << std::endl; - std::cout << std::setprecision(13) << "LM875A: " << LM875A << std::endl; - std::cout << std::setprecision(13) << "LM875B: " << LM875B << std::endl; - std::cout << std::setprecision(13) << "LM875C: " << LM875C << std::endl; - std::cout << std::setprecision(13) << "HP875: " << HP875 << std::endl; - std::cout << std::setprecision(13) << "VP875: " << VP875 << std::endl; - std::cout << std::setprecision(13) << "HPTG1: " << HPTG1 << std::endl; - std::cout << std::setprecision(13) << "VPTG1: " << VPTG1 << std::endl; - std::cout << std::setprecision(13) << "HPTG2: " << HPTG2 << std::endl; - std::cout << std::setprecision(13) << "VPTG2: " << VPTG2 << std::endl; - std::cout << std::setprecision(13) << "BTJT2: " << BTJT2 << std::endl; - std::cout << std::setprecision(13) << "THCURR: " << THCURR << std::endl; - //crunch the times unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; @@ -555,8 +529,6 @@ sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo beamInfo.spill_time_s = time_closest_int; beamInfo.spill_time_ns = time_closest_ns; - // std::cout << "TOR860: " << TOR860 << std::endl; - for(auto const& MWRdata: unpacked_MWR){ std::ignore = MWRdata; assert(!MWRdata.empty()); @@ -604,13 +576,13 @@ void SBNDBNBRetriever::beginSubRun(art::SubRun& sr) void SBNDBNBRetriever::endSubRun(art::SubRun& sr) { - //mf::LogDebug("SBNDBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; - //mf::LogDebug("SBNDBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; + mf::LogDebug("SBNDBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; + mf::LogDebug("SBNDBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; - //auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); - //std::swap(*p, fOutbeamInfos); + auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); + std::swap(*p, fOutbeamInfos); - //sr.put(std::move(p), art::subRunFragment()); + sr.put(std::move(p), art::subRunFragment()); return; } From f5288577bee1902a8b667689f8b430b167ff2175 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Sun, 10 Nov 2024 19:56:00 -0600 Subject: [PATCH 11/26] Undo build fix, get rid of junk --- Module.txt | 1 - sbncode/CAFMaker/FillReco.cxx | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) delete mode 100644 Module.txt diff --git a/Module.txt b/Module.txt deleted file mode 100644 index 16f27c055..000000000 --- a/Module.txt +++ /dev/null @@ -1 +0,0 @@ -Art has completed and will exit with status 90. diff --git a/sbncode/CAFMaker/FillReco.cxx b/sbncode/CAFMaker/FillReco.cxx index 0c35b8a59..a478bc840 100644 --- a/sbncode/CAFMaker/FillReco.cxx +++ b/sbncode/CAFMaker/FillReco.cxx @@ -119,8 +119,8 @@ namespace caf srspacepoint.position = SRVector3D(spacepoint.X(), spacepoint.Y(), spacepoint.Z()); srspacepoint.position_err = SRVector3D(spacepoint.XErr(), spacepoint.YErr(), spacepoint.ZErr()); srspacepoint.pe = spacepoint.PE(); - srspacepoint.time = spacepoint.Time(); - srspacepoint.time_err = spacepoint.TimeErr(); + srspacepoint.time = spacepoint.Ts1(); + srspacepoint.time_err = spacepoint.Ts1Err(); srspacepoint.complete = spacepoint.Complete(); } @@ -131,8 +131,8 @@ namespace caf for(auto const& point : track.Points()) srsbndcrttrack.points.emplace_back(point.X(), point.Y(), point.Z()); - srsbndcrttrack.time = track.Time(); - srsbndcrttrack.time_err = track.TimeErr(); + srsbndcrttrack.time = track.Ts1(); + srsbndcrttrack.time_err = track.Ts1Err(); srsbndcrttrack.pe = track.PE(); srsbndcrttrack.tof = track.ToF(); } From 8a7fbf8e151ddc25613d94e082a9743393b8624a Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Mon, 11 Nov 2024 18:26:25 -0600 Subject: [PATCH 12/26] Moving fcls to jobs --- .../BNBRetriever/SBNDBNBRetriever_module.cc | 42 +++++++++-------- .../BNBRetriever/runbnb.fcl | 31 ------------- .../job/run_sbndbnbinfo_sbn.fcl | 45 +++++++++++++++++++ .../sbndbnbspillinfo.fcl} | 2 +- sbncode/CAFMaker/FillReco.cxx | 8 ++-- 5 files changed, 74 insertions(+), 54 deletions(-) delete mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl rename sbncode/BeamSpillInfoRetriever/{BNBRetriever/SBNDBNBDefaults.fcl => job/sbndbnbspillinfo.fcl} (96%) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc index 91b1694c2..bb970c366 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc @@ -62,6 +62,11 @@ class SBNDBNBRetriever : public art::EDProducer { std::unique_ptr bfp; std::unique_ptr bfp_mwr; + struct PTBInfo_t { + double prevPTBTimeStamp = 0; + unsigned int GateCounter = 0; // FIXME needs to be integral type + }; + struct TriggerInfo_t { double t_current_event = 0; double t_previous_event = 0; @@ -76,7 +81,7 @@ class SBNDBNBRetriever : public art::EDProducer { static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - double extractPTBTimeStamp(art::Handle > cont_frags) const; + PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; double extractTDCTimeStamp(art::Handle > cont_frags) const; MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; int matchMultiWireData( @@ -85,7 +90,7 @@ class SBNDBNBRetriever : public art::EDProducer { MWRdata_t const& MWRdata, bool isFirstEventInRun, std::vector< sbn::BNBSpillInfo >& beamInfos ) const; - + unsigned int TotalBeamSpills; sbn::BNBSpillInfo makeBNBSpillInfo (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; }; @@ -104,6 +109,7 @@ SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); bfp_mwr->set_epsilon(0.5); bfp_mwr->setValidWindow(3605); + TotalBeamSpills = 0; } int eventNum =0; @@ -120,7 +126,7 @@ void SBNDBNBRetriever::produce(art::Event & e) // spill information TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - + TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); @@ -131,10 +137,9 @@ void SBNDBNBRetriever::produce(art::Event & e) mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; } -double SBNDBNBRetriever::extractprevPTBTimeStamp(art::Handle > cont_frags) const { +SBNDBNBRetriever::PTBInfo_t SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { int numcont = 0; - uint64_t prevPTBTimeStamp = 0; - uint64_t PTBTriggerWord; + PTBInfo_t PTBInfo; for (auto const& cont : *cont_frags) { artdaq::ContainerFragment cont_frag(cont); @@ -151,17 +156,17 @@ double SBNDBNBRetriever::extractprevPTBTimeStamp(art::Handleword_type; wt = word_type; - PTBTriggerWord = ctb_frag.Trigger(word_i)->trigger_word & 0x1FFFFFFFFFFFFFFF; //& 0x1FFFFFFFFFFF extracts the 61 bit payload - std::bitset<32> desired_word{"00000000000000000000000000000010"};// Only use timestamp from specific HLT trigger word - if (wt == 2 && std::bitset<32>(PTBTriggerWord) == desired_word) + if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) { - prevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers - return std::bitset<64>(prevPTBTimeStamp/20).to_ullong()/50e6; + return PTBInfo; } double SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { @@ -191,13 +196,14 @@ SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); auto TDC_cont_frags = e.getHandle(TDC_itag); + PTBInfo_t PTBInfo; TriggerInfo_t triggerInfo; - double prevPTBTimeStamp = extractprevPTBTimeStamp(PTB_cont_frags); + PTBInfo = extractPTBInfo(PTB_cont_frags); double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); triggerInfo.t_current_event = TDCTimeStamp; - triggerInfo.t_previous_event = prevPTBTimeStamp; - triggerInfo.number_of_gates_since_previous_event = -999; + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; + triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; return triggerInfo; } @@ -205,10 +211,10 @@ SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event SBNDBNBRetriever::MWRdata_t SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { // These lines get everything primed within the IFBeamDB. - try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {} - try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} - try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad)} catch (WebAPIException &we) {} - try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad)} catch (WebAPIException &we) {} + try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; + try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {}; + try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; + try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {}; // The multiwire chambers provide their // data in a vector format but we'll have diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl b/sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl deleted file mode 100644 index 780b32bb3..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/runbnb.fcl +++ /dev/null @@ -1,31 +0,0 @@ -#include "SBNDBNBDefaults.fcl" - -services:{ - IFBeam:{} -} - -physics: -{ - producers: - { - bnb: @local::SBNDBNBDefaults - } - - analyzers: {} - - my_producer_modules: [bnb] - trigger_paths: [my_producer_modules] - a: [rootout] - end_paths: [a] -} - -source: {} - -outputs: { - rootout: { - module_type: "RootOutput" - fileName: "BNB_out.root" - } -} - -process_name: BNBTEST diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl new file mode 100644 index 000000000..b14918934 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl @@ -0,0 +1,45 @@ +#include "sbndbnbspillinfo.fcl" + +process_name: SBNDBNBInfoGen + +services:{ + + message: { + debugModules: [ "*" ] + destinations: { + LogDebugFile:{ + type: "file" + filename: "debug.log" + append: false + threshold: "DEBUG" + categories: { + default: {} + } + } + } + } + IFBeam:{} +} + + +source: { + +} + +physics: { + producers: { + sbndbnbinfo: @local::sbndbnbspillinfo + } + + simulate: [sbndbnbinfo ] + stream1: [ out1 ] +} + +outputs: { + out1: { + module_type: RootOutput + fileName: "%ifb_%tc_sbndbnbinfo.root" + dataTier: "raw" + compressionLevel: 1 + } +} diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl similarity index 96% rename from sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl rename to sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl index 03e3cdc87..2bf598456 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBDefaults.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl @@ -1,6 +1,6 @@ BEGIN_PROLOG -SBNDBNBDefaults: { +sbndbnbspillinfo: { module_type: SBNDBNBRetriever InputLabel: "bnb" InputContainerInstance: "ContainerBNB" diff --git a/sbncode/CAFMaker/FillReco.cxx b/sbncode/CAFMaker/FillReco.cxx index a478bc840..0c35b8a59 100644 --- a/sbncode/CAFMaker/FillReco.cxx +++ b/sbncode/CAFMaker/FillReco.cxx @@ -119,8 +119,8 @@ namespace caf srspacepoint.position = SRVector3D(spacepoint.X(), spacepoint.Y(), spacepoint.Z()); srspacepoint.position_err = SRVector3D(spacepoint.XErr(), spacepoint.YErr(), spacepoint.ZErr()); srspacepoint.pe = spacepoint.PE(); - srspacepoint.time = spacepoint.Ts1(); - srspacepoint.time_err = spacepoint.Ts1Err(); + srspacepoint.time = spacepoint.Time(); + srspacepoint.time_err = spacepoint.TimeErr(); srspacepoint.complete = spacepoint.Complete(); } @@ -131,8 +131,8 @@ namespace caf for(auto const& point : track.Points()) srsbndcrttrack.points.emplace_back(point.X(), point.Y(), point.Z()); - srsbndcrttrack.time = track.Ts1(); - srsbndcrttrack.time_err = track.Ts1Err(); + srsbndcrttrack.time = track.Time(); + srsbndcrttrack.time_err = track.TimeErr(); srsbndcrttrack.pe = track.PE(); srsbndcrttrack.tof = track.ToF(); } From 12b93b444c10ca13cb094df289bb1941f00e1a0c Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Tue, 12 Nov 2024 14:04:22 -0600 Subject: [PATCH 13/26] Rename ICARUS module --- .../BNBRetriever/BNBRetriever_module.cc | 780 ------------------ .../BNBRetriever/CMakeLists.txt | 55 -- .../BNBRetriever/MWRData.cpp | 71 -- .../BNBRetriever/MWRData.h | 39 - .../BNBRetriever/SBNDBNBRetriever_module.cc | 596 ------------- sbncode/BeamSpillInfoRetriever/CMakeLists.txt | 3 +- .../job/bnbspillinfo.fcl | 17 - .../job/run_bnbinfo_sbn.fcl | 46 -- 8 files changed, 2 insertions(+), 1605 deletions(-) delete mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc delete mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt delete mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.cpp delete mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.h delete mode 100644 sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc delete mode 100644 sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/run_bnbinfo_sbn.fcl diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc deleted file mode 100644 index 88c20b40d..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/BNBRetriever_module.cc +++ /dev/null @@ -1,780 +0,0 @@ -/** ******************************************************************** - * @file BNBRetriever_module.cc - * @date Wed April 9 2021 - * @author J. Zennamo (FNAL) - * - * 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/SubRun.h" -#include "canvas/Utilities/InputTag.h" -#include "fhiclcpp/types/Atom.h" -#include "messagefacility/MessageLogger/MessageLogger.h" -#include "larcorealg/CoreUtils/counter.h" - -#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" - -#include "ifdh_art/IFBeamService/IFBeam_service.h" -#include "ifbeam_c.h" -#include "MWRData.h" - -#include -#include -#include - -#include -#include -#include - -namespace sbn { - class BNBRetriever; -} - -class sbn::BNBRetriever : public art::EDProducer { -public: - - struct Config { - - using Name = fhicl::Name; - using Comment = fhicl::Comment; - - fhicl::Atom TimePadding { - Name{ "TimePadding" }, - Comment{ "extension to the time window considered when collecting spills [seconds]" }, - 0.0333 // default - }; - - fhicl::Atom RawDataLabel { - Name{ "raw_data_label" }, - Comment{ "art data product instance name for trigger information (product label is 'daq')" } - }; - - fhicl::Atom DeviceUsedForTiming { - Name{ "DeviceUsedForTiming" }, - Comment{ "name in the IFBeam database of the device used to extract spill times" } - }; - - fhicl::Atom URL { - Name{ "URL" }, - Comment{ "IFBeam database access URL" } - }; - - fhicl::Atom Bundle { - Name{ "Bundle" }, - Comment{ "" } // explain what this is and which database/table it's looking for - }; - - fhicl::Atom TimeWindow { - Name{ "TimeWindow" }, - Comment{ "" } // explain what this is, what's for and its unit - }; - - fhicl::Atom MultiWireBundle { - Name{ "MultiWireBundle" }, - Comment{ "" } // explain what this is and which database/table it's looking for - }; - - fhicl::Atom MWR_TimeWindow { - 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; - - - explicit BNBRetriever(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. - BNBRetriever(BNBRetriever const&) = delete; - BNBRetriever(BNBRetriever&&) = delete; - BNBRetriever& operator=(BNBRetriever const&) = delete; - BNBRetriever& operator=(BNBRetriever&&) = delete; - - // Required functions. - void produce(art::Event& e) override; - void beginSubRun(art::SubRun& sr) override; - void endSubRun(art::SubRun& sr) override; - -private: - // input labels - std::vector< sbn::BNBSpillInfo > fOutbeamInfos; - double fTimePad; - std::string fURL; - MWRData mwrdata; - int run_number; - std::string raw_data_label; - std::string fDeviceUsedForTiming; - unsigned int TotalBeamSpills; - // - art::ServiceHandle ifbeam_handle; - std::unique_ptr bfp; - std::unique_ptr bfp_mwr; - - // - std::string fTriggerDatabaseFile; - 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 { - std::vector< std::vector > MWR_times; - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - }; - - - static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] - - /// Returns the information of the trigger in the current event. - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - - /** - * @brief Determines spill times and extracts data based on multiwire devices. - * @param triggerInfo information from the trigger of this event - * @return times and unpacked data, per device (`"M875BB"`, `"M876BB"`, `"MMBTBB"`) - */ - MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; - - /** - * @brief Matches spill times with multiwire chamber data from the database. - * @param eventID ID of the event the information is associated to - * @param triggerInfo information from the trigger of this event - * @param MWRdata data from multiwire chambers - * @param isFirstEventInRun whether we are processing the first event of the run - * @param[out] beamInfos container to _add_ spill information records to - * @return count of matched spills - */ - int matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos - ) const; - - /** - * @brief Assembles and returns a spill information record. - * @param eventID ID of the event the information is associated to - * @param time time of the spill - * @param MWRdata all extracted data from multiwire chambers - * @param matched_MWR data from multiwire chambers matched with the time - * @return a `sbn::BNBSpillInfo` object with information on the spill at `time` - */ - 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, - float 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, float 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) - { - mf::LogError("BNBEXTRetriever") << "SQL error: " << sqlite3_errmsg(db); - trigger_type = -1; - } - return trigger_type; -} - -sbn::BNBRetriever::BNBRetriever(Parameters const& params) - : EDProducer{params}, - fTimePad(params().TimePadding()), - 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())), - fTriggerDatabaseFile(params().TriggerDatabaseFile()) -{ - - // Check fTimePad is positive - if (fTimePad < 0) { - throw art::Exception(art::errors::Configuration) - << "Parameter `TimePadding` must be non-negative (" << fTimePad << " was specified).\n"; - }//End Time check - - // how close in time does the spill time have to be from the DAQ time (in seconds). - // If these are too large then it fails to capture the device - // If these are too small then the time jitter in devices means we miss good data - // - // These values should likely not be changed unless authors of the IFBeam API are consulted - // - bfp->set_epsilon(0.02); //20 ms, this was tuned by hand and compared to IFBeamDB times - bfp_mwr->set_epsilon(0.5); - - //bfp_mwr->setValidWindow(86400); - bfp_mwr->setValidWindow(3605); - produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); - TotalBeamSpills = 0; - - 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); - } - -} - - -void sbn::BNBRetriever::produce(art::Event& e) -{ - - // If this is the first event in the run, then ignore it - // We do not currently have the ability to figure out the first - // spill that the DAQ was sensitive to, so don't try to save any - // 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 - if(triggerInfo.gate_type != 1) return; - // Keep track of the number of beam gates the DAQ thinks - // are in this job - TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - - - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); - - - int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); - - - 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; - -}//end iteration over art::Events - - -sbn::BNBRetriever::TriggerInfo_t sbn::BNBRetriever::extractTriggerInfo(art::Event const& e) const { - - //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 - - auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV3" }); - 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(); - 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); - 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); - else - 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 << ", Spill Count " << triggerInfo.number_of_gates_since_previous_event << std::endl; - - return triggerInfo; -} - - -sbn::BNBRetriever::MWRdata_t sbn::BNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { - - // These lines get everything primed within the IFBeamDB - // They seem redundant but they are needed - try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} - try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 - // to correctly unpack it - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - std::vector< std::vector< double> > MWR_times; - unpacked_MWR.resize(3); - MWR_times.resize(3); - std::string packed_data_str; - - //Create a list of all the MWR devices with their different - // memory buffer increments - // generally in the format: "E:.{Memory Block}" - std::vector vars = bfp_mwr->GetDeviceList(); - mf::LogDebug("BNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; - // Tracking the time from the IFBeamDB - double time_for_mwr; - - // this is an iterator to track which of the - // three devices we will be working with - int dev = 0; - - // The MWR devices are annoying and have confusing buffer - // what we'll do is sort through all of them first and then - // match them to the closest spills in time - // - - // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; - int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; - mf::LogDebug("BNBRetriever") << " t_steps " << t_steps << std::endl; - - for(int t = 0; t < t_steps; t++){//Iterate through time increments - for (std::string const& var : vars) {// Iterate through the devices - - //Make sure we have a device - if(var.empty()){ - //mf::LogDebug("BNBRetriever") << " NO MWR DEVICES?!" << std::endl; - continue; - } - /// Check the device name and interate the double-vector index - if(var.find("M875BB") != std::string::npos ) dev = 0; - 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; - continue;} - - time_for_mwr = 0; - - try{ - //Pull the MWR data for the device - // these data are "packed" - std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); - - //We'll convert this into a format - // that we can unpack doubles >> strings - // - packed_data_str.clear(); - packed_data_str += std::to_string(int(time_for_mwr)); - packed_data_str.append(","); - packed_data_str.append(var); - packed_data_str.append(",,"); - - /* for(auto const value: packed_MWR){ - packed_data_str += ','; - packed_data_str += std::to_string(int(value)); - }*/ - for(int j = 0; j < int(packed_MWR.size()); j++){ - packed_data_str += std::to_string(int(packed_MWR[j])); - if(j < int(packed_MWR.size())-1) - packed_data_str.append(","); - } - - // Use Zarko's unpacking function to turn this into consumeable data - std::vector MWR_times_temp; - - // There is a 35 ms offset between the toriod and the MWR times - // we'll just remove that here to match to the spill times - std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); - - //There are four events that are packed into one MWR IFBeam entry - for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ - - // If this entry has a unique time them store it for later - if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ - unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); - MWR_times[dev].push_back(MWR_times_temp[s]); - }//check for unique time - }//Iterate through the unpacked events - }//try - catch (WebAPIException &we) { - //Ignore when we can't find the MWR devices - // they don't always report and the timing of them can be annoying - - }//catch - }// Iterate over all the multiwire devices - }// Iterate over all times - - mf::LogDebug("BNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; - mf::LogDebug("BNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; - - return { std::move(MWR_times), std::move(unpacked_MWR) }; -} - - -int sbn::BNBRetriever::matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos -) const { - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - //Here we will start collecting all the other beamline devices - // First we get the times that the beamline device fired - // 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); - - - // NOTE: for now, this is dead code because we don't - // do anything for the first event in a run. We may want to revisit - // this later to understand if there is a way we can do the POT - // accounting in the first event. - // - // Need to handle the first event in a run differently - if(isFirstEventInRun){ - - //We'll remove the spills after our event - int spills_after_our_target = 0; - // iterate through all the spills to find the - // spills that are after our triggered event - for (size_t i = 0; i < times_temps.size(); i++) { - if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ - spills_after_our_target++; - } - }//end loop through spill times - - // Remove the spills after our trigger - times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); - - // 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)+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 - /* - 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){ - 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++; - - //Loop through the multiwire devices: - - for(int dev = 0; dev < int(MWR_times.size()); dev++){ - - //Loop through the multiwire times: - double Tdiff = 1000000000.; - matched_MWR[dev] = 0; - - for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ - - //found a candidate match! - if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} - - bool best_match = true; - - //Check for a better match... - 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;} - - //is there a better match later in the spill sequence - if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < - fabs((MWR_times[dev][mwrt] - times_temps[i]))){ - //we can have patience... - best_match = false; - break; - } - }//end better match check - - //Verified best match! - if(best_match == true){ - matched_MWR[dev] = mwrt; - Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); - } - - }//end loop over MWR times - - }//end loop over MWR devices - - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); - - beamInfos.push_back(std::move(spillInfo)); - - // 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 iteration over beam device times - - // mf::LogDebug("BNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; - - return spill_count; -} - - -sbn::BNBSpillInfo sbn::BNBRetriever::makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const -{ - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - // initializing all of our device carriers - // device definitions can be found in BNBSpillInfo.h - - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps - - double TOR860_time = 0; // units s - - // Here we request all the devices - // since sometimes devices fail to report we'll - // allow each to throw an exception but still move forward - // interpreting these failures will be part of the beam quality analyses - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("BNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - - //crunch the times - unsigned long int time_closest_int = (int) TOR860_time; - double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - - //Store everything in our data-product - sbn::BNBSpillInfo beamInfo; - beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.LM875A = LM875A; - beamInfo.LM875B = LM875B; - beamInfo.LM875C = LM875C; - beamInfo.HP875 = HP875; - beamInfo.VP875 = VP875; - beamInfo.HPTG1 = HPTG1; - beamInfo.VPTG1 = VPTG1; - beamInfo.HPTG2 = HPTG2; - beamInfo.VPTG2 = VPTG2; - beamInfo.BTJT2 = BTJT2; - beamInfo.THCURR = THCURR; - beamInfo.spill_time_s = time_closest_int; - beamInfo.spill_time_ns = time_closest_ns; - - for(auto const& MWRdata: unpacked_MWR){ - std::ignore = MWRdata; - assert(!MWRdata.empty()); - } - - if(unpacked_MWR[0].empty()){ - beamInfo.M875BB.clear(); - beamInfo.M875BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; - beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); - } - - if(unpacked_MWR[1].empty()){ - beamInfo.M876BB.clear(); - beamInfo.M876BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; - beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); - } - - if(unpacked_MWR[2].empty()){ - beamInfo.MMBTBB.clear(); - beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; - beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); - } - // 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 - - beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun - - return beamInfo; -} - - -void sbn::BNBRetriever::beginSubRun(art::SubRun& sr) -{ - return; -} - -//____________________________________________________________________________ -void sbn::BNBRetriever::endSubRun(art::SubRun& sr) -{ - // We will add all of the BNBSpillInfo data-products to the - // art::SubRun so it persists - // currently this is ~2.7 kB/event or ~0.07 kB/spill - -mf::LogDebug("BNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; -mf::LogDebug("BNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; - - auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); - std::swap(*p, fOutbeamInfos); - - sr.put(std::move(p), art::subRunFragment()); - - return; -} - -DEFINE_ART_MODULE(sbn::BNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt deleted file mode 100644 index 1f0dab80a..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/CMakeLists.txt +++ /dev/null @@ -1,55 +0,0 @@ -find_package(ifbeam) -find_package(ifdh_art) - - -art_make_library(LIBRARIES Boost::system - LIBRARY_NAME sbn_BNBSpillInfoRetriever_MWRData - SOURCE MWRData.cpp -) - - -cet_build_plugin(BNBRetriever art::module - LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - ROOT::X3d - Boost::system - 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 - artdaq_core::artdaq-core_Utilities - sbn_BNBSpillInfoRetriever_MWRData - sbnobj::Common_POTAccounting - larcorealg::CoreUtils -) - -cet_build_plugin(SBNDBNBRetriever art::module - LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - ROOT::X3d - Boost::system - 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 - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND - artdaq_core::artdaq-core_Utilities - sbn_BNBSpillInfoRetriever_MWRData - sbnobj::Common_POTAccounting - larcorealg::CoreUtils -) - -install_headers() -install_fhicl() -install_source() - diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.cpp deleted file mode 100644 index 48d0bda3f..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/MWRData.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "MWRData.h" - -using namespace std; - -namespace sbn{ - - std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const -{ - - std::vector > unpacked_data; - unpacked_data.resize(4); - short data[444]; - - std::vector row(0); - boost::split(row, packed_data, boost::is_any_of(",")); - if (row.size()==447) { - for (int i=3;i<447;i++) { - data[i-3]=atoi(row[i].c_str()); - } - string devname=row[1].substr(0,8); - for (int idev=0;idev<4;idev++) { - mwrpulse_t mwr=getMWRdata(data,idev); - time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.hor[ich]); - } - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.ver[ich]); - } - } - } else { - cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< -namespace sbn{ -class MWRData -{ - typedef struct swicheader_t { - long timesec; - long timensec; - long gpstime1; - long gpstime2; - short boosterevent; - short mievent; - short hz15micnt; - long delta1f; - short pulsemi; - short pulsesc; - } swicheader_t; - - typedef struct mwrpulse_t { - short hor[48]; - short ver[48]; - swicheader_t sheader; - } mwrpulse_t; - - static long flipByte(long data) - { - return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); - } - - mwrpulse_t getMWRdata(short* data, int nblock) const; - - public: - std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; -}; -} - -#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc deleted file mode 100644 index bb970c366..000000000 --- a/sbncode/BeamSpillInfoRetriever/BNBRetriever/SBNDBNBRetriever_module.cc +++ /dev/null @@ -1,596 +0,0 @@ -//////////////////////////////////////////////////////////////////////// -// Class: SBNDBNBRetriever -// Plugin Type: producer -// File: SBNDBNBRetriever_module.cc -// -//////////////////////////////////////////////////////////////////////// - -#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/ParameterSet.h" -#include "messagefacility/MessageLogger/MessageLogger.h" - -#include -#include -#include -#include - -#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" -#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" -#include "artdaq-core/Data/ContainerFragment.hh" -//#include "sbndcode/Decoders/PTB/sbndptb.h" -#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" - -#include "ifdh_art/IFBeamService/IFBeam_service.h" -#include "ifbeam_c.h" -#include "MWRData.h" - -#include "larcorealg/CoreUtils/counter.h" - -class SBNDBNBRetriever : public art::EDProducer { -public: - explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); - - // Plugins should not be copied or assigned. - SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; - - // Required functions. - void produce(art::Event & e) override; - void beginSubRun(art::SubRun& sr) override; - void endSubRun(art::SubRun& sr) override; - -private: - // Declare member data here. - std::vector< sbn::BNBSpillInfo > fOutbeamInfos; - double fTimePad; - std::string fInputLabel; - std::string fInputNonContainerInstance; - std::string fDeviceUsedForTiming; - std::string fOutputInstance; - std::string raw_data_label; - int fDebugLevel; - sbn::MWRData mwrdata; - art::ServiceHandle ifbeam_handle; - std::unique_ptr bfp; - std::unique_ptr bfp_mwr; - - struct PTBInfo_t { - double prevPTBTimeStamp = 0; - unsigned int GateCounter = 0; // FIXME needs to be integral type - }; - - struct TriggerInfo_t { - 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 - }; - - struct MWRdata_t { - std::vector< std::vector > MWR_times; - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - }; - - static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] - - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; - double extractTDCTimeStamp(art::Handle > cont_frags) const; - MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; - int matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos - ) const; - unsigned int TotalBeamSpills; - sbn::BNBSpillInfo makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; -}; - -SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) - : EDProducer{params} { - raw_data_label = params.get("raw_data_label", "daq"); - fInputLabel = params.get("InputLabel"); - fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); - fTimePad = params.get("TimePadding"); - fInputNonContainerInstance = params.get("InputNonContainerInstance"); - fOutputInstance = params.get("OutputInstance"); - fDebugLevel = params.get("DebugLevel",0); - bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); - bfp->set_epsilon(0.02); - bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); - bfp_mwr->set_epsilon(0.5); - bfp_mwr->setValidWindow(3605); - TotalBeamSpills = 0; -} - -int eventNum =0; -int _run; -int _subrun; -int _event; - -void SBNDBNBRetriever::produce(art::Event & e) -{ - - // If this is the first event in the run, then ignore it - // We do not currently have the ability to figure out the first - // spill that the DAQ was sensitive to, so don't try to save any - // spill information - - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); - - int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); - - if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) - mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; - else - mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; -} - -SBNDBNBRetriever::PTBInfo_t SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { - int numcont = 0; - PTBInfo_t PTBInfo; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck - for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) - { - if(ctb_frag.Trigger(word_i)){ - uint32_t wt = 0; - uint32_t word_type = ctb_frag.Word(word_i)->word_type; - wt = word_type; - if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) - { - PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; - uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; - PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; - } - } - } //End of loop over the number of trigger words - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - return PTBInfo; -} - -double SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { - int numcont = 0; - uint64_t TDCTimeStamp = 0; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::TDCTimestampFragment tdc_frag(frag); - TDCTimeStamp = tdc_frag.getTDCTimestamp()->timestamp_ns()/1e9; - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - return TDCTimeStamp; -} - -SBNDBNBRetriever::TriggerInfo_t SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { - // Using TDC for current event, but PTB for previous event - art::InputTag PTB_itag("daq", "ContainerPTB"); - auto PTB_cont_frags = e.getHandle(PTB_itag); - - art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); - auto TDC_cont_frags = e.getHandle(TDC_itag); - - PTBInfo_t PTBInfo; - TriggerInfo_t triggerInfo; - PTBInfo = extractPTBInfo(PTB_cont_frags); - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - - triggerInfo.t_current_event = TDCTimeStamp; - triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; - triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; - - return triggerInfo; -} - -SBNDBNBRetriever::MWRdata_t SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { - - // These lines get everything primed within the IFBeamDB. - try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; - try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {}; - try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; - try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 - // to correctly unpack it - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - std::vector< std::vector< double> > MWR_times; - unpacked_MWR.resize(3); - MWR_times.resize(3); - std::string packed_data_str; - - // Create a list of all the MWR devices with their different - // memory buffer increments - // generally in the format: "E:.{Memory Block}" - std::vector vars = bfp_mwr->GetDeviceList(); - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; - // Tracking the time from the IFBeamDB - double time_for_mwr; - - // this is an iterator to track which of the - // three devices we will be working with - int dev = 0; - - // The MWR devices are annoying and have confusing buffer - // what we'll do is sort through all of them first and then - // match them to the closest spills in time - // - - // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; - int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; - mf::LogDebug("SBNDBNBRetriever") << " t_steps " << t_steps << std::endl; - - for(int t = 0; t < t_steps; t++){//Iterate through time increments - for (std::string const& var : vars) {// Iterate through the devices - - //Make sure we have a device - if(var.empty()){ - //mf::LogDebug("SBNDBNBRetriever") << " NO MWR DEVICES?!" << std::endl; - continue; - } - /// Check the device name and interate the double-vector index - if(var.find("M875BB") != std::string::npos ) dev = 0; - else if(var.find("M876BB") != std::string::npos ) dev = 1; - else if(var.find("MMBTBB") != std::string::npos ) dev = 2; - else{ - //mf::LogDebug("SBNDBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; - continue;} - - time_for_mwr = 0; - - try{ - - //Pull the MWR data for the device - // these data are "packed" - std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); - - //We'll convert this into a format - // that we can unpack doubles >> strings - // - packed_data_str.clear(); - packed_data_str += std::to_string(int(time_for_mwr)); - packed_data_str.append(","); - packed_data_str.append(var); - packed_data_str.append(",,"); - - /* for(auto const value: packed_MWR){ - packed_data_str += ','; - packed_data_str += std::to_string(int(value)); - }*/ - for(int j = 0; j < int(packed_MWR.size()); j++){ - packed_data_str += std::to_string(int(packed_MWR[j])); - if(j < int(packed_MWR.size())-1) - packed_data_str.append(","); - } - - // Use Zarko's unpacking function to turn this into consumeable data - std::vector MWR_times_temp; - - // There is a 35 ms offset between the toriod and the MWR times - // we'll just remove that here to match to the spill times - std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); - - //There are four events that are packed into one MWR IFBeam entry - for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ - - // If this entry has a unique time them store it for later - if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ - unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); - MWR_times[dev].push_back(MWR_times_temp[s]); - }//check for unique time - }//Iterate through the unpacked events - }//try - catch (WebAPIException &we) { - //Ignore when we can't find the MWR devices - // they don't always report and the timing of them can be annoying - }//catch - }// Iterate over all the multiwire devices - }// Iterate over all times - - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; - - return { std::move(MWR_times), std::move(unpacked_MWR) }; -} - -int SBNDBNBRetriever::matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos -) const { - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - //Here we will start collecting all the other beamline devices - // First we get the times that the beamline device fired - // we have to pick a specific variable to use - std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); - - mf::LogDebug("SBNDBNBRetriever") << "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); - - // NOTE: for now, this is dead code because we don't - // do anything for the first event in a run. We may want to revisit - // this later to understand if there is a way we can do the POT - // accounting in the first event. - // - // Need to handle the first event in a run differently - if(isFirstEventInRun){ - //We'll remove the spills after our event - int spills_after_our_target = 0; - // iterate through all the spills to find the - // spills that are after our triggered event - for (size_t i = 0; i < times_temps.size(); i++) { - if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ - spills_after_our_target++; - } - }//end loop through spill times - - // Remove the spills after our trigger - times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); - - // 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("SBNDBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; - // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; - // mf::LogDebug("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; - - if(!isFirstEventInRun){//We already addressed the "first event" above - if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ - //mf::LogDebug("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; - continue;} - } - - //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("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << 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++; - - //Loop through the multiwire devices: - - for(int dev = 0; dev < int(MWR_times.size()); dev++){ - - //Loop through the multiwire times: - double Tdiff = 1000000000.; - matched_MWR[dev] = 0; - - for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ - - //found a candidate match! - if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} - - bool best_match = true; - - //Check for a better match... - 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;} - - //is there a better match later in the spill sequence - if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < - fabs((MWR_times[dev][mwrt] - times_temps[i]))){ - //we can have patience... - best_match = false; - break; - } - }//end better match check - - //Verified best match! - if(best_match == true){ - matched_MWR[dev] = mwrt; - Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); - } - - }//end loop over MWR times - - }//end loop over MWR devices - - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); - - beamInfos.push_back(std::move(spillInfo)); - - // 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 iteration over beam device times - - // mf::LogDebug("SBNDBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; - - return spill_count; -} - -sbn::BNBSpillInfo SBNDBNBRetriever::makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const -{ - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - // initializing all of our device carriers - // device definitions can be found in BNBSpillInfo.h - - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps - - double TOR860_time = 0; // units s - - // Here we request all the devices - // since sometimes devices fail to report we'll - // allow each to throw an exception but still move forward - // interpreting these failures will be part of the beam quality analyses - - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - - //crunch the times - unsigned long int time_closest_int = (int) TOR860_time; - double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - - //Store everything in our data-product - sbn::BNBSpillInfo beamInfo; - beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.LM875A = LM875A; - beamInfo.LM875B = LM875B; - beamInfo.LM875C = LM875C; - beamInfo.HP875 = HP875; - beamInfo.VP875 = VP875; - beamInfo.HPTG1 = HPTG1; - beamInfo.VPTG1 = VPTG1; - beamInfo.HPTG2 = HPTG2; - beamInfo.VPTG2 = VPTG2; - beamInfo.BTJT2 = BTJT2; - beamInfo.THCURR = THCURR; - beamInfo.spill_time_s = time_closest_int; - beamInfo.spill_time_ns = time_closest_ns; - - for(auto const& MWRdata: unpacked_MWR){ - std::ignore = MWRdata; - assert(!MWRdata.empty()); - } - - if(unpacked_MWR[0].empty()){ - beamInfo.M875BB.clear(); - beamInfo.M875BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; - beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); - } - - if(unpacked_MWR[1].empty()){ - beamInfo.M876BB.clear(); - beamInfo.M876BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; - beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); - } - - if(unpacked_MWR[2].empty()){ - beamInfo.MMBTBB.clear(); - beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; - beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); - } - // 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 - - beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun - - return beamInfo; -} - -void SBNDBNBRetriever::beginSubRun(art::SubRun& sr) -{ - return; -} - -void SBNDBNBRetriever::endSubRun(art::SubRun& sr) -{ - mf::LogDebug("SBNDBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; - mf::LogDebug("SBNDBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; - - auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); - std::swap(*p, fOutbeamInfos); - - sr.put(std::move(p), art::subRunFragment()); - - return; -} - -DEFINE_ART_MODULE(SBNDBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt index 0caadf2a5..b99d4dcfa 100644 --- a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt @@ -1,4 +1,5 @@ -add_subdirectory(BNBRetriever) +add_subdirectory(ICARUSBNBRetriever) +add_subdirectory(SBNDBNBRetriever) add_subdirectory(NuMIRetriever) add_subdirectory(BNBEXTRetriever) add_subdirectory(NuMIEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl deleted file mode 100644 index 3aece8658..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/bnbspillinfo.fcl +++ /dev/null @@ -1,17 +0,0 @@ - -BEGIN_PROLOG - -bnbspillinfo: { - - module_type: "BNBRetriever" - TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away - URL: "" #keep this blank and we're good - Bundle: "BoosterNeutrinoBeam_read" - MultiWireBundle: "BNBMultiWire" - TimeWindow: "700" #seconds - MWR_TimeWindow: "3601" #seconds - raw_data_label: "daq" - DeviceUsedForTiming: "E:TOR860" - TriggerDatabaseFile: "triggerDatabase/icarus_triggers.db" -} -END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/run_bnbinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_bnbinfo_sbn.fcl deleted file mode 100644 index 34f8f689d..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/run_bnbinfo_sbn.fcl +++ /dev/null @@ -1,46 +0,0 @@ -#include "bnbspillinfo.fcl" - -process_name: BNBInfoGen - -services:{ - - message: { - debugModules: [ "*" ] - destinations: { - LogDebugFile:{ - type: "file" - filename: "debug.log" - append: false - threshold: "DEBUG" - categories: { - default: {} - } - } - } - } - IFBeam:{} -} - - -source: { - -} - -physics: { - producers: { - bnbinfo: @local::bnbspillinfo - } - - simulate: [bnbinfo ] - stream1: [ out1 ] -} - -outputs: { - out1: { - module_type: RootOutput - fileName: "%ifb_%tc_bnbinfo.root" - dataTier: "raw" - compressionLevel: 1 - } -} - From 3742907492ba3f2219e45f7682e866a8405afa10 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Tue, 12 Nov 2024 14:10:13 -0600 Subject: [PATCH 14/26] Build issues returned to match branch --- .../ICARUSBNBRetriever/CMakeLists.txt | 34 + .../ICARUSBNBRetriever_module.cc | 780 ++++++++++++++++++ .../ICARUSBNBRetriever/MWRData.cpp | 71 ++ .../ICARUSBNBRetriever/MWRData.h | 39 + .../SBNDBNBRetriever/CMakeLists.txt | 34 + .../SBNDBNBRetriever/MWRData.cpp | 71 ++ .../SBNDBNBRetriever/MWRData.h | 39 + .../SBNDBNBRetriever_module.cc | 600 ++++++++++++++ .../job/icarusspillinfo.fcl | 17 + .../job/run_icarusbnbinfo_sbn.fcl | 46 ++ sbncode/CAFMaker/FillReco.cxx | 8 +- 11 files changed, 1735 insertions(+), 4 deletions(-) create mode 100644 sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt create mode 100644 sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc create mode 100644 sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp create mode 100644 sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.h create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.h create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc create mode 100644 sbncode/BeamSpillInfoRetriever/job/icarusspillinfo.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/run_icarusbnbinfo_sbn.fcl diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt new file mode 100644 index 000000000..2668311a3 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt @@ -0,0 +1,34 @@ +find_package(ifbeam) +find_package(ifdh_art) + + +art_make_library(LIBRARIES Boost::system + LIBRARY_NAME sbn_ICARUSBNBSpillInfoRetriever_MWRData + SOURCE MWRData.cpp +) + + +cet_build_plugin(ICARUSBNBRetriever art::module + LIBRARIES + art::Persistency_Common + art::Utilities canvas::canvas + cetlib::cetlib cetlib_except::cetlib_except + ROOT::X3d + Boost::system + 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 + artdaq_core::artdaq-core_Utilities + sbn_ICARUSBNBSpillInfoRetriever_MWRData + sbnobj::Common_POTAccounting + larcorealg::CoreUtils +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc new file mode 100644 index 000000000..f559c1df8 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc @@ -0,0 +1,780 @@ +/** ******************************************************************** + * @file ICARUSBNBRetriever_module.cc + * @date Wed April 9 2021 + * @author J. Zennamo (FNAL) + * + * 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/SubRun.h" +#include "canvas/Utilities/InputTag.h" +#include "fhiclcpp/types/Atom.h" +#include "messagefacility/MessageLogger/MessageLogger.h" +#include "larcorealg/CoreUtils/counter.h" + +#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" + +#include "ifdh_art/IFBeamService/IFBeam_service.h" +#include "ifbeam_c.h" +#include "MWRData.h" + +#include +#include +#include + +#include +#include +#include + +namespace sbn { + class ICARUSBNBRetriever; +} + +class sbn::ICARUSBNBRetriever : public art::EDProducer { +public: + + struct Config { + + using Name = fhicl::Name; + using Comment = fhicl::Comment; + + fhicl::Atom TimePadding { + Name{ "TimePadding" }, + Comment{ "extension to the time window considered when collecting spills [seconds]" }, + 0.0333 // default + }; + + fhicl::Atom RawDataLabel { + Name{ "raw_data_label" }, + Comment{ "art data product instance name for trigger information (product label is 'daq')" } + }; + + fhicl::Atom DeviceUsedForTiming { + Name{ "DeviceUsedForTiming" }, + Comment{ "name in the IFBeam database of the device used to extract spill times" } + }; + + fhicl::Atom URL { + Name{ "URL" }, + Comment{ "IFBeam database access URL" } + }; + + fhicl::Atom Bundle { + Name{ "Bundle" }, + Comment{ "" } // explain what this is and which database/table it's looking for + }; + + fhicl::Atom TimeWindow { + Name{ "TimeWindow" }, + Comment{ "" } // explain what this is, what's for and its unit + }; + + fhicl::Atom MultiWireBundle { + Name{ "MultiWireBundle" }, + Comment{ "" } // explain what this is and which database/table it's looking for + }; + + fhicl::Atom MWR_TimeWindow { + 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; + + + explicit ICARUSBNBRetriever(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. + ICARUSBNBRetriever(ICARUSBNBRetriever const&) = delete; + ICARUSBNBRetriever(ICARUSBNBRetriever&&) = delete; + ICARUSBNBRetriever& operator=(ICARUSBNBRetriever const&) = delete; + ICARUSBNBRetriever& operator=(ICARUSBNBRetriever&&) = delete; + + // Required functions. + void produce(art::Event& e) override; + void beginSubRun(art::SubRun& sr) override; + void endSubRun(art::SubRun& sr) override; + +private: + // input labels + std::vector< sbn::BNBSpillInfo > fOutbeamInfos; + double fTimePad; + std::string fURL; + MWRData mwrdata; + int run_number; + std::string raw_data_label; + std::string fDeviceUsedForTiming; + unsigned int TotalBeamSpills; + // + art::ServiceHandle ifbeam_handle; + std::unique_ptr bfp; + std::unique_ptr bfp_mwr; + + // + std::string fTriggerDatabaseFile; + 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 { + std::vector< std::vector > MWR_times; + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + }; + + + static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] + + /// Returns the information of the trigger in the current event. + TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + + /** + * @brief Determines spill times and extracts data based on multiwire devices. + * @param triggerInfo information from the trigger of this event + * @return times and unpacked data, per device (`"M875BB"`, `"M876BB"`, `"MMBTBB"`) + */ + MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; + + /** + * @brief Matches spill times with multiwire chamber data from the database. + * @param eventID ID of the event the information is associated to + * @param triggerInfo information from the trigger of this event + * @param MWRdata data from multiwire chambers + * @param isFirstEventInRun whether we are processing the first event of the run + * @param[out] beamInfos container to _add_ spill information records to + * @return count of matched spills + */ + int matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos + ) const; + + /** + * @brief Assembles and returns a spill information record. + * @param eventID ID of the event the information is associated to + * @param time time of the spill + * @param MWRdata all extracted data from multiwire chambers + * @param matched_MWR data from multiwire chambers matched with the time + * @return a `sbn::BNBSpillInfo` object with information on the spill at `time` + */ + 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, + float 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::ICARUSBNBRetriever::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; + 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) + { + mf::LogError("BNBEXTRetriever") << "SQL error: " << sqlite3_errmsg(db); + trigger_type = -1; + } + return trigger_type; +} + +sbn::ICARUSBNBRetriever::ICARUSBNBRetriever(Parameters const& params) + : EDProducer{params}, + fTimePad(params().TimePadding()), + 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())), + fTriggerDatabaseFile(params().TriggerDatabaseFile()) +{ + + // Check fTimePad is positive + if (fTimePad < 0) { + throw art::Exception(art::errors::Configuration) + << "Parameter `TimePadding` must be non-negative (" << fTimePad << " was specified).\n"; + }//End Time check + + // how close in time does the spill time have to be from the DAQ time (in seconds). + // If these are too large then it fails to capture the device + // If these are too small then the time jitter in devices means we miss good data + // + // These values should likely not be changed unless authors of the IFBeam API are consulted + // + bfp->set_epsilon(0.02); //20 ms, this was tuned by hand and compared to IFBeamDB times + bfp_mwr->set_epsilon(0.5); + + //bfp_mwr->setValidWindow(86400); + bfp_mwr->setValidWindow(3605); + produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); + TotalBeamSpills = 0; + + 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); + } + +} + + +void sbn::ICARUSBNBRetriever::produce(art::Event& e) +{ + + // If this is the first event in the run, then ignore it + // We do not currently have the ability to figure out the first + // spill that the DAQ was sensitive to, so don't try to save any + // 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 + if(triggerInfo.gate_type != 1) return; + // Keep track of the number of beam gates the DAQ thinks + // are in this job + TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; + + + MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); + + + int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); + + + if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) + mf::LogDebug("ICARUSBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; + else + mf::LogDebug("ICARUSBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; + +}//end iteration over art::Events + + +sbn::ICARUSBNBRetriever::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& e) const { + + //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 + + auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV3" }); + 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(); + 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); + 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); + else + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()-3.6e7))/(1000000000.0); + + } + + mf::LogDebug("ICARUSBNBRetriever") << 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; +} + + +sbn::ICARUSBNBRetriever::MWRdata_t sbn::ICARUSBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { + + // These lines get everything primed within the IFBeamDB + // They seem redundant but they are needed + try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} + try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 + // to correctly unpack it + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + std::vector< std::vector< double> > MWR_times; + unpacked_MWR.resize(3); + MWR_times.resize(3); + std::string packed_data_str; + + //Create a list of all the MWR devices with their different + // memory buffer increments + // generally in the format: "E:.{Memory Block}" + std::vector vars = bfp_mwr->GetDeviceList(); + mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; + // Tracking the time from the IFBeamDB + double time_for_mwr; + + // this is an iterator to track which of the + // three devices we will be working with + int dev = 0; + + // The MWR devices are annoying and have confusing buffer + // what we'll do is sort through all of them first and then + // match them to the closest spills in time + // + + // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; + int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; + mf::LogDebug("ICARUSBNBRetriever") << " t_steps " << t_steps << std::endl; + + for(int t = 0; t < t_steps; t++){//Iterate through time increments + for (std::string const& var : vars) {// Iterate through the devices + + //Make sure we have a device + if(var.empty()){ + //mf::LogDebug("ICARUSBNBRetriever") << " NO MWR DEVICES?!" << std::endl; + continue; + } + /// Check the device name and interate the double-vector index + if(var.find("M875BB") != std::string::npos ) dev = 0; + else if(var.find("M876BB") != std::string::npos ) dev = 1; + else if(var.find("MMBTBB") != std::string::npos ) dev = 2; + else{ + //mf::LogDebug("ICARUSBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; + continue;} + + time_for_mwr = 0; + + try{ + //Pull the MWR data for the device + // these data are "packed" + std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); + + //We'll convert this into a format + // that we can unpack doubles >> strings + // + packed_data_str.clear(); + packed_data_str += std::to_string(int(time_for_mwr)); + packed_data_str.append(","); + packed_data_str.append(var); + packed_data_str.append(",,"); + + /* for(auto const value: packed_MWR){ + packed_data_str += ','; + packed_data_str += std::to_string(int(value)); + }*/ + for(int j = 0; j < int(packed_MWR.size()); j++){ + packed_data_str += std::to_string(int(packed_MWR[j])); + if(j < int(packed_MWR.size())-1) + packed_data_str.append(","); + } + + // Use Zarko's unpacking function to turn this into consumeable data + std::vector MWR_times_temp; + + // There is a 35 ms offset between the toriod and the MWR times + // we'll just remove that here to match to the spill times + std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); + + //There are four events that are packed into one MWR IFBeam entry + for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ + + // If this entry has a unique time them store it for later + if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ + unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); + MWR_times[dev].push_back(MWR_times_temp[s]); + }//check for unique time + }//Iterate through the unpacked events + }//try + catch (WebAPIException &we) { + //Ignore when we can't find the MWR devices + // they don't always report and the timing of them can be annoying + + }//catch + }// Iterate over all the multiwire devices + }// Iterate over all times + + mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; + mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; + mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; + mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; + mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; + mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; + + return { std::move(MWR_times), std::move(unpacked_MWR) }; +} + + +int sbn::ICARUSBNBRetriever::matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos +) const { + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + //Here we will start collecting all the other beamline devices + // First we get the times that the beamline device fired + // we have to pick a specific variable to use + std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); + + mf::LogDebug("ICARUSBNBRetriever") << "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); + + + // NOTE: for now, this is dead code because we don't + // do anything for the first event in a run. We may want to revisit + // this later to understand if there is a way we can do the POT + // accounting in the first event. + // + // Need to handle the first event in a run differently + if(isFirstEventInRun){ + + //We'll remove the spills after our event + int spills_after_our_target = 0; + // iterate through all the spills to find the + // spills that are after our triggered event + for (size_t i = 0; i < times_temps.size(); i++) { + if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ + spills_after_our_target++; + } + }//end loop through spill times + + // Remove the spills after our trigger + times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); + + // 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("ICARUSBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; + // mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; + // mf::LogDebug("ICARUSBNBRetriever") << 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("ICARUSBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; + + if(!isFirstEventInRun){//We already addressed the "first event" above + if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ + //mf::LogDebug("ICARUSBNBRetriever") << 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("ICARUSBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + continue;} + } + + //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("ICARUSBNBRetriever") << 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("ICARUSBNBRetriever") << 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++; + + //Loop through the multiwire devices: + + for(int dev = 0; dev < int(MWR_times.size()); dev++){ + + //Loop through the multiwire times: + double Tdiff = 1000000000.; + matched_MWR[dev] = 0; + + for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ + + //found a candidate match! + if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} + + bool best_match = true; + + //Check for a better match... + 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;} + + //is there a better match later in the spill sequence + if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < + fabs((MWR_times[dev][mwrt] - times_temps[i]))){ + //we can have patience... + best_match = false; + break; + } + }//end better match check + + //Verified best match! + if(best_match == true){ + matched_MWR[dev] = mwrt; + Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); + } + + }//end loop over MWR times + + }//end loop over MWR devices + + sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); + + beamInfos.push_back(std::move(spillInfo)); + + // 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 iteration over beam device times + + // mf::LogDebug("ICARUSBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; + + return spill_count; +} + + +sbn::BNBSpillInfo sbn::ICARUSBNBRetriever::makeBNBSpillInfo + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const +{ + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + // initializing all of our device carriers + // device definitions can be found in BNBSpillInfo.h + + double TOR860 = 0; // units e12 protons + double TOR875 = 0; // units e12 protons + double LM875A = 0; // units R/s + double LM875B = 0; // units R/s + double LM875C = 0; // units R/s + double HP875 = 0; // units mm + double VP875 = 0; // units mm + double HPTG1 = 0; // units mm + double VPTG1 = 0; // units mm + double HPTG2 = 0; // units mm + double VPTG2 = 0; // units mm + double BTJT2 = 0; // units Deg C + double THCURR = 0; // units kiloAmps + + double TOR860_time = 0; // units s + + // Here we request all the devices + // since sometimes devices fail to report we'll + // allow each to throw an exception but still move forward + // interpreting these failures will be part of the beam quality analyses + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + + //crunch the times + unsigned long int time_closest_int = (int) TOR860_time; + double time_closest_ns = (TOR860_time - time_closest_int)*1e9; + + //Store everything in our data-product + sbn::BNBSpillInfo beamInfo; + beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.LM875A = LM875A; + beamInfo.LM875B = LM875B; + beamInfo.LM875C = LM875C; + beamInfo.HP875 = HP875; + beamInfo.VP875 = VP875; + beamInfo.HPTG1 = HPTG1; + beamInfo.VPTG1 = VPTG1; + beamInfo.HPTG2 = HPTG2; + beamInfo.VPTG2 = VPTG2; + beamInfo.BTJT2 = BTJT2; + beamInfo.THCURR = THCURR; + beamInfo.spill_time_s = time_closest_int; + beamInfo.spill_time_ns = time_closest_ns; + + for(auto const& MWRdata: unpacked_MWR){ + std::ignore = MWRdata; + assert(!MWRdata.empty()); + } + + if(unpacked_MWR[0].empty()){ + beamInfo.M875BB.clear(); + beamInfo.M875BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; + beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); + } + + if(unpacked_MWR[1].empty()){ + beamInfo.M876BB.clear(); + beamInfo.M876BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; + beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); + } + + if(unpacked_MWR[2].empty()){ + beamInfo.MMBTBB.clear(); + beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; + beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); + } + // 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 + + beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun + + return beamInfo; +} + + +void sbn::ICARUSBNBRetriever::beginSubRun(art::SubRun& sr) +{ + return; +} + +//____________________________________________________________________________ +void sbn::ICARUSBNBRetriever::endSubRun(art::SubRun& sr) +{ + // We will add all of the BNBSpillInfo data-products to the + // art::SubRun so it persists + // currently this is ~2.7 kB/event or ~0.07 kB/spill + +mf::LogDebug("ICARUSBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; +mf::LogDebug("ICARUSBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; + + auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); + std::swap(*p, fOutbeamInfos); + + sr.put(std::move(p), art::subRunFragment()); + + return; +} + +DEFINE_ART_MODULE(sbn::ICARUSBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp new file mode 100644 index 000000000..48d0bda3f --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include "MWRData.h" + +using namespace std; + +namespace sbn{ + + std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const +{ + + std::vector > unpacked_data; + unpacked_data.resize(4); + short data[444]; + + std::vector row(0); + boost::split(row, packed_data, boost::is_any_of(",")); + if (row.size()==447) { + for (int i=3;i<447;i++) { + data[i-3]=atoi(row[i].c_str()); + } + string devname=row[1].substr(0,8); + for (int idev=0;idev<4;idev++) { + mwrpulse_t mwr=getMWRdata(data,idev); + time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); + for (int ich=0;ich<48;ich++) { + unpacked_data[idev].push_back(mwr.hor[ich]); + } + for (int ich=0;ich<48;ich++) { + unpacked_data[idev].push_back(mwr.ver[ich]); + } + } + } else { + cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< +namespace sbn{ +class MWRData +{ + typedef struct swicheader_t { + long timesec; + long timensec; + long gpstime1; + long gpstime2; + short boosterevent; + short mievent; + short hz15micnt; + long delta1f; + short pulsemi; + short pulsesc; + } swicheader_t; + + typedef struct mwrpulse_t { + short hor[48]; + short ver[48]; + swicheader_t sheader; + } mwrpulse_t; + + static long flipByte(long data) + { + return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); + } + + mwrpulse_t getMWRdata(short* data, int nblock) const; + + public: + std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; +}; +} + +#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt new file mode 100644 index 000000000..d04512753 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt @@ -0,0 +1,34 @@ +find_package(ifbeam) +find_package(ifdh_art) + + +art_make_library(LIBRARIES Boost::system + LIBRARY_NAME sbn_SBNDBNBSpillInfoRetriever_MWRData + SOURCE MWRData.cpp +) + +cet_build_plugin(SBNDBNBRetriever art::module + LIBRARIES + art::Persistency_Common + art::Utilities canvas::canvas + cetlib::cetlib cetlib_except::cetlib_except + ROOT::X3d + Boost::system + 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 + sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND + artdaq_core::artdaq-core_Utilities + sbn_SBNDBNBSpillInfoRetriever_MWRData + sbnobj::Common_POTAccounting + larcorealg::CoreUtils +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp new file mode 100644 index 000000000..48d0bda3f --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include "MWRData.h" + +using namespace std; + +namespace sbn{ + + std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const +{ + + std::vector > unpacked_data; + unpacked_data.resize(4); + short data[444]; + + std::vector row(0); + boost::split(row, packed_data, boost::is_any_of(",")); + if (row.size()==447) { + for (int i=3;i<447;i++) { + data[i-3]=atoi(row[i].c_str()); + } + string devname=row[1].substr(0,8); + for (int idev=0;idev<4;idev++) { + mwrpulse_t mwr=getMWRdata(data,idev); + time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); + for (int ich=0;ich<48;ich++) { + unpacked_data[idev].push_back(mwr.hor[ich]); + } + for (int ich=0;ich<48;ich++) { + unpacked_data[idev].push_back(mwr.ver[ich]); + } + } + } else { + cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< +namespace sbn{ +class MWRData +{ + typedef struct swicheader_t { + long timesec; + long timensec; + long gpstime1; + long gpstime2; + short boosterevent; + short mievent; + short hz15micnt; + long delta1f; + short pulsemi; + short pulsesc; + } swicheader_t; + + typedef struct mwrpulse_t { + short hor[48]; + short ver[48]; + swicheader_t sheader; + } mwrpulse_t; + + static long flipByte(long data) + { + return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); + } + + mwrpulse_t getMWRdata(short* data, int nblock) const; + + public: + std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; +}; +} + +#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc new file mode 100644 index 000000000..a0a75b2ad --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -0,0 +1,600 @@ +//////////////////////////////////////////////////////////////////////// +// Class: SBNDBNBRetriever +// Plugin Type: producer +// File: SBNDBNBRetriever_module.cc +// +//////////////////////////////////////////////////////////////////////// + +#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/ParameterSet.h" +#include "messagefacility/MessageLogger/MessageLogger.h" + +#include +#include +#include +#include + +#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" +#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" +#include "artdaq-core/Data/ContainerFragment.hh" +//#include "sbndcode/Decoders/PTB/sbndptb.h" +#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" + +#include "ifdh_art/IFBeamService/IFBeam_service.h" +#include "ifbeam_c.h" +#include "MWRData.h" + +#include "larcorealg/CoreUtils/counter.h" + +namespace sbn { + class SBNDBNBRetriever; +} + +class sbn::SBNDBNBRetriever : public art::EDProducer { +public: + explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); + // Required functions. + void produce(art::Event & e) override; + void beginSubRun(art::SubRun& sr) override; + void endSubRun(art::SubRun& sr) override; + + // Plugins should not be copied or assigned. + SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; + + +private: + // Declare member data here. + std::vector< sbn::BNBSpillInfo > fOutbeamInfos; + double fTimePad; + std::string fInputLabel; + std::string fInputNonContainerInstance; + std::string fDeviceUsedForTiming; + std::string fOutputInstance; + std::string raw_data_label; + int fDebugLevel; + sbn::MWRData mwrdata; + art::ServiceHandle ifbeam_handle; + std::unique_ptr bfp; + std::unique_ptr bfp_mwr; + + struct PTBInfo_t { + double prevPTBTimeStamp = 0; + unsigned int GateCounter = 0; // FIXME needs to be integral type + }; + + struct TriggerInfo_t { + 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 + }; + + struct MWRdata_t { + std::vector< std::vector > MWR_times; + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + }; + + static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] + + TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; + double extractTDCTimeStamp(art::Handle > cont_frags) const; + MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; + int matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos + ) const; + unsigned int TotalBeamSpills; + sbn::BNBSpillInfo makeBNBSpillInfo + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; +}; + +sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) + : EDProducer{params} { + raw_data_label = params.get("raw_data_label", "daq"); + fInputLabel = params.get("InputLabel"); + fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); + fTimePad = params.get("TimePadding"); + fInputNonContainerInstance = params.get("InputNonContainerInstance"); + fOutputInstance = params.get("OutputInstance"); + fDebugLevel = params.get("DebugLevel",0); + bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); + bfp->set_epsilon(0.02); + bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); + bfp_mwr->set_epsilon(0.5); + bfp_mwr->setValidWindow(3605); + TotalBeamSpills = 0; +} + +int eventNum =0; +int _run; +int _subrun; +int _event; + +void sbn::SBNDBNBRetriever::produce(art::Event & e) +{ + + // If this is the first event in the run, then ignore it + // We do not currently have the ability to figure out the first + // spill that the DAQ was sensitive to, so don't try to save any + // spill information + + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; + MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); + + int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); + + if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) + mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; + else + mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; +} + +sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { + int numcont = 0; + PTBInfo_t PTBInfo; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag = 0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck + for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) + { + if(ctb_frag.Trigger(word_i)){ + uint32_t wt = 0; + uint32_t word_type = ctb_frag.Word(word_i)->word_type; + wt = word_type; + if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) + { + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + } + } + } //End of loop over the number of trigger words + } //End of loop over the number of fragments per container + } //End of loop over the number of containers + return PTBInfo; +} + +double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { + int numcont = 0; + uint64_t TDCTimeStamp = 0; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag = 0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::TDCTimestampFragment tdc_frag(frag); + TDCTimeStamp = tdc_frag.getTDCTimestamp()->timestamp_ns()/1e9; + } //End of loop over the number of fragments per container + } //End of loop over the number of containers + return TDCTimeStamp; +} + +sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { + // Using TDC for current event, but PTB for previous event + art::InputTag PTB_itag("daq", "ContainerPTB"); + auto PTB_cont_frags = e.getHandle(PTB_itag); + + art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); + auto TDC_cont_frags = e.getHandle(TDC_itag); + + PTBInfo_t PTBInfo; + TriggerInfo_t triggerInfo; + PTBInfo = extractPTBInfo(PTB_cont_frags); + double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + + triggerInfo.t_current_event = TDCTimeStamp; + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; + triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; + + return triggerInfo; +} + +sbn::SBNDBNBRetriever::MWRdata_t sbn::SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { + + // These lines get everything primed within the IFBeamDB. + try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; + try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {}; + try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; + try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 + // to correctly unpack it + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + std::vector< std::vector< double> > MWR_times; + unpacked_MWR.resize(3); + MWR_times.resize(3); + std::string packed_data_str; + + // Create a list of all the MWR devices with their different + // memory buffer increments + // generally in the format: "E:.{Memory Block}" + std::vector vars = bfp_mwr->GetDeviceList(); + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; + // Tracking the time from the IFBeamDB + double time_for_mwr; + + // this is an iterator to track which of the + // three devices we will be working with + int dev = 0; + + // The MWR devices are annoying and have confusing buffer + // what we'll do is sort through all of them first and then + // match them to the closest spills in time + // + + // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; + int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; + mf::LogDebug("SBNDBNBRetriever") << " t_steps " << t_steps << std::endl; + + for(int t = 0; t < t_steps; t++){//Iterate through time increments + for (std::string const& var : vars) {// Iterate through the devices + + //Make sure we have a device + if(var.empty()){ + //mf::LogDebug("SBNDBNBRetriever") << " NO MWR DEVICES?!" << std::endl; + continue; + } + /// Check the device name and interate the double-vector index + if(var.find("M875BB") != std::string::npos ) dev = 0; + else if(var.find("M876BB") != std::string::npos ) dev = 1; + else if(var.find("MMBTBB") != std::string::npos ) dev = 2; + else{ + //mf::LogDebug("SBNDBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; + continue;} + + time_for_mwr = 0; + + try{ + + //Pull the MWR data for the device + // these data are "packed" + std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); + + //We'll convert this into a format + // that we can unpack doubles >> strings + // + packed_data_str.clear(); + packed_data_str += std::to_string(int(time_for_mwr)); + packed_data_str.append(","); + packed_data_str.append(var); + packed_data_str.append(",,"); + + /* for(auto const value: packed_MWR){ + packed_data_str += ','; + packed_data_str += std::to_string(int(value)); + }*/ + for(int j = 0; j < int(packed_MWR.size()); j++){ + packed_data_str += std::to_string(int(packed_MWR[j])); + if(j < int(packed_MWR.size())-1) + packed_data_str.append(","); + } + + // Use Zarko's unpacking function to turn this into consumeable data + std::vector MWR_times_temp; + + // There is a 35 ms offset between the toriod and the MWR times + // we'll just remove that here to match to the spill times + std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); + + //There are four events that are packed into one MWR IFBeam entry + for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ + + // If this entry has a unique time them store it for later + if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ + unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); + MWR_times[dev].push_back(MWR_times_temp[s]); + }//check for unique time + }//Iterate through the unpacked events + }//try + catch (WebAPIException &we) { + //Ignore when we can't find the MWR devices + // they don't always report and the timing of them can be annoying + }//catch + }// Iterate over all the multiwire devices + }// Iterate over all times + + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; + + return { std::move(MWR_times), std::move(unpacked_MWR) }; +} + +int sbn::SBNDBNBRetriever::matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos +) const { + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + //Here we will start collecting all the other beamline devices + // First we get the times that the beamline device fired + // we have to pick a specific variable to use + std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); + + mf::LogDebug("SBNDBNBRetriever") << "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); + + // NOTE: for now, this is dead code because we don't + // do anything for the first event in a run. We may want to revisit + // this later to understand if there is a way we can do the POT + // accounting in the first event. + // + // Need to handle the first event in a run differently + if(isFirstEventInRun){ + //We'll remove the spills after our event + int spills_after_our_target = 0; + // iterate through all the spills to find the + // spills that are after our triggered event + for (size_t i = 0; i < times_temps.size(); i++) { + if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ + spills_after_our_target++; + } + }//end loop through spill times + + // Remove the spills after our trigger + times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); + + // 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("SBNDBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; + + if(!isFirstEventInRun){//We already addressed the "first event" above + if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ + //mf::LogDebug("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + continue;} + } + + //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("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << 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++; + + //Loop through the multiwire devices: + + for(int dev = 0; dev < int(MWR_times.size()); dev++){ + + //Loop through the multiwire times: + double Tdiff = 1000000000.; + matched_MWR[dev] = 0; + + for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ + + //found a candidate match! + if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} + + bool best_match = true; + + //Check for a better match... + 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;} + + //is there a better match later in the spill sequence + if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < + fabs((MWR_times[dev][mwrt] - times_temps[i]))){ + //we can have patience... + best_match = false; + break; + } + }//end better match check + + //Verified best match! + if(best_match == true){ + matched_MWR[dev] = mwrt; + Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); + } + + }//end loop over MWR times + + }//end loop over MWR devices + + sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); + + beamInfos.push_back(std::move(spillInfo)); + + // 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 iteration over beam device times + + // mf::LogDebug("SBNDBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; + + return spill_count; +} + +sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const +{ + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + // initializing all of our device carriers + // device definitions can be found in BNBSpillInfo.h + + double TOR860 = 0; // units e12 protons + double TOR875 = 0; // units e12 protons + double LM875A = 0; // units R/s + double LM875B = 0; // units R/s + double LM875C = 0; // units R/s + double HP875 = 0; // units mm + double VP875 = 0; // units mm + double HPTG1 = 0; // units mm + double VPTG1 = 0; // units mm + double HPTG2 = 0; // units mm + double VPTG2 = 0; // units mm + double BTJT2 = 0; // units Deg C + double THCURR = 0; // units kiloAmps + + double TOR860_time = 0; // units s + + // Here we request all the devices + // since sometimes devices fail to report we'll + // allow each to throw an exception but still move forward + // interpreting these failures will be part of the beam quality analyses + + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + + //crunch the times + unsigned long int time_closest_int = (int) TOR860_time; + double time_closest_ns = (TOR860_time - time_closest_int)*1e9; + + //Store everything in our data-product + sbn::BNBSpillInfo beamInfo; + beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.LM875A = LM875A; + beamInfo.LM875B = LM875B; + beamInfo.LM875C = LM875C; + beamInfo.HP875 = HP875; + beamInfo.VP875 = VP875; + beamInfo.HPTG1 = HPTG1; + beamInfo.VPTG1 = VPTG1; + beamInfo.HPTG2 = HPTG2; + beamInfo.VPTG2 = VPTG2; + beamInfo.BTJT2 = BTJT2; + beamInfo.THCURR = THCURR; + beamInfo.spill_time_s = time_closest_int; + beamInfo.spill_time_ns = time_closest_ns; + + for(auto const& MWRdata: unpacked_MWR){ + std::ignore = MWRdata; + assert(!MWRdata.empty()); + } + + if(unpacked_MWR[0].empty()){ + beamInfo.M875BB.clear(); + beamInfo.M875BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; + beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); + } + + if(unpacked_MWR[1].empty()){ + beamInfo.M876BB.clear(); + beamInfo.M876BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; + beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); + } + + if(unpacked_MWR[2].empty()){ + beamInfo.MMBTBB.clear(); + beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; + beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); + } + // 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 + + beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun + + return beamInfo; +} + +void sbn::SBNDBNBRetriever::beginSubRun(art::SubRun& sr) +{ + return; +} + +void sbn::SBNDBNBRetriever::endSubRun(art::SubRun& sr) +{ + mf::LogDebug("SBNDBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; + mf::LogDebug("SBNDBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; + + auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); + std::swap(*p, fOutbeamInfos); + + sr.put(std::move(p), art::subRunFragment()); + + return; +} + +DEFINE_ART_MODULE(sbn::SBNDBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/job/icarusspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/icarusspillinfo.fcl new file mode 100644 index 000000000..9bc7e15d2 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/icarusspillinfo.fcl @@ -0,0 +1,17 @@ + +BEGIN_PROLOG + +icarusbnbspillinfo: { + + module_type: "ICARUSBNBRetriever" + TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away + URL: "" #keep this blank and we're good + Bundle: "BoosterNeutrinoBeam_read" + MultiWireBundle: "BNBMultiWire" + TimeWindow: "700" #seconds + MWR_TimeWindow: "3601" #seconds + raw_data_label: "daq" + DeviceUsedForTiming: "E:TOR860" + TriggerDatabaseFile: "triggerDatabase/icarus_triggers.db" +} +END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbinfo_sbn.fcl new file mode 100644 index 000000000..68984021a --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbinfo_sbn.fcl @@ -0,0 +1,46 @@ +#include "icarusbnbspillinfo.fcl" + +process_name: ICARUSBNBInfoGen + +services:{ + + message: { + debugModules: [ "*" ] + destinations: { + LogDebugFile:{ + type: "file" + filename: "debug.log" + append: false + threshold: "DEBUG" + categories: { + default: {} + } + } + } + } + IFBeam:{} +} + + +source: { + +} + +physics: { + producers: { + icarusbnbinfo: @local::icarusbnbspillinfo + } + + simulate: [icarusbnbinfo ] + stream1: [ out1 ] +} + +outputs: { + out1: { + module_type: RootOutput + fileName: "%ifb_%tc_icarusbnbinfo.root" + dataTier: "raw" + compressionLevel: 1 + } +} + diff --git a/sbncode/CAFMaker/FillReco.cxx b/sbncode/CAFMaker/FillReco.cxx index 0c35b8a59..a478bc840 100644 --- a/sbncode/CAFMaker/FillReco.cxx +++ b/sbncode/CAFMaker/FillReco.cxx @@ -119,8 +119,8 @@ namespace caf srspacepoint.position = SRVector3D(spacepoint.X(), spacepoint.Y(), spacepoint.Z()); srspacepoint.position_err = SRVector3D(spacepoint.XErr(), spacepoint.YErr(), spacepoint.ZErr()); srspacepoint.pe = spacepoint.PE(); - srspacepoint.time = spacepoint.Time(); - srspacepoint.time_err = spacepoint.TimeErr(); + srspacepoint.time = spacepoint.Ts1(); + srspacepoint.time_err = spacepoint.Ts1Err(); srspacepoint.complete = spacepoint.Complete(); } @@ -131,8 +131,8 @@ namespace caf for(auto const& point : track.Points()) srsbndcrttrack.points.emplace_back(point.X(), point.Y(), point.Z()); - srsbndcrttrack.time = track.Time(); - srsbndcrttrack.time_err = track.TimeErr(); + srsbndcrttrack.time = track.Ts1(); + srsbndcrttrack.time_err = track.Ts1Err(); srsbndcrttrack.pe = track.PE(); srsbndcrttrack.tof = track.ToF(); } From 1656232ea808aaf25bdbccf222aa7fbd7007bc99 Mon Sep 17 00:00:00 2001 From: nathanielerowe <70993723+nathanielerowe@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:46:04 -0600 Subject: [PATCH 15/26] Rename icarusspillinfo.fcl to icarusbnbspillinfo.fcl --- .../job/{icarusspillinfo.fcl => icarusbnbspillinfo.fcl} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sbncode/BeamSpillInfoRetriever/job/{icarusspillinfo.fcl => icarusbnbspillinfo.fcl} (100%) diff --git a/sbncode/BeamSpillInfoRetriever/job/icarusspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl similarity index 100% rename from sbncode/BeamSpillInfoRetriever/job/icarusspillinfo.fcl rename to sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl From 6e3023f8236751c5a7394bfb69d8c0a3fb3beb6a Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Fri, 22 Nov 2024 13:07:25 -0600 Subject: [PATCH 16/26] New producer modules --- sbncode/BeamSpillInfoRetriever/CMakeLists.txt | 2 + .../SBNDBNBEXTRetriever/CMakeLists.txt | 27 + .../SBNDBNBEXTRetriever_module.cc | 214 +++++++ .../SBNDBNBRetriever_module.cc | 2 +- .../SBNDBNBZEROBIASRetriever/CMakeLists.txt | 34 ++ .../SBNDBNBZEROBIASRetriever/MWRData.cpp | 71 +++ .../SBNDBNBZEROBIASRetriever/MWRData.h | 39 ++ .../SBNDBNBZEROBIASRetriever_module.cc | 545 ++++++++++++++++++ .../job/run_sbndbnbextinfo_sbn.fcl | 46 ++ .../job/run_sbndbnbinfo_sbn.fcl | 2 + .../job/run_sbndbnbzerobiasinfo_sbn.fcl | 46 ++ .../job/sbndbnbextspillinfo.fcl | 21 + .../job/sbndbnbspillinfo.fcl | 2 +- .../job/sbndbnbzerobiasspillinfo.fcl | 21 + sbncode/CAFMaker/FillReco.cxx | 8 +- 15 files changed, 1074 insertions(+), 6 deletions(-) create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.h create mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc create mode 100644 sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl diff --git a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt index b99d4dcfa..2e5998b50 100644 --- a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt @@ -1,5 +1,7 @@ add_subdirectory(ICARUSBNBRetriever) add_subdirectory(SBNDBNBRetriever) +add_subdirectory(SBNDBNBEXTRetriever) +add_subdirectory(SBNDBNBZEROBIASRetriever) add_subdirectory(NuMIRetriever) add_subdirectory(BNBEXTRetriever) add_subdirectory(NuMIEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt new file mode 100644 index 000000000..3877de304 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt @@ -0,0 +1,27 @@ +find_package(ifbeam) +find_package(ifdh_art) + +cet_build_plugin(SBNDBNBEXTRetriever art::module + LIBRARIES + art::Persistency_Common + art::Utilities canvas::canvas + cetlib::cetlib cetlib_except::cetlib_except + ROOT::X3d + Boost::system + 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 + sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND + artdaq_core::artdaq-core_Utilities + sbnobj::Common_POTAccounting + larcorealg::CoreUtils +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc new file mode 100644 index 000000000..7ae66eedb --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc @@ -0,0 +1,214 @@ +//////////////////////////////////////////////////////////////////////// +// Class: SBNDBNBRetriever +// Plugin Type: producer +// File: SBNDBNBRetriever_module.cc +// +//////////////////////////////////////////////////////////////////////// + +#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/ParameterSet.h" +#include "messagefacility/MessageLogger/MessageLogger.h" + +#include +#include +#include +#include + +#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" +#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" +#include "artdaq-core/Data/ContainerFragment.hh" +//#include "sbndcode/Decoders/PTB/sbndptb.h" +#include "sbnobj/Common/POTAccounting/EXTCountInfo.h" + +#include "ifdh_art/IFBeamService/IFBeam_service.h" +#include "ifbeam_c.h" + +#include "larcorealg/CoreUtils/counter.h" + +namespace sbn { + class SBNDBNBRetriever; +} + +class sbn::SBNDBNBRetriever : public art::EDProducer { +public: + explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); + // Required functions. + void produce(art::Event & e) override; + void beginSubRun(art::SubRun& sr) override; + void endSubRun(art::SubRun& sr) override; + + // Plugins should not be copied or assigned. + SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; + + +private: + // Declare member data here. + std::vector< sbn::EXTCountInfo > fOutExtInfos; + struct PTBInfo_t { + double prevPTBTimeStamp = 0; + unsigned int GateCounter = 0; // FIXME needs to be integral type + }; + + struct TriggerInfo_t { + 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 + }; + + TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; + double extractTDCTimeStamp(art::Handle > cont_frags) const; + + // input labels + std::string raw_data_label_; + float TotalEXTCounts; + float totalMinBias; + float evtCount; + float scale_factor; +}; + +sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) + : EDProducer{params} { + produces< std::vector< sbn::EXTCountInfo >, art::InSubRun >(); + TotalEXTCounts = 0; + totalMinBias = 0; + evtCount = 0; + scale_factor = 0; +} + +int eventNum =0; +int _run; +int _subrun; +int _event; + +void sbn::SBNDBNBRetriever::produce(art::Event & e) +{ + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + TotalEXTCounts += triggerInfo.number_of_gates_since_previous_event; + + if(triggerInfo.number_of_gates_since_previous_event > 0){ + evtCount++; + totalMinBias += triggerInfo.number_of_gates_since_previous_event; + } + + //Store everything in our data-product + sbn::EXTCountInfo extInfo; + extInfo.gates_since_last_trigger = triggerInfo.number_of_gates_since_previous_event; + + fOutExtInfos.push_back(extInfo); +} + +sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { + int numcont = 0; + PTBInfo_t PTBInfo; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag = 0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck + for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) + { + if(ctb_frag.Trigger(word_i)){ + uint32_t wt = 0; + uint32_t word_type = ctb_frag.Word(word_i)->word_type; + wt = word_type; + if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(4)) + { + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + } + } + } //End of loop over the number of trigger words + } //End of loop over the number of fragments per container + } //End of loop over the number of containers + return PTBInfo; +} + +double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { + int numcont = 0; + uint64_t TDCTimeStamp = 0; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag = 0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::TDCTimestampFragment tdc_frag(frag); + TDCTimeStamp = tdc_frag.getTDCTimestamp()->timestamp_ns()/1e9; + } //End of loop over the number of fragments per container + } //End of loop over the number of containers + return TDCTimeStamp; +} + +sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { + // Using TDC for current event, but PTB for previous event + art::InputTag PTB_itag("daq", "ContainerPTB"); + auto PTB_cont_frags = e.getHandle(PTB_itag); + + art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); + auto TDC_cont_frags = e.getHandle(TDC_itag); + + PTBInfo_t PTBInfo; + TriggerInfo_t triggerInfo; + PTBInfo = extractPTBInfo(PTB_cont_frags); + double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + + triggerInfo.t_current_event = TDCTimeStamp; + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; + triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; + + return triggerInfo; +} + +void sbn::SBNDBNBRetriever::beginSubRun(art::SubRun& sr) +{ + TotalEXTCounts = 0; + totalMinBias = 0; + evtCount = 0; + scale_factor = 0; + return; +} + +void sbn::SBNDBNBRetriever::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()); + + return; +} + +DEFINE_ART_MODULE(sbn::SBNDBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc index a0a75b2ad..0c7d42179 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -160,7 +160,7 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand uint32_t wt = 0; uint32_t word_type = ctb_frag.Word(word_i)->word_type; wt = word_type; - if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) + if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(2)) { PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt new file mode 100644 index 000000000..675168065 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt @@ -0,0 +1,34 @@ +find_package(ifbeam) +find_package(ifdh_art) + + +art_make_library(LIBRARIES Boost::system + LIBRARY_NAME sbn_SBNDBNBZEROBIASSpillInfoRetriever_MWRData + SOURCE MWRData.cpp +) + +cet_build_plugin(SBNDBNBZEROBIASRetriever art::module + LIBRARIES + art::Persistency_Common + art::Utilities canvas::canvas + cetlib::cetlib cetlib_except::cetlib_except + ROOT::X3d + Boost::system + 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 + sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND + artdaq_core::artdaq-core_Utilities + sbn_SBNDBNBZEROBIASSpillInfoRetriever_MWRData + sbnobj::Common_POTAccounting + larcorealg::CoreUtils +) + +install_headers() +install_fhicl() +install_source() + diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp new file mode 100644 index 000000000..48d0bda3f --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include "MWRData.h" + +using namespace std; + +namespace sbn{ + + std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const +{ + + std::vector > unpacked_data; + unpacked_data.resize(4); + short data[444]; + + std::vector row(0); + boost::split(row, packed_data, boost::is_any_of(",")); + if (row.size()==447) { + for (int i=3;i<447;i++) { + data[i-3]=atoi(row[i].c_str()); + } + string devname=row[1].substr(0,8); + for (int idev=0;idev<4;idev++) { + mwrpulse_t mwr=getMWRdata(data,idev); + time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); + for (int ich=0;ich<48;ich++) { + unpacked_data[idev].push_back(mwr.hor[ich]); + } + for (int ich=0;ich<48;ich++) { + unpacked_data[idev].push_back(mwr.ver[ich]); + } + } + } else { + cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< +namespace sbn{ +class MWRData +{ + typedef struct swicheader_t { + long timesec; + long timensec; + long gpstime1; + long gpstime2; + short boosterevent; + short mievent; + short hz15micnt; + long delta1f; + short pulsemi; + short pulsesc; + } swicheader_t; + + typedef struct mwrpulse_t { + short hor[48]; + short ver[48]; + swicheader_t sheader; + } mwrpulse_t; + + static long flipByte(long data) + { + return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); + } + + mwrpulse_t getMWRdata(short* data, int nblock) const; + + public: + std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; +}; +} + +#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc new file mode 100644 index 000000000..424a30121 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc @@ -0,0 +1,545 @@ +//////////////////////////////////////////////////////////////////////// +// Class: SBNDBNBRetriever +// Plugin Type: producer +// File: SBNDBNBRetriever_module.cc +// +//////////////////////////////////////////////////////////////////////// + +#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/ParameterSet.h" +#include "messagefacility/MessageLogger/MessageLogger.h" + +#include +#include +#include +#include + +#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" +#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" +#include "artdaq-core/Data/ContainerFragment.hh" +//#include "sbndcode/Decoders/PTB/sbndptb.h" +#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" + +#include "ifdh_art/IFBeamService/IFBeam_service.h" +#include "ifbeam_c.h" +#include "MWRData.h" + +#include "larcorealg/CoreUtils/counter.h" + +namespace sbn { + class SBNDBNBRetriever; +} + +class sbn::SBNDBNBRetriever : public art::EDProducer { +public: + explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); + // Required functions. + void produce(art::Event & e) override; + void beginSubRun(art::SubRun& sr) override; + void endSubRun(art::SubRun& sr) override; + + // Plugins should not be copied or assigned. + SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; + SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; + + +private: + // Declare member data here. + std::vector< sbn::BNBSpillInfo > fOutbeamInfos; + double fTimePad; + std::string fInputLabel; + std::string fInputNonContainerInstance; + std::string fDeviceUsedForTiming; + std::string fOutputInstance; + std::string raw_data_label; + int fDebugLevel; + sbn::MWRData mwrdata; + art::ServiceHandle ifbeam_handle; + std::unique_ptr bfp; + std::unique_ptr bfp_mwr; + + struct PTBInfo_t { + double prevPTBTimeStamp = 0; + unsigned int GateCounter = 0; // FIXME needs to be integral type + }; + + struct TriggerInfo_t { + 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 + }; + + struct MWRdata_t { + std::vector< std::vector > MWR_times; + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + }; + + static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] + + TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; + double extractTDCTimeStamp(art::Handle > cont_frags) const; + MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; + void matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos + ) const; + unsigned int TotalBeamSpills; + sbn::BNBSpillInfo makeBNBSpillInfo + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; +}; + +sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) + : EDProducer{params} { + raw_data_label = params.get("raw_data_label", "daq"); + fInputLabel = params.get("InputLabel"); + fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); + fTimePad = params.get("TimePadding"); + fInputNonContainerInstance = params.get("InputNonContainerInstance"); + fOutputInstance = params.get("OutputInstance"); + fDebugLevel = params.get("DebugLevel",0); + bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); + bfp->set_epsilon(0.02); + bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); + bfp_mwr->set_epsilon(0.5); + bfp_mwr->setValidWindow(3605); + TotalBeamSpills = 0; + + produces< std::vector >(); +} + +int eventNum =0; +int _run; +int _subrun; +int _event; + +void sbn::SBNDBNBRetriever::produce(art::Event & e) +{ + + // If this is the first event in the run, then ignore it + // We do not currently have the ability to figure out the first + // spill that the DAQ was sensitive to, so don't try to save any + // spill information + + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; + MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); + + matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); + + auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); + std::swap(*p, fOutbeamInfos); + e.put(std::move(p)); +} + +sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { + int numcont = 0; + PTBInfo_t PTBInfo; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag = 0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck + for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) + { + if(ctb_frag.Trigger(word_i)){ + uint32_t wt = 0; + uint32_t word_type = ctb_frag.Word(word_i)->word_type; + wt = word_type; + if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) + { + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + } + } + } //End of loop over the number of trigger words + } //End of loop over the number of fragments per container + } //End of loop over the number of containers + return PTBInfo; +} + +double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { + int numcont = 0; + uint64_t TDCTimeStamp = 0; + for (auto const& cont : *cont_frags) + { + artdaq::ContainerFragment cont_frag(cont); + numcont++; + int numfrag = 0; + for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) + { + numfrag++; + artdaq::Fragment frag = *cont_frag[fragi]; + sbndaq::TDCTimestampFragment tdc_frag(frag); + TDCTimeStamp = tdc_frag.getTDCTimestamp()->timestamp_ns()/1e9; + } //End of loop over the number of fragments per container + } //End of loop over the number of containers + return TDCTimeStamp; +} + +sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { + // Using TDC for current event, but PTB for previous event + art::InputTag PTB_itag("daq", "ContainerPTB"); + auto PTB_cont_frags = e.getHandle(PTB_itag); + + art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); + auto TDC_cont_frags = e.getHandle(TDC_itag); + + PTBInfo_t PTBInfo; + TriggerInfo_t triggerInfo; + PTBInfo = extractPTBInfo(PTB_cont_frags); + double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + + triggerInfo.t_current_event = TDCTimeStamp; + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; + triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; + + return triggerInfo; +} + +sbn::SBNDBNBRetriever::MWRdata_t sbn::SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { + + // These lines get everything primed within the IFBeamDB. + try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; + try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; + try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {}; + try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; + try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 + // to correctly unpack it + std::vector< std::vector< std::vector< int > > > unpacked_MWR; + std::vector< std::vector< double> > MWR_times; + unpacked_MWR.resize(3); + MWR_times.resize(3); + std::string packed_data_str; + + // Create a list of all the MWR devices with their different + // memory buffer increments + // generally in the format: "E:.{Memory Block}" + std::vector vars = bfp_mwr->GetDeviceList(); + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; + // Tracking the time from the IFBeamDB + double time_for_mwr; + + // this is an iterator to track which of the + // three devices we will be working with + int dev = 0; + + // The MWR devices are annoying and have confusing buffer + // what we'll do is sort through all of them first and then + // match them to the closest spills in time + // + + // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; + int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; + mf::LogDebug("SBNDBNBRetriever") << " t_steps " << t_steps << std::endl; + + for(int t = 0; t < t_steps; t++){//Iterate through time increments + for (std::string const& var : vars) {// Iterate through the devices + + //Make sure we have a device + if(var.empty()){ + //mf::LogDebug("SBNDBNBRetriever") << " NO MWR DEVICES?!" << std::endl; + continue; + } + /// Check the device name and interate the double-vector index + if(var.find("M875BB") != std::string::npos ) dev = 0; + else if(var.find("M876BB") != std::string::npos ) dev = 1; + else if(var.find("MMBTBB") != std::string::npos ) dev = 2; + else{ + //mf::LogDebug("SBNDBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; + continue;} + + time_for_mwr = 0; + + try{ + + //Pull the MWR data for the device + // these data are "packed" + std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); + + //We'll convert this into a format + // that we can unpack doubles >> strings + // + packed_data_str.clear(); + packed_data_str += std::to_string(int(time_for_mwr)); + packed_data_str.append(","); + packed_data_str.append(var); + packed_data_str.append(",,"); + + /* for(auto const value: packed_MWR){ + packed_data_str += ','; + packed_data_str += std::to_string(int(value)); + }*/ + for(int j = 0; j < int(packed_MWR.size()); j++){ + packed_data_str += std::to_string(int(packed_MWR[j])); + if(j < int(packed_MWR.size())-1) + packed_data_str.append(","); + } + + // Use Zarko's unpacking function to turn this into consumeable data + std::vector MWR_times_temp; + + // There is a 35 ms offset between the toriod and the MWR times + // we'll just remove that here to match to the spill times + std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); + + //There are four events that are packed into one MWR IFBeam entry + for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ + + // If this entry has a unique time them store it for later + if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ + unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); + MWR_times[dev].push_back(MWR_times_temp[s]); + }//check for unique time + }//Iterate through the unpacked events + }//try + catch (WebAPIException &we) { + //Ignore when we can't find the MWR devices + // they don't always report and the timing of them can be annoying + }//catch + }// Iterate over all the multiwire devices + }// Iterate over all times + + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; + + return { std::move(MWR_times), std::move(unpacked_MWR) }; +} + +void sbn::SBNDBNBRetriever::matchMultiWireData( + art::EventID const& eventID, + TriggerInfo_t const& triggerInfo, + MWRdata_t const& MWRdata, bool isFirstEventInRun, + std::vector< sbn::BNBSpillInfo >& beamInfos +) const { + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + //Here we will start collecting all the other beamline devices + // First we get the times that the beamline device fired + // we have to pick a specific variable to use + std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); + + mf::LogDebug("SBNDBNBRetriever") << "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 spills_removed = 0; + std::vector matched_MWR; + matched_MWR.resize(3); + + // mf::LogDebug("SBNDBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; + // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Lower Limit : " << (triggerInfo.t_previous_event)+fTimePad << std::endl; + + // Iterating through each of the beamline times + + double best_diff = 10000000000.0; + double diff; + size_t i = 0; + + for (size_t k = 0; k < times_temps.size(); k++){ + diff = times_temps[k] - (triggerInfo.t_current_event + fTimePad); + if( diff < 0 and diff < best_diff){ + if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ + spills_removed++; + continue;} + if(times_temps[i] <= (triggerInfo.t_previous_event)+fTimePad){ + spills_removed++; + //mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + continue;} + best_diff = diff; + i = k; + } + } + for(int dev = 0; dev < int(MWR_times.size()); dev++){ + + //Loop through the multiwire times: + double Tdiff = 1000000000.; + matched_MWR[dev] = 0; + + for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ + + //found a candidate match! + if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} + + bool best_match = true; + + //Check for a better match... + 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;} + + //is there a better match later in the spill sequence + if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < + fabs((MWR_times[dev][mwrt] - times_temps[i]))){ + //we can have patience... + best_match = false; + break; + } + }//end better match check + + //Verified best match! + if(best_match == true){ + matched_MWR[dev] = mwrt; + Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); + } + }//end loop over MWR times + }//end loop over MWR devices + + sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); + + beamInfos.push_back(std::move(spillInfo)); + + // 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 iteration over beam device times + + // mf::LogDebug("SBNDBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; + +} + +sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo + (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const +{ + + auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias + + // initializing all of our device carriers + // device definitions can be found in BNBSpillInfo.h + + double TOR860 = 0; // units e12 protons + double TOR875 = 0; // units e12 protons + double LM875A = 0; // units R/s + double LM875B = 0; // units R/s + double LM875C = 0; // units R/s + double HP875 = 0; // units mm + double VP875 = 0; // units mm + double HPTG1 = 0; // units mm + double VPTG1 = 0; // units mm + double HPTG2 = 0; // units mm + double VPTG2 = 0; // units mm + double BTJT2 = 0; // units Deg C + double THCURR = 0; // units kiloAmps + + double TOR860_time = 0; // units s + + // Here we request all the devices + // since sometimes devices fail to report we'll + // allow each to throw an exception but still move forward + // interpreting these failures will be part of the beam quality analyses + + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + + //crunch the times + unsigned long int time_closest_int = (int) TOR860_time; + double time_closest_ns = (TOR860_time - time_closest_int)*1e9; + + //Store everything in our data-product + sbn::BNBSpillInfo beamInfo; + beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units + beamInfo.LM875A = LM875A; + beamInfo.LM875B = LM875B; + beamInfo.LM875C = LM875C; + beamInfo.HP875 = HP875; + beamInfo.VP875 = VP875; + beamInfo.HPTG1 = HPTG1; + beamInfo.VPTG1 = VPTG1; + beamInfo.HPTG2 = HPTG2; + beamInfo.VPTG2 = VPTG2; + beamInfo.BTJT2 = BTJT2; + beamInfo.THCURR = THCURR; + beamInfo.spill_time_s = time_closest_int; + beamInfo.spill_time_ns = time_closest_ns; + + for(auto const& MWRdata: unpacked_MWR){ + std::ignore = MWRdata; + assert(!MWRdata.empty()); + } + + if(unpacked_MWR[0].empty()){ + beamInfo.M875BB.clear(); + beamInfo.M875BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; + beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); + } + + if(unpacked_MWR[1].empty()){ + beamInfo.M876BB.clear(); + beamInfo.M876BB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; + beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); + } + + if(unpacked_MWR[2].empty()){ + beamInfo.MMBTBB.clear(); + beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds + } + else{ + beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; + beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); + } + // 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 + + beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun + + return beamInfo; +} + +void sbn::SBNDBNBRetriever::beginSubRun(art::SubRun& sr) +{ + return; +} + +void sbn::SBNDBNBRetriever::endSubRun(art::SubRun& sr) +{ + return; +} + +DEFINE_ART_MODULE(sbn::SBNDBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl new file mode 100644 index 000000000..e20bc0323 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl @@ -0,0 +1,46 @@ +#include "sbndbnbextspillinfo.fcl" + +process_name: SBNDBNBEXTInfoGen + +services:{ + + message: { + debugModules: [ "*" ] + destinations: { + LogDebugFile:{ + type: "file" + filename: "debug.log" + append: false + threshold: "DEBUG" + categories: { + default: {} + } + } + } + } + IFBeam:{} +} + + +source: { +} + +physics: { + producers: { + sbndbnbextinfo: @local::sbndbnbextspillinfo + } + + simulate: [sbndbnbextinfo ] + stream1: [ out1 ] +} + +outputs: { + out1: { + module_type: RootOutput + fileName: "%ifb_%tc_sbndbnbextinfo.root" + dataTier: "raw" + compressionLevel: 1 + } +} + +physics.producers.sbndbnbextinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl index b14918934..7f9f26df1 100644 --- a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl @@ -43,3 +43,5 @@ outputs: { compressionLevel: 1 } } + +physics.producers.sbndbnbinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl new file mode 100644 index 000000000..bcdf6635b --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl @@ -0,0 +1,46 @@ +#include "sbndbnbzerobiasspillinfo.fcl" + +process_name: SBNDBNBZEROBIASInfoGen + +services:{ + + message: { + debugModules: [ "*" ] + destinations: { + LogDebugFile:{ + type: "file" + filename: "debug.log" + append: false + threshold: "DEBUG" + categories: { + default: {} + } + } + } + } + IFBeam:{} +} + + +source: { +} + +physics: { + producers: { + sbndbnbzerobiasinfo: @local::sbndbnbzerobiasspillinfo + } + + simulate: [sbndbnbzerobiasinfo ] + stream1: [ out1 ] +} + +outputs: { + out1: { + module_type: RootOutput + fileName: "%ifb_%tc_sbndbnbzerobiasinfo.root" + dataTier: "raw" + compressionLevel: 1 + } +} + +physics.producers.sbndbnbzerobiasinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl new file mode 100644 index 000000000..14daf561f --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl @@ -0,0 +1,21 @@ +BEGIN_PROLOG + +sbndbnbextspillinfo: { + module_type: SBNDBNBEXTRetriever + InputLabel: "bnbext" + InputContainerInstance: "ContainerBNB" + InputNonContainerInstance: "BNB" + OutputInstance: "" + DebugLevel: 0 + TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away + URL: "https://dbdata3vm.fnal.gov:9443/ifbeam" + Bundle: "BoosterNeutrinoBeam_read" + MultiWireBundle: "BNBMultiWire" + TimeWindow: "700" #seconds + MWR_TimeWindow: "3601" #seconds + raw_data_label: "daq" + DeviceUsedForTiming: "E:TOR860" +} + +END_PROLOG + diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl index 2bf598456..4bdb732ef 100644 --- a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl @@ -8,7 +8,7 @@ sbndbnbspillinfo: { OutputInstance: "" DebugLevel: 0 TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away - URL: "" + URL: "" #Default URL having issues Nov 21 2024, used this instead https://dbdata3vm.fnal.gov:9443/ifbeam Bundle: "BoosterNeutrinoBeam_read" MultiWireBundle: "BNBMultiWire" TimeWindow: "700" #seconds diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl new file mode 100644 index 000000000..bb1c2302d --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl @@ -0,0 +1,21 @@ +BEGIN_PROLOG + +sbndbnbzerobiasspillinfo: { + module_type: SBNDBNBZEROBIASRetriever + InputLabel: "bnbzerobias" + InputContainerInstance: "ContainerBNB" + InputNonContainerInstance: "BNB" + OutputInstance: "" + DebugLevel: 0 + TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away + URL: "" + Bundle: "BoosterNeutrinoBeam_read" + MultiWireBundle: "BNBMultiWire" + TimeWindow: "700" #seconds + MWR_TimeWindow: "3601" #seconds + raw_data_label: "daq" + DeviceUsedForTiming: "E:TOR860" +} + +END_PROLOG + diff --git a/sbncode/CAFMaker/FillReco.cxx b/sbncode/CAFMaker/FillReco.cxx index a478bc840..0c35b8a59 100644 --- a/sbncode/CAFMaker/FillReco.cxx +++ b/sbncode/CAFMaker/FillReco.cxx @@ -119,8 +119,8 @@ namespace caf srspacepoint.position = SRVector3D(spacepoint.X(), spacepoint.Y(), spacepoint.Z()); srspacepoint.position_err = SRVector3D(spacepoint.XErr(), spacepoint.YErr(), spacepoint.ZErr()); srspacepoint.pe = spacepoint.PE(); - srspacepoint.time = spacepoint.Ts1(); - srspacepoint.time_err = spacepoint.Ts1Err(); + srspacepoint.time = spacepoint.Time(); + srspacepoint.time_err = spacepoint.TimeErr(); srspacepoint.complete = spacepoint.Complete(); } @@ -131,8 +131,8 @@ namespace caf for(auto const& point : track.Points()) srsbndcrttrack.points.emplace_back(point.X(), point.Y(), point.Z()); - srsbndcrttrack.time = track.Ts1(); - srsbndcrttrack.time_err = track.Ts1Err(); + srsbndcrttrack.time = track.Time(); + srsbndcrttrack.time_err = track.TimeErr(); srsbndcrttrack.pe = track.PE(); srsbndcrttrack.tof = track.ToF(); } From e2d8f6a11cb783e0a9ce05c06666a6477703680c Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Mon, 25 Nov 2024 19:10:16 -0600 Subject: [PATCH 17/26] Rename EXT producers, add catch for TDC --- sbncode/BeamSpillInfoRetriever/CMakeLists.txt | 2 +- .../CMakeLists.txt | 2 +- .../ICARUSBNBEXTRetriever_module.cc} | 28 +++++++-------- .../SBNDBNBEXTRetriever_module.cc | 34 +++++++++---------- .../SBNDBNBRetriever_module.cc | 24 ++++++++++--- .../job/bnbextcountinfo.fcl | 10 ------ .../job/icarusbnbextcountinfo.fcl | 10 ++++++ .../job/run_bnbextinfo_sbn.fcl | 32 ----------------- .../job/run_icarusbnbextinfo_sbn.fcl | 32 +++++++++++++++++ 9 files changed, 94 insertions(+), 80 deletions(-) rename sbncode/BeamSpillInfoRetriever/{BNBEXTRetriever => ICARUSBNBEXTRetriever}/CMakeLists.txt (91%) rename sbncode/BeamSpillInfoRetriever/{BNBEXTRetriever/BNBEXTRetriever_module.cc => ICARUSBNBEXTRetriever/ICARUSBNBEXTRetriever_module.cc} (86%) delete mode 100644 sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/icarusbnbextcountinfo.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl create mode 100644 sbncode/BeamSpillInfoRetriever/job/run_icarusbnbextinfo_sbn.fcl diff --git a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt index 2e5998b50..290ac1e29 100644 --- a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt @@ -1,9 +1,9 @@ add_subdirectory(ICARUSBNBRetriever) +add_subdirectory(ICARUSBNBEXTRetriever) add_subdirectory(SBNDBNBRetriever) add_subdirectory(SBNDBNBEXTRetriever) add_subdirectory(SBNDBNBZEROBIASRetriever) add_subdirectory(NuMIRetriever) -add_subdirectory(BNBEXTRetriever) add_subdirectory(NuMIEXTRetriever) add_subdirectory(job) diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/CMakeLists.txt similarity index 91% rename from sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt rename to sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/CMakeLists.txt index 1c49f8646..1b1cbce80 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/CMakeLists.txt @@ -1,5 +1,5 @@ -cet_build_plugin(BNBEXTRetriever art::module +cet_build_plugin(ICARUSBNBEXTRetriever art::module LIBRARIES art::Persistency_Common art::Utilities canvas::canvas diff --git a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/ICARUSBNBEXTRetriever_module.cc similarity index 86% rename from sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc rename to sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/ICARUSBNBEXTRetriever_module.cc index 1b7c79dd5..b54ea184f 100644 --- a/sbncode/BeamSpillInfoRetriever/BNBEXTRetriever/BNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBEXTRetriever/ICARUSBNBEXTRetriever_module.cc @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////// -// Class: BNBEXTRetriever +// Class: ICARUSBNBEXTRetriever // Plugin Type: producer -// File: BNBEXTRetriever_module.cc +// File: ICARUSBNBEXTRetriever_module.cc // // Created by hand Thurs June 24th 2021 by J. Zennamo (FNAL) // @@ -36,10 +36,10 @@ #include namespace sbn { - class BNBEXTRetriever; + class ICARUSBNBEXTRetriever; } -class sbn::BNBEXTRetriever : public art::EDProducer { +class sbn::ICARUSBNBEXTRetriever : public art::EDProducer { public: struct Config { @@ -57,15 +57,15 @@ class sbn::BNBEXTRetriever : public art::EDProducer { using Parameters = art::EDProducer::Table; - explicit BNBEXTRetriever(Parameters const& params); + explicit ICARUSBNBEXTRetriever(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. - BNBEXTRetriever(BNBEXTRetriever const&) = delete; - BNBEXTRetriever(BNBEXTRetriever&&) = delete; - BNBEXTRetriever& operator=(BNBEXTRetriever const&) = delete; - BNBEXTRetriever& operator=(BNBEXTRetriever&&) = delete; + ICARUSBNBEXTRetriever(ICARUSBNBEXTRetriever const&) = delete; + ICARUSBNBEXTRetriever(ICARUSBNBEXTRetriever&&) = delete; + ICARUSBNBEXTRetriever& operator=(ICARUSBNBEXTRetriever const&) = delete; + ICARUSBNBEXTRetriever& operator=(ICARUSBNBEXTRetriever&&) = delete; // Required functions. void produce(art::Event& e) override; @@ -85,7 +85,7 @@ class sbn::BNBEXTRetriever : public art::EDProducer { }; -sbn::BNBEXTRetriever::BNBEXTRetriever(Parameters const& params) +sbn::ICARUSBNBEXTRetriever::ICARUSBNBEXTRetriever(Parameters const& params) : EDProducer{params}, raw_data_label_(params().RawDataLabel()) { @@ -97,7 +97,7 @@ sbn::BNBEXTRetriever::BNBEXTRetriever(Parameters const& params) scale_factor = 0; } -void sbn::BNBEXTRetriever::produce(art::Event& e) +void sbn::ICARUSBNBEXTRetriever::produce(art::Event& e) { //Here we read in the artdaq Fragments and extract three pieces of information: @@ -152,7 +152,7 @@ void sbn::BNBEXTRetriever::produce(art::Event& e) } //end loop over events -void sbn::BNBEXTRetriever::beginSubRun(art::SubRun& sr) +void sbn::ICARUSBNBEXTRetriever::beginSubRun(art::SubRun& sr) { TotalEXTCounts = 0; totalMinBias = 0; @@ -162,7 +162,7 @@ void sbn::BNBEXTRetriever::beginSubRun(art::SubRun& sr) } //____________________________________________________________________________ -void sbn::BNBEXTRetriever::endSubRun(art::SubRun& sr) +void sbn::ICARUSBNBEXTRetriever::endSubRun(art::SubRun& sr) { // We will add all of the EXTCountInfo data-products to the // art::SubRun so it persists @@ -186,4 +186,4 @@ void sbn::BNBEXTRetriever::endSubRun(art::SubRun& sr) return; } -DEFINE_ART_MODULE(sbn::BNBEXTRetriever) +DEFINE_ART_MODULE(sbn::ICARUSBNBEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc index 7ae66eedb..108881fbf 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////// -// Class: SBNDBNBRetriever +// Class: SBNDBNBEXTRetriever // Plugin Type: producer -// File: SBNDBNBRetriever_module.cc +// File: SBNDBNBEXTRetriever_module.cc // //////////////////////////////////////////////////////////////////////// @@ -32,22 +32,22 @@ #include "larcorealg/CoreUtils/counter.h" namespace sbn { - class SBNDBNBRetriever; + class SBNDBNBEXTRetriever; } -class sbn::SBNDBNBRetriever : public art::EDProducer { +class sbn::SBNDBNBEXTRetriever : public art::EDProducer { public: - explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); + explicit SBNDBNBEXTRetriever(fhicl::ParameterSet const & params); // Required functions. void produce(art::Event & e) override; void beginSubRun(art::SubRun& sr) override; void endSubRun(art::SubRun& sr) override; // Plugins should not be copied or assigned. - SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; + SBNDBNBEXTRetriever(SBNDBNBEXTRetriever const &) = delete; + SBNDBNBEXTRetriever(SBNDBNBEXTRetriever &&) = delete; + SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever const &) = delete; + SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever &&) = delete; private: @@ -76,7 +76,7 @@ class sbn::SBNDBNBRetriever : public art::EDProducer { float scale_factor; }; -sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) +sbn::SBNDBNBEXTRetriever::SBNDBNBEXTRetriever(fhicl::ParameterSet const & params) : EDProducer{params} { produces< std::vector< sbn::EXTCountInfo >, art::InSubRun >(); TotalEXTCounts = 0; @@ -90,7 +90,7 @@ int _run; int _subrun; int _event; -void sbn::SBNDBNBRetriever::produce(art::Event & e) +void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) { TriggerInfo_t const triggerInfo = extractTriggerInfo(e); TotalEXTCounts += triggerInfo.number_of_gates_since_previous_event; @@ -107,7 +107,7 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) fOutExtInfos.push_back(extInfo); } -sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { +sbn::SBNDBNBEXTRetriever::PTBInfo_t sbn::SBNDBNBEXTRetriever::extractPTBInfo(art::Handle > cont_frags) const { int numcont = 0; PTBInfo_t PTBInfo; for (auto const& cont : *cont_frags) @@ -139,7 +139,7 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand return PTBInfo; } -double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { +double sbn::SBNDBNBEXTRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { int numcont = 0; uint64_t TDCTimeStamp = 0; for (auto const& cont : *cont_frags) @@ -158,7 +158,7 @@ double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle(PTB_itag); @@ -178,7 +178,7 @@ sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(a return triggerInfo; } -void sbn::SBNDBNBRetriever::beginSubRun(art::SubRun& sr) +void sbn::SBNDBNBEXTRetriever::beginSubRun(art::SubRun& sr) { TotalEXTCounts = 0; totalMinBias = 0; @@ -187,7 +187,7 @@ void sbn::SBNDBNBRetriever::beginSubRun(art::SubRun& sr) return; } -void sbn::SBNDBNBRetriever::endSubRun(art::SubRun& sr) +void sbn::SBNDBNBEXTRetriever::endSubRun(art::SubRun& sr) { // We will add all of the EXTCountInfo data-products to the // art::SubRun so it persists @@ -211,4 +211,4 @@ void sbn::SBNDBNBRetriever::endSubRun(art::SubRun& sr) return; } -DEFINE_ART_MODULE(sbn::SBNDBNBRetriever) +DEFINE_ART_MODULE(sbn::SBNDBNBEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc index 0c7d42179..0714f5b3f 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -12,9 +12,11 @@ #include "art/Framework/Principal/Run.h" #include "art/Framework/Principal/SubRun.h" #include "canvas/Utilities/InputTag.h" +#include "canvas/Utilities/Exception.h" #include "fhiclcpp/ParameterSet.h" #include "messagefacility/MessageLogger/MessageLogger.h" +#include #include #include #include @@ -67,6 +69,7 @@ class sbn::SBNDBNBRetriever : public art::EDProducer { std::unique_ptr bfp_mwr; struct PTBInfo_t { + double currPTBTimeStamp = 0; double prevPTBTimeStamp = 0; unsigned int GateCounter = 0; // FIXME needs to be integral type }; @@ -101,6 +104,7 @@ class sbn::SBNDBNBRetriever : public art::EDProducer { sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) : EDProducer{params} { + produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); raw_data_label = params.get("raw_data_label", "daq"); fInputLabel = params.get("InputLabel"); fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); @@ -162,9 +166,11 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand wt = word_type; if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(2)) { - PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; + PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp/20).to_ullong()/50e6; PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; } } } //End of loop over the number of trigger words @@ -175,6 +181,7 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { int numcont = 0; + uint64_t TDCTimeStamp = 0; for (auto const& cont : *cont_frags) { @@ -193,7 +200,7 @@ double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle(PTB_itag); @@ -203,9 +210,16 @@ sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(a PTBInfo_t PTBInfo; TriggerInfo_t triggerInfo; PTBInfo = extractPTBInfo(PTB_cont_frags); - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - triggerInfo.t_current_event = TDCTimeStamp; + if (TDC_cont_frags) { + double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + triggerInfo.t_current_event = TDCTimeStamp; + } + else{ + mf::LogDebug("SBNDBNBRetriever") << " Missing TDC Container Fragments!!! " << std::endl; + triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; + } + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; @@ -588,7 +602,7 @@ void sbn::SBNDBNBRetriever::endSubRun(art::SubRun& sr) { mf::LogDebug("SBNDBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; mf::LogDebug("SBNDBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; - + auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); std::swap(*p, fOutbeamInfos); diff --git a/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl deleted file mode 100644 index e8191e2dc..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/bnbextcountinfo.fcl +++ /dev/null @@ -1,10 +0,0 @@ - -BEGIN_PROLOG - -bnbextcountinfo: { - - module_type: "BNBEXTRetriever" - raw_data_label: "daq" - -} -END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/icarusbnbextcountinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/icarusbnbextcountinfo.fcl new file mode 100644 index 000000000..cabe02824 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/icarusbnbextcountinfo.fcl @@ -0,0 +1,10 @@ + +BEGIN_PROLOG + +icarusbnbextcountinfo: { + + module_type: "ICARUSBNBEXTRetriever" + raw_data_label: "daq" + +} +END_PROLOG diff --git a/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl deleted file mode 100644 index fbee5871f..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/run_bnbextinfo_sbn.fcl +++ /dev/null @@ -1,32 +0,0 @@ -#include "bnbextcountinfo.fcl" - -process_name: BNBEXTInfoGen - -services:{ - IFBeam:{} -} - - -source: { - -} - -physics: { - producers: { - bnbextinfo: @local::bnbextcountinfo - } - - simulate: [bnbextinfo ] - stream1: [ out1 ] - -} - -outputs: { - out1: { - module_type: RootOutput - fileName: "%ifb_%tc_bnbextinfo.root" - dataTier: "raw" - compressionLevel: 1 - } -} - diff --git a/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbextinfo_sbn.fcl new file mode 100644 index 000000000..c73bd8fc6 --- /dev/null +++ b/sbncode/BeamSpillInfoRetriever/job/run_icarusbnbextinfo_sbn.fcl @@ -0,0 +1,32 @@ +#include "icarusbnbextcountinfo.fcl" + +process_name: ICARUSBNBEXTInfoGen + +services:{ + IFBeam:{} +} + + +source: { + +} + +physics: { + producers: { + icarusbnbextinfo: @local::icarusbnbextcountinfo + } + + simulate: [icarusbnbextinfo ] + stream1: [ out1 ] + +} + +outputs: { + out1: { + module_type: RootOutput + fileName: "%ifb_%tc_icarusbnbextinfo.root" + dataTier: "raw" + compressionLevel: 1 + } +} + From 96682e93647964f4aa78985a56a4cbbb5f92f8cf Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Wed, 27 Nov 2024 16:50:28 -0600 Subject: [PATCH 18/26] Temp debug lines, TDC catch --- .../SBNDBNBEXTRetriever_module.cc | 35 ++++++++-- .../SBNDBNBRetriever_module.cc | 26 +++++-- .../SBNDBNBZEROBIASRetriever_module.cc | 67 +++++++++++++++---- 3 files changed, 107 insertions(+), 21 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc index 108881fbf..eaa3424fc 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc @@ -54,6 +54,7 @@ class sbn::SBNDBNBEXTRetriever : public art::EDProducer { // Declare member data here. std::vector< sbn::EXTCountInfo > fOutExtInfos; struct PTBInfo_t { + double currPTBTimeStamp = 0; double prevPTBTimeStamp = 0; unsigned int GateCounter = 0; // FIXME needs to be integral type }; @@ -92,7 +93,16 @@ int _event; void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) { + + std::cout << "new_event: " << e.event() << std::endl; + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + + std::cout << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; + std::cout << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; + std::cout << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; + + TotalEXTCounts += triggerInfo.number_of_gates_since_previous_event; if(triggerInfo.number_of_gates_since_previous_event > 0){ @@ -108,6 +118,8 @@ void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) } sbn::SBNDBNBEXTRetriever::PTBInfo_t sbn::SBNDBNBEXTRetriever::extractPTBInfo(art::Handle > cont_frags) const { + int HLT_count = 0; + int numcont = 0; PTBInfo_t PTBInfo; for (auto const& cont : *cont_frags) @@ -128,20 +140,26 @@ sbn::SBNDBNBEXTRetriever::PTBInfo_t sbn::SBNDBNBEXTRetriever::extractPTBInfo(art wt = word_type; if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(4)) { - PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + HLT_count++; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp / 20).to_ullong()/50e6; + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers + + std::cout << "HLT_count: " << HLT_count << std::endl; return PTBInfo; } double sbn::SBNDBNBEXTRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { int numcont = 0; - uint64_t TDCTimeStamp = 0; + double TDCTimeStamp = 0; for (auto const& cont : *cont_frags) { artdaq::ContainerFragment cont_frag(cont); @@ -152,7 +170,7 @@ double sbn::SBNDBNBEXTRetriever::extractTDCTimeStamp(art::Handletimestamp_ns()/1e9; + TDCTimeStamp = static_cast(tdc_frag.getTDCTimestamp()->timestamp_ns())/1e9; } //End of loop over the number of fragments per container } //End of loop over the number of containers return TDCTimeStamp; @@ -169,9 +187,16 @@ sbn::SBNDBNBEXTRetriever::TriggerInfo_t sbn::SBNDBNBEXTRetriever::extractTrigger PTBInfo_t PTBInfo; TriggerInfo_t triggerInfo; PTBInfo = extractPTBInfo(PTB_cont_frags); - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - triggerInfo.t_current_event = TDCTimeStamp; + if (TDC_cont_frags) { + double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + triggerInfo.t_current_event = TDCTimeStamp; + } + else{ + mf::LogDebug("SBNDBNBEXTRetriever") << " Missing TDC Contaienr Fragments!!! " << std::endl; + triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; + } + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc index 0714f5b3f..a77aa9dab 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -133,7 +133,14 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) // spill that the DAQ was sensitive to, so don't try to save any // spill information + std::cout << "new_event: " << e.event() << std::endl; + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + + std::cout << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; + std::cout << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; + std::cout << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; + TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); @@ -146,6 +153,8 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) } sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { + int HLT_count = 0; + int numcont = 0; PTBInfo_t PTBInfo; for (auto const& cont : *cont_frags) @@ -166,23 +175,28 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand wt = word_type; if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(2)) { + + HLT_count++; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; - PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp/20).to_ullong()/50e6; PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp/20).to_ullong()/50e6; PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers + + std::cout << "HLT_count: " << HLT_count << std::endl; return PTBInfo; } double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { int numcont = 0; - uint64_t TDCTimeStamp = 0; + double TDCTimeStamp = 0; for (auto const& cont : *cont_frags) { artdaq::ContainerFragment cont_frag(cont); @@ -193,7 +207,7 @@ double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handletimestamp_ns()/1e9; + TDCTimeStamp = static_cast(tdc_frag.getTDCTimestamp()->timestamp_ns())/1e9; } //End of loop over the number of fragments per container } //End of loop over the number of containers return TDCTimeStamp; @@ -217,6 +231,7 @@ sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(a } else{ mf::LogDebug("SBNDBNBRetriever") << " Missing TDC Container Fragments!!! " << std::endl; + std::cout << " Missing TDC Container Fragments!!! " << std::endl; triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; } @@ -517,7 +532,7 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo // allow each to throw an exception but still move forward // interpreting these failures will be part of the beam quality analyses - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n"; std::cout << "ifbeam_failed" << std::endl;} try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} @@ -534,6 +549,9 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo //crunch the times unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; + + std::cout << "TOR860_time: " << std::setprecision(19) << TOR860_time << std::endl; + std::cout << "TOR860: " << TOR860 << std::endl; //Store everything in our data-product sbn::BNBSpillInfo beamInfo; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc index 424a30121..5cf95fe2d 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc @@ -67,6 +67,7 @@ class sbn::SBNDBNBRetriever : public art::EDProducer { std::unique_ptr bfp_mwr; struct PTBInfo_t { + double currPTBTimeStamp = 0; double prevPTBTimeStamp = 0; unsigned int GateCounter = 0; // FIXME needs to be integral type }; @@ -131,7 +132,14 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) // spill that the DAQ was sensitive to, so don't try to save any // spill information + std::cout << "new_event: " << e.event() << std::endl; + TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + + std::cout << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; + std::cout << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; + std::cout << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; + TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); @@ -143,6 +151,8 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) } sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { + int HLT_count = 0; + int numcont = 0; PTBInfo_t PTBInfo; for (auto const& cont : *cont_frags) @@ -163,20 +173,27 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand wt = word_type; if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) { - PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + HLT_count++; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + // uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; + uint64_t RawcurrPTBTimeStamp = ctb_frag.TimeStamp(word_i) * 20; PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; + PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp / 20).to_ullong()/50e6; + PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers + + std::cout << "HLT_count: " << HLT_count << std::endl; return PTBInfo; } double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { int numcont = 0; - uint64_t TDCTimeStamp = 0; + double TDCTimeStamp = 0; for (auto const& cont : *cont_frags) { artdaq::ContainerFragment cont_frag(cont); @@ -186,8 +203,8 @@ double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handletimestamp_ns()/1e9; + sbndaq::TDCTimestampFragment tdc_frag(frag); + TDCTimeStamp = static_cast(tdc_frag.getTDCTimestamp()->timestamp_ns()) / 1e9; } //End of loop over the number of fragments per container } //End of loop over the number of containers return TDCTimeStamp; @@ -204,12 +221,35 @@ sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(a PTBInfo_t PTBInfo; TriggerInfo_t triggerInfo; PTBInfo = extractPTBInfo(PTB_cont_frags); - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - triggerInfo.t_current_event = TDCTimeStamp; + if (TDC_cont_frags) { + double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + triggerInfo.t_current_event = TDCTimeStamp; + } + else{ + mf::LogDebug("SBNDBNBZEROBIASRetriever") << " Missing TDC Container Fragments!!!" << std::endl; + std::cout << " Missing TDC Container Fragments!!!" << std::endl; + triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; + } + triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; + std::cout << "PTBdiff: " << PTBInfo.currPTBTimeStamp-PTBInfo.prevPTBTimeStamp << std::endl; + + if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp >= 1){ + std::cout << "Caught PTB bug, PTB late" << std::endl; + std::cout << "Before: " << triggerInfo.t_previous_event << std::endl; + triggerInfo.t_previous_event+=1; + std::cout << "After: " << triggerInfo.t_previous_event << std::endl; + } + else if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp <= -1){ + std::cout << "Caught PTB bug, PTB early" << std::endl; + std::cout << "Before: " << triggerInfo.t_previous_event << std::endl; + triggerInfo.t_previous_event-=1; + std::cout << "After: " << triggerInfo.t_previous_event << std::endl; + } + return triggerInfo; } @@ -364,13 +404,13 @@ void sbn::SBNDBNBRetriever::matchMultiWireData( for (size_t k = 0; k < times_temps.size(); k++){ diff = times_temps[k] - (triggerInfo.t_current_event + fTimePad); + if( diff < 0 and diff < best_diff){ - if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ + if(times_temps[k] > (triggerInfo.t_current_event)+fTimePad){ spills_removed++; continue;} - if(times_temps[i] <= (triggerInfo.t_previous_event)+fTimePad){ + if(times_temps[k] <= (triggerInfo.t_previous_event)+fTimePad){ spills_removed++; - //mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; continue;} best_diff = diff; i = k; @@ -455,8 +495,8 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo // since sometimes devices fail to report we'll // allow each to throw an exception but still move forward // interpreting these failures will be part of the beam quality analyses - - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} + + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n"; std::cout << "ifbeam_failed" << std::endl;} try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} @@ -473,7 +513,10 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo //crunch the times unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - + + std::cout << "TOR860_time: " << std::setprecision(19) << TOR860_time << std::endl; + std::cout << "TOR860: " << TOR860 << std::endl; + //Store everything in our data-product sbn::BNBSpillInfo beamInfo; beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units From 83f8895d064ea4da0e135628a60a8b6ce0f2a48d Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Thu, 5 Dec 2024 21:16:57 -0600 Subject: [PATCH 19/26] remove scaling, add debug --- .../SBNDBNBEXTRetriever_module.cc | 39 ++++---- .../SBNDBNBRetriever_module.cc | 37 +++++--- .../SBNDBNBZEROBIASRetriever_module.cc | 90 ++++++++++++------- 3 files changed, 101 insertions(+), 65 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc index eaa3424fc..2246dde03 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc @@ -74,7 +74,6 @@ class sbn::SBNDBNBEXTRetriever : public art::EDProducer { float TotalEXTCounts; float totalMinBias; float evtCount; - float scale_factor; }; sbn::SBNDBNBEXTRetriever::SBNDBNBEXTRetriever(fhicl::ParameterSet const & params) @@ -83,7 +82,6 @@ sbn::SBNDBNBEXTRetriever::SBNDBNBEXTRetriever(fhicl::ParameterSet const & params TotalEXTCounts = 0; totalMinBias = 0; evtCount = 0; - scale_factor = 0; } int eventNum =0; @@ -94,13 +92,13 @@ int _event; void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) { - std::cout << "new_event: " << e.event() << std::endl; + mf::LogDebug("SBNDBNBEXTRetriever") << "new_event: " << e.event() << std::endl; TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - std::cout << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; - std::cout << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; - std::cout << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; + mf::LogDebug("SBNDBNBEXTRetriever") << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; + mf::LogDebug("SBNDBNBEXTRetriever") << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; + mf::LogDebug("SBNDBNBEXTRetriever") << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; TotalEXTCounts += triggerInfo.number_of_gates_since_previous_event; @@ -113,7 +111,6 @@ void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) //Store everything in our data-product sbn::EXTCountInfo extInfo; extInfo.gates_since_last_trigger = triggerInfo.number_of_gates_since_previous_event; - fOutExtInfos.push_back(extInfo); } @@ -147,13 +144,13 @@ sbn::SBNDBNBEXTRetriever::PTBInfo_t sbn::SBNDBNBEXTRetriever::extractPTBInfo(art PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp / 20).to_ullong()/50e6; PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + break; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers - std::cout << "HLT_count: " << HLT_count << std::endl; return PTBInfo; } @@ -200,6 +197,19 @@ sbn::SBNDBNBEXTRetriever::TriggerInfo_t sbn::SBNDBNBEXTRetriever::extractTrigger triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; + if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp >= 1){ + mf::LogDebug("SBNDBNBEXTRetriever") << "Caught PTB bug, PTB late" << std::endl; + mf::LogDebug("SBNDBNBEXTRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; + triggerInfo.t_previous_event+=1; + mf::LogDebug("SBNDBNBEXTRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; + } + else if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp <= -1){ + mf::LogDebug("SBNDBNBEXTRetriever") << "Caught PTB bug, PTB early" << std::endl; + mf::LogDebug("SBNDBNBEXTRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; + triggerInfo.t_previous_event-=1; + mf::LogDebug("SBNDBNBEXTRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; + } + return triggerInfo; } @@ -208,7 +218,6 @@ void sbn::SBNDBNBEXTRetriever::beginSubRun(art::SubRun& sr) TotalEXTCounts = 0; totalMinBias = 0; evtCount = 0; - scale_factor = 0; return; } @@ -217,18 +226,6 @@ void sbn::SBNDBNBEXTRetriever::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()); diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc index a77aa9dab..8fbfae9ea 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -133,13 +133,13 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) // spill that the DAQ was sensitive to, so don't try to save any // spill information - std::cout << "new_event: " << e.event() << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "new_event: " << e.event() << std::endl; TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - std::cout << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; - std::cout << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; - std::cout << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); @@ -153,7 +153,6 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) } sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { - int HLT_count = 0; int numcont = 0; PTBInfo_t PTBInfo; @@ -173,23 +172,24 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand uint32_t wt = 0; uint32_t word_type = ctb_frag.Word(word_i)->word_type; wt = word_type; + mf::LogDebug("SBNDBNBRetriever") << "GateCounter!: " << ctb_frag.Trigger(word_i)->gate_counter << std::endl; if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(2)) { - HLT_count++; - uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp/20).to_ullong()/50e6; PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " prevPTBTimeStamp: " << PTBInfo.prevPTBTimeStamp << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " currPTBTimeStamp: " << PTBInfo.currPTBTimeStamp << std::endl; + break; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers - std::cout << "HLT_count: " << HLT_count << std::endl; return PTBInfo; } @@ -231,13 +231,26 @@ sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(a } else{ mf::LogDebug("SBNDBNBRetriever") << " Missing TDC Container Fragments!!! " << std::endl; - std::cout << " Missing TDC Container Fragments!!! " << std::endl; + mf::LogDebug("SBNDBNBRetriever") << " Missing TDC Container Fragments!!! " << std::endl; triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; } triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; + if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp >= 1){ + mf::LogDebug("SBNDBNBRetriever") << "Caught PTB bug, PTB late" << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; + triggerInfo.t_previous_event+=1; + mf::LogDebug("SBNDBNBRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; + } + else if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp <= -1){ + mf::LogDebug("SBNDBNBRetriever") << "Caught PTB bug, PTB early" << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; + triggerInfo.t_previous_event-=1; + mf::LogDebug("SBNDBNBRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; + } + return triggerInfo; } @@ -532,7 +545,7 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo // allow each to throw an exception but still move forward // interpreting these failures will be part of the beam quality analyses - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n"; std::cout << "ifbeam_failed" << std::endl;} + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} @@ -550,8 +563,8 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - std::cout << "TOR860_time: " << std::setprecision(19) << TOR860_time << std::endl; - std::cout << "TOR860: " << TOR860 << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "TOR860_time: " << std::setprecision(19) << TOR860_time << std::endl; + mf::LogDebug("SBNDBNBRetriever") << "TOR860: " << TOR860 << std::endl; //Store everything in our data-product sbn::BNBSpillInfo beamInfo; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc index 5cf95fe2d..b87e7bca0 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc @@ -115,8 +115,8 @@ sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) bfp_mwr->set_epsilon(0.5); bfp_mwr->setValidWindow(3605); TotalBeamSpills = 0; - - produces< std::vector >(); + produces< std::vector< sbn::BNBSpillInfo >, art::InEvent >(); + // produces< std::vector >(); } int eventNum =0; @@ -132,13 +132,20 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) // spill that the DAQ was sensitive to, so don't try to save any // spill information - std::cout << "new_event: " << e.event() << std::endl; + if (e.event() == 1) { + auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); + std::swap(*p, fOutbeamInfos); + e.put(std::move(p)); + return; + } + + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "new_event: " << e.event() << std::endl; TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - std::cout << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; - std::cout << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; - std::cout << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "triggerInfo.t_current_event: " << std::setprecision(25) << triggerInfo.t_current_event << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "triggerInfo.t_previous_event: " << std::setprecision(25) << triggerInfo.t_previous_event << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); @@ -151,7 +158,6 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) } sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { - int HLT_count = 0; int numcont = 0; PTBInfo_t PTBInfo; @@ -173,21 +179,23 @@ sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Hand wt = word_type; if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) { - HLT_count++; - - uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; + uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20.0; // uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; - uint64_t RawcurrPTBTimeStamp = ctb_frag.TimeStamp(word_i) * 20; - PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; - PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp / 20).to_ullong()/50e6; + uint64_t RawcurrPTBTimeStamp = ctb_frag.TimeStamp(word_i) * 20.0; + PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20.0).to_ullong()/50e6; + PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp / 20.0).to_ullong()/50e6; PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; + + mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " prevPTBTimeStamp: " << PTBInfo.prevPTBTimeStamp << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " currPTBTimeStamp: " << PTBInfo.currPTBTimeStamp << std::endl; + + break; } } } //End of loop over the number of trigger words } //End of loop over the number of fragments per container } //End of loop over the number of containers - std::cout << "HLT_count: " << HLT_count << std::endl; return PTBInfo; } @@ -228,26 +236,24 @@ sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(a } else{ mf::LogDebug("SBNDBNBZEROBIASRetriever") << " Missing TDC Container Fragments!!!" << std::endl; - std::cout << " Missing TDC Container Fragments!!!" << std::endl; triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; } triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; - std::cout << "PTBdiff: " << PTBInfo.currPTBTimeStamp-PTBInfo.prevPTBTimeStamp << std::endl; if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp >= 1){ - std::cout << "Caught PTB bug, PTB late" << std::endl; - std::cout << "Before: " << triggerInfo.t_previous_event << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Caught PTB bug, PTB late" << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; triggerInfo.t_previous_event+=1; - std::cout << "After: " << triggerInfo.t_previous_event << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; } else if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp <= -1){ - std::cout << "Caught PTB bug, PTB early" << std::endl; - std::cout << "Before: " << triggerInfo.t_previous_event << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Caught PTB bug, PTB early" << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; triggerInfo.t_previous_event-=1; - std::cout << "After: " << triggerInfo.t_previous_event << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; } return triggerInfo; @@ -398,6 +404,25 @@ void sbn::SBNDBNBRetriever::matchMultiWireData( // Iterating through each of the beamline times + if(isFirstEventInRun){ + //We'll remove the spills after our event + int spills_after_our_target = 0; + // iterate through all the spills to find the + // spills that are after our triggered event + for (size_t i = 0; i < times_temps.size(); i++) { + if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ + spills_after_our_target++; + } + }//end loop through spill times + + // Remove the spills after our trigger + times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); + + // 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" + double best_diff = 10000000000.0; double diff; size_t i = 0; @@ -406,12 +431,14 @@ void sbn::SBNDBNBRetriever::matchMultiWireData( diff = times_temps[k] - (triggerInfo.t_current_event + fTimePad); if( diff < 0 and diff < best_diff){ - if(times_temps[k] > (triggerInfo.t_current_event)+fTimePad){ - spills_removed++; - continue;} - if(times_temps[k] <= (triggerInfo.t_previous_event)+fTimePad){ - spills_removed++; - continue;} + if(!isFirstEventInRun){ + if(times_temps[k] > (triggerInfo.t_current_event)+fTimePad){ + spills_removed++; + continue;} + if(times_temps[k] <= (triggerInfo.t_previous_event)+fTimePad){ + spills_removed++; + continue;} + } best_diff = diff; i = k; } @@ -469,7 +496,6 @@ void sbn::SBNDBNBRetriever::matchMultiWireData( sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const { - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias // initializing all of our device carriers @@ -496,7 +522,7 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo // allow each to throw an exception but still move forward // interpreting these failures will be part of the beam quality analyses - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n"; std::cout << "ifbeam_failed" << std::endl;} + try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} @@ -514,8 +540,8 @@ sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo unsigned long int time_closest_int = (int) TOR860_time; double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - std::cout << "TOR860_time: " << std::setprecision(19) << TOR860_time << std::endl; - std::cout << "TOR860: " << TOR860 << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "TOR860_time: " << std::setprecision(25) << TOR860_time << std::endl; + mf::LogDebug("SBNDBNBZEROBIASRetriever") << "TOR860: " << TOR860 << std::endl; //Store everything in our data-product sbn::BNBSpillInfo beamInfo; From 448561a072cbf540cb4f5f277734af71f60ea03e Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Thu, 13 Feb 2025 18:43:26 -0600 Subject: [PATCH 20/26] Remove SBND modules --- .../SBNDBNBEXTRetriever/CMakeLists.txt | 27 - .../SBNDBNBEXTRetriever_module.cc | 236 ------- .../SBNDBNBRetriever/CMakeLists.txt | 34 - .../SBNDBNBRetriever/MWRData.cpp | 71 -- .../SBNDBNBRetriever/MWRData.h | 39 -- .../SBNDBNBRetriever_module.cc | 645 ------------------ .../SBNDBNBZEROBIASRetriever/CMakeLists.txt | 34 - .../SBNDBNBZEROBIASRetriever/MWRData.cpp | 71 -- .../SBNDBNBZEROBIASRetriever/MWRData.h | 39 -- .../SBNDBNBZEROBIASRetriever_module.cc | 614 ----------------- .../job/run_sbndbnbextinfo_sbn.fcl | 46 -- .../job/run_sbndbnbinfo_sbn.fcl | 47 -- .../job/run_sbndbnbzerobiasinfo_sbn.fcl | 46 -- .../job/sbndbnbextspillinfo.fcl | 21 - .../job/sbndbnbspillinfo.fcl | 21 - .../job/sbndbnbzerobiasspillinfo.fcl | 21 - sbncode/CAFMaker/FillReco.cxx | 8 +- 17 files changed, 4 insertions(+), 2016 deletions(-) delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.h delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.h delete mode 100644 sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc delete mode 100644 sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl delete mode 100644 sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt deleted file mode 100644 index 3877de304..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -find_package(ifbeam) -find_package(ifdh_art) - -cet_build_plugin(SBNDBNBEXTRetriever art::module - LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - ROOT::X3d - Boost::system - 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 - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND - artdaq_core::artdaq-core_Utilities - sbnobj::Common_POTAccounting - larcorealg::CoreUtils -) - -install_headers() -install_fhicl() -install_source() - diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc deleted file mode 100644 index 2246dde03..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc +++ /dev/null @@ -1,236 +0,0 @@ -//////////////////////////////////////////////////////////////////////// -// Class: SBNDBNBEXTRetriever -// Plugin Type: producer -// File: SBNDBNBEXTRetriever_module.cc -// -//////////////////////////////////////////////////////////////////////// - -#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/ParameterSet.h" -#include "messagefacility/MessageLogger/MessageLogger.h" - -#include -#include -#include -#include - -#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" -#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" -#include "artdaq-core/Data/ContainerFragment.hh" -//#include "sbndcode/Decoders/PTB/sbndptb.h" -#include "sbnobj/Common/POTAccounting/EXTCountInfo.h" - -#include "ifdh_art/IFBeamService/IFBeam_service.h" -#include "ifbeam_c.h" - -#include "larcorealg/CoreUtils/counter.h" - -namespace sbn { - class SBNDBNBEXTRetriever; -} - -class sbn::SBNDBNBEXTRetriever : public art::EDProducer { -public: - explicit SBNDBNBEXTRetriever(fhicl::ParameterSet const & params); - // Required functions. - void produce(art::Event & e) override; - void beginSubRun(art::SubRun& sr) override; - void endSubRun(art::SubRun& sr) override; - - // Plugins should not be copied or assigned. - SBNDBNBEXTRetriever(SBNDBNBEXTRetriever const &) = delete; - SBNDBNBEXTRetriever(SBNDBNBEXTRetriever &&) = delete; - SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever const &) = delete; - SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever &&) = delete; - - -private: - // Declare member data here. - std::vector< sbn::EXTCountInfo > fOutExtInfos; - struct PTBInfo_t { - double currPTBTimeStamp = 0; - double prevPTBTimeStamp = 0; - unsigned int GateCounter = 0; // FIXME needs to be integral type - }; - - struct TriggerInfo_t { - 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 - }; - - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; - double extractTDCTimeStamp(art::Handle > cont_frags) const; - - // input labels - std::string raw_data_label_; - float TotalEXTCounts; - float totalMinBias; - float evtCount; -}; - -sbn::SBNDBNBEXTRetriever::SBNDBNBEXTRetriever(fhicl::ParameterSet const & params) - : EDProducer{params} { - produces< std::vector< sbn::EXTCountInfo >, art::InSubRun >(); - TotalEXTCounts = 0; - totalMinBias = 0; - evtCount = 0; -} - -int eventNum =0; -int _run; -int _subrun; -int _event; - -void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) -{ - - mf::LogDebug("SBNDBNBEXTRetriever") << "new_event: " << e.event() << std::endl; - - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - - mf::LogDebug("SBNDBNBEXTRetriever") << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; - mf::LogDebug("SBNDBNBEXTRetriever") << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; - mf::LogDebug("SBNDBNBEXTRetriever") << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; - - - TotalEXTCounts += triggerInfo.number_of_gates_since_previous_event; - - if(triggerInfo.number_of_gates_since_previous_event > 0){ - evtCount++; - totalMinBias += triggerInfo.number_of_gates_since_previous_event; - } - - //Store everything in our data-product - sbn::EXTCountInfo extInfo; - extInfo.gates_since_last_trigger = triggerInfo.number_of_gates_since_previous_event; - fOutExtInfos.push_back(extInfo); -} - -sbn::SBNDBNBEXTRetriever::PTBInfo_t sbn::SBNDBNBEXTRetriever::extractPTBInfo(art::Handle > cont_frags) const { - int HLT_count = 0; - - int numcont = 0; - PTBInfo_t PTBInfo; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck - for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) - { - if(ctb_frag.Trigger(word_i)){ - uint32_t wt = 0; - uint32_t word_type = ctb_frag.Word(word_i)->word_type; - wt = word_type; - if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(4)) - { - HLT_count++; - - uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; - uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; - PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; - PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp / 20).to_ullong()/50e6; - PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; - break; - } - } - } //End of loop over the number of trigger words - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - - return PTBInfo; -} - -double sbn::SBNDBNBEXTRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { - int numcont = 0; - double TDCTimeStamp = 0; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::TDCTimestampFragment tdc_frag(frag); - TDCTimeStamp = static_cast(tdc_frag.getTDCTimestamp()->timestamp_ns())/1e9; - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - return TDCTimeStamp; -} - -sbn::SBNDBNBEXTRetriever::TriggerInfo_t sbn::SBNDBNBEXTRetriever::extractTriggerInfo(art::Event const& e) const { - // Using TDC for current event, but PTB for previous event - art::InputTag PTB_itag("daq", "ContainerPTB"); - auto PTB_cont_frags = e.getHandle(PTB_itag); - - art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); - auto TDC_cont_frags = e.getHandle(TDC_itag); - - PTBInfo_t PTBInfo; - TriggerInfo_t triggerInfo; - PTBInfo = extractPTBInfo(PTB_cont_frags); - - if (TDC_cont_frags) { - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - triggerInfo.t_current_event = TDCTimeStamp; - } - else{ - mf::LogDebug("SBNDBNBEXTRetriever") << " Missing TDC Contaienr Fragments!!! " << std::endl; - triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; - } - - triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; - triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; - - if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp >= 1){ - mf::LogDebug("SBNDBNBEXTRetriever") << "Caught PTB bug, PTB late" << std::endl; - mf::LogDebug("SBNDBNBEXTRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; - triggerInfo.t_previous_event+=1; - mf::LogDebug("SBNDBNBEXTRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; - } - else if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp <= -1){ - mf::LogDebug("SBNDBNBEXTRetriever") << "Caught PTB bug, PTB early" << std::endl; - mf::LogDebug("SBNDBNBEXTRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; - triggerInfo.t_previous_event-=1; - mf::LogDebug("SBNDBNBEXTRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; - } - - return triggerInfo; -} - -void sbn::SBNDBNBEXTRetriever::beginSubRun(art::SubRun& sr) -{ - TotalEXTCounts = 0; - totalMinBias = 0; - evtCount = 0; - return; -} - -void sbn::SBNDBNBEXTRetriever::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), art::subRunFragment()); - - return; -} - -DEFINE_ART_MODULE(sbn::SBNDBNBEXTRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt deleted file mode 100644 index d04512753..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -find_package(ifbeam) -find_package(ifdh_art) - - -art_make_library(LIBRARIES Boost::system - LIBRARY_NAME sbn_SBNDBNBSpillInfoRetriever_MWRData - SOURCE MWRData.cpp -) - -cet_build_plugin(SBNDBNBRetriever art::module - LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - ROOT::X3d - Boost::system - 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 - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND - artdaq_core::artdaq-core_Utilities - sbn_SBNDBNBSpillInfoRetriever_MWRData - sbnobj::Common_POTAccounting - larcorealg::CoreUtils -) - -install_headers() -install_fhicl() -install_source() - diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp deleted file mode 100644 index 48d0bda3f..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/MWRData.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "MWRData.h" - -using namespace std; - -namespace sbn{ - - std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const -{ - - std::vector > unpacked_data; - unpacked_data.resize(4); - short data[444]; - - std::vector row(0); - boost::split(row, packed_data, boost::is_any_of(",")); - if (row.size()==447) { - for (int i=3;i<447;i++) { - data[i-3]=atoi(row[i].c_str()); - } - string devname=row[1].substr(0,8); - for (int idev=0;idev<4;idev++) { - mwrpulse_t mwr=getMWRdata(data,idev); - time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.hor[ich]); - } - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.ver[ich]); - } - } - } else { - cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< -namespace sbn{ -class MWRData -{ - typedef struct swicheader_t { - long timesec; - long timensec; - long gpstime1; - long gpstime2; - short boosterevent; - short mievent; - short hz15micnt; - long delta1f; - short pulsemi; - short pulsesc; - } swicheader_t; - - typedef struct mwrpulse_t { - short hor[48]; - short ver[48]; - swicheader_t sheader; - } mwrpulse_t; - - static long flipByte(long data) - { - return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); - } - - mwrpulse_t getMWRdata(short* data, int nblock) const; - - public: - std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; -}; -} - -#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc deleted file mode 100644 index 8fbfae9ea..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ /dev/null @@ -1,645 +0,0 @@ -//////////////////////////////////////////////////////////////////////// -// Class: SBNDBNBRetriever -// Plugin Type: producer -// File: SBNDBNBRetriever_module.cc -// -//////////////////////////////////////////////////////////////////////// - -#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 "canvas/Utilities/Exception.h" -#include "fhiclcpp/ParameterSet.h" -#include "messagefacility/MessageLogger/MessageLogger.h" - -#include -#include -#include -#include -#include - -#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" -#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" -#include "artdaq-core/Data/ContainerFragment.hh" -//#include "sbndcode/Decoders/PTB/sbndptb.h" -#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" - -#include "ifdh_art/IFBeamService/IFBeam_service.h" -#include "ifbeam_c.h" -#include "MWRData.h" - -#include "larcorealg/CoreUtils/counter.h" - -namespace sbn { - class SBNDBNBRetriever; -} - -class sbn::SBNDBNBRetriever : public art::EDProducer { -public: - explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); - // Required functions. - void produce(art::Event & e) override; - void beginSubRun(art::SubRun& sr) override; - void endSubRun(art::SubRun& sr) override; - - // Plugins should not be copied or assigned. - SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; - - -private: - // Declare member data here. - std::vector< sbn::BNBSpillInfo > fOutbeamInfos; - double fTimePad; - std::string fInputLabel; - std::string fInputNonContainerInstance; - std::string fDeviceUsedForTiming; - std::string fOutputInstance; - std::string raw_data_label; - int fDebugLevel; - sbn::MWRData mwrdata; - art::ServiceHandle ifbeam_handle; - std::unique_ptr bfp; - std::unique_ptr bfp_mwr; - - struct PTBInfo_t { - double currPTBTimeStamp = 0; - double prevPTBTimeStamp = 0; - unsigned int GateCounter = 0; // FIXME needs to be integral type - }; - - struct TriggerInfo_t { - 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 - }; - - struct MWRdata_t { - std::vector< std::vector > MWR_times; - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - }; - - static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] - - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; - double extractTDCTimeStamp(art::Handle > cont_frags) const; - MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; - int matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos - ) const; - unsigned int TotalBeamSpills; - sbn::BNBSpillInfo makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; -}; - -sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) - : EDProducer{params} { - produces< std::vector< sbn::BNBSpillInfo >, art::InSubRun >(); - raw_data_label = params.get("raw_data_label", "daq"); - fInputLabel = params.get("InputLabel"); - fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); - fTimePad = params.get("TimePadding"); - fInputNonContainerInstance = params.get("InputNonContainerInstance"); - fOutputInstance = params.get("OutputInstance"); - fDebugLevel = params.get("DebugLevel",0); - bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); - bfp->set_epsilon(0.02); - bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); - bfp_mwr->set_epsilon(0.5); - bfp_mwr->setValidWindow(3605); - TotalBeamSpills = 0; -} - -int eventNum =0; -int _run; -int _subrun; -int _event; - -void sbn::SBNDBNBRetriever::produce(art::Event & e) -{ - - // If this is the first event in the run, then ignore it - // We do not currently have the ability to figure out the first - // spill that the DAQ was sensitive to, so don't try to save any - // spill information - - mf::LogDebug("SBNDBNBRetriever") << "new_event: " << e.event() << std::endl; - - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - - mf::LogDebug("SBNDBNBRetriever") << "triggerInfo.t_current_event: " << std::setprecision(19) << triggerInfo.t_current_event << std::endl; - mf::LogDebug("SBNDBNBRetriever") << "triggerInfo.t_previous_event: " << std::setprecision(19) << triggerInfo.t_previous_event << std::endl; - mf::LogDebug("SBNDBNBRetriever") << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; - - TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); - - int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); - - if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) - mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; - else - mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; -} - -sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { - - int numcont = 0; - PTBInfo_t PTBInfo; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck - for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) - { - if(ctb_frag.Trigger(word_i)){ - uint32_t wt = 0; - uint32_t word_type = ctb_frag.Word(word_i)->word_type; - wt = word_type; - mf::LogDebug("SBNDBNBRetriever") << "GateCounter!: " << ctb_frag.Trigger(word_i)->gate_counter << std::endl; - if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(2)) - { - - uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20; - uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; - PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20).to_ullong()/50e6; - PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp/20).to_ullong()/50e6; - PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " prevPTBTimeStamp: " << PTBInfo.prevPTBTimeStamp << std::endl; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " currPTBTimeStamp: " << PTBInfo.currPTBTimeStamp << std::endl; - break; - } - } - } //End of loop over the number of trigger words - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - - return PTBInfo; -} - -double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { - int numcont = 0; - - double TDCTimeStamp = 0; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::TDCTimestampFragment tdc_frag(frag); - TDCTimeStamp = static_cast(tdc_frag.getTDCTimestamp()->timestamp_ns())/1e9; - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - return TDCTimeStamp; -} - -sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { - // Using TDC for current event, but PTB for previous event. Exception for case where no TDC. - art::InputTag PTB_itag("daq", "ContainerPTB"); - auto PTB_cont_frags = e.getHandle(PTB_itag); - - art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); - auto TDC_cont_frags = e.getHandle(TDC_itag); - - PTBInfo_t PTBInfo; - TriggerInfo_t triggerInfo; - PTBInfo = extractPTBInfo(PTB_cont_frags); - - if (TDC_cont_frags) { - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - triggerInfo.t_current_event = TDCTimeStamp; - } - else{ - mf::LogDebug("SBNDBNBRetriever") << " Missing TDC Container Fragments!!! " << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Missing TDC Container Fragments!!! " << std::endl; - triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; - } - - triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; - triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; - - if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp >= 1){ - mf::LogDebug("SBNDBNBRetriever") << "Caught PTB bug, PTB late" << std::endl; - mf::LogDebug("SBNDBNBRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; - triggerInfo.t_previous_event+=1; - mf::LogDebug("SBNDBNBRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; - } - else if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp <= -1){ - mf::LogDebug("SBNDBNBRetriever") << "Caught PTB bug, PTB early" << std::endl; - mf::LogDebug("SBNDBNBRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; - triggerInfo.t_previous_event-=1; - mf::LogDebug("SBNDBNBRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; - } - - return triggerInfo; -} - -sbn::SBNDBNBRetriever::MWRdata_t sbn::SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { - - // These lines get everything primed within the IFBeamDB. - try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; - try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {}; - try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; - try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 - // to correctly unpack it - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - std::vector< std::vector< double> > MWR_times; - unpacked_MWR.resize(3); - MWR_times.resize(3); - std::string packed_data_str; - - // Create a list of all the MWR devices with their different - // memory buffer increments - // generally in the format: "E:.{Memory Block}" - std::vector vars = bfp_mwr->GetDeviceList(); - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; - // Tracking the time from the IFBeamDB - double time_for_mwr; - - // this is an iterator to track which of the - // three devices we will be working with - int dev = 0; - - // The MWR devices are annoying and have confusing buffer - // what we'll do is sort through all of them first and then - // match them to the closest spills in time - // - - // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; - int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; - mf::LogDebug("SBNDBNBRetriever") << " t_steps " << t_steps << std::endl; - - for(int t = 0; t < t_steps; t++){//Iterate through time increments - for (std::string const& var : vars) {// Iterate through the devices - - //Make sure we have a device - if(var.empty()){ - //mf::LogDebug("SBNDBNBRetriever") << " NO MWR DEVICES?!" << std::endl; - continue; - } - /// Check the device name and interate the double-vector index - if(var.find("M875BB") != std::string::npos ) dev = 0; - else if(var.find("M876BB") != std::string::npos ) dev = 1; - else if(var.find("MMBTBB") != std::string::npos ) dev = 2; - else{ - //mf::LogDebug("SBNDBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; - continue;} - - time_for_mwr = 0; - - try{ - - //Pull the MWR data for the device - // these data are "packed" - std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); - - //We'll convert this into a format - // that we can unpack doubles >> strings - // - packed_data_str.clear(); - packed_data_str += std::to_string(int(time_for_mwr)); - packed_data_str.append(","); - packed_data_str.append(var); - packed_data_str.append(",,"); - - /* for(auto const value: packed_MWR){ - packed_data_str += ','; - packed_data_str += std::to_string(int(value)); - }*/ - for(int j = 0; j < int(packed_MWR.size()); j++){ - packed_data_str += std::to_string(int(packed_MWR[j])); - if(j < int(packed_MWR.size())-1) - packed_data_str.append(","); - } - - // Use Zarko's unpacking function to turn this into consumeable data - std::vector MWR_times_temp; - - // There is a 35 ms offset between the toriod and the MWR times - // we'll just remove that here to match to the spill times - std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); - - //There are four events that are packed into one MWR IFBeam entry - for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ - - // If this entry has a unique time them store it for later - if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ - unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); - MWR_times[dev].push_back(MWR_times_temp[s]); - }//check for unique time - }//Iterate through the unpacked events - }//try - catch (WebAPIException &we) { - //Ignore when we can't find the MWR devices - // they don't always report and the timing of them can be annoying - }//catch - }// Iterate over all the multiwire devices - }// Iterate over all times - - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; - - return { std::move(MWR_times), std::move(unpacked_MWR) }; -} - -int sbn::SBNDBNBRetriever::matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos -) const { - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - //Here we will start collecting all the other beamline devices - // First we get the times that the beamline device fired - // we have to pick a specific variable to use - std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); - - mf::LogDebug("SBNDBNBRetriever") << "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); - - // NOTE: for now, this is dead code because we don't - // do anything for the first event in a run. We may want to revisit - // this later to understand if there is a way we can do the POT - // accounting in the first event. - // - // Need to handle the first event in a run differently - if(isFirstEventInRun){ - //We'll remove the spills after our event - int spills_after_our_target = 0; - // iterate through all the spills to find the - // spills that are after our triggered event - for (size_t i = 0; i < times_temps.size(); i++) { - if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ - spills_after_our_target++; - } - }//end loop through spill times - - // Remove the spills after our trigger - times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); - - // 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("SBNDBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; - // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; - // mf::LogDebug("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; - - if(!isFirstEventInRun){//We already addressed the "first event" above - if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ - //mf::LogDebug("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; - continue;} - } - - //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("SBNDBNBRetriever") << 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("SBNDBNBRetriever") << 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++; - - //Loop through the multiwire devices: - - for(int dev = 0; dev < int(MWR_times.size()); dev++){ - - //Loop through the multiwire times: - double Tdiff = 1000000000.; - matched_MWR[dev] = 0; - - for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ - - //found a candidate match! - if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} - - bool best_match = true; - - //Check for a better match... - 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;} - - //is there a better match later in the spill sequence - if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < - fabs((MWR_times[dev][mwrt] - times_temps[i]))){ - //we can have patience... - best_match = false; - break; - } - }//end better match check - - //Verified best match! - if(best_match == true){ - matched_MWR[dev] = mwrt; - Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); - } - - }//end loop over MWR times - - }//end loop over MWR devices - - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); - - beamInfos.push_back(std::move(spillInfo)); - - // 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 iteration over beam device times - - // mf::LogDebug("SBNDBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; - - return spill_count; -} - -sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const -{ - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - // initializing all of our device carriers - // device definitions can be found in BNBSpillInfo.h - - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps - - double TOR860_time = 0; // units s - - // Here we request all the devices - // since sometimes devices fail to report we'll - // allow each to throw an exception but still move forward - // interpreting these failures will be part of the beam quality analyses - - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - - //crunch the times - unsigned long int time_closest_int = (int) TOR860_time; - double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - - mf::LogDebug("SBNDBNBRetriever") << "TOR860_time: " << std::setprecision(19) << TOR860_time << std::endl; - mf::LogDebug("SBNDBNBRetriever") << "TOR860: " << TOR860 << std::endl; - - //Store everything in our data-product - sbn::BNBSpillInfo beamInfo; - beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.LM875A = LM875A; - beamInfo.LM875B = LM875B; - beamInfo.LM875C = LM875C; - beamInfo.HP875 = HP875; - beamInfo.VP875 = VP875; - beamInfo.HPTG1 = HPTG1; - beamInfo.VPTG1 = VPTG1; - beamInfo.HPTG2 = HPTG2; - beamInfo.VPTG2 = VPTG2; - beamInfo.BTJT2 = BTJT2; - beamInfo.THCURR = THCURR; - beamInfo.spill_time_s = time_closest_int; - beamInfo.spill_time_ns = time_closest_ns; - - for(auto const& MWRdata: unpacked_MWR){ - std::ignore = MWRdata; - assert(!MWRdata.empty()); - } - - if(unpacked_MWR[0].empty()){ - beamInfo.M875BB.clear(); - beamInfo.M875BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; - beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); - } - - if(unpacked_MWR[1].empty()){ - beamInfo.M876BB.clear(); - beamInfo.M876BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; - beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); - } - - if(unpacked_MWR[2].empty()){ - beamInfo.MMBTBB.clear(); - beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; - beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); - } - // 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 - - beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun - - return beamInfo; -} - -void sbn::SBNDBNBRetriever::beginSubRun(art::SubRun& sr) -{ - return; -} - -void sbn::SBNDBNBRetriever::endSubRun(art::SubRun& sr) -{ - mf::LogDebug("SBNDBNBRetriever")<< "Total number of DAQ Spills : " << TotalBeamSpills << std::endl; - mf::LogDebug("SBNDBNBRetriever")<< "Total number of Selected Spills : " << fOutbeamInfos.size() << std::endl; - - auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); - std::swap(*p, fOutbeamInfos); - - sr.put(std::move(p), art::subRunFragment()); - - return; -} - -DEFINE_ART_MODULE(sbn::SBNDBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt deleted file mode 100644 index 675168065..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -find_package(ifbeam) -find_package(ifdh_art) - - -art_make_library(LIBRARIES Boost::system - LIBRARY_NAME sbn_SBNDBNBZEROBIASSpillInfoRetriever_MWRData - SOURCE MWRData.cpp -) - -cet_build_plugin(SBNDBNBZEROBIASRetriever art::module - LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - ROOT::X3d - Boost::system - 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 - sbndaq_artdaq_core::sbndaq-artdaq-core_Overlays_SBND - artdaq_core::artdaq-core_Utilities - sbn_SBNDBNBZEROBIASSpillInfoRetriever_MWRData - sbnobj::Common_POTAccounting - larcorealg::CoreUtils -) - -install_headers() -install_fhicl() -install_source() - diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp deleted file mode 100644 index 48d0bda3f..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/MWRData.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "MWRData.h" - -using namespace std; - -namespace sbn{ - - std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const -{ - - std::vector > unpacked_data; - unpacked_data.resize(4); - short data[444]; - - std::vector row(0); - boost::split(row, packed_data, boost::is_any_of(",")); - if (row.size()==447) { - for (int i=3;i<447;i++) { - data[i-3]=atoi(row[i].c_str()); - } - string devname=row[1].substr(0,8); - for (int idev=0;idev<4;idev++) { - mwrpulse_t mwr=getMWRdata(data,idev); - time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.hor[ich]); - } - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.ver[ich]); - } - } - } else { - cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< -namespace sbn{ -class MWRData -{ - typedef struct swicheader_t { - long timesec; - long timensec; - long gpstime1; - long gpstime2; - short boosterevent; - short mievent; - short hz15micnt; - long delta1f; - short pulsemi; - short pulsesc; - } swicheader_t; - - typedef struct mwrpulse_t { - short hor[48]; - short ver[48]; - swicheader_t sheader; - } mwrpulse_t; - - static long flipByte(long data) - { - return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); - } - - mwrpulse_t getMWRdata(short* data, int nblock) const; - - public: - std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; -}; -} - -#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc deleted file mode 100644 index b87e7bca0..000000000 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc +++ /dev/null @@ -1,614 +0,0 @@ -//////////////////////////////////////////////////////////////////////// -// Class: SBNDBNBRetriever -// Plugin Type: producer -// File: SBNDBNBRetriever_module.cc -// -//////////////////////////////////////////////////////////////////////// - -#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/ParameterSet.h" -#include "messagefacility/MessageLogger/MessageLogger.h" - -#include -#include -#include -#include - -#include "sbndaq-artdaq-core/Overlays/SBND/PTBFragment.hh" -#include "sbndaq-artdaq-core/Overlays/SBND/TDCTimestampFragment.hh" -#include "artdaq-core/Data/ContainerFragment.hh" -//#include "sbndcode/Decoders/PTB/sbndptb.h" -#include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" - -#include "ifdh_art/IFBeamService/IFBeam_service.h" -#include "ifbeam_c.h" -#include "MWRData.h" - -#include "larcorealg/CoreUtils/counter.h" - -namespace sbn { - class SBNDBNBRetriever; -} - -class sbn::SBNDBNBRetriever : public art::EDProducer { -public: - explicit SBNDBNBRetriever(fhicl::ParameterSet const & params); - // Required functions. - void produce(art::Event & e) override; - void beginSubRun(art::SubRun& sr) override; - void endSubRun(art::SubRun& sr) override; - - // Plugins should not be copied or assigned. - SBNDBNBRetriever(SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever(SBNDBNBRetriever &&) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever const &) = delete; - SBNDBNBRetriever & operator = (SBNDBNBRetriever &&) = delete; - - -private: - // Declare member data here. - std::vector< sbn::BNBSpillInfo > fOutbeamInfos; - double fTimePad; - std::string fInputLabel; - std::string fInputNonContainerInstance; - std::string fDeviceUsedForTiming; - std::string fOutputInstance; - std::string raw_data_label; - int fDebugLevel; - sbn::MWRData mwrdata; - art::ServiceHandle ifbeam_handle; - std::unique_ptr bfp; - std::unique_ptr bfp_mwr; - - struct PTBInfo_t { - double currPTBTimeStamp = 0; - double prevPTBTimeStamp = 0; - unsigned int GateCounter = 0; // FIXME needs to be integral type - }; - - struct TriggerInfo_t { - 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 - }; - - struct MWRdata_t { - std::vector< std::vector > MWR_times; - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - }; - - static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] - - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - PTBInfo_t extractPTBInfo(art::Handle > cont_frags) const; - double extractTDCTimeStamp(art::Handle > cont_frags) const; - MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; - void matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos - ) const; - unsigned int TotalBeamSpills; - sbn::BNBSpillInfo makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const; -}; - -sbn::SBNDBNBRetriever::SBNDBNBRetriever(fhicl::ParameterSet const & params) - : EDProducer{params} { - raw_data_label = params.get("raw_data_label", "daq"); - fInputLabel = params.get("InputLabel"); - fDeviceUsedForTiming = params.get("DeviceUsedForTiming"); - fTimePad = params.get("TimePadding"); - fInputNonContainerInstance = params.get("InputNonContainerInstance"); - fOutputInstance = params.get("OutputInstance"); - fDebugLevel = params.get("DebugLevel",0); - bfp = ifbeam_handle->getBeamFolder(params.get("Bundle"), params.get("URL"), std::stod(params.get("TimeWindow"))); - bfp->set_epsilon(0.02); - bfp_mwr = ifbeam_handle->getBeamFolder(params.get("MultiWireBundle"), params.get("URL"), std::stod(params.get("MWR_TimeWindow"))); - bfp_mwr->set_epsilon(0.5); - bfp_mwr->setValidWindow(3605); - TotalBeamSpills = 0; - produces< std::vector< sbn::BNBSpillInfo >, art::InEvent >(); - // produces< std::vector >(); -} - -int eventNum =0; -int _run; -int _subrun; -int _event; - -void sbn::SBNDBNBRetriever::produce(art::Event & e) -{ - - // If this is the first event in the run, then ignore it - // We do not currently have the ability to figure out the first - // spill that the DAQ was sensitive to, so don't try to save any - // spill information - - if (e.event() == 1) { - auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); - std::swap(*p, fOutbeamInfos); - e.put(std::move(p)); - return; - } - - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "new_event: " << e.event() << std::endl; - - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); - - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "triggerInfo.t_current_event: " << std::setprecision(25) << triggerInfo.t_current_event << std::endl; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "triggerInfo.t_previous_event: " << std::setprecision(25) << triggerInfo.t_previous_event << std::endl; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "triggerInfo.number_of_gates_since_previous_event: " << triggerInfo.number_of_gates_since_previous_event << std::endl; - - TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); - - matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); - - auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); - std::swap(*p, fOutbeamInfos); - e.put(std::move(p)); -} - -sbn::SBNDBNBRetriever::PTBInfo_t sbn::SBNDBNBRetriever::extractPTBInfo(art::Handle > cont_frags) const { - - int numcont = 0; - PTBInfo_t PTBInfo; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::CTBFragment ctb_frag(frag); // somehow the name CTBFragment stuck - for(size_t word_i = 0; word_i < ctb_frag.NWords(); ++word_i) - { - if(ctb_frag.Trigger(word_i)){ - uint32_t wt = 0; - uint32_t word_type = ctb_frag.Word(word_i)->word_type; - wt = word_type; - if (wt == 2 && ctb_frag.Trigger(word_i)->IsTrigger(1)) - { - uint64_t RawprevPTBTimeStamp = ctb_frag.PTBWord(word_i)->prevTS * 20.0; - // uint64_t RawcurrPTBTimeStamp = ctb_frag.Trigger(word_i)->timestamp * 20; - uint64_t RawcurrPTBTimeStamp = ctb_frag.TimeStamp(word_i) * 20.0; - PTBInfo.prevPTBTimeStamp = std::bitset<64>(RawprevPTBTimeStamp / 20.0).to_ullong()/50e6; - PTBInfo.currPTBTimeStamp = std::bitset<64>(RawcurrPTBTimeStamp / 20.0).to_ullong()/50e6; - PTBInfo.GateCounter = ctb_frag.Trigger(word_i)->gate_counter; - - mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " prevPTBTimeStamp: " << PTBInfo.prevPTBTimeStamp << std::endl; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << std::setprecision(25) << " currPTBTimeStamp: " << PTBInfo.currPTBTimeStamp << std::endl; - - break; - } - } - } //End of loop over the number of trigger words - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - - return PTBInfo; -} - -double sbn::SBNDBNBRetriever::extractTDCTimeStamp(art::Handle > cont_frags) const { - int numcont = 0; - double TDCTimeStamp = 0; - for (auto const& cont : *cont_frags) - { - artdaq::ContainerFragment cont_frag(cont); - numcont++; - int numfrag = 0; - for (size_t fragi = 0; fragi < cont_frag.block_count(); ++fragi) - { - numfrag++; - artdaq::Fragment frag = *cont_frag[fragi]; - sbndaq::TDCTimestampFragment tdc_frag(frag); - TDCTimeStamp = static_cast(tdc_frag.getTDCTimestamp()->timestamp_ns()) / 1e9; - } //End of loop over the number of fragments per container - } //End of loop over the number of containers - return TDCTimeStamp; -} - -sbn::SBNDBNBRetriever::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { - // Using TDC for current event, but PTB for previous event - art::InputTag PTB_itag("daq", "ContainerPTB"); - auto PTB_cont_frags = e.getHandle(PTB_itag); - - art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); - auto TDC_cont_frags = e.getHandle(TDC_itag); - - PTBInfo_t PTBInfo; - TriggerInfo_t triggerInfo; - PTBInfo = extractPTBInfo(PTB_cont_frags); - - if (TDC_cont_frags) { - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); - triggerInfo.t_current_event = TDCTimeStamp; - } - else{ - mf::LogDebug("SBNDBNBZEROBIASRetriever") << " Missing TDC Container Fragments!!!" << std::endl; - triggerInfo.t_current_event = PTBInfo.currPTBTimeStamp; - } - - triggerInfo.t_previous_event = PTBInfo.prevPTBTimeStamp; - triggerInfo.number_of_gates_since_previous_event = PTBInfo.GateCounter; - - - if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp >= 1){ - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Caught PTB bug, PTB late" << std::endl; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; - triggerInfo.t_previous_event+=1; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; - } - else if(triggerInfo.t_current_event - PTBInfo.currPTBTimeStamp <= -1){ - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Caught PTB bug, PTB early" << std::endl; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "Before: " << triggerInfo.t_previous_event << std::endl; - triggerInfo.t_previous_event-=1; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "After: " << triggerInfo.t_previous_event << std::endl; - } - - return triggerInfo; -} - -sbn::SBNDBNBRetriever::MWRdata_t sbn::SBNDBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { - - // These lines get everything primed within the IFBeamDB. - try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; - try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; - try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {}; - try{bfp_mwr->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; - try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 - // to correctly unpack it - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - std::vector< std::vector< double> > MWR_times; - unpacked_MWR.resize(3); - MWR_times.resize(3); - std::string packed_data_str; - - // Create a list of all the MWR devices with their different - // memory buffer increments - // generally in the format: "E:.{Memory Block}" - std::vector vars = bfp_mwr->GetDeviceList(); - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; - // Tracking the time from the IFBeamDB - double time_for_mwr; - - // this is an iterator to track which of the - // three devices we will be working with - int dev = 0; - - // The MWR devices are annoying and have confusing buffer - // what we'll do is sort through all of them first and then - // match them to the closest spills in time - // - - // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; - int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; - mf::LogDebug("SBNDBNBRetriever") << " t_steps " << t_steps << std::endl; - - for(int t = 0; t < t_steps; t++){//Iterate through time increments - for (std::string const& var : vars) {// Iterate through the devices - - //Make sure we have a device - if(var.empty()){ - //mf::LogDebug("SBNDBNBRetriever") << " NO MWR DEVICES?!" << std::endl; - continue; - } - /// Check the device name and interate the double-vector index - if(var.find("M875BB") != std::string::npos ) dev = 0; - else if(var.find("M876BB") != std::string::npos ) dev = 1; - else if(var.find("MMBTBB") != std::string::npos ) dev = 2; - else{ - //mf::LogDebug("SBNDBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; - continue;} - - time_for_mwr = 0; - - try{ - - //Pull the MWR data for the device - // these data are "packed" - std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); - - //We'll convert this into a format - // that we can unpack doubles >> strings - // - packed_data_str.clear(); - packed_data_str += std::to_string(int(time_for_mwr)); - packed_data_str.append(","); - packed_data_str.append(var); - packed_data_str.append(",,"); - - /* for(auto const value: packed_MWR){ - packed_data_str += ','; - packed_data_str += std::to_string(int(value)); - }*/ - for(int j = 0; j < int(packed_MWR.size()); j++){ - packed_data_str += std::to_string(int(packed_MWR[j])); - if(j < int(packed_MWR.size())-1) - packed_data_str.append(","); - } - - // Use Zarko's unpacking function to turn this into consumeable data - std::vector MWR_times_temp; - - // There is a 35 ms offset between the toriod and the MWR times - // we'll just remove that here to match to the spill times - std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); - - //There are four events that are packed into one MWR IFBeam entry - for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ - - // If this entry has a unique time them store it for later - if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ - unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); - MWR_times[dev].push_back(MWR_times_temp[s]); - }//check for unique time - }//Iterate through the unpacked events - }//try - catch (WebAPIException &we) { - //Ignore when we can't find the MWR devices - // they don't always report and the timing of them can be annoying - }//catch - }// Iterate over all the multiwire devices - }// Iterate over all times - - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; - mf::LogDebug("SBNDBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; - - return { std::move(MWR_times), std::move(unpacked_MWR) }; -} - -void sbn::SBNDBNBRetriever::matchMultiWireData( - art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, - std::vector< sbn::BNBSpillInfo >& beamInfos -) const { - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - //Here we will start collecting all the other beamline devices - // First we get the times that the beamline device fired - // we have to pick a specific variable to use - std::vector times_temps = bfp->GetTimeList(fDeviceUsedForTiming); - - mf::LogDebug("SBNDBNBRetriever") << "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 spills_removed = 0; - std::vector matched_MWR; - matched_MWR.resize(3); - - // mf::LogDebug("SBNDBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; - // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; - // mf::LogDebug("SBNDBNBRetriever") << std::setprecision(19) << "Lower Limit : " << (triggerInfo.t_previous_event)+fTimePad << std::endl; - - // Iterating through each of the beamline times - - if(isFirstEventInRun){ - //We'll remove the spills after our event - int spills_after_our_target = 0; - // iterate through all the spills to find the - // spills that are after our triggered event - for (size_t i = 0; i < times_temps.size(); i++) { - if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ - spills_after_our_target++; - } - }//end loop through spill times - - // Remove the spills after our trigger - times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); - - // 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" - - double best_diff = 10000000000.0; - double diff; - size_t i = 0; - - for (size_t k = 0; k < times_temps.size(); k++){ - diff = times_temps[k] - (triggerInfo.t_current_event + fTimePad); - - if( diff < 0 and diff < best_diff){ - if(!isFirstEventInRun){ - if(times_temps[k] > (triggerInfo.t_current_event)+fTimePad){ - spills_removed++; - continue;} - if(times_temps[k] <= (triggerInfo.t_previous_event)+fTimePad){ - spills_removed++; - continue;} - } - best_diff = diff; - i = k; - } - } - for(int dev = 0; dev < int(MWR_times.size()); dev++){ - - //Loop through the multiwire times: - double Tdiff = 1000000000.; - matched_MWR[dev] = 0; - - for(int mwrt = 0; mwrt < int(MWR_times[dev].size()); mwrt++){ - - //found a candidate match! - if(fabs((MWR_times[dev][mwrt] - times_temps[i])) >= Tdiff){continue;} - - bool best_match = true; - - //Check for a better match... - 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;} - - //is there a better match later in the spill sequence - if(fabs((MWR_times[dev][mwrt] - times_temps[j])) < - fabs((MWR_times[dev][mwrt] - times_temps[i]))){ - //we can have patience... - best_match = false; - break; - } - }//end better match check - - //Verified best match! - if(best_match == true){ - matched_MWR[dev] = mwrt; - Tdiff = fabs((MWR_times[dev][mwrt] - times_temps[i])); - } - }//end loop over MWR times - }//end loop over MWR devices - - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); - - beamInfos.push_back(std::move(spillInfo)); - - // 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 iteration over beam device times - - // mf::LogDebug("SBNDBNBRetriever") << "matchMultiWireData:: Total spills counted: " << spill_count << " Total spills removed : " << spills_removed << std::endl; - -} - -sbn::BNBSpillInfo sbn::SBNDBNBRetriever::makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const -{ - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - // initializing all of our device carriers - // device definitions can be found in BNBSpillInfo.h - - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps - - double TOR860_time = 0; // units s - - // Here we request all the devices - // since sometimes devices fail to report we'll - // allow each to throw an exception but still move forward - // interpreting these failures will be part of the beam quality analyses - - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("SBNDBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - - //crunch the times - unsigned long int time_closest_int = (int) TOR860_time; - double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "TOR860_time: " << std::setprecision(25) << TOR860_time << std::endl; - mf::LogDebug("SBNDBNBZEROBIASRetriever") << "TOR860: " << TOR860 << std::endl; - - //Store everything in our data-product - sbn::BNBSpillInfo beamInfo; - beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.LM875A = LM875A; - beamInfo.LM875B = LM875B; - beamInfo.LM875C = LM875C; - beamInfo.HP875 = HP875; - beamInfo.VP875 = VP875; - beamInfo.HPTG1 = HPTG1; - beamInfo.VPTG1 = VPTG1; - beamInfo.HPTG2 = HPTG2; - beamInfo.VPTG2 = VPTG2; - beamInfo.BTJT2 = BTJT2; - beamInfo.THCURR = THCURR; - beamInfo.spill_time_s = time_closest_int; - beamInfo.spill_time_ns = time_closest_ns; - - for(auto const& MWRdata: unpacked_MWR){ - std::ignore = MWRdata; - assert(!MWRdata.empty()); - } - - if(unpacked_MWR[0].empty()){ - beamInfo.M875BB.clear(); - beamInfo.M875BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; - beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); - } - - if(unpacked_MWR[1].empty()){ - beamInfo.M876BB.clear(); - beamInfo.M876BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; - beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); - } - - if(unpacked_MWR[2].empty()){ - beamInfo.MMBTBB.clear(); - beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; - beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); - } - // 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 - - beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun - - return beamInfo; -} - -void sbn::SBNDBNBRetriever::beginSubRun(art::SubRun& sr) -{ - return; -} - -void sbn::SBNDBNBRetriever::endSubRun(art::SubRun& sr) -{ - return; -} - -DEFINE_ART_MODULE(sbn::SBNDBNBRetriever) diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl deleted file mode 100644 index e20bc0323..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbextinfo_sbn.fcl +++ /dev/null @@ -1,46 +0,0 @@ -#include "sbndbnbextspillinfo.fcl" - -process_name: SBNDBNBEXTInfoGen - -services:{ - - message: { - debugModules: [ "*" ] - destinations: { - LogDebugFile:{ - type: "file" - filename: "debug.log" - append: false - threshold: "DEBUG" - categories: { - default: {} - } - } - } - } - IFBeam:{} -} - - -source: { -} - -physics: { - producers: { - sbndbnbextinfo: @local::sbndbnbextspillinfo - } - - simulate: [sbndbnbextinfo ] - stream1: [ out1 ] -} - -outputs: { - out1: { - module_type: RootOutput - fileName: "%ifb_%tc_sbndbnbextinfo.root" - dataTier: "raw" - compressionLevel: 1 - } -} - -physics.producers.sbndbnbextinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl deleted file mode 100644 index 7f9f26df1..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbinfo_sbn.fcl +++ /dev/null @@ -1,47 +0,0 @@ -#include "sbndbnbspillinfo.fcl" - -process_name: SBNDBNBInfoGen - -services:{ - - message: { - debugModules: [ "*" ] - destinations: { - LogDebugFile:{ - type: "file" - filename: "debug.log" - append: false - threshold: "DEBUG" - categories: { - default: {} - } - } - } - } - IFBeam:{} -} - - -source: { - -} - -physics: { - producers: { - sbndbnbinfo: @local::sbndbnbspillinfo - } - - simulate: [sbndbnbinfo ] - stream1: [ out1 ] -} - -outputs: { - out1: { - module_type: RootOutput - fileName: "%ifb_%tc_sbndbnbinfo.root" - dataTier: "raw" - compressionLevel: 1 - } -} - -physics.producers.sbndbnbinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl b/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl deleted file mode 100644 index bcdf6635b..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/run_sbndbnbzerobiasinfo_sbn.fcl +++ /dev/null @@ -1,46 +0,0 @@ -#include "sbndbnbzerobiasspillinfo.fcl" - -process_name: SBNDBNBZEROBIASInfoGen - -services:{ - - message: { - debugModules: [ "*" ] - destinations: { - LogDebugFile:{ - type: "file" - filename: "debug.log" - append: false - threshold: "DEBUG" - categories: { - default: {} - } - } - } - } - IFBeam:{} -} - - -source: { -} - -physics: { - producers: { - sbndbnbzerobiasinfo: @local::sbndbnbzerobiasspillinfo - } - - simulate: [sbndbnbzerobiasinfo ] - stream1: [ out1 ] -} - -outputs: { - out1: { - module_type: RootOutput - fileName: "%ifb_%tc_sbndbnbzerobiasinfo.root" - dataTier: "raw" - compressionLevel: 1 - } -} - -physics.producers.sbndbnbzerobiasinfo.fileNames: @local::outputs.out1.fileName diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl deleted file mode 100644 index 14daf561f..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/sbndbnbextspillinfo.fcl +++ /dev/null @@ -1,21 +0,0 @@ -BEGIN_PROLOG - -sbndbnbextspillinfo: { - module_type: SBNDBNBEXTRetriever - InputLabel: "bnbext" - InputContainerInstance: "ContainerBNB" - InputNonContainerInstance: "BNB" - OutputInstance: "" - DebugLevel: 0 - TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away - URL: "https://dbdata3vm.fnal.gov:9443/ifbeam" - Bundle: "BoosterNeutrinoBeam_read" - MultiWireBundle: "BNBMultiWire" - TimeWindow: "700" #seconds - MWR_TimeWindow: "3601" #seconds - raw_data_label: "daq" - DeviceUsedForTiming: "E:TOR860" -} - -END_PROLOG - diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl deleted file mode 100644 index 4bdb732ef..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl +++ /dev/null @@ -1,21 +0,0 @@ -BEGIN_PROLOG - -sbndbnbspillinfo: { - module_type: SBNDBNBRetriever - InputLabel: "bnb" - InputContainerInstance: "ContainerBNB" - InputNonContainerInstance: "BNB" - OutputInstance: "" - DebugLevel: 0 - TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away - URL: "" #Default URL having issues Nov 21 2024, used this instead https://dbdata3vm.fnal.gov:9443/ifbeam - Bundle: "BoosterNeutrinoBeam_read" - MultiWireBundle: "BNBMultiWire" - TimeWindow: "700" #seconds - MWR_TimeWindow: "3601" #seconds - raw_data_label: "daq" - DeviceUsedForTiming: "E:TOR860" -} - -END_PROLOG - diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl deleted file mode 100644 index bb1c2302d..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/sbndbnbzerobiasspillinfo.fcl +++ /dev/null @@ -1,21 +0,0 @@ -BEGIN_PROLOG - -sbndbnbzerobiasspillinfo: { - module_type: SBNDBNBZEROBIASRetriever - InputLabel: "bnbzerobias" - InputContainerInstance: "ContainerBNB" - InputNonContainerInstance: "BNB" - OutputInstance: "" - DebugLevel: 0 - TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away - URL: "" - Bundle: "BoosterNeutrinoBeam_read" - MultiWireBundle: "BNBMultiWire" - TimeWindow: "700" #seconds - MWR_TimeWindow: "3601" #seconds - raw_data_label: "daq" - DeviceUsedForTiming: "E:TOR860" -} - -END_PROLOG - diff --git a/sbncode/CAFMaker/FillReco.cxx b/sbncode/CAFMaker/FillReco.cxx index db4abc88f..358be7d94 100644 --- a/sbncode/CAFMaker/FillReco.cxx +++ b/sbncode/CAFMaker/FillReco.cxx @@ -122,8 +122,8 @@ namespace caf srspacepoint.position = SRVector3D(spacepoint.X(), spacepoint.Y(), spacepoint.Z()); srspacepoint.position_err = SRVector3D(spacepoint.XErr(), spacepoint.YErr(), spacepoint.ZErr()); srspacepoint.pe = spacepoint.PE(); - srspacepoint.time = spacepoint.Time(); - srspacepoint.time_err = spacepoint.TimeErr(); + srspacepoint.time = spacepoint.Ts1(); + srspacepoint.time_err = spacepoint.Ts1Err(); srspacepoint.complete = spacepoint.Complete(); } @@ -134,8 +134,8 @@ namespace caf for(auto const& point : track.Points()) srsbndcrttrack.points.emplace_back(point.X(), point.Y(), point.Z()); - srsbndcrttrack.time = track.Time(); - srsbndcrttrack.time_err = track.TimeErr(); + srsbndcrttrack.time = track.Ts1(); + srsbndcrttrack.time_err = track.Ts1Err(); srsbndcrttrack.pe = track.PE(); srsbndcrttrack.tof = track.ToF(); } From 0aef7afb681a671508e8504214032f83e819af66 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Thu, 13 Feb 2025 19:20:20 -0600 Subject: [PATCH 21/26] $1D offset was hardcoded! Change BES offset to fcl param. --- .../ICARUSBNBRetriever_module.cc | 14 +++++++++----- .../job/icarusbnbspillinfo.fcl | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc index f559c1df8..86e6f9d34 100644 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc @@ -121,6 +121,7 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { // input labels std::vector< sbn::BNBSpillInfo > fOutbeamInfos; double fTimePad; + double fBESOffset; std::string fURL; MWRData mwrdata; int run_number; @@ -257,6 +258,7 @@ int sbn::ICARUSBNBRetriever::get_trigger_type_matching_gate(sqlite3 *db, int fun sbn::ICARUSBNBRetriever::ICARUSBNBRetriever(Parameters const& params) : EDProducer{params}, fTimePad(params().TimePadding()), + fBESOffset(params().BESOffset()), raw_data_label(params().RawDataLabel()), fDeviceUsedForTiming(params().DeviceUsedForTiming()), bfp( ifbeam_handle->getBeamFolder(params().Bundle(), params().URL(), params().TimeWindow())), @@ -370,11 +372,12 @@ sbn::ICARUSBNBRetriever::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerIn */ - triggerInfo.t_current_event = static_cast(artdaq_ts-3.6e7)/(1000000000.0); //check this offset... + double BESinTSUnits = fBESOffset*1e9; // flc param is in seconds need to convert to match TS + triggerInfo.t_current_event = static_cast(artdaq_ts-BESinTSUnits)/(1000000000.0); //check this offset... if(triggerInfo.gate_type == 1) - triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()-3.6e7))/(1e9); + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampBNBMaj()-BESinTSUnits))/(1e9); else - triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()-3.6e7))/(1000000000.0); + triggerInfo.t_previous_event = (static_cast(frag.getLastTimestampOther()-BESinTSUnits))/(1000000000.0); } @@ -581,9 +584,10 @@ int sbn::ICARUSBNBRetriever::matchMultiWireData( DocDB 33155 provides documentation of this */ - mf::LogDebug("ICARUSBNBRetriever") << 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; + double BESinTSUnits = fBESOffset*1e9; // flc param is in seconds need to convert to match TS + mf::LogDebug("ICARUSBNBRetriever") << 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+BESinTSUnits, 40.) << " times : spill " << times_temps[i]*1.e9 << " - " << triggerInfo.WR_to_Spill_conversion << " + " << BESinTSUnits << 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){ + if(get_trigger_type_matching_gate(db, callback_trigger_type, run_number, times_temps[i]*1.e9-triggerInfo.WR_to_Spill_conversion+BESinTSUnits, 40.) == 1){ mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "matchMultiWireData:: Skipped a MinBias gate at : " << times_temps[i]*1000. << std::endl; continue; diff --git a/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl index 9bc7e15d2..a7b4b0dbc 100644 --- a/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl @@ -5,6 +5,7 @@ icarusbnbspillinfo: { module_type: "ICARUSBNBRetriever" TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away + BESOffset: 0.036 #unit seconds, we expect the BES to be about 35 ms from $1D. Need to adjust search range accordingly. URL: "" #keep this blank and we're good Bundle: "BoosterNeutrinoBeam_read" MultiWireBundle: "BNBMultiWire" From f1a7e632bfabb43aca5f2b4a717bcefb248a2e23 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Tue, 18 Mar 2025 13:30:25 -0500 Subject: [PATCH 22/26] remove newly-duplicate code in icarus --- .../ICARUSBNBRetriever/CMakeLists.txt | 26 +- .../ICARUSBNBRetriever_module.cc | 302 +----------------- .../ICARUSBNBRetriever/MWRData.cpp | 71 ---- .../ICARUSBNBRetriever/MWRData.h | 39 --- sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h | 2 + 5 files changed, 17 insertions(+), 423 deletions(-) delete mode 100644 sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp delete mode 100644 sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.h diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt index 2668311a3..a9379d867 100644 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt @@ -1,34 +1,12 @@ find_package(ifbeam) find_package(ifdh_art) - -art_make_library(LIBRARIES Boost::system - LIBRARY_NAME sbn_ICARUSBNBSpillInfoRetriever_MWRData - SOURCE MWRData.cpp -) - - cet_build_plugin(ICARUSBNBRetriever art::module LIBRARIES - art::Persistency_Common - art::Utilities canvas::canvas - cetlib::cetlib cetlib_except::cetlib_except - ROOT::X3d - Boost::system - 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 - artdaq_core::artdaq-core_Utilities - sbn_ICARUSBNBSpillInfoRetriever_MWRData - sbnobj::Common_POTAccounting - larcorealg::CoreUtils + sbn_SBNDPOTTools + SQLite::SQLite3 ) install_headers() install_fhicl() install_source() - diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc index 86e6f9d34..6be19dc1b 100644 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc @@ -10,33 +10,12 @@ */ #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 "larcorealg/CoreUtils/counter.h" - -#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" - -#include "ifdh_art/IFBeamService/IFBeam_service.h" -#include "ifbeam_c.h" -#include "MWRData.h" - -#include -#include -#include - +#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbnobj/Common/Trigger/ExtraTriggerInfo.h" +#include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" #include -#include -#include namespace sbn { class ICARUSBNBRetriever; @@ -55,7 +34,13 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { Comment{ "extension to the time window considered when collecting spills [seconds]" }, 0.0333 // default }; - + + fhicl::Atom BESOffset { + Name{ "BESOffset" }, + Comment{ "Offset between beam early warning signal and $1D [seconds]" }, + 0.036 // default + }; + fhicl::Atom RawDataLabel { Name{ "raw_data_label" }, Comment{ "art data product instance name for trigger information (product label is 'daq')" } @@ -138,32 +123,10 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { 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 { - std::vector< std::vector > MWR_times; - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - }; - - static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] /// Returns the information of the trigger in the current event. TriggerInfo_t extractTriggerInfo(art::Event const& e) const; - - /** - * @brief Determines spill times and extracts data based on multiwire devices. - * @param triggerInfo information from the trigger of this event - * @return times and unpacked data, per device (`"M875BB"`, `"M876BB"`, `"MMBTBB"`) - */ - MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo) const; - /** * @brief Matches spill times with multiwire chamber data from the database. * @param eventID ID of the event the information is associated to @@ -180,17 +143,6 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { std::vector< sbn::BNBSpillInfo >& beamInfos ) const; - /** - * @brief Assembles and returns a spill information record. - * @param eventID ID of the event the information is associated to - * @param time time of the spill - * @param MWRdata all extracted data from multiwire chambers - * @param matched_MWR data from multiwire chambers matched with the time - * @return a `sbn::BNBSpillInfo` object with information on the spill at `time` - */ - 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. @@ -323,14 +275,10 @@ void sbn::ICARUSBNBRetriever::produce(art::Event& e) // Keep track of the number of beam gates the DAQ thinks // are in this job TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - - - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo); - + MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); - if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) mf::LogDebug("ICARUSBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; else @@ -339,7 +287,7 @@ void sbn::ICARUSBNBRetriever::produce(art::Event& e) }//end iteration over art::Events -sbn::ICARUSBNBRetriever::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& e) const { +sbn::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& e) const { //Here we read in the artdaq Fragments and extract three pieces of information: // 1. The time of the current event, t_current_event @@ -386,123 +334,6 @@ sbn::ICARUSBNBRetriever::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerIn return triggerInfo; } - -sbn::ICARUSBNBRetriever::MWRdata_t sbn::ICARUSBNBRetriever::extractSpillTimes(TriggerInfo_t const& triggerInfo) const { - - // These lines get everything primed within the IFBeamDB - // They seem redundant but they are needed - try{bfp->FillCache((triggerInfo.t_previous_event)-fTimePad);} catch (WebAPIException &we) {} - try{bfp_mwr->FillCache((triggerInfo.t_previous_event)-fTimePad);} 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 - // to correctly unpack it - std::vector< std::vector< std::vector< int > > > unpacked_MWR; - std::vector< std::vector< double> > MWR_times; - unpacked_MWR.resize(3); - MWR_times.resize(3); - std::string packed_data_str; - - //Create a list of all the MWR devices with their different - // memory buffer increments - // generally in the format: "E:.{Memory Block}" - std::vector vars = bfp_mwr->GetDeviceList(); - mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR Device Blocks Found : " << vars.size() << std::endl; - // Tracking the time from the IFBeamDB - double time_for_mwr; - - // this is an iterator to track which of the - // three devices we will be working with - int dev = 0; - - // The MWR devices are annoying and have confusing buffer - // what we'll do is sort through all of them first and then - // match them to the closest spills in time - // - - // int t_steps = int(((triggerInfo.t_previous_event - fTimePad) - (triggerInfo.t_current_event + fTimePad))/0.5)+25; - int t_steps = int(((triggerInfo.t_current_event + fTimePad) - (triggerInfo.t_previous_event - fTimePad - 20.))/0.5)+25; - mf::LogDebug("ICARUSBNBRetriever") << " t_steps " << t_steps << std::endl; - - for(int t = 0; t < t_steps; t++){//Iterate through time increments - for (std::string const& var : vars) {// Iterate through the devices - - //Make sure we have a device - if(var.empty()){ - //mf::LogDebug("ICARUSBNBRetriever") << " NO MWR DEVICES?!" << std::endl; - continue; - } - /// Check the device name and interate the double-vector index - if(var.find("M875BB") != std::string::npos ) dev = 0; - else if(var.find("M876BB") != std::string::npos ) dev = 1; - else if(var.find("MMBTBB") != std::string::npos ) dev = 2; - else{ - //mf::LogDebug("ICARUSBNBRetriever") << " NOT matched to a MWR DEVICES?!" << var << std::endl; - continue;} - - time_for_mwr = 0; - - try{ - //Pull the MWR data for the device - // these data are "packed" - std::vector packed_MWR = bfp_mwr->GetNamedVector((triggerInfo.t_previous_event)-20.-fTimePad+double(0.5*t),var,&time_for_mwr); - - //We'll convert this into a format - // that we can unpack doubles >> strings - // - packed_data_str.clear(); - packed_data_str += std::to_string(int(time_for_mwr)); - packed_data_str.append(","); - packed_data_str.append(var); - packed_data_str.append(",,"); - - /* for(auto const value: packed_MWR){ - packed_data_str += ','; - packed_data_str += std::to_string(int(value)); - }*/ - for(int j = 0; j < int(packed_MWR.size()); j++){ - packed_data_str += std::to_string(int(packed_MWR[j])); - if(j < int(packed_MWR.size())-1) - packed_data_str.append(","); - } - - // Use Zarko's unpacking function to turn this into consumeable data - std::vector MWR_times_temp; - - // There is a 35 ms offset between the toriod and the MWR times - // we'll just remove that here to match to the spill times - std::vector< std::vector< int > > unpacked_MWR_temp = mwrdata.unpackMWR(packed_data_str,MWR_times_temp,MWRtoroidDelay); - - //There are four events that are packed into one MWR IFBeam entry - for(std::size_t s: util::counter(unpacked_MWR_temp.size())){ - - // If this entry has a unique time them store it for later - if(std::find(MWR_times[dev].begin(), MWR_times[dev].end(), MWR_times_temp[s]) == MWR_times[dev].end()){ - unpacked_MWR[dev].push_back(unpacked_MWR_temp[s]); - MWR_times[dev].push_back(MWR_times_temp[s]); - }//check for unique time - }//Iterate through the unpacked events - }//try - catch (WebAPIException &we) { - //Ignore when we can't find the MWR devices - // they don't always report and the timing of them can be annoying - - }//catch - }// Iterate over all the multiwire devices - }// Iterate over all times - - mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[0] times : " << MWR_times[0].size() << std::endl; - mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[0]s : " << unpacked_MWR[0].size() << std::endl; - mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[1] times : " << MWR_times[1].size() << std::endl; - mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[1]s : " << unpacked_MWR[1].size() << std::endl; - mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[2] times : " << MWR_times[2].size() << std::endl; - mf::LogDebug("ICARUSBNBRetriever") << " Number of MWR[2]s : " << unpacked_MWR[2].size() << std::endl; - - return { std::move(MWR_times), std::move(unpacked_MWR) }; -} - - int sbn::ICARUSBNBRetriever::matchMultiWireData( art::EventID const& eventID, TriggerInfo_t const& triggerInfo, @@ -636,7 +467,7 @@ int sbn::ICARUSBNBRetriever::matchMultiWireData( }//end loop over MWR devices - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR); + sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp); beamInfos.push_back(std::move(spillInfo)); @@ -651,113 +482,6 @@ int sbn::ICARUSBNBRetriever::matchMultiWireData( return spill_count; } - -sbn::BNBSpillInfo sbn::ICARUSBNBRetriever::makeBNBSpillInfo - (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR) const -{ - - auto const& [ MWR_times, unpacked_MWR ] = MWRdata; // alias - - // initializing all of our device carriers - // device definitions can be found in BNBSpillInfo.h - - double TOR860 = 0; // units e12 protons - double TOR875 = 0; // units e12 protons - double LM875A = 0; // units R/s - double LM875B = 0; // units R/s - double LM875C = 0; // units R/s - double HP875 = 0; // units mm - double VP875 = 0; // units mm - double HPTG1 = 0; // units mm - double VPTG1 = 0; // units mm - double HPTG2 = 0; // units mm - double VPTG2 = 0; // units mm - double BTJT2 = 0; // units Deg C - double THCURR = 0; // units kiloAmps - - double TOR860_time = 0; // units s - - // Here we request all the devices - // since sometimes devices fail to report we'll - // allow each to throw an exception but still move forward - // interpreting these failures will be part of the beam quality analyses - try{bfp->GetNamedData(time, "E:TOR860@",&TOR860,&TOR860_time);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:TOR875",&TOR875);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875A",&LM875A);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875B",&LM875B);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:LM875C",&LM875C);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HP875",&HP875);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VP875",&VP875);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG1",&HPTG1);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG1",&VPTG1);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:HPTG2",&HPTG2);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:VPTG2",&VPTG2);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:BTJT2",&BTJT2);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - try{bfp->GetNamedData(time, "E:THCURR",&THCURR);}catch (WebAPIException &we) {mf::LogDebug("ICARUSBNBRetriever")<< "At time : " << time << " " << "got exception: " << we.what() << "\n";} - - //crunch the times - unsigned long int time_closest_int = (int) TOR860_time; - double time_closest_ns = (TOR860_time - time_closest_int)*1e9; - - //Store everything in our data-product - sbn::BNBSpillInfo beamInfo; - beamInfo.TOR860 = TOR860*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.TOR875 = TOR875*1e12; //add in factor of 1e12 protons to get correct POT units - beamInfo.LM875A = LM875A; - beamInfo.LM875B = LM875B; - beamInfo.LM875C = LM875C; - beamInfo.HP875 = HP875; - beamInfo.VP875 = VP875; - beamInfo.HPTG1 = HPTG1; - beamInfo.VPTG1 = VPTG1; - beamInfo.HPTG2 = HPTG2; - beamInfo.VPTG2 = VPTG2; - beamInfo.BTJT2 = BTJT2; - beamInfo.THCURR = THCURR; - beamInfo.spill_time_s = time_closest_int; - beamInfo.spill_time_ns = time_closest_ns; - - for(auto const& MWRdata: unpacked_MWR){ - std::ignore = MWRdata; - assert(!MWRdata.empty()); - } - - if(unpacked_MWR[0].empty()){ - beamInfo.M875BB.clear(); - beamInfo.M875BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M875BB = unpacked_MWR[0][matched_MWR[0]]; - beamInfo.M875BB_spill_time_diff = (MWR_times[0][matched_MWR[0]] - time); - } - - if(unpacked_MWR[1].empty()){ - beamInfo.M876BB.clear(); - beamInfo.M876BB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.M876BB = unpacked_MWR[1][matched_MWR[1]]; - beamInfo.M876BB_spill_time_diff = (MWR_times[1][matched_MWR[1]] - time); - } - - if(unpacked_MWR[2].empty()){ - beamInfo.MMBTBB.clear(); - beamInfo.MMBTBB_spill_time_diff = -999;//units in seconds - } - else{ - beamInfo.MMBTBB = unpacked_MWR[2][matched_MWR[2]]; - beamInfo.MMBTBB_spill_time_diff = (MWR_times[2][matched_MWR[2]] - time); - } - // 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 - - beamInfo.event = eventID.event(); // the rest of ID is known by art::SubRun - - return beamInfo; -} - - void sbn::ICARUSBNBRetriever::beginSubRun(art::SubRun& sr) { return; diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp deleted file mode 100644 index 48d0bda3f..000000000 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/MWRData.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "MWRData.h" - -using namespace std; - -namespace sbn{ - - std::vector< std::vector < int > > MWRData::unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset) const -{ - - std::vector > unpacked_data; - unpacked_data.resize(4); - short data[444]; - - std::vector row(0); - boost::split(row, packed_data, boost::is_any_of(",")); - if (row.size()==447) { - for (int i=3;i<447;i++) { - data[i-3]=atoi(row[i].c_str()); - } - string devname=row[1].substr(0,8); - for (int idev=0;idev<4;idev++) { - mwrpulse_t mwr=getMWRdata(data,idev); - time_stamp.push_back(mwr.sheader.timesec+mwr.sheader.timensec/1000000000.+timeoffset); - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.hor[ich]); - } - for (int ich=0;ich<48;ich++) { - unpacked_data[idev].push_back(mwr.ver[ich]); - } - } - } else { - cout <<"BeamSpillInfoRetriever: MRWData: Bad data!"< -namespace sbn{ -class MWRData -{ - typedef struct swicheader_t { - long timesec; - long timensec; - long gpstime1; - long gpstime2; - short boosterevent; - short mievent; - short hz15micnt; - long delta1f; - short pulsemi; - short pulsesc; - } swicheader_t; - - typedef struct mwrpulse_t { - short hor[48]; - short ver[48]; - swicheader_t sheader; - } mwrpulse_t; - - static long flipByte(long data) - { - return ((data>>16)&0x0000FFFF) | ((data<<16)&0xFFFF0000); - } - - mwrpulse_t getMWRdata(short* data, int nblock) const; - - public: - std::vector< std::vector < int > > unpackMWR(std::string packed_data, std::vector &time_stamp, double timeoffset=0) const; -}; -} - -#endif /* #ifndef _MWRDATA_H */ diff --git a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h b/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h index 46d16c709..250d9dfda 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h +++ b/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h @@ -22,9 +22,11 @@ namespace sbn{ } PTBInfo_t; typedef 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; + std::int64_t WR_to_Spill_conversion = 0; } TriggerInfo_t; typedef struct MWRdata_t { From bbb400facf141dc2d88e8f6e7dabb52ef7f96117 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Tue, 18 Mar 2025 13:39:52 -0500 Subject: [PATCH 23/26] rename POTTools --- sbncode/BeamSpillInfoRetriever/CMakeLists.txt | 4 +- .../ICARUSBNBRetriever/CMakeLists.txt | 2 +- .../ICARUSBNBRetriever_module.cc | 41 +++---------------- .../{SBNDPOTTools.cpp => POTTools.cpp} | 2 +- .../{SBNDPOTTools.h => POTTools.h} | 0 .../SBNDBNBEXTRetriever/CMakeLists.txt | 2 +- .../SBNDBNBEXTRetriever_module.cc | 3 +- .../SBNDBNBRetriever/CMakeLists.txt | 2 +- .../SBNDBNBRetriever_module.cc | 2 +- .../SBNDBNBZEROBIASRetriever/CMakeLists.txt | 2 +- .../SBNDBNBZEROBIASRetriever_module.cc | 2 +- 11 files changed, 15 insertions(+), 47 deletions(-) rename sbncode/BeamSpillInfoRetriever/{SBNDPOTTools.cpp => POTTools.cpp} (99%) rename sbncode/BeamSpillInfoRetriever/{SBNDPOTTools.h => POTTools.h} (100%) diff --git a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt index 56decf315..bb6237405 100644 --- a/sbncode/BeamSpillInfoRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/CMakeLists.txt @@ -27,8 +27,8 @@ art_make_library( sbn_MWRData larcorealg::CoreUtils - LIBRARY_NAME sbn_SBNDPOTTools - SOURCE SBNDPOTTools.cpp + LIBRARY_NAME sbn_POTTools + SOURCE POTTools.cpp ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt index a9379d867..ffa6afb95 100644 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/CMakeLists.txt @@ -3,7 +3,7 @@ find_package(ifdh_art) cet_build_plugin(ICARUSBNBRetriever art::module LIBRARIES - sbn_SBNDPOTTools + sbn_POTTools SQLite::SQLite3 ) diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc index 6be19dc1b..ccdac097f 100644 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc @@ -12,7 +12,7 @@ #include "art/Framework/Core/EDProducer.h" #include "messagefacility/MessageLogger/MessageLogger.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" -#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" #include "sbnobj/Common/Trigger/ExtraTriggerInfo.h" #include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerV3Fragment.hh" #include @@ -259,10 +259,6 @@ void sbn::ICARUSBNBRetriever::produce(art::Event& e) // We do not currently have the ability to figure out the first // spill that the DAQ was sensitive to, so don't try to save any // 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; @@ -277,7 +273,7 @@ void sbn::ICARUSBNBRetriever::produce(art::Event& e) TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); - int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, e.event() == 1, fOutbeamInfos); + int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, fOutbeamInfos); if(spill_count > int(triggerInfo.number_of_gates_since_previous_event)) mf::LogDebug("ICARUSBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << " \t \t ::: WRONG!"<< std::endl; @@ -357,37 +353,10 @@ int sbn::ICARUSBNBRetriever::matchMultiWireData( std::vector matched_MWR; matched_MWR.resize(3); - - // NOTE: for now, this is dead code because we don't - // do anything for the first event in a run. We may want to revisit - // this later to understand if there is a way we can do the POT - // accounting in the first event. - // - // Need to handle the first event in a run differently - if(isFirstEventInRun){ - - //We'll remove the spills after our event - int spills_after_our_target = 0; - // iterate through all the spills to find the - // spills that are after our triggered event - for (size_t i = 0; i < times_temps.size(); i++) { - if(times_temps[i] > (triggerInfo.t_current_event+fTimePad)){ - spills_after_our_target++; - } - }//end loop through spill times - - // Remove the spills after our trigger - times_temps.erase(times_temps.end()-spills_after_our_target,times_temps.end()); - - // 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 + ///reject time_stamps which have a trigger_type == 1 from data-base + //To-Do - // mf::LogDebug("ICARUSBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; + // mf::LogDebug("ICARUSBNBRetriever") << "Total number of Times we're going to test: " << times_temps.size() << std::endl; // mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Upper Limit : " << (triggerInfo.t_current_event)+fTimePad << std::endl; // mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Lower Limit : " << (triggerInfo.t_previous_event)+fTimePad << std::endl; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.cpp b/sbncode/BeamSpillInfoRetriever/POTTools.cpp similarity index 99% rename from sbncode/BeamSpillInfoRetriever/SBNDPOTTools.cpp rename to sbncode/BeamSpillInfoRetriever/POTTools.cpp index 9acb26ab2..10086ba03 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.cpp +++ b/sbncode/BeamSpillInfoRetriever/POTTools.cpp @@ -1,4 +1,4 @@ -#include "SBNDPOTTools.h" +#include "POTTools.h" using namespace std; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h b/sbncode/BeamSpillInfoRetriever/POTTools.h similarity index 100% rename from sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h rename to sbncode/BeamSpillInfoRetriever/POTTools.h diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt index d7f43205a..8a16ef2e9 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/CMakeLists.txt @@ -3,7 +3,7 @@ find_package(ifdh_art) cet_build_plugin(SBNDBNBEXTRetriever art::module LIBRARIES - sbn_SBNDPOTTools + sbn_POTTools ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc index 298734bff..b6f3e7827 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc @@ -8,7 +8,7 @@ #include "art/Framework/Core/EDProducer.h" #include "messagefacility/MessageLogger/MessageLogger.h" #include "sbnobj/Common/POTAccounting/EXTCountInfo.h" -#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" namespace sbn { class SBNDBNBEXTRetriever; @@ -28,7 +28,6 @@ class sbn::SBNDBNBEXTRetriever : public art::EDProducer { SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever const &) = delete; SBNDBNBEXTRetriever & operator = (SBNDBNBEXTRetriever &&) = delete; - private: // Declare member data here. std::vector< sbn::EXTCountInfo > fOutExtInfos; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt index ea89ae916..72a102476 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/CMakeLists.txt @@ -3,7 +3,7 @@ find_package(ifdh_art) cet_build_plugin(SBNDBNBRetriever art::module LIBRARIES - sbn_SBNDPOTTools + sbn_POTTools ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc index 5e709c2b0..0de226122 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -8,7 +8,7 @@ #include "art/Framework/Core/EDProducer.h" #include "messagefacility/MessageLogger/MessageLogger.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" -#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" namespace sbn { class SBNDBNBRetriever; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt index d1ef9e65f..8edd88618 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/CMakeLists.txt @@ -3,7 +3,7 @@ find_package(ifdh_art) cet_build_plugin(SBNDBNBZEROBIASRetriever art::module LIBRARIES - sbn_SBNDPOTTools + sbn_POTTools ) install_headers() diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc index fa7e8b793..399c036d0 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc @@ -8,7 +8,7 @@ #include "art/Framework/Core/EDProducer.h" #include "messagefacility/MessageLogger/MessageLogger.h" #include "sbnobj/Common/POTAccounting/BNBSpillInfo.h" -#include "sbncode/BeamSpillInfoRetriever/SBNDPOTTools.h" +#include "sbncode/BeamSpillInfoRetriever/POTTools.h" namespace sbn { class SBNDBNBZEROBIASRetriever; From 631dc9f385cf5c4527d5b5be129d50d199aa05f7 Mon Sep 17 00:00:00 2001 From: Nathaniel Rowe Date: Wed, 19 Mar 2025 14:38:01 -0500 Subject: [PATCH 24/26] matchMultiWire changes --- .../ICARUSBNBRetriever_module.cc | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc index ccdac097f..16d383fd0 100644 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc @@ -139,7 +139,7 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { int matchMultiWireData( art::EventID const& eventID, TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, + MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const; @@ -333,7 +333,7 @@ sbn::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& int sbn::ICARUSBNBRetriever::matchMultiWireData( art::EventID const& eventID, TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, bool isFirstEventInRun, + MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const { @@ -366,16 +366,14 @@ int sbn::ICARUSBNBRetriever::matchMultiWireData( // Only continue if these times are matched to our DAQ time //mf::LogDebug("ICARUSBNBRetriever") << std::setprecision(19) << "Time # : " << i << std::endl; - if(!isFirstEventInRun){//We already addressed the "first event" above - if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ - //mf::LogDebug("ICARUSBNBRetriever") << 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("ICARUSBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; - continue;} - } + if(times_temps[i] > (triggerInfo.t_current_event)+fTimePad){ + //mf::LogDebug("ICARUSBNBRetriever") << 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("ICARUSBNBRetriever") << std::setprecision(19) << "Removed! : " << times_temps[i] << std::endl; + continue;} //check if this spill is is minbias /* From f5403aa9205db75f577d003ed40d8b865e11cf7d Mon Sep 17 00:00:00 2001 From: nathanielerowe Date: Thu, 3 Apr 2025 10:24:18 -0500 Subject: [PATCH 25/26] Remove extra fcl --- .../job/sbndbnbspillinfo.fcl | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl diff --git a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl deleted file mode 100644 index 26327a100..000000000 --- a/sbncode/BeamSpillInfoRetriever/job/sbndbnbspillinfo.fcl +++ /dev/null @@ -1,17 +0,0 @@ -BEGIN_PROLOG - -sbndbnbspillinfo: { - module_type: "" - DebugLevel: 0 - TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away was 0.0333 - BESOffset: 0.035 #unit seconds, we expect the BES to be about 35 ms from $1D. Need to adjust search range accordingly. - URL: "" #Default URL having issues Nov 21 2024, used this instead https://dbdata3vm.fnal.gov:9443/ifbeam - Bundle: "BoosterNeutrinoBeam_read" - MultiWireBundle: "BNBMultiWire" - TimeWindow: "700" #seconds - MWR_TimeWindow: "3601" #seconds - DeviceUsedForTiming: "E:TOR860" -} - -END_PROLOG - From 1e60e40d29898d7472af84434a00ad2820ac8b1f Mon Sep 17 00:00:00 2001 From: nathanielerowe Date: Mon, 7 Apr 2025 13:33:01 -0500 Subject: [PATCH 26/26] Doxygen+namespace updates --- .../ICARUSBNBRetriever_module.cc | 31 +++++++------- sbncode/BeamSpillInfoRetriever/POTTools.cpp | 15 ++----- sbncode/BeamSpillInfoRetriever/POTTools.h | 40 ++++++++++++++++++- .../SBNDBNBEXTRetriever_module.cc | 4 +- .../SBNDBNBRetriever_module.cc | 24 +++++------ .../SBNDBNBZEROBIASRetriever_module.cc | 24 +++++------ .../job/icarusbnbspillinfo.fcl | 2 +- 7 files changed, 84 insertions(+), 56 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc index 16d383fd0..8802db658 100644 --- a/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/ICARUSBNBRetriever/ICARUSBNBRetriever_module.cc @@ -38,7 +38,6 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { fhicl::Atom BESOffset { Name{ "BESOffset" }, Comment{ "Offset between beam early warning signal and $1D [seconds]" }, - 0.036 // default }; fhicl::Atom RawDataLabel { @@ -58,27 +57,27 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { fhicl::Atom Bundle { Name{ "Bundle" }, - Comment{ "" } // explain what this is and which database/table it's looking for + Comment{ "Name of the collection of devices to grab from IFbeam for everything except MWRdevices." } }; fhicl::Atom TimeWindow { Name{ "TimeWindow" }, - Comment{ "" } // explain what this is, what's for and its unit + Comment{ "Window of time in seconds to use for non-mwr ifbeam queries." } }; fhicl::Atom MultiWireBundle { Name{ "MultiWireBundle" }, - Comment{ "" } // explain what this is and which database/table it's looking for + Comment{ "name of collections of devices to grab from IFbeam for use as MWRdevices." } }; fhicl::Atom MWR_TimeWindow { Name{ "MWR_TimeWindow" }, - Comment{ "" } // explain what this is, what's for and its unit + Comment{ "Window of time in seconds to use for mwr ifbeam queries." } }; fhicl::Atom TriggerDatabaseFile { Name{ "TriggerDatabaseFile" }, - Comment{ "" } // explain what this is, what's for and its unit + Comment{ "path of local database of all recorded events and their trigger, in SQLite format" } }; @@ -126,7 +125,7 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] /// Returns the information of the trigger in the current event. - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; /** * @brief Matches spill times with multiwire chamber data from the database. * @param eventID ID of the event the information is associated to @@ -138,8 +137,8 @@ class sbn::ICARUSBNBRetriever : public art::EDProducer { */ int matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const; @@ -264,14 +263,14 @@ void sbn::ICARUSBNBRetriever::produce(art::Event& e) run_number = e.id().run(); - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + sbn::pot::TriggerInfo_t const triggerInfo = extractTriggerInfo(e); //We only want to process BNB gates, i.e. type 1 if(triggerInfo.gate_type != 1) return; // Keep track of the number of beam gates the DAQ thinks // are in this job TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); + sbn::pot::MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, fOutbeamInfos); @@ -283,7 +282,7 @@ void sbn::ICARUSBNBRetriever::produce(art::Event& e) }//end iteration over art::Events -sbn::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& e) const { +sbn::pot::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& e) const { //Here we read in the artdaq Fragments and extract three pieces of information: // 1. The time of the current event, t_current_event @@ -293,7 +292,7 @@ sbn::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& auto const & raw_data = e.getProduct< std::vector >({ raw_data_label, "ICARUSTriggerV3" }); auto const & extraTrigInfo = e.getProduct< sbn::ExtraTriggerInfo >("daqTrigger"); - TriggerInfo_t triggerInfo; + sbn::pot::TriggerInfo_t triggerInfo; triggerInfo.WR_to_Spill_conversion = extraTrigInfo.WRtimeToTriggerTime; @@ -332,8 +331,8 @@ sbn::TriggerInfo_t sbn::ICARUSBNBRetriever::extractTriggerInfo(art::Event const& int sbn::ICARUSBNBRetriever::matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const { @@ -434,7 +433,7 @@ int sbn::ICARUSBNBRetriever::matchMultiWireData( }//end loop over MWR devices - sbn::BNBSpillInfo spillInfo = makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp); + sbn::BNBSpillInfo spillInfo = sbn::pot::makeBNBSpillInfo(eventID, times_temps[i], MWRdata, matched_MWR, bfp); beamInfos.push_back(std::move(spillInfo)); diff --git a/sbncode/BeamSpillInfoRetriever/POTTools.cpp b/sbncode/BeamSpillInfoRetriever/POTTools.cpp index 10086ba03..48751f675 100644 --- a/sbncode/BeamSpillInfoRetriever/POTTools.cpp +++ b/sbncode/BeamSpillInfoRetriever/POTTools.cpp @@ -2,11 +2,9 @@ using namespace std; -namespace sbn{ +namespace sbn::pot{ - // Function for extracting the PTB information (current timestamp, - // previous timestamp, gate counter). Used in SBND modules only. - sbn::PTBInfo_t extractPTBInfo(art::Handle > cont_frags, int HLT) { + sbn::pot::PTBInfo_t extractPTBInfo(art::Handle > cont_frags, int HLT) { bool foundHLT = false; PTBInfo_t PTBInfo; for (auto const& cont : *cont_frags) @@ -45,7 +43,6 @@ namespace sbn{ } } - // Function for extracting the TDC timestamp. Used in SBND modules only. double extractTDCTimeStamp(art::Handle > cont_frags) { double TDCTimeStamp = 0; @@ -62,8 +59,6 @@ namespace sbn{ return TDCTimeStamp; } - // Collect device information to make BNBSpillInfo object. Used in - // ICARUS and SBND modules. sbn::BNBSpillInfo makeBNBSpillInfo (art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR, std::unique_ptr const& bfp) { @@ -170,9 +165,6 @@ namespace sbn{ return beamInfo; } - // Returns true if a time in IFBeam corresponds to a broken clock false if - // it is a real spill. Check this by seeing if all devices fire at once or - // if time is just a single outlier. bool BrokenClock(double time, std::unique_ptr const& bfp) { double TOR860 = 0; // units e12 protons @@ -221,8 +213,7 @@ namespace sbn{ } } - // extract spill times from the IFBeam data base. Used in SBND and ICARUS. - sbn::MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo, std::unique_ptr const& bfp, std::unique_ptr const& bfp_mwr, double fTimePad, double MWRtoroidDelay, sbn::MWRData mwrdata) { + sbn::pot::MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo, std::unique_ptr const& bfp, std::unique_ptr const& bfp_mwr, double fTimePad, double MWRtoroidDelay, sbn::MWRData mwrdata) { // These lines get everything primed within the IFBeamDB. try{bfp->FillCache((triggerInfo.t_current_event)+fTimePad);} catch (WebAPIException &we) {}; diff --git a/sbncode/BeamSpillInfoRetriever/POTTools.h b/sbncode/BeamSpillInfoRetriever/POTTools.h index 250d9dfda..43bb6b68a 100644 --- a/sbncode/BeamSpillInfoRetriever/POTTools.h +++ b/sbncode/BeamSpillInfoRetriever/POTTools.h @@ -14,7 +14,8 @@ #include "sbncode/BeamSpillInfoRetriever/MWRData.h" #include "larcorealg/CoreUtils/counter.h" -namespace sbn{ +namespace sbn::pot{ + typedef struct PTBInfo_t { double currPTBTimeStamp = 1e20; double prevPTBTimeStamp = 0; @@ -34,10 +35,47 @@ namespace sbn{ std::vector< std::vector< std::vector< int > > > unpacked_MWR; } MWRdata_t; + /** + * @brief Extracts information from PTB for use in SBND POT accounting. + * + * @param cont_frags The PTB fragments to examine. + * @param HLT The high level trigger we are searching for. + */ PTBInfo_t extractPTBInfo(art::Handle > cont_frags, int HLT); + + /** + * @brief Extracts information from TDC for use in SBND POT accounting. + * + * @param cont_frags The TDC fragments to examine. + */ double extractTDCTimeStamp(art::Handle > cont_frags); + + /** + * @brief Checks for a known IFBeam issue where TOR clocks are incorrect. + * @return true if a time in IFBeam corresponds to a broken clock false if + * it is a real spill. + * @details Check validity by seeing if all devices fire at once or + * if time is just a single outlier. + * @param time The time for which we want to check for a spill. + * @param bfp beamfolder object to check. + */ bool BrokenClock(double time, std::unique_ptr const& bfp); + + /** + * @brief extract spill times from the IFBeam data base. Used in SBND and ICARUS. + * + * @param triggerInfo triggerInfo object to match + * @param bfp beamfolder to match + * @param bfp_mwr mwr devices have separate daq system, much treat separately + * @param fTimePad Padding to use in ifbeam queries + * @param MWRtoroidDelay Delay between MWR and toroids + * @param mwrdata MWRData to unpack + */ MWRdata_t extractSpillTimes(TriggerInfo_t const& triggerInfo, std::unique_ptr const& bfp, std::unique_ptr const& bfp_mwr, double fTimePad, double MWRtoroidDelay, sbn::MWRData mwrdata ); + + /** + * @brief Compile spill information into BNBSpillInfo object + */ sbn::BNBSpillInfo makeBNBSpillInfo(art::EventID const& eventID, double time, MWRdata_t const& MWRdata, std::vector const& matched_MWR, std::unique_ptr const& bfp); } diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc index b6f3e7827..a8b3f9d67 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBEXTRetriever/SBNDBNBEXTRetriever_module.cc @@ -31,7 +31,7 @@ class sbn::SBNDBNBEXTRetriever : public art::EDProducer { private: // Declare member data here. std::vector< sbn::EXTCountInfo > fOutExtInfos; - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; // input labels float TotalEXTCounts; }; @@ -46,7 +46,7 @@ void sbn::SBNDBNBEXTRetriever::produce(art::Event & e) { art::InputTag PTB_itag("daq", "ContainerPTB"); auto PTB_cont_frags = e.getHandle(PTB_itag); - PTBInfo_t PTBInfo = extractPTBInfo(PTB_cont_frags, 4); + sbn::pot::PTBInfo_t PTBInfo = sbn::pot::extractPTBInfo(PTB_cont_frags, 4); int SingleEventGateCounter = PTBInfo.GateCounter; TotalEXTCounts += SingleEventGateCounter; diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc index 0de226122..db0c44c21 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBRetriever/SBNDBNBRetriever_module.cc @@ -40,11 +40,11 @@ class sbn::SBNDBNBRetriever : public art::EDProducer { art::ServiceHandle ifbeam_handle; static constexpr double MWRtoroidDelay = -0.035; ///< the same time point is measured _t_ by MWR and _t + MWRtoroidDelay`_ by the toroid [ms] - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; int matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const; unsigned int TotalBeamSpills; @@ -77,14 +77,14 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) } mf::LogDebug("SBNDBNBRetriever")<< "ptb_event: " << e.event() << std::endl; - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + sbn::pot::TriggerInfo_t const triggerInfo = extractTriggerInfo(e); if (triggerInfo.t_previous_event == 0) { return; } TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); + sbn::pot::MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); int const spill_count = matchMultiWireData(e.id(), triggerInfo, MWRdata, fOutbeamInfos); @@ -94,7 +94,7 @@ void sbn::SBNDBNBRetriever::produce(art::Event & e) mf::LogDebug("SBNDBNBRetriever")<< "Event Spills : " << spill_count << ", DAQ Spills : " << triggerInfo.number_of_gates_since_previous_event << std::endl; } -sbn::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { +sbn::pot::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e) const { // Using TDC for current event, but PTB for previous event. Exception for case where no TDC. art::InputTag PTB_itag("daq", "ContainerPTB"); auto PTB_cont_frags = e.getHandle(PTB_itag); @@ -102,11 +102,11 @@ sbn::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); auto TDC_cont_frags = e.getHandle(TDC_itag); - TriggerInfo_t triggerInfo; - PTBInfo_t PTBInfo = extractPTBInfo(PTB_cont_frags, 2); + sbn::pot::TriggerInfo_t triggerInfo; + sbn::pot::PTBInfo_t PTBInfo = sbn::pot::extractPTBInfo(PTB_cont_frags, 2); if (TDC_cont_frags) { - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + double TDCTimeStamp = sbn::pot::extractTDCTimeStamp(TDC_cont_frags); triggerInfo.t_current_event = TDCTimeStamp - fBESOffset; } else{ @@ -134,8 +134,8 @@ sbn::TriggerInfo_t sbn::SBNDBNBRetriever::extractTriggerInfo(art::Event const& e int sbn::SBNDBNBRetriever::matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const { @@ -164,7 +164,7 @@ int sbn::SBNDBNBRetriever::matchMultiWireData( spills_removed++; continue;} - if(BrokenClock(times_temps[i], bfp)){ + if(sbn::pot::BrokenClock(times_temps[i], bfp)){ continue; } diff --git a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc index 399c036d0..3f6fc7eb8 100644 --- a/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/SBNDBNBZEROBIASRetriever/SBNDBNBZEROBIASRetriever_module.cc @@ -41,11 +41,11 @@ class sbn::SBNDBNBZEROBIASRetriever : public art::EDProducer { std::vector< sbn::BNBSpillInfo > fOutbeamInfos; std::vector< sbn::BNBSpillInfo > fOutbeamInfosTotal; - TriggerInfo_t extractTriggerInfo(art::Event const& e) const; + sbn::pot::TriggerInfo_t extractTriggerInfo(art::Event const& e) const; void matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const; unsigned int TotalBeamSpills; @@ -79,7 +79,7 @@ void sbn::SBNDBNBZEROBIASRetriever::produce(art::Event & e) return; } - TriggerInfo_t const triggerInfo = extractTriggerInfo(e); + sbn::pot::TriggerInfo_t const triggerInfo = extractTriggerInfo(e); if (triggerInfo.t_previous_event == 0) { auto p = std::make_unique< std::vector< sbn::BNBSpillInfo > >(); @@ -89,7 +89,7 @@ void sbn::SBNDBNBZEROBIASRetriever::produce(art::Event & e) } TotalBeamSpills += triggerInfo.number_of_gates_since_previous_event; - MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); + sbn::pot::MWRdata_t const MWRdata = extractSpillTimes(triggerInfo, bfp, bfp_mwr, fTimePad, MWRtoroidDelay, mwrdata); matchMultiWireData(e.id(), triggerInfo, MWRdata, fOutbeamInfos); fOutbeamInfosTotal.insert(fOutbeamInfosTotal.end(),fOutbeamInfos.begin(),fOutbeamInfos.end()); @@ -98,7 +98,7 @@ void sbn::SBNDBNBZEROBIASRetriever::produce(art::Event & e) e.put(std::move(p)); } -sbn::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event const& e) const { +sbn::pot::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event const& e) const { // Using TDC for current event, but PTB for previous event art::InputTag PTB_itag("daq", "ContainerPTB"); auto PTB_cont_frags = e.getHandle(PTB_itag); @@ -106,11 +106,11 @@ sbn::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event art::InputTag TDC_itag("daq", "ContainerTDCTIMESTAMP"); auto TDC_cont_frags = e.getHandle(TDC_itag); - TriggerInfo_t triggerInfo; - PTBInfo_t PTBInfo = extractPTBInfo(PTB_cont_frags, 1); + sbn::pot::TriggerInfo_t triggerInfo; + sbn::pot::PTBInfo_t PTBInfo = sbn::pot::extractPTBInfo(PTB_cont_frags, 1); if (TDC_cont_frags) { - double TDCTimeStamp = extractTDCTimeStamp(TDC_cont_frags); + double TDCTimeStamp = sbn::pot::extractTDCTimeStamp(TDC_cont_frags); triggerInfo.t_current_event = TDCTimeStamp - fBESOffset; } else{ @@ -138,8 +138,8 @@ sbn::TriggerInfo_t sbn::SBNDBNBZEROBIASRetriever::extractTriggerInfo(art::Event void sbn::SBNDBNBZEROBIASRetriever::matchMultiWireData( art::EventID const& eventID, - TriggerInfo_t const& triggerInfo, - MWRdata_t const& MWRdata, + sbn::pot::TriggerInfo_t const& triggerInfo, + sbn::pot::MWRdata_t const& MWRdata, std::vector< sbn::BNBSpillInfo >& beamInfos ) const { @@ -174,7 +174,7 @@ void sbn::SBNDBNBZEROBIASRetriever::matchMultiWireData( spills_removed++; continue;} - if(BrokenClock(times_temps[i], bfp)){ + if(sbn::pot::BrokenClock(times_temps[i], bfp)){ continue; } diff --git a/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl b/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl index a7b4b0dbc..7c33421f8 100644 --- a/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl +++ b/sbncode/BeamSpillInfoRetriever/job/icarusbnbspillinfo.fcl @@ -5,7 +5,7 @@ icarusbnbspillinfo: { module_type: "ICARUSBNBRetriever" TimePadding: 0.0333 #unit seconds, Booster Rep Rate is 15 Hz, so the closest spill could be 66ms away - BESOffset: 0.036 #unit seconds, we expect the BES to be about 35 ms from $1D. Need to adjust search range accordingly. + BESOffset: 0.035746 #unit seconds, we expect the BES to be about 35 ms from $1D. Need to adjust search range accordingly. URL: "" #keep this blank and we're good Bundle: "BoosterNeutrinoBeam_read" MultiWireBundle: "BNBMultiWire"