ATLAS Offline Software
Loading...
Searching...
No Matches
MTTmaxPatternRecognition.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
7#include <cmath>
8
10#include "GaudiKernel/MsgStream.h"
11#include "TF1.h"
12#include "TGraph.h"
13#include "TH1.h"
14#include "TLine.h"
15
16namespace MuonCalib {
17
18 inline Double_t myexp(Double_t* x, Double_t* p) { return std::exp(p[0] + p[1] * (x[0] - p[3])) + p[2]; }
19
20 bool MTTmaxPatternRecognition::Initialize(TH1F* hist, double t0, const T0MTSettings* settings) {
21 m_settings = settings->TMaxSettings();
23 m_error = false;
24 m_t0 = t0;
25 m_error = !m_vbh.Initialize(hist, m_settings->VBHBinContent(), m_settings->MaxBinwidth(), t0 + m_settings->VBHLow());
26 if (m_error || m_vbh.GetNumberOfBins() < 2) {
27 MsgStream log(Athena::getMessageSvc(), "MTTmaxPatternRecognition");
28 log << MSG::WARNING << "Initialize() - Initialization of VBH failed!" << endmsg;
29 }
30 if (!m_error) estimate_background(hist);
31 if (!m_error) estimate_height(hist);
32 return !m_error;
33 }
34
36 // estimate_background( TH1 *hist) //
38
40 // smooth VBH in several steps
41 for (double i = 1.0; i <= 4.0; i *= 2.0) {
42 for (int j = 0; j < 2; j++)
43 if (!m_vbh.Smooth(hist->GetBinWidth(1) / i)) {
44 m_error = true;
45 return false;
46 }
47 }
49 m_vbh.DenistyGraph()->Write("tmax_density");
50 m_vbh.BinWidthGraph()->Write("tmax_binwidth");
51 m_vbh.DiffDensityGraph()->Write("tmax_diffdensity");
52 m_vbh.DiffBinwidthGraph()->Write("tmax_diffbinwidth");
53 }
54 // find falling edge - search for steepenst slope in a range of 600 to 800 ns after the t0
55 double peak_falling(0.0), peak_falling_slope(-9e9);
56 for (unsigned int i = 0; i < m_vbh.GetNumberOfBins() - 1; i++) {
57 const VariableBinwidthHistogramBin &bin1(m_vbh.GetBin(i)), &bin2(m_vbh.GetBin(i + 1));
58 // select region
59 if (bin1.Center() < m_t0 + m_settings->EndMin() || bin1.Center() > m_t0 + m_settings->EndMax()) continue;
60 if (bin2.Width() - bin1.Width() > peak_falling_slope && bin2.Right() < hist->GetBinLowEdge(hist->GetNbinsX()) - 10) {
61 peak_falling = bin1.Right();
62 peak_falling_slope = bin2.Width() - bin1.Width();
63 }
64 }
65 // check region size
66 int firstbin = hist->FindBin(peak_falling + m_settings->DistBackground());
67 int lastbin = hist->FindBin(m_vbh.GetBin(m_vbh.GetNumberOfBins() - 1).Right());
68 if (lastbin - firstbin < m_settings->MinBackgroundBins()) lastbin = hist->GetNbinsX();
69 if (lastbin - firstbin < m_settings->MinBackgroundBins()) {
70 MsgStream log(Athena::getMessageSvc(), "MTTmaxPatternRecognition");
71 log << MSG::WARNING << "estimate_background() - Falling edge is to glose to upper histogram range!" << endmsg;
72 m_error = true;
73 return false;
74 }
75 // calcul;ate mean
76 m_background = 0.0;
77 double n_bins = 0.0;
78 if (lastbin > hist->GetNbinsX()) lastbin = hist->GetNbinsX();
79 for (int i = firstbin; i <= lastbin; i++) {
80 m_background += hist->GetBinContent(i);
81 n_bins++;
82 }
83 // mark selected range
85 (new TLine(hist->GetBinCenter(firstbin), 0, hist->GetBinCenter(firstbin), hist->GetMaximum()))->Write("tmax_back_left");
86 (new TLine(hist->GetBinCenter(lastbin), 0, hist->GetBinCenter(lastbin), hist->GetMaximum()))->Write("tmax_back_right");
87 }
88
89 m_background /= n_bins;
90 m_tmax_est = peak_falling;
91 m_fit_max = hist->GetBinCenter(lastbin);
92 return true;
93 }
94
96 // estimate_height( TH1 *hist, double background_min) //
98
100 // get start value for fit
101 Double_t left = m_tmax_est - m_settings->DistAB() - m_settings->WidthAB(), right = m_tmax_est - m_settings->DistAB();
102 // fit
103 TF1* fun = new TF1("myexp", myexp, left, right, 4);
104 fun->FixParameter(2, m_background);
105 fun->FixParameter(3, m_t0);
106 hist->Fit("myexp", "Q0+", "", left, right);
107 // store parameters
108 m_a = fun->GetParameter(0);
109 m_b = fun->GetParameter(1);
110 m_fit_min = m_tmax_est - m_settings->DistAB() - m_settings->WidthAB();
111 // mark selected range
112 if (m_draw_debug_graph) {
113 (new TLine(left, 0, left, hist->GetMaximum()))->Write("tmax_height_left");
114 (new TLine(right, 0, right, hist->GetMaximum()))->Write("tmax_height_right");
115 }
116 return true;
117 }
118
119} // namespace MuonCalib
#define endmsg
static Double_t t0
#define x
VariableBinwidthHistogram m_vbh
Variable binwidth histogram.
const T0MTSettingsTMax * m_settings
settings
bool estimate_background(TH1F *hist)
estimates the background level
bool Initialize(TH1F *hist, double t0, const T0MTSettings *settings)
Initialize class.
double m_a
parameters a, b in a*exp(b*x)
bool estimate_height(TH1F *hist)
estimates the height of the spectrum.
Settings for the T0 calibration (histogram booking and fitting parameters) Parameters for pattern rec...
const bool & DrawDebugGraphs() const
If set to true for every tube a TDirectory will be created.
const T0MTSettingsTMax * TMaxSettings() const
get settings for the tmax-fit
A bin of a VariableBinwidthHistogram.
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.
Double_t myexp(Double_t *x, Double_t *p)