BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
BesMdcDigitizer.cc
Go to the documentation of this file.
1//---------------------------------------------------------------------------//
2// BOOST --- BESIII Object_Oriented Simulation Tool //
3//---------------------------------------------------------------------------//
4//Description:
5//Author: Yuan Ye([email protected])
6//Created: Oct. 26, 2004
7//Modified:
8//Comment:
9//---------------------------------------------------------------------------//
10
11#include "BesMdcDigitizer.hh"
12#include "BesMdcDigitizerMessenger.hh"
13#include "BesMdcHit.hh"
14
15#include "G4DigiManager.hh"
16#include "Randomize.hh"
17#include "G4ios.hh"
18#include <string>
19
20#include "TFile.h"
21#include "TH1F.h"
22
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"
28
29BesMdcDigitizer::BesMdcDigitizer(G4String modName):G4VDigitizerModule(modName){
30 noiseFlag=0;
31 noiseType=3;
32 noiseLevel=0.1;//10%
33 maxNoiseT=300.;//ns
34 smearFlag=1;
35 mdcDRes = 0.13; //mm
36 effFlag = 0;
37 for(G4int i=0; i<43;i++){
38 layerEff.push_back(1.);
39 }
40 collectionName.push_back("BesMdcDigisCollection");
41 digitizerMessenger = new BesMdcDigitizerMessenger(this);
42 mdcGeoPointer=BesMdcGeoParameter::GetGeo();
43 mdcCalPointer=new BesMdcCalTransfer;
44
45 // ISvcLocator* svcLocator = Gaudi::svcLocator();
46 IG4Svc* tmpSvc;
47 //G4Svc* m_G4Svc;
48 StatusCode sc=Gaudi::svcLocator()->service("G4Svc", tmpSvc);
49 if (!sc.isSuccess())
50 G4cout <<" MdcDigitizer::Error,could not open G4Svc"<<G4endl;
51
52 m_G4Svc=dynamic_cast<G4Svc *>(tmpSvc);
53
55 sc= Gaudi::svcLocator()->service("MdcTunningSvc",IMdcTunningSvc);
56 if (!sc.isSuccess()){
57 G4cout <<" MdcDigitizer::Error,could not open Mdc Tunning Service"<<G4endl;
58 }else{
59 G4cout<<" MdcDigitizer:: Open Mdc Tunning Service"<<G4endl;
60 }
61 mdcTunningSvc=dynamic_cast<MdcTunningSvc *>(IMdcTunningSvc);
62
63 std::string noiseFile=m_G4Svc->GetMdcNoiseFile();
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");
68 /*
69 //get Mdc Ntuple from G4Svc
70 if(m_G4Svc->MdcRootFlag())
71 {
72 m_tupleMdc = m_G4Svc->GetTupleMdc();
73 sc = m_tupleMdc->addItem("NHits",m_NHits);
74 sc = m_tupleMdc->addItem("LayerId",m_layerId);
75 sc = m_tupleMdc->addItem("cellId",m_cellId);
76 sc = m_tupleMdc->addItem("Edep",m_edep);
77 sc = m_tupleMdc->addItem("driftD",m_driftD);
78 // sc = m_tupleMdc->addItem("driftT",m_driftT);
79 sc = m_tupleMdc->addItem("globalT",m_globalT);
80 sc = m_tupleMdc->addItem("theta",m_theta);
81 sc = m_tupleMdc->addItem("enterAngle",m_enterAngle);
82 sc = m_tupleMdc->addItem("driftDNew",m_driftDNew);
83 sc = m_tupleMdc->addItem("driftTNew",m_driftTNew);
84 // sc = m_tupleMdc->addItem("adc",m_adc);
85 // sc = m_tupleMdc->addItem("tdc",m_tdc);
86 }
87 */
88}
89
90BesMdcDigitizer::~BesMdcDigitizer(){delete digitizerMessenger;}
91
92void BesMdcDigitizer::SetEff(G4int layer, G4double eff){
93 if(layer==-1){
94 for(G4int i=0; i<43;i++){
95 layerEff[i]=eff;
96 }
97 }else{
98 layerEff[layer]=eff;
99 }
100}
101
103
104 //initialize
105 for(G4int i=0; i<43;i++){
106 for(G4int j=0;j<288;j++){
107 digiPointer[i][j]=-1;
108 }
109 }
110
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;
114 G4double tempEff;
115 G4double resLargest,resSmallest,resRatio;//added by liukai
116
117 G4DigiManager* DigiMan = G4DigiManager::GetDMpointer();
118
119 //hits collection ID
120 G4int THCID=-1;
121 THCID = DigiMan->GetHitsCollectionID("BesMdcHitsCollection");
122
123 //hits collection
124 BesMdcHitsCollection* THC = 0;
125 THC = (BesMdcHitsCollection*) (DigiMan->GetHitsCollection(THCID));
126
127 if(THC){
128 digisCollection=new BesMdcDigisCollection
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();
141
142 //Transfer hit pointer to BesMdcCalTransfer
143 mdcCalPointer->SetHitPointer((*THC)[i]);
144
145 //Filter with wire efficiency
146 if(effFlag==0){
147 //tempEff = mdcCalPointer->GetEff();
148 tempEff=mdcTunningSvc->GetEff(layerId,cellId,driftD,cosTheta,posFlag);
149 }else{
150 tempEff = layerEff[layerId];
151 }
152 fRandom=G4UniformRand();
153 if(fRandom>tempEff)continue;
154
155 //cout<<"layerid "<<layerId<<" cellid "<<cellId<<" theta "<<cosTheta<<" enterangle "<<enterAngle<<endl;
156 //Drift distance smear
157 if(smearFlag==0){ //No smear
158 driftDNew = driftD;
159 }else if(smearFlag==1){ //Smear from TuningSvc
160 // mdcTunningSvc->GetRes(layerId,cellId,driftD,cosTheta,posFlag,enterAngle,mean,sigma);
161 //mdcTunningSvc->GetRes2(layerId,cellId,driftD,cosTheta,posFlag,enterAngle,f,mean1,sigma1,mean2,sigma2);
162 mdcTunningSvc->GetRes3(layerId,cellId,driftD,cosTheta,posFlag,enterAngle,f,mean1,sigma1,mean2,sigma2,resLargest,resSmallest,resRatio);
163
164 //driftDNew = Smear(driftD,f,mean1,sigma1,mean2,sigma2);
165 //driftDNew = Smear(driftD-(f*mean1+(1-f)*mean2),f,mean1,sigma1,mean2,sigma2);//new method
166
167 driftDNew = Smear(driftD-(f*mean1+(1-f)*mean2),f,mean1,sigma1,mean2,sigma2,resLargest,resSmallest,resRatio);//----added by liukai 2012-6-4
168
169
170 }else if(smearFlag==2){ //Smear with fixed resolution
171 driftDNew = Smear(driftD);
172 }else{
173 G4cerr<<"MdcDigitizer::worong smearFlag: "<<smearFlag<<G4endl;
174 }
175
176 //Do X-T conversion
177 driftTNew = mdcCalPointer->D2T(driftDNew);
178
179 //Do Q-T correct
180 driftTNew += mdcCalPointer->GetTimeWalk();
181
182 //Add T0
183 driftTNew += mdcCalPointer->GetT0();
184
185 //Add TOF
186 driftTNew += globalT;
187
188 //Signal transfer time on wire
189 // transferT=Transfer(layerId,cellId,hitPosition);
190 //driftTNew+=transferT;
191
192 if(isnan(driftTNew)){
193 G4cout<<"MdcDigitizer::error, driftT is nan"<<G4endl;
194 continue;
195 }
196
197 /*
198 if(m_G4Svc->MdcRootFlag())
199 {
200 m_NHits= NHits;
201 m_layerId= layerId;
202 m_cellId= cellId;
203 m_edep= edep;
204 m_driftD= driftD;
205 // m_driftT= driftT;
206 m_globalT = globalT;
207 m_enterAngle = enterAngle;
208 m_driftDNew = driftDNew;
209 m_driftTNew = driftTNew;
210 m_theta = theta;
211 m_tupleMdc ->write();
212 }
213 */
214 BesMdcDigi* newDigi = new BesMdcDigi();
215 newDigi->SetTrackID((*THC)[i]->GetTrackID());
216 newDigi->SetLayerNo(layerId);
217 newDigi->SetCellNo(cellId);
218 newDigi->SetEdep(edep);
219 newDigi->SetDriftT(driftTNew);
220 G4int NbDigis = digisCollection->insert(newDigi);
221 digiPointer[layerId][cellId]=NbDigis-1;
222 }
223
224 if(noiseFlag==1)AddNoise();
225 if(noiseFlag==2){
226 ifstream readNoiseLevel("$MDCSIMROOT/share/noiselevel.txt");
227 if(!readNoiseLevel.good()){
228 std::cout<<" Error , noiselevel file not exist "<<std::endl;
229 }else{
230 std::cout<<" MdcDigitizer:: Open noiselevel file "<<std::endl;
231 }
232 G4int NLayer=mdcGeoPointer->SignalLayerNo();
233 G4double level;
234 for(G4int i=0;i<NLayer;i++){
235 readNoiseLevel>>level;
236 mixLevel.push_back(level);
237 }
238 AddNoise2();
239 }
240
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();
246 }
247 StoreDigiCollection(digisCollection);
248 }
249
250}
251
252G4double BesMdcDigitizer::Smear(G4double driftD){
253 G4double r, driftDNew;
254 r = G4RandGauss::shoot();
255 driftDNew = driftD + mdcDRes * r;
256 while(driftDNew<=0){
257 r = G4RandGauss::shoot();
258 driftDNew = driftD + mdcDRes * r;
259 }
260 return driftDNew;
261}
262
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;
270 }
271 return driftDNew;
272}
273
274G4double BesMdcDigitizer::Smear(G4double driftD,G4double f,G4double mean1,G4double sigma1,G4double mean2,G4double sigma2){
275 G4double r, driftDNew,mean,sigma;
276 r = G4UniformRand();
277 if(r<=f){
278 mean=mean1;
279 sigma=sigma1;
280 }else{
281 mean=mean2;
282 sigma=sigma2;
283 }
284 int times=0;
285 r = G4RandGauss::shoot();
286 driftDNew = driftD + sigma * r+mean;
287 while(driftDNew <= 0){
288 r = G4RandGauss::shoot();
289 driftDNew = driftD + sigma * r+mean;
290 times++;
291 if(times>10)driftDNew=0.01;
292 }
293 return driftDNew;
294}
295
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();
300 int times;
301 if(ratio<=resRatio)
302 {
303 //for hitOnTrk distribution
304 r = G4UniformRand();
305 if(r<=f){
306 mean=mean1;
307 sigma=sigma1;
308 }else{
309 mean=mean2;
310 sigma=sigma2;
311 }
312 times=0;
313 r = G4RandGauss::shoot();
314 driftDNew = driftD + sigma * r+mean;
315 }
316 else//for hitNotOnTrk
317 {
318 tempd=G4UniformRand()*2.0+resLargest;
319 times=0;
320 driftDNew = driftD + tempd;
321 }
322 while(driftDNew <= 0){
323 r = G4RandGauss::shoot();
324 driftDNew = driftD + sigma * r+mean;
325 times++;
326 if(times>10)driftDNew=0.01;
327 }
328 return driftDNew;
329}
330
331void BesMdcDigitizer::AddNoise2(void){
332 G4int wireNo;
333 G4double random;
334 G4double randomT;
335 G4double randomQ;
336 G4int NLayer=mdcGeoPointer->SignalLayerNo();
337 for(G4int i=0;i<NLayer;i++){
338 wireNo=mdcGeoPointer->SignalLayer(i).WireNo()/2;
339 for(G4int j=0;j<wireNo;j++){
340 random=G4UniformRand();
341 if(random < mixLevel[i]){
342 randomT=G4UniformRand() * 2000;
343 if(isnan(randomT)){
344 G4cout<<"MdcDigitizer: error, randomT is nan"<<G4endl;
345 continue;
346 }
347
348 randomQ=200.;
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);
355 }else{
356 BesMdcDigi* newDigi = new BesMdcDigi();
357 newDigi->SetTrackID(-1);
358 newDigi->SetLayerNo(i);
359 newDigi->SetCellNo(j);
360 newDigi->SetEdep(randomQ);
361 newDigi->SetDriftT(randomT);
362 digisCollection->insert(newDigi);
363 }
364 }
365 }
366 }
367}
368
369
370void BesMdcDigitizer::AddNoise(void){
371 G4double r0,r;
372 vector<G4double> noise; //Noise level of each layer
373
374 G4int NLayer=mdcGeoPointer->SignalLayerNo();
375 if(noiseType==0){
376 for(G4int i=0;i<NLayer;i++){
377 noise.push_back(noiseLevel);
378 }
379 }else if(noiseType==1){
380 r0=mdcGeoPointer->SignalLayer(0).R();
381 for(G4int i=0;i<NLayer;i++){
382 r=mdcGeoPointer->SignalLayer(i).R();
383 noise.push_back(noiseLevel * r0 / r);
384 }
385 }else if(noiseType==2){
386 r0=mdcGeoPointer->SignalLayer(0).R();
387 for(G4int i=0;i<NLayer;i++){
388 r=mdcGeoPointer->SignalLayer(i).R();
389 noise.push_back(noiseLevel * r0 * r0 / r / r);
390 }
391 }else if(noiseType==3){ // liugc add 22:11 4/14/06
392 Int_t Nbins=(Int_t)h1->GetNbinsX();
393
394 double xmax=h1->GetXaxis()->GetXmax();
395 double xmin=h1->GetXaxis()->GetXmin();
396 double dx=(xmax-xmin)/Nbins;
397 G4double y;
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;// Normalize use noise level of 1st layer in "run23096noise.root"
402 noise.push_back(y);
403 }
404 }
405
406 G4int wireNo;
407 G4double random;
408 G4double randomT;
409 G4double randomQ;
410 G4double T0=m_G4Svc->GetBeamStartTime();
411 for(G4int i=0;i<NLayer;i++){
412 wireNo=mdcGeoPointer->SignalLayer(i).WireNo()/2;
413 for(G4int j=0;j<wireNo;j++){
414 random=G4UniformRand();
415 if(random < noise[i]){
416 //randomT=G4UniformRand() * maxNoiseT;
417 randomT=h2->GetRandom()+T0;
418 // randomT=randomT;
419 // randomQ=h3->GetRandom();
420 // randomQ=randomQ*0.001*0.001; //Transfer from TDC channels to Mev, coef. is temporary one.
421 if(isnan(randomT)){
422 G4cout<<"MdcDigitizer: error, randomT is nan"<<G4endl;
423 continue;
424 }
425
426 randomQ=0.;
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);
435 }else{
436 BesMdcDigi* newDigi = new BesMdcDigi();
437 newDigi->SetTrackID(-1);
438 newDigi->SetLayerNo(i);
439 newDigi->SetCellNo(j);
440 newDigi->SetEdep(randomQ);
441 newDigi->SetDriftT(randomT);
442 digisCollection->insert(newDigi);
443 }
444 }
445 }
446 }
447}
Double_t x[10]
const int wireNo
double cos(const BesAngle a)
G4TDigiCollection< BesMdcDigi > BesMdcDigisCollection
G4THitsCollection< BesMdcHit > BesMdcHitsCollection
double D2T(double driftDNew)
void SetHitPointer(BesMdcHit *hit)
double GetTimeWalk(void)
virtual void Digitize()
BesMdcDigitizer(G4String modName)
void SetEff(G4int layer, G4double eff)
static BesMdcGeoParameter * GetGeo(void)
const BesMdcLayer & SignalLayer(int) const
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)