FairShip
Loading...
Searching...
No Matches
exitHadronAbsorber.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 MUONSHIELDOPTIMIZATION_EXITHADRONABSORBER_H_
6#define MUONSHIELDOPTIMIZATION_EXITHADRONABSORBER_H_
7
8#include <cstddef>
9#include <map>
10#include <set>
11#include <string>
12#include <utility>
13#include <vector>
14
15#include "Detector.h"
16#include "TFile.h"
17#include "TNtuple.h"
18#include "vetoPoint.h"
19
20class FairVolume;
21class G4Step;
22
24 Int_t pdg;
25 Double_t px, py, pz, e;
26 Double_t x, y, z, t;
27 Double_t polx, poly, polz;
28 Double_t weight;
29 Int_t parentID;
30 // A continuation of a per-step split track rather than a clone: it is
31 // transported normally instead of being forced to decay at once.
32 Bool_t continuation = kFALSE;
33 // Split set of the split track and the role of this entry in it
34 // (ShipMCTrack::SplitRole), recorded on the stack when it is pushed.
35 Int_t splitSet = -1;
36 Int_t splitRole = 0;
37};
38
39class exitHadronAbsorber : public SHiP::Detector<vetoPoint> {
40 public:
41 exitHadronAbsorber(const char* Name, Bool_t Active);
44 if (fgSplitter == this) {
45 fgSplitter = nullptr;
46 }
47 }
48
49 // Number of stack entries each pending split clone is assumed to add to the
50 // event once it has decayed and its products have showered. This is an
51 // empirical, conservative estimate from production runs with per-step
52 // splitting. It projects the event size that --max-event-size is checked
53 // against, and run_fixedTarget.py uses it to validate that option.
54 static constexpr std::size_t kShowerSafetyFactor = 500;
55
56 void Initialize() override;
57
58 Bool_t ProcessHits(FairVolume* v = nullptr) override;
59
60 void Register() override;
61
62 void ConstructGeometry() override;
63
64 void FinishRun() override;
65 void PreTrack() override;
66 void PostTrack() override;
67 void BeginEvent() override;
68 void FinishEvent() override;
69
70 void SetNSplits(Int_t n) { fNsplits = n; }
72 void SetMaxSplitBuffer(Int_t n);
73 void SetMaxEventSize(Int_t n);
74 void SetSplitMultipleTimes() { fSplitOnce = kFALSE; }
75
76 // Per-step splitting entry point, called for every Geant4 step by
77 // ShipSteppingAction (gconfig/g4Config.C installs it through
78 // ShipRunConfiguration). Forwards to the one instance that splits per step.
79 static void SplitStepHook(const G4Step* step);
80
81 inline void SetEnergyCut(Float_t emax) { EMax = emax; }
82 inline void SetOnlyMuons() { fOnlyMuons = kTRUE; }
83 inline void SetOpt4DP() { withNtuple = kTRUE; }
84 inline void SkipNeutrinos() { fSkipNeutrinos = kTRUE; }
85 inline void SetZposition(Float_t x) { fzPos = x; }
86 inline void SetVetoPointName(TString name) { fVetoName = std::move(name); }
87 inline void SetCylindricalPlane() { fCylindricalPlane = kTRUE; }
88 inline void SetUseCaveCoordinates() { fUseCaveCoordinates = kTRUE; }
89
90 private:
91 // Hand the buffered clones of a splitting decay to the stack popper. They
92 // can only be popped during a step, so the first track after the decay is
93 // designated as their carrier and must not be stopped before it steps.
94 // The designation is shared by all instances: run_fixedTarget can add
95 // several sensitive planes and whichever is polled first would otherwise
96 // stop the carrier before the splitting instance ever sees it.
97 static constexpr Int_t kNoCarrier = -1;
98 static constexpr Int_t kCarrierRequested = -2;
99 static Int_t fgCarrierTrackID;
100
101 // Ask for the next track to carry the clones just added to the buffer.
103
104 // Tracks that an unsplit run would not have. A decay that the clones
105 // re-sample is replaced as a whole: its products, and everything they go on
106 // to make, would otherwise be counted on top of the same channels from the
107 // clones. A carrier that an unsplit run would have stopped only steps so
108 // that the clones get popped, and is replaced with all its descendants too.
109 // Shared by all instances for the same reason as the carrier: a sibling
110 // plane must not score them either.
111 static std::set<Int_t> fgReplacedDecays;
113 static std::set<Int_t> fgReplacedTracks;
116
117 // Weight of a per-step split track along its path. Geant4 hands a track's
118 // secondaries to the stack only once the track is done, and ShipStack then
119 // scales them by its final weight, although one made mid-track should carry
120 // the weight the track had when it made it. PreTrack() puts that right from
121 // this history. Shared by all instances, because the first plane to run
122 // PreTrack() for a secondary has to see it.
124 Double_t initial = 1.;
127 std::vector<std::pair<Double_t, Double_t>> splits;
131 Double_t WeightAt(Double_t t) const;
132 };
133 static std::map<Int_t, WeightHistory> fgWeightHistory;
134 static std::set<Int_t> fgWeightCorrected;
135
136 // Drop the clones still buffered at the end of an event or run, keeping
137 // track of how much weight was never simulated.
139
140 // Split a kaon or pion step: hand its decay probability to clones and lower
141 // the track's weight. Called for every Geant4 step through SplitStepHook().
142 void SplitStep(const G4Step* step);
143
144 // The instance that splits per step, if any.
146 // Logical volumes in which steps are split, decided from the pre-step point.
147 std::set<std::string> fSplitVolumes;
148 // Kaon/pion steps that reached SplitStep(), to catch a run configuration
149 // without ShipSteppingAction.
150 Long64_t fHookSteps = 0;
151
152 // Split set of a track that is being split, created on first use. The
153 // track is recorded on the stack as the survivor of its set; its clones
154 // and continuations join the same set. A continuation maps to the set of
155 // the track it continues, so a per-step split track keeps one set along its
156 // whole path. The set's weight is the track's weight when the set is
157 // created, before any split lowers it.
158 Int_t SplitSetOf(Int_t trackId, Double_t weight);
159
160 Int_t fUniqueID = 0;
161 Bool_t fOnlyMuons;
163 TString fVetoName; // name to save veto collection
165 Double_t fzPos;
166 Bool_t withNtuple;
167 TNtuple* fNtuple;
168 Float_t EMax = 0.;
170 Bool_t fUseCaveCoordinates = kFALSE;
171
172 int32_t fNsplits;
173 // intermediate splits to use at each step before the particle decays
175 // Upper bound on the clone buffer, in both splitting modes. Per-step
176 // splitting is the pathological case: the buffer grows by
177 // fIntermediateNsplits on every qualifying step and is only drained in
178 // PreTrack(), so a bad split count could make it grow without bound within a
179 // single track. ~25k TrackBuffer records is about 2.5 MB, far above what any
180 // sane configuration reaches. Set via --max-split-buffer.
181 // This is a hard bound: SplitStep() stops accepting per-step clones early
182 // enough to leave room for the fNsplits endpoint clones PostTrack() appends,
183 // and Initialize() rejects a cap that fNsplits alone would exceed.
184 std::size_t fMaxSplitBuffer = 25'000;
185 // Upper bound on the projected event size: the stack size plus
186 // kShowerSafetyFactor entries for every pending clone. With the defaults this
187 // is the cap that stops per-step splitting, at about 10k pending clones.
188 // Set via --max-event-size.
189 std::size_t fMaxEventSize = 5'000'000;
190 // latches so each cap is reported at most once per event
191 Bool_t fSplitBufferLimitWarned = kFALSE;
192 Bool_t fEventSizeLimitWarned = kFALSE;
193 Bool_t fSplitOnce =
194 kTRUE; // determine if we want to split once (when the particle decays)
195 // or at every step (taking decay probabilities into account)
196
197 std::vector<TrackBuffer> fSecondaryBuffer;
198 std::set<Int_t> fCloneTracks;
199 // The last per-step split, so that PostTrack() can tell whether the step on
200 // which a track decayed was split.
201 Int_t fLastSplitTrackID = -1;
202 Int_t fLastSplitStep = -1;
203 // Split set per stack index of a split track or continuation, and the next
204 // free set ID in the event.
205 std::map<Int_t, Int_t> fSplitSetOfTrack;
206 std::vector<Double_t> fSplitSetWeight;
207 Int_t fNextSplitSet = 0;
208
209 Int_t fSplitDecays = 0;
210 Int_t fClonesBuffered = 0;
214 Double_t fLostCloneWeight = 0.;
215
216 TFile* fout;
217 TClonesArray* fElectrons;
218 Int_t index;
219};
220
221#endif // MUONSHIELDOPTIMIZATION_EXITHADRONABSORBER_H_
static constexpr Int_t kCarrierRequested
static constexpr Int_t kNoCarrier
void DiscardBufferedClones()
secondaries already rescaled
std::vector< Double_t > fSplitSetWeight
Int_t fContinuedDecays
clones created for those decays
static Int_t fgCarrierTrackID
std::map< Int_t, Int_t > fSplitSetOfTrack
static std::map< Int_t, WeightHistory > fgWeightHistory
void ConstructGeometry() override
void SetMaxSplitBuffer(Int_t n)
void SetVetoPointName(TString name)
Int_t fNextSplitSet
indexed by set ID
Bool_t fUseCaveCoordinates
cylindrical sensPlane flag
TClonesArray * fElectrons
static std::set< Int_t > fgReplacedDecays
Int_t SplitSetOf(Int_t trackId, Double_t weight)
Bool_t ProcessHits(FairVolume *v=nullptr) override
static void SplitStepHook(const G4Step *step)
Bool_t fCylindricalPlane
max energy to transport
Double_t fzPos
name of the sensitive plane volume
void FinishEvent() override
Double_t fLostCloneWeight
clones which were never tracked
static constexpr std::size_t kShowerSafetyFactor
int32_t fNsplits
set position from cave
std::vector< TrackBuffer > fSecondaryBuffer
Bool_t withNtuple
zPos, optional
Int_t fClonesBuffered
decays replaced by clones
static void RequestCloneCarrier()
exitHadronAbsorber(const char *Name, Bool_t Active)
void SetIntermediateNSplits(Int_t n)
static std::set< Int_t > fgReplacedTracks
void SetEnergyCut(Float_t emax)
Int_t fLostBufferEvents
natural decays replaced by a continuation
TString fVetoName
flag if neutrinos should be ignored
TFile * fout
summed weight of those clones
TNtuple * fNtuple
special option for Dark Photon physics studies
Bool_t fSkipNeutrinos
flag if only muons should be stored
static std::set< Int_t > fgWeightCorrected
void SplitStep(const G4Step *step)
Int_t fLostCloneTracks
events ending with unflushed clones
std::set< std::string > fSplitVolumes
static exitHadronAbsorber * fgSplitter
std::set< Int_t > fCloneTracks
void SetZposition(Float_t x)
Double_t WeightAt(Double_t t) const
std::vector< std::pair< Double_t, Double_t > > splits