ATLAS Offline Software
Loading...
Searching...
No Matches
TFCSEnergyBinParametrization.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "CLHEP/Random/RandFlat.h"
6
10#include "TFile.h"
11#include "TVectorF.h"
12#include "TMath.h"
13
14//=============================================
15//======= TFCSSelectEnergyBin =========
16//=============================================
17
19 const char *title)
20 : TFCSEnergyParametrization(name, title) {
22}
23
25 if (Ekin_bin >= 1 && Ekin_bin <= n_bins())
26 return true;
27 return false;
28}
29
31 for (int id : pdgid()) {
32 ATH_MSG_VERBOSE("PDGid=" << id << " resize to " << n_bins() + 1);
33 m_pdgid_Ebin_probability[id].resize(n_bins() + 1);
34 }
35 for (auto it = m_pdgid_Ebin_probability.begin();
36 it != m_pdgid_Ebin_probability.end(); ++it) {
37 if (!is_match_pdgid(it->first)) {
39 it = m_pdgid_Ebin_probability.begin();
40 }
41 }
42}
43
48
53
58
63
65 int id, std::vector<float> prob) {
66 add_pdgid(id);
67 if (prob.size() != m_pdgid_Ebin_probability[id].size()) {
68 ATH_MSG_ERROR("TFCSEnergyBinParametrization::set_pdgid_Ekin_bin_"
69 "probability(): size of vectors does not match! in.size()="
70 << prob.size()
71 << " instance=" << m_pdgid_Ebin_probability[id].size());
72 return;
73 }
74 float ptot = 0;
75 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin){
76 ptot += prob[iEbin];
77 }
78 float p = 0;
79 if (ptot == 0.f)[[unlikely]]{
80 ATH_MSG_ERROR("TFCSEnergyBinParametrization::set_pdgid_Ekin_bin_probability: ptot is zero.");
81 return;
82 }
83 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin) {
84 p += prob[iEbin] / ptot;
85 m_pdgid_Ebin_probability[id][iEbin] = p;
86 }
87}
88
90 int id, TFile *file, std::string prob_object_name) {
91 add_pdgid(id);
92
93 file->cd();
94 auto *probFromFile =
95 dynamic_cast<TVectorF *>(gDirectory->Get(prob_object_name.c_str()));
96
97 std::vector<float> prob;
98 prob.reserve(m_pdgid_Ebin_probability[id].size());
99 if (probFromFile == nullptr) {
100 ATH_MSG_INFO("TFCSEnergyBinParametrization::load_pdgid_Ekin_bin_"
101 "probability_from_file(): "
102 << prob_object_name
103 << " is null. Using equal PCA probabilities.");
104 prob.push_back(0.0);
105 for (size_t i = 1; i < m_pdgid_Ebin_probability[id].size(); i++) {
106 prob.push_back(1.0);
107 }
108 } else {
109 auto size = static_cast<size_t>(probFromFile->GetNoElements());
110 if (size != m_pdgid_Ebin_probability[id].size()) {
112 "TFCSEnergyBinParametrization::load_pdgid_Ekin_bin_probability_from_"
113 "file(): size of prob array does not match! in.size()="
114 << size << " instance=" << m_pdgid_Ebin_probability[id].size());
115 return false;
116 }
117
118 const float *probArray = probFromFile->GetMatrixArray();
119 for (size_t i = 0; i < size; i++) {
120 prob.push_back(probArray[i]);
121 }
122 }
123
124 float ptot{};
125 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin){
126 ptot += prob[iEbin];
127 }
128 float p{};
129 if (ptot ==0.0f)[[unlikely]]{
130 ATH_MSG_ERROR("TFCSEnergyBinParametrization::load_pdgid_Ekin_bin_probability_from_file: ptot is zero");
131 return false;
132 }
133 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin) {
134 p += prob[iEbin] / ptot;
135 m_pdgid_Ebin_probability[id][iEbin] = p;
136 }
137
138 return true;
139}
140
141void TFCSEnergyBinParametrization::Print(Option_t *option) const {
142 TString opt(option);
143 bool shortprint = opt.Index("short") >= 0;
144 bool longprint = msgLvl(MSG::DEBUG) || (msgLvl(MSG::INFO) && !shortprint);
145 TString optprint = opt;
146 optprint.ReplaceAll("short", "");
148 if (longprint) {
149 for (std::set<int>::iterator it = pdgid().begin(); it != pdgid().end();
150 ++it) {
151 ATH_MSG(INFO) << optprint << " PDGID=" << *it << " : ";
152 float p = 0;
153 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin) {
154 if (iEbin > 0)
155 ATH_MSG(INFO) << ", ";
156 auto mapit = m_pdgid_Ebin_probability.find(*it);
157 ATH_MSG(INFO) << "b" << iEbin << "="
158 << (mapit->second[iEbin] - p) / mapit->second.back() * 100
159 << "%";
160 p = mapit->second[iEbin];
161 }
162 ATH_MSG(INFO) << END_MSG(INFO);
163 }
164 }
165}
166
168 TFCSSimulationState &simulstate, const TFCSTruthState *truth,
169 const TFCSExtrapolationState * /*extrapol*/) const {
170 if (!simulstate.randomEngine()) {
171 return FCSFatal;
172 }
173
174 const int truth_pdgid = truth->pdgid();
175 int pdgid = -99;
176
177 if (is_match_pdgid(truth_pdgid))
178 pdgid = truth_pdgid;
179 else if (is_match_pdgid(0))
180 pdgid = 0;
181 else {
183 "TFCSEnergyBinParametrization::simulate(): cannot simulate pdgid="
184 << truth_pdgid);
185 return FCSFatal;
186 }
187
188 const float searchRand = CLHEP::RandFlat::shoot(simulstate.randomEngine());
189 const auto &Ebin_probability = m_pdgid_Ebin_probability.at(pdgid);
190 int chosenBin =
191 TMath::BinarySearch(n_bins() + 1, Ebin_probability.data(), searchRand) +
192 1;
193 if (chosenBin < 0) {
194 ATH_MSG_WARNING("TFCSEnergyBinParametrization::simulate(): chosenBin<0 "
195 "(will use chosenBin=0)");
196 std::string array = "";
197 for (const auto &prob : Ebin_probability) {
198 array += prob;
199 array += " ";
200 }
201 ATH_MSG_WARNING(" E=" << simulstate.E() << " Ebin=" << chosenBin
202 << " rnd=" << searchRand << " array=" << array);
203 chosenBin = 0;
204 } else if (chosenBin > n_bins()) {
205 ATH_MSG_WARNING("TFCSEnergyBinParametrization::simulate(): "
206 "chosenBin>n_bins() (will use chosenBin=n_bins())");
207 std::string array = "";
208 for (const auto &prob : Ebin_probability) {
209 array += prob;
210 array += " ";
211 }
212 ATH_MSG_WARNING(" E=" << simulstate.E() << " Ebin=" << chosenBin
213 << " rnd=" << searchRand << " array=" << array);
214 chosenBin = n_bins();
215 }
216 simulstate.set_Ebin(chosenBin);
217 ATH_MSG_DEBUG("Ebin=" << chosenBin);
218
219 return FCSSuccess;
220}
#define ATH_MSG_ERROR(x)
#define ATH_MSG(lvl)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
#define END_MSG(lvl)
Definition MLogging.h:171
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
bool msgLvl(const MSG::Level lvl) const
Check whether the logging system is active at the provided verbosity level.
Definition MLogging.h:222
virtual void set_pdgid(int id) override
virtual void set_pdgid_Ekin_bin_probability(int id, std::vector< float > prob)
set_pdgid_Ekin_bin_probability(): format of probability should be a vector of float with probability ...
virtual FCSReturnCode simulate(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override
Method in all derived classes to do some simulation.
TFCSEnergyBinParametrization(const char *name=nullptr, const char *title=nullptr)
virtual int n_bins() const override
virtual bool load_pdgid_Ekin_bin_probability_from_file(int id, TFile *file, std::string prob_object_name)
virtual bool is_match_Ekin_bin(int Ekin_bin) const override
virtual void add_pdgid(int id) override
std::map< int, std::vector< float > > m_pdgid_Ebin_probability
void Print(Option_t *option="") const override
TFCSEnergyParametrization(const char *name=nullptr, const char *title=nullptr)
virtual unsigned int size() const
Some derived classes have daughter instances of TFCSParametrizationBase objects The size() and operat...
void Print(Option_t *option="") const
Print object information.
virtual void add_pdgid(int id)
const std::set< int > & pdgid() const override
virtual bool is_match_pdgid(int id) const override
virtual void set_pdgid(int id)
CLHEP::HepRandomEngine * randomEngine()
int pdgid() const
#define unlikely(x)
TFile * file