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