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