ATLAS Offline Software
TFCS1DFunction.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include "TH1.h"
8 #include "TCanvas.h"
9 #include "TRandom.h"
10 #include <string>
11 #include <iostream>
12 
13 //=============================================
14 //======= TFCS1DFunction =========
15 //=============================================
16 
17 void TFCS1DFunction::rnd_to_fct(float value[], const float rnd[]) const {
18  value[0] = rnd_to_fct(rnd[0]);
19 }
20 
21 double TFCS1DFunction::get_maxdev(TH1 *h_input1, TH1 *h_approx1) {
22  TH1D *h_input = (TH1D *)h_input1->Clone("h_input");
23  TH1D *h_approx = (TH1D *)h_approx1->Clone("h_approx");
24 
25  double maxdev = 0.0;
26 
27  // normalize the histos to the same area:
28  double integral_input = h_input->Integral();
29  double integral_approx = 0.0;
30  for (int b = 1; b <= h_input->GetNbinsX(); b++)
31  integral_approx +=
32  h_approx->GetBinContent(h_approx->FindBin(h_input->GetBinCenter(b)));
33  h_approx->Scale(integral_input / integral_approx);
34 
35  double ymax = h_approx->GetBinContent(h_approx->GetNbinsX()) -
36  h_approx->GetBinContent(h_approx->GetMinimumBin());
37  for (int i = 1; i <= h_input->GetNbinsX(); i++) {
38  double val = fabs(h_approx->GetBinContent(
39  h_approx->FindBin(h_input->GetBinCenter(i))) -
40  h_input->GetBinContent(i)) /
41  ymax;
42  if (val > maxdev)
43  maxdev = val;
44  }
45 
46  delete h_input;
47  delete h_approx;
48 
49  return maxdev * 100.0;
50 }
51 
53  const TH1 *hist, std::vector<double> &integral_vec, int &first, int &last) {
55  Int_t nbins = hist->GetNbinsX();
56 
57  float integral = 0;
58  integral_vec.resize(nbins);
59  for (int ix = 1; ix <= nbins; ix++) {
60  float binval = hist->GetBinContent(ix);
61  if (binval < 0) {
62  // Can't work if a bin is negative, forcing bins to 0 in this case
63  double fraction = binval / hist->Integral();
64  if (TMath::Abs(fraction) > 1e-5) {
65  ATH_MSG_NOCLASS(logger, "Warning: bin content is negative in histogram "
66  << hist->GetName() << " : "
67  << hist->GetTitle() << " binval=" << binval
68  << " " << fraction * 100
69  << "% of integral=" << hist->Integral()
70  << ". Forcing bin to 0.");
71  }
72  binval = 0;
73  }
74  integral += binval;
75  integral_vec[ix - 1] = integral;
76  }
77 
78  for (first = 0; first < nbins; first++)
79  if (integral_vec[first] != 0)
80  break;
81  for (last = nbins - 1; last > 0; last--)
82  if (integral_vec[last] != integral)
83  break;
84  last++;
85 
86  if (integral <= 0) {
87  ATH_MSG_NOCLASS(logger, "Error: histogram "
88  << hist->GetName() << " : " << hist->GetTitle()
89  << " integral=" << integral << " is <=0");
90  }
91  return integral;
92 }
93 
94 TH1 *TFCS1DFunction::generate_histogram_random_slope
95 ATLAS_NOT_THREAD_SAFE(int nbinsx, double xmin, double xmax,
96  double zerothreshold) {
97  TH1 *hist = new TH1D("test_slope1D", "test_slope1D", nbinsx, xmin, xmax);
98  hist->Sumw2();
99  for (int ix = 1; ix <= nbinsx; ++ix) {
100  double val = (0.5 + gRandom->Rndm()) * (nbinsx + ix);
101  if (gRandom->Rndm() < zerothreshold)
102  val = 0;
103  hist->SetBinContent(ix, val);
104  hist->SetBinError(ix, 0);
105  }
106  return hist;
107 }
108 
109 TH1 *TFCS1DFunction::generate_histogram_random_gauss
110 ATLAS_NOT_THREAD_SAFE(int nbinsx, int ntoy, double xmin, double xmax,
111  double xpeak, double sigma) {
112  TH1 *hist = new TH1D("test_gauss1D", "test_gauss1D", nbinsx, xmin, xmax);
113  hist->Sumw2();
114  for (int i = 1; i <= ntoy; ++i) {
115  double x = gRandom->Gaus(xpeak, sigma);
116  if (x >= xmin && x < xmax)
117  hist->Fill(x);
118  }
119  return hist;
120 }
121 
122 void TFCS1DFunction::unit_test ATLAS_NOT_THREAD_SAFE(TH1 *hist,
123  TFCS1DFunction *rtof,
124  int nrnd, TH1 *histfine) {
126  ATH_MSG_NOCLASS(logger, "========= " << hist->GetName() << " funcsize="
127  << rtof->MemorySize() << " ========");
128  int nbinsx = hist->GetNbinsX();
129  double integral = hist->Integral();
130 
131  float value[2];
132  float rnd[2];
133  //cppcheck-suppress uninitvar
134  for (rnd[0] = 0; rnd[0] < 0.9999; rnd[0] += 0.25) {
135  rtof->rnd_to_fct(value, rnd);
136  ATH_MSG_NOCLASS(logger, "rnd0=" << rnd[0] << " -> x=" << value[0]);
137  }
138 
139  TH1 *hist_val = nullptr;
140  if (histfine)
141  hist_val = (TH1 *)histfine->Clone(TString(hist->GetName()) + "hist_val");
142  else
143  hist_val = (TH1 *)hist->Clone(TString(hist->GetName()) + "hist_val");
144  double weightfine = hist_val->Integral() / nrnd;
145  hist_val->SetTitle("toy simulation");
146  hist_val->Reset();
147  hist_val->SetLineColor(2);
148  hist_val->Sumw2();
149 
150  TH1 *hist_diff = (TH1 *)hist->Clone(TString(hist->GetName()) + "_difference");
151  hist_diff->SetTitle("cut efficiency difference");
152  hist_diff->Reset();
153  hist_diff->Sumw2();
154 
155  double weight = integral / nrnd;
156  for (int i = 0; i < nrnd; ++i) {
157  rnd[0] = gRandom->Rndm();
158  rtof->rnd_to_fct(value, rnd);
159  hist_val->Fill(value[0], weightfine);
160  hist_diff->Fill(value[0], weight);
161  }
162  hist_diff->Add(hist, -1);
163  hist_diff->Scale(1.0 / integral);
164 
165  TH1F *hist_pull =
166  new TH1F(TString(hist->GetName()) + "_pull", "pull", 200, -10, 10);
167  for (int ix = 1; ix <= nbinsx; ++ix) {
168  float val = hist_diff->GetBinContent(ix);
169  float err = hist_diff->GetBinError(ix);
170  if (err > 0)
171  hist_pull->Fill(val / err);
172  }
173 
174 // Screen output in athena won't make sense and would require linking of
175 // additional libraries
176 #if defined(__FastCaloSimStandAlone__)
177  TCanvas *c = new TCanvas(hist->GetName(), hist->GetName());
178  c->Divide(2, 2);
179 
180  c->cd(1);
181  if (histfine) {
182  histfine->SetLineColor(kGray);
183  histfine->DrawClone("hist");
184  hist->DrawClone("same");
185  } else {
186  hist->DrawClone();
187  }
188  hist_val->DrawClone("sameshist");
189 
190  c->cd(2);
191  if (histfine) {
192  histfine->SetLineColor(kGray);
193  histfine->DrawClone("hist");
194  hist->DrawClone("same");
195  } else {
196  hist->DrawClone();
197  }
198  hist_val->DrawClone("sameshist");
199  gPad->SetLogy();
200 
201  c->cd(3);
202  hist_diff->Draw();
203 
204  c->cd(4);
205  hist_pull->Draw();
206 
207  c->SaveAs(".png");
208 #endif
209 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
ISF_FCS::MLogging
Cut down AthMessaging.
Definition: MLogging.h:176
pdg_comparison.sigma
sigma
Definition: pdg_comparison.py:324
integral
double integral(TH1 *h)
Definition: computils.cxx:58
plotmaker.hist
hist
Definition: plotmaker.py:148
TFCS1DFunction::rnd_to_fct
virtual void rnd_to_fct(float value[], const float rnd[]) const
Function gets array of random numbers rnd[] in the range [0,1) as arguments and returns function valu...
Definition: TFCS1DFunction.cxx:17
athena.value
value
Definition: athena.py:124
x
#define x
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
ATLAS_NOT_THREAD_SAFE
TH1 *TFCS1DFunction::generate_histogram_random_slope ATLAS_NOT_THREAD_SAFE(int nbinsx, double xmin, double xmax, double zerothreshold)
Definition: TFCS1DFunction.cxx:95
TFCS1DFunction::get_maxdev
static double get_maxdev(TH1 *, TH1 *)
Definition: TFCS1DFunction.cxx:21
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
lumiFormat.i
int i
Definition: lumiFormat.py:85
xmin
double xmin
Definition: listroot.cxx:60
TFCS1DFunction::CheckAndIntegrate1DHistogram
static double CheckAndIntegrate1DHistogram(const TH1 *hist, std::vector< double > &integral_vec, int &first, int &last)
Definition: TFCS1DFunction.cxx:52
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
ATH_MSG_NOCLASS
#define ATH_MSG_NOCLASS(logger_name, x)
Definition: MLogging.h:52
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
DeMoScan.first
bool first
Definition: DeMoScan.py:536
xmax
double xmax
Definition: listroot.cxx:61
TFCS1DFunction.h
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
TFCS1DFunction
Definition: TFCS1DFunction.h:17
python.compressB64.c
def c
Definition: compressB64.py:93
python.iconfTool.gui.pad.logger
logger
Definition: pad.py:14
TFCSFunction::MemorySize
virtual std::size_t MemorySize() const
Gives the total memory size, including the size of additional memory allocated inside the class.
Definition: TFCSFunction.h:19
ymax
double ymax
Definition: listroot.cxx:64