38#include "BesCgemSD.hh"
39#include "ReadBoostRoot.hh"
48#include "G4RunManager.hh"
49#include "G4SDManager.hh"
50#include "G4HCofThisEvent.hh"
52#include "G4ThreeVector.hh"
53#include "G4UnitsTable.hh"
55#include "Randomize.hh"
69#include "GaudiKernel/IMessageSvc.h"
70#include "GaudiKernel/MsgStream.h"
71#include "GaudiKernel/ISvcLocator.h"
72#include "GaudiKernel/Bootstrap.h"
73#include "GaudiKernel/SmartIF.h"
74#include "GaudiKernel/Property.h"
75#include "GaudiKernel/IJobOptionsSvc.h"
88 collectionName.insert(
"BesCgemHitsCollection");
89 collectionName.insert(
"BesCgemTruthCollection");
92 ISvcLocator* iface = Gaudi::svcLocator();
93 SmartIF<IJobOptionsSvc> jos(iface->service(
"JobOptionsSvc"));
94 const std::vector<const Property*>* ppp = jos->getProperties(
"BesCgemSD");
101 for (std::vector<const Property*>::const_iterator it = ppp->begin();
102 it != ppp->end(); ++it) {
103 if ( (*it)->name() ==
"outputFile") {
104 outputFile = (*it)->toString();
109 Gaudi::svcLocator() -> service(
"MessageSvc",
msgSvc);
110 MsgStream log(
msgSvc,
"BesCgemSD::BesCgemSD():BesSensitiveDetector()");
113 if (m_F_outputTruth == 1)
115 log<< MSG::INFO <<
"CgemSim::BesCgemSD::Initialize(), McTruth Hit will be output to a txt file!"
117 <<
"outputFile Path: " << outputFile << endreq;
119 m_outputTruth.open(outputFile.c_str(), ios::out);
121 if (!m_outputTruth.good())
123 log<< MSG::ERROR <<
"CgemSim::BesCgemSD::Initialize(), Fail to open McTruth output file: "
124 << outputFile << endreq;
133 m_outputTruth.close();
166 static G4int HLID=-1;
169 HLID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[1]);
172 G4HCofThisEvent *HCE = f_event->GetHCofThisEvent();
175 Gaudi::svcLocator() -> service(
"MessageSvc",
msgSvc);
176 MsgStream log(
msgSvc,
"BesCgemSD::EndOfTruthEvent()");
180 HCE->AddHitsCollection(HLID, m_collection_truth);
183 if (m_F_printTruth==1 or m_F_outputTruth==1)
186 if (m_F_printTruth == 1)
188 log<< MSG::INFO <<
"-------------------------------------------------------------------------------"
190 log<< MSG::INFO <<
"CgemSim::BesCgemSD::EndOfTruthEvent(), McTruth information: " << endreq;
191 log<< MSG::INFO << left <<
"Track " << left <<
"Layer " << left <<
"PDG "
192 << left << setw(12) <<
"R_Pre "
193 << left << setw(12) <<
"R_Post "
194 << left << setw(40) <<
"pos_Pre "
195 << left << setw(40) <<
"pos_Post "
196 << left << setw(12) <<
"P_Pre "
197 << left << setw(12) <<
"P_Post "
198 << left << setw(16) <<
"StepL "
199 << left << setw(16) <<
"Time "
205 for (G4int ii=0; ii<m_collection_truth->entries(); ii++)
207 BesCgemHit *lv_truth = (*m_collection_truth)[ii];
210 if (m_F_printTruth == 1)
212 G4cout << left << setw( 6) << lv_truth-> GetTrackID ()
213 << left << setw( 6) << lv_truth-> GetLayerID ()
214 << left << setw( 6) << lv_truth-> GetPDGCode ()
215 << left << setw(12) << lv_truth-> GetPositionOfPrePoint ().rho()
216 << left << setw(12) << lv_truth-> GetPositionOfPostPoint ().rho()
217 << left << lv_truth-> GetPositionOfPrePoint()
218 << left << lv_truth-> GetPositionOfPostPoint()
219 << left << setw(12) << lv_truth-> GetMomentumOfPrePoint().mag()
220 << left << setw(12) << lv_truth-> GetMomentumOfPostPoint().mag()
221 << left << setw(16) << lv_truth-> GetStepLength ()
222 << left << setw(16) << lv_truth-> GetGlobalTime ()
223 << left << setw(16) << lv_truth-> GetTotalEnergyDeposit ()
228 if (m_F_outputTruth == 1)
230 m_outputTruth << left << setw( 5) << lv_truth->
GetTrackID()
259 bool printFlag=
false;
262 Gaudi::svcLocator() -> service(
"MessageSvc",
msgSvc);
263 MsgStream log(
msgSvc,
"BesCgemSD::ProcessHits()");
266 G4Track *lv_track = f_step->GetTrack();
270 G4double charge = lv_track->GetDefinition()->GetPDGCharge();
274 log<< MSG::WARNING <<
"CgemSim::BesCgemSD::ProcessHits(), Track is neutral, skip the track" << endreq;
277 G4int lvi_pdg_code = lv_track->GetDefinition()->GetPDGEncoding();
280 G4double lvd_global_time = lv_track->GetGlobalTime();
281 if (isnan(lvd_global_time))
284 log<< MSG::WARNING <<
"CgemSim::BesCgemSD::ProcessHits(), Gloabal time is nan" << endreq;
288 if (lvd_global_time > 2000)
291 log<< MSG::WARNING <<
"CgemSim::BesCgemSD::ProcessHits(), Time window is >2000" << endreq;
304 G4double lvd_L_step = f_step->GetStepLength();
305 if (lvd_L_step == 0.)
308 log<< MSG::WARNING <<
"CgemSim::BesCgemSD::ProcessHits(), Step length is 0, skip track" << endreq;
313 G4StepPoint *lv_step_pre = f_step->GetPreStepPoint();
314 G4StepPoint *lv_step_post = f_step->GetPostStepPoint();
315 G4TrackStatus currentTrackStatus = lv_track->GetTrackStatus();
318 G4int lvi_RdtElectron = 0;
319 G4String process_name=
"Generator";
320 if (NULL != lv_track->GetCreatorProcess()){
321 process_name = lv_track->GetCreatorProcess()->GetProcessName();
322 if (process_name ==
"hIoni" || process_name ==
"ionIoni" || process_name ==
"conv" || process_name ==
"compt" || process_name ==
"muIoni" || process_name ==
"eIoni" || process_name ==
"phot" || process_name ==
"muMinusCaptureAtRest" ) lvi_RdtElectron = 1;
329 if(lvi_RdtElectron)
return false;
347 G4int lvi_ID_track = 0;
348 G4int lvi_ID_truth = 0;
349 G4int lvi_ID_G4Track = 0;
356 lvi_ID_truth = lv_track->GetTrackID();
361 G4int lvi_ID_layer = 0;
363 G4int lvi_ID_parent = lv_track->GetParentID();
364 G4double lvd_E_deposit = f_step->GetTotalEnergyDeposit();
366 G4ThreeVector lv3_XYZ_pre = lv_step_pre ->GetPosition();
367 G4ThreeVector lv3_XYZ_post = lv_step_post->GetPosition();
369 G4ThreeVector lv3_P_pre = lv_step_pre ->GetMomentum();
370 G4ThreeVector lv3_P_post = lv_step_post->GetMomentum();
373 log<< MSG::INFO <<
"..............................................................................." << endreq;
374 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Process a hit at track="
375 << lvi_ID_track << endreq;
383 G4StepStatus lv_status_pre = lv_step_pre ->GetStepStatus();
384 G4StepStatus lv_status_post = lv_step_post->GetStepStatus();
387 const G4VTouchable *lv_TOUCHABLE = lv_step_pre ->GetTouchable();
389 G4String lvs_volume_name = lv_TOUCHABLE ->GetVolume(0)->GetLogicalVolume()->GetName();
396 if (lvs_volume_name ==
"Gap_D_logic0" or lvs_volume_name ==
"Gap_D_logic1" or lvs_volume_name ==
"Gap_D_logic2")
399 lvi_ID_layer = lv_TOUCHABLE->GetVolume(1)->GetCopyNo();
403 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Track enters the SD volume: "
404 << lvs_volume_name << endreq;
411 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Create a hit at layer="
412 << lvi_ID_layer <<
" track=" << lvi_ID_track << endreq;
418 l_new_hit->
SetRdtEl ( lvi_RdtElectron );
434 m_collection_hit->insert(l_new_hit);
437 hit_ID_vector.push_back(lvi_ID_hit);
450 KeyType lv_key(lvi_ID_track, lvi_ID_layer);
452 if (m_collection_truth)
457 m_map_truth_it = m_map_truth.find(lv_key);
458 if (m_map_truth_it == m_map_truth.end())
460 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Create a Truth at layer="
461 << lvi_ID_layer <<
" track=" << lvi_ID_track << endreq;
479 for(
int ii =0; ii < hit_ID_vector.size();ii++){
480 tmp[ii] = hit_ID_vector[ii];
483 hit_ID_vector.clear();
487 m_collection_truth->insert(l_new_truth);
489 m_map_truth[lv_key] = m_collection_truth->entries() - 1;
491 if (lv_step_pre->GetStepStatus() == fGeomBoundary)
493 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Truth starts from boundry!!!"
498 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Truth doesn't start from boundry!!!"
505 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Add a step to Truth at layer="
506 << lvi_ID_layer <<
" track=" << lvi_ID_track << endreq;
508 G4int lvi_ID_truth = (*m_map_truth_it).second;
509 G4int lvi_ID_track_pre = (*m_collection_truth)[lvi_ID_truth]->GetTrackID();
510 G4double lvd_E_pre = (*m_collection_truth)[lvi_ID_truth]->GetTotalEnergyDeposit();
511 G4double lvd_L_step_pre = (*m_collection_truth)[lvi_ID_truth]->GetStepLength();
513 (*m_collection_truth)[lvi_ID_truth]->SetTotalEnergyDeposit(lvd_E_deposit + lvd_E_pre);
514 (*m_collection_truth)[lvi_ID_truth]->SetStepLength(lvd_L_step + lvd_L_step_pre);
515 (*m_collection_truth)[lvi_ID_truth]->SetPositionOfPostPoint(lv3_XYZ_post);
516 (*m_collection_truth)[lvi_ID_truth]->SetMomentumOfPostPoint(lv3_P_post);
522 if (lv_step_post->GetStepStatus() == fGeomBoundary)
524 log<< MSG::INFO <<
"CgemSim::BesCgemSD::ProcessHits(), Finish a Truth at layer="
525 << lvi_ID_layer <<
" track=" << lvi_ID_track << endreq;
530 if(myCgemTruth==NULL)
534 cout<<
"creat a new myCgemTruth, prePos is Boundary: "<<(lv_status_pre==fGeomBoundary)<<
" trkID="<<lvi_ID_track<<
", LayerID="<<lvi_ID_layer<<endl;
548 double dphi=lv3_XYZ_pre.phi()-lv3_XYZ_post.phi();
565 cout<<
"update myCgemTruth: prePos is Boundary: "<<(lv_status_pre==fGeomBoundary)<<
", postPos is Boundary:"<<(lv_status_post==fGeomBoundary)<<
", trkID="<<lvi_ID_track<<
", LayerID="<<lvi_ID_layer<<endl;
567 if( myCgemTruth && (currentTrackStatus==fStopAndKill || lv_status_post==fGeomBoundary) )
571 for(
int ii =0; ii < hit_ID_vector.size();ii++){
572 tmp[ii] = hit_ID_vector[ii];
578 m_collection_truth->insert(myCgemTruth);
582 hit_ID_vector.clear();
586 cout<<
"save a myCgemTruth: prePos is Boundary: "<<(lv_status_pre==fGeomBoundary)<<
", postPos is Boundary:"<<(lv_status_post==fGeomBoundary)<<
", trkID="<<lvi_ID_track<<
", LayerID="<<lvi_ID_layer<<endl;
600 Gaudi::svcLocator() -> service(
"MessageSvc",
msgSvc);
601 MsgStream log(
msgSvc,
"BesCgemSD::EndOfEvent()");
603 bool printFlag=
false;
610 static G4int HCID = -1;
613 HCID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
619 log<< MSG::INFO <<
"-------------------------------------------------------------------------------" << endreq;
620 log<< MSG::INFO <<
"CgemSim::BesCgemSD::EndOfEvent(), Entries of hits collection is "
621 << m_collection_hit->entries() << endreq;
624 HCE->AddHitsCollection(HCID, m_collection_hit);
626 if (verboseLevel > 0)
628 m_collection_hit->PrintAllHits();
G4THitsCollection< BesCgemHit > BesCgemHitsCollection
void SetTotalEnergyDeposit(G4double f_E_deposit)
void SetGlobalTime(G4double f_global_time)
G4ThreeVector GetPositionOfPrePoint() const
void AddIdentifier(G4int f_ID_Identifier[2000], G4int N_dim)
G4ThreeVector GetMomentumOfPostPoint() const
void SetMomentumOfPostPoint(G4ThreeVector f_P_post)
void SetHitID(G4int f_ID_hit)
void SetPDGCode(G4int f_pdg_code)
G4double GetStepLength() const
void SetLayerID(G4int f_ID_layer)
void SetRdtEl(G4int f_RdtElectron)
void SetParentID(G4int f_ID_parent)
void SetTrackID(G4int f_ID_track)
G4ThreeVector GetPositionOfPostPoint() const
void SetPositionOfPrePoint(G4ThreeVector f_XYZ_pre)
G4double GetTotalEnergyDeposit() const
void SetMomentumOfPrePoint(G4ThreeVector f_P_pre)
G4ThreeVector GetMomentumOfPrePoint() const
void SetCreatorProcess(G4String proName)
void SetPositionOfPostPoint(G4ThreeVector f_XYZ_post)
void SetStepLength(G4double f_L_step)
void EndOfTruthEvent(const G4Event *)
void EndOfEvent(G4HCofThisEvent *)
G4bool ProcessHits(G4Step *, G4TouchableHistory *)
void BeginOfTruthEvent(const G4Event *)
void Initialize(G4HCofThisEvent *)
void GetCurrentTrackIndex(G4int &trackIndex, G4int &g4TrackId) const