11#include "GaudiKernel/MsgStream.h"
14#include "TDirectory.h"
18#include "boost/thread/tss.hpp"
25 TRandom3* random =
rnd.get();
27 random =
new TRandom3();
36 const Double_t &t(
x[0]);
40 return (back +
A / (1 + std::exp(-(t - t_0) / T)));
50 return (back + (std::exp(
a + b * (t - t_0))) / (1 + std::exp((t - t_max) / T)));
65 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"T0MTHistos::Initialize: called" <<
endmsg;
69 snprintf(buf, 100,
"t_spec_%d",
id);
71 snprintf(buf, 100,
"t_spec_%s", hname);
73 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"directory=" << gDirectory->GetName() <<
endmsg;
79 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"T0MTHistos::Initialize: debug directory created" <<
endmsg;
81 TDirectory *
cwd = gDirectory;
82 snprintf(buf, 100,
"t0_tmax_dir_%d",
id);
83 m_dir = gDirectory->mkdir(buf, buf);
97 TDirectory *
cwd = gDirectory;
99 m_time = std::make_unique<TH1F>(*spec);
105 snprintf(buffer, 100,
"t0_tmax_dir_%d",
id);
106 m_dir = gDirectory->mkdir(buffer, buffer);
115 if (
m_time->GetEntries() < 1000) {
120 if (
m_time->GetEntries() < 10000) {
133 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
m_time->GetName() <<
" " <<
m_chi2 <<
endmsg;
143 TDirectory *
cwd = gDirectory;
147 log << MSG::WARNING <<
"T0MTHistos::FitTmax: Class is not initialized!" <<
endmsg;
155 log << MSG::WARNING <<
"T0MTHistos::FitTmax for tube " <<
m_id <<
": No valid t0-value!" <<
endmsg;
165 log << MSG::WARNING <<
"T0MTHistos::FitTmax for tube " <<
m_id <<
": Pattern recognition failed!" <<
endmsg;
172 snprintf(buffer, 100,
"mt_tmax_fermi");
191 if (
m_dir !=
nullptr) {
196 std::string fitopt(
"LR");
197 if (
m_settings->VerboseLevel() == 0) fitopt +=
"Q";
215 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"T0MTHistos::FitT0(): called" <<
endmsg;
217 TDirectory *
cwd = gDirectory;
224 log << MSG::WARNING <<
"T0MTHistos::FitT0: Class is not initialized!" <<
endmsg;
238 log << MSG::WARNING <<
"T0MTHistos::FitT0 for tube " <<
m_id <<
": Pattern recognition failed!" <<
endmsg;
247 snprintf(buffer, 100,
"mt_t0_fermi");
262 if (
m_dir !=
nullptr) {
264 ln->Write(
"t0_range_min");
266 ln->Write(
"t0_range_max");
271 std::string fitopt(
"BLR");
272 if (
m_settings->VerboseLevel() == 0) fitopt +=
"Q";
282 m_chi2 =
m_time->GetFunction(
"mt_t0_fermi")->GetChisquare() /
m_time->GetFunction(
"mt_t0_fermi")->GetNDF();
295 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"Scrambling for " <<
m_time->GetName() <<
endmsg;
297 std::string fitopt(
"BLR");
298 if (
m_settings->VerboseLevel() == 0) fitopt +=
"Q";
305 char scramhistname[100];
306 snprintf(scramhistname, 100,
"%s_scram",
m_time->GetName());
307 std::unique_ptr<TH1F> scramhist = std::make_unique<TH1F>(scramhistname,
"scrambled histogram",
m_time->GetSize() - 2,
308 m_time->GetXaxis()->GetXmin(),
m_time->GetXaxis()->GetXmax());
310 for (
int binnr = 0; binnr <
m_time->GetSize(); binnr++) {
312 scramhist->SetBinError(binnr,
m_time->GetBinError(binnr) * 1.41421356);
313 if (scramhist->GetBinContent(binnr) < 0) scramhist->SetBinContent(binnr, 0);
315 TDirectory *
cwd = gDirectory;
323 log << MSG::WARNING <<
"T0MTHistos::FitT0 for tube " <<
m_id <<
": Scrambed pattern recognition failed!" <<
endmsg;
327 char scrambuffer[100];
328 snprintf(scrambuffer, 100,
"scrammt_t0_fermi");
329 std::unique_ptr<TF1> scramm_t0_fermi = std::make_unique<TF1>();
331 scramm_t0_fermi->SetName(scrambuffer);
348 m_time->GetListOfFunctions()->Clear();
356 m_time->GetListOfFunctions()->Clear();
361 m_chi2 =
m_time->GetFunction(
"mt_t0_fermi")->GetChisquare() /
m_time->GetFunction(
"mt_t0_fermi")->GetNDF();
370 TF1 *t0_fermi =
m_time->GetFunction(
"mt_t0_fermi");
372 t0_fermi->GetRange(
min,
max);
375 for (
int bin = startbin;
bin < endbin;
bin++) {
376 float measval =
m_time->GetBinContent(
bin);
377 float funcval = t0_fermi->Eval(
m_time->GetBinCenter(
bin));
380 if (measval < 10 && funcval < 10 &&
384 m_chi2 += (measval - funcval) * (measval - funcval);
386 m_chi2 += (measval - funcval) * (measval - funcval) / (errval * errval);
395 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"Slicing for " <<
m_time->GetName() <<
endmsg;
398 std::list<Slice> slice_chi2;
402 TF1 *t0_fermi =
m_time->GetFunction(
"mt_t0_fermi");
405 t0_fermi->GetRange(
min,
max);
407 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE << current.min_bin <<
" " <<
max <<
endmsg;
410 bin < m_time->FindBin(
max) - 1;
bin++) {
411 if (current.n_bins == 10) {
413 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE << current.chi_2 / current.n_bins <<
endmsg;
415 current.max_bin =
bin;
416 slice_chi2.push_back(current);
418 current.min_bin =
bin;
421 double measval =
m_time->GetBinContent(
bin);
422 double funcval = t0_fermi->Eval(
m_time->GetBinCenter(
bin));
423 double errval =
m_time->GetBinError(
bin);
425 current.chi_2 += std::pow(measval - funcval, 2.0);
427 current.chi_2 += std::pow(measval - funcval, 2.0) / std::pow(errval, 2);
431 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"number of slices: " << slice_chi2.size() <<
endmsg;
433 std::list<Slice>::iterator it = slice_chi2.end();
436 if (it == slice_chi2.begin()) {
438 if (log.level() <= MSG::VERBOSE) log << MSG::VERBOSE <<
"No gain in slicing!" <<
endmsg;
442 }
while (it->chi_2 /
static_cast<double>(it->n_bins) > 3);
444 m_time->GetListOfFunctions()->Clear();
445 std::string fitopt(
"BLR");
446 if (
m_settings->VerboseLevel() == 0) fitopt +=
"Q";
452 m_time->Fit(
"mt_t0_fermi", fitopt.c_str(),
"",
min,
max);
456 m_chi2 =
m_time->GetFunction(
"mt_t0_fermi")->GetChisquare() /
m_time->GetFunction(
"mt_t0_fermi")->GetNDF();
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
double GetEstimatedT0() const
get estimated t0
bool Initialize(TH1F *hist, const T0MTSettings *settings)
Initialize class - returns true if pattern recognition was successfull.
double GetBackground() const
get the background level
double GetFitRangeMax() const
get fit range
double GetFitRangeMin() const
get fit range
double GetHeight() const
get height
double GetB() const
get parameter a in exp-function representing the end of the spectrum
double GetEstimatedTMax() const
get estimated t0
bool Initialize(TH1F *hist, double t0, const T0MTSettings *settings)
Initialize class.
double GetFitRangeMin() const
get fit range
double GetBackground() const
get the background level
double GetFitRangeMax() const
get fit range
double GetA() const
get parameter a in exp-function representing the end of the spectrum
static constexpr int T0_PAR_NR_T0
parameter numbers in t0 fit
static constexpr int N_T0_FIT_PAR
number of parameters in t0 fit
static constexpr int TMAX_PAR_NR_TMAX
parameters numbers for tmax fit
static constexpr int TMAX_PAR_NR_T0
bool m_t0_ok
is true if t0 fit was successful
static constexpr int T0_PAR_NR_T
double m_tmax_ok
is true if tmax fit was successful
static constexpr int TMAX_PAR_NR_T
void Initialize(int id, const T0MTSettings *settings, const char *hname=nullptr)
Initialize class.
std::unique_ptr< TF1 > m_t0_fermi
function fitted to the riding edghe of the spectrum
static constexpr int TMAX_PAR_NR_B
int m_status_code
status code for t0 fit (0 ok, 1 not fitted, 2 low statistics, 3 failed)
bool T0Scramble()
try to get better start values from a scrambled histogram
static constexpr int TMAX_PAR_NR_BACK
bool FitT0()
Perform t0-fit Returns true if fit is successfull.
bool FitTmax()
Performs tmax-fit Returns true if fit is successfull.
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.
static constexpr int T0_PAR_NR_A
static constexpr int N_TMAX_FIT_PAR
number of parameters for tmax fit
double m_chi2
chi2/NDF value
void TopChi2()
top chi2 calculation
bool NormalFit()
normal t0 fit
TDirectory * m_dir
TDirectory where debug and result histograms are stored.
std::unique_ptr< TH1F > m_time
time spectrum
std::unique_ptr< TF1 > m_tmax_fermi
function fitted to the falling edge of the spectrum
const T0MTSettings * m_settings
Pointer to settings class.
static constexpr int TMAX_PAR_NR_A
void TopSlicing()
top slicing metyhod
void FillT(double t)
fill drift time spectrum
static constexpr int T0_PAR_NR_BACK
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 int & NBinsTime() const
Number of bins for time histogram and range.
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.
TRandom3 * getTLSRandomGen()
Double_t mt_t0_fermi(Double_t *x, Double_t *par)
The fermi function to be fitted at the rising edge of the spectrum.
Double_t mt_tmax_fermi(Double_t *x, Double_t *par)
The fermi function to be fitted at the trailing slope of the spectrum.
hold the test vectors and ease the comparison