Belle II Software development
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 header.
10#include <top/modules/TOPReconstruction/TOPReconstructorModule.h>
11
12// TOP headers.
13#include <top/reconstruction_cpp/TOPRecoManager.h>
14#include <top/geometry/TOPGeometryPar.h>
15
16// framework aux
17#include <framework/core/Environment.h>
18#include <framework/logging/Logger.h>
19#include <set>
20#include <map>
21
22using namespace std;
23
24namespace Belle2 {
29 using namespace TOP;
30
31 //-----------------------------------------------------------------
33 //-----------------------------------------------------------------
34
35 REG_MODULE(TOPReconstructor);
36
37 //-----------------------------------------------------------------
38 // Implementation
39 //-----------------------------------------------------------------
40
42 {
43 // Set description
44 setDescription("Reconstruction for TOP counter. Uses reconstructed tracks "
45 "extrapolated to TOP and TOPDigits to calculate log likelihoods "
46 "for charged stable particles");
47
48 // Set property flags
50
51 // Add parameters
52 addParam("minTime", m_minTime,
53 "lower limit for photon time [ns] (if minTime >= maxTime use the default from DB)", 0.0);
54 addParam("maxTime", m_maxTime,
55 "upper limit for photon time [ns] (if minTime >= maxTime use the default from DB)", 0.0);
56 addParam("PDGCode", m_PDGCode,
57 "PDG code of hypothesis to construct pulls (0 means: use MC truth, -1: switched off)", -1);
58 addParam("deltaRayModeling", m_deltaRayModeling,
59 "include (True) or exclude (False) delta-ray modeling in log likelihood calculation", false);
60 addParam("pTCut", m_pTCut,
61 "pT cut to suppress badly extrapolated tracks that cannot reach TOP counter", 0.27);
62 addParam("TOPDigitCollectionName", m_topDigitCollectionName,
63 "Name of the collection of TOPDigits", string(""));
64 addParam("TOPLikelihoodCollectionName", m_topLikelihoodCollectionName,
65 "Name of the produced collection of TOPLikelihoods", string(""));
66 addParam("TOPPullCollectionName", m_topPullCollectionName, "Name of the collection of produced TOPPulls", string(""));
67 }
68
69
71 {
72 // input
73
75 m_tracks.isRequired();
76 m_extHits.isRequired();
77 m_barHits.isOptional();
78 m_recBunch.isOptional();
79
80 // output
81
83 m_likelihoods.registerRelationTo(m_extHits);
84 m_likelihoods.registerRelationTo(m_barHits);
85 m_tracks.registerRelationTo(m_likelihoods);
86
89 }
90
91
93 {
94 // clear output collections
95
96 m_likelihoods.clear();
97 m_topPulls.clear();
98
99 // check bunch reconstruction status and do the reconstruction:
100 // - if object exists and bunch is found (collision data w/ bunch finder in the path)
101 // - if object doesn't exist (cosmic data and other cases w/o bunch finder in the path)
102
103 if (m_recBunch.isValid()) {
104 if (not m_recBunch->isReconstructed()) return;
105 }
106
107 // set time window in which photon hits are accepted for likelihood determination
108
110
111 // is MC ?
112
113 bool isMC = Environment::Instance().isMC();
114
115 // sort tracks by module ID
116
117 std::unordered_multimap<int, const TOPTrack*> topTracks; // tracks sorted by top modules
118 for (const auto& track : m_tracks) {
119 auto* trk = new TOPTrack(track, m_topDigitCollectionName);
120 if (trk->isValid() and trk->getTransverseMomentum() > m_pTCut) {
121 if (not isMC) trk->setTOFCorrection(m_tofCorrections); // TOF corrections only for data
122 topTracks.emplace(trk->getModuleID(), trk);
123 } else {
124 delete trk;
125 }
126 }
127
128 // reconstruct module-by-module
129
130 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
131 for (unsigned moduleID = 1; moduleID <= geo->getNumModules(); moduleID++) {
132
133 // make a vector of PDF collections for a given TOP module
134
135 std::vector<PDFCollection> pdfCollections;
136 const auto& range = topTracks.equal_range(moduleID);
137 for (auto it = range.first; it != range.second; ++it) {
138 const auto* trk = it->second;
139 PDFCollection collection(*trk, m_deltaRayModeling);
140 if (collection.isValid) pdfCollections.push_back(collection);
141 }
142 if (pdfCollections.empty()) continue;
143
144 // add most probable PDF's of other tracks if present
145
146 if (pdfCollections.size() > 1) {
147 std::vector<int> lastMostProbables;
148 for (int iter = 0; iter < 10; iter++) {
149 std::vector<int> mostProbables;
150 for (auto& collection : pdfCollections) { // set most probable PDF
151 collection.setMostProbable();
152 mostProbables.push_back(collection.mostProbable->getHypothesis().getPDGCode());
153 }
154 if (mostProbables == lastMostProbables) break;
155 else lastMostProbables = mostProbables;
156
157 for (auto& collection : pdfCollections) collection.clearPDFOther(); // reset
158 for (auto& collection : pdfCollections) { // append
159 for (const auto& other : pdfCollections) {
160 if (&other == &collection) continue;
161 collection.appendPDFOther(other.mostProbable);
162 }
163 }
164 } // loop: iter
165 }
166
167 // determine and save log likelihoods
168
169 for (const auto& collection : pdfCollections) {
170 auto* topLL = m_likelihoods.appendNew();
171 const auto* trk = collection.topTrack;
172 const auto* track = trk->getTrack();
173 track->addRelationTo(topLL);
174 topLL->addRelationTo(trk->getExtHit());
175 topLL->addRelationTo(trk->getBarHit());
176
177 int pdgCode = (m_PDGCode != 0) ? m_PDGCode : trk->getPDGCode(); // PDG code used for providing pulls
178 std::set<int> nfotSet; // to x-check if the number of photons is the same for all particle hypotheses
179 std::set<double> nbkgSet; // to x-check if the background is the same for all particle hypotheses
180
181 for (const auto* pdfConstructor : collection.PDFs) {
182 const auto& chargedStable = pdfConstructor->getHypothesis();
183 auto LL = pdfConstructor->getLogL();
184 auto expBkgPhotons = pdfConstructor->getExpectedBkgPhotons();
185 topLL->set(chargedStable, LL.numPhotons, LL.logL, LL.expPhotons, expBkgPhotons, LL.effectiveSignalYield);
186
187 nfotSet.insert(LL.numPhotons);
188 nbkgSet.insert(expBkgPhotons);
189
190 if (abs(chargedStable.getPDGCode()) == abs(pdgCode)) {
191 for (const auto& p : pdfConstructor->getPulls()) {
192 const auto* pull = m_topPulls.appendNew(p.pixelID, p.time, p.peakT0 + p.ttsT0, p.sigma, p.phiCer, p.wt);
193 track->addRelationTo(pull);
194 }
195 }
196 }
197 topLL->setFlag(1);
198 topLL->setModuleID(trk->getModuleID());
199 const auto& emi = trk->getEmissionPoint();
200 topLL->setXZ(emi.position.X(), emi.position.Z());
201
202 if (nfotSet.size() > 1) B2ERROR("Bug in TOP::PDFConstructor: number of photons differs between particle hypotheses");
203 if (nbkgSet.size() > 1) B2ERROR("Bug in TOP::PDFConstructor: estimated background differs between particle hypotheses");
204 }
205
206 for (auto& collection : pdfCollections) collection.deletePDFs();
207
208 } // loop: moduleID
209
210 for (const auto& x : topTracks) delete x.second;
211
212 }
213
215} // end Belle2 namespace
216
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition DataStore.h:71
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
bool isMC() const
Do we have generated, not real data?
static Environment & Instance()
Static method to get a reference to the Environment instance.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
bool m_deltaRayModeling
include or exclude delta-ray modeling in log likelihood calculation
StoreObjPtr< TOPRecBunch > m_recBunch
reconstructed bunch
StoreArray< TOPLikelihood > m_likelihoods
collection of likelihoods
std::string m_topDigitCollectionName
name of the collection of TOPDigits
double m_maxTime
optional upper time limit for photons
double m_minTime
optional lower time limit for photons
StoreArray< Track > m_tracks
collection of tracks
StoreArray< TOPPull > m_topPulls
collection of pulls
double m_pTCut
pT cut to suppress badly extrapolated tracks that cannot reach TOP counter
DBObjPtr< TOPCalTOFCorrection > m_tofCorrections
time-of-flight corrections
std::string m_topLikelihoodCollectionName
name of the collection of created TOPLikelihoods
StoreArray< TOPDigit > m_digits
collection of digits
StoreArray< ExtHit > m_extHits
collection of extrapolated hits
StoreArray< TOPBarHit > m_barHits
collection of MCParticle hits at TOP
std::string m_topPullCollectionName
name of the collection of created TOPPulls
int m_PDGCode
PDG code of hypothesis to construct pulls.
const TOPGeometry * getGeometry() const
Returns pointer to geometry object using basf2 units.
static TOPGeometryPar * Instance()
Static method to obtain the pointer to its instance.
static void setTimeWindow(double minTime, double maxTime)
Sets time window.
Reconstructed track at TOP.
Definition TOPTrack.h:40
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
Abstract base class for different kinds of events.
STL namespace.
A collection of PDF's of charged stable particles for a given track.