ATLAS Offline Software
Loading...
Searching...
No Matches
MTT0PatternRecognition.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
13namespace MuonCalib {
14
16 // estimate_background(const TH1 &hist, double scale_min) //
18
19 bool MTT0PatternRecognition::estimate_background(TH1F* hist, double scale_min) {
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 close 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 if (n_bins == 0){
69 //messaging should be revisited in this whole class
70 MsgStream log(Athena::getMessageSvc(), "MTT0PattternRecognition");
71 log << MSG::WARNING << "n_bins is zero" << endmsg;
72 m_background =0.;
73 } else {
74 m_background /= n_bins;
75 }
76 // store lower edge of fit range
77 m_fit_min = hist->GetBinCenter(min);
78 m_t0_est = 0.5 * (scale_min + hist->GetBinCenter(max));
79 // mark selected range
81 (new TLine(hist->GetBinCenter(min), 0, hist->GetBinCenter(min), hist->GetMaximum()))->Write("t0_back_left");
82 (new TLine(hist->GetBinCenter(max), 0, hist->GetBinCenter(max), hist->GetMaximum()))->Write("t0_back_right");
83 }
84
85 return true;
86 }
87
89 // estimate_height(const TH1 &hist) //
91
93 // smooth histogram
94 if (!m_vbh.Smooth(hist->GetBinWidth(1))) {
95 m_error = true;
96 return -1;
97 }
98 if (!m_vbh.Smooth(hist->GetBinWidth(1) / 2)) {
99 m_error = true;
100 return -1;
101 }
102 // draw debug graphs
103 if (m_draw_debug_graph) {
104 m_vbh.DenistyGraph()->Write("t0_density");
105 m_vbh.BinWidthGraph()->Write("t0_binwidth");
106 m_vbh.BinContentGraph()->Write("t0_bin_content");
107 m_vbh.DiffDensityGraph()->Write("t0_diff_density");
108 m_vbh.DiffDensityGraph()->Write("t0_diffbinwidth");
109 }
110 // get the range in which the smallest, and second smallest bins are
111 double lower = m_vbh.GetSortedBin(0).Width();
112 double left(9e9), right(-9e9);
113 // bool smallest_found(false);
114 for (unsigned int i = 0; i < m_vbh.GetNumberOfBins(); i++) {
115 if (m_vbh.GetSortedBin(i).Left() < left) left = m_vbh.GetSortedBin(i).Left();
116 if (m_vbh.GetSortedBin(i).Right() > right) right = m_vbh.GetSortedBin(i).Right();
117 // NOTE: For greater numerical stability m_vbh.GetSortedBin(i).Width() is considered greater only if it is greater by
118 // hist->GetBinWidth(1) / 100.0. I have seen differences in Binwidth in the order of 1e-13. On the other hand, due to the
119 // binning of the input histogram, e real difference in the binwidth of the VBH n*hist->GetBinWidth(1).
120 if ((m_vbh.GetSortedBin(i).Width() - lower) > hist->GetBinWidth(1) / 100.0) {
121 // reqire minimum distance between bins, and minimum number if significant bins
122 if (right - left > 50 && i > 20) break;
123 lower = m_vbh.GetSortedBin(i).Width();
124 }
125 }
126 // mark selected range
127 if (m_draw_debug_graph) {
128 (new TLine(left, 0, left, hist->GetMaximum()))->Write("t0_height_left");
129 (new TLine(right, 0, right, hist->GetMaximum()))->Write("t0_height_right");
130 }
131 // calcularte mean bin content of input histogram in range
132 int bleft(hist->FindBin(left)), bright(hist->FindBin(right));
133 double nbins = 0.0;
134 m_height = 0.0;
135 for (int i = bleft; i <= bright; i++) {
136 m_height += hist->GetBinContent(i);
137 nbins++;
138 }
139 if (nbins < 1) {
140 MsgStream log(Athena::getMessageSvc(), "MTT0PattternRecognition");
141 log << MSG::WARNING << "estimate_height() - top region is too small! left=" << left << " right=" << right << endmsg;
142 m_error = true;
143 return -1;
144 }
145 m_height /= nbins;
146 m_fit_max = right;
147 return left;
148 }
149
150} // namespace MuonCalib
#define endmsg
#define min(a, b)
Definition cfImp.cxx:40
#define max(a, b)
Definition cfImp.cxx:41
double estimate_height(TH1F *hist)
estimates the height of the spectrum.
VariableBinwidthHistogram m_vbh
Variable binwidth histogram.
bool estimate_background(TH1F *hist, double scale_min)
estimates the background level
const T0MTSettingsT0 * m_settings
settings
double chi2(TH1 *h0, TH1 *h1)
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.