Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CRVResponse/fcl/prolog_v12.fcl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand Down
5 changes: 5 additions & 0 deletions CRVResponse/inc/MakeCrvPhotons.hh
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class MakeCrvPhotons
const std::vector<double> &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:

Expand All @@ -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<double,double> &photons);
int GetNumberOfPhotonsFromAverage(double average, int nSteps);

Expand Down
8 changes: 7 additions & 1 deletion CRVResponse/src/CrvPhotonGenerator_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace mu2e
Comment("time window before digitization starts to account for photon travel time and electronics response.")};
fhicl::Atom<int> numberSamplesNZS{Name("numberSamplesNZS")}; //134
fhicl::Atom<bool> simulateNZS{Name("simulateNZS")}; //false
fhicl::Atom<bool> 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<art::InputTag> eventWindowMarkerTag{ Name("eventWindowMarkerTag"), Comment("EventWindowMarker producer"),"EWMProducer" };
fhicl::Atom<art::InputTag> protonBunchTimeMCTag{ Name("protonBunchTimeMCTag"), Comment("ProtonBunchTimeMC producer"),"EWMProducer" };
};
Expand Down Expand Up @@ -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;
Expand All @@ -149,6 +151,7 @@ namespace mu2e
CLHEP::RandFlat _randFlat;
CLHEP::RandGaussQ _randGaussQ;
CLHEP::RandPoissonQ _randPoissonQ;
CLHEP::RandBinomial _randBinomial;
};

CrvPhotonGenerator::CrvPhotonGenerator(const Parameters& conf) :
Expand All @@ -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<SeedService>()->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)");
Expand Down Expand Up @@ -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 "<<i<<" ("<<_CRVSectors[i]<<") uses "<<_makeCrvPhotons.back()->GetFileName()<<" with scintillation yield of "<<_scintillationYields[i]<<" photons/MeV"<<std::endl;
}

Expand Down
100 changes: 71 additions & 29 deletions CRVResponse/src/MakeCrvPhotons.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Offline/CRVResponse/inc/MakeCrvPhotons.hh"

#include <sstream>
#include <algorithm>
#include <cmath>

#include "CLHEP/Units/GlobalSystemOfUnits.h"
#include "CLHEP/Vector/TwoVector.h"
Expand Down Expand Up @@ -118,12 +120,13 @@ unsigned int LookupBinDefinitions::getNFiberCerenkovBins()

unsigned int LookupBinDefinitions::findBin(const std::vector<double> &v, const double &x, bool &notFound)
{
for(size_t i=1; i<v.size(); i++)
//bin edges are ascending; x on an inner edge belongs to the lower bin
if(v.size()<2 || !(v.front()<=x && x<=v.back()))
{
if(v[i-1]<=x && v[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)
{
Expand Down Expand Up @@ -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; i<nPhotons; i++)
if(_randBinomial)
{
//get the right bin
const LookupBin *theBin=cerenkovBin;
if(i<nPhotonsScintillation) theBin=scintillationBin;
if(theBin==NULL) continue; //this can't actually happen

//photon arrival probability at SiPM
double probability = theBin->arrivalProbability;
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; iEmission<nEmissions; iEmission++) arrivalTime+=-_LC.WLSfiberDecayTime*log(_randFlat.fire());

//add additional time delay due to the photons bouncing around
arrivalTime+=GetRandomTime(theBin);

if(reflector!=-1 && reflector!=-2) _arrivalTimes[SiPM].push_back(arrivalTime);
else _arrivalTimes[SiPM+1].push_back(arrivalTime);

}// if a photon was created
}//loop over all photons at this point
if(theBins[type]==NULL || nCreated[type]<=0) continue;
double probability = theBins[type]->arrivalProbability;
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; i<nArrived; i++) AddArrivingPhoton(theBins[type], t, SiPM, reflector);
}
}
else
{
for(int i=0; i<nPhotons; i++)
{
//get the right bin
const LookupBin *theBin=cerenkovBin;
if(i<nPhotonsScintillation) theBin=scintillationBin;
if(theBin==NULL) continue; //this can't actually happen

//photon arrival probability at SiPM
double probability = theBin->arrivalProbability;
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

Expand Down Expand Up @@ -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<long>(_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 && k<n && pk>0.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<nEmissions; iEmission++) arrivalTime+=-_LC.WLSfiberDecayTime*log(_randFlat.fire());

//add additional time delay due to the photons bouncing around
arrivalTime+=GetRandomTime(theBin);

if(reflector!=-1 && reflector!=-2) _arrivalTimes[SiPM].push_back(arrivalTime);
else _arrivalTimes[SiPM+1].push_back(arrivalTime);
}

int MakeCrvPhotons::GetNumberOfPhotonsFromAverage(double average, int nSteps) //from G4Scintillation
{
int nPhotons;
Expand Down