Belle II Software  release-05-01-25
TOP1Dpdf.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <top/reconstruction/TOP1Dpdf.h>
12 #include <algorithm>
13 #include <math.h>
14 
15 namespace Belle2 {
20  namespace TOP {
21 
22  TOP1Dpdf::TOP1Dpdf(TOPreco& reco, const StoreArray<TOPDigit>& digits,
23  int moduleID, double binSize):
24  m_moduleID(moduleID)
25  {
26 
27  if (binSize <= 0) B2FATAL("TOP1Dpdf: bin size must be positive");
28 
29  // time window and binning
30  reco.getTimeWindow(m_minTime, m_maxTime);
31  m_numBins = (m_maxTime - m_minTime) / binSize + 1;
32  m_binSize = (m_maxTime - m_minTime) / m_numBins;
33 
34  // save photon times and determine background using other slots
35  if (digits.getEntries() != 0) m_tminFot = m_tmaxFot = digits[0]->getTime();
36  for (const auto& digit : digits) {
37  if (digit.getHitQuality() == TOPDigit::c_Good) {
38  double t = digit.getTime();
39  m_tminFot = std::min(m_tminFot, t);
40  m_tmaxFot = std::max(m_tmaxFot, t);
41  if (digit.getModuleID() == m_moduleID) {
42  m_times.push_back(t);
43  } else {
44  m_expectedBG += 1;
45  }
46  }
47  }
48  m_expectedBG /= 15; // per module
49  int nbins = (m_tmaxFot - m_tminFot) / m_binSize;
50  m_bkg = std::max(m_expectedBG, 1.0) / std::max(nbins, m_numBins);
51 
52  // temporary histogram to make 1D projection of PDF
53  TH1F pdfHisto("pdf_temporary", "", m_numBins, m_minTime, m_maxTime);
54 
55  // first add background
56  for (int i = 0; i < m_numBins; i++) pdfHisto.SetBinContent(i + 1, m_bkg);
57 
58  // then fill signal PDF
59  bool start = true;
60  for (int pix = 1; pix <= 512; pix++) {
61  for (int k = 0; k < reco.getNumofPDFPeaks(pix); k++) {
62  float pos = 0;
63  float wid = 0;
64  float nph = 0;
65  reco.getPDFPeak(pix, k, pos, wid, nph);
66  m_expectedSignal += nph;
67  double time = pos;
68  pdfHisto.Fill(time, nph);
69  if (start) {
70  start = false;
71  m_tminPDF = time;
72  m_tmaxPDF = time;
73  }
74  m_tminPDF = std::min(m_tminPDF, time);
75  m_tmaxPDF = std::max(m_tmaxPDF, time);
76  }
77  }
78 
79  // finally make look-up table for log(PDF)
80  for (int i = 0; i < m_numBins; i++) {
81  m_logF.push_back(log(pdfHisto.GetBinContent(i + 1)));
82  }
83  m_logBkg = log(m_bkg);
84 
85  // determine T0 search region
86  m_minT0 = m_tminFot - m_tmaxPDF - 2 * m_binSize;
87  double maxT0 = m_tmaxFot - m_tminPDF;
88  m_numBinsT0 = std::max(int((maxT0 - m_minT0) / m_binSize), 3) + 1;
89  m_maxT0 = m_minT0 + m_binSize * m_numBinsT0;
90 
91  }
92 
93 
94  double TOP1Dpdf::getLogL(double timeShift) const
95  {
96 
97  double logL = 0;
98  for (const auto& time : m_times) {
99  double f = m_logBkg;
100  double t = time - timeShift;
101  if (t >= m_minTime and t < m_maxTime) {
102  unsigned i = (t - m_minTime) / m_binSize;
103  if (i < m_logF.size()) f = m_logF[i];
104  }
105  logL += f;
106  }
107  return logL;
108  }
109 
110 
111  TH1F TOP1Dpdf::getHistogram(std::string name, std::string title) const
112  {
113  TH1F h(name.c_str(), title.c_str(), m_logF.size(), m_minTime, m_maxTime);
114  for (size_t i = 0; i < m_logF.size(); i++) {
115  h.SetBinContent(i + 1, exp(m_logF[i]));
116  }
117  return h;
118  }
119 
120  } // end top namespace
122 } // end Belle2 namespace
Belle2::TOP::TOP1Dpdf::m_times
std::vector< double > m_times
photon times, from a given slot
Definition: TOP1Dpdf.h:154
Belle2::TOP::TOP1Dpdf::m_logBkg
double m_logBkg
log(m_bkg)
Definition: TOP1Dpdf.h:168
Belle2::TOP::TOP1Dpdf::m_minTime
double m_minTime
lower edge of the first bin
Definition: TOP1Dpdf.h:151
Belle2::TOP::TOP1Dpdf::m_maxTime
double m_maxTime
upper edge of the last bin
Definition: TOP1Dpdf.h:152
Belle2::TOP::TOP1Dpdf::m_binSize
double m_binSize
bin size
Definition: TOP1Dpdf.h:166
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOP::TOP1Dpdf::m_logF
std::vector< double > m_logF
log(PDF) values
Definition: TOP1Dpdf.h:167
Belle2::TOP::TOP1Dpdf::getHistogram
TH1F getHistogram(std::string name, std::string title) const
Returns binned one dimensional PDF (projection to time axis)
Definition: TOP1Dpdf.cc:119
Belle2::TOP::TOP1Dpdf::TOP1Dpdf
TOP1Dpdf(TOPreco &reco, const StoreArray< TOPDigit > &digits, int moduleID, double binSize)
Full constructor.
Definition: TOP1Dpdf.cc:30
Belle2::TOP::TOP1Dpdf::getLogL
double getLogL(double timeShift) const
Returns log likelihood.
Definition: TOP1Dpdf.cc:102
Belle2::TOP::TOPtrack::m_moduleID
int m_moduleID
module ID or 0
Definition: TOPtrack.h:285