Belle II Software  release-06-02-00
TOPReconstructorModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 // Own include
10 #include <top/modules/TOPReconstruction/TOPReconstructorModule.h>
11 #include <top/reconstruction_cpp/TOPRecoManager.h>
12 #include <top/reconstruction_cpp/TOPTrack.h>
13 #include <top/reconstruction_cpp/PDFConstructor.h>
14 
15 // framework aux
16 #include <framework/gearbox/Const.h>
17 #include <framework/logging/Logger.h>
18 #include <set>
19 
20 using namespace std;
21 
22 namespace Belle2 {
27  using namespace TOP;
28 
29  //-----------------------------------------------------------------
30  // Register the Module
31  //-----------------------------------------------------------------
32 
33  REG_MODULE(TOPReconstructor)
34 
35  //-----------------------------------------------------------------
36  // Implementation
37  //-----------------------------------------------------------------
38 
40  {
41  // Set description
42  setDescription("Reconstruction for TOP counter. Uses reconstructed tracks "
43  "extrapolated to TOP and TOPDigits to calculate log likelihoods "
44  "for charged stable particles");
45 
46  // Set property flags
47  setPropertyFlags(c_ParallelProcessingCertified);
48 
49  // Add parameters
50  addParam("minTime", m_minTime,
51  "lower limit for photon time [ns] (if minTime >= maxTime use the default from DB)", 0.0);
52  addParam("maxTime", m_maxTime,
53  "upper limit for photon time [ns] (if minTime >= maxTime use the default from DB)", 0.0);
54  addParam("PDGCode", m_PDGCode,
55  "PDG code of hypothesis to construct pulls (0 means: use MC truth)", 211);
56  addParam("deltaRayModeling", m_deltaRayModeling,
57  "include (True) or exclude (False) delta-ray modeling in log likelihood calculation", false);
58  addParam("pTCut", m_pTCut,
59  "pT cut to suppress badly extrapolated tracks that cannot reach TOP counter", 0.27);
60  addParam("TOPDigitCollectionName", m_topDigitCollectionName,
61  "Name of the collection of TOPDigits", string(""));
62  addParam("TOPLikelihoodCollectionName", m_topLikelihoodCollectionName,
63  "Name of the produced collection of TOPLikelihoods", string(""));
64  addParam("TOPPullCollectionName", m_topPullCollectionName, "Name of the collection of produced TOPPulls", string(""));
65  }
66 
67 
68  void TOPReconstructorModule::initialize()
69  {
70  // input
71 
72  m_digits.isRequired(m_topDigitCollectionName);
73  m_tracks.isRequired();
74  m_extHits.isRequired();
75  m_barHits.isOptional();
76  m_recBunch.isOptional();
77 
78  // output
79 
80  m_likelihoods.registerInDataStore(m_topLikelihoodCollectionName);
81  m_likelihoods.registerRelationTo(m_extHits);
82  m_likelihoods.registerRelationTo(m_barHits);
83  m_tracks.registerRelationTo(m_likelihoods);
84 
85  m_topPulls.registerInDataStore(m_topPullCollectionName, DataStore::c_DontWriteOut);
86  m_tracks.registerRelationTo(m_topPulls, DataStore::c_Event, DataStore::c_DontWriteOut);
87  }
88 
89 
90  void TOPReconstructorModule::event()
91  {
92  // clear output collections
93 
94  m_likelihoods.clear();
95  m_topPulls.clear();
96 
97  // check bunch reconstruction status and do the reconstruction:
98  // - if object exists and bunch is found (collision data w/ bunch finder in the path)
99  // - if object doesn't exist (cosmic data and other cases w/o bunch finder in the path)
100 
101  if (m_recBunch.isValid()) {
102  if (not m_recBunch->isReconstructed()) return;
103  }
104 
105  // set time window in which photon hits are accepted for likelihood determination
106 
107  TOPRecoManager::setTimeWindow(m_minTime, m_maxTime);
108 
109  // reconstruct track-by-track and store the results
110 
111  for (const auto& track : m_tracks) {
112 
113  const TOPTrack trk(track, m_topDigitCollectionName);
114  if (not trk.isValid()) continue; // track missed the bars
115  if (trk.getTransverseMomentum() < m_pTCut) continue; // badly extrapolated
116 
117  auto* topLL = m_likelihoods.appendNew();
118  track.addRelationTo(topLL);
119  topLL->addRelationTo(trk.getExtHit());
120  topLL->addRelationTo(trk.getBarHit());
121 
122  int pdgCode = (m_PDGCode != 0) ? m_PDGCode : trk.getPDGCode(); // PDG code used for providing pulls
123  std::set<int> nfotSet; // to x-check if the number of photons differs between particle hypotheses
124  std::set<double> nbkgSet; // to x-check if the background differs between particle hypotheses
125  int flag = 1;
126  for (const auto& chargedStable : Const::chargedStableSet) {
127  const PDFConstructor pdfConstructor(trk, chargedStable);
128  pdfConstructor.switchDeltaRayPDF(m_deltaRayModeling);
129  if (not pdfConstructor.isValid()) {
130  flag = -1;
131  continue;
132  }
133  auto LL = pdfConstructor.getLogL();
134  topLL->set(chargedStable, LL.numPhotons, LL.logL, LL.expPhotons, pdfConstructor.getExpectedBkgPhotons());
135 
136  nfotSet.insert(LL.numPhotons);
137  nbkgSet.insert(pdfConstructor.getExpectedBkgPhotons());
138 
139  if (abs(chargedStable.getPDGCode()) == abs(pdgCode)) {
140  for (const auto& p : pdfConstructor.getPulls()) {
141  auto* pull = m_topPulls.appendNew(p.pixelID, p.time, p.peakT0 + p.ttsT0, p.sigma, p.phiCer, p.wt);
142  track.addRelationTo(pull);
143  }
144  }
145  }
146  topLL->setFlag(flag);
147 
148  if (nfotSet.size() > 1) B2ERROR("Bug in TOP::PDFConstructor: number of photons differs between particle hypotheses");
149  if (nbkgSet.size() > 1) B2ERROR("Bug in TOP::PDFConstructor: estimated background differs between particle hypotheses");
150  }
151 
152  }
153 
155 } // end Belle2 namespace
156 
Base class for Modules.
Definition: Module.h:72
TOP reconstruction module.
PDF construction and log likelihood determination for a given track and particle hypothesis.
LogL getLogL() const
Returns extended log likelihood (using the default time window)
bool isValid() const
Checks the object status.
const std::vector< Pull > & getPulls() const
Returns photon pulls w.r.t PDF peaks.
double getExpectedBkgPhotons() const
Returns the expected number of background photons within the default time window.
void switchDeltaRayPDF(bool deltaPDFOn) const
Include or exclude delta-ray PDF in log likelihood calculation.
Reconstructed track at TOP.
Definition: TOPTrack.h:40
int getPDGCode() const
Returns PDG code of associated MCParticle (returns 0 if none)
Definition: TOPTrack.h:230
bool isValid() const
Checks if track is successfully constructed.
Definition: TOPTrack.h:139
const ExtHit * getExtHit() const
Returns extrapolated hit (track entrance to the bar)
Definition: TOPTrack.h:218
const TOPBarHit * getBarHit() const
Returns bar hit of MC particle assigned to this track (if any)
Definition: TOPTrack.h:240
double getTransverseMomentum() const
Returns transverse momentum (at POCA)
Definition: TOPTrack.h:157
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.