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