ATLAS Offline Software
MTT0PatternRecognition.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 #include "GaudiKernel/MsgStream.h"
9 #include "TGraph.h"
10 #include "TH1.h"
11 #include "TLine.h"
12 
13 namespace MuonCalib {
14 
16  // estimate_background(const TH1 &hist, double scale_min) //
18 
20  // Get first bin in input histogram which is not empty. This avoids underestimation of the background rate if the range of
21  // the input histogram exceeds the TDC-range
22  int min(-1);
23  for (int i = 1; i <= hist->GetNbinsX(); i++) {
24  if (hist->GetBinContent(i) > 0) {
25  min = i;
26  break;
27  }
28  }
29  if (min == -1) {
30  MsgStream log(Athena::getMessageSvc(), "MTT0PattternRecognition");
31  log << MSG::WARNING << "estimate_background() - No hits in input histogram!" << endmsg;
32  m_error = true;
33  return false;
34  }
35  double maxx = scale_min - 40;
36  int max = hist->FindBin(maxx);
37  // we want 10 bins minimum for the background estimate
38  if (max - min < m_settings->MinBackgroundBins()) {
39  min = max - 128;
40  if (min < 0) min = 0;
41  }
42  // if there are still not enough bins for the background estimate we have a problem
43  if (max - min < m_settings->MinBackgroundBins()) {
44  MsgStream log(Athena::getMessageSvc(), "MTT0PattternRecognition");
45  log << MSG::WARNING << "estimate_background() - Rising edge is to glose to lower histogram range!" << endmsg;
46  m_error = true;
47  return false;
48  }
49  // calculate average bin content
50  m_background = 0.0;
51  double back_squared = 0.0;
52  double n_bins = 0.0;
53  double referece_chi2 = 0.0;
54  for (int i = min; i < max; i++) {
55  n_bins++;
56  m_background += hist->GetBinContent(i);
57  back_squared += hist->GetBinContent(i) * hist->GetBinContent(i);
58  if (n_bins == m_settings->MinBackgroundBins()) {
59  double bac = m_background / n_bins;
60  referece_chi2 = 2 * (back_squared / n_bins - bac * bac);
61  }
62  if (n_bins > m_settings->MinBackgroundBins()) {
63  double bac = m_background / n_bins;
64  double chi2 = 2 * (back_squared / n_bins - bac * bac);
65  if (chi2 > 5 * referece_chi2) break;
66  }
67  }
68  m_background /= n_bins;
69  // store lower edge of fit range
70  m_fit_min = hist->GetBinCenter(min);
71  m_t0_est = 0.5 * (scale_min + hist->GetBinCenter(max));
72  // mark selected range
73  if (m_draw_debug_graph) {
74  (new TLine(hist->GetBinCenter(min), 0, hist->GetBinCenter(min), hist->GetMaximum()))->Write("t0_back_left");
75  (new TLine(hist->GetBinCenter(max), 0, hist->GetBinCenter(max), hist->GetMaximum()))->Write("t0_back_right");
76  }
77 
78  return true;
79  }
80 
82  // estimate_height(const TH1 &hist) //
84 
86  // smooth histogram
87  if (!m_vbh.Smooth(hist->GetBinWidth(1))) {
88  m_error = true;
89  return -1;
90  }
91  if (!m_vbh.Smooth(hist->GetBinWidth(1) / 2)) {
92  m_error = true;
93  return -1;
94  }
95  // draw debug graphs
96  if (m_draw_debug_graph) {
97  m_vbh.DenistyGraph()->Write("t0_density");
98  m_vbh.BinWidthGraph()->Write("t0_binwidth");
99  m_vbh.BinContentGraph()->Write("t0_bin_content");
100  m_vbh.DiffDensityGraph()->Write("t0_diff_density");
101  m_vbh.DiffDensityGraph()->Write("t0_diffbinwidth");
102  }
103  // get the range in which the smallest, and second smallest bins are
104  double lower = m_vbh.GetSortedBin(0).Width();
105  double left(9e9), right(-9e9);
106  // bool smallest_found(false);
107  for (unsigned int i = 0; i < m_vbh.GetNumberOfBins(); i++) {
108  if (m_vbh.GetSortedBin(i).Left() < left) left = m_vbh.GetSortedBin(i).Left();
109  if (m_vbh.GetSortedBin(i).Right() > right) right = m_vbh.GetSortedBin(i).Right();
110  // NOTE: For greater numerical stability m_vbh.GetSortedBin(i).Width() is considered greater only if it is greater by
111  // hist->GetBinWidth(1) / 100.0. I have seen differences in Binwidth in the order of 1e-13. On the other hand, due to the
112  // binning of the input histogram, e real difference in the binwidth of the VBH n*hist->GetBinWidth(1).
113  if ((m_vbh.GetSortedBin(i).Width() - lower) > hist->GetBinWidth(1) / 100.0) {
114  // reqire minimum distance between bins, and minimum number if significant bins
115  if (right - left > 50 && i > 20) break;
116  lower = m_vbh.GetSortedBin(i).Width();
117  }
118  }
119  // mark selected range
120  if (m_draw_debug_graph) {
121  (new TLine(left, 0, left, hist->GetMaximum()))->Write("t0_height_left");
122  (new TLine(right, 0, right, hist->GetMaximum()))->Write("t0_height_right");
123  }
124  // calcularte mean bin content of input histogram in range
125  int bleft(hist->FindBin(left)), bright(hist->FindBin(right));
126  double nbins = 0.0;
127  m_height = 0.0;
128  for (int i = bleft; i <= bright; i++) {
129  m_height += hist->GetBinContent(i);
130  nbins++;
131  }
132  if (nbins < 1) {
133  MsgStream log(Athena::getMessageSvc(), "MTT0PattternRecognition");
134  log << MSG::WARNING << "estimate_height() - top region is too small! left=" << left << " right=" << right << endmsg;
135  m_error = true;
136  return -1;
137  }
138  m_height /= nbins;
139  m_fit_max = right;
140  return left;
141  }
142 
143 } // namespace MuonCalib
MuonCalib::MTT0PatternRecognition::m_draw_debug_graph
bool m_draw_debug_graph
Definition: MTT0PatternRecognition.h:85
max
#define max(a, b)
Definition: cfImp.cxx:41
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
MuonCalib::VariableBinwidthHistogram::GetSortedBin
const VariableBinwidthHistogramBin & GetSortedBin(unsigned int bin)
Get a bin sorted by content.
Definition: VariableBinwidthHistogram.h:76
plotmaker.hist
hist
Definition: plotmaker.py:148
MuonCalib::VariableBinwidthHistogram::DiffDensityGraph
TGraph * DiffDensityGraph() const
Plot graph with differential density.
Definition: VariableBinwidthHistogram.cxx:151
MuonCalib::MTT0PatternRecognition::m_settings
const T0MTSettingsT0 * m_settings
settings
Definition: MTT0PatternRecognition.h:84
MuonCalib::MTT0PatternRecognition::m_background
double m_background
background level
Definition: MTT0PatternRecognition.h:88
MuonCalib::VariableBinwidthHistogram::DenistyGraph
TGraph * DenistyGraph() const
create density graph - density vs bin center
Definition: VariableBinwidthHistogram.cxx:113
MuonCalib::VariableBinwidthHistogram::GetNumberOfBins
unsigned int GetNumberOfBins() const
Get the number of bins
Definition: VariableBinwidthHistogram.h:85
SCT_CalibAlgs::nbins
@ nbins
Definition: SCT_CalibNumbers.h:10
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
MuonCalib::MTT0PatternRecognition::m_height
double m_height
height
Definition: MTT0PatternRecognition.h:91
MTT0PatternRecognition.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
MuonCalib::VariableBinwidthHistogram::BinContentGraph
TGraph * BinContentGraph() const
Plot bin content graph - bin content vs bin center.
Definition: VariableBinwidthHistogram.cxx:141
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:522
MuonCalib::VariableBinwidthHistogram::Smooth
bool Smooth(double width)
Removes steps that origin in a binning effekt.
Definition: VariableBinwidthHistogram.cxx:87
MuonCalib::MTT0PatternRecognition::m_fit_min
double m_fit_min
fit range
Definition: MTT0PatternRecognition.h:94
MuonCalib
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
Definition: CscCalcPed.cxx:22
MuonCalib::T0MTSettingsT0::MinBackgroundBins
int MinBackgroundBins() const
The minimum width of the region for the background estimation.
Definition: T0MTSettingsT0.h:54
min
#define min(a, b)
Definition: cfImp.cxx:40
MuonCalib::VariableBinwidthHistogramBin::Left
double Left() const
Get left (lower) bin border.
Definition: VariableBinwidthHistogramBin.h:111
MuonCalib::MTT0PatternRecognition::m_error
bool m_error
error flag
Definition: MTT0PatternRecognition.h:103
MuonCalib::MTT0PatternRecognition::m_fit_max
double m_fit_max
Definition: MTT0PatternRecognition.h:94
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
MuonCalib::VariableBinwidthHistogram::BinWidthGraph
TGraph * BinWidthGraph() const
Plot binwidth graph - binwidth versus bin center.
Definition: VariableBinwidthHistogram.cxx:127
MuonCalib::MTT0PatternRecognition::m_vbh
VariableBinwidthHistogram m_vbh
Variable binwidth histogram.
Definition: MTT0PatternRecognition.h:100
MuonCalib::VariableBinwidthHistogramBin::Right
double Right() const
Get right (upper) bin border.
Definition: VariableBinwidthHistogramBin.h:114
MuonCalib::MTT0PatternRecognition::estimate_background
bool estimate_background(TH1F *hist, double scale_min)
estimates the background level
Definition: MTT0PatternRecognition.cxx:19
MuonCalib::MTT0PatternRecognition::estimate_height
double estimate_height(TH1F *hist)
estimates the height of the spectrum.
Definition: MTT0PatternRecognition.cxx:85
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
MuonCalib::VariableBinwidthHistogramBin::Width
double Width() const
Get width of the bin.
Definition: VariableBinwidthHistogramBin.h:108
MuonCalib::MTT0PatternRecognition::m_t0_est
double m_t0_est
t0 estimate
Definition: MTT0PatternRecognition.h:97