diff --git a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc index a0eb1e48a..ba3396c99 100644 --- a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc @@ -109,6 +109,13 @@ void sbn::NuMIRetriever::produce(art::Event &e) e.getByLabel(raw_data_label_, "ICARUSTriggerV3", raw_data_ptr); auto const & raw_data = (*raw_data_ptr); + // NOTE: Really we should skip the first event of each trigger type, so let's make this look at that too... + if ( raw_data.empty() ) return; + else { + icarus::ICARUSTriggerV3Fragment frag(raw_data.at(0)); + if ( frag.getTotalTriggerNuMIMaj() <= 1 ) return; + } + double t_current_event = 0; double t_previous_event = 0; double number_of_gates_since_previous_event = 0; @@ -163,8 +170,8 @@ void sbn::NuMIRetriever::produce(art::Event &e) // plus or minus some time padding, currently using 3.3 ms // which is half the Booster Rep Rate if(e.event() != 1){//We already addressed the "first event" above - if(times_temps[i] > t_current_event){continue;} - if(times_temps[i] <= t_previous_event){continue;} + if(times_temps[i] > t_current_event+fTimePad){continue;} + if(times_temps[i] <= t_previous_event+fTimePad){continue;} } //count found spills diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index e6efee59d..7744547b2 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -150,6 +150,13 @@ namespace sbn{ namespace caf { +/// Function to calculate a timestamp from the spill info product +template +double spillInfoToTimestamp(SpillInfo const& info) { + return static_cast(info.spill_time_s) + + static_cast(info.spill_time_ns)*1.0e-9; +} + /// Module to create Common Analysis Files from ART files class CAFMaker : public art::EDProducer { public: @@ -195,6 +202,10 @@ class CAFMaker : public art::EDProducer { double fPrescaleEvents; std::vector fBNBInfo; ///< Store detailed BNB info to save into the first StandardRecord of the output file std::vector fNuMIInfo; ///< Store detailed NuMI info to save into the first StandardRecord of the output file + std::map fBNBInfoEventMap; ///< Store detailed BNB info to save for the particular spills of events + std::map fNuMIInfoEventMap; ///< Store detailed NuMI info to save for the particular spills of events + bool fHasBNBInfo; + bool fHasNuMIInfo; // int fCycle; // int fBatch; @@ -747,15 +758,43 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { // get POT information fBNBInfo.clear(); fNuMIInfo.clear(); + + fBNBInfoEventMap.clear(); + fNuMIInfoEventMap.clear(); + fHasBNBInfo = false; + fHasNuMIInfo = false; + fSubRunPOT = 0; if(auto bnb_spill = sr.getHandle>(fParams.BNBPOTDataLabel())){ FillExposure(*bnb_spill, fBNBInfo, fSubRunPOT); fTotalPOT += fSubRunPOT; + + // Find the spill for each event and fill the event map: + // We take the latest spill for a given event number to be the one to keep + fHasBNBInfo = true; + for(const sbn::BNBSpillInfo& info: *bnb_spill) + { + auto& storedInfo = fBNBInfoEventMap[info.event]; // creates if needed + if ( (storedInfo.event == UINT_MAX) || spillInfoToTimestamp(info) > spillInfoToTimestamp(storedInfo) ) { + storedInfo = std::move(info); + } + } } else if (auto numi_spill = sr.getHandle>(fParams.NuMIPOTDataLabel())) { FillExposureNuMI(*numi_spill, fNuMIInfo, fSubRunPOT); fTotalPOT += fSubRunPOT; + + // Find the spill for each event and fill the event map: + // We take the latest spill for a given event number to be the one to keep + fHasNuMIInfo = true; + for(const sbn::NuMISpillInfo& info: *numi_spill) + { + auto& storedInfo = fNuMIInfoEventMap[info.event]; // creates if needed + if ( (storedInfo.event == UINT_MAX) || spillInfoToTimestamp(info) > spillInfoToTimestamp(storedInfo) ) { + storedInfo = std::move(info); + } + } } else if(auto pot_handle = sr.getHandle(fParams.GenLabel())){ fSubRunPOT = pot_handle->totgoodpot; @@ -1956,6 +1995,21 @@ void CAFMaker::produce(art::Event& evt) noexcept { // rec.hdr.blind = 0; // rec.hdr.filt = rb::IsFiltered(evt, slices, sliceID); + // Fill the header info for the given event's spill quality info + if ( fHasBNBInfo && fHasNuMIInfo ) { + std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Throwing..." << std::endl; + abort(); + } + unsigned int const eventNo = evt.id().event(); + if ( fBNBInfoEventMap.count(eventNo) > 0 ) { + rec.hdr.spillbnbinfo = makeSRBNBInfo(fBNBInfoEventMap.at(eventNo)); + } + else if ( fNuMIInfoEventMap.count(eventNo) > 0 ) { + rec.hdr.spillnumiinfo = makeSRNuMIInfo(fNuMIInfoEventMap.at(eventNo)); + } + else { + std::cout << "Did not find this event in the spill info map." << std::endl; + } if(fRecTree){ // Save the standard-record diff --git a/sbncode/CAFMaker/FillExposure.cxx b/sbncode/CAFMaker/FillExposure.cxx index 544be125e..01dd11b80 100644 --- a/sbncode/CAFMaker/FillExposure.cxx +++ b/sbncode/CAFMaker/FillExposure.cxx @@ -72,4 +72,34 @@ namespace caf NuMIInfo.back().daq_gates = info.daq_gates; } } + + caf::SRNuMIInfo makeSRNuMIInfo(sbn::NuMISpillInfo const& info) + { + caf::SRNuMIInfo single_store; + + single_store.HP121 = info.HP121; + single_store.VP121 = info.VP121; + single_store.HPTGT = info.HPTGT; + single_store.VPTGT = info.VPTGT; + single_store.HITGT = info.HITGT; + single_store.VITGT = info.VITGT; + single_store.MTGTDS = info.MTGTDS; + single_store.HRNDIR = info.HRNDIR; + single_store.NSLINA = info.NSLINA; + single_store.NSLINB = info.NSLINB; + single_store.NSLINC = info.NSLINC; + single_store.NSLIND = info.NSLIND; + single_store.TRTGTD = info.TRTGTD; + single_store.TR101D = info.TR101D; + single_store.TORTGT = info.TORTGT; + single_store.TOR101 = info.TOR101; + single_store.time = info.time; + single_store.spill_time_s = info.spill_time_s; + single_store.spill_time_ns = info.spill_time_ns; + single_store.event = info.event; + single_store.daq_gates = info.daq_gates; + + return single_store; + } + } diff --git a/sbncode/CAFMaker/FillExposure.h b/sbncode/CAFMaker/FillExposure.h index 69df72ab8..ec8def3d2 100644 --- a/sbncode/CAFMaker/FillExposure.h +++ b/sbncode/CAFMaker/FillExposure.h @@ -20,6 +20,7 @@ namespace caf std::vector& NuMIInfo, double& subRunPOT); + caf::SRNuMIInfo makeSRNuMIInfo(sbn::NuMISpillInfo const& info); }