From c8575b8d15f20bc4cd14e38947e957aa8e68d331 Mon Sep 17 00:00:00 2001 From: Rob Mina Date: Tue, 10 Nov 2026 10:38:38 -0600 Subject: [PATCH 1/2] CRVResponse: binomial photon arrivals per sub-step and a binary search for lookup bins --- CRVResponse/inc/MakeCrvPhotons.hh | 5 + CRVResponse/src/CrvPhotonGenerator_module.cc | 8 +- CRVResponse/src/MakeCrvPhotons.cc | 100 +++++++++++++------ 3 files changed, 83 insertions(+), 30 deletions(-) diff --git a/CRVResponse/inc/MakeCrvPhotons.hh b/CRVResponse/inc/MakeCrvPhotons.hh index 42c9fc8de4..1860788e07 100644 --- a/CRVResponse/inc/MakeCrvPhotons.hh +++ b/CRVResponse/inc/MakeCrvPhotons.hh @@ -117,6 +117,8 @@ class MakeCrvPhotons const std::vector &GetArrivalTimes(int SiPM); void SetScintillationYield(double yield) {_scintillationYield=yield;} void SetPhotonYieldDeviation(double deviation, int SiPM) {_photonYieldDeviation[SiPM]=deviation;} + // draw the number of arriving photons per step point and photon type (randBinomial only for large n*p); nullptr draws one number per photon + void SetBinomialArrivals(CLHEP::RandBinomial *randBinomial) {_randBinomial=randBinomial;} private: @@ -135,11 +137,14 @@ class MakeCrvPhotons CLHEP::RandFlat &_randFlat; CLHEP::RandGaussQ &_randGaussQ; CLHEP::RandPoissonQ &_randPoissonQ; + CLHEP::RandBinomial *_randBinomial=nullptr; bool IsInsideScintillator(const CLHEP::Hep3Vector &p); bool IsInsideFiber(const CLHEP::Hep3Vector &p, const CLHEP::Hep3Vector &dir, double &r, double &phi); double GetRandomTime(const LookupBin *theBin); int GetRandomFiberEmissions(const LookupBin *theBin); + void AddArrivingPhoton(const LookupBin *theBin, double t, int SiPM, int reflector); + long GetNumberOfArrivals(int n, double p); double GetAverageNumberOfCerenkovPhotons(double beta, double charge, std::map &photons); int GetNumberOfPhotonsFromAverage(double average, int nSteps); diff --git a/CRVResponse/src/CrvPhotonGenerator_module.cc b/CRVResponse/src/CrvPhotonGenerator_module.cc index e21dd1c8f3..0e63e7d75a 100644 --- a/CRVResponse/src/CrvPhotonGenerator_module.cc +++ b/CRVResponse/src/CrvPhotonGenerator_module.cc @@ -75,6 +75,7 @@ namespace mu2e Comment("time window before digitization starts to account for photon travel time and electronics response.")}; fhicl::Atom numberSamplesNZS{Name("numberSamplesNZS")}; //134 fhicl::Atom simulateNZS{Name("simulateNZS")}; //false + fhicl::Atom binomialArrivals{ Name("binomialArrivals"), Comment("draw the number of photons arriving at a SiPM per step point and photon type, instead of one random number per created photon"), true}; fhicl::Atom eventWindowMarkerTag{ Name("eventWindowMarkerTag"), Comment("EventWindowMarker producer"),"EWMProducer" }; fhicl::Atom protonBunchTimeMCTag{ Name("protonBunchTimeMCTag"), Comment("ProtonBunchTimeMC producer"),"EWMProducer" }; }; @@ -141,6 +142,7 @@ namespace mu2e double _digitizationStartMargin; //100ns (used to account for photon travel time and electronics response time) int _numberSamplesNZS; //134 bool _simulateNZS; //false + bool _binomialArrivals; art::InputTag _eventWindowMarkerTag; art::InputTag _protonBunchTimeMCTag; double _microBunchPeriod; @@ -149,6 +151,7 @@ namespace mu2e CLHEP::RandFlat _randFlat; CLHEP::RandGaussQ _randGaussQ; CLHEP::RandPoissonQ _randPoissonQ; + CLHEP::RandBinomial _randBinomial; }; CrvPhotonGenerator::CrvPhotonGenerator(const Parameters& conf) : @@ -168,12 +171,14 @@ namespace mu2e _digitizationStartMargin(conf().digitizationStartMargin()), _numberSamplesNZS(conf().numberSamplesNZS()), _simulateNZS(conf().simulateNZS()), + _binomialArrivals(conf().binomialArrivals()), _eventWindowMarkerTag(conf().eventWindowMarkerTag()), _protonBunchTimeMCTag(conf().protonBunchTimeMCTag()), _engine{createEngine(art::ServiceHandle()->getSeed())}, _randFlat(_engine), _randGaussQ(_engine), - _randPoissonQ(_engine) + _randPoissonQ(_engine), + _randBinomial(_engine) { //if(_moduleLabels.size()==0) throw std::logic_error("ERROR: a list of crvSteps module labels needs to be provided"); if(_moduleLabels.size()!=_processNames.size()) throw std::logic_error("ERROR: mismatch between specified selectors (crvStepModuleLabels/crvStepProcessNames)"); @@ -224,6 +229,7 @@ namespace mu2e photonMaker->LoadLookupTable(filespec,_debug); } photonMaker->SetScintillationYield(_scintillationYields[i]); + if(_binomialArrivals) photonMaker->SetBinomialArrivals(&_randBinomial); if(_debug>0) std::cout<<"CRV sector "<GetFileName()<<" with scintillation yield of "<<_scintillationYields[i]<<" photons/MeV"< +#include +#include #include "CLHEP/Units/GlobalSystemOfUnits.h" #include "CLHEP/Vector/TwoVector.h" @@ -118,12 +120,13 @@ unsigned int LookupBinDefinitions::getNFiberCerenkovBins() unsigned int LookupBinDefinitions::findBin(const std::vector &v, const double &x, bool ¬Found) { - for(size_t i=1; i=x) return(i-1); + notFound=true; + return(-1); } - notFound=true; - return(-1); + return(std::lower_bound(v.begin()+1,v.end(),x)-v.begin()-1); } int LookupBinDefinitions::findScintillatorScintillationBin(double x, double y, double z) { @@ -390,33 +393,37 @@ void MakeCrvPhotons::MakePhotons(const CLHEP::Hep3Vector &stepStartTmp, //they //loop over all photons created at this point int nPhotons = nPhotonsScintillation + nPhotonsCerenkov; - for(int i=0; iarrivalProbability; - probability*=_photonYieldDeviation[SiPM]; //channel-specific deviation from nominal, e.g. due to scintillator variations or SiPM misalignments - if(_randFlat.fire()<=probability) //a photon arrives at the SiPM --> calculate arrival time + //number of photons arriving at the SiPM per photon type: the same distribution as one draw per photon + const LookupBin *theBins[2] = {scintillationBin, cerenkovBin}; + int nCreated[2] = {nPhotonsScintillation, nPhotonsCerenkov}; + for(int type=0; type<2; type++) { - //start time of photons - double arrivalTime = t; - - //add fiber decay times depending on the number of emissions - int nEmissions = GetRandomFiberEmissions(theBin); - for(int iEmission=0; iEmissionarrivalProbability; + probability*=_photonYieldDeviation[SiPM]; + long nArrived = 0; + if(probability>=1.0) nArrived = nCreated[type]; + else if(probability>0.0) nArrived = GetNumberOfArrivals(nCreated[type],probability); + for(long i=0; iarrivalProbability; + probability*=_photonYieldDeviation[SiPM]; //channel-specific deviation from nominal, e.g. due to scintillator variations or SiPM misalignments + if(_randFlat.fire()<=probability) AddArrivingPhoton(theBin, t, SiPM, reflector); //a photon arrives at the SiPM --> calculate arrival time + }//loop over all photons at this point + } }//loop over all steps along the track }//loop over all SiPMs @@ -512,6 +519,41 @@ int MakeCrvPhotons::GetRandomFiberEmissions(const LookupBin *theBin) return emissions; } +long MakeCrvPhotons::GetNumberOfArrivals(int n, double p) +{ + //binomial(n,p) by inversion with one uniform, exact at any n*p; CLHEP::RandBinomial (too low for small n*p) only where P(0) underflows + double logq = std::log1p(-p); + if(n*logq < -700.0) return static_cast(_randBinomial->fire(n,p)); + double pk = std::exp(n*logq); + double cdf = pk; + double r = p/(1.0-p); + double u = _randFlat.fire(); + long k = 0; + while(u>cdf && k0.0) + { + pk *= r*(n-k)/(k+1.0); + cdf += pk; + ++k; + } + return k; +} + +void MakeCrvPhotons::AddArrivingPhoton(const LookupBin *theBin, double t, int SiPM, int reflector) +{ + //start time of photons + double arrivalTime = t; + + //add fiber decay times depending on the number of emissions + int nEmissions = GetRandomFiberEmissions(theBin); + for(int iEmission=0; iEmission Date: Tue, 10 Nov 2026 19:21:40 -0600 Subject: [PATCH 2/2] CRVResponse: binomialArrivals defaults to false in C++ and is set true in prolog_v12 Older CRVResponse prologs keep the per-photon draw, bit-identical to main; current configurations, which include prolog_v12, get the binomial arrivals. --- CRVResponse/fcl/prolog_v12.fcl | 1 + CRVResponse/src/CrvPhotonGenerator_module.cc | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CRVResponse/fcl/prolog_v12.fcl b/CRVResponse/fcl/prolog_v12.fcl index f4145ef32d..3290f73c60 100644 --- a/CRVResponse/fcl/prolog_v12.fcl +++ b/CRVResponse/fcl/prolog_v12.fcl @@ -91,6 +91,7 @@ BEGIN_PROLOG digitizationStartMargin : 100.0 //ns start recording earlier to account for photon travel times and electronics response times numberSamplesNZS : @local::NumberSamplesNZS simulateNZS : @local::SimulateNZS + binomialArrivals : true //number of arriving photons drawn per step point and photon type } CrvSiPMCharges: { diff --git a/CRVResponse/src/CrvPhotonGenerator_module.cc b/CRVResponse/src/CrvPhotonGenerator_module.cc index 0e63e7d75a..58f4720ca2 100644 --- a/CRVResponse/src/CrvPhotonGenerator_module.cc +++ b/CRVResponse/src/CrvPhotonGenerator_module.cc @@ -75,7 +75,7 @@ namespace mu2e Comment("time window before digitization starts to account for photon travel time and electronics response.")}; fhicl::Atom numberSamplesNZS{Name("numberSamplesNZS")}; //134 fhicl::Atom simulateNZS{Name("simulateNZS")}; //false - fhicl::Atom binomialArrivals{ Name("binomialArrivals"), Comment("draw the number of photons arriving at a SiPM per step point and photon type, instead of one random number per created photon"), true}; + fhicl::Atom binomialArrivals{ Name("binomialArrivals"), Comment("draw the number of photons arriving at a SiPM per step point and photon type, instead of one random number per created photon"), false}; fhicl::Atom eventWindowMarkerTag{ Name("eventWindowMarkerTag"), Comment("EventWindowMarker producer"),"EWMProducer" }; fhicl::Atom protonBunchTimeMCTag{ Name("protonBunchTimeMCTag"), Comment("ProtonBunchTimeMC producer"),"EWMProducer" }; };