Belle II Software  release-08-01-10
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/logging/Logger.h>
18 #include <set>
19 #include <map>
20 
21 using namespace std;
22 
23 namespace Belle2 {
28  using namespace TOP;
29 
30  //-----------------------------------------------------------------
32  //-----------------------------------------------------------------
33 
34  REG_MODULE(TOPReconstructor);
35 
36  //-----------------------------------------------------------------
37  // Implementation
38  //-----------------------------------------------------------------
39 
40  TOPReconstructorModule::TOPReconstructorModule() : Module()
41  {
42  // Set description
43  setDescription("Reconstruction for TOP counter. Uses reconstructed tracks "
44  "extrapolated to TOP and TOPDigits to calculate log likelihoods "
45  "for charged stable particles");
46 
47  // Set property flags
49 
50  // Add parameters
51  addParam("minTime", m_minTime,
52  "lower limit for photon time [ns] (if minTime >= maxTime use the default from DB)", 0.0);
53  addParam("maxTime", m_maxTime,
54  "upper limit for photon time [ns] (if minTime >= maxTime use the default from DB)", 0.0);
55  addParam("PDGCode", m_PDGCode,
56  "PDG code of hypothesis to construct pulls (0 means: use MC truth)", 211);
57  addParam("deltaRayModeling", m_deltaRayModeling,
58  "include (True) or exclude (False) delta-ray modeling in log likelihood calculation", false);
59  addParam("pTCut", m_pTCut,
60  "pT cut to suppress badly extrapolated tracks that cannot reach TOP counter", 0.27);
61  addParam("TOPDigitCollectionName", m_topDigitCollectionName,
62  "Name of the collection of TOPDigits", string(""));
63  addParam("TOPLikelihoodCollectionName", m_topLikelihoodCollectionName,
64  "Name of the produced collection of TOPLikelihoods", string(""));
65  addParam("TOPPullCollectionName", m_topPullCollectionName, "Name of the collection of produced TOPPulls", string(""));
66  }
67 
68 
70  {
71  // input
72 
74  m_tracks.isRequired();
75  m_extHits.isRequired();
76  m_barHits.isOptional();
77  m_recBunch.isOptional();
78 
79  // output
80 
81  m_likelihoods.registerInDataStore(m_topLikelihoodCollectionName);
82  m_likelihoods.registerRelationTo(m_extHits);
83  m_likelihoods.registerRelationTo(m_barHits);
84  m_tracks.registerRelationTo(m_likelihoods);
85 
88  }
89 
90 
92  {
93  // clear output collections
94 
95  m_likelihoods.clear();
96  m_topPulls.clear();
97 
98  // check bunch reconstruction status and do the reconstruction:
99  // - if object exists and bunch is found (collision data w/ bunch finder in the path)
100  // - if object doesn't exist (cosmic data and other cases w/o bunch finder in the path)
101 
102  if (m_recBunch.isValid()) {
103  if (not m_recBunch->isReconstructed()) return;
104  }
105 
106  // set time window in which photon hits are accepted for likelihood determination
107 
109 
110  // sort tracks by module ID
111 
112  std::unordered_multimap<int, const TOPTrack*> topTracks; // tracks sorted by top modules
113  for (const auto& track : m_tracks) {
114  auto* trk = new TOPTrack(track, m_topDigitCollectionName);
115  if (trk->isValid() and trk->getTransverseMomentum() > m_pTCut) {
116  topTracks.emplace(trk->getModuleID(), trk);
117  } else {
118  delete trk;
119  }
120  }
121 
122  // reconstruct module-by-module
123 
124  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
125  for (unsigned moduleID = 1; moduleID <= geo->getNumModules(); moduleID++) {
126 
127  // make a vector of PDF collections for a given TOP module
128 
129  std::vector<PDFCollection> pdfCollections;
130  const auto& range = topTracks.equal_range(moduleID);
131  for (auto it = range.first; it != range.second; ++it) {
132  const auto* trk = it->second;
133  PDFCollection collection(*trk, m_deltaRayModeling);
134  if (collection.isValid) pdfCollections.push_back(collection);
135  }
136  if (pdfCollections.empty()) continue;
137 
138  // add most probable PDF's of other tracks if present
139 
140  if (pdfCollections.size() > 1) {
141  std::vector<int> lastMostProbables;
142  for (int iter = 0; iter < 10; iter++) {
143  std::vector<int> mostProbables;
144  for (auto& collection : pdfCollections) { // set most probable PDF
145  collection.setMostProbable();
146  mostProbables.push_back(collection.mostProbable->getHypothesis().getPDGCode());
147  }
148  if (mostProbables == lastMostProbables) break;
149  else lastMostProbables = mostProbables;
150 
151  for (auto& collection : pdfCollections) collection.clearPDFOther(); // reset
152  for (auto& collection : pdfCollections) { // append
153  for (auto& other : pdfCollections) {
154  if (&other == &collection) continue;
155  collection.appendPDFOther(other.mostProbable);
156  }
157  }
158  } // loop: iter
159  }
160 
161  // determine and save log likelihoods
162 
163  for (auto& collection : pdfCollections) {
164  auto* topLL = m_likelihoods.appendNew();
165  const auto* trk = collection.topTrack;
166  const auto* track = trk->getTrack();
167  track->addRelationTo(topLL);
168  topLL->addRelationTo(trk->getExtHit());
169  topLL->addRelationTo(trk->getBarHit());
170 
171  int pdgCode = (m_PDGCode != 0) ? m_PDGCode : trk->getPDGCode(); // PDG code used for providing pulls
172  std::set<int> nfotSet; // to x-check if the number of photons is the same for all particle hypotheses
173  std::set<double> nbkgSet; // to x-check if the background is the same for all particle hypotheses
174 
175  for (const auto* pdfConstructor : collection.PDFs) {
176  const auto& chargedStable = pdfConstructor->getHypothesis();
177  auto LL = pdfConstructor->getLogL();
178  auto expBkgPhotons = pdfConstructor->getExpectedBkgPhotons();
179  topLL->set(chargedStable, LL.numPhotons, LL.logL, LL.expPhotons, expBkgPhotons);
180 
181  nfotSet.insert(LL.numPhotons);
182  nbkgSet.insert(expBkgPhotons);
183 
184  if (abs(chargedStable.getPDGCode()) == abs(pdgCode)) {
185  for (const auto& p : pdfConstructor->getPulls()) {
186  auto* pull = m_topPulls.appendNew(p.pixelID, p.time, p.peakT0 + p.ttsT0, p.sigma, p.phiCer, p.wt);
187  track->addRelationTo(pull);
188  }
189  }
190  }
191  topLL->setFlag(1);
192 
193  if (nfotSet.size() > 1) B2ERROR("Bug in TOP::PDFConstructor: number of photons differs between particle hypotheses");
194  if (nbkgSet.size() > 1) B2ERROR("Bug in TOP::PDFConstructor: estimated background differs between particle hypotheses");
195  }
196 
197  for (auto& collection : pdfCollections) collection.deletePDFs();
198 
199  } // loop: moduleID
200 
201  for (auto& x : topTracks) delete x.second;
202 
203  }
204 
206 } // end Belle2 namespace
207 
@ 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
Base class for Modules.
Definition: Module.h:72
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
@ 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
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:39
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:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
Abstract base class for different kinds of events.
A collection of PDF's of charged stable particles for a given track.