5#include "CLHEP/Random/RandGaussZiggurat.h"
6#include "CLHEP/Random/RandFlat.h"
18#include "TMatrixDSymEigen.h"
35 if (Ekin_bin >= 1 && Ekin_bin <=
n_bins())
50 bool shortprint = opt.Index(
"short") >= 0;
51 bool longprint =
msgLvl(MSG::DEBUG) || (
msgLvl(MSG::INFO) && !shortprint);
52 TString optprint = opt;
53 optprint.ReplaceAll(
"short",
"");
76 return hist->GetBinContent(1);
77 if (
x >= hist->GetBinCenter(hist->GetNbinsX()))
78 return hist->GetBinContent(hist->GetNbinsX());
80 int bin = hist->FindBin(
x);
82 if (
x > hist->GetBinCenter(
bin)) {
83 x1 = hist->GetBinCenter(
bin);
84 y1 = hist->GetBinContent(
bin);
85 x2 = hist->GetBinCenter(
bin + 1);
86 y2 = hist->GetBinContent(
bin + 1);
91 x1 = hist->GetBinCenter(
bin - 1);
92 y1 = hist->GetBinContent(
bin - 1);
93 x2 = hist->GetBinCenter(
bin);
94 y2 = hist->GetBinContent(
bin);
97 m = (y1 - y2) / (x1 - x2);
112 int Ekin_bin)
const {
128 int pcabin = simulstate.
Ebin();
133 simulstate.
set_E(s, 0.0);
138 TMatrixD *EV =
m_EV[pcabin - 1];
143 std::vector<TFCS1DFunction *> cumulative =
m_cumulative[pcabin - 1];
147 double *vals_gauss_means = (
double *)Gauss_means->GetMatrixArray();
148 double *vals_gauss_rms = Gauss_rms->GetMatrixArray();
150 double *output_data =
new double[layerNr.size() + 1];
151 double *input_data =
new double[layerNr.size() + 1];
153 for (
unsigned int l = 0; l <= layerNr.size(); l++) {
154 double mean = vals_gauss_means[l];
155 double rms = vals_gauss_rms[l];
158 input_data[l] = gauszz;
161 P2X(SigmaValues, MeanValues, EV, layerNr.size() + 1, input_data,
162 output_data, layerNr.size() + 1);
164 double *simdata =
new double[layerNr.size() + 1];
165 double sum_fraction = 0.0;
166 for (
unsigned int l = 0; l <= layerNr.size(); l++) {
167 double simdata_uniform =
168 (TMath::Erf(output_data[l] / 1.414213562) + 1) / 2.f;
170 simdata[l] = cumulative[l]->rnd_to_fct(simdata_uniform);
172 if (l != layerNr.size())
173 sum_fraction += simdata[l];
176 double scalefactor = 1.0 / sum_fraction;
182 int layerNrsize =
static_cast<int>(layerNr.size());
183 for (
int l = 0; l < layerNrsize; l++) {
184 simdata[l] *= scalefactor;
191 if (h_totalE_ratio) {
192 float pass_probability = 0;
193 float Etot = simdata[layerNr.size()];
194 if (Etot > h_totalE_ratio->GetXaxis()->GetXmin() &&
195 Etot < h_totalE_ratio->GetXaxis()->GetXmax()) {
198 float random = CLHEP::RandFlat::shoot(simulstate.
randomEngine());
199 if (random > pass_probability) {
200 delete[] output_data;
208 double total_energy =
210 simulstate.
set_E(total_energy);
214 double energyfrac = 0.0;
215 for (
unsigned int l = 0; l < layerNr.size(); l++) {
217 energyfrac = simdata[l];
220 simulstate.
set_E(s, energyfrac * total_energy);
221 simulstate.
set_SF(scalefactor);
224 delete[] output_data;
233 TVectorD *MeanValues, TMatrixD *EV,
234 int gNVariables,
const double *p,
237 const double *gSigmaValues = SigmaValues->GetMatrixArray();
238 const double *gMeanValues = MeanValues->GetMatrixArray();
239 const double *gEigenVectors = EV->GetMatrixArray();
241 for (
int i = 0; i < gNVariables; i++) {
242 x[i] = gMeanValues[i];
243 for (
int j = 0; j < nTest; j++) {
245 p[j] * gSigmaValues[i] * (double)(gEigenVectors[i * gNVariables + j]);
255 const std::string &folder) {
276 file->cd(
x +
"1/pca");
278 if (RelevantLayers ==
nullptr) {
279 ATH_MSG_ERROR(
"TFCSPCAEnergyParametrization::m_RelevantLayers in first "
288 for (
int i = 0; i < RelevantLayers->GetSize(); i++)
295 TMatrixDSym *symCov = (TMatrixDSym *)gDirectory->Get(
"symCov");
296 TVectorD *MeanValues = (TVectorD *)gDirectory->Get(
"MeanValues");
297 TVectorD *SigmaValues = (TVectorD *)gDirectory->Get(
"SigmaValues");
298 TVectorD *Gauss_means = (TVectorD *)gDirectory->Get(
"Gauss_means");
299 TVectorD *Gauss_rms = (TVectorD *)gDirectory->Get(
"Gauss_rms");
301 if (symCov ==
nullptr) {
303 <<
bin <<
" is null!");
306 if (MeanValues ==
nullptr) {
308 <<
bin <<
" is null!");
311 if (SigmaValues ==
nullptr) {
313 <<
bin <<
" is null!");
316 if (Gauss_means ==
nullptr) {
318 <<
bin <<
" is null!");
321 if (Gauss_rms ==
nullptr) {
323 <<
bin <<
" is null!");
330 TMatrixDSymEigen cov_eigen(*symCov);
331 TMatrixD *EV =
new TMatrixD(cov_eigen.GetEigenVectors());
338 std::vector<std::string> layer;
341 for (
unsigned int i = 0; i < layerNr.size(); i++) {
342 layer.emplace_back(Form(
"layer%i", layerNr[i]));
344 layer.emplace_back(
"totalE");
346 std::vector<TFCS1DFunction *> cumulative;
347 cumulative.reserve(layer.size());
349 for (
unsigned int l = 0; l < layer.size(); l++) {
350 file->cd(Form(
"%s/bin%i/%s", folder.c_str(),
bin, layer[l].c_str()));
353 fct = (
TFCS1DFunction *)gDirectory->Get(
"TFCS1DFunctionRegression");
355 fct = (
TFCS1DFunction *)gDirectory->Get(
"TFCS1DFunctionRegressionTF");
357 fct = (
TFCS1DFunction *)gDirectory->Get(
"TFCS1DFunctionHistogram");
358 cumulative.push_back(fct);
369 for (
unsigned int i = 0; i <
m_EV.size(); i++)
373void TFCSPCAEnergyParametrization::Streamer(TBuffer &R__b) {
376 if (R__b.IsReading()) {
378 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
379 R__b.SetBufferOffset(R__s);
381 R__b.ReadClassBuffer(TFCSPCAEnergyParametrization::Class(),
this);
395 R__b.WriteClassBuffer(TFCSPCAEnergyParametrization::Class(),
this);
#define ATH_MSG_WARNING(x)
float interpolate_get_y(TH1 *hist, float x)
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.
TFCSEnergyParametrization(const char *name=nullptr, const char *title=nullptr)
std::vector< TVectorD * > m_MeanValues
float m_total_energy_normalization
std::vector< TVectorD * > m_Gauss_rms
std::vector< TVectorD * > m_Gauss_means
virtual bool is_match_calosample(int calosample) const override
void set_total_energy_normalization(float norm)
static void P2X(TVectorD *, TVectorD *, TMatrixD *, int, const double *, double *, int)
std::vector< TMatrixD * > m_EV
std::vector< TH1 * > m_totalE_probability_ratio
virtual int n_bins() const override
bool loadInputs(TFile *file)
TH1 * get_totalE_probability_ratio(int Ekin_bin) const
void set_totalE_probability_ratio(int Ekin_bin, TH1 *hist)
void Print(Option_t *option="") const override
virtual FCSReturnCode simulate(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override
Method in all derived classes to do some simulation.
virtual bool is_match_Ekin_bin(int Ekin_bin) const override
std::vector< TVectorD * > m_SigmaValues
std::vector< std::vector< TFCS1DFunction * > > m_cumulative
std::vector< int > m_RelevantLayers
TFCSPCAEnergyParametrization(const char *name=nullptr, const char *title=nullptr)
void Print(Option_t *option="") const
Print object information.
double Ekin_nominal() const override
double Ekin_min() const override
double Ekin_max() const override
virtual void set_Ekin_nominal(double min)
void set_E(int sample, double Esample)
void set_Efrac(int sample, double Efracsample)
CLHEP::HepRandomEngine * randomEngine()
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Macro wrapping the nonstandard restrict keyword.