15#include "G4DigiManager.hh"
16#include "Randomize.hh"
25#include "GaudiKernel/ISvcLocator.h"
26#include "GaudiKernel/Bootstrap.h"
27#include "GaudiKernel/IDataProviderSvc.h"
37 for(G4int i=0; i<43;i++){
38 layerEff.push_back(1.);
40 collectionName.push_back(
"BesMdcDigisCollection");
48 StatusCode sc=Gaudi::svcLocator()->service(
"G4Svc", tmpSvc);
50 G4cout <<
" MdcDigitizer::Error,could not open G4Svc"<<G4endl;
52 m_G4Svc=
dynamic_cast<G4Svc *
>(tmpSvc);
57 G4cout <<
" MdcDigitizer::Error,could not open Mdc Tunning Service"<<G4endl;
59 G4cout<<
" MdcDigitizer:: Open Mdc Tunning Service"<<G4endl;
64 f=
new TFile(noiseFile.c_str());
65 h1=(TH1F*)f->Get(
"h703");
66 h2=(TH1F*)f->Get(
"h501");
67 h3=(TH1F*)f->Get(
"h801");
94 for(G4int i=0; i<43;i++){
105 for(G4int i=0; i<43;i++){
106 for(G4int j=0;j<288;j++){
107 digiPointer[i][j]=-1;
111 G4int
NHits,layerId, cellId, posFlag;
112 G4double edep,driftD,driftT, globalT, theta,cosTheta,enterAngle;
113 G4double mean,sigma,mean1,mean2,sigma1,
sigma2, f,sig,delSig, fRandom, driftDNew, driftTNew;
115 G4double resLargest,resSmallest,resRatio;
117 G4DigiManager* DigiMan = G4DigiManager::GetDMpointer();
121 THCID = DigiMan->GetHitsCollectionID(
"BesMdcHitsCollection");
129 (moduleName, collectionName[0]);
130 NHits=THC->entries();
131 for(G4int i=0;i<
NHits;i++){
132 layerId = (*THC)[i]->GetLayerNo();
133 cellId = (*THC)[i]->GetCellNo();
134 edep = (*THC)[i]->GetEdep();
135 driftD = (*THC)[i]->GetDriftD();
136 globalT = (*THC)[i]->GetGlobalT();
137 theta = (*THC)[i]->GetTheta();
138 cosTheta =
cos(theta);
139 enterAngle = (*THC)[i]->GetEnterAngle();
140 posFlag = (*THC)[i]->GetPosFlag();
148 tempEff=mdcTunningSvc->
GetEff(layerId,cellId,driftD,cosTheta,posFlag);
150 tempEff = layerEff[layerId];
152 fRandom=G4UniformRand();
153 if(fRandom>tempEff)
continue;
159 }
else if(smearFlag==1){
162 mdcTunningSvc->
GetRes3(layerId,cellId,driftD,cosTheta,posFlag,enterAngle,f,mean1,sigma1,mean2,
sigma2,resLargest,resSmallest,resRatio);
167 driftDNew = Smear(driftD-(f*mean1+(1-f)*mean2),f,mean1,sigma1,mean2,
sigma2,resLargest,resSmallest,resRatio);
170 }
else if(smearFlag==2){
171 driftDNew = Smear(driftD);
173 G4cerr<<
"MdcDigitizer::worong smearFlag: "<<smearFlag<<G4endl;
177 driftTNew = mdcCalPointer->
D2T(driftDNew);
183 driftTNew += mdcCalPointer->
GetT0();
186 driftTNew += globalT;
192 if(isnan(driftTNew)){
193 G4cout<<
"MdcDigitizer::error, driftT is nan"<<G4endl;
220 G4int NbDigis = digisCollection->insert(newDigi);
221 digiPointer[layerId][cellId]=NbDigis-1;
222 (*THC)[i]->SetDigiId(NbDigis-1);
225 if(noiseFlag==1)AddNoise();
227 ifstream readNoiseLevel(
"$MDCSIMROOT/share/noiselevel.txt");
228 if(!readNoiseLevel.good()){
229 std::cout<<
" Error , noiselevel file not exist "<<std::endl;
231 std::cout<<
" MdcDigitizer:: Open noiselevel file "<<std::endl;
235 for(G4int i=0;i<NLayer;i++){
236 readNoiseLevel>>level;
237 mixLevel.push_back(level);
242 if (verboseLevel>0) {
243 G4cout <<
"\n-------->digis Collection: in this event they are "
244 << digisCollection->entries()
245 <<
" digis in the MDC chambers: " << G4endl;
246 digisCollection->PrintAllDigi();
248 StoreDigiCollection(digisCollection);
253G4double BesMdcDigitizer::Smear(G4double driftD){
254 G4double r, driftDNew;
255 r = G4RandGauss::shoot();
256 driftDNew = driftD + mdcDRes * r;
258 r = G4RandGauss::shoot();
259 driftDNew = driftD + mdcDRes * r;
264G4double BesMdcDigitizer::Smear(G4double driftD,G4double sigma,G4double mean){
265 G4double r, driftDNew;
266 r = G4RandGauss::shoot();
267 driftDNew = driftD + sigma * r+mean;
268 while(driftDNew <= 0){
269 r = G4RandGauss::shoot();
270 driftDNew = driftD + sigma * r+mean;
275G4double BesMdcDigitizer::Smear(G4double driftD,G4double f,G4double mean1,G4double sigma1,G4double mean2,G4double
sigma2){
276 G4double r, driftDNew,mean,sigma;
286 r = G4RandGauss::shoot();
287 driftDNew = driftD + sigma * r+mean;
288 while(driftDNew <= 0){
289 r = G4RandGauss::shoot();
290 driftDNew = driftD + sigma * r+mean;
292 if(times>10)driftDNew=0.01;
297G4double BesMdcDigitizer::Smear(G4double driftD,G4double f,G4double mean1,G4double sigma1,G4double mean2,G4double
sigma2,G4double resLargest,G4double resSmallest,G4double resRatio){
298 G4double r, driftDNew,mean,sigma;
299 G4double ratio,tempd;
300 ratio=G4UniformRand();
314 r = G4RandGauss::shoot();
315 driftDNew = driftD + sigma * r+mean;
319 tempd=G4UniformRand()*2.0+resLargest;
321 driftDNew = driftD + tempd;
323 while(driftDNew <= 0){
324 r = G4RandGauss::shoot();
325 driftDNew = driftD + sigma * r+mean;
327 if(times>10)driftDNew=0.01;
332void BesMdcDigitizer::AddNoise2(
void){
338 for(G4int i=0;i<NLayer;i++){
340 for(G4int j=0;j<
wireNo;j++){
341 random=G4UniformRand();
342 if(random < mixLevel[i]){
343 randomT=G4UniformRand() * 2000;
345 G4cout<<
"MdcDigitizer: error, randomT is nan"<<G4endl;
350 if(digiPointer[i][j]!=-1){
351 G4int pointer=digiPointer[i][j];
352 G4double preDriftT=(*digisCollection)[pointer]->GetDriftT();
353 if(preDriftT <= randomT)
continue;
354 (*digisCollection)[pointer]->SetDriftT(randomT);
355 (*digisCollection)[pointer]->SetTrackID(-1);
363 digisCollection->insert(newDigi);
371void BesMdcDigitizer::AddNoise(
void){
373 vector<G4double> noise;
377 for(G4int i=0;i<NLayer;i++){
378 noise.push_back(noiseLevel);
380 }
else if(noiseType==1){
382 for(G4int i=0;i<NLayer;i++){
384 noise.push_back(noiseLevel * r0 / r);
386 }
else if(noiseType==2){
388 for(G4int i=0;i<NLayer;i++){
390 noise.push_back(noiseLevel * r0 * r0 / r / r);
392 }
else if(noiseType==3){
393 Int_t Nbins=(Int_t)h1->GetNbinsX();
395 double xmax=h1->GetXaxis()->GetXmax();
396 double xmin=h1->GetXaxis()->GetXmin();
397 double dx=(xmax-xmin)/Nbins;
399 for(G4int i=0;i<Nbins;i++){
400 double x=double(i+1)*dx;
401 y=(G4double)h1->GetBinContent(x);
402 y=y*noiseLevel/0.05608559;
412 for(G4int i=0;i<NLayer;i++){
414 for(G4int j=0;j<
wireNo;j++){
415 random=G4UniformRand();
416 if(random < noise[i]){
418 randomT=h2->GetRandom()+T0;
423 G4cout<<
"MdcDigitizer: error, randomT is nan"<<G4endl;
428 if(digiPointer[i][j]!=-1){
429 G4int pointer=digiPointer[i][j];
430 G4double signalEdep=(*digisCollection)[pointer]->GetEdep();
431 (*digisCollection)[pointer]->SetEdep(randomQ+signalEdep);
432 G4double preDriftT=(*digisCollection)[pointer]->GetDriftT();
433 if(preDriftT <= randomT)
continue;
434 (*digisCollection)[pointer]->SetDriftT(randomT);
435 (*digisCollection)[pointer]->SetTrackID(-1);
443 digisCollection->insert(newDigi);
double cos(const BesAngle a)
G4TDigiCollection< BesMdcDigi > BesMdcDigisCollection
G4THitsCollection< BesMdcHit > BesMdcHitsCollection
void NHits(const AList< TMLink > &links, unsigned nHits[43])
double D2T(double driftDNew)
void SetHitPointer(BesMdcHit *hit)
void SetEdep(G4double de)
void SetCellNo(G4int cell)
void SetDriftT(G4double time)
void SetTrackID(G4int track)
void SetLayerNo(G4int layer)
BesMdcDigitizer(G4String modName)
void SetEff(G4int layer, G4double eff)
static BesMdcGeoParameter * GetGeo(void)
const BesMdcLayer & SignalLayer(int) const
std::string GetMdcNoiseFile()
double GetBeamStartTime()
double GetEff(int layerId, int cellId, double driftD, double cosTheta, int posFlag)
double GetRes3(int layerId, int cellId, double driftD, double cosTheta, int posFlag, double entranceAngle, double &f, double &mean1, double &sigma1, double &mean2, double &sigma2, double &ResLargest, double &ResSmallest, double &ResRatio)