FairShip
Loading...
Searching...
No Matches
ScoringPlane.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5#ifndef DETECTOR_SCORINGPLANE_H_
6#define DETECTOR_SCORINGPLANE_H_
7
8#include <algorithm>
9#include <cstdlib>
10#include <string>
11#include <vector>
12
13#include "Detector.h"
14#include "FairLogger.h"
15#include "FairVolume.h"
16#include "ShipDetectorList.h"
17#include "ShipGeoUtil.h"
18#include "ShipStack.h"
19#include "TGeoManager.h"
20#include "TGeoMatrix.h"
21#include "TGeoMedium.h"
22#include "TParticle.h"
23#include "TVector3.h"
24#include "TVirtualMC.h"
25
26namespace SHiP {
27
50template <typename PointType>
51class ScoringPlane : public Detector<PointType> {
52 public:
53 using Detector<PointType>::Detector;
54
58 void AddPlane(const char* name, Double_t x, Double_t y, Double_t z,
59 Double_t sizeX, Double_t sizeY, Double_t sizeZ,
60 const std::vector<Int_t>& absPdgCodes = {},
61 Bool_t keepTracks = kTRUE) {
62 fPlanes.push_back(
63 {name, x, y, z, sizeX, sizeY, sizeZ, absPdgCodes, keepTracks});
64 }
65
66 Bool_t ProcessHits(FairVolume* /*vol*/ = nullptr) override {
67 // Score each track once, on its first step in the plane.
68 if (!gMC->IsTrackEntering()) {
69 return kFALSE;
70 }
71 Int_t copyNo = 0;
72 gMC->CurrentVolID(copyNo);
73 if (copyNo < 1 || static_cast<size_t>(copyNo) > fPlanes.size()) {
74 LOG(fatal) << this->GetName() << ": copy number " << copyNo
75 << " of the plane does not match any of the " << fPlanes.size()
76 << " configured planes; the geometry differs from the "
77 "configuration.";
78 }
79 const Plane& plane = fPlanes[copyNo - 1];
80 TParticle* p = gMC->GetStack()->GetCurrentTrack();
81 const Int_t pdgCode = p->GetPdgCode();
82 const std::vector<Int_t>& accepted = plane.absPdgCodes;
83 if (!accepted.empty() && std::find(accepted.begin(), accepted.end(),
84 std::abs(pdgCode)) == accepted.end()) {
85 return kFALSE;
86 }
87
88 this->fEventID = gMC->CurrentEvent();
89 this->fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
90 this->fVolumeID = copyNo;
91 this->fTime = gMC->TrackTime() * 1.0e09; // ns
92 this->fLength = gMC->TrackLength();
93 gMC->TrackPosition(this->fPos);
94 gMC->TrackMomentum(this->fMom);
95 this->AddHit(this->fEventID, this->fTrackID, this->fVolumeID,
96 TVector3(this->fPos.X(), this->fPos.Y(), this->fPos.Z()),
97 TVector3(this->fMom.Px(), this->fMom.Py(), this->fMom.Pz()),
98 this->fTime, this->fLength, 0., pdgCode);
99
100 // Count a point for this track. ShipStack only keeps a secondary because
101 // of its points when it is configured with a minimum number of points
102 // (MCTracksWithHitsOnly / MCTracksWithHitsOrEnergyCut in
103 // run_simScript.py). With the default MCTracksWithEnergyCutOnly,
104 // secondaries below the energy cut are dropped and their points get track
105 // ID -2.
106 if (plane.keepTracks) {
107 ShipStack* stack = dynamic_cast<ShipStack*>(gMC->GetStack());
108 stack->AddPoint(static_cast<DetectorId>(this->GetDetId()));
109 }
110 return kTRUE;
111 }
112
113 void ConstructGeometry() override {
114 TGeoVolume* top = gGeoManager->GetTopVolume();
115 ShipGeo::InitMedium("vacuum");
116 TGeoMedium* vacuum = gGeoManager->GetMedium("vacuum");
117 if (!vacuum) {
118 LOG(fatal) << this->GetName() << ": medium 'vacuum' not found.";
119 }
120 if (fPlanes.empty()) {
121 LOG(fatal) << this->GetName() << ": no scoring planes.";
122 }
123
124 for (size_t i = 0; i < fPlanes.size(); ++i) {
125 const Plane& pl = fPlanes[i];
126 if (pl.sizeX <= 0. || pl.sizeY <= 0. || pl.sizeZ <= 0.) {
127 LOG(fatal) << this->GetName() << ": invalid dimensions of " << pl.name;
128 }
129 TGeoVolume* plane = gGeoManager->MakeBox(
130 pl.name.c_str(), vacuum, pl.sizeX / 2., pl.sizeY / 2., pl.sizeZ / 2.);
131 plane->SetLineColor(fLineColor);
132 this->AddSensitiveVolume(plane);
133 // The copy number, i + 1, is the point detector ID: node <name>_<i + 1>.
134 top->AddNode(plane, i + 1, new TGeoTranslation(pl.x, pl.y, pl.z));
135 this->fDetector = plane;
136 LOG(info) << pl.name << ": " << pl.sizeX << " x " << pl.sizeY << " x "
137 << pl.sizeZ << " cm3 centred at (" << pl.x << ", " << pl.y
138 << ", " << pl.z << ") cm"
139 << (pl.absPdgCodes.empty() ? "" : ", PDG-filtered");
140 }
141 }
142
143 protected:
144 Color_t fLineColor = kGray;
145
146 private:
147 struct Plane {
148 std::string name; // volume name
149 Double_t x, y, z; // centre (cm)
150 Double_t sizeX, sizeY, sizeZ; // full sizes (cm)
151 std::vector<Int_t> absPdgCodes; // if not empty, the only species recorded
152 Bool_t keepTracks; // points keep their tracks in MCTrack
153 };
154 std::vector<Plane> fPlanes;
155};
156
157} // namespace SHiP
158
159#endif // DETECTOR_SCORINGPLANE_H_
DetectorId
Int_t fTrackID
event index
Definition Detector.h:83
Int_t fVolumeID
track index
Definition Detector.h:84
TLorentzVector fPos
volume id
Definition Detector.h:85
Double_t fTime
momentum at entrance
Definition Detector.h:87
TGeoVolume * fDetector
Definition Detector.h:92
PointType * AddHit(Args &&... args)
Definition Detector.h:38
TLorentzVector fMom
position at entrance
Definition Detector.h:86
Int_t fEventID
Definition Detector.h:82
Double_t fLength
time
Definition Detector.h:88
Detector()=default
Thin vacuum planes that record every track entering them.
Bool_t ProcessHits(FairVolume *=nullptr) override
void AddPlane(const char *name, Double_t x, Double_t y, Double_t z, Double_t sizeX, Double_t sizeY, Double_t sizeZ, const std::vector< Int_t > &absPdgCodes={}, Bool_t keepTracks=kTRUE)
std::vector< Plane > fPlanes
void ConstructGeometry() override
Int_t InitMedium(const char *name)
Definition ShipGeoUtil.h:22
colour of the planes in the event display
std::vector< Int_t > absPdgCodes