12#include "BesTofDigitizerBrV2.hh"
13#include "BesTofHit.hh"
14#include "G4DigiManager.hh"
15#include "G4RunManager.hh"
16#include "Randomize.hh"
17#include "G4Poisson.hh"
18#include "BesTofDigi.hh"
19#include "BesTofGeoParameter.hh"
20#include "GaudiKernel/ISvcLocator.h"
21#include "GaudiKernel/Bootstrap.h"
22#include "GaudiKernel/IDataProviderSvc.h"
23#include "G4Svc/IG4Svc.h"
24#include "G4Svc/G4Svc.h"
33 ISvcLocator* svcLocator = Gaudi::svcLocator();
35 StatusCode sc = svcLocator->service(
"G4Svc", tmpSvc);
38 std::cout <<
" Could not initialize Realization Service in BesTofDigitizerBrV2" << std::endl;
42 m_G4Svc =
dynamic_cast<G4Svc *
>(tmpSvc);
47 StatusCode scReal = svcLocator->service(
"RealizationSvc",tmpReal);
48 if (!scReal.isSuccess())
50 std::cout <<
" Could not initialize Realization Service in BesTofDigitizerBrV2" << std::endl;
64 m_scinLength = tofPara->
GetBr1L();
73 m_QE = tofPara->
GetQE();
74 m_CE = tofPara->
GetCE();
79 m_Ce = tofPara->
GetCe();
98 G4DigiManager* digiManager = G4DigiManager::GetDMpointer();
99 G4int THCID = digiManager->GetHitsCollectionID(
"BesTofHitsCollection");
115 G4int partId, scinNb, nHits;
151 for (G4int j=0;j<nHits;j++)
175 if ( partId==1 && (temp0 > -1998. || temp1 > -1998.))
220 for (G4int i=0;i<2;i++)
250 G4double cvelScint=c_light/m_refIndex/1.07;
252 G4ThreeVector pos=hit->
GetPos();
258 G4double timeFlight=hit->
GetTime()-m_beamTime;
267 G4double nx=pDirection.x();
268 G4double ny=pDirection.y();
269 G4double nz=pDirection.z();
281 G4double thetaProb=1-
cos( asin(1.43/m_refIndex));
285 G4double nMean, nPhoton;
288 G4int runId = m_RealizationSvc->
getRunId();
289 if( ( runId>=-11396 && runId<=-8093 ) || ( runId>-80000 && runId<=-23463 ) ) {
296 G4double resolutionScale=1.;
297 G4double sigma=resolutionScale*sqrt(nMean);
298 nPhoton=G4int(G4RandGauss::shoot(nMean,sigma)+0.5);
301 nPhoton=G4int(G4Poisson(nMean));
306 NphStep=G4int(nPhoton*thetaProb*m_rAngle*m_QE*m_CE*m_peCorFac);
322 for (G4int i=0;i<NphStep;i++)
325 ddS=stepL*G4UniformRand();
326 ddT=deltaT*G4UniformRand();
327 G4ThreeVector emtPos;
328 emtPos.setX(pos.x() + nx*ddS);
329 emtPos.setY(pos.y() + ny*ddS);
330 emtPos.setZ(pos.z() + nz*ddS);
336 DirectPh(partId, emtPos, pathL, forb);
341 G4double ran = G4UniformRand();
345 attenL = 10.*attenL/0.75;
347 if (pathL>0 &&
exp(-pathL/attenL) > ran)
350 G4double scinSwim=pathL/cvelScint;
358 G4double endTime= timeFlight + ddT + scinSwim + scinTime + transitTime;
376 if (m_t1st[forb]>endTime) m_t1st[forb] = endTime;
377 if (m_tLast[forb]<endTime) m_tLast[forb]= endTime;
385 const G4double kappa = 0.015*cm/MeV;
386 const G4String brMaterial =
"BC408";
392 G4double cor_dE = dE;
394 if(charge!=0.&& dX!=0.)
396 cor_dE = dE/(1+kappa*dE/dX);
416 G4double I1,I2,I3,rp1,rs1,rp2,rs2,Rp1,Rs1,Rp2,Rs2,Rp,Rs;
429 I3=asin(
sin(I1)*(
n1/n3));
434 Rp=(Rp1+Rp2-2*Rp1*Rp2)/(1-Rp1*Rp2);
435 Rs=(Rs1+Rs2-2*Rs1*Rs2)/(1-Rs1*Rs2);
444 G4double I1,I2,rp,rs,Rp,Rs;
464 const static G4double silicon_oil_index = 1.43;
465 const static G4double glass_index = 1.532;
468 G4double cos_span=1-
cos( asin(silicon_oil_index/m_refIndex) );
473 dcos=cos_span*(ran*2.0-1.0);
475 G4double r2=sqrt(emtPos.x()*emtPos.x()+emtPos.y()*emtPos.y());
477 G4double costheta,sintheta;
478 G4double theta,thetaR;
479 costheta=dcos>0?(1-dcos):(1+dcos);
480 theta=acos(costheta);
482 thetaR=asin(1/m_refIndex);
485 G4double ratio1Mean=(CLHEP::pi)*25.5*25.5/(57.1+60.7)/25.0;
486 G4double ratio2Mean=(19.5/25.5)*(19.5/25.5);
487 G4double ratio1=G4RandGauss::shoot(ratio1Mean,0.015);
488 G4double ratio2=G4RandGauss::shoot(ratio2Mean,0.015);
489 G4double ratio3Mean=0.945*ratio1Mean*ratio2Mean;
490 G4double ratio3=G4RandGauss::shoot(ratio3Mean,0.015);
492 G4double R2=1-
Reflectivity(m_refIndex,silicon_oil_index, glass_index, theta);
493 if (dcos > 0 && dcos != 1)
497 if (G4UniformRand()<ratio1)
499 if (G4UniformRand()<R2)
501 if (G4UniformRand()<ratio2)
504 pathL=(m_scinLength/2-emtPos.z())/costheta;
509 if (G4UniformRand()<ratio3)
512 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
518 if (G4UniformRand()<R1)
520 G4double tempran=G4UniformRand();
524 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
526 else if (tempran>ratio1&&G4UniformRand()<R1&&G4UniformRand()<ratio3)
529 pathL=(2.5*m_scinLength-emtPos.z())/costheta;
536 if (G4UniformRand()<ratio1)
538 if (G4UniformRand()<R2)
540 if (G4UniformRand()<ratio2)
543 pathL=(m_scinLength/2-emtPos.z())/costheta;
548 if (G4UniformRand()<ratio3)
551 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
557 G4double tempran=G4UniformRand();
561 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
563 else if (tempran>ratio1 && G4UniformRand()<ratio3)
566 pathL=(2.5*m_scinLength-emtPos.z())/costheta;
571 else if (dcos < 0 && dcos != -1)
575 if (G4UniformRand()<ratio1)
577 if (G4UniformRand()<R2)
579 if (G4UniformRand()<ratio2)
582 pathL=(m_scinLength/2+emtPos.z())/costheta;
587 if (G4UniformRand()<ratio3)
590 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
596 if (G4UniformRand()<R1)
598 G4double tempran=G4UniformRand();
602 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
604 else if (tempran>ratio1&&G4UniformRand()<R1&&G4UniformRand()<ratio3)
607 pathL=(2.5*m_scinLength+emtPos.z())/costheta;
614 if (G4UniformRand()<ratio1)
616 if (G4UniformRand()<R2)
618 if (G4UniformRand()<ratio2)
621 pathL=(m_scinLength/2+emtPos.z())/costheta;
626 if (G4UniformRand()<ratio3)
629 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
635 G4double tempran=G4UniformRand();
639 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
641 else if (tempran>ratio1 && G4UniformRand()<ratio3)
644 pathL=(2.5*m_scinLength+emtPos.z())/costheta;
650 G4double convFactor=180./3.1415926;
651 if (theta>asin(1)-thetaR)
653 G4double
alpha = G4UniformRand()*asin(1.);
654 G4int nRef1 = pathL*sintheta*
cos(
alpha)/50.0+0.5;
655 G4int nRef2 = pathL*sintheta*
sin(
alpha)/58.9+0.5;
656 G4double beta1=acos(sintheta*
cos(
alpha));
657 G4double beta2=acos(sintheta*
sin(
alpha));
658 beta2 -= 3.75/convFactor;
662 for (G4int i=0;i<nRef1;i++)
664 if (G4UniformRand()<(1-R21) && G4UniformRand()<0.15)
667 for (G4int i=0;i<nRef2;i++)
669 if (G4UniformRand()<(1-R22) && G4UniformRand()<0.15)
699 G4double tmp_tauRatio,tmp_tau1,tmp_tau2,tmp_tau3;
700 tmp_tauRatio = m_tauRatio;
704 G4double UniformR = tmp_tauRatio/(1+tmp_tauRatio);
705 G4double EmissionTime;
706 if (G4UniformRand()>UniformR) {
708 EmissionTime = -tmp_tau2*log( G4UniformRand() );
709 if (G4UniformRand()-
exp(EmissionTime/tmp_tau2-EmissionTime/tmp_tau1)>1.E-8)
713 else EmissionTime = -tmp_tau3*log( G4UniformRand() );
747 return G4RandGauss::shoot(m_ttsMean,m_ttsSigma);
753 ihst=G4int(endTime/m_timeBinSize);
756 m_nPhot[ihst][forb]=m_nPhot[ihst][forb]+1;
757 m_totalPhot[forb]=m_totalPhot[forb]+1;
772 G4double norma_const;
773 G4double echarge=1.6e-7;
786 norma_const = sqrt(CLHEP::pi)*tau*tau*tau/4.0;
787 t = (i+1)*m_timeBinSize;
788 snpe[i][0] = m_Ce*
t*
t*
exp(- (
t/tau) * (
t/tau) )/norma_const;
794 norma_const = sqrt(CLHEP::pi)*tau*tau*tau/4.0;
795 t = (i+1)*m_timeBinSize;
796 snpe[i][1] = m_Ce*
t*
t*
exp(- (
t/tau) * (
t/tau) )/norma_const;
801 G4double pmtGain0,pmtGain,relativeGain,smearPMTgain;
802 G4double smearADC[2] = {0.,0.};
803 G4int runId = m_RealizationSvc->
getRunId();
819 G4double timeSmear = G4RandGauss::shoot(0,0.020);
820 if (runId>=-22913 && runId<=-20448) {
821 timeSmear = G4RandGauss::shoot(0,0.040);
823 else if( ( runId>=-11396 && runId<=-8093 ) || ( runId>-80000 && runId<=-23463 ) ) {
824 timeSmear = G4RandGauss::shoot(0,0.025);
827 for (G4int j=0; j<fb; j++)
829 if (m_totalPhot[j] > 0)
831 n1=G4int(m_t1st[j]/m_timeBinSize);
832 n2=G4int(m_tLast[j]/m_timeBinSize);
840 for (G4int i=
n1;i<
n2;i++)
846 Npoisson = G4Poisson(10.0);
847 if(Npoisson>0.)
break;
853 pmtGain = pmtGain0*relativeGain;
854 smearPMTgain = G4RandGauss::shoot(pmtGain,pmtGain/sqrt(Npoisson));
856 if(smearPMTgain>0)
break;
858 smearADC[j] += phtn*smearPMTgain;
864 profPmt[ii][j] += smearPMTgain*phtn*snpe[ihst][j];
875 profPmt[i][j] = m_preGain*profPmt[i][j]+G4RandGauss::shoot(0,m_noiseSigma);
882 if (profPmt[i][j]>
max)
891 G4double tmp_HLthresh, tmp_LLthresh, adcFactor;
894 if(runId>=-80000 && runId<=-9484)
898 tmp_HLthresh = m_HLthresh;
899 tmp_LLthresh = m_LLthresh;
923 if (
max>=tmp_HLthresh)
927 if ( profPmt[i][j] >= tmp_LLthresh )
929 m_TDC[j] = i*m_timeBinSize + timeSmear +G4RandGauss::shoot(0,0.025);
931 if( ( runId>=-11396 && runId<=-8093 ) || ( runId>-80000 && runId<=-23463 ) ) {
933 m_TDC[j] = i*m_timeBinSize + timeSmear +G4RandGauss::shoot(0,0.055);
936 m_TDC[j] = i*m_timeBinSize + timeSmear +G4RandGauss::shoot(0,0.062);
943 double x = m_preGain*smearADC[j]*echarge*ratio;
954 m_ADC[j] = m_preGain*smearADC[j]*echarge*adcFactor;
EvtComplex exp(const EvtComplex &c)
double sin(const BesAngle a)
double cos(const BesAngle a)
G4TDigiCollection< BesTofDigi > BesTofDigitsCollection
G4THitsCollection< BesTofHit > BesTofHitsCollection
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_tdc0
static NTuple::Item< double > m_timelast1
static NTuple::Item< double > m_max0
static NTuple::Item< double > m_adc1
static NTuple::Item< double > m_totalPhot0
static NTuple::Item< double > m_nDigi
static NTuple::Item< double > m_scinNbMPV
static NTuple::Item< double > m_partId
static NTuple::Item< double > m_endTime
static NTuple::Item< double > m_time1st0
static NTuple::Item< double > m_nHits
static NTuple::Tuple * m_tupleTof1
static NTuple::Item< double > m_nDigiOut
static NTuple::Item< double > m_tdc1
static NTuple::Item< double > m_eTotal
static NTuple::Tuple * m_tupleTof2
static NTuple::Item< double > m_time1st1
static NTuple::Item< double > m_NphAllSteps
static NTuple::Tuple * m_tupleTof3
static NTuple::Item< double > m_max1
ITofQElecSvc * m_tofQElecSvc
BesTofHitsCollection * m_THC
static NTuple::Item< double > m_totalPhot1
static NTuple::Item< double > m_edep
static NTuple::Item< double > m_adc0
static NTuple::Item< double > m_scinNb
BesTofDigitsCollection * m_besTofDigitsCollection
static NTuple::Item< double > m_scinSwim
static NTuple::Item< double > m_partIdMPV
static NTuple::Item< double > m_timelast0
static NTuple::Item< double > m_edepMPV
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()