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