5#include "GaudiKernel/Bootstrap.h"
14#include "Randomize.hh"
24const int BesMucNoise::m_kSegment[m_kPart] = {4, 8, 4};
25const int BesMucNoise::m_kAbsorber[m_kPart] = {9, 9, 9};
26const int BesMucNoise::m_kGap[m_kPart] = {8, 9, 8};
27const int BesMucNoise::m_kPanel[m_kPart] = {4, 3, 4};
40 {G4Exception(
"BesMucNoise constructed twice.");}
47 if( m_ptrIdTr != NULL )
delete m_ptrIdTr;
86 for(G4int part=0;part<3;part++){
87 for(G4int seg=0;seg<8;seg++){
88 for(G4int gap=0;gap<9;gap++){
89 m_noise[part][seg][gap]=1;
94 G4cout<<
"filename: "<<filename<<G4endl;
95 std::ifstream fin(filename);
97 G4cout<<
"error opening muc_noise data"<<G4endl;
100 fin.getline(buffer,200,
'\n');
101 std::istringstream stringBuf(buffer);
104 G4int tot_NoDaughter = logicalMuc->GetNoDaughters();
106 for(G4int i=0; i<tot_NoDaughter;i++){
107 G4LogicalVolume* i_LogicalGap = logicalMuc->GetDaughter(i)->GetLogicalVolume();
108 G4String i_GapName = i_LogicalGap->GetName();
110 if(i_GapName.find(
"G")==8){
111 G4LogicalVolume* i_LogicalBox = i_LogicalGap->GetDaughter(0)->GetLogicalVolume();
112 G4LogicalVolume* i_LogicalStripPlane = i_LogicalBox->GetDaughter(0)->GetLogicalVolume();
114 G4String strPart = i_GapName.substr(5,1);
115 G4String strSeg = i_GapName.substr(7,1);
116 G4String strGap = i_GapName.substr(9,1);
118 std::istrstream partBuf(strPart.c_str(), strlen(strPart.c_str()));
119 std::istrstream segBuf(strSeg.c_str(), strlen(strSeg.c_str()));
120 std::istrstream gapBuf(strGap.c_str(), strlen(strGap.c_str()));
128 G4int tot_NoStrip = i_LogicalStripPlane->GetNoDaughters();
129 area[part][seg][gap][0]=tot_NoStrip;
132 G4LogicalVolume* i_LogicalStrip1 = i_LogicalStripPlane->GetDaughter(1)->GetLogicalVolume();
133 G4LogicalVolume* i_LogicalStrip2 = i_LogicalStripPlane->GetDaughter(2)->GetLogicalVolume();
134 G4Box *temp1; G4Box *temp2;
136 temp1=(G4Box *)i_LogicalStrip1->GetSolid();temp2=(G4Box *)i_LogicalStrip2->GetSolid();
137 G4float Width1 =temp1->GetXHalfLength()*2;G4float Width2 =temp2->GetXHalfLength()*2;
138 G4float pos1 =i_LogicalStripPlane->GetDaughter(1)->GetObjectTranslation().x();
139 G4float pos2 =i_LogicalStripPlane->GetDaughter(2)->GetObjectTranslation().x();
140 if ( (part == 1 && gap%2 != 0) || (part != 1 && gap%2 == 0) ) {
141 Width1=temp1->GetYHalfLength()*2; Width2 =temp2->GetYHalfLength()*2;
142 pos1 =i_LogicalStripPlane->GetDaughter(1)->GetObjectTranslation().y();
143 pos2 =i_LogicalStripPlane->GetDaughter(2)->GetObjectTranslation().y();
145 G4float width_between_strip=pos2-pos1-Width1/2-Width2/2;
148 for(G4int j=0;j<tot_NoStrip;j++){
149 G4LogicalVolume* i_LogicalStrip = i_LogicalStripPlane->GetDaughter(j)->GetLogicalVolume();
151 temp=(G4Box *)i_LogicalStrip->GetSolid();
152 G4float
Width =temp->GetXHalfLength()*2;
153 G4float Length=temp->GetYHalfLength()*2;
154 if ( (part == 1 && gap%2 != 0) || (part != 1 && gap%2 == 0) ) {
155 Width =temp->GetYHalfLength()*2;
156 Length=temp->GetXHalfLength()*2;
159 if(j==0||j==(tot_NoStrip-1))
Width=
Width+width_between_strip/2;
161 G4float Strip_Area=fabs(
Width*Length);
162 tot_Area=tot_Area+Strip_Area;
163 area[part][seg][gap][j+1]=tot_Area;
164 strip_area[part][seg][gap][j] = Strip_Area;
169 box_area[part][seg][gap] = tot_Area;
171 for(G4int k=1;k<tot_NoStrip+1;k++){
172 area[part][seg][gap][k]=area[part][seg][gap][k]/tot_Area;
192 ISvcLocator* svcLocator = Gaudi::svcLocator();
193 StatusCode sc = svcLocator->service(
"MucCalibConstSvc", m_ptrCalibSvc,
true);
195 if( sc != StatusCode::SUCCESS){
196 G4cout<<
"Can not use MucCalibConstSvc!" << G4endl;
205 for(
int i=0; i<NHIT; i++) {
206 m_Prob[i][0] = m_Prob[i][1] = 0.;
210 double EU = TMath::Power(TMath::Exp(1.0), -m_HitMean);
211 for(
int i=0; i<NHIT; i++)
213 m_Prob[i][0] = EU*TMath::Power(m_HitMean, i)/TMath::Factorial(i);
223 if( model == 1 ) noiseNum =
NoiseByCnt(aMucHitCollection, aMucHitList);
233 m_noiseLevel = m_ptrCalibSvc->
getLevel();
235 for(
int i = 0; i < m_kPart; i++) {
236 for(
int j = 0; j < m_kSegment[i]; j++) {
237 for(
int k = 0; k < m_kGap[i]; k++) {
238 if(m_noiseLevel == 2)
241 for(G4int ii=0;ii<hitNum;ii++)
246 bool noiseHitExist =
false;
247 noiseHitExist =
IsExist(noiseHit, MucHitList);
248 MucHitCollection->insert(noiseHit);
250 MucHitList->insert(noiseHit2);
254 else delete noiseHit2;
258 if(m_noiseLevel == 3)
261 for(
int strip = 0; strip < stripNum; strip++)
263 G4int hitNum =
NoiseSampling( m_noiseLevel, i, j, k, strip );
267 bool noiseHitExist =
false;
268 noiseHitExist =
IsExist(noiseHit, MucHitList);
269 MucHitCollection->insert(noiseHit);
271 MucHitList->insert(noiseHit2);
275 else delete noiseHit2;
292 random = G4UniformRand();
293 for(
int i=0; i<20; i++) {
294 if(random<m_Prob[i][1]) {noiseNum = i;
break;}
298 int prt, seg, lay, str, tmp_strip;
299 G4float nosRatio = 0.;
301 for(
int i=0; i<noiseNum; i++)
304 random = G4UniformRand();
305 tmp_strip = TMath::Nint(random*
STRIP_MAX);
308 random = G4UniformRand();
309 if( random<nosRatio )
break;
317 bool noiseHitExist =
false;
318 noiseHitExist =
IsExist(noiseHit, MucHitList);
319 MucHitCollection->insert(noiseHit);
320 if(!noiseHitExist) MucHitList->insert(noiseHit2);
321 else delete noiseHit2;
329 bool isExist =
false;
330 G4int n_hit = aMucHitList->entries();
331 for(G4int iNoise=0;iNoise<n_hit;iNoise++)
333 if ( aNoiseHit->
GetTrackIndex()%1000 == (*aMucHitList)[iNoise]->GetTrackIndex()%1000 &&
334 aNoiseHit->
GetPart() == (*aMucHitList)[iNoise]->GetPart() &&
335 aNoiseHit->
GetSeg() == (*aMucHitList)[iNoise]->GetSeg() &&
336 aNoiseHit->
GetGap() == (*aMucHitList)[iNoise]->GetGap() &&
337 aNoiseHit->
GetStrip() == (*aMucHitList)[iNoise]->GetStrip() )
353 G4double e=2.71828182845904590;
356 lambda = m_ptrCalibSvc->
getBoxCnt(
prt,seg,lay) * box_area[
prt][seg][lay] * 1E-2 * 1E-9;
358 lambda = m_ptrCalibSvc->
getStripCnt(
prt,seg,lay,strip) * strip_area[
prt][seg][lay][strip] * 1E-2 * 1E-9;
363 G4float random=G4UniformRand();
366 prob=prob+pow(e,-lambda*
t)*pow(lambda*
t,hitNum)/
Factorial(hitNum);
367 if(random<prob)
break;
378 if(i==0||i==1)
return 1;
380 for(G4int ii=2;ii<=i;ii++){
390 G4float random=(rand()%100000)/100000.0;
392 G4float width=area[part][seg][gap][3]-area[part][seg][gap][2];
393 stripno=G4int((random-area[part][seg][gap][1])/width)+2;
394 if(stripno<1)stripno=1;
395 if(stripno>111)stripno=111;
409 max=area[part][seg][gap][0];
415 if(random>area[part][seg][gap][mid]){
419 else if(random<area[part][seg][gap][mid-1]){
420 if(random<area[part][seg][gap][1]){
430 }
while(pass==0&&(
max>mid&&mid>
min));
443 if(area[part][seg][gap][stripno]!=0){
445 if(random<=area[part][seg][gap][stripno]&&random>area[part][seg][gap][stripno-1])
return 0;
446 if(random<=area[part][seg][gap][stripno-1])
return -1;
447 if(random>area[part][seg][gap][stripno])
return 1;
G4THitsCollection< BesMucHit > BesMucHitsCollection
unsigned Width(const AList< TMLink > &list)
returns width(wire cell unit) of given AList<TMLink>. This function assumes that all TMLink's are in ...
G4int AddNoise(int model, BesMucHitsCollection *MucHitCollection, BesMucHitsCollection *MucHitList)
bool IsExist(BesMucHit *aNoiseHit, BesMucHitsCollection *aMucHitList)
G4int NoiseByNosRatio(BesMucHitsCollection *MucHitCollection, BesMucHitsCollection *MucHitList)
static BesMucNoise * Instance(void)
G4int IsNearestStrip(G4int, G4int, G4int, G4int, G4float)
G4float Factorial(G4int i)
G4int NoiseByCnt(BesMucHitsCollection *MucHitCollection, BesMucHitsCollection *MucHitList)
G4int NoiseSampling(int level, int prt, int seg, int lay, int strip)
G4int GetStripNo(G4int, G4int, G4int)
void Initialize(G4String filename, G4LogicalVolume *logicalMuc)
virtual double getStripCnt(int part, int segment, int layer, int strip) const =0
virtual double getBoxCnt(int part, int segment, int layer) const =0
virtual double getNosRatio(int part, int segment, int layer, int strip) const =0
virtual int getLevel() const =0