ATLAS Offline Software
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)
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 
46  resize();
47 }
48 
49 void TFCSEnergyBinParametrization::set_pdgid(const std::set<int> &ids) {
51  resize();
52 }
53 
56  resize();
57 }
58 
61  resize();
62 }
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 
131 void 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 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
FCSReturnCode
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
Definition: TFCSParametrizationBase.h:41
TFCSParametrization::pdgid
const std::set< int > & pdgid() const override
Definition: TFCSParametrization.h:34
TFCSParametrization::is_match_pdgid
virtual bool is_match_pdgid(int id) const override
Definition: TFCSParametrization.h:15
ATH_MSG
#define ATH_MSG(lvl)
Definition: AthMsgStreamMacros.h:38
TFCSEnergyBinParametrization::m_number_of_Ekin_bins
int m_number_of_Ekin_bins
Definition: TFCSEnergyBinParametrization.h:53
TFCSEnergyParametrization
Definition: TFCSEnergyParametrization.h:10
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
make_coralServer_rep.opt
opt
Definition: make_coralServer_rep.py:19
TFCSParametrization::add_pdgid
virtual void add_pdgid(int id)
Definition: TFCSParametrization.cxx:35
TFCSEnergyBinParametrization::clear_pdgid
virtual void clear_pdgid() override
Definition: TFCSEnergyBinParametrization.cxx:59
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
skel.it
it
Definition: skel.GENtoEVGEN.py:396
TFCSEnergyBinParametrization::load_pdgid_Ekin_bin_probability_from_file
virtual bool load_pdgid_Ekin_bin_probability_from_file(int id, TFile *file, std::string prob_object_name)
Definition: TFCSEnergyBinParametrization.cxx:84
TFCSExtrapolationState
Definition: TFCSExtrapolationState.h:13
TFCSSimulationState::randomEngine
CLHEP::HepRandomEngine * randomEngine()
Definition: TFCSSimulationState.h:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
TFCSEnergyBinParametrization::is_match_Ekin_bin
virtual bool is_match_Ekin_bin(int Ekin_bin) const override
Definition: TFCSEnergyBinParametrization.cxx:24
TFCSEnergyBinParametrization::n_bins
virtual int n_bins() const override
Definition: TFCSEnergyBinParametrization.h:23
covarianceTool.prob
prob
Definition: covarianceTool.py:678
TFCSEnergyBinParametrization::set_pdgid
virtual void set_pdgid(int id) override
Definition: TFCSEnergyBinParametrization.cxx:44
TFCSParametrization::set_pdgid
virtual void set_pdgid(int id)
Definition: TFCSParametrization.cxx:28
TFCSSimulationState::set_Ebin
void set_Ebin(int bin)
Definition: TFCSSimulationState.h:47
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
lumiFormat.i
int i
Definition: lumiFormat.py:85
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TFCSParametrization::clear_pdgid
virtual void clear_pdgid()
Definition: TFCSParametrization.cxx:37
covarianceTool.title
title
Definition: covarianceTool.py:542
file
TFile * file
Definition: tile_monitor.h:29
TFCSParametrizationBase::Print
void Print(Option_t *option="") const
Print object information.
Definition: TFCSParametrizationBase.cxx:52
TFCSEnergyBinParametrization::resize
void resize()
Definition: TFCSEnergyBinParametrization.cxx:30
FCSFatal
@ FCSFatal
Definition: TFCSParametrizationBase.h:41
FCSSuccess
@ FCSSuccess
Definition: TFCSParametrizationBase.h:41
lumiFormat.array
array
Definition: lumiFormat.py:91
TFCSEnergyBinParametrization.h
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.subdetectors.mmg.ids
ids
Definition: mmg.py:8
TFCSTruthState::pdgid
int pdgid() const
Definition: TFCSTruthState.h:25
TFCSEnergyBinParametrization::add_pdgid
virtual void add_pdgid(int id) override
Definition: TFCSEnergyBinParametrization.cxx:54
TFCSEnergyBinParametrization::set_pdgid_Ekin_bin_probability
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 ...
Definition: TFCSEnergyBinParametrization.cxx:64
END_MSG
#define END_MSG(lvl)
Definition: MLogging.h:171
TFCSTruthState.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.Constants.INFO
int INFO
Definition: Control/AthenaCommon/python/Constants.py:16
TFCSEnergyBinParametrization::Print
void Print(Option_t *option="") const override
Definition: TFCSEnergyBinParametrization.cxx:131
DEBUG
#define DEBUG
Definition: page_access.h:11
TFCSSimulationState::E
double E() const
Definition: TFCSSimulationState.h:42
TFCSSimulationState.h
TFCSEnergyBinParametrization::simulate
virtual FCSReturnCode simulate(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override
Method in all derived classes to do some simulation.
Definition: TFCSEnergyBinParametrization.cxx:157
TFCSTruthState
Definition: TFCSTruthState.h:13
TFCSParametrizationBase::size
virtual unsigned int size() const
Some derived classes have daughter instances of TFCSParametrizationBase objects The size() and operat...
Definition: TFCSParametrizationBase.h:93
TFCSSimulationState
Definition: TFCSSimulationState.h:32
ISF_FCS::MLogging::msgLvl
bool msgLvl(const MSG::Level lvl) const
Check whether the logging system is active at the provided verbosity level.
Definition: MLogging.h:222
TFCSEnergyBinParametrization::m_pdgid_Ebin_probability
std::map< int, std::vector< float > > m_pdgid_Ebin_probability
Definition: TFCSEnergyBinParametrization.h:54
TFCSEnergyBinParametrization::TFCSEnergyBinParametrization
TFCSEnergyBinParametrization(const char *name=nullptr, const char *title=nullptr)
Definition: TFCSEnergyBinParametrization.cxx:18