11#include "BesMdcDigitizer.hh"
12#include "BesMdcDigitizerMessenger.hh"
13#include "BesMdcHit.hh"
15#include "G4DigiManager.hh"
16#include "Randomize.hh"
23#include "G4Svc/IG4Svc.h"
24#include "G4Svc/G4Svc.h"
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;
224 if(noiseFlag==1)AddNoise();
226 ifstream readNoiseLevel(
"$MDCSIMROOT/share/noiselevel.txt");
227 if(!readNoiseLevel.good()){
228 std::cout<<
" Error , noiselevel file not exist "<<std::endl;
230 std::cout<<
" MdcDigitizer:: Open noiselevel file "<<std::endl;
234 for(G4int i=0;i<NLayer;i++){
235 readNoiseLevel>>level;
236 mixLevel.push_back(level);
241 if (verboseLevel>0) {
242 G4cout <<
"\n-------->digis Collection: in this event they are "
243 << digisCollection->entries()
244 <<
" digis in the MDC chambers: " << G4endl;
245 digisCollection->PrintAllDigi();
247 StoreDigiCollection(digisCollection);
252G4double BesMdcDigitizer::Smear(G4double driftD){
253 G4double r, driftDNew;
254 r = G4RandGauss::shoot();
255 driftDNew = driftD + mdcDRes * r;
257 r = G4RandGauss::shoot();
258 driftDNew = driftD + mdcDRes * r;
263G4double BesMdcDigitizer::Smear(G4double driftD,G4double sigma,G4double mean){
264 G4double r, driftDNew;
265 r = G4RandGauss::shoot();
266 driftDNew = driftD + sigma * r+mean;
267 while(driftDNew <= 0){
268 r = G4RandGauss::shoot();
269 driftDNew = driftD + sigma * r+mean;
274G4double BesMdcDigitizer::Smear(G4double driftD,G4double f,G4double mean1,G4double sigma1,G4double mean2,G4double sigma2){
275 G4double r, driftDNew,mean,sigma;
285 r = G4RandGauss::shoot();
286 driftDNew = driftD + sigma * r+mean;
287 while(driftDNew <= 0){
288 r = G4RandGauss::shoot();
289 driftDNew = driftD + sigma * r+mean;
291 if(times>10)driftDNew=0.01;
296G4double BesMdcDigitizer::Smear(G4double driftD,G4double f,G4double mean1,G4double sigma1,G4double mean2,G4double sigma2,G4double resLargest,G4double resSmallest,G4double resRatio){
297 G4double r, driftDNew,mean,sigma;
298 G4double ratio,tempd;
299 ratio=G4UniformRand();
313 r = G4RandGauss::shoot();
314 driftDNew = driftD + sigma * r+mean;
318 tempd=G4UniformRand()*2.0+resLargest;
320 driftDNew = driftD + tempd;
322 while(driftDNew <= 0){
323 r = G4RandGauss::shoot();
324 driftDNew = driftD + sigma * r+mean;
326 if(times>10)driftDNew=0.01;
331void BesMdcDigitizer::AddNoise2(
void){
337 for(G4int i=0;i<NLayer;i++){
339 for(G4int j=0;j<
wireNo;j++){
340 random=G4UniformRand();
341 if(random < mixLevel[i]){
342 randomT=G4UniformRand() * 2000;
344 G4cout<<
"MdcDigitizer: error, randomT is nan"<<G4endl;
349 if(digiPointer[i][j]!=-1){
350 G4int pointer=digiPointer[i][j];
351 G4double preDriftT=(*digisCollection)[pointer]->GetDriftT();
352 if(preDriftT <= randomT)
continue;
353 (*digisCollection)[pointer]->SetDriftT(randomT);
354 (*digisCollection)[pointer]->SetTrackID(-1);
362 digisCollection->insert(newDigi);
370void BesMdcDigitizer::AddNoise(
void){
372 vector<G4double> noise;
376 for(G4int i=0;i<NLayer;i++){
377 noise.push_back(noiseLevel);
379 }
else if(noiseType==1){
381 for(G4int i=0;i<NLayer;i++){
383 noise.push_back(noiseLevel * r0 / r);
385 }
else if(noiseType==2){
387 for(G4int i=0;i<NLayer;i++){
389 noise.push_back(noiseLevel * r0 * r0 / r / r);
391 }
else if(noiseType==3){
392 Int_t Nbins=(Int_t)h1->GetNbinsX();
394 double xmax=h1->GetXaxis()->GetXmax();
395 double xmin=h1->GetXaxis()->GetXmin();
396 double dx=(xmax-xmin)/Nbins;
398 for(G4int i=0;i<Nbins;i++){
399 double x=double(i+1)*dx;
400 y=(G4double)h1->GetBinContent(x);
401 y=y*noiseLevel/0.05608559;
411 for(G4int i=0;i<NLayer;i++){
413 for(G4int j=0;j<
wireNo;j++){
414 random=G4UniformRand();
415 if(random < noise[i]){
417 randomT=h2->GetRandom()+T0;
422 G4cout<<
"MdcDigitizer: error, randomT is nan"<<G4endl;
427 if(digiPointer[i][j]!=-1){
428 G4int pointer=digiPointer[i][j];
429 G4double signalEdep=(*digisCollection)[pointer]->GetEdep();
430 (*digisCollection)[pointer]->SetEdep(randomQ+signalEdep);
431 G4double preDriftT=(*digisCollection)[pointer]->GetDriftT();
432 if(preDriftT <= randomT)
continue;
433 (*digisCollection)[pointer]->SetDriftT(randomT);
434 (*digisCollection)[pointer]->SetTrackID(-1);
442 digisCollection->insert(newDigi);
double cos(const BesAngle a)
G4TDigiCollection< BesMdcDigi > BesMdcDigisCollection
G4THitsCollection< BesMdcHit > BesMdcHitsCollection
void NHits(const AList< TMLink > &links, unsigned nHits[50])
returns # of hits per layer.
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)