From 7c887d70a14ac1918bc4bed24f1e681e61c8aba0 Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Fri, 8 Sep 2023 17:56:55 -0500 Subject: [PATCH 1/5] Add individual BNBInfo and NuMIInfo to SRHeader to enable saving of the spill quality for the spill that goes with a given event, and code supporting that. --- sbncode/CAFMaker/CAFMaker_module.cc | 52 +++++++++++++++++++++++++++++ sbncode/CAFMaker/FillExposure.cxx | 30 +++++++++++++++++ sbncode/CAFMaker/FillExposure.h | 1 + 3 files changed, 83 insertions(+) diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index e7704b2c7..6c2096ea1 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -195,6 +195,10 @@ class CAFMaker : public art::EDProducer { double fPrescaleEvents; std::vector fBNBInfo; ///< Store detailed BNB info to save into the first StandardRecord of the output file std::vector fNuMIInfo; ///< Store detailed NuMI info to save into the first StandardRecord of the output file + std::map fBNBInfoEventMap; ///< Store detailed BNB info to save for the particular spills of events + std::map fNuMIInfoEventMap; ///< Store detailed NuMI info to save for the particular spills of events + bool fHasBNBInfo; + bool fHasNuMIInfo; // int fCycle; // int fBatch; @@ -745,6 +749,12 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { // get POT information fBNBInfo.clear(); fNuMIInfo.clear(); + + fBNBInfoEventMap.clear(); + fNuMIInfoEventMap.clear(); + fHasBNBInfo = false; + fHasNuMIInfo = false; + fSubRunPOT = 0; fOffbeamBNBGates = 0; fOffbeamNuMIGates = 0; @@ -769,10 +779,38 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { if(bnb_spill){ FillExposure(*bnb_spill, fBNBInfo, fSubRunPOT); fTotalPOT += fSubRunPOT; + + // Find the spill for each event and fill the event map: + // We take the latest spill for a given event number to be the one to keep + fHasBNBInfo = true; + for(const sbn::BNBSpillInfo& info: *bnb_spill) + { + if ( fBNBInfoEventMap.find(info.event)==fBNBInfoEventMap.end() ) { + fBNBInfoEventMap[info.event] = info; + } + else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > + (fBNBInfoEventMap[info.event].spill_time_s+(fBNBInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { + fBNBInfoEventMap[info.event] = info; + } + } } else if (numi_spill) { FillExposureNuMI(*numi_spill, fNuMIInfo, fSubRunPOT); fTotalPOT += fSubRunPOT; + + // Find the spill for each event and fill the event map: + // We take the latest spill for a given event number to be the one to keep + fHasNuMIInfo = true; + for(const sbn::NuMISpillInfo& info: *numi_spill) + { + if ( fNuMIInfoEventMap.find(info.event)==fNuMIInfoEventMap.end() ) { + fNuMIInfoEventMap[info.event] = info; + } + else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > + (fNuMIInfoEventMap[info.event].spill_time_s+(fNuMIInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { + fNuMIInfoEventMap[info.event] = info; + } + } } else if (bnb_offbeam_spill){ for(const auto& spill: *bnb_offbeam_spill) { @@ -1984,6 +2022,20 @@ void CAFMaker::produce(art::Event& evt) noexcept { // rec.hdr.blind = 0; // rec.hdr.filt = rb::IsFiltered(evt, slices, sliceID); + // Fill the header info for the given event's spill quality info + if ( fHasBNBInfo && fHasNuMIInfo ) { + std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Will not fill event-specific spill quality info for this event..." << std::endl; + } + else if ( fHasBNBInfo ) { + if ( fBNBInfoEventMap.find(evt.id().event()) == fBNBInfoEventMap.end() ) + std::cout << "We think (since fHasBNBInfo) that this event should be BNB, but did not find this event in the spill info map." << std::endl; + else rec.hdr.spillbnbinfo = makeSRBNBInfo(fBNBInfoEventMap.at(evt.id().event())); + } + else if ( fHasNuMIInfo ) { + if ( fNuMIInfoEventMap.find(evt.id().event()) == fNuMIInfoEventMap.end() ) + std::cout << "We think (since fHasNuMIInfo) that this event should be NuMI, but did not find this event in the spill info map." << std::endl; + else rec.hdr.spillnumiinfo = makeSRNuMIInfo(fNuMIInfoEventMap.at(evt.id().event())); + } if(fRecTree){ // Save the standard-record diff --git a/sbncode/CAFMaker/FillExposure.cxx b/sbncode/CAFMaker/FillExposure.cxx index 544be125e..01dd11b80 100644 --- a/sbncode/CAFMaker/FillExposure.cxx +++ b/sbncode/CAFMaker/FillExposure.cxx @@ -72,4 +72,34 @@ namespace caf NuMIInfo.back().daq_gates = info.daq_gates; } } + + caf::SRNuMIInfo makeSRNuMIInfo(sbn::NuMISpillInfo const& info) + { + caf::SRNuMIInfo single_store; + + single_store.HP121 = info.HP121; + single_store.VP121 = info.VP121; + single_store.HPTGT = info.HPTGT; + single_store.VPTGT = info.VPTGT; + single_store.HITGT = info.HITGT; + single_store.VITGT = info.VITGT; + single_store.MTGTDS = info.MTGTDS; + single_store.HRNDIR = info.HRNDIR; + single_store.NSLINA = info.NSLINA; + single_store.NSLINB = info.NSLINB; + single_store.NSLINC = info.NSLINC; + single_store.NSLIND = info.NSLIND; + single_store.TRTGTD = info.TRTGTD; + single_store.TR101D = info.TR101D; + single_store.TORTGT = info.TORTGT; + single_store.TOR101 = info.TOR101; + single_store.time = info.time; + single_store.spill_time_s = info.spill_time_s; + single_store.spill_time_ns = info.spill_time_ns; + single_store.event = info.event; + single_store.daq_gates = info.daq_gates; + + return single_store; + } + } diff --git a/sbncode/CAFMaker/FillExposure.h b/sbncode/CAFMaker/FillExposure.h index 69df72ab8..ec8def3d2 100644 --- a/sbncode/CAFMaker/FillExposure.h +++ b/sbncode/CAFMaker/FillExposure.h @@ -20,6 +20,7 @@ namespace caf std::vector& NuMIInfo, double& subRunPOT); + caf::SRNuMIInfo makeSRNuMIInfo(sbn::NuMISpillInfo const& info); } From ed71ae6134bd607e152ae1d81fd612f59505354b Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Tue, 12 Sep 2023 17:38:28 -0500 Subject: [PATCH 2/5] Fix a few issues - one adds a check on PadTime before deciding if it wants to use this spill. The other checks not just for the first event but the first event OF A GIVEN STREAM for the numi case. --- .../NuMIRetriever/NuMIRetriever_module.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc index a0eb1e48a..432a6f0de 100644 --- a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc @@ -109,6 +109,13 @@ void sbn::NuMIRetriever::produce(art::Event &e) e.getByLabel(raw_data_label_, "ICARUSTriggerV3", raw_data_ptr); auto const & raw_data = (*raw_data_ptr); + // NOTE: Really we should skip the first event of each trigger type, so let's make this look at that too... + if ( raw_data.size()==0 ) return; + else { + icarus::ICARUSTriggerV3Fragment frag(raw_data.at(0)); + if ( frag.getTotalTriggerNuMIMaj() <= 1 ) return; + } + double t_current_event = 0; double t_previous_event = 0; double number_of_gates_since_previous_event = 0; @@ -163,8 +170,8 @@ void sbn::NuMIRetriever::produce(art::Event &e) // plus or minus some time padding, currently using 3.3 ms // which is half the Booster Rep Rate if(e.event() != 1){//We already addressed the "first event" above - if(times_temps[i] > t_current_event){continue;} - if(times_temps[i] <= t_previous_event){continue;} + if(times_temps[i] > t_current_event+fTimePad){continue;} + if(times_temps[i] <= t_previous_event+fTimePad){continue;} } //count found spills From 97fa843b65bf342a5a91a9fac446d262b90cf1fc Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Tue, 14 Nov 2023 11:34:15 -0600 Subject: [PATCH 3/5] Updated code with some of Gianluca P's suggestions (need to test and add another). Thanks Gianluca --- sbncode/CAFMaker/CAFMaker_module.cc | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index 6c2096ea1..992cbc08a 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -148,6 +148,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: @@ -785,12 +792,9 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { fHasBNBInfo = true; for(const sbn::BNBSpillInfo& info: *bnb_spill) { - if ( fBNBInfoEventMap.find(info.event)==fBNBInfoEventMap.end() ) { - fBNBInfoEventMap[info.event] = info; - } - else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > - (fBNBInfoEventMap[info.event].spill_time_s+(fBNBInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { - fBNBInfoEventMap[info.event] = info; + auto& storedInfo = fBNBInfoEventMap[info.event]; // creates if needed + if ( (storedInfo.event == UINT_MAX) || spillInfoToTimestamp(info) > spillInfoToTimestamp(storedInfo) ) { + storedInfo = std::move(info); } } } @@ -803,12 +807,9 @@ void CAFMaker::beginSubRun(art::SubRun& sr) { fHasNuMIInfo = true; for(const sbn::NuMISpillInfo& info: *numi_spill) { - if ( fNuMIInfoEventMap.find(info.event)==fNuMIInfoEventMap.end() ) { - fNuMIInfoEventMap[info.event] = info; - } - else if ( (info.spill_time_s+(info.spill_time_ns/1.0e9)) > - (fNuMIInfoEventMap[info.event].spill_time_s+(fNuMIInfoEventMap[info.event].spill_time_ns/1.0e9)) ) { - fNuMIInfoEventMap[info.event] = info; + auto& storedInfo = fNuMIInfoEventMap[info.event]; // creates if needed + if ( (storedInfo.event == UINT_MAX) || spillInfoToTimestamp(info) > spillInfoToTimestamp(storedInfo) ) { + storedInfo = std::move(info); } } } @@ -2024,7 +2025,8 @@ void CAFMaker::produce(art::Event& evt) noexcept { // Fill the header info for the given event's spill quality info if ( fHasBNBInfo && fHasNuMIInfo ) { - std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Will not fill event-specific spill quality info for this event..." << std::endl; + std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Throwing..." << std::endl; + abort(); } else if ( fHasBNBInfo ) { if ( fBNBInfoEventMap.find(evt.id().event()) == fBNBInfoEventMap.end() ) From d3e595904975a223bd4c5062a0ab1d76670b4a64 Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Tue, 14 Nov 2023 15:14:22 -0600 Subject: [PATCH 4/5] Second part of incorporating Gianluca's comments. Thanks --- sbncode/CAFMaker/CAFMaker_module.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sbncode/CAFMaker/CAFMaker_module.cc b/sbncode/CAFMaker/CAFMaker_module.cc index 992cbc08a..82e1770f2 100644 --- a/sbncode/CAFMaker/CAFMaker_module.cc +++ b/sbncode/CAFMaker/CAFMaker_module.cc @@ -2028,15 +2028,15 @@ void CAFMaker::produce(art::Event& evt) noexcept { std::cout << "Found > 0 BNBInfo size and NuMIInfo size, which seems strange. Throwing..." << std::endl; abort(); } - else if ( fHasBNBInfo ) { - if ( fBNBInfoEventMap.find(evt.id().event()) == fBNBInfoEventMap.end() ) - std::cout << "We think (since fHasBNBInfo) that this event should be BNB, but did not find this event in the spill info map." << std::endl; - else rec.hdr.spillbnbinfo = makeSRBNBInfo(fBNBInfoEventMap.at(evt.id().event())); - } - else if ( fHasNuMIInfo ) { - if ( fNuMIInfoEventMap.find(evt.id().event()) == fNuMIInfoEventMap.end() ) - std::cout << "We think (since fHasNuMIInfo) that this event should be NuMI, but did not find this event in the spill info map." << std::endl; - else rec.hdr.spillnumiinfo = makeSRNuMIInfo(fNuMIInfoEventMap.at(evt.id().event())); + unsigned int const eventNo = evt.id().event(); + if ( fBNBInfoEventMap.count(eventNo) > 0 ) { + rec.hdr.spillbnbinfo = makeSRBNBInfo(fBNBInfoEventMap.at(eventNo)); + } + else if ( fNuMIInfoEventMap.count(eventNo) > 0 ) { + rec.hdr.spillnumiinfo = makeSRNuMIInfo(fNuMIInfoEventMap.at(eventNo)); + } + else { + std::cout << "Did not find this event in the spill info map." << std::endl; } if(fRecTree){ From 2dd75359dbfd0486f67954bd3e0d8caa9017f2c9 Mon Sep 17 00:00:00 2001 From: Bruce Howard Date: Wed, 6 Dec 2023 22:42:12 -0600 Subject: [PATCH 5/5] Adding to fix a pet peeve of Gianluca since I was re-testing things anyway :) --- .../NuMIRetriever/NuMIRetriever_module.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc index 432a6f0de..ba3396c99 100644 --- a/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc +++ b/sbncode/BeamSpillInfoRetriever/NuMIRetriever/NuMIRetriever_module.cc @@ -110,7 +110,7 @@ void sbn::NuMIRetriever::produce(art::Event &e) auto const & raw_data = (*raw_data_ptr); // NOTE: Really we should skip the first event of each trigger type, so let's make this look at that too... - if ( raw_data.size()==0 ) return; + if ( raw_data.empty() ) return; else { icarus::ICARUSTriggerV3Fragment frag(raw_data.at(0)); if ( frag.getTotalTriggerNuMIMaj() <= 1 ) return;