BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
BesMucNoise.cc
Go to the documentation of this file.
1//
2//
3//
4//
5#include "GaudiKernel/Bootstrap.h"
6
7#include "BesMucNoise.hh"
8#include "G4Trap.hh"
9#include "G4VSolid.hh"
10#include <iostream>
11#include <fstream>
12#include <sstream>
13#include "ReadBoostRoot.hh"
14#include "Randomize.hh"
15#include "math.h"
16#include "strstream"
17#include "G4Box.hh"
18#include "stdlib.h"
19
20
21using namespace std;
22
23//const int m_kPart = 3;
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};
28//const int m_kGasChamber = 2;
29
30BesMucNoise * BesMucNoise::fPointer=0;
32 if(!fPointer)fPointer = new BesMucNoise();
33 return fPointer;
34}
35
36
38{
39 if(fPointer)
40 {G4Exception("BesMucNoise constructed twice.");}
41 fPointer=this;
42
43}
44
46{
47 if( m_ptrIdTr != NULL ) delete m_ptrIdTr;
48
49}
50
51void BesMucNoise::Initialize(G4String filename,G4LogicalVolume* logicalMuc,G4String temp)
52{
53 //m_noiseLevel = 2; // set to 2 temporary. In future, expected to get from cal servise,
54 //like m_noiseLevel = m_ptrCalibSvc->getNoiseLevel();
55/*
56 m_noiseLevel = m_ptrCalibSvc->getLevel();
57
58 if(m_noiseLevel!=2 && m_noiseLevel!=3){
59 G4cout<<"ERROR, In BesMucNoise, noise level should be 2 or 3 ..."<<G4endl;
60 m_noiseLevel = 2;
61 }
62
63 for(int i = 0; i < m_kPart; i++) {
64 for(int j = 0; j < m_kSegment[i]; j++) {
65 for(int k = 0; k < m_kGap[i]; k++) {
66 m_noise[i][j][k] = m_ptrCalibSvc->getBoxCnt(i,j,k);
67 int stripNum = 0;
68 if(i != 1) stripNum = 64; //endcap;
69 else{
70 if(k%2 == 0) stripNum = 48;
71 if(k%2 == 1 && j != 2) stripNum = 96;
72 if(k%2 == 1 && j == 2) stripNum = 112;
73 }
74
75 for(int strip = 0; strip < stripNum; strip++){
76 m_noise_strip[i][j][k][strip] = m_ptrCalibSvc->getStripCnt(i,j,k,strip);
77 }
78 }
79 }
80 }
81 */
82}
83
84void BesMucNoise::Initialize(G4String filename,G4LogicalVolume* logicalMuc)
85{
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; //init -1
90 }
91 }
92 }
93 //read noise from file
94 G4cout<<"filename: "<<filename<<G4endl;
95 std::ifstream fin(filename);
96 if(!fin){
97 G4cout<<"error opening muc_noise data"<<G4endl;
98 }
99 char buffer[200];
100 fin.getline(buffer,200,'\n'); //get info whether add noise or not!
101 std::istringstream stringBuf(buffer);
102
103 //get strip area from logicalMuc!
104 G4int tot_NoDaughter = logicalMuc->GetNoDaughters();
105 //G4cout<<"---in noise::Init()--- "<<tot_NoDaughter<<G4endl;
106 for(G4int i=0; i<tot_NoDaughter;i++){
107 G4LogicalVolume* i_LogicalGap = logicalMuc->GetDaughter(i)->GetLogicalVolume();
108 G4String i_GapName = i_LogicalGap->GetName();
109
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();
113 //G4cout<<"---in noise::Init()--- "<<i<<" "<<i_GapName<<" "<<i_LogicalStripPlane->GetDaughter(1)->GetLogicalVolume()->GetName()<<G4endl;
114 G4String strPart = i_GapName.substr(5,1);
115 G4String strSeg = i_GapName.substr(7,1);
116 G4String strGap = i_GapName.substr(9,1);
117
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()));
121
122 G4int part,seg,gap;
123
124 partBuf >> part;
125 segBuf >> seg;
126 gapBuf >> gap;
127 //G4cout<<"-------in noise::Init()---- "<<part<<" "<<seg<<" "<<gap<<" "<<i_LogicalStripPlane->GetNoDaughters()<<G4endl;
128 G4int tot_NoStrip = i_LogicalStripPlane->GetNoDaughters();
129 area[part][seg][gap][0]=tot_NoStrip; //the first element is the total strip number
130 G4float tot_Area=0;
131 // get width between two strip!
132 G4LogicalVolume* i_LogicalStrip1 = i_LogicalStripPlane->GetDaughter(1)->GetLogicalVolume();
133 G4LogicalVolume* i_LogicalStrip2 = i_LogicalStripPlane->GetDaughter(2)->GetLogicalVolume();
134 G4Box *temp1; G4Box *temp2;
135
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();
144 }
145 G4float width_between_strip=pos2-pos1-Width1/2-Width2/2;
146 // G4cout<<"-----what I want---"<<width_between_strip<<" "<<pos2<<" "<<pos1<<" "<<Width1/2<<" "<<Width2/2<<G4endl;
147
148 for(G4int j=0;j<tot_NoStrip;j++){
149 G4LogicalVolume* i_LogicalStrip = i_LogicalStripPlane->GetDaughter(j)->GetLogicalVolume();
150 G4Box *temp;
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;
157 } //end if
158 // G4cout<<"----in noise::init()---- "<<i_LogicalStrip->GetName()<<" "<<Width<<" "<<Length<<G4endl;
159 if(j==0||j==(tot_NoStrip-1)) Width=Width+width_between_strip/2;
160 else Width=Width+width_between_strip;
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;
165 //G4cout<<" id: ( "<<part<<" "<<seg<<" "<<gap<<") "<<Strip_Area<<" , w: "<<Width<<" L: "<<Length<<G4endl;
166 } //end for(j)
167 // unitary
168
169 box_area[part][seg][gap] = tot_Area;
170
171 for(G4int k=1;k<tot_NoStrip+1;k++){
172 area[part][seg][gap][k]=area[part][seg][gap][k]/tot_Area;
173 // G4cout<<"----in noise::init---"<<area[part][seg][gap][k]<<G4endl;
174 } //end for(k)
175
176 } //end if(gap)
177 } //end for(i)
178
179 // G4cout<<"---in noise::init()---area--"<<G4endl;
180 //for(G4int kk=0;kk<96;kk++){
181 // G4cout<<area[1][0][0][kk+1]-area[1][0][0][kk]<<" ,";
182 // }
183
184 // init MucIdTransform and const pointer
185 m_ptrIdTr = new MucIdTransform();
187 InitProb();
188}
189
191{
192 ISvcLocator* svcLocator = Gaudi::svcLocator();
193 StatusCode sc = svcLocator->service("MucCalibConstSvc", m_ptrCalibSvc, true);
194
195 if( sc != StatusCode::SUCCESS){
196 G4cout<< "Can not use MucCalibConstSvc!" << G4endl;
197 }
198}
199
201{
202 m_HitMean = 0.08;
203 int NHIT = 20;
204
205 for( int i=0; i<NHIT; i++) {
206 m_Prob[i][0] = m_Prob[i][1] = 0.;
207 }
208
209 double sum = 0.;
210 double EU = TMath::Power(TMath::Exp(1.0), -m_HitMean);
211 for( int i=0; i<NHIT; i++)
212 {
213 m_Prob[i][0] = EU*TMath::Power(m_HitMean, i)/TMath::Factorial(i);
214 sum += m_Prob[i][0];
215 m_Prob[i][1] = sum;
216 //G4cout << i << "\tProb:\t" << m_Prob[i][0] << "\tSumProb:\t" << m_Prob[i][1] << G4endl;
217 }
218}
219
220G4int BesMucNoise::AddNoise(int model, BesMucHitsCollection* aMucHitCollection,BesMucHitsCollection* aMucHitList)
221{
222 G4int noiseNum = 0;
223 if( model == 1 ) noiseNum = NoiseByCnt(aMucHitCollection, aMucHitList);
224 else noiseNum = NoiseByNosRatio(aMucHitCollection, aMucHitList);
225 //G4cout << "Noise hit(s) produced:\t" << noiseNum << "\tby model:\t" << model << G4endl;
226 return noiseNum;
227
228}
229
231{
232 G4int noiseNum = 0;
233 m_noiseLevel = m_ptrCalibSvc->getLevel();
234
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)
239 {
240 G4int hitNum = NoiseSampling( m_noiseLevel, i, j, k, 0 );
241 for(G4int ii=0;ii<hitNum;ii++)
242 {
243 G4int strip = GetStripNo(i,j,k);
244 BesMucHit *noiseHit=new BesMucHit(i, j, k, strip, -1, -1);
245 BesMucHit *noiseHit2=new BesMucHit(i, j, k, strip, -1, -1);
246 bool noiseHitExist = false;
247 noiseHitExist = IsExist(noiseHit, MucHitList);
248 MucHitCollection->insert(noiseHit);
249 if(!noiseHitExist) {
250 MucHitList->insert(noiseHit2);
251 noiseNum += 1;
252 //G4cout<<"-------insert------- "<<" "<<noiseHit2->GetStrip()<<" "<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<m<<G4endl;
253 }
254 else delete noiseHit2;
255 } // hitNum
256 } //box level
257
258 if(m_noiseLevel == 3)
259 {
260 int stripNum = m_ptrIdTr->GetStripMax(i, j, k);
261 for(int strip = 0; strip < stripNum; strip++)
262 {
263 G4int hitNum = NoiseSampling( m_noiseLevel, i, j, k, strip );
264 if(hitNum > 0){
265 BesMucHit *noiseHit=new BesMucHit(i, j, k, strip, -1, -1);
266 BesMucHit *noiseHit2=new BesMucHit(i, j, k, strip, -1, -1);
267 bool noiseHitExist = false;
268 noiseHitExist = IsExist(noiseHit, MucHitList);
269 MucHitCollection->insert(noiseHit);
270 if(!noiseHitExist) {
271 MucHitList->insert(noiseHit2);
272 noiseNum += 1;
273 //G4cout<<"-------insert------- "<<" "<<noiseHit2->GetStrip()<<" "<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<m<<G4endl;
274 }
275 else delete noiseHit2;
276 }
277 } // stripNum
278 } // strip level
279 } // part
280 } // segment
281 } // layer
282
283 return noiseNum;
284}
285
287{
288 G4int noiseNum = 0;
289 G4float random = 0;
290
291 //sample noise hit number;
292 random = G4UniformRand();
293 for(int i=0; i<20; i++) {
294 if(random<m_Prob[i][1]) {noiseNum = i; break;}
295 }
296 //G4cout << "Random for noiseNum:\t" << random << "\tProbSum:\t" << m_Prob[noiseNum][1] << "\t" << noiseNum << G4endl;
297
298 int prt, seg, lay, str, tmp_strip;
299 G4float nosRatio = 0.;
300
301 for(int i=0; i<noiseNum; i++)
302 {
303 do{
304 random = G4UniformRand();
305 tmp_strip = TMath::Nint(random*STRIP_MAX); //9152
306 m_ptrIdTr->SetStripPos(tmp_strip, &prt, &seg, &lay, &str);
307 nosRatio = m_ptrCalibSvc->getNosRatio(prt, seg, lay, str);
308 random = G4UniformRand();
309 if( random<nosRatio ) break;
310 }while( 1 );
311
312 //G4cout<<"Strip:\t"<<prt<<"\t"<<seg<<"\t"<<lay<<"\t"<<str<<"\t"<<random<<" "<<nosRatio<<G4endl;
313
314 BesMucHit *noiseHit = new BesMucHit(prt, seg, lay, str, -1, -1);
315 BesMucHit *noiseHit2 = new BesMucHit(prt, seg, lay, str, -1, -1);
316
317 bool noiseHitExist = false;
318 noiseHitExist = IsExist(noiseHit, MucHitList);
319 MucHitCollection->insert(noiseHit);
320 if(!noiseHitExist) MucHitList->insert(noiseHit2);
321 else delete noiseHit2;
322 } // noiseNum;
323
324 return noiseNum;
325}
326
328{
329 bool isExist = false;
330 G4int n_hit = aMucHitList->entries();
331 for(G4int iNoise=0;iNoise<n_hit;iNoise++)
332 {
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() )
338 {
339 isExist = true;
340 break;
341 }
342 }
343
344 return isExist;
345}
346
347G4int BesMucNoise::NoiseSampling(int level, int prt, int seg, int lay, int strip)
348{
349 G4int hitNum = 0;
350 G4double lambda;
351 //must get the time of this event
352 G4double t=800; //800ns temporary!
353 G4double e=2.71828182845904590;
354
355 if( level == 2 )
356 lambda = m_ptrCalibSvc->getBoxCnt(prt,seg,lay) * box_area[prt][seg][lay] * 1E-2 * 1E-9; //1E-2:mm2->cm2 1E-9:ns
357 else if( level == 3)
358 lambda = m_ptrCalibSvc->getStripCnt(prt,seg,lay,strip) * strip_area[prt][seg][lay][strip] * 1E-2 * 1E-9; //1E-2:mm2->cm2 1E-9:ns
359 else
360 lambda = 0.;
361
362 //sample noise hit number;
363 G4float random=G4UniformRand(); //***use other random
364 G4double prob = 0;
365 do{
366 prob=prob+pow(e,-lambda*t)*pow(lambda*t,hitNum)/Factorial(hitNum);
367 if(random<prob) break;
368 hitNum++;
369 }while(1);
370
371 // G4cout<<" hitNum: "<<hitNum<<G4endl;
372 return hitNum;
373}
374
376{
377 G4float fact=1;
378 if(i==0||i==1)return 1;
379 else{
380 for(G4int ii=2;ii<=i;ii++){
381 fact=fact*ii;}
382 return fact;
383 }
384
385}
386
387G4int BesMucNoise::GetStripNo(G4int part,G4int seg,G4int gap)
388{ G4int stripno;
389 //G4float random=G4UniformRand();//***use other random
390 G4float random=(rand()%100000)/100000.0;
391 if(part==1){ //berrel
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;
396 // G4cout<<"---in noise::GetStripNo()---stripno= "<<stripno<<" "<<(random-area[part][seg][gap][1])/width<<G4endl;
397 G4int step = IsNearestStrip(stripno,part,seg,gap,random);
398 while(step!=0) {
399 // G4cout<<"---in noise::GetStripNo()---while "<<G4endl;
400 stripno += step;
401 step = IsNearestStrip(stripno,part,seg,gap,random);
402 }//endl while
403 stripno--; //the first element is total strip number
404 return stripno;
405 }
406 else{ //endcap
407 G4int max,min,mid,pass;
408 min=1;
409 max=area[part][seg][gap][0];
410 mid=G4int((min+max)/2);
411 //G4cout<<"---in noise first---"<<min<<" "<<mid<<" "<<max<<G4endl;
412 do{
413 // G4cout<<"-----in noise---"<<random<<" "<<area[part][seg][gap][min]<<" "<<area[part][seg][gap][mid]<<" "<<area[part][seg][gap][max]<<G4endl;
414 pass=0;
415 if(random>area[part][seg][gap][mid]){
416 min=mid;
417 mid=G4int((min+max)/2);
418 }
419 else if(random<area[part][seg][gap][mid-1]){
420 if(random<area[part][seg][gap][1]){
421 pass=1; mid=1;max=1;
422 }else{
423 max=mid-1;
424 mid=G4int((min+max)/2);
425 }
426 }else{pass=1;}
427
428 if(min==mid)mid=max;
429 // G4cout<<"-----in noise---"<<min<<" "<<mid<<" "<<max<<G4endl;
430 }while(pass==0&&(max>mid&&mid>min));
431 //G4cout<<"-----in noise--- "<<mid-1<<G4endl;
432 return mid-1;
433
434 }
435
436}
437
438G4int BesMucNoise::IsNearestStrip(G4int stripno,G4int part,G4int seg,G4int gap,G4float random)
439{
440 if(stripno==1){
441 return 0;
442 }else{
443 if(area[part][seg][gap][stripno]!=0){
444 // G4cout<<"--in noise::IsNearestStrip()-- "<<area[part][seg][gap][stripno]<<" "<<area[part][seg][gap][stripno-1]<<" "<<random<<G4endl;
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;
448 }else{
449 return -1;
450 }
451 } //end else
452}
G4THitsCollection< BesMucHit > BesMucHitsCollection
#define prt(n)
TTree * t
Definition: binning.cxx:23
G4int AddNoise(int model, BesMucHitsCollection *MucHitCollection, BesMucHitsCollection *MucHitList)
Definition: BesMucNoise.cc:220
bool IsExist(BesMucHit *aNoiseHit, BesMucHitsCollection *aMucHitList)
Definition: BesMucNoise.cc:327
void CheckCalibSvc()
Definition: BesMucNoise.cc:190
G4int NoiseByNosRatio(BesMucHitsCollection *MucHitCollection, BesMucHitsCollection *MucHitList)
Definition: BesMucNoise.cc:286
static BesMucNoise * Instance(void)
Definition: BesMucNoise.cc:31
G4int IsNearestStrip(G4int, G4int, G4int, G4int, G4float)
Definition: BesMucNoise.cc:438
G4float Factorial(G4int i)
Definition: BesMucNoise.cc:375
void InitProb()
Definition: BesMucNoise.cc:200
G4int NoiseByCnt(BesMucHitsCollection *MucHitCollection, BesMucHitsCollection *MucHitList)
Definition: BesMucNoise.cc:230
G4int NoiseSampling(int level, int prt, int seg, int lay, int strip)
Definition: BesMucNoise.cc:347
G4int GetStripNo(G4int, G4int, G4int)
Definition: BesMucNoise.cc:387
void Initialize(G4String filename, G4LogicalVolume *logicalMuc)
Definition: BesMucNoise.cc:84
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
int GetStripMax(int part, int segment, int layer)
bool SetStripPos(int stripid, int *part, int *segment, int *layer, int *subid)