From a6861534dcc6d72bc4170f0c7d5ac162b9db99b6 Mon Sep 17 00:00:00 2001 From: Rob Mina Date: Tue, 10 Nov 2026 10:14:31 -0600 Subject: [PATCH 1/2] CRVResponse, CaloMC: index the input collections once per event CrvSiPMChargeGenerator builds the first CrvPhotons of each channel once per event instead of a linear search per channel; CaloDigiMaker collects the CaloShowerROs of each readout in one pass instead of scanning all of them per readout. Output bitwise identical; CaloDigiMaker 55 % and CrvSiPMCharges 14 % faster. Co-Authored-By: Claude Opus 5.5 --- .../src/CrvSiPMChargeGenerator_module.cc | 31 ++++++++++++------- CaloMC/src/CaloDigiMaker_module.cc | 20 +++++++----- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/CRVResponse/src/CrvSiPMChargeGenerator_module.cc b/CRVResponse/src/CrvSiPMChargeGenerator_module.cc index a94c39e10c..30f2c9852d 100644 --- a/CRVResponse/src/CrvSiPMChargeGenerator_module.cc +++ b/CRVResponse/src/CrvSiPMChargeGenerator_module.cc @@ -195,6 +195,16 @@ namespace mu2e GeomHandle CRS; const std::vector > &counters = CRS->getAllCRSScintillatorBars(); std::vector >::const_iterator iter; + + //first photon collection of each channel (bar index * channels per bar + SiPM number) + std::vector photonsOfChannel(counters.size()*CRVId::nChanPerBar, crvPhotonsCollection->end()); + for(CrvPhotonsCollection::const_iterator p=crvPhotonsCollection->begin(); p!=crvPhotonsCollection->end(); ++p) + { + int SiPM = p->GetSiPMNumber(); + if(SiPM<0 || SiPM>=static_cast(CRVId::nChanPerBar)) continue; + size_t channel = p->GetScintillatorBarIndex().asUint()*CRVId::nChanPerBar + SiPM; + if(channelend()) photonsOfChannel[channel]=p; + } for(iter=counters.begin(); iter!=counters.end(); iter++) { const CRSScintillatorBarIndex &barIndex = (*iter)->index(); @@ -215,20 +225,17 @@ namespace mu2e //time wrapping happened in the photon generator std::vector > photonTimesNew; //pair of photon time and index in the original photon vector - CrvPhotonsCollection::const_iterator crvPhotons; //after this loop, it will point to the correct index in the photon collection, if barIndex and SiPM is found - //otherwise it will be crvPhotonsCollection->end() - for(crvPhotons=crvPhotonsCollection->begin(); crvPhotons!=crvPhotonsCollection->end(); crvPhotons++) + //points to the photon collection of this barIndex and SiPM, or to crvPhotonsCollection->end() if there is none + size_t channel = barIndex.asUint()*CRVId::nChanPerBar + SiPM; + CrvPhotonsCollection::const_iterator crvPhotons = channelend(); + if(crvPhotons!=crvPhotonsCollection->end()) { - if(crvPhotons->GetScintillatorBarIndex()==barIndex && crvPhotons->GetSiPMNumber()==(int)SiPM) + const std::vector &photonTimes = crvPhotons->GetPhotons(); + for(size_t iphoton=0; iphoton &photonTimes = crvPhotons->GetPhotons(); - for(size_t iphoton=0; iphoton& waveform, const CaloShowerROCollection&, const ProtonBunchTimeMC&, const CalSimParams&); + bool fillROHits (unsigned iRO, std::vector& waveform, const std::vector&, const ProtonBunchTimeMC&, const CalSimParams&); void addWFNoise (std::vector& waveform, int noiseWFID); void buildOutputDigi (unsigned iRO, std::vector& waveform, double pedestal, CaloDigiCollection&); void extract (const std::vector& wf, std::vector& starts, std::vector& stops) const; @@ -190,9 +190,16 @@ namespace mu2e { bool resetWaveform(false); std::vector waveform(waveformSize,0.0); + // CaloShowerROs of each readout, in collection order + std::vector> showerROsOfRO(nWaveforms); + for (const auto& CaloShowerRO : CaloShowerROs) { + unsigned SiPMID = CaloShowerRO.SiPMID(); + if (SiPMID < unsigned(nWaveforms)) showerROsOfRO[SiPMID].push_back(&CaloShowerRO); + } + for (int iRO=0;iRO& waveform, const CaloShowerROCollection& CaloShowerROs, + bool CaloDigiMaker::fillROHits(unsigned iRO, std::vector& waveform, const std::vector& showerROs, const ProtonBunchTimeMC& pbtmc, const CalSimParams& calCrystalConds) { bool isEmpty = true; const double scaleFactor = readoutScaleFactor(iRO, calCrystalConds); - for (const auto& CaloShowerRO : CaloShowerROs) { - unsigned SiPMID = CaloShowerRO.SiPMID(); - if (SiPMID != iRO) continue; - + for (const auto* showerRO : showerROs) { isEmpty = false; - for (const auto PEtime : CaloShowerRO.PETime()) { + for (const auto PEtime : showerRO->PETime()) { //PE time is given in DR frame, we need to subtract the event window start and the digi Start time float time = PEtime + pbtmc.pbtime_- digitizationStart_ + timeFromProtonsToDRMarker_ + startTimeBuffer_; unsigned startSample = std::max(0u,unsigned(time/CaloConst::_digitizationPeriod)); From 0a9a6084991cfdad1553d9a585e8f49b7994e824 Mon Sep 17 00:00:00 2001 From: Rob Mina Date: Tue, 10 Nov 2026 19:20:57 -0600 Subject: [PATCH 2/2] CaloDigiMaker: name the index loop variable showerRO --- CaloMC/src/CaloDigiMaker_module.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CaloMC/src/CaloDigiMaker_module.cc b/CaloMC/src/CaloDigiMaker_module.cc index 1708bf5fc7..ced0708f7c 100644 --- a/CaloMC/src/CaloDigiMaker_module.cc +++ b/CaloMC/src/CaloDigiMaker_module.cc @@ -192,9 +192,9 @@ namespace mu2e { // CaloShowerROs of each readout, in collection order std::vector> showerROsOfRO(nWaveforms); - for (const auto& CaloShowerRO : CaloShowerROs) { - unsigned SiPMID = CaloShowerRO.SiPMID(); - if (SiPMID < unsigned(nWaveforms)) showerROsOfRO[SiPMID].push_back(&CaloShowerRO); + for (const auto& showerRO : CaloShowerROs) { + unsigned SiPMID = showerRO.SiPMID(); + if (SiPMID < unsigned(nWaveforms)) showerROsOfRO[SiPMID].push_back(&showerRO); } for (int iRO=0;iRO