ATLAS Offline Software
Loading...
Searching...
No Matches
TFCSEnergyBinParametrization.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 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 float p = 0;
78 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin) {
79 p += prob[iEbin] / ptot;
80 m_pdgid_Ebin_probability[id][iEbin] = p;
81 }
82}
83
85 int id, TFile *file, std::string prob_object_name) {
86 add_pdgid(id);
87
88 file->cd();
89 auto *probFromFile =
90 dynamic_cast<TVectorF *>(gDirectory->Get(prob_object_name.c_str()));
91
92 std::vector<float> prob;
93 prob.reserve(m_pdgid_Ebin_probability[id].size());
94 if (probFromFile == nullptr) {
95 ATH_MSG_INFO("TFCSEnergyBinParametrization::load_pdgid_Ekin_bin_"
96 "probability_from_file(): "
97 << prob_object_name
98 << " is null. Using equal PCA probabilities.");
99 prob.push_back(0.0);
100 for (size_t i = 1; i < m_pdgid_Ebin_probability[id].size(); i++) {
101 prob.push_back(1.0);
102 }
103 } else {
104 auto size = static_cast<size_t>(probFromFile->GetNoElements());
105 if (size != m_pdgid_Ebin_probability[id].size()) {
107 "TFCSEnergyBinParametrization::load_pdgid_Ekin_bin_probability_from_"
108 "file(): size of prob array does not match! in.size()="
109 << size << " instance=" << m_pdgid_Ebin_probability[id].size());
110 return false;
111 }
112
113 const float *probArray = probFromFile->GetMatrixArray();
114 for (size_t i = 0; i < size; i++) {
115 prob.push_back(probArray[i]);
116 }
117 }
118
119 float ptot{};
120 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin)
121 ptot += prob[iEbin];
122 float p{};
123 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin) {
124 p += prob[iEbin] / ptot;
125 m_pdgid_Ebin_probability[id][iEbin] = p;
126 }
127
128 return true;
129}
130
131void TFCSEnergyBinParametrization::Print(Option_t *option) const {
132 TString opt(option);
133 bool shortprint = opt.Index("short") >= 0;
134 bool longprint = msgLvl(MSG::DEBUG) || (msgLvl(MSG::INFO) && !shortprint);
135 TString optprint = opt;
136 optprint.ReplaceAll("short", "");
138 if (longprint) {
139 for (std::set<int>::iterator it = pdgid().begin(); it != pdgid().end();
140 ++it) {
141 ATH_MSG(INFO) << optprint << " PDGID=" << *it << " : ";
142 float p = 0;
143 for (int iEbin = 0; iEbin <= n_bins(); ++iEbin) {
144 if (iEbin > 0)
145 ATH_MSG(INFO) << ", ";
146 auto mapit = m_pdgid_Ebin_probability.find(*it);
147 ATH_MSG(INFO) << "b" << iEbin << "="
148 << (mapit->second[iEbin] - p) / mapit->second.back() * 100
149 << "%";
150 p = mapit->second[iEbin];
151 }
152 ATH_MSG(INFO) << END_MSG(INFO);
153 }
154 }
155}
156
158 TFCSSimulationState &simulstate, const TFCSTruthState *truth,
159 const TFCSExtrapolationState * /*extrapol*/) const {
160 if (!simulstate.randomEngine()) {
161 return FCSFatal;
162 }
163
164 const int truth_pdgid = truth->pdgid();
165 int pdgid = -99;
166
167 if (is_match_pdgid(truth_pdgid))
168 pdgid = truth_pdgid;
169 else if (is_match_pdgid(0))
170 pdgid = 0;
171 else {
173 "TFCSEnergyBinParametrization::simulate(): cannot simulate pdgid="
174 << truth_pdgid);
175 return FCSFatal;
176 }
177
178 const float searchRand = CLHEP::RandFlat::shoot(simulstate.randomEngine());
179 const auto &Ebin_probability = m_pdgid_Ebin_probability.at(pdgid);
180 int chosenBin =
181 TMath::BinarySearch(n_bins() + 1, Ebin_probability.data(), searchRand) +
182 1;
183 if (chosenBin < 0) {
184 ATH_MSG_WARNING("TFCSEnergyBinParametrization::simulate(): chosenBin<0 "
185 "(will use chosenBin=0)");
186 std::string array = "";
187 for (const auto &prob : Ebin_probability) {
188 array += prob;
189 array += " ";
190 }
191 ATH_MSG_WARNING(" E=" << simulstate.E() << " Ebin=" << chosenBin
192 << " rnd=" << searchRand << " array=" << array);
193 chosenBin = 0;
194 } else if (chosenBin > n_bins()) {
195 ATH_MSG_WARNING("TFCSEnergyBinParametrization::simulate(): "
196 "chosenBin>n_bins() (will use chosenBin=n_bins())");
197 std::string array = "";
198 for (const auto &prob : Ebin_probability) {
199 array += prob;
200 array += " ";
201 }
202 ATH_MSG_WARNING(" E=" << simulstate.E() << " Ebin=" << chosenBin
203 << " rnd=" << searchRand << " array=" << array);
204 chosenBin = n_bins();
205 }
206 simulstate.set_Ebin(chosenBin);
207 ATH_MSG_DEBUG("Ebin=" << chosenBin);
208
209 return FCSSuccess;
210}
#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
TFile * file