FairShip
Loading...
Searching...
No Matches
MuonPath.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 "MuonPath.h"
6
7#include <iostream>
8#include <sstream>
9
10#include "FairLogger.h"
11
13 flabel = "None";
14 fdensity = 0;
15 fwdensity = 0;
16 flength = 0;
17 fzlength = 0;
18}
19
20void MuonPath::SetVertexInfo(const TVector3& vecpos, const TVector3& vecp,
21 const double& time) {
22 fvtx.push_back(vecpos);
23 fvtxT.push_back(time);
24 fpvec.push_back(vecp);
25}
26
27// Integrate density only over the sampled tail of the cumulative path.
28double MuonPath::GetWeightedDensity(double minLength) const {
29 minLength = std::clamp(minLength, 0., flength);
30 double weightedDensity = 0.;
31 double previous = 0.;
32 for (unsigned i = 0; i < fendLength.size(); ++i) {
33 const double length =
34 std::max(0., fendLength[i] - std::max(previous, minLength));
35 weightedDensity += length * fsliceDensity[i];
36 previous = fendLength[i];
37 }
38 return weightedDensity;
39}
40
41double MuonPath::GetZAtLength(double length, unsigned& idx) const {
42 idx = 0;
43 if (fendLength.empty()) return 0.;
44 length = std::clamp(length, 0., flength);
45 const auto end =
46 std::upper_bound(fendLength.begin(), fendLength.end(), length);
47 idx = end == fendLength.end() ? fendLength.size() - 1
48 : end - fendLength.begin();
49 const double previous = idx == 0 ? 0. : fendLength[idx - 1];
50 const double step = fendLength[idx] - previous;
51 return step > 0. ? fstart[idx].Z() + (length - previous) / step *
52 (fendZ[idx] - fstart[idx].Z())
53 : fstart[idx].Z();
54}
55
56// Convert the existing compact z coordinate (gaps removed) into path length.
57double MuonPath::GetLengthAtZ(const double& aZ) const {
58 if (fendLength.empty()) return 0.;
59 unsigned idx = 0;
60 const double z = GetZ(aZ, idx);
61 const double previous = idx == 0 ? 0. : fendLength[idx - 1];
62 const double dz = fendZ[idx] - fstart[idx].Z();
63 return previous + (dz > 0. ? std::clamp((z - fstart[idx].Z()) / dz, 0., 1.) *
64 (fendLength[idx] - previous)
65 : 0.);
66}
67
68double MuonPath::GetZ(const double& aZ, unsigned& idx) const {
69 //@FIXME AMM- is this efficient enough??
70 const unsigned nSlices = GetNSlices();
71 if (nSlices == 0) {
72 idx = 0;
73 return aZ;
74 }
75
76 if (aZ < fendZ[0]) {
77 idx = 0;
78 return aZ;
79 }
80 double prevz = fendZ[0];
81 for (unsigned iz(1); iz < nSlices; ++iz) {
82 double extraz = aZ - prevz;
83 double stepz = fendZ[iz] - fstart[iz].Z();
84 if (extraz < stepz) {
85 idx = iz;
86 return fstart[iz].Z() + extraz;
87 }
88 prevz += stepz;
89 }
90 // set a default to the last value
91 idx = nSlices - 1;
92 return fendZ[idx];
93}
94
95std::string MuonPath::GetLabel(const std::string& aVol,
96 const std::string& aMat) const {
97 //@FIXME AMM-avoid hardcoding, pass by config ?
98 std::string label = "REST";
99 if (aVol.find("Magn") != aVol.npos)
100 label = "MS";
101 else if (aVol.find("Upstream") != aVol.npos)
102 label = "UBT";
103 // The balloon skin includes the walls and both lids; decay_medium is HE.
104 else if (aVol.find("HeBalloon") != aVol.npos)
105 label = "HeBalloon";
106 else if (aMat.find("helium") != aMat.npos)
107 label = "HE";
108 else if (aMat.find("air") != aMat.npos)
109 label = "AIR";
110 else if (aVol.find("straw") != aVol.npos)
111 label = "SSTsens";
112 else if (aVol.find("gas") != aVol.npos && aMat.find("STT") != aMat.npos)
113 label = "SSTsens";
114 else if (aVol.find("wire") != aVol.npos && aMat.find("tungsten") != aMat.npos)
115 label = "SSTsens";
116 else if ((aVol.find("Tr1_frame") != aVol.npos ||
117 aVol.find("Tr2_frame") != aVol.npos ||
118 aVol.find("Tr3_frame") != aVol.npos ||
119 aVol.find("Tr4_frame") != aVol.npos))
120 label = "SSTfr";
121 else if ((aVol.find("Veto") != aVol.npos ||
122 aVol.find("vLongitRib") != aVol.npos))
123 label = "SBTfr";
124 else if (aVol.find("LiSc") != aVol.npos)
125 label = "SBTsens";
126 LOG(debug) << aVol << " " << aMat << " assigned to " << label << ".";
127 return label;
128}
129
131 const unsigned nSlices = GetNSlices();
132
133 std::ostringstream ldebug;
134 ldebug << flabel << " "
135 << " d=" << fdensity << " l=" << flength << " l_in_z=" << fzlength;
136 if (flength > 0) ldebug << " <d>=" << fwdensity / flength << std::endl;
137
138 if (nSlices == 0) {
139 ldebug << "z-slices n=0" << std::endl;
140 LOG(debug) << ldebug.str();
141 return;
142 }
143
144 ldebug << " zIn=" << fstart[0].Z() << " zOut=" << fendZ[nSlices - 1]
145 << std::endl;
146 ldebug << "z-slices n=" << nSlices << ": " << std::endl;
147 for (unsigned iz(0); iz < nSlices; ++iz) {
148 ldebug << fvolName[iz] << " " << fmaterial[iz] << " vtxz=" << fvtx[iz].Z()
149 << " slice [" << fstart[iz].Z() << "-" << fendZ[iz] << "] "
150 << std::endl;
151 }
152 ldebug << std::endl;
153 LOG(debug) << ldebug.str();
154}
155
156bool MuonPath::Add(const MuonPath& aEle) {
157 // path added should always have only one element...
158 if (aEle.GetNSlices() != 1) {
159 LOG(error) << " -- incorrect number of elements in path: "
160 << aEle.GetNSlices();
161 return false;
162 }
163 fvolName.push_back(aEle.fvolName[0]);
164 fmaterial.push_back(aEle.fmaterial[0]);
165 fpvec.push_back(aEle.fpvec[0]);
166 fvtx.push_back(aEle.fvtx[0]);
167 fvtxT.push_back(aEle.fvtxT[0]);
168 fstart.push_back(aEle.fstart[0]);
169 fstartT.push_back(aEle.fstartT[0]);
170 fendZ.push_back(aEle.fendZ[0]);
171 flength += aEle.flength;
172 fendLength.push_back(flength);
173 fsliceDensity.push_back(aEle.fsliceDensity[0]);
174 fwdensity += aEle.fwdensity;
175 fzlength += aEle.fendZ[0] - aEle.fstart[0].Z();
176 return true;
177}
const Int_t debug
double flength
Definition MuonPath.h:175
double fzlength
Definition MuonPath.h:176
void Print()
Definition MuonPath.cxx:130
std::string GetLabel() const
Definition MuonPath.h:67
std::vector< TVector3 > fpvec
Definition MuonPath.h:188
double GetWeightedDensity() const
Definition MuonPath.h:73
std::vector< double > fsliceDensity
Definition MuonPath.h:185
double fdensity
Definition MuonPath.h:173
std::vector< std::string > fvolName
Definition MuonPath.h:178
unsigned GetNSlices() const
Definition MuonPath.h:97
std::vector< std::string > fmaterial
Definition MuonPath.h:179
std::string flabel
Definition MuonPath.h:172
double fwdensity
Definition MuonPath.h:174
void SetVertexInfo(const TVector3 &vecpos, const TVector3 &vecp, const double &time)
Definition MuonPath.cxx:20
std::vector< TVector3 > fvtx
Definition MuonPath.h:180
std::vector< double > fvtxT
Definition MuonPath.h:181
double GetZ(const double &aZ, unsigned &idx) const
Definition MuonPath.cxx:68
std::vector< double > fendLength
Definition MuonPath.h:187
std::vector< TVector3 > fstart
Definition MuonPath.h:182
std::vector< double > fstartT
Definition MuonPath.h:183
double GetLengthAtZ(const double &aZ) const
Definition MuonPath.cxx:57
double GetZAtLength(double length, unsigned &idx) const
Definition MuonPath.cxx:41
std::vector< double > fendZ
Definition MuonPath.h:184
bool Add(const MuonPath &aEle)
Definition MuonPath.cxx:156