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