BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
BesSim.cc
Go to the documentation of this file.
1#include "BesSim.hh"
4#include "BesEventAction.hh"
5#include "BesRunAction.hh"
8#include "GenSim/BesPrimaryGeneratorAction.hh"
9#include "MdcSim/BesMdcGeoParameter.hh"
10#include "PhySim/BesPhysicsList.hh"
11#include "PhySim/BgsPhysicsList.hh" //caogf
12#include "SimUtil/ReadBoostRoot.hh"
13#include "TruSim/BesSensitiveManager.hh"
14// #include "BesVisManager.hh"
15// #include "G4VisManager.hh"
16#include "FTFP_BERT.hh"
17#include "FTFP_BERT_ATL.hh"
18#include "FTFP_BERT_HP.hh"
19#include "FTFP_BERT_TRV.hh"
20#include "FTFQGSP_BERT.hh"
21#include "FTF_BIC.hh"
22#include "QBBC.hh"
23#include "QGSP_BERT.hh"
24#include "QGSP_BERT_HP.hh"
25#include "QGSP_BIC.hh"
26#include "QGSP_BIC_AllHP.hh"
27#include "QGSP_BIC_HP.hh"
28#include "QGSP_FTFP_BERT.hh"
29#include "QGS_BIC.hh"
30
31#include "G4Run.hh"
32#include "G4RunManager.hh"
33
34#include "GaudiKernel/MsgStream.h"
35#include "GaudiKernel/NTuple.h"
36#include "GaudiKernel/SmartIF.h"
37// #include "GaudiKernel/Property.h"
38// #include "G4Svc/G4Svc.h"
39#include "BesRndmGenSvc/IBesRndmGenSvc.h"
40#include "G4Svc/IG4Svc.h"
41
42#include "G4Writer/G4GDMLWriter.h"
43// caogf for random seed
44#include "CLHEP/Random/Random.h"
45// sunss for detector version
46#include "DetVerSvc/IDetVerSvc.h"
47
48// static const AlgFactory<BesSim> xxBAFactory;
49// const IAlgFactory& BesSimFactory = xxBAFactory;
50
51/////////////////////////////////////////////////////////////////////////////
53
54BesSim::BesSim( const std::string& name, ISvcLocator* pSvcLocator )
55 : Algorithm( name, pSvcLocator ) {
56 declareProperty( "Mdc", m_mdc = 2 );
57 declareProperty( "Tof", m_tof = 2 );
58 declareProperty( "Emc", m_emc = 2 );
59 declareProperty( "Muc", m_muc = 2 );
60 declareProperty( "PipeSCM", m_pipeSCM = 2 );
61 declareProperty( "Field", m_field = 1 );
62 declareProperty( "PhysicsList", m_physicsList = 0 );
63 declareProperty( "FormatAR", m_formatAR = 0 );
64 declareProperty( "Tuning", m_tuning = 0 );
65 declareProperty( "TuningFile", m_tuningFile );
66 declareProperty( "HitOut", m_hitOut = 0 );
67 declareProperty( "TDSFlag", m_TDSFlag = false );
68 declareProperty( "AsciiFlag", m_asciiFlag = "00000000" );
69 declareProperty( "AsciiFile", m_asciiFile = "boost.dat" );
70 declareProperty( "RootFlag", m_rootFlag = false );
71 declareProperty( "RootFile", m_rootFile = "Hit.root" );
72 declareProperty( "CutValue", newcut = 0.7 ); // unite is mm
73 declareProperty( "LogLevel", m_logLevel = 5000 );
74 declareProperty( "BesGenAction", m_besGenAction = false );
75}
76
77// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
78
79StatusCode BesSim::initialize() {
80
81 MsgStream log( msgSvc(), name() );
82 log << MSG::INFO << "initialize()" << endmsg;
83 m_nEvent = 0;
84 m_nRun = 1;
85
86 // caogf for random seed
87 static const bool CREATEIFNOTTHERE( true );
88 StatusCode RndmStatus = service( "BesRndmGenSvc", p_BesRndmGenSvc, CREATEIFNOTTHERE );
89 if ( !RndmStatus.isSuccess() || 0 == p_BesRndmGenSvc )
90 {
91 log << MSG::ERROR << " Could not initialize Random Number Service" << endmsg;
92 return RndmStatus;
93 }
94
95 StatusCode status;
96 IG4Svc* tmpSvc;
97 status = service( "G4Svc", tmpSvc );
98 if ( status.isSuccess() )
99 {
100 log << MSG::INFO << "got the G4Svc" << endmsg;
101 // m_G4Svc=dynamic_cast<G4Svc *>(tmpSvc);
102 m_G4Svc = tmpSvc;
103 }
104 else
105 {
106 log << MSG::ERROR << "could not get the G4Svc" << endmsg;
107 return StatusCode::FAILURE;
108 }
109
111 StatusCode sc_det = service( "DetVerSvc", detVerSvc );
112 if ( sc_det.isFailure() )
113 {
114 log << MSG::ERROR << "can't retrieve DetVerSvc instance" << endmsg;
115 return sc_det;
116 }
117
118 int phase = detVerSvc->phase();
119 log << MSG::INFO << "** ~~~ZZZ~~~ ** : retrieved DetectorStage = " << phase << endmsg;
120
121 // phase 1: Endcap TOF Scintillator; phase 2: 4 Scintillator==> 2 MRPC; phase 3: Endcap TOF
122 // MRPC
123 if ( phase > 2 ) { m_tof = 4; }
124
125 // book Ntuple
126 if ( m_G4Svc->MdcRootFlag() ) bookMdcRootFile();
127
128 if ( m_G4Svc->TofRootFlag() ) bookTofRootFile();
129
130 if ( m_G4Svc->EmcRootFlag() ) bookEmcRootFile();
131
132 if ( m_G4Svc->MucRootFlag() ) bookMucRootFile();
133
134 log << MSG::INFO << "Four sub-detector construction flags: " << m_mdc << " " << m_tof << " "
135 << m_emc << " " << m_muc << endmsg;
136
137 ReadBoostRoot* readBoost =
138 new ReadBoostRoot( m_mdc, m_tof, m_emc, m_muc, m_field, m_formatAR, m_tuning, m_hitOut );
139 BesMdcGeoParameter* mdcGeoService;
140 mdcGeoService = new BesMdcGeoParameter;
141
142 log << MSG::INFO << "Instantiating BESIII Detector" << endmsg;
143
144 BesSensitiveManager* sensitiveManager = new BesSensitiveManager;
145 sensitiveManager->SetLogLevel( m_G4Svc->LogLevel() );
146
147 BesDetectorConstruction* detectorConstruction = new BesDetectorConstruction();
148 detectorConstruction->SetPipeSCM( m_pipeSCM );
149 m_G4Svc->SetUserInitialization( detectorConstruction );
150
151 switch ( m_physicsList )
152 {
153 case 1: m_G4Svc->SetUserInitialization( new QBBC ); break;
154 case 2: m_G4Svc->SetUserInitialization( new QGS_BIC ); break;
155 case 3: m_G4Svc->SetUserInitialization( new QGSP_BERT ); break;
156 case 4: m_G4Svc->SetUserInitialization( new QGSP_BERT_HP ); break;
157 case 5: m_G4Svc->SetUserInitialization( new QGSP_BIC_AllHP ); break;
158 case 6: m_G4Svc->SetUserInitialization( new QGSP_BIC ); break;
159 case 7: m_G4Svc->SetUserInitialization( new FTF_BIC ); break;
160 case 8: m_G4Svc->SetUserInitialization( new FTFP_BERT_ATL ); break;
161 case 9: m_G4Svc->SetUserInitialization( new FTFP_BERT ); break;
162 case 10: m_G4Svc->SetUserInitialization( new FTFP_BERT_HP ); break;
163 case 11: m_G4Svc->SetUserInitialization( new FTFP_BERT_TRV ); break;
164 case 12: m_G4Svc->SetUserInitialization( new FTFQGSP_BERT ); break;
165 default: m_G4Svc->SetUserInitialization( new FTFP_BERT ); break;
166 }
167
168 BesActionInitializer* actionInit = new BesActionInitializer();
169 actionInit->SetBesGenActionFlag( m_besGenAction );
170 actionInit->SetTDSFlag( m_TDSFlag );
171 actionInit->SetAsciiFlag( m_asciiFlag );
172 actionInit->SetAsciiFile( m_asciiFile );
173 actionInit->SetRootFlag( m_rootFlag );
174 actionInit->SetRootFile( m_rootFile );
175 actionInit->SetTuningFile( m_tuningFile );
176
177 m_G4Svc->SetUserInitialization( actionInit );
178
179 // m_G4Svc->SetUserAction(new BesPrimaryGeneratorAction);
180 // BesRunAction* runAction = new BesRunAction;
181 // runAction->SetTDSFlag(m_TDSFlag);
182 // runAction->SetAsciiFlag(m_asciiFlag);
183 // runAction->SetAsciiFile(m_asciiFile);
184 // runAction->SetRootFlag(m_rootFlag);
185 // runAction->SetRootFile(m_rootFile);
186 // vector<string>::iterator pd;
187 // for (pd = m_tuningFile.begin(); pd != m_tuningFile.end(); pd++){
188 // std::cout << "tuning File: " << *pd << std::endl;
189 // }
190
191 // runAction->SetTuningFile(m_tuningFile);
192 // m_G4Svc->SetUserAction(runAction);
193
194 // BesEventAction* evtAction = new BesEventAction(runAction);
195 // m_G4Svc->SetUserAction(evtAction);
196
197 // m_G4Svc->SetUserAction(new BesSteppingAction);
198
199 // m_G4Svc->SetUserAction(new BesTrackingAction(runAction) );
200
201 // #ifdef G4VIS_USE
202 // visualization manager
203 // G4VisManager* visManager = new BesVisManager;
204 // visManager->Initialize();
205 // #endif
206
207 return StatusCode::SUCCESS;
208}
209
210// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
211
212// StatusCode BesSim::beginRun() {
213StatusCode BesSim::beginRun() {
214 debug() << "In start()" << endmsg;
215 static int flag = -1;
216 if ( flag < 0 )
217 {
218 MsgStream log( msgSvc(), name() );
219 log << MSG::DEBUG << "\t start initializing G4 " << endmsg;
220 // new visMgr, StartUISession(), runMgr->Initialize()
221 m_G4Svc->G4Init();
222
223 // to do: runMgr->RunInitialization()
224 m_G4Svc->RunInitialize();
225
226 // G4VPhysicalVolume* g4wv =G4TransportationManager::GetTransportationManager()->
227 // GetNavigatorForTracking()->GetWorldVolume(); //Apply this for storing TOF GDML
228 // G4GDMLWriter
229 // g4writerBes("/afs/ihep.ac.cn/bes3/offline/sw/packages/BesGDML/2.8.0/GDMLSchema/gdml.xsd",
230 // "Bes.gdml",2); G4GDMLWriter
231 // g4writerMdc("/afs/ihep.ac.cn/bes3/offline/sw/packages/BesGDML/2.8.0/GDMLSchema/gdml.xsd",
232 // "Mdc.gdml",2); G4GDMLWriter
233 // g4writerTof("/afs/ihep.ac.cn/bes3/offline/sw/packages/BesGDML/2.8.0/GDMLSchema/gdml.xsd",
234 // "Tof.gdml",2);//Apply this for storing TOF GDML G4GDMLWriter
235 // g4writerEmc("/afs/ihep.ac.cn/bes3/offline/sw/packages/BesGDML/2.8.0/GDMLSchema/gdml.xsd",
236 // "Emc.gdml",2); G4GDMLWriter
237 // g4writerMuc("/afs/ihep.ac.cn/bes3/offline/sw/packages/BesGDML/2.8.0/GDMLSchema/gdml.xsd",
238 // "Muc.gdml",2); try
239 //{
240 // g4writerBes.DumpGeometryInfo(g4wv);
241 // g4writerTof.DumpGeometryInfo(g4wv->GetLogicalVolume()->GetDaughter(0)); //Changed from 0
242 // to 3//Apply this for storing TOF GDML
243
244 //}
245 // catch(std::logic_error &lerr)
246 //{
247 // std::cout << "Caught an exception: "
248 // << lerr.what () << std::endl;
249 //}
250
251 // For random seed added by caogf. Note the position of the code, otherwise it is not
252 // available.
253 CLHEP::HepRandomEngine* engine = p_BesRndmGenSvc->GetEngine( "SIM" );
254 HepRandom::setTheEngine( engine );
255 HepRandom::showEngineStatus();
256 }
257 return StatusCode::SUCCESS;
258}
259
261 MsgStream log( msgSvc(), name() );
262 NTuplePtr nt( ntupleSvc(), "FILE801/n1" );
263 NTuple::Tuple* tupleMdc;
264 if ( nt ) tupleMdc = nt;
265 else
266 {
267 tupleMdc = ntupleSvc()->book( "FILE801/n1", CLID_ColumnWiseTuple, "BesSim" );
268 m_G4Svc->SetTupleMdc( tupleMdc );
269 }
270 return StatusCode::SUCCESS;
271}
272
273// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
274
276 MsgStream log( msgSvc(), name() );
277 NTuplePtr nt1( ntupleSvc(), "FILE802/n1" );
278 NTuple::Tuple* tupleTof1;
279 if ( nt1 ) tupleTof1 = nt1;
280 else
281 {
282 tupleTof1 = ntupleSvc()->book( "FILE802/n1", CLID_ColumnWiseTuple, "BesSim" );
283 m_G4Svc->SetTupleTof1( tupleTof1 );
284 }
285 NTuplePtr nt2( ntupleSvc(), "FILE802/n2" );
286 NTuple::Tuple* tupleTof2;
287 if ( nt2 ) tupleTof2 = nt2;
288 else
289 {
290 tupleTof2 = ntupleSvc()->book( "FILE802/n2", CLID_ColumnWiseTuple, "BesSim" );
291 m_G4Svc->SetTupleTof2( tupleTof2 );
292 }
293 NTuplePtr nt3( ntupleSvc(), "FILE802/n3" );
294 NTuple::Tuple* tupleTof3;
295 if ( nt3 ) tupleTof3 = nt3;
296 else
297 {
298 tupleTof3 = ntupleSvc()->book( "FILE802/n3", CLID_ColumnWiseTuple, "BesSim" );
299 m_G4Svc->SetTupleTof3( tupleTof3 );
300 }
301 /*
302 NTuplePtr nt4(ntupleSvc(), "FILE802/n4");
303 NTuple::Tuple* tupleTof4;
304 if(nt4) tupleTof4 = nt4;
305 else {
306 tupleTof4 = ntupleSvc()->book("FILE802/n4",CLID_ColumnWiseTuple,"BesSim");
307 m_G4Svc->SetTupleTof4(tupleTof4);
308 }
309 */
310 return StatusCode::SUCCESS;
311}
312
313// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
314
316 NTuplePtr nt1( ntupleSvc(), "FILE803/n1" );
317 NTuple::Tuple* tupleEmc1;
318 if ( nt1 ) tupleEmc1 = nt1;
319 else
320 {
321 tupleEmc1 = ntupleSvc()->book( "FILE803/n1", CLID_ColumnWiseTuple, "BesSim" );
322 m_G4Svc->SetTupleEmc1( tupleEmc1 );
323 }
324
325 NTuplePtr nt2( ntupleSvc(), "FILE803/n2" );
326 NTuple::Tuple* tupleEmc2;
327 if ( nt2 ) tupleEmc2 = nt2;
328 else
329 {
330 tupleEmc2 = ntupleSvc()->book( "FILE803/n2", CLID_ColumnWiseTuple, "BesSim" );
331 m_G4Svc->SetTupleEmc2( tupleEmc2 );
332 }
333
334 return StatusCode::SUCCESS;
335}
336
337// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
338
340 MsgStream log( msgSvc(), name() );
341 NTuplePtr nt( ntupleSvc(), "FILE804/n1" );
342 NTuple::Tuple* tupleMuc;
343 if ( nt ) tupleMuc = nt;
344 else
345 {
346 tupleMuc = ntupleSvc()->book( "FILE804/n1", CLID_ColumnWiseTuple, "BesSim" );
347 m_G4Svc->SetTupleMuc( tupleMuc );
348 }
349 return StatusCode::SUCCESS;
350}
351
352// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
353
354StatusCode BesSim::execute() {
355 if ( !m_beginRun )
356 {
357 StatusCode sc = beginRun();
358 if ( sc.isFailure() )
359 {
360 error() << "beginRun failed" << endmsg;
361 return StatusCode::FAILURE;
362 }
363 m_beginRun = true;
364 }
365
366 MsgStream log( msgSvc(), name() );
367
368 // log << MSG::INFO << "event ID:"<<m_nEvent<<endmsg;
369 if ( m_nEvent % m_logLevel == 0 )
370 std::cout << " BesSim Begin of Event: " << m_nEvent << std::endl;
371
372 m_G4Svc->SimulateEvents( m_nEvent );
373 m_nEvent++;
374
375 return StatusCode::SUCCESS;
376}
377
378// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
379
380StatusCode BesSim::finalize() {
381
382 MsgStream log( msgSvc(), name() );
383 log << MSG::INFO << "finalize()" << endmsg;
384 log << MSG::DEBUG << "\t terminating the current G4 run" << endmsg;
385 m_G4Svc->RunTerminate();
386
387 std::cout << "BesSim::finalize(), total events in this run: " << m_nEvent << std::endl;
388
389 if ( m_G4Svc->runMgr() != 0 ) delete m_G4Svc->runMgr();
390
391#ifdef G4VIS_USE
392 if ( m_G4Svc->visMgr() != 0 ) delete m_G4Svc->visMgr();
393#endif
394
395 return StatusCode::SUCCESS;
396}
DECLARE_COMPONENT(BesBdkRc)
INTupleSvc * ntupleSvc()
IMessageSvc * msgSvc()
IDetVerSvc * detVerSvc
void SetAsciiFile(std::string file)
void SetAsciiFlag(std::string flag)
void SetBesGenActionFlag(G4bool flag)
void SetTDSFlag(G4bool flag)
void SetTuningFile(std::vector< std::string > file)
void SetRootFile(std::string file)
void SetRootFlag(G4bool flag)
StatusCode bookEmcRootFile()
Definition BesSim.cc:315
StatusCode execute()
Definition BesSim.cc:354
StatusCode beginRun()
Definition BesSim.cc:213
StatusCode bookMdcRootFile()
Definition BesSim.cc:260
StatusCode finalize()
Definition BesSim.cc:380
StatusCode bookMucRootFile()
Definition BesSim.cc:339
BesSim(const std::string &name, ISvcLocator *pSvcLocator)
Definition BesSim.cc:54
StatusCode initialize()
Definition BesSim.cc:79
StatusCode bookTofRootFile()
Definition BesSim.cc:275