14#include "G4DigiManager.hh"
15#include "G4RunManager.hh"
16#include "Randomize.hh"
17#include "G4Poisson.hh"
20#include "GaudiKernel/ISvcLocator.h"
21#include "GaudiKernel/Bootstrap.h"
22#include "GaudiKernel/IDataProviderSvc.h"
26#include "G4SystemOfUnits.hh"
27#include "G4PhysicalConstants.hh"
35 ISvcLocator* svcLocator = Gaudi::svcLocator();
37 StatusCode sc = svcLocator->service(
"G4Svc", tmpSvc);
40 std::cout <<
" Could not initialize Realization Service in BesTofDigitizerBrV2" << std::endl;
44 m_G4Svc =
dynamic_cast<G4Svc *
>(tmpSvc);
49 StatusCode scReal = svcLocator->service(
"RealizationSvc",tmpReal);
50 if (!scReal.isSuccess())
52 std::cout <<
" Could not initialize Realization Service in BesTofDigitizerBrV2" << std::endl;
66 m_scinLength = tofPara->
GetBr1L();
75 m_QE = tofPara->
GetQE();
76 m_CE = tofPara->
GetCE();
81 m_Ce = tofPara->
GetCe();
100 G4DigiManager* digiManager = G4DigiManager::GetDMpointer();
101 G4int THCID = digiManager->GetHitsCollectionID(
"BesTofHitsCollection");
117 G4int partId, scinNb, nHits;
153 for (G4int j=0;j<nHits;j++)
177 if ( partId==1 && (temp0 > -1998. || temp1 > -1998.))
222 for (G4int i=0;i<2;i++)
252 G4double cvelScint=c_light/m_refIndex/1.07;
254 G4ThreeVector pos=hit->
GetPos();
260 G4double timeFlight=hit->
GetTime()-m_beamTime;
269 G4double nx=pDirection.x();
270 G4double ny=pDirection.y();
271 G4double nz=pDirection.z();
283 G4double thetaProb=1-
cos( asin(1.43/m_refIndex));
287 G4double nMean, nPhoton;
290 G4int runId = m_RealizationSvc->
getRunId();
291 if( ( runId>=-11396 && runId<=-8093 ) || ( runId>-80000 && runId<=-23463 ) ) {
298 G4double resolutionScale=1.;
299 G4double
sigma=resolutionScale*sqrt(nMean);
300 nPhoton=G4int(G4RandGauss::shoot(nMean,
sigma)+0.5);
303 nPhoton=G4int(G4Poisson(nMean));
308 NphStep=G4int(nPhoton*thetaProb*m_rAngle*m_QE*m_CE*m_peCorFac);
324 for (G4int i=0;i<NphStep;i++)
327 ddS=stepL*G4UniformRand();
328 ddT=deltaT*G4UniformRand();
329 G4ThreeVector emtPos;
330 emtPos.setX(pos.x() + nx*ddS);
331 emtPos.setY(pos.y() + ny*ddS);
332 emtPos.setZ(pos.z() + nz*ddS);
338 DirectPh(partId, emtPos, pathL, forb);
343 G4double ran = G4UniformRand();
347 attenL = 10.*attenL/0.75;
349 if (pathL>0 &&
exp(-pathL/attenL) > ran)
352 G4double scinSwim=pathL/cvelScint;
360 G4double endTime= timeFlight + ddT + scinSwim + scinTime + transitTime;
378 if (m_t1st[forb]>endTime) m_t1st[forb] = endTime;
379 if (m_tLast[forb]<endTime) m_tLast[forb]= endTime;
387 const G4double kappa = 0.015*cm/MeV;
388 const G4String brMaterial =
"BC408";
394 G4double cor_dE = dE;
398 cor_dE = dE/(1+kappa*dE/dX);
418 G4double I1,I2,I3,rp1,rs1,rp2,rs2,Rp1,Rs1,Rp2,Rs2,Rp,Rs;
431 I3=asin(
sin(I1)*(
n1/n3));
436 Rp=(Rp1+Rp2-2*Rp1*Rp2)/(1-Rp1*Rp2);
437 Rs=(Rs1+Rs2-2*Rs1*Rs2)/(1-Rs1*Rs2);
446 G4double I1,I2,rp,rs,Rp,Rs;
466 const static G4double silicon_oil_index = 1.43;
467 const static G4double glass_index = 1.532;
470 G4double cos_span=1-
cos( asin(silicon_oil_index/m_refIndex) );
475 dcos=cos_span*(ran*2.0-1.0);
477 G4double r2=sqrt(emtPos.x()*emtPos.x()+emtPos.y()*emtPos.y());
480 G4double theta,thetaR;
484 thetaR=asin(1/m_refIndex);
487 G4double ratio1Mean=(CLHEP::pi)*25.5*25.5/(57.1+60.7)/25.0;
488 G4double ratio2Mean=(19.5/25.5)*(19.5/25.5);
489 G4double ratio1=G4RandGauss::shoot(ratio1Mean,0.015);
490 G4double ratio2=G4RandGauss::shoot(ratio2Mean,0.015);
491 G4double ratio3Mean=0.945*ratio1Mean*ratio2Mean;
492 G4double ratio3=G4RandGauss::shoot(ratio3Mean,0.015);
494 G4double R2=1-
Reflectivity(m_refIndex,silicon_oil_index, glass_index, theta);
495 if (dcos > 0 && dcos != 1)
499 if (G4UniformRand()<ratio1)
501 if (G4UniformRand()<R2)
503 if (G4UniformRand()<ratio2)
506 pathL=(m_scinLength/2-emtPos.z())/
costheta;
511 if (G4UniformRand()<ratio3)
514 pathL=(1.5*m_scinLength-emtPos.z())/
costheta;
520 if (G4UniformRand()<R1)
522 G4double tempran=G4UniformRand();
526 pathL=(1.5*m_scinLength-emtPos.z())/
costheta;
528 else if (tempran>ratio1&&G4UniformRand()<R1&&G4UniformRand()<ratio3)
531 pathL=(2.5*m_scinLength-emtPos.z())/
costheta;
538 if (G4UniformRand()<ratio1)
540 if (G4UniformRand()<R2)
542 if (G4UniformRand()<ratio2)
545 pathL=(m_scinLength/2-emtPos.z())/
costheta;
550 if (G4UniformRand()<ratio3)
553 pathL=(1.5*m_scinLength-emtPos.z())/
costheta;
559 G4double tempran=G4UniformRand();
563 pathL=(1.5*m_scinLength-emtPos.z())/
costheta;
565 else if (tempran>ratio1 && G4UniformRand()<ratio3)
568 pathL=(2.5*m_scinLength-emtPos.z())/
costheta;
573 else if (dcos < 0 && dcos != -1)
577 if (G4UniformRand()<ratio1)
579 if (G4UniformRand()<R2)
581 if (G4UniformRand()<ratio2)
584 pathL=(m_scinLength/2+emtPos.z())/
costheta;
589 if (G4UniformRand()<ratio3)
592 pathL=(1.5*m_scinLength+emtPos.z())/
costheta;
598 if (G4UniformRand()<R1)
600 G4double tempran=G4UniformRand();
604 pathL=(1.5*m_scinLength+emtPos.z())/
costheta;
606 else if (tempran>ratio1&&G4UniformRand()<R1&&G4UniformRand()<ratio3)
609 pathL=(2.5*m_scinLength+emtPos.z())/
costheta;
616 if (G4UniformRand()<ratio1)
618 if (G4UniformRand()<R2)
620 if (G4UniformRand()<ratio2)
623 pathL=(m_scinLength/2+emtPos.z())/
costheta;
628 if (G4UniformRand()<ratio3)
631 pathL=(1.5*m_scinLength+emtPos.z())/
costheta;
637 G4double tempran=G4UniformRand();
641 pathL=(1.5*m_scinLength+emtPos.z())/
costheta;
643 else if (tempran>ratio1 && G4UniformRand()<ratio3)
646 pathL=(2.5*m_scinLength+emtPos.z())/
costheta;
652 G4double convFactor=180./3.1415926;
653 if (theta>asin(1)-thetaR)
655 G4double
alpha = G4UniformRand()*asin(1.);
656 G4int nRef1 = pathL*sintheta*
cos(
alpha)/50.0+0.5;
657 G4int nRef2 = pathL*sintheta*
sin(
alpha)/58.9+0.5;
658 G4double beta1=acos(sintheta*
cos(
alpha));
659 G4double beta2=acos(sintheta*
sin(
alpha));
660 beta2 -= 3.75/convFactor;
664 for (G4int i=0;i<nRef1;i++)
666 if (G4UniformRand()<(1-R21) && G4UniformRand()<0.15)
669 for (G4int i=0;i<nRef2;i++)
671 if (G4UniformRand()<(1-R22) && G4UniformRand()<0.15)
701 G4double tmp_tauRatio,tmp_tau1,tmp_tau2,tmp_tau3;
702 tmp_tauRatio = m_tauRatio;
706 G4double UniformR = tmp_tauRatio/(1+tmp_tauRatio);
707 G4double EmissionTime;
708 if (G4UniformRand()>UniformR) {
710 EmissionTime = -tmp_tau2*log( G4UniformRand() );
711 if (G4UniformRand()-
exp(EmissionTime/tmp_tau2-EmissionTime/tmp_tau1)>1.E-8)
715 else EmissionTime = -tmp_tau3*log( G4UniformRand() );
749 return G4RandGauss::shoot(m_ttsMean,m_ttsSigma);
755 ihst=G4int(endTime/m_timeBinSize);
758 m_nPhot[ihst][forb]=m_nPhot[ihst][forb]+1;
759 m_totalPhot[forb]=m_totalPhot[forb]+1;
774 G4double norma_const;
775 G4double echarge=1.6e-7;
788 norma_const = sqrt(CLHEP::pi)*tau*tau*tau/4.0;
789 t = (i+1)*m_timeBinSize;
790 snpe[i][0] = m_Ce*
t*
t*
exp(- (
t/tau) * (
t/tau) )/norma_const;
796 norma_const = sqrt(CLHEP::pi)*tau*tau*tau/4.0;
797 t = (i+1)*m_timeBinSize;
798 snpe[i][1] = m_Ce*
t*
t*
exp(- (
t/tau) * (
t/tau) )/norma_const;
803 G4double pmtGain0,pmtGain,relativeGain,smearPMTgain;
804 G4double smearADC[2] = {0.,0.};
805 G4int runId = m_RealizationSvc->
getRunId();
821 G4double timeSmear = G4RandGauss::shoot(0,0.020);
822 if (runId>=-22913 && runId<=-20448) {
823 timeSmear = G4RandGauss::shoot(0,0.040);
825 else if( ( runId>=-11396 && runId<=-8093 ) || ( runId>-80000 && runId<=-23463 ) ) {
826 timeSmear = G4RandGauss::shoot(0,0.025);
829 for (G4int j=0; j<fb; j++)
831 if (m_totalPhot[j] > 0)
833 n1=G4int(m_t1st[j]/m_timeBinSize);
834 n2=G4int(m_tLast[j]/m_timeBinSize);
842 for (G4int i=
n1;i<
n2;i++)
848 Npoisson = G4Poisson(10.0);
849 if(Npoisson>0.)
break;
855 pmtGain = pmtGain0*relativeGain;
856 smearPMTgain = G4RandGauss::shoot(pmtGain,pmtGain/sqrt(Npoisson));
858 if(smearPMTgain>0)
break;
860 smearADC[j] += phtn*smearPMTgain;
866 profPmt[ii][j] += smearPMTgain*phtn*snpe[ihst][j];
877 profPmt[i][j] = m_preGain*profPmt[i][j]+G4RandGauss::shoot(0,m_noiseSigma);
884 if (profPmt[i][j]>
max)
893 G4double tmp_HLthresh, tmp_LLthresh, adcFactor;
896 if(runId>=-80000 && runId<=-9484)
900 tmp_HLthresh = m_HLthresh;
901 tmp_LLthresh = m_LLthresh;
925 if (
max>=tmp_HLthresh)
929 if ( profPmt[i][j] >= tmp_LLthresh )
931 m_TDC[j] = i*m_timeBinSize + timeSmear +G4RandGauss::shoot(0,0.025);
933 if( ( runId>=-11396 && runId<=-8093 ) || ( runId>-80000 && runId<=-23463 ) ) {
935 m_TDC[j] = i*m_timeBinSize + timeSmear +G4RandGauss::shoot(0,0.055);
938 m_TDC[j] = i*m_timeBinSize + timeSmear +G4RandGauss::shoot(0,0.062);
945 double x = m_preGain*smearADC[j]*echarge*ratio;
956 m_ADC[j] = m_preGain*smearADC[j]*echarge*adcFactor;
double sin(const BesAngle a)
double cos(const BesAngle a)
G4TDigiCollection< BesTofDigi > BesTofDigitsCollection
G4THitsCollection< BesTofHit > BesTofHitsCollection
EvtComplex exp(const EvtComplex &c)
void SetPartId(G4int partId)
void SetForwADC(G4double ADC)
void SetTrackIndex(G4int index)
void SetBackADC(G4double ADC)
void SetForwTDC(G4double TDC)
void SetScinNb(G4int scinNb)
void SetBackTDC(G4double TDC)
G4double Reflectivity(G4double n1, G4double n2, G4double theta)
void AccuSignal(G4double, G4int)
virtual void Digitize(ScintSingle *, BesTofDigitsCollection *)
G4double Scintillation(G4int)
void DirectPh(G4int, G4ThreeVector, G4double &, G4int &)
G4double BirksLaw(BesTofHit *hit)
void TofPmtAccum(BesTofHit *, G4int)
static NTuple::Item< double > m_partId
static NTuple::Tuple * m_tupleTof3
static NTuple::Item< double > m_tdc1
static NTuple::Item< double > m_max0
static NTuple::Item< double > m_eTotal
static NTuple::Item< double > m_NphAllSteps
static NTuple::Item< double > m_tdc0
static NTuple::Item< double > m_timelast1
static NTuple::Item< double > m_timelast0
ITofQElecSvc * m_tofQElecSvc
static NTuple::Item< double > m_scinSwim
static NTuple::Item< double > m_scinNb
static NTuple::Item< double > m_adc1
static NTuple::Tuple * m_tupleTof2
static NTuple::Item< double > m_edep
static NTuple::Item< double > m_totalPhot0
static NTuple::Item< double > m_endTime
static NTuple::Item< double > m_scinNbMPV
static NTuple::Item< double > m_nDigi
BesTofDigitsCollection * m_besTofDigitsCollection
static NTuple::Item< double > m_adc0
static NTuple::Item< double > m_nHits
static NTuple::Item< double > m_partIdMPV
static NTuple::Item< double > m_time1st1
static NTuple::Item< double > m_nDigiOut
BesTofHitsCollection * m_THC
static NTuple::Item< double > m_totalPhot1
static NTuple::Tuple * m_tupleTof1
static NTuple::Item< double > m_edepMPV
static NTuple::Item< double > m_max1
static NTuple::Item< double > m_time1st0
G4double GetBrWRiseTime(int scinNb)
static BesTofGeoParameter * GetInstance()
G4double GetBrERiseTime(int scinNb)
G4ThreeVector GetPDirection()
virtual const double BQChannel2(int id, double q)=0
virtual const double BQChannel1(int id, double q)=0
virtual const double BarConstant()=0
virtual const double BarLowThres()=0
virtual const double BarPMTGain()=0
virtual const double BarGain2(unsigned int id)=0
virtual const double BarAttenLength(unsigned int id)=0
virtual const double BarHighThres()=0
virtual const double BarGain1(unsigned int id)=0
vector< G4int > * GetHitIndexes()