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;
292 G4double resolutionScale=1.;
293 G4double sigma=resolutionScale*sqrt(nMean);
294 nPhoton=G4int(G4RandGauss::shoot(nMean,sigma)+0.5);
297 nPhoton=G4int(G4Poisson(nMean));
302 NphStep=G4int(nPhoton*thetaProb*m_rAngle*m_QE*m_CE*m_peCorFac);
318 for (G4int i=0;i<NphStep;i++)
321 ddS=stepL*G4UniformRand();
322 ddT=deltaT*G4UniformRand();
323 G4ThreeVector emtPos;
324 emtPos.setX(pos.x() + nx*ddS);
325 emtPos.setY(pos.y() + ny*ddS);
326 emtPos.setZ(pos.z() + nz*ddS);
332 DirectPh(partId, emtPos, pathL, forb);
337 G4double ran = G4UniformRand();
341 attenL = 10.*attenL/0.75;
343 if (pathL>0 &&
exp(-pathL/attenL) > ran)
346 G4double scinSwim=pathL/cvelScint;
354 G4double endTime= timeFlight + ddT + scinSwim + scinTime + transitTime;
372 if (m_t1st[forb]>endTime) m_t1st[forb] = endTime;
373 if (m_tLast[forb]<endTime) m_tLast[forb]= endTime;
381 const G4double kappa = 0.015*cm/MeV;
382 const G4String brMaterial =
"BC408";
388 G4double cor_dE = dE;
390 if(charge!=0.&& dX!=0.)
392 cor_dE = dE/(1+kappa*dE/dX);
412 G4double I1,I2,I3,rp1,rs1,rp2,rs2,Rp1,Rs1,Rp2,Rs2,Rp,Rs;
425 I3=asin(
sin(I1)*(
n1/n3));
430 Rp=(Rp1+Rp2-2*Rp1*Rp2)/(1-Rp1*Rp2);
431 Rs=(Rs1+Rs2-2*Rs1*Rs2)/(1-Rs1*Rs2);
440 G4double I1,I2,rp,rs,Rp,Rs;
460 const static G4double silicon_oil_index = 1.43;
461 const static G4double glass_index = 1.532;
464 G4double cos_span=1-
cos( asin(silicon_oil_index/m_refIndex) );
469 dcos=cos_span*(ran*2.0-1.0);
471 G4double r2=sqrt(emtPos.x()*emtPos.x()+emtPos.y()*emtPos.y());
473 G4double costheta,sintheta;
474 G4double theta,thetaR;
475 costheta=dcos>0?(1-dcos):(1+dcos);
476 theta=acos(costheta);
478 thetaR=asin(1/m_refIndex);
481 G4double ratio1Mean=(CLHEP::pi)*25.5*25.5/(57.1+60.7)/25.0;
482 G4double ratio2Mean=(19.5/25.5)*(19.5/25.5);
483 G4double ratio1=G4RandGauss::shoot(ratio1Mean,0.015);
484 G4double ratio2=G4RandGauss::shoot(ratio2Mean,0.015);
485 G4double ratio3Mean=0.945*ratio1Mean*ratio2Mean;
486 G4double ratio3=G4RandGauss::shoot(ratio3Mean,0.015);
488 G4double R2=1-
Reflectivity(m_refIndex,silicon_oil_index, glass_index, theta);
489 if (dcos > 0 && dcos != 1)
493 if (G4UniformRand()<ratio1)
495 if (G4UniformRand()<R2)
497 if (G4UniformRand()<ratio2)
500 pathL=(m_scinLength/2-emtPos.z())/costheta;
505 if (G4UniformRand()<ratio3)
508 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
514 if (G4UniformRand()<R1)
516 G4double tempran=G4UniformRand();
520 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
522 else if (tempran>ratio1&&G4UniformRand()<R1&&G4UniformRand()<ratio3)
525 pathL=(2.5*m_scinLength-emtPos.z())/costheta;
532 if (G4UniformRand()<ratio1)
534 if (G4UniformRand()<R2)
536 if (G4UniformRand()<ratio2)
539 pathL=(m_scinLength/2-emtPos.z())/costheta;
544 if (G4UniformRand()<ratio3)
547 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
553 G4double tempran=G4UniformRand();
557 pathL=(1.5*m_scinLength-emtPos.z())/costheta;
559 else if (tempran>ratio1 && G4UniformRand()<ratio3)
562 pathL=(2.5*m_scinLength-emtPos.z())/costheta;
567 else if (dcos < 0 && dcos != -1)
571 if (G4UniformRand()<ratio1)
573 if (G4UniformRand()<R2)
575 if (G4UniformRand()<ratio2)
578 pathL=(m_scinLength/2+emtPos.z())/costheta;
583 if (G4UniformRand()<ratio3)
586 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
592 if (G4UniformRand()<R1)
594 G4double tempran=G4UniformRand();
598 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
600 else if (tempran>ratio1&&G4UniformRand()<R1&&G4UniformRand()<ratio3)
603 pathL=(2.5*m_scinLength+emtPos.z())/costheta;
610 if (G4UniformRand()<ratio1)
612 if (G4UniformRand()<R2)
614 if (G4UniformRand()<ratio2)
617 pathL=(m_scinLength/2+emtPos.z())/costheta;
622 if (G4UniformRand()<ratio3)
625 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
631 G4double tempran=G4UniformRand();
635 pathL=(1.5*m_scinLength+emtPos.z())/costheta;
637 else if (tempran>ratio1 && G4UniformRand()<ratio3)
640 pathL=(2.5*m_scinLength+emtPos.z())/costheta;
646 G4double convFactor=180./3.1415926;
647 if (theta>asin(1)-thetaR)
649 G4double
alpha = G4UniformRand()*asin(1.);
650 G4int nRef1 = pathL*sintheta*
cos(
alpha)/50.0+0.5;
651 G4int nRef2 = pathL*sintheta*
sin(
alpha)/58.9+0.5;
652 G4double beta1=acos(sintheta*
cos(
alpha));
653 G4double beta2=acos(sintheta*
sin(
alpha));
654 beta2 -= 3.75/convFactor;
658 for (G4int i=0;i<nRef1;i++)
660 if (G4UniformRand()<(1-R21) && G4UniformRand()<0.15)
663 for (G4int i=0;i<nRef2;i++)
665 if (G4UniformRand()<(1-R22) && G4UniformRand()<0.15)
695 G4double tmp_tauRatio,tmp_tau1,tmp_tau2,tmp_tau3;
696 tmp_tauRatio = m_tauRatio;
700 G4double UniformR = tmp_tauRatio/(1+tmp_tauRatio);
701 G4double EmissionTime;
702 if (G4UniformRand()>UniformR) {
704 EmissionTime = -tmp_tau2*log( G4UniformRand() );
705 if (G4UniformRand()-
exp(EmissionTime/tmp_tau2-EmissionTime/tmp_tau1)>1.E-8)
709 else EmissionTime = -tmp_tau3*log( G4UniformRand() );
743 return G4RandGauss::shoot(m_ttsMean,m_ttsSigma);
749 ihst=G4int(endTime/m_timeBinSize);
752 m_nPhot[ihst][forb]=m_nPhot[ihst][forb]+1;
753 m_totalPhot[forb]=m_totalPhot[forb]+1;
768 G4double norma_const;
769 G4double echarge=1.6e-7;
782 norma_const = sqrt(CLHEP::pi)*tau*tau*tau/4.0;
783 t = (i+1)*m_timeBinSize;
784 snpe[i][0] = m_Ce*
t*
t*
exp(- (
t/tau) * (
t/tau) )/norma_const;
790 norma_const = sqrt(CLHEP::pi)*tau*tau*tau/4.0;
791 t = (i+1)*m_timeBinSize;
792 snpe[i][1] = m_Ce*
t*
t*
exp(- (
t/tau) * (
t/tau) )/norma_const;
797 G4double pmtGain0,pmtGain,relativeGain,smearPMTgain;
798 G4double smearADC[2] = {0.,0.};
799 G4int runId = m_RealizationSvc->
getRunId();
815 G4double timeSmear = G4RandGauss::shoot(0,0.020);
816 if (runId>=-22913 && runId<=-20448) {
817 timeSmear = G4RandGauss::shoot(0,0.040);
820 for (G4int j=0; j<fb; j++)
822 if (m_totalPhot[j] > 0)
824 n1=G4int(m_t1st[j]/m_timeBinSize);
825 n2=G4int(m_tLast[j]/m_timeBinSize);
833 for (G4int i=
n1;i<
n2;i++)
839 Npoisson = G4Poisson(10.0);
840 if(Npoisson>0.)
break;
846 pmtGain = pmtGain0*relativeGain;
847 smearPMTgain = G4RandGauss::shoot(pmtGain,pmtGain/sqrt(Npoisson));
849 if(smearPMTgain>0)
break;
851 smearADC[j] += phtn*smearPMTgain;
857 profPmt[ii][j] += smearPMTgain*phtn*snpe[ihst][j];
868 profPmt[i][j] = m_preGain*profPmt[i][j]+G4RandGauss::shoot(0,m_noiseSigma);
875 if (profPmt[i][j]>max)
884 G4double tmp_HLthresh, tmp_LLthresh, adcFactor;
887 if(runId>=-80000 && runId<=-9484)
891 tmp_HLthresh = m_HLthresh;
892 tmp_LLthresh = m_LLthresh;
916 if (max>=tmp_HLthresh)
920 if ( profPmt[i][j] >= tmp_LLthresh )
922 m_TDC[j] = i*m_timeBinSize + timeSmear +G4RandGauss::shoot(0,0.025);
927 double x = m_preGain*smearADC[j]*echarge*ratio;
938 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()