FairShip
Loading...
Searching...
No Matches
UpstreamTagger.cxx
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#include "UpstreamTagger.h"
6
7#include <cmath>
8#include <iostream>
9#include <map>
10#include <tuple>
11
12#include "FairVolume.h"
13#include "ShipDetectorList.h"
14#include "ShipGeoUtil.h"
15#include "ShipStack.h"
16#include "TGeoManager.h"
17#include "TGeoMedium.h"
18#include "TParticle.h"
19#include "TString.h"
20#include "TVector3.h"
21#include "TVirtualMC.h"
22#include "UpstreamTaggerPoint.h"
23using std::cout;
24using std::endl;
25
27 : Detector("UpstreamTagger", kTRUE, kUpstreamTagger), det_zPos(0) {}
28
29UpstreamTagger::UpstreamTagger(const char* name, Bool_t active)
30 : Detector(name, active, kUpstreamTagger), det_zPos(0) {}
31
32Bool_t UpstreamTagger::ProcessHits(FairVolume* vol) {
34 // Set parameters at entrance of volume, or at the first step of a track
35 // created inside it (e.g. a delta ray). Reset ELoss.
36 if (gMC->IsTrackEntering() || gMC->IsNewTrack()) {
37 // Geant4 VMC also flags the first step of a new track as entering.
38 fEntered = !gMC->IsNewTrack();
39 fELoss = 0.;
40 fTime = gMC->TrackTime() * 1.0e09;
41 fLength = gMC->TrackLength();
42 gMC->TrackPosition(fPos);
43 gMC->TrackMomentum(fMom);
44 }
45
46 // Sum energy loss for all steps in the active volume
47 fELoss += gMC->Edep();
48
49 // Create vetoPoint at exit of active volume
50 if (gMC->IsTrackExiting() || gMC->IsTrackStop() ||
51 gMC->IsTrackDisappeared()) {
52 if (fELoss == 0.) {
53 return kFALSE;
54 }
55
56 fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
57 fEventID = gMC->CurrentEvent();
58 Int_t tileId;
59 gMC->CurrentVolID(tileId);
60
61 TParticle* p = gMC->GetStack()->GetCurrentTrack();
62 Int_t pdgCode = p->GetPdgCode();
63 TLorentzVector Pos;
64 gMC->TrackPosition(Pos);
65 TLorentzVector Mom;
66 gMC->TrackMomentum(Mom);
67 Double_t xmean = (fPos.X() + Pos.X()) / 2.;
68 Double_t ymean = (fPos.Y() + Pos.Y()) / 2.;
69 Double_t zmean = (fPos.Z() + Pos.Z()) / 2.;
70
71 AddHit(fEventID, fTrackID, tileId, TVector3(xmean, ymean, zmean),
72 TVector3(fMom.Px(), fMom.Py(), fMom.Pz()), fTime, fLength, fELoss,
73 pdgCode, TVector3(Pos.X(), Pos.Y(), Pos.Z()),
74 TVector3(Mom.Px(), Mom.Py(), Mom.Pz()));
75 fELoss = 0.;
76
77 // Count a point for tracks that entered the region. A track created in
78 // the plastic (delta ray, Compton electron) still gives a point, for its
79 // energy deposit, but does not keep its track in MCTrack: with
80 // MCTracksWithHitsOnly (--MuonBack) every soft electron of every crossing
81 // muon would be kept. Its point gets track ID -2 unless the track is kept
82 // for another reason.
83 if (fEntered) {
84 ShipStack* stack = dynamic_cast<ShipStack*>(gMC->GetStack());
85 stack->AddPoint(kUpstreamTagger);
86 }
87 }
88
89 return kTRUE;
90}
91
93 TGeoVolume* top = gGeoManager->GetTopVolume();
94
95 ShipGeo::InitMedium("Scintillator");
96 TGeoMedium* scintillator = gGeoManager->GetMedium("Scintillator");
97 if (!scintillator) {
98 Fatal("ConstructGeometry", "Medium 'Scintillator' not found.");
99 }
100 if (fRegions.empty()) {
101 Fatal("ConstructGeometry", "No regions loaded from the UBT detector map.");
102 }
103 if (fSizeX <= 0. || fSizeY <= 0. || fEnvelopeZ <= 0.) {
104 Fatal("ConstructGeometry", "UBT box dimensions not set.");
105 }
106
107 fDetector = new TGeoVolumeAssembly("Upstream_Tagger");
108
109 std::map<Double_t, Int_t> regionsPerTileSize;
110 // Regions of the same shape share one volume, placed with the region ID as
111 // copy number: most regions are full 20 x 20 cm2 squares, and only the
112 // regions cut by the box edges differ. Sizes are keyed in um so that
113 // rounding differences between equal regions do not split them.
114 using ShapeKey = std::tuple<Long64_t, Long64_t, Long64_t, Long64_t>;
115 const auto um = [](Double_t length) { return std::llround(length * 1.0e4); };
116 std::map<ShapeKey, TGeoVolume*> volumes;
117 for (const Region& region : fRegions) {
118 if (region.sizeX <= 0. || region.sizeY <= 0.) {
119 Fatal("ConstructGeometry", "UBT region %d has a non-positive size.",
120 region.id);
121 }
122 if (region.thickness <= 0. || region.thickness > fEnvelopeZ) {
123 Fatal("ConstructGeometry",
124 "UBT region %d: the thickness %g cm of its %g cm tiles is not in "
125 "(0, %g] cm, the space reserved for the UBT.",
126 region.id, region.thickness, region.constituentTileSize,
127 fEnvelopeZ);
128 }
129 if (std::abs(region.x - fCenterX) + region.sizeX / 2. > fSizeX / 2. ||
130 std::abs(region.y - fCenterY) + region.sizeY / 2. > fSizeY / 2.) {
131 Fatal("ConstructGeometry", "UBT region %d lies outside the UBT box.",
132 region.id);
133 }
134 const ShapeKey shape{um(region.sizeX), um(region.sizeY),
135 um(region.thickness), um(region.constituentTileSize)};
136 TGeoVolume*& regionVolume = volumes[shape];
137 if (!regionVolume) {
138 // UpstreamTaggerRegion_<tile size>cm_<shape number>: the tile size is
139 // read back by UpstreamTaggerDetector.validate_stored_geometry.
140 const TString volumeName = TString::Format(
141 "UpstreamTaggerRegion_%dcm_%zu",
142 static_cast<Int_t>(region.constituentTileSize), volumes.size() - 1);
143 regionVolume =
144 gGeoManager->MakeBox(volumeName, scintillator, region.sizeX / 2.,
145 region.sizeY / 2., region.thickness / 2.);
146 // A shade of blue per tile size in the event display.
147 regionVolume->SetLineColor(
148 kBlue - static_cast<Int_t>(region.constituentTileSize));
149 AddSensitiveVolume(regionVolume);
150 }
151 fDetector->AddNode(regionVolume, region.id,
152 new TGeoTranslation(region.x, region.y, 0.));
153 ++regionsPerTileSize[region.constituentTileSize];
154 }
155
156 top->AddNode(fDetector, 1, new TGeoTranslation(0., 0., det_zPos));
157 cout << " Z Position (Upstream Tagger) " << det_zPos << ", "
158 << fRegions.size() << " mapped regions (";
159 for (auto it = regionsPerTileSize.begin(); it != regionsPerTileSize.end();
160 ++it) {
161 cout << (it == regionsPerTileSize.begin() ? "" : ", ") << it->second
162 << " with " << it->first << " cm tiles";
163 }
164 cout << ") in " << volumes.size() << " region shapes" << endl;
165}
@ kUpstreamTagger
constexpr double um
Definition ShipUnit.h:26
UpstreamTaggerPoint * AddHit(Args &&... args)
Definition Detector.h:38
Double_t fSizeX
coarse regions loaded from detector map
Double_t fCenterY
detector centre x
Double_t fCenterX
detector height
Bool_t ProcessHits(FairVolume *v=nullptr) override
std::vector< Region > fRegions
void ConstructGeometry() override
Double_t fSizeY
detector width
Double_t fEnvelopeZ
detector centre y
Bool_t fEntered
allocated longitudinal space
Int_t InitMedium(const char *name)
Definition ShipGeoUtil.h:22
z-position of the active tile plane