Belle II Software  release-06-02-00
SensitiveBar.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <top/simulation/SensitiveBar.h>
10 
11 #include <simulation/kernel/UserInfo.h>
12 #include <G4Step.hh>
13 #include <G4Track.hh>
14 #include <G4UnitsTable.hh>
15 #include <G4ParticleDefinition.hh>
16 #include <G4ParticleTypes.hh>
17 
18 #include <framework/logging/Logger.h>
19 #include <framework/gearbox/Unit.h>
20 
21 #include <TVector3.h>
22 #include <TRandom.h>
23 #include <cmath>
24 
25 using namespace std;
26 
27 namespace Belle2 {
32  namespace TOP {
33 
34  SensitiveBar::SensitiveBar():
35  Simulation::SensitiveDetectorBase("TOP", Const::TOP)
36  {
37 
38  // registration of store arrays and relations
39 
40  m_barHits.registerInDataStore();
42 
43  // additional registration of MCParticle relation (required for correct relations)
44 
46 
47  const auto* geo = m_topgp->getGeometry();
48  m_trackIDs.resize(geo->getNumModules(), 0);
49 
50  }
51 
52 
53  bool SensitiveBar::step(G4Step* aStep, G4TouchableHistory*)
54  {
55 
56  // get track and particle definition
57 
58  G4Track* aTrack = aStep->GetTrack();
59  G4ParticleDefinition* particle = aTrack->GetDefinition();
60 
61  // if optical photon, apply QE and return false
62 
63  if (particle == G4OpticalPhoton::OpticalPhotonDefinition()) {
64  auto* info = dynamic_cast<Simulation::TrackInfo*>(aTrack->GetUserInformation());
65  if (!info) return false;
66  if (info->getStatus() < 2) {
67  double energy = aTrack->GetKineticEnergy() * Unit::MeV / Unit::eV;
68  double qeffi = m_topgp->getPMTEfficiencyEnvelope(energy);
69  double fraction = info->getFraction();
70  if (qeffi == 0 or gRandom->Uniform() * fraction > qeffi) {
71  aTrack->SetTrackStatus(fStopAndKill);
72  return false;
73  }
74  info->setStatus(2);
75  info->setFraction(qeffi);
76  }
77  return false;
78  }
79 
80  // continue for other particles, if they enter the volume for the first time
81 
82  G4StepPoint* PrePosition = aStep->GetPreStepPoint();
83  if (PrePosition->GetStepStatus() != fGeomBoundary) return false; // not on boundary
84 
85  if (m_barHits.getEntries() == 0) {
86  for (auto& trackID : m_trackIDs) trackID = -1; // reset on new event
87  }
88 
89  int moduleID = PrePosition->GetTouchableHandle()->GetReplicaNumber(m_replicaDepth);
90  const auto* geo = m_topgp->getGeometry();
91  if (!geo->isModuleIDValid(moduleID)) {
92  B2ERROR("TOP::SensitiveBar: undefined module ID."
93  << LogVar("moduleID", moduleID));
94  return false;
95  }
96 
97  int trackID = aTrack->GetTrackID();
98  if (trackID == m_trackIDs[moduleID - 1]) return false; // not first time on boundary
99  m_trackIDs[moduleID - 1] = trackID;
100 
101  // particle other than optical photon entered the volume for the first time
102  // convert to basf2 units and write-out the hit
103 
104  G4ThreeVector worldPosition = PrePosition->GetPosition();
105  double tracklength = aTrack->GetTrackLength() - aStep->GetStepLength();
106  double globalTime = PrePosition->GetGlobalTime();
107  G4ThreeVector momentum = PrePosition->GetMomentum();
108 
109  TVector3 TPosition(worldPosition.x(), worldPosition.y(), worldPosition.z());
110  TVector3 TMomentum(momentum.x(), momentum.y(), momentum.z());
111  TVector3 TOrigin(aTrack->GetVertexPosition().x(),
112  aTrack->GetVertexPosition().y(),
113  aTrack->GetVertexPosition().z());
114 
115  // convert to basf2 units
116  TPosition = TPosition * Unit::mm;
117  TMomentum = TMomentum * Unit::MeV;
118  TOrigin = TOrigin * Unit::mm;
119  tracklength = tracklength * Unit::mm;
120 
121  const auto& module = geo->getModule(moduleID);
122  TVector3 locPosition = module.pointToLocal(TPosition);
123  TVector3 locMomentum = module.momentumToLocal(TMomentum);
124  double theta = locMomentum.Theta();
125  double phi = locMomentum.Phi();
126 
127  int PDG = (int)(particle->GetPDGEncoding());
128 
129  // write hit to datastore
130  auto* hit = m_barHits.appendNew(moduleID, PDG, TOrigin, TPosition, TMomentum,
131  globalTime, tracklength, locPosition,
132  theta, phi);
133 
134  // set the relation
135  m_relParticleHit.add(trackID, hit->getArrayIndex());
136 
137  return true;
138  }
139 
140 
141  } // end of namespace top
143 } // end of namespace Belle2
This class provides a set of constants for the framework.
Definition: Const.h:34
@ c_deleteElement
Delete the whole relation element if the original element got re-attributed.
Definition: RelationArray.h:81
void add(index_type from, index_type to, weight_type weight=1.0)
Add a new element to the relation.
static void registerMCParticleRelation(const std::string &name, RelationArray::EConsolidationAction ignoreAction=RelationArray::c_negativeWeight)
Register an relation involving MCParticles.
UserInfo class which is used to attach additional information to Geant4 particles and tracks.
Definition: UserInfo.h:36
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:140
bool step(G4Step *aStep, G4TouchableHistory *) override
Process each step and fill TOPBarHits.
Definition: SensitiveBar.cc:53
int m_replicaDepth
replica depth of module volume
Definition: SensitiveBar.h:55
std::vector< int > m_trackIDs
track ID's
Definition: SensitiveBar.h:57
RelationArray m_relParticleHit
relations
Definition: SensitiveBar.h:61
TOPGeometryPar * m_topgp
geometry parameters
Definition: SensitiveBar.h:56
StoreArray< MCParticle > m_mcParticles
collection of MC particles
Definition: SensitiveBar.h:59
StoreArray< TOPBarHit > m_barHits
collection of entrance-to-bar hits
Definition: SensitiveBar.h:60
double getPMTEfficiencyEnvelope(double energy) const
Returns PMT efficiency envelope, e.g.
const TOPGeometry * getGeometry() const
Returns pointer to geometry object using basf2 units.
static const double mm
[millimeters]
Definition: Unit.h:70
static const double eV
[electronvolt]
Definition: Unit.h:112
static const double MeV
[megaelectronvolt]
Definition: Unit.h:114
Class to store variables with their name which were sent to the logging service.
Abstract base class for different kinds of events.