ATLAS Offline Software
RtCalibrationIntegration.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 <algorithm>
8 #include <fstream>
9 #include <iostream>
10 
12 #include "GaudiKernel/MsgStream.h"
15 #include "MdtCalibT0/T0MTHistos.h"
16 #include "TF1.h"
17 #include "TList.h"
18 
19 using namespace MuonCalib;
20 
21 inline Double_t slope_function_C(Double_t *x, Double_t *par) {
22  Double_t &t(x[0]);
23  Double_t &a(par[0]), &b(par[1]), &t_0(par[2]), &back(par[3]);
24  return back + std::exp(a + b * (t - t_0));
25 }
26 
27 inline void update_parameter_on_mttmax(TH1 *h, TF1 *f, const float &b, const float &T, const T0MTSettingsTMax &tmax_settings) {
28  Double_t rmin, rmax;
29  f->GetRange(rmin, rmax);
30  std::unique_ptr<TF1> slope_function =
31  std::make_unique<TF1>("slope_function", slope_function_C, rmin, rmin + tmax_settings.WidthAB(), 4);
32  slope_function->SetParameter(0, f->GetParameter(T0MTHistos::TMAX_PAR_NR_A));
33  slope_function->FixParameter(1, b);
34  slope_function->FixParameter(2, f->GetParameter(T0MTHistos::TMAX_PAR_NR_T0));
35  slope_function->FixParameter(3, f->GetParameter(T0MTHistos::TMAX_PAR_NR_BACK));
36  h->Fit("slope_function", "R+", "");
37  f->FixParameter(T0MTHistos::TMAX_PAR_NR_A, slope_function->GetParameter(0));
38  f->FixParameter(T0MTHistos::TMAX_PAR_NR_B, b);
39  f->FixParameter(T0MTHistos::TMAX_PAR_NR_T, T);
40 }
41 void RtCalibrationIntegration::init(bool close_hits, double r_max, double lower_extrapolation_radius, double upper_extrapolation_radius,
42  bool add_tmax_difference) {
43  m_close_hits = close_hits;
44  m_rt = nullptr;
45  m_r_max = r_max;
46  m_lower_extrapolation_radius = lower_extrapolation_radius;
47  m_upper_extrapolation_radius = upper_extrapolation_radius;
48  m_output = nullptr;
49  m_nb_hits_used = 0;
51  m_add_tmax_difference = add_tmax_difference;
52  return;
53 }
55 
57  for (const auto & k : seg) { handleSegment(*k); }
58  m_nb_segments_used = seg.size();
59  analyse();
60 
61  return getResults();
62 }
65  // LOOP OVER THE HITS OF THE SEGMENTS AND STORE THEM //
67 
68  // start with hits on the segment //
69  for (unsigned int k = 0; k < seg.mdtHitsOnTrack(); k++) {
70  if (seg.mdtHOT()[k]->driftTime() < -8e8) continue;
71  m_t_drift.push_back(std::pair<double, bool>(seg.mdtHOT()[k]->driftTime(), seg.mdtHOT()[k]->identify().mdtMultilayer() == 2));
72  }
73 
74  // continue with close hits if requested //
75  if (m_close_hits == true) {
76  for (unsigned int k = 0; k < seg.mdtCloseHits(); k++) {
77  m_t_drift.push_back(std::pair<double, bool>(seg.mdtHOT()[k]->driftTime(), seg.mdtHOT()[k]->identify().mdtMultilayer() == 2));
78  }
79  }
80 
81  return true;
82 }
83 
84 void RtCalibrationIntegration::setInput(const IMdtCalibrationOutput * /*rt_input*/) { return; }
87  // VARIABLES //
89 
90  T0MTSettings t0_setting; // settings of the MT t0 fitter
91  t0_setting.AddFitfun() = true;
92  // t0_setting.DrawDebugGraphs()=true;
93  t0_setting.T0Settings()->ScrambleThreshold() = 2;
94  t0_setting.T0Settings()->SlicingThreshold() = 3;
95  t0_setting.TMaxSettings()->DistAB() += 50;
96  T0MTHistos drift_time_spec; // drift time spectrum used in the t0 and
97  // the tmax fit
98  std::array<T0MTHistos, 2> drift_time_spec_ml;
99 
100  double t0, tmax; // t0 and tmax
101  int k_min(-1); //, k_max(-1); // first and last drift-time entry to be
102  // used in the integration procedure
103  unsigned int nb_bins(100); // number of integration bins
104  double bin_content; // number of entries per bin
105  std::vector<SamplePoint> point(nb_bins + 1); // (t, r) points
106  double radius(0.0); // r(t)
107  double scf = 0.; // scale factor (r_max/number of used hits)
108  RtFromPoints rt_from_points; // r-t converter
109 
111  // STOP HERE, IF THERE ARE NOT ENOUGH ENTRIES IN THE DRIFT-TIME SPECTRUM //
113 
114  if (m_t_drift.size() < 2000) {
115  MsgStream log(Athena::getMessageSvc(), "RtCalibrationIntegration");
116  log << MSG::WARNING << "analyse() - Less than 2000 drift-time entries! No r-t relationship will be determined!" << endmsg;
117  return false;
118  }
119 
121  // INTEGRATION METHOD //
123 
124  // sort the hits in ascending order of their drift times //
125  sort(m_t_drift.begin(), m_t_drift.end());
126 
127  // perform a t0 and tmax fit //
128  // fill the histogram with the drift-time spectrum //
129  int n_bins = static_cast<int>(32 * (200.0 + m_t_drift[m_t_drift.size() - 1].first - m_t_drift[0].first) / 25.0);
130  float min_t = m_t_drift[0].first - 100.0;
131  float max_t = m_t_drift[m_t_drift.size() - 1].first + 100.0;
132  std::unique_ptr<TH1F> tspec = std::make_unique<TH1F>("tspec", "DRIFT-TIME SPECTRUM", n_bins, min_t, max_t);
133  std::array<std::unique_ptr<TH1F>, 2> tspec_ml;
134  tspec_ml[0] = std::make_unique<TH1F>("tspec_ml0", "DRIFT-TIME SPECTRUM ML 0", n_bins, min_t, max_t);
135  tspec_ml[1] = std::make_unique<TH1F>("tspec_ml1", "DRIFT-TIME SPECTRUM ML 1", n_bins, min_t, max_t);
136 
137  for (auto & k : m_t_drift) {
138  tspec->Fill(k.first, 1.0);
139  tspec_ml[static_cast<unsigned int>(k.second)]->Fill(k.first, 1.0);
140  }
141  drift_time_spec.SetTSpec(1, tspec.get(), &t0_setting, false);
142  drift_time_spec_ml[0].SetTSpec(2, tspec_ml[0].get(), &t0_setting, false);
143  drift_time_spec_ml[1].SetTSpec(3, tspec_ml[1].get(), &t0_setting, false);
144 
145  // t0 and tmax fits //
146 
147  if (!drift_time_spec.FitT0() || !drift_time_spec.T0Ok()) {
148  MsgStream log(Athena::getMessageSvc(), "RtCalibrationIntegration");
149  log << MSG::WARNING << "analyse() - t0 fit not successful, no r-t relationship will be calculated!" << endmsg;
150  return false;
151  }
152  t0 = drift_time_spec.GetT0Function()->GetParameter(T0MTHistos::T0_PAR_NR_T0);
153 
154  if (!drift_time_spec.FitTmax() || !drift_time_spec.TmaxOk()) {
155  MsgStream log(Athena::getMessageSvc(), "RtCalibrationIntegration");
156  log << MSG::WARNING << "analyse() - tmax fit not successful, no r-t relationship will be calculated!" << endmsg;
157  return false;
158  }
159  tmax = drift_time_spec.GetTMaxFunction()->GetParameter(T0MTHistos::TMAX_PAR_NR_TMAX) +
160  2.0 * drift_time_spec.GetTMaxFunction()->GetParameter(T0MTHistos::TMAX_PAR_NR_T);
161 
162  // determine (t,r) points by integration //
163  m_nb_hits_used = 0;
164  for (unsigned int k = 0; k < m_t_drift.size(); k++) {
165  if (m_t_drift[k].first >= t0 && m_t_drift[k].first <= tmax) { m_nb_hits_used++; }
166  if (k_min < 0 && m_t_drift[k].first >= t0) { k_min = k; }
167  }
168  // k_max = k_min+m_nb_hits_used-1;
169 
170  bin_content = static_cast<double>(m_nb_hits_used) / static_cast<double>(nb_bins);
171 
172  if (m_nb_hits_used > 0) { scf = m_r_max / static_cast<double>(m_nb_hits_used); }
173 
174  point[0].set_x1(t0);
175  point[0].set_x2(0.0);
176  point[0].set_error(0.1); // give a higher weight to the end point in
177 
178  // the final fit
179  for (unsigned int k = 1; k < nb_bins; k++) {
180  radius = radius + scf * bin_content;
181  point[k].set_x1(m_t_drift[k_min + static_cast<int>(bin_content) * (k)].first);
182  point[k].set_x2(radius);
183  point[k].set_error(1.0);
184  }
185 
186  point[nb_bins].set_x1(tmax);
187  point[nb_bins].set_x2(m_r_max);
188  point[nb_bins].set_error(1.);
189 
190  // get the r-t relationship //
191  m_rt = std::make_shared<RtChebyshev>(rt_from_points.getRtChebyshev(point, 15));
192 
194  // PARABOLIC EXTRAPOLATION FOR LARGE DRIFT RADII //
196  std::vector<SamplePoint> add_fit_point; // additional r-t points for r(t_max)
197  add_fit_point.push_back(SamplePoint(tmax, m_r_max, 1.0));
198  RtParabolicExtrapolation rt_extrapolated;
200  m_upper_extrapolation_radius, m_r_max, add_fit_point));
201  std::shared_ptr<IRtRelation> rt_new = std::make_shared<RtRelationLookUp>(tmp_rt);
202 
204  // Get length difference between multilayers //
206 
207  if (tspec_ml[0]->GetEntries() >= 10000 && tspec_ml[0]->GetEntries() > 10000) {
208  bool fit_ok(true);
209  std::array<float, 2> b{}, tmax{}, T{};
210  for (unsigned int i = 0; i < 2; i++) {
211  if (!drift_time_spec_ml[i].FitT0()) {
212  fit_ok = false;
213  break;
214  }
215  if (!drift_time_spec_ml[i].FitTmax()) {
216  fit_ok = false;
217  break;
218  }
219  b[i] = drift_time_spec_ml[i].GetTMaxFunction()->GetParameter(T0MTHistos::TMAX_PAR_NR_B);
220  tmax[i] = drift_time_spec_ml[i].GetTMaxFunction()->GetParameter(T0MTHistos::TMAX_PAR_NR_TMAX) -
221  drift_time_spec_ml[i].GetT0Function()->GetParameter(T0MTHistos::T0_PAR_NR_T0);
222  T[i] = drift_time_spec_ml[i].GetTMaxFunction()->GetParameter(T0MTHistos::TMAX_PAR_NR_T);
223  }
224  if (fit_ok) {
225  int refit = static_cast<int>((b[1] + 1.33717e-03) > (b[0] + 1.33717e-03));
226  int norefit = static_cast<bool>(refit) ? 0 : 1;
227  TF1 *fixfun = drift_time_spec_ml[refit].GetTMaxFunctionNC();
228  fixfun->FixParameter(T0MTHistos::TMAX_PAR_NR_B, b[norefit]);
229  update_parameter_on_mttmax(drift_time_spec_ml[refit].GetTSpec(), fixfun, b[norefit], T[norefit], *t0_setting.TMaxSettings());
230  TList *l = drift_time_spec_ml[refit].GetTSpec()->GetListOfFunctions();
231  l->Remove(l->FindObject("mt_tmax_fermi"));
232  fit_ok = drift_time_spec_ml[refit].FitTmax();
233  tmax[refit] = drift_time_spec_ml[refit].GetTMaxFunction()->GetParameter(T0MTHistos::TMAX_PAR_NR_TMAX) -
234  drift_time_spec_ml[refit].GetT0Function()->GetParameter(T0MTHistos::T0_PAR_NR_T0);
235  }
236 
237  if (fit_ok && m_add_tmax_difference) rt_new->SetTmaxDiff(tmax[0] - tmax[1]);
238  }
239 
241  // STORE THE RESULTS IN THE RtCalibrationOutput //
243 
244  m_output = std::make_unique<RtCalibrationOutput>(
245  rt_new, std::make_shared<RtFullInfo>("RtCalibrationIntegration", 1, m_nb_segments_used, 0.0, 0.0, 0.0, 0.0));
246 
247  return true;
248 }
249 
250 //*****************************************************************************
251 
252 //::::::::::::::::::::::
253 //:: METHOD converged ::
254 //::::::::::::::::::::::
255 
256 bool RtCalibrationIntegration::converged() const { return (m_output != nullptr); }
257 
258 //*****************************************************************************
259 
260 //:::::::::::::::::::::::
261 //:: METHOD getResults ::
262 //:::::::::::::::::::::::
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
MuonCalib::T0MTSettings::TMaxSettings
const T0MTSettingsTMax * TMaxSettings() const
get settings for the tmax-fit
Definition: T0MTSettings.h:81
MuonCalib::IMdtCalibration::MdtCalibOutputPtr
std::shared_ptr< IMdtCalibrationOutput > MdtCalibOutputPtr
Definition: IMdtCalibration.h:30
MuonCalib::RtCalibrationIntegration::m_nb_hits_used
unsigned int m_nb_hits_used
Definition: RtCalibrationIntegration.h:86
T0MTHistos.h
MuonCalib::RtCalibrationIntegration::handleSegment
bool handleSegment(MuonCalibSegment &seg)
analyse the segment "seg"
Definition: RtCalibrationIntegration.cxx:63
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
MuonCalib::T0MTSettingsTMax::WidthAB
double WidthAB() const
Width of the region in which the parameters a and b are estimated.
Definition: T0MTSettingsTMax.h:77
MuonCalib::RtCalibrationIntegration::m_add_tmax_difference
bool m_add_tmax_difference
Definition: RtCalibrationIntegration.h:80
MuonCalib::RtParabolicExtrapolation
Definition: RtParabolicExtrapolation.h:27
RtParabolicExtrapolation.h
MuonCalib::MuonCalibSegment
Definition: MuonCalibSegment.h:39
MuonCalib::T0MTHistos::GetT0Function
const TF1 * GetT0Function() const
returns function fitted to the riding edge of the spectrum
Definition: T0MTHistos.h:104
MuonCalib::RtCalibrationIntegration::m_nb_segments_used
unsigned int m_nb_segments_used
Definition: RtCalibrationIntegration.h:87
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
MuonCalib::T0MTSettings::T0Settings
const T0MTSettingsT0 * T0Settings() const
get settings for the t0-fit
Definition: T0MTSettings.h:78
GetEntries
TGraphErrors * GetEntries(TH2F *histo)
Definition: TRTCalib_makeplots.cxx:4019
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
MuonCalib::IRtRelation::SetTmaxDiff
void SetTmaxDiff(const double d)
set the difference in total drift time betwene the two multilayers (ML1 - ML2)
Definition: IRtRelation.h:39
MuonCalib::RtRelationLookUp
Equidistant look up table for rt-relations with the time as key.
Definition: RtRelationLookUp.h:23
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
x
#define x
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
MuonCalib::RtCalibrationIntegration::m_rt
std::shared_ptr< IRtRelation > m_rt
Definition: RtCalibrationIntegration.h:85
MuonCalib::RtCalibrationIntegration::setInput
void setInput(const IMdtCalibrationOutput *rt_input) override
the method is empty as no initial r-t relationship is required by the algorithm
Definition: RtCalibrationIntegration.cxx:84
MuonCalib::MuonCalibSegment::mdtCloseHits
unsigned int mdtCloseHits() const
retrieve the number of nearby mdt hits.
Definition: MuonCalibSegment.cxx:150
MuonCalib::RtCalibrationIntegration::m_upper_extrapolation_radius
double m_upper_extrapolation_radius
parabolic extrapolation.
Definition: RtCalibrationIntegration.h:76
MuonCalib::RtCalibrationIntegration::analyse
bool analyse()
perform the integration method
Definition: RtCalibrationIntegration.cxx:85
MuonCalib::T0MTHistos::FitTmax
bool FitTmax()
Performs tmax-fit Returns true if fit is successfull.
Definition: T0MTHistos.cxx:142
MuonCalib::T0MTHistos
Definition: T0MTHistos.h:40
MuonCalib::T0MTSettingsT0::SlicingThreshold
double SlicingThreshold() const
the chi2 threshold at which the slicing method is used
Definition: T0MTSettingsT0.h:70
MuonCalib::T0MTSettingsT0::ScrambleThreshold
double ScrambleThreshold() const
the chi2 threshold at which the scrambling method is used
Definition: T0MTSettingsT0.h:63
MuonCalib::T0MTSettingsTMax::DistAB
double DistAB() const
Distance of the a/b region from the detected falling edge.
Definition: T0MTSettingsTMax.h:73
MuonCalib::T0MTSettingsTMax
Definition: T0MTSettingsTMax.h:24
lumiFormat.i
int i
Definition: lumiFormat.py:85
MuonCalib::T0MTHistos::SetTSpec
void SetTSpec(int id, TH1F *spec, const T0MTSettings *settings, bool copy_spec=true)
set the pointer of the drift-time spectrum to an existing spectrum.
Definition: T0MTHistos.cxx:95
slope_function_C
Double_t slope_function_C(Double_t *x, Double_t *par)
Definition: RtCalibrationIntegration.cxx:21
h
MuonCalib::RtCalibrationIntegration::getResults
MdtCalibOutputPtr getResults() const override
returns the final r-t relationship
Definition: RtCalibrationIntegration.cxx:263
RtFromPoints.h
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
MuonCalib::RtCalibrationIntegration::m_output
std::shared_ptr< RtCalibrationOutput > m_output
Definition: RtCalibrationIntegration.h:89
MuonCalib::T0MTHistos::TMAX_PAR_NR_A
static constexpr int TMAX_PAR_NR_A
Definition: T0MTHistos.h:61
MuonCalib
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
Definition: CscCalcPed.cxx:22
MuonCalib::RtFromPoints::getRtChebyshev
static RtChebyshev getRtChebyshev(const std::vector< SamplePoint > &sample_points, const unsigned int &order)
< get an RtChebyshev resembling the r(t) function as described by the sample points in the vector "sa...
Definition: RtFromPoints.cxx:39
MuonCalib::RtCalibrationIntegration::m_lower_extrapolation_radius
double m_lower_extrapolation_radius
sets the lower radius to perform the
Definition: RtCalibrationIntegration.h:75
hist_file_dump.f
f
Definition: hist_file_dump.py:135
MuonCalib::RtCalibrationIntegration::converged
bool converged() const
returns true, if the integration method has been performed
Definition: RtCalibrationIntegration.cxx:256
MuonCalib::T0MTHistos::TMAX_PAR_NR_B
static constexpr int TMAX_PAR_NR_B
Definition: T0MTHistos.h:61
MuonCalib::T0MTHistos::TMAX_PAR_NR_T0
static constexpr int TMAX_PAR_NR_T0
Definition: T0MTHistos.h:62
MuonCalib::T0MTHistos::T0_PAR_NR_T0
static constexpr int T0_PAR_NR_T0
parameter numbers in t0 fit
Definition: T0MTHistos.h:57
MuonCalib::RtCalibrationIntegration::analyseSegments
MdtCalibOutputPtr analyseSegments(const MuonSegVec &seg) override
determine r(t)
Definition: RtCalibrationIntegration.cxx:56
MuonCalib::MuonCalibSegment::mdtHitsOnTrack
unsigned int mdtHitsOnTrack() const
retrieve the number of MdtCalibHitBase s assigned to this segment
Definition: MuonCalibSegment.cxx:146
MuonCalib::RtFromPoints
Definition: RtFromPoints.h:30
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
MuonCalib::SamplePoint
Definition: SamplePoint.h:16
MuonCalib::T0MTHistos::GetTMaxFunction
const TF1 * GetTMaxFunction() const
returns function fitted to the riding edge of the spectrum
Definition: T0MTHistos.h:110
MuonCalib::RtCalibrationIntegration::init
void init(bool close_hits, double r_max, double lower_extrapolation_radius, double higher_extrapolation_radius, bool add_tmax_difference)
Definition: RtCalibrationIntegration.cxx:41
MuonCalib::IMdtCalibrationOutput
Definition: IMdtCalibrationOutput.h:28
ParticleGun_SamplingFraction.radius
radius
Definition: ParticleGun_SamplingFraction.py:96
MuonCalib::T0MTHistos::FitT0
bool FitT0()
Perform t0-fit Returns true if fit is successfull.
Definition: T0MTHistos.cxx:114
MuonCalib::T0MTSettings::AddFitfun
const bool & AddFitfun() const
If set to true the fitted functions are added to the histograms.
Definition: T0MTSettings.h:63
MuonCalib::T0MTHistos::TMAX_PAR_NR_T
static constexpr int TMAX_PAR_NR_T
Definition: T0MTHistos.h:61
a
TList * a
Definition: liststreamerinfos.cxx:10
MuonCalib::RtCalibrationIntegration::m_t_drift
std::vector< std::pair< double, bool > > m_t_drift
Definition: RtCalibrationIntegration.h:83
RtCalibrationIntegration.h
DeMoScan.first
bool first
Definition: DeMoScan.py:536
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
MuonCalib::T0MTHistos::TMAX_PAR_NR_TMAX
static constexpr int TMAX_PAR_NR_TMAX
parameters numbers for tmax fit
Definition: T0MTHistos.h:61
MuonCalib::RtCalibrationIntegration::number_of_hits_used
unsigned int number_of_hits_used() const
get the number of hits used in the r-t determination
Definition: RtCalibrationIntegration.cxx:54
MuonCalib::IMdtCalibration::MuonSegVec
std::vector< std::shared_ptr< MuonCalibSegment > > MuonSegVec
Definition: IMdtCalibration.h:27
MuonCalib::T0MTHistos::TMAX_PAR_NR_BACK
static constexpr int TMAX_PAR_NR_BACK
Definition: T0MTHistos.h:61
MuonCalib::MuonCalibSegment::mdtHOT
const MdtHitVec & mdtHOT() const
retrieve the full set of MdtCalibHitBase s assigned to this segment
Definition: MuonCalibSegment.cxx:147
MuonCalib::RtCalibrationIntegration::m_r_max
double m_r_max
Definition: RtCalibrationIntegration.h:88
update_parameter_on_mttmax
void update_parameter_on_mttmax(TH1 *h, TF1 *f, const float &b, const float &T, const T0MTSettingsTMax &tmax_settings)
Definition: RtCalibrationIntegration.cxx:27
MuonCalib::RtCalibrationIntegration::m_close_hits
bool m_close_hits
Definition: RtCalibrationIntegration.h:73
MuonCalib::T0MTHistos::T0Ok
bool T0Ok() const
returns true if t0-fit was successfull
Definition: T0MTHistos.h:100
MuonCalib::T0MTHistos::TmaxOk
bool TmaxOk() const
returns true if tmax-fir was successfull
Definition: T0MTHistos.h:108
MuonCalib::T0MTSettings
Definition: T0MTSettings.h:28
fitman.k
k
Definition: fitman.py:528
MuonCalib::RtParabolicExtrapolation::getRtWithParabolicExtrapolation
RtRelationLookUp getRtWithParabolicExtrapolation(const IRtRelation &in_rt, const double r_min=13.0, const double r_max=14.0) const
get an r-t relationship which is equivalent to the input relationship in_rt for r<r_min and has r(t) ...
Definition: RtParabolicExtrapolation.cxx:24